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>> [--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 "\tnew_file: This option indicates the layout of the split\n"
303 "\t mirror will be stored into. If not specified,\n"
304 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
305 "\t will be used.\n" },
306 { .pc_name = "read", .pc_func = lfs_mirror_read,
307 .pc_help = "Read the content of a specified mirror of a file.\n"
308 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
309 "[--outfile|-o <output_file>] <mirrored_file>\n" },
310 { .pc_name = "write", .pc_func = lfs_mirror_write,
311 .pc_help = "Write to a specified mirror of a file.\n"
312 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
313 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
314 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
315 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
316 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
317 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
318 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
319 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
320 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
321 "<mirrored file> [<mirrored file2>...]\n"},
322 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
323 .pc_help = "Verify mirrored file(s).\n"
324 "usage: lfs mirror verify "
325 "[--only <mirror_id,mirror_id2[,...]>] "
326 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
327 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
328 .pc_help = "list commands supported by lfs mirror"},
329 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
330 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
331 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
336 * command_t pcc_cmdlist - lfs pcc commands.
338 command_t pcc_cmdlist[] = {
339 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
340 .pc_help = "Attach given files to the Persistent Client Cache.\n"
341 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
342 "\t-i: archive id for RW-PCC\n" },
343 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
344 .pc_help = "Attach given files into PCC by FID(s).\n"
345 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
347 "\t-i: archive id for RW-PCC\n"
348 "\t-m: Lustre mount point\n" },
349 { .pc_name = "state", .pc_func = lfs_pcc_state,
350 .pc_help = "Display the PCC state for given files.\n"
351 "usage: lfs pcc state <file> ...\n" },
352 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
353 .pc_help = "Detach given files from the Persistent Client Cache.\n"
354 "usage: lfs pcc detach <file> ...\n" },
355 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
356 .pc_help = "Detach given files from PCC by FID(s).\n"
357 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
358 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
359 .pc_help = "list commands supported by lfs pcc"},
360 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
361 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
362 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
366 /* all available commands */
367 command_t cmdlist[] = {
368 {"setstripe", lfs_setstripe, 0,
369 "To create a file with specified striping/composite layout, or\n"
370 "create/replace the default layout on an existing directory:\n"
371 SSM_CMD_COMMON("setstripe")
373 " <directory|filename>\n"
375 "To add component(s) to an existing composite file:\n"
376 SSM_CMD_COMMON("setstripe --component-add")
378 "To totally delete the default striping from an existing directory:\n"
379 "usage: setstripe -d <directory>\n"
381 "To create a mirrored file or set s default mirror layout on a directory:\n"
382 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
384 "To delete the last component(s) from an existing composite file\n"
385 "(note that this will also delete any data in those components):\n"
386 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
387 " [--component-flags|-F <comp_flags>]\n"
389 "\tcomp_id: Unique component ID to delete\n"
390 "\tcomp_flags: 'init' indicating all instantiated components\n"
391 "\t '^init' indicating all uninstantiated components\n"
392 "\t-I and -F cannot be specified at the same time\n"
393 "To create a file with a foreign (free format) layout:\n"
394 "usage: setstripe --foreign[=<foreign_type>]\n"
395 " --xattr|-x <layout_string> [--flags <hex>]\n"
396 " [--mode <mode>] <filename>\n"},
397 {"getstripe", lfs_getstripe, 0,
398 "To list the layout pattern for a given file or files in a\n"
399 "directory or recursively for all files in a directory tree.\n"
400 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
401 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
402 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
403 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
404 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
405 " [--component-id[=comp_id]|-I[comp_id]]\n"
406 " [--component-flags[=comp_flags]]\n"
407 " [--component-count]\n"
408 " [--component-start[=[+-]comp_start]]\n"
409 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
410 " [[!] --mirror-index=[+-]<index> |\n"
411 " [!] --mirror-id=[+-]<id>]\n"
412 " <directory|filename> ..."},
413 {"setdirstripe", lfs_setdirstripe, 0,
414 "To create a striped directory on a specified MDT. This can only\n"
415 "be done on MDT0 with the right of administrator.\n"
416 "usage: setdirstripe [OPTION] <directory>\n"
418 {"getdirstripe", lfs_getdirstripe, 0,
419 "To list the layout pattern info for a given directory\n"
420 "or recursively for all directories in a directory tree.\n"
421 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
422 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
423 " [--recursive|-r] [--yaml|-y]\n"
424 " [--verbose|-v] [--default|-D] <dir> ..."},
425 {"mkdir", lfs_setdirstripe, 0,
426 "To create a striped directory on a specified MDT. This can only\n"
427 "be done on MDT0 with the right of administrator.\n"
428 "usage: mkdir [OPTION] <directory>\n"
430 {"rm_entry", lfs_rmentry, 0,
431 "To remove the name entry of the remote directory. Note: This\n"
432 "command will only delete the name entry, i.e. the remote directory\n"
433 "will become inaccessable after this command. This can only be done\n"
434 "by the administrator\n"
435 "usage: rm_entry <dir>\n"},
436 {"pool_list", lfs_poollist, 0,
437 "List pools or pool OSTs\n"
438 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
439 {"find", lfs_find, 0,
440 "find files matching given attributes recursively in directory tree.\n"
441 "usage: find <directory|filename> ...\n"
442 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
443 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
444 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
445 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
446 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
447 " [[!] --stripe-count|-c [+-]<stripes>]\n"
448 " [[!] --stripe-index|-i <index,...>]\n"
449 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
450 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
451 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
452 " [[!] --projid <projid>]\n"
453 " [[!] --foreign[=<foreign_type>]]\n"
454 " [[!] --layout|-L released,raid0,mdt]\n"
455 " [[!] --foreign[=<foreign_type>]]\n"
456 " [[!] --component-count [+-]<comp_cnt>]\n"
457 " [[!] --component-start [+-]N[kMGTPE]]\n"
458 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
459 " [[!] --component-flags <comp_flags>]\n"
460 " [[!] --mirror-count|-N [+-]<n>]\n"
461 " [[!] --mirror-state <[^]state>]\n"
462 " [[!] --mdt-count|-T [+-]<stripes>]\n"
463 " [[!] --mdt-hash|-H <hashtype>\n"
464 " [[!] --mdt-index|-m <uuid|index,...>]\n"
465 "\t !: used before an option indicates 'NOT' requested attribute\n"
466 "\t -: used before a value indicates less than requested value\n"
467 "\t +: used before a value indicates more than requested value\n"
468 "\thashtype: hash type of the striped directory.\n"
469 "\t fnv_1a_64 FNV-1a hash algorithm\n"
470 "\t all_char sum of characters % MDT_COUNT\n"},
471 {"check", lfs_check, 0,
472 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
473 "or all the servers (MGTs, MDTs and OSTs).\n"
474 "usage: check <mgts|osts|mdts|all>"},
475 {"osts", lfs_osts, 0, "list OSTs connected to client "
476 "[for specified path only]\n" "usage: osts [path]"},
477 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
478 "[for specified path only]\n" "usage: mdts [path]"},
480 "report filesystem disk space usage or inodes usage "
481 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
482 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
483 {"getname", lfs_getname, 0,
484 "list instances and specified mount points [for specified path only]\n"
485 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
486 #ifdef HAVE_SYS_QUOTA_H
487 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
488 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
489 " -b <block-softlimit> -B <block-hardlimit>\n"
490 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
491 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
492 " [--block-softlimit <block-softlimit>]\n"
493 " [--block-hardlimit <block-hardlimit>]\n"
494 " [--inode-softlimit <inode-softlimit>]\n"
495 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
496 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
497 " [--block-grace <block-grace>]\n"
498 " [--inode-grace <inode-grace>] <filesystem>\n"
499 " setquota <-U|-G|-P>\n"
500 " -b <block-softlimit> -B <block-hardlimit>\n"
501 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
502 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
503 " [--block-softlimit <block-softlimit>]\n"
504 " [--block-hardlimit <block-hardlimit>]\n"
505 " [--inode-softlimit <inode-softlimit>]\n"
506 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
507 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
509 " -b can be used instead of --block-softlimit/--block-grace\n"
510 " -B can be used instead of --block-hardlimit\n"
511 " -i can be used instead of --inode-softlimit/--inode-grace\n"
512 " -I can be used instead of --inode-hardlimit\n"
513 " -d can be used instead of --default\n\n"
514 "Note: The total quota space will be split into many qunits and\n"
515 " balanced over all server targets, the minimal qunit size is\n"
516 " 1M bytes for block space and 1K inodes for inode space.\n\n"
517 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
518 " Quota space rebalancing process will stop when this mininum\n"
519 " value is reached. As a result, quota exceeded can be returned\n"
520 " while many targets still have 1MB or 1K inodes of spare\n"
522 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
523 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
525 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
526 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
527 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
528 {"project", lfs_project, 0,
529 "Change or list project attribute for specified file or directory.\n"
530 "usage: project [-d|-r] <file|directory...>\n"
531 " list project ID and flags on file(s) or directories\n"
532 " project [-p id] [-s] [-r] <file|directory...>\n"
533 " set project ID and/or inherit flag for specified file(s) or directories\n"
534 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
535 " check project ID and flags on file(s) or directories, print outliers\n"
536 " project -C [-r] [-k] <file|directory...>\n"
537 " clear the project inherit flag and ID on the file or directory\n"
540 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
541 "usage: flushctx [-k] [mountpoint...]"},
542 {"changelog", lfs_changelog, 0,
543 "Show the metadata changes on an MDT."
544 "\nusage: changelog <mdtname> [startrec [endrec]]"},
545 {"changelog_clear", lfs_changelog_clear, 0,
546 "Indicate that old changelog records up to <endrec> are no longer of "
547 "interest to consumer <id>, allowing the system to free up space.\n"
548 "An <endrec> of 0 means all records.\n"
549 "usage: changelog_clear <mdtname> <id> <endrec>"},
550 {"fid2path", lfs_fid2path, 0,
551 "Resolve the full path(s) for given FID(s). For a specific hardlink "
552 "specify link number <linkno>.\n"
553 /* "For a historical link name, specify changelog record <recno>.\n" */
554 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
555 /* [ --rec <recno> ] */ },
556 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
557 "usage: path2fid [--parents] <path> ..."},
558 {"data_version", lfs_data_version, 0, "Display file data version for "
559 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
560 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
561 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
562 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
563 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
564 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
565 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
567 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
568 "[--archived] [--lost] <file> ..."},
569 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
570 "given files.\n" "usage: hsm_action <file> ..."},
571 {"hsm_archive", lfs_hsm_archive, 0,
572 "Archive file to external storage.\n"
573 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
575 {"hsm_restore", lfs_hsm_restore, 0,
576 "Restore file from external storage.\n"
577 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
578 {"hsm_release", lfs_hsm_release, 0,
579 "Release files from Lustre.\n"
580 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
581 {"hsm_remove", lfs_hsm_remove, 0,
582 "Remove file copy from external storage.\n"
583 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
585 " (FILE [FILE ...] | "
586 "--mntpath MOUNTPATH FID [FID ...])\n"
588 "Note: To remove an archived copy of a file already deleted from a "
590 "--mntpath option and a list of FIDs must be specified"
592 {"hsm_cancel", lfs_hsm_cancel, 0,
593 "Cancel requests related to specified files.\n"
594 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
595 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
596 "usage: swap_layouts <path1> <path2>"},
597 {"migrate", lfs_setstripe_migrate, 0,
598 "migrate a directory between MDTs.\n"
599 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
600 " [--mdt-hash|-H] <hash_type>\n"
601 " [--mdt-index|-m] <start_mdt_index>\n"
604 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
605 " it's the MDT index of first stripe\n"
606 "\tmdt_count: number of MDTs to stripe a directory over\n"
607 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
608 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
609 " all_char sum of characters % MDT_COUNT\n"
611 "migrate file objects from one OST "
612 "layout\nto another (may be not safe with concurent writes).\n"
614 "[--stripe-count|-c] <stripe_count>\n"
615 "[--overstripe-count|-C] <stripe_count>\n"
616 " [--stripe-index|-i] <start_ost_index>\n"
617 " [--stripe-size|-S] <stripe_size>\n"
618 " [--pool|-p] <pool_name>\n"
619 " [--ost|-o] <ost_indices>\n"
621 " [--non-block|-n]\n"
622 " [--non-direct|-D]\n"
623 " <file|directory>\n"
624 "\tstripe_count: number of OSTs to stripe a file over\n"
625 "\t Using -C instead of -c allows overstriping, which\n"
626 "\t will place more than one stripe per OST if\n"
627 "\t stripe_count is greater than the number of OSTs\n"
628 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
629 "\tstripe_size: number of bytes to store before moving to the next OST\n"
630 "\tpool_name: name of the predefined pool of OSTs\n"
631 "\tost_indices: OSTs to stripe over, in order\n"
632 "\tblock: Block file access during data migration (default)\n"
633 "\tnon-block: Abort migrations if concurrent access is detected\n"
634 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
636 "To move directories between MDTs. This command is deprecated, "
637 "use \"migrate\" instead.\n"
638 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
640 {"ladvise", lfs_ladvise, 0,
641 "Provide servers with advice about access patterns for a file.\n"
642 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
643 " [--background|-b] [--unset|-u]\n\n"
644 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
645 " {[--mode|-m [READ,WRITE]}\n"
647 {"mirror", lfs_mirror, mirror_cmdlist,
648 "lfs commands used to manage files with mirrored components:\n"
649 "lfs mirror create - create a mirrored file or directory\n"
650 "lfs mirror extend - add mirror(s) to an existing file\n"
651 "lfs mirror split - split a mirror from an existing mirrored file\n"
652 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
653 "lfs mirror read - read a mirror content of a mirrored file\n"
654 "lfs mirror write - write to a mirror of a mirrored file\n"
655 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
656 "lfs mirror verify - verify mirrored file(s)\n"},
657 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
658 "usage: getsom [-s] [-b] [-f] <path>\n"
659 "\t-s: Only show the size value of the SOM data for a given file\n"
660 "\t-b: Only show the blocks value of the SOM data for a given file\n"
661 "\t-f: Only show the flags value of the SOM data for a given file\n"},
662 {"heat_get", lfs_heat_get, 0,
663 "To get heat of files.\n"
664 "usage: heat_get <file> ...\n"},
665 {"heat_set", lfs_heat_set, 0,
666 "To set heat flags of files.\n"
667 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
668 "\t--clear|-c: Clear file heat for given files\n"
669 "\t--off|-o: Turn off file heat for given files\n"
670 "\t--on|-O: Turn on file heat for given files\n"},
671 {"pcc", lfs_pcc, pcc_cmdlist,
672 "lfs commands used to interact with PCC features:\n"
673 "lfs pcc attach - attach given files to Persistent Client Cache\n"
674 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
675 "lfs pcc state - display the PCC state for given files\n"
676 "lfs pcc detach - detach given files from Persistent Client Cache\n"
677 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
678 {"help", Parser_help, 0, "help"},
679 {"exit", Parser_quit, 0, "quit"},
680 {"quit", Parser_quit, 0, "quit"},
681 {"--version", Parser_version, 0,
682 "output build version of the utility and exit"},
683 {"--list-commands", lfs_list_commands, 0,
684 "list commands supported by the utility and exit"},
689 static int check_hashtype(const char *hashtype)
693 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
694 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
697 if (!strcmp(hashtype, LMV_HASH_NAME_SPACE))
698 return LMV_HASH_TYPE_DEFAULT | LMV_HASH_FLAG_SPACE;
703 static uint32_t check_foreign_type_name(const char *foreign_type_name)
707 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
708 if (lu_foreign_types[i].lft_name == NULL)
710 if (strcmp(foreign_type_name,
711 lu_foreign_types[i].lft_name) == 0)
712 return lu_foreign_types[i].lft_type;
715 return LU_FOREIGN_TYPE_UNKNOWN;
718 static const char *error_loc = "syserror";
721 MIGRATION_NONBLOCK = 0x0001,
722 MIGRATION_MIRROR = 0x0002,
723 MIGRATION_NONDIRECT = 0x0004,
724 MIGRATION_VERBOSE = 0x0008,
727 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
728 struct llapi_layout *layout);
731 migrate_open_files(const char *name, __u64 migration_flags,
732 const struct llapi_stripe_param *param,
733 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
740 char parent[PATH_MAX];
741 char volatile_file[PATH_MAX];
747 if (param == NULL && layout == NULL) {
748 error_loc = "layout information";
752 /* search for file directory pathname */
753 if (strlen(name) > sizeof(parent) - 1) {
754 error_loc = "source file name";
758 strncpy(parent, name, sizeof(parent));
759 ptr = strrchr(parent, '/');
761 if (getcwd(parent, sizeof(parent)) == NULL) {
762 error_loc = "getcwd";
766 if (ptr == parent) /* leading '/' */
771 /* open file, direct io */
772 /* even if the file is only read, WR mode is nedeed to allow
773 * layout swap on fd */
775 if (!(migration_flags & MIGRATION_NONDIRECT))
777 fd = open(name, rflags);
780 error_loc = "cannot open source file";
784 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
786 error_loc = "cannot get MDT index";
791 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
792 mode_t open_mode = S_IRUSR | S_IWUSR;
794 random_value = random();
795 rc = snprintf(volatile_file, sizeof(volatile_file),
796 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
797 mdt_index, random_value);
798 if (rc >= sizeof(volatile_file)) {
803 /* create, open a volatile file, use caching (ie no directio) */
805 fdv = llapi_file_open_param(volatile_file, open_flags,
808 fdv = lfs_component_create(volatile_file, open_flags,
810 } while (fdv < 0 && (rc = fdv) == -EEXIST);
813 error_loc = "cannot create volatile file";
817 /* In case the MDT does not support creation of volatile files
818 * we should try to unlink it. */
819 (void)unlink(volatile_file);
821 /* Not-owner (root?) special case.
822 * Need to set owner/group of volatile file like original.
823 * This will allow to pass related check during layout_swap.
828 error_loc = "cannot stat source file";
832 rc = fstat(fdv, &stv);
835 error_loc = "cannot stat volatile";
839 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
840 rc = fchown(fdv, st.st_uid, st.st_gid);
843 error_loc = "cannot change ownwership of volatile";
862 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
864 struct llapi_layout *layout;
865 size_t buf_size = 4 * 1024 * 1024;
874 layout = llapi_layout_get_by_fd(fd_src, 0);
875 if (layout != NULL) {
876 uint64_t stripe_size;
878 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
880 buf_size = stripe_size;
882 llapi_layout_free(layout);
885 /* Use a page-aligned buffer for direct I/O */
886 rc = posix_memalign(&buf, getpagesize(), buf_size);
891 /* read new data only if we have written all
892 * previously read data */
895 rc = check_file(fd_src);
900 rsize = read(fd_src, buf, buf_size);
912 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
931 static int migrate_copy_timestamps(int fd, int fdv)
935 if (fstat(fd, &st) == 0) {
936 struct timeval tv[2] = {
937 {.tv_sec = st.st_atime},
938 {.tv_sec = st.st_mtime}
941 return futimes(fdv, tv);
947 static int migrate_block(int fd, int fdv)
954 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
956 error_loc = "cannot get dataversion";
964 /* The grouplock blocks all concurrent accesses to the file.
965 * It has to be taken after llapi_get_data_version as it would
967 rc = llapi_group_lock(fd, gid);
969 error_loc = "cannot get group lock";
973 rc = migrate_copy_data(fd, fdv, NULL);
975 error_loc = "data copy failed";
979 /* Make sure we keep original atime/mtime values */
980 rc = migrate_copy_timestamps(fd, fdv);
982 error_loc = "timestamp copy failed";
987 * for a migration we need to check data version on file did
990 * Pass in gid=0 since we already own grouplock. */
991 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
992 SWAP_LAYOUTS_CHECK_DV1);
994 error_loc = "file changed";
997 error_loc = "cannot swap layout";
1002 rc2 = llapi_group_unlock(fd, gid);
1003 if (rc2 < 0 && rc == 0) {
1004 error_loc = "unlock group lock";
1012 * Internal helper for migrate_copy_data(). Check lease and report error if
1015 * \param[in] fd File descriptor on which to check the lease.
1017 * \retval 0 Migration can keep on going.
1018 * \retval -errno Error occurred, abort migration.
1020 static int check_lease(int fd)
1024 rc = llapi_lease_check(fd);
1026 return 0; /* llapi_check_lease returns > 0 on success. */
1031 static int migrate_nonblock(int fd, int fdv)
1037 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1039 error_loc = "cannot get data version";
1043 rc = migrate_copy_data(fd, fdv, check_lease);
1045 error_loc = "data copy failed";
1049 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1051 error_loc = "cannot get data version";
1057 error_loc = "source file changed";
1061 /* Make sure we keep original atime/mtime values */
1062 rc = migrate_copy_timestamps(fd, fdv);
1064 error_loc = "timestamp copy failed";
1071 static int lfs_component_set(char *fname, int comp_id,
1072 __u32 flags, __u32 neg_flags)
1075 __u32 flags_array[2];
1080 ids[count] = comp_id;
1081 flags_array[count] = flags;
1086 ids[count] = comp_id;
1087 flags_array[count] = neg_flags | LCME_FL_NEG;
1091 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1094 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1095 progname, comp_id, fname, flags, neg_flags);
1100 static int lfs_component_del(char *fname, __u32 comp_id,
1101 __u32 flags, __u32 neg_flags)
1105 if (flags && neg_flags)
1108 if (!flags && neg_flags)
1109 flags = neg_flags | LCME_FL_NEG;
1111 if ((flags && comp_id) || (!flags && !comp_id))
1115 if (flags & ~LCME_KNOWN_FLAGS) {
1117 "%s setstripe: unknown flags %#x\n",
1121 } else if (comp_id > LCME_ID_MAX) {
1122 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1127 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1130 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1131 progname, comp_id, fname, strerror(errno));
1135 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1142 rc = llapi_layout_file_comp_add(fname, layout);
1144 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1145 fname, strerror(errno));
1149 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1150 struct llapi_layout *layout)
1158 fd = lstat(fname, &st);
1159 if (fd == 0 && S_ISDIR(st.st_mode))
1160 open_flags = O_DIRECTORY | O_RDONLY;
1162 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1164 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1165 S_ISDIR(st.st_mode) ?
1166 "set default composite layout for" :
1167 "create composite file",
1168 fname, strerror(errno));
1172 static int lfs_migrate(char *name, __u64 migration_flags,
1173 struct llapi_stripe_param *param,
1174 struct llapi_layout *layout)
1180 rc = migrate_open_files(name, migration_flags, param, layout,
1185 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1186 /* Blocking mode (forced if servers do not support file lease).
1187 * It is also the default mode, since we cannot distinguish
1188 * between a broken lease and a server that does not support
1189 * atomic swap/close (LU-6785) */
1190 rc = migrate_block(fd, fdv);
1194 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1196 error_loc = "cannot get lease";
1200 rc = migrate_nonblock(fd, fdv);
1202 llapi_lease_release(fd);
1206 /* Atomically put lease, swap layouts and close.
1207 * for a migration we need to check data version on file did
1209 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1211 error_loc = "cannot swap layout";
1223 fprintf(stderr, "error: %s: %s: %s: %s\n",
1224 progname, name, error_loc, strerror(-rc));
1225 else if (migration_flags & MIGRATION_VERBOSE)
1226 printf("%s\n", name);
1231 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1240 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1244 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1245 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1246 const char *comp_name = comp_flags_table[i].cfn_name;
1248 if (strcmp(name, comp_name) == 0) {
1249 *flags |= comp_flag;
1251 } else if (strncmp(name, "^", 1) == 0 &&
1252 strcmp(name + 1, comp_name) == 0) {
1253 *neg_flags |= comp_flag;
1258 llapi_printf(LLAPI_MSG_ERROR,
1259 "%s: component flag '%s' not supported\n",
1265 if (!*flags && !*neg_flags)
1268 /* don't allow to set and exclude the same flag */
1269 if (*flags & *neg_flags)
1275 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1283 if (strncmp(string, "^", 1) == 0) {
1284 *neg_state = llapi_layout_string_flags(string + 1);
1285 if (*neg_state != 0)
1288 *state = llapi_layout_string_flags(string);
1293 llapi_printf(LLAPI_MSG_ERROR,
1294 "%s: mirrored file state '%s' not supported\n",
1300 * struct mirror_args - Command-line arguments for mirror(s).
1301 * @m_count: Number of mirrors to be created with this layout.
1302 * @m_flags: Mirror level flags, only 'prefer' is supported.
1303 * @m_layout: Mirror layout.
1304 * @m_file: A victim file. Its layout will be split and used as a mirror.
1305 * @m_next: Point to the next node of the list.
1307 * Command-line arguments for mirror(s) will be parsed and stored in
1308 * a linked list that consists of this structure.
1310 struct mirror_args {
1313 struct llapi_layout *m_layout;
1315 struct mirror_args *m_next;
1318 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1323 rc = llapi_layout_comp_flags_get(layout, &flags);
1327 if (flags & LCME_FL_NEG) {
1328 fprintf(stderr, "error: %s: negative flags are not supported\n",
1333 if (flags & LCME_FL_STALE) {
1334 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1335 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1339 return LLAPI_LAYOUT_ITER_CONT;
1342 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1344 uint64_t start, end;
1348 /* LU-10112: do not support dom+flr in phase 1 */
1349 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1353 rc = llapi_layout_pattern_get(layout, &pattern);
1357 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1358 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1363 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1367 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1371 if (end != LUSTRE_EOF) {
1372 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1377 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1382 * enum mirror_flags - Flags for extending a mirrored file.
1383 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1384 * in case the victim file(s) contains the same data as the
1385 * original mirrored file.
1386 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1387 * @MF_COMP_ID: specified component id instead of mirror id
1389 * Flags for extending a mirrored file.
1398 * mirror_create_sanity_check() - Check mirror list.
1399 * @list: A linked list that stores the mirror arguments.
1401 * This function does a sanity check on @list for creating
1404 * Return: 0 on success or a negative error code on failure.
1406 static int mirror_create_sanity_check(const char *fname,
1407 struct mirror_args *list)
1410 bool has_m_file = false;
1411 bool has_m_layout = false;
1417 struct llapi_layout *layout;
1419 layout = llapi_layout_get_by_path(fname, 0);
1422 "error: %s: file '%s' couldn't get layout\n",
1427 rc = mirror_sanity_check_one(layout);
1428 llapi_layout_free(layout);
1434 while (list != NULL) {
1435 if (list->m_file != NULL) {
1437 llapi_layout_free(list->m_layout);
1440 llapi_layout_get_by_path(list->m_file, 0);
1441 if (list->m_layout == NULL) {
1443 "error: %s: file '%s' has no layout\n",
1444 progname, list->m_file);
1448 has_m_layout = true;
1449 if (list->m_layout == NULL) {
1450 fprintf(stderr, "error: %s: no mirror layout\n",
1456 rc = mirror_sanity_check_one(list->m_layout);
1460 list = list->m_next;
1463 if (has_m_file && has_m_layout) {
1465 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1473 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1475 __u32 mirror_flags = *(__u32 *)cbdata;
1479 rc = llapi_layout_comp_flags_get(layout, &flags);
1484 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1489 return LLAPI_LAYOUT_ITER_CONT;
1493 * mirror_create() - Create a mirrored file.
1494 * @fname: The file to be created.
1495 * @mirror_list: A linked list that stores the mirror arguments.
1497 * This function creates a mirrored file @fname with the mirror(s)
1498 * from @mirror_list.
1500 * Return: 0 on success or a negative error code on failure.
1502 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1504 struct llapi_layout *layout = NULL;
1505 struct mirror_args *cur_mirror = NULL;
1506 uint16_t mirror_count = 0;
1510 rc = mirror_create_sanity_check(NULL, mirror_list);
1514 cur_mirror = mirror_list;
1515 while (cur_mirror != NULL) {
1516 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1518 &cur_mirror->m_flags);
1521 fprintf(stderr, "%s: failed to set mirror flags\n",
1526 for (i = 0; i < cur_mirror->m_count; i++) {
1527 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1530 fprintf(stderr, "error: %s: "
1531 "merge layout failed: %s\n",
1532 progname, strerror(errno));
1536 mirror_count += cur_mirror->m_count;
1537 cur_mirror = cur_mirror->m_next;
1540 if (layout == NULL) {
1541 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1545 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1548 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1549 progname, strerror(errno));
1553 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1561 llapi_layout_free(layout);
1566 * Compare files and check lease on @fd.
1568 * \retval bytes number of bytes are the same
1570 static ssize_t mirror_file_compare(int fd, int fdv)
1572 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1574 ssize_t bytes_done = 0;
1575 ssize_t bytes_read = 0;
1577 buf = malloc(buflen * 2);
1582 if (!llapi_lease_check(fd)) {
1583 bytes_done = -EBUSY;
1587 bytes_read = read(fd, buf, buflen);
1588 if (bytes_read <= 0)
1591 if (bytes_read != read(fdv, buf + buflen, buflen))
1594 /* XXX: should compute the checksum on each buffer and then
1595 * compare checksum to avoid cache collision */
1596 if (memcmp(buf, buf + buflen, bytes_read))
1599 bytes_done += bytes_read;
1607 static int mirror_extend_file(const char *fname, const char *victim_file,
1608 enum mirror_flags mirror_flags)
1613 struct stat stbuf_v;
1614 struct ll_ioc_lease *data = NULL;
1617 fd = open(fname, O_RDWR);
1619 error_loc = "open source file";
1624 fdv = open(victim_file, O_RDWR);
1626 error_loc = "open target file";
1631 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1632 error_loc = "stat source or target file";
1637 if (stbuf.st_dev != stbuf_v.st_dev) {
1638 error_loc = "stat source and target file";
1643 /* mirrors should be of the same size */
1644 if (stbuf.st_size != stbuf_v.st_size) {
1645 error_loc = "file sizes don't match";
1650 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1652 error_loc = "cannot get lease";
1656 if (!(mirror_flags & MF_NO_VERIFY)) {
1658 /* mirrors should have the same contents */
1659 ret = mirror_file_compare(fd, fdv);
1660 if (ret != stbuf.st_size) {
1661 error_loc = "file busy or contents don't match";
1662 rc = ret < 0 ? ret : -EINVAL;
1667 /* Get rid of caching pages from clients */
1668 rc = llapi_file_flush(fd);
1670 error_loc = "cannot get data version";
1674 rc = llapi_file_flush(fdv);
1676 error_loc = "cannot get data version";
1681 /* Make sure we keep original atime/mtime values */
1682 rc = migrate_copy_timestamps(fd, fdv);
1684 error_loc = "cannot copy timestamp";
1688 /* Atomically put lease, merge layouts and close. */
1689 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1691 error_loc = "memory allocation";
1694 data->lil_mode = LL_LEASE_UNLCK;
1695 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1696 data->lil_count = 1;
1697 data->lil_ids[0] = fdv;
1698 rc = llapi_lease_set(fd, data);
1700 error_loc = "cannot merge layout";
1702 } else if (rc == 0) {
1704 error_loc = "lost lease lock";
1717 (void) unlink(victim_file);
1719 fprintf(stderr, "error: %s: %s: %s: %s\n",
1720 progname, fname, error_loc, strerror(-rc));
1724 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1726 struct ll_ioc_lease *data = NULL;
1731 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1735 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1737 error_loc = "cannot get lease";
1741 rc = migrate_nonblock(fd, fdv);
1743 llapi_lease_release(fd);
1747 /* Atomically put lease, merge layouts and close. */
1748 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1750 error_loc = "memory allocation";
1753 data->lil_mode = LL_LEASE_UNLCK;
1754 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1755 data->lil_count = 1;
1756 data->lil_ids[0] = fdv;
1757 rc = llapi_lease_set(fd, data);
1759 error_loc = "cannot merge layout";
1761 } else if (rc == 0) {
1763 error_loc = "lost lease lock";
1776 fprintf(stderr, "error: %s: %s: %s: %s\n",
1777 progname, name, error_loc, strerror(-rc));
1781 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1782 enum mirror_flags mirror_flags)
1786 rc = mirror_create_sanity_check(fname, mirror_list);
1790 while (mirror_list) {
1791 if (mirror_list->m_file != NULL) {
1792 rc = mirror_extend_file(fname, mirror_list->m_file,
1795 __u32 mirror_count = mirror_list->m_count;
1797 while (mirror_count > 0) {
1798 rc = mirror_extend_layout(fname,
1799 mirror_list->m_layout);
1809 mirror_list = mirror_list->m_next;
1815 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1820 rc = llapi_layout_mirror_id_get(layout, &id);
1824 if ((__u16)id == *(__u16 *)cbdata)
1825 return LLAPI_LAYOUT_ITER_STOP;
1827 return LLAPI_LAYOUT_ITER_CONT;
1830 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1835 rc = llapi_layout_comp_id_get(layout, &id);
1839 if (id == *(__u32 *)cbdata)
1840 return LLAPI_LAYOUT_ITER_STOP;
1842 return LLAPI_LAYOUT_ITER_CONT;
1844 static int mirror_split(const char *fname, __u32 id,
1845 enum mirror_flags mflags, const char *victim_file)
1847 struct llapi_layout *layout;
1848 char parent[PATH_MAX];
1849 char victim[PATH_MAX];
1850 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1852 struct ll_ioc_lease *data;
1853 uint16_t mirror_count;
1858 /* check fname contains mirror with mirror_id/comp_id */
1859 layout = llapi_layout_get_by_path(fname, 0);
1862 "error %s: file '%s' couldn't get layout\n",
1867 rc = mirror_sanity_check_one(layout);
1871 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1874 "error %s: file '%s' couldn't get mirror count\n",
1878 if (mirror_count < 2) {
1880 "error %s: file '%s' has %d component, cannot split\n",
1881 progname, fname, mirror_count);
1885 if (mflags & MF_COMP_ID) {
1886 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1887 id = mirror_id_of(id);
1889 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1892 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1895 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1897 "error %s: file '%s' does not contain mirror with id %u\n",
1898 progname, fname, id);
1902 fd = open(fname, O_RDWR);
1905 "error %s: open file '%s' failed: %s\n",
1906 progname, fname, strerror(errno));
1910 /* get victim file directory pathname */
1911 if (strlen(fname) > sizeof(parent) - 1) {
1912 fprintf(stderr, "error %s: file name of '%s' too long\n",
1917 strncpy(parent, fname, sizeof(parent));
1918 ptr = strrchr(parent, '/');
1920 if (getcwd(parent, sizeof(parent)) == NULL) {
1921 fprintf(stderr, "error %s: getcwd failed: %s\n",
1922 progname, strerror(errno));
1932 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1934 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1939 if (victim_file == NULL) {
1940 /* use a temp file to store the splitted layout */
1941 if (mflags & MF_DESTROY) {
1942 fdv = llapi_create_volatile_idx(parent, mdt_index,
1943 O_LOV_DELAY_CREATE);
1945 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1947 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1950 /* user specified victim file */
1951 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1956 "error %s: create victim file failed: %s\n",
1957 progname, strerror(errno));
1961 /* get lease lock of fname */
1962 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1965 "error %s: cannot get lease of file '%s': %d\n",
1966 progname, fname, rc);
1970 /* Atomatically put lease, split layouts and close. */
1971 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1977 data->lil_mode = LL_LEASE_UNLCK;
1978 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1979 data->lil_count = 2;
1980 data->lil_ids[0] = fdv;
1981 data->lil_ids[1] = id;
1982 rc = llapi_lease_set(fd, data);
1984 if (rc == 0) /* lost lease lock */
1987 "error %s: cannot split '%s': %s\n",
1988 progname, fname, strerror(-rc));
1999 llapi_layout_free(layout);
2004 * Parse a string containing an target index list into an array of integers.
2006 * The input string contains a comma delimited list of individual
2007 * indices and ranges, for example "1,2-4,7". Add the indices into the
2008 * \a tgts array and remove duplicates.
2010 * \param[out] tgts array to store indices in
2011 * \param[in] size size of \a tgts array
2012 * \param[in] offset starting index in \a tgts
2013 * \param[in] arg string containing OST index list
2014 * \param[in/out] overstriping index list may contain duplicates
2016 * \retval positive number of indices in \a tgts
2017 * \retval -EINVAL unable to parse \a arg
2019 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2020 unsigned long long *pattern)
2024 int slots = size - offset;
2026 bool overstriped = false;
2032 end_of_loop = false;
2033 while (!end_of_loop) {
2034 int start_index = 0;
2037 char *endptr = NULL;
2041 ptr = strchrnul(arg, ',');
2043 end_of_loop = *ptr == '\0';
2046 start_index = strtol(arg, &endptr, 0);
2047 if (endptr == arg) /* no data at all */
2049 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2052 end_index = start_index;
2053 if (*endptr == '-') {
2054 end_index = strtol(endptr + 1, &endptr, 0);
2055 if (*endptr != '\0')
2057 if (end_index < start_index)
2061 for (i = start_index; i <= end_index && slots > 0; i++) {
2064 /* remove duplicate */
2065 for (j = 0; j < offset; j++) {
2066 if (tgts[j] == i && pattern &&
2067 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2069 else if (tgts[j] == i)
2075 if (j == offset) { /* check complete */
2081 if (slots == 0 && i < end_index)
2089 if (!end_of_loop && ptr != NULL)
2092 if (!overstriped && pattern)
2093 *pattern = LLAPI_LAYOUT_DEFAULT;
2095 return rc < 0 ? rc : nr;
2098 struct lfs_setstripe_args {
2099 unsigned long long lsa_comp_end;
2100 unsigned long long lsa_stripe_size;
2101 unsigned long long lsa_extension_size;
2102 long long lsa_stripe_count;
2103 long long lsa_stripe_off;
2104 __u32 lsa_comp_flags;
2105 __u32 lsa_comp_neg_flags;
2106 unsigned long long lsa_pattern;
2107 unsigned int lsa_mirror_count;
2109 bool lsa_first_comp;
2110 bool lsa_extension_comp;
2112 char *lsa_pool_name;
2115 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2117 unsigned int mirror_count = lsa->lsa_mirror_count;
2118 bool first_comp = lsa->lsa_first_comp;
2120 memset(lsa, 0, sizeof(*lsa));
2122 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2123 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2124 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2125 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2126 lsa->lsa_pool_name = NULL;
2128 lsa->lsa_mirror_count = mirror_count;
2129 lsa->lsa_first_comp = first_comp;
2133 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2134 * @lsa: Stripe options to be initialized and inherited.
2136 * This function initializes stripe options in @lsa and inherit
2137 * stripe_size, stripe_count and OST pool_name options.
2141 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2143 unsigned long long stripe_size;
2144 long long stripe_count;
2145 char *pool_name = NULL;
2147 stripe_size = lsa->lsa_stripe_size;
2148 stripe_count = lsa->lsa_stripe_count;
2149 pool_name = lsa->lsa_pool_name;
2151 setstripe_args_init(lsa);
2153 lsa->lsa_stripe_size = stripe_size;
2154 lsa->lsa_stripe_count = stripe_count;
2155 lsa->lsa_pool_name = pool_name;
2158 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2160 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2161 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2162 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2163 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2164 lsa->lsa_pool_name != NULL ||
2165 lsa->lsa_comp_end != 0);
2169 * comp_args_to_layout() - Create or extend a composite layout.
2170 * @composite: Pointer to the composite layout.
2171 * @lsa: Stripe options for the new component.
2173 * This function creates or extends a composite layout by adding a new
2174 * component with stripe options from @lsa.
2176 * Return: 0 on success or an error code on failure.
2178 static int comp_args_to_layout(struct llapi_layout **composite,
2179 struct lfs_setstripe_args *lsa,
2182 struct llapi_layout *layout = *composite;
2183 uint64_t prev_end = 0;
2188 if (layout == NULL) {
2189 layout = llapi_layout_alloc();
2190 if (layout == NULL) {
2191 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2195 *composite = layout;
2196 lsa->lsa_first_comp = true;
2200 /* Get current component extent, current component
2201 * must be the tail component. */
2202 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2204 fprintf(stderr, "Get comp extent failed. %s\n",
2209 if (lsa->lsa_first_comp)
2212 if (lsa->lsa_first_comp)
2213 rc = llapi_layout_add_first_comp(layout);
2215 rc = llapi_layout_comp_add(layout);
2217 fprintf(stderr, "Add component failed. %s\n",
2223 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2225 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2226 lsa->lsa_comp_flags, strerror(errno));
2231 uint64_t comp_end = lsa->lsa_comp_end;
2233 /* Extension space handling:
2234 * If this is the first component, length is extension_size.
2235 * If not, it is zero length, so it can be removed if there is
2236 * insufficient space to extend it.
2238 if (lsa->lsa_extension_comp && lsa->lsa_first_comp)
2239 comp_end = prev_end + lsa->lsa_extension_size;
2240 else if (lsa->lsa_extension_comp)
2241 comp_end = prev_end;
2243 rc = llapi_layout_comp_extent_set(layout, prev_end,
2246 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2247 prev_end, comp_end, strerror(errno));
2251 /* reset lsa_first_comp */
2252 lsa->lsa_first_comp = false;
2254 /* Data-on-MDT component setting */
2255 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2256 /* In case of Data-on-MDT patterns the only extra option
2257 * applicable is stripe size option. */
2258 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2259 fprintf(stderr, "Option 'stripe-count' can't be "
2260 "specified with Data-on-MDT component: %lld\n",
2261 lsa->lsa_stripe_count);
2264 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2265 fprintf(stderr, "Option 'stripe-size' can't be "
2266 "specified with Data-on-MDT component: %llu\n",
2267 lsa->lsa_stripe_size);
2270 if (lsa->lsa_nr_tgts != 0) {
2271 fprintf(stderr, "Option 'ost-list' can't be specified "
2272 "with Data-on-MDT component: '%i'\n",
2276 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2277 fprintf(stderr, "Option 'stripe-offset' can't be "
2278 "specified with Data-on-MDT component: %lld\n",
2279 lsa->lsa_stripe_off);
2282 if (lsa->lsa_pool_name != 0) {
2283 fprintf(stderr, "Option 'pool' can't be specified "
2284 "with Data-on-MDT component: '%s'\n",
2285 lsa->lsa_pool_name);
2289 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2291 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2292 lsa->lsa_pattern, strerror(errno));
2295 /* Data-on-MDT component has always single stripe up to end */
2296 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2297 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2298 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2300 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2301 lsa->lsa_pattern, strerror(errno));
2306 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2307 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2309 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2310 rc = llapi_layout_extension_size_set(layout, size);
2312 rc = llapi_layout_stripe_size_set(layout, size);
2315 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2316 size, strerror(errno));
2320 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2322 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2323 lsa->lsa_stripe_count, strerror(errno));
2327 if (lsa->lsa_pool_name != NULL) {
2328 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2330 fprintf(stderr, "Set pool name: %s failed. %s\n",
2331 lsa->lsa_pool_name, strerror(errno));
2335 rc = llapi_layout_pool_name_set(layout, "");
2337 fprintf(stderr, "Clear pool name failed: %s\n",
2343 if (lsa->lsa_nr_tgts > 0) {
2344 if (lsa->lsa_stripe_count > 0 &&
2345 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2346 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2347 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2348 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2349 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2352 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2353 rc = llapi_layout_ost_index_set(layout, i,
2358 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2359 lsa->lsa_stripe_off != -1) {
2360 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2363 fprintf(stderr, "Set ost index %d failed. %s\n",
2364 i, strerror(errno));
2368 /* Create the second, virtual component of extension space */
2369 if (lsa->lsa_extension_comp) {
2370 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2371 lsa->lsa_extension_comp = false;
2378 static int build_component(struct llapi_layout **layout,
2379 struct lfs_setstripe_args *lsa, bool set_extent)
2383 rc = comp_args_to_layout(layout, lsa, set_extent);
2387 if (lsa->lsa_mirror_count > 0) {
2388 rc = llapi_layout_mirror_count_set(*layout,
2389 lsa->lsa_mirror_count);
2393 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2396 lsa->lsa_mirror_count = 0;
2402 static int build_layout_from_yaml_node(struct cYAML *node,
2403 struct llapi_layout **layout,
2404 struct lfs_setstripe_args *lsa,
2411 if (node->cy_type == CYAML_TYPE_OBJECT) {
2412 /* go deep to sub blocks */
2413 rc = build_layout_from_yaml_node(node->cy_child, layout,
2418 if (node->cy_string == NULL)
2421 string = node->cy_string;
2422 /* skip leading lmm_ if present, to simplify parsing */
2423 if (strncmp(string, "lmm_", 4) == 0)
2426 if (node->cy_type == CYAML_TYPE_STRING) {
2427 if (!strcmp(string, "lcme_extent.e_end")) {
2428 if (!strcmp(node->cy_valuestring, "EOF") ||
2429 !strcmp(node->cy_valuestring, "eof"))
2430 lsa->lsa_comp_end = LUSTRE_EOF;
2431 } else if (!strcmp(string, "pool")) {
2432 lsa->lsa_pool_name = node->cy_valuestring;
2433 } else if (!strcmp(string, "pattern")) {
2434 if (!strcmp(node->cy_valuestring, "mdt"))
2435 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2436 if (!strcmp(node->cy_valuestring,
2437 "raid0,overstriped"))
2439 LLAPI_LAYOUT_OVERSTRIPING;
2440 } else if (!strcmp(string, "lcme_flags")) {
2441 rc = comp_str2flags(node->cy_valuestring,
2442 &lsa->lsa_comp_flags,
2443 &lsa->lsa_comp_neg_flags);
2446 /* Only template flags have meaning in
2447 * the layout for a new file
2449 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2451 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2452 if (!strcmp(string, "lcm_mirror_count")) {
2453 lsa->lsa_mirror_count = node->cy_valueint;
2454 } else if (!strcmp(string, "lcme_extent.e_start")) {
2455 if (node->cy_valueint != 0 || *layout != NULL) {
2456 rc = build_component(layout, lsa, true);
2461 if (node->cy_valueint == 0)
2462 lsa->lsa_first_comp = true;
2464 /* initialize lsa */
2465 setstripe_args_init(lsa);
2466 lsa->lsa_tgts = osts;
2467 } else if (!strcmp(string, "lcme_extent.e_end")) {
2468 if (node->cy_valueint == -1)
2469 lsa->lsa_comp_end = LUSTRE_EOF;
2471 lsa->lsa_comp_end = node->cy_valueint;
2472 } else if (!strcmp(string, "stripe_count")) {
2473 lsa->lsa_stripe_count = node->cy_valueint;
2474 } else if (!strcmp(string, "stripe_size")) {
2475 lsa->lsa_stripe_size = node->cy_valueint;
2476 } else if (!strcmp(string, "stripe_offset")) {
2477 lsa->lsa_stripe_off = node->cy_valueint;
2478 } else if (!strcmp(string, "l_ost_idx")) {
2479 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2484 node = node->cy_next;
2490 static int lfs_comp_create_from_yaml(char *template,
2491 struct llapi_layout **layout,
2492 struct lfs_setstripe_args *lsa,
2495 struct cYAML *tree = NULL, *err_rc = NULL;
2498 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2500 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2501 progname, template);
2502 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2503 "can't parse", &err_rc);
2504 cYAML_print_tree2file(stderr, err_rc);
2505 cYAML_free_tree(err_rc);
2510 /* initialize lsa for plain file */
2511 setstripe_args_init(lsa);
2512 lsa->lsa_tgts = osts;
2514 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2516 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2517 progname, template);
2520 rc = build_component(layout, lsa, *layout != NULL);
2522 /* clean clean lsa */
2523 setstripe_args_init(lsa);
2527 cYAML_free_tree(tree);
2531 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2532 * end of the last component in the existing file, and adjust the
2533 * first extent start of the components to be added accordingly. */
2534 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2536 struct llapi_layout *head;
2537 uint64_t start, end, stripe_size, prev_end = 0;
2540 if (layout == NULL) {
2542 "%s setstripe: layout must be specified\n",
2548 head = llapi_layout_get_by_path(fname, 0);
2551 "%s setstripe: cannot read layout from '%s': %s\n",
2552 progname, fname, strerror(errno));
2554 } else if (errno == ENODATA) {
2555 /* file without LOVEA, this component-add will be turned
2556 * into a component-create. */
2557 llapi_layout_free(head);
2559 } else if (!llapi_layout_is_composite(head)) {
2560 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2562 llapi_layout_free(head);
2566 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2568 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2569 progname, strerror(errno));
2570 llapi_layout_free(head);
2574 llapi_layout_free(head);
2576 /* Make sure we use the first component of the layout to be added. */
2577 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2580 "%s setstripe: cannot move component cursor: %s\n",
2581 progname, strerror(errno));
2585 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2587 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2588 progname, strerror(errno));
2592 if (start > prev_end || end <= prev_end) {
2594 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2595 progname, start, end, prev_end);
2599 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2601 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2602 progname, strerror(errno));
2606 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2607 (prev_end & (stripe_size - 1))) {
2609 "%s setstripe: stripe size %lu not aligned with %lu\n",
2610 progname, stripe_size, prev_end);
2614 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2617 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2618 progname, prev_end, end, strerror(errno));
2625 static inline bool arg_is_eof(char *arg)
2627 return !strncmp(arg, "-1", strlen("-1")) ||
2628 !strncmp(arg, "EOF", strlen("EOF")) ||
2629 !strncmp(arg, "eof", strlen("eof"));
2633 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2635 * Return: Valid mirror_args pointer on success and
2636 * NULL if memory allocation fails.
2638 static struct mirror_args *lfs_mirror_alloc(void)
2640 struct mirror_args *mirror = NULL;
2643 mirror = calloc(1, sizeof(*mirror));
2654 * lfs_mirror_free() - Free memory allocated for a mirror argument
2656 * @mirror: Previously allocated mirror argument structure by
2657 * lfs_mirror_alloc().
2659 * Free memory allocated for @mirror.
2663 static void lfs_mirror_free(struct mirror_args *mirror)
2665 if (mirror->m_layout != NULL)
2666 llapi_layout_free(mirror->m_layout);
2671 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2672 * @mirror_list: Previously allocated mirror list.
2674 * Free memory allocated for @mirror_list.
2678 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2680 struct mirror_args *next_mirror = NULL;
2682 while (mirror_list != NULL) {
2683 next_mirror = mirror_list->m_next;
2684 lfs_mirror_free(mirror_list);
2685 mirror_list = next_mirror;
2697 LFS_COMP_NO_VERIFY_OPT,
2699 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2701 LFS_MIRROR_STATE_OPT,
2703 LFS_MIRROR_INDEX_OPT,
2704 LFS_LAYOUT_FOREIGN_OPT,
2709 static int lfs_setstripe_internal(int argc, char **argv,
2710 enum setstripe_origin opc)
2712 struct lfs_setstripe_args lsa = { 0 };
2713 struct llapi_stripe_param *param = NULL;
2714 struct find_param migrate_mdt_param = {
2724 unsigned long long size_units = 1;
2725 bool migrate_mode = false;
2726 bool migrate_mdt_mode = false;
2727 bool setstripe_mode = false;
2728 bool migration_block = false;
2729 __u64 migration_flags = 0;
2730 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2731 int comp_del = 0, comp_set = 0;
2734 struct llapi_layout *layout = NULL;
2735 struct llapi_layout **lpp = &layout;
2736 bool mirror_mode = false;
2737 bool has_m_file = false;
2738 __u32 mirror_count = 0;
2739 enum mirror_flags mirror_flags = 0;
2740 struct mirror_args *mirror_list = NULL;
2741 struct mirror_args *new_mirror = NULL;
2742 struct mirror_args *last_mirror = NULL;
2743 __u16 mirror_id = 0;
2745 bool from_yaml = false;
2746 bool from_copy = false;
2747 char *template = NULL;
2748 bool foreign_mode = false;
2750 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
2751 char *mode_opt = NULL;
2752 mode_t previous_umask = 0;
2753 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2755 struct option long_opts[] = {
2756 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2757 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2758 /* --block is only valid in migrate mode */
2759 { .val = 'b', .name = "block", .has_arg = no_argument },
2760 { .val = LFS_COMP_ADD_OPT,
2761 .name = "comp-add", .has_arg = no_argument },
2762 { .val = LFS_COMP_ADD_OPT,
2763 .name = "component-add", .has_arg = no_argument },
2764 { .val = LFS_COMP_DEL_OPT,
2765 .name = "comp-del", .has_arg = no_argument },
2766 { .val = LFS_COMP_DEL_OPT,
2767 .name = "component-del", .has_arg = no_argument },
2768 { .val = LFS_COMP_FLAGS_OPT,
2769 .name = "comp-flags", .has_arg = required_argument },
2770 { .val = LFS_COMP_FLAGS_OPT,
2771 .name = "component-flags",
2772 .has_arg = required_argument },
2773 { .val = LFS_COMP_SET_OPT,
2774 .name = "comp-set", .has_arg = no_argument },
2775 { .val = LFS_COMP_SET_OPT,
2776 .name = "component-set",
2777 .has_arg = no_argument},
2778 { .val = LFS_COMP_NO_VERIFY_OPT,
2779 .name = "no-verify", .has_arg = no_argument},
2780 { .val = LFS_LAYOUT_FLAGS_OPT,
2781 .name = "flags", .has_arg = required_argument},
2782 { .val = LFS_LAYOUT_FOREIGN_OPT,
2783 .name = "foreign", .has_arg = optional_argument},
2784 { .val = LFS_MIRROR_ID_OPT,
2785 .name = "mirror-id", .has_arg = required_argument},
2786 { .val = LFS_MODE_OPT,
2787 .name = "mode", .has_arg = required_argument},
2788 { .val = LFS_LAYOUT_COPY,
2789 .name = "copy", .has_arg = required_argument},
2790 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2791 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2792 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2793 { .val = 'C', .name = "overstripe-count",
2794 .has_arg = required_argument},
2795 { .val = 'd', .name = "delete", .has_arg = no_argument},
2796 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2797 /* --non-direct is only valid in migrate mode */
2798 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2799 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2800 { .val = 'E', .name = "component-end",
2801 .has_arg = required_argument},
2802 { .val = 'f', .name = "file", .has_arg = required_argument },
2803 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2804 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2805 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2806 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2807 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2808 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2809 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2810 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2811 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2812 { .val = 'L', .name = "layout", .has_arg = required_argument },
2813 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2814 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2815 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2816 /* --non-block is only valid in migrate mode */
2817 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2818 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2819 { .val = 'o', .name = "ost", .has_arg = required_argument },
2820 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2821 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2822 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2824 { .val = 'p', .name = "pool", .has_arg = required_argument },
2825 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2826 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2827 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2828 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2829 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2830 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2831 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2832 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2833 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2834 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2835 /* --verbose is only valid in migrate mode */
2836 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2837 { .val = 'x', .name = "xattr", .has_arg = required_argument },
2838 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2839 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
2840 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
2843 setstripe_args_init(&lsa);
2845 migrate_mode = (opc == SO_MIGRATE);
2846 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2847 setstripe_mode = (opc == SO_SETSTRIPE);
2849 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2851 while ((c = getopt_long(argc, argv,
2852 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
2853 long_opts, NULL)) >= 0) {
2858 case LFS_COMP_ADD_OPT:
2861 case LFS_COMP_DEL_OPT:
2864 case LFS_COMP_FLAGS_OPT:
2865 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2866 &lsa.lsa_comp_neg_flags);
2869 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2870 fprintf(stderr, "%s: inverted flags are not supported\n",
2874 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2876 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2883 case LFS_COMP_SET_OPT:
2886 case LFS_COMP_NO_VERIFY_OPT:
2887 mirror_flags |= MF_NO_VERIFY;
2889 case LFS_MIRROR_ID_OPT:
2890 mirror_id = strtoul(optarg, &end, 0);
2891 if (*end != '\0' || mirror_id == 0) {
2893 "%s %s: invalid mirror ID '%s'\n",
2894 progname, argv[0], optarg);
2898 case LFS_LAYOUT_FLAGS_OPT: {
2901 /* check for numeric flags (foreign and mirror cases) */
2902 if (setstripe_mode && !mirror_mode && !last_mirror) {
2903 flags = strtoul(optarg, &end, 16);
2906 "%s %s: bad flags '%s'\n",
2907 progname, argv[0], optarg);
2913 if (!mirror_mode || !last_mirror) {
2914 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2919 result = comp_str2flags(optarg, &last_mirror->m_flags,
2925 fprintf(stderr, "%s: inverted flags are not supported\n",
2930 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
2932 "%s: unsupported mirror flags: %s\n",
2939 case LFS_LAYOUT_FOREIGN_OPT:
2940 if (optarg != NULL) {
2941 /* check pure numeric */
2942 type = strtoul(optarg, &end, 0);
2945 type = check_foreign_type_name(optarg);
2946 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
2948 "%s %s: unrecognized foreign type '%s'\n",
2955 foreign_mode = true;
2959 if (mode_opt != NULL) {
2960 mode = strtoul(mode_opt, &end, 8);
2963 "%s %s: bad mode '%s'\n",
2964 progname, argv[0], mode_opt);
2967 previous_umask = umask(0);
2970 case LFS_LAYOUT_COPY:
2975 if (!migrate_mode) {
2977 "%s %s: -b|--block valid only for migrate command\n",
2981 migration_block = true;
2984 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
2987 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2990 "%s %s: invalid stripe count '%s'\n",
2991 progname, argv[0], optarg);
2995 if (lsa.lsa_stripe_count == -1)
2996 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2999 /* delete the default striping pattern */
3001 if (opc == SO_MIRROR_SPLIT) {
3004 "%s %s: -d cannot used with -f\n",
3008 mirror_flags |= MF_DESTROY;
3012 if (!migrate_mode) {
3014 "%s %s: -D|--non-direct is valid "
3015 "only for migrate command\n",
3019 migration_flags |= MIGRATION_NONDIRECT;
3022 if (lsa.lsa_comp_end != 0) {
3023 result = comp_args_to_layout(lpp, &lsa, true);
3025 fprintf(stderr, "%s: invalid layout\n",
3030 setstripe_args_init_inherit(&lsa);
3033 if (arg_is_eof(optarg)) {
3034 lsa.lsa_comp_end = LUSTRE_EOF;
3036 result = llapi_parse_size(optarg,
3041 "%s %s: invalid component end '%s'\n",
3042 progname, argv[0], optarg);
3048 if (!migrate_mode) {
3049 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3053 lsa.lsa_pattern = check_hashtype(optarg);
3054 if (lsa.lsa_pattern == 0) {
3056 "%s %s: bad stripe hash type '%s'\n",
3057 progname, argv[0], optarg);
3062 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3065 "%s %s: invalid stripe offset '%s'\n",
3066 progname, argv[0], optarg);
3069 if (lsa.lsa_stripe_off == -1)
3070 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3073 comp_id = strtoul(optarg, &end, 0);
3074 if (*end != '\0' || comp_id == 0 ||
3075 comp_id > LCME_ID_MAX) {
3077 "%s %s: invalid component ID '%s'\n",
3078 progname, argv[0], optarg);
3083 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3085 "error: %s: invalid option: %s\n",
3086 progname, argv[optopt + 1]);
3089 if (opc == SO_MIRROR_EXTEND) {
3090 if (last_mirror == NULL) {
3092 "error: %s: '-N' must exist in front of '%s'\n",
3093 progname, argv[optopt + 1]);
3096 last_mirror->m_file = optarg;
3097 last_mirror->m_count = 1;
3100 if (mirror_list == NULL)
3101 mirror_list = lfs_mirror_alloc();
3102 mirror_list->m_file = optarg;
3107 if (strcmp(argv[optind - 1], "mdt") == 0) {
3108 /* Can be only the first component */
3109 if (layout != NULL) {
3111 fprintf(stderr, "error: 'mdt' layout "
3112 "can be only the first one\n");
3115 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3117 fprintf(stderr, "error: 'mdt' layout "
3118 "size is too big\n");
3121 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3122 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3124 fprintf(stderr, "error: layout '%s' is "
3125 "unknown, supported layouts are: "
3126 "'mdt', 'raid0'\n", argv[optind]);
3131 if (!migrate_mode) {
3133 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3137 migrate_mdt_mode = true;
3138 lsa.lsa_nr_tgts = parse_targets(tgts,
3139 sizeof(tgts) / sizeof(__u32),
3140 lsa.lsa_nr_tgts, optarg, NULL);
3141 if (lsa.lsa_nr_tgts < 0) {
3143 "%s %s: invalid MDT target(s) '%s'\n",
3144 progname, argv[0], optarg);
3148 lsa.lsa_tgts = tgts;
3149 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3150 lsa.lsa_stripe_off = tgts[0];
3153 if (!migrate_mode) {
3155 "%s %s: -n|--non-block valid only for migrate command\n",
3159 migration_flags |= MIGRATION_NONBLOCK;
3162 if (opc == SO_SETSTRIPE) {
3163 opc = SO_MIRROR_CREATE;
3167 if (optarg != NULL) {
3168 mirror_count = strtoul(optarg, &end, 0);
3169 if (*end != '\0' || mirror_count == 0) {
3171 "error: %s: bad mirror count: %s\n",
3178 new_mirror = lfs_mirror_alloc();
3179 new_mirror->m_count = mirror_count;
3181 if (mirror_list == NULL)
3182 mirror_list = new_mirror;
3184 if (last_mirror != NULL) {
3185 /* wrap up last mirror */
3186 if (lsa.lsa_comp_end == 0)
3187 lsa.lsa_comp_end = LUSTRE_EOF;
3189 result = comp_args_to_layout(lpp, &lsa, true);
3191 lfs_mirror_free(new_mirror);
3195 setstripe_args_init_inherit(&lsa);
3197 last_mirror->m_next = new_mirror;
3200 last_mirror = new_mirror;
3201 lpp = &last_mirror->m_layout;
3204 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3205 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3206 fprintf(stderr, "warning: '--ost-list' is "
3207 "deprecated, use '--ost' instead\n");
3209 /* -o allows overstriping, and must note it because
3210 * parse_targets is shared with MDT striping, which
3211 * does not allow duplicates
3213 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3214 lsa.lsa_nr_tgts = parse_targets(tgts,
3215 sizeof(tgts) / sizeof(__u32),
3216 lsa.lsa_nr_tgts, optarg,
3218 if (lsa.lsa_nr_tgts < 0) {
3220 "%s %s: invalid OST target(s) '%s'\n",
3221 progname, argv[0], optarg);
3225 lsa.lsa_tgts = tgts;
3226 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3227 lsa.lsa_stripe_off = tgts[0];
3232 lsa.lsa_pool_name = optarg;
3234 if (strlen(lsa.lsa_pool_name) == 0 ||
3235 strncmp(lsa.lsa_pool_name, "none",
3236 LOV_MAXPOOLNAME) == 0)
3237 lsa.lsa_pool_name = NULL;
3240 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3244 "%s %s: invalid stripe size '%s'\n",
3245 progname, argv[0], optarg);
3250 if (!migrate_mode) {
3252 "%s %s: -v|--verbose valid only for migrate command\n",
3256 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3257 migration_flags = MIGRATION_VERBOSE;
3267 result = llapi_parse_size(optarg,
3268 &lsa.lsa_extension_size,
3272 "%s %s: invalid extension size '%s'\n",
3273 progname, argv[0], optarg);
3277 lsa.lsa_extension_comp = true;
3280 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3281 progname, argv[0], argv[optind - 1]);
3286 fname = argv[optind];
3288 if (optind == argc) {
3289 fprintf(stderr, "%s %s: FILE must be specified\n",
3294 if (xattr && !foreign_mode) {
3295 /* only print a warning as this is harmless and will be ignored
3298 "%s %s: xattr has been specified for non-foreign layout\n",
3300 } else if (foreign_mode && !xattr) {
3302 "%s %s: xattr must be provided in foreign mode\n",
3307 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3308 comp_set || comp_id || delete || from_copy ||
3309 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3312 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3317 if (mirror_mode && mirror_count == 0) {
3319 "error: %s: --mirror-count|-N option is required\n",
3326 if (lsa.lsa_comp_end == 0)
3327 lsa.lsa_comp_end = LUSTRE_EOF;
3330 if (lsa.lsa_comp_end != 0) {
3331 result = comp_args_to_layout(lpp, &lsa, true);
3333 fprintf(stderr, "error: %s: invalid layout\n",
3340 if (mirror_flags & MF_NO_VERIFY) {
3341 if (opc != SO_MIRROR_EXTEND) {
3343 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3347 } else if (!has_m_file) {
3349 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3356 if (comp_set && !comp_id) {
3357 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3362 if ((delete + comp_set + comp_del + comp_add) > 1) {
3364 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3369 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3370 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3372 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3377 if ((comp_set || comp_del) &&
3378 (setstripe_args_specified(&lsa) || layout != NULL)) {
3380 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3385 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3387 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3392 if (comp_add || comp_del) {
3395 result = lstat(fname, &st);
3396 if (result == 0 && S_ISDIR(st.st_mode)) {
3398 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3404 fprintf(stderr, "error: %s: can't use --component-add "
3405 "or --component-del for mirror operation\n",
3412 if (layout == NULL) {
3414 "%s %s: option -E must be specified with --component-add\n",
3419 result = adjust_first_extent(fname, layout);
3420 if (result == -ENODATA)
3422 else if (result != 0) {
3423 fprintf(stderr, "error: %s: invalid layout\n",
3429 if (from_yaml && from_copy) {
3431 "%s: can't specify --yaml and --copy together\n",
3436 if ((from_yaml || from_copy) &&
3437 (setstripe_args_specified(&lsa) || layout != NULL)) {
3438 fprintf(stderr, "error: %s: can't specify --yaml with "
3439 "-c, -S, -i, -o, -p or -E options.\n",
3444 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3446 "%s %s: options --non-block and --block are mutually exclusive\n",
3451 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3454 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3459 if (migrate_mdt_mode) {
3460 struct lmv_user_md *lmu;
3462 /* initialize migrate mdt parameters */
3463 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3464 LMV_USER_MAGIC_SPECIFIC));
3467 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3468 progname, argv[0], strerror(ENOMEM));
3472 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3473 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3474 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3476 "%s %s: migrate should specify MDT index\n",
3481 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3482 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3483 lmu->lum_hash_type = lsa.lsa_pattern;
3485 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3486 if (lsa.lsa_pool_name)
3487 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3488 sizeof(lmu->lum_pool_name) - 1);
3489 if (lsa.lsa_nr_tgts > 1) {
3492 if (lsa.lsa_stripe_count > 0 &&
3493 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3494 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3496 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3497 progname, lsa.lsa_stripe_count,
3503 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3504 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3505 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3506 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3508 lmu->lum_magic = LMV_USER_MAGIC;
3511 migrate_mdt_param.fp_lmv_md = lmu;
3512 migrate_mdt_param.fp_migrate = 1;
3513 } else if (layout == NULL) {
3514 /* initialize stripe parameters */
3515 param = calloc(1, offsetof(typeof(*param),
3516 lsp_osts[lsa.lsa_nr_tgts]));
3517 if (param == NULL) {
3519 "%s %s: cannot allocate memory for parameters: %s\n",
3520 progname, argv[0], strerror(ENOMEM));
3525 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3526 param->lsp_stripe_size = lsa.lsa_stripe_size;
3527 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3528 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3529 param->lsp_stripe_count = -1;
3531 param->lsp_stripe_count = lsa.lsa_stripe_count;
3533 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3534 param->lsp_stripe_offset = -1;
3536 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3537 param->lsp_stripe_pattern =
3538 llapi_pattern_to_lov(lsa.lsa_pattern);
3539 if (param->lsp_stripe_pattern == EINVAL) {
3540 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3545 param->lsp_pool = lsa.lsa_pool_name;
3546 param->lsp_is_specific = false;
3547 if (lsa.lsa_nr_tgts > 0) {
3548 if (lsa.lsa_stripe_count > 0 &&
3549 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3550 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3551 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3552 fprintf(stderr, "error: %s: stripe count %lld "
3553 "doesn't match the number of OSTs: %d\n"
3554 , argv[0], lsa.lsa_stripe_count,
3560 param->lsp_is_specific = true;
3561 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3562 memcpy(param->lsp_osts, tgts,
3563 sizeof(*tgts) * lsa.lsa_nr_tgts);
3568 /* generate a layout from a YAML template */
3569 result = lfs_comp_create_from_yaml(template, &layout,
3572 fprintf(stderr, "error: %s: can't create composite "
3573 "layout from template file %s\n",
3577 } else if (from_copy) {
3578 layout = llapi_layout_get_by_path(template, 0);
3579 if (layout == NULL) {
3581 "%s: can't create composite layout from file %s.\n",
3582 progname, template);
3587 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3588 if (migrate_mdt_mode) {
3589 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3590 } else if (migrate_mode) {
3591 result = lfs_migrate(fname, migration_flags, param,
3593 } else if (comp_set != 0) {
3594 result = lfs_component_set(fname, comp_id,
3596 lsa.lsa_comp_neg_flags);
3597 } else if (comp_del != 0) {
3598 result = lfs_component_del(fname, comp_id,
3600 lsa.lsa_comp_neg_flags);
3601 } else if (comp_add != 0) {
3602 result = lfs_component_add(fname, layout);
3603 } else if (opc == SO_MIRROR_CREATE) {
3604 result = mirror_create(fname, mirror_list);
3605 } else if (opc == SO_MIRROR_EXTEND) {
3606 result = mirror_extend(fname, mirror_list,
3608 } else if (opc == SO_MIRROR_SPLIT) {
3609 if (mirror_id == 0 && comp_id == 0) {
3611 "%s %s: no mirror id or component id is specified\n",
3616 comp_id = mirror_id;
3618 mirror_flags |= MF_COMP_ID;
3619 result = mirror_split(fname, comp_id, mirror_flags,
3620 has_m_file ? mirror_list->m_file :
3622 } else if (layout != NULL) {
3623 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3629 } else if (foreign_mode) {
3630 result = llapi_file_create_foreign(fname, mode, type,
3637 result = llapi_file_open_param(fname,
3646 /* Save the first error encountered. */
3653 if (mode_opt != NULL)
3654 umask(previous_umask);
3657 free(migrate_mdt_param.fp_lmv_md);
3658 llapi_layout_free(layout);
3659 lfs_mirror_list_free(mirror_list);
3664 llapi_layout_free(layout);
3665 lfs_mirror_list_free(mirror_list);
3669 static int lfs_poollist(int argc, char **argv)
3674 return llapi_poollist(argv[1]);
3677 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3682 char *endptr = "AD";
3687 else if (str[0] == '-')
3693 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3694 long long val = strtoll(timebuf, &endptr, 0);
3699 unit *= 52; /* 52 weeks + 1 day below */
3700 case 'w': /* fallthrough */
3702 case '\0': /* days are default unit if none used */
3703 case 'd': /* fallthrough */
3704 unit = (unit + (*endptr == 'y')) * 24;
3705 case 'h': /* fallthrough */
3707 case 'm': /* fallthrough */
3709 case 's': /* fallthrough */
3711 /* don't need to multiply by 1 for seconds */
3714 "%s find: bad time string '%s': %s\n",
3715 progname, timebuf, strerror(EINVAL));
3719 if (param->fp_time_margin == 0 ||
3720 (*endptr && unit < param->fp_time_margin))
3721 param->fp_time_margin = unit;
3728 fprintf(stderr, "%s find: bad time '%s': too large\n",
3738 static int name2uid(unsigned int *id, const char *name)
3740 struct passwd *passwd;
3742 passwd = getpwnam(name);
3745 *id = passwd->pw_uid;
3750 static int name2gid(unsigned int *id, const char *name)
3752 struct group *group;
3754 group = getgrnam(name);
3757 *id = group->gr_gid;
3762 static inline int name2projid(unsigned int *id, const char *name)
3767 static int uid2name(char **name, unsigned int id)
3769 struct passwd *passwd;
3771 passwd = getpwuid(id);
3774 *name = passwd->pw_name;
3779 static inline int gid2name(char **name, unsigned int id)
3781 struct group *group;
3783 group = getgrgid(id);
3786 *name = group->gr_name;
3791 static int name2layout(__u32 *layout, char *name)
3793 char *ptr, *layout_name;
3796 for (ptr = name; ; ptr = NULL) {
3797 layout_name = strtok(ptr, ",");
3798 if (layout_name == NULL)
3800 if (strcmp(layout_name, "released") == 0)
3801 *layout |= LOV_PATTERN_F_RELEASED;
3802 else if (strcmp(layout_name, "raid0") == 0)
3803 *layout |= LOV_PATTERN_RAID0;
3804 else if (strcmp(layout_name, "mdt") == 0)
3805 *layout |= LOV_PATTERN_MDT;
3806 else if (strcmp(layout_name, "overstriping") == 0)
3807 *layout |= LOV_PATTERN_OVERSTRIPING;
3814 static int lfs_find(int argc, char **argv)
3819 struct find_param param = {
3822 .fp_time_margin = 24 * 60 * 60,
3824 struct option long_opts[] = {
3825 { .val = 'A', .name = "atime", .has_arg = required_argument },
3826 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3827 { .val = LFS_COMP_COUNT_OPT,
3828 .name = "comp-count", .has_arg = required_argument },
3829 { .val = LFS_COMP_COUNT_OPT,
3830 .name = "component-count",
3831 .has_arg = required_argument },
3832 { .val = LFS_COMP_FLAGS_OPT,
3833 .name = "comp-flags", .has_arg = required_argument },
3834 { .val = LFS_COMP_FLAGS_OPT,
3835 .name = "component-flags",
3836 .has_arg = required_argument },
3837 { .val = LFS_COMP_START_OPT,
3838 .name = "comp-start", .has_arg = required_argument },
3839 { .val = LFS_COMP_START_OPT,
3840 .name = "component-start",
3841 .has_arg = required_argument },
3842 { .val = LFS_MIRROR_STATE_OPT,
3843 .name = "mirror-state", .has_arg = required_argument },
3844 { .val = LFS_LAYOUT_FOREIGN_OPT,
3845 .name = "foreign", .has_arg = optional_argument},
3846 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3847 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3848 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3849 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3850 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3851 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3852 { .val = 'E', .name = "component-end",
3853 .has_arg = required_argument },
3854 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3855 { .val = LFS_LAYOUT_FOREIGN_OPT,
3856 .name = "foreign", .has_arg = optional_argument},
3857 { .val = 'g', .name = "gid", .has_arg = required_argument },
3858 { .val = 'G', .name = "group", .has_arg = required_argument },
3859 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3860 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3861 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3862 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3863 { .val = 'L', .name = "layout", .has_arg = required_argument },
3864 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3865 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3866 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3867 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3868 { .val = 'n', .name = "name", .has_arg = required_argument },
3869 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3870 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3871 { .val = 'O', .name = "obd", .has_arg = required_argument },
3872 { .val = 'O', .name = "ost", .has_arg = required_argument },
3873 /* no short option for pool yet, can be 'p' after 2.18 */
3874 { .val = LFS_POOL_OPT,
3875 .name = "pool", .has_arg = required_argument },
3876 { .val = '0', .name = "print0", .has_arg = no_argument },
3877 { .val = 'P', .name = "print", .has_arg = no_argument },
3878 { .val = LFS_PROJID_OPT,
3879 .name = "projid", .has_arg = required_argument },
3880 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3881 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3882 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3883 { .val = 's', .name = "size", .has_arg = required_argument },
3884 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3885 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3886 { .val = 't', .name = "type", .has_arg = required_argument },
3887 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3888 { .val = 'u', .name = "uid", .has_arg = required_argument },
3889 { .val = 'U', .name = "user", .has_arg = required_argument },
3890 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3891 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3903 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3904 while ((c = getopt_long_only(argc, argv,
3905 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3906 long_opts, NULL)) >= 0) {
3911 /* '!' is part of option */
3912 /* when getopt_long_only() finds a string which is not
3913 * an option nor a known option argument it returns 1
3914 * in that case if we already have found pathstart and pathend
3915 * (i.e. we have the list of pathnames),
3916 * the only supported value is "!"
3918 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3919 if (!isoption && pathend != -1) {
3920 fprintf(stderr, "err: %s: filename|dirname must either "
3921 "precede options or follow options\n",
3926 if (!isoption && pathstart == -1)
3927 pathstart = optind - 1;
3928 if (isoption && pathstart != -1 && pathend == -1)
3929 pathend = optind - 2;
3935 /* unknown; opt is "!" or path component,
3936 * checking done above.
3938 if (strcmp(optarg, "!") == 0)
3942 xtime = ¶m.fp_atime;
3943 xsign = ¶m.fp_asign;
3944 param.fp_exclude_atime = !!neg_opt;
3945 /* no break, this falls through to 'C' for ctime */
3948 xtime = ¶m.fp_ctime;
3949 xsign = ¶m.fp_csign;
3950 param.fp_exclude_ctime = !!neg_opt;
3952 /* no break, this falls through to 'M' for mtime */
3955 xtime = ¶m.fp_mtime;
3956 xsign = ¶m.fp_msign;
3957 param.fp_exclude_mtime = !!neg_opt;
3959 rc = set_time(¶m, &t, xtime, optarg);
3960 if (rc == LONG_MAX) {
3968 if (optarg[0] == '+') {
3969 param.fp_blocks_sign = -1;
3971 } else if (optarg[0] == '-') {
3972 param.fp_blocks_sign = 1;
3976 param.fp_blocks_units = 1024;
3977 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3978 ¶m.fp_blocks_units, 0);
3980 fprintf(stderr, "error: bad blocks '%s'\n",
3984 param.fp_check_blocks = 1;
3985 param.fp_exclude_blocks = !!neg_opt;
3987 case LFS_COMP_COUNT_OPT:
3988 if (optarg[0] == '+') {
3989 param.fp_comp_count_sign = -1;
3991 } else if (optarg[0] == '-') {
3992 param.fp_comp_count_sign = 1;
3996 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3997 if (*endptr != '\0') {
3998 fprintf(stderr, "error: bad component count "
4002 param.fp_check_comp_count = 1;
4003 param.fp_exclude_comp_count = !!neg_opt;
4005 case LFS_COMP_FLAGS_OPT:
4006 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4007 ¶m.fp_comp_neg_flags);
4009 fprintf(stderr, "error: bad component flags "
4013 param.fp_check_comp_flags = 1;
4015 __u32 flags = param.fp_comp_neg_flags;
4016 param.fp_comp_neg_flags = param.fp_comp_flags;
4017 param.fp_comp_flags = flags;
4020 case LFS_COMP_START_OPT:
4021 if (optarg[0] == '+') {
4022 param.fp_comp_start_sign = -1;
4024 } else if (optarg[0] == '-') {
4025 param.fp_comp_start_sign = 1;
4029 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4030 ¶m.fp_comp_start_units, 0);
4032 fprintf(stderr, "error: bad component start "
4036 param.fp_check_comp_start = 1;
4037 param.fp_exclude_comp_start = !!neg_opt;
4039 case LFS_MIRROR_STATE_OPT:
4040 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4041 ¶m.fp_mirror_neg_state);
4044 "error: bad mirrored file state '%s'\n",
4048 param.fp_check_mirror_state = 1;
4050 __u16 state = param.fp_mirror_neg_state;
4051 param.fp_mirror_neg_state =
4052 param.fp_mirror_state;
4053 param.fp_mirror_state = state;
4057 if (optarg[0] == '+') {
4058 param.fp_stripe_count_sign = -1;
4060 } else if (optarg[0] == '-') {
4061 param.fp_stripe_count_sign = 1;
4065 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4066 if (*endptr != '\0') {
4067 fprintf(stderr,"error: bad stripe_count '%s'\n",
4072 param.fp_check_stripe_count = 1;
4073 param.fp_exclude_stripe_count = !!neg_opt;
4076 param.fp_max_depth = strtol(optarg, 0, 0);
4079 if (optarg[0] == '+') {
4080 param.fp_comp_end_sign = -1;
4082 } else if (optarg[0] == '-') {
4083 param.fp_comp_end_sign = 1;
4087 if (arg_is_eof(optarg)) {
4088 param.fp_comp_end = LUSTRE_EOF;
4089 param.fp_comp_end_units = 1;
4092 rc = llapi_parse_size(optarg,
4094 ¶m.fp_comp_end_units, 0);
4097 fprintf(stderr, "error: bad component end "
4101 param.fp_check_comp_end = 1;
4102 param.fp_exclude_comp_end = !!neg_opt;
4104 case LFS_LAYOUT_FOREIGN_OPT: {
4105 /* all types by default */
4106 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4108 if (optarg != NULL) {
4109 /* check pure numeric */
4110 type = strtoul(optarg, &endptr, 0);
4113 type = check_foreign_type_name(optarg);
4114 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4116 "%s %s: unknown foreign type '%s'\n",
4123 param.fp_foreign_type = type;
4124 param.fp_check_foreign = 1;
4125 param.fp_exclude_foreign = !!neg_opt;
4130 rc = name2gid(¶m.fp_gid, optarg);
4132 param.fp_gid = strtoul(optarg, &endptr, 10);
4133 if (*endptr != '\0') {
4134 fprintf(stderr, "Group/GID: %s cannot "
4135 "be found.\n", optarg);
4140 param.fp_exclude_gid = !!neg_opt;
4141 param.fp_check_gid = 1;
4144 param.fp_hash_type = check_hashtype(optarg);
4145 if (param.fp_hash_type == 0) {
4146 fprintf(stderr, "error: bad hash_type '%s'\n",
4151 param.fp_check_hash_type = 1;
4152 param.fp_exclude_hash_type = !!neg_opt;
4155 ret = name2layout(¶m.fp_layout, optarg);
4158 param.fp_exclude_layout = !!neg_opt;
4159 param.fp_check_layout = 1;
4163 rc = name2uid(¶m.fp_uid, optarg);
4165 param.fp_uid = strtoul(optarg, &endptr, 10);
4166 if (*endptr != '\0') {
4167 fprintf(stderr, "User/UID: %s cannot "
4168 "be found.\n", optarg);
4173 param.fp_exclude_uid = !!neg_opt;
4174 param.fp_check_uid = 1;
4177 param.fp_pattern = (char *)optarg;
4178 param.fp_exclude_pattern = !!neg_opt;
4181 if (optarg[0] == '+') {
4182 param.fp_mirror_count_sign = -1;
4184 } else if (optarg[0] == '-') {
4185 param.fp_mirror_count_sign = 1;
4189 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4190 if (*endptr != '\0') {
4192 "error: bad mirror count '%s'\n",
4196 param.fp_check_mirror_count = 1;
4197 param.fp_exclude_mirror_count = !!neg_opt;
4202 char *buf, *token, *next, *p;
4206 buf = strdup(optarg);
4212 param.fp_exclude_obd = !!neg_opt;
4215 while (token && *token) {
4216 token = strchr(token, ',');
4223 param.fp_exclude_mdt = !!neg_opt;
4224 param.fp_num_alloc_mdts += len;
4225 tmp = realloc(param.fp_mdt_uuid,
4226 param.fp_num_alloc_mdts *
4227 sizeof(*param.fp_mdt_uuid));
4233 param.fp_mdt_uuid = tmp;
4235 param.fp_exclude_obd = !!neg_opt;
4236 param.fp_num_alloc_obds += len;
4237 tmp = realloc(param.fp_obd_uuid,
4238 param.fp_num_alloc_obds *
4239 sizeof(*param.fp_obd_uuid));
4245 param.fp_obd_uuid = tmp;
4247 for (token = buf; token && *token; token = next) {
4248 struct obd_uuid *puuid;
4251 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4254 ¶m.fp_obd_uuid[param.fp_num_obds++];
4256 p = strchr(token, ',');
4263 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4268 strncpy(puuid->uuid, token,
4269 sizeof(puuid->uuid));
4276 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4280 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4282 "Pool name %s is too long (max %d)\n",
4283 optarg, LOV_MAXPOOLNAME);
4288 * We do check for empty pool because empty pool
4289 * is used to find V1 LOV attributes
4291 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4292 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4293 param.fp_exclude_pool = !!neg_opt;
4294 param.fp_check_pool = 1;
4296 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4297 case 'p': /* want this for --pool, to match getstripe/find */
4299 "warning: -p deprecated, use --print0 or -0\n");
4302 param.fp_zero_end = 1;
4304 case 'P': /* we always print, this option is a no-op */
4306 case LFS_PROJID_OPT:
4307 rc = name2projid(¶m.fp_projid, optarg);
4309 param.fp_projid = strtoul(optarg, &endptr, 10);
4310 if (*endptr != '\0') {
4312 "Invalid project ID: %s",
4318 param.fp_exclude_projid = !!neg_opt;
4319 param.fp_check_projid = 1;
4322 if (optarg[0] == '+') {
4323 param.fp_size_sign = -1;
4325 } else if (optarg[0] == '-') {
4326 param.fp_size_sign = 1;
4330 ret = llapi_parse_size(optarg, ¶m.fp_size,
4331 ¶m.fp_size_units, 0);
4333 fprintf(stderr, "error: bad file size '%s'\n",
4337 param.fp_check_size = 1;
4338 param.fp_exclude_size = !!neg_opt;
4341 if (optarg[0] == '+') {
4342 param.fp_stripe_size_sign = -1;
4344 } else if (optarg[0] == '-') {
4345 param.fp_stripe_size_sign = 1;
4349 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4350 ¶m.fp_stripe_size_units, 0);
4352 fprintf(stderr, "error: bad stripe_size '%s'\n",
4356 param.fp_check_stripe_size = 1;
4357 param.fp_exclude_stripe_size = !!neg_opt;
4360 param.fp_exclude_type = !!neg_opt;
4361 switch (optarg[0]) {
4363 param.fp_type = S_IFBLK;
4366 param.fp_type = S_IFCHR;
4369 param.fp_type = S_IFDIR;
4372 param.fp_type = S_IFREG;
4375 param.fp_type = S_IFLNK;
4378 param.fp_type = S_IFIFO;
4381 param.fp_type = S_IFSOCK;
4384 fprintf(stderr, "error: %s: bad type '%s'\n",
4391 if (optarg[0] == '+') {
4392 param.fp_mdt_count_sign = -1;
4394 } else if (optarg[0] == '-') {
4395 param.fp_mdt_count_sign = 1;
4399 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4400 if (*endptr != '\0') {
4401 fprintf(stderr, "error: bad mdt_count '%s'\n",
4406 param.fp_check_mdt_count = 1;
4407 param.fp_exclude_mdt_count = !!neg_opt;
4415 if (pathstart == -1) {
4416 fprintf(stderr, "error: %s: no filename|pathname\n",
4420 } else if (pathend == -1) {
4426 rc = llapi_find(argv[pathstart], ¶m);
4427 if (rc != 0 && ret == 0)
4429 } while (++pathstart < pathend);
4432 fprintf(stderr, "error: %s failed for %s.\n",
4433 argv[0], argv[optind - 1]);
4435 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4436 free(param.fp_obd_uuid);
4438 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4439 free(param.fp_mdt_uuid);
4444 static int lfs_getstripe_internal(int argc, char **argv,
4445 struct find_param *param)
4447 struct option long_opts[] = {
4448 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4449 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4450 { .val = LFS_COMP_COUNT_OPT,
4451 .name = "comp-count", .has_arg = no_argument },
4452 { .val = LFS_COMP_COUNT_OPT,
4453 .name = "component-count", .has_arg = no_argument },
4454 { .val = LFS_COMP_FLAGS_OPT,
4455 .name = "comp-flags", .has_arg = optional_argument },
4456 { .val = LFS_COMP_FLAGS_OPT,
4457 .name = "component-flags", .has_arg = optional_argument },
4458 { .val = LFS_COMP_START_OPT,
4459 .name = "comp-start", .has_arg = optional_argument },
4460 { .val = LFS_COMP_START_OPT,
4461 .name = "component-start", .has_arg = optional_argument },
4462 { .val = LFS_MIRROR_INDEX_OPT,
4463 .name = "mirror-index", .has_arg = required_argument },
4464 { .val = LFS_MIRROR_ID_OPT,
4465 .name = "mirror-id", .has_arg = required_argument },
4466 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4467 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4468 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4469 { .val = 'd', .name = "directory", .has_arg = no_argument },
4470 { .val = 'D', .name = "default", .has_arg = no_argument },
4471 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4472 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4473 { .val = 'F', .name = "fid", .has_arg = no_argument },
4474 { .val = 'g', .name = "generation", .has_arg = no_argument },
4475 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4476 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4477 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4478 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4479 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4480 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4481 { .val = 'L', .name = "layout", .has_arg = no_argument },
4482 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4483 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4484 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4485 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4486 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4487 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4488 { .val = 'O', .name = "obd", .has_arg = required_argument },
4489 { .val = 'O', .name = "ost", .has_arg = required_argument },
4490 { .val = 'p', .name = "pool", .has_arg = no_argument },
4491 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4492 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4493 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4494 { .val = 'R', .name = "raw", .has_arg = no_argument },
4495 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4496 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4497 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4498 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4499 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4500 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4501 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4502 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4506 int pathstart = -1, pathend = -1;
4510 while ((c = getopt_long(argc, argv,
4511 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4512 long_opts, NULL)) != -1) {
4516 /* '!' is part of option */
4517 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4518 if (!isoption && pathend != -1) {
4520 "error: %s: filename|dirname must either precede options or follow options\n",
4524 if (!isoption && pathstart == -1)
4525 pathstart = optind - 1;
4526 if (isoption && pathstart != -1 && pathend == -1)
4527 pathend = optind - 2;
4531 /* unknown: opt is "!" */
4532 if (strcmp(optarg, "!") == 0)
4536 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4537 param->fp_verbose |= VERBOSE_COUNT;
4538 param->fp_max_depth = 0;
4541 case LFS_COMP_COUNT_OPT:
4542 param->fp_verbose |= VERBOSE_COMP_COUNT;
4543 param->fp_max_depth = 0;
4545 case LFS_COMP_FLAGS_OPT:
4546 if (optarg != NULL) {
4547 rc = comp_str2flags(optarg,
4548 ¶m->fp_comp_flags,
4549 ¶m->fp_comp_neg_flags);
4551 fprintf(stderr, "error: %s bad "
4552 "component flags '%s'.\n",
4556 param->fp_check_comp_flags = 1;
4558 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4559 param->fp_max_depth = 0;
4562 case LFS_COMP_START_OPT:
4563 if (optarg != NULL) {
4565 if (tmp[0] == '+') {
4566 param->fp_comp_start_sign = -1;
4568 } else if (tmp[0] == '-') {
4569 param->fp_comp_start_sign = 1;
4572 rc = llapi_parse_size(tmp,
4573 ¶m->fp_comp_start,
4574 ¶m->fp_comp_start_units, 0);
4576 fprintf(stderr, "error: %s bad "
4577 "component start '%s'.\n",
4581 param->fp_check_comp_start = 1;
4584 param->fp_verbose |= VERBOSE_COMP_START;
4585 param->fp_max_depth = 0;
4588 case LFS_MIRROR_INDEX_OPT:
4589 if (optarg[0] == '+') {
4590 param->fp_mirror_index_sign = -1;
4592 } else if (optarg[0] == '-') {
4593 param->fp_mirror_index_sign = 1;
4597 param->fp_mirror_index = strtoul(optarg, &end, 0);
4598 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4599 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4601 "%s %s: invalid mirror index '%s'\n",
4602 progname, argv[0], optarg);
4605 if (param->fp_mirror_id != 0) {
4607 "%s %s: can't specify both mirror index and mirror ID\n",
4611 param->fp_check_mirror_index = 1;
4612 param->fp_exclude_mirror_index = !!neg_opt;
4614 case LFS_MIRROR_ID_OPT:
4615 if (optarg[0] == '+') {
4616 param->fp_mirror_id_sign = -1;
4618 } else if (optarg[0] == '-') {
4619 param->fp_mirror_id_sign = 1;
4623 param->fp_mirror_id = strtoul(optarg, &end, 0);
4624 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4625 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4627 "%s %s: invalid mirror ID '%s'\n",
4628 progname, argv[0], optarg);
4631 if (param->fp_mirror_index != 0) {
4633 "%s %s: can't specify both mirror index and mirror ID\n",
4637 param->fp_check_mirror_id = 1;
4638 param->fp_exclude_mirror_id = !!neg_opt;
4641 param->fp_max_depth = 0;
4644 param->fp_get_default_lmv = 1;
4647 if (optarg != NULL) {
4649 if (tmp[0] == '+') {
4650 param->fp_comp_end_sign = -1;
4652 } else if (tmp[0] == '-') {
4653 param->fp_comp_end_sign = 1;
4657 if (arg_is_eof(tmp)) {
4658 param->fp_comp_end = LUSTRE_EOF;
4659 param->fp_comp_end_units = 1;
4662 rc = llapi_parse_size(tmp,
4663 ¶m->fp_comp_end,
4664 ¶m->fp_comp_end_units, 0);
4667 fprintf(stderr, "error: %s bad "
4668 "component end '%s'.\n",
4672 param->fp_check_comp_end = 1;
4674 param->fp_verbose |= VERBOSE_COMP_END;
4675 param->fp_max_depth = 0;
4679 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4680 param->fp_verbose |= VERBOSE_DFID;
4681 param->fp_max_depth = 0;
4685 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4686 param->fp_verbose |= VERBOSE_GENERATION;
4687 param->fp_max_depth = 0;
4691 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4692 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4693 param->fp_max_depth = 0;
4697 if (optarg != NULL) {
4698 param->fp_comp_id = strtoul(optarg, &end, 0);
4699 if (*end != '\0' || param->fp_comp_id == 0 ||
4700 param->fp_comp_id > LCME_ID_MAX) {
4701 fprintf(stderr, "error: %s bad "
4702 "component id '%s'\n",
4706 param->fp_check_comp_id = 1;
4709 param->fp_max_depth = 0;
4710 param->fp_verbose |= VERBOSE_COMP_ID;
4714 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4715 param->fp_verbose |= VERBOSE_PATTERN;
4716 param->fp_max_depth = 0;
4719 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4721 fprintf(stderr, "warning: '-M' deprecated"
4722 ", use '--mdt-index' or '-m' instead\n");
4725 if (!(param->fp_verbose & VERBOSE_DETAIL))
4726 param->fp_max_depth = 0;
4727 param->fp_verbose |= VERBOSE_MDTINDEX;
4730 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4731 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4732 param->fp_max_depth = 0;
4736 if (param->fp_obd_uuid) {
4738 "error: %s: only one obduuid allowed",
4742 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4745 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4746 param->fp_verbose |= VERBOSE_POOL;
4747 param->fp_max_depth = 0;
4754 param->fp_recursive = 1;
4760 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4761 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4762 param->fp_max_depth = 0;
4766 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4776 if (pathstart == -1) {
4777 fprintf(stderr, "error: %s: no filename|pathname\n",
4780 } else if (pathend == -1) {
4788 if (param->fp_recursive)
4789 param->fp_max_depth = -1;
4790 else if (param->fp_verbose & VERBOSE_DETAIL)
4791 param->fp_max_depth = 1;
4793 if (!param->fp_verbose)
4794 param->fp_verbose = VERBOSE_DEFAULT;
4795 if (param->fp_quiet)
4796 param->fp_verbose = VERBOSE_OBJID;
4799 rc = llapi_getstripe(argv[pathstart], param);
4800 } while (++pathstart < pathend && !rc);
4803 fprintf(stderr, "error: %s failed for %s.\n",
4804 argv[0], argv[optind - 1]);
4808 static int lfs_tgts(int argc, char **argv)
4810 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4811 struct find_param param;
4812 int index = 0, rc=0;
4817 if (argc == 2 && !realpath(argv[1], path)) {
4819 fprintf(stderr, "error: invalid path '%s': %s\n",
4820 argv[1], strerror(-rc));
4824 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4825 /* Check if we have a mount point */
4826 if (mntdir[0] == '\0')
4829 memset(¶m, 0, sizeof(param));
4830 if (!strcmp(argv[0], "mdts"))
4831 param.fp_get_lmv = 1;
4833 rc = llapi_ostlist(mntdir, ¶m);
4835 fprintf(stderr, "error: %s: failed on %s\n",
4838 if (path[0] != '\0')
4840 memset(mntdir, 0, PATH_MAX);
4846 static int lfs_getstripe(int argc, char **argv)
4848 struct find_param param = { 0 };
4850 param.fp_max_depth = 1;
4851 return lfs_getstripe_internal(argc, argv, ¶m);
4855 static int lfs_getdirstripe(int argc, char **argv)
4857 struct find_param param = { 0 };
4858 struct option long_opts[] = {
4859 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4860 { .val = 'D', .name = "default", .has_arg = no_argument },
4861 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4862 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4863 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4864 { .val = 'O', .name = "obd", .has_arg = required_argument },
4865 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4866 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4867 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4868 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4872 param.fp_get_lmv = 1;
4874 while ((c = getopt_long(argc, argv,
4875 "cDHimO:rtTvy", long_opts, NULL)) != -1)
4880 param.fp_verbose |= VERBOSE_COUNT;
4883 param.fp_get_default_lmv = 1;
4887 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4889 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4891 fprintf(stderr, "warning: '-t' deprecated, "
4892 "use '--mdt-hash' or '-H' instead\n");
4895 param.fp_verbose |= VERBOSE_HASH_TYPE;
4898 if (param.fp_obd_uuid) {
4900 "error: %s: only one obduuid allowed",
4904 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4907 param.fp_recursive = 1;
4910 param.fp_verbose |= VERBOSE_DETAIL;
4916 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4917 progname, argv[0], argv[optind - 1]);
4925 if (param.fp_recursive)
4926 param.fp_max_depth = -1;
4928 if (!param.fp_verbose)
4929 param.fp_verbose = VERBOSE_DEFAULT;
4932 rc = llapi_getstripe(argv[optind], ¶m);
4933 } while (++optind < argc && !rc);
4936 fprintf(stderr, "error: %s failed for %s.\n",
4937 argv[0], argv[optind - 1]);
4942 MNTDF_INODES = 0x0001,
4943 MNTDF_COOKED = 0x0002,
4944 MNTDF_LAZY = 0x0004,
4945 MNTDF_VERBOSE = 0x0008,
4946 MNTDF_SHOW = 0x0010,
4949 #define COOK(value) \
4952 while (value > 1024) { \
4960 #define CDF "%11llu"
4961 #define HDF "%8.1f%c"
4965 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4967 double avail, used, ratio = 0;
4970 avail = st->os_ffree;
4971 used = st->os_files - st->os_ffree;
4973 avail = st->os_bavail;
4974 used = st->os_blocks - st->os_bfree;
4976 if (avail + used > 0)
4977 ratio = used / (used + avail) * 100;
4979 /* Round up to match df(1) usage percentage */
4980 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4983 /* This is only used to reflect various problem states for lfs df, so we only
4984 * translate the flags reflecting those states.
4986 static char obd_statfs_state_names[] = {
4987 [OS_STATE_DEGRADED] = 'D',
4988 [OS_STATE_READONLY] = 'R',
4989 [OS_STATE_NOPRECREATE] = 'N',
4990 [OS_STATE_ENOSPC] = 'S',
4991 [OS_STATE_ENOINO] = 'I',
4994 static char obd_statfs_state2char(int s)
4997 if (s > ARRAY_SIZE(obd_statfs_state_names)/sizeof(char) ||
4998 obd_statfs_state_names[s] == 0)
5001 return obd_statfs_state_names[s];
5004 static int showdf(char *mntdir, struct obd_statfs *stat,
5005 char *uuid, enum mntdf_flags flags,
5006 char *type, int index, int rc)
5008 long long avail, used, total;
5010 char *suffix = "KMGTPEZY";
5011 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5012 char tbuf[3 * sizeof(__u64)];
5013 char ubuf[3 * sizeof(__u64)];
5014 char abuf[3 * sizeof(__u64)];
5015 char rbuf[3 * sizeof(__u64)];
5022 if (flags & MNTDF_INODES) {
5023 avail = stat->os_ffree;
5024 used = stat->os_files - stat->os_ffree;
5025 total = stat->os_files;
5027 int shift = flags & MNTDF_COOKED ? 0 : 10;
5029 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5030 used = ((stat->os_blocks - stat->os_bfree) *
5031 stat->os_bsize) >> shift;
5032 total = (stat->os_blocks * stat->os_bsize) >> shift;
5035 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5037 if (flags & MNTDF_COOKED) {
5041 cook_val = (double)total;
5044 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5047 snprintf(tbuf, sizeof(tbuf), CDF, total);
5049 cook_val = (double)used;
5052 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5055 snprintf(ubuf, sizeof(ubuf), CDF, used);
5057 cook_val = (double)avail;
5060 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5063 snprintf(abuf, sizeof(abuf), CDF, avail);
5065 snprintf(tbuf, sizeof(tbuf), CDF, total);
5066 snprintf(ubuf, sizeof(tbuf), CDF, used);
5067 snprintf(abuf, sizeof(tbuf), CDF, avail);
5070 sprintf(rbuf, RDF, ratio);
5071 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5072 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5074 printf("[%s:%d]", type, index);
5076 if (stat->os_state) {
5081 for (i = 0, state = stat->os_state; state != 0; i++) {
5082 uint32_t mask = 1 << i;
5083 if (!(state & mask) || mask == OS_STATE_NONROT)
5085 printf("%c", obd_statfs_state2char(mask));
5093 printf(UUF": inactive device\n", uuid);
5096 printf(UUF": %s\n", uuid, strerror(-rc));
5103 struct ll_stat_type {
5108 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5110 struct ll_statfs_data {
5112 struct obd_statfs sd_st;
5115 struct ll_statfs_buf {
5117 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5120 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5121 int ops, struct ll_statfs_buf *lsb)
5123 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5124 struct obd_uuid uuid_buf;
5125 char *poolname = NULL;
5126 struct ll_stat_type types[] = {
5127 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5128 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5129 { .st_name = NULL } };
5130 struct ll_stat_type *tp;
5131 __u64 ost_files = 0;
5132 __u64 ost_ffree = 0;
5140 poolname = strchr(pool, '.');
5141 if (poolname != NULL) {
5142 if (strncmp(fsname, pool, strlen(fsname))) {
5143 fprintf(stderr, "filesystem name incorrect\n");
5151 fd = open(mntdir, O_RDONLY);
5154 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5159 if (flags & MNTDF_SHOW) {
5160 if (flags & MNTDF_INODES)
5161 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5162 "UUID", "Inodes", "IUsed", "IFree",
5163 "IUse%", "Mounted on");
5165 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5167 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5168 "Used", "Available", "Use%", "Mounted on");
5171 for (tp = types; tp->st_name != NULL; tp++) {
5172 bool have_ost = false;
5174 if (!(tp->st_op & ops))
5177 for (index = 0; ; index++) {
5178 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5179 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5180 type = flags & MNTDF_LAZY ?
5181 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5182 rc2 = llapi_obd_fstatfs(fd, type, index,
5183 &stat_buf, &uuid_buf);
5188 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5189 if (!(flags & MNTDF_VERBOSE))
5191 } else if (rc2 < 0 && rc == 0) {
5195 /* If we have OSTs then don't report MDT block counts.
5196 * For MDT-only filesystems the expectation is that all
5197 * layouts have a DoM component. For filesystems with
5198 * OSTs, files are not necessarily going to store data
5199 * on MDTs, and MDT space is limited to a fraction of
5200 * OST space, so don't include it in the summary.
5202 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5209 if (poolname && tp->st_op == LL_STATFS_LOV &&
5210 llapi_search_ost(fsname, poolname,
5211 obd_uuid2str(&uuid_buf)) != 1)
5214 /* the llapi_obd_statfs() call may have returned with
5215 * an error, but if it filled in uuid_buf we will at
5216 * lease use that to print out a message for that OBD.
5217 * If we didn't get anything in the uuid_buf, then fill
5218 * it in so that we can print an error message. */
5219 if (uuid_buf.uuid[0] == '\0')
5220 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5221 "%s%04x", tp->st_name, index);
5223 lsb->sb_buf[lsb->sb_count].sd_index = index;
5224 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5227 if (flags & MNTDF_SHOW)
5228 showdf(mntdir, &stat_buf,
5229 obd_uuid2str(&uuid_buf), flags,
5230 tp->st_name, index, rc2);
5235 if (tp->st_op == LL_STATFS_LMV) {
5236 sum.os_ffree += stat_buf.os_ffree;
5237 sum.os_files += stat_buf.os_files;
5238 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5239 ost_files += stat_buf.os_files;
5240 ost_ffree += stat_buf.os_ffree;
5242 sum.os_blocks += stat_buf.os_blocks *
5244 sum.os_bfree += stat_buf.os_bfree *
5246 sum.os_bavail += stat_buf.os_bavail *
5253 /* If we have _some_ OSTs, but don't have as many free objects on the
5254 * OST as inodes on the MDTs, reduce the reported number of inodes
5255 * to compensate, so that the "inodes in use" number is correct.
5256 * This should be kept in sync with ll_statfs_internal().
5258 if (ost_files && ost_ffree < sum.os_ffree) {
5259 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5260 sum.os_ffree = ost_ffree;
5262 if (flags & MNTDF_SHOW) {
5264 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5271 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5273 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5275 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5277 int r1 = obd_statfs_ratio(st1, false);
5278 int r2 = obd_statfs_ratio(st2, false);
5279 int64_t result = r1 - r2;
5281 /* if both space usage are above 90, compare free inodes */
5282 if (r1 > 90 && r2 > 90)
5283 result = st2->os_ffree - st1->os_ffree;
5287 else if (result == 0)
5294 static int lfs_setdirstripe(int argc, char **argv)
5297 struct lfs_setstripe_args lsa = { 0 };
5298 struct llapi_stripe_param *param = NULL;
5299 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5302 char *mode_opt = NULL;
5303 bool default_stripe = false;
5304 bool delete = false;
5305 bool auto_distributed = false;
5306 bool foreign_mode = false;
5307 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5308 mode_t previous_mode = 0;
5309 struct ll_statfs_buf *lsb = NULL;
5310 char mntdir[PATH_MAX] = "";
5312 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5313 struct option long_opts[] = {
5314 { .val = 'c', .name = "count", .has_arg = required_argument },
5315 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5316 { .val = 'd', .name = "delete", .has_arg = no_argument },
5317 { .val = 'D', .name = "default", .has_arg = no_argument },
5318 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5319 { .val = LFS_LAYOUT_FLAGS_OPT,
5320 .name = "flags", .has_arg = required_argument },
5321 { .val = LFS_LAYOUT_FOREIGN_OPT,
5322 .name = "foreign", .has_arg = optional_argument},
5323 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5324 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5325 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5326 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5328 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5329 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5331 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5332 { .val = 'i', .name = "index", .has_arg = required_argument },
5334 { .val = 'o', .name = "mode", .has_arg = required_argument },
5335 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5336 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5338 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5339 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5340 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5344 setstripe_args_init(&lsa);
5346 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5347 long_opts, NULL)) >= 0) {
5354 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5357 "%s %s: invalid stripe count '%s'\n",
5358 progname, argv[0], optarg);
5364 default_stripe = true;
5367 default_stripe = true;
5369 case LFS_LAYOUT_FOREIGN_OPT:
5370 if (optarg != NULL) {
5371 /* check pure numeric */
5372 type = strtoul(optarg, &end, 0);
5375 type = check_foreign_type_name(optarg);
5376 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5378 "%s %s: unknown foreign type '%s'\n",
5385 foreign_mode = true;
5387 case LFS_LAYOUT_FLAGS_OPT:
5388 flags = strtoul(optarg, &end, 16);
5391 "%s %s: bad flags '%s'\n",
5392 progname, argv[0], optarg);
5396 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5398 fprintf(stderr, "warning: '--hash-type' and '-t' "
5399 "deprecated, use '--mdt-hash' or '-H' instead\n");
5402 lsa.lsa_pattern = check_hashtype(optarg);
5403 if (lsa.lsa_pattern == 0) {
5405 "%s %s: bad stripe hash type '%s'\n",
5406 progname, argv[0], optarg);
5411 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5414 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5415 if (strcmp(argv[optind - 1], "--index") == 0)
5417 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5420 lsa.lsa_nr_tgts = parse_targets(mdts,
5421 sizeof(mdts) / sizeof(__u32),
5422 lsa.lsa_nr_tgts, optarg, NULL);
5423 if (lsa.lsa_nr_tgts < 0) {
5425 "%s %s: invalid MDT target(s) '%s'\n",
5426 progname, argv[0], optarg);
5430 lsa.lsa_tgts = mdts;
5431 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5432 lsa.lsa_stripe_off = mdts[0];
5434 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5436 fprintf(stderr, "warning: '-m' is deprecated, "
5437 "use '--mode' or '-o' instead\n");
5446 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5447 progname, argv[0], argv[optind - 1]);
5452 if (optind == argc) {
5453 fprintf(stderr, "%s %s: DIR must be specified\n",
5458 if (xattr && !foreign_mode) {
5459 /* only print a warning as this is armless and will be
5463 "%s %s: xattr has been specified for non-foreign layout\n",
5465 } else if (foreign_mode && !xattr) {
5467 "%s %s: xattr must be provided in foreign mode\n",
5472 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5473 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5475 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5480 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5481 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5483 "%s %s: stripe offset and count must be specified\n",
5489 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5490 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5492 "%s %s: cannot specify -d with -c or -i options\n",
5497 if (mode_opt != NULL) {
5498 mode = strtoul(mode_opt, &end, 8);
5501 "%s %s: bad MODE '%s'\n",
5502 progname, argv[0], mode_opt);
5505 previous_mode = umask(0);
5508 /* foreign LMV/dir case */
5510 if (argc > optind + 1) {
5512 "%s %s: cannot specify multiple foreign dirs\n",
5517 dname = argv[optind];
5518 result = llapi_dir_create_foreign(dname, mode, type, flags,
5522 "%s mkdir: can't create foreign dir '%s': %s\n",
5523 progname, dname, strerror(-result));
5528 * initialize stripe parameters, in case param is converted to specific,
5529 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5531 param = calloc(1, offsetof(typeof(*param),
5532 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5533 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5534 if (param == NULL) {
5536 "%s %s: cannot allocate memory for parameters: %s\n",
5537 progname, argv[0], strerror(ENOMEM));
5541 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5542 param->lsp_stripe_count = lsa.lsa_stripe_count;
5543 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5544 param->lsp_stripe_offset = -1;
5546 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5547 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5548 param->lsp_stripe_pattern = lsa.lsa_pattern;
5550 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
5551 param->lsp_pool = lsa.lsa_pool_name;
5552 param->lsp_is_specific = false;
5553 if (lsa.lsa_nr_tgts > 1) {
5554 if (lsa.lsa_stripe_count > 0 &&
5555 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5556 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5557 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5558 "match the number of MDTs: %d\n",
5559 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5564 param->lsp_is_specific = true;
5565 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5566 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5569 if (!default_stripe && (lsa.lsa_pattern & LMV_HASH_FLAG_SPACE)) {
5570 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5576 if (param->lsp_stripe_offset != -1 &&
5577 lsa.lsa_pattern & LMV_HASH_FLAG_SPACE) {
5578 fprintf(stderr, "%s %s: can only specify -H space with -i -1\n",
5584 dname = argv[optind];
5586 if (default_stripe) {
5587 result = llapi_dir_set_default_lmv(dname, param);
5590 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5591 progname, dname, strerror(-result));
5596 * if current \a dname isn't under the same \a mntdir as the
5597 * last one, and the last one was auto-distributed, restore
5600 if (mntdir[0] != '\0' &&
5601 strncmp(dname, mntdir, strlen(mntdir)) &&
5603 param->lsp_is_specific = false;
5604 param->lsp_stripe_offset = -1;
5605 auto_distributed = false;
5609 * TODO: when MDT can allocate object with QoS (LU-9435), below
5610 * code should be removed, instead we should let LMV to allocate
5611 * the starting MDT object, and then let LOD allocate other MDT
5614 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5615 char path[PATH_MAX] = "";
5618 lsb = malloc(sizeof(*lsb));
5626 /* use mntdir for dirname() temporarily */
5627 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5628 if (!realpath(dirname(mntdir), path)) {
5631 "error: invalid path '%s': %s\n",
5632 argv[optind], strerror(errno));
5637 result = llapi_search_mounts(path, 0, mntdir, NULL);
5638 if (result < 0 || mntdir[0] == '\0') {
5640 "No suitable Lustre mount found\n");
5644 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5649 if (param->lsp_stripe_count > lsb->sb_count) {
5651 "error: stripe count %d is too big\n",
5652 param->lsp_stripe_count);
5657 qsort(lsb->sb_buf, lsb->sb_count,
5658 sizeof(struct ll_statfs_data),
5659 ll_statfs_data_comp);
5661 auto_distributed = true;
5664 if (auto_distributed) {
5666 int nr = MAX(param->lsp_stripe_count,
5669 /* don't use server whose usage is above 90% */
5670 while (nr != param->lsp_stripe_count &&
5671 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5673 nr = MAX(param->lsp_stripe_count, nr / 2);
5675 /* get \a r between [0, nr) */
5678 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5679 if (param->lsp_stripe_count > 1) {
5682 param->lsp_is_specific = true;
5683 for (; i < param->lsp_stripe_count; i++)
5684 param->lsp_tgts[(i + r) % nr] =
5685 lsb->sb_buf[i].sd_index;
5689 result = llapi_dir_create(dname, mode, param);
5692 "%s setdirstripe: cannot create dir '%s': %s\n",
5693 progname, dname, strerror(-result));
5694 } while (!result && (dname = argv[++optind]));
5696 if (mode_opt != NULL)
5697 umask(previous_mode);
5705 static int lfs_rmentry(int argc, char **argv)
5712 fprintf(stderr, "error: %s: missing dirname\n",
5718 dname = argv[index];
5719 while (dname != NULL) {
5720 result = llapi_direntry_remove(dname);
5722 fprintf(stderr, "error: %s: remove dir entry '%s' "
5723 "failed\n", argv[0], dname);
5726 dname = argv[++index];
5731 static int lfs_mv(int argc, char **argv)
5733 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5734 struct find_param param = {
5741 struct option long_opts[] = {
5742 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5743 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5744 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5747 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5749 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5751 fprintf(stderr, "warning: '-M' deprecated"
5752 ", use '--mdt-index' or '-m' instead\n");
5755 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5757 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5763 param.fp_verbose = VERBOSE_DETAIL;
5766 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5767 progname, argv[optind - 1]);
5772 if (lmu.lum_stripe_offset == -1) {
5773 fprintf(stderr, "%s mv: MDT index must be specified\n",
5778 if (optind >= argc) {
5779 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5784 /* initialize migrate mdt parameters */
5785 param.fp_lmv_md = &lmu;
5786 param.fp_migrate = 1;
5787 rc = llapi_migrate_mdt(argv[optind], ¶m);
5789 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5790 progname, argv[optind], param.fp_mdt_index,
5795 static int lfs_osts(int argc, char **argv)
5797 return lfs_tgts(argc, argv);
5800 static int lfs_mdts(int argc, char **argv)
5802 return lfs_tgts(argc, argv);
5805 static int lfs_df(int argc, char **argv)
5807 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5808 enum mntdf_flags flags = MNTDF_SHOW;
5809 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5810 int c, rc = 0, index = 0;
5811 char fsname[PATH_MAX] = "", *pool_name = NULL;
5812 struct option long_opts[] = {
5813 { .val = 'h', .name = "human-readable",
5814 .has_arg = no_argument },
5815 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5816 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5817 { .val = 'p', .name = "pool", .has_arg = required_argument },
5818 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5821 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5824 flags |= MNTDF_COOKED;
5827 flags |= MNTDF_INODES;
5830 flags |= MNTDF_LAZY;
5836 flags |= MNTDF_VERBOSE;
5842 if (optind < argc && !realpath(argv[optind], path)) {
5844 fprintf(stderr, "error: invalid path '%s': %s\n",
5845 argv[optind], strerror(-rc));
5849 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5850 /* Check if we have a mount point */
5851 if (mntdir[0] == '\0')
5854 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5855 if (rc || path[0] != '\0')
5857 fsname[0] = '\0'; /* avoid matching in next loop */
5858 mntdir[0] = '\0'; /* avoid matching in next loop */
5864 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5865 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5869 /* llapi_search_mounts() fills "fsname", but that is not called if
5870 * explicit paths are specified on the command-line
5872 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5873 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5876 fprintf(stderr, "cannot get instance for '%s': %s\n",
5877 mntdir, strerror(-rc));
5881 if (!opt_instance) {
5882 /* print only the fsname name */
5883 buf = strchr(fsname, '-');
5887 } else if (!opt_fsname) {
5888 /* print only the instance name */
5889 buf = strchr(fsname, '-');
5895 } else if (opt_fsname) {
5896 /* print only the fsname */
5900 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5905 static int lfs_getname(int argc, char **argv)
5907 struct option long_opts[] = {
5908 { .val = 'h', .name = "help", .has_arg = no_argument },
5909 { .val = 'i', .name = "instance", .has_arg = no_argument },
5910 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5912 bool opt_instance = false, opt_fsname = false;
5913 char fsname[PATH_MAX] = "";
5916 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5919 opt_instance = true;
5930 /* If neither option is given, print both instance and fsname */
5931 if (!opt_instance && !opt_fsname)
5932 opt_instance = opt_fsname = true;
5934 if (optind == argc) { /* no paths specified, get all paths. */
5935 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5938 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5939 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5940 opt_instance, opt_fsname, true);
5943 path[0] = fsname[0] = mntdir[0] = '\0';
5945 } else { /* paths specified, only attempt to search these. */
5948 /* if only one path is given, print only requested info */
5949 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5951 for (; optind < argc; optind++) {
5952 rc2 = print_instance(argv[optind], fsname,
5953 sizeof(fsname), opt_instance,
5954 opt_fsname, opt_mntdir);
5964 static int lfs_check(int argc, char **argv)
5966 char mntdir[PATH_MAX] = {'\0'};
5975 fprintf(stderr, "%s check: server type must be specified\n",
5980 obd_types[0] = obd_type1;
5981 obd_types[1] = obd_type2;
5982 obd_types[2] = obd_type3;
5984 if (strcmp(argv[1], "osts") == 0) {
5985 strcpy(obd_types[0], "osc");
5986 } else if (strcmp(argv[1], "mdts") == 0 ||
5987 strcmp(argv[1], "mds") == 0) {
5988 strcpy(obd_types[0], "mdc");
5989 } else if (strcmp(argv[1], "mgts") == 0) {
5990 strcpy(obd_types[0], "mgc");
5991 } else if (strcmp(argv[1], "all") == 0 ||
5992 strcmp(argv[1], "servers") == 0) {
5994 strcpy(obd_types[0], "osc");
5995 strcpy(obd_types[1], "mdc");
5996 strcpy(obd_types[2], "mgc");
5998 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6003 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6004 if (rc < 0 || mntdir[0] == '\0') {
6006 "%s check: cannot find mounted Lustre filesystem: %s\n",
6007 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6011 rc = llapi_target_check(num_types, obd_types, mntdir);
6013 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6014 progname, argv[1], strerror(-rc));
6020 #ifdef HAVE_SYS_QUOTA_H
6021 #define ARG2INT(nr, str, msg) \
6024 nr = strtol(str, &endp, 0); \
6025 if (*endp != '\0') { \
6026 fprintf(stderr, "%s: bad %s '%s'\n", \
6027 progname, msg, str); \
6032 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6034 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6035 * returns the value or ULONG_MAX on integer overflow or incorrect format
6037 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6038 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6039 * 3. empty integer value is interpreted as 0
6041 static unsigned long str2sec(const char* timestr)
6043 const char spec[] = "smhdw";
6044 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6045 unsigned long val = 0;
6048 if (strpbrk(timestr, spec) == NULL) {
6049 /* no specifiers inside the time string,
6050 should treat it as an integer value */
6051 val = strtoul(timestr, &tail, 10);
6052 return *tail ? ULONG_MAX : val;
6055 /* format string is XXwXXdXXhXXmXXs */
6061 v = strtoul(timestr, &tail, 10);
6062 if (v == ULONG_MAX || *tail == '\0')
6063 /* value too large (ULONG_MAX or more)
6064 or missing specifier */
6067 ptr = strchr(spec, *tail);
6069 /* unknown specifier */
6074 /* check if product will overflow the type */
6075 if (!(v < ULONG_MAX / mult[ind]))
6078 ADD_OVERFLOW(val, mult[ind] * v);
6079 if (val == ULONG_MAX)
6091 #define ARG2ULL(nr, str, def_units) \
6093 unsigned long long limit, units = def_units; \
6096 rc = llapi_parse_size(str, &limit, &units, 1); \
6098 fprintf(stderr, "%s: invalid limit '%s'\n", \
6105 static inline int has_times_option(int argc, char **argv)
6109 for (i = 1; i < argc; i++)
6110 if (!strcmp(argv[i], "-t"))
6116 int lfs_setquota_times(int argc, char **argv)
6119 struct if_quotactl qctl;
6120 char *mnt, *obd_type = (char *)qctl.obd_type;
6121 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6122 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6123 struct option long_opts[] = {
6124 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6125 { .val = 'g', .name = "group", .has_arg = no_argument },
6126 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6127 { .val = 'p', .name = "projid", .has_arg = no_argument },
6128 { .val = 't', .name = "times", .has_arg = no_argument },
6129 { .val = 'u', .name = "user", .has_arg = no_argument },
6133 memset(&qctl, 0, sizeof(qctl));
6134 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6135 qctl.qc_type = ALLQUOTA;
6137 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6138 long_opts, NULL)) != -1) {
6149 if (qctl.qc_type != ALLQUOTA) {
6150 fprintf(stderr, "error: -u/g/p can't be used "
6151 "more than once\n");
6154 qctl.qc_type = qtype;
6157 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6158 fprintf(stderr, "error: bad block-grace: %s\n",
6162 dqb->dqb_valid |= QIF_BTIME;
6165 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6166 fprintf(stderr, "error: bad inode-grace: %s\n",
6170 dqb->dqb_valid |= QIF_ITIME;
6172 case 't': /* Yes, of course! */
6174 default: /* getopt prints error message for us when opterr != 0 */
6179 if (qctl.qc_type == ALLQUOTA) {
6180 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6184 if (optind != argc - 1) {
6185 fprintf(stderr, "error: unexpected parameters encountered\n");
6189 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6190 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6191 fprintf(stderr, "error: grace time is too large\n");
6196 rc = llapi_quotactl(mnt, &qctl);
6199 fprintf(stderr, "%s %s ", obd_type,
6200 obd_uuid2str(&qctl.obd_uuid));
6201 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6208 #define BSLIMIT (1 << 0)
6209 #define BHLIMIT (1 << 1)
6210 #define ISLIMIT (1 << 2)
6211 #define IHLIMIT (1 << 3)
6213 int lfs_setquota(int argc, char **argv)
6216 struct if_quotactl qctl;
6217 char *mnt, *obd_type = (char *)qctl.obd_type;
6218 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6219 struct option long_opts[] = {
6220 { .val = 'b', .name = "block-softlimit",
6221 .has_arg = required_argument },
6222 { .val = 'B', .name = "block-hardlimit",
6223 .has_arg = required_argument },
6224 { .val = 'd', .name = "default", .has_arg = no_argument },
6225 { .val = 'g', .name = "group", .has_arg = required_argument },
6226 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6227 { .val = 'i', .name = "inode-softlimit",
6228 .has_arg = required_argument },
6229 { .val = 'I', .name = "inode-hardlimit",
6230 .has_arg = required_argument },
6231 { .val = 'p', .name = "projid", .has_arg = required_argument },
6232 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6233 { .val = 'u', .name = "user", .has_arg = required_argument },
6234 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6236 unsigned limit_mask = 0;
6238 bool use_default = false;
6241 if (has_times_option(argc, argv))
6242 return lfs_setquota_times(argc, argv);
6244 memset(&qctl, 0, sizeof(qctl));
6245 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6246 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6247 * so it can be used as a marker that qc_type
6248 * isn't reinitialized from command line */
6250 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6251 long_opts, NULL)) != -1) {
6254 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6257 goto quota_type_def;
6260 rc = name2uid(&qctl.qc_id, optarg);
6263 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6266 goto quota_type_def;
6269 rc = name2gid(&qctl.qc_id, optarg);
6272 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6275 goto quota_type_def;
6278 rc = name2projid(&qctl.qc_id, optarg);
6281 qctl.qc_id = strtoul(optarg, &endptr, 10);
6282 if (*endptr != '\0') {
6283 fprintf(stderr, "%s setquota: invalid"
6284 " id '%s'\n", progname, optarg);
6289 if (qctl.qc_id == 0) {
6290 fprintf(stderr, "%s setquota: can't set quota"
6291 " for root usr/group/project.\n",
6297 if (qctl.qc_type != ALLQUOTA) {
6299 "%s setquota: only one of -u, -U, -g,"
6300 " -G, -p or -P may be specified\n",
6304 qctl.qc_type = qtype;
6307 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6311 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6312 dqb->dqb_bsoftlimit >>= 10;
6313 limit_mask |= BSLIMIT;
6314 if (dqb->dqb_bsoftlimit &&
6315 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6317 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6318 "See '%s help setquota' or Lustre manual for details\n",
6319 progname, dqb->dqb_bsoftlimit,
6323 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6324 dqb->dqb_bhardlimit >>= 10;
6325 limit_mask |= BHLIMIT;
6326 if (dqb->dqb_bhardlimit &&
6327 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6329 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6330 "See '%s help setquota' or Lustre manual for details\n",
6331 progname, dqb->dqb_bhardlimit,
6335 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6336 limit_mask |= ISLIMIT;
6337 if (dqb->dqb_isoftlimit &&
6338 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6340 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6341 "See '%s help setquota' or Lustre manual for details\n",
6342 progname, dqb->dqb_isoftlimit,
6346 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6347 limit_mask |= IHLIMIT;
6348 if (dqb->dqb_ihardlimit &&
6349 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6351 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6352 "See '%s help setquota' or Lustre manual for details\n",
6353 progname, dqb->dqb_ihardlimit,
6358 "%s setquota: unrecognized option '%s'\n",
6359 progname, argv[optind - 1]);
6364 if (qctl.qc_type == ALLQUOTA) {
6366 "%s setquota: either -u or -g must be specified\n",
6371 if (!use_default && limit_mask == 0) {
6373 "%s setquota: at least one limit must be specified\n",
6378 if (use_default && limit_mask != 0) {
6380 "%s setquota: limits should not be specified when"
6381 " using default quota\n",
6386 if (use_default && qctl.qc_id == 0) {
6388 "%s setquota: can not set default quota for root"
6389 " user/group/project\n",
6394 if (optind != argc - 1) {
6396 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6397 progname, argv[optind]);
6404 dqb->dqb_bhardlimit = 0;
6405 dqb->dqb_bsoftlimit = 0;
6406 dqb->dqb_ihardlimit = 0;
6407 dqb->dqb_isoftlimit = 0;
6410 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6411 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6412 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6413 /* sigh, we can't just set blimits/ilimits */
6414 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6415 .qc_type = qctl.qc_type,
6416 .qc_id = qctl.qc_id};
6418 rc = llapi_quotactl(mnt, &tmp_qctl);
6422 if (!(limit_mask & BHLIMIT))
6423 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6424 if (!(limit_mask & BSLIMIT))
6425 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6426 if (!(limit_mask & IHLIMIT))
6427 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6428 if (!(limit_mask & ISLIMIT))
6429 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6431 /* Keep grace times if we have got no softlimit arguments */
6432 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6433 dqb->dqb_valid |= QIF_BTIME;
6434 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6437 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6438 dqb->dqb_valid |= QIF_ITIME;
6439 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6443 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6444 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6446 rc = llapi_quotactl(mnt, &qctl);
6450 "%s setquota: cannot quotactl '%s' '%s': %s",
6452 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6459 /* Converts seconds value into format string
6460 * result is returned in buf
6462 * 1. result is in descenting order: 1w2d3h4m5s
6463 * 2. zero fields are not filled (except for p. 3): 5d1s
6464 * 3. zero seconds value is presented as "0s"
6466 static char * __sec2str(time_t seconds, char *buf)
6468 const char spec[] = "smhdw";
6469 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6474 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6475 c = seconds / mult[i];
6477 if (c > 0 || (i == 0 && buf == tail))
6478 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6486 static void sec2str(time_t seconds, char *buf, int rc)
6493 tail = __sec2str(seconds, tail);
6495 if (rc && tail - buf < 39) {
6501 static void diff2str(time_t seconds, char *buf, time_t now)
6507 if (seconds <= now) {
6508 strcpy(buf, "none");
6511 __sec2str(seconds - now, buf);
6514 static void print_quota_title(char *name, struct if_quotactl *qctl,
6515 bool human_readable, bool show_default)
6518 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6519 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6520 "Filesystem", "bquota", "blimit", "bgrace",
6521 "iquota", "ilimit", "igrace");
6523 printf("Disk quotas for %s %s (%cid %u):\n",
6524 qtype_name(qctl->qc_type), name,
6525 *qtype_name(qctl->qc_type), qctl->qc_id);
6526 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6527 "Filesystem", human_readable ? "used" : "kbytes",
6528 "quota", "limit", "grace",
6529 "files", "quota", "limit", "grace");
6533 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6536 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6539 snprintf(buf, buflen, "%5.4gP",
6540 (double)num / ((__u64)1 << 40));
6542 snprintf(buf, buflen, "%5.4gT",
6543 (double)num / (1 << 30));
6545 snprintf(buf, buflen, "%5.4gG",
6546 (double)num / (1 << 20));
6548 snprintf(buf, buflen, "%5.4gM",
6549 (double)num / (1 << 10));
6551 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6555 #define STRBUF_LEN 32
6556 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6557 int rc, bool h, bool show_default)
6563 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6564 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6565 int bover = 0, iover = 0;
6566 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6567 char numbuf[3][STRBUF_LEN];
6569 char strbuf[STRBUF_LEN];
6571 if (dqb->dqb_bhardlimit &&
6572 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6574 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6575 if (dqb->dqb_btime > now) {
6582 if (dqb->dqb_ihardlimit &&
6583 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6585 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6586 if (dqb->dqb_itime > now) {
6594 if (strlen(mnt) > 15)
6595 printf("%s\n%15s", mnt, "");
6597 printf("%15s", mnt);
6600 diff2str(dqb->dqb_btime, timebuf, now);
6601 else if (show_default)
6602 snprintf(timebuf, sizeof(timebuf), "%llu",
6605 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6606 strbuf, sizeof(strbuf), h);
6607 if (rc == -EREMOTEIO)
6608 sprintf(numbuf[0], "%s*", strbuf);
6610 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6611 "%s" : "[%s]", strbuf);
6613 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6614 if (type == QC_GENERAL)
6615 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6616 "%s" : "[%s]", strbuf);
6618 sprintf(numbuf[1], "%s", "-");
6620 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6621 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6622 "%s" : "[%s]", strbuf);
6625 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6627 printf(" %7s%c %6s %7s %7s",
6628 numbuf[0], bover ? '*' : ' ', numbuf[1],
6629 numbuf[2], bover > 1 ? timebuf : "-");
6633 diff2str(dqb->dqb_itime, timebuf, now);
6634 else if (show_default)
6635 snprintf(timebuf, sizeof(timebuf), "%llu",
6638 snprintf(numbuf[0], sizeof(numbuf),
6639 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6640 (uintmax_t)dqb->dqb_curinodes);
6642 if (type == QC_GENERAL)
6643 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6645 (uintmax_t)dqb->dqb_isoftlimit);
6647 sprintf(numbuf[1], "%s", "-");
6649 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6650 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6653 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6654 else if (type != QC_OSTIDX)
6655 printf(" %7s%c %6s %7s %7s",
6656 numbuf[0], iover ? '*' : ' ', numbuf[1],
6657 numbuf[2], iover > 1 ? timebuf : "-");
6659 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6662 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6663 qctl->qc_cmd == Q_GETOINFO) {
6667 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6668 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6669 printf("Block grace time: %s; Inode grace time: %s\n",
6670 bgtimebuf, igtimebuf);
6674 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6675 bool h, __u64 *total)
6677 int rc = 0, rc1 = 0, count = 0;
6678 __u32 valid = qctl->qc_valid;
6680 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6682 fprintf(stderr, "can not get %s count: %s\n",
6683 is_mdt ? "mdt": "ost", strerror(-rc));
6687 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6688 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6689 rc = llapi_quotactl(mnt, qctl);
6691 /* It is remote client case. */
6692 if (rc == -EOPNOTSUPP) {
6699 fprintf(stderr, "quotactl %s%d failed.\n",
6700 is_mdt ? "mdt": "ost", qctl->qc_idx);
6704 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6705 qctl->qc_valid, 0, h, false);
6706 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6707 qctl->qc_dqblk.dqb_bhardlimit;
6710 qctl->qc_valid = valid;
6714 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6715 int verbose, int quiet, bool human_readable,
6718 int rc1 = 0, rc2 = 0, rc3 = 0;
6719 char *obd_type = (char *)qctl->obd_type;
6720 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6721 __u64 total_ialloc = 0, total_balloc = 0;
6722 bool use_default_for_blk = false;
6723 bool use_default_for_file = false;
6726 rc1 = llapi_quotactl(mnt, qctl);
6730 fprintf(stderr, "%s quotas are not enabled.\n",
6731 qtype_name(qctl->qc_type));
6734 fprintf(stderr, "Permission denied.\n");
6737 /* We already got error message. */
6740 fprintf(stderr, "Unexpected quotactl error: %s\n",
6745 if (!show_default && qctl->qc_id == 0) {
6746 qctl->qc_dqblk.dqb_bhardlimit = 0;
6747 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6748 qctl->qc_dqblk.dqb_ihardlimit = 0;
6749 qctl->qc_dqblk.dqb_isoftlimit = 0;
6750 qctl->qc_dqblk.dqb_btime = 0;
6751 qctl->qc_dqblk.dqb_itime = 0;
6752 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6755 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6756 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6757 use_default_for_blk = true;
6758 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6761 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6762 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6763 use_default_for_file = true;
6764 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6767 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6768 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6769 print_quota_title(name, qctl, human_readable, show_default);
6771 if (rc1 && *obd_type)
6772 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6774 if (qctl->qc_valid != QC_GENERAL)
6777 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6778 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6779 (QIF_LIMITS|QIF_USAGE));
6781 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6783 if (!show_default && verbose &&
6784 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6785 char strbuf[STRBUF_LEN];
6787 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6789 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6791 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6793 printf("Total allocated inode limit: %ju, total "
6794 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6798 if (use_default_for_blk)
6799 printf("%cid %u is using default block quota setting\n",
6800 *qtype_name(qctl->qc_type), qctl->qc_id);
6802 if (use_default_for_file)
6803 printf("%cid %u is using default file quota setting\n",
6804 *qtype_name(qctl->qc_type), qctl->qc_id);
6806 if (rc1 || rc2 || rc3 || inacc)
6807 printf("Some errors happened when getting quota info. "
6808 "Some devices may be not working or deactivated. "
6809 "The data in \"[]\" is inaccurate.\n");
6815 static int lfs_project(int argc, char **argv)
6817 int ret = 0, err = 0, c, i;
6818 struct project_handle_control phc = { 0 };
6819 enum lfs_project_ops_t op;
6822 phc.assign_projid = false;
6823 /* default action */
6824 op = LFS_PROJECT_LIST;
6826 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6829 if (op != LFS_PROJECT_LIST) {
6831 "%s: cannot specify '-c' '-C' '-s' together\n",
6836 op = LFS_PROJECT_CHECK;
6839 if (op != LFS_PROJECT_LIST) {
6841 "%s: cannot specify '-c' '-C' '-s' together\n",
6846 op = LFS_PROJECT_CLEAR;
6849 if (op != LFS_PROJECT_LIST) {
6851 "%s: cannot specify '-c' '-C' '-s' together\n",
6856 phc.set_inherit = true;
6857 op = LFS_PROJECT_SET;
6863 phc.keep_projid = true;
6866 phc.recursive = true;
6869 phc.projid = strtoul(optarg, NULL, 0);
6870 phc.assign_projid = true;
6874 phc.newline = false;
6877 fprintf(stderr, "%s: invalid option '%c'\n",
6883 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6884 op = LFS_PROJECT_SET;
6885 phc.set_projid = true;
6886 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6887 phc.set_projid = true;
6891 case LFS_PROJECT_CHECK:
6892 if (phc.keep_projid) {
6894 "%s: '-k' is useless together with '-c'\n",
6899 case LFS_PROJECT_CLEAR:
6902 "%s: '-0' is useless together with '-C'\n",
6906 if (phc.assign_projid) {
6908 "%s: '-p' is useless together with '-C'\n",
6913 case LFS_PROJECT_SET:
6916 "%s: '-0' is useless together with '-s'\n",
6920 if (phc.keep_projid) {
6922 "%s: '-k' is useless together with '-s'\n",
6930 "%s: '-0' is useless for list operations\n",
6940 fprintf(stderr, "%s: missing file or directory target(s)\n",
6945 for (i = 0; i < argc; i++) {
6947 case LFS_PROJECT_CHECK:
6948 err = lfs_project_check(argv[i], &phc);
6950 case LFS_PROJECT_LIST:
6951 err = lfs_project_list(argv[i], &phc);
6953 case LFS_PROJECT_CLEAR:
6954 err = lfs_project_clear(argv[i], &phc);
6956 case LFS_PROJECT_SET:
6957 err = lfs_project_set(argv[i], &phc);
6969 static int lfs_quota(int argc, char **argv)
6972 char *mnt, *name = NULL;
6973 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6974 .qc_type = ALLQUOTA };
6975 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6976 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6978 __u32 valid = QC_GENERAL, idx = 0;
6979 bool human_readable = false;
6980 bool show_default = false;
6983 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6986 show_default = true;
6991 show_default = true;
6996 show_default = true;
7000 if (qctl.qc_type != ALLQUOTA) {
7002 "%s quota: only one of -u, -g, or -p may be specified\n",
7006 qctl.qc_type = qtype;
7009 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7012 valid = qctl.qc_valid = QC_UUID;
7013 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7016 valid = qctl.qc_valid = QC_MDTIDX;
7017 idx = qctl.qc_idx = atoi(optarg);
7018 if (idx == 0 && *optarg != '0') {
7020 "%s quota: invalid MDT index '%s'\n",
7026 valid = qctl.qc_valid = QC_OSTIDX;
7027 idx = qctl.qc_idx = atoi(optarg);
7028 if (idx == 0 && *optarg != '0') {
7030 "%s quota: invalid OST index '%s'\n",
7042 human_readable = true;
7045 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7046 progname, argv[optind - 1]);
7051 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7052 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7053 optind == argc - 1 && !show_default) {
7055 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7056 qctl.qc_valid = valid;
7059 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7060 qctl.qc_type = qtype;
7061 if (qtype == USRQUOTA) {
7062 qctl.qc_id = geteuid();
7063 rc = uid2name(&name, qctl.qc_id);
7065 qctl.qc_id = getegid();
7066 rc = gid2name(&name, qctl.qc_id);
7071 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7072 human_readable, show_default);
7077 /* lfs quota -u username /path/to/lustre/mount */
7078 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7079 /* options should be followed by u/g-name and mntpoint */
7080 if ((!show_default && optind + 2 != argc) ||
7081 (show_default && optind + 1 != argc) ||
7082 qctl.qc_type == ALLQUOTA) {
7084 "%s quota: name and mount point must be specified\n",
7089 if (!show_default) {
7090 name = argv[optind++];
7091 switch (qctl.qc_type) {
7093 rc = name2uid(&qctl.qc_id, name);
7096 rc = name2gid(&qctl.qc_id, name);
7099 rc = name2projid(&qctl.qc_id, name);
7106 qctl.qc_valid = QC_GENERAL;
7107 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7112 qctl.qc_id = strtoul(name, &endptr, 10);
7113 if (*endptr != '\0') {
7114 fprintf(stderr, "%s quota: invalid id '%s'\n",
7119 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7120 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7126 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7127 human_readable, show_default);
7130 #endif /* HAVE_SYS_QUOTA_H! */
7132 static int flushctx_ioctl(char *mp)
7136 fd = open(mp, O_RDONLY);
7138 fprintf(stderr, "flushctx: error open %s: %s\n",
7139 mp, strerror(errno));
7143 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7145 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7146 mp, strerror(errno));
7152 static int lfs_flushctx(int argc, char **argv)
7154 int kdestroy = 0, c;
7155 char mntdir[PATH_MAX] = {'\0'};
7159 while ((c = getopt(argc, argv, "k")) != -1) {
7165 fprintf(stderr, "error: %s: option '-%c' "
7166 "unrecognized\n", argv[0], c);
7172 if ((rc = system("kdestroy > /dev/null")) != 0) {
7173 rc = WEXITSTATUS(rc);
7174 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7178 if (optind >= argc) {
7179 /* flush for all mounted lustre fs. */
7180 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7181 /* Check if we have a mount point */
7182 if (mntdir[0] == '\0')
7185 if (flushctx_ioctl(mntdir))
7188 mntdir[0] = '\0'; /* avoid matching in next loop */
7191 /* flush fs as specified */
7192 while (optind < argc) {
7193 if (flushctx_ioctl(argv[optind++]))
7200 static int lfs_changelog(int argc, char **argv)
7202 void *changelog_priv;
7203 struct changelog_rec *rec;
7204 long long startrec = 0, endrec = 0;
7206 struct option long_opts[] = {
7207 { .val = 'f', .name = "follow", .has_arg = no_argument },
7209 char short_opts[] = "f";
7212 while ((rc = getopt_long(argc, argv, short_opts,
7213 long_opts, NULL)) != -1) {
7220 "%s changelog: unrecognized option '%s'\n",
7221 progname, argv[optind - 1]);
7225 if (optind >= argc) {
7226 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7231 mdd = argv[optind++];
7233 startrec = strtoll(argv[optind++], NULL, 10);
7235 endrec = strtoll(argv[optind++], NULL, 10);
7237 rc = llapi_changelog_start(&changelog_priv,
7238 CHANGELOG_FLAG_BLOCK |
7239 CHANGELOG_FLAG_JOBID |
7240 CHANGELOG_FLAG_EXTRA_FLAGS |
7241 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7244 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7245 progname, strerror(errno = -rc));
7249 rc = llapi_changelog_set_xflags(changelog_priv,
7250 CHANGELOG_EXTRA_FLAG_UIDGID |
7251 CHANGELOG_EXTRA_FLAG_NID |
7252 CHANGELOG_EXTRA_FLAG_OMODE |
7253 CHANGELOG_EXTRA_FLAG_XATTR);
7256 "%s changelog: cannot set xflags for changelog: %s\n",
7257 progname, strerror(errno = -rc));
7261 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7265 if (endrec && rec->cr_index > endrec) {
7266 llapi_changelog_free(&rec);
7269 if (rec->cr_index < startrec) {
7270 llapi_changelog_free(&rec);
7274 secs = rec->cr_time >> 30;
7275 gmtime_r(&secs, &ts);
7276 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7277 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7278 changelog_type2str(rec->cr_type),
7279 ts.tm_hour, ts.tm_min, ts.tm_sec,
7280 (int)(rec->cr_time & ((1 << 30) - 1)),
7281 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7282 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7284 if (rec->cr_flags & CLF_JOBID) {
7285 struct changelog_ext_jobid *jid =
7286 changelog_rec_jobid(rec);
7288 if (jid->cr_jobid[0] != '\0')
7289 printf(" j=%s", jid->cr_jobid);
7292 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7293 struct changelog_ext_extra_flags *ef =
7294 changelog_rec_extra_flags(rec);
7296 printf(" ef=0x%llx", ef->cr_extra_flags);
7298 if (ef->cr_extra_flags & CLFE_UIDGID) {
7299 struct changelog_ext_uidgid *uidgid =
7300 changelog_rec_uidgid(rec);
7302 printf(" u=%llu:%llu",
7303 uidgid->cr_uid, uidgid->cr_gid);
7305 if (ef->cr_extra_flags & CLFE_NID) {
7306 struct changelog_ext_nid *nid =
7307 changelog_rec_nid(rec);
7310 libcfs_nid2str(nid->cr_nid));
7313 if (ef->cr_extra_flags & CLFE_OPEN) {
7314 struct changelog_ext_openmode *omd =
7315 changelog_rec_openmode(rec);
7316 char mode[] = "---";
7318 /* exec mode must be exclusive */
7319 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7322 if (omd->cr_openflags & MDS_FMODE_READ)
7324 if (omd->cr_openflags &
7331 if (strcmp(mode, "---") != 0)
7332 printf(" m=%s", mode);
7336 if (ef->cr_extra_flags & CLFE_XATTR) {
7337 struct changelog_ext_xattr *xattr =
7338 changelog_rec_xattr(rec);
7340 if (xattr->cr_xattr[0] != '\0')
7341 printf(" x=%s", xattr->cr_xattr);
7345 if (rec->cr_namelen)
7346 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7347 rec->cr_namelen, changelog_rec_name(rec));
7349 if (rec->cr_flags & CLF_RENAME) {
7350 struct changelog_ext_rename *rnm =
7351 changelog_rec_rename(rec);
7353 if (!fid_is_zero(&rnm->cr_sfid))
7354 printf(" s="DFID" sp="DFID" %.*s",
7355 PFID(&rnm->cr_sfid),
7356 PFID(&rnm->cr_spfid),
7357 (int)changelog_rec_snamelen(rec),
7358 changelog_rec_sname(rec));
7362 llapi_changelog_free(&rec);
7365 llapi_changelog_fini(&changelog_priv);
7368 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7369 progname, strerror(errno = -rc));
7371 return (rc == 1 ? 0 : rc);
7374 static int lfs_changelog_clear(int argc, char **argv)
7382 endrec = strtoll(argv[3], NULL, 10);
7384 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7387 fprintf(stderr, "%s: record out of range: %llu\n",
7389 else if (rc == -ENOENT)
7390 fprintf(stderr, "%s: no changelog user: %s\n",
7393 fprintf(stderr, "%s error: %s\n", argv[0],
7402 static int lfs_fid2path(int argc, char **argv)
7404 struct option long_opts[] = {
7405 { .val = 'c', .name = "cur", .has_arg = no_argument },
7406 { .val = 'l', .name = "link", .has_arg = required_argument },
7407 { .val = 'r', .name = "rec", .has_arg = required_argument },
7409 char short_opts[] = "cl:r:";
7410 char *device, *fid, *path;
7411 long long recno = -1;
7416 char *endptr = NULL;
7418 while ((rc = getopt_long(argc, argv, short_opts,
7419 long_opts, NULL)) != -1) {
7425 linkno = strtol(optarg, &endptr, 10);
7426 if (*endptr != '\0') {
7428 "%s fid2path: invalid linkno '%s'\n",
7434 recno = strtoll(optarg, &endptr, 10);
7435 if (*endptr != '\0') {
7437 "%s fid2path: invalid recno '%s'\n",
7444 "%s fid2path: unrecognized option '%s'\n",
7445 progname, argv[optind - 1]);
7452 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7457 device = argv[optind++];
7458 path = calloc(1, PATH_MAX);
7462 "%s fid2path: cannot allocate memory for path: %s\n",
7463 progname, strerror(-rc));
7468 while (optind < argc) {
7469 fid = argv[optind++];
7471 lnktmp = (linkno >= 0) ? linkno : 0;
7473 int oldtmp = lnktmp;
7474 long long rectmp = recno;
7476 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7480 "%s fid2path: cannot find '%s': %s\n",
7481 progname, fid, strerror(errno = -rc2));
7488 fprintf(stdout, "%lld ", rectmp);
7489 if (device[0] == '/') {
7490 fprintf(stdout, "%s", device);
7491 if (device[strlen(device) - 1] != '/')
7492 fprintf(stdout, "/");
7493 } else if (path[0] == '\0') {
7494 fprintf(stdout, "/");
7496 fprintf(stdout, "%s\n", path);
7499 /* specified linkno */
7501 if (oldtmp == lnktmp)
7511 static int lfs_path2fid(int argc, char **argv)
7513 struct option long_opts[] = {
7514 { .val = 'p', .name = "parents", .has_arg = no_argument },
7517 const char short_opts[] = "p";
7518 const char *sep = "";
7521 bool show_parents = false;
7523 while ((rc = getopt_long(argc, argv, short_opts,
7524 long_opts, NULL)) != -1) {
7527 show_parents = true;
7531 "%s path2fid: unrecognized option '%s'\n",
7532 progname, argv[optind - 1]);
7537 if (optind > argc - 1) {
7538 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7542 else if (optind < argc - 1)
7546 for (path = argv + optind; *path != NULL; path++) {
7548 if (!show_parents) {
7549 err = llapi_path2fid(*path, &fid);
7551 printf("%s%s"DFID"\n",
7552 *sep != '\0' ? *path : "", sep,
7555 char name[NAME_MAX + 1];
7556 unsigned int linkno = 0;
7558 while ((err = llapi_path2parent(*path, linkno, &fid,
7559 name, sizeof(name))) == 0) {
7560 if (*sep != '\0' && linkno == 0)
7561 printf("%s%s", *path, sep);
7563 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7568 /* err == -ENODATA is end-of-loop */
7569 if (linkno > 0 && err == -ENODATA) {
7577 "%s path2fid: cannot get %sfid for '%s': %s\n",
7578 progname, show_parents ? "parent " : "", *path,
7590 static int lfs_data_version(int argc, char **argv)
7597 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7600 fprintf(stderr, "%s data_version: FILE must be specified\n",
7605 while ((c = getopt(argc, argv, "nrw")) != -1) {
7608 data_version_flags = 0;
7611 data_version_flags |= LL_DV_RD_FLUSH;
7614 data_version_flags |= LL_DV_WR_FLUSH;
7618 "%s data_version: unrecognized option '%s'\n",
7619 progname, argv[optind - 1]);
7623 if (optind == argc) {
7624 fprintf(stderr, "%s data_version: FILE must be specified\n",
7629 path = argv[optind];
7630 fd = open(path, O_RDONLY);
7633 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7634 progname, path, strerror(-rc));
7638 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7641 "%s data_version: cannot get version for '%s': %s\n",
7642 progname, path, strerror(-rc));
7644 printf("%ju" "\n", (uintmax_t)data_version);
7650 static int lfs_hsm_state(int argc, char **argv)
7655 struct hsm_user_state hus;
7663 rc = llapi_hsm_state_get(path, &hus);
7665 fprintf(stderr, "can't get hsm state for %s: %s\n",
7666 path, strerror(errno = -rc));
7670 /* Display path name and status flags */
7671 printf("%s: (0x%08x)", path, hus.hus_states);
7673 if (hus.hus_states & HS_RELEASED)
7674 printf(" released");
7675 if (hus.hus_states & HS_EXISTS)
7677 if (hus.hus_states & HS_DIRTY)
7679 if (hus.hus_states & HS_ARCHIVED)
7680 printf(" archived");
7681 /* Display user-settable flags */
7682 if (hus.hus_states & HS_NORELEASE)
7683 printf(" never_release");
7684 if (hus.hus_states & HS_NOARCHIVE)
7685 printf(" never_archive");
7686 if (hus.hus_states & HS_LOST)
7687 printf(" lost_from_hsm");
7689 if (hus.hus_archive_id != 0)
7690 printf(", archive_id:%d", hus.hus_archive_id);
7693 } while (++i < argc);
7698 #define LFS_HSM_SET 0
7699 #define LFS_HSM_CLEAR 1
7702 * Generic function to set or clear HSM flags.
7703 * Used by hsm_set and hsm_clear.
7705 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7707 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7709 struct option long_opts[] = {
7710 { .val = 'A', .name = "archived", .has_arg = no_argument },
7711 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7712 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7713 { .val = 'e', .name = "exists", .has_arg = no_argument },
7714 { .val = 'l', .name = "lost", .has_arg = no_argument },
7715 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7716 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7718 char short_opts[] = "lraAdei:";
7722 __u32 archive_id = 0;
7728 while ((c = getopt_long(argc, argv, short_opts,
7729 long_opts, NULL)) != -1) {
7735 mask |= HS_NOARCHIVE;
7738 mask |= HS_ARCHIVED;
7741 mask |= HS_NORELEASE;
7750 archive_id = strtol(optarg, &end, 10);
7752 fprintf(stderr, "invalid archive_id: '%s'\n",
7760 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7761 argv[0], argv[optind - 1]);
7766 /* User should have specified a flag */
7770 while (optind < argc) {
7772 path = argv[optind];
7774 /* If mode == 0, this means we apply the mask. */
7775 if (mode == LFS_HSM_SET)
7776 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7778 rc = llapi_hsm_state_set(path, 0, mask, 0);
7781 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7782 path, strerror(errno = -rc));
7791 static int lfs_hsm_action(int argc, char **argv)
7796 struct hsm_current_action hca;
7797 struct hsm_extent he;
7798 enum hsm_user_action hua;
7799 enum hsm_progress_states hps;
7807 rc = llapi_hsm_current_action(path, &hca);
7809 fprintf(stderr, "can't get hsm action for %s: %s\n",
7810 path, strerror(errno = -rc));
7813 he = hca.hca_location;
7814 hua = hca.hca_action;
7815 hps = hca.hca_state;
7817 printf("%s: %s", path, hsm_user_action2name(hua));
7819 /* Skip file without action */
7820 if (hca.hca_action == HUA_NONE) {
7825 printf(" %s ", hsm_progress_state2name(hps));
7827 if ((hps == HPS_RUNNING) &&
7828 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7829 printf("(%llu bytes moved)\n",
7830 (unsigned long long)he.length);
7831 else if ((he.offset + he.length) == LUSTRE_EOF)
7832 printf("(from %llu to EOF)\n",
7833 (unsigned long long)he.offset);
7835 printf("(from %llu to %llu)\n",
7836 (unsigned long long)he.offset,
7837 (unsigned long long)(he.offset + he.length));
7839 } while (++i < argc);
7844 static int lfs_hsm_set(int argc, char **argv)
7846 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7849 static int lfs_hsm_clear(int argc, char **argv)
7851 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7855 * Check file state and return its fid, to be used by lfs_hsm_request().
7857 * \param[in] file Path to file to check
7858 * \param[in,out] fid Pointer to allocated lu_fid struct.
7859 * \param[in,out] last_dev Pointer to last device id used.
7861 * \return 0 on success.
7863 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7869 rc = lstat(file, &st);
7871 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7874 /* Checking for regular file as archiving as posix copytool
7875 * rejects archiving files other than regular files
7877 if (!S_ISREG(st.st_mode)) {
7878 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7881 /* A request should be ... */
7882 if (*last_dev != st.st_dev && *last_dev != 0) {
7883 fprintf(stderr, "All files should be "
7884 "on the same filesystem: %s\n", file);
7887 *last_dev = st.st_dev;
7889 rc = llapi_path2fid(file, fid);
7891 fprintf(stderr, "Cannot read FID of %s: %s\n",
7892 file, strerror(-rc));
7898 /* Fill an HSM HUR item with a given file name.
7900 * If mntpath is set, then the filename is actually a FID, and no
7901 * lookup on the filesystem will be performed.
7903 * \param[in] hur the user request to fill
7904 * \param[in] idx index of the item inside the HUR to fill
7905 * \param[in] mntpath mountpoint of Lustre
7906 * \param[in] fname filename (if mtnpath is NULL)
7907 * or FID (if mntpath is set)
7908 * \param[in] last_dev pointer to last device id used
7910 * \retval 0 on success
7911 * \retval CMD_HELP or a negative errno on error
7913 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7914 const char *mntpath, const char *fname,
7917 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7920 hui->hui_extent.length = -1;
7922 if (mntpath != NULL) {
7925 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7929 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7934 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7938 hur->hur_request.hr_itemcount++;
7943 static int lfs_hsm_request(int argc, char **argv, int action)
7945 struct option long_opts[] = {
7946 { .val = 'a', .name = "archive", .has_arg = required_argument },
7947 { .val = 'D', .name = "data", .has_arg = required_argument },
7948 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7949 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7952 char short_opts[] = "l:D:a:m:";
7953 struct hsm_user_request *hur, *oldhur;
7958 char *filelist = NULL;
7959 char fullpath[PATH_MAX];
7960 char *opaque = NULL;
7964 int nbfile_alloc = 0;
7965 char *some_file = NULL;
7966 char *mntpath = NULL;
7972 while ((c = getopt_long(argc, argv, short_opts,
7973 long_opts, NULL)) != -1) {
7982 if (action != HUA_ARCHIVE &&
7983 action != HUA_REMOVE) {
7985 "error: -a is supported only "
7986 "when archiving or removing\n");
7989 archive_id = atoi(optarg);
7992 if (some_file == NULL) {
7994 some_file = strdup(optarg);
8000 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8001 argv[0], argv[optind - 1]);
8006 /* All remaining args are files, so we have at least nbfile */
8007 nbfile = argc - optind;
8009 if ((nbfile == 0) && (filelist == NULL))
8013 opaque_len = strlen(opaque);
8015 /* Alloc the request structure with enough place to store all files
8016 * from command line. */
8017 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8019 fprintf(stderr, "Cannot create the request: %s\n",
8023 nbfile_alloc = nbfile;
8025 hur->hur_request.hr_action = action;
8026 hur->hur_request.hr_archive_id = archive_id;
8027 hur->hur_request.hr_flags = 0;
8029 /* All remaining args are files, add them */
8030 if (nbfile != 0 && some_file == NULL)
8031 some_file = strdup(argv[optind]);
8033 for (i = 0; i < nbfile; i++) {
8034 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8040 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8042 /* If a filelist was specified, read the filelist from it. */
8043 if (filelist != NULL) {
8044 fp = fopen(filelist, "r");
8046 fprintf(stderr, "Cannot read the file list %s: %s\n",
8047 filelist, strerror(errno));
8052 while ((rc = getline(&line, &len, fp)) != -1) {
8053 /* If allocated buffer was too small, get something
8055 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8058 nbfile_alloc = nbfile_alloc * 2 + 1;
8060 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8063 fprintf(stderr, "hsm: cannot allocate "
8064 "the request: %s\n",
8071 size = hur_len(oldhur);
8073 fprintf(stderr, "hsm: cannot allocate "
8074 "%u files + %u bytes data\n",
8075 oldhur->hur_request.hr_itemcount,
8076 oldhur->hur_request.hr_data_len);
8083 memcpy(hur, oldhur, size);
8088 if (line[strlen(line) - 1] == '\n')
8089 line[strlen(line) - 1] = '\0';
8091 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8092 mntpath, line, &last_dev);
8098 if (some_file == NULL) {
8108 /* If a --data was used, add it to the request */
8109 hur->hur_request.hr_data_len = opaque_len;
8111 memcpy(hur_data(hur), opaque, opaque_len);
8113 /* Send the HSM request */
8114 if (realpath(some_file, fullpath) == NULL) {
8115 fprintf(stderr, "Could not find path '%s': %s\n",
8116 some_file, strerror(errno));
8118 rc = llapi_hsm_request(fullpath, hur);
8120 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8121 some_file, strerror(-rc));
8131 static int lfs_hsm_archive(int argc, char **argv)
8133 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8136 static int lfs_hsm_restore(int argc, char **argv)
8138 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8141 static int lfs_hsm_release(int argc, char **argv)
8143 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8146 static int lfs_hsm_remove(int argc, char **argv)
8148 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8151 static int lfs_hsm_cancel(int argc, char **argv)
8153 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8156 static int lfs_swap_layouts(int argc, char **argv)
8161 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8162 SWAP_LAYOUTS_KEEP_MTIME |
8163 SWAP_LAYOUTS_KEEP_ATIME);
8166 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8168 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8170 int lfs_get_mode(const char *string)
8172 enum lock_mode_user mode;
8174 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8175 if (lock_mode_names[mode] == NULL)
8177 if (strcmp(string, lock_mode_names[mode]) == 0)
8184 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8186 enum lu_ladvise_type advice;
8189 advice < ARRAY_SIZE(ladvise_names); advice++) {
8190 if (ladvise_names[advice] == NULL)
8192 if (strcmp(string, ladvise_names[advice]) == 0)
8196 return LU_LADVISE_INVALID;
8199 static int lfs_ladvise(int argc, char **argv)
8201 struct option long_opts[] = {
8202 { .val = 'a', .name = "advice", .has_arg = required_argument },
8203 { .val = 'b', .name = "background", .has_arg = no_argument },
8204 { .val = 'e', .name = "end", .has_arg = required_argument },
8205 { .val = 'l', .name = "length", .has_arg = required_argument },
8206 { .val = 'm', .name = "mode", .has_arg = required_argument },
8207 { .val = 's', .name = "start", .has_arg = required_argument },
8208 { .val = 'u', .name = "unset", .has_arg = no_argument },
8210 char short_opts[] = "a:be:l:m:s:u";
8215 struct llapi_lu_ladvise advice;
8216 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8217 unsigned long long start = 0;
8218 unsigned long long end = LUSTRE_EOF;
8219 unsigned long long length = 0;
8220 unsigned long long size_units;
8221 unsigned long long flags = 0;
8225 while ((c = getopt_long(argc, argv, short_opts,
8226 long_opts, NULL)) != -1) {
8229 advice_type = lfs_get_ladvice(optarg);
8230 if (advice_type == LU_LADVISE_INVALID) {
8231 fprintf(stderr, "%s: invalid advice type "
8232 "'%s'\n", argv[0], optarg);
8233 fprintf(stderr, "Valid types:");
8235 for (advice_type = 0;
8236 advice_type < ARRAY_SIZE(ladvise_names);
8238 if (ladvise_names[advice_type] == NULL)
8240 fprintf(stderr, " %s",
8241 ladvise_names[advice_type]);
8243 fprintf(stderr, "\n");
8256 rc = llapi_parse_size(optarg, &end,
8259 fprintf(stderr, "%s: bad end offset '%s'\n",
8266 rc = llapi_parse_size(optarg, &start,
8269 fprintf(stderr, "%s: bad start offset "
8270 "'%s'\n", argv[0], optarg);
8276 rc = llapi_parse_size(optarg, &length,
8279 fprintf(stderr, "%s: bad length '%s'\n",
8285 mode = lfs_get_mode(optarg);
8287 fprintf(stderr, "%s: bad mode '%s', valid "
8288 "modes are READ or WRITE\n",
8296 fprintf(stderr, "%s: option '%s' unrecognized\n",
8297 argv[0], argv[optind - 1]);
8302 if (advice_type == LU_LADVISE_INVALID) {
8303 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8304 fprintf(stderr, "Valid types:");
8305 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8307 if (ladvise_names[advice_type] == NULL)
8309 fprintf(stderr, " %s", ladvise_names[advice_type]);
8311 fprintf(stderr, "\n");
8315 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8316 fprintf(stderr, "%s: Lock no expand advice is a per file "
8317 "descriptor advice, so when called from lfs, "
8318 "it does nothing.\n", argv[0]);
8322 if (argc <= optind) {
8323 fprintf(stderr, "%s: please give one or more file names\n",
8328 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8329 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8334 if (end == LUSTRE_EOF && length != 0)
8335 end = start + length;
8338 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8339 argv[0], start, end);
8343 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8344 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8349 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8350 fprintf(stderr, "%s: mode is required with lockahead\n",
8355 while (optind < argc) {
8358 path = argv[optind++];
8360 fd = open(path, O_RDONLY);
8362 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8363 argv[0], path, strerror(errno));
8368 advice.lla_start = start;
8369 advice.lla_end = end;
8370 advice.lla_advice = advice_type;
8371 advice.lla_value1 = 0;
8372 advice.lla_value2 = 0;
8373 advice.lla_value3 = 0;
8374 advice.lla_value4 = 0;
8375 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8376 advice.lla_lockahead_mode = mode;
8377 advice.lla_peradvice_flags = flags;
8380 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8383 fprintf(stderr, "%s: cannot give advice '%s' to file "
8384 "'%s': %s\n", argv[0],
8385 ladvise_names[advice_type],
8386 path, strerror(errno));
8392 if (rc == 0 && rc2 < 0)
8398 static const char *const heat_names[] = LU_HEAT_NAMES;
8400 static int lfs_heat_get(int argc, char **argv)
8402 struct lu_heat *heat;
8411 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8413 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8418 while (optind < argc) {
8419 path = argv[optind++];
8421 fd = open(path, O_RDONLY);
8423 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8424 argv[0], path, strerror(errno));
8429 heat->lh_count = OBD_HEAT_COUNT;
8430 rc2 = llapi_heat_get(fd, heat);
8433 fprintf(stderr, "%s: cannot get heat of file '%s'"
8434 ": %s\n", argv[0], path, strerror(errno));
8438 printf("flags: %x\n", heat->lh_flags);
8439 for (i = 0; i < heat->lh_count; i++)
8440 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8442 if (rc == 0 && rc2 < 0)
8450 static int lfs_heat_set(int argc, char **argv)
8452 struct option long_opts[] = {
8453 { .val = 'c', .name = "clear", .has_arg = no_argument },
8454 { .val = 'o', .name = "off", .has_arg = no_argument },
8455 { .val = 'O', .name = "on", .has_arg = no_argument },
8457 enum lu_heat_flag flags = 0;
8467 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8470 flags |= LU_HEAT_FLAG_CLEAR;
8473 flags |= LU_HEAT_FLAG_CLEAR;
8474 flags |= LU_HEAT_FLAG_OFF;
8477 flags &= ~LU_HEAT_FLAG_OFF;
8482 fprintf(stderr, "%s: option '%s' unrecognized\n",
8483 argv[0], argv[optind - 1]);
8488 if (argc <= optind) {
8489 fprintf(stderr, "%s: please give one or more file names\n",
8494 while (optind < argc) {
8495 path = argv[optind++];
8497 fd = open(path, O_RDONLY);
8499 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8500 argv[0], path, strerror(errno));
8505 rc2 = llapi_heat_set(fd, flags);
8508 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8509 ": %s\n", argv[0], path, strerror(errno));
8513 if (rc == 0 && rc2 < 0)
8519 /** The input string contains a comma delimited list of component ids and
8520 * ranges, for example "1,2-4,7".
8522 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8524 bool end_of_loop = false;
8532 while (!end_of_loop) {
8536 char *endptr = NULL;
8539 ptr = strchrnul(arg, ',');
8540 end_of_loop = *ptr == '\0';
8543 start_index = strtol(arg, &endptr, 0);
8544 if (endptr == arg) /* no data at all */
8546 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8548 if (start_index < 0)
8551 end_index = start_index;
8552 if (*endptr == '-') {
8553 end_index = strtol(endptr + 1, &endptr, 0);
8554 if (*endptr != '\0')
8556 if (end_index < start_index)
8560 for (i = start_index; i <= end_index && size > 0; i++) {
8563 /* remove duplicate */
8564 for (j = 0; j < nr; j++) {
8568 if (j == nr) { /* no duplicate */
8574 if (size == 0 && i < end_index)
8581 if (!end_of_loop && ptr != NULL)
8584 return rc < 0 ? rc : nr;
8588 * struct verify_mirror_id - Mirror id to be verified.
8589 * @mirror_id: A specified mirror id.
8590 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8592 struct verify_mirror_id {
8598 * compare_mirror_ids() - Compare mirror ids.
8599 * @layout: Mirror component list.
8600 * @cbdata: Callback data in verify_mirror_id structure.
8602 * This is a callback function called by llapi_layout_comp_iterate()
8603 * to compare the specified mirror id with the one in the current
8604 * component of @layout. If they are the same, then the specified
8605 * mirror id is valid.
8607 * Return: a negative error code on failure or
8608 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8609 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8612 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8614 struct verify_mirror_id *mirror_id_cbdata =
8615 (struct verify_mirror_id *)cbdata;
8619 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8623 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8624 progname, strerror(errno));
8628 if (mirror_id_cbdata->mirror_id == mirror_id) {
8629 mirror_id_cbdata->is_valid_id = true;
8630 return LLAPI_LAYOUT_ITER_STOP;
8633 return LLAPI_LAYOUT_ITER_CONT;
8637 * verify_mirror_ids() - Verify specified mirror ids.
8638 * @fname: Mirrored file name.
8639 * @mirror_ids: Specified mirror ids to be verified.
8640 * @ids_nr: Number of specified mirror ids.
8642 * This function verifies that specified @mirror_ids are valid
8643 * in the mirrored file @fname.
8645 * Return: 0 on success or a negative error code on failure.
8648 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8650 struct llapi_layout *layout = NULL;
8651 struct verify_mirror_id mirror_id_cbdata = { 0 };
8662 if (stat(fname, &stbuf) < 0) {
8663 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8664 progname, fname, strerror(errno));
8669 if (!S_ISREG(stbuf.st_mode)) {
8670 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8676 fd = open(fname, O_DIRECT | O_RDONLY);
8678 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8679 progname, fname, strerror(errno));
8684 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8686 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8687 progname, fname, strerror(errno));
8691 layout = llapi_layout_get_by_fd(fd, 0);
8692 if (layout == NULL) {
8693 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8694 progname, fname, strerror(errno));
8696 llapi_lease_release(fd);
8700 rc = llapi_layout_flags_get(layout, &flr_state);
8702 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8703 progname, fname, strerror(errno));
8708 flr_state &= LCM_FL_FLR_MASK;
8709 switch (flr_state) {
8712 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8713 progname, fname, llapi_layout_flags_string(flr_state));
8720 for (i = 0; i < ids_nr; i++) {
8721 mirror_id_cbdata.mirror_id = mirror_ids[i];
8722 mirror_id_cbdata.is_valid_id = false;
8724 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8729 "%s: '%s' failed to verify mirror id: %u.\n",
8730 progname, fname, mirror_ids[i]);
8734 if (!mirror_id_cbdata.is_valid_id) {
8737 "%s: '%s' invalid specified mirror id: %u.\n",
8738 progname, fname, mirror_ids[i]);
8744 llapi_layout_free(layout);
8745 llapi_lease_release(fd);
8753 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8754 __u16 *mirror_ids, int ids_nr)
8756 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8757 struct llapi_layout *layout;
8767 if (stat(fname, &stbuf) < 0) {
8768 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8769 progname, fname, strerror(errno));
8773 if (!S_ISREG(stbuf.st_mode)) {
8774 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8780 fd = open(fname, O_DIRECT | O_RDWR);
8782 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8783 progname, fname, strerror(errno));
8788 layout = llapi_layout_get_by_fd(fd, 0);
8789 if (layout == NULL) {
8790 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8791 progname, fname, strerror(errno));
8796 rc = llapi_layout_flags_get(layout, &flr_state);
8798 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8799 progname, fname, strerror(errno));
8804 flr_state &= LCM_FL_FLR_MASK;
8805 if (flr_state == LCM_FL_NONE) {
8807 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8812 /* get stale component info */
8813 comp_size = llapi_mirror_find_stale(layout, comp_array,
8814 ARRAY_SIZE(comp_array),
8815 mirror_ids, ids_nr);
8816 if (comp_size <= 0) {
8821 ioc->lil_mode = LL_LEASE_WRLCK;
8822 ioc->lil_flags = LL_LEASE_RESYNC;
8823 rc = llapi_lease_set(fd, ioc);
8825 if (rc == -EALREADY)
8829 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8830 progname, fname, strerror(errno));
8834 /* get the read range [start, end) */
8835 start = comp_array[0].lrc_start;
8836 end = comp_array[0].lrc_end;
8837 for (idx = 1; idx < comp_size; idx++) {
8838 if (comp_array[idx].lrc_start < start)
8839 start = comp_array[idx].lrc_start;
8840 if (end < comp_array[idx].lrc_end)
8841 end = comp_array[idx].lrc_end;
8844 rc = llapi_lease_check(fd);
8845 if (rc != LL_LEASE_WRLCK) {
8846 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8851 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8854 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8855 progname, fname, rc);
8857 /* prepare ioc for lease put */
8858 ioc->lil_mode = LL_LEASE_UNLCK;
8859 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8861 for (idx = 0; idx < comp_size; idx++) {
8862 if (comp_array[idx].lrc_synced) {
8863 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8868 rc = llapi_lease_set(fd, ioc);
8870 if (rc == 0) /* lost lease lock */
8872 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8873 progname, fname, strerror(errno));
8877 * llapi_lease_set returns lease mode when it request to unlock
8883 llapi_layout_free(layout);
8890 static inline int lfs_mirror_resync(int argc, char **argv)
8892 struct ll_ioc_lease *ioc = NULL;
8893 __u16 mirror_ids[128] = { 0 };
8898 struct option long_opts[] = {
8899 { .val = 'o', .name = "only", .has_arg = required_argument },
8902 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8905 rc = parse_mirror_ids(mirror_ids,
8906 sizeof(mirror_ids) / sizeof(__u16),
8910 "%s: bad mirror ids '%s'.\n",
8917 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8918 argv[0], argv[optind - 1]);
8924 if (argc == optind) {
8925 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8930 if (ids_nr > 0 && argc > optind + 1) {
8932 "%s: option '--only' cannot be used upon multiple files.\n",
8940 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8945 /* set the lease on the file */
8946 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8948 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8949 argv[0], strerror(errno));
8954 for (; optind < argc; optind++) {
8955 rc = lfs_mirror_resync_file(argv[optind], ioc,
8956 mirror_ids, ids_nr);
8957 /* ignore previous file's error, continue with next file */
8960 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8968 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8970 struct llapi_layout *layout;
8973 layout = llapi_layout_get_by_fd(fd, 0);
8974 if (layout == NULL) {
8975 fprintf(stderr, "could not get layout.\n");
8979 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8981 fprintf(stderr, "failed to iterate layout\n");
8982 llapi_layout_free(layout);
8985 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8986 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8987 llapi_layout_free(layout);
8991 llapi_layout_free(layout);
8997 * Check whether two files are the same file
8998 * \retval 0 same file
8999 * \retval 1 not the same file
9000 * \retval <0 error code
9002 static inline int check_same_file(const char *f1, const char *f2)
9007 if (stat(f1, &stbuf1) < 0) {
9008 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9009 progname, f1, strerror(errno));
9013 if (stat(f2, &stbuf2) < 0) {
9014 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9015 progname, f2, strerror(errno));
9019 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9020 stbuf1.st_ino == stbuf2.st_ino)
9026 static inline int lfs_mirror_read(int argc, char **argv)
9029 __u16 mirror_id = 0;
9030 const char *outfile = NULL;
9036 const size_t buflen = 4 << 20;
9038 struct option long_opts[] = {
9039 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9040 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9043 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9048 mirror_id = strtoul(optarg, &end, 0);
9049 if (*end != '\0' || mirror_id == 0) {
9051 "%s %s: invalid mirror ID '%s'\n",
9052 progname, argv[0], optarg);
9060 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9061 progname, argv[optind - 1]);
9066 if (argc == optind) {
9067 fprintf(stderr, "%s %s: no mirrored file provided\n",
9070 } else if (argc > optind + 1) {
9071 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9075 if (mirror_id == 0) {
9076 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9081 /* open mirror file */
9082 fname = argv[optind];
9085 rc = check_same_file(fname, outfile);
9088 "%s %s: output file cannot be the mirrored file\n",
9096 fd = open(fname, O_DIRECT | O_RDONLY);
9098 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9099 progname, argv[0], fname, strerror(errno));
9103 /* verify mirror id */
9104 rc = verify_mirror_id_by_fd(fd, mirror_id);
9107 "%s %s: cannot find mirror with ID %u in '%s'\n",
9108 progname, argv[0], mirror_id, fname);
9112 /* open output file */
9114 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9116 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9117 progname, argv[0], outfile, strerror(errno));
9122 outfd = STDOUT_FILENO;
9125 /* allocate buffer */
9126 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9128 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9129 progname, argv[0], rc);
9136 ssize_t written = 0;
9138 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9139 if (bytes_read < 0) {
9142 "%s %s: fail to read data from mirror %u: %s\n",
9143 progname, argv[0], mirror_id, strerror(-rc));
9148 if (bytes_read == 0)
9151 while (written < bytes_read) {
9154 written2 = write(outfd, buf + written,
9155 bytes_read - written);
9158 "%s %s: fail to write %s: %s\n",
9159 progname, argv[0], outfile ? : "STDOUT",
9164 written += written2;
9167 if (written != bytes_read) {
9169 "%s %s: written %ld bytes does not match with %ld read.\n",
9170 progname, argv[0], written, bytes_read);
9192 static inline int lfs_mirror_write(int argc, char **argv)
9195 __u16 mirror_id = 0;
9196 const char *inputfile = NULL;
9202 const size_t buflen = 4 << 20;
9204 size_t page_size = sysconf(_SC_PAGESIZE);
9205 struct ll_ioc_lease_id ioc;
9207 struct option long_opts[] = {
9208 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9209 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9212 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9217 mirror_id = strtoul(optarg, &end, 0);
9218 if (*end != '\0' || mirror_id == 0) {
9220 "%s %s: invalid mirror ID '%s'\n",
9221 progname, argv[0], optarg);
9229 fprintf(stderr, "%s: option '%s' unrecognized\n",
9230 progname, argv[optind - 1]);
9235 if (argc == optind) {
9236 fprintf(stderr, "%s %s: no mirrored file provided\n",
9239 } else if (argc > optind + 1) {
9240 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9244 if (mirror_id == 0) {
9245 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9250 /* open mirror file */
9251 fname = argv[optind];
9254 rc = check_same_file(fname, inputfile);
9257 "%s %s: input file cannot be the mirrored file\n",
9265 fd = open(fname, O_DIRECT | O_WRONLY);
9267 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9268 progname, argv[0], fname, strerror(errno));
9272 /* verify mirror id */
9273 rc = verify_mirror_id_by_fd(fd, mirror_id);
9276 "%s %s: cannot find mirror with ID %u in '%s'\n",
9277 progname, argv[0], mirror_id, fname);
9281 /* open input file */
9283 inputfd = open(inputfile, O_RDONLY, 0644);
9285 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9286 progname, argv[0], inputfile, strerror(errno));
9291 inputfd = STDIN_FILENO;
9294 /* allocate buffer */
9295 rc = posix_memalign(&buf, page_size, buflen);
9297 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9298 progname, argv[0], rc);
9302 /* prepare target mirror components instantiation */
9303 ioc.lil_mode = LL_LEASE_WRLCK;
9304 ioc.lil_flags = LL_LEASE_RESYNC;
9305 ioc.lil_mirror_id = mirror_id;
9306 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9309 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9310 progname, argv[0], fname, strerror(errno));
9320 rc = llapi_lease_check(fd);
9321 if (rc != LL_LEASE_WRLCK) {
9322 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9323 progname, argv[0], fname);
9327 bytes_read = read(inputfd, buf, buflen);
9328 if (bytes_read < 0) {
9331 "%s %s: fail to read data from '%s': %s\n",
9332 progname, argv[0], inputfile ? : "STDIN",
9339 if (bytes_read == 0)
9342 /* round up to page align to make direct IO happy. */
9343 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9345 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9350 "%s %s: fail to write to mirror %u: %s\n",
9351 progname, argv[0], mirror_id,
9359 if (pos & (page_size - 1)) {
9360 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9365 ioc.lil_mode = LL_LEASE_UNLCK;
9366 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9368 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9373 "%s %s: release lease lock of '%s' failed: %s\n",
9374 progname, argv[0], fname, strerror(errno));
9391 struct collect_ids_data {
9397 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9399 struct collect_ids_data *cid = cbdata;
9403 rc = llapi_layout_mirror_id_get(layout, &id);
9407 if ((__u16)id != cid->cid_exclude) {
9410 for (i = 0; i < cid->cid_count; i++) {
9411 /* already collected the mirror id */
9412 if (id == cid->cid_ids[i])
9413 return LLAPI_LAYOUT_ITER_CONT;
9415 cid->cid_ids[cid->cid_count] = id;
9419 return LLAPI_LAYOUT_ITER_CONT;
9422 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9424 struct llapi_layout *layout;
9425 struct collect_ids_data cid = { .cid_ids = ids,
9427 .cid_exclude = exclude_id, };
9430 layout = llapi_layout_get_by_fd(fd, 0);
9431 if (layout == NULL) {
9432 fprintf(stderr, "could not get layout\n");
9436 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9438 fprintf(stderr, "failed to iterate layout\n");
9439 llapi_layout_free(layout);
9443 llapi_layout_free(layout);
9445 return cid.cid_count;
9448 static inline int lfs_mirror_copy(int argc, char **argv)
9451 __u16 read_mirror_id = 0;
9452 __u16 ids[128] = { 0 };
9454 struct llapi_layout *layout = NULL;
9455 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9462 struct ll_ioc_lease *ioc = NULL;
9463 struct ll_ioc_lease_id *resync_ioc;
9465 struct option long_opts[] = {
9466 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9467 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9470 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9475 read_mirror_id = strtoul(optarg, &end, 0);
9476 if (*end != '\0' || read_mirror_id == 0) {
9478 "%s %s: invalid read mirror ID '%s'\n",
9479 progname, argv[0], optarg);
9484 if (!strcmp(optarg, "-1")) {
9485 /* specify all other mirrors */
9489 count = parse_mirror_ids((__u16 *)ids,
9497 fprintf(stderr, "%s: option '%s' unrecognized\n",
9498 progname, argv[optind - 1]);
9503 if (argc == optind) {
9504 fprintf(stderr, "%s %s: no mirrored file provided\n",
9507 } else if (argc > optind + 1) {
9508 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9512 if (read_mirror_id == 0) {
9514 "%s %s: no valid read mirror ID %d is provided\n",
9515 progname, argv[0], read_mirror_id);
9521 "%s %s: no write mirror ID is provided\n",
9526 for (i = 0; i < count; i++) {
9527 if (read_mirror_id == ids[i]) {
9529 "%s %s: read and write mirror ID cannot be the same\n",
9535 /* open mirror file */
9536 fname = argv[optind];
9538 fd = open(fname, O_DIRECT | O_RDWR);
9540 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9541 progname, argv[0], fname, strerror(errno));
9545 /* write to all other mirrors */
9546 if (ids[0] == (__u16)-1) {
9547 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9551 "%s %s: failed to get other mirror ids in '%s': %d\n",
9552 progname, argv[0], fname, rc);
9557 /* verify mirror id */
9558 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9561 "%s %s: cannot find mirror with ID %u in '%s'\n",
9562 progname, argv[0], read_mirror_id, fname);
9566 for (i = 0; i < count; i++) {
9567 rc = verify_mirror_id_by_fd(fd, ids[i]);
9570 "%s %s: cannot find mirror with ID %u in '%s'\n",
9571 progname, argv[0], ids[i], fname);
9576 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9579 "%s %s: cannot alloc comp id array for ioc: %s\n",
9580 progname, argv[0], strerror(errno));
9585 /* get stale component info */
9586 layout = llapi_layout_get_by_fd(fd, 0);
9587 if (layout == NULL) {
9588 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9589 progname, argv[0], fname, strerror(errno));
9593 comp_size = llapi_mirror_find_stale(layout, comp_array,
9594 ARRAY_SIZE(comp_array),
9596 llapi_layout_free(layout);
9597 if (comp_size < 0) {
9602 /* prepare target mirror components instantiation */
9603 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9604 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9605 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9607 resync_ioc->lil_mirror_id = ids[0];
9609 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9610 rc = llapi_lease_set(fd, ioc);
9613 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9614 progname, argv[0], fname, strerror(errno));
9618 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9621 fprintf(stderr, "%s %s: copy error: %d\n",
9622 progname, argv[0], rc);
9626 fprintf(stdout, "mirror copied successfully: ");
9627 for (i = 0; i < copied; i++)
9628 fprintf(stdout, "%d ", ids[i]);
9629 fprintf(stdout, "\n");
9631 ioc->lil_mode = LL_LEASE_UNLCK;
9632 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9634 for (i = 0; i < comp_size; i++) {
9637 for (j = 0; j < copied; j++) {
9638 if (comp_array[i].lrc_mirror_id != ids[j])
9641 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9645 rc = llapi_lease_set(fd, ioc);
9650 "%s %s: release lease lock of '%s' failed: %s\n",
9651 progname, argv[0], fname, strerror(errno));
9665 * struct verify_chunk - Mirror chunk to be verified.
9666 * @chunk: [start, end) of the chunk.
9667 * @mirror_count: Number of mirror ids in @mirror_id array.
9668 * @mirror_id: Array of valid mirror ids that cover the chunk.
9670 struct verify_chunk {
9671 struct lu_extent chunk;
9672 unsigned int mirror_count;
9673 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9677 * print_chunks() - Print chunk information.
9678 * @fname: Mirrored file name.
9679 * @chunks: Array of chunks.
9680 * @chunk_count: Number of chunks in @chunks array.
9682 * This function prints [start, end) of each chunk in @chunks
9683 * for mirrored file @fname, and also prints the valid mirror ids
9684 * that cover the chunk.
9689 void print_chunks(const char *fname, struct verify_chunk *chunks,
9695 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9696 for (i = 0; i < chunk_count; i++) {
9697 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9699 if (chunks[i].mirror_count == 0)
9700 fprintf(stdout, "\t[");
9702 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9703 for (j = 1; j < chunks[i].mirror_count; j++)
9704 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9706 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9708 fprintf(stdout, "\n");
9712 * print_checksums() - Print CRC-32 checksum values.
9713 * @chunk: A chunk and its corresponding valid mirror ids.
9714 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9716 * This function prints CRC-32 checksum values on @chunk for
9717 * each valid mirror that covers it.
9722 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9727 "CRC-32 checksum value for chunk "DEXT":\n",
9728 PEXT(&chunk->chunk));
9729 for (i = 0; i < chunk->mirror_count; i++)
9730 fprintf(stdout, "Mirror %u:\t%#lx\n",
9731 chunk->mirror_id[i], crc[i]);
9732 fprintf(stdout, "\n");
9736 * filter_mirror_id() - Filter specified mirror ids.
9737 * @chunks: Array of chunks.
9738 * @chunk_count: Number of chunks in @chunks array.
9739 * @mirror_ids: Specified mirror ids to be verified.
9740 * @ids_nr: Number of specified mirror ids.
9742 * This function scans valid mirror ids that cover each chunk in @chunks
9743 * and filters specified mirror ids.
9748 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9749 __u16 *mirror_ids, int ids_nr)
9754 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9755 unsigned int valid_count = 0;
9757 for (i = 0; i < chunk_count; i++) {
9758 if (chunks[i].mirror_count == 0)
9762 for (j = 0; j < ids_nr; j++) {
9763 for (k = 0; k < chunks[i].mirror_count; k++) {
9764 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9765 valid_id[valid_count] = mirror_ids[j];
9772 memcpy(chunks[i].mirror_id, valid_id,
9773 sizeof(__u16) * valid_count);
9774 chunks[i].mirror_count = valid_count;
9779 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9780 * @layout: Mirror component list.
9781 * @chunks: Array of chunks.
9782 * @chunks_size: Array size of @chunks.
9784 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9785 * to find out chunk segments and store them in @chunks array.
9787 * The @mirror_id array in each element of @chunks will store the valid
9788 * mirror ids that cover the chunk. If a mirror component covering the
9789 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9790 * will not be stored into the @mirror_id array, and the chunk for that
9791 * mirror will not be verified.
9793 * The @mirror_count in each element of @chunks will store the number of
9794 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9795 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9796 * indicates the chunk is valid in only one mirror. In both cases, the
9797 * chunk will not be verified.
9799 * Here is an example:
9802 * +------+-------------+--------------------+
9804 * +------+------+------+------+-------------+
9805 * | | S | S | | mirror2
9806 * +-------------+------+------+-------------+
9808 * prepared @chunks array will contain 5 elements:
9809 * (([0, 1M), [1, 2], 2),
9810 * ([1M, 2M), [1, 2], 2),
9811 * ([2M, 3M), [1], 1),
9812 * ([3M, 4M], [], 0),
9813 * ([4M, EOF), [2], 1))
9815 * Return: the actual array size of @chunks on success
9816 * or a negative error code on failure.
9819 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9820 struct verify_chunk *chunks,
9831 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9834 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9837 "%s: move to the first layout component: %s.\n",
9838 progname, strerror(errno));
9844 chunks[idx].chunk.e_end = LUSTRE_EOF;
9846 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9849 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9850 progname, strerror(errno));
9854 if (start > chunks[idx].chunk.e_start ||
9855 end <= chunks[idx].chunk.e_start)
9858 if (end < chunks[idx].chunk.e_end)
9859 chunks[idx].chunk.e_end = end;
9861 rc = llapi_layout_comp_flags_get(layout, &flags);
9864 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9865 progname, strerror(errno));
9869 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9872 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9875 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9876 progname, strerror(errno));
9880 chunks[idx].mirror_id[i] = mirror_id;
9882 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9884 "%s: mirror_id array is too small.\n",
9891 rc = llapi_layout_comp_use(layout,
9892 LLAPI_LAYOUT_COMP_USE_NEXT);
9895 "%s: move to the next layout component: %s.\n",
9896 progname, strerror(errno));
9899 } /* loop through all components */
9901 chunks[idx].mirror_count = i;
9903 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9907 if (idx >= chunks_size) {
9908 fprintf(stderr, "%s: chunks array is too small.\n",
9914 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9918 return rc < 0 ? rc : idx + 1;
9922 * lfs_mirror_verify_chunk() - Verify a chunk.
9923 * @fd: File descriptor of the mirrored file.
9924 * @file_size: Size of the mirrored file.
9925 * @chunk: A chunk and its corresponding valid mirror ids.
9926 * @verbose: Verbose mode.
9928 * This function verifies a @chunk contains exactly the same data
9929 * ammong the mirrors that cover it.
9931 * If @verbose is specified, then the function will print where the
9932 * differences are if the data do not match. Otherwise, it will
9933 * just return an error in that case.
9935 * Return: 0 on success or a negative error code on failure.
9938 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9939 struct verify_chunk *chunk, int verbose)
9941 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9943 size_t page_size = sysconf(_SC_PAGESIZE);
9949 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9956 rc = posix_memalign(&buf, page_size, buflen);
9957 if (rc) /* error code is returned directly */
9961 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9962 PEXT(&chunk->chunk));
9963 for (i = 0; i < chunk->mirror_count; i++)
9964 fprintf(stdout, " %u", chunk->mirror_id[i]);
9965 fprintf(stdout, "\n");
9969 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9970 pos = chunk->chunk.e_start;
9971 while (bytes_done < count) {
9972 /* compute initial CRC-32 checksum */
9973 crc = crc32(0L, Z_NULL, 0);
9974 memset(crc_array, 0, sizeof(crc_array));
9977 for (i = 0; i < chunk->mirror_count; i++) {
9978 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9980 if (bytes_read < 0) {
9983 "%s: failed to read data from mirror %u: %s.\n",
9984 progname, chunk->mirror_id[i],
9989 /* compute new CRC-32 checksum */
9990 crc_array[i] = crc32(crc, buf, bytes_read);
9994 print_checksums(chunk, crc_array);
9996 /* compare CRC-32 checksum values */
9997 for (i = 1; i < chunk->mirror_count; i++) {
9998 if (crc_array[i] != crc_array[0]) {
10002 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10003 progname, PEXT(&chunk->chunk),
10004 chunk->mirror_id[0],
10005 chunk->mirror_id[i]);
10010 bytes_done += bytes_read;
10013 if (verbose > 1 && rc == 0) {
10014 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10015 PEXT(&chunk->chunk));
10016 for (i = 0; i < chunk->mirror_count; i++)
10017 fprintf(stdout, " %u", chunk->mirror_id[i]);
10018 fprintf(stdout, " PASS\n\n");
10027 * lfs_mirror_verify_file() - Verify a mirrored file.
10028 * @fname: Mirrored file name.
10029 * @mirror_ids: Specified mirror ids to be verified.
10030 * @ids_nr: Number of specified mirror ids.
10031 * @verbose: Verbose mode.
10033 * This function verifies that each SYNC mirror of a mirrored file
10034 * specified by @fname contains exactly the same data.
10036 * If @mirror_ids is specified, then the function will verify the
10037 * mirrors specified by @mirror_ids contain exactly the same data.
10039 * If @verbose is specified, then the function will print where the
10040 * differences are if the data do not match. Otherwise, it will
10041 * just return an error in that case.
10043 * Return: 0 on success or a negative error code on failure.
10046 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10049 struct verify_chunk chunks_array[1024] = { };
10050 struct llapi_layout *layout = NULL;
10052 uint32_t flr_state;
10054 int chunk_count = 0;
10060 if (stat(fname, &stbuf) < 0) {
10061 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10062 progname, fname, strerror(errno));
10067 if (!S_ISREG(stbuf.st_mode)) {
10068 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10074 if (stbuf.st_size == 0) {
10076 fprintf(stdout, "%s: '%s' file size is 0.\n",
10082 fd = open(fname, O_DIRECT | O_RDONLY);
10084 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10085 progname, fname, strerror(errno));
10090 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10092 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10093 progname, fname, strerror(errno));
10097 layout = llapi_layout_get_by_fd(fd, 0);
10098 if (layout == NULL) {
10099 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10100 progname, fname, strerror(errno));
10102 llapi_lease_release(fd);
10106 rc = llapi_layout_flags_get(layout, &flr_state);
10108 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10109 progname, fname, strerror(errno));
10114 flr_state &= LCM_FL_FLR_MASK;
10115 switch (flr_state) {
10118 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10119 progname, fname, llapi_layout_flags_string(flr_state));
10125 /* find out mirror chunks to be verified */
10126 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10127 ARRAY_SIZE(chunks_array));
10128 if (chunk_count < 0) {
10134 /* filter specified mirror ids */
10135 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10138 print_chunks(fname, chunks_array, chunk_count);
10140 for (idx = 0; idx < chunk_count; idx++) {
10141 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10144 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10146 PEXT(&chunks_array[idx].chunk),
10147 (unsigned long long)stbuf.st_size);
10151 if (chunks_array[idx].mirror_count == 0) {
10153 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10155 PEXT(&chunks_array[idx].chunk));
10157 fprintf(stderr, "skipped\n");
10161 fprintf(stderr, "failed\n");
10165 if (chunks_array[idx].mirror_count == 1) {
10168 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10170 PEXT(&chunks_array[idx].chunk),
10171 chunks_array[idx].mirror_id[0]);
10175 rc = llapi_lease_check(fd);
10176 if (rc != LL_LEASE_RDLCK) {
10177 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10182 /* verify one chunk */
10183 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10184 &chunks_array[idx], verbose);
10198 llapi_layout_free(layout);
10199 llapi_lease_release(fd);
10207 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10208 * @argc: The count of lfs mirror verify command line arguments.
10209 * @argv: Array of strings for lfs mirror verify command line arguments.
10211 * This function parses lfs mirror verify command and verifies the
10212 * specified mirrored file(s).
10214 * Return: 0 on success or a negative error code on failure.
10216 static inline int lfs_mirror_verify(int argc, char **argv)
10218 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10224 char cmd[PATH_MAX];
10226 struct option long_opts[] = {
10227 { .val = 'o', .name = "only", .has_arg = required_argument },
10228 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10229 { .name = NULL } };
10231 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10233 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10236 rc = parse_mirror_ids(mirror_ids,
10237 ARRAY_SIZE(mirror_ids),
10241 "%s: bad mirror ids '%s'.\n",
10248 "%s: at least 2 mirror ids needed with '--only' option.\n",
10258 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10259 progname, argv[optind - 1]);
10265 if (argc == optind) {
10266 fprintf(stderr, "%s: no file name given.\n", progname);
10271 if (ids_nr > 0 && argc > optind + 1) {
10273 "%s: '--only' cannot be used upon multiple files.\n",
10281 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10287 for (; optind < argc; optind++) {
10288 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10298 * lfs_mirror() - Parse and execute lfs mirror commands.
10299 * @argc: The count of lfs mirror command line arguments.
10300 * @argv: Array of strings for lfs mirror command line arguments.
10302 * This function parses lfs mirror commands and performs the
10303 * corresponding functions specified in mirror_cmdlist[].
10305 * Return: 0 on success or an error code on failure.
10307 static int lfs_mirror(int argc, char **argv)
10309 char cmd[PATH_MAX];
10312 setlinebuf(stdout);
10314 Parser_init("lfs-mirror > ", mirror_cmdlist);
10316 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10318 program_invocation_short_name = cmd;
10320 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10322 rc = Parser_commands();
10324 return rc < 0 ? -rc : rc;
10327 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10329 #if __BYTE_ORDER == __BIG_ENDIAN
10330 __swab16s(&attrs->lsa_valid);
10331 __swab64s(&attrs->lsa_size);
10332 __swab64s(&attrs->lsa_blocks);
10336 enum lfs_som_type {
10337 LFS_SOM_SIZE = 0x1,
10338 LFS_SOM_BLOCKS = 0x2,
10339 LFS_SOM_FLAGS = 0x4,
10340 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10344 static int lfs_getsom(int argc, char **argv)
10347 struct lustre_som_attrs *attrs;
10348 char buf[sizeof(*attrs) + 64];
10349 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10352 while ((c = getopt(argc, argv, "sbf")) != -1) {
10355 type = LFS_SOM_SIZE;
10358 type = LFS_SOM_BLOCKS;
10361 type = LFS_SOM_FLAGS;
10364 fprintf(stderr, "%s: invalid option '%c'\n",
10374 fprintf(stderr, "%s: %s\n",
10375 progname, argc == 0 ? "miss file target" :
10376 "input more than 2 files");
10381 attrs = (void *)buf;
10382 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10385 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10386 argv[0], strerror(errno), errno);
10390 lustre_som_swab(attrs);
10393 case LFS_SOM_ATTR_ALL:
10394 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10395 path, attrs->lsa_size, attrs->lsa_blocks,
10399 printf("%llu\n", attrs->lsa_size);
10401 case LFS_SOM_BLOCKS:
10402 printf("%llu\n", attrs->lsa_blocks);
10404 case LFS_SOM_FLAGS:
10405 printf("%x\n", attrs->lsa_valid);
10408 fprintf(stderr, "%s: unknown option\n", progname);
10416 * lfs_mirror_list_commands() - List lfs mirror commands.
10417 * @argc: The count of command line arguments.
10418 * @argv: Array of strings for command line arguments.
10420 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10422 * Return: 0 on success.
10424 static int lfs_mirror_list_commands(int argc, char **argv)
10426 char buffer[81] = "";
10428 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10434 static int lfs_pcc_attach(int argc, char **argv)
10436 struct option long_opts[] = {
10437 { .val = 'i', .name = "id", .has_arg = required_argument },
10438 { .name = NULL } };
10441 __u32 archive_id = 0;
10444 char fullpath[PATH_MAX];
10445 enum lu_pcc_type type = LU_PCC_READWRITE;
10448 while ((c = getopt_long(argc, argv, "i:",
10449 long_opts, NULL)) != -1) {
10452 archive_id = strtoul(optarg, &end, 0);
10453 if (*end != '\0' || archive_id == 0) {
10454 fprintf(stderr, "error: %s: bad archive ID "
10455 "'%s'\n", argv[0], optarg);
10462 fprintf(stderr, "%s: option '%s' unrecognized\n",
10463 argv[0], argv[optind - 1]);
10468 if (argc <= optind) {
10469 fprintf(stderr, "%s: must specify one or more file names\n",
10474 while (optind < argc) {
10477 path = argv[optind++];
10478 if (realpath(path, fullpath) == NULL) {
10479 fprintf(stderr, "%s: could not find path '%s': %s\n",
10480 argv[0], path, strerror(errno));
10486 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10488 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10489 "with archive ID '%u': %s\n", argv[0],
10490 path, archive_id, strerror(-rc2));
10498 static int lfs_pcc_attach_fid(int argc, char **argv)
10500 struct option long_opts[] = {
10501 { .val = 'i', .name = "id", .has_arg = required_argument },
10502 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10503 { .name = NULL } };
10504 char short_opts[] = "i:m:";
10507 __u32 archive_id = 0;
10509 const char *mntpath = NULL;
10510 const char *fidstr;
10511 enum lu_pcc_type type = LU_PCC_READWRITE;
10514 while ((c = getopt_long(argc, argv, short_opts,
10515 long_opts, NULL)) != -1) {
10518 archive_id = strtoul(optarg, &end, 0);
10519 if (*end != '\0') {
10520 fprintf(stderr, "error: %s: bad archive ID "
10521 "'%s'\n", argv[0], optarg);
10531 fprintf(stderr, "%s: option '%s' unrecognized\n",
10532 argv[0], argv[optind - 1]);
10537 if (archive_id == 0) {
10538 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10542 if (mntpath == NULL) {
10543 fprintf(stderr, "%s: must specify Lustre mount point\n",
10548 if (argc <= optind) {
10549 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10553 while (optind < argc) {
10556 fidstr = argv[optind++];
10558 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10561 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10562 "with archive ID '%u': %s\n", argv[0],
10563 fidstr, mntpath, archive_id, strerror(rc2));
10565 if (rc == 0 && rc2 < 0)
10571 static int lfs_pcc_detach(int argc, char **argv)
10573 struct option long_opts[] = {
10574 { .val = 'k', .name = "keep", .has_arg = no_argument },
10575 { .name = NULL } };
10576 char short_opts[] = "k";
10580 char fullpath[PATH_MAX];
10581 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10584 while ((c = getopt_long(argc, argv, short_opts,
10585 long_opts, NULL)) != -1) {
10588 detach_opt = PCC_DETACH_OPT_NONE;
10593 fprintf(stderr, "%s: option '%s' unrecognized\n",
10594 argv[0], argv[optind - 1]);
10599 while (optind < argc) {
10602 path = argv[optind++];
10603 if (realpath(path, fullpath) == NULL) {
10604 fprintf(stderr, "%s: could not find path '%s': %s\n",
10605 argv[0], path, strerror(errno));
10611 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
10614 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
10615 "%s\n", argv[0], path, strerror(errno));
10623 static int lfs_pcc_detach_fid(int argc, char **argv)
10625 struct option long_opts[] = {
10626 { .val = 'k', .name = "keep", .has_arg = no_argument },
10627 { .name = NULL } };
10628 char short_opts[] = "k";
10632 const char *mntpath;
10633 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10636 while ((c = getopt_long(argc, argv, short_opts,
10637 long_opts, NULL)) != -1) {
10640 detach_opt = PCC_DETACH_OPT_NONE;
10645 fprintf(stderr, "%s: option '%s' unrecognized\n",
10646 argv[0], argv[optind - 1]);
10651 mntpath = argv[optind++];
10653 while (optind < argc) {
10656 fid = argv[optind++];
10658 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
10660 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
10661 "from PCC: %s\n", argv[0], fid, mntpath,
10670 static int lfs_pcc_state(int argc, char **argv)
10674 char fullpath[PATH_MAX];
10675 struct lu_pcc_state state;
10680 fprintf(stderr, "%s: must specify one or more file names\n",
10685 while (optind < argc) {
10688 path = argv[optind++];
10689 if (realpath(path, fullpath) == NULL) {
10690 fprintf(stderr, "%s: could not find path '%s': %s\n",
10691 argv[0], path, strerror(errno));
10697 rc2 = llapi_pcc_state_get(fullpath, &state);
10701 fprintf(stderr, "%s: cannot get PCC state of '%s': "
10702 "%s\n", argv[0], path, strerror(-rc2));
10706 printf("file: %s", path);
10707 printf(", type: %s", pcc_type2string(state.pccs_type));
10708 if (state.pccs_type == LU_PCC_NONE &&
10709 state.pccs_open_count == 0) {
10714 printf(", PCC file: %s", state.pccs_path);
10715 printf(", user number: %u", state.pccs_open_count);
10716 printf(", flags: %x", state.pccs_flags);
10723 * lfs_pcc_list_commands() - List lfs pcc commands.
10724 * @argc: The count of command line arguments.
10725 * @argv: Array of strings for command line arguments.
10727 * This function lists lfs pcc commands defined in pcc_cmdlist[].
10729 * Return: 0 on success.
10731 static int lfs_pcc_list_commands(int argc, char **argv)
10733 char buffer[81] = "";
10735 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
10742 * lfs_pcc() - Parse and execute lfs pcc commands.
10743 * @argc: The count of lfs pcc command line arguments.
10744 * @argv: Array of strings for lfs pcc command line arguments.
10746 * This function parses lfs pcc commands and performs the
10747 * corresponding functions specified in pcc_cmdlist[].
10749 * Return: 0 on success or an error code on failure.
10751 static int lfs_pcc(int argc, char **argv)
10753 char cmd[PATH_MAX];
10756 setlinebuf(stdout);
10758 Parser_init("lfs-pcc > ", pcc_cmdlist);
10760 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10762 program_invocation_short_name = cmd;
10764 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
10766 rc = Parser_commands();
10768 return rc < 0 ? -rc : rc;
10771 static int lfs_list_commands(int argc, char **argv)
10773 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10775 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10780 int main(int argc, char **argv)
10784 /* Ensure that liblustreapi constructor has run */
10785 if (!llapi_liblustreapi_initialized())
10786 fprintf(stderr, "liblustreapi was not properly initialized\n");
10788 setlinebuf(stdout);
10791 Parser_init("lfs > ", cmdlist);
10793 progname = program_invocation_short_name; /* Used in error messages */
10795 llapi_set_command_name(argv[1]);
10796 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10797 llapi_clear_command_name();
10799 rc = Parser_commands();
10802 return rc < 0 ? -rc : rc;
10805 #ifdef _LUSTRE_IDL_H_
10806 /* Everything we need here should be included by lustreapi.h. */
10807 # error "lfs should not depend on lustre_idl.h"
10808 #endif /* _LUSTRE_IDL_H_ */