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 " [--extension-size|--ext-size|-z]\n"
409 " [--component-start[=[+-]comp_start]]\n"
410 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
411 " [[!] --mirror-index=[+-]<index> |\n"
412 " [!] --mirror-id=[+-]<id>]\n"
413 " <directory|filename> ..."},
414 {"setdirstripe", lfs_setdirstripe, 0,
415 "To create a striped directory on a specified MDT. This can only\n"
416 "be done on MDT0 with the right of administrator.\n"
417 "usage: setdirstripe [OPTION] <directory>\n"
419 {"getdirstripe", lfs_getdirstripe, 0,
420 "To list the layout pattern info for a given directory\n"
421 "or recursively for all directories in a directory tree.\n"
422 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
423 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
424 " [--recursive|-r] [--yaml|-y]\n"
425 " [--verbose|-v] [--default|-D] <dir> ..."},
426 {"mkdir", lfs_setdirstripe, 0,
427 "To create a striped directory on a specified MDT. This can only\n"
428 "be done on MDT0 with the right of administrator.\n"
429 "usage: mkdir [OPTION] <directory>\n"
431 {"rm_entry", lfs_rmentry, 0,
432 "To remove the name entry of the remote directory. Note: This\n"
433 "command will only delete the name entry, i.e. the remote directory\n"
434 "will become inaccessable after this command. This can only be done\n"
435 "by the administrator\n"
436 "usage: rm_entry <dir>\n"},
437 {"pool_list", lfs_poollist, 0,
438 "List pools or pool OSTs\n"
439 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
440 {"find", lfs_find, 0,
441 "find files matching given attributes recursively in directory tree.\n"
442 "usage: find <directory|filename> ...\n"
443 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
444 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
445 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
446 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
447 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
448 " [[!] --stripe-count|-c [+-]<stripes>]\n"
449 " [[!] --stripe-index|-i <index,...>]\n"
450 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
451 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
452 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
453 " [[!] --projid <projid>]\n"
454 " [[!] --foreign[=<foreign_type>]]\n"
455 " [[!] --layout|-L released,raid0,mdt]\n"
456 " [[!] --foreign[=<foreign_type>]]\n"
457 " [[!] --component-count [+-]<comp_cnt>]\n"
458 " [[!] --component-start [+-]N[kMGTPE]]\n"
459 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
460 " [[!] --component-flags <comp_flags>]\n"
461 " [[!] --mirror-count|-N [+-]<n>]\n"
462 " [[!] --mirror-state <[^]state>]\n"
463 " [[!] --mdt-count|-T [+-]<stripes>]\n"
464 " [[!] --mdt-hash|-H <hashtype>\n"
465 " [[!] --mdt-index|-m <uuid|index,...>]\n"
466 "\t !: used before an option indicates 'NOT' requested attribute\n"
467 "\t -: used before a value indicates less than requested value\n"
468 "\t +: used before a value indicates more than requested value\n"
469 "\thashtype: hash type of the striped directory.\n"
470 "\t fnv_1a_64 FNV-1a hash algorithm\n"
471 "\t all_char sum of characters % MDT_COUNT\n"},
472 {"check", lfs_check, 0,
473 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
474 "or all the servers (MGTs, MDTs and OSTs).\n"
475 "usage: check <mgts|osts|mdts|all>"},
476 {"osts", lfs_osts, 0, "list OSTs connected to client "
477 "[for specified path only]\n" "usage: osts [path]"},
478 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
479 "[for specified path only]\n" "usage: mdts [path]"},
481 "report filesystem disk space usage or inodes usage "
482 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
483 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
484 {"getname", lfs_getname, 0,
485 "list instances and specified mount points [for specified path only]\n"
486 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
487 #ifdef HAVE_SYS_QUOTA_H
488 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
489 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
490 " -b <block-softlimit> -B <block-hardlimit>\n"
491 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
492 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
493 " [--block-softlimit <block-softlimit>]\n"
494 " [--block-hardlimit <block-hardlimit>]\n"
495 " [--inode-softlimit <inode-softlimit>]\n"
496 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
497 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
498 " [--block-grace <block-grace>]\n"
499 " [--inode-grace <inode-grace>] <filesystem>\n"
500 " setquota <-U|-G|-P>\n"
501 " -b <block-softlimit> -B <block-hardlimit>\n"
502 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
503 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
504 " [--block-softlimit <block-softlimit>]\n"
505 " [--block-hardlimit <block-hardlimit>]\n"
506 " [--inode-softlimit <inode-softlimit>]\n"
507 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
508 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
510 " -b can be used instead of --block-softlimit/--block-grace\n"
511 " -B can be used instead of --block-hardlimit\n"
512 " -i can be used instead of --inode-softlimit/--inode-grace\n"
513 " -I can be used instead of --inode-hardlimit\n"
514 " -d can be used instead of --default\n\n"
515 "Note: The total quota space will be split into many qunits and\n"
516 " balanced over all server targets, the minimal qunit size is\n"
517 " 1M bytes for block space and 1K inodes for inode space.\n\n"
518 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
519 " Quota space rebalancing process will stop when this mininum\n"
520 " value is reached. As a result, quota exceeded can be returned\n"
521 " while many targets still have 1MB or 1K inodes of spare\n"
523 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
524 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
526 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
527 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
528 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
529 {"project", lfs_project, 0,
530 "Change or list project attribute for specified file or directory.\n"
531 "usage: project [-d|-r] <file|directory...>\n"
532 " list project ID and flags on file(s) or directories\n"
533 " project [-p id] [-s] [-r] <file|directory...>\n"
534 " set project ID and/or inherit flag for specified file(s) or directories\n"
535 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
536 " check project ID and flags on file(s) or directories, print outliers\n"
537 " project -C [-r] [-k] <file|directory...>\n"
538 " clear the project inherit flag and ID on the file or directory\n"
541 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
542 "usage: flushctx [-k] [mountpoint...]"},
543 {"changelog", lfs_changelog, 0,
544 "Show the metadata changes on an MDT."
545 "\nusage: changelog <mdtname> [startrec [endrec]]"},
546 {"changelog_clear", lfs_changelog_clear, 0,
547 "Indicate that old changelog records up to <endrec> are no longer of "
548 "interest to consumer <id>, allowing the system to free up space.\n"
549 "An <endrec> of 0 means all records.\n"
550 "usage: changelog_clear <mdtname> <id> <endrec>"},
551 {"fid2path", lfs_fid2path, 0,
552 "Resolve the full path(s) for given FID(s). For a specific hardlink "
553 "specify link number <linkno>.\n"
554 /* "For a historical link name, specify changelog record <recno>.\n" */
555 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
556 /* [ --rec <recno> ] */ },
557 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
558 "usage: path2fid [--parents] <path> ..."},
559 {"data_version", lfs_data_version, 0, "Display file data version for "
560 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
561 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
562 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
563 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
564 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
565 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
566 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
568 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
569 "[--archived] [--lost] <file> ..."},
570 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
571 "given files.\n" "usage: hsm_action <file> ..."},
572 {"hsm_archive", lfs_hsm_archive, 0,
573 "Archive file to external storage.\n"
574 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
576 {"hsm_restore", lfs_hsm_restore, 0,
577 "Restore file from external storage.\n"
578 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
579 {"hsm_release", lfs_hsm_release, 0,
580 "Release files from Lustre.\n"
581 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
582 {"hsm_remove", lfs_hsm_remove, 0,
583 "Remove file copy from external storage.\n"
584 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
586 " (FILE [FILE ...] | "
587 "--mntpath MOUNTPATH FID [FID ...])\n"
589 "Note: To remove an archived copy of a file already deleted from a "
591 "--mntpath option and a list of FIDs must be specified"
593 {"hsm_cancel", lfs_hsm_cancel, 0,
594 "Cancel requests related to specified files.\n"
595 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
596 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
597 "usage: swap_layouts <path1> <path2>"},
598 {"migrate", lfs_setstripe_migrate, 0,
599 "migrate a directory between MDTs.\n"
600 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
601 " [--mdt-hash|-H] <hash_type>\n"
602 " [--mdt-index|-m] <start_mdt_index>\n"
605 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
606 " it's the MDT index of first stripe\n"
607 "\tmdt_count: number of MDTs to stripe a directory over\n"
608 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
609 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
610 " all_char sum of characters % MDT_COUNT\n"
612 "migrate file objects from one OST "
613 "layout\nto another (may be not safe with concurent writes).\n"
615 "[--stripe-count|-c] <stripe_count>\n"
616 "[--overstripe-count|-C] <stripe_count>\n"
617 " [--stripe-index|-i] <start_ost_index>\n"
618 " [--stripe-size|-S] <stripe_size>\n"
619 " [--pool|-p] <pool_name>\n"
620 " [--ost|-o] <ost_indices>\n"
622 " [--non-block|-n]\n"
623 " [--non-direct|-D]\n"
624 " <file|directory>\n"
625 "\tstripe_count: number of OSTs to stripe a file over\n"
626 "\t Using -C instead of -c allows overstriping, which\n"
627 "\t will place more than one stripe per OST if\n"
628 "\t stripe_count is greater than the number of OSTs\n"
629 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
630 "\tstripe_size: number of bytes to store before moving to the next OST\n"
631 "\tpool_name: name of the predefined pool of OSTs\n"
632 "\tost_indices: OSTs to stripe over, in order\n"
633 "\tblock: Block file access during data migration (default)\n"
634 "\tnon-block: Abort migrations if concurrent access is detected\n"
635 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
637 "To move directories between MDTs. This command is deprecated, "
638 "use \"migrate\" instead.\n"
639 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
641 {"ladvise", lfs_ladvise, 0,
642 "Provide servers with advice about access patterns for a file.\n"
643 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
644 " [--background|-b] [--unset|-u]\n\n"
645 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
646 " {[--mode|-m [READ,WRITE]}\n"
648 {"mirror", lfs_mirror, mirror_cmdlist,
649 "lfs commands used to manage files with mirrored components:\n"
650 "lfs mirror create - create a mirrored file or directory\n"
651 "lfs mirror extend - add mirror(s) to an existing file\n"
652 "lfs mirror split - split a mirror from an existing mirrored file\n"
653 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
654 "lfs mirror read - read a mirror content of a mirrored file\n"
655 "lfs mirror write - write to a mirror of a mirrored file\n"
656 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
657 "lfs mirror verify - verify mirrored file(s)\n"},
658 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
659 "usage: getsom [-s] [-b] [-f] <path>\n"
660 "\t-s: Only show the size value of the SOM data for a given file\n"
661 "\t-b: Only show the blocks value of the SOM data for a given file\n"
662 "\t-f: Only show the flags value of the SOM data for a given file\n"},
663 {"heat_get", lfs_heat_get, 0,
664 "To get heat of files.\n"
665 "usage: heat_get <file> ...\n"},
666 {"heat_set", lfs_heat_set, 0,
667 "To set heat flags of files.\n"
668 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
669 "\t--clear|-c: Clear file heat for given files\n"
670 "\t--off|-o: Turn off file heat for given files\n"
671 "\t--on|-O: Turn on file heat for given files\n"},
672 {"pcc", lfs_pcc, pcc_cmdlist,
673 "lfs commands used to interact with PCC features:\n"
674 "lfs pcc attach - attach given files to Persistent Client Cache\n"
675 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
676 "lfs pcc state - display the PCC state for given files\n"
677 "lfs pcc detach - detach given files from Persistent Client Cache\n"
678 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
679 {"help", Parser_help, 0, "help"},
680 {"exit", Parser_quit, 0, "quit"},
681 {"quit", Parser_quit, 0, "quit"},
682 {"--version", Parser_version, 0,
683 "output build version of the utility and exit"},
684 {"--list-commands", lfs_list_commands, 0,
685 "list commands supported by the utility and exit"},
690 static int check_hashtype(const char *hashtype)
694 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
695 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
698 if (!strcmp(hashtype, LMV_HASH_NAME_SPACE))
699 return LMV_HASH_TYPE_DEFAULT | LMV_HASH_FLAG_SPACE;
704 static uint32_t check_foreign_type_name(const char *foreign_type_name)
708 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
709 if (lu_foreign_types[i].lft_name == NULL)
711 if (strcmp(foreign_type_name,
712 lu_foreign_types[i].lft_name) == 0)
713 return lu_foreign_types[i].lft_type;
716 return LU_FOREIGN_TYPE_UNKNOWN;
719 static const char *error_loc = "syserror";
722 MIGRATION_NONBLOCK = 0x0001,
723 MIGRATION_MIRROR = 0x0002,
724 MIGRATION_NONDIRECT = 0x0004,
725 MIGRATION_VERBOSE = 0x0008,
728 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
729 struct llapi_layout *layout);
732 migrate_open_files(const char *name, __u64 migration_flags,
733 const struct llapi_stripe_param *param,
734 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
741 char parent[PATH_MAX];
742 char volatile_file[PATH_MAX];
748 if (param == NULL && layout == NULL) {
749 error_loc = "layout information";
753 /* search for file directory pathname */
754 if (strlen(name) > sizeof(parent) - 1) {
755 error_loc = "source file name";
759 strncpy(parent, name, sizeof(parent));
760 ptr = strrchr(parent, '/');
762 if (getcwd(parent, sizeof(parent)) == NULL) {
763 error_loc = "getcwd";
767 if (ptr == parent) /* leading '/' */
772 /* open file, direct io */
773 /* even if the file is only read, WR mode is nedeed to allow
774 * layout swap on fd */
776 if (!(migration_flags & MIGRATION_NONDIRECT))
778 fd = open(name, rflags);
781 error_loc = "cannot open source file";
785 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
787 error_loc = "cannot get MDT index";
792 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
793 mode_t open_mode = S_IRUSR | S_IWUSR;
795 random_value = random();
796 rc = snprintf(volatile_file, sizeof(volatile_file),
797 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
798 mdt_index, random_value);
799 if (rc >= sizeof(volatile_file)) {
804 /* create, open a volatile file, use caching (ie no directio) */
806 fdv = llapi_file_open_param(volatile_file, open_flags,
809 fdv = lfs_component_create(volatile_file, open_flags,
811 } while (fdv < 0 && (rc = fdv) == -EEXIST);
814 error_loc = "cannot create volatile file";
818 /* In case the MDT does not support creation of volatile files
819 * we should try to unlink it. */
820 (void)unlink(volatile_file);
822 /* Not-owner (root?) special case.
823 * Need to set owner/group of volatile file like original.
824 * This will allow to pass related check during layout_swap.
829 error_loc = "cannot stat source file";
833 rc = fstat(fdv, &stv);
836 error_loc = "cannot stat volatile";
840 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
841 rc = fchown(fdv, st.st_uid, st.st_gid);
844 error_loc = "cannot change ownwership of volatile";
863 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
865 struct llapi_layout *layout;
866 size_t buf_size = 4 * 1024 * 1024;
875 layout = llapi_layout_get_by_fd(fd_src, 0);
876 if (layout != NULL) {
877 uint64_t stripe_size;
879 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
881 buf_size = stripe_size;
883 llapi_layout_free(layout);
886 /* Use a page-aligned buffer for direct I/O */
887 rc = posix_memalign(&buf, getpagesize(), buf_size);
892 /* read new data only if we have written all
893 * previously read data */
896 rc = check_file(fd_src);
901 rsize = read(fd_src, buf, buf_size);
913 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
932 static int migrate_copy_timestamps(int fd, int fdv)
936 if (fstat(fd, &st) == 0) {
937 struct timeval tv[2] = {
938 {.tv_sec = st.st_atime},
939 {.tv_sec = st.st_mtime}
942 return futimes(fdv, tv);
948 static int migrate_block(int fd, int fdv)
955 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
957 error_loc = "cannot get dataversion";
965 /* The grouplock blocks all concurrent accesses to the file.
966 * It has to be taken after llapi_get_data_version as it would
968 rc = llapi_group_lock(fd, gid);
970 error_loc = "cannot get group lock";
974 rc = migrate_copy_data(fd, fdv, NULL);
976 error_loc = "data copy failed";
980 /* Make sure we keep original atime/mtime values */
981 rc = migrate_copy_timestamps(fd, fdv);
983 error_loc = "timestamp copy failed";
988 * for a migration we need to check data version on file did
991 * Pass in gid=0 since we already own grouplock. */
992 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
993 SWAP_LAYOUTS_CHECK_DV1);
995 error_loc = "file changed";
998 error_loc = "cannot swap layout";
1003 rc2 = llapi_group_unlock(fd, gid);
1004 if (rc2 < 0 && rc == 0) {
1005 error_loc = "unlock group lock";
1013 * Internal helper for migrate_copy_data(). Check lease and report error if
1016 * \param[in] fd File descriptor on which to check the lease.
1018 * \retval 0 Migration can keep on going.
1019 * \retval -errno Error occurred, abort migration.
1021 static int check_lease(int fd)
1025 rc = llapi_lease_check(fd);
1027 return 0; /* llapi_check_lease returns > 0 on success. */
1032 static int migrate_nonblock(int fd, int fdv)
1038 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1040 error_loc = "cannot get data version";
1044 rc = migrate_copy_data(fd, fdv, check_lease);
1046 error_loc = "data copy failed";
1050 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1052 error_loc = "cannot get data version";
1058 error_loc = "source file changed";
1062 /* Make sure we keep original atime/mtime values */
1063 rc = migrate_copy_timestamps(fd, fdv);
1065 error_loc = "timestamp copy failed";
1072 static int lfs_component_set(char *fname, int comp_id,
1073 __u32 flags, __u32 neg_flags)
1076 __u32 flags_array[2];
1081 ids[count] = comp_id;
1082 flags_array[count] = flags;
1087 if (neg_flags & LCME_FL_STALE) {
1088 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1089 "component. Please use lfs-mirror-resync(1) "
1090 "instead\n", progname);
1094 ids[count] = comp_id;
1095 flags_array[count] = neg_flags | LCME_FL_NEG;
1099 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1102 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1103 progname, comp_id, fname, flags, neg_flags);
1108 static int lfs_component_del(char *fname, __u32 comp_id,
1109 __u32 flags, __u32 neg_flags)
1113 if (flags && neg_flags)
1116 if (!flags && neg_flags)
1117 flags = neg_flags | LCME_FL_NEG;
1119 if ((flags && comp_id) || (!flags && !comp_id))
1123 if (flags & ~LCME_KNOWN_FLAGS) {
1125 "%s setstripe: unknown flags %#x\n",
1129 } else if (comp_id > LCME_ID_MAX) {
1130 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1135 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1138 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1139 progname, comp_id, fname, strerror(errno));
1143 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1150 rc = llapi_layout_file_comp_add(fname, layout);
1152 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1153 fname, strerror(errno));
1157 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1158 struct llapi_layout *layout)
1166 fd = lstat(fname, &st);
1167 if (fd == 0 && S_ISDIR(st.st_mode))
1168 open_flags = O_DIRECTORY | O_RDONLY;
1170 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1172 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1173 S_ISDIR(st.st_mode) ?
1174 "set default composite layout for" :
1175 "create composite file",
1176 fname, strerror(errno));
1180 static int lfs_migrate(char *name, __u64 migration_flags,
1181 struct llapi_stripe_param *param,
1182 struct llapi_layout *layout)
1188 rc = migrate_open_files(name, migration_flags, param, layout,
1193 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1194 /* Blocking mode (forced if servers do not support file lease).
1195 * It is also the default mode, since we cannot distinguish
1196 * between a broken lease and a server that does not support
1197 * atomic swap/close (LU-6785) */
1198 rc = migrate_block(fd, fdv);
1202 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1204 error_loc = "cannot get lease";
1208 rc = migrate_nonblock(fd, fdv);
1210 llapi_lease_release(fd);
1214 /* Atomically put lease, swap layouts and close.
1215 * for a migration we need to check data version on file did
1217 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1219 error_loc = "cannot swap layout";
1231 fprintf(stderr, "error: %s: %s: %s: %s\n",
1232 progname, name, error_loc, strerror(-rc));
1233 else if (migration_flags & MIGRATION_VERBOSE)
1234 printf("%s\n", name);
1239 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1248 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1252 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1253 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1254 const char *comp_name = comp_flags_table[i].cfn_name;
1256 if (strcmp(name, comp_name) == 0) {
1257 *flags |= comp_flag;
1259 } else if (strncmp(name, "^", 1) == 0 &&
1260 strcmp(name + 1, comp_name) == 0) {
1261 *neg_flags |= comp_flag;
1266 llapi_printf(LLAPI_MSG_ERROR,
1267 "%s: component flag '%s' not supported\n",
1273 if (!*flags && !*neg_flags)
1276 /* don't allow to set and exclude the same flag */
1277 if (*flags & *neg_flags)
1283 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1291 if (strncmp(string, "^", 1) == 0) {
1292 *neg_state = llapi_layout_string_flags(string + 1);
1293 if (*neg_state != 0)
1296 *state = llapi_layout_string_flags(string);
1301 llapi_printf(LLAPI_MSG_ERROR,
1302 "%s: mirrored file state '%s' not supported\n",
1308 * struct mirror_args - Command-line arguments for mirror(s).
1309 * @m_count: Number of mirrors to be created with this layout.
1310 * @m_flags: Mirror level flags, only 'prefer' is supported.
1311 * @m_layout: Mirror layout.
1312 * @m_file: A victim file. Its layout will be split and used as a mirror.
1313 * @m_next: Point to the next node of the list.
1315 * Command-line arguments for mirror(s) will be parsed and stored in
1316 * a linked list that consists of this structure.
1318 struct mirror_args {
1321 struct llapi_layout *m_layout;
1323 struct mirror_args *m_next;
1327 * enum mirror_flags - Flags for extending a mirrored file.
1328 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1329 * in case the victim file(s) contains the same data as the
1330 * original mirrored file.
1331 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1332 * @MF_COMP_ID: specified component id instead of mirror id
1334 * Flags for extending a mirrored file.
1343 * mirror_create_sanity_check() - Check mirror list.
1344 * @list: A linked list that stores the mirror arguments.
1346 * This function does a sanity check on @list for creating
1349 * Return: 0 on success or a negative error code on failure.
1351 static int mirror_create_sanity_check(const char *fname,
1352 struct mirror_args *list)
1355 bool has_m_file = false;
1356 bool has_m_layout = false;
1362 struct llapi_layout *layout;
1364 layout = llapi_layout_get_by_path(fname, 0);
1367 "error: %s: file '%s' couldn't get layout\n",
1372 rc = llapi_layout_sanity(layout, false, true);
1374 llapi_layout_free(layout);
1377 llapi_layout_sanity_perror(rc);
1382 while (list != NULL) {
1383 if (list->m_file != NULL) {
1385 llapi_layout_free(list->m_layout);
1388 llapi_layout_get_by_path(list->m_file, 0);
1389 if (list->m_layout == NULL) {
1391 "error: %s: file '%s' has no layout\n",
1392 progname, list->m_file);
1396 has_m_layout = true;
1397 if (list->m_layout == NULL) {
1398 fprintf(stderr, "error: %s: no mirror layout\n",
1404 rc = llapi_layout_sanity(list->m_layout, false, true);
1406 llapi_layout_sanity_perror(rc);
1410 list = list->m_next;
1413 if (has_m_file && has_m_layout) {
1415 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1423 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1425 __u32 mirror_flags = *(__u32 *)cbdata;
1429 rc = llapi_layout_comp_flags_get(layout, &flags);
1434 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1439 return LLAPI_LAYOUT_ITER_CONT;
1443 * mirror_create() - Create a mirrored file.
1444 * @fname: The file to be created.
1445 * @mirror_list: A linked list that stores the mirror arguments.
1447 * This function creates a mirrored file @fname with the mirror(s)
1448 * from @mirror_list.
1450 * Return: 0 on success or a negative error code on failure.
1452 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1454 struct llapi_layout *layout = NULL;
1455 struct mirror_args *cur_mirror = NULL;
1456 uint16_t mirror_count = 0;
1460 rc = mirror_create_sanity_check(NULL, mirror_list);
1464 cur_mirror = mirror_list;
1465 while (cur_mirror != NULL) {
1466 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1468 &cur_mirror->m_flags);
1471 fprintf(stderr, "%s: failed to set mirror flags\n",
1476 for (i = 0; i < cur_mirror->m_count; i++) {
1477 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1480 fprintf(stderr, "error: %s: "
1481 "merge layout failed: %s\n",
1482 progname, strerror(errno));
1486 mirror_count += cur_mirror->m_count;
1487 cur_mirror = cur_mirror->m_next;
1490 if (layout == NULL) {
1491 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1495 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1498 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1499 progname, strerror(errno));
1503 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1511 llapi_layout_free(layout);
1516 * Compare files and check lease on @fd.
1518 * \retval bytes number of bytes are the same
1520 static ssize_t mirror_file_compare(int fd, int fdv)
1522 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1524 ssize_t bytes_done = 0;
1525 ssize_t bytes_read = 0;
1527 buf = malloc(buflen * 2);
1532 if (!llapi_lease_check(fd)) {
1533 bytes_done = -EBUSY;
1537 bytes_read = read(fd, buf, buflen);
1538 if (bytes_read <= 0)
1541 if (bytes_read != read(fdv, buf + buflen, buflen))
1544 /* XXX: should compute the checksum on each buffer and then
1545 * compare checksum to avoid cache collision */
1546 if (memcmp(buf, buf + buflen, bytes_read))
1549 bytes_done += bytes_read;
1557 static int mirror_extend_file(const char *fname, const char *victim_file,
1558 enum mirror_flags mirror_flags)
1563 struct stat stbuf_v;
1564 struct ll_ioc_lease *data = NULL;
1567 fd = open(fname, O_RDWR);
1569 error_loc = "open source file";
1574 fdv = open(victim_file, O_RDWR);
1576 error_loc = "open target file";
1581 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1582 error_loc = "stat source or target file";
1587 if (stbuf.st_dev != stbuf_v.st_dev) {
1588 error_loc = "stat source and target file";
1593 /* mirrors should be of the same size */
1594 if (stbuf.st_size != stbuf_v.st_size) {
1595 error_loc = "file sizes don't match";
1600 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1602 error_loc = "cannot get lease";
1606 if (!(mirror_flags & MF_NO_VERIFY)) {
1608 /* mirrors should have the same contents */
1609 ret = mirror_file_compare(fd, fdv);
1610 if (ret != stbuf.st_size) {
1611 error_loc = "file busy or contents don't match";
1612 rc = ret < 0 ? ret : -EINVAL;
1617 /* Get rid of caching pages from clients */
1618 rc = llapi_file_flush(fd);
1620 error_loc = "cannot get data version";
1624 rc = llapi_file_flush(fdv);
1626 error_loc = "cannot get data version";
1631 /* Make sure we keep original atime/mtime values */
1632 rc = migrate_copy_timestamps(fd, fdv);
1634 error_loc = "cannot copy timestamp";
1638 /* Atomically put lease, merge layouts and close. */
1639 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1641 error_loc = "memory allocation";
1644 data->lil_mode = LL_LEASE_UNLCK;
1645 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1646 data->lil_count = 1;
1647 data->lil_ids[0] = fdv;
1648 rc = llapi_lease_set(fd, data);
1650 error_loc = "cannot merge layout";
1652 } else if (rc == 0) {
1654 error_loc = "lost lease lock";
1667 (void) unlink(victim_file);
1669 fprintf(stderr, "error: %s: %s: %s: %s\n",
1670 progname, fname, error_loc, strerror(-rc));
1674 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1676 struct ll_ioc_lease *data = NULL;
1681 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1685 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1687 error_loc = "cannot get lease";
1691 rc = migrate_nonblock(fd, fdv);
1693 llapi_lease_release(fd);
1697 /* Atomically put lease, merge layouts and close. */
1698 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1700 error_loc = "memory allocation";
1703 data->lil_mode = LL_LEASE_UNLCK;
1704 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1705 data->lil_count = 1;
1706 data->lil_ids[0] = fdv;
1707 rc = llapi_lease_set(fd, data);
1709 error_loc = "cannot merge layout";
1711 } else if (rc == 0) {
1713 error_loc = "lost lease lock";
1726 fprintf(stderr, "error: %s: %s: %s: %s\n",
1727 progname, name, error_loc, strerror(-rc));
1731 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1732 enum mirror_flags mirror_flags)
1736 rc = mirror_create_sanity_check(fname, mirror_list);
1740 while (mirror_list) {
1741 if (mirror_list->m_file != NULL) {
1742 rc = mirror_extend_file(fname, mirror_list->m_file,
1745 __u32 mirror_count = mirror_list->m_count;
1747 while (mirror_count > 0) {
1748 rc = mirror_extend_layout(fname,
1749 mirror_list->m_layout);
1759 mirror_list = mirror_list->m_next;
1765 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1770 rc = llapi_layout_mirror_id_get(layout, &id);
1774 if ((__u16)id == *(__u16 *)cbdata)
1775 return LLAPI_LAYOUT_ITER_STOP;
1777 return LLAPI_LAYOUT_ITER_CONT;
1780 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1785 rc = llapi_layout_comp_id_get(layout, &id);
1789 if (id == *(__u32 *)cbdata)
1790 return LLAPI_LAYOUT_ITER_STOP;
1792 return LLAPI_LAYOUT_ITER_CONT;
1794 static int mirror_split(const char *fname, __u32 id,
1795 enum mirror_flags mflags, const char *victim_file)
1797 struct llapi_layout *layout;
1798 char parent[PATH_MAX];
1799 char victim[PATH_MAX];
1800 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1802 struct ll_ioc_lease *data;
1803 uint16_t mirror_count;
1808 /* check fname contains mirror with mirror_id/comp_id */
1809 layout = llapi_layout_get_by_path(fname, 0);
1812 "error %s: file '%s' couldn't get layout\n",
1817 rc = llapi_layout_sanity(layout, false, true);
1819 llapi_layout_sanity_perror(rc);
1823 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1826 "error %s: file '%s' couldn't get mirror count\n",
1830 if (mirror_count < 2) {
1832 "error %s: file '%s' has %d component, cannot split\n",
1833 progname, fname, mirror_count);
1837 if (mflags & MF_COMP_ID) {
1838 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1839 id = mirror_id_of(id);
1841 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1844 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1847 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1849 "error %s: file '%s' does not contain mirror with id %u\n",
1850 progname, fname, id);
1854 fd = open(fname, O_RDWR);
1857 "error %s: open file '%s' failed: %s\n",
1858 progname, fname, strerror(errno));
1862 /* get victim file directory pathname */
1863 if (strlen(fname) > sizeof(parent) - 1) {
1864 fprintf(stderr, "error %s: file name of '%s' too long\n",
1869 strncpy(parent, fname, sizeof(parent));
1870 ptr = strrchr(parent, '/');
1872 if (getcwd(parent, sizeof(parent)) == NULL) {
1873 fprintf(stderr, "error %s: getcwd failed: %s\n",
1874 progname, strerror(errno));
1884 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1886 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1891 if (victim_file == NULL) {
1892 /* use a temp file to store the splitted layout */
1893 if (mflags & MF_DESTROY) {
1894 fdv = llapi_create_volatile_idx(parent, mdt_index,
1895 O_LOV_DELAY_CREATE);
1897 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1899 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1902 /* user specified victim file */
1903 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1908 "error %s: create victim file failed: %s\n",
1909 progname, strerror(errno));
1913 /* get lease lock of fname */
1914 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1917 "error %s: cannot get lease of file '%s': %d\n",
1918 progname, fname, rc);
1922 /* Atomatically put lease, split layouts and close. */
1923 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1929 data->lil_mode = LL_LEASE_UNLCK;
1930 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1931 data->lil_count = 2;
1932 data->lil_ids[0] = fdv;
1933 data->lil_ids[1] = id;
1934 rc = llapi_lease_set(fd, data);
1936 if (rc == 0) /* lost lease lock */
1939 "error %s: cannot split '%s': %s\n",
1940 progname, fname, strerror(-rc));
1951 llapi_layout_free(layout);
1956 * Parse a string containing an target index list into an array of integers.
1958 * The input string contains a comma delimited list of individual
1959 * indices and ranges, for example "1,2-4,7". Add the indices into the
1960 * \a tgts array and remove duplicates.
1962 * \param[out] tgts array to store indices in
1963 * \param[in] size size of \a tgts array
1964 * \param[in] offset starting index in \a tgts
1965 * \param[in] arg string containing OST index list
1966 * \param[in/out] overstriping index list may contain duplicates
1968 * \retval positive number of indices in \a tgts
1969 * \retval -EINVAL unable to parse \a arg
1971 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
1972 unsigned long long *pattern)
1976 int slots = size - offset;
1978 bool overstriped = false;
1984 end_of_loop = false;
1985 while (!end_of_loop) {
1986 int start_index = 0;
1989 char *endptr = NULL;
1993 ptr = strchrnul(arg, ',');
1995 end_of_loop = *ptr == '\0';
1998 start_index = strtol(arg, &endptr, 0);
1999 if (endptr == arg) /* no data at all */
2001 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2004 end_index = start_index;
2005 if (*endptr == '-') {
2006 end_index = strtol(endptr + 1, &endptr, 0);
2007 if (*endptr != '\0')
2009 if (end_index < start_index)
2013 for (i = start_index; i <= end_index && slots > 0; i++) {
2016 /* remove duplicate */
2017 for (j = 0; j < offset; j++) {
2018 if (tgts[j] == i && pattern &&
2019 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2021 else if (tgts[j] == i)
2027 if (j == offset) { /* check complete */
2033 if (slots == 0 && i < end_index)
2041 if (!end_of_loop && ptr != NULL)
2044 if (!overstriped && pattern)
2045 *pattern = LLAPI_LAYOUT_DEFAULT;
2047 return rc < 0 ? rc : nr;
2050 struct lfs_setstripe_args {
2051 unsigned long long lsa_comp_end;
2052 unsigned long long lsa_stripe_size;
2053 unsigned long long lsa_extension_size;
2054 long long lsa_stripe_count;
2055 long long lsa_stripe_off;
2056 __u32 lsa_comp_flags;
2057 __u32 lsa_comp_neg_flags;
2058 unsigned long long lsa_pattern;
2059 unsigned int lsa_mirror_count;
2061 bool lsa_first_comp;
2062 bool lsa_extension_comp;
2064 char *lsa_pool_name;
2067 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2069 unsigned int mirror_count = lsa->lsa_mirror_count;
2070 bool first_comp = lsa->lsa_first_comp;
2072 memset(lsa, 0, sizeof(*lsa));
2074 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2075 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2076 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2077 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2078 lsa->lsa_pool_name = NULL;
2080 lsa->lsa_mirror_count = mirror_count;
2081 lsa->lsa_first_comp = first_comp;
2085 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2086 * @lsa: Stripe options to be initialized and inherited.
2088 * This function initializes stripe options in @lsa and inherit
2089 * stripe_size, stripe_count and OST pool_name options.
2093 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2095 unsigned long long stripe_size;
2096 long long stripe_count;
2097 char *pool_name = NULL;
2099 stripe_size = lsa->lsa_stripe_size;
2100 stripe_count = lsa->lsa_stripe_count;
2101 pool_name = lsa->lsa_pool_name;
2103 setstripe_args_init(lsa);
2105 lsa->lsa_stripe_size = stripe_size;
2106 lsa->lsa_stripe_count = stripe_count;
2107 lsa->lsa_pool_name = pool_name;
2110 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2112 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2113 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2114 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2115 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2116 lsa->lsa_pool_name != NULL ||
2117 lsa->lsa_comp_end != 0);
2121 * comp_args_to_layout() - Create or extend a composite layout.
2122 * @composite: Pointer to the composite layout.
2123 * @lsa: Stripe options for the new component.
2125 * This function creates or extends a composite layout by adding a new
2126 * component with stripe options from @lsa.
2128 * Return: 0 on success or an error code on failure.
2130 static int comp_args_to_layout(struct llapi_layout **composite,
2131 struct lfs_setstripe_args *lsa,
2134 struct llapi_layout *layout = *composite;
2135 uint64_t prev_end = 0;
2140 if (layout == NULL) {
2141 layout = llapi_layout_alloc();
2142 if (layout == NULL) {
2143 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2148 *composite = layout;
2149 lsa->lsa_first_comp = true;
2153 /* Get current component extent, current component
2154 * must be the tail component. */
2155 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2157 fprintf(stderr, "Get comp extent failed. %s\n",
2162 if (lsa->lsa_first_comp)
2165 if (lsa->lsa_first_comp)
2166 rc = llapi_layout_add_first_comp(layout);
2168 rc = llapi_layout_comp_add(layout);
2170 fprintf(stderr, "Add component failed. %s\n",
2176 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2178 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2179 lsa->lsa_comp_flags, strerror(errno));
2184 uint64_t comp_end = lsa->lsa_comp_end;
2186 /* Extension space handling:
2187 * If this is the first component, length is extension_size.
2188 * If not, it is zero length, so it can be removed if there is
2189 * insufficient space to extend it.
2191 if (lsa->lsa_extension_comp && lsa->lsa_first_comp)
2192 comp_end = prev_end + lsa->lsa_extension_size;
2193 else if (lsa->lsa_extension_comp)
2194 comp_end = prev_end;
2196 rc = llapi_layout_comp_extent_set(layout, prev_end,
2199 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2200 prev_end, comp_end, strerror(errno));
2204 /* reset lsa_first_comp */
2205 lsa->lsa_first_comp = false;
2207 /* Data-on-MDT component setting */
2208 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2209 /* In case of Data-on-MDT patterns the only extra option
2210 * applicable is stripe size option. */
2211 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2212 fprintf(stderr, "Option 'stripe-count' can't be "
2213 "specified with Data-on-MDT component: %lld\n",
2214 lsa->lsa_stripe_count);
2218 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2219 fprintf(stderr, "Option 'stripe-size' can't be "
2220 "specified with Data-on-MDT component: %llu\n",
2221 lsa->lsa_stripe_size);
2225 if (lsa->lsa_nr_tgts != 0) {
2226 fprintf(stderr, "Option 'ost-list' can't be specified "
2227 "with Data-on-MDT component: '%i'\n",
2232 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2233 fprintf(stderr, "Option 'stripe-offset' can't be "
2234 "specified with Data-on-MDT component: %lld\n",
2235 lsa->lsa_stripe_off);
2239 if (lsa->lsa_pool_name != 0) {
2240 fprintf(stderr, "Option 'pool' can't be specified "
2241 "with Data-on-MDT component: '%s'\n",
2242 lsa->lsa_pool_name);
2247 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2249 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2250 lsa->lsa_pattern, strerror(errno));
2253 /* Data-on-MDT component has always single stripe up to end */
2254 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2255 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2256 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2258 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2259 lsa->lsa_pattern, strerror(errno));
2264 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2265 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2267 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2268 rc = llapi_layout_extension_size_set(layout, size);
2270 rc = llapi_layout_stripe_size_set(layout, size);
2273 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2274 size, strerror(errno));
2278 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2280 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2281 lsa->lsa_stripe_count, strerror(errno));
2285 if (lsa->lsa_pool_name != NULL) {
2286 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2288 fprintf(stderr, "Set pool name: %s failed. %s\n",
2289 lsa->lsa_pool_name, strerror(errno));
2293 rc = llapi_layout_pool_name_set(layout, "");
2295 fprintf(stderr, "Clear pool name failed: %s\n",
2301 if (lsa->lsa_nr_tgts > 0) {
2302 if (lsa->lsa_stripe_count > 0 &&
2303 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2304 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2305 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2306 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2307 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2311 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2312 rc = llapi_layout_ost_index_set(layout, i,
2317 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2318 lsa->lsa_stripe_off != -1) {
2319 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2322 fprintf(stderr, "Set ost index %d failed. %s\n",
2323 i, strerror(errno));
2327 /* Create the second, virtual component of extension space */
2328 if (lsa->lsa_extension_comp) {
2329 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2330 lsa->lsa_extension_comp = false;
2337 static int build_component(struct llapi_layout **layout,
2338 struct lfs_setstripe_args *lsa, bool set_extent)
2342 rc = comp_args_to_layout(layout, lsa, set_extent);
2346 if (lsa->lsa_mirror_count > 0) {
2347 rc = llapi_layout_mirror_count_set(*layout,
2348 lsa->lsa_mirror_count);
2352 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2355 lsa->lsa_mirror_count = 0;
2361 static int build_layout_from_yaml_node(struct cYAML *node,
2362 struct llapi_layout **layout,
2363 struct lfs_setstripe_args *lsa,
2370 if (node->cy_type == CYAML_TYPE_OBJECT) {
2371 /* go deep to sub blocks */
2372 rc = build_layout_from_yaml_node(node->cy_child, layout,
2377 if (node->cy_string == NULL)
2380 string = node->cy_string;
2381 /* skip leading lmm_ if present, to simplify parsing */
2382 if (strncmp(string, "lmm_", 4) == 0)
2385 if (node->cy_type == CYAML_TYPE_STRING) {
2386 if (!strcmp(string, "lcme_extent.e_end")) {
2387 if (!strcmp(node->cy_valuestring, "EOF") ||
2388 !strcmp(node->cy_valuestring, "eof"))
2389 lsa->lsa_comp_end = LUSTRE_EOF;
2390 } else if (!strcmp(string, "pool")) {
2391 lsa->lsa_pool_name = node->cy_valuestring;
2392 } else if (!strcmp(string, "pattern")) {
2393 if (!strcmp(node->cy_valuestring, "mdt"))
2394 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2395 if (!strcmp(node->cy_valuestring,
2396 "raid0,overstriped"))
2398 LLAPI_LAYOUT_OVERSTRIPING;
2399 } else if (!strcmp(string, "lcme_flags")) {
2400 rc = comp_str2flags(node->cy_valuestring,
2401 &lsa->lsa_comp_flags,
2402 &lsa->lsa_comp_neg_flags);
2405 /* Only template flags have meaning in
2406 * the layout for a new file
2408 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2410 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2411 if (!strcmp(string, "lcm_mirror_count")) {
2412 lsa->lsa_mirror_count = node->cy_valueint;
2413 } else if (!strcmp(string, "lcme_extent.e_start")) {
2414 if (node->cy_valueint != 0 || *layout != NULL) {
2415 rc = build_component(layout, lsa, true);
2420 if (node->cy_valueint == 0)
2421 lsa->lsa_first_comp = true;
2423 /* initialize lsa */
2424 setstripe_args_init(lsa);
2425 lsa->lsa_tgts = osts;
2426 } else if (!strcmp(string, "lcme_extent.e_end")) {
2427 if (node->cy_valueint == -1)
2428 lsa->lsa_comp_end = LUSTRE_EOF;
2430 lsa->lsa_comp_end = node->cy_valueint;
2431 } else if (!strcmp(string, "stripe_count")) {
2432 lsa->lsa_stripe_count = node->cy_valueint;
2433 } else if (!strcmp(string, "stripe_size")) {
2434 lsa->lsa_stripe_size = node->cy_valueint;
2435 } else if (!strcmp(string, "stripe_offset")) {
2436 lsa->lsa_stripe_off = node->cy_valueint;
2437 } else if (!strcmp(string, "l_ost_idx")) {
2438 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2443 node = node->cy_next;
2449 static int lfs_comp_create_from_yaml(char *template,
2450 struct llapi_layout **layout,
2451 struct lfs_setstripe_args *lsa,
2454 struct cYAML *tree = NULL, *err_rc = NULL;
2457 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2459 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2460 progname, template);
2461 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2462 "can't parse", &err_rc);
2463 cYAML_print_tree2file(stderr, err_rc);
2464 cYAML_free_tree(err_rc);
2469 /* initialize lsa for plain file */
2470 setstripe_args_init(lsa);
2471 lsa->lsa_tgts = osts;
2473 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2475 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2476 progname, template);
2479 rc = build_component(layout, lsa, *layout != NULL);
2481 /* clean clean lsa */
2482 setstripe_args_init(lsa);
2486 cYAML_free_tree(tree);
2490 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2491 * end of the last component in the existing file, and adjust the
2492 * first extent start of the components to be added accordingly. */
2493 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2495 struct llapi_layout *head;
2496 uint64_t start, end, stripe_size, prev_end = 0;
2499 if (layout == NULL) {
2501 "%s setstripe: layout must be specified\n",
2507 head = llapi_layout_get_by_path(fname, 0);
2510 "%s setstripe: cannot read layout from '%s': %s\n",
2511 progname, fname, strerror(errno));
2513 } else if (errno == ENODATA) {
2514 /* file without LOVEA, this component-add will be turned
2515 * into a component-create. */
2516 llapi_layout_free(head);
2518 } else if (!llapi_layout_is_composite(head)) {
2519 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2521 llapi_layout_free(head);
2525 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2527 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2528 progname, strerror(errno));
2529 llapi_layout_free(head);
2533 llapi_layout_free(head);
2535 /* Make sure we use the first component of the layout to be added. */
2536 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2539 "%s setstripe: cannot move component cursor: %s\n",
2540 progname, strerror(errno));
2544 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2546 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2547 progname, strerror(errno));
2551 if (start > prev_end || end <= prev_end) {
2553 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2554 progname, start, end, prev_end);
2558 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2560 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2561 progname, strerror(errno));
2565 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2566 (prev_end & (stripe_size - 1))) {
2568 "%s setstripe: stripe size %lu not aligned with %lu\n",
2569 progname, stripe_size, prev_end);
2573 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2576 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2577 progname, prev_end, end, strerror(errno));
2584 static inline bool arg_is_eof(char *arg)
2586 return !strncmp(arg, "-1", strlen("-1")) ||
2587 !strncmp(arg, "EOF", strlen("EOF")) ||
2588 !strncmp(arg, "eof", strlen("eof"));
2592 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2594 * Return: Valid mirror_args pointer on success and
2595 * NULL if memory allocation fails.
2597 static struct mirror_args *lfs_mirror_alloc(void)
2599 struct mirror_args *mirror = NULL;
2602 mirror = calloc(1, sizeof(*mirror));
2613 * lfs_mirror_free() - Free memory allocated for a mirror argument
2615 * @mirror: Previously allocated mirror argument structure by
2616 * lfs_mirror_alloc().
2618 * Free memory allocated for @mirror.
2622 static void lfs_mirror_free(struct mirror_args *mirror)
2624 if (mirror->m_layout != NULL)
2625 llapi_layout_free(mirror->m_layout);
2630 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2631 * @mirror_list: Previously allocated mirror list.
2633 * Free memory allocated for @mirror_list.
2637 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2639 struct mirror_args *next_mirror = NULL;
2641 while (mirror_list != NULL) {
2642 next_mirror = mirror_list->m_next;
2643 lfs_mirror_free(mirror_list);
2644 mirror_list = next_mirror;
2656 LFS_COMP_NO_VERIFY_OPT,
2658 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2660 LFS_MIRROR_STATE_OPT,
2662 LFS_MIRROR_INDEX_OPT,
2663 LFS_LAYOUT_FOREIGN_OPT,
2668 static int lfs_setstripe_internal(int argc, char **argv,
2669 enum setstripe_origin opc)
2671 struct lfs_setstripe_args lsa = { 0 };
2672 struct llapi_stripe_param *param = NULL;
2673 struct find_param migrate_mdt_param = {
2683 unsigned long long size_units = 1;
2684 bool migrate_mode = false;
2685 bool migrate_mdt_mode = false;
2686 bool setstripe_mode = false;
2687 bool migration_block = false;
2688 __u64 migration_flags = 0;
2689 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2690 int comp_del = 0, comp_set = 0;
2693 struct llapi_layout *layout = NULL;
2694 struct llapi_layout **lpp = &layout;
2695 bool mirror_mode = false;
2696 bool has_m_file = false;
2697 __u32 mirror_count = 0;
2698 enum mirror_flags mirror_flags = 0;
2699 struct mirror_args *mirror_list = NULL;
2700 struct mirror_args *new_mirror = NULL;
2701 struct mirror_args *last_mirror = NULL;
2702 __u16 mirror_id = 0;
2704 bool from_yaml = false;
2705 bool from_copy = false;
2706 char *template = NULL;
2707 bool foreign_mode = false;
2709 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
2710 char *mode_opt = NULL;
2711 mode_t previous_umask = 0;
2712 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2714 struct option long_opts[] = {
2715 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2716 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2717 /* --block is only valid in migrate mode */
2718 { .val = 'b', .name = "block", .has_arg = no_argument },
2719 { .val = LFS_COMP_ADD_OPT,
2720 .name = "comp-add", .has_arg = no_argument },
2721 { .val = LFS_COMP_ADD_OPT,
2722 .name = "component-add", .has_arg = no_argument },
2723 { .val = LFS_COMP_DEL_OPT,
2724 .name = "comp-del", .has_arg = no_argument },
2725 { .val = LFS_COMP_DEL_OPT,
2726 .name = "component-del", .has_arg = no_argument },
2727 { .val = LFS_COMP_FLAGS_OPT,
2728 .name = "comp-flags", .has_arg = required_argument },
2729 { .val = LFS_COMP_FLAGS_OPT,
2730 .name = "component-flags",
2731 .has_arg = required_argument },
2732 { .val = LFS_COMP_SET_OPT,
2733 .name = "comp-set", .has_arg = no_argument },
2734 { .val = LFS_COMP_SET_OPT,
2735 .name = "component-set",
2736 .has_arg = no_argument},
2737 { .val = LFS_COMP_NO_VERIFY_OPT,
2738 .name = "no-verify", .has_arg = no_argument},
2739 { .val = LFS_LAYOUT_FLAGS_OPT,
2740 .name = "flags", .has_arg = required_argument},
2741 { .val = LFS_LAYOUT_FOREIGN_OPT,
2742 .name = "foreign", .has_arg = optional_argument},
2743 { .val = LFS_MIRROR_ID_OPT,
2744 .name = "mirror-id", .has_arg = required_argument},
2745 { .val = LFS_MODE_OPT,
2746 .name = "mode", .has_arg = required_argument},
2747 { .val = LFS_LAYOUT_COPY,
2748 .name = "copy", .has_arg = required_argument},
2749 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2750 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2751 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2752 { .val = 'C', .name = "overstripe-count",
2753 .has_arg = required_argument},
2754 { .val = 'd', .name = "delete", .has_arg = no_argument},
2755 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2756 /* --non-direct is only valid in migrate mode */
2757 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2758 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2759 { .val = 'E', .name = "component-end",
2760 .has_arg = required_argument},
2761 { .val = 'f', .name = "file", .has_arg = required_argument },
2762 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2763 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2764 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2765 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2766 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2767 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2768 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2769 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2770 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2771 { .val = 'L', .name = "layout", .has_arg = required_argument },
2772 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2773 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2774 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2775 /* --non-block is only valid in migrate mode */
2776 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2777 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2778 { .val = 'o', .name = "ost", .has_arg = required_argument },
2779 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2780 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2781 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2783 { .val = 'p', .name = "pool", .has_arg = required_argument },
2784 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2785 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2786 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2787 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2788 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2789 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2790 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2791 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2792 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2793 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2794 /* --verbose is only valid in migrate mode */
2795 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2796 { .val = 'x', .name = "xattr", .has_arg = required_argument },
2797 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2798 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
2799 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
2802 setstripe_args_init(&lsa);
2804 migrate_mode = (opc == SO_MIGRATE);
2805 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2806 setstripe_mode = (opc == SO_SETSTRIPE);
2808 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2810 while ((c = getopt_long(argc, argv,
2811 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
2812 long_opts, NULL)) >= 0) {
2818 case LFS_COMP_ADD_OPT:
2821 case LFS_COMP_DEL_OPT:
2824 case LFS_COMP_FLAGS_OPT:
2825 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2826 &lsa.lsa_comp_neg_flags);
2829 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2830 fprintf(stderr, "%s: inverted flags are not supported\n",
2835 case LFS_COMP_SET_OPT:
2838 case LFS_COMP_NO_VERIFY_OPT:
2839 mirror_flags |= MF_NO_VERIFY;
2841 case LFS_MIRROR_ID_OPT:
2842 mirror_id = strtoul(optarg, &end, 0);
2843 if (*end != '\0' || mirror_id == 0) {
2845 "%s %s: invalid mirror ID '%s'\n",
2846 progname, argv[0], optarg);
2850 case LFS_LAYOUT_FLAGS_OPT: {
2853 /* check for numeric flags (foreign and mirror cases) */
2854 if (setstripe_mode && !mirror_mode && !last_mirror) {
2855 flags = strtoul(optarg, &end, 16);
2858 "%s %s: bad flags '%s'\n",
2859 progname, argv[0], optarg);
2865 if (!mirror_mode || !last_mirror) {
2866 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2871 result = comp_str2flags(optarg, &last_mirror->m_flags,
2877 fprintf(stderr, "%s: inverted flags are not supported\n",
2882 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
2884 "%s: unsupported mirror flags: %s\n",
2891 case LFS_LAYOUT_FOREIGN_OPT:
2892 if (optarg != NULL) {
2893 /* check pure numeric */
2894 type = strtoul(optarg, &end, 0);
2897 type = check_foreign_type_name(optarg);
2898 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
2900 "%s %s: unrecognized foreign type '%s'\n",
2907 foreign_mode = true;
2911 if (mode_opt != NULL) {
2912 mode = strtoul(mode_opt, &end, 8);
2915 "%s %s: bad mode '%s'\n",
2916 progname, argv[0], mode_opt);
2919 previous_umask = umask(0);
2922 case LFS_LAYOUT_COPY:
2927 if (!migrate_mode) {
2929 "%s %s: -b|--block valid only for migrate command\n",
2933 migration_block = true;
2936 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
2939 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2942 "%s %s: invalid stripe count '%s'\n",
2943 progname, argv[0], optarg);
2947 if (lsa.lsa_stripe_count == -1)
2948 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2951 /* delete the default striping pattern */
2953 if (opc == SO_MIRROR_SPLIT) {
2956 "%s %s: -d cannot used with -f\n",
2960 mirror_flags |= MF_DESTROY;
2964 if (!migrate_mode) {
2966 "%s %s: -D|--non-direct is valid "
2967 "only for migrate command\n",
2971 migration_flags |= MIGRATION_NONDIRECT;
2974 if (lsa.lsa_comp_end != 0) {
2975 result = comp_args_to_layout(lpp, &lsa, true);
2977 fprintf(stderr, "%s: invalid layout\n",
2982 setstripe_args_init_inherit(&lsa);
2985 if (arg_is_eof(optarg)) {
2986 lsa.lsa_comp_end = LUSTRE_EOF;
2988 result = llapi_parse_size(optarg,
2993 "%s %s: invalid component end '%s'\n",
2994 progname, argv[0], optarg);
3000 if (!migrate_mode) {
3001 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3005 lsa.lsa_pattern = check_hashtype(optarg);
3006 if (lsa.lsa_pattern == 0) {
3008 "%s %s: bad stripe hash type '%s'\n",
3009 progname, argv[0], optarg);
3014 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3017 "%s %s: invalid stripe offset '%s'\n",
3018 progname, argv[0], optarg);
3021 if (lsa.lsa_stripe_off == -1)
3022 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3025 comp_id = strtoul(optarg, &end, 0);
3026 if (*end != '\0' || comp_id == 0 ||
3027 comp_id > LCME_ID_MAX) {
3029 "%s %s: invalid component ID '%s'\n",
3030 progname, argv[0], optarg);
3035 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3037 "error: %s: invalid option: %s\n",
3038 progname, argv[optopt + 1]);
3041 if (opc == SO_MIRROR_EXTEND) {
3042 if (last_mirror == NULL) {
3044 "error: %s: '-N' must exist in front of '%s'\n",
3045 progname, argv[optopt + 1]);
3048 last_mirror->m_file = optarg;
3049 last_mirror->m_count = 1;
3052 if (mirror_list == NULL)
3053 mirror_list = lfs_mirror_alloc();
3054 mirror_list->m_file = optarg;
3059 if (strcmp(argv[optind - 1], "mdt") == 0) {
3060 /* Can be only the first component */
3061 if (layout != NULL) {
3063 fprintf(stderr, "error: 'mdt' layout "
3064 "can be only the first one\n");
3067 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3069 fprintf(stderr, "error: 'mdt' layout "
3070 "size is too big\n");
3073 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3074 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3076 fprintf(stderr, "error: layout '%s' is "
3077 "unknown, supported layouts are: "
3078 "'mdt', 'raid0'\n", argv[optind]);
3083 if (!migrate_mode) {
3085 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3089 migrate_mdt_mode = true;
3090 lsa.lsa_nr_tgts = parse_targets(tgts,
3091 sizeof(tgts) / sizeof(__u32),
3092 lsa.lsa_nr_tgts, optarg, NULL);
3093 if (lsa.lsa_nr_tgts < 0) {
3095 "%s %s: invalid MDT target(s) '%s'\n",
3096 progname, argv[0], optarg);
3100 lsa.lsa_tgts = tgts;
3101 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3102 lsa.lsa_stripe_off = tgts[0];
3105 if (!migrate_mode) {
3107 "%s %s: -n|--non-block valid only for migrate command\n",
3111 migration_flags |= MIGRATION_NONBLOCK;
3114 if (opc == SO_SETSTRIPE) {
3115 opc = SO_MIRROR_CREATE;
3119 if (optarg != NULL) {
3120 mirror_count = strtoul(optarg, &end, 0);
3121 if (*end != '\0' || mirror_count == 0) {
3123 "error: %s: bad mirror count: %s\n",
3130 new_mirror = lfs_mirror_alloc();
3131 new_mirror->m_count = mirror_count;
3133 if (mirror_list == NULL)
3134 mirror_list = new_mirror;
3136 if (last_mirror != NULL) {
3137 /* wrap up last mirror */
3138 if (lsa.lsa_comp_end == 0)
3139 lsa.lsa_comp_end = LUSTRE_EOF;
3141 result = comp_args_to_layout(lpp, &lsa, true);
3143 lfs_mirror_free(new_mirror);
3147 setstripe_args_init_inherit(&lsa);
3149 last_mirror->m_next = new_mirror;
3152 last_mirror = new_mirror;
3153 lpp = &last_mirror->m_layout;
3156 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3157 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3158 fprintf(stderr, "warning: '--ost-list' is "
3159 "deprecated, use '--ost' instead\n");
3161 /* -o allows overstriping, and must note it because
3162 * parse_targets is shared with MDT striping, which
3163 * does not allow duplicates
3165 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3166 lsa.lsa_nr_tgts = parse_targets(tgts,
3167 sizeof(tgts) / sizeof(__u32),
3168 lsa.lsa_nr_tgts, optarg,
3170 if (lsa.lsa_nr_tgts < 0) {
3172 "%s %s: invalid OST target(s) '%s'\n",
3173 progname, argv[0], optarg);
3177 lsa.lsa_tgts = tgts;
3178 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3179 lsa.lsa_stripe_off = tgts[0];
3184 lsa.lsa_pool_name = optarg;
3186 if (strlen(lsa.lsa_pool_name) == 0 ||
3187 strncmp(lsa.lsa_pool_name, "none",
3188 LOV_MAXPOOLNAME) == 0)
3189 lsa.lsa_pool_name = NULL;
3192 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3196 "%s %s: invalid stripe size '%s'\n",
3197 progname, argv[0], optarg);
3202 if (!migrate_mode) {
3204 "%s %s: -v|--verbose valid only for migrate command\n",
3208 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3209 migration_flags = MIGRATION_VERBOSE;
3219 result = llapi_parse_size(optarg,
3220 &lsa.lsa_extension_size,
3224 "%s %s: invalid extension size '%s'\n",
3225 progname, argv[0], optarg);
3229 lsa.lsa_extension_comp = true;
3232 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3233 progname, argv[0], argv[optind - 1]);
3238 fname = argv[optind];
3240 if (optind == argc) {
3241 fprintf(stderr, "%s %s: FILE must be specified\n",
3246 if (xattr && !foreign_mode) {
3247 /* only print a warning as this is harmless and will be ignored
3250 "%s %s: xattr has been specified for non-foreign layout\n",
3252 } else if (foreign_mode && !xattr) {
3254 "%s %s: xattr must be provided in foreign mode\n",
3259 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3260 comp_set || comp_id || delete || from_copy ||
3261 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3264 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3269 if (mirror_mode && mirror_count == 0) {
3271 "error: %s: --mirror-count|-N option is required\n",
3278 if (lsa.lsa_comp_end == 0)
3279 lsa.lsa_comp_end = LUSTRE_EOF;
3282 if (lsa.lsa_comp_end != 0) {
3283 result = comp_args_to_layout(lpp, &lsa, true);
3285 fprintf(stderr, "error: %s: invalid layout\n",
3292 if (mirror_flags & MF_NO_VERIFY) {
3293 if (opc != SO_MIRROR_EXTEND) {
3295 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3299 } else if (!has_m_file) {
3301 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3308 if (comp_set && !comp_id) {
3309 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3314 if ((delete + comp_set + comp_del + comp_add) > 1) {
3316 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3321 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3322 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3324 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3329 if ((comp_set || comp_del) &&
3330 (setstripe_args_specified(&lsa) || layout != NULL)) {
3332 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3337 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3339 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3344 if (comp_add || comp_del) {
3347 result = lstat(fname, &st);
3348 if (result == 0 && S_ISDIR(st.st_mode)) {
3350 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3356 fprintf(stderr, "error: %s: can't use --component-add "
3357 "or --component-del for mirror operation\n",
3364 if (layout == NULL) {
3366 "%s %s: option -E must be specified with --component-add\n",
3371 result = adjust_first_extent(fname, layout);
3372 if (result == -ENODATA)
3374 else if (result != 0) {
3375 fprintf(stderr, "error: %s: invalid layout\n",
3381 if (from_yaml && from_copy) {
3383 "%s: can't specify --yaml and --copy together\n",
3388 if ((from_yaml || from_copy) &&
3389 (setstripe_args_specified(&lsa) || layout != NULL)) {
3390 fprintf(stderr, "error: %s: can't specify --yaml with "
3391 "-c, -S, -i, -o, -p or -E options.\n",
3396 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3398 "%s %s: options --non-block and --block are mutually exclusive\n",
3403 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3406 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3411 if (migrate_mdt_mode) {
3412 struct lmv_user_md *lmu;
3414 /* initialize migrate mdt parameters */
3415 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3416 LMV_USER_MAGIC_SPECIFIC));
3419 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3420 progname, argv[0], strerror(ENOMEM));
3424 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3425 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3426 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3428 "%s %s: migrate should specify MDT index\n",
3433 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3434 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3435 lmu->lum_hash_type = lsa.lsa_pattern;
3437 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3438 if (lsa.lsa_pool_name)
3439 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3440 sizeof(lmu->lum_pool_name) - 1);
3441 if (lsa.lsa_nr_tgts > 1) {
3444 if (lsa.lsa_stripe_count > 0 &&
3445 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3446 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3448 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3449 progname, lsa.lsa_stripe_count,
3455 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3456 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3457 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3458 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3460 lmu->lum_magic = LMV_USER_MAGIC;
3463 migrate_mdt_param.fp_lmv_md = lmu;
3464 migrate_mdt_param.fp_migrate = 1;
3465 } else if (layout == NULL) {
3466 /* initialize stripe parameters */
3467 param = calloc(1, offsetof(typeof(*param),
3468 lsp_osts[lsa.lsa_nr_tgts]));
3469 if (param == NULL) {
3471 "%s %s: cannot allocate memory for parameters: %s\n",
3472 progname, argv[0], strerror(ENOMEM));
3477 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3478 param->lsp_stripe_size = lsa.lsa_stripe_size;
3479 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3480 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3481 param->lsp_stripe_count = -1;
3483 param->lsp_stripe_count = lsa.lsa_stripe_count;
3485 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3486 param->lsp_stripe_offset = -1;
3488 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3489 param->lsp_stripe_pattern =
3490 llapi_pattern_to_lov(lsa.lsa_pattern);
3491 if (param->lsp_stripe_pattern == EINVAL) {
3492 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3497 param->lsp_pool = lsa.lsa_pool_name;
3498 param->lsp_is_specific = false;
3499 if (lsa.lsa_nr_tgts > 0) {
3500 if (lsa.lsa_stripe_count > 0 &&
3501 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3502 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3503 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3504 fprintf(stderr, "error: %s: stripe count %lld "
3505 "doesn't match the number of OSTs: %d\n"
3506 , argv[0], lsa.lsa_stripe_count,
3512 param->lsp_is_specific = true;
3513 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3514 memcpy(param->lsp_osts, tgts,
3515 sizeof(*tgts) * lsa.lsa_nr_tgts);
3520 /* generate a layout from a YAML template */
3521 result = lfs_comp_create_from_yaml(template, &layout,
3524 fprintf(stderr, "error: %s: can't create composite "
3525 "layout from template file %s\n",
3529 } else if (from_copy) {
3530 layout = llapi_layout_get_by_path(template, 0);
3531 if (layout == NULL) {
3533 "%s: can't create composite layout from file %s.\n",
3534 progname, template);
3539 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3540 if (migrate_mdt_mode) {
3541 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3542 } else if (migrate_mode) {
3543 result = lfs_migrate(fname, migration_flags, param,
3545 } else if (comp_set != 0) {
3546 result = lfs_component_set(fname, comp_id,
3548 lsa.lsa_comp_neg_flags);
3549 } else if (comp_del != 0) {
3550 result = lfs_component_del(fname, comp_id,
3552 lsa.lsa_comp_neg_flags);
3553 } else if (comp_add != 0) {
3554 result = lfs_component_add(fname, layout);
3555 } else if (opc == SO_MIRROR_CREATE) {
3556 result = mirror_create(fname, mirror_list);
3557 } else if (opc == SO_MIRROR_EXTEND) {
3558 result = mirror_extend(fname, mirror_list,
3560 } else if (opc == SO_MIRROR_SPLIT) {
3561 if (mirror_id == 0 && comp_id == 0) {
3563 "%s %s: no mirror id or component id is specified\n",
3568 comp_id = mirror_id;
3570 mirror_flags |= MF_COMP_ID;
3571 result = mirror_split(fname, comp_id, mirror_flags,
3572 has_m_file ? mirror_list->m_file :
3574 } else if (layout != NULL) {
3575 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3581 } else if (foreign_mode) {
3582 result = llapi_file_create_foreign(fname, mode, type,
3589 result = llapi_file_open_param(fname,
3598 /* Save the first error encountered. */
3605 if (mode_opt != NULL)
3606 umask(previous_umask);
3609 free(migrate_mdt_param.fp_lmv_md);
3610 llapi_layout_free(layout);
3611 lfs_mirror_list_free(mirror_list);
3616 llapi_layout_free(layout);
3617 lfs_mirror_list_free(mirror_list);
3621 static int lfs_poollist(int argc, char **argv)
3626 return llapi_poollist(argv[1]);
3629 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3634 char *endptr = "AD";
3639 else if (str[0] == '-')
3645 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3646 long long val = strtoll(timebuf, &endptr, 0);
3651 unit *= 52; /* 52 weeks + 1 day below */
3652 case 'w': /* fallthrough */
3654 case '\0': /* days are default unit if none used */
3655 case 'd': /* fallthrough */
3656 unit = (unit + (*endptr == 'y')) * 24;
3657 case 'h': /* fallthrough */
3659 case 'm': /* fallthrough */
3661 case 's': /* fallthrough */
3663 /* don't need to multiply by 1 for seconds */
3666 "%s find: bad time string '%s': %s\n",
3667 progname, timebuf, strerror(EINVAL));
3671 if (param->fp_time_margin == 0 ||
3672 (*endptr && unit < param->fp_time_margin))
3673 param->fp_time_margin = unit;
3680 fprintf(stderr, "%s find: bad time '%s': too large\n",
3690 static int name2uid(unsigned int *id, const char *name)
3692 struct passwd *passwd;
3694 passwd = getpwnam(name);
3697 *id = passwd->pw_uid;
3702 static int name2gid(unsigned int *id, const char *name)
3704 struct group *group;
3706 group = getgrnam(name);
3709 *id = group->gr_gid;
3714 static inline int name2projid(unsigned int *id, const char *name)
3719 static int uid2name(char **name, unsigned int id)
3721 struct passwd *passwd;
3723 passwd = getpwuid(id);
3726 *name = passwd->pw_name;
3731 static inline int gid2name(char **name, unsigned int id)
3733 struct group *group;
3735 group = getgrgid(id);
3738 *name = group->gr_name;
3743 static int name2layout(__u32 *layout, char *name)
3745 char *ptr, *layout_name;
3748 for (ptr = name; ; ptr = NULL) {
3749 layout_name = strtok(ptr, ",");
3750 if (layout_name == NULL)
3752 if (strcmp(layout_name, "released") == 0)
3753 *layout |= LOV_PATTERN_F_RELEASED;
3754 else if (strcmp(layout_name, "raid0") == 0)
3755 *layout |= LOV_PATTERN_RAID0;
3756 else if (strcmp(layout_name, "mdt") == 0)
3757 *layout |= LOV_PATTERN_MDT;
3758 else if (strcmp(layout_name, "overstriping") == 0)
3759 *layout |= LOV_PATTERN_OVERSTRIPING;
3766 static int lfs_find(int argc, char **argv)
3771 struct find_param param = {
3774 .fp_time_margin = 24 * 60 * 60,
3776 struct option long_opts[] = {
3777 { .val = 'A', .name = "atime", .has_arg = required_argument },
3778 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3779 { .val = LFS_COMP_COUNT_OPT,
3780 .name = "comp-count", .has_arg = required_argument },
3781 { .val = LFS_COMP_COUNT_OPT,
3782 .name = "component-count",
3783 .has_arg = required_argument },
3784 { .val = LFS_COMP_FLAGS_OPT,
3785 .name = "comp-flags", .has_arg = required_argument },
3786 { .val = LFS_COMP_FLAGS_OPT,
3787 .name = "component-flags",
3788 .has_arg = required_argument },
3789 { .val = LFS_COMP_START_OPT,
3790 .name = "comp-start", .has_arg = required_argument },
3791 { .val = LFS_COMP_START_OPT,
3792 .name = "component-start",
3793 .has_arg = required_argument },
3794 { .val = LFS_MIRROR_STATE_OPT,
3795 .name = "mirror-state", .has_arg = required_argument },
3796 { .val = LFS_LAYOUT_FOREIGN_OPT,
3797 .name = "foreign", .has_arg = optional_argument},
3798 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3799 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3800 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3801 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3802 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3803 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3804 { .val = 'E', .name = "component-end",
3805 .has_arg = required_argument },
3806 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3807 { .val = LFS_LAYOUT_FOREIGN_OPT,
3808 .name = "foreign", .has_arg = optional_argument},
3809 { .val = 'g', .name = "gid", .has_arg = required_argument },
3810 { .val = 'G', .name = "group", .has_arg = required_argument },
3811 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3812 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3813 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3814 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3815 { .val = 'L', .name = "layout", .has_arg = required_argument },
3816 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3817 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3818 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3819 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3820 { .val = 'n', .name = "name", .has_arg = required_argument },
3821 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3822 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3823 { .val = 'O', .name = "obd", .has_arg = required_argument },
3824 { .val = 'O', .name = "ost", .has_arg = required_argument },
3825 /* no short option for pool yet, can be 'p' after 2.18 */
3826 { .val = LFS_POOL_OPT,
3827 .name = "pool", .has_arg = required_argument },
3828 { .val = '0', .name = "print0", .has_arg = no_argument },
3829 { .val = 'P', .name = "print", .has_arg = no_argument },
3830 { .val = LFS_PROJID_OPT,
3831 .name = "projid", .has_arg = required_argument },
3832 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3833 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3834 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3835 { .val = 's', .name = "size", .has_arg = required_argument },
3836 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3837 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3838 { .val = 't', .name = "type", .has_arg = required_argument },
3839 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3840 { .val = 'u', .name = "uid", .has_arg = required_argument },
3841 { .val = 'U', .name = "user", .has_arg = required_argument },
3842 { .val = 'z', .name = "extension-size",
3843 .has_arg = required_argument },
3844 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
3845 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3846 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3858 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3859 while ((c = getopt_long_only(argc, argv,
3860 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
3861 long_opts, NULL)) >= 0) {
3866 /* '!' is part of option */
3867 /* when getopt_long_only() finds a string which is not
3868 * an option nor a known option argument it returns 1
3869 * in that case if we already have found pathstart and pathend
3870 * (i.e. we have the list of pathnames),
3871 * the only supported value is "!"
3873 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3874 if (!isoption && pathend != -1) {
3875 fprintf(stderr, "err: %s: filename|dirname must either "
3876 "precede options or follow options\n",
3881 if (!isoption && pathstart == -1)
3882 pathstart = optind - 1;
3883 if (isoption && pathstart != -1 && pathend == -1)
3884 pathend = optind - 2;
3890 /* unknown; opt is "!" or path component,
3891 * checking done above.
3893 if (strcmp(optarg, "!") == 0)
3897 xtime = ¶m.fp_atime;
3898 xsign = ¶m.fp_asign;
3899 param.fp_exclude_atime = !!neg_opt;
3900 /* no break, this falls through to 'C' for ctime */
3903 xtime = ¶m.fp_ctime;
3904 xsign = ¶m.fp_csign;
3905 param.fp_exclude_ctime = !!neg_opt;
3907 /* no break, this falls through to 'M' for mtime */
3910 xtime = ¶m.fp_mtime;
3911 xsign = ¶m.fp_msign;
3912 param.fp_exclude_mtime = !!neg_opt;
3914 rc = set_time(¶m, &t, xtime, optarg);
3915 if (rc == LONG_MAX) {
3923 if (optarg[0] == '+') {
3924 param.fp_blocks_sign = -1;
3926 } else if (optarg[0] == '-') {
3927 param.fp_blocks_sign = 1;
3931 param.fp_blocks_units = 1024;
3932 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3933 ¶m.fp_blocks_units, 0);
3935 fprintf(stderr, "error: bad blocks '%s'\n",
3939 param.fp_check_blocks = 1;
3940 param.fp_exclude_blocks = !!neg_opt;
3942 case LFS_COMP_COUNT_OPT:
3943 if (optarg[0] == '+') {
3944 param.fp_comp_count_sign = -1;
3946 } else if (optarg[0] == '-') {
3947 param.fp_comp_count_sign = 1;
3951 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3952 if (*endptr != '\0') {
3953 fprintf(stderr, "error: bad component count "
3957 param.fp_check_comp_count = 1;
3958 param.fp_exclude_comp_count = !!neg_opt;
3960 case LFS_COMP_FLAGS_OPT:
3961 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3962 ¶m.fp_comp_neg_flags);
3964 fprintf(stderr, "error: bad component flags "
3968 param.fp_check_comp_flags = 1;
3970 __u32 flags = param.fp_comp_neg_flags;
3971 param.fp_comp_neg_flags = param.fp_comp_flags;
3972 param.fp_comp_flags = flags;
3975 case LFS_COMP_START_OPT:
3976 if (optarg[0] == '+') {
3977 param.fp_comp_start_sign = -1;
3979 } else if (optarg[0] == '-') {
3980 param.fp_comp_start_sign = 1;
3984 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3985 ¶m.fp_comp_start_units, 0);
3987 fprintf(stderr, "error: bad component start "
3991 param.fp_check_comp_start = 1;
3992 param.fp_exclude_comp_start = !!neg_opt;
3994 case LFS_MIRROR_STATE_OPT:
3995 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3996 ¶m.fp_mirror_neg_state);
3999 "error: bad mirrored file state '%s'\n",
4003 param.fp_check_mirror_state = 1;
4005 __u16 state = param.fp_mirror_neg_state;
4006 param.fp_mirror_neg_state =
4007 param.fp_mirror_state;
4008 param.fp_mirror_state = state;
4012 if (optarg[0] == '+') {
4013 param.fp_stripe_count_sign = -1;
4015 } else if (optarg[0] == '-') {
4016 param.fp_stripe_count_sign = 1;
4020 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4021 if (*endptr != '\0') {
4022 fprintf(stderr,"error: bad stripe_count '%s'\n",
4027 param.fp_check_stripe_count = 1;
4028 param.fp_exclude_stripe_count = !!neg_opt;
4031 param.fp_max_depth = strtol(optarg, 0, 0);
4034 if (optarg[0] == '+') {
4035 param.fp_comp_end_sign = -1;
4037 } else if (optarg[0] == '-') {
4038 param.fp_comp_end_sign = 1;
4042 if (arg_is_eof(optarg)) {
4043 param.fp_comp_end = LUSTRE_EOF;
4044 param.fp_comp_end_units = 1;
4047 rc = llapi_parse_size(optarg,
4049 ¶m.fp_comp_end_units, 0);
4052 fprintf(stderr, "error: bad component end "
4056 param.fp_check_comp_end = 1;
4057 param.fp_exclude_comp_end = !!neg_opt;
4059 case LFS_LAYOUT_FOREIGN_OPT: {
4060 /* all types by default */
4061 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4063 if (optarg != NULL) {
4064 /* check pure numeric */
4065 type = strtoul(optarg, &endptr, 0);
4068 type = check_foreign_type_name(optarg);
4069 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4071 "%s %s: unknown foreign type '%s'\n",
4078 param.fp_foreign_type = type;
4079 param.fp_check_foreign = 1;
4080 param.fp_exclude_foreign = !!neg_opt;
4085 rc = name2gid(¶m.fp_gid, optarg);
4087 param.fp_gid = strtoul(optarg, &endptr, 10);
4088 if (*endptr != '\0') {
4089 fprintf(stderr, "Group/GID: %s cannot "
4090 "be found.\n", optarg);
4095 param.fp_exclude_gid = !!neg_opt;
4096 param.fp_check_gid = 1;
4099 param.fp_hash_type = check_hashtype(optarg);
4100 if (param.fp_hash_type == 0) {
4101 fprintf(stderr, "error: bad hash_type '%s'\n",
4106 param.fp_check_hash_type = 1;
4107 param.fp_exclude_hash_type = !!neg_opt;
4110 ret = name2layout(¶m.fp_layout, optarg);
4113 param.fp_exclude_layout = !!neg_opt;
4114 param.fp_check_layout = 1;
4118 rc = name2uid(¶m.fp_uid, optarg);
4120 param.fp_uid = strtoul(optarg, &endptr, 10);
4121 if (*endptr != '\0') {
4122 fprintf(stderr, "User/UID: %s cannot "
4123 "be found.\n", optarg);
4128 param.fp_exclude_uid = !!neg_opt;
4129 param.fp_check_uid = 1;
4132 param.fp_pattern = (char *)optarg;
4133 param.fp_exclude_pattern = !!neg_opt;
4136 if (optarg[0] == '+') {
4137 param.fp_mirror_count_sign = -1;
4139 } else if (optarg[0] == '-') {
4140 param.fp_mirror_count_sign = 1;
4144 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4145 if (*endptr != '\0') {
4147 "error: bad mirror count '%s'\n",
4151 param.fp_check_mirror_count = 1;
4152 param.fp_exclude_mirror_count = !!neg_opt;
4157 char *buf, *token, *next, *p;
4161 buf = strdup(optarg);
4167 param.fp_exclude_obd = !!neg_opt;
4170 while (token && *token) {
4171 token = strchr(token, ',');
4178 param.fp_exclude_mdt = !!neg_opt;
4179 param.fp_num_alloc_mdts += len;
4180 tmp = realloc(param.fp_mdt_uuid,
4181 param.fp_num_alloc_mdts *
4182 sizeof(*param.fp_mdt_uuid));
4188 param.fp_mdt_uuid = tmp;
4190 param.fp_exclude_obd = !!neg_opt;
4191 param.fp_num_alloc_obds += len;
4192 tmp = realloc(param.fp_obd_uuid,
4193 param.fp_num_alloc_obds *
4194 sizeof(*param.fp_obd_uuid));
4200 param.fp_obd_uuid = tmp;
4202 for (token = buf; token && *token; token = next) {
4203 struct obd_uuid *puuid;
4206 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4209 ¶m.fp_obd_uuid[param.fp_num_obds++];
4211 p = strchr(token, ',');
4218 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4223 strncpy(puuid->uuid, token,
4224 sizeof(puuid->uuid));
4231 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4235 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4237 "Pool name %s is too long (max %d)\n",
4238 optarg, LOV_MAXPOOLNAME);
4243 * We do check for empty pool because empty pool
4244 * is used to find V1 LOV attributes
4246 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4247 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4248 param.fp_exclude_pool = !!neg_opt;
4249 param.fp_check_pool = 1;
4251 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4252 case 'p': /* want this for --pool, to match getstripe/find */
4254 "warning: -p deprecated, use --print0 or -0\n");
4257 param.fp_zero_end = 1;
4259 case 'P': /* we always print, this option is a no-op */
4261 case LFS_PROJID_OPT:
4262 rc = name2projid(¶m.fp_projid, optarg);
4264 param.fp_projid = strtoul(optarg, &endptr, 10);
4265 if (*endptr != '\0') {
4267 "Invalid project ID: %s",
4273 param.fp_exclude_projid = !!neg_opt;
4274 param.fp_check_projid = 1;
4277 if (optarg[0] == '+') {
4278 param.fp_size_sign = -1;
4280 } else if (optarg[0] == '-') {
4281 param.fp_size_sign = 1;
4285 ret = llapi_parse_size(optarg, ¶m.fp_size,
4286 ¶m.fp_size_units, 0);
4288 fprintf(stderr, "error: bad file size '%s'\n",
4292 param.fp_check_size = 1;
4293 param.fp_exclude_size = !!neg_opt;
4296 if (optarg[0] == '+') {
4297 param.fp_stripe_size_sign = -1;
4299 } else if (optarg[0] == '-') {
4300 param.fp_stripe_size_sign = 1;
4304 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4305 ¶m.fp_stripe_size_units, 0);
4307 fprintf(stderr, "error: bad stripe_size '%s'\n",
4311 param.fp_check_stripe_size = 1;
4312 param.fp_exclude_stripe_size = !!neg_opt;
4315 param.fp_exclude_type = !!neg_opt;
4316 switch (optarg[0]) {
4318 param.fp_type = S_IFBLK;
4321 param.fp_type = S_IFCHR;
4324 param.fp_type = S_IFDIR;
4327 param.fp_type = S_IFREG;
4330 param.fp_type = S_IFLNK;
4333 param.fp_type = S_IFIFO;
4336 param.fp_type = S_IFSOCK;
4339 fprintf(stderr, "error: %s: bad type '%s'\n",
4346 if (optarg[0] == '+') {
4347 param.fp_mdt_count_sign = -1;
4349 } else if (optarg[0] == '-') {
4350 param.fp_mdt_count_sign = 1;
4354 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4355 if (*endptr != '\0') {
4356 fprintf(stderr, "error: bad mdt_count '%s'\n",
4361 param.fp_check_mdt_count = 1;
4362 param.fp_exclude_mdt_count = !!neg_opt;
4365 if (optarg[0] == '+') {
4366 param.fp_ext_size_sign = -1;
4368 } else if (optarg[0] == '-') {
4369 param.fp_ext_size_sign = 1;
4373 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4374 ¶m.fp_ext_size_units, 0);
4376 fprintf(stderr, "error: bad ext-size '%s'\n",
4380 param.fp_ext_size /= SEL_UNIT_SIZE;
4381 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4382 param.fp_check_ext_size = 1;
4383 param.fp_exclude_ext_size = !!neg_opt;
4392 if (pathstart == -1) {
4393 fprintf(stderr, "error: %s: no filename|pathname\n",
4397 } else if (pathend == -1) {
4403 rc = llapi_find(argv[pathstart], ¶m);
4404 if (rc != 0 && ret == 0)
4406 } while (++pathstart < pathend);
4409 fprintf(stderr, "error: %s failed for %s.\n",
4410 argv[0], argv[optind - 1]);
4412 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4413 free(param.fp_obd_uuid);
4415 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4416 free(param.fp_mdt_uuid);
4421 static int lfs_getstripe_internal(int argc, char **argv,
4422 struct find_param *param)
4424 struct option long_opts[] = {
4425 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4426 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4427 { .val = LFS_COMP_COUNT_OPT,
4428 .name = "comp-count", .has_arg = no_argument },
4429 { .val = LFS_COMP_COUNT_OPT,
4430 .name = "component-count", .has_arg = no_argument },
4431 { .val = LFS_COMP_FLAGS_OPT,
4432 .name = "comp-flags", .has_arg = optional_argument },
4433 { .val = LFS_COMP_FLAGS_OPT,
4434 .name = "component-flags", .has_arg = optional_argument },
4435 { .val = LFS_COMP_START_OPT,
4436 .name = "comp-start", .has_arg = optional_argument },
4437 { .val = LFS_COMP_START_OPT,
4438 .name = "component-start", .has_arg = optional_argument },
4439 { .val = LFS_MIRROR_INDEX_OPT,
4440 .name = "mirror-index", .has_arg = required_argument },
4441 { .val = LFS_MIRROR_ID_OPT,
4442 .name = "mirror-id", .has_arg = required_argument },
4443 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4444 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4445 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4446 { .val = 'd', .name = "directory", .has_arg = no_argument },
4447 { .val = 'D', .name = "default", .has_arg = no_argument },
4448 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4449 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4450 { .val = 'F', .name = "fid", .has_arg = no_argument },
4451 { .val = 'g', .name = "generation", .has_arg = no_argument },
4452 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4453 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4454 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4455 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4456 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4457 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4458 { .val = 'L', .name = "layout", .has_arg = no_argument },
4459 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4460 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4461 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4462 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4463 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4464 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4465 { .val = 'O', .name = "obd", .has_arg = required_argument },
4466 { .val = 'O', .name = "ost", .has_arg = required_argument },
4467 { .val = 'p', .name = "pool", .has_arg = no_argument },
4468 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4469 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4470 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4471 { .val = 'R', .name = "raw", .has_arg = no_argument },
4472 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4473 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4474 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4475 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4476 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4477 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4478 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4479 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4480 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
4481 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
4485 int pathstart = -1, pathend = -1;
4489 while ((c = getopt_long(argc, argv,
4490 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
4491 long_opts, NULL)) != -1) {
4495 /* '!' is part of option */
4496 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4497 if (!isoption && pathend != -1) {
4499 "error: %s: filename|dirname must either precede options or follow options\n",
4503 if (!isoption && pathstart == -1)
4504 pathstart = optind - 1;
4505 if (isoption && pathstart != -1 && pathend == -1)
4506 pathend = optind - 2;
4510 /* unknown: opt is "!" */
4511 if (strcmp(optarg, "!") == 0)
4515 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4516 param->fp_verbose |= VERBOSE_COUNT;
4517 param->fp_max_depth = 0;
4520 case LFS_COMP_COUNT_OPT:
4521 param->fp_verbose |= VERBOSE_COMP_COUNT;
4522 param->fp_max_depth = 0;
4524 case LFS_COMP_FLAGS_OPT:
4525 if (optarg != NULL) {
4526 rc = comp_str2flags(optarg,
4527 ¶m->fp_comp_flags,
4528 ¶m->fp_comp_neg_flags);
4530 fprintf(stderr, "error: %s bad "
4531 "component flags '%s'.\n",
4535 param->fp_check_comp_flags = 1;
4537 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4538 param->fp_max_depth = 0;
4541 case LFS_COMP_START_OPT:
4542 if (optarg != NULL) {
4544 if (tmp[0] == '+') {
4545 param->fp_comp_start_sign = -1;
4547 } else if (tmp[0] == '-') {
4548 param->fp_comp_start_sign = 1;
4551 rc = llapi_parse_size(tmp,
4552 ¶m->fp_comp_start,
4553 ¶m->fp_comp_start_units, 0);
4555 fprintf(stderr, "error: %s bad "
4556 "component start '%s'.\n",
4560 param->fp_check_comp_start = 1;
4563 param->fp_verbose |= VERBOSE_COMP_START;
4564 param->fp_max_depth = 0;
4567 case LFS_MIRROR_INDEX_OPT:
4568 if (optarg[0] == '+') {
4569 param->fp_mirror_index_sign = -1;
4571 } else if (optarg[0] == '-') {
4572 param->fp_mirror_index_sign = 1;
4576 param->fp_mirror_index = strtoul(optarg, &end, 0);
4577 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4578 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4580 "%s %s: invalid mirror index '%s'\n",
4581 progname, argv[0], optarg);
4584 if (param->fp_mirror_id != 0) {
4586 "%s %s: can't specify both mirror index and mirror ID\n",
4590 param->fp_check_mirror_index = 1;
4591 param->fp_exclude_mirror_index = !!neg_opt;
4593 case LFS_MIRROR_ID_OPT:
4594 if (optarg[0] == '+') {
4595 param->fp_mirror_id_sign = -1;
4597 } else if (optarg[0] == '-') {
4598 param->fp_mirror_id_sign = 1;
4602 param->fp_mirror_id = strtoul(optarg, &end, 0);
4603 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4604 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4606 "%s %s: invalid mirror ID '%s'\n",
4607 progname, argv[0], optarg);
4610 if (param->fp_mirror_index != 0) {
4612 "%s %s: can't specify both mirror index and mirror ID\n",
4616 param->fp_check_mirror_id = 1;
4617 param->fp_exclude_mirror_id = !!neg_opt;
4620 param->fp_max_depth = 0;
4623 param->fp_get_default_lmv = 1;
4626 if (optarg != NULL) {
4628 if (tmp[0] == '+') {
4629 param->fp_comp_end_sign = -1;
4631 } else if (tmp[0] == '-') {
4632 param->fp_comp_end_sign = 1;
4636 if (arg_is_eof(tmp)) {
4637 param->fp_comp_end = LUSTRE_EOF;
4638 param->fp_comp_end_units = 1;
4641 rc = llapi_parse_size(tmp,
4642 ¶m->fp_comp_end,
4643 ¶m->fp_comp_end_units, 0);
4646 fprintf(stderr, "error: %s bad "
4647 "component end '%s'.\n",
4651 param->fp_check_comp_end = 1;
4653 param->fp_verbose |= VERBOSE_COMP_END;
4654 param->fp_max_depth = 0;
4658 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4659 param->fp_verbose |= VERBOSE_DFID;
4660 param->fp_max_depth = 0;
4664 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4665 param->fp_verbose |= VERBOSE_GENERATION;
4666 param->fp_max_depth = 0;
4670 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4671 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4672 param->fp_max_depth = 0;
4676 if (optarg != NULL) {
4677 param->fp_comp_id = strtoul(optarg, &end, 0);
4678 if (*end != '\0' || param->fp_comp_id == 0 ||
4679 param->fp_comp_id > LCME_ID_MAX) {
4680 fprintf(stderr, "error: %s bad "
4681 "component id '%s'\n",
4685 param->fp_check_comp_id = 1;
4688 param->fp_max_depth = 0;
4689 param->fp_verbose |= VERBOSE_COMP_ID;
4693 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4694 param->fp_verbose |= VERBOSE_PATTERN;
4695 param->fp_max_depth = 0;
4698 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4700 fprintf(stderr, "warning: '-M' deprecated"
4701 ", use '--mdt-index' or '-m' instead\n");
4704 if (!(param->fp_verbose & VERBOSE_DETAIL))
4705 param->fp_max_depth = 0;
4706 param->fp_verbose |= VERBOSE_MDTINDEX;
4709 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4710 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4711 param->fp_max_depth = 0;
4715 if (param->fp_obd_uuid) {
4717 "error: %s: only one obduuid allowed",
4721 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4724 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4725 param->fp_verbose |= VERBOSE_POOL;
4726 param->fp_max_depth = 0;
4733 param->fp_recursive = 1;
4739 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4740 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4741 param->fp_max_depth = 0;
4745 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4751 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4752 param->fp_verbose |= VERBOSE_EXT_SIZE;
4753 param->fp_max_depth = 0;
4761 if (pathstart == -1) {
4762 fprintf(stderr, "error: %s: no filename|pathname\n",
4765 } else if (pathend == -1) {
4773 if (param->fp_recursive)
4774 param->fp_max_depth = -1;
4775 else if (param->fp_verbose & VERBOSE_DETAIL)
4776 param->fp_max_depth = 1;
4778 if (!param->fp_verbose)
4779 param->fp_verbose = VERBOSE_DEFAULT;
4780 if (param->fp_quiet)
4781 param->fp_verbose = VERBOSE_OBJID;
4784 rc = llapi_getstripe(argv[pathstart], param);
4785 } while (++pathstart < pathend && !rc);
4788 fprintf(stderr, "error: %s failed for %s.\n",
4789 argv[0], argv[optind - 1]);
4793 static int lfs_tgts(int argc, char **argv)
4795 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4796 struct find_param param;
4797 int index = 0, rc=0;
4802 if (argc == 2 && !realpath(argv[1], path)) {
4804 fprintf(stderr, "error: invalid path '%s': %s\n",
4805 argv[1], strerror(-rc));
4809 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4810 /* Check if we have a mount point */
4811 if (mntdir[0] == '\0')
4814 memset(¶m, 0, sizeof(param));
4815 if (!strcmp(argv[0], "mdts"))
4816 param.fp_get_lmv = 1;
4818 rc = llapi_ostlist(mntdir, ¶m);
4820 fprintf(stderr, "error: %s: failed on %s\n",
4823 if (path[0] != '\0')
4825 memset(mntdir, 0, PATH_MAX);
4831 static int lfs_getstripe(int argc, char **argv)
4833 struct find_param param = { 0 };
4835 param.fp_max_depth = 1;
4836 return lfs_getstripe_internal(argc, argv, ¶m);
4840 static int lfs_getdirstripe(int argc, char **argv)
4842 struct find_param param = { 0 };
4843 struct option long_opts[] = {
4844 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4845 { .val = 'D', .name = "default", .has_arg = no_argument },
4846 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4847 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4848 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4849 { .val = 'O', .name = "obd", .has_arg = required_argument },
4850 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4851 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4852 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4853 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4857 param.fp_get_lmv = 1;
4859 while ((c = getopt_long(argc, argv,
4860 "cDHimO:rtTvy", long_opts, NULL)) != -1)
4865 param.fp_verbose |= VERBOSE_COUNT;
4868 param.fp_get_default_lmv = 1;
4872 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4874 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4876 fprintf(stderr, "warning: '-t' deprecated, "
4877 "use '--mdt-hash' or '-H' instead\n");
4880 param.fp_verbose |= VERBOSE_HASH_TYPE;
4883 if (param.fp_obd_uuid) {
4885 "error: %s: only one obduuid allowed",
4889 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4892 param.fp_recursive = 1;
4895 param.fp_verbose |= VERBOSE_DETAIL;
4901 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4902 progname, argv[0], argv[optind - 1]);
4910 if (param.fp_recursive)
4911 param.fp_max_depth = -1;
4913 if (!param.fp_verbose)
4914 param.fp_verbose = VERBOSE_DEFAULT;
4917 rc = llapi_getstripe(argv[optind], ¶m);
4918 } while (++optind < argc && !rc);
4921 fprintf(stderr, "error: %s failed for %s.\n",
4922 argv[0], argv[optind - 1]);
4927 MNTDF_INODES = 0x0001,
4928 MNTDF_COOKED = 0x0002,
4929 MNTDF_LAZY = 0x0004,
4930 MNTDF_VERBOSE = 0x0008,
4931 MNTDF_SHOW = 0x0010,
4934 #define COOK(value) \
4937 while (value > 1024) { \
4945 #define CDF "%11llu"
4946 #define HDF "%8.1f%c"
4950 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4952 double avail, used, ratio = 0;
4955 avail = st->os_ffree;
4956 used = st->os_files - st->os_ffree;
4958 avail = st->os_bavail;
4959 used = st->os_blocks - st->os_bfree;
4961 if (avail + used > 0)
4962 ratio = used / (used + avail) * 100;
4964 /* Round up to match df(1) usage percentage */
4965 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4968 /* This is to identify various problem states for "lfs df" if .osn_err = true,
4969 * so only show flags reflecting those states by default. Informational states
4970 * are only shown with "-v" and use lower-case names to distinguish them.
4971 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
4973 static struct obd_statfs_state_names {
4974 enum obd_statfs_state osn_state;
4975 const char osn_name;
4978 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
4979 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
4980 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
4981 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
4982 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
4983 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
4984 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
4985 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
4986 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
4989 static int showdf(char *mntdir, struct obd_statfs *stat,
4990 char *uuid, enum mntdf_flags flags,
4991 char *type, int index, int rc)
4993 long long avail, used, total;
4995 char *suffix = "KMGTPEZY";
4996 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4997 char tbuf[3 * sizeof(__u64)];
4998 char ubuf[3 * sizeof(__u64)];
4999 char abuf[3 * sizeof(__u64)];
5000 char rbuf[3 * sizeof(__u64)];
5007 if (flags & MNTDF_INODES) {
5008 avail = stat->os_ffree;
5009 used = stat->os_files - stat->os_ffree;
5010 total = stat->os_files;
5012 int shift = flags & MNTDF_COOKED ? 0 : 10;
5014 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5015 used = ((stat->os_blocks - stat->os_bfree) *
5016 stat->os_bsize) >> shift;
5017 total = (stat->os_blocks * stat->os_bsize) >> shift;
5020 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5022 if (flags & MNTDF_COOKED) {
5026 cook_val = (double)total;
5029 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5032 snprintf(tbuf, sizeof(tbuf), CDF, total);
5034 cook_val = (double)used;
5037 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5040 snprintf(ubuf, sizeof(ubuf), CDF, used);
5042 cook_val = (double)avail;
5045 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5048 snprintf(abuf, sizeof(abuf), CDF, avail);
5050 snprintf(tbuf, sizeof(tbuf), CDF, total);
5051 snprintf(ubuf, sizeof(tbuf), CDF, used);
5052 snprintf(abuf, sizeof(tbuf), CDF, avail);
5055 sprintf(rbuf, RDF, ratio);
5056 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5057 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5059 printf("[%s:%d]", type, index);
5061 if (stat->os_state) {
5065 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5066 if (oss_names[i].osn_state & stat->os_state &&
5067 (oss_names[i].osn_err ||
5068 flags & MNTDF_VERBOSE))
5069 printf("%c", oss_names[i].osn_name);
5076 printf(UUF": inactive device\n", uuid);
5079 printf(UUF": %s\n", uuid, strerror(-rc));
5086 struct ll_stat_type {
5091 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5093 struct ll_statfs_data {
5095 struct obd_statfs sd_st;
5098 struct ll_statfs_buf {
5100 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5103 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5104 int ops, struct ll_statfs_buf *lsb)
5106 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5107 struct obd_uuid uuid_buf;
5108 char *poolname = NULL;
5109 struct ll_stat_type types[] = {
5110 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5111 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5112 { .st_name = NULL } };
5113 struct ll_stat_type *tp;
5114 __u64 ost_files = 0;
5115 __u64 ost_ffree = 0;
5123 poolname = strchr(pool, '.');
5124 if (poolname != NULL) {
5125 if (strncmp(fsname, pool, strlen(fsname))) {
5126 fprintf(stderr, "filesystem name incorrect\n");
5134 fd = open(mntdir, O_RDONLY);
5137 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5142 if (flags & MNTDF_SHOW) {
5143 if (flags & MNTDF_INODES)
5144 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5145 "UUID", "Inodes", "IUsed", "IFree",
5146 "IUse%", "Mounted on");
5148 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5150 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5151 "Used", "Available", "Use%", "Mounted on");
5154 for (tp = types; tp->st_name != NULL; tp++) {
5155 bool have_ost = false;
5157 if (!(tp->st_op & ops))
5160 for (index = 0; ; index++) {
5161 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5162 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5163 type = flags & MNTDF_LAZY ?
5164 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5165 rc2 = llapi_obd_fstatfs(fd, type, index,
5166 &stat_buf, &uuid_buf);
5171 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5172 if (!(flags & MNTDF_VERBOSE))
5174 } else if (rc2 < 0 && rc == 0) {
5178 /* If we have OSTs then don't report MDT block counts.
5179 * For MDT-only filesystems the expectation is that all
5180 * layouts have a DoM component. For filesystems with
5181 * OSTs, files are not necessarily going to store data
5182 * on MDTs, and MDT space is limited to a fraction of
5183 * OST space, so don't include it in the summary.
5185 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5192 if (poolname && tp->st_op == LL_STATFS_LOV &&
5193 llapi_search_ost(fsname, poolname,
5194 obd_uuid2str(&uuid_buf)) != 1)
5197 /* the llapi_obd_statfs() call may have returned with
5198 * an error, but if it filled in uuid_buf we will at
5199 * lease use that to print out a message for that OBD.
5200 * If we didn't get anything in the uuid_buf, then fill
5201 * it in so that we can print an error message. */
5202 if (uuid_buf.uuid[0] == '\0')
5203 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5204 "%s%04x", tp->st_name, index);
5206 lsb->sb_buf[lsb->sb_count].sd_index = index;
5207 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5210 if (flags & MNTDF_SHOW)
5211 showdf(mntdir, &stat_buf,
5212 obd_uuid2str(&uuid_buf), flags,
5213 tp->st_name, index, rc2);
5218 if (tp->st_op == LL_STATFS_LMV) {
5219 sum.os_ffree += stat_buf.os_ffree;
5220 sum.os_files += stat_buf.os_files;
5221 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5222 ost_files += stat_buf.os_files;
5223 ost_ffree += stat_buf.os_ffree;
5225 sum.os_blocks += stat_buf.os_blocks *
5227 sum.os_bfree += stat_buf.os_bfree *
5229 sum.os_bavail += stat_buf.os_bavail *
5236 /* If we have _some_ OSTs, but don't have as many free objects on the
5237 * OST as inodes on the MDTs, reduce the reported number of inodes
5238 * to compensate, so that the "inodes in use" number is correct.
5239 * This should be kept in sync with ll_statfs_internal().
5241 if (ost_files && ost_ffree < sum.os_ffree) {
5242 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5243 sum.os_ffree = ost_ffree;
5245 if (flags & MNTDF_SHOW) {
5247 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5254 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5256 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5258 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5260 int r1 = obd_statfs_ratio(st1, false);
5261 int r2 = obd_statfs_ratio(st2, false);
5262 int64_t result = r1 - r2;
5264 /* if both space usage are above 90, compare free inodes */
5265 if (r1 > 90 && r2 > 90)
5266 result = st2->os_ffree - st1->os_ffree;
5270 else if (result == 0)
5277 static int lfs_setdirstripe(int argc, char **argv)
5280 struct lfs_setstripe_args lsa = { 0 };
5281 struct llapi_stripe_param *param = NULL;
5282 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5285 char *mode_opt = NULL;
5286 bool default_stripe = false;
5287 bool delete = false;
5288 bool auto_distributed = false;
5289 bool foreign_mode = false;
5290 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5291 mode_t previous_mode = 0;
5292 struct ll_statfs_buf *lsb = NULL;
5293 char mntdir[PATH_MAX] = "";
5295 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5296 struct option long_opts[] = {
5297 { .val = 'c', .name = "count", .has_arg = required_argument },
5298 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5299 { .val = 'd', .name = "delete", .has_arg = no_argument },
5300 { .val = 'D', .name = "default", .has_arg = no_argument },
5301 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5302 { .val = LFS_LAYOUT_FLAGS_OPT,
5303 .name = "flags", .has_arg = required_argument },
5304 { .val = LFS_LAYOUT_FOREIGN_OPT,
5305 .name = "foreign", .has_arg = optional_argument},
5306 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5307 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5308 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5309 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5311 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5312 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5314 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5315 { .val = 'i', .name = "index", .has_arg = required_argument },
5317 { .val = 'o', .name = "mode", .has_arg = required_argument },
5318 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5319 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5321 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5322 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5323 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5327 setstripe_args_init(&lsa);
5329 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5330 long_opts, NULL)) >= 0) {
5337 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5340 "%s %s: invalid stripe count '%s'\n",
5341 progname, argv[0], optarg);
5347 default_stripe = true;
5350 default_stripe = true;
5352 case LFS_LAYOUT_FOREIGN_OPT:
5353 if (optarg != NULL) {
5354 /* check pure numeric */
5355 type = strtoul(optarg, &end, 0);
5358 type = check_foreign_type_name(optarg);
5359 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5361 "%s %s: unknown foreign type '%s'\n",
5368 foreign_mode = true;
5370 case LFS_LAYOUT_FLAGS_OPT:
5371 flags = strtoul(optarg, &end, 16);
5374 "%s %s: bad flags '%s'\n",
5375 progname, argv[0], optarg);
5379 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5381 fprintf(stderr, "warning: '--hash-type' and '-t' "
5382 "deprecated, use '--mdt-hash' or '-H' instead\n");
5385 lsa.lsa_pattern = check_hashtype(optarg);
5386 if (lsa.lsa_pattern == 0) {
5388 "%s %s: bad stripe hash type '%s'\n",
5389 progname, argv[0], optarg);
5394 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5397 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5398 if (strcmp(argv[optind - 1], "--index") == 0)
5400 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5403 lsa.lsa_nr_tgts = parse_targets(mdts,
5404 sizeof(mdts) / sizeof(__u32),
5405 lsa.lsa_nr_tgts, optarg, NULL);
5406 if (lsa.lsa_nr_tgts < 0) {
5408 "%s %s: invalid MDT target(s) '%s'\n",
5409 progname, argv[0], optarg);
5413 lsa.lsa_tgts = mdts;
5414 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5415 lsa.lsa_stripe_off = mdts[0];
5417 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5419 fprintf(stderr, "warning: '-m' is deprecated, "
5420 "use '--mode' or '-o' instead\n");
5429 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5430 progname, argv[0], argv[optind - 1]);
5435 if (optind == argc) {
5436 fprintf(stderr, "%s %s: DIR must be specified\n",
5441 if (xattr && !foreign_mode) {
5442 /* only print a warning as this is armless and will be
5446 "%s %s: xattr has been specified for non-foreign layout\n",
5448 } else if (foreign_mode && !xattr) {
5450 "%s %s: xattr must be provided in foreign mode\n",
5455 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5456 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5458 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5463 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5464 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5466 "%s %s: stripe offset and count must be specified\n",
5472 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5473 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5475 "%s %s: cannot specify -d with -c or -i options\n",
5480 if (mode_opt != NULL) {
5481 mode = strtoul(mode_opt, &end, 8);
5484 "%s %s: bad MODE '%s'\n",
5485 progname, argv[0], mode_opt);
5488 previous_mode = umask(0);
5491 /* foreign LMV/dir case */
5493 if (argc > optind + 1) {
5495 "%s %s: cannot specify multiple foreign dirs\n",
5500 dname = argv[optind];
5501 result = llapi_dir_create_foreign(dname, mode, type, flags,
5505 "%s mkdir: can't create foreign dir '%s': %s\n",
5506 progname, dname, strerror(-result));
5511 * initialize stripe parameters, in case param is converted to specific,
5512 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5514 param = calloc(1, offsetof(typeof(*param),
5515 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5516 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5517 if (param == NULL) {
5519 "%s %s: cannot allocate memory for parameters: %s\n",
5520 progname, argv[0], strerror(ENOMEM));
5524 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5525 param->lsp_stripe_count = lsa.lsa_stripe_count;
5526 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5527 param->lsp_stripe_offset = -1;
5529 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5530 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5531 param->lsp_stripe_pattern = lsa.lsa_pattern;
5533 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
5534 param->lsp_pool = lsa.lsa_pool_name;
5535 param->lsp_is_specific = false;
5536 if (lsa.lsa_nr_tgts > 1) {
5537 if (lsa.lsa_stripe_count > 0 &&
5538 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5539 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5540 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5541 "match the number of MDTs: %d\n",
5542 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5547 param->lsp_is_specific = true;
5548 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5549 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5552 if (!default_stripe && (lsa.lsa_pattern & LMV_HASH_FLAG_SPACE)) {
5553 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5559 if (param->lsp_stripe_offset != -1 &&
5560 lsa.lsa_pattern & LMV_HASH_FLAG_SPACE) {
5561 fprintf(stderr, "%s %s: can only specify -H space with -i -1\n",
5567 dname = argv[optind];
5569 if (default_stripe) {
5570 result = llapi_dir_set_default_lmv(dname, param);
5573 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5574 progname, dname, strerror(-result));
5579 * if current \a dname isn't under the same \a mntdir as the
5580 * last one, and the last one was auto-distributed, restore
5583 if (mntdir[0] != '\0' &&
5584 strncmp(dname, mntdir, strlen(mntdir)) &&
5586 param->lsp_is_specific = false;
5587 param->lsp_stripe_offset = -1;
5588 auto_distributed = false;
5592 * TODO: when MDT can allocate object with QoS (LU-9435), below
5593 * code should be removed, instead we should let LMV to allocate
5594 * the starting MDT object, and then let LOD allocate other MDT
5597 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5598 char path[PATH_MAX] = "";
5601 lsb = malloc(sizeof(*lsb));
5609 /* use mntdir for dirname() temporarily */
5610 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5611 if (!realpath(dirname(mntdir), path)) {
5614 "error: invalid path '%s': %s\n",
5615 argv[optind], strerror(errno));
5620 result = llapi_search_mounts(path, 0, mntdir, NULL);
5621 if (result < 0 || mntdir[0] == '\0') {
5623 "No suitable Lustre mount found\n");
5627 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5632 if (param->lsp_stripe_count > lsb->sb_count) {
5634 "error: stripe count %d is too big\n",
5635 param->lsp_stripe_count);
5640 qsort(lsb->sb_buf, lsb->sb_count,
5641 sizeof(struct ll_statfs_data),
5642 ll_statfs_data_comp);
5644 auto_distributed = true;
5647 if (auto_distributed) {
5649 int nr = MAX(param->lsp_stripe_count,
5652 /* don't use server whose usage is above 90% */
5653 while (nr != param->lsp_stripe_count &&
5654 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5656 nr = MAX(param->lsp_stripe_count, nr / 2);
5658 /* get \a r between [0, nr) */
5661 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5662 if (param->lsp_stripe_count > 1) {
5665 param->lsp_is_specific = true;
5666 for (; i < param->lsp_stripe_count; i++)
5667 param->lsp_tgts[(i + r) % nr] =
5668 lsb->sb_buf[i].sd_index;
5672 result = llapi_dir_create(dname, mode, param);
5675 "%s setdirstripe: cannot create dir '%s': %s\n",
5676 progname, dname, strerror(-result));
5677 } while (!result && (dname = argv[++optind]));
5679 if (mode_opt != NULL)
5680 umask(previous_mode);
5688 static int lfs_rmentry(int argc, char **argv)
5695 fprintf(stderr, "error: %s: missing dirname\n",
5701 dname = argv[index];
5702 while (dname != NULL) {
5703 result = llapi_direntry_remove(dname);
5705 fprintf(stderr, "error: %s: remove dir entry '%s' "
5706 "failed\n", argv[0], dname);
5709 dname = argv[++index];
5714 static int lfs_mv(int argc, char **argv)
5716 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5717 struct find_param param = {
5724 struct option long_opts[] = {
5725 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5726 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5727 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5730 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5732 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5734 fprintf(stderr, "warning: '-M' deprecated"
5735 ", use '--mdt-index' or '-m' instead\n");
5738 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5740 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5746 param.fp_verbose = VERBOSE_DETAIL;
5749 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5750 progname, argv[optind - 1]);
5755 if (lmu.lum_stripe_offset == -1) {
5756 fprintf(stderr, "%s mv: MDT index must be specified\n",
5761 if (optind >= argc) {
5762 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5767 /* initialize migrate mdt parameters */
5768 param.fp_lmv_md = &lmu;
5769 param.fp_migrate = 1;
5770 rc = llapi_migrate_mdt(argv[optind], ¶m);
5772 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5773 progname, argv[optind], param.fp_mdt_index,
5778 static int lfs_osts(int argc, char **argv)
5780 return lfs_tgts(argc, argv);
5783 static int lfs_mdts(int argc, char **argv)
5785 return lfs_tgts(argc, argv);
5788 static int lfs_df(int argc, char **argv)
5790 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5791 enum mntdf_flags flags = MNTDF_SHOW;
5792 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5793 int c, rc = 0, index = 0;
5794 char fsname[PATH_MAX] = "", *pool_name = NULL;
5795 struct option long_opts[] = {
5796 { .val = 'h', .name = "human-readable",
5797 .has_arg = no_argument },
5798 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5799 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5800 { .val = 'p', .name = "pool", .has_arg = required_argument },
5801 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5804 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5807 flags |= MNTDF_COOKED;
5810 flags |= MNTDF_INODES;
5813 flags |= MNTDF_LAZY;
5819 flags |= MNTDF_VERBOSE;
5825 if (optind < argc && !realpath(argv[optind], path)) {
5827 fprintf(stderr, "error: invalid path '%s': %s\n",
5828 argv[optind], strerror(-rc));
5832 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5833 /* Check if we have a mount point */
5834 if (mntdir[0] == '\0')
5837 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5838 if (rc || path[0] != '\0')
5840 fsname[0] = '\0'; /* avoid matching in next loop */
5841 mntdir[0] = '\0'; /* avoid matching in next loop */
5847 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5848 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5852 /* llapi_search_mounts() fills "fsname", but that is not called if
5853 * explicit paths are specified on the command-line
5855 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5856 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5859 fprintf(stderr, "cannot get instance for '%s': %s\n",
5860 mntdir, strerror(-rc));
5864 if (!opt_instance) {
5865 /* print only the fsname name */
5866 buf = strchr(fsname, '-');
5870 } else if (!opt_fsname) {
5871 /* print only the instance name */
5872 buf = strchr(fsname, '-');
5878 } else if (opt_fsname) {
5879 /* print only the fsname */
5883 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5888 static int lfs_getname(int argc, char **argv)
5890 struct option long_opts[] = {
5891 { .val = 'h', .name = "help", .has_arg = no_argument },
5892 { .val = 'i', .name = "instance", .has_arg = no_argument },
5893 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5895 bool opt_instance = false, opt_fsname = false;
5896 char fsname[PATH_MAX] = "";
5899 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5902 opt_instance = true;
5913 /* If neither option is given, print both instance and fsname */
5914 if (!opt_instance && !opt_fsname)
5915 opt_instance = opt_fsname = true;
5917 if (optind == argc) { /* no paths specified, get all paths. */
5918 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5921 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5922 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5923 opt_instance, opt_fsname, true);
5926 path[0] = fsname[0] = mntdir[0] = '\0';
5928 } else { /* paths specified, only attempt to search these. */
5931 /* if only one path is given, print only requested info */
5932 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5934 for (; optind < argc; optind++) {
5935 rc2 = print_instance(argv[optind], fsname,
5936 sizeof(fsname), opt_instance,
5937 opt_fsname, opt_mntdir);
5947 static int lfs_check(int argc, char **argv)
5949 char mntdir[PATH_MAX] = {'\0'};
5958 fprintf(stderr, "%s check: server type must be specified\n",
5963 obd_types[0] = obd_type1;
5964 obd_types[1] = obd_type2;
5965 obd_types[2] = obd_type3;
5967 if (strcmp(argv[1], "osts") == 0) {
5968 strcpy(obd_types[0], "osc");
5969 } else if (strcmp(argv[1], "mdts") == 0 ||
5970 strcmp(argv[1], "mds") == 0) {
5971 strcpy(obd_types[0], "mdc");
5972 } else if (strcmp(argv[1], "mgts") == 0) {
5973 strcpy(obd_types[0], "mgc");
5974 } else if (strcmp(argv[1], "all") == 0 ||
5975 strcmp(argv[1], "servers") == 0) {
5977 strcpy(obd_types[0], "osc");
5978 strcpy(obd_types[1], "mdc");
5979 strcpy(obd_types[2], "mgc");
5981 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5986 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5987 if (rc < 0 || mntdir[0] == '\0') {
5989 "%s check: cannot find mounted Lustre filesystem: %s\n",
5990 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5994 rc = llapi_target_check(num_types, obd_types, mntdir);
5996 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5997 progname, argv[1], strerror(-rc));
6003 #ifdef HAVE_SYS_QUOTA_H
6004 #define ARG2INT(nr, str, msg) \
6007 nr = strtol(str, &endp, 0); \
6008 if (*endp != '\0') { \
6009 fprintf(stderr, "%s: bad %s '%s'\n", \
6010 progname, msg, str); \
6015 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6017 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6018 * returns the value or ULONG_MAX on integer overflow or incorrect format
6020 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6021 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6022 * 3. empty integer value is interpreted as 0
6024 static unsigned long str2sec(const char* timestr)
6026 const char spec[] = "smhdw";
6027 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6028 unsigned long val = 0;
6031 if (strpbrk(timestr, spec) == NULL) {
6032 /* no specifiers inside the time string,
6033 should treat it as an integer value */
6034 val = strtoul(timestr, &tail, 10);
6035 return *tail ? ULONG_MAX : val;
6038 /* format string is XXwXXdXXhXXmXXs */
6044 v = strtoul(timestr, &tail, 10);
6045 if (v == ULONG_MAX || *tail == '\0')
6046 /* value too large (ULONG_MAX or more)
6047 or missing specifier */
6050 ptr = strchr(spec, *tail);
6052 /* unknown specifier */
6057 /* check if product will overflow the type */
6058 if (!(v < ULONG_MAX / mult[ind]))
6061 ADD_OVERFLOW(val, mult[ind] * v);
6062 if (val == ULONG_MAX)
6074 #define ARG2ULL(nr, str, def_units) \
6076 unsigned long long limit, units = def_units; \
6079 rc = llapi_parse_size(str, &limit, &units, 1); \
6081 fprintf(stderr, "%s: invalid limit '%s'\n", \
6088 static inline int has_times_option(int argc, char **argv)
6092 for (i = 1; i < argc; i++)
6093 if (!strcmp(argv[i], "-t"))
6099 int lfs_setquota_times(int argc, char **argv)
6102 struct if_quotactl qctl;
6103 char *mnt, *obd_type = (char *)qctl.obd_type;
6104 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6105 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6106 struct option long_opts[] = {
6107 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6108 { .val = 'g', .name = "group", .has_arg = no_argument },
6109 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6110 { .val = 'p', .name = "projid", .has_arg = no_argument },
6111 { .val = 't', .name = "times", .has_arg = no_argument },
6112 { .val = 'u', .name = "user", .has_arg = no_argument },
6116 memset(&qctl, 0, sizeof(qctl));
6117 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6118 qctl.qc_type = ALLQUOTA;
6120 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6121 long_opts, NULL)) != -1) {
6132 if (qctl.qc_type != ALLQUOTA) {
6133 fprintf(stderr, "error: -u/g/p can't be used "
6134 "more than once\n");
6137 qctl.qc_type = qtype;
6140 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6141 fprintf(stderr, "error: bad block-grace: %s\n",
6145 dqb->dqb_valid |= QIF_BTIME;
6148 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6149 fprintf(stderr, "error: bad inode-grace: %s\n",
6153 dqb->dqb_valid |= QIF_ITIME;
6155 case 't': /* Yes, of course! */
6157 default: /* getopt prints error message for us when opterr != 0 */
6162 if (qctl.qc_type == ALLQUOTA) {
6163 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6167 if (optind != argc - 1) {
6168 fprintf(stderr, "error: unexpected parameters encountered\n");
6172 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6173 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6174 fprintf(stderr, "error: grace time is too large\n");
6179 rc = llapi_quotactl(mnt, &qctl);
6182 fprintf(stderr, "%s %s ", obd_type,
6183 obd_uuid2str(&qctl.obd_uuid));
6184 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6191 #define BSLIMIT (1 << 0)
6192 #define BHLIMIT (1 << 1)
6193 #define ISLIMIT (1 << 2)
6194 #define IHLIMIT (1 << 3)
6196 int lfs_setquota(int argc, char **argv)
6199 struct if_quotactl qctl;
6200 char *mnt, *obd_type = (char *)qctl.obd_type;
6201 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6202 struct option long_opts[] = {
6203 { .val = 'b', .name = "block-softlimit",
6204 .has_arg = required_argument },
6205 { .val = 'B', .name = "block-hardlimit",
6206 .has_arg = required_argument },
6207 { .val = 'd', .name = "default", .has_arg = no_argument },
6208 { .val = 'g', .name = "group", .has_arg = required_argument },
6209 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6210 { .val = 'i', .name = "inode-softlimit",
6211 .has_arg = required_argument },
6212 { .val = 'I', .name = "inode-hardlimit",
6213 .has_arg = required_argument },
6214 { .val = 'p', .name = "projid", .has_arg = required_argument },
6215 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6216 { .val = 'u', .name = "user", .has_arg = required_argument },
6217 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6219 unsigned limit_mask = 0;
6221 bool use_default = false;
6224 if (has_times_option(argc, argv))
6225 return lfs_setquota_times(argc, argv);
6227 memset(&qctl, 0, sizeof(qctl));
6228 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6229 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6230 * so it can be used as a marker that qc_type
6231 * isn't reinitialized from command line */
6233 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6234 long_opts, NULL)) != -1) {
6237 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6240 goto quota_type_def;
6243 rc = name2uid(&qctl.qc_id, optarg);
6246 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6249 goto quota_type_def;
6252 rc = name2gid(&qctl.qc_id, optarg);
6255 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6258 goto quota_type_def;
6261 rc = name2projid(&qctl.qc_id, optarg);
6264 qctl.qc_id = strtoul(optarg, &endptr, 10);
6265 if (*endptr != '\0') {
6266 fprintf(stderr, "%s setquota: invalid"
6267 " id '%s'\n", progname, optarg);
6272 if (qctl.qc_id == 0) {
6273 fprintf(stderr, "%s setquota: can't set quota"
6274 " for root usr/group/project.\n",
6280 if (qctl.qc_type != ALLQUOTA) {
6282 "%s setquota: only one of -u, -U, -g,"
6283 " -G, -p or -P may be specified\n",
6287 qctl.qc_type = qtype;
6290 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6294 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6295 dqb->dqb_bsoftlimit >>= 10;
6296 limit_mask |= BSLIMIT;
6297 if (dqb->dqb_bsoftlimit &&
6298 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6300 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6301 "See '%s help setquota' or Lustre manual for details\n",
6302 progname, dqb->dqb_bsoftlimit,
6306 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6307 dqb->dqb_bhardlimit >>= 10;
6308 limit_mask |= BHLIMIT;
6309 if (dqb->dqb_bhardlimit &&
6310 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6312 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6313 "See '%s help setquota' or Lustre manual for details\n",
6314 progname, dqb->dqb_bhardlimit,
6318 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6319 limit_mask |= ISLIMIT;
6320 if (dqb->dqb_isoftlimit &&
6321 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6323 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6324 "See '%s help setquota' or Lustre manual for details\n",
6325 progname, dqb->dqb_isoftlimit,
6329 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6330 limit_mask |= IHLIMIT;
6331 if (dqb->dqb_ihardlimit &&
6332 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6334 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6335 "See '%s help setquota' or Lustre manual for details\n",
6336 progname, dqb->dqb_ihardlimit,
6341 "%s setquota: unrecognized option '%s'\n",
6342 progname, argv[optind - 1]);
6347 if (qctl.qc_type == ALLQUOTA) {
6349 "%s setquota: either -u or -g must be specified\n",
6354 if (!use_default && limit_mask == 0) {
6356 "%s setquota: at least one limit must be specified\n",
6361 if (use_default && limit_mask != 0) {
6363 "%s setquota: limits should not be specified when"
6364 " using default quota\n",
6369 if (use_default && qctl.qc_id == 0) {
6371 "%s setquota: can not set default quota for root"
6372 " user/group/project\n",
6377 if (optind != argc - 1) {
6379 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6380 progname, argv[optind]);
6387 dqb->dqb_bhardlimit = 0;
6388 dqb->dqb_bsoftlimit = 0;
6389 dqb->dqb_ihardlimit = 0;
6390 dqb->dqb_isoftlimit = 0;
6393 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6394 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6395 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6396 /* sigh, we can't just set blimits/ilimits */
6397 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6398 .qc_type = qctl.qc_type,
6399 .qc_id = qctl.qc_id};
6401 rc = llapi_quotactl(mnt, &tmp_qctl);
6405 if (!(limit_mask & BHLIMIT))
6406 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6407 if (!(limit_mask & BSLIMIT))
6408 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6409 if (!(limit_mask & IHLIMIT))
6410 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6411 if (!(limit_mask & ISLIMIT))
6412 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6414 /* Keep grace times if we have got no softlimit arguments */
6415 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6416 dqb->dqb_valid |= QIF_BTIME;
6417 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6420 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6421 dqb->dqb_valid |= QIF_ITIME;
6422 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6426 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6427 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6429 rc = llapi_quotactl(mnt, &qctl);
6433 "%s setquota: cannot quotactl '%s' '%s': %s",
6435 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6442 /* Converts seconds value into format string
6443 * result is returned in buf
6445 * 1. result is in descenting order: 1w2d3h4m5s
6446 * 2. zero fields are not filled (except for p. 3): 5d1s
6447 * 3. zero seconds value is presented as "0s"
6449 static char * __sec2str(time_t seconds, char *buf)
6451 const char spec[] = "smhdw";
6452 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6457 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6458 c = seconds / mult[i];
6460 if (c > 0 || (i == 0 && buf == tail))
6461 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6469 static void sec2str(time_t seconds, char *buf, int rc)
6476 tail = __sec2str(seconds, tail);
6478 if (rc && tail - buf < 39) {
6484 static void diff2str(time_t seconds, char *buf, time_t now)
6490 if (seconds <= now) {
6491 strcpy(buf, "none");
6494 __sec2str(seconds - now, buf);
6497 static void print_quota_title(char *name, struct if_quotactl *qctl,
6498 bool human_readable, bool show_default)
6501 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6502 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6503 "Filesystem", "bquota", "blimit", "bgrace",
6504 "iquota", "ilimit", "igrace");
6506 printf("Disk quotas for %s %s (%cid %u):\n",
6507 qtype_name(qctl->qc_type), name,
6508 *qtype_name(qctl->qc_type), qctl->qc_id);
6509 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6510 "Filesystem", human_readable ? "used" : "kbytes",
6511 "quota", "limit", "grace",
6512 "files", "quota", "limit", "grace");
6516 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6519 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6522 snprintf(buf, buflen, "%5.4gP",
6523 (double)num / ((__u64)1 << 40));
6525 snprintf(buf, buflen, "%5.4gT",
6526 (double)num / (1 << 30));
6528 snprintf(buf, buflen, "%5.4gG",
6529 (double)num / (1 << 20));
6531 snprintf(buf, buflen, "%5.4gM",
6532 (double)num / (1 << 10));
6534 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6538 #define STRBUF_LEN 32
6539 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6540 int rc, bool h, bool show_default)
6546 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6547 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6548 int bover = 0, iover = 0;
6549 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6550 char numbuf[3][STRBUF_LEN];
6552 char strbuf[STRBUF_LEN];
6554 if (dqb->dqb_bhardlimit &&
6555 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6557 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6558 if (dqb->dqb_btime > now) {
6565 if (dqb->dqb_ihardlimit &&
6566 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6568 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6569 if (dqb->dqb_itime > now) {
6577 if (strlen(mnt) > 15)
6578 printf("%s\n%15s", mnt, "");
6580 printf("%15s", mnt);
6583 diff2str(dqb->dqb_btime, timebuf, now);
6584 else if (show_default)
6585 snprintf(timebuf, sizeof(timebuf), "%llu",
6588 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6589 strbuf, sizeof(strbuf), h);
6590 if (rc == -EREMOTEIO)
6591 sprintf(numbuf[0], "%s*", strbuf);
6593 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6594 "%s" : "[%s]", strbuf);
6596 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6597 if (type == QC_GENERAL)
6598 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6599 "%s" : "[%s]", strbuf);
6601 sprintf(numbuf[1], "%s", "-");
6603 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6604 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6605 "%s" : "[%s]", strbuf);
6608 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6610 printf(" %7s%c %6s %7s %7s",
6611 numbuf[0], bover ? '*' : ' ', numbuf[1],
6612 numbuf[2], bover > 1 ? timebuf : "-");
6616 diff2str(dqb->dqb_itime, timebuf, now);
6617 else if (show_default)
6618 snprintf(timebuf, sizeof(timebuf), "%llu",
6621 snprintf(numbuf[0], sizeof(numbuf),
6622 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6623 (uintmax_t)dqb->dqb_curinodes);
6625 if (type == QC_GENERAL)
6626 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6628 (uintmax_t)dqb->dqb_isoftlimit);
6630 sprintf(numbuf[1], "%s", "-");
6632 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6633 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6636 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6637 else if (type != QC_OSTIDX)
6638 printf(" %7s%c %6s %7s %7s",
6639 numbuf[0], iover ? '*' : ' ', numbuf[1],
6640 numbuf[2], iover > 1 ? timebuf : "-");
6642 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6645 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6646 qctl->qc_cmd == Q_GETOINFO) {
6650 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6651 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6652 printf("Block grace time: %s; Inode grace time: %s\n",
6653 bgtimebuf, igtimebuf);
6657 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6658 bool h, __u64 *total)
6660 int rc = 0, rc1 = 0, count = 0;
6661 __u32 valid = qctl->qc_valid;
6663 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6665 fprintf(stderr, "can not get %s count: %s\n",
6666 is_mdt ? "mdt": "ost", strerror(-rc));
6670 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6671 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6672 rc = llapi_quotactl(mnt, qctl);
6674 /* It is remote client case. */
6675 if (rc == -EOPNOTSUPP) {
6682 fprintf(stderr, "quotactl %s%d failed.\n",
6683 is_mdt ? "mdt": "ost", qctl->qc_idx);
6687 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6688 qctl->qc_valid, 0, h, false);
6689 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6690 qctl->qc_dqblk.dqb_bhardlimit;
6693 qctl->qc_valid = valid;
6697 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6698 int verbose, int quiet, bool human_readable,
6701 int rc1 = 0, rc2 = 0, rc3 = 0;
6702 char *obd_type = (char *)qctl->obd_type;
6703 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6704 __u64 total_ialloc = 0, total_balloc = 0;
6705 bool use_default_for_blk = false;
6706 bool use_default_for_file = false;
6709 rc1 = llapi_quotactl(mnt, qctl);
6713 fprintf(stderr, "%s quotas are not enabled.\n",
6714 qtype_name(qctl->qc_type));
6717 fprintf(stderr, "Permission denied.\n");
6720 /* We already got error message. */
6723 fprintf(stderr, "Unexpected quotactl error: %s\n",
6728 if (!show_default && qctl->qc_id == 0) {
6729 qctl->qc_dqblk.dqb_bhardlimit = 0;
6730 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6731 qctl->qc_dqblk.dqb_ihardlimit = 0;
6732 qctl->qc_dqblk.dqb_isoftlimit = 0;
6733 qctl->qc_dqblk.dqb_btime = 0;
6734 qctl->qc_dqblk.dqb_itime = 0;
6735 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6738 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6739 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6740 use_default_for_blk = true;
6741 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6744 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6745 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6746 use_default_for_file = true;
6747 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6750 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6751 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6752 print_quota_title(name, qctl, human_readable, show_default);
6754 if (rc1 && *obd_type)
6755 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6757 if (qctl->qc_valid != QC_GENERAL)
6760 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6761 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6762 (QIF_LIMITS|QIF_USAGE));
6764 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6766 if (!show_default && verbose &&
6767 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6768 char strbuf[STRBUF_LEN];
6770 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6772 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6774 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6776 printf("Total allocated inode limit: %ju, total "
6777 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6781 if (use_default_for_blk)
6782 printf("%cid %u is using default block quota setting\n",
6783 *qtype_name(qctl->qc_type), qctl->qc_id);
6785 if (use_default_for_file)
6786 printf("%cid %u is using default file quota setting\n",
6787 *qtype_name(qctl->qc_type), qctl->qc_id);
6789 if (rc1 || rc2 || rc3 || inacc)
6790 printf("Some errors happened when getting quota info. "
6791 "Some devices may be not working or deactivated. "
6792 "The data in \"[]\" is inaccurate.\n");
6798 static int lfs_project(int argc, char **argv)
6800 int ret = 0, err = 0, c, i;
6801 struct project_handle_control phc = { 0 };
6802 enum lfs_project_ops_t op;
6805 phc.assign_projid = false;
6806 /* default action */
6807 op = LFS_PROJECT_LIST;
6809 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6812 if (op != LFS_PROJECT_LIST) {
6814 "%s: cannot specify '-c' '-C' '-s' together\n",
6819 op = LFS_PROJECT_CHECK;
6822 if (op != LFS_PROJECT_LIST) {
6824 "%s: cannot specify '-c' '-C' '-s' together\n",
6829 op = LFS_PROJECT_CLEAR;
6832 if (op != LFS_PROJECT_LIST) {
6834 "%s: cannot specify '-c' '-C' '-s' together\n",
6839 phc.set_inherit = true;
6840 op = LFS_PROJECT_SET;
6846 phc.keep_projid = true;
6849 phc.recursive = true;
6852 phc.projid = strtoul(optarg, NULL, 0);
6853 phc.assign_projid = true;
6857 phc.newline = false;
6860 fprintf(stderr, "%s: invalid option '%c'\n",
6866 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6867 op = LFS_PROJECT_SET;
6868 phc.set_projid = true;
6869 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6870 phc.set_projid = true;
6874 case LFS_PROJECT_CHECK:
6875 if (phc.keep_projid) {
6877 "%s: '-k' is useless together with '-c'\n",
6882 case LFS_PROJECT_CLEAR:
6885 "%s: '-0' is useless together with '-C'\n",
6889 if (phc.assign_projid) {
6891 "%s: '-p' is useless together with '-C'\n",
6896 case LFS_PROJECT_SET:
6899 "%s: '-0' is useless together with '-s'\n",
6903 if (phc.keep_projid) {
6905 "%s: '-k' is useless together with '-s'\n",
6913 "%s: '-0' is useless for list operations\n",
6923 fprintf(stderr, "%s: missing file or directory target(s)\n",
6928 for (i = 0; i < argc; i++) {
6930 case LFS_PROJECT_CHECK:
6931 err = lfs_project_check(argv[i], &phc);
6933 case LFS_PROJECT_LIST:
6934 err = lfs_project_list(argv[i], &phc);
6936 case LFS_PROJECT_CLEAR:
6937 err = lfs_project_clear(argv[i], &phc);
6939 case LFS_PROJECT_SET:
6940 err = lfs_project_set(argv[i], &phc);
6952 static int lfs_quota(int argc, char **argv)
6955 char *mnt, *name = NULL;
6956 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6957 .qc_type = ALLQUOTA };
6958 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6959 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6961 __u32 valid = QC_GENERAL, idx = 0;
6962 bool human_readable = false;
6963 bool show_default = false;
6966 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6969 show_default = true;
6974 show_default = true;
6979 show_default = true;
6983 if (qctl.qc_type != ALLQUOTA) {
6985 "%s quota: only one of -u, -g, or -p may be specified\n",
6989 qctl.qc_type = qtype;
6992 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6995 valid = qctl.qc_valid = QC_UUID;
6996 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6999 valid = qctl.qc_valid = QC_MDTIDX;
7000 idx = qctl.qc_idx = atoi(optarg);
7001 if (idx == 0 && *optarg != '0') {
7003 "%s quota: invalid MDT index '%s'\n",
7009 valid = qctl.qc_valid = QC_OSTIDX;
7010 idx = qctl.qc_idx = atoi(optarg);
7011 if (idx == 0 && *optarg != '0') {
7013 "%s quota: invalid OST index '%s'\n",
7025 human_readable = true;
7028 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7029 progname, argv[optind - 1]);
7034 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7035 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7036 optind == argc - 1 && !show_default) {
7038 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7039 qctl.qc_valid = valid;
7042 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7043 qctl.qc_type = qtype;
7044 if (qtype == USRQUOTA) {
7045 qctl.qc_id = geteuid();
7046 rc = uid2name(&name, qctl.qc_id);
7048 qctl.qc_id = getegid();
7049 rc = gid2name(&name, qctl.qc_id);
7054 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7055 human_readable, show_default);
7060 /* lfs quota -u username /path/to/lustre/mount */
7061 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7062 /* options should be followed by u/g-name and mntpoint */
7063 if ((!show_default && optind + 2 != argc) ||
7064 (show_default && optind + 1 != argc) ||
7065 qctl.qc_type == ALLQUOTA) {
7067 "%s quota: name and mount point must be specified\n",
7072 if (!show_default) {
7073 name = argv[optind++];
7074 switch (qctl.qc_type) {
7076 rc = name2uid(&qctl.qc_id, name);
7079 rc = name2gid(&qctl.qc_id, name);
7082 rc = name2projid(&qctl.qc_id, name);
7089 qctl.qc_valid = QC_GENERAL;
7090 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7095 qctl.qc_id = strtoul(name, &endptr, 10);
7096 if (*endptr != '\0') {
7097 fprintf(stderr, "%s quota: invalid id '%s'\n",
7102 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7103 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7109 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7110 human_readable, show_default);
7113 #endif /* HAVE_SYS_QUOTA_H! */
7115 static int flushctx_ioctl(char *mp)
7119 fd = open(mp, O_RDONLY);
7121 fprintf(stderr, "flushctx: error open %s: %s\n",
7122 mp, strerror(errno));
7126 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7128 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7129 mp, strerror(errno));
7135 static int lfs_flushctx(int argc, char **argv)
7137 int kdestroy = 0, c;
7138 char mntdir[PATH_MAX] = {'\0'};
7142 while ((c = getopt(argc, argv, "k")) != -1) {
7148 fprintf(stderr, "error: %s: option '-%c' "
7149 "unrecognized\n", argv[0], c);
7155 if ((rc = system("kdestroy > /dev/null")) != 0) {
7156 rc = WEXITSTATUS(rc);
7157 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7161 if (optind >= argc) {
7162 /* flush for all mounted lustre fs. */
7163 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7164 /* Check if we have a mount point */
7165 if (mntdir[0] == '\0')
7168 if (flushctx_ioctl(mntdir))
7171 mntdir[0] = '\0'; /* avoid matching in next loop */
7174 /* flush fs as specified */
7175 while (optind < argc) {
7176 if (flushctx_ioctl(argv[optind++]))
7183 static int lfs_changelog(int argc, char **argv)
7185 void *changelog_priv;
7186 struct changelog_rec *rec;
7187 long long startrec = 0, endrec = 0;
7189 struct option long_opts[] = {
7190 { .val = 'f', .name = "follow", .has_arg = no_argument },
7192 char short_opts[] = "f";
7195 while ((rc = getopt_long(argc, argv, short_opts,
7196 long_opts, NULL)) != -1) {
7203 "%s changelog: unrecognized option '%s'\n",
7204 progname, argv[optind - 1]);
7208 if (optind >= argc) {
7209 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7214 mdd = argv[optind++];
7216 startrec = strtoll(argv[optind++], NULL, 10);
7218 endrec = strtoll(argv[optind++], NULL, 10);
7220 rc = llapi_changelog_start(&changelog_priv,
7221 CHANGELOG_FLAG_BLOCK |
7222 CHANGELOG_FLAG_JOBID |
7223 CHANGELOG_FLAG_EXTRA_FLAGS |
7224 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7227 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7228 progname, strerror(errno = -rc));
7232 rc = llapi_changelog_set_xflags(changelog_priv,
7233 CHANGELOG_EXTRA_FLAG_UIDGID |
7234 CHANGELOG_EXTRA_FLAG_NID |
7235 CHANGELOG_EXTRA_FLAG_OMODE |
7236 CHANGELOG_EXTRA_FLAG_XATTR);
7239 "%s changelog: cannot set xflags for changelog: %s\n",
7240 progname, strerror(errno = -rc));
7244 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7248 if (endrec && rec->cr_index > endrec) {
7249 llapi_changelog_free(&rec);
7252 if (rec->cr_index < startrec) {
7253 llapi_changelog_free(&rec);
7257 secs = rec->cr_time >> 30;
7258 gmtime_r(&secs, &ts);
7259 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7260 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7261 changelog_type2str(rec->cr_type),
7262 ts.tm_hour, ts.tm_min, ts.tm_sec,
7263 (int)(rec->cr_time & ((1 << 30) - 1)),
7264 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7265 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7267 if (rec->cr_flags & CLF_JOBID) {
7268 struct changelog_ext_jobid *jid =
7269 changelog_rec_jobid(rec);
7271 if (jid->cr_jobid[0] != '\0')
7272 printf(" j=%s", jid->cr_jobid);
7275 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7276 struct changelog_ext_extra_flags *ef =
7277 changelog_rec_extra_flags(rec);
7279 printf(" ef=0x%llx", ef->cr_extra_flags);
7281 if (ef->cr_extra_flags & CLFE_UIDGID) {
7282 struct changelog_ext_uidgid *uidgid =
7283 changelog_rec_uidgid(rec);
7285 printf(" u=%llu:%llu",
7286 uidgid->cr_uid, uidgid->cr_gid);
7288 if (ef->cr_extra_flags & CLFE_NID) {
7289 struct changelog_ext_nid *nid =
7290 changelog_rec_nid(rec);
7293 libcfs_nid2str(nid->cr_nid));
7296 if (ef->cr_extra_flags & CLFE_OPEN) {
7297 struct changelog_ext_openmode *omd =
7298 changelog_rec_openmode(rec);
7299 char mode[] = "---";
7301 /* exec mode must be exclusive */
7302 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7305 if (omd->cr_openflags & MDS_FMODE_READ)
7307 if (omd->cr_openflags &
7314 if (strcmp(mode, "---") != 0)
7315 printf(" m=%s", mode);
7319 if (ef->cr_extra_flags & CLFE_XATTR) {
7320 struct changelog_ext_xattr *xattr =
7321 changelog_rec_xattr(rec);
7323 if (xattr->cr_xattr[0] != '\0')
7324 printf(" x=%s", xattr->cr_xattr);
7328 if (rec->cr_namelen)
7329 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7330 rec->cr_namelen, changelog_rec_name(rec));
7332 if (rec->cr_flags & CLF_RENAME) {
7333 struct changelog_ext_rename *rnm =
7334 changelog_rec_rename(rec);
7336 if (!fid_is_zero(&rnm->cr_sfid))
7337 printf(" s="DFID" sp="DFID" %.*s",
7338 PFID(&rnm->cr_sfid),
7339 PFID(&rnm->cr_spfid),
7340 (int)changelog_rec_snamelen(rec),
7341 changelog_rec_sname(rec));
7345 llapi_changelog_free(&rec);
7348 llapi_changelog_fini(&changelog_priv);
7351 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7352 progname, strerror(errno = -rc));
7354 return (rc == 1 ? 0 : rc);
7357 static int lfs_changelog_clear(int argc, char **argv)
7365 endrec = strtoll(argv[3], NULL, 10);
7367 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7370 fprintf(stderr, "%s: record out of range: %llu\n",
7372 else if (rc == -ENOENT)
7373 fprintf(stderr, "%s: no changelog user: %s\n",
7376 fprintf(stderr, "%s error: %s\n", argv[0],
7385 static int lfs_fid2path(int argc, char **argv)
7387 struct option long_opts[] = {
7388 { .val = 'c', .name = "cur", .has_arg = no_argument },
7389 { .val = 'l', .name = "link", .has_arg = required_argument },
7390 { .val = 'r', .name = "rec", .has_arg = required_argument },
7392 char short_opts[] = "cl:r:";
7393 char *device, *fid, *path;
7394 long long recno = -1;
7399 char *endptr = NULL;
7401 while ((rc = getopt_long(argc, argv, short_opts,
7402 long_opts, NULL)) != -1) {
7408 linkno = strtol(optarg, &endptr, 10);
7409 if (*endptr != '\0') {
7411 "%s fid2path: invalid linkno '%s'\n",
7417 recno = strtoll(optarg, &endptr, 10);
7418 if (*endptr != '\0') {
7420 "%s fid2path: invalid recno '%s'\n",
7427 "%s fid2path: unrecognized option '%s'\n",
7428 progname, argv[optind - 1]);
7435 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7440 device = argv[optind++];
7441 path = calloc(1, PATH_MAX);
7445 "%s fid2path: cannot allocate memory for path: %s\n",
7446 progname, strerror(-rc));
7451 while (optind < argc) {
7452 fid = argv[optind++];
7454 lnktmp = (linkno >= 0) ? linkno : 0;
7456 int oldtmp = lnktmp;
7457 long long rectmp = recno;
7459 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7463 "%s fid2path: cannot find '%s': %s\n",
7464 progname, fid, strerror(errno = -rc2));
7471 fprintf(stdout, "%lld ", rectmp);
7472 if (device[0] == '/') {
7473 fprintf(stdout, "%s", device);
7474 if (device[strlen(device) - 1] != '/')
7475 fprintf(stdout, "/");
7476 } else if (path[0] == '\0') {
7477 fprintf(stdout, "/");
7479 fprintf(stdout, "%s\n", path);
7482 /* specified linkno */
7484 if (oldtmp == lnktmp)
7494 static int lfs_path2fid(int argc, char **argv)
7496 struct option long_opts[] = {
7497 { .val = 'p', .name = "parents", .has_arg = no_argument },
7500 const char short_opts[] = "p";
7501 const char *sep = "";
7504 bool show_parents = false;
7506 while ((rc = getopt_long(argc, argv, short_opts,
7507 long_opts, NULL)) != -1) {
7510 show_parents = true;
7514 "%s path2fid: unrecognized option '%s'\n",
7515 progname, argv[optind - 1]);
7520 if (optind > argc - 1) {
7521 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7525 else if (optind < argc - 1)
7529 for (path = argv + optind; *path != NULL; path++) {
7531 if (!show_parents) {
7532 err = llapi_path2fid(*path, &fid);
7534 printf("%s%s"DFID"\n",
7535 *sep != '\0' ? *path : "", sep,
7538 char name[NAME_MAX + 1];
7539 unsigned int linkno = 0;
7541 while ((err = llapi_path2parent(*path, linkno, &fid,
7542 name, sizeof(name))) == 0) {
7543 if (*sep != '\0' && linkno == 0)
7544 printf("%s%s", *path, sep);
7546 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7551 /* err == -ENODATA is end-of-loop */
7552 if (linkno > 0 && err == -ENODATA) {
7560 "%s path2fid: cannot get %sfid for '%s': %s\n",
7561 progname, show_parents ? "parent " : "", *path,
7573 static int lfs_data_version(int argc, char **argv)
7580 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7583 fprintf(stderr, "%s data_version: FILE must be specified\n",
7588 while ((c = getopt(argc, argv, "nrw")) != -1) {
7591 data_version_flags = 0;
7594 data_version_flags |= LL_DV_RD_FLUSH;
7597 data_version_flags |= LL_DV_WR_FLUSH;
7601 "%s data_version: unrecognized option '%s'\n",
7602 progname, argv[optind - 1]);
7606 if (optind == argc) {
7607 fprintf(stderr, "%s data_version: FILE must be specified\n",
7612 path = argv[optind];
7613 fd = open(path, O_RDONLY);
7616 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7617 progname, path, strerror(-rc));
7621 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7624 "%s data_version: cannot get version for '%s': %s\n",
7625 progname, path, strerror(-rc));
7627 printf("%ju" "\n", (uintmax_t)data_version);
7633 static int lfs_hsm_state(int argc, char **argv)
7638 struct hsm_user_state hus;
7646 rc = llapi_hsm_state_get(path, &hus);
7648 fprintf(stderr, "can't get hsm state for %s: %s\n",
7649 path, strerror(errno = -rc));
7653 /* Display path name and status flags */
7654 printf("%s: (0x%08x)", path, hus.hus_states);
7656 if (hus.hus_states & HS_RELEASED)
7657 printf(" released");
7658 if (hus.hus_states & HS_EXISTS)
7660 if (hus.hus_states & HS_DIRTY)
7662 if (hus.hus_states & HS_ARCHIVED)
7663 printf(" archived");
7664 /* Display user-settable flags */
7665 if (hus.hus_states & HS_NORELEASE)
7666 printf(" never_release");
7667 if (hus.hus_states & HS_NOARCHIVE)
7668 printf(" never_archive");
7669 if (hus.hus_states & HS_LOST)
7670 printf(" lost_from_hsm");
7672 if (hus.hus_archive_id != 0)
7673 printf(", archive_id:%d", hus.hus_archive_id);
7676 } while (++i < argc);
7681 #define LFS_HSM_SET 0
7682 #define LFS_HSM_CLEAR 1
7685 * Generic function to set or clear HSM flags.
7686 * Used by hsm_set and hsm_clear.
7688 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7690 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7692 struct option long_opts[] = {
7693 { .val = 'A', .name = "archived", .has_arg = no_argument },
7694 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7695 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7696 { .val = 'e', .name = "exists", .has_arg = no_argument },
7697 { .val = 'l', .name = "lost", .has_arg = no_argument },
7698 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7699 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7701 char short_opts[] = "lraAdei:";
7705 __u32 archive_id = 0;
7711 while ((c = getopt_long(argc, argv, short_opts,
7712 long_opts, NULL)) != -1) {
7718 mask |= HS_NOARCHIVE;
7721 mask |= HS_ARCHIVED;
7724 mask |= HS_NORELEASE;
7733 archive_id = strtol(optarg, &end, 10);
7735 fprintf(stderr, "invalid archive_id: '%s'\n",
7743 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7744 argv[0], argv[optind - 1]);
7749 /* User should have specified a flag */
7753 while (optind < argc) {
7755 path = argv[optind];
7757 /* If mode == 0, this means we apply the mask. */
7758 if (mode == LFS_HSM_SET)
7759 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7761 rc = llapi_hsm_state_set(path, 0, mask, 0);
7764 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7765 path, strerror(errno = -rc));
7774 static int lfs_hsm_action(int argc, char **argv)
7779 struct hsm_current_action hca;
7780 struct hsm_extent he;
7781 enum hsm_user_action hua;
7782 enum hsm_progress_states hps;
7790 rc = llapi_hsm_current_action(path, &hca);
7792 fprintf(stderr, "can't get hsm action for %s: %s\n",
7793 path, strerror(errno = -rc));
7796 he = hca.hca_location;
7797 hua = hca.hca_action;
7798 hps = hca.hca_state;
7800 printf("%s: %s", path, hsm_user_action2name(hua));
7802 /* Skip file without action */
7803 if (hca.hca_action == HUA_NONE) {
7808 printf(" %s ", hsm_progress_state2name(hps));
7810 if ((hps == HPS_RUNNING) &&
7811 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7812 printf("(%llu bytes moved)\n",
7813 (unsigned long long)he.length);
7814 else if ((he.offset + he.length) == LUSTRE_EOF)
7815 printf("(from %llu to EOF)\n",
7816 (unsigned long long)he.offset);
7818 printf("(from %llu to %llu)\n",
7819 (unsigned long long)he.offset,
7820 (unsigned long long)(he.offset + he.length));
7822 } while (++i < argc);
7827 static int lfs_hsm_set(int argc, char **argv)
7829 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7832 static int lfs_hsm_clear(int argc, char **argv)
7834 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7838 * Check file state and return its fid, to be used by lfs_hsm_request().
7840 * \param[in] file Path to file to check
7841 * \param[in,out] fid Pointer to allocated lu_fid struct.
7842 * \param[in,out] last_dev Pointer to last device id used.
7844 * \return 0 on success.
7846 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7852 rc = lstat(file, &st);
7854 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7857 /* Checking for regular file as archiving as posix copytool
7858 * rejects archiving files other than regular files
7860 if (!S_ISREG(st.st_mode)) {
7861 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7864 /* A request should be ... */
7865 if (*last_dev != st.st_dev && *last_dev != 0) {
7866 fprintf(stderr, "All files should be "
7867 "on the same filesystem: %s\n", file);
7870 *last_dev = st.st_dev;
7872 rc = llapi_path2fid(file, fid);
7874 fprintf(stderr, "Cannot read FID of %s: %s\n",
7875 file, strerror(-rc));
7881 /* Fill an HSM HUR item with a given file name.
7883 * If mntpath is set, then the filename is actually a FID, and no
7884 * lookup on the filesystem will be performed.
7886 * \param[in] hur the user request to fill
7887 * \param[in] idx index of the item inside the HUR to fill
7888 * \param[in] mntpath mountpoint of Lustre
7889 * \param[in] fname filename (if mtnpath is NULL)
7890 * or FID (if mntpath is set)
7891 * \param[in] last_dev pointer to last device id used
7893 * \retval 0 on success
7894 * \retval CMD_HELP or a negative errno on error
7896 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7897 const char *mntpath, const char *fname,
7900 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7903 hui->hui_extent.length = -1;
7905 if (mntpath != NULL) {
7908 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7912 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7917 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7921 hur->hur_request.hr_itemcount++;
7926 static int lfs_hsm_request(int argc, char **argv, int action)
7928 struct option long_opts[] = {
7929 { .val = 'a', .name = "archive", .has_arg = required_argument },
7930 { .val = 'D', .name = "data", .has_arg = required_argument },
7931 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7932 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7935 char short_opts[] = "l:D:a:m:";
7936 struct hsm_user_request *hur, *oldhur;
7941 char *filelist = NULL;
7942 char fullpath[PATH_MAX];
7943 char *opaque = NULL;
7947 int nbfile_alloc = 0;
7948 char *some_file = NULL;
7949 char *mntpath = NULL;
7955 while ((c = getopt_long(argc, argv, short_opts,
7956 long_opts, NULL)) != -1) {
7965 if (action != HUA_ARCHIVE &&
7966 action != HUA_REMOVE) {
7968 "error: -a is supported only "
7969 "when archiving or removing\n");
7972 archive_id = atoi(optarg);
7975 if (some_file == NULL) {
7977 some_file = strdup(optarg);
7983 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7984 argv[0], argv[optind - 1]);
7989 /* All remaining args are files, so we have at least nbfile */
7990 nbfile = argc - optind;
7992 if ((nbfile == 0) && (filelist == NULL))
7996 opaque_len = strlen(opaque);
7998 /* Alloc the request structure with enough place to store all files
7999 * from command line. */
8000 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8002 fprintf(stderr, "Cannot create the request: %s\n",
8006 nbfile_alloc = nbfile;
8008 hur->hur_request.hr_action = action;
8009 hur->hur_request.hr_archive_id = archive_id;
8010 hur->hur_request.hr_flags = 0;
8012 /* All remaining args are files, add them */
8013 if (nbfile != 0 && some_file == NULL)
8014 some_file = strdup(argv[optind]);
8016 for (i = 0; i < nbfile; i++) {
8017 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8023 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8025 /* If a filelist was specified, read the filelist from it. */
8026 if (filelist != NULL) {
8027 fp = fopen(filelist, "r");
8029 fprintf(stderr, "Cannot read the file list %s: %s\n",
8030 filelist, strerror(errno));
8035 while ((rc = getline(&line, &len, fp)) != -1) {
8036 /* If allocated buffer was too small, get something
8038 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8041 nbfile_alloc = nbfile_alloc * 2 + 1;
8043 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8046 fprintf(stderr, "hsm: cannot allocate "
8047 "the request: %s\n",
8054 size = hur_len(oldhur);
8056 fprintf(stderr, "hsm: cannot allocate "
8057 "%u files + %u bytes data\n",
8058 oldhur->hur_request.hr_itemcount,
8059 oldhur->hur_request.hr_data_len);
8066 memcpy(hur, oldhur, size);
8071 if (line[strlen(line) - 1] == '\n')
8072 line[strlen(line) - 1] = '\0';
8074 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8075 mntpath, line, &last_dev);
8081 if (some_file == NULL) {
8091 /* If a --data was used, add it to the request */
8092 hur->hur_request.hr_data_len = opaque_len;
8094 memcpy(hur_data(hur), opaque, opaque_len);
8096 /* Send the HSM request */
8097 if (realpath(some_file, fullpath) == NULL) {
8098 fprintf(stderr, "Could not find path '%s': %s\n",
8099 some_file, strerror(errno));
8101 rc = llapi_hsm_request(fullpath, hur);
8103 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8104 some_file, strerror(-rc));
8114 static int lfs_hsm_archive(int argc, char **argv)
8116 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8119 static int lfs_hsm_restore(int argc, char **argv)
8121 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8124 static int lfs_hsm_release(int argc, char **argv)
8126 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8129 static int lfs_hsm_remove(int argc, char **argv)
8131 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8134 static int lfs_hsm_cancel(int argc, char **argv)
8136 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8139 static int lfs_swap_layouts(int argc, char **argv)
8144 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8145 SWAP_LAYOUTS_KEEP_MTIME |
8146 SWAP_LAYOUTS_KEEP_ATIME);
8149 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8151 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8153 int lfs_get_mode(const char *string)
8155 enum lock_mode_user mode;
8157 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8158 if (lock_mode_names[mode] == NULL)
8160 if (strcmp(string, lock_mode_names[mode]) == 0)
8167 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8169 enum lu_ladvise_type advice;
8172 advice < ARRAY_SIZE(ladvise_names); advice++) {
8173 if (ladvise_names[advice] == NULL)
8175 if (strcmp(string, ladvise_names[advice]) == 0)
8179 return LU_LADVISE_INVALID;
8182 static int lfs_ladvise(int argc, char **argv)
8184 struct option long_opts[] = {
8185 { .val = 'a', .name = "advice", .has_arg = required_argument },
8186 { .val = 'b', .name = "background", .has_arg = no_argument },
8187 { .val = 'e', .name = "end", .has_arg = required_argument },
8188 { .val = 'l', .name = "length", .has_arg = required_argument },
8189 { .val = 'm', .name = "mode", .has_arg = required_argument },
8190 { .val = 's', .name = "start", .has_arg = required_argument },
8191 { .val = 'u', .name = "unset", .has_arg = no_argument },
8193 char short_opts[] = "a:be:l:m:s:u";
8198 struct llapi_lu_ladvise advice;
8199 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8200 unsigned long long start = 0;
8201 unsigned long long end = LUSTRE_EOF;
8202 unsigned long long length = 0;
8203 unsigned long long size_units;
8204 unsigned long long flags = 0;
8208 while ((c = getopt_long(argc, argv, short_opts,
8209 long_opts, NULL)) != -1) {
8212 advice_type = lfs_get_ladvice(optarg);
8213 if (advice_type == LU_LADVISE_INVALID) {
8214 fprintf(stderr, "%s: invalid advice type "
8215 "'%s'\n", argv[0], optarg);
8216 fprintf(stderr, "Valid types:");
8218 for (advice_type = 0;
8219 advice_type < ARRAY_SIZE(ladvise_names);
8221 if (ladvise_names[advice_type] == NULL)
8223 fprintf(stderr, " %s",
8224 ladvise_names[advice_type]);
8226 fprintf(stderr, "\n");
8239 rc = llapi_parse_size(optarg, &end,
8242 fprintf(stderr, "%s: bad end offset '%s'\n",
8249 rc = llapi_parse_size(optarg, &start,
8252 fprintf(stderr, "%s: bad start offset "
8253 "'%s'\n", argv[0], optarg);
8259 rc = llapi_parse_size(optarg, &length,
8262 fprintf(stderr, "%s: bad length '%s'\n",
8268 mode = lfs_get_mode(optarg);
8270 fprintf(stderr, "%s: bad mode '%s', valid "
8271 "modes are READ or WRITE\n",
8279 fprintf(stderr, "%s: option '%s' unrecognized\n",
8280 argv[0], argv[optind - 1]);
8285 if (advice_type == LU_LADVISE_INVALID) {
8286 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8287 fprintf(stderr, "Valid types:");
8288 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8290 if (ladvise_names[advice_type] == NULL)
8292 fprintf(stderr, " %s", ladvise_names[advice_type]);
8294 fprintf(stderr, "\n");
8298 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8299 fprintf(stderr, "%s: Lock no expand advice is a per file "
8300 "descriptor advice, so when called from lfs, "
8301 "it does nothing.\n", argv[0]);
8305 if (argc <= optind) {
8306 fprintf(stderr, "%s: please give one or more file names\n",
8311 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8312 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8317 if (end == LUSTRE_EOF && length != 0)
8318 end = start + length;
8321 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8322 argv[0], start, end);
8326 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8327 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8332 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8333 fprintf(stderr, "%s: mode is required with lockahead\n",
8338 while (optind < argc) {
8341 path = argv[optind++];
8343 fd = open(path, O_RDONLY);
8345 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8346 argv[0], path, strerror(errno));
8351 advice.lla_start = start;
8352 advice.lla_end = end;
8353 advice.lla_advice = advice_type;
8354 advice.lla_value1 = 0;
8355 advice.lla_value2 = 0;
8356 advice.lla_value3 = 0;
8357 advice.lla_value4 = 0;
8358 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8359 advice.lla_lockahead_mode = mode;
8360 advice.lla_peradvice_flags = flags;
8363 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8366 fprintf(stderr, "%s: cannot give advice '%s' to file "
8367 "'%s': %s\n", argv[0],
8368 ladvise_names[advice_type],
8369 path, strerror(errno));
8375 if (rc == 0 && rc2 < 0)
8381 static const char *const heat_names[] = LU_HEAT_NAMES;
8383 static int lfs_heat_get(int argc, char **argv)
8385 struct lu_heat *heat;
8394 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8396 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8401 while (optind < argc) {
8402 path = argv[optind++];
8404 fd = open(path, O_RDONLY);
8406 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8407 argv[0], path, strerror(errno));
8412 heat->lh_count = OBD_HEAT_COUNT;
8413 rc2 = llapi_heat_get(fd, heat);
8416 fprintf(stderr, "%s: cannot get heat of file '%s'"
8417 ": %s\n", argv[0], path, strerror(errno));
8421 printf("flags: %x\n", heat->lh_flags);
8422 for (i = 0; i < heat->lh_count; i++)
8423 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8425 if (rc == 0 && rc2 < 0)
8433 static int lfs_heat_set(int argc, char **argv)
8435 struct option long_opts[] = {
8436 { .val = 'c', .name = "clear", .has_arg = no_argument },
8437 { .val = 'o', .name = "off", .has_arg = no_argument },
8438 { .val = 'O', .name = "on", .has_arg = no_argument },
8440 enum lu_heat_flag flags = 0;
8450 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8453 flags |= LU_HEAT_FLAG_CLEAR;
8456 flags |= LU_HEAT_FLAG_CLEAR;
8457 flags |= LU_HEAT_FLAG_OFF;
8460 flags &= ~LU_HEAT_FLAG_OFF;
8465 fprintf(stderr, "%s: option '%s' unrecognized\n",
8466 argv[0], argv[optind - 1]);
8471 if (argc <= optind) {
8472 fprintf(stderr, "%s: please give one or more file names\n",
8477 while (optind < argc) {
8478 path = argv[optind++];
8480 fd = open(path, O_RDONLY);
8482 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8483 argv[0], path, strerror(errno));
8488 rc2 = llapi_heat_set(fd, flags);
8491 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8492 ": %s\n", argv[0], path, strerror(errno));
8496 if (rc == 0 && rc2 < 0)
8502 /** The input string contains a comma delimited list of component ids and
8503 * ranges, for example "1,2-4,7".
8505 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8507 bool end_of_loop = false;
8515 while (!end_of_loop) {
8519 char *endptr = NULL;
8522 ptr = strchrnul(arg, ',');
8523 end_of_loop = *ptr == '\0';
8526 start_index = strtol(arg, &endptr, 0);
8527 if (endptr == arg) /* no data at all */
8529 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8531 if (start_index < 0)
8534 end_index = start_index;
8535 if (*endptr == '-') {
8536 end_index = strtol(endptr + 1, &endptr, 0);
8537 if (*endptr != '\0')
8539 if (end_index < start_index)
8543 for (i = start_index; i <= end_index && size > 0; i++) {
8546 /* remove duplicate */
8547 for (j = 0; j < nr; j++) {
8551 if (j == nr) { /* no duplicate */
8557 if (size == 0 && i < end_index)
8564 if (!end_of_loop && ptr != NULL)
8567 return rc < 0 ? rc : nr;
8571 * struct verify_mirror_id - Mirror id to be verified.
8572 * @mirror_id: A specified mirror id.
8573 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8575 struct verify_mirror_id {
8581 * compare_mirror_ids() - Compare mirror ids.
8582 * @layout: Mirror component list.
8583 * @cbdata: Callback data in verify_mirror_id structure.
8585 * This is a callback function called by llapi_layout_comp_iterate()
8586 * to compare the specified mirror id with the one in the current
8587 * component of @layout. If they are the same, then the specified
8588 * mirror id is valid.
8590 * Return: a negative error code on failure or
8591 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8592 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8595 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8597 struct verify_mirror_id *mirror_id_cbdata =
8598 (struct verify_mirror_id *)cbdata;
8602 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8606 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8607 progname, strerror(errno));
8611 if (mirror_id_cbdata->mirror_id == mirror_id) {
8612 mirror_id_cbdata->is_valid_id = true;
8613 return LLAPI_LAYOUT_ITER_STOP;
8616 return LLAPI_LAYOUT_ITER_CONT;
8620 * verify_mirror_ids() - Verify specified mirror ids.
8621 * @fname: Mirrored file name.
8622 * @mirror_ids: Specified mirror ids to be verified.
8623 * @ids_nr: Number of specified mirror ids.
8625 * This function verifies that specified @mirror_ids are valid
8626 * in the mirrored file @fname.
8628 * Return: 0 on success or a negative error code on failure.
8631 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8633 struct llapi_layout *layout = NULL;
8634 struct verify_mirror_id mirror_id_cbdata = { 0 };
8645 if (stat(fname, &stbuf) < 0) {
8646 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8647 progname, fname, strerror(errno));
8652 if (!S_ISREG(stbuf.st_mode)) {
8653 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8659 fd = open(fname, O_DIRECT | O_RDONLY);
8661 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8662 progname, fname, strerror(errno));
8667 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8669 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8670 progname, fname, strerror(errno));
8674 layout = llapi_layout_get_by_fd(fd, 0);
8675 if (layout == NULL) {
8676 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8677 progname, fname, strerror(errno));
8679 llapi_lease_release(fd);
8683 rc = llapi_layout_flags_get(layout, &flr_state);
8685 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8686 progname, fname, strerror(errno));
8691 flr_state &= LCM_FL_FLR_MASK;
8692 switch (flr_state) {
8695 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8696 progname, fname, llapi_layout_flags_string(flr_state));
8703 for (i = 0; i < ids_nr; i++) {
8704 mirror_id_cbdata.mirror_id = mirror_ids[i];
8705 mirror_id_cbdata.is_valid_id = false;
8707 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8712 "%s: '%s' failed to verify mirror id: %u.\n",
8713 progname, fname, mirror_ids[i]);
8717 if (!mirror_id_cbdata.is_valid_id) {
8720 "%s: '%s' invalid specified mirror id: %u.\n",
8721 progname, fname, mirror_ids[i]);
8727 llapi_layout_free(layout);
8728 llapi_lease_release(fd);
8736 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8737 __u16 *mirror_ids, int ids_nr)
8739 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8740 struct llapi_layout *layout;
8750 if (stat(fname, &stbuf) < 0) {
8751 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8752 progname, fname, strerror(errno));
8756 if (!S_ISREG(stbuf.st_mode)) {
8757 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8763 fd = open(fname, O_DIRECT | O_RDWR);
8765 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8766 progname, fname, strerror(errno));
8771 layout = llapi_layout_get_by_fd(fd, 0);
8772 if (layout == NULL) {
8773 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8774 progname, fname, strerror(errno));
8779 rc = llapi_layout_flags_get(layout, &flr_state);
8781 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8782 progname, fname, strerror(errno));
8787 flr_state &= LCM_FL_FLR_MASK;
8788 if (flr_state == LCM_FL_NONE) {
8790 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8795 /* get stale component info */
8796 comp_size = llapi_mirror_find_stale(layout, comp_array,
8797 ARRAY_SIZE(comp_array),
8798 mirror_ids, ids_nr);
8799 if (comp_size <= 0) {
8804 ioc->lil_mode = LL_LEASE_WRLCK;
8805 ioc->lil_flags = LL_LEASE_RESYNC;
8806 rc = llapi_lease_set(fd, ioc);
8808 if (rc == -EALREADY)
8812 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8813 progname, fname, strerror(errno));
8817 /* get the read range [start, end) */
8818 start = comp_array[0].lrc_start;
8819 end = comp_array[0].lrc_end;
8820 for (idx = 1; idx < comp_size; idx++) {
8821 if (comp_array[idx].lrc_start < start)
8822 start = comp_array[idx].lrc_start;
8823 if (end < comp_array[idx].lrc_end)
8824 end = comp_array[idx].lrc_end;
8827 rc = llapi_lease_check(fd);
8828 if (rc != LL_LEASE_WRLCK) {
8829 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8834 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8837 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8838 progname, fname, rc);
8840 /* prepare ioc for lease put */
8841 ioc->lil_mode = LL_LEASE_UNLCK;
8842 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8844 for (idx = 0; idx < comp_size; idx++) {
8845 if (comp_array[idx].lrc_synced) {
8846 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8851 rc = llapi_lease_set(fd, ioc);
8853 if (rc == 0) /* lost lease lock */
8855 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8856 progname, fname, strerror(errno));
8860 * llapi_lease_set returns lease mode when it request to unlock
8866 llapi_layout_free(layout);
8873 static inline int lfs_mirror_resync(int argc, char **argv)
8875 struct ll_ioc_lease *ioc = NULL;
8876 __u16 mirror_ids[128] = { 0 };
8881 struct option long_opts[] = {
8882 { .val = 'o', .name = "only", .has_arg = required_argument },
8885 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8888 rc = parse_mirror_ids(mirror_ids,
8889 sizeof(mirror_ids) / sizeof(__u16),
8893 "%s: bad mirror ids '%s'.\n",
8900 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8901 argv[0], argv[optind - 1]);
8907 if (argc == optind) {
8908 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8913 if (ids_nr > 0 && argc > optind + 1) {
8915 "%s: option '--only' cannot be used upon multiple files.\n",
8923 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8928 /* set the lease on the file */
8929 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8931 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8932 argv[0], strerror(errno));
8937 for (; optind < argc; optind++) {
8938 rc = lfs_mirror_resync_file(argv[optind], ioc,
8939 mirror_ids, ids_nr);
8940 /* ignore previous file's error, continue with next file */
8943 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8951 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8953 struct llapi_layout *layout;
8956 layout = llapi_layout_get_by_fd(fd, 0);
8957 if (layout == NULL) {
8958 fprintf(stderr, "could not get layout.\n");
8962 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8964 fprintf(stderr, "failed to iterate layout\n");
8965 llapi_layout_free(layout);
8968 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8969 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8970 llapi_layout_free(layout);
8974 llapi_layout_free(layout);
8980 * Check whether two files are the same file
8981 * \retval 0 same file
8982 * \retval 1 not the same file
8983 * \retval <0 error code
8985 static inline int check_same_file(const char *f1, const char *f2)
8990 if (stat(f1, &stbuf1) < 0) {
8991 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8992 progname, f1, strerror(errno));
8996 if (stat(f2, &stbuf2) < 0) {
8997 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8998 progname, f2, strerror(errno));
9002 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9003 stbuf1.st_ino == stbuf2.st_ino)
9009 static inline int lfs_mirror_read(int argc, char **argv)
9012 __u16 mirror_id = 0;
9013 const char *outfile = NULL;
9019 const size_t buflen = 4 << 20;
9021 struct option long_opts[] = {
9022 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9023 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9026 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9031 mirror_id = strtoul(optarg, &end, 0);
9032 if (*end != '\0' || mirror_id == 0) {
9034 "%s %s: invalid mirror ID '%s'\n",
9035 progname, argv[0], optarg);
9043 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9044 progname, argv[optind - 1]);
9049 if (argc == optind) {
9050 fprintf(stderr, "%s %s: no mirrored file provided\n",
9053 } else if (argc > optind + 1) {
9054 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9058 if (mirror_id == 0) {
9059 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9064 /* open mirror file */
9065 fname = argv[optind];
9068 rc = check_same_file(fname, outfile);
9071 "%s %s: output file cannot be the mirrored file\n",
9079 fd = open(fname, O_DIRECT | O_RDONLY);
9081 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9082 progname, argv[0], fname, strerror(errno));
9086 /* verify mirror id */
9087 rc = verify_mirror_id_by_fd(fd, mirror_id);
9090 "%s %s: cannot find mirror with ID %u in '%s'\n",
9091 progname, argv[0], mirror_id, fname);
9095 /* open output file */
9097 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9099 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9100 progname, argv[0], outfile, strerror(errno));
9105 outfd = STDOUT_FILENO;
9108 /* allocate buffer */
9109 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9111 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9112 progname, argv[0], rc);
9119 ssize_t written = 0;
9121 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9122 if (bytes_read < 0) {
9125 "%s %s: fail to read data from mirror %u: %s\n",
9126 progname, argv[0], mirror_id, strerror(-rc));
9131 if (bytes_read == 0)
9134 while (written < bytes_read) {
9137 written2 = write(outfd, buf + written,
9138 bytes_read - written);
9141 "%s %s: fail to write %s: %s\n",
9142 progname, argv[0], outfile ? : "STDOUT",
9147 written += written2;
9150 if (written != bytes_read) {
9152 "%s %s: written %ld bytes does not match with %ld read.\n",
9153 progname, argv[0], written, bytes_read);
9175 static inline int lfs_mirror_write(int argc, char **argv)
9178 __u16 mirror_id = 0;
9179 const char *inputfile = NULL;
9185 const size_t buflen = 4 << 20;
9187 size_t page_size = sysconf(_SC_PAGESIZE);
9188 struct ll_ioc_lease_id ioc;
9190 struct option long_opts[] = {
9191 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9192 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9195 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9200 mirror_id = strtoul(optarg, &end, 0);
9201 if (*end != '\0' || mirror_id == 0) {
9203 "%s %s: invalid mirror ID '%s'\n",
9204 progname, argv[0], optarg);
9212 fprintf(stderr, "%s: option '%s' unrecognized\n",
9213 progname, argv[optind - 1]);
9218 if (argc == optind) {
9219 fprintf(stderr, "%s %s: no mirrored file provided\n",
9222 } else if (argc > optind + 1) {
9223 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9227 if (mirror_id == 0) {
9228 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9233 /* open mirror file */
9234 fname = argv[optind];
9237 rc = check_same_file(fname, inputfile);
9240 "%s %s: input file cannot be the mirrored file\n",
9248 fd = open(fname, O_DIRECT | O_WRONLY);
9250 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9251 progname, argv[0], fname, strerror(errno));
9255 /* verify mirror id */
9256 rc = verify_mirror_id_by_fd(fd, mirror_id);
9259 "%s %s: cannot find mirror with ID %u in '%s'\n",
9260 progname, argv[0], mirror_id, fname);
9264 /* open input file */
9266 inputfd = open(inputfile, O_RDONLY, 0644);
9268 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9269 progname, argv[0], inputfile, strerror(errno));
9274 inputfd = STDIN_FILENO;
9277 /* allocate buffer */
9278 rc = posix_memalign(&buf, page_size, buflen);
9280 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9281 progname, argv[0], rc);
9285 /* prepare target mirror components instantiation */
9286 ioc.lil_mode = LL_LEASE_WRLCK;
9287 ioc.lil_flags = LL_LEASE_RESYNC;
9288 ioc.lil_mirror_id = mirror_id;
9289 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9292 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9293 progname, argv[0], fname, strerror(errno));
9303 rc = llapi_lease_check(fd);
9304 if (rc != LL_LEASE_WRLCK) {
9305 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9306 progname, argv[0], fname);
9310 bytes_read = read(inputfd, buf, buflen);
9311 if (bytes_read < 0) {
9314 "%s %s: fail to read data from '%s': %s\n",
9315 progname, argv[0], inputfile ? : "STDIN",
9322 if (bytes_read == 0)
9325 /* round up to page align to make direct IO happy. */
9326 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9328 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9333 "%s %s: fail to write to mirror %u: %s\n",
9334 progname, argv[0], mirror_id,
9342 if (pos & (page_size - 1)) {
9343 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9348 ioc.lil_mode = LL_LEASE_UNLCK;
9349 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9351 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9356 "%s %s: release lease lock of '%s' failed: %s\n",
9357 progname, argv[0], fname, strerror(errno));
9374 struct collect_ids_data {
9380 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9382 struct collect_ids_data *cid = cbdata;
9386 rc = llapi_layout_mirror_id_get(layout, &id);
9390 if ((__u16)id != cid->cid_exclude) {
9393 for (i = 0; i < cid->cid_count; i++) {
9394 /* already collected the mirror id */
9395 if (id == cid->cid_ids[i])
9396 return LLAPI_LAYOUT_ITER_CONT;
9398 cid->cid_ids[cid->cid_count] = id;
9402 return LLAPI_LAYOUT_ITER_CONT;
9405 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9407 struct llapi_layout *layout;
9408 struct collect_ids_data cid = { .cid_ids = ids,
9410 .cid_exclude = exclude_id, };
9413 layout = llapi_layout_get_by_fd(fd, 0);
9414 if (layout == NULL) {
9415 fprintf(stderr, "could not get layout\n");
9419 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9421 fprintf(stderr, "failed to iterate layout\n");
9422 llapi_layout_free(layout);
9426 llapi_layout_free(layout);
9428 return cid.cid_count;
9431 static inline int lfs_mirror_copy(int argc, char **argv)
9434 __u16 read_mirror_id = 0;
9435 __u16 ids[128] = { 0 };
9437 struct llapi_layout *layout = NULL;
9438 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9445 struct ll_ioc_lease *ioc = NULL;
9446 struct ll_ioc_lease_id *resync_ioc;
9448 struct option long_opts[] = {
9449 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9450 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9453 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9458 read_mirror_id = strtoul(optarg, &end, 0);
9459 if (*end != '\0' || read_mirror_id == 0) {
9461 "%s %s: invalid read mirror ID '%s'\n",
9462 progname, argv[0], optarg);
9467 if (!strcmp(optarg, "-1")) {
9468 /* specify all other mirrors */
9472 count = parse_mirror_ids((__u16 *)ids,
9480 fprintf(stderr, "%s: option '%s' unrecognized\n",
9481 progname, argv[optind - 1]);
9486 if (argc == optind) {
9487 fprintf(stderr, "%s %s: no mirrored file provided\n",
9490 } else if (argc > optind + 1) {
9491 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9495 if (read_mirror_id == 0) {
9497 "%s %s: no valid read mirror ID %d is provided\n",
9498 progname, argv[0], read_mirror_id);
9504 "%s %s: no write mirror ID is provided\n",
9509 for (i = 0; i < count; i++) {
9510 if (read_mirror_id == ids[i]) {
9512 "%s %s: read and write mirror ID cannot be the same\n",
9518 /* open mirror file */
9519 fname = argv[optind];
9521 fd = open(fname, O_DIRECT | O_RDWR);
9523 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9524 progname, argv[0], fname, strerror(errno));
9528 /* write to all other mirrors */
9529 if (ids[0] == (__u16)-1) {
9530 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9534 "%s %s: failed to get other mirror ids in '%s': %d\n",
9535 progname, argv[0], fname, rc);
9540 /* verify mirror id */
9541 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9544 "%s %s: cannot find mirror with ID %u in '%s'\n",
9545 progname, argv[0], read_mirror_id, fname);
9549 for (i = 0; i < count; i++) {
9550 rc = verify_mirror_id_by_fd(fd, ids[i]);
9553 "%s %s: cannot find mirror with ID %u in '%s'\n",
9554 progname, argv[0], ids[i], fname);
9559 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9562 "%s %s: cannot alloc comp id array for ioc: %s\n",
9563 progname, argv[0], strerror(errno));
9568 /* get stale component info */
9569 layout = llapi_layout_get_by_fd(fd, 0);
9570 if (layout == NULL) {
9571 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9572 progname, argv[0], fname, strerror(errno));
9576 comp_size = llapi_mirror_find_stale(layout, comp_array,
9577 ARRAY_SIZE(comp_array),
9579 llapi_layout_free(layout);
9580 if (comp_size < 0) {
9585 /* prepare target mirror components instantiation */
9586 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9587 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9588 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9590 resync_ioc->lil_mirror_id = ids[0];
9592 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9593 rc = llapi_lease_set(fd, ioc);
9596 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9597 progname, argv[0], fname, strerror(errno));
9601 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9604 fprintf(stderr, "%s %s: copy error: %d\n",
9605 progname, argv[0], rc);
9609 fprintf(stdout, "mirror copied successfully: ");
9610 for (i = 0; i < copied; i++)
9611 fprintf(stdout, "%d ", ids[i]);
9612 fprintf(stdout, "\n");
9614 ioc->lil_mode = LL_LEASE_UNLCK;
9615 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9617 for (i = 0; i < comp_size; i++) {
9620 for (j = 0; j < copied; j++) {
9621 if (comp_array[i].lrc_mirror_id != ids[j])
9624 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9628 rc = llapi_lease_set(fd, ioc);
9633 "%s %s: release lease lock of '%s' failed: %s\n",
9634 progname, argv[0], fname, strerror(errno));
9648 * struct verify_chunk - Mirror chunk to be verified.
9649 * @chunk: [start, end) of the chunk.
9650 * @mirror_count: Number of mirror ids in @mirror_id array.
9651 * @mirror_id: Array of valid mirror ids that cover the chunk.
9653 struct verify_chunk {
9654 struct lu_extent chunk;
9655 unsigned int mirror_count;
9656 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9660 * print_chunks() - Print chunk information.
9661 * @fname: Mirrored file name.
9662 * @chunks: Array of chunks.
9663 * @chunk_count: Number of chunks in @chunks array.
9665 * This function prints [start, end) of each chunk in @chunks
9666 * for mirrored file @fname, and also prints the valid mirror ids
9667 * that cover the chunk.
9672 void print_chunks(const char *fname, struct verify_chunk *chunks,
9678 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9679 for (i = 0; i < chunk_count; i++) {
9680 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9682 if (chunks[i].mirror_count == 0)
9683 fprintf(stdout, "\t[");
9685 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9686 for (j = 1; j < chunks[i].mirror_count; j++)
9687 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9689 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9691 fprintf(stdout, "\n");
9695 * print_checksums() - Print CRC-32 checksum values.
9696 * @chunk: A chunk and its corresponding valid mirror ids.
9697 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9699 * This function prints CRC-32 checksum values on @chunk for
9700 * each valid mirror that covers it.
9705 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9710 "CRC-32 checksum value for chunk "DEXT":\n",
9711 PEXT(&chunk->chunk));
9712 for (i = 0; i < chunk->mirror_count; i++)
9713 fprintf(stdout, "Mirror %u:\t%#lx\n",
9714 chunk->mirror_id[i], crc[i]);
9715 fprintf(stdout, "\n");
9719 * filter_mirror_id() - Filter specified mirror ids.
9720 * @chunks: Array of chunks.
9721 * @chunk_count: Number of chunks in @chunks array.
9722 * @mirror_ids: Specified mirror ids to be verified.
9723 * @ids_nr: Number of specified mirror ids.
9725 * This function scans valid mirror ids that cover each chunk in @chunks
9726 * and filters specified mirror ids.
9731 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9732 __u16 *mirror_ids, int ids_nr)
9737 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9738 unsigned int valid_count = 0;
9740 for (i = 0; i < chunk_count; i++) {
9741 if (chunks[i].mirror_count == 0)
9745 for (j = 0; j < ids_nr; j++) {
9746 for (k = 0; k < chunks[i].mirror_count; k++) {
9747 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9748 valid_id[valid_count] = mirror_ids[j];
9755 memcpy(chunks[i].mirror_id, valid_id,
9756 sizeof(__u16) * valid_count);
9757 chunks[i].mirror_count = valid_count;
9762 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9763 * @layout: Mirror component list.
9764 * @chunks: Array of chunks.
9765 * @chunks_size: Array size of @chunks.
9767 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9768 * to find out chunk segments and store them in @chunks array.
9770 * The @mirror_id array in each element of @chunks will store the valid
9771 * mirror ids that cover the chunk. If a mirror component covering the
9772 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9773 * will not be stored into the @mirror_id array, and the chunk for that
9774 * mirror will not be verified.
9776 * The @mirror_count in each element of @chunks will store the number of
9777 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9778 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9779 * indicates the chunk is valid in only one mirror. In both cases, the
9780 * chunk will not be verified.
9782 * Here is an example:
9785 * +------+-------------+--------------------+
9787 * +------+------+------+------+-------------+
9788 * | | S | S | | mirror2
9789 * +-------------+------+------+-------------+
9791 * prepared @chunks array will contain 5 elements:
9792 * (([0, 1M), [1, 2], 2),
9793 * ([1M, 2M), [1, 2], 2),
9794 * ([2M, 3M), [1], 1),
9795 * ([3M, 4M], [], 0),
9796 * ([4M, EOF), [2], 1))
9798 * Return: the actual array size of @chunks on success
9799 * or a negative error code on failure.
9802 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9803 struct verify_chunk *chunks,
9814 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9817 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9820 "%s: move to the first layout component: %s.\n",
9821 progname, strerror(errno));
9827 chunks[idx].chunk.e_end = LUSTRE_EOF;
9829 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9832 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9833 progname, strerror(errno));
9837 if (start > chunks[idx].chunk.e_start ||
9838 end <= chunks[idx].chunk.e_start)
9841 if (end < chunks[idx].chunk.e_end)
9842 chunks[idx].chunk.e_end = end;
9844 rc = llapi_layout_comp_flags_get(layout, &flags);
9847 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9848 progname, strerror(errno));
9852 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9855 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9858 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9859 progname, strerror(errno));
9863 chunks[idx].mirror_id[i] = mirror_id;
9865 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9867 "%s: mirror_id array is too small.\n",
9874 rc = llapi_layout_comp_use(layout,
9875 LLAPI_LAYOUT_COMP_USE_NEXT);
9878 "%s: move to the next layout component: %s.\n",
9879 progname, strerror(errno));
9882 } /* loop through all components */
9884 chunks[idx].mirror_count = i;
9886 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9890 if (idx >= chunks_size) {
9891 fprintf(stderr, "%s: chunks array is too small.\n",
9897 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9901 return rc < 0 ? rc : idx + 1;
9905 * lfs_mirror_verify_chunk() - Verify a chunk.
9906 * @fd: File descriptor of the mirrored file.
9907 * @file_size: Size of the mirrored file.
9908 * @chunk: A chunk and its corresponding valid mirror ids.
9909 * @verbose: Verbose mode.
9911 * This function verifies a @chunk contains exactly the same data
9912 * ammong the mirrors that cover it.
9914 * If @verbose is specified, then the function will print where the
9915 * differences are if the data do not match. Otherwise, it will
9916 * just return an error in that case.
9918 * Return: 0 on success or a negative error code on failure.
9921 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9922 struct verify_chunk *chunk, int verbose)
9924 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9926 size_t page_size = sysconf(_SC_PAGESIZE);
9932 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9939 rc = posix_memalign(&buf, page_size, buflen);
9940 if (rc) /* error code is returned directly */
9944 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9945 PEXT(&chunk->chunk));
9946 for (i = 0; i < chunk->mirror_count; i++)
9947 fprintf(stdout, " %u", chunk->mirror_id[i]);
9948 fprintf(stdout, "\n");
9952 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9953 pos = chunk->chunk.e_start;
9954 while (bytes_done < count) {
9955 /* compute initial CRC-32 checksum */
9956 crc = crc32(0L, Z_NULL, 0);
9957 memset(crc_array, 0, sizeof(crc_array));
9960 for (i = 0; i < chunk->mirror_count; i++) {
9961 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9963 if (bytes_read < 0) {
9966 "%s: failed to read data from mirror %u: %s.\n",
9967 progname, chunk->mirror_id[i],
9972 /* compute new CRC-32 checksum */
9973 crc_array[i] = crc32(crc, buf, bytes_read);
9977 print_checksums(chunk, crc_array);
9979 /* compare CRC-32 checksum values */
9980 for (i = 1; i < chunk->mirror_count; i++) {
9981 if (crc_array[i] != crc_array[0]) {
9985 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9986 progname, PEXT(&chunk->chunk),
9987 chunk->mirror_id[0],
9988 chunk->mirror_id[i]);
9993 bytes_done += bytes_read;
9996 if (verbose > 1 && rc == 0) {
9997 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9998 PEXT(&chunk->chunk));
9999 for (i = 0; i < chunk->mirror_count; i++)
10000 fprintf(stdout, " %u", chunk->mirror_id[i]);
10001 fprintf(stdout, " PASS\n\n");
10010 * lfs_mirror_verify_file() - Verify a mirrored file.
10011 * @fname: Mirrored file name.
10012 * @mirror_ids: Specified mirror ids to be verified.
10013 * @ids_nr: Number of specified mirror ids.
10014 * @verbose: Verbose mode.
10016 * This function verifies that each SYNC mirror of a mirrored file
10017 * specified by @fname contains exactly the same data.
10019 * If @mirror_ids is specified, then the function will verify the
10020 * mirrors specified by @mirror_ids contain exactly the same data.
10022 * If @verbose is specified, then the function will print where the
10023 * differences are if the data do not match. Otherwise, it will
10024 * just return an error in that case.
10026 * Return: 0 on success or a negative error code on failure.
10029 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10032 struct verify_chunk chunks_array[1024] = { };
10033 struct llapi_layout *layout = NULL;
10035 uint32_t flr_state;
10037 int chunk_count = 0;
10043 if (stat(fname, &stbuf) < 0) {
10044 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10045 progname, fname, strerror(errno));
10050 if (!S_ISREG(stbuf.st_mode)) {
10051 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10057 if (stbuf.st_size == 0) {
10059 fprintf(stdout, "%s: '%s' file size is 0.\n",
10065 fd = open(fname, O_DIRECT | O_RDONLY);
10067 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10068 progname, fname, strerror(errno));
10073 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10075 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10076 progname, fname, strerror(errno));
10080 layout = llapi_layout_get_by_fd(fd, 0);
10081 if (layout == NULL) {
10082 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10083 progname, fname, strerror(errno));
10085 llapi_lease_release(fd);
10089 rc = llapi_layout_flags_get(layout, &flr_state);
10091 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10092 progname, fname, strerror(errno));
10097 flr_state &= LCM_FL_FLR_MASK;
10098 switch (flr_state) {
10101 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10102 progname, fname, llapi_layout_flags_string(flr_state));
10108 /* find out mirror chunks to be verified */
10109 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10110 ARRAY_SIZE(chunks_array));
10111 if (chunk_count < 0) {
10117 /* filter specified mirror ids */
10118 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10121 print_chunks(fname, chunks_array, chunk_count);
10123 for (idx = 0; idx < chunk_count; idx++) {
10124 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10127 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10129 PEXT(&chunks_array[idx].chunk),
10130 (unsigned long long)stbuf.st_size);
10134 if (chunks_array[idx].mirror_count == 0) {
10136 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10138 PEXT(&chunks_array[idx].chunk));
10140 fprintf(stderr, "skipped\n");
10144 fprintf(stderr, "failed\n");
10148 if (chunks_array[idx].mirror_count == 1) {
10151 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10153 PEXT(&chunks_array[idx].chunk),
10154 chunks_array[idx].mirror_id[0]);
10158 rc = llapi_lease_check(fd);
10159 if (rc != LL_LEASE_RDLCK) {
10160 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10165 /* verify one chunk */
10166 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10167 &chunks_array[idx], verbose);
10181 llapi_layout_free(layout);
10182 llapi_lease_release(fd);
10190 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10191 * @argc: The count of lfs mirror verify command line arguments.
10192 * @argv: Array of strings for lfs mirror verify command line arguments.
10194 * This function parses lfs mirror verify command and verifies the
10195 * specified mirrored file(s).
10197 * Return: 0 on success or a negative error code on failure.
10199 static inline int lfs_mirror_verify(int argc, char **argv)
10201 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10207 char cmd[PATH_MAX];
10209 struct option long_opts[] = {
10210 { .val = 'o', .name = "only", .has_arg = required_argument },
10211 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10212 { .name = NULL } };
10214 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10216 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10219 rc = parse_mirror_ids(mirror_ids,
10220 ARRAY_SIZE(mirror_ids),
10224 "%s: bad mirror ids '%s'.\n",
10231 "%s: at least 2 mirror ids needed with '--only' option.\n",
10241 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10242 progname, argv[optind - 1]);
10248 if (argc == optind) {
10249 fprintf(stderr, "%s: no file name given.\n", progname);
10254 if (ids_nr > 0 && argc > optind + 1) {
10256 "%s: '--only' cannot be used upon multiple files.\n",
10264 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10270 for (; optind < argc; optind++) {
10271 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10281 * lfs_mirror() - Parse and execute lfs mirror commands.
10282 * @argc: The count of lfs mirror command line arguments.
10283 * @argv: Array of strings for lfs mirror command line arguments.
10285 * This function parses lfs mirror commands and performs the
10286 * corresponding functions specified in mirror_cmdlist[].
10288 * Return: 0 on success or an error code on failure.
10290 static int lfs_mirror(int argc, char **argv)
10292 char cmd[PATH_MAX];
10295 setlinebuf(stdout);
10297 Parser_init("lfs-mirror > ", mirror_cmdlist);
10299 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10301 program_invocation_short_name = cmd;
10303 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10305 rc = Parser_commands();
10307 return rc < 0 ? -rc : rc;
10310 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10312 #if __BYTE_ORDER == __BIG_ENDIAN
10313 __swab16s(&attrs->lsa_valid);
10314 __swab64s(&attrs->lsa_size);
10315 __swab64s(&attrs->lsa_blocks);
10319 enum lfs_som_type {
10320 LFS_SOM_SIZE = 0x1,
10321 LFS_SOM_BLOCKS = 0x2,
10322 LFS_SOM_FLAGS = 0x4,
10323 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10327 static int lfs_getsom(int argc, char **argv)
10330 struct lustre_som_attrs *attrs;
10331 char buf[sizeof(*attrs) + 64];
10332 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10335 while ((c = getopt(argc, argv, "sbf")) != -1) {
10338 type = LFS_SOM_SIZE;
10341 type = LFS_SOM_BLOCKS;
10344 type = LFS_SOM_FLAGS;
10347 fprintf(stderr, "%s: invalid option '%c'\n",
10357 fprintf(stderr, "%s: %s\n",
10358 progname, argc == 0 ? "miss file target" :
10359 "input more than 2 files");
10364 attrs = (void *)buf;
10365 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10368 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10369 argv[0], strerror(errno), errno);
10373 lustre_som_swab(attrs);
10376 case LFS_SOM_ATTR_ALL:
10377 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10378 path, attrs->lsa_size, attrs->lsa_blocks,
10382 printf("%llu\n", attrs->lsa_size);
10384 case LFS_SOM_BLOCKS:
10385 printf("%llu\n", attrs->lsa_blocks);
10387 case LFS_SOM_FLAGS:
10388 printf("%x\n", attrs->lsa_valid);
10391 fprintf(stderr, "%s: unknown option\n", progname);
10399 * lfs_mirror_list_commands() - List lfs mirror commands.
10400 * @argc: The count of command line arguments.
10401 * @argv: Array of strings for command line arguments.
10403 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10405 * Return: 0 on success.
10407 static int lfs_mirror_list_commands(int argc, char **argv)
10409 char buffer[81] = "";
10411 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10417 static int lfs_pcc_attach(int argc, char **argv)
10419 struct option long_opts[] = {
10420 { .val = 'i', .name = "id", .has_arg = required_argument },
10421 { .name = NULL } };
10424 __u32 archive_id = 0;
10427 char fullpath[PATH_MAX];
10428 enum lu_pcc_type type = LU_PCC_READWRITE;
10431 while ((c = getopt_long(argc, argv, "i:",
10432 long_opts, NULL)) != -1) {
10435 archive_id = strtoul(optarg, &end, 0);
10436 if (*end != '\0' || archive_id == 0) {
10437 fprintf(stderr, "error: %s: bad archive ID "
10438 "'%s'\n", argv[0], optarg);
10445 fprintf(stderr, "%s: option '%s' unrecognized\n",
10446 argv[0], argv[optind - 1]);
10451 if (argc <= optind) {
10452 fprintf(stderr, "%s: must specify one or more file names\n",
10457 while (optind < argc) {
10460 path = argv[optind++];
10461 if (realpath(path, fullpath) == NULL) {
10462 fprintf(stderr, "%s: could not find path '%s': %s\n",
10463 argv[0], path, strerror(errno));
10469 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10471 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10472 "with archive ID '%u': %s\n", argv[0],
10473 path, archive_id, strerror(-rc2));
10481 static int lfs_pcc_attach_fid(int argc, char **argv)
10483 struct option long_opts[] = {
10484 { .val = 'i', .name = "id", .has_arg = required_argument },
10485 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10486 { .name = NULL } };
10487 char short_opts[] = "i:m:";
10490 __u32 archive_id = 0;
10492 const char *mntpath = NULL;
10493 const char *fidstr;
10494 enum lu_pcc_type type = LU_PCC_READWRITE;
10497 while ((c = getopt_long(argc, argv, short_opts,
10498 long_opts, NULL)) != -1) {
10501 archive_id = strtoul(optarg, &end, 0);
10502 if (*end != '\0') {
10503 fprintf(stderr, "error: %s: bad archive ID "
10504 "'%s'\n", argv[0], optarg);
10514 fprintf(stderr, "%s: option '%s' unrecognized\n",
10515 argv[0], argv[optind - 1]);
10520 if (archive_id == 0) {
10521 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10525 if (mntpath == NULL) {
10526 fprintf(stderr, "%s: must specify Lustre mount point\n",
10531 if (argc <= optind) {
10532 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10536 while (optind < argc) {
10539 fidstr = argv[optind++];
10541 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10544 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10545 "with archive ID '%u': %s\n", argv[0],
10546 fidstr, mntpath, archive_id, strerror(rc2));
10548 if (rc == 0 && rc2 < 0)
10554 static int lfs_pcc_detach(int argc, char **argv)
10556 struct option long_opts[] = {
10557 { .val = 'k', .name = "keep", .has_arg = no_argument },
10558 { .name = NULL } };
10559 char short_opts[] = "k";
10563 char fullpath[PATH_MAX];
10564 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10567 while ((c = getopt_long(argc, argv, short_opts,
10568 long_opts, NULL)) != -1) {
10571 detach_opt = PCC_DETACH_OPT_NONE;
10576 fprintf(stderr, "%s: option '%s' unrecognized\n",
10577 argv[0], argv[optind - 1]);
10582 while (optind < argc) {
10585 path = argv[optind++];
10586 if (realpath(path, fullpath) == NULL) {
10587 fprintf(stderr, "%s: could not find path '%s': %s\n",
10588 argv[0], path, strerror(errno));
10594 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
10597 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
10598 "%s\n", argv[0], path, strerror(errno));
10606 static int lfs_pcc_detach_fid(int argc, char **argv)
10608 struct option long_opts[] = {
10609 { .val = 'k', .name = "keep", .has_arg = no_argument },
10610 { .name = NULL } };
10611 char short_opts[] = "k";
10615 const char *mntpath;
10616 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10619 while ((c = getopt_long(argc, argv, short_opts,
10620 long_opts, NULL)) != -1) {
10623 detach_opt = PCC_DETACH_OPT_NONE;
10628 fprintf(stderr, "%s: option '%s' unrecognized\n",
10629 argv[0], argv[optind - 1]);
10634 mntpath = argv[optind++];
10636 while (optind < argc) {
10639 fid = argv[optind++];
10641 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
10643 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
10644 "from PCC: %s\n", argv[0], fid, mntpath,
10653 static int lfs_pcc_state(int argc, char **argv)
10657 char fullpath[PATH_MAX];
10658 struct lu_pcc_state state;
10663 fprintf(stderr, "%s: must specify one or more file names\n",
10668 while (optind < argc) {
10671 path = argv[optind++];
10672 if (realpath(path, fullpath) == NULL) {
10673 fprintf(stderr, "%s: could not find path '%s': %s\n",
10674 argv[0], path, strerror(errno));
10680 rc2 = llapi_pcc_state_get(fullpath, &state);
10684 fprintf(stderr, "%s: cannot get PCC state of '%s': "
10685 "%s\n", argv[0], path, strerror(-rc2));
10689 printf("file: %s", path);
10690 printf(", type: %s", pcc_type2string(state.pccs_type));
10691 if (state.pccs_type == LU_PCC_NONE &&
10692 state.pccs_open_count == 0) {
10697 printf(", PCC file: %s", state.pccs_path);
10698 printf(", user number: %u", state.pccs_open_count);
10699 printf(", flags: %x", state.pccs_flags);
10706 * lfs_pcc_list_commands() - List lfs pcc commands.
10707 * @argc: The count of command line arguments.
10708 * @argv: Array of strings for command line arguments.
10710 * This function lists lfs pcc commands defined in pcc_cmdlist[].
10712 * Return: 0 on success.
10714 static int lfs_pcc_list_commands(int argc, char **argv)
10716 char buffer[81] = "";
10718 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
10725 * lfs_pcc() - Parse and execute lfs pcc commands.
10726 * @argc: The count of lfs pcc command line arguments.
10727 * @argv: Array of strings for lfs pcc command line arguments.
10729 * This function parses lfs pcc commands and performs the
10730 * corresponding functions specified in pcc_cmdlist[].
10732 * Return: 0 on success or an error code on failure.
10734 static int lfs_pcc(int argc, char **argv)
10736 char cmd[PATH_MAX];
10739 setlinebuf(stdout);
10741 Parser_init("lfs-pcc > ", pcc_cmdlist);
10743 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10745 program_invocation_short_name = cmd;
10747 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
10749 rc = Parser_commands();
10751 return rc < 0 ? -rc : rc;
10754 static int lfs_list_commands(int argc, char **argv)
10756 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10758 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10763 int main(int argc, char **argv)
10767 /* Ensure that liblustreapi constructor has run */
10768 if (!llapi_liblustreapi_initialized())
10769 fprintf(stderr, "liblustreapi was not properly initialized\n");
10771 setlinebuf(stdout);
10774 Parser_init("lfs > ", cmdlist);
10776 progname = program_invocation_short_name; /* Used in error messages */
10778 llapi_set_command_name(argv[1]);
10779 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10780 llapi_clear_command_name();
10782 rc = Parser_commands();
10785 return rc < 0 ? -rc : rc;
10788 #ifdef _LUSTRE_IDL_H_
10789 /* Everything we need here should be included by lustreapi.h. */
10790 # error "lfs should not depend on lustre_idl.h"
10791 #endif /* _LUSTRE_IDL_H_ */