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 <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
81 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
82 #endif /* !ARRAY_SIZE */
85 static int lfs_find(int argc, char **argv);
86 static int lfs_getstripe(int argc, char **argv);
87 static int lfs_getdirstripe(int argc, char **argv);
88 static int lfs_setdirstripe(int argc, char **argv);
89 static int lfs_rmentry(int argc, char **argv);
90 static int lfs_osts(int argc, char **argv);
91 static int lfs_mdts(int argc, char **argv);
92 static int lfs_df(int argc, char **argv);
93 static int lfs_getname(int argc, char **argv);
94 static int lfs_check(int argc, char **argv);
95 #ifdef HAVE_SYS_QUOTA_H
96 static int lfs_setquota(int argc, char **argv);
97 static int lfs_quota(int argc, char **argv);
98 static int lfs_project(int argc, char **argv);
100 static int lfs_flushctx(int argc, char **argv);
101 static int lfs_poollist(int argc, char **argv);
102 static int lfs_changelog(int argc, char **argv);
103 static int lfs_changelog_clear(int argc, char **argv);
104 static int lfs_fid2path(int argc, char **argv);
105 static int lfs_path2fid(int argc, char **argv);
106 static int lfs_data_version(int argc, char **argv);
107 static int lfs_hsm_state(int argc, char **argv);
108 static int lfs_hsm_set(int argc, char **argv);
109 static int lfs_hsm_clear(int argc, char **argv);
110 static int lfs_hsm_action(int argc, char **argv);
111 static int lfs_hsm_archive(int argc, char **argv);
112 static int lfs_hsm_restore(int argc, char **argv);
113 static int lfs_hsm_release(int argc, char **argv);
114 static int lfs_hsm_remove(int argc, char **argv);
115 static int lfs_hsm_cancel(int argc, char **argv);
116 static int lfs_swap_layouts(int argc, char **argv);
117 static int lfs_mv(int argc, char **argv);
118 static int lfs_ladvise(int argc, char **argv);
119 static int lfs_getsom(int argc, char **argv);
120 static int lfs_heat_get(int argc, char **argv);
121 static int lfs_heat_set(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_read(int argc, char **argv);
128 static inline int lfs_mirror_write(int argc, char **argv);
129 static inline int lfs_mirror_copy(int argc, char **argv);
130 static int lfs_pcc_attach(int argc, char **argv);
131 static int lfs_pcc_attach_fid(int argc, char **argv);
132 static int lfs_pcc_detach(int argc, char **argv);
133 static int lfs_pcc_detach_fid(int argc, char **argv);
134 static int lfs_pcc_state(int argc, char **argv);
135 static int lfs_pcc(int argc, char **argv);
136 static int lfs_pcc_list_commands(int argc, char **argv);
138 enum setstripe_origin {
146 static int lfs_setstripe_internal(int argc, char **argv,
147 enum setstripe_origin opc);
149 static inline int lfs_setstripe(int argc, char **argv)
151 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
153 static inline int lfs_setstripe_migrate(int argc, char **argv)
155 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
157 static inline int lfs_mirror_create(int argc, char **argv)
159 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
161 static inline int lfs_mirror_extend(int argc, char **argv)
163 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
165 static inline int lfs_mirror_split(int argc, char **argv)
167 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
170 /* Setstripe and migrate share mostly the same parameters */
171 #define SSM_CMD_COMMON(cmd) \
172 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
173 " [--stripe-count|-c <stripe_count>]\n" \
174 " [--overstripe-count|-C <stripe_count>]\n" \
175 " [--stripe-index|-i <start_ost_idx>]\n" \
176 " [--stripe-size|-S <stripe_size>]\n" \
177 " [--layout|-L <pattern>]\n" \
178 " [--pool|-p <pool_name>]\n" \
179 " [--ost|-o <ost_indices>]\n" \
180 " [--yaml|-y <yaml_template_file>]\n" \
181 " [--copy=<lustre_src>]\n"
183 #define SSM_HELP_COMMON \
184 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
185 "\t Using -C instead of -c allows overstriping, which\n" \
186 "\t will place more than one stripe per OST if\n" \
187 "\t stripe_count is greater than the number of OSTs\n" \
188 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
189 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
190 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
191 "\t respectively)\n" \
192 "\tpool_name: Name of OST pool to use (default none)\n" \
193 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
194 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
195 "\t Indices be specified in a format of:\n" \
196 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
198 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
199 "\t If --pool is set with --ost, then the OSTs\n" \
200 "\t must be the members of the pool.\n" \
201 "\tcomp_end: Extent end of component, start after previous end.\n"\
202 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
203 "\t respectively, -1 for EOF). Must be a multiple of\n"\
204 "\t stripe_size.\n" \
205 "\tyaml_template_file:\n" \
206 "\t YAML layout template file, can't be used with -c,\n" \
207 "\t -i, -S, -p, -o, or -E arguments.\n" \
208 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
209 "\t another lustre file or directory, can't used with\n" \
210 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
212 #define MIRROR_CREATE_HELP \
213 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
214 "\t setstripe layout options\n" \
215 "\t It defaults to 1 if not specified; if specified,\n" \
216 "\t it must follow the option without a space.\n" \
217 "\t The option can also be repeated multiple times to\n" \
218 "\t separate mirrors that have different layouts.\n" \
219 "\tsetstripe options: Mirror layout\n" \
220 "\t It can be a plain layout or a composite layout.\n" \
221 "\t If not specified, the stripe options inherited\n" \
222 "\t from the previous component will be used.\n" \
223 "\tflags: set flags to the component of the current mirror.\n" \
224 "\t Only \"prefer\" flag is supported so far.\n"
226 #define MIRROR_EXTEND_HELP \
228 "\tvictim_file: The layout of victim_file will be split and used\n" \
229 "\t as a mirror added to the mirrored file.\n" \
230 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
231 "\t from victim file(s) in case the victim file(s)\n" \
232 "\t contains the same data as the original mirrored\n" \
235 #define MIRROR_EXTEND_USAGE \
236 " <--mirror-count|-N[mirror_count]>\n" \
237 " [setstripe options|-f <victim_file>]\n" \
240 #define SETSTRIPE_USAGE \
241 SSM_CMD_COMMON("setstripe") \
242 MIRROR_EXTEND_USAGE \
243 " <directory|filename>\n" \
247 #define MIGRATE_USAGE \
248 SSM_CMD_COMMON("migrate ") \
249 " [--block|-b] [--non-block|-n]\n" \
250 " [--non-direct|-D] [--verbose|-v]\n" \
254 "\tblock: Block file access during data migration (default)\n" \
255 "\tnon-block: Abort migrations if concurrent access is detected\n" \
256 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
257 "\tverbose: Print each filename as it is migrated\n" \
259 #define SETDIRSTRIPE_USAGE \
260 " [--mdt-count|-c stripe_count>\n" \
261 " [--mdt-hash|-H mdt_hash]\n" \
262 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
263 " [--default|-D] [--mode|-o mode] <dir>\n" \
264 "\tstripe_count: stripe count of the striped directory\n" \
265 "\tmdt_index: MDT index of first stripe\n" \
266 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
267 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
268 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
269 " space create subdirectories with balanced space usage\n" \
270 "\tdefault_stripe: set default dirstripe of the directory\n" \
271 "\tmode: the file access permission of the directory (octal)\n" \
272 "To create dir with a foreign (free format) layout :\n" \
273 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
274 "[--mode|-m mode] [--flags <hex>] <dir>\n" \
275 "\tmode: the mode of the directory\n" \
276 "\tforeign_type: none or daos\n"
279 * command_t mirror_cmdlist - lfs mirror commands.
281 command_t mirror_cmdlist[] = {
282 { .pc_name = "create", .pc_func = lfs_mirror_create,
283 .pc_help = "Create a mirrored file.\n"
284 "usage: lfs mirror create "
285 "<--mirror-count|-N[mirror_count]> "
286 "[setstripe options] ... <filename|directory>\n"
287 MIRROR_CREATE_HELP },
288 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
289 .pc_help = "Extend a mirrored file.\n"
290 "usage: lfs mirror extend "
291 "<--mirror-count|-N[mirror_count]> [--no-verify] "
292 "[setstripe options|-f <victim_file>] ... <filename>\n"
293 MIRROR_EXTEND_HELP },
294 { .pc_name = "split", .pc_func = lfs_mirror_split,
295 .pc_help = "Split a mirrored file.\n"
296 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
297 "\t <--component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
298 "\t [-f <new_file>] <mirrored file>\n"
299 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
300 "\t can be fetched by lfs getstripe command.\n"
301 "\tcomp_id: Unique component ID within a mirror.\n"
302 "\tpool: Components on specified pool.\n"
303 "\tnew_file: This option indicates the layout of the split\n"
304 "\t mirror will be stored into. If not specified,\n"
305 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
306 "\t will be used.\n" },
307 { .pc_name = "read", .pc_func = lfs_mirror_read,
308 .pc_help = "Read the content of a specified mirror of a file.\n"
309 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
310 "[--outfile|-o <output_file>] <mirrored_file>\n" },
311 { .pc_name = "write", .pc_func = lfs_mirror_write,
312 .pc_help = "Write to a specified mirror of a file.\n"
313 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
314 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
315 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
316 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
317 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
318 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
319 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
320 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
321 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
322 "<mirrored file> [<mirrored file2>...]\n"},
323 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
324 .pc_help = "Verify mirrored file(s).\n"
325 "usage: lfs mirror verify "
326 "[--only <mirror_id,mirror_id2[,...]>] "
327 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
328 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
329 .pc_help = "list commands supported by lfs mirror"},
330 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
331 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
332 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
337 * command_t pcc_cmdlist - lfs pcc commands.
339 command_t pcc_cmdlist[] = {
340 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
341 .pc_help = "Attach given files to the Persistent Client Cache.\n"
342 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
343 "\t-i: archive id for RW-PCC\n" },
344 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
345 .pc_help = "Attach given files into PCC by FID(s).\n"
346 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
348 "\t-i: archive id for RW-PCC\n"
349 "\t-m: Lustre mount point\n" },
350 { .pc_name = "state", .pc_func = lfs_pcc_state,
351 .pc_help = "Display the PCC state for given files.\n"
352 "usage: lfs pcc state <file> ...\n" },
353 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
354 .pc_help = "Detach given files from the Persistent Client Cache.\n"
355 "usage: lfs pcc detach <file> ...\n" },
356 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
357 .pc_help = "Detach given files from PCC by FID(s).\n"
358 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
359 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
360 .pc_help = "list commands supported by lfs pcc"},
361 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
362 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
363 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
367 /* all available commands */
368 command_t cmdlist[] = {
369 {"setstripe", lfs_setstripe, 0,
370 "To create a file with specified striping/composite layout, or\n"
371 "create/replace the default layout on an existing directory:\n"
372 SSM_CMD_COMMON("setstripe")
374 " <directory|filename>\n"
376 "To add component(s) to an existing composite file:\n"
377 SSM_CMD_COMMON("setstripe --component-add")
379 "To totally delete the default striping from an existing directory:\n"
380 "usage: setstripe -d <directory>\n"
382 "To create a mirrored file or set s default mirror layout on a directory:\n"
383 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
385 "To delete the last component(s) from an existing composite file\n"
386 "(note that this will also delete any data in those components):\n"
387 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
388 " [--component-flags|-F <comp_flags>]\n"
390 "\tcomp_id: Unique component ID to delete\n"
391 "\tcomp_flags: 'init' indicating all instantiated components\n"
392 "\t '^init' indicating all uninstantiated components\n"
393 "\t-I and -F cannot be specified at the same time\n"
394 "To create a file with a foreign (free format) layout:\n"
395 "usage: setstripe --foreign[=<foreign_type>]\n"
396 " --xattr|-x <layout_string> [--flags <hex>]\n"
397 " [--mode <mode>] <filename>\n"},
398 {"getstripe", lfs_getstripe, 0,
399 "To list the layout pattern for a given file or files in a\n"
400 "directory or recursively for all files in a directory tree.\n"
401 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
402 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
403 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
404 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
405 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
406 " [--component-id[=comp_id]|-I[comp_id]]\n"
407 " [--component-flags[=comp_flags]]\n"
408 " [--component-count]\n"
409 " [--extension-size|--ext-size|-z]\n"
410 " [--component-start[=[+-]comp_start]]\n"
411 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
412 " [[!] --mirror-index=[+-]<index> |\n"
413 " [!] --mirror-id=[+-]<id>]\n"
414 " <directory|filename> ..."},
415 {"setdirstripe", lfs_setdirstripe, 0,
416 "To create a striped directory on a specified MDT. This can only\n"
417 "be done on MDT0 with the right of administrator.\n"
418 "usage: setdirstripe [OPTION] <directory>\n"
420 {"getdirstripe", lfs_getdirstripe, 0,
421 "To list the layout pattern info for a given directory\n"
422 "or recursively for all directories in a directory tree.\n"
423 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
424 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
425 " [--recursive|-r] [--yaml|-y]\n"
426 " [--verbose|-v] [--default|-D] <dir> ..."},
427 {"mkdir", lfs_setdirstripe, 0,
428 "To create a striped directory on a specified MDT. This can only\n"
429 "be done on MDT0 with the right of administrator.\n"
430 "usage: mkdir [OPTION] <directory>\n"
432 {"rm_entry", lfs_rmentry, 0,
433 "To remove the name entry of the remote directory. Note: This\n"
434 "command will only delete the name entry, i.e. the remote directory\n"
435 "will become inaccessable after this command. This can only be done\n"
436 "by the administrator\n"
437 "usage: rm_entry <dir>\n"},
438 {"pool_list", lfs_poollist, 0,
439 "List pools or pool OSTs\n"
440 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
441 {"find", lfs_find, 0,
442 "find files matching given attributes recursively in directory tree.\n"
443 "usage: find <directory|filename> ...\n"
444 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
445 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
446 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
447 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
448 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
449 " [[!] --stripe-count|-c [+-]<stripes>]\n"
450 " [[!] --stripe-index|-i <index,...>]\n"
451 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
452 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
453 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
454 " [[!] --projid <projid>]\n"
455 " [[!] --foreign[=<foreign_type>]]\n"
456 " [[!] --layout|-L released,raid0,mdt]\n"
457 " [[!] --foreign[=<foreign_type>]]\n"
458 " [[!] --component-count [+-]<comp_cnt>]\n"
459 " [[!] --component-start [+-]N[kMGTPE]]\n"
460 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
461 " [[!] --component-flags <comp_flags>]\n"
462 " [[!] --mirror-count|-N [+-]<n>]\n"
463 " [[!] --mirror-state <[^]state>]\n"
464 " [[!] --mdt-count|-T [+-]<stripes>]\n"
465 " [[!] --mdt-hash|-H <hashtype>\n"
466 " [[!] --mdt-index|-m <uuid|index,...>]\n"
467 "\t !: used before an option indicates 'NOT' requested attribute\n"
468 "\t -: used before a value indicates less than requested value\n"
469 "\t +: used before a value indicates more than requested value\n"
470 "\thashtype: hash type of the striped directory.\n"
471 "\t fnv_1a_64 FNV-1a hash algorithm\n"
472 "\t all_char sum of characters % MDT_COUNT\n"},
473 {"check", lfs_check, 0,
474 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
475 "or all the servers (MGTs, MDTs and OSTs).\n"
476 "usage: check <mgts|osts|mdts|all>"},
477 {"osts", lfs_osts, 0, "list OSTs connected to client "
478 "[for specified path only]\n" "usage: osts [path]"},
479 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
480 "[for specified path only]\n" "usage: mdts [path]"},
482 "report filesystem disk space usage or inodes usage "
483 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
484 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
485 {"getname", lfs_getname, 0,
486 "list instances and specified mount points [for specified path only]\n"
487 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
488 #ifdef HAVE_SYS_QUOTA_H
489 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
490 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
491 " -b <block-softlimit> -B <block-hardlimit>\n"
492 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
493 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
494 " [--block-softlimit <block-softlimit>]\n"
495 " [--block-hardlimit <block-hardlimit>]\n"
496 " [--inode-softlimit <inode-softlimit>]\n"
497 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
498 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
499 " [--block-grace <block-grace>]\n"
500 " [--inode-grace <inode-grace>] <filesystem>\n"
501 " setquota <-U|-G|-P>\n"
502 " -b <block-softlimit> -B <block-hardlimit>\n"
503 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
504 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
505 " [--block-softlimit <block-softlimit>]\n"
506 " [--block-hardlimit <block-hardlimit>]\n"
507 " [--inode-softlimit <inode-softlimit>]\n"
508 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
509 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
511 " -b can be used instead of --block-softlimit/--block-grace\n"
512 " -B can be used instead of --block-hardlimit\n"
513 " -i can be used instead of --inode-softlimit/--inode-grace\n"
514 " -I can be used instead of --inode-hardlimit\n"
515 " -d can be used instead of --default\n\n"
516 "Note: The total quota space will be split into many qunits and\n"
517 " balanced over all server targets, the minimal qunit size is\n"
518 " 1M bytes for block space and 1K inodes for inode space.\n\n"
519 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
520 " Quota space rebalancing process will stop when this mininum\n"
521 " value is reached. As a result, quota exceeded can be returned\n"
522 " while many targets still have 1MB or 1K inodes of spare\n"
524 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
525 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
527 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
528 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
529 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
530 {"project", lfs_project, 0,
531 "Change or list project attribute for specified file or directory.\n"
532 "usage: project [-d|-r] <file|directory...>\n"
533 " list project ID and flags on file(s) or directories\n"
534 " project [-p id] [-s] [-r] <file|directory...>\n"
535 " set project ID and/or inherit flag for specified file(s) or directories\n"
536 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
537 " check project ID and flags on file(s) or directories, print outliers\n"
538 " project -C [-r] [-k] <file|directory...>\n"
539 " clear the project inherit flag and ID on the file or directory\n"
542 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
543 "usage: flushctx [-k] [mountpoint...]"},
544 {"changelog", lfs_changelog, 0,
545 "Show the metadata changes on an MDT."
546 "\nusage: changelog <mdtname> [startrec [endrec]]"},
547 {"changelog_clear", lfs_changelog_clear, 0,
548 "Indicate that old changelog records up to <endrec> are no longer of "
549 "interest to consumer <id>, allowing the system to free up space.\n"
550 "An <endrec> of 0 means all records.\n"
551 "usage: changelog_clear <mdtname> <id> <endrec>"},
552 {"fid2path", lfs_fid2path, 0,
553 "Resolve the full path(s) for given FID(s). For a specific hardlink "
554 "specify link number <linkno>.\n"
555 /* "For a historical link name, specify changelog record <recno>.\n" */
556 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
557 /* [ --rec <recno> ] */ },
558 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
559 "usage: path2fid [--parents] <path> ..."},
560 {"data_version", lfs_data_version, 0, "Display file data version for "
561 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
562 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
563 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
564 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
565 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
566 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
567 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
569 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
570 "[--archived] [--lost] <file> ..."},
571 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
572 "given files.\n" "usage: hsm_action <file> ..."},
573 {"hsm_archive", lfs_hsm_archive, 0,
574 "Archive file to external storage.\n"
575 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
577 {"hsm_restore", lfs_hsm_restore, 0,
578 "Restore file from external storage.\n"
579 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
580 {"hsm_release", lfs_hsm_release, 0,
581 "Release files from Lustre.\n"
582 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
583 {"hsm_remove", lfs_hsm_remove, 0,
584 "Remove file copy from external storage.\n"
585 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
587 " (FILE [FILE ...] | "
588 "--mntpath MOUNTPATH FID [FID ...])\n"
590 "Note: To remove an archived copy of a file already deleted from a "
592 "--mntpath option and a list of FIDs must be specified"
594 {"hsm_cancel", lfs_hsm_cancel, 0,
595 "Cancel requests related to specified files.\n"
596 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
597 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
598 "usage: swap_layouts <path1> <path2>"},
599 {"migrate", lfs_setstripe_migrate, 0,
600 "migrate a directory between MDTs.\n"
601 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
602 " [--mdt-hash|-H] <hash_type>\n"
603 " [--mdt-index|-m] <start_mdt_index>\n"
606 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
607 " it's the MDT index of first stripe\n"
608 "\tmdt_count: number of MDTs to stripe a directory over\n"
609 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
610 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
611 " all_char sum of characters % MDT_COUNT\n"
613 "migrate file objects from one OST "
614 "layout\nto another (may be not safe with concurent writes).\n"
616 "[--stripe-count|-c] <stripe_count>\n"
617 "[--overstripe-count|-C] <stripe_count>\n"
618 " [--stripe-index|-i] <start_ost_index>\n"
619 " [--stripe-size|-S] <stripe_size>\n"
620 " [--pool|-p] <pool_name>\n"
621 " [--ost|-o] <ost_indices>\n"
623 " [--non-block|-n]\n"
624 " [--non-direct|-D]\n"
625 " <file|directory>\n"
626 "\tstripe_count: number of OSTs to stripe a file over\n"
627 "\t Using -C instead of -c allows overstriping, which\n"
628 "\t will place more than one stripe per OST if\n"
629 "\t stripe_count is greater than the number of OSTs\n"
630 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
631 "\tstripe_size: number of bytes to store before moving to the next OST\n"
632 "\tpool_name: name of the predefined pool of OSTs\n"
633 "\tost_indices: OSTs to stripe over, in order\n"
634 "\tblock: Block file access during data migration (default)\n"
635 "\tnon-block: Abort migrations if concurrent access is detected\n"
636 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
638 "To move directories between MDTs. This command is deprecated, "
639 "use \"migrate\" instead.\n"
640 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
642 {"ladvise", lfs_ladvise, 0,
643 "Provide servers with advice about access patterns for a file.\n"
644 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
645 " [--background|-b] [--unset|-u]\n\n"
646 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
647 " {[--mode|-m [READ,WRITE]}\n"
649 {"mirror", lfs_mirror, mirror_cmdlist,
650 "lfs commands used to manage files with mirrored components:\n"
651 "lfs mirror create - create a mirrored file or directory\n"
652 "lfs mirror extend - add mirror(s) to an existing file\n"
653 "lfs mirror split - split a mirror from an existing mirrored file\n"
654 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
655 "lfs mirror read - read a mirror content of a mirrored file\n"
656 "lfs mirror write - write to a mirror of a mirrored file\n"
657 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
658 "lfs mirror verify - verify mirrored file(s)\n"},
659 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
660 "usage: getsom [-s] [-b] [-f] <path>\n"
661 "\t-s: Only show the size value of the SOM data for a given file\n"
662 "\t-b: Only show the blocks value of the SOM data for a given file\n"
663 "\t-f: Only show the flags value of the SOM data for a given file\n"},
664 {"heat_get", lfs_heat_get, 0,
665 "To get heat of files.\n"
666 "usage: heat_get <file> ...\n"},
667 {"heat_set", lfs_heat_set, 0,
668 "To set heat flags of files.\n"
669 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
670 "\t--clear|-c: Clear file heat for given files\n"
671 "\t--off|-o: Turn off file heat for given files\n"
672 "\t--on|-O: Turn on file heat for given files\n"},
673 {"pcc", lfs_pcc, pcc_cmdlist,
674 "lfs commands used to interact with PCC features:\n"
675 "lfs pcc attach - attach given files to Persistent Client Cache\n"
676 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
677 "lfs pcc state - display the PCC state for given files\n"
678 "lfs pcc detach - detach given files from Persistent Client Cache\n"
679 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
680 {"help", Parser_help, 0, "help"},
681 {"exit", Parser_quit, 0, "quit"},
682 {"quit", Parser_quit, 0, "quit"},
683 {"--version", Parser_version, 0,
684 "output build version of the utility and exit"},
685 {"--list-commands", lfs_list_commands, 0,
686 "list commands supported by the utility and exit"},
691 static int check_hashtype(const char *hashtype)
695 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
696 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
699 if (!strcmp(hashtype, LMV_HASH_NAME_SPACE))
700 return LMV_HASH_TYPE_DEFAULT | LMV_HASH_FLAG_SPACE;
705 static uint32_t check_foreign_type_name(const char *foreign_type_name)
709 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
710 if (lu_foreign_types[i].lft_name == NULL)
712 if (strcmp(foreign_type_name,
713 lu_foreign_types[i].lft_name) == 0)
714 return lu_foreign_types[i].lft_type;
717 return LU_FOREIGN_TYPE_UNKNOWN;
720 static const char *error_loc = "syserror";
723 MIGRATION_NONBLOCK = 0x0001,
724 MIGRATION_MIRROR = 0x0002,
725 MIGRATION_NONDIRECT = 0x0004,
726 MIGRATION_VERBOSE = 0x0008,
729 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
730 struct llapi_layout *layout);
733 migrate_open_files(const char *name, __u64 migration_flags,
734 const struct llapi_stripe_param *param,
735 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
742 char parent[PATH_MAX];
743 char volatile_file[PATH_MAX];
749 if (param == NULL && layout == NULL) {
750 error_loc = "layout information";
754 /* search for file directory pathname */
755 if (strlen(name) > sizeof(parent) - 1) {
756 error_loc = "source file name";
760 strncpy(parent, name, sizeof(parent));
761 ptr = strrchr(parent, '/');
763 if (getcwd(parent, sizeof(parent)) == NULL) {
764 error_loc = "getcwd";
768 if (ptr == parent) /* leading '/' */
773 /* open file, direct io */
774 /* even if the file is only read, WR mode is nedeed to allow
775 * layout swap on fd */
777 if (!(migration_flags & MIGRATION_NONDIRECT))
779 fd = open(name, rflags);
782 error_loc = "cannot open source file";
786 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
788 error_loc = "cannot get MDT index";
793 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
794 mode_t open_mode = S_IRUSR | S_IWUSR;
796 random_value = random();
797 rc = snprintf(volatile_file, sizeof(volatile_file),
798 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
799 mdt_index, random_value);
800 if (rc >= sizeof(volatile_file)) {
805 /* create, open a volatile file, use caching (ie no directio) */
807 fdv = llapi_file_open_param(volatile_file, open_flags,
810 fdv = lfs_component_create(volatile_file, open_flags,
812 } while (fdv < 0 && (rc = fdv) == -EEXIST);
815 error_loc = "cannot create volatile file";
819 /* In case the MDT does not support creation of volatile files
820 * we should try to unlink it. */
821 (void)unlink(volatile_file);
823 /* Not-owner (root?) special case.
824 * Need to set owner/group of volatile file like original.
825 * This will allow to pass related check during layout_swap.
830 error_loc = "cannot stat source file";
834 rc = fstat(fdv, &stv);
837 error_loc = "cannot stat volatile";
841 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
842 rc = fchown(fdv, st.st_uid, st.st_gid);
845 error_loc = "cannot change ownwership of volatile";
864 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
866 struct llapi_layout *layout;
867 size_t buf_size = 4 * 1024 * 1024;
876 layout = llapi_layout_get_by_fd(fd_src, 0);
877 if (layout != NULL) {
878 uint64_t stripe_size;
880 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
882 buf_size = stripe_size;
884 llapi_layout_free(layout);
887 /* Use a page-aligned buffer for direct I/O */
888 rc = posix_memalign(&buf, getpagesize(), buf_size);
893 /* read new data only if we have written all
894 * previously read data */
897 rc = check_file(fd_src);
902 rsize = read(fd_src, buf, buf_size);
914 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
933 static int migrate_copy_timestamps(int fd, int fdv)
937 if (fstat(fd, &st) == 0) {
938 struct timeval tv[2] = {
939 {.tv_sec = st.st_atime},
940 {.tv_sec = st.st_mtime}
943 return futimes(fdv, tv);
949 static int migrate_block(int fd, int fdv)
956 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
958 error_loc = "cannot get dataversion";
966 /* The grouplock blocks all concurrent accesses to the file.
967 * It has to be taken after llapi_get_data_version as it would
969 rc = llapi_group_lock(fd, gid);
971 error_loc = "cannot get group lock";
975 rc = migrate_copy_data(fd, fdv, NULL);
977 error_loc = "data copy failed";
981 /* Make sure we keep original atime/mtime values */
982 rc = migrate_copy_timestamps(fd, fdv);
984 error_loc = "timestamp copy failed";
989 * for a migration we need to check data version on file did
992 * Pass in gid=0 since we already own grouplock. */
993 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
994 SWAP_LAYOUTS_CHECK_DV1);
996 error_loc = "file changed";
999 error_loc = "cannot swap layout";
1004 rc2 = llapi_group_unlock(fd, gid);
1005 if (rc2 < 0 && rc == 0) {
1006 error_loc = "unlock group lock";
1014 * Internal helper for migrate_copy_data(). Check lease and report error if
1017 * \param[in] fd File descriptor on which to check the lease.
1019 * \retval 0 Migration can keep on going.
1020 * \retval -errno Error occurred, abort migration.
1022 static int check_lease(int fd)
1026 rc = llapi_lease_check(fd);
1028 return 0; /* llapi_check_lease returns > 0 on success. */
1033 static int migrate_nonblock(int fd, int fdv)
1039 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1041 error_loc = "cannot get data version";
1045 rc = migrate_copy_data(fd, fdv, check_lease);
1047 error_loc = "data copy failed";
1051 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1053 error_loc = "cannot get data version";
1059 error_loc = "source file changed";
1063 /* Make sure we keep original atime/mtime values */
1064 rc = migrate_copy_timestamps(fd, fdv);
1066 error_loc = "timestamp copy failed";
1073 static int lfs_component_set(char *fname, int comp_id,
1074 __u32 flags, __u32 neg_flags)
1077 __u32 flags_array[2];
1082 ids[count] = comp_id;
1083 flags_array[count] = flags;
1088 if (neg_flags & LCME_FL_STALE) {
1089 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1090 "component. Please use lfs-mirror-resync(1) "
1091 "instead\n", progname);
1095 ids[count] = comp_id;
1096 flags_array[count] = neg_flags | LCME_FL_NEG;
1100 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1103 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1104 progname, comp_id, fname, flags, neg_flags);
1109 static int lfs_component_del(char *fname, __u32 comp_id,
1110 __u32 flags, __u32 neg_flags)
1114 if (flags && neg_flags)
1117 if (!flags && neg_flags)
1118 flags = neg_flags | LCME_FL_NEG;
1120 if ((flags && comp_id) || (!flags && !comp_id))
1124 if (flags & ~LCME_KNOWN_FLAGS) {
1126 "%s setstripe: unknown flags %#x\n",
1130 } else if (comp_id > LCME_ID_MAX) {
1131 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1136 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1139 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1140 progname, comp_id, fname, strerror(errno));
1144 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1151 rc = llapi_layout_file_comp_add(fname, layout);
1153 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1154 fname, strerror(errno));
1158 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1159 struct llapi_layout *layout)
1167 fd = lstat(fname, &st);
1168 if (fd == 0 && S_ISDIR(st.st_mode))
1169 open_flags = O_DIRECTORY | O_RDONLY;
1171 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1173 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1174 S_ISDIR(st.st_mode) ?
1175 "set default composite layout for" :
1176 "create composite file",
1177 fname, strerror(errno));
1181 static int lfs_migrate(char *name, __u64 migration_flags,
1182 struct llapi_stripe_param *param,
1183 struct llapi_layout *layout)
1189 rc = migrate_open_files(name, migration_flags, param, layout,
1194 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1195 /* Blocking mode (forced if servers do not support file lease).
1196 * It is also the default mode, since we cannot distinguish
1197 * between a broken lease and a server that does not support
1198 * atomic swap/close (LU-6785) */
1199 rc = migrate_block(fd, fdv);
1203 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1205 error_loc = "cannot get lease";
1209 rc = migrate_nonblock(fd, fdv);
1211 llapi_lease_release(fd);
1215 /* Atomically put lease, swap layouts and close.
1216 * for a migration we need to check data version on file did
1218 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1220 error_loc = "cannot swap layout";
1232 fprintf(stderr, "error: %s: %s: %s: %s\n",
1233 progname, name, error_loc, strerror(-rc));
1234 else if (migration_flags & MIGRATION_VERBOSE)
1235 printf("%s\n", name);
1240 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1249 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1253 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1254 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1255 const char *comp_name = comp_flags_table[i].cfn_name;
1257 if (strcmp(name, comp_name) == 0) {
1258 *flags |= comp_flag;
1260 } else if (strncmp(name, "^", 1) == 0 &&
1261 strcmp(name + 1, comp_name) == 0) {
1262 *neg_flags |= comp_flag;
1267 llapi_printf(LLAPI_MSG_ERROR,
1268 "%s: component flag '%s' not supported\n",
1274 if (!*flags && !*neg_flags)
1277 /* don't allow to set and exclude the same flag */
1278 if (*flags & *neg_flags)
1284 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1292 if (strncmp(string, "^", 1) == 0) {
1293 *neg_state = llapi_layout_string_flags(string + 1);
1294 if (*neg_state != 0)
1297 *state = llapi_layout_string_flags(string);
1302 llapi_printf(LLAPI_MSG_ERROR,
1303 "%s: mirrored file state '%s' not supported\n",
1309 * struct mirror_args - Command-line arguments for mirror(s).
1310 * @m_count: Number of mirrors to be created with this layout.
1311 * @m_flags: Mirror level flags, only 'prefer' is supported.
1312 * @m_layout: Mirror layout.
1313 * @m_file: A victim file. Its layout will be split and used as a mirror.
1314 * @m_next: Point to the next node of the list.
1316 * Command-line arguments for mirror(s) will be parsed and stored in
1317 * a linked list that consists of this structure.
1319 struct mirror_args {
1322 struct llapi_layout *m_layout;
1324 struct mirror_args *m_next;
1328 * enum mirror_flags - Flags for extending a mirrored file.
1329 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1330 * in case the victim file(s) contains the same data as the
1331 * original mirrored file.
1332 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1333 * @MF_COMP_ID: specified component id instead of mirror id
1335 * Flags for extending a mirrored file.
1345 * mirror_create_sanity_check() - Check mirror list.
1346 * @list: A linked list that stores the mirror arguments.
1348 * This function does a sanity check on @list for creating
1351 * Return: 0 on success or a negative error code on failure.
1353 static int mirror_create_sanity_check(const char *fname,
1354 struct mirror_args *list)
1357 bool has_m_file = false;
1358 bool has_m_layout = false;
1364 struct llapi_layout *layout;
1366 layout = llapi_layout_get_by_path(fname, 0);
1369 "error: %s: file '%s' couldn't get layout\n",
1374 rc = llapi_layout_sanity(layout, false, true);
1376 llapi_layout_free(layout);
1379 llapi_layout_sanity_perror(rc);
1384 while (list != NULL) {
1385 if (list->m_file != NULL) {
1387 llapi_layout_free(list->m_layout);
1390 llapi_layout_get_by_path(list->m_file, 0);
1391 if (list->m_layout == NULL) {
1393 "error: %s: file '%s' has no layout\n",
1394 progname, list->m_file);
1398 has_m_layout = true;
1399 if (list->m_layout == NULL) {
1400 fprintf(stderr, "error: %s: no mirror layout\n",
1406 rc = llapi_layout_sanity(list->m_layout, false, true);
1408 llapi_layout_sanity_perror(rc);
1412 list = list->m_next;
1415 if (has_m_file && has_m_layout) {
1417 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1425 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1427 __u32 mirror_flags = *(__u32 *)cbdata;
1431 rc = llapi_layout_comp_flags_get(layout, &flags);
1436 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1441 return LLAPI_LAYOUT_ITER_CONT;
1445 * mirror_create() - Create a mirrored file.
1446 * @fname: The file to be created.
1447 * @mirror_list: A linked list that stores the mirror arguments.
1449 * This function creates a mirrored file @fname with the mirror(s)
1450 * from @mirror_list.
1452 * Return: 0 on success or a negative error code on failure.
1454 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1456 struct llapi_layout *layout = NULL;
1457 struct mirror_args *cur_mirror = NULL;
1458 uint16_t mirror_count = 0;
1462 rc = mirror_create_sanity_check(NULL, mirror_list);
1466 cur_mirror = mirror_list;
1467 while (cur_mirror != NULL) {
1468 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1470 &cur_mirror->m_flags);
1473 fprintf(stderr, "%s: failed to set mirror flags\n",
1478 for (i = 0; i < cur_mirror->m_count; i++) {
1479 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1482 fprintf(stderr, "error: %s: "
1483 "merge layout failed: %s\n",
1484 progname, strerror(errno));
1488 mirror_count += cur_mirror->m_count;
1489 cur_mirror = cur_mirror->m_next;
1492 if (layout == NULL) {
1493 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1497 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1500 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1501 progname, strerror(errno));
1505 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1513 llapi_layout_free(layout);
1518 * Compare files and check lease on @fd.
1520 * \retval bytes number of bytes are the same
1522 static ssize_t mirror_file_compare(int fd, int fdv)
1524 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1526 ssize_t bytes_done = 0;
1527 ssize_t bytes_read = 0;
1529 buf = malloc(buflen * 2);
1534 if (!llapi_lease_check(fd)) {
1535 bytes_done = -EBUSY;
1539 bytes_read = read(fd, buf, buflen);
1540 if (bytes_read <= 0)
1543 if (bytes_read != read(fdv, buf + buflen, buflen))
1546 /* XXX: should compute the checksum on each buffer and then
1547 * compare checksum to avoid cache collision */
1548 if (memcmp(buf, buf + buflen, bytes_read))
1551 bytes_done += bytes_read;
1559 static int mirror_extend_file(const char *fname, const char *victim_file,
1560 enum mirror_flags mirror_flags)
1565 struct stat stbuf_v;
1566 struct ll_ioc_lease *data = NULL;
1569 fd = open(fname, O_RDWR);
1571 error_loc = "open source file";
1576 fdv = open(victim_file, O_RDWR);
1578 error_loc = "open target file";
1583 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1584 error_loc = "stat source or target file";
1589 if (stbuf.st_dev != stbuf_v.st_dev) {
1590 error_loc = "stat source and target file";
1595 /* mirrors should be of the same size */
1596 if (stbuf.st_size != stbuf_v.st_size) {
1597 error_loc = "file sizes don't match";
1602 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1604 error_loc = "cannot get lease";
1608 if (!(mirror_flags & MF_NO_VERIFY)) {
1610 /* mirrors should have the same contents */
1611 ret = mirror_file_compare(fd, fdv);
1612 if (ret != stbuf.st_size) {
1613 error_loc = "file busy or contents don't match";
1614 rc = ret < 0 ? ret : -EINVAL;
1619 /* Get rid of caching pages from clients */
1620 rc = llapi_file_flush(fd);
1622 error_loc = "cannot get data version";
1626 rc = llapi_file_flush(fdv);
1628 error_loc = "cannot get data version";
1633 /* Make sure we keep original atime/mtime values */
1634 rc = migrate_copy_timestamps(fd, fdv);
1636 error_loc = "cannot copy timestamp";
1640 /* Atomically put lease, merge layouts and close. */
1641 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1643 error_loc = "memory allocation";
1646 data->lil_mode = LL_LEASE_UNLCK;
1647 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1648 data->lil_count = 1;
1649 data->lil_ids[0] = fdv;
1650 rc = llapi_lease_set(fd, data);
1652 error_loc = "cannot merge layout";
1654 } else if (rc == 0) {
1656 error_loc = "lost lease lock";
1669 (void) unlink(victim_file);
1671 fprintf(stderr, "error: %s: %s: %s: %s\n",
1672 progname, fname, error_loc, strerror(-rc));
1676 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1678 struct ll_ioc_lease *data = NULL;
1683 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1687 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1689 error_loc = "cannot get lease";
1693 rc = migrate_nonblock(fd, fdv);
1695 llapi_lease_release(fd);
1699 /* Atomically put lease, merge layouts and close. */
1700 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1702 error_loc = "memory allocation";
1705 data->lil_mode = LL_LEASE_UNLCK;
1706 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1707 data->lil_count = 1;
1708 data->lil_ids[0] = fdv;
1709 rc = llapi_lease_set(fd, data);
1711 error_loc = "cannot merge layout";
1713 } else if (rc == 0) {
1715 error_loc = "lost lease lock";
1728 fprintf(stderr, "error: %s: %s: %s: %s\n",
1729 progname, name, error_loc, strerror(-rc));
1733 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1734 enum mirror_flags mirror_flags)
1738 rc = mirror_create_sanity_check(fname, mirror_list);
1742 while (mirror_list) {
1743 if (mirror_list->m_file != NULL) {
1744 rc = mirror_extend_file(fname, mirror_list->m_file,
1747 __u32 mirror_count = mirror_list->m_count;
1749 while (mirror_count > 0) {
1750 rc = mirror_extend_layout(fname,
1751 mirror_list->m_layout);
1761 mirror_list = mirror_list->m_next;
1767 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1772 rc = llapi_layout_mirror_id_get(layout, &id);
1776 if ((__u16)id == *(__u16 *)cbdata)
1777 return LLAPI_LAYOUT_ITER_STOP;
1779 return LLAPI_LAYOUT_ITER_CONT;
1782 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1787 rc = llapi_layout_comp_id_get(layout, &id);
1791 if (id == *(__u32 *)cbdata)
1792 return LLAPI_LAYOUT_ITER_STOP;
1794 return LLAPI_LAYOUT_ITER_CONT;
1797 struct pool_to_id_cbdata {
1801 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1803 char buf[LOV_MAXPOOLNAME + 1];
1804 struct pool_to_id_cbdata *d = (void *)cbdata;
1808 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1811 if (strcmp(d->pool, buf))
1812 return LLAPI_LAYOUT_ITER_CONT;
1814 rc = llapi_layout_mirror_id_get(layout, &id);
1819 return LLAPI_LAYOUT_ITER_STOP;
1822 static int mirror_split(const char *fname, __u32 id, const char *pool,
1823 enum mirror_flags mflags, const char *victim_file)
1825 struct llapi_layout *layout;
1826 char parent[PATH_MAX];
1827 char victim[PATH_MAX];
1828 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1830 struct ll_ioc_lease *data;
1831 uint16_t mirror_count;
1836 /* check fname contains mirror with mirror_id/comp_id */
1837 layout = llapi_layout_get_by_path(fname, 0);
1840 "error %s: file '%s' couldn't get layout\n",
1845 rc = llapi_layout_sanity(layout, false, true);
1847 llapi_layout_sanity_perror(rc);
1851 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1854 "error %s: file '%s' couldn't get mirror count\n",
1858 if (mirror_count < 2) {
1860 "error %s: file '%s' has %d component, cannot split\n",
1861 progname, fname, mirror_count);
1865 if (mflags & MF_COMP_POOL) {
1866 struct pool_to_id_cbdata data = { .pool = pool };
1868 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
1871 } else if (mflags & MF_COMP_ID) {
1872 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1873 id = mirror_id_of(id);
1875 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1878 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1881 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1883 "error %s: file '%s' does not contain mirror with id %u\n",
1884 progname, fname, id);
1888 fd = open(fname, O_RDWR);
1891 "error %s: open file '%s' failed: %s\n",
1892 progname, fname, strerror(errno));
1896 /* get victim file directory pathname */
1897 if (strlen(fname) > sizeof(parent) - 1) {
1898 fprintf(stderr, "error %s: file name of '%s' too long\n",
1903 strncpy(parent, fname, sizeof(parent));
1904 ptr = strrchr(parent, '/');
1906 if (getcwd(parent, sizeof(parent)) == NULL) {
1907 fprintf(stderr, "error %s: getcwd failed: %s\n",
1908 progname, strerror(errno));
1918 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1920 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1925 if (victim_file == NULL) {
1926 /* use a temp file to store the splitted layout */
1927 if (mflags & MF_DESTROY) {
1928 fdv = llapi_create_volatile_idx(parent, mdt_index,
1929 O_LOV_DELAY_CREATE);
1931 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1933 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1936 /* user specified victim file */
1937 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1942 "error %s: create victim file failed: %s\n",
1943 progname, strerror(errno));
1947 /* get lease lock of fname */
1948 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1951 "error %s: cannot get lease of file '%s': %d\n",
1952 progname, fname, rc);
1956 /* Atomatically put lease, split layouts and close. */
1957 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1963 data->lil_mode = LL_LEASE_UNLCK;
1964 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1965 data->lil_count = 2;
1966 data->lil_ids[0] = fdv;
1967 data->lil_ids[1] = id;
1968 rc = llapi_lease_set(fd, data);
1970 if (rc == 0) /* lost lease lock */
1973 "error %s: cannot split '%s': %s\n",
1974 progname, fname, strerror(-rc));
1985 llapi_layout_free(layout);
1990 * Parse a string containing an target index list into an array of integers.
1992 * The input string contains a comma delimited list of individual
1993 * indices and ranges, for example "1,2-4,7". Add the indices into the
1994 * \a tgts array and remove duplicates.
1996 * \param[out] tgts array to store indices in
1997 * \param[in] size size of \a tgts array
1998 * \param[in] offset starting index in \a tgts
1999 * \param[in] arg string containing OST index list
2000 * \param[in/out] overstriping index list may contain duplicates
2002 * \retval positive number of indices in \a tgts
2003 * \retval -EINVAL unable to parse \a arg
2005 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2006 unsigned long long *pattern)
2010 int slots = size - offset;
2012 bool overstriped = false;
2018 end_of_loop = false;
2019 while (!end_of_loop) {
2020 int start_index = 0;
2023 char *endptr = NULL;
2027 ptr = strchrnul(arg, ',');
2029 end_of_loop = *ptr == '\0';
2032 start_index = strtol(arg, &endptr, 0);
2033 if (endptr == arg) /* no data at all */
2035 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2038 end_index = start_index;
2039 if (*endptr == '-') {
2040 end_index = strtol(endptr + 1, &endptr, 0);
2041 if (*endptr != '\0')
2043 if (end_index < start_index)
2047 for (i = start_index; i <= end_index && slots > 0; i++) {
2050 /* remove duplicate */
2051 for (j = 0; j < offset; j++) {
2052 if (tgts[j] == i && pattern &&
2053 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2055 else if (tgts[j] == i)
2061 if (j == offset) { /* check complete */
2067 if (slots == 0 && i < end_index)
2075 if (!end_of_loop && ptr != NULL)
2078 if (!overstriped && pattern)
2079 *pattern = LLAPI_LAYOUT_DEFAULT;
2081 return rc < 0 ? rc : nr;
2084 struct lfs_setstripe_args {
2085 unsigned long long lsa_comp_end;
2086 unsigned long long lsa_stripe_size;
2087 unsigned long long lsa_extension_size;
2088 long long lsa_stripe_count;
2089 long long lsa_stripe_off;
2090 __u32 lsa_comp_flags;
2091 __u32 lsa_comp_neg_flags;
2092 unsigned long long lsa_pattern;
2093 unsigned int lsa_mirror_count;
2095 bool lsa_first_comp;
2096 bool lsa_extension_comp;
2098 char *lsa_pool_name;
2101 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2103 unsigned int mirror_count = lsa->lsa_mirror_count;
2104 bool first_comp = lsa->lsa_first_comp;
2106 memset(lsa, 0, sizeof(*lsa));
2108 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2109 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2110 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2111 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2112 lsa->lsa_pool_name = NULL;
2114 lsa->lsa_mirror_count = mirror_count;
2115 lsa->lsa_first_comp = first_comp;
2119 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2120 * @lsa: Stripe options to be initialized and inherited.
2122 * This function initializes stripe options in @lsa and inherit
2123 * stripe_size, stripe_count and OST pool_name options.
2127 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2129 unsigned long long stripe_size;
2130 long long stripe_count;
2131 char *pool_name = NULL;
2133 stripe_size = lsa->lsa_stripe_size;
2134 stripe_count = lsa->lsa_stripe_count;
2135 pool_name = lsa->lsa_pool_name;
2137 setstripe_args_init(lsa);
2139 lsa->lsa_stripe_size = stripe_size;
2140 lsa->lsa_stripe_count = stripe_count;
2141 lsa->lsa_pool_name = pool_name;
2144 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2146 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2147 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2148 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2149 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2150 lsa->lsa_comp_end != 0);
2154 * comp_args_to_layout() - Create or extend a composite layout.
2155 * @composite: Pointer to the composite layout.
2156 * @lsa: Stripe options for the new component.
2158 * This function creates or extends a composite layout by adding a new
2159 * component with stripe options from @lsa.
2161 * Return: 0 on success or an error code on failure.
2163 static int comp_args_to_layout(struct llapi_layout **composite,
2164 struct lfs_setstripe_args *lsa,
2167 struct llapi_layout *layout = *composite;
2168 uint64_t prev_end = 0;
2173 if (layout == NULL) {
2174 layout = llapi_layout_alloc();
2175 if (layout == NULL) {
2176 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2181 *composite = layout;
2182 lsa->lsa_first_comp = true;
2186 /* Get current component extent, current component
2187 * must be the tail component. */
2188 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2190 fprintf(stderr, "Get comp extent failed. %s\n",
2195 if (lsa->lsa_first_comp)
2198 if (lsa->lsa_first_comp)
2199 rc = llapi_layout_add_first_comp(layout);
2201 rc = llapi_layout_comp_add(layout);
2203 fprintf(stderr, "Add component failed. %s\n",
2209 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2211 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2212 lsa->lsa_comp_flags, strerror(errno));
2217 uint64_t comp_end = lsa->lsa_comp_end;
2219 /* The extendable component is 0-length, so it can be removed
2220 * if there is insufficient space to extend it. */
2221 if (lsa->lsa_extension_comp)
2222 comp_end = prev_end;
2224 rc = llapi_layout_comp_extent_set(layout, prev_end,
2227 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2228 prev_end, comp_end, strerror(errno));
2232 /* reset lsa_first_comp */
2233 lsa->lsa_first_comp = false;
2235 /* Data-on-MDT component setting */
2236 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2237 /* In case of Data-on-MDT patterns the only extra option
2238 * applicable is stripe size option. */
2239 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2240 fprintf(stderr, "Option 'stripe-count' can't be "
2241 "specified with Data-on-MDT component: %lld\n",
2242 lsa->lsa_stripe_count);
2246 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2247 fprintf(stderr, "Option 'stripe-size' can't be "
2248 "specified with Data-on-MDT component: %llu\n",
2249 lsa->lsa_stripe_size);
2253 if (lsa->lsa_nr_tgts != 0) {
2254 fprintf(stderr, "Option 'ost-list' can't be specified "
2255 "with Data-on-MDT component: '%i'\n",
2260 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2261 fprintf(stderr, "Option 'stripe-offset' can't be "
2262 "specified with Data-on-MDT component: %lld\n",
2263 lsa->lsa_stripe_off);
2267 if (lsa->lsa_pool_name != 0) {
2268 fprintf(stderr, "Option 'pool' can't be specified "
2269 "with Data-on-MDT component: '%s'\n",
2270 lsa->lsa_pool_name);
2275 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2277 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2278 lsa->lsa_pattern, strerror(errno));
2281 /* Data-on-MDT component has always single stripe up to end */
2282 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2283 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2284 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2286 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2287 lsa->lsa_pattern, strerror(errno));
2292 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2293 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2295 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2296 rc = llapi_layout_extension_size_set(layout, size);
2298 rc = llapi_layout_stripe_size_set(layout, size);
2301 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2302 size, strerror(errno));
2306 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2308 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2309 lsa->lsa_stripe_count, strerror(errno));
2313 if (lsa->lsa_pool_name != NULL) {
2314 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2316 fprintf(stderr, "Set pool name: %s failed. %s\n",
2317 lsa->lsa_pool_name, strerror(errno));
2321 rc = llapi_layout_pool_name_set(layout, "");
2323 fprintf(stderr, "Clear pool name failed: %s\n",
2329 if (lsa->lsa_nr_tgts > 0) {
2330 if (lsa->lsa_stripe_count > 0 &&
2331 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2332 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2333 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2334 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2335 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2339 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2340 rc = llapi_layout_ost_index_set(layout, i,
2345 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2346 lsa->lsa_stripe_off != -1) {
2347 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2350 fprintf(stderr, "Set ost index %d failed. %s\n",
2351 i, strerror(errno));
2355 /* Create the second, virtual component of extension space */
2356 if (lsa->lsa_extension_comp) {
2357 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2358 lsa->lsa_extension_comp = false;
2365 static int build_component(struct llapi_layout **layout,
2366 struct lfs_setstripe_args *lsa, bool set_extent)
2370 rc = comp_args_to_layout(layout, lsa, set_extent);
2374 if (lsa->lsa_mirror_count > 0) {
2375 rc = llapi_layout_mirror_count_set(*layout,
2376 lsa->lsa_mirror_count);
2380 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2383 lsa->lsa_mirror_count = 0;
2389 static int build_layout_from_yaml_node(struct cYAML *node,
2390 struct llapi_layout **layout,
2391 struct lfs_setstripe_args *lsa,
2398 if (node->cy_type == CYAML_TYPE_OBJECT) {
2399 /* go deep to sub blocks */
2400 rc = build_layout_from_yaml_node(node->cy_child, layout,
2405 if (node->cy_string == NULL)
2408 string = node->cy_string;
2409 /* skip leading lmm_ if present, to simplify parsing */
2410 if (strncmp(string, "lmm_", 4) == 0)
2413 if (node->cy_type == CYAML_TYPE_STRING) {
2414 if (!strcmp(string, "lcme_extent.e_end")) {
2415 if (!strcmp(node->cy_valuestring, "EOF") ||
2416 !strcmp(node->cy_valuestring, "eof"))
2417 lsa->lsa_comp_end = LUSTRE_EOF;
2418 } else if (!strcmp(string, "pool")) {
2419 lsa->lsa_pool_name = node->cy_valuestring;
2420 } else if (!strcmp(string, "pattern")) {
2421 if (!strcmp(node->cy_valuestring, "mdt"))
2422 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2423 if (!strcmp(node->cy_valuestring,
2424 "raid0,overstriped"))
2426 LLAPI_LAYOUT_OVERSTRIPING;
2427 } else if (!strcmp(string, "lcme_flags")) {
2428 rc = comp_str2flags(node->cy_valuestring,
2429 &lsa->lsa_comp_flags,
2430 &lsa->lsa_comp_neg_flags);
2433 /* Only template flags have meaning in
2434 * the layout for a new file
2436 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2438 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2439 if (!strcmp(string, "lcm_mirror_count")) {
2440 lsa->lsa_mirror_count = node->cy_valueint;
2441 } else if (!strcmp(string, "lcme_extent.e_start")) {
2442 if (node->cy_valueint != 0 || *layout != NULL) {
2443 rc = build_component(layout, lsa, true);
2448 if (node->cy_valueint == 0)
2449 lsa->lsa_first_comp = true;
2451 /* initialize lsa */
2452 setstripe_args_init(lsa);
2453 lsa->lsa_tgts = osts;
2454 } else if (!strcmp(string, "lcme_extent.e_end")) {
2455 if (node->cy_valueint == -1)
2456 lsa->lsa_comp_end = LUSTRE_EOF;
2458 lsa->lsa_comp_end = node->cy_valueint;
2459 } else if (!strcmp(string, "stripe_count")) {
2460 lsa->lsa_stripe_count = node->cy_valueint;
2461 } else if (!strcmp(string, "stripe_size")) {
2462 lsa->lsa_stripe_size = node->cy_valueint;
2463 } else if (!strcmp(string, "stripe_offset")) {
2464 lsa->lsa_stripe_off = node->cy_valueint;
2465 } else if (!strcmp(string, "l_ost_idx")) {
2466 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2471 node = node->cy_next;
2477 static int lfs_comp_create_from_yaml(char *template,
2478 struct llapi_layout **layout,
2479 struct lfs_setstripe_args *lsa,
2482 struct cYAML *tree = NULL, *err_rc = NULL;
2485 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2487 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2488 progname, template);
2489 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2490 "can't parse", &err_rc);
2491 cYAML_print_tree2file(stderr, err_rc);
2492 cYAML_free_tree(err_rc);
2497 /* initialize lsa for plain file */
2498 setstripe_args_init(lsa);
2499 lsa->lsa_tgts = osts;
2501 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2503 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2504 progname, template);
2507 rc = build_component(layout, lsa, *layout != NULL);
2509 /* clean clean lsa */
2510 setstripe_args_init(lsa);
2514 cYAML_free_tree(tree);
2519 * Get the extension size from the next (SEL) component and extend the
2520 * current component on it. The start of the next component is to be
2523 * \param[in] layout the current layout
2524 * \param[in] start the start of the current component
2525 * \param[in,out] end the end of the current component
2526 * \param[in] offset the offset to adjust the end position to instead of
2529 * \retval 0 - extended successfully
2530 * \retval < 0 - error
2532 static int layout_extend_comp(struct llapi_layout *layout,
2533 uint64_t start, uint64_t *end,
2536 uint64_t size, next_start, next_end;
2539 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2541 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2542 "%s\n", progname, strerror(errno));
2546 /* Even if the @size will not be used below, this will fail if
2547 * this is not a SEL component - a good confirmation we are
2548 * working on right components. */
2549 rc = llapi_layout_extension_size_get(layout, &size);
2551 fprintf(stderr, "%s setstripe: cannot get component ext size: "
2552 "%s\n", progname, strerror(errno));
2556 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2558 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2559 progname, strerror(errno));
2563 next_start += offset ?: size;
2564 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2566 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2567 progname, strerror(errno));
2571 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2573 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2574 "%s\n", progname, strerror(errno));
2578 *end += offset ?: size;
2579 rc = llapi_layout_comp_extent_set(layout, start, *end);
2581 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2582 progname, strerror(errno));
2590 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2591 * end of the last component in the existing file, and adjust the
2592 * first extent start of the components to be added accordingly.
2594 * In the create mode, we need to check if the first component is an extendable
2595 * SEL component and extend its length to the extension size (first component
2596 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2598 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2601 struct llapi_layout *head;
2602 uint64_t start = 0, prev_end = 0;
2611 head = llapi_layout_get_by_path(fname, 0);
2614 "%s setstripe: cannot read layout from '%s': "
2615 "%s\n", progname, fname, strerror(errno));
2617 } else if (errno == ENODATA) {
2618 /* file without LOVEA, this component-add will be turned
2619 * into a component-create. */
2620 llapi_layout_free(head);
2623 /* the new layout will be added to an empty one, it
2624 * still needs to be adjusted below */
2627 } else if (!llapi_layout_is_composite(head)) {
2628 fprintf(stderr, "%s setstripe: '%s' not a composite "
2629 "file\n", progname, fname);
2630 llapi_layout_free(head);
2634 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2636 fprintf(stderr, "%s setstripe: cannot get prev "
2637 "extent: %s\n", progname, strerror(errno));
2638 llapi_layout_free(head);
2642 llapi_layout_free(head);
2646 /* Make sure we use the first component of the layout to be added. */
2647 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2650 "%s setstripe: cannot move component cursor: %s\n",
2651 progname, strerror(errno));
2655 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2657 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2658 progname, strerror(errno));
2662 if (start == 0 && end == 0) {
2663 rc = layout_extend_comp(layout, start, &end,
2664 comp_add ? prev_end : 0);
2669 if (start > prev_end || end < prev_end) {
2670 fprintf(stderr, "%s setstripe: first extent [%lu, %lu) not "
2671 "adjacent with extent end %lu\n",
2672 progname, start, end, prev_end);
2676 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2678 fprintf(stderr, "%s setstripe: cannot set component extent "
2680 progname, prev_end, end, strerror(errno));
2687 static int mirror_adjust_first_extents(struct mirror_args *list)
2694 while (list != NULL) {
2695 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
2698 list = list->m_next;
2704 static inline bool arg_is_eof(char *arg)
2706 return !strncmp(arg, "-1", strlen("-1")) ||
2707 !strncmp(arg, "EOF", strlen("EOF")) ||
2708 !strncmp(arg, "eof", strlen("eof"));
2712 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2714 * Return: Valid mirror_args pointer on success and
2715 * NULL if memory allocation fails.
2717 static struct mirror_args *lfs_mirror_alloc(void)
2719 struct mirror_args *mirror = NULL;
2722 mirror = calloc(1, sizeof(*mirror));
2733 * lfs_mirror_free() - Free memory allocated for a mirror argument
2735 * @mirror: Previously allocated mirror argument structure by
2736 * lfs_mirror_alloc().
2738 * Free memory allocated for @mirror.
2742 static void lfs_mirror_free(struct mirror_args *mirror)
2744 if (mirror->m_layout != NULL)
2745 llapi_layout_free(mirror->m_layout);
2750 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2751 * @mirror_list: Previously allocated mirror list.
2753 * Free memory allocated for @mirror_list.
2757 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2759 struct mirror_args *next_mirror = NULL;
2761 while (mirror_list != NULL) {
2762 next_mirror = mirror_list->m_next;
2763 lfs_mirror_free(mirror_list);
2764 mirror_list = next_mirror;
2776 LFS_COMP_NO_VERIFY_OPT,
2778 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2780 LFS_MIRROR_STATE_OPT,
2782 LFS_MIRROR_INDEX_OPT,
2783 LFS_LAYOUT_FOREIGN_OPT,
2788 static int lfs_setstripe_internal(int argc, char **argv,
2789 enum setstripe_origin opc)
2791 struct lfs_setstripe_args lsa = { 0 };
2792 struct llapi_stripe_param *param = NULL;
2793 struct find_param migrate_mdt_param = {
2803 unsigned long long size_units = 1;
2804 bool migrate_mode = false;
2805 bool migrate_mdt_mode = false;
2806 bool setstripe_mode = false;
2807 bool migration_block = false;
2808 __u64 migration_flags = 0;
2809 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2810 int comp_del = 0, comp_set = 0;
2813 struct llapi_layout *layout = NULL;
2814 struct llapi_layout **lpp = &layout;
2815 bool mirror_mode = false;
2816 bool has_m_file = false;
2817 __u32 mirror_count = 0;
2818 enum mirror_flags mirror_flags = 0;
2819 struct mirror_args *mirror_list = NULL;
2820 struct mirror_args *new_mirror = NULL;
2821 struct mirror_args *last_mirror = NULL;
2822 __u16 mirror_id = 0;
2824 bool from_yaml = false;
2825 bool from_copy = false;
2826 char *template = NULL;
2827 bool foreign_mode = false;
2829 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
2830 char *mode_opt = NULL;
2831 mode_t previous_umask = 0;
2832 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2834 struct option long_opts[] = {
2835 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2836 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2837 /* --block is only valid in migrate mode */
2838 { .val = 'b', .name = "block", .has_arg = no_argument },
2839 { .val = LFS_COMP_ADD_OPT,
2840 .name = "comp-add", .has_arg = no_argument },
2841 { .val = LFS_COMP_ADD_OPT,
2842 .name = "component-add", .has_arg = no_argument },
2843 { .val = LFS_COMP_DEL_OPT,
2844 .name = "comp-del", .has_arg = no_argument },
2845 { .val = LFS_COMP_DEL_OPT,
2846 .name = "component-del", .has_arg = no_argument },
2847 { .val = LFS_COMP_FLAGS_OPT,
2848 .name = "comp-flags", .has_arg = required_argument },
2849 { .val = LFS_COMP_FLAGS_OPT,
2850 .name = "component-flags",
2851 .has_arg = required_argument },
2852 { .val = LFS_COMP_SET_OPT,
2853 .name = "comp-set", .has_arg = no_argument },
2854 { .val = LFS_COMP_SET_OPT,
2855 .name = "component-set",
2856 .has_arg = no_argument},
2857 { .val = LFS_COMP_NO_VERIFY_OPT,
2858 .name = "no-verify", .has_arg = no_argument},
2859 { .val = LFS_LAYOUT_FLAGS_OPT,
2860 .name = "flags", .has_arg = required_argument},
2861 { .val = LFS_LAYOUT_FOREIGN_OPT,
2862 .name = "foreign", .has_arg = optional_argument},
2863 { .val = LFS_MIRROR_ID_OPT,
2864 .name = "mirror-id", .has_arg = required_argument},
2865 { .val = LFS_MODE_OPT,
2866 .name = "mode", .has_arg = required_argument},
2867 { .val = LFS_LAYOUT_COPY,
2868 .name = "copy", .has_arg = required_argument},
2869 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2870 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2871 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2872 { .val = 'C', .name = "overstripe-count",
2873 .has_arg = required_argument},
2874 { .val = 'd', .name = "delete", .has_arg = no_argument},
2875 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2876 /* --non-direct is only valid in migrate mode */
2877 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2878 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2879 { .val = 'E', .name = "component-end",
2880 .has_arg = required_argument},
2881 { .val = 'f', .name = "file", .has_arg = required_argument },
2882 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2883 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2884 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2885 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2886 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2887 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2888 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2889 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2890 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2891 { .val = 'L', .name = "layout", .has_arg = required_argument },
2892 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2893 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2894 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2895 /* --non-block is only valid in migrate mode */
2896 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2897 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2898 { .val = 'o', .name = "ost", .has_arg = required_argument },
2899 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2900 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2901 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2903 { .val = 'p', .name = "pool", .has_arg = required_argument },
2904 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2905 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2906 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2907 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2908 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2909 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2910 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2911 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2912 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2913 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2914 /* --verbose is only valid in migrate mode */
2915 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2916 { .val = 'x', .name = "xattr", .has_arg = required_argument },
2917 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2918 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
2919 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
2922 setstripe_args_init(&lsa);
2924 migrate_mode = (opc == SO_MIGRATE);
2925 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2926 setstripe_mode = (opc == SO_SETSTRIPE);
2928 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2930 while ((c = getopt_long(argc, argv,
2931 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
2932 long_opts, NULL)) >= 0) {
2938 case LFS_COMP_ADD_OPT:
2941 case LFS_COMP_DEL_OPT:
2944 case LFS_COMP_FLAGS_OPT:
2945 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2946 &lsa.lsa_comp_neg_flags);
2949 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2950 fprintf(stderr, "%s: inverted flags are not supported\n",
2955 case LFS_COMP_SET_OPT:
2958 case LFS_COMP_NO_VERIFY_OPT:
2959 mirror_flags |= MF_NO_VERIFY;
2961 case LFS_MIRROR_ID_OPT:
2962 mirror_id = strtoul(optarg, &end, 0);
2963 if (*end != '\0' || mirror_id == 0) {
2965 "%s %s: invalid mirror ID '%s'\n",
2966 progname, argv[0], optarg);
2970 case LFS_LAYOUT_FLAGS_OPT: {
2973 /* check for numeric flags (foreign and mirror cases) */
2974 if (setstripe_mode && !mirror_mode && !last_mirror) {
2975 flags = strtoul(optarg, &end, 16);
2978 "%s %s: bad flags '%s'\n",
2979 progname, argv[0], optarg);
2985 if (!mirror_mode || !last_mirror) {
2986 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2991 result = comp_str2flags(optarg, &last_mirror->m_flags,
2997 fprintf(stderr, "%s: inverted flags are not supported\n",
3002 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3004 "%s: unsupported mirror flags: %s\n",
3011 case LFS_LAYOUT_FOREIGN_OPT:
3012 if (optarg != NULL) {
3013 /* check pure numeric */
3014 type = strtoul(optarg, &end, 0);
3017 type = check_foreign_type_name(optarg);
3018 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3020 "%s %s: unrecognized foreign type '%s'\n",
3027 foreign_mode = true;
3031 if (mode_opt != NULL) {
3032 mode = strtoul(mode_opt, &end, 8);
3035 "%s %s: bad mode '%s'\n",
3036 progname, argv[0], mode_opt);
3039 previous_umask = umask(0);
3042 case LFS_LAYOUT_COPY:
3047 if (!migrate_mode) {
3049 "%s %s: -b|--block valid only for migrate command\n",
3053 migration_block = true;
3056 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3059 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3062 "%s %s: invalid stripe count '%s'\n",
3063 progname, argv[0], optarg);
3067 if (lsa.lsa_stripe_count == -1)
3068 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3071 /* delete the default striping pattern */
3073 if (opc == SO_MIRROR_SPLIT) {
3076 "%s %s: -d cannot used with -f\n",
3080 mirror_flags |= MF_DESTROY;
3084 if (!migrate_mode) {
3086 "%s %s: -D|--non-direct is valid "
3087 "only for migrate command\n",
3091 migration_flags |= MIGRATION_NONDIRECT;
3094 if (lsa.lsa_comp_end != 0) {
3095 result = comp_args_to_layout(lpp, &lsa, true);
3097 fprintf(stderr, "%s: invalid layout\n",
3102 setstripe_args_init_inherit(&lsa);
3105 if (arg_is_eof(optarg)) {
3106 lsa.lsa_comp_end = LUSTRE_EOF;
3108 result = llapi_parse_size(optarg,
3113 "%s %s: invalid component end '%s'\n",
3114 progname, argv[0], optarg);
3120 if (!migrate_mode) {
3121 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3125 lsa.lsa_pattern = check_hashtype(optarg);
3126 if (lsa.lsa_pattern == 0) {
3128 "%s %s: bad stripe hash type '%s'\n",
3129 progname, argv[0], optarg);
3134 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3137 "%s %s: invalid stripe offset '%s'\n",
3138 progname, argv[0], optarg);
3141 if (lsa.lsa_stripe_off == -1)
3142 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3145 comp_id = strtoul(optarg, &end, 0);
3146 if (*end != '\0' || comp_id == 0 ||
3147 comp_id > LCME_ID_MAX) {
3149 "%s %s: invalid component ID '%s'\n",
3150 progname, argv[0], optarg);
3155 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3157 "error: %s: invalid option: %s\n",
3158 progname, argv[optopt + 1]);
3161 if (opc == SO_MIRROR_EXTEND) {
3162 if (last_mirror == NULL) {
3164 "error: %s: '-N' must exist in front of '%s'\n",
3165 progname, argv[optopt + 1]);
3168 last_mirror->m_file = optarg;
3169 last_mirror->m_count = 1;
3172 if (mirror_list == NULL)
3173 mirror_list = lfs_mirror_alloc();
3174 mirror_list->m_file = optarg;
3179 if (strcmp(argv[optind - 1], "mdt") == 0) {
3180 /* Can be only the first component */
3181 if (layout != NULL) {
3183 fprintf(stderr, "error: 'mdt' layout "
3184 "can be only the first one\n");
3187 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3189 fprintf(stderr, "error: 'mdt' layout "
3190 "size is too big\n");
3193 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3194 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3196 fprintf(stderr, "error: layout '%s' is "
3197 "unknown, supported layouts are: "
3198 "'mdt', 'raid0'\n", argv[optind]);
3203 if (!migrate_mode) {
3205 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3209 migrate_mdt_mode = true;
3210 lsa.lsa_nr_tgts = parse_targets(tgts,
3211 sizeof(tgts) / sizeof(__u32),
3212 lsa.lsa_nr_tgts, optarg, NULL);
3213 if (lsa.lsa_nr_tgts < 0) {
3215 "%s %s: invalid MDT target(s) '%s'\n",
3216 progname, argv[0], optarg);
3220 lsa.lsa_tgts = tgts;
3221 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3222 lsa.lsa_stripe_off = tgts[0];
3225 if (!migrate_mode) {
3227 "%s %s: -n|--non-block valid only for migrate command\n",
3231 migration_flags |= MIGRATION_NONBLOCK;
3234 if (opc == SO_SETSTRIPE) {
3235 opc = SO_MIRROR_CREATE;
3239 if (optarg != NULL) {
3240 mirror_count = strtoul(optarg, &end, 0);
3241 if (*end != '\0' || mirror_count == 0) {
3243 "error: %s: bad mirror count: %s\n",
3250 new_mirror = lfs_mirror_alloc();
3251 new_mirror->m_count = mirror_count;
3253 if (mirror_list == NULL)
3254 mirror_list = new_mirror;
3256 if (last_mirror != NULL) {
3257 /* wrap up last mirror */
3258 if (lsa.lsa_comp_end == 0)
3259 lsa.lsa_comp_end = LUSTRE_EOF;
3261 result = comp_args_to_layout(lpp, &lsa, true);
3263 lfs_mirror_free(new_mirror);
3267 setstripe_args_init_inherit(&lsa);
3269 last_mirror->m_next = new_mirror;
3272 last_mirror = new_mirror;
3273 lpp = &last_mirror->m_layout;
3276 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3277 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3278 fprintf(stderr, "warning: '--ost-list' is "
3279 "deprecated, use '--ost' instead\n");
3281 /* -o allows overstriping, and must note it because
3282 * parse_targets is shared with MDT striping, which
3283 * does not allow duplicates
3285 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3286 lsa.lsa_nr_tgts = parse_targets(tgts,
3287 sizeof(tgts) / sizeof(__u32),
3288 lsa.lsa_nr_tgts, optarg,
3290 if (lsa.lsa_nr_tgts < 0) {
3292 "%s %s: invalid OST target(s) '%s'\n",
3293 progname, argv[0], optarg);
3297 lsa.lsa_tgts = tgts;
3298 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3299 lsa.lsa_stripe_off = tgts[0];
3304 lsa.lsa_pool_name = optarg;
3306 if (strlen(lsa.lsa_pool_name) == 0 ||
3307 strncmp(lsa.lsa_pool_name, "none",
3308 LOV_MAXPOOLNAME) == 0)
3309 lsa.lsa_pool_name = NULL;
3312 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3316 "%s %s: invalid stripe size '%s'\n",
3317 progname, argv[0], optarg);
3322 if (!migrate_mode) {
3324 "%s %s: -v|--verbose valid only for migrate command\n",
3328 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3329 migration_flags = MIGRATION_VERBOSE;
3339 result = llapi_parse_size(optarg,
3340 &lsa.lsa_extension_size,
3344 "%s %s: invalid extension size '%s'\n",
3345 progname, argv[0], optarg);
3349 lsa.lsa_extension_comp = true;
3352 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3353 progname, argv[0], argv[optind - 1]);
3358 fname = argv[optind];
3360 if (optind == argc) {
3361 fprintf(stderr, "%s %s: FILE must be specified\n",
3366 if (xattr && !foreign_mode) {
3367 /* only print a warning as this is harmless and will be ignored
3370 "%s %s: xattr has been specified for non-foreign layout\n",
3372 } else if (foreign_mode && !xattr) {
3374 "%s %s: xattr must be provided in foreign mode\n",
3379 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3380 comp_set || comp_id || delete || from_copy ||
3381 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3384 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3389 if (mirror_mode && mirror_count == 0) {
3391 "error: %s: --mirror-count|-N option is required\n",
3398 if (lsa.lsa_comp_end == 0)
3399 lsa.lsa_comp_end = LUSTRE_EOF;
3402 if (lsa.lsa_comp_end != 0) {
3403 result = comp_args_to_layout(lpp, &lsa, true);
3405 fprintf(stderr, "error: %s: invalid layout\n",
3412 if (mirror_flags & MF_NO_VERIFY) {
3413 if (opc != SO_MIRROR_EXTEND) {
3415 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3419 } else if (!has_m_file) {
3421 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3428 if (comp_set && !comp_id) {
3429 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3434 if ((delete + comp_set + comp_del + comp_add) > 1) {
3436 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3441 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3442 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3444 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3449 if ((comp_set || comp_del) &&
3450 (setstripe_args_specified(&lsa) || layout != NULL)) {
3452 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3457 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3459 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3464 if (comp_add || comp_del) {
3467 result = lstat(fname, &st);
3468 if (result == 0 && S_ISDIR(st.st_mode)) {
3470 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3476 fprintf(stderr, "error: %s: can't use --component-add "
3477 "or --component-del for mirror operation\n",
3484 if (layout == NULL) {
3486 "%s %s: option -E must be specified with --component-add\n",
3492 if (layout != NULL || mirror_list != NULL) {
3494 result = mirror_adjust_first_extents(mirror_list);
3496 result = layout_adjust_first_extent(fname, layout,
3498 if (result == -ENODATA)
3500 else if (result != 0) {
3501 fprintf(stderr, "error: %s: invalid layout\n",
3507 if (from_yaml && from_copy) {
3509 "%s: can't specify --yaml and --copy together\n",
3514 if ((from_yaml || from_copy) &&
3515 (setstripe_args_specified(&lsa) || layout != NULL)) {
3516 fprintf(stderr, "error: %s: can't specify --yaml with "
3517 "-c, -S, -i, -o, -p or -E options.\n",
3522 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3524 "%s %s: options --non-block and --block are mutually exclusive\n",
3529 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3532 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3537 if (migrate_mdt_mode) {
3538 struct lmv_user_md *lmu;
3540 /* initialize migrate mdt parameters */
3541 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3542 LMV_USER_MAGIC_SPECIFIC));
3545 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3546 progname, argv[0], strerror(ENOMEM));
3550 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3551 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3552 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3554 "%s %s: migrate should specify MDT index\n",
3559 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3560 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3561 lmu->lum_hash_type = lsa.lsa_pattern;
3563 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3564 if (lsa.lsa_pool_name) {
3565 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3566 sizeof(lmu->lum_pool_name) - 1);
3567 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3569 if (lsa.lsa_nr_tgts > 1) {
3572 if (lsa.lsa_stripe_count > 0 &&
3573 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3574 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3576 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3577 progname, lsa.lsa_stripe_count,
3583 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3584 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3585 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3586 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3588 lmu->lum_magic = LMV_USER_MAGIC;
3591 migrate_mdt_param.fp_lmv_md = lmu;
3592 migrate_mdt_param.fp_migrate = 1;
3593 } else if (layout == NULL) {
3594 /* initialize stripe parameters */
3595 param = calloc(1, offsetof(typeof(*param),
3596 lsp_osts[lsa.lsa_nr_tgts]));
3597 if (param == NULL) {
3599 "%s %s: cannot allocate memory for parameters: %s\n",
3600 progname, argv[0], strerror(ENOMEM));
3605 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3606 param->lsp_stripe_size = lsa.lsa_stripe_size;
3607 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3608 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3609 param->lsp_stripe_count = -1;
3611 param->lsp_stripe_count = lsa.lsa_stripe_count;
3613 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3614 param->lsp_stripe_offset = -1;
3616 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3617 param->lsp_stripe_pattern =
3618 llapi_pattern_to_lov(lsa.lsa_pattern);
3619 if (param->lsp_stripe_pattern == EINVAL) {
3620 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3625 param->lsp_pool = lsa.lsa_pool_name;
3626 param->lsp_is_specific = false;
3627 if (lsa.lsa_nr_tgts > 0) {
3628 if (lsa.lsa_stripe_count > 0 &&
3629 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3630 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3631 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3632 fprintf(stderr, "error: %s: stripe count %lld "
3633 "doesn't match the number of OSTs: %d\n"
3634 , argv[0], lsa.lsa_stripe_count,
3640 param->lsp_is_specific = true;
3641 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3642 memcpy(param->lsp_osts, tgts,
3643 sizeof(*tgts) * lsa.lsa_nr_tgts);
3648 /* generate a layout from a YAML template */
3649 result = lfs_comp_create_from_yaml(template, &layout,
3652 fprintf(stderr, "error: %s: can't create composite "
3653 "layout from template file %s\n",
3657 } else if (from_copy) {
3658 layout = llapi_layout_get_by_path(template, 0);
3659 if (layout == NULL) {
3661 "%s: can't create composite layout from file %s.\n",
3662 progname, template);
3667 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3668 if (migrate_mdt_mode) {
3669 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3670 } else if (migrate_mode) {
3671 result = lfs_migrate(fname, migration_flags, param,
3673 } else if (comp_set != 0) {
3674 result = lfs_component_set(fname, comp_id,
3676 lsa.lsa_comp_neg_flags);
3677 } else if (comp_del != 0) {
3678 result = lfs_component_del(fname, comp_id,
3680 lsa.lsa_comp_neg_flags);
3681 } else if (comp_add != 0) {
3682 result = lfs_component_add(fname, layout);
3683 } else if (opc == SO_MIRROR_CREATE) {
3684 result = mirror_create(fname, mirror_list);
3685 } else if (opc == SO_MIRROR_EXTEND) {
3686 result = mirror_extend(fname, mirror_list,
3688 } else if (opc == SO_MIRROR_SPLIT) {
3689 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3691 "%s %s: no mirror id or component id or pool name"
3692 " is specified\n", progname, argv[0]);
3695 if (lsa.lsa_pool_name)
3696 mirror_flags |= MF_COMP_POOL;
3697 else if (mirror_id != 0)
3698 comp_id = mirror_id;
3700 mirror_flags |= MF_COMP_ID;
3701 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3703 has_m_file ? mirror_list->m_file :
3705 } else if (layout != NULL) {
3706 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3712 } else if (foreign_mode) {
3713 result = llapi_file_create_foreign(fname, mode, type,
3720 result = llapi_file_open_param(fname,
3729 /* Save the first error encountered. */
3736 if (mode_opt != NULL)
3737 umask(previous_umask);
3740 free(migrate_mdt_param.fp_lmv_md);
3741 llapi_layout_free(layout);
3742 lfs_mirror_list_free(mirror_list);
3747 llapi_layout_free(layout);
3748 lfs_mirror_list_free(mirror_list);
3752 static int lfs_poollist(int argc, char **argv)
3757 return llapi_poollist(argv[1]);
3760 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3765 char *endptr = "AD";
3770 else if (str[0] == '-')
3776 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3777 long long val = strtoll(timebuf, &endptr, 0);
3782 unit *= 52; /* 52 weeks + 1 day below */
3783 case 'w': /* fallthrough */
3785 case '\0': /* days are default unit if none used */
3786 case 'd': /* fallthrough */
3787 unit = (unit + (*endptr == 'y')) * 24;
3788 case 'h': /* fallthrough */
3790 case 'm': /* fallthrough */
3792 case 's': /* fallthrough */
3794 /* don't need to multiply by 1 for seconds */
3797 "%s find: bad time string '%s': %s\n",
3798 progname, timebuf, strerror(EINVAL));
3802 if (param->fp_time_margin == 0 ||
3803 (*endptr && unit < param->fp_time_margin))
3804 param->fp_time_margin = unit;
3811 fprintf(stderr, "%s find: bad time '%s': too large\n",
3821 static int name2uid(unsigned int *id, const char *name)
3823 struct passwd *passwd;
3825 passwd = getpwnam(name);
3828 *id = passwd->pw_uid;
3833 static int name2gid(unsigned int *id, const char *name)
3835 struct group *group;
3837 group = getgrnam(name);
3840 *id = group->gr_gid;
3845 static inline int name2projid(unsigned int *id, const char *name)
3850 static int uid2name(char **name, unsigned int id)
3852 struct passwd *passwd;
3854 passwd = getpwuid(id);
3857 *name = passwd->pw_name;
3862 static inline int gid2name(char **name, unsigned int id)
3864 struct group *group;
3866 group = getgrgid(id);
3869 *name = group->gr_name;
3874 static int name2layout(__u32 *layout, char *name)
3876 char *ptr, *layout_name;
3879 for (ptr = name; ; ptr = NULL) {
3880 layout_name = strtok(ptr, ",");
3881 if (layout_name == NULL)
3883 if (strcmp(layout_name, "released") == 0)
3884 *layout |= LOV_PATTERN_F_RELEASED;
3885 else if (strcmp(layout_name, "raid0") == 0)
3886 *layout |= LOV_PATTERN_RAID0;
3887 else if (strcmp(layout_name, "mdt") == 0)
3888 *layout |= LOV_PATTERN_MDT;
3889 else if (strcmp(layout_name, "overstriping") == 0)
3890 *layout |= LOV_PATTERN_OVERSTRIPING;
3897 static int lfs_find(int argc, char **argv)
3902 struct find_param param = {
3905 .fp_time_margin = 24 * 60 * 60,
3907 struct option long_opts[] = {
3908 { .val = 'A', .name = "atime", .has_arg = required_argument },
3909 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3910 { .val = LFS_COMP_COUNT_OPT,
3911 .name = "comp-count", .has_arg = required_argument },
3912 { .val = LFS_COMP_COUNT_OPT,
3913 .name = "component-count",
3914 .has_arg = required_argument },
3915 { .val = LFS_COMP_FLAGS_OPT,
3916 .name = "comp-flags", .has_arg = required_argument },
3917 { .val = LFS_COMP_FLAGS_OPT,
3918 .name = "component-flags",
3919 .has_arg = required_argument },
3920 { .val = LFS_COMP_START_OPT,
3921 .name = "comp-start", .has_arg = required_argument },
3922 { .val = LFS_COMP_START_OPT,
3923 .name = "component-start",
3924 .has_arg = required_argument },
3925 { .val = LFS_MIRROR_STATE_OPT,
3926 .name = "mirror-state", .has_arg = required_argument },
3927 { .val = LFS_LAYOUT_FOREIGN_OPT,
3928 .name = "foreign", .has_arg = optional_argument},
3929 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3930 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3931 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3932 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3933 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3934 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3935 { .val = 'E', .name = "component-end",
3936 .has_arg = required_argument },
3937 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3938 { .val = LFS_LAYOUT_FOREIGN_OPT,
3939 .name = "foreign", .has_arg = optional_argument},
3940 { .val = 'g', .name = "gid", .has_arg = required_argument },
3941 { .val = 'G', .name = "group", .has_arg = required_argument },
3942 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3943 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3944 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3945 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3946 { .val = 'L', .name = "layout", .has_arg = required_argument },
3947 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3948 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3949 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3950 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3951 { .val = 'n', .name = "name", .has_arg = required_argument },
3952 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3953 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3954 { .val = 'O', .name = "obd", .has_arg = required_argument },
3955 { .val = 'O', .name = "ost", .has_arg = required_argument },
3956 /* no short option for pool yet, can be 'p' after 2.18 */
3957 { .val = LFS_POOL_OPT,
3958 .name = "pool", .has_arg = required_argument },
3959 { .val = '0', .name = "print0", .has_arg = no_argument },
3960 { .val = 'P', .name = "print", .has_arg = no_argument },
3961 { .val = LFS_PROJID_OPT,
3962 .name = "projid", .has_arg = required_argument },
3963 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3964 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3965 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3966 { .val = 's', .name = "size", .has_arg = required_argument },
3967 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3968 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3969 { .val = 't', .name = "type", .has_arg = required_argument },
3970 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3971 { .val = 'u', .name = "uid", .has_arg = required_argument },
3972 { .val = 'U', .name = "user", .has_arg = required_argument },
3973 { .val = 'z', .name = "extension-size",
3974 .has_arg = required_argument },
3975 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
3976 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3977 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3989 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3990 while ((c = getopt_long_only(argc, argv,
3991 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
3992 long_opts, NULL)) >= 0) {
3997 /* '!' is part of option */
3998 /* when getopt_long_only() finds a string which is not
3999 * an option nor a known option argument it returns 1
4000 * in that case if we already have found pathstart and pathend
4001 * (i.e. we have the list of pathnames),
4002 * the only supported value is "!"
4004 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4005 if (!isoption && pathend != -1) {
4006 fprintf(stderr, "err: %s: filename|dirname must either "
4007 "precede options or follow options\n",
4012 if (!isoption && pathstart == -1)
4013 pathstart = optind - 1;
4014 if (isoption && pathstart != -1 && pathend == -1)
4015 pathend = optind - 2;
4021 /* unknown; opt is "!" or path component,
4022 * checking done above.
4024 if (strcmp(optarg, "!") == 0)
4028 xtime = ¶m.fp_atime;
4029 xsign = ¶m.fp_asign;
4030 param.fp_exclude_atime = !!neg_opt;
4031 /* no break, this falls through to 'C' for ctime */
4034 xtime = ¶m.fp_ctime;
4035 xsign = ¶m.fp_csign;
4036 param.fp_exclude_ctime = !!neg_opt;
4038 /* no break, this falls through to 'M' for mtime */
4041 xtime = ¶m.fp_mtime;
4042 xsign = ¶m.fp_msign;
4043 param.fp_exclude_mtime = !!neg_opt;
4045 rc = set_time(¶m, &t, xtime, optarg);
4046 if (rc == LONG_MAX) {
4054 if (optarg[0] == '+') {
4055 param.fp_blocks_sign = -1;
4057 } else if (optarg[0] == '-') {
4058 param.fp_blocks_sign = 1;
4062 param.fp_blocks_units = 1024;
4063 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4064 ¶m.fp_blocks_units, 0);
4066 fprintf(stderr, "error: bad blocks '%s'\n",
4070 param.fp_check_blocks = 1;
4071 param.fp_exclude_blocks = !!neg_opt;
4073 case LFS_COMP_COUNT_OPT:
4074 if (optarg[0] == '+') {
4075 param.fp_comp_count_sign = -1;
4077 } else if (optarg[0] == '-') {
4078 param.fp_comp_count_sign = 1;
4082 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4083 if (*endptr != '\0') {
4084 fprintf(stderr, "error: bad component count "
4088 param.fp_check_comp_count = 1;
4089 param.fp_exclude_comp_count = !!neg_opt;
4091 case LFS_COMP_FLAGS_OPT:
4092 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4093 ¶m.fp_comp_neg_flags);
4095 fprintf(stderr, "error: bad component flags "
4099 param.fp_check_comp_flags = 1;
4101 __u32 flags = param.fp_comp_neg_flags;
4102 param.fp_comp_neg_flags = param.fp_comp_flags;
4103 param.fp_comp_flags = flags;
4106 case LFS_COMP_START_OPT:
4107 if (optarg[0] == '+') {
4108 param.fp_comp_start_sign = -1;
4110 } else if (optarg[0] == '-') {
4111 param.fp_comp_start_sign = 1;
4115 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4116 ¶m.fp_comp_start_units, 0);
4118 fprintf(stderr, "error: bad component start "
4122 param.fp_check_comp_start = 1;
4123 param.fp_exclude_comp_start = !!neg_opt;
4125 case LFS_MIRROR_STATE_OPT:
4126 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4127 ¶m.fp_mirror_neg_state);
4130 "error: bad mirrored file state '%s'\n",
4134 param.fp_check_mirror_state = 1;
4136 __u16 state = param.fp_mirror_neg_state;
4137 param.fp_mirror_neg_state =
4138 param.fp_mirror_state;
4139 param.fp_mirror_state = state;
4143 if (optarg[0] == '+') {
4144 param.fp_stripe_count_sign = -1;
4146 } else if (optarg[0] == '-') {
4147 param.fp_stripe_count_sign = 1;
4151 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4152 if (*endptr != '\0') {
4153 fprintf(stderr,"error: bad stripe_count '%s'\n",
4158 param.fp_check_stripe_count = 1;
4159 param.fp_exclude_stripe_count = !!neg_opt;
4162 param.fp_max_depth = strtol(optarg, 0, 0);
4165 if (optarg[0] == '+') {
4166 param.fp_comp_end_sign = -1;
4168 } else if (optarg[0] == '-') {
4169 param.fp_comp_end_sign = 1;
4173 if (arg_is_eof(optarg)) {
4174 param.fp_comp_end = LUSTRE_EOF;
4175 param.fp_comp_end_units = 1;
4178 rc = llapi_parse_size(optarg,
4180 ¶m.fp_comp_end_units, 0);
4183 fprintf(stderr, "error: bad component end "
4187 param.fp_check_comp_end = 1;
4188 param.fp_exclude_comp_end = !!neg_opt;
4190 case LFS_LAYOUT_FOREIGN_OPT: {
4191 /* all types by default */
4192 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4194 if (optarg != NULL) {
4195 /* check pure numeric */
4196 type = strtoul(optarg, &endptr, 0);
4199 type = check_foreign_type_name(optarg);
4200 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4202 "%s %s: unknown foreign type '%s'\n",
4209 param.fp_foreign_type = type;
4210 param.fp_check_foreign = 1;
4211 param.fp_exclude_foreign = !!neg_opt;
4216 rc = name2gid(¶m.fp_gid, optarg);
4218 param.fp_gid = strtoul(optarg, &endptr, 10);
4219 if (*endptr != '\0') {
4220 fprintf(stderr, "Group/GID: %s cannot "
4221 "be found.\n", optarg);
4226 param.fp_exclude_gid = !!neg_opt;
4227 param.fp_check_gid = 1;
4230 param.fp_hash_type = check_hashtype(optarg);
4231 if (param.fp_hash_type == 0) {
4232 fprintf(stderr, "error: bad hash_type '%s'\n",
4237 param.fp_check_hash_type = 1;
4238 param.fp_exclude_hash_type = !!neg_opt;
4241 ret = name2layout(¶m.fp_layout, optarg);
4244 param.fp_exclude_layout = !!neg_opt;
4245 param.fp_check_layout = 1;
4249 rc = name2uid(¶m.fp_uid, optarg);
4251 param.fp_uid = strtoul(optarg, &endptr, 10);
4252 if (*endptr != '\0') {
4253 fprintf(stderr, "User/UID: %s cannot "
4254 "be found.\n", optarg);
4259 param.fp_exclude_uid = !!neg_opt;
4260 param.fp_check_uid = 1;
4263 param.fp_pattern = (char *)optarg;
4264 param.fp_exclude_pattern = !!neg_opt;
4267 if (optarg[0] == '+') {
4268 param.fp_mirror_count_sign = -1;
4270 } else if (optarg[0] == '-') {
4271 param.fp_mirror_count_sign = 1;
4275 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4276 if (*endptr != '\0') {
4278 "error: bad mirror count '%s'\n",
4282 param.fp_check_mirror_count = 1;
4283 param.fp_exclude_mirror_count = !!neg_opt;
4288 char *buf, *token, *next, *p;
4292 buf = strdup(optarg);
4298 param.fp_exclude_obd = !!neg_opt;
4301 while (token && *token) {
4302 token = strchr(token, ',');
4309 param.fp_exclude_mdt = !!neg_opt;
4310 param.fp_num_alloc_mdts += len;
4311 tmp = realloc(param.fp_mdt_uuid,
4312 param.fp_num_alloc_mdts *
4313 sizeof(*param.fp_mdt_uuid));
4319 param.fp_mdt_uuid = tmp;
4321 param.fp_exclude_obd = !!neg_opt;
4322 param.fp_num_alloc_obds += len;
4323 tmp = realloc(param.fp_obd_uuid,
4324 param.fp_num_alloc_obds *
4325 sizeof(*param.fp_obd_uuid));
4331 param.fp_obd_uuid = tmp;
4333 for (token = buf; token && *token; token = next) {
4334 struct obd_uuid *puuid;
4337 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4340 ¶m.fp_obd_uuid[param.fp_num_obds++];
4342 p = strchr(token, ',');
4349 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4354 strncpy(puuid->uuid, token,
4355 sizeof(puuid->uuid));
4362 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4366 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4368 "Pool name %s is too long (max %d)\n",
4369 optarg, LOV_MAXPOOLNAME);
4374 * We do check for empty pool because empty pool
4375 * is used to find V1 LOV attributes
4377 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4378 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4379 param.fp_exclude_pool = !!neg_opt;
4380 param.fp_check_pool = 1;
4382 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4383 case 'p': /* want this for --pool, to match getstripe/find */
4385 "warning: -p deprecated, use --print0 or -0\n");
4388 param.fp_zero_end = 1;
4390 case 'P': /* we always print, this option is a no-op */
4392 case LFS_PROJID_OPT:
4393 rc = name2projid(¶m.fp_projid, optarg);
4395 param.fp_projid = strtoul(optarg, &endptr, 10);
4396 if (*endptr != '\0') {
4398 "Invalid project ID: %s",
4404 param.fp_exclude_projid = !!neg_opt;
4405 param.fp_check_projid = 1;
4408 if (optarg[0] == '+') {
4409 param.fp_size_sign = -1;
4411 } else if (optarg[0] == '-') {
4412 param.fp_size_sign = 1;
4416 ret = llapi_parse_size(optarg, ¶m.fp_size,
4417 ¶m.fp_size_units, 0);
4419 fprintf(stderr, "error: bad file size '%s'\n",
4423 param.fp_check_size = 1;
4424 param.fp_exclude_size = !!neg_opt;
4427 if (optarg[0] == '+') {
4428 param.fp_stripe_size_sign = -1;
4430 } else if (optarg[0] == '-') {
4431 param.fp_stripe_size_sign = 1;
4435 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4436 ¶m.fp_stripe_size_units, 0);
4438 fprintf(stderr, "error: bad stripe_size '%s'\n",
4442 param.fp_check_stripe_size = 1;
4443 param.fp_exclude_stripe_size = !!neg_opt;
4446 param.fp_exclude_type = !!neg_opt;
4447 switch (optarg[0]) {
4449 param.fp_type = S_IFBLK;
4452 param.fp_type = S_IFCHR;
4455 param.fp_type = S_IFDIR;
4458 param.fp_type = S_IFREG;
4461 param.fp_type = S_IFLNK;
4464 param.fp_type = S_IFIFO;
4467 param.fp_type = S_IFSOCK;
4470 fprintf(stderr, "error: %s: bad type '%s'\n",
4477 if (optarg[0] == '+') {
4478 param.fp_mdt_count_sign = -1;
4480 } else if (optarg[0] == '-') {
4481 param.fp_mdt_count_sign = 1;
4485 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4486 if (*endptr != '\0') {
4487 fprintf(stderr, "error: bad mdt_count '%s'\n",
4492 param.fp_check_mdt_count = 1;
4493 param.fp_exclude_mdt_count = !!neg_opt;
4496 if (optarg[0] == '+') {
4497 param.fp_ext_size_sign = -1;
4499 } else if (optarg[0] == '-') {
4500 param.fp_ext_size_sign = 1;
4504 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4505 ¶m.fp_ext_size_units, 0);
4507 fprintf(stderr, "error: bad ext-size '%s'\n",
4511 param.fp_ext_size /= SEL_UNIT_SIZE;
4512 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4513 param.fp_check_ext_size = 1;
4514 param.fp_exclude_ext_size = !!neg_opt;
4523 if (pathstart == -1) {
4524 fprintf(stderr, "error: %s: no filename|pathname\n",
4528 } else if (pathend == -1) {
4534 rc = llapi_find(argv[pathstart], ¶m);
4535 if (rc != 0 && ret == 0)
4537 } while (++pathstart < pathend);
4540 fprintf(stderr, "error: %s failed for %s.\n",
4541 argv[0], argv[optind - 1]);
4543 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4544 free(param.fp_obd_uuid);
4546 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4547 free(param.fp_mdt_uuid);
4552 static int lfs_getstripe_internal(int argc, char **argv,
4553 struct find_param *param)
4555 struct option long_opts[] = {
4556 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4557 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4558 { .val = LFS_COMP_COUNT_OPT,
4559 .name = "comp-count", .has_arg = no_argument },
4560 { .val = LFS_COMP_COUNT_OPT,
4561 .name = "component-count", .has_arg = no_argument },
4562 { .val = LFS_COMP_FLAGS_OPT,
4563 .name = "comp-flags", .has_arg = optional_argument },
4564 { .val = LFS_COMP_FLAGS_OPT,
4565 .name = "component-flags", .has_arg = optional_argument },
4566 { .val = LFS_COMP_START_OPT,
4567 .name = "comp-start", .has_arg = optional_argument },
4568 { .val = LFS_COMP_START_OPT,
4569 .name = "component-start", .has_arg = optional_argument },
4570 { .val = LFS_MIRROR_INDEX_OPT,
4571 .name = "mirror-index", .has_arg = required_argument },
4572 { .val = LFS_MIRROR_ID_OPT,
4573 .name = "mirror-id", .has_arg = required_argument },
4574 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4575 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4576 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4577 { .val = 'd', .name = "directory", .has_arg = no_argument },
4578 { .val = 'D', .name = "default", .has_arg = no_argument },
4579 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4580 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4581 { .val = 'F', .name = "fid", .has_arg = no_argument },
4582 { .val = 'g', .name = "generation", .has_arg = no_argument },
4583 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4584 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4585 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4586 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4587 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4588 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4589 { .val = 'L', .name = "layout", .has_arg = no_argument },
4590 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4591 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4592 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4593 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4594 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4595 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4596 { .val = 'O', .name = "obd", .has_arg = required_argument },
4597 { .val = 'O', .name = "ost", .has_arg = required_argument },
4598 { .val = 'p', .name = "pool", .has_arg = no_argument },
4599 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4600 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4601 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4602 { .val = 'R', .name = "raw", .has_arg = no_argument },
4603 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4604 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4605 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4606 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4607 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4608 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4609 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4610 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4611 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
4612 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
4616 int pathstart = -1, pathend = -1;
4620 while ((c = getopt_long(argc, argv,
4621 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
4622 long_opts, NULL)) != -1) {
4626 /* '!' is part of option */
4627 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4628 if (!isoption && pathend != -1) {
4630 "error: %s: filename|dirname must either precede options or follow options\n",
4634 if (!isoption && pathstart == -1)
4635 pathstart = optind - 1;
4636 if (isoption && pathstart != -1 && pathend == -1)
4637 pathend = optind - 2;
4641 /* unknown: opt is "!" */
4642 if (strcmp(optarg, "!") == 0)
4646 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4647 param->fp_verbose |= VERBOSE_COUNT;
4648 param->fp_max_depth = 0;
4651 case LFS_COMP_COUNT_OPT:
4652 param->fp_verbose |= VERBOSE_COMP_COUNT;
4653 param->fp_max_depth = 0;
4655 case LFS_COMP_FLAGS_OPT:
4656 if (optarg != NULL) {
4657 rc = comp_str2flags(optarg,
4658 ¶m->fp_comp_flags,
4659 ¶m->fp_comp_neg_flags);
4661 fprintf(stderr, "error: %s bad "
4662 "component flags '%s'.\n",
4666 param->fp_check_comp_flags = 1;
4668 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4669 param->fp_max_depth = 0;
4672 case LFS_COMP_START_OPT:
4673 if (optarg != NULL) {
4675 if (tmp[0] == '+') {
4676 param->fp_comp_start_sign = -1;
4678 } else if (tmp[0] == '-') {
4679 param->fp_comp_start_sign = 1;
4682 rc = llapi_parse_size(tmp,
4683 ¶m->fp_comp_start,
4684 ¶m->fp_comp_start_units, 0);
4686 fprintf(stderr, "error: %s bad "
4687 "component start '%s'.\n",
4691 param->fp_check_comp_start = 1;
4694 param->fp_verbose |= VERBOSE_COMP_START;
4695 param->fp_max_depth = 0;
4698 case LFS_MIRROR_INDEX_OPT:
4699 if (optarg[0] == '+') {
4700 param->fp_mirror_index_sign = -1;
4702 } else if (optarg[0] == '-') {
4703 param->fp_mirror_index_sign = 1;
4707 param->fp_mirror_index = strtoul(optarg, &end, 0);
4708 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4709 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4711 "%s %s: invalid mirror index '%s'\n",
4712 progname, argv[0], optarg);
4715 if (param->fp_mirror_id != 0) {
4717 "%s %s: can't specify both mirror index and mirror ID\n",
4721 param->fp_check_mirror_index = 1;
4722 param->fp_exclude_mirror_index = !!neg_opt;
4724 case LFS_MIRROR_ID_OPT:
4725 if (optarg[0] == '+') {
4726 param->fp_mirror_id_sign = -1;
4728 } else if (optarg[0] == '-') {
4729 param->fp_mirror_id_sign = 1;
4733 param->fp_mirror_id = strtoul(optarg, &end, 0);
4734 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4735 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4737 "%s %s: invalid mirror ID '%s'\n",
4738 progname, argv[0], optarg);
4741 if (param->fp_mirror_index != 0) {
4743 "%s %s: can't specify both mirror index and mirror ID\n",
4747 param->fp_check_mirror_id = 1;
4748 param->fp_exclude_mirror_id = !!neg_opt;
4751 param->fp_max_depth = 0;
4754 param->fp_get_default_lmv = 1;
4757 if (optarg != NULL) {
4759 if (tmp[0] == '+') {
4760 param->fp_comp_end_sign = -1;
4762 } else if (tmp[0] == '-') {
4763 param->fp_comp_end_sign = 1;
4767 if (arg_is_eof(tmp)) {
4768 param->fp_comp_end = LUSTRE_EOF;
4769 param->fp_comp_end_units = 1;
4772 rc = llapi_parse_size(tmp,
4773 ¶m->fp_comp_end,
4774 ¶m->fp_comp_end_units, 0);
4777 fprintf(stderr, "error: %s bad "
4778 "component end '%s'.\n",
4782 param->fp_check_comp_end = 1;
4784 param->fp_verbose |= VERBOSE_COMP_END;
4785 param->fp_max_depth = 0;
4789 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4790 param->fp_verbose |= VERBOSE_DFID;
4791 param->fp_max_depth = 0;
4795 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4796 param->fp_verbose |= VERBOSE_GENERATION;
4797 param->fp_max_depth = 0;
4801 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4802 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4803 param->fp_max_depth = 0;
4807 if (optarg != NULL) {
4808 param->fp_comp_id = strtoul(optarg, &end, 0);
4809 if (*end != '\0' || param->fp_comp_id == 0 ||
4810 param->fp_comp_id > LCME_ID_MAX) {
4811 fprintf(stderr, "error: %s bad "
4812 "component id '%s'\n",
4816 param->fp_check_comp_id = 1;
4819 param->fp_max_depth = 0;
4820 param->fp_verbose |= VERBOSE_COMP_ID;
4824 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4825 param->fp_verbose |= VERBOSE_PATTERN;
4826 param->fp_max_depth = 0;
4829 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4831 fprintf(stderr, "warning: '-M' deprecated"
4832 ", use '--mdt-index' or '-m' instead\n");
4835 if (!(param->fp_verbose & VERBOSE_DETAIL))
4836 param->fp_max_depth = 0;
4837 param->fp_verbose |= VERBOSE_MDTINDEX;
4840 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4841 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4842 param->fp_max_depth = 0;
4846 if (param->fp_obd_uuid) {
4848 "error: %s: only one obduuid allowed",
4852 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4855 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4856 param->fp_verbose |= VERBOSE_POOL;
4857 param->fp_max_depth = 0;
4864 param->fp_recursive = 1;
4870 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4871 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4872 param->fp_max_depth = 0;
4876 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4882 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4883 param->fp_verbose |= VERBOSE_EXT_SIZE;
4884 param->fp_max_depth = 0;
4892 if (pathstart == -1) {
4893 fprintf(stderr, "error: %s: no filename|pathname\n",
4896 } else if (pathend == -1) {
4904 if (param->fp_recursive)
4905 param->fp_max_depth = -1;
4906 else if (param->fp_verbose & VERBOSE_DETAIL)
4907 param->fp_max_depth = 1;
4909 if (!param->fp_verbose)
4910 param->fp_verbose = VERBOSE_DEFAULT;
4911 if (param->fp_quiet)
4912 param->fp_verbose = VERBOSE_OBJID;
4915 rc = llapi_getstripe(argv[pathstart], param);
4916 } while (++pathstart < pathend && !rc);
4919 fprintf(stderr, "error: %s failed for %s.\n",
4920 argv[0], argv[optind - 1]);
4924 static int lfs_tgts(int argc, char **argv)
4926 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4927 struct find_param param;
4928 int index = 0, rc=0;
4933 if (argc == 2 && !realpath(argv[1], path)) {
4935 fprintf(stderr, "error: invalid path '%s': %s\n",
4936 argv[1], strerror(-rc));
4940 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4941 /* Check if we have a mount point */
4942 if (mntdir[0] == '\0')
4945 memset(¶m, 0, sizeof(param));
4946 if (!strcmp(argv[0], "mdts"))
4947 param.fp_get_lmv = 1;
4949 rc = llapi_ostlist(mntdir, ¶m);
4951 fprintf(stderr, "error: %s: failed on %s\n",
4954 if (path[0] != '\0')
4956 memset(mntdir, 0, PATH_MAX);
4962 static int lfs_getstripe(int argc, char **argv)
4964 struct find_param param = { 0 };
4966 param.fp_max_depth = 1;
4967 return lfs_getstripe_internal(argc, argv, ¶m);
4971 static int lfs_getdirstripe(int argc, char **argv)
4973 struct find_param param = { 0 };
4974 struct option long_opts[] = {
4975 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4976 { .val = 'D', .name = "default", .has_arg = no_argument },
4977 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4978 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4979 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4980 { .val = 'O', .name = "obd", .has_arg = required_argument },
4981 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4982 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4983 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4984 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4988 param.fp_get_lmv = 1;
4990 while ((c = getopt_long(argc, argv,
4991 "cDHimO:rtTvy", long_opts, NULL)) != -1)
4996 param.fp_verbose |= VERBOSE_COUNT;
4999 param.fp_get_default_lmv = 1;
5003 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5005 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5007 fprintf(stderr, "warning: '-t' deprecated, "
5008 "use '--mdt-hash' or '-H' instead\n");
5011 param.fp_verbose |= VERBOSE_HASH_TYPE;
5014 if (param.fp_obd_uuid) {
5016 "error: %s: only one obduuid allowed",
5020 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5023 param.fp_recursive = 1;
5026 param.fp_verbose |= VERBOSE_DETAIL;
5032 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5033 progname, argv[0], argv[optind - 1]);
5041 if (param.fp_recursive)
5042 param.fp_max_depth = -1;
5044 if (!param.fp_verbose)
5045 param.fp_verbose = VERBOSE_DEFAULT;
5048 rc = llapi_getstripe(argv[optind], ¶m);
5049 } while (++optind < argc && !rc);
5052 fprintf(stderr, "error: %s failed for %s.\n",
5053 argv[0], argv[optind - 1]);
5058 MNTDF_INODES = 0x0001,
5059 MNTDF_COOKED = 0x0002,
5060 MNTDF_LAZY = 0x0004,
5061 MNTDF_VERBOSE = 0x0008,
5062 MNTDF_SHOW = 0x0010,
5065 #define COOK(value) \
5068 while (value > 1024) { \
5076 #define CDF "%11llu"
5077 #define HDF "%8.1f%c"
5081 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5083 double avail, used, ratio = 0;
5086 avail = st->os_ffree;
5087 used = st->os_files - st->os_ffree;
5089 avail = st->os_bavail;
5090 used = st->os_blocks - st->os_bfree;
5092 if (avail + used > 0)
5093 ratio = used / (used + avail) * 100;
5095 /* Round up to match df(1) usage percentage */
5096 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5099 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5100 * so only show flags reflecting those states by default. Informational states
5101 * are only shown with "-v" and use lower-case names to distinguish them.
5102 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5104 static struct obd_statfs_state_names {
5105 enum obd_statfs_state osn_state;
5106 const char osn_name;
5109 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
5110 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
5111 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
5112 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
5113 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
5114 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
5115 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
5116 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
5117 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
5120 static int showdf(char *mntdir, struct obd_statfs *stat,
5121 char *uuid, enum mntdf_flags flags,
5122 char *type, int index, int rc)
5124 long long avail, used, total;
5126 char *suffix = "KMGTPEZY";
5127 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5128 char tbuf[3 * sizeof(__u64)];
5129 char ubuf[3 * sizeof(__u64)];
5130 char abuf[3 * sizeof(__u64)];
5131 char rbuf[3 * sizeof(__u64)];
5138 if (flags & MNTDF_INODES) {
5139 avail = stat->os_ffree;
5140 used = stat->os_files - stat->os_ffree;
5141 total = stat->os_files;
5143 int shift = flags & MNTDF_COOKED ? 0 : 10;
5145 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5146 used = ((stat->os_blocks - stat->os_bfree) *
5147 stat->os_bsize) >> shift;
5148 total = (stat->os_blocks * stat->os_bsize) >> shift;
5151 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5153 if (flags & MNTDF_COOKED) {
5157 cook_val = (double)total;
5160 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5163 snprintf(tbuf, sizeof(tbuf), CDF, total);
5165 cook_val = (double)used;
5168 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5171 snprintf(ubuf, sizeof(ubuf), CDF, used);
5173 cook_val = (double)avail;
5176 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5179 snprintf(abuf, sizeof(abuf), CDF, avail);
5181 snprintf(tbuf, sizeof(tbuf), CDF, total);
5182 snprintf(ubuf, sizeof(tbuf), CDF, used);
5183 snprintf(abuf, sizeof(tbuf), CDF, avail);
5186 sprintf(rbuf, RDF, ratio);
5187 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5188 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5190 printf("[%s:%d]", type, index);
5192 if (stat->os_state) {
5196 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5197 if (oss_names[i].osn_state & stat->os_state &&
5198 (oss_names[i].osn_err ||
5199 flags & MNTDF_VERBOSE))
5200 printf("%c", oss_names[i].osn_name);
5207 printf(UUF": inactive device\n", uuid);
5210 printf(UUF": %s\n", uuid, strerror(-rc));
5217 struct ll_stat_type {
5222 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5224 struct ll_statfs_data {
5226 struct obd_statfs sd_st;
5229 struct ll_statfs_buf {
5231 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5234 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5235 int ops, struct ll_statfs_buf *lsb)
5237 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5238 struct obd_uuid uuid_buf;
5239 char *poolname = NULL;
5240 struct ll_stat_type types[] = {
5241 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5242 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5243 { .st_name = NULL } };
5244 struct ll_stat_type *tp;
5245 __u64 ost_files = 0;
5246 __u64 ost_ffree = 0;
5254 poolname = strchr(pool, '.');
5255 if (poolname != NULL) {
5256 if (strncmp(fsname, pool, strlen(fsname))) {
5257 fprintf(stderr, "filesystem name incorrect\n");
5265 fd = open(mntdir, O_RDONLY);
5268 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5273 if (flags & MNTDF_SHOW) {
5274 if (flags & MNTDF_INODES)
5275 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5276 "UUID", "Inodes", "IUsed", "IFree",
5277 "IUse%", "Mounted on");
5279 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5281 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5282 "Used", "Available", "Use%", "Mounted on");
5285 for (tp = types; tp->st_name != NULL; tp++) {
5286 bool have_ost = false;
5288 if (!(tp->st_op & ops))
5291 for (index = 0; ; index++) {
5292 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5293 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5294 type = flags & MNTDF_LAZY ?
5295 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5296 rc2 = llapi_obd_fstatfs(fd, type, index,
5297 &stat_buf, &uuid_buf);
5302 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5303 if (!(flags & MNTDF_VERBOSE))
5305 } else if (rc2 < 0 && rc == 0) {
5309 /* If we have OSTs then don't report MDT block counts.
5310 * For MDT-only filesystems the expectation is that all
5311 * layouts have a DoM component. For filesystems with
5312 * OSTs, files are not necessarily going to store data
5313 * on MDTs, and MDT space is limited to a fraction of
5314 * OST space, so don't include it in the summary.
5316 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5323 if (poolname && tp->st_op == LL_STATFS_LOV &&
5324 llapi_search_ost(fsname, poolname,
5325 obd_uuid2str(&uuid_buf)) != 1)
5328 /* the llapi_obd_statfs() call may have returned with
5329 * an error, but if it filled in uuid_buf we will at
5330 * lease use that to print out a message for that OBD.
5331 * If we didn't get anything in the uuid_buf, then fill
5332 * it in so that we can print an error message. */
5333 if (uuid_buf.uuid[0] == '\0')
5334 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5335 "%s%04x", tp->st_name, index);
5337 lsb->sb_buf[lsb->sb_count].sd_index = index;
5338 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5341 if (flags & MNTDF_SHOW)
5342 showdf(mntdir, &stat_buf,
5343 obd_uuid2str(&uuid_buf), flags,
5344 tp->st_name, index, rc2);
5349 if (tp->st_op == LL_STATFS_LMV) {
5350 sum.os_ffree += stat_buf.os_ffree;
5351 sum.os_files += stat_buf.os_files;
5352 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5353 ost_files += stat_buf.os_files;
5354 ost_ffree += stat_buf.os_ffree;
5356 sum.os_blocks += stat_buf.os_blocks *
5358 sum.os_bfree += stat_buf.os_bfree *
5360 sum.os_bavail += stat_buf.os_bavail *
5367 /* If we have _some_ OSTs, but don't have as many free objects on the
5368 * OST as inodes on the MDTs, reduce the reported number of inodes
5369 * to compensate, so that the "inodes in use" number is correct.
5370 * This should be kept in sync with ll_statfs_internal().
5372 if (ost_files && ost_ffree < sum.os_ffree) {
5373 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5374 sum.os_ffree = ost_ffree;
5376 if (flags & MNTDF_SHOW) {
5378 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5385 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5387 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5389 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5391 int r1 = obd_statfs_ratio(st1, false);
5392 int r2 = obd_statfs_ratio(st2, false);
5393 int64_t result = r1 - r2;
5395 /* if both space usage are above 90, compare free inodes */
5396 if (r1 > 90 && r2 > 90)
5397 result = st2->os_ffree - st1->os_ffree;
5401 else if (result == 0)
5408 static int lfs_setdirstripe(int argc, char **argv)
5411 struct lfs_setstripe_args lsa = { 0 };
5412 struct llapi_stripe_param *param = NULL;
5413 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5416 char *mode_opt = NULL;
5417 bool default_stripe = false;
5418 bool delete = false;
5419 bool auto_distributed = false;
5420 bool foreign_mode = false;
5421 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5422 mode_t previous_mode = 0;
5423 struct ll_statfs_buf *lsb = NULL;
5424 char mntdir[PATH_MAX] = "";
5426 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5427 struct option long_opts[] = {
5428 { .val = 'c', .name = "count", .has_arg = required_argument },
5429 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5430 { .val = 'd', .name = "delete", .has_arg = no_argument },
5431 { .val = 'D', .name = "default", .has_arg = no_argument },
5432 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5433 { .val = LFS_LAYOUT_FLAGS_OPT,
5434 .name = "flags", .has_arg = required_argument },
5435 { .val = LFS_LAYOUT_FOREIGN_OPT,
5436 .name = "foreign", .has_arg = optional_argument},
5437 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5438 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5439 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5440 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5442 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5443 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5445 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5446 { .val = 'i', .name = "index", .has_arg = required_argument },
5448 { .val = 'o', .name = "mode", .has_arg = required_argument },
5449 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5450 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5452 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5453 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5454 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5458 setstripe_args_init(&lsa);
5460 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5461 long_opts, NULL)) >= 0) {
5468 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5471 "%s %s: invalid stripe count '%s'\n",
5472 progname, argv[0], optarg);
5478 default_stripe = true;
5481 default_stripe = true;
5483 case LFS_LAYOUT_FOREIGN_OPT:
5484 if (optarg != NULL) {
5485 /* check pure numeric */
5486 type = strtoul(optarg, &end, 0);
5489 type = check_foreign_type_name(optarg);
5490 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5492 "%s %s: unknown foreign type '%s'\n",
5499 foreign_mode = true;
5501 case LFS_LAYOUT_FLAGS_OPT:
5502 flags = strtoul(optarg, &end, 16);
5505 "%s %s: bad flags '%s'\n",
5506 progname, argv[0], optarg);
5510 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5512 fprintf(stderr, "warning: '--hash-type' and '-t' "
5513 "deprecated, use '--mdt-hash' or '-H' instead\n");
5516 lsa.lsa_pattern = check_hashtype(optarg);
5517 if (lsa.lsa_pattern == 0) {
5519 "%s %s: bad stripe hash type '%s'\n",
5520 progname, argv[0], optarg);
5525 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5528 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5529 if (strcmp(argv[optind - 1], "--index") == 0)
5531 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5534 lsa.lsa_nr_tgts = parse_targets(mdts,
5535 sizeof(mdts) / sizeof(__u32),
5536 lsa.lsa_nr_tgts, optarg, NULL);
5537 if (lsa.lsa_nr_tgts < 0) {
5539 "%s %s: invalid MDT target(s) '%s'\n",
5540 progname, argv[0], optarg);
5544 lsa.lsa_tgts = mdts;
5545 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5546 lsa.lsa_stripe_off = mdts[0];
5548 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5550 fprintf(stderr, "warning: '-m' is deprecated, "
5551 "use '--mode' or '-o' instead\n");
5560 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5561 progname, argv[0], argv[optind - 1]);
5566 if (optind == argc) {
5567 fprintf(stderr, "%s %s: DIR must be specified\n",
5572 if (xattr && !foreign_mode) {
5573 /* only print a warning as this is armless and will be
5577 "%s %s: xattr has been specified for non-foreign layout\n",
5579 } else if (foreign_mode && !xattr) {
5581 "%s %s: xattr must be provided in foreign mode\n",
5586 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5587 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5589 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5594 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5595 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5597 "%s %s: stripe offset and count must be specified\n",
5603 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5604 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5606 "%s %s: cannot specify -d with -c or -i options\n",
5611 if (mode_opt != NULL) {
5612 mode = strtoul(mode_opt, &end, 8);
5615 "%s %s: bad MODE '%s'\n",
5616 progname, argv[0], mode_opt);
5619 previous_mode = umask(0);
5622 /* foreign LMV/dir case */
5624 if (argc > optind + 1) {
5626 "%s %s: cannot specify multiple foreign dirs\n",
5631 dname = argv[optind];
5632 result = llapi_dir_create_foreign(dname, mode, type, flags,
5636 "%s mkdir: can't create foreign dir '%s': %s\n",
5637 progname, dname, strerror(-result));
5642 * initialize stripe parameters, in case param is converted to specific,
5643 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5645 param = calloc(1, offsetof(typeof(*param),
5646 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5647 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5648 if (param == NULL) {
5650 "%s %s: cannot allocate memory for parameters: %s\n",
5651 progname, argv[0], strerror(ENOMEM));
5655 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5656 param->lsp_stripe_count = lsa.lsa_stripe_count;
5657 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5658 param->lsp_stripe_offset = -1;
5660 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5661 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5662 param->lsp_stripe_pattern = lsa.lsa_pattern;
5664 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
5665 param->lsp_pool = lsa.lsa_pool_name;
5666 param->lsp_is_specific = false;
5667 if (lsa.lsa_nr_tgts > 1) {
5668 if (lsa.lsa_stripe_count > 0 &&
5669 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5670 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5671 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5672 "match the number of MDTs: %d\n",
5673 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5678 param->lsp_is_specific = true;
5679 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5680 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5683 if (!default_stripe && (lsa.lsa_pattern & LMV_HASH_FLAG_SPACE)) {
5684 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5690 if (param->lsp_stripe_offset != -1 &&
5691 lsa.lsa_pattern & LMV_HASH_FLAG_SPACE) {
5692 fprintf(stderr, "%s %s: can only specify -H space with -i -1\n",
5698 dname = argv[optind];
5700 if (default_stripe) {
5701 result = llapi_dir_set_default_lmv(dname, param);
5704 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5705 progname, dname, strerror(-result));
5710 * if current \a dname isn't under the same \a mntdir as the
5711 * last one, and the last one was auto-distributed, restore
5714 if (mntdir[0] != '\0' &&
5715 strncmp(dname, mntdir, strlen(mntdir)) &&
5717 param->lsp_is_specific = false;
5718 param->lsp_stripe_offset = -1;
5719 auto_distributed = false;
5723 * TODO: when MDT can allocate object with QoS (LU-9435), below
5724 * code should be removed, instead we should let LMV to allocate
5725 * the starting MDT object, and then let LOD allocate other MDT
5728 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5729 char path[PATH_MAX] = "";
5732 lsb = malloc(sizeof(*lsb));
5740 /* use mntdir for dirname() temporarily */
5741 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5742 if (!realpath(dirname(mntdir), path)) {
5745 "error: invalid path '%s': %s\n",
5746 argv[optind], strerror(errno));
5751 result = llapi_search_mounts(path, 0, mntdir, NULL);
5752 if (result < 0 || mntdir[0] == '\0') {
5754 "No suitable Lustre mount found\n");
5758 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5763 if (param->lsp_stripe_count > lsb->sb_count) {
5765 "error: stripe count %d is too big\n",
5766 param->lsp_stripe_count);
5771 qsort(lsb->sb_buf, lsb->sb_count,
5772 sizeof(struct ll_statfs_data),
5773 ll_statfs_data_comp);
5775 auto_distributed = true;
5778 if (auto_distributed) {
5780 int nr = MAX(param->lsp_stripe_count,
5783 /* don't use server whose usage is above 90% */
5784 while (nr != param->lsp_stripe_count &&
5785 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5787 nr = MAX(param->lsp_stripe_count, nr / 2);
5789 /* get \a r between [0, nr) */
5792 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5793 if (param->lsp_stripe_count > 1) {
5796 param->lsp_is_specific = true;
5797 for (; i < param->lsp_stripe_count; i++)
5798 param->lsp_tgts[(i + r) % nr] =
5799 lsb->sb_buf[i].sd_index;
5803 result = llapi_dir_create(dname, mode, param);
5806 "%s setdirstripe: cannot create dir '%s': %s\n",
5807 progname, dname, strerror(-result));
5808 } while (!result && (dname = argv[++optind]));
5810 if (mode_opt != NULL)
5811 umask(previous_mode);
5819 static int lfs_rmentry(int argc, char **argv)
5826 fprintf(stderr, "error: %s: missing dirname\n",
5832 dname = argv[index];
5833 while (dname != NULL) {
5834 result = llapi_direntry_remove(dname);
5836 fprintf(stderr, "error: %s: remove dir entry '%s' "
5837 "failed\n", argv[0], dname);
5840 dname = argv[++index];
5845 static int lfs_mv(int argc, char **argv)
5847 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5848 struct find_param param = {
5855 struct option long_opts[] = {
5856 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5857 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5858 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5861 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5863 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5865 fprintf(stderr, "warning: '-M' deprecated"
5866 ", use '--mdt-index' or '-m' instead\n");
5869 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5871 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5877 param.fp_verbose = VERBOSE_DETAIL;
5880 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5881 progname, argv[optind - 1]);
5886 if (lmu.lum_stripe_offset == -1) {
5887 fprintf(stderr, "%s mv: MDT index must be specified\n",
5892 if (optind >= argc) {
5893 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5898 /* initialize migrate mdt parameters */
5899 param.fp_lmv_md = &lmu;
5900 param.fp_migrate = 1;
5901 rc = llapi_migrate_mdt(argv[optind], ¶m);
5903 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5904 progname, argv[optind], param.fp_mdt_index,
5909 static int lfs_osts(int argc, char **argv)
5911 return lfs_tgts(argc, argv);
5914 static int lfs_mdts(int argc, char **argv)
5916 return lfs_tgts(argc, argv);
5919 static int lfs_df(int argc, char **argv)
5921 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5922 enum mntdf_flags flags = MNTDF_SHOW;
5923 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5924 int c, rc = 0, index = 0;
5925 char fsname[PATH_MAX] = "", *pool_name = NULL;
5926 struct option long_opts[] = {
5927 { .val = 'h', .name = "human-readable",
5928 .has_arg = no_argument },
5929 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5930 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5931 { .val = 'p', .name = "pool", .has_arg = required_argument },
5932 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5935 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5938 flags |= MNTDF_COOKED;
5941 flags |= MNTDF_INODES;
5944 flags |= MNTDF_LAZY;
5950 flags |= MNTDF_VERBOSE;
5956 if (optind < argc && !realpath(argv[optind], path)) {
5958 fprintf(stderr, "error: invalid path '%s': %s\n",
5959 argv[optind], strerror(-rc));
5963 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5964 /* Check if we have a mount point */
5965 if (mntdir[0] == '\0')
5968 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5969 if (rc || path[0] != '\0')
5971 fsname[0] = '\0'; /* avoid matching in next loop */
5972 mntdir[0] = '\0'; /* avoid matching in next loop */
5978 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5979 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5983 /* llapi_search_mounts() fills "fsname", but that is not called if
5984 * explicit paths are specified on the command-line
5986 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5987 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5990 fprintf(stderr, "cannot get instance for '%s': %s\n",
5991 mntdir, strerror(-rc));
5995 if (!opt_instance) {
5996 /* print only the fsname name */
5997 buf = strchr(fsname, '-');
6001 } else if (!opt_fsname) {
6002 /* print only the instance name */
6003 buf = strchr(fsname, '-');
6009 } else if (opt_fsname) {
6010 /* print only the fsname */
6014 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
6019 static int lfs_getname(int argc, char **argv)
6021 struct option long_opts[] = {
6022 { .val = 'h', .name = "help", .has_arg = no_argument },
6023 { .val = 'i', .name = "instance", .has_arg = no_argument },
6024 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6026 bool opt_instance = false, opt_fsname = false;
6027 char fsname[PATH_MAX] = "";
6030 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6033 opt_instance = true;
6044 /* If neither option is given, print both instance and fsname */
6045 if (!opt_instance && !opt_fsname)
6046 opt_instance = opt_fsname = true;
6048 if (optind == argc) { /* no paths specified, get all paths. */
6049 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6052 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6053 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6054 opt_instance, opt_fsname, true);
6057 path[0] = fsname[0] = mntdir[0] = '\0';
6059 } else { /* paths specified, only attempt to search these. */
6062 /* if only one path is given, print only requested info */
6063 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6065 for (; optind < argc; optind++) {
6066 rc2 = print_instance(argv[optind], fsname,
6067 sizeof(fsname), opt_instance,
6068 opt_fsname, opt_mntdir);
6078 static int lfs_check(int argc, char **argv)
6080 char mntdir[PATH_MAX] = {'\0'};
6089 fprintf(stderr, "%s check: server type must be specified\n",
6094 obd_types[0] = obd_type1;
6095 obd_types[1] = obd_type2;
6096 obd_types[2] = obd_type3;
6098 if (strcmp(argv[1], "osts") == 0) {
6099 strcpy(obd_types[0], "osc");
6100 } else if (strcmp(argv[1], "mdts") == 0 ||
6101 strcmp(argv[1], "mds") == 0) {
6102 strcpy(obd_types[0], "mdc");
6103 } else if (strcmp(argv[1], "mgts") == 0) {
6104 strcpy(obd_types[0], "mgc");
6105 } else if (strcmp(argv[1], "all") == 0 ||
6106 strcmp(argv[1], "servers") == 0) {
6108 strcpy(obd_types[0], "osc");
6109 strcpy(obd_types[1], "mdc");
6110 strcpy(obd_types[2], "mgc");
6112 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6117 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6118 if (rc < 0 || mntdir[0] == '\0') {
6120 "%s check: cannot find mounted Lustre filesystem: %s\n",
6121 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6125 rc = llapi_target_check(num_types, obd_types, mntdir);
6127 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6128 progname, argv[1], strerror(-rc));
6134 #ifdef HAVE_SYS_QUOTA_H
6135 #define ARG2INT(nr, str, msg) \
6138 nr = strtol(str, &endp, 0); \
6139 if (*endp != '\0') { \
6140 fprintf(stderr, "%s: bad %s '%s'\n", \
6141 progname, msg, str); \
6146 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6148 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6149 * returns the value or ULONG_MAX on integer overflow or incorrect format
6151 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6152 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6153 * 3. empty integer value is interpreted as 0
6155 static unsigned long str2sec(const char* timestr)
6157 const char spec[] = "smhdw";
6158 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6159 unsigned long val = 0;
6162 if (strpbrk(timestr, spec) == NULL) {
6163 /* no specifiers inside the time string,
6164 should treat it as an integer value */
6165 val = strtoul(timestr, &tail, 10);
6166 return *tail ? ULONG_MAX : val;
6169 /* format string is XXwXXdXXhXXmXXs */
6175 v = strtoul(timestr, &tail, 10);
6176 if (v == ULONG_MAX || *tail == '\0')
6177 /* value too large (ULONG_MAX or more)
6178 or missing specifier */
6181 ptr = strchr(spec, *tail);
6183 /* unknown specifier */
6188 /* check if product will overflow the type */
6189 if (!(v < ULONG_MAX / mult[ind]))
6192 ADD_OVERFLOW(val, mult[ind] * v);
6193 if (val == ULONG_MAX)
6205 #define ARG2ULL(nr, str, def_units) \
6207 unsigned long long limit, units = def_units; \
6210 rc = llapi_parse_size(str, &limit, &units, 1); \
6212 fprintf(stderr, "%s: invalid limit '%s'\n", \
6219 static inline int has_times_option(int argc, char **argv)
6223 for (i = 1; i < argc; i++)
6224 if (!strcmp(argv[i], "-t"))
6230 int lfs_setquota_times(int argc, char **argv)
6233 struct if_quotactl qctl;
6234 char *mnt, *obd_type = (char *)qctl.obd_type;
6235 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6236 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6237 struct option long_opts[] = {
6238 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6239 { .val = 'g', .name = "group", .has_arg = no_argument },
6240 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6241 { .val = 'p', .name = "projid", .has_arg = no_argument },
6242 { .val = 't', .name = "times", .has_arg = no_argument },
6243 { .val = 'u', .name = "user", .has_arg = no_argument },
6247 memset(&qctl, 0, sizeof(qctl));
6248 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6249 qctl.qc_type = ALLQUOTA;
6251 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6252 long_opts, NULL)) != -1) {
6263 if (qctl.qc_type != ALLQUOTA) {
6264 fprintf(stderr, "error: -u/g/p can't be used "
6265 "more than once\n");
6268 qctl.qc_type = qtype;
6271 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6272 fprintf(stderr, "error: bad block-grace: %s\n",
6276 dqb->dqb_valid |= QIF_BTIME;
6279 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6280 fprintf(stderr, "error: bad inode-grace: %s\n",
6284 dqb->dqb_valid |= QIF_ITIME;
6286 case 't': /* Yes, of course! */
6288 default: /* getopt prints error message for us when opterr != 0 */
6293 if (qctl.qc_type == ALLQUOTA) {
6294 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6298 if (optind != argc - 1) {
6299 fprintf(stderr, "error: unexpected parameters encountered\n");
6303 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6304 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6305 fprintf(stderr, "error: grace time is too large\n");
6310 rc = llapi_quotactl(mnt, &qctl);
6313 fprintf(stderr, "%s %s ", obd_type,
6314 obd_uuid2str(&qctl.obd_uuid));
6315 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6322 #define BSLIMIT (1 << 0)
6323 #define BHLIMIT (1 << 1)
6324 #define ISLIMIT (1 << 2)
6325 #define IHLIMIT (1 << 3)
6327 int lfs_setquota(int argc, char **argv)
6330 struct if_quotactl qctl;
6331 char *mnt, *obd_type = (char *)qctl.obd_type;
6332 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6333 struct option long_opts[] = {
6334 { .val = 'b', .name = "block-softlimit",
6335 .has_arg = required_argument },
6336 { .val = 'B', .name = "block-hardlimit",
6337 .has_arg = required_argument },
6338 { .val = 'd', .name = "default", .has_arg = no_argument },
6339 { .val = 'g', .name = "group", .has_arg = required_argument },
6340 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6341 { .val = 'i', .name = "inode-softlimit",
6342 .has_arg = required_argument },
6343 { .val = 'I', .name = "inode-hardlimit",
6344 .has_arg = required_argument },
6345 { .val = 'p', .name = "projid", .has_arg = required_argument },
6346 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6347 { .val = 'u', .name = "user", .has_arg = required_argument },
6348 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6350 unsigned limit_mask = 0;
6352 bool use_default = false;
6355 if (has_times_option(argc, argv))
6356 return lfs_setquota_times(argc, argv);
6358 memset(&qctl, 0, sizeof(qctl));
6359 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6360 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6361 * so it can be used as a marker that qc_type
6362 * isn't reinitialized from command line */
6364 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6365 long_opts, NULL)) != -1) {
6368 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6371 goto quota_type_def;
6374 rc = name2uid(&qctl.qc_id, optarg);
6377 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6380 goto quota_type_def;
6383 rc = name2gid(&qctl.qc_id, optarg);
6386 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6389 goto quota_type_def;
6392 rc = name2projid(&qctl.qc_id, optarg);
6395 qctl.qc_id = strtoul(optarg, &endptr, 10);
6396 if (*endptr != '\0') {
6397 fprintf(stderr, "%s setquota: invalid"
6398 " id '%s'\n", progname, optarg);
6403 if (qctl.qc_id == 0) {
6404 fprintf(stderr, "%s setquota: can't set quota"
6405 " for root usr/group/project.\n",
6411 if (qctl.qc_type != ALLQUOTA) {
6413 "%s setquota: only one of -u, -U, -g,"
6414 " -G, -p or -P may be specified\n",
6418 qctl.qc_type = qtype;
6421 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6425 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6426 dqb->dqb_bsoftlimit >>= 10;
6427 limit_mask |= BSLIMIT;
6428 if (dqb->dqb_bsoftlimit &&
6429 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6431 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6432 "See '%s help setquota' or Lustre manual for details\n",
6433 progname, dqb->dqb_bsoftlimit,
6437 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6438 dqb->dqb_bhardlimit >>= 10;
6439 limit_mask |= BHLIMIT;
6440 if (dqb->dqb_bhardlimit &&
6441 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6443 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6444 "See '%s help setquota' or Lustre manual for details\n",
6445 progname, dqb->dqb_bhardlimit,
6449 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6450 limit_mask |= ISLIMIT;
6451 if (dqb->dqb_isoftlimit &&
6452 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6454 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6455 "See '%s help setquota' or Lustre manual for details\n",
6456 progname, dqb->dqb_isoftlimit,
6460 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6461 limit_mask |= IHLIMIT;
6462 if (dqb->dqb_ihardlimit &&
6463 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6465 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6466 "See '%s help setquota' or Lustre manual for details\n",
6467 progname, dqb->dqb_ihardlimit,
6472 "%s setquota: unrecognized option '%s'\n",
6473 progname, argv[optind - 1]);
6478 if (qctl.qc_type == ALLQUOTA) {
6480 "%s setquota: either -u or -g must be specified\n",
6485 if (!use_default && limit_mask == 0) {
6487 "%s setquota: at least one limit must be specified\n",
6492 if (use_default && limit_mask != 0) {
6494 "%s setquota: limits should not be specified when"
6495 " using default quota\n",
6500 if (use_default && qctl.qc_id == 0) {
6502 "%s setquota: can not set default quota for root"
6503 " user/group/project\n",
6508 if (optind != argc - 1) {
6510 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6511 progname, argv[optind]);
6518 dqb->dqb_bhardlimit = 0;
6519 dqb->dqb_bsoftlimit = 0;
6520 dqb->dqb_ihardlimit = 0;
6521 dqb->dqb_isoftlimit = 0;
6524 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6525 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6526 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6527 /* sigh, we can't just set blimits/ilimits */
6528 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6529 .qc_type = qctl.qc_type,
6530 .qc_id = qctl.qc_id};
6532 rc = llapi_quotactl(mnt, &tmp_qctl);
6536 if (!(limit_mask & BHLIMIT))
6537 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6538 if (!(limit_mask & BSLIMIT))
6539 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6540 if (!(limit_mask & IHLIMIT))
6541 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6542 if (!(limit_mask & ISLIMIT))
6543 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6545 /* Keep grace times if we have got no softlimit arguments */
6546 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6547 dqb->dqb_valid |= QIF_BTIME;
6548 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6551 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6552 dqb->dqb_valid |= QIF_ITIME;
6553 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6557 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6558 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6560 rc = llapi_quotactl(mnt, &qctl);
6564 "%s setquota: cannot quotactl '%s' '%s': %s",
6566 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6573 /* Converts seconds value into format string
6574 * result is returned in buf
6576 * 1. result is in descenting order: 1w2d3h4m5s
6577 * 2. zero fields are not filled (except for p. 3): 5d1s
6578 * 3. zero seconds value is presented as "0s"
6580 static char * __sec2str(time_t seconds, char *buf)
6582 const char spec[] = "smhdw";
6583 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6588 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6589 c = seconds / mult[i];
6591 if (c > 0 || (i == 0 && buf == tail))
6592 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6600 static void sec2str(time_t seconds, char *buf, int rc)
6607 tail = __sec2str(seconds, tail);
6609 if (rc && tail - buf < 39) {
6615 static void diff2str(time_t seconds, char *buf, time_t now)
6621 if (seconds <= now) {
6622 strcpy(buf, "none");
6625 __sec2str(seconds - now, buf);
6628 static void print_quota_title(char *name, struct if_quotactl *qctl,
6629 bool human_readable, bool show_default)
6632 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6633 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6634 "Filesystem", "bquota", "blimit", "bgrace",
6635 "iquota", "ilimit", "igrace");
6637 printf("Disk quotas for %s %s (%cid %u):\n",
6638 qtype_name(qctl->qc_type), name,
6639 *qtype_name(qctl->qc_type), qctl->qc_id);
6640 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6641 "Filesystem", human_readable ? "used" : "kbytes",
6642 "quota", "limit", "grace",
6643 "files", "quota", "limit", "grace");
6647 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6650 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6653 snprintf(buf, buflen, "%5.4gP",
6654 (double)num / ((__u64)1 << 40));
6656 snprintf(buf, buflen, "%5.4gT",
6657 (double)num / (1 << 30));
6659 snprintf(buf, buflen, "%5.4gG",
6660 (double)num / (1 << 20));
6662 snprintf(buf, buflen, "%5.4gM",
6663 (double)num / (1 << 10));
6665 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6669 #define STRBUF_LEN 32
6670 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6671 int rc, bool h, bool show_default)
6677 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6678 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6679 int bover = 0, iover = 0;
6680 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6681 char numbuf[3][STRBUF_LEN];
6683 char strbuf[STRBUF_LEN];
6685 if (dqb->dqb_bhardlimit &&
6686 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6688 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6689 if (dqb->dqb_btime > now) {
6696 if (dqb->dqb_ihardlimit &&
6697 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6699 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6700 if (dqb->dqb_itime > now) {
6708 if (strlen(mnt) > 15)
6709 printf("%s\n%15s", mnt, "");
6711 printf("%15s", mnt);
6714 diff2str(dqb->dqb_btime, timebuf, now);
6715 else if (show_default)
6716 snprintf(timebuf, sizeof(timebuf), "%llu",
6719 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6720 strbuf, sizeof(strbuf), h);
6721 if (rc == -EREMOTEIO)
6722 sprintf(numbuf[0], "%s*", strbuf);
6724 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6725 "%s" : "[%s]", strbuf);
6727 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6728 if (type == QC_GENERAL)
6729 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6730 "%s" : "[%s]", strbuf);
6732 sprintf(numbuf[1], "%s", "-");
6734 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6735 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6736 "%s" : "[%s]", strbuf);
6739 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6741 printf(" %7s%c %6s %7s %7s",
6742 numbuf[0], bover ? '*' : ' ', numbuf[1],
6743 numbuf[2], bover > 1 ? timebuf : "-");
6747 diff2str(dqb->dqb_itime, timebuf, now);
6748 else if (show_default)
6749 snprintf(timebuf, sizeof(timebuf), "%llu",
6752 snprintf(numbuf[0], sizeof(numbuf),
6753 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6754 (uintmax_t)dqb->dqb_curinodes);
6756 if (type == QC_GENERAL)
6757 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6759 (uintmax_t)dqb->dqb_isoftlimit);
6761 sprintf(numbuf[1], "%s", "-");
6763 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6764 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6767 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6768 else if (type != QC_OSTIDX)
6769 printf(" %7s%c %6s %7s %7s",
6770 numbuf[0], iover ? '*' : ' ', numbuf[1],
6771 numbuf[2], iover > 1 ? timebuf : "-");
6773 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6776 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6777 qctl->qc_cmd == Q_GETOINFO) {
6781 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6782 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6783 printf("Block grace time: %s; Inode grace time: %s\n",
6784 bgtimebuf, igtimebuf);
6788 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6789 bool h, __u64 *total)
6791 int rc = 0, rc1 = 0, count = 0;
6792 __u32 valid = qctl->qc_valid;
6794 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6796 fprintf(stderr, "can not get %s count: %s\n",
6797 is_mdt ? "mdt": "ost", strerror(-rc));
6801 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6802 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6803 rc = llapi_quotactl(mnt, qctl);
6805 /* It is remote client case. */
6806 if (rc == -EOPNOTSUPP) {
6813 fprintf(stderr, "quotactl %s%d failed.\n",
6814 is_mdt ? "mdt": "ost", qctl->qc_idx);
6818 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6819 qctl->qc_valid, 0, h, false);
6820 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6821 qctl->qc_dqblk.dqb_bhardlimit;
6824 qctl->qc_valid = valid;
6828 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6829 int verbose, int quiet, bool human_readable,
6832 int rc1 = 0, rc2 = 0, rc3 = 0;
6833 char *obd_type = (char *)qctl->obd_type;
6834 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6835 __u64 total_ialloc = 0, total_balloc = 0;
6836 bool use_default_for_blk = false;
6837 bool use_default_for_file = false;
6840 rc1 = llapi_quotactl(mnt, qctl);
6844 fprintf(stderr, "%s quotas are not enabled.\n",
6845 qtype_name(qctl->qc_type));
6848 fprintf(stderr, "Permission denied.\n");
6851 /* We already got error message. */
6854 fprintf(stderr, "Unexpected quotactl error: %s\n",
6859 if (!show_default && qctl->qc_id == 0) {
6860 qctl->qc_dqblk.dqb_bhardlimit = 0;
6861 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6862 qctl->qc_dqblk.dqb_ihardlimit = 0;
6863 qctl->qc_dqblk.dqb_isoftlimit = 0;
6864 qctl->qc_dqblk.dqb_btime = 0;
6865 qctl->qc_dqblk.dqb_itime = 0;
6866 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6869 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6870 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6871 use_default_for_blk = true;
6872 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6875 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6876 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6877 use_default_for_file = true;
6878 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6881 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6882 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6883 print_quota_title(name, qctl, human_readable, show_default);
6885 if (rc1 && *obd_type)
6886 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6888 if (qctl->qc_valid != QC_GENERAL)
6891 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6892 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6893 (QIF_LIMITS|QIF_USAGE));
6895 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6897 if (!show_default && verbose &&
6898 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6899 char strbuf[STRBUF_LEN];
6901 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6903 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6905 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6907 printf("Total allocated inode limit: %ju, total "
6908 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6912 if (use_default_for_blk)
6913 printf("%cid %u is using default block quota setting\n",
6914 *qtype_name(qctl->qc_type), qctl->qc_id);
6916 if (use_default_for_file)
6917 printf("%cid %u is using default file quota setting\n",
6918 *qtype_name(qctl->qc_type), qctl->qc_id);
6920 if (rc1 || rc2 || rc3 || inacc)
6921 printf("Some errors happened when getting quota info. "
6922 "Some devices may be not working or deactivated. "
6923 "The data in \"[]\" is inaccurate.\n");
6929 static int lfs_project(int argc, char **argv)
6931 int ret = 0, err = 0, c, i;
6932 struct project_handle_control phc = { 0 };
6933 enum lfs_project_ops_t op;
6936 phc.assign_projid = false;
6937 /* default action */
6938 op = LFS_PROJECT_LIST;
6940 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6943 if (op != LFS_PROJECT_LIST) {
6945 "%s: cannot specify '-c' '-C' '-s' together\n",
6950 op = LFS_PROJECT_CHECK;
6953 if (op != LFS_PROJECT_LIST) {
6955 "%s: cannot specify '-c' '-C' '-s' together\n",
6960 op = LFS_PROJECT_CLEAR;
6963 if (op != LFS_PROJECT_LIST) {
6965 "%s: cannot specify '-c' '-C' '-s' together\n",
6970 phc.set_inherit = true;
6971 op = LFS_PROJECT_SET;
6977 phc.keep_projid = true;
6980 phc.recursive = true;
6983 phc.projid = strtoul(optarg, NULL, 0);
6984 phc.assign_projid = true;
6988 phc.newline = false;
6991 fprintf(stderr, "%s: invalid option '%c'\n",
6997 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6998 op = LFS_PROJECT_SET;
6999 phc.set_projid = true;
7000 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7001 phc.set_projid = true;
7005 case LFS_PROJECT_CHECK:
7006 if (phc.keep_projid) {
7008 "%s: '-k' is useless together with '-c'\n",
7013 case LFS_PROJECT_CLEAR:
7016 "%s: '-0' is useless together with '-C'\n",
7020 if (phc.assign_projid) {
7022 "%s: '-p' is useless together with '-C'\n",
7027 case LFS_PROJECT_SET:
7030 "%s: '-0' is useless together with '-s'\n",
7034 if (phc.keep_projid) {
7036 "%s: '-k' is useless together with '-s'\n",
7044 "%s: '-0' is useless for list operations\n",
7054 fprintf(stderr, "%s: missing file or directory target(s)\n",
7059 for (i = 0; i < argc; i++) {
7061 case LFS_PROJECT_CHECK:
7062 err = lfs_project_check(argv[i], &phc);
7064 case LFS_PROJECT_LIST:
7065 err = lfs_project_list(argv[i], &phc);
7067 case LFS_PROJECT_CLEAR:
7068 err = lfs_project_clear(argv[i], &phc);
7070 case LFS_PROJECT_SET:
7071 err = lfs_project_set(argv[i], &phc);
7083 static int lfs_quota(int argc, char **argv)
7086 char *mnt, *name = NULL;
7087 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
7088 .qc_type = ALLQUOTA };
7089 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
7090 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7092 __u32 valid = QC_GENERAL, idx = 0;
7093 bool human_readable = false;
7094 bool show_default = false;
7097 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
7100 show_default = true;
7105 show_default = true;
7110 show_default = true;
7114 if (qctl.qc_type != ALLQUOTA) {
7116 "%s quota: only one of -u, -g, or -p may be specified\n",
7120 qctl.qc_type = qtype;
7123 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7126 valid = qctl.qc_valid = QC_UUID;
7127 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7130 valid = qctl.qc_valid = QC_MDTIDX;
7131 idx = qctl.qc_idx = atoi(optarg);
7132 if (idx == 0 && *optarg != '0') {
7134 "%s quota: invalid MDT index '%s'\n",
7140 valid = qctl.qc_valid = QC_OSTIDX;
7141 idx = qctl.qc_idx = atoi(optarg);
7142 if (idx == 0 && *optarg != '0') {
7144 "%s quota: invalid OST index '%s'\n",
7156 human_readable = true;
7159 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7160 progname, argv[optind - 1]);
7165 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7166 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7167 optind == argc - 1 && !show_default) {
7169 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7170 qctl.qc_valid = valid;
7173 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7174 qctl.qc_type = qtype;
7175 if (qtype == USRQUOTA) {
7176 qctl.qc_id = geteuid();
7177 rc = uid2name(&name, qctl.qc_id);
7179 qctl.qc_id = getegid();
7180 rc = gid2name(&name, qctl.qc_id);
7185 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7186 human_readable, show_default);
7191 /* lfs quota -u username /path/to/lustre/mount */
7192 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7193 /* options should be followed by u/g-name and mntpoint */
7194 if ((!show_default && optind + 2 != argc) ||
7195 (show_default && optind + 1 != argc) ||
7196 qctl.qc_type == ALLQUOTA) {
7198 "%s quota: name and mount point must be specified\n",
7203 if (!show_default) {
7204 name = argv[optind++];
7205 switch (qctl.qc_type) {
7207 rc = name2uid(&qctl.qc_id, name);
7210 rc = name2gid(&qctl.qc_id, name);
7213 rc = name2projid(&qctl.qc_id, name);
7220 qctl.qc_valid = QC_GENERAL;
7221 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7226 qctl.qc_id = strtoul(name, &endptr, 10);
7227 if (*endptr != '\0') {
7228 fprintf(stderr, "%s quota: invalid id '%s'\n",
7233 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7234 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7240 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7241 human_readable, show_default);
7244 #endif /* HAVE_SYS_QUOTA_H! */
7246 static int flushctx_ioctl(char *mp)
7250 fd = open(mp, O_RDONLY);
7252 fprintf(stderr, "flushctx: error open %s: %s\n",
7253 mp, strerror(errno));
7257 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7259 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7260 mp, strerror(errno));
7266 static int lfs_flushctx(int argc, char **argv)
7268 int kdestroy = 0, c;
7269 char mntdir[PATH_MAX] = {'\0'};
7273 while ((c = getopt(argc, argv, "k")) != -1) {
7279 fprintf(stderr, "error: %s: option '-%c' "
7280 "unrecognized\n", argv[0], c);
7286 if ((rc = system("kdestroy > /dev/null")) != 0) {
7287 rc = WEXITSTATUS(rc);
7288 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7292 if (optind >= argc) {
7293 /* flush for all mounted lustre fs. */
7294 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7295 /* Check if we have a mount point */
7296 if (mntdir[0] == '\0')
7299 if (flushctx_ioctl(mntdir))
7302 mntdir[0] = '\0'; /* avoid matching in next loop */
7305 /* flush fs as specified */
7306 while (optind < argc) {
7307 if (flushctx_ioctl(argv[optind++]))
7314 static int lfs_changelog(int argc, char **argv)
7316 void *changelog_priv;
7317 struct changelog_rec *rec;
7318 long long startrec = 0, endrec = 0;
7320 struct option long_opts[] = {
7321 { .val = 'f', .name = "follow", .has_arg = no_argument },
7323 char short_opts[] = "f";
7326 while ((rc = getopt_long(argc, argv, short_opts,
7327 long_opts, NULL)) != -1) {
7334 "%s changelog: unrecognized option '%s'\n",
7335 progname, argv[optind - 1]);
7339 if (optind >= argc) {
7340 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7345 mdd = argv[optind++];
7347 startrec = strtoll(argv[optind++], NULL, 10);
7349 endrec = strtoll(argv[optind++], NULL, 10);
7351 rc = llapi_changelog_start(&changelog_priv,
7352 CHANGELOG_FLAG_BLOCK |
7353 CHANGELOG_FLAG_JOBID |
7354 CHANGELOG_FLAG_EXTRA_FLAGS |
7355 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7358 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7359 progname, strerror(errno = -rc));
7363 rc = llapi_changelog_set_xflags(changelog_priv,
7364 CHANGELOG_EXTRA_FLAG_UIDGID |
7365 CHANGELOG_EXTRA_FLAG_NID |
7366 CHANGELOG_EXTRA_FLAG_OMODE |
7367 CHANGELOG_EXTRA_FLAG_XATTR);
7370 "%s changelog: cannot set xflags for changelog: %s\n",
7371 progname, strerror(errno = -rc));
7375 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7379 if (endrec && rec->cr_index > endrec) {
7380 llapi_changelog_free(&rec);
7383 if (rec->cr_index < startrec) {
7384 llapi_changelog_free(&rec);
7388 secs = rec->cr_time >> 30;
7389 gmtime_r(&secs, &ts);
7390 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7391 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7392 changelog_type2str(rec->cr_type),
7393 ts.tm_hour, ts.tm_min, ts.tm_sec,
7394 (int)(rec->cr_time & ((1 << 30) - 1)),
7395 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7396 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7398 if (rec->cr_flags & CLF_JOBID) {
7399 struct changelog_ext_jobid *jid =
7400 changelog_rec_jobid(rec);
7402 if (jid->cr_jobid[0] != '\0')
7403 printf(" j=%s", jid->cr_jobid);
7406 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7407 struct changelog_ext_extra_flags *ef =
7408 changelog_rec_extra_flags(rec);
7410 printf(" ef=0x%llx", ef->cr_extra_flags);
7412 if (ef->cr_extra_flags & CLFE_UIDGID) {
7413 struct changelog_ext_uidgid *uidgid =
7414 changelog_rec_uidgid(rec);
7416 printf(" u=%llu:%llu",
7417 uidgid->cr_uid, uidgid->cr_gid);
7419 if (ef->cr_extra_flags & CLFE_NID) {
7420 struct changelog_ext_nid *nid =
7421 changelog_rec_nid(rec);
7424 libcfs_nid2str(nid->cr_nid));
7427 if (ef->cr_extra_flags & CLFE_OPEN) {
7428 struct changelog_ext_openmode *omd =
7429 changelog_rec_openmode(rec);
7430 char mode[] = "---";
7432 /* exec mode must be exclusive */
7433 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7436 if (omd->cr_openflags & MDS_FMODE_READ)
7438 if (omd->cr_openflags &
7445 if (strcmp(mode, "---") != 0)
7446 printf(" m=%s", mode);
7450 if (ef->cr_extra_flags & CLFE_XATTR) {
7451 struct changelog_ext_xattr *xattr =
7452 changelog_rec_xattr(rec);
7454 if (xattr->cr_xattr[0] != '\0')
7455 printf(" x=%s", xattr->cr_xattr);
7459 if (rec->cr_namelen)
7460 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7461 rec->cr_namelen, changelog_rec_name(rec));
7463 if (rec->cr_flags & CLF_RENAME) {
7464 struct changelog_ext_rename *rnm =
7465 changelog_rec_rename(rec);
7467 if (!fid_is_zero(&rnm->cr_sfid))
7468 printf(" s="DFID" sp="DFID" %.*s",
7469 PFID(&rnm->cr_sfid),
7470 PFID(&rnm->cr_spfid),
7471 (int)changelog_rec_snamelen(rec),
7472 changelog_rec_sname(rec));
7476 llapi_changelog_free(&rec);
7479 llapi_changelog_fini(&changelog_priv);
7482 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7483 progname, strerror(errno = -rc));
7485 return (rc == 1 ? 0 : rc);
7488 static int lfs_changelog_clear(int argc, char **argv)
7496 endrec = strtoll(argv[3], NULL, 10);
7498 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7501 fprintf(stderr, "%s: record out of range: %llu\n",
7503 else if (rc == -ENOENT)
7504 fprintf(stderr, "%s: no changelog user: %s\n",
7507 fprintf(stderr, "%s error: %s\n", argv[0],
7516 static int lfs_fid2path(int argc, char **argv)
7518 struct option long_opts[] = {
7519 { .val = 'c', .name = "cur", .has_arg = no_argument },
7520 { .val = 'l', .name = "link", .has_arg = required_argument },
7521 { .val = 'r', .name = "rec", .has_arg = required_argument },
7523 char short_opts[] = "cl:r:";
7524 char *device, *fid, *path;
7525 long long recno = -1;
7530 char *endptr = NULL;
7532 while ((rc = getopt_long(argc, argv, short_opts,
7533 long_opts, NULL)) != -1) {
7539 linkno = strtol(optarg, &endptr, 10);
7540 if (*endptr != '\0') {
7542 "%s fid2path: invalid linkno '%s'\n",
7548 recno = strtoll(optarg, &endptr, 10);
7549 if (*endptr != '\0') {
7551 "%s fid2path: invalid recno '%s'\n",
7558 "%s fid2path: unrecognized option '%s'\n",
7559 progname, argv[optind - 1]);
7566 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7571 device = argv[optind++];
7572 path = calloc(1, PATH_MAX);
7576 "%s fid2path: cannot allocate memory for path: %s\n",
7577 progname, strerror(-rc));
7582 while (optind < argc) {
7583 fid = argv[optind++];
7585 lnktmp = (linkno >= 0) ? linkno : 0;
7587 int oldtmp = lnktmp;
7588 long long rectmp = recno;
7590 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7594 "%s fid2path: cannot find '%s': %s\n",
7595 progname, fid, strerror(errno = -rc2));
7602 fprintf(stdout, "%lld ", rectmp);
7603 if (device[0] == '/') {
7604 fprintf(stdout, "%s", device);
7605 if (device[strlen(device) - 1] != '/')
7606 fprintf(stdout, "/");
7607 } else if (path[0] == '\0') {
7608 fprintf(stdout, "/");
7610 fprintf(stdout, "%s\n", path);
7613 /* specified linkno */
7615 if (oldtmp == lnktmp)
7625 static int lfs_path2fid(int argc, char **argv)
7627 struct option long_opts[] = {
7628 { .val = 'p', .name = "parents", .has_arg = no_argument },
7631 const char short_opts[] = "p";
7632 const char *sep = "";
7635 bool show_parents = false;
7637 while ((rc = getopt_long(argc, argv, short_opts,
7638 long_opts, NULL)) != -1) {
7641 show_parents = true;
7645 "%s path2fid: unrecognized option '%s'\n",
7646 progname, argv[optind - 1]);
7651 if (optind > argc - 1) {
7652 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7656 else if (optind < argc - 1)
7660 for (path = argv + optind; *path != NULL; path++) {
7662 if (!show_parents) {
7663 err = llapi_path2fid(*path, &fid);
7665 printf("%s%s"DFID"\n",
7666 *sep != '\0' ? *path : "", sep,
7669 char name[NAME_MAX + 1];
7670 unsigned int linkno = 0;
7672 while ((err = llapi_path2parent(*path, linkno, &fid,
7673 name, sizeof(name))) == 0) {
7674 if (*sep != '\0' && linkno == 0)
7675 printf("%s%s", *path, sep);
7677 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7682 /* err == -ENODATA is end-of-loop */
7683 if (linkno > 0 && err == -ENODATA) {
7691 "%s path2fid: cannot get %sfid for '%s': %s\n",
7692 progname, show_parents ? "parent " : "", *path,
7704 static int lfs_data_version(int argc, char **argv)
7711 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7714 fprintf(stderr, "%s data_version: FILE must be specified\n",
7719 while ((c = getopt(argc, argv, "nrw")) != -1) {
7722 data_version_flags = 0;
7725 data_version_flags |= LL_DV_RD_FLUSH;
7728 data_version_flags |= LL_DV_WR_FLUSH;
7732 "%s data_version: unrecognized option '%s'\n",
7733 progname, argv[optind - 1]);
7737 if (optind == argc) {
7738 fprintf(stderr, "%s data_version: FILE must be specified\n",
7743 path = argv[optind];
7744 fd = open(path, O_RDONLY);
7747 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7748 progname, path, strerror(-rc));
7752 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7755 "%s data_version: cannot get version for '%s': %s\n",
7756 progname, path, strerror(-rc));
7758 printf("%ju" "\n", (uintmax_t)data_version);
7764 static int lfs_hsm_state(int argc, char **argv)
7769 struct hsm_user_state hus;
7777 rc = llapi_hsm_state_get(path, &hus);
7779 fprintf(stderr, "can't get hsm state for %s: %s\n",
7780 path, strerror(errno = -rc));
7784 /* Display path name and status flags */
7785 printf("%s: (0x%08x)", path, hus.hus_states);
7787 if (hus.hus_states & HS_RELEASED)
7788 printf(" released");
7789 if (hus.hus_states & HS_EXISTS)
7791 if (hus.hus_states & HS_DIRTY)
7793 if (hus.hus_states & HS_ARCHIVED)
7794 printf(" archived");
7795 /* Display user-settable flags */
7796 if (hus.hus_states & HS_NORELEASE)
7797 printf(" never_release");
7798 if (hus.hus_states & HS_NOARCHIVE)
7799 printf(" never_archive");
7800 if (hus.hus_states & HS_LOST)
7801 printf(" lost_from_hsm");
7803 if (hus.hus_archive_id != 0)
7804 printf(", archive_id:%d", hus.hus_archive_id);
7807 } while (++i < argc);
7812 #define LFS_HSM_SET 0
7813 #define LFS_HSM_CLEAR 1
7816 * Generic function to set or clear HSM flags.
7817 * Used by hsm_set and hsm_clear.
7819 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7821 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7823 struct option long_opts[] = {
7824 { .val = 'A', .name = "archived", .has_arg = no_argument },
7825 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7826 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7827 { .val = 'e', .name = "exists", .has_arg = no_argument },
7828 { .val = 'l', .name = "lost", .has_arg = no_argument },
7829 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7830 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7832 char short_opts[] = "lraAdei:";
7836 __u32 archive_id = 0;
7842 while ((c = getopt_long(argc, argv, short_opts,
7843 long_opts, NULL)) != -1) {
7849 mask |= HS_NOARCHIVE;
7852 mask |= HS_ARCHIVED;
7855 mask |= HS_NORELEASE;
7864 archive_id = strtol(optarg, &end, 10);
7866 fprintf(stderr, "invalid archive_id: '%s'\n",
7874 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7875 argv[0], argv[optind - 1]);
7880 /* User should have specified a flag */
7884 while (optind < argc) {
7886 path = argv[optind];
7888 /* If mode == 0, this means we apply the mask. */
7889 if (mode == LFS_HSM_SET)
7890 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7892 rc = llapi_hsm_state_set(path, 0, mask, 0);
7895 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7896 path, strerror(errno = -rc));
7905 static int lfs_hsm_action(int argc, char **argv)
7910 struct hsm_current_action hca;
7911 struct hsm_extent he;
7912 enum hsm_user_action hua;
7913 enum hsm_progress_states hps;
7921 rc = llapi_hsm_current_action(path, &hca);
7923 fprintf(stderr, "can't get hsm action for %s: %s\n",
7924 path, strerror(errno = -rc));
7927 he = hca.hca_location;
7928 hua = hca.hca_action;
7929 hps = hca.hca_state;
7931 printf("%s: %s", path, hsm_user_action2name(hua));
7933 /* Skip file without action */
7934 if (hca.hca_action == HUA_NONE) {
7939 printf(" %s ", hsm_progress_state2name(hps));
7941 if ((hps == HPS_RUNNING) &&
7942 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7943 printf("(%llu bytes moved)\n",
7944 (unsigned long long)he.length);
7945 else if ((he.offset + he.length) == LUSTRE_EOF)
7946 printf("(from %llu to EOF)\n",
7947 (unsigned long long)he.offset);
7949 printf("(from %llu to %llu)\n",
7950 (unsigned long long)he.offset,
7951 (unsigned long long)(he.offset + he.length));
7953 } while (++i < argc);
7958 static int lfs_hsm_set(int argc, char **argv)
7960 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7963 static int lfs_hsm_clear(int argc, char **argv)
7965 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7969 * Check file state and return its fid, to be used by lfs_hsm_request().
7971 * \param[in] file Path to file to check
7972 * \param[in,out] fid Pointer to allocated lu_fid struct.
7973 * \param[in,out] last_dev Pointer to last device id used.
7975 * \return 0 on success.
7977 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7983 rc = lstat(file, &st);
7985 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7988 /* Checking for regular file as archiving as posix copytool
7989 * rejects archiving files other than regular files
7991 if (!S_ISREG(st.st_mode)) {
7992 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7995 /* A request should be ... */
7996 if (*last_dev != st.st_dev && *last_dev != 0) {
7997 fprintf(stderr, "All files should be "
7998 "on the same filesystem: %s\n", file);
8001 *last_dev = st.st_dev;
8003 rc = llapi_path2fid(file, fid);
8005 fprintf(stderr, "Cannot read FID of %s: %s\n",
8006 file, strerror(-rc));
8012 /* Fill an HSM HUR item with a given file name.
8014 * If mntpath is set, then the filename is actually a FID, and no
8015 * lookup on the filesystem will be performed.
8017 * \param[in] hur the user request to fill
8018 * \param[in] idx index of the item inside the HUR to fill
8019 * \param[in] mntpath mountpoint of Lustre
8020 * \param[in] fname filename (if mtnpath is NULL)
8021 * or FID (if mntpath is set)
8022 * \param[in] last_dev pointer to last device id used
8024 * \retval 0 on success
8025 * \retval CMD_HELP or a negative errno on error
8027 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8028 const char *mntpath, const char *fname,
8031 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8034 hui->hui_extent.length = -1;
8036 if (mntpath != NULL) {
8039 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
8043 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8048 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8052 hur->hur_request.hr_itemcount++;
8057 static int lfs_hsm_request(int argc, char **argv, int action)
8059 struct option long_opts[] = {
8060 { .val = 'a', .name = "archive", .has_arg = required_argument },
8061 { .val = 'D', .name = "data", .has_arg = required_argument },
8062 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8063 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8066 char short_opts[] = "l:D:a:m:";
8067 struct hsm_user_request *hur, *oldhur;
8072 char *filelist = NULL;
8073 char fullpath[PATH_MAX];
8074 char *opaque = NULL;
8078 int nbfile_alloc = 0;
8079 char *some_file = NULL;
8080 char *mntpath = NULL;
8086 while ((c = getopt_long(argc, argv, short_opts,
8087 long_opts, NULL)) != -1) {
8096 if (action != HUA_ARCHIVE &&
8097 action != HUA_REMOVE) {
8099 "error: -a is supported only "
8100 "when archiving or removing\n");
8103 archive_id = atoi(optarg);
8106 if (some_file == NULL) {
8108 some_file = strdup(optarg);
8114 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8115 argv[0], argv[optind - 1]);
8120 /* All remaining args are files, so we have at least nbfile */
8121 nbfile = argc - optind;
8123 if ((nbfile == 0) && (filelist == NULL))
8127 opaque_len = strlen(opaque);
8129 /* Alloc the request structure with enough place to store all files
8130 * from command line. */
8131 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8133 fprintf(stderr, "Cannot create the request: %s\n",
8137 nbfile_alloc = nbfile;
8139 hur->hur_request.hr_action = action;
8140 hur->hur_request.hr_archive_id = archive_id;
8141 hur->hur_request.hr_flags = 0;
8143 /* All remaining args are files, add them */
8144 if (nbfile != 0 && some_file == NULL)
8145 some_file = strdup(argv[optind]);
8147 for (i = 0; i < nbfile; i++) {
8148 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8154 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8156 /* If a filelist was specified, read the filelist from it. */
8157 if (filelist != NULL) {
8158 fp = fopen(filelist, "r");
8160 fprintf(stderr, "Cannot read the file list %s: %s\n",
8161 filelist, strerror(errno));
8166 while ((rc = getline(&line, &len, fp)) != -1) {
8167 /* If allocated buffer was too small, get something
8169 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8172 nbfile_alloc = nbfile_alloc * 2 + 1;
8174 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8177 fprintf(stderr, "hsm: cannot allocate "
8178 "the request: %s\n",
8185 size = hur_len(oldhur);
8187 fprintf(stderr, "hsm: cannot allocate "
8188 "%u files + %u bytes data\n",
8189 oldhur->hur_request.hr_itemcount,
8190 oldhur->hur_request.hr_data_len);
8197 memcpy(hur, oldhur, size);
8202 if (line[strlen(line) - 1] == '\n')
8203 line[strlen(line) - 1] = '\0';
8205 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8206 mntpath, line, &last_dev);
8212 if (some_file == NULL) {
8222 /* If a --data was used, add it to the request */
8223 hur->hur_request.hr_data_len = opaque_len;
8225 memcpy(hur_data(hur), opaque, opaque_len);
8227 /* Send the HSM request */
8228 if (realpath(some_file, fullpath) == NULL) {
8229 fprintf(stderr, "Could not find path '%s': %s\n",
8230 some_file, strerror(errno));
8232 rc = llapi_hsm_request(fullpath, hur);
8234 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8235 some_file, strerror(-rc));
8245 static int lfs_hsm_archive(int argc, char **argv)
8247 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8250 static int lfs_hsm_restore(int argc, char **argv)
8252 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8255 static int lfs_hsm_release(int argc, char **argv)
8257 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8260 static int lfs_hsm_remove(int argc, char **argv)
8262 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8265 static int lfs_hsm_cancel(int argc, char **argv)
8267 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8270 static int lfs_swap_layouts(int argc, char **argv)
8275 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8276 SWAP_LAYOUTS_KEEP_MTIME |
8277 SWAP_LAYOUTS_KEEP_ATIME);
8280 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8282 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8284 int lfs_get_mode(const char *string)
8286 enum lock_mode_user mode;
8288 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8289 if (lock_mode_names[mode] == NULL)
8291 if (strcmp(string, lock_mode_names[mode]) == 0)
8298 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8300 enum lu_ladvise_type advice;
8303 advice < ARRAY_SIZE(ladvise_names); advice++) {
8304 if (ladvise_names[advice] == NULL)
8306 if (strcmp(string, ladvise_names[advice]) == 0)
8310 return LU_LADVISE_INVALID;
8313 static int lfs_ladvise(int argc, char **argv)
8315 struct option long_opts[] = {
8316 { .val = 'a', .name = "advice", .has_arg = required_argument },
8317 { .val = 'b', .name = "background", .has_arg = no_argument },
8318 { .val = 'e', .name = "end", .has_arg = required_argument },
8319 { .val = 'l', .name = "length", .has_arg = required_argument },
8320 { .val = 'm', .name = "mode", .has_arg = required_argument },
8321 { .val = 's', .name = "start", .has_arg = required_argument },
8322 { .val = 'u', .name = "unset", .has_arg = no_argument },
8324 char short_opts[] = "a:be:l:m:s:u";
8329 struct llapi_lu_ladvise advice;
8330 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8331 unsigned long long start = 0;
8332 unsigned long long end = LUSTRE_EOF;
8333 unsigned long long length = 0;
8334 unsigned long long size_units;
8335 unsigned long long flags = 0;
8339 while ((c = getopt_long(argc, argv, short_opts,
8340 long_opts, NULL)) != -1) {
8343 advice_type = lfs_get_ladvice(optarg);
8344 if (advice_type == LU_LADVISE_INVALID) {
8345 fprintf(stderr, "%s: invalid advice type "
8346 "'%s'\n", argv[0], optarg);
8347 fprintf(stderr, "Valid types:");
8349 for (advice_type = 0;
8350 advice_type < ARRAY_SIZE(ladvise_names);
8352 if (ladvise_names[advice_type] == NULL)
8354 fprintf(stderr, " %s",
8355 ladvise_names[advice_type]);
8357 fprintf(stderr, "\n");
8370 rc = llapi_parse_size(optarg, &end,
8373 fprintf(stderr, "%s: bad end offset '%s'\n",
8380 rc = llapi_parse_size(optarg, &start,
8383 fprintf(stderr, "%s: bad start offset "
8384 "'%s'\n", argv[0], optarg);
8390 rc = llapi_parse_size(optarg, &length,
8393 fprintf(stderr, "%s: bad length '%s'\n",
8399 mode = lfs_get_mode(optarg);
8401 fprintf(stderr, "%s: bad mode '%s', valid "
8402 "modes are READ or WRITE\n",
8410 fprintf(stderr, "%s: option '%s' unrecognized\n",
8411 argv[0], argv[optind - 1]);
8416 if (advice_type == LU_LADVISE_INVALID) {
8417 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8418 fprintf(stderr, "Valid types:");
8419 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8421 if (ladvise_names[advice_type] == NULL)
8423 fprintf(stderr, " %s", ladvise_names[advice_type]);
8425 fprintf(stderr, "\n");
8429 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8430 fprintf(stderr, "%s: Lock no expand advice is a per file "
8431 "descriptor advice, so when called from lfs, "
8432 "it does nothing.\n", argv[0]);
8436 if (argc <= optind) {
8437 fprintf(stderr, "%s: please give one or more file names\n",
8442 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8443 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8448 if (end == LUSTRE_EOF && length != 0)
8449 end = start + length;
8452 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8453 argv[0], start, end);
8457 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8458 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8463 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8464 fprintf(stderr, "%s: mode is required with lockahead\n",
8469 while (optind < argc) {
8472 path = argv[optind++];
8474 fd = open(path, O_RDONLY);
8476 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8477 argv[0], path, strerror(errno));
8482 advice.lla_start = start;
8483 advice.lla_end = end;
8484 advice.lla_advice = advice_type;
8485 advice.lla_value1 = 0;
8486 advice.lla_value2 = 0;
8487 advice.lla_value3 = 0;
8488 advice.lla_value4 = 0;
8489 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8490 advice.lla_lockahead_mode = mode;
8491 advice.lla_peradvice_flags = flags;
8494 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8497 fprintf(stderr, "%s: cannot give advice '%s' to file "
8498 "'%s': %s\n", argv[0],
8499 ladvise_names[advice_type],
8500 path, strerror(errno));
8506 if (rc == 0 && rc2 < 0)
8512 static const char *const heat_names[] = LU_HEAT_NAMES;
8514 static int lfs_heat_get(int argc, char **argv)
8516 struct lu_heat *heat;
8525 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8527 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8532 while (optind < argc) {
8533 path = argv[optind++];
8535 fd = open(path, O_RDONLY);
8537 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8538 argv[0], path, strerror(errno));
8543 heat->lh_count = OBD_HEAT_COUNT;
8544 rc2 = llapi_heat_get(fd, heat);
8547 fprintf(stderr, "%s: cannot get heat of file '%s'"
8548 ": %s\n", argv[0], path, strerror(errno));
8552 printf("flags: %x\n", heat->lh_flags);
8553 for (i = 0; i < heat->lh_count; i++)
8554 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8556 if (rc == 0 && rc2 < 0)
8564 static int lfs_heat_set(int argc, char **argv)
8566 struct option long_opts[] = {
8567 { .val = 'c', .name = "clear", .has_arg = no_argument },
8568 { .val = 'o', .name = "off", .has_arg = no_argument },
8569 { .val = 'O', .name = "on", .has_arg = no_argument },
8571 enum lu_heat_flag flags = 0;
8581 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8584 flags |= LU_HEAT_FLAG_CLEAR;
8587 flags |= LU_HEAT_FLAG_CLEAR;
8588 flags |= LU_HEAT_FLAG_OFF;
8591 flags &= ~LU_HEAT_FLAG_OFF;
8596 fprintf(stderr, "%s: option '%s' unrecognized\n",
8597 argv[0], argv[optind - 1]);
8602 if (argc <= optind) {
8603 fprintf(stderr, "%s: please give one or more file names\n",
8608 while (optind < argc) {
8609 path = argv[optind++];
8611 fd = open(path, O_RDONLY);
8613 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8614 argv[0], path, strerror(errno));
8619 rc2 = llapi_heat_set(fd, flags);
8622 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8623 ": %s\n", argv[0], path, strerror(errno));
8627 if (rc == 0 && rc2 < 0)
8633 /** The input string contains a comma delimited list of component ids and
8634 * ranges, for example "1,2-4,7".
8636 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8638 bool end_of_loop = false;
8646 while (!end_of_loop) {
8650 char *endptr = NULL;
8653 ptr = strchrnul(arg, ',');
8654 end_of_loop = *ptr == '\0';
8657 start_index = strtol(arg, &endptr, 0);
8658 if (endptr == arg) /* no data at all */
8660 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8662 if (start_index < 0)
8665 end_index = start_index;
8666 if (*endptr == '-') {
8667 end_index = strtol(endptr + 1, &endptr, 0);
8668 if (*endptr != '\0')
8670 if (end_index < start_index)
8674 for (i = start_index; i <= end_index && size > 0; i++) {
8677 /* remove duplicate */
8678 for (j = 0; j < nr; j++) {
8682 if (j == nr) { /* no duplicate */
8688 if (size == 0 && i < end_index)
8695 if (!end_of_loop && ptr != NULL)
8698 return rc < 0 ? rc : nr;
8702 * struct verify_mirror_id - Mirror id to be verified.
8703 * @mirror_id: A specified mirror id.
8704 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8706 struct verify_mirror_id {
8712 * compare_mirror_ids() - Compare mirror ids.
8713 * @layout: Mirror component list.
8714 * @cbdata: Callback data in verify_mirror_id structure.
8716 * This is a callback function called by llapi_layout_comp_iterate()
8717 * to compare the specified mirror id with the one in the current
8718 * component of @layout. If they are the same, then the specified
8719 * mirror id is valid.
8721 * Return: a negative error code on failure or
8722 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8723 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8726 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8728 struct verify_mirror_id *mirror_id_cbdata =
8729 (struct verify_mirror_id *)cbdata;
8733 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8737 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8738 progname, strerror(errno));
8742 if (mirror_id_cbdata->mirror_id == mirror_id) {
8743 mirror_id_cbdata->is_valid_id = true;
8744 return LLAPI_LAYOUT_ITER_STOP;
8747 return LLAPI_LAYOUT_ITER_CONT;
8751 * verify_mirror_ids() - Verify specified mirror ids.
8752 * @fname: Mirrored file name.
8753 * @mirror_ids: Specified mirror ids to be verified.
8754 * @ids_nr: Number of specified mirror ids.
8756 * This function verifies that specified @mirror_ids are valid
8757 * in the mirrored file @fname.
8759 * Return: 0 on success or a negative error code on failure.
8762 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8764 struct llapi_layout *layout = NULL;
8765 struct verify_mirror_id mirror_id_cbdata = { 0 };
8776 if (stat(fname, &stbuf) < 0) {
8777 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8778 progname, fname, strerror(errno));
8783 if (!S_ISREG(stbuf.st_mode)) {
8784 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8790 fd = open(fname, O_DIRECT | O_RDONLY);
8792 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8793 progname, fname, strerror(errno));
8798 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8800 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8801 progname, fname, strerror(errno));
8805 layout = llapi_layout_get_by_fd(fd, 0);
8806 if (layout == NULL) {
8807 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8808 progname, fname, strerror(errno));
8810 llapi_lease_release(fd);
8814 rc = llapi_layout_flags_get(layout, &flr_state);
8816 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8817 progname, fname, strerror(errno));
8822 flr_state &= LCM_FL_FLR_MASK;
8823 switch (flr_state) {
8826 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8827 progname, fname, llapi_layout_flags_string(flr_state));
8834 for (i = 0; i < ids_nr; i++) {
8835 mirror_id_cbdata.mirror_id = mirror_ids[i];
8836 mirror_id_cbdata.is_valid_id = false;
8838 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8843 "%s: '%s' failed to verify mirror id: %u.\n",
8844 progname, fname, mirror_ids[i]);
8848 if (!mirror_id_cbdata.is_valid_id) {
8851 "%s: '%s' invalid specified mirror id: %u.\n",
8852 progname, fname, mirror_ids[i]);
8858 llapi_layout_free(layout);
8859 llapi_lease_release(fd);
8867 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8868 __u16 *mirror_ids, int ids_nr)
8870 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8871 struct llapi_layout *layout;
8881 if (stat(fname, &stbuf) < 0) {
8882 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8883 progname, fname, strerror(errno));
8887 if (!S_ISREG(stbuf.st_mode)) {
8888 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8894 fd = open(fname, O_DIRECT | O_RDWR);
8896 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8897 progname, fname, strerror(errno));
8902 layout = llapi_layout_get_by_fd(fd, 0);
8903 if (layout == NULL) {
8904 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8905 progname, fname, strerror(errno));
8910 rc = llapi_layout_flags_get(layout, &flr_state);
8912 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8913 progname, fname, strerror(errno));
8918 flr_state &= LCM_FL_FLR_MASK;
8919 if (flr_state == LCM_FL_NONE) {
8921 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8926 /* get stale component info */
8927 comp_size = llapi_mirror_find_stale(layout, comp_array,
8928 ARRAY_SIZE(comp_array),
8929 mirror_ids, ids_nr);
8930 if (comp_size <= 0) {
8935 ioc->lil_mode = LL_LEASE_WRLCK;
8936 ioc->lil_flags = LL_LEASE_RESYNC;
8937 rc = llapi_lease_set(fd, ioc);
8939 if (rc == -EALREADY)
8943 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8944 progname, fname, strerror(errno));
8948 /* get the read range [start, end) */
8949 start = comp_array[0].lrc_start;
8950 end = comp_array[0].lrc_end;
8951 for (idx = 1; idx < comp_size; idx++) {
8952 if (comp_array[idx].lrc_start < start)
8953 start = comp_array[idx].lrc_start;
8954 if (end < comp_array[idx].lrc_end)
8955 end = comp_array[idx].lrc_end;
8958 rc = llapi_lease_check(fd);
8959 if (rc != LL_LEASE_WRLCK) {
8960 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8965 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8968 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8969 progname, fname, rc);
8971 /* prepare ioc for lease put */
8972 ioc->lil_mode = LL_LEASE_UNLCK;
8973 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8975 for (idx = 0; idx < comp_size; idx++) {
8976 if (comp_array[idx].lrc_synced) {
8977 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8982 rc = llapi_lease_set(fd, ioc);
8984 if (rc == 0) /* lost lease lock */
8986 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8987 progname, fname, strerror(errno));
8991 * llapi_lease_set returns lease mode when it request to unlock
8997 llapi_layout_free(layout);
9004 static inline int lfs_mirror_resync(int argc, char **argv)
9006 struct ll_ioc_lease *ioc = NULL;
9007 __u16 mirror_ids[128] = { 0 };
9012 struct option long_opts[] = {
9013 { .val = 'o', .name = "only", .has_arg = required_argument },
9016 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9019 rc = parse_mirror_ids(mirror_ids,
9020 sizeof(mirror_ids) / sizeof(__u16),
9024 "%s: bad mirror ids '%s'.\n",
9031 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9032 argv[0], argv[optind - 1]);
9038 if (argc == optind) {
9039 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9044 if (ids_nr > 0 && argc > optind + 1) {
9046 "%s: option '--only' cannot be used upon multiple files.\n",
9054 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9059 /* set the lease on the file */
9060 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9062 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9063 argv[0], strerror(errno));
9068 for (; optind < argc; optind++) {
9069 rc = lfs_mirror_resync_file(argv[optind], ioc,
9070 mirror_ids, ids_nr);
9071 /* ignore previous file's error, continue with next file */
9074 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9082 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9084 struct llapi_layout *layout;
9087 layout = llapi_layout_get_by_fd(fd, 0);
9088 if (layout == NULL) {
9089 fprintf(stderr, "could not get layout.\n");
9093 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9095 fprintf(stderr, "failed to iterate layout\n");
9096 llapi_layout_free(layout);
9099 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9100 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9101 llapi_layout_free(layout);
9105 llapi_layout_free(layout);
9111 * Check whether two files are the same file
9112 * \retval 0 same file
9113 * \retval 1 not the same file
9114 * \retval <0 error code
9116 static inline int check_same_file(const char *f1, const char *f2)
9121 if (stat(f1, &stbuf1) < 0) {
9122 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9123 progname, f1, strerror(errno));
9127 if (stat(f2, &stbuf2) < 0) {
9128 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9129 progname, f2, strerror(errno));
9133 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9134 stbuf1.st_ino == stbuf2.st_ino)
9140 static inline int lfs_mirror_read(int argc, char **argv)
9143 __u16 mirror_id = 0;
9144 const char *outfile = NULL;
9150 const size_t buflen = 4 << 20;
9152 struct option long_opts[] = {
9153 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9154 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9157 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9162 mirror_id = strtoul(optarg, &end, 0);
9163 if (*end != '\0' || mirror_id == 0) {
9165 "%s %s: invalid mirror ID '%s'\n",
9166 progname, argv[0], optarg);
9174 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9175 progname, argv[optind - 1]);
9180 if (argc == optind) {
9181 fprintf(stderr, "%s %s: no mirrored file provided\n",
9184 } else if (argc > optind + 1) {
9185 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9189 if (mirror_id == 0) {
9190 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9195 /* open mirror file */
9196 fname = argv[optind];
9199 rc = check_same_file(fname, outfile);
9202 "%s %s: output file cannot be the mirrored file\n",
9210 fd = open(fname, O_DIRECT | O_RDONLY);
9212 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9213 progname, argv[0], fname, strerror(errno));
9217 /* verify mirror id */
9218 rc = verify_mirror_id_by_fd(fd, mirror_id);
9221 "%s %s: cannot find mirror with ID %u in '%s'\n",
9222 progname, argv[0], mirror_id, fname);
9226 /* open output file */
9228 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9230 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9231 progname, argv[0], outfile, strerror(errno));
9236 outfd = STDOUT_FILENO;
9239 /* allocate buffer */
9240 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9242 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9243 progname, argv[0], rc);
9250 ssize_t written = 0;
9252 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9253 if (bytes_read < 0) {
9256 "%s %s: fail to read data from mirror %u: %s\n",
9257 progname, argv[0], mirror_id, strerror(-rc));
9262 if (bytes_read == 0)
9265 while (written < bytes_read) {
9268 written2 = write(outfd, buf + written,
9269 bytes_read - written);
9272 "%s %s: fail to write %s: %s\n",
9273 progname, argv[0], outfile ? : "STDOUT",
9278 written += written2;
9281 if (written != bytes_read) {
9283 "%s %s: written %ld bytes does not match with %ld read.\n",
9284 progname, argv[0], written, bytes_read);
9306 static inline int lfs_mirror_write(int argc, char **argv)
9309 __u16 mirror_id = 0;
9310 const char *inputfile = NULL;
9316 const size_t buflen = 4 << 20;
9318 size_t page_size = sysconf(_SC_PAGESIZE);
9319 struct ll_ioc_lease_id ioc;
9321 struct option long_opts[] = {
9322 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9323 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9326 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9331 mirror_id = strtoul(optarg, &end, 0);
9332 if (*end != '\0' || mirror_id == 0) {
9334 "%s %s: invalid mirror ID '%s'\n",
9335 progname, argv[0], optarg);
9343 fprintf(stderr, "%s: option '%s' unrecognized\n",
9344 progname, argv[optind - 1]);
9349 if (argc == optind) {
9350 fprintf(stderr, "%s %s: no mirrored file provided\n",
9353 } else if (argc > optind + 1) {
9354 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9358 if (mirror_id == 0) {
9359 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9364 /* open mirror file */
9365 fname = argv[optind];
9368 rc = check_same_file(fname, inputfile);
9371 "%s %s: input file cannot be the mirrored file\n",
9379 fd = open(fname, O_DIRECT | O_WRONLY);
9381 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9382 progname, argv[0], fname, strerror(errno));
9386 /* verify mirror id */
9387 rc = verify_mirror_id_by_fd(fd, mirror_id);
9390 "%s %s: cannot find mirror with ID %u in '%s'\n",
9391 progname, argv[0], mirror_id, fname);
9395 /* open input file */
9397 inputfd = open(inputfile, O_RDONLY, 0644);
9399 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9400 progname, argv[0], inputfile, strerror(errno));
9405 inputfd = STDIN_FILENO;
9408 /* allocate buffer */
9409 rc = posix_memalign(&buf, page_size, buflen);
9411 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9412 progname, argv[0], rc);
9416 /* prepare target mirror components instantiation */
9417 ioc.lil_mode = LL_LEASE_WRLCK;
9418 ioc.lil_flags = LL_LEASE_RESYNC;
9419 ioc.lil_mirror_id = mirror_id;
9420 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9423 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9424 progname, argv[0], fname, strerror(errno));
9434 rc = llapi_lease_check(fd);
9435 if (rc != LL_LEASE_WRLCK) {
9436 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9437 progname, argv[0], fname);
9441 bytes_read = read(inputfd, buf, buflen);
9442 if (bytes_read < 0) {
9445 "%s %s: fail to read data from '%s': %s\n",
9446 progname, argv[0], inputfile ? : "STDIN",
9453 if (bytes_read == 0)
9456 /* round up to page align to make direct IO happy. */
9457 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9459 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9464 "%s %s: fail to write to mirror %u: %s\n",
9465 progname, argv[0], mirror_id,
9473 if (pos & (page_size - 1)) {
9474 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9479 ioc.lil_mode = LL_LEASE_UNLCK;
9480 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9482 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9487 "%s %s: release lease lock of '%s' failed: %s\n",
9488 progname, argv[0], fname, strerror(errno));
9505 struct collect_ids_data {
9511 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9513 struct collect_ids_data *cid = cbdata;
9517 rc = llapi_layout_mirror_id_get(layout, &id);
9521 if ((__u16)id != cid->cid_exclude) {
9524 for (i = 0; i < cid->cid_count; i++) {
9525 /* already collected the mirror id */
9526 if (id == cid->cid_ids[i])
9527 return LLAPI_LAYOUT_ITER_CONT;
9529 cid->cid_ids[cid->cid_count] = id;
9533 return LLAPI_LAYOUT_ITER_CONT;
9536 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9538 struct llapi_layout *layout;
9539 struct collect_ids_data cid = { .cid_ids = ids,
9541 .cid_exclude = exclude_id, };
9544 layout = llapi_layout_get_by_fd(fd, 0);
9545 if (layout == NULL) {
9546 fprintf(stderr, "could not get layout\n");
9550 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9552 fprintf(stderr, "failed to iterate layout\n");
9553 llapi_layout_free(layout);
9557 llapi_layout_free(layout);
9559 return cid.cid_count;
9562 static inline int lfs_mirror_copy(int argc, char **argv)
9565 __u16 read_mirror_id = 0;
9566 __u16 ids[128] = { 0 };
9568 struct llapi_layout *layout = NULL;
9569 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9576 struct ll_ioc_lease *ioc = NULL;
9577 struct ll_ioc_lease_id *resync_ioc;
9579 struct option long_opts[] = {
9580 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9581 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9584 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9589 read_mirror_id = strtoul(optarg, &end, 0);
9590 if (*end != '\0' || read_mirror_id == 0) {
9592 "%s %s: invalid read mirror ID '%s'\n",
9593 progname, argv[0], optarg);
9598 if (!strcmp(optarg, "-1")) {
9599 /* specify all other mirrors */
9603 count = parse_mirror_ids((__u16 *)ids,
9611 fprintf(stderr, "%s: option '%s' unrecognized\n",
9612 progname, argv[optind - 1]);
9617 if (argc == optind) {
9618 fprintf(stderr, "%s %s: no mirrored file provided\n",
9621 } else if (argc > optind + 1) {
9622 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9626 if (read_mirror_id == 0) {
9628 "%s %s: no valid read mirror ID %d is provided\n",
9629 progname, argv[0], read_mirror_id);
9635 "%s %s: no write mirror ID is provided\n",
9640 for (i = 0; i < count; i++) {
9641 if (read_mirror_id == ids[i]) {
9643 "%s %s: read and write mirror ID cannot be the same\n",
9649 /* open mirror file */
9650 fname = argv[optind];
9652 fd = open(fname, O_DIRECT | O_RDWR);
9654 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9655 progname, argv[0], fname, strerror(errno));
9659 /* write to all other mirrors */
9660 if (ids[0] == (__u16)-1) {
9661 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9665 "%s %s: failed to get other mirror ids in '%s': %d\n",
9666 progname, argv[0], fname, rc);
9671 /* verify mirror id */
9672 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9675 "%s %s: cannot find mirror with ID %u in '%s'\n",
9676 progname, argv[0], read_mirror_id, fname);
9680 for (i = 0; i < count; i++) {
9681 rc = verify_mirror_id_by_fd(fd, ids[i]);
9684 "%s %s: cannot find mirror with ID %u in '%s'\n",
9685 progname, argv[0], ids[i], fname);
9690 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9693 "%s %s: cannot alloc comp id array for ioc: %s\n",
9694 progname, argv[0], strerror(errno));
9699 /* get stale component info */
9700 layout = llapi_layout_get_by_fd(fd, 0);
9701 if (layout == NULL) {
9702 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9703 progname, argv[0], fname, strerror(errno));
9707 comp_size = llapi_mirror_find_stale(layout, comp_array,
9708 ARRAY_SIZE(comp_array),
9710 llapi_layout_free(layout);
9711 if (comp_size < 0) {
9716 /* prepare target mirror components instantiation */
9717 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9718 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9719 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9721 resync_ioc->lil_mirror_id = ids[0];
9723 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9724 rc = llapi_lease_set(fd, ioc);
9727 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9728 progname, argv[0], fname, strerror(errno));
9732 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9735 fprintf(stderr, "%s %s: copy error: %d\n",
9736 progname, argv[0], rc);
9740 fprintf(stdout, "mirror copied successfully: ");
9741 for (i = 0; i < copied; i++)
9742 fprintf(stdout, "%d ", ids[i]);
9743 fprintf(stdout, "\n");
9745 ioc->lil_mode = LL_LEASE_UNLCK;
9746 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9748 for (i = 0; i < comp_size; i++) {
9751 for (j = 0; j < copied; j++) {
9752 if (comp_array[i].lrc_mirror_id != ids[j])
9755 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9759 rc = llapi_lease_set(fd, ioc);
9764 "%s %s: release lease lock of '%s' failed: %s\n",
9765 progname, argv[0], fname, strerror(errno));
9779 * struct verify_chunk - Mirror chunk to be verified.
9780 * @chunk: [start, end) of the chunk.
9781 * @mirror_count: Number of mirror ids in @mirror_id array.
9782 * @mirror_id: Array of valid mirror ids that cover the chunk.
9784 struct verify_chunk {
9785 struct lu_extent chunk;
9786 unsigned int mirror_count;
9787 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9791 * print_chunks() - Print chunk information.
9792 * @fname: Mirrored file name.
9793 * @chunks: Array of chunks.
9794 * @chunk_count: Number of chunks in @chunks array.
9796 * This function prints [start, end) of each chunk in @chunks
9797 * for mirrored file @fname, and also prints the valid mirror ids
9798 * that cover the chunk.
9803 void print_chunks(const char *fname, struct verify_chunk *chunks,
9809 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9810 for (i = 0; i < chunk_count; i++) {
9811 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9813 if (chunks[i].mirror_count == 0)
9814 fprintf(stdout, "\t[");
9816 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9817 for (j = 1; j < chunks[i].mirror_count; j++)
9818 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9820 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9822 fprintf(stdout, "\n");
9826 * print_checksums() - Print CRC-32 checksum values.
9827 * @chunk: A chunk and its corresponding valid mirror ids.
9828 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9830 * This function prints CRC-32 checksum values on @chunk for
9831 * each valid mirror that covers it.
9836 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9841 "CRC-32 checksum value for chunk "DEXT":\n",
9842 PEXT(&chunk->chunk));
9843 for (i = 0; i < chunk->mirror_count; i++)
9844 fprintf(stdout, "Mirror %u:\t%#lx\n",
9845 chunk->mirror_id[i], crc[i]);
9846 fprintf(stdout, "\n");
9850 * filter_mirror_id() - Filter specified mirror ids.
9851 * @chunks: Array of chunks.
9852 * @chunk_count: Number of chunks in @chunks array.
9853 * @mirror_ids: Specified mirror ids to be verified.
9854 * @ids_nr: Number of specified mirror ids.
9856 * This function scans valid mirror ids that cover each chunk in @chunks
9857 * and filters specified mirror ids.
9862 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9863 __u16 *mirror_ids, int ids_nr)
9868 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9869 unsigned int valid_count = 0;
9871 for (i = 0; i < chunk_count; i++) {
9872 if (chunks[i].mirror_count == 0)
9876 for (j = 0; j < ids_nr; j++) {
9877 for (k = 0; k < chunks[i].mirror_count; k++) {
9878 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9879 valid_id[valid_count] = mirror_ids[j];
9886 memcpy(chunks[i].mirror_id, valid_id,
9887 sizeof(__u16) * valid_count);
9888 chunks[i].mirror_count = valid_count;
9893 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9894 * @layout: Mirror component list.
9895 * @chunks: Array of chunks.
9896 * @chunks_size: Array size of @chunks.
9898 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9899 * to find out chunk segments and store them in @chunks array.
9901 * The @mirror_id array in each element of @chunks will store the valid
9902 * mirror ids that cover the chunk. If a mirror component covering the
9903 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9904 * will not be stored into the @mirror_id array, and the chunk for that
9905 * mirror will not be verified.
9907 * The @mirror_count in each element of @chunks will store the number of
9908 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9909 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9910 * indicates the chunk is valid in only one mirror. In both cases, the
9911 * chunk will not be verified.
9913 * Here is an example:
9916 * +------+-------------+--------------------+
9918 * +------+------+------+------+-------------+
9919 * | | S | S | | mirror2
9920 * +-------------+------+------+-------------+
9922 * prepared @chunks array will contain 5 elements:
9923 * (([0, 1M), [1, 2], 2),
9924 * ([1M, 2M), [1, 2], 2),
9925 * ([2M, 3M), [1], 1),
9926 * ([3M, 4M], [], 0),
9927 * ([4M, EOF), [2], 1))
9929 * Return: the actual array size of @chunks on success
9930 * or a negative error code on failure.
9933 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9934 struct verify_chunk *chunks,
9945 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9948 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9951 "%s: move to the first layout component: %s.\n",
9952 progname, strerror(errno));
9958 chunks[idx].chunk.e_end = LUSTRE_EOF;
9960 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9963 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9964 progname, strerror(errno));
9968 if (start > chunks[idx].chunk.e_start ||
9969 end <= chunks[idx].chunk.e_start)
9972 if (end < chunks[idx].chunk.e_end)
9973 chunks[idx].chunk.e_end = end;
9975 rc = llapi_layout_comp_flags_get(layout, &flags);
9978 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9979 progname, strerror(errno));
9983 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9986 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9989 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9990 progname, strerror(errno));
9994 chunks[idx].mirror_id[i] = mirror_id;
9996 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9998 "%s: mirror_id array is too small.\n",
10005 rc = llapi_layout_comp_use(layout,
10006 LLAPI_LAYOUT_COMP_USE_NEXT);
10009 "%s: move to the next layout component: %s.\n",
10010 progname, strerror(errno));
10013 } /* loop through all components */
10015 chunks[idx].mirror_count = i;
10017 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10021 if (idx >= chunks_size) {
10022 fprintf(stderr, "%s: chunks array is too small.\n",
10028 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10032 return rc < 0 ? rc : idx + 1;
10036 * lfs_mirror_verify_chunk() - Verify a chunk.
10037 * @fd: File descriptor of the mirrored file.
10038 * @file_size: Size of the mirrored file.
10039 * @chunk: A chunk and its corresponding valid mirror ids.
10040 * @verbose: Verbose mode.
10042 * This function verifies a @chunk contains exactly the same data
10043 * ammong the mirrors that cover it.
10045 * If @verbose is specified, then the function will print where the
10046 * differences are if the data do not match. Otherwise, it will
10047 * just return an error in that case.
10049 * Return: 0 on success or a negative error code on failure.
10052 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10053 struct verify_chunk *chunk, int verbose)
10055 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10057 size_t page_size = sysconf(_SC_PAGESIZE);
10058 ssize_t bytes_read;
10059 ssize_t bytes_done;
10063 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10067 if (file_size == 0)
10070 rc = posix_memalign(&buf, page_size, buflen);
10071 if (rc) /* error code is returned directly */
10075 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10076 PEXT(&chunk->chunk));
10077 for (i = 0; i < chunk->mirror_count; i++)
10078 fprintf(stdout, " %u", chunk->mirror_id[i]);
10079 fprintf(stdout, "\n");
10083 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10084 pos = chunk->chunk.e_start;
10085 while (bytes_done < count) {
10086 /* compute initial CRC-32 checksum */
10087 crc = crc32(0L, Z_NULL, 0);
10088 memset(crc_array, 0, sizeof(crc_array));
10091 for (i = 0; i < chunk->mirror_count; i++) {
10092 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10094 if (bytes_read < 0) {
10097 "%s: failed to read data from mirror %u: %s.\n",
10098 progname, chunk->mirror_id[i],
10103 /* compute new CRC-32 checksum */
10104 crc_array[i] = crc32(crc, buf, bytes_read);
10108 print_checksums(chunk, crc_array);
10110 /* compare CRC-32 checksum values */
10111 for (i = 1; i < chunk->mirror_count; i++) {
10112 if (crc_array[i] != crc_array[0]) {
10116 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10117 progname, PEXT(&chunk->chunk),
10118 chunk->mirror_id[0],
10119 chunk->mirror_id[i]);
10124 bytes_done += bytes_read;
10127 if (verbose > 1 && rc == 0) {
10128 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10129 PEXT(&chunk->chunk));
10130 for (i = 0; i < chunk->mirror_count; i++)
10131 fprintf(stdout, " %u", chunk->mirror_id[i]);
10132 fprintf(stdout, " PASS\n\n");
10141 * lfs_mirror_verify_file() - Verify a mirrored file.
10142 * @fname: Mirrored file name.
10143 * @mirror_ids: Specified mirror ids to be verified.
10144 * @ids_nr: Number of specified mirror ids.
10145 * @verbose: Verbose mode.
10147 * This function verifies that each SYNC mirror of a mirrored file
10148 * specified by @fname contains exactly the same data.
10150 * If @mirror_ids is specified, then the function will verify the
10151 * mirrors specified by @mirror_ids contain exactly the same data.
10153 * If @verbose is specified, then the function will print where the
10154 * differences are if the data do not match. Otherwise, it will
10155 * just return an error in that case.
10157 * Return: 0 on success or a negative error code on failure.
10160 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10163 struct verify_chunk chunks_array[1024] = { };
10164 struct llapi_layout *layout = NULL;
10166 uint32_t flr_state;
10168 int chunk_count = 0;
10174 if (stat(fname, &stbuf) < 0) {
10175 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10176 progname, fname, strerror(errno));
10181 if (!S_ISREG(stbuf.st_mode)) {
10182 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10188 if (stbuf.st_size == 0) {
10190 fprintf(stdout, "%s: '%s' file size is 0.\n",
10196 fd = open(fname, O_DIRECT | O_RDONLY);
10198 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10199 progname, fname, strerror(errno));
10204 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10206 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10207 progname, fname, strerror(errno));
10211 layout = llapi_layout_get_by_fd(fd, 0);
10212 if (layout == NULL) {
10213 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10214 progname, fname, strerror(errno));
10216 llapi_lease_release(fd);
10220 rc = llapi_layout_flags_get(layout, &flr_state);
10222 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10223 progname, fname, strerror(errno));
10228 flr_state &= LCM_FL_FLR_MASK;
10229 switch (flr_state) {
10232 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10233 progname, fname, llapi_layout_flags_string(flr_state));
10239 /* find out mirror chunks to be verified */
10240 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10241 ARRAY_SIZE(chunks_array));
10242 if (chunk_count < 0) {
10248 /* filter specified mirror ids */
10249 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10252 print_chunks(fname, chunks_array, chunk_count);
10254 for (idx = 0; idx < chunk_count; idx++) {
10255 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10258 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10260 PEXT(&chunks_array[idx].chunk),
10261 (unsigned long long)stbuf.st_size);
10265 if (chunks_array[idx].mirror_count == 0) {
10267 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10269 PEXT(&chunks_array[idx].chunk));
10271 fprintf(stderr, "skipped\n");
10275 fprintf(stderr, "failed\n");
10279 if (chunks_array[idx].mirror_count == 1) {
10282 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10284 PEXT(&chunks_array[idx].chunk),
10285 chunks_array[idx].mirror_id[0]);
10289 rc = llapi_lease_check(fd);
10290 if (rc != LL_LEASE_RDLCK) {
10291 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10296 /* verify one chunk */
10297 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10298 &chunks_array[idx], verbose);
10312 llapi_layout_free(layout);
10313 llapi_lease_release(fd);
10321 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10322 * @argc: The count of lfs mirror verify command line arguments.
10323 * @argv: Array of strings for lfs mirror verify command line arguments.
10325 * This function parses lfs mirror verify command and verifies the
10326 * specified mirrored file(s).
10328 * Return: 0 on success or a negative error code on failure.
10330 static inline int lfs_mirror_verify(int argc, char **argv)
10332 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10338 char cmd[PATH_MAX];
10340 struct option long_opts[] = {
10341 { .val = 'o', .name = "only", .has_arg = required_argument },
10342 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10343 { .name = NULL } };
10345 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10347 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10350 rc = parse_mirror_ids(mirror_ids,
10351 ARRAY_SIZE(mirror_ids),
10355 "%s: bad mirror ids '%s'.\n",
10362 "%s: at least 2 mirror ids needed with '--only' option.\n",
10372 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10373 progname, argv[optind - 1]);
10379 if (argc == optind) {
10380 fprintf(stderr, "%s: no file name given.\n", progname);
10385 if (ids_nr > 0 && argc > optind + 1) {
10387 "%s: '--only' cannot be used upon multiple files.\n",
10395 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10401 for (; optind < argc; optind++) {
10402 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10412 * lfs_mirror() - Parse and execute lfs mirror commands.
10413 * @argc: The count of lfs mirror command line arguments.
10414 * @argv: Array of strings for lfs mirror command line arguments.
10416 * This function parses lfs mirror commands and performs the
10417 * corresponding functions specified in mirror_cmdlist[].
10419 * Return: 0 on success or an error code on failure.
10421 static int lfs_mirror(int argc, char **argv)
10423 char cmd[PATH_MAX];
10426 setlinebuf(stdout);
10428 Parser_init("lfs-mirror > ", mirror_cmdlist);
10430 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10432 program_invocation_short_name = cmd;
10434 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10436 rc = Parser_commands();
10438 return rc < 0 ? -rc : rc;
10441 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10443 #if __BYTE_ORDER == __BIG_ENDIAN
10444 __swab16s(&attrs->lsa_valid);
10445 __swab64s(&attrs->lsa_size);
10446 __swab64s(&attrs->lsa_blocks);
10450 enum lfs_som_type {
10451 LFS_SOM_SIZE = 0x1,
10452 LFS_SOM_BLOCKS = 0x2,
10453 LFS_SOM_FLAGS = 0x4,
10454 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10458 static int lfs_getsom(int argc, char **argv)
10461 struct lustre_som_attrs *attrs;
10462 char buf[sizeof(*attrs) + 64];
10463 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10466 while ((c = getopt(argc, argv, "sbf")) != -1) {
10469 type = LFS_SOM_SIZE;
10472 type = LFS_SOM_BLOCKS;
10475 type = LFS_SOM_FLAGS;
10478 fprintf(stderr, "%s: invalid option '%c'\n",
10488 fprintf(stderr, "%s: %s\n",
10489 progname, argc == 0 ? "miss file target" :
10490 "input more than 2 files");
10495 attrs = (void *)buf;
10496 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10499 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10500 argv[0], strerror(errno), errno);
10504 lustre_som_swab(attrs);
10507 case LFS_SOM_ATTR_ALL:
10508 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10509 path, attrs->lsa_size, attrs->lsa_blocks,
10513 printf("%llu\n", attrs->lsa_size);
10515 case LFS_SOM_BLOCKS:
10516 printf("%llu\n", attrs->lsa_blocks);
10518 case LFS_SOM_FLAGS:
10519 printf("%x\n", attrs->lsa_valid);
10522 fprintf(stderr, "%s: unknown option\n", progname);
10530 * lfs_mirror_list_commands() - List lfs mirror commands.
10531 * @argc: The count of command line arguments.
10532 * @argv: Array of strings for command line arguments.
10534 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10536 * Return: 0 on success.
10538 static int lfs_mirror_list_commands(int argc, char **argv)
10540 char buffer[81] = "";
10542 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10548 static int lfs_pcc_attach(int argc, char **argv)
10550 struct option long_opts[] = {
10551 { .val = 'i', .name = "id", .has_arg = required_argument },
10552 { .name = NULL } };
10555 __u32 archive_id = 0;
10558 char fullpath[PATH_MAX];
10559 enum lu_pcc_type type = LU_PCC_READWRITE;
10562 while ((c = getopt_long(argc, argv, "i:",
10563 long_opts, NULL)) != -1) {
10566 archive_id = strtoul(optarg, &end, 0);
10567 if (*end != '\0' || archive_id == 0) {
10568 fprintf(stderr, "error: %s: bad archive ID "
10569 "'%s'\n", argv[0], optarg);
10576 fprintf(stderr, "%s: option '%s' unrecognized\n",
10577 argv[0], argv[optind - 1]);
10582 if (argc <= optind) {
10583 fprintf(stderr, "%s: must specify one or more file names\n",
10588 while (optind < argc) {
10591 path = argv[optind++];
10592 if (realpath(path, fullpath) == NULL) {
10593 fprintf(stderr, "%s: could not find path '%s': %s\n",
10594 argv[0], path, strerror(errno));
10600 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10602 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10603 "with archive ID '%u': %s\n", argv[0],
10604 path, archive_id, strerror(-rc2));
10612 static int lfs_pcc_attach_fid(int argc, char **argv)
10614 struct option long_opts[] = {
10615 { .val = 'i', .name = "id", .has_arg = required_argument },
10616 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10617 { .name = NULL } };
10618 char short_opts[] = "i:m:";
10621 __u32 archive_id = 0;
10623 const char *mntpath = NULL;
10624 const char *fidstr;
10625 enum lu_pcc_type type = LU_PCC_READWRITE;
10628 while ((c = getopt_long(argc, argv, short_opts,
10629 long_opts, NULL)) != -1) {
10632 archive_id = strtoul(optarg, &end, 0);
10633 if (*end != '\0') {
10634 fprintf(stderr, "error: %s: bad archive ID "
10635 "'%s'\n", argv[0], optarg);
10645 fprintf(stderr, "%s: option '%s' unrecognized\n",
10646 argv[0], argv[optind - 1]);
10651 if (archive_id == 0) {
10652 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10656 if (mntpath == NULL) {
10657 fprintf(stderr, "%s: must specify Lustre mount point\n",
10662 if (argc <= optind) {
10663 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10667 while (optind < argc) {
10670 fidstr = argv[optind++];
10672 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10675 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10676 "with archive ID '%u': %s\n", argv[0],
10677 fidstr, mntpath, archive_id, strerror(rc2));
10679 if (rc == 0 && rc2 < 0)
10685 static int lfs_pcc_detach(int argc, char **argv)
10687 struct option long_opts[] = {
10688 { .val = 'k', .name = "keep", .has_arg = no_argument },
10689 { .name = NULL } };
10690 char short_opts[] = "k";
10694 char fullpath[PATH_MAX];
10695 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10698 while ((c = getopt_long(argc, argv, short_opts,
10699 long_opts, NULL)) != -1) {
10702 detach_opt = PCC_DETACH_OPT_NONE;
10707 fprintf(stderr, "%s: option '%s' unrecognized\n",
10708 argv[0], argv[optind - 1]);
10713 while (optind < argc) {
10716 path = argv[optind++];
10717 if (realpath(path, fullpath) == NULL) {
10718 fprintf(stderr, "%s: could not find path '%s': %s\n",
10719 argv[0], path, strerror(errno));
10725 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
10728 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
10729 "%s\n", argv[0], path, strerror(errno));
10737 static int lfs_pcc_detach_fid(int argc, char **argv)
10739 struct option long_opts[] = {
10740 { .val = 'k', .name = "keep", .has_arg = no_argument },
10741 { .name = NULL } };
10742 char short_opts[] = "k";
10746 const char *mntpath;
10747 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10750 while ((c = getopt_long(argc, argv, short_opts,
10751 long_opts, NULL)) != -1) {
10754 detach_opt = PCC_DETACH_OPT_NONE;
10759 fprintf(stderr, "%s: option '%s' unrecognized\n",
10760 argv[0], argv[optind - 1]);
10765 mntpath = argv[optind++];
10767 while (optind < argc) {
10770 fid = argv[optind++];
10772 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
10774 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
10775 "from PCC: %s\n", argv[0], fid, mntpath,
10784 static int lfs_pcc_state(int argc, char **argv)
10788 char fullpath[PATH_MAX];
10789 struct lu_pcc_state state;
10794 fprintf(stderr, "%s: must specify one or more file names\n",
10799 while (optind < argc) {
10802 path = argv[optind++];
10803 if (realpath(path, fullpath) == NULL) {
10804 fprintf(stderr, "%s: could not find path '%s': %s\n",
10805 argv[0], path, strerror(errno));
10811 rc2 = llapi_pcc_state_get(fullpath, &state);
10815 fprintf(stderr, "%s: cannot get PCC state of '%s': "
10816 "%s\n", argv[0], path, strerror(-rc2));
10820 printf("file: %s", path);
10821 printf(", type: %s", pcc_type2string(state.pccs_type));
10822 if (state.pccs_type == LU_PCC_NONE &&
10823 state.pccs_open_count == 0) {
10828 printf(", PCC file: %s", state.pccs_path);
10829 printf(", user number: %u", state.pccs_open_count);
10830 printf(", flags: %x", state.pccs_flags);
10837 * lfs_pcc_list_commands() - List lfs pcc commands.
10838 * @argc: The count of command line arguments.
10839 * @argv: Array of strings for command line arguments.
10841 * This function lists lfs pcc commands defined in pcc_cmdlist[].
10843 * Return: 0 on success.
10845 static int lfs_pcc_list_commands(int argc, char **argv)
10847 char buffer[81] = "";
10849 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
10856 * lfs_pcc() - Parse and execute lfs pcc commands.
10857 * @argc: The count of lfs pcc command line arguments.
10858 * @argv: Array of strings for lfs pcc command line arguments.
10860 * This function parses lfs pcc commands and performs the
10861 * corresponding functions specified in pcc_cmdlist[].
10863 * Return: 0 on success or an error code on failure.
10865 static int lfs_pcc(int argc, char **argv)
10867 char cmd[PATH_MAX];
10870 setlinebuf(stdout);
10872 Parser_init("lfs-pcc > ", pcc_cmdlist);
10874 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10876 program_invocation_short_name = cmd;
10878 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
10880 rc = Parser_commands();
10882 return rc < 0 ? -rc : rc;
10885 static int lfs_list_commands(int argc, char **argv)
10887 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10889 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10894 int main(int argc, char **argv)
10898 /* Ensure that liblustreapi constructor has run */
10899 if (!llapi_liblustreapi_initialized())
10900 fprintf(stderr, "liblustreapi was not properly initialized\n");
10902 setlinebuf(stdout);
10905 Parser_init("lfs > ", cmdlist);
10907 progname = program_invocation_short_name; /* Used in error messages */
10909 llapi_set_command_name(argv[1]);
10910 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10911 llapi_clear_command_name();
10913 rc = Parser_commands();
10916 return rc < 0 ? -rc : rc;
10919 #ifdef _LUSTRE_IDL_H_
10920 /* Everything we need here should be included by lustreapi.h. */
10921 # error "lfs should not depend on lustre_idl.h"
10922 #endif /* _LUSTRE_IDL_H_ */