4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
61 #include <sys/xattr.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
81 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
82 #endif /* !ARRAY_SIZE */
85 static int lfs_find(int argc, char **argv);
86 static int lfs_getstripe(int argc, char **argv);
87 static int lfs_getdirstripe(int argc, char **argv);
88 static int lfs_setdirstripe(int argc, char **argv);
89 static int lfs_rmentry(int argc, char **argv);
90 static int lfs_osts(int argc, char **argv);
91 static int lfs_mdts(int argc, char **argv);
92 static int lfs_df(int argc, char **argv);
93 static int lfs_getname(int argc, char **argv);
94 static int lfs_check(int argc, char **argv);
95 #ifdef HAVE_SYS_QUOTA_H
96 static int lfs_setquota(int argc, char **argv);
97 static int lfs_quota(int argc, char **argv);
98 static int lfs_project(int argc, char **argv);
100 static int lfs_flushctx(int argc, char **argv);
101 static int lfs_poollist(int argc, char **argv);
102 static int lfs_changelog(int argc, char **argv);
103 static int lfs_changelog_clear(int argc, char **argv);
104 static int lfs_fid2path(int argc, char **argv);
105 static int lfs_path2fid(int argc, char **argv);
106 static int lfs_data_version(int argc, char **argv);
107 static int lfs_hsm_state(int argc, char **argv);
108 static int lfs_hsm_set(int argc, char **argv);
109 static int lfs_hsm_clear(int argc, char **argv);
110 static int lfs_hsm_action(int argc, char **argv);
111 static int lfs_hsm_archive(int argc, char **argv);
112 static int lfs_hsm_restore(int argc, char **argv);
113 static int lfs_hsm_release(int argc, char **argv);
114 static int lfs_hsm_remove(int argc, char **argv);
115 static int lfs_hsm_cancel(int argc, char **argv);
116 static int lfs_swap_layouts(int argc, char **argv);
117 static int lfs_mv(int argc, char **argv);
118 static int lfs_ladvise(int argc, char **argv);
119 static int lfs_getsom(int argc, char **argv);
120 static int lfs_heat_get(int argc, char **argv);
121 static int lfs_heat_set(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_read(int argc, char **argv);
128 static inline int lfs_mirror_write(int argc, char **argv);
129 static inline int lfs_mirror_copy(int argc, char **argv);
130 static int lfs_pcc_attach(int argc, char **argv);
131 static int lfs_pcc_attach_fid(int argc, char **argv);
132 static int lfs_pcc_detach(int argc, char **argv);
133 static int lfs_pcc_detach_fid(int argc, char **argv);
134 static int lfs_pcc_state(int argc, char **argv);
135 static int lfs_pcc(int argc, char **argv);
136 static int lfs_pcc_list_commands(int argc, char **argv);
138 enum setstripe_origin {
146 static int lfs_setstripe_internal(int argc, char **argv,
147 enum setstripe_origin opc);
149 static inline int lfs_setstripe(int argc, char **argv)
151 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
153 static inline int lfs_setstripe_migrate(int argc, char **argv)
155 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
157 static inline int lfs_mirror_create(int argc, char **argv)
159 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
161 static inline int lfs_mirror_extend(int argc, char **argv)
163 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
165 static inline int lfs_mirror_split(int argc, char **argv)
167 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
170 /* Setstripe and migrate share mostly the same parameters */
171 #define SSM_CMD_COMMON(cmd) \
172 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
173 " [--stripe-count|-c <stripe_count>]\n" \
174 " [--overstripe-count|-C <stripe_count>]\n" \
175 " [--stripe-index|-i <start_ost_idx>]\n" \
176 " [--stripe-size|-S <stripe_size>]\n" \
177 " [--layout|-L <pattern>]\n" \
178 " [--pool|-p <pool_name>]\n" \
179 " [--ost|-o <ost_indices>]\n" \
180 " [--yaml|-y <yaml_template_file>]\n" \
181 " [--copy=<lustre_src>]\n"
183 #define SSM_HELP_COMMON \
184 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
185 "\t Using -C instead of -c allows overstriping, which\n" \
186 "\t will place more than one stripe per OST if\n" \
187 "\t stripe_count is greater than the number of OSTs\n" \
188 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
189 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
190 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
191 "\t respectively)\n" \
192 "\tpool_name: Name of OST pool to use (default none)\n" \
193 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
194 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
195 "\t Indices be specified in a format of:\n" \
196 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
198 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
199 "\t If --pool is set with --ost, then the OSTs\n" \
200 "\t must be the members of the pool.\n" \
201 "\tcomp_end: Extent end of component, start after previous end.\n"\
202 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
203 "\t respectively, -1 for EOF). Must be a multiple of\n"\
204 "\t stripe_size.\n" \
205 "\tyaml_template_file:\n" \
206 "\t YAML layout template file, can't be used with -c,\n" \
207 "\t -i, -S, -p, -o, or -E arguments.\n" \
208 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
209 "\t another lustre file or directory, can't used with\n" \
210 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
212 #define MIRROR_CREATE_HELP \
213 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
214 "\t setstripe layout options\n" \
215 "\t It defaults to 1 if not specified; if specified,\n" \
216 "\t it must follow the option without a space.\n" \
217 "\t The option can also be repeated multiple times to\n" \
218 "\t separate mirrors that have different layouts.\n" \
219 "\tsetstripe options: Mirror layout\n" \
220 "\t It can be a plain layout or a composite layout.\n" \
221 "\t If not specified, the stripe options inherited\n" \
222 "\t from the previous component will be used.\n" \
223 "\tflags: set flags to the component of the current mirror.\n" \
224 "\t Only \"prefer\" flag is supported so far.\n"
226 #define MIRROR_EXTEND_HELP \
228 "\tvictim_file: The layout of victim_file will be split and used\n" \
229 "\t as a mirror added to the mirrored file.\n" \
230 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
231 "\t from victim file(s) in case the victim file(s)\n" \
232 "\t contains the same data as the original mirrored\n" \
235 #define MIRROR_EXTEND_USAGE \
236 " <--mirror-count|-N[mirror_count]>\n" \
237 " [setstripe options|-f <victim_file>]\n" \
240 #define SETSTRIPE_USAGE \
241 SSM_CMD_COMMON("setstripe") \
242 MIRROR_EXTEND_USAGE \
243 " <directory|filename>\n" \
247 #define MIGRATE_USAGE \
248 SSM_CMD_COMMON("migrate ") \
249 " [--block|-b] [--non-block|-n]\n" \
250 " [--non-direct|-D] [--verbose|-v]\n" \
254 "\tblock: Block file access during data migration (default)\n" \
255 "\tnon-block: Abort migrations if concurrent access is detected\n" \
256 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
257 "\tverbose: Print each filename as it is migrated\n" \
259 #define SETDIRSTRIPE_USAGE \
260 " [--mdt-count|-c stripe_count>\n" \
261 " [--mdt-hash|-H mdt_hash]\n" \
262 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
263 " [--default|-D] [--mode|-o mode] <dir>\n" \
264 "\tstripe_count: stripe count of the striped directory\n" \
265 "\tmdt_index: MDT index of first stripe\n" \
266 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
267 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
268 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
269 " space create subdirectories with balanced space usage\n" \
270 "\tdefault_stripe: set default dirstripe of the directory\n" \
271 "\tmode: the file access permission of the directory (octal)\n" \
272 "To create dir with a foreign (free format) layout :\n" \
273 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
274 "[--mode|-m mode] [--flags <hex>] <dir>\n" \
275 "\tmode: the mode of the directory\n" \
276 "\tforeign_type: none or daos\n"
279 * command_t mirror_cmdlist - lfs mirror commands.
281 command_t mirror_cmdlist[] = {
282 { .pc_name = "create", .pc_func = lfs_mirror_create,
283 .pc_help = "Create a mirrored file.\n"
284 "usage: lfs mirror create "
285 "<--mirror-count|-N[mirror_count]> "
286 "[setstripe options] ... <filename|directory>\n"
287 MIRROR_CREATE_HELP },
288 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
289 .pc_help = "Extend a mirrored file.\n"
290 "usage: lfs mirror extend "
291 "<--mirror-count|-N[mirror_count]> [--no-verify] "
292 "[setstripe options|-f <victim_file>] ... <filename>\n"
293 MIRROR_EXTEND_HELP },
294 { .pc_name = "split", .pc_func = lfs_mirror_split,
295 .pc_help = "Split a mirrored file.\n"
296 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
297 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
298 "\t [-f <new_file>] <mirrored file>\n"
299 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
300 "\t can be fetched by lfs getstripe command.\n"
301 "\tcomp_id: Unique component ID within a mirror.\n"
302 "\tnew_file: This option indicates the layout of the split\n"
303 "\t mirror will be stored into. If not specified,\n"
304 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
305 "\t will be used.\n" },
306 { .pc_name = "read", .pc_func = lfs_mirror_read,
307 .pc_help = "Read the content of a specified mirror of a file.\n"
308 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
309 "[--outfile|-o <output_file>] <mirrored_file>\n" },
310 { .pc_name = "write", .pc_func = lfs_mirror_write,
311 .pc_help = "Write to a specified mirror of a file.\n"
312 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
313 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
314 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
315 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
316 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
317 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
318 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
319 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
320 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
321 "<mirrored file> [<mirrored file2>...]\n"},
322 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
323 .pc_help = "Verify mirrored file(s).\n"
324 "usage: lfs mirror verify "
325 "[--only <mirror_id,mirror_id2[,...]>] "
326 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
327 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
328 .pc_help = "list commands supported by lfs mirror"},
329 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
330 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
331 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
336 * command_t pcc_cmdlist - lfs pcc commands.
338 command_t pcc_cmdlist[] = {
339 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
340 .pc_help = "Attach given files to the Persistent Client Cache.\n"
341 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
342 "\t-i: archive id for RW-PCC\n" },
343 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
344 .pc_help = "Attach given files into PCC by FID(s).\n"
345 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
347 "\t-i: archive id for RW-PCC\n"
348 "\t-m: Lustre mount point\n" },
349 { .pc_name = "state", .pc_func = lfs_pcc_state,
350 .pc_help = "Display the PCC state for given files.\n"
351 "usage: lfs pcc state <file> ...\n" },
352 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
353 .pc_help = "Detach given files from the Persistent Client Cache.\n"
354 "usage: lfs pcc detach <file> ...\n" },
355 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
356 .pc_help = "Detach given files from PCC by FID(s).\n"
357 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
358 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
359 .pc_help = "list commands supported by lfs pcc"},
360 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
361 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
362 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
366 /* all available commands */
367 command_t cmdlist[] = {
368 {"setstripe", lfs_setstripe, 0,
369 "To create a file with specified striping/composite layout, or\n"
370 "create/replace the default layout on an existing directory:\n"
371 SSM_CMD_COMMON("setstripe")
373 " <directory|filename>\n"
375 "To add component(s) to an existing composite file:\n"
376 SSM_CMD_COMMON("setstripe --component-add")
378 "To totally delete the default striping from an existing directory:\n"
379 "usage: setstripe -d <directory>\n"
381 "To create a mirrored file or set s default mirror layout on a directory:\n"
382 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
384 "To delete the last component(s) from an existing composite file\n"
385 "(note that this will also delete any data in those components):\n"
386 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
387 " [--component-flags|-F <comp_flags>]\n"
389 "\tcomp_id: Unique component ID to delete\n"
390 "\tcomp_flags: 'init' indicating all instantiated components\n"
391 "\t '^init' indicating all uninstantiated components\n"
392 "\t-I and -F cannot be specified at the same time\n"
393 "To create a file with a foreign (free format) layout:\n"
394 "usage: setstripe --foreign[=<foreign_type>]\n"
395 " --xattr|-x <layout_string> [--flags <hex>]\n"
396 " [--mode <mode>] <filename>\n"},
397 {"getstripe", lfs_getstripe, 0,
398 "To list the layout pattern for a given file or files in a\n"
399 "directory or recursively for all files in a directory tree.\n"
400 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
401 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
402 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
403 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
404 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
405 " [--component-id[=comp_id]|-I[comp_id]]\n"
406 " [--component-flags[=comp_flags]]\n"
407 " [--component-count]\n"
408 " [--component-start[=[+-]comp_start]]\n"
409 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
410 " [[!] --mirror-index=[+-]<index> |\n"
411 " [!] --mirror-id=[+-]<id>]\n"
412 " <directory|filename> ..."},
413 {"setdirstripe", lfs_setdirstripe, 0,
414 "To create a striped directory on a specified MDT. This can only\n"
415 "be done on MDT0 with the right of administrator.\n"
416 "usage: setdirstripe [OPTION] <directory>\n"
418 {"getdirstripe", lfs_getdirstripe, 0,
419 "To list the layout pattern info for a given directory\n"
420 "or recursively for all directories in a directory tree.\n"
421 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
422 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
423 " [--recursive|-r] [--yaml|-y]\n"
424 " [--verbose|-v] [--default|-D] <dir> ..."},
425 {"mkdir", lfs_setdirstripe, 0,
426 "To create a striped directory on a specified MDT. This can only\n"
427 "be done on MDT0 with the right of administrator.\n"
428 "usage: mkdir [OPTION] <directory>\n"
430 {"rm_entry", lfs_rmentry, 0,
431 "To remove the name entry of the remote directory. Note: This\n"
432 "command will only delete the name entry, i.e. the remote directory\n"
433 "will become inaccessable after this command. This can only be done\n"
434 "by the administrator\n"
435 "usage: rm_entry <dir>\n"},
436 {"pool_list", lfs_poollist, 0,
437 "List pools or pool OSTs\n"
438 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
439 {"find", lfs_find, 0,
440 "find files matching given attributes recursively in directory tree.\n"
441 "usage: find <directory|filename> ...\n"
442 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
443 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
444 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
445 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
446 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
447 " [[!] --stripe-count|-c [+-]<stripes>]\n"
448 " [[!] --stripe-index|-i <index,...>]\n"
449 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
450 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
451 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
452 " [[!] --projid <projid>]\n"
453 " [[!] --foreign[=<foreign_type>]]\n"
454 " [[!] --layout|-L released,raid0,mdt]\n"
455 " [[!] --foreign[=<foreign_type>]]\n"
456 " [[!] --component-count [+-]<comp_cnt>]\n"
457 " [[!] --component-start [+-]N[kMGTPE]]\n"
458 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
459 " [[!] --component-flags <comp_flags>]\n"
460 " [[!] --mirror-count|-N [+-]<n>]\n"
461 " [[!] --mirror-state <[^]state>]\n"
462 " [[!] --mdt-count|-T [+-]<stripes>]\n"
463 " [[!] --mdt-hash|-H <hashtype>\n"
464 " [[!] --mdt-index|-m <uuid|index,...>]\n"
465 "\t !: used before an option indicates 'NOT' requested attribute\n"
466 "\t -: used before a value indicates less than requested value\n"
467 "\t +: used before a value indicates more than requested value\n"
468 "\thashtype: hash type of the striped directory.\n"
469 "\t fnv_1a_64 FNV-1a hash algorithm\n"
470 "\t all_char sum of characters % MDT_COUNT\n"},
471 {"check", lfs_check, 0,
472 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
473 "or all the servers (MGTs, MDTs and OSTs).\n"
474 "usage: check <mgts|osts|mdts|all>"},
475 {"osts", lfs_osts, 0, "list OSTs connected to client "
476 "[for specified path only]\n" "usage: osts [path]"},
477 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
478 "[for specified path only]\n" "usage: mdts [path]"},
480 "report filesystem disk space usage or inodes usage "
481 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
482 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
483 {"getname", lfs_getname, 0,
484 "list instances and specified mount points [for specified path only]\n"
485 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
486 #ifdef HAVE_SYS_QUOTA_H
487 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
488 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
489 " -b <block-softlimit> -B <block-hardlimit>\n"
490 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
491 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
492 " [--block-softlimit <block-softlimit>]\n"
493 " [--block-hardlimit <block-hardlimit>]\n"
494 " [--inode-softlimit <inode-softlimit>]\n"
495 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
496 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
497 " [--block-grace <block-grace>]\n"
498 " [--inode-grace <inode-grace>] <filesystem>\n"
499 " setquota <-U|-G|-P>\n"
500 " -b <block-softlimit> -B <block-hardlimit>\n"
501 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
502 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
503 " [--block-softlimit <block-softlimit>]\n"
504 " [--block-hardlimit <block-hardlimit>]\n"
505 " [--inode-softlimit <inode-softlimit>]\n"
506 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
507 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
509 " -b can be used instead of --block-softlimit/--block-grace\n"
510 " -B can be used instead of --block-hardlimit\n"
511 " -i can be used instead of --inode-softlimit/--inode-grace\n"
512 " -I can be used instead of --inode-hardlimit\n"
513 " -d can be used instead of --default\n\n"
514 "Note: The total quota space will be split into many qunits and\n"
515 " balanced over all server targets, the minimal qunit size is\n"
516 " 1M bytes for block space and 1K inodes for inode space.\n\n"
517 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
518 " Quota space rebalancing process will stop when this mininum\n"
519 " value is reached. As a result, quota exceeded can be returned\n"
520 " while many targets still have 1MB or 1K inodes of spare\n"
522 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
523 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
525 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
526 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
527 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
528 {"project", lfs_project, 0,
529 "Change or list project attribute for specified file or directory.\n"
530 "usage: project [-d|-r] <file|directory...>\n"
531 " list project ID and flags on file(s) or directories\n"
532 " project [-p id] [-s] [-r] <file|directory...>\n"
533 " set project ID and/or inherit flag for specified file(s) or directories\n"
534 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
535 " check project ID and flags on file(s) or directories, print outliers\n"
536 " project -C [-r] [-k] <file|directory...>\n"
537 " clear the project inherit flag and ID on the file or directory\n"
540 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
541 "usage: flushctx [-k] [mountpoint...]"},
542 {"changelog", lfs_changelog, 0,
543 "Show the metadata changes on an MDT."
544 "\nusage: changelog <mdtname> [startrec [endrec]]"},
545 {"changelog_clear", lfs_changelog_clear, 0,
546 "Indicate that old changelog records up to <endrec> are no longer of "
547 "interest to consumer <id>, allowing the system to free up space.\n"
548 "An <endrec> of 0 means all records.\n"
549 "usage: changelog_clear <mdtname> <id> <endrec>"},
550 {"fid2path", lfs_fid2path, 0,
551 "Resolve the full path(s) for given FID(s). For a specific hardlink "
552 "specify link number <linkno>.\n"
553 /* "For a historical link name, specify changelog record <recno>.\n" */
554 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
555 /* [ --rec <recno> ] */ },
556 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
557 "usage: path2fid [--parents] <path> ..."},
558 {"data_version", lfs_data_version, 0, "Display file data version for "
559 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
560 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
561 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
562 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
563 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
564 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
565 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
567 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
568 "[--archived] [--lost] <file> ..."},
569 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
570 "given files.\n" "usage: hsm_action <file> ..."},
571 {"hsm_archive", lfs_hsm_archive, 0,
572 "Archive file to external storage.\n"
573 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
575 {"hsm_restore", lfs_hsm_restore, 0,
576 "Restore file from external storage.\n"
577 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
578 {"hsm_release", lfs_hsm_release, 0,
579 "Release files from Lustre.\n"
580 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
581 {"hsm_remove", lfs_hsm_remove, 0,
582 "Remove file copy from external storage.\n"
583 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
585 " (FILE [FILE ...] | "
586 "--mntpath MOUNTPATH FID [FID ...])\n"
588 "Note: To remove an archived copy of a file already deleted from a "
590 "--mntpath option and a list of FIDs must be specified"
592 {"hsm_cancel", lfs_hsm_cancel, 0,
593 "Cancel requests related to specified files.\n"
594 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
595 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
596 "usage: swap_layouts <path1> <path2>"},
597 {"migrate", lfs_setstripe_migrate, 0,
598 "migrate a directory between MDTs.\n"
599 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
600 " [--mdt-hash|-H] <hash_type>\n"
601 " [--mdt-index|-m] <start_mdt_index>\n"
604 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
605 " it's the MDT index of first stripe\n"
606 "\tmdt_count: number of MDTs to stripe a directory over\n"
607 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
608 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
609 " all_char sum of characters % MDT_COUNT\n"
611 "migrate file objects from one OST "
612 "layout\nto another (may be not safe with concurent writes).\n"
614 "[--stripe-count|-c] <stripe_count>\n"
615 "[--overstripe-count|-C] <stripe_count>\n"
616 " [--stripe-index|-i] <start_ost_index>\n"
617 " [--stripe-size|-S] <stripe_size>\n"
618 " [--pool|-p] <pool_name>\n"
619 " [--ost|-o] <ost_indices>\n"
621 " [--non-block|-n]\n"
622 " [--non-direct|-D]\n"
623 " <file|directory>\n"
624 "\tstripe_count: number of OSTs to stripe a file over\n"
625 "\t Using -C instead of -c allows overstriping, which\n"
626 "\t will place more than one stripe per OST if\n"
627 "\t stripe_count is greater than the number of OSTs\n"
628 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
629 "\tstripe_size: number of bytes to store before moving to the next OST\n"
630 "\tpool_name: name of the predefined pool of OSTs\n"
631 "\tost_indices: OSTs to stripe over, in order\n"
632 "\tblock: Block file access during data migration (default)\n"
633 "\tnon-block: Abort migrations if concurrent access is detected\n"
634 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
636 "To move directories between MDTs. This command is deprecated, "
637 "use \"migrate\" instead.\n"
638 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
640 {"ladvise", lfs_ladvise, 0,
641 "Provide servers with advice about access patterns for a file.\n"
642 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
643 " [--background|-b] [--unset|-u]\n\n"
644 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
645 " {[--mode|-m [READ,WRITE]}\n"
647 {"mirror", lfs_mirror, mirror_cmdlist,
648 "lfs commands used to manage files with mirrored components:\n"
649 "lfs mirror create - create a mirrored file or directory\n"
650 "lfs mirror extend - add mirror(s) to an existing file\n"
651 "lfs mirror split - split a mirror from an existing mirrored file\n"
652 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
653 "lfs mirror read - read a mirror content of a mirrored file\n"
654 "lfs mirror write - write to a mirror of a mirrored file\n"
655 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
656 "lfs mirror verify - verify mirrored file(s)\n"},
657 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
658 "usage: getsom [-s] [-b] [-f] <path>\n"
659 "\t-s: Only show the size value of the SOM data for a given file\n"
660 "\t-b: Only show the blocks value of the SOM data for a given file\n"
661 "\t-f: Only show the flags value of the SOM data for a given file\n"},
662 {"heat_get", lfs_heat_get, 0,
663 "To get heat of files.\n"
664 "usage: heat_get <file> ...\n"},
665 {"heat_set", lfs_heat_set, 0,
666 "To set heat flags of files.\n"
667 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
668 "\t--clear|-c: Clear file heat for given files\n"
669 "\t--off|-o: Turn off file heat for given files\n"
670 "\t--on|-O: Turn on file heat for given files\n"},
671 {"pcc", lfs_pcc, pcc_cmdlist,
672 "lfs commands used to interact with PCC features:\n"
673 "lfs pcc attach - attach given files to Persistent Client Cache\n"
674 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
675 "lfs pcc state - display the PCC state for given files\n"
676 "lfs pcc detach - detach given files from Persistent Client Cache\n"
677 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
678 {"help", Parser_help, 0, "help"},
679 {"exit", Parser_quit, 0, "quit"},
680 {"quit", Parser_quit, 0, "quit"},
681 {"--version", Parser_version, 0,
682 "output build version of the utility and exit"},
683 {"--list-commands", lfs_list_commands, 0,
684 "list commands supported by the utility and exit"},
689 static int check_hashtype(const char *hashtype)
693 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
694 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
697 if (!strcmp(hashtype, LMV_HASH_NAME_SPACE))
698 return LMV_HASH_TYPE_DEFAULT | LMV_HASH_FLAG_SPACE;
703 static uint32_t check_foreign_type_name(const char *foreign_type_name)
707 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
708 if (lu_foreign_types[i].lft_name == NULL)
710 if (strcmp(foreign_type_name,
711 lu_foreign_types[i].lft_name) == 0)
712 return lu_foreign_types[i].lft_type;
715 return LU_FOREIGN_TYPE_UNKNOWN;
718 static const char *error_loc = "syserror";
721 MIGRATION_NONBLOCK = 0x0001,
722 MIGRATION_MIRROR = 0x0002,
723 MIGRATION_NONDIRECT = 0x0004,
724 MIGRATION_VERBOSE = 0x0008,
727 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
728 struct llapi_layout *layout);
731 migrate_open_files(const char *name, __u64 migration_flags,
732 const struct llapi_stripe_param *param,
733 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
740 char parent[PATH_MAX];
741 char volatile_file[PATH_MAX];
747 if (param == NULL && layout == NULL) {
748 error_loc = "layout information";
752 /* search for file directory pathname */
753 if (strlen(name) > sizeof(parent) - 1) {
754 error_loc = "source file name";
758 strncpy(parent, name, sizeof(parent));
759 ptr = strrchr(parent, '/');
761 if (getcwd(parent, sizeof(parent)) == NULL) {
762 error_loc = "getcwd";
766 if (ptr == parent) /* leading '/' */
771 /* open file, direct io */
772 /* even if the file is only read, WR mode is nedeed to allow
773 * layout swap on fd */
775 if (!(migration_flags & MIGRATION_NONDIRECT))
777 fd = open(name, rflags);
780 error_loc = "cannot open source file";
784 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
786 error_loc = "cannot get MDT index";
791 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
792 mode_t open_mode = S_IRUSR | S_IWUSR;
794 random_value = random();
795 rc = snprintf(volatile_file, sizeof(volatile_file),
796 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
797 mdt_index, random_value);
798 if (rc >= sizeof(volatile_file)) {
803 /* create, open a volatile file, use caching (ie no directio) */
805 fdv = llapi_file_open_param(volatile_file, open_flags,
808 fdv = lfs_component_create(volatile_file, open_flags,
810 } while (fdv < 0 && (rc = fdv) == -EEXIST);
813 error_loc = "cannot create volatile file";
817 /* In case the MDT does not support creation of volatile files
818 * we should try to unlink it. */
819 (void)unlink(volatile_file);
821 /* Not-owner (root?) special case.
822 * Need to set owner/group of volatile file like original.
823 * This will allow to pass related check during layout_swap.
828 error_loc = "cannot stat source file";
832 rc = fstat(fdv, &stv);
835 error_loc = "cannot stat volatile";
839 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
840 rc = fchown(fdv, st.st_uid, st.st_gid);
843 error_loc = "cannot change ownwership of volatile";
862 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
864 struct llapi_layout *layout;
865 size_t buf_size = 4 * 1024 * 1024;
874 layout = llapi_layout_get_by_fd(fd_src, 0);
875 if (layout != NULL) {
876 uint64_t stripe_size;
878 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
880 buf_size = stripe_size;
882 llapi_layout_free(layout);
885 /* Use a page-aligned buffer for direct I/O */
886 rc = posix_memalign(&buf, getpagesize(), buf_size);
891 /* read new data only if we have written all
892 * previously read data */
895 rc = check_file(fd_src);
900 rsize = read(fd_src, buf, buf_size);
912 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
931 static int migrate_copy_timestamps(int fd, int fdv)
935 if (fstat(fd, &st) == 0) {
936 struct timeval tv[2] = {
937 {.tv_sec = st.st_atime},
938 {.tv_sec = st.st_mtime}
941 return futimes(fdv, tv);
947 static int migrate_block(int fd, int fdv)
954 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
956 error_loc = "cannot get dataversion";
964 /* The grouplock blocks all concurrent accesses to the file.
965 * It has to be taken after llapi_get_data_version as it would
967 rc = llapi_group_lock(fd, gid);
969 error_loc = "cannot get group lock";
973 rc = migrate_copy_data(fd, fdv, NULL);
975 error_loc = "data copy failed";
979 /* Make sure we keep original atime/mtime values */
980 rc = migrate_copy_timestamps(fd, fdv);
982 error_loc = "timestamp copy failed";
987 * for a migration we need to check data version on file did
990 * Pass in gid=0 since we already own grouplock. */
991 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
992 SWAP_LAYOUTS_CHECK_DV1);
994 error_loc = "file changed";
997 error_loc = "cannot swap layout";
1002 rc2 = llapi_group_unlock(fd, gid);
1003 if (rc2 < 0 && rc == 0) {
1004 error_loc = "unlock group lock";
1012 * Internal helper for migrate_copy_data(). Check lease and report error if
1015 * \param[in] fd File descriptor on which to check the lease.
1017 * \retval 0 Migration can keep on going.
1018 * \retval -errno Error occurred, abort migration.
1020 static int check_lease(int fd)
1024 rc = llapi_lease_check(fd);
1026 return 0; /* llapi_check_lease returns > 0 on success. */
1031 static int migrate_nonblock(int fd, int fdv)
1037 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1039 error_loc = "cannot get data version";
1043 rc = migrate_copy_data(fd, fdv, check_lease);
1045 error_loc = "data copy failed";
1049 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1051 error_loc = "cannot get data version";
1057 error_loc = "source file changed";
1061 /* Make sure we keep original atime/mtime values */
1062 rc = migrate_copy_timestamps(fd, fdv);
1064 error_loc = "timestamp copy failed";
1071 static int lfs_component_set(char *fname, int comp_id,
1072 __u32 flags, __u32 neg_flags)
1075 __u32 flags_array[2];
1080 ids[count] = comp_id;
1081 flags_array[count] = flags;
1086 if (neg_flags & LCME_FL_STALE) {
1087 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1088 "component. Please use lfs-mirror-resync(1) "
1089 "instead\n", progname);
1093 ids[count] = comp_id;
1094 flags_array[count] = neg_flags | LCME_FL_NEG;
1098 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1101 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1102 progname, comp_id, fname, flags, neg_flags);
1107 static int lfs_component_del(char *fname, __u32 comp_id,
1108 __u32 flags, __u32 neg_flags)
1112 if (flags && neg_flags)
1115 if (!flags && neg_flags)
1116 flags = neg_flags | LCME_FL_NEG;
1118 if ((flags && comp_id) || (!flags && !comp_id))
1122 if (flags & ~LCME_KNOWN_FLAGS) {
1124 "%s setstripe: unknown flags %#x\n",
1128 } else if (comp_id > LCME_ID_MAX) {
1129 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1134 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1137 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1138 progname, comp_id, fname, strerror(errno));
1142 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1149 rc = llapi_layout_file_comp_add(fname, layout);
1151 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1152 fname, strerror(errno));
1156 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1157 struct llapi_layout *layout)
1165 fd = lstat(fname, &st);
1166 if (fd == 0 && S_ISDIR(st.st_mode))
1167 open_flags = O_DIRECTORY | O_RDONLY;
1169 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1171 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1172 S_ISDIR(st.st_mode) ?
1173 "set default composite layout for" :
1174 "create composite file",
1175 fname, strerror(errno));
1179 static int lfs_migrate(char *name, __u64 migration_flags,
1180 struct llapi_stripe_param *param,
1181 struct llapi_layout *layout)
1187 rc = migrate_open_files(name, migration_flags, param, layout,
1192 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1193 /* Blocking mode (forced if servers do not support file lease).
1194 * It is also the default mode, since we cannot distinguish
1195 * between a broken lease and a server that does not support
1196 * atomic swap/close (LU-6785) */
1197 rc = migrate_block(fd, fdv);
1201 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1203 error_loc = "cannot get lease";
1207 rc = migrate_nonblock(fd, fdv);
1209 llapi_lease_release(fd);
1213 /* Atomically put lease, swap layouts and close.
1214 * for a migration we need to check data version on file did
1216 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1218 error_loc = "cannot swap layout";
1230 fprintf(stderr, "error: %s: %s: %s: %s\n",
1231 progname, name, error_loc, strerror(-rc));
1232 else if (migration_flags & MIGRATION_VERBOSE)
1233 printf("%s\n", name);
1238 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1247 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1251 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1252 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1253 const char *comp_name = comp_flags_table[i].cfn_name;
1255 if (strcmp(name, comp_name) == 0) {
1256 *flags |= comp_flag;
1258 } else if (strncmp(name, "^", 1) == 0 &&
1259 strcmp(name + 1, comp_name) == 0) {
1260 *neg_flags |= comp_flag;
1265 llapi_printf(LLAPI_MSG_ERROR,
1266 "%s: component flag '%s' not supported\n",
1272 if (!*flags && !*neg_flags)
1275 /* don't allow to set and exclude the same flag */
1276 if (*flags & *neg_flags)
1282 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1290 if (strncmp(string, "^", 1) == 0) {
1291 *neg_state = llapi_layout_string_flags(string + 1);
1292 if (*neg_state != 0)
1295 *state = llapi_layout_string_flags(string);
1300 llapi_printf(LLAPI_MSG_ERROR,
1301 "%s: mirrored file state '%s' not supported\n",
1307 * struct mirror_args - Command-line arguments for mirror(s).
1308 * @m_count: Number of mirrors to be created with this layout.
1309 * @m_flags: Mirror level flags, only 'prefer' is supported.
1310 * @m_layout: Mirror layout.
1311 * @m_file: A victim file. Its layout will be split and used as a mirror.
1312 * @m_next: Point to the next node of the list.
1314 * Command-line arguments for mirror(s) will be parsed and stored in
1315 * a linked list that consists of this structure.
1317 struct mirror_args {
1320 struct llapi_layout *m_layout;
1322 struct mirror_args *m_next;
1326 * enum mirror_flags - Flags for extending a mirrored file.
1327 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1328 * in case the victim file(s) contains the same data as the
1329 * original mirrored file.
1330 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1331 * @MF_COMP_ID: specified component id instead of mirror id
1333 * Flags for extending a mirrored file.
1342 * mirror_create_sanity_check() - Check mirror list.
1343 * @list: A linked list that stores the mirror arguments.
1345 * This function does a sanity check on @list for creating
1348 * Return: 0 on success or a negative error code on failure.
1350 static int mirror_create_sanity_check(const char *fname,
1351 struct mirror_args *list)
1354 bool has_m_file = false;
1355 bool has_m_layout = false;
1361 struct llapi_layout *layout;
1363 layout = llapi_layout_get_by_path(fname, 0);
1366 "error: %s: file '%s' couldn't get layout\n",
1371 rc = llapi_layout_sanity(layout, false, true);
1373 llapi_layout_free(layout);
1376 llapi_layout_sanity_perror(rc);
1381 while (list != NULL) {
1382 if (list->m_file != NULL) {
1384 llapi_layout_free(list->m_layout);
1387 llapi_layout_get_by_path(list->m_file, 0);
1388 if (list->m_layout == NULL) {
1390 "error: %s: file '%s' has no layout\n",
1391 progname, list->m_file);
1395 has_m_layout = true;
1396 if (list->m_layout == NULL) {
1397 fprintf(stderr, "error: %s: no mirror layout\n",
1403 rc = llapi_layout_sanity(list->m_layout, false, true);
1405 llapi_layout_sanity_perror(rc);
1409 list = list->m_next;
1412 if (has_m_file && has_m_layout) {
1414 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1422 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1424 __u32 mirror_flags = *(__u32 *)cbdata;
1428 rc = llapi_layout_comp_flags_get(layout, &flags);
1433 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1438 return LLAPI_LAYOUT_ITER_CONT;
1442 * mirror_create() - Create a mirrored file.
1443 * @fname: The file to be created.
1444 * @mirror_list: A linked list that stores the mirror arguments.
1446 * This function creates a mirrored file @fname with the mirror(s)
1447 * from @mirror_list.
1449 * Return: 0 on success or a negative error code on failure.
1451 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1453 struct llapi_layout *layout = NULL;
1454 struct mirror_args *cur_mirror = NULL;
1455 uint16_t mirror_count = 0;
1459 rc = mirror_create_sanity_check(NULL, mirror_list);
1463 cur_mirror = mirror_list;
1464 while (cur_mirror != NULL) {
1465 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1467 &cur_mirror->m_flags);
1470 fprintf(stderr, "%s: failed to set mirror flags\n",
1475 for (i = 0; i < cur_mirror->m_count; i++) {
1476 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1479 fprintf(stderr, "error: %s: "
1480 "merge layout failed: %s\n",
1481 progname, strerror(errno));
1485 mirror_count += cur_mirror->m_count;
1486 cur_mirror = cur_mirror->m_next;
1489 if (layout == NULL) {
1490 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1494 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1497 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1498 progname, strerror(errno));
1502 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1510 llapi_layout_free(layout);
1515 * Compare files and check lease on @fd.
1517 * \retval bytes number of bytes are the same
1519 static ssize_t mirror_file_compare(int fd, int fdv)
1521 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1523 ssize_t bytes_done = 0;
1524 ssize_t bytes_read = 0;
1526 buf = malloc(buflen * 2);
1531 if (!llapi_lease_check(fd)) {
1532 bytes_done = -EBUSY;
1536 bytes_read = read(fd, buf, buflen);
1537 if (bytes_read <= 0)
1540 if (bytes_read != read(fdv, buf + buflen, buflen))
1543 /* XXX: should compute the checksum on each buffer and then
1544 * compare checksum to avoid cache collision */
1545 if (memcmp(buf, buf + buflen, bytes_read))
1548 bytes_done += bytes_read;
1556 static int mirror_extend_file(const char *fname, const char *victim_file,
1557 enum mirror_flags mirror_flags)
1562 struct stat stbuf_v;
1563 struct ll_ioc_lease *data = NULL;
1566 fd = open(fname, O_RDWR);
1568 error_loc = "open source file";
1573 fdv = open(victim_file, O_RDWR);
1575 error_loc = "open target file";
1580 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1581 error_loc = "stat source or target file";
1586 if (stbuf.st_dev != stbuf_v.st_dev) {
1587 error_loc = "stat source and target file";
1592 /* mirrors should be of the same size */
1593 if (stbuf.st_size != stbuf_v.st_size) {
1594 error_loc = "file sizes don't match";
1599 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1601 error_loc = "cannot get lease";
1605 if (!(mirror_flags & MF_NO_VERIFY)) {
1607 /* mirrors should have the same contents */
1608 ret = mirror_file_compare(fd, fdv);
1609 if (ret != stbuf.st_size) {
1610 error_loc = "file busy or contents don't match";
1611 rc = ret < 0 ? ret : -EINVAL;
1616 /* Get rid of caching pages from clients */
1617 rc = llapi_file_flush(fd);
1619 error_loc = "cannot get data version";
1623 rc = llapi_file_flush(fdv);
1625 error_loc = "cannot get data version";
1630 /* Make sure we keep original atime/mtime values */
1631 rc = migrate_copy_timestamps(fd, fdv);
1633 error_loc = "cannot copy timestamp";
1637 /* Atomically put lease, merge layouts and close. */
1638 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1640 error_loc = "memory allocation";
1643 data->lil_mode = LL_LEASE_UNLCK;
1644 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1645 data->lil_count = 1;
1646 data->lil_ids[0] = fdv;
1647 rc = llapi_lease_set(fd, data);
1649 error_loc = "cannot merge layout";
1651 } else if (rc == 0) {
1653 error_loc = "lost lease lock";
1666 (void) unlink(victim_file);
1668 fprintf(stderr, "error: %s: %s: %s: %s\n",
1669 progname, fname, error_loc, strerror(-rc));
1673 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1675 struct ll_ioc_lease *data = NULL;
1680 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1684 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1686 error_loc = "cannot get lease";
1690 rc = migrate_nonblock(fd, fdv);
1692 llapi_lease_release(fd);
1696 /* Atomically put lease, merge layouts and close. */
1697 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1699 error_loc = "memory allocation";
1702 data->lil_mode = LL_LEASE_UNLCK;
1703 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1704 data->lil_count = 1;
1705 data->lil_ids[0] = fdv;
1706 rc = llapi_lease_set(fd, data);
1708 error_loc = "cannot merge layout";
1710 } else if (rc == 0) {
1712 error_loc = "lost lease lock";
1725 fprintf(stderr, "error: %s: %s: %s: %s\n",
1726 progname, name, error_loc, strerror(-rc));
1730 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1731 enum mirror_flags mirror_flags)
1735 rc = mirror_create_sanity_check(fname, mirror_list);
1739 while (mirror_list) {
1740 if (mirror_list->m_file != NULL) {
1741 rc = mirror_extend_file(fname, mirror_list->m_file,
1744 __u32 mirror_count = mirror_list->m_count;
1746 while (mirror_count > 0) {
1747 rc = mirror_extend_layout(fname,
1748 mirror_list->m_layout);
1758 mirror_list = mirror_list->m_next;
1764 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1769 rc = llapi_layout_mirror_id_get(layout, &id);
1773 if ((__u16)id == *(__u16 *)cbdata)
1774 return LLAPI_LAYOUT_ITER_STOP;
1776 return LLAPI_LAYOUT_ITER_CONT;
1779 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1784 rc = llapi_layout_comp_id_get(layout, &id);
1788 if (id == *(__u32 *)cbdata)
1789 return LLAPI_LAYOUT_ITER_STOP;
1791 return LLAPI_LAYOUT_ITER_CONT;
1793 static int mirror_split(const char *fname, __u32 id,
1794 enum mirror_flags mflags, const char *victim_file)
1796 struct llapi_layout *layout;
1797 char parent[PATH_MAX];
1798 char victim[PATH_MAX];
1799 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1801 struct ll_ioc_lease *data;
1802 uint16_t mirror_count;
1807 /* check fname contains mirror with mirror_id/comp_id */
1808 layout = llapi_layout_get_by_path(fname, 0);
1811 "error %s: file '%s' couldn't get layout\n",
1816 rc = llapi_layout_sanity(layout, false, true);
1818 llapi_layout_sanity_perror(rc);
1822 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1825 "error %s: file '%s' couldn't get mirror count\n",
1829 if (mirror_count < 2) {
1831 "error %s: file '%s' has %d component, cannot split\n",
1832 progname, fname, mirror_count);
1836 if (mflags & MF_COMP_ID) {
1837 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1838 id = mirror_id_of(id);
1840 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1843 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1846 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1848 "error %s: file '%s' does not contain mirror with id %u\n",
1849 progname, fname, id);
1853 fd = open(fname, O_RDWR);
1856 "error %s: open file '%s' failed: %s\n",
1857 progname, fname, strerror(errno));
1861 /* get victim file directory pathname */
1862 if (strlen(fname) > sizeof(parent) - 1) {
1863 fprintf(stderr, "error %s: file name of '%s' too long\n",
1868 strncpy(parent, fname, sizeof(parent));
1869 ptr = strrchr(parent, '/');
1871 if (getcwd(parent, sizeof(parent)) == NULL) {
1872 fprintf(stderr, "error %s: getcwd failed: %s\n",
1873 progname, strerror(errno));
1883 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1885 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1890 if (victim_file == NULL) {
1891 /* use a temp file to store the splitted layout */
1892 if (mflags & MF_DESTROY) {
1893 fdv = llapi_create_volatile_idx(parent, mdt_index,
1894 O_LOV_DELAY_CREATE);
1896 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1898 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1901 /* user specified victim file */
1902 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1907 "error %s: create victim file failed: %s\n",
1908 progname, strerror(errno));
1912 /* get lease lock of fname */
1913 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1916 "error %s: cannot get lease of file '%s': %d\n",
1917 progname, fname, rc);
1921 /* Atomatically put lease, split layouts and close. */
1922 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1928 data->lil_mode = LL_LEASE_UNLCK;
1929 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1930 data->lil_count = 2;
1931 data->lil_ids[0] = fdv;
1932 data->lil_ids[1] = id;
1933 rc = llapi_lease_set(fd, data);
1935 if (rc == 0) /* lost lease lock */
1938 "error %s: cannot split '%s': %s\n",
1939 progname, fname, strerror(-rc));
1950 llapi_layout_free(layout);
1955 * Parse a string containing an target index list into an array of integers.
1957 * The input string contains a comma delimited list of individual
1958 * indices and ranges, for example "1,2-4,7". Add the indices into the
1959 * \a tgts array and remove duplicates.
1961 * \param[out] tgts array to store indices in
1962 * \param[in] size size of \a tgts array
1963 * \param[in] offset starting index in \a tgts
1964 * \param[in] arg string containing OST index list
1965 * \param[in/out] overstriping index list may contain duplicates
1967 * \retval positive number of indices in \a tgts
1968 * \retval -EINVAL unable to parse \a arg
1970 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
1971 unsigned long long *pattern)
1975 int slots = size - offset;
1977 bool overstriped = false;
1983 end_of_loop = false;
1984 while (!end_of_loop) {
1985 int start_index = 0;
1988 char *endptr = NULL;
1992 ptr = strchrnul(arg, ',');
1994 end_of_loop = *ptr == '\0';
1997 start_index = strtol(arg, &endptr, 0);
1998 if (endptr == arg) /* no data at all */
2000 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2003 end_index = start_index;
2004 if (*endptr == '-') {
2005 end_index = strtol(endptr + 1, &endptr, 0);
2006 if (*endptr != '\0')
2008 if (end_index < start_index)
2012 for (i = start_index; i <= end_index && slots > 0; i++) {
2015 /* remove duplicate */
2016 for (j = 0; j < offset; j++) {
2017 if (tgts[j] == i && pattern &&
2018 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2020 else if (tgts[j] == i)
2026 if (j == offset) { /* check complete */
2032 if (slots == 0 && i < end_index)
2040 if (!end_of_loop && ptr != NULL)
2043 if (!overstriped && pattern)
2044 *pattern = LLAPI_LAYOUT_DEFAULT;
2046 return rc < 0 ? rc : nr;
2049 struct lfs_setstripe_args {
2050 unsigned long long lsa_comp_end;
2051 unsigned long long lsa_stripe_size;
2052 unsigned long long lsa_extension_size;
2053 long long lsa_stripe_count;
2054 long long lsa_stripe_off;
2055 __u32 lsa_comp_flags;
2056 __u32 lsa_comp_neg_flags;
2057 unsigned long long lsa_pattern;
2058 unsigned int lsa_mirror_count;
2060 bool lsa_first_comp;
2061 bool lsa_extension_comp;
2063 char *lsa_pool_name;
2066 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2068 unsigned int mirror_count = lsa->lsa_mirror_count;
2069 bool first_comp = lsa->lsa_first_comp;
2071 memset(lsa, 0, sizeof(*lsa));
2073 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2074 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2075 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2076 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2077 lsa->lsa_pool_name = NULL;
2079 lsa->lsa_mirror_count = mirror_count;
2080 lsa->lsa_first_comp = first_comp;
2084 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2085 * @lsa: Stripe options to be initialized and inherited.
2087 * This function initializes stripe options in @lsa and inherit
2088 * stripe_size, stripe_count and OST pool_name options.
2092 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2094 unsigned long long stripe_size;
2095 long long stripe_count;
2096 char *pool_name = NULL;
2098 stripe_size = lsa->lsa_stripe_size;
2099 stripe_count = lsa->lsa_stripe_count;
2100 pool_name = lsa->lsa_pool_name;
2102 setstripe_args_init(lsa);
2104 lsa->lsa_stripe_size = stripe_size;
2105 lsa->lsa_stripe_count = stripe_count;
2106 lsa->lsa_pool_name = pool_name;
2109 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2111 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2112 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2113 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2114 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2115 lsa->lsa_pool_name != NULL ||
2116 lsa->lsa_comp_end != 0);
2120 * comp_args_to_layout() - Create or extend a composite layout.
2121 * @composite: Pointer to the composite layout.
2122 * @lsa: Stripe options for the new component.
2124 * This function creates or extends a composite layout by adding a new
2125 * component with stripe options from @lsa.
2127 * Return: 0 on success or an error code on failure.
2129 static int comp_args_to_layout(struct llapi_layout **composite,
2130 struct lfs_setstripe_args *lsa,
2133 struct llapi_layout *layout = *composite;
2134 uint64_t prev_end = 0;
2139 if (layout == NULL) {
2140 layout = llapi_layout_alloc();
2141 if (layout == NULL) {
2142 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2147 *composite = layout;
2148 lsa->lsa_first_comp = true;
2152 /* Get current component extent, current component
2153 * must be the tail component. */
2154 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2156 fprintf(stderr, "Get comp extent failed. %s\n",
2161 if (lsa->lsa_first_comp)
2164 if (lsa->lsa_first_comp)
2165 rc = llapi_layout_add_first_comp(layout);
2167 rc = llapi_layout_comp_add(layout);
2169 fprintf(stderr, "Add component failed. %s\n",
2175 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2177 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2178 lsa->lsa_comp_flags, strerror(errno));
2183 uint64_t comp_end = lsa->lsa_comp_end;
2185 /* Extension space handling:
2186 * If this is the first component, length is extension_size.
2187 * If not, it is zero length, so it can be removed if there is
2188 * insufficient space to extend it.
2190 if (lsa->lsa_extension_comp && lsa->lsa_first_comp)
2191 comp_end = prev_end + lsa->lsa_extension_size;
2192 else if (lsa->lsa_extension_comp)
2193 comp_end = prev_end;
2195 rc = llapi_layout_comp_extent_set(layout, prev_end,
2198 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2199 prev_end, comp_end, strerror(errno));
2203 /* reset lsa_first_comp */
2204 lsa->lsa_first_comp = false;
2206 /* Data-on-MDT component setting */
2207 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2208 /* In case of Data-on-MDT patterns the only extra option
2209 * applicable is stripe size option. */
2210 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2211 fprintf(stderr, "Option 'stripe-count' can't be "
2212 "specified with Data-on-MDT component: %lld\n",
2213 lsa->lsa_stripe_count);
2217 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2218 fprintf(stderr, "Option 'stripe-size' can't be "
2219 "specified with Data-on-MDT component: %llu\n",
2220 lsa->lsa_stripe_size);
2224 if (lsa->lsa_nr_tgts != 0) {
2225 fprintf(stderr, "Option 'ost-list' can't be specified "
2226 "with Data-on-MDT component: '%i'\n",
2231 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2232 fprintf(stderr, "Option 'stripe-offset' can't be "
2233 "specified with Data-on-MDT component: %lld\n",
2234 lsa->lsa_stripe_off);
2238 if (lsa->lsa_pool_name != 0) {
2239 fprintf(stderr, "Option 'pool' can't be specified "
2240 "with Data-on-MDT component: '%s'\n",
2241 lsa->lsa_pool_name);
2246 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2248 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2249 lsa->lsa_pattern, strerror(errno));
2252 /* Data-on-MDT component has always single stripe up to end */
2253 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2254 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2255 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2257 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2258 lsa->lsa_pattern, strerror(errno));
2263 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2264 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2266 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2267 rc = llapi_layout_extension_size_set(layout, size);
2269 rc = llapi_layout_stripe_size_set(layout, size);
2272 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2273 size, strerror(errno));
2277 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2279 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2280 lsa->lsa_stripe_count, strerror(errno));
2284 if (lsa->lsa_pool_name != NULL) {
2285 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2287 fprintf(stderr, "Set pool name: %s failed. %s\n",
2288 lsa->lsa_pool_name, strerror(errno));
2292 rc = llapi_layout_pool_name_set(layout, "");
2294 fprintf(stderr, "Clear pool name failed: %s\n",
2300 if (lsa->lsa_nr_tgts > 0) {
2301 if (lsa->lsa_stripe_count > 0 &&
2302 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2303 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2304 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2305 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2306 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2310 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2311 rc = llapi_layout_ost_index_set(layout, i,
2316 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2317 lsa->lsa_stripe_off != -1) {
2318 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2321 fprintf(stderr, "Set ost index %d failed. %s\n",
2322 i, strerror(errno));
2326 /* Create the second, virtual component of extension space */
2327 if (lsa->lsa_extension_comp) {
2328 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2329 lsa->lsa_extension_comp = false;
2336 static int build_component(struct llapi_layout **layout,
2337 struct lfs_setstripe_args *lsa, bool set_extent)
2341 rc = comp_args_to_layout(layout, lsa, set_extent);
2345 if (lsa->lsa_mirror_count > 0) {
2346 rc = llapi_layout_mirror_count_set(*layout,
2347 lsa->lsa_mirror_count);
2351 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2354 lsa->lsa_mirror_count = 0;
2360 static int build_layout_from_yaml_node(struct cYAML *node,
2361 struct llapi_layout **layout,
2362 struct lfs_setstripe_args *lsa,
2369 if (node->cy_type == CYAML_TYPE_OBJECT) {
2370 /* go deep to sub blocks */
2371 rc = build_layout_from_yaml_node(node->cy_child, layout,
2376 if (node->cy_string == NULL)
2379 string = node->cy_string;
2380 /* skip leading lmm_ if present, to simplify parsing */
2381 if (strncmp(string, "lmm_", 4) == 0)
2384 if (node->cy_type == CYAML_TYPE_STRING) {
2385 if (!strcmp(string, "lcme_extent.e_end")) {
2386 if (!strcmp(node->cy_valuestring, "EOF") ||
2387 !strcmp(node->cy_valuestring, "eof"))
2388 lsa->lsa_comp_end = LUSTRE_EOF;
2389 } else if (!strcmp(string, "pool")) {
2390 lsa->lsa_pool_name = node->cy_valuestring;
2391 } else if (!strcmp(string, "pattern")) {
2392 if (!strcmp(node->cy_valuestring, "mdt"))
2393 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2394 if (!strcmp(node->cy_valuestring,
2395 "raid0,overstriped"))
2397 LLAPI_LAYOUT_OVERSTRIPING;
2398 } else if (!strcmp(string, "lcme_flags")) {
2399 rc = comp_str2flags(node->cy_valuestring,
2400 &lsa->lsa_comp_flags,
2401 &lsa->lsa_comp_neg_flags);
2404 /* Only template flags have meaning in
2405 * the layout for a new file
2407 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2409 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2410 if (!strcmp(string, "lcm_mirror_count")) {
2411 lsa->lsa_mirror_count = node->cy_valueint;
2412 } else if (!strcmp(string, "lcme_extent.e_start")) {
2413 if (node->cy_valueint != 0 || *layout != NULL) {
2414 rc = build_component(layout, lsa, true);
2419 if (node->cy_valueint == 0)
2420 lsa->lsa_first_comp = true;
2422 /* initialize lsa */
2423 setstripe_args_init(lsa);
2424 lsa->lsa_tgts = osts;
2425 } else if (!strcmp(string, "lcme_extent.e_end")) {
2426 if (node->cy_valueint == -1)
2427 lsa->lsa_comp_end = LUSTRE_EOF;
2429 lsa->lsa_comp_end = node->cy_valueint;
2430 } else if (!strcmp(string, "stripe_count")) {
2431 lsa->lsa_stripe_count = node->cy_valueint;
2432 } else if (!strcmp(string, "stripe_size")) {
2433 lsa->lsa_stripe_size = node->cy_valueint;
2434 } else if (!strcmp(string, "stripe_offset")) {
2435 lsa->lsa_stripe_off = node->cy_valueint;
2436 } else if (!strcmp(string, "l_ost_idx")) {
2437 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2442 node = node->cy_next;
2448 static int lfs_comp_create_from_yaml(char *template,
2449 struct llapi_layout **layout,
2450 struct lfs_setstripe_args *lsa,
2453 struct cYAML *tree = NULL, *err_rc = NULL;
2456 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2458 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2459 progname, template);
2460 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2461 "can't parse", &err_rc);
2462 cYAML_print_tree2file(stderr, err_rc);
2463 cYAML_free_tree(err_rc);
2468 /* initialize lsa for plain file */
2469 setstripe_args_init(lsa);
2470 lsa->lsa_tgts = osts;
2472 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2474 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2475 progname, template);
2478 rc = build_component(layout, lsa, *layout != NULL);
2480 /* clean clean lsa */
2481 setstripe_args_init(lsa);
2485 cYAML_free_tree(tree);
2489 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2490 * end of the last component in the existing file, and adjust the
2491 * first extent start of the components to be added accordingly. */
2492 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2494 struct llapi_layout *head;
2495 uint64_t start, end, stripe_size, prev_end = 0;
2498 if (layout == NULL) {
2500 "%s setstripe: layout must be specified\n",
2506 head = llapi_layout_get_by_path(fname, 0);
2509 "%s setstripe: cannot read layout from '%s': %s\n",
2510 progname, fname, strerror(errno));
2512 } else if (errno == ENODATA) {
2513 /* file without LOVEA, this component-add will be turned
2514 * into a component-create. */
2515 llapi_layout_free(head);
2517 } else if (!llapi_layout_is_composite(head)) {
2518 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2520 llapi_layout_free(head);
2524 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2526 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2527 progname, strerror(errno));
2528 llapi_layout_free(head);
2532 llapi_layout_free(head);
2534 /* Make sure we use the first component of the layout to be added. */
2535 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2538 "%s setstripe: cannot move component cursor: %s\n",
2539 progname, strerror(errno));
2543 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2545 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2546 progname, strerror(errno));
2550 if (start > prev_end || end <= prev_end) {
2552 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2553 progname, start, end, prev_end);
2557 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2559 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2560 progname, strerror(errno));
2564 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2565 (prev_end & (stripe_size - 1))) {
2567 "%s setstripe: stripe size %lu not aligned with %lu\n",
2568 progname, stripe_size, prev_end);
2572 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2575 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2576 progname, prev_end, end, strerror(errno));
2583 static inline bool arg_is_eof(char *arg)
2585 return !strncmp(arg, "-1", strlen("-1")) ||
2586 !strncmp(arg, "EOF", strlen("EOF")) ||
2587 !strncmp(arg, "eof", strlen("eof"));
2591 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2593 * Return: Valid mirror_args pointer on success and
2594 * NULL if memory allocation fails.
2596 static struct mirror_args *lfs_mirror_alloc(void)
2598 struct mirror_args *mirror = NULL;
2601 mirror = calloc(1, sizeof(*mirror));
2612 * lfs_mirror_free() - Free memory allocated for a mirror argument
2614 * @mirror: Previously allocated mirror argument structure by
2615 * lfs_mirror_alloc().
2617 * Free memory allocated for @mirror.
2621 static void lfs_mirror_free(struct mirror_args *mirror)
2623 if (mirror->m_layout != NULL)
2624 llapi_layout_free(mirror->m_layout);
2629 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2630 * @mirror_list: Previously allocated mirror list.
2632 * Free memory allocated for @mirror_list.
2636 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2638 struct mirror_args *next_mirror = NULL;
2640 while (mirror_list != NULL) {
2641 next_mirror = mirror_list->m_next;
2642 lfs_mirror_free(mirror_list);
2643 mirror_list = next_mirror;
2655 LFS_COMP_NO_VERIFY_OPT,
2657 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2659 LFS_MIRROR_STATE_OPT,
2661 LFS_MIRROR_INDEX_OPT,
2662 LFS_LAYOUT_FOREIGN_OPT,
2667 static int lfs_setstripe_internal(int argc, char **argv,
2668 enum setstripe_origin opc)
2670 struct lfs_setstripe_args lsa = { 0 };
2671 struct llapi_stripe_param *param = NULL;
2672 struct find_param migrate_mdt_param = {
2682 unsigned long long size_units = 1;
2683 bool migrate_mode = false;
2684 bool migrate_mdt_mode = false;
2685 bool setstripe_mode = false;
2686 bool migration_block = false;
2687 __u64 migration_flags = 0;
2688 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2689 int comp_del = 0, comp_set = 0;
2692 struct llapi_layout *layout = NULL;
2693 struct llapi_layout **lpp = &layout;
2694 bool mirror_mode = false;
2695 bool has_m_file = false;
2696 __u32 mirror_count = 0;
2697 enum mirror_flags mirror_flags = 0;
2698 struct mirror_args *mirror_list = NULL;
2699 struct mirror_args *new_mirror = NULL;
2700 struct mirror_args *last_mirror = NULL;
2701 __u16 mirror_id = 0;
2703 bool from_yaml = false;
2704 bool from_copy = false;
2705 char *template = NULL;
2706 bool foreign_mode = false;
2708 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
2709 char *mode_opt = NULL;
2710 mode_t previous_umask = 0;
2711 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2713 struct option long_opts[] = {
2714 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2715 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2716 /* --block is only valid in migrate mode */
2717 { .val = 'b', .name = "block", .has_arg = no_argument },
2718 { .val = LFS_COMP_ADD_OPT,
2719 .name = "comp-add", .has_arg = no_argument },
2720 { .val = LFS_COMP_ADD_OPT,
2721 .name = "component-add", .has_arg = no_argument },
2722 { .val = LFS_COMP_DEL_OPT,
2723 .name = "comp-del", .has_arg = no_argument },
2724 { .val = LFS_COMP_DEL_OPT,
2725 .name = "component-del", .has_arg = no_argument },
2726 { .val = LFS_COMP_FLAGS_OPT,
2727 .name = "comp-flags", .has_arg = required_argument },
2728 { .val = LFS_COMP_FLAGS_OPT,
2729 .name = "component-flags",
2730 .has_arg = required_argument },
2731 { .val = LFS_COMP_SET_OPT,
2732 .name = "comp-set", .has_arg = no_argument },
2733 { .val = LFS_COMP_SET_OPT,
2734 .name = "component-set",
2735 .has_arg = no_argument},
2736 { .val = LFS_COMP_NO_VERIFY_OPT,
2737 .name = "no-verify", .has_arg = no_argument},
2738 { .val = LFS_LAYOUT_FLAGS_OPT,
2739 .name = "flags", .has_arg = required_argument},
2740 { .val = LFS_LAYOUT_FOREIGN_OPT,
2741 .name = "foreign", .has_arg = optional_argument},
2742 { .val = LFS_MIRROR_ID_OPT,
2743 .name = "mirror-id", .has_arg = required_argument},
2744 { .val = LFS_MODE_OPT,
2745 .name = "mode", .has_arg = required_argument},
2746 { .val = LFS_LAYOUT_COPY,
2747 .name = "copy", .has_arg = required_argument},
2748 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2749 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2750 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2751 { .val = 'C', .name = "overstripe-count",
2752 .has_arg = required_argument},
2753 { .val = 'd', .name = "delete", .has_arg = no_argument},
2754 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2755 /* --non-direct is only valid in migrate mode */
2756 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2757 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2758 { .val = 'E', .name = "component-end",
2759 .has_arg = required_argument},
2760 { .val = 'f', .name = "file", .has_arg = required_argument },
2761 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2762 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2763 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2764 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2765 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2766 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2767 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2768 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2769 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2770 { .val = 'L', .name = "layout", .has_arg = required_argument },
2771 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2772 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2773 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2774 /* --non-block is only valid in migrate mode */
2775 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2776 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2777 { .val = 'o', .name = "ost", .has_arg = required_argument },
2778 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2779 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2780 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2782 { .val = 'p', .name = "pool", .has_arg = required_argument },
2783 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2784 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2785 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2786 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2787 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2788 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2789 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2790 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2791 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2792 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2793 /* --verbose is only valid in migrate mode */
2794 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2795 { .val = 'x', .name = "xattr", .has_arg = required_argument },
2796 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2797 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
2798 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
2801 setstripe_args_init(&lsa);
2803 migrate_mode = (opc == SO_MIGRATE);
2804 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2805 setstripe_mode = (opc == SO_SETSTRIPE);
2807 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2809 while ((c = getopt_long(argc, argv,
2810 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
2811 long_opts, NULL)) >= 0) {
2816 case LFS_COMP_ADD_OPT:
2819 case LFS_COMP_DEL_OPT:
2822 case LFS_COMP_FLAGS_OPT:
2823 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2824 &lsa.lsa_comp_neg_flags);
2827 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2828 fprintf(stderr, "%s: inverted flags are not supported\n",
2833 case LFS_COMP_SET_OPT:
2836 case LFS_COMP_NO_VERIFY_OPT:
2837 mirror_flags |= MF_NO_VERIFY;
2839 case LFS_MIRROR_ID_OPT:
2840 mirror_id = strtoul(optarg, &end, 0);
2841 if (*end != '\0' || mirror_id == 0) {
2843 "%s %s: invalid mirror ID '%s'\n",
2844 progname, argv[0], optarg);
2848 case LFS_LAYOUT_FLAGS_OPT: {
2851 /* check for numeric flags (foreign and mirror cases) */
2852 if (setstripe_mode && !mirror_mode && !last_mirror) {
2853 flags = strtoul(optarg, &end, 16);
2856 "%s %s: bad flags '%s'\n",
2857 progname, argv[0], optarg);
2863 if (!mirror_mode || !last_mirror) {
2864 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2869 result = comp_str2flags(optarg, &last_mirror->m_flags,
2875 fprintf(stderr, "%s: inverted flags are not supported\n",
2880 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
2882 "%s: unsupported mirror flags: %s\n",
2889 case LFS_LAYOUT_FOREIGN_OPT:
2890 if (optarg != NULL) {
2891 /* check pure numeric */
2892 type = strtoul(optarg, &end, 0);
2895 type = check_foreign_type_name(optarg);
2896 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
2898 "%s %s: unrecognized foreign type '%s'\n",
2905 foreign_mode = true;
2909 if (mode_opt != NULL) {
2910 mode = strtoul(mode_opt, &end, 8);
2913 "%s %s: bad mode '%s'\n",
2914 progname, argv[0], mode_opt);
2917 previous_umask = umask(0);
2920 case LFS_LAYOUT_COPY:
2925 if (!migrate_mode) {
2927 "%s %s: -b|--block valid only for migrate command\n",
2931 migration_block = true;
2934 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
2937 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2940 "%s %s: invalid stripe count '%s'\n",
2941 progname, argv[0], optarg);
2945 if (lsa.lsa_stripe_count == -1)
2946 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2949 /* delete the default striping pattern */
2951 if (opc == SO_MIRROR_SPLIT) {
2954 "%s %s: -d cannot used with -f\n",
2958 mirror_flags |= MF_DESTROY;
2962 if (!migrate_mode) {
2964 "%s %s: -D|--non-direct is valid "
2965 "only for migrate command\n",
2969 migration_flags |= MIGRATION_NONDIRECT;
2972 if (lsa.lsa_comp_end != 0) {
2973 result = comp_args_to_layout(lpp, &lsa, true);
2975 fprintf(stderr, "%s: invalid layout\n",
2980 setstripe_args_init_inherit(&lsa);
2983 if (arg_is_eof(optarg)) {
2984 lsa.lsa_comp_end = LUSTRE_EOF;
2986 result = llapi_parse_size(optarg,
2991 "%s %s: invalid component end '%s'\n",
2992 progname, argv[0], optarg);
2998 if (!migrate_mode) {
2999 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3003 lsa.lsa_pattern = check_hashtype(optarg);
3004 if (lsa.lsa_pattern == 0) {
3006 "%s %s: bad stripe hash type '%s'\n",
3007 progname, argv[0], optarg);
3012 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3015 "%s %s: invalid stripe offset '%s'\n",
3016 progname, argv[0], optarg);
3019 if (lsa.lsa_stripe_off == -1)
3020 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3023 comp_id = strtoul(optarg, &end, 0);
3024 if (*end != '\0' || comp_id == 0 ||
3025 comp_id > LCME_ID_MAX) {
3027 "%s %s: invalid component ID '%s'\n",
3028 progname, argv[0], optarg);
3033 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3035 "error: %s: invalid option: %s\n",
3036 progname, argv[optopt + 1]);
3039 if (opc == SO_MIRROR_EXTEND) {
3040 if (last_mirror == NULL) {
3042 "error: %s: '-N' must exist in front of '%s'\n",
3043 progname, argv[optopt + 1]);
3046 last_mirror->m_file = optarg;
3047 last_mirror->m_count = 1;
3050 if (mirror_list == NULL)
3051 mirror_list = lfs_mirror_alloc();
3052 mirror_list->m_file = optarg;
3057 if (strcmp(argv[optind - 1], "mdt") == 0) {
3058 /* Can be only the first component */
3059 if (layout != NULL) {
3061 fprintf(stderr, "error: 'mdt' layout "
3062 "can be only the first one\n");
3065 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3067 fprintf(stderr, "error: 'mdt' layout "
3068 "size is too big\n");
3071 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3072 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3074 fprintf(stderr, "error: layout '%s' is "
3075 "unknown, supported layouts are: "
3076 "'mdt', 'raid0'\n", argv[optind]);
3081 if (!migrate_mode) {
3083 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3087 migrate_mdt_mode = true;
3088 lsa.lsa_nr_tgts = parse_targets(tgts,
3089 sizeof(tgts) / sizeof(__u32),
3090 lsa.lsa_nr_tgts, optarg, NULL);
3091 if (lsa.lsa_nr_tgts < 0) {
3093 "%s %s: invalid MDT target(s) '%s'\n",
3094 progname, argv[0], optarg);
3098 lsa.lsa_tgts = tgts;
3099 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3100 lsa.lsa_stripe_off = tgts[0];
3103 if (!migrate_mode) {
3105 "%s %s: -n|--non-block valid only for migrate command\n",
3109 migration_flags |= MIGRATION_NONBLOCK;
3112 if (opc == SO_SETSTRIPE) {
3113 opc = SO_MIRROR_CREATE;
3117 if (optarg != NULL) {
3118 mirror_count = strtoul(optarg, &end, 0);
3119 if (*end != '\0' || mirror_count == 0) {
3121 "error: %s: bad mirror count: %s\n",
3128 new_mirror = lfs_mirror_alloc();
3129 new_mirror->m_count = mirror_count;
3131 if (mirror_list == NULL)
3132 mirror_list = new_mirror;
3134 if (last_mirror != NULL) {
3135 /* wrap up last mirror */
3136 if (lsa.lsa_comp_end == 0)
3137 lsa.lsa_comp_end = LUSTRE_EOF;
3139 result = comp_args_to_layout(lpp, &lsa, true);
3141 lfs_mirror_free(new_mirror);
3145 setstripe_args_init_inherit(&lsa);
3147 last_mirror->m_next = new_mirror;
3150 last_mirror = new_mirror;
3151 lpp = &last_mirror->m_layout;
3154 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3155 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3156 fprintf(stderr, "warning: '--ost-list' is "
3157 "deprecated, use '--ost' instead\n");
3159 /* -o allows overstriping, and must note it because
3160 * parse_targets is shared with MDT striping, which
3161 * does not allow duplicates
3163 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3164 lsa.lsa_nr_tgts = parse_targets(tgts,
3165 sizeof(tgts) / sizeof(__u32),
3166 lsa.lsa_nr_tgts, optarg,
3168 if (lsa.lsa_nr_tgts < 0) {
3170 "%s %s: invalid OST target(s) '%s'\n",
3171 progname, argv[0], optarg);
3175 lsa.lsa_tgts = tgts;
3176 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3177 lsa.lsa_stripe_off = tgts[0];
3182 lsa.lsa_pool_name = optarg;
3184 if (strlen(lsa.lsa_pool_name) == 0 ||
3185 strncmp(lsa.lsa_pool_name, "none",
3186 LOV_MAXPOOLNAME) == 0)
3187 lsa.lsa_pool_name = NULL;
3190 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3194 "%s %s: invalid stripe size '%s'\n",
3195 progname, argv[0], optarg);
3200 if (!migrate_mode) {
3202 "%s %s: -v|--verbose valid only for migrate command\n",
3206 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3207 migration_flags = MIGRATION_VERBOSE;
3217 result = llapi_parse_size(optarg,
3218 &lsa.lsa_extension_size,
3222 "%s %s: invalid extension size '%s'\n",
3223 progname, argv[0], optarg);
3227 lsa.lsa_extension_comp = true;
3230 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3231 progname, argv[0], argv[optind - 1]);
3236 fname = argv[optind];
3238 if (optind == argc) {
3239 fprintf(stderr, "%s %s: FILE must be specified\n",
3244 if (xattr && !foreign_mode) {
3245 /* only print a warning as this is harmless and will be ignored
3248 "%s %s: xattr has been specified for non-foreign layout\n",
3250 } else if (foreign_mode && !xattr) {
3252 "%s %s: xattr must be provided in foreign mode\n",
3257 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3258 comp_set || comp_id || delete || from_copy ||
3259 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3262 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3267 if (mirror_mode && mirror_count == 0) {
3269 "error: %s: --mirror-count|-N option is required\n",
3276 if (lsa.lsa_comp_end == 0)
3277 lsa.lsa_comp_end = LUSTRE_EOF;
3280 if (lsa.lsa_comp_end != 0) {
3281 result = comp_args_to_layout(lpp, &lsa, true);
3283 fprintf(stderr, "error: %s: invalid layout\n",
3290 if (mirror_flags & MF_NO_VERIFY) {
3291 if (opc != SO_MIRROR_EXTEND) {
3293 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3297 } else if (!has_m_file) {
3299 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3306 if (comp_set && !comp_id) {
3307 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3312 if ((delete + comp_set + comp_del + comp_add) > 1) {
3314 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3319 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3320 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3322 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3327 if ((comp_set || comp_del) &&
3328 (setstripe_args_specified(&lsa) || layout != NULL)) {
3330 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3335 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3337 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3342 if (comp_add || comp_del) {
3345 result = lstat(fname, &st);
3346 if (result == 0 && S_ISDIR(st.st_mode)) {
3348 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3354 fprintf(stderr, "error: %s: can't use --component-add "
3355 "or --component-del for mirror operation\n",
3362 if (layout == NULL) {
3364 "%s %s: option -E must be specified with --component-add\n",
3369 result = adjust_first_extent(fname, layout);
3370 if (result == -ENODATA)
3372 else if (result != 0) {
3373 fprintf(stderr, "error: %s: invalid layout\n",
3379 if (from_yaml && from_copy) {
3381 "%s: can't specify --yaml and --copy together\n",
3386 if ((from_yaml || from_copy) &&
3387 (setstripe_args_specified(&lsa) || layout != NULL)) {
3388 fprintf(stderr, "error: %s: can't specify --yaml with "
3389 "-c, -S, -i, -o, -p or -E options.\n",
3394 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3396 "%s %s: options --non-block and --block are mutually exclusive\n",
3401 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3404 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3409 if (migrate_mdt_mode) {
3410 struct lmv_user_md *lmu;
3412 /* initialize migrate mdt parameters */
3413 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3414 LMV_USER_MAGIC_SPECIFIC));
3417 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3418 progname, argv[0], strerror(ENOMEM));
3422 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3423 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3424 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3426 "%s %s: migrate should specify MDT index\n",
3431 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3432 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3433 lmu->lum_hash_type = lsa.lsa_pattern;
3435 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3436 if (lsa.lsa_pool_name)
3437 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3438 sizeof(lmu->lum_pool_name) - 1);
3439 if (lsa.lsa_nr_tgts > 1) {
3442 if (lsa.lsa_stripe_count > 0 &&
3443 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3444 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3446 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3447 progname, lsa.lsa_stripe_count,
3453 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3454 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3455 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3456 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3458 lmu->lum_magic = LMV_USER_MAGIC;
3461 migrate_mdt_param.fp_lmv_md = lmu;
3462 migrate_mdt_param.fp_migrate = 1;
3463 } else if (layout == NULL) {
3464 /* initialize stripe parameters */
3465 param = calloc(1, offsetof(typeof(*param),
3466 lsp_osts[lsa.lsa_nr_tgts]));
3467 if (param == NULL) {
3469 "%s %s: cannot allocate memory for parameters: %s\n",
3470 progname, argv[0], strerror(ENOMEM));
3475 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3476 param->lsp_stripe_size = lsa.lsa_stripe_size;
3477 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3478 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3479 param->lsp_stripe_count = -1;
3481 param->lsp_stripe_count = lsa.lsa_stripe_count;
3483 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3484 param->lsp_stripe_offset = -1;
3486 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3487 param->lsp_stripe_pattern =
3488 llapi_pattern_to_lov(lsa.lsa_pattern);
3489 if (param->lsp_stripe_pattern == EINVAL) {
3490 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3495 param->lsp_pool = lsa.lsa_pool_name;
3496 param->lsp_is_specific = false;
3497 if (lsa.lsa_nr_tgts > 0) {
3498 if (lsa.lsa_stripe_count > 0 &&
3499 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3500 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3501 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3502 fprintf(stderr, "error: %s: stripe count %lld "
3503 "doesn't match the number of OSTs: %d\n"
3504 , argv[0], lsa.lsa_stripe_count,
3510 param->lsp_is_specific = true;
3511 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3512 memcpy(param->lsp_osts, tgts,
3513 sizeof(*tgts) * lsa.lsa_nr_tgts);
3518 /* generate a layout from a YAML template */
3519 result = lfs_comp_create_from_yaml(template, &layout,
3522 fprintf(stderr, "error: %s: can't create composite "
3523 "layout from template file %s\n",
3527 } else if (from_copy) {
3528 layout = llapi_layout_get_by_path(template, 0);
3529 if (layout == NULL) {
3531 "%s: can't create composite layout from file %s.\n",
3532 progname, template);
3537 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3538 if (migrate_mdt_mode) {
3539 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3540 } else if (migrate_mode) {
3541 result = lfs_migrate(fname, migration_flags, param,
3543 } else if (comp_set != 0) {
3544 result = lfs_component_set(fname, comp_id,
3546 lsa.lsa_comp_neg_flags);
3547 } else if (comp_del != 0) {
3548 result = lfs_component_del(fname, comp_id,
3550 lsa.lsa_comp_neg_flags);
3551 } else if (comp_add != 0) {
3552 result = lfs_component_add(fname, layout);
3553 } else if (opc == SO_MIRROR_CREATE) {
3554 result = mirror_create(fname, mirror_list);
3555 } else if (opc == SO_MIRROR_EXTEND) {
3556 result = mirror_extend(fname, mirror_list,
3558 } else if (opc == SO_MIRROR_SPLIT) {
3559 if (mirror_id == 0 && comp_id == 0) {
3561 "%s %s: no mirror id or component id is specified\n",
3566 comp_id = mirror_id;
3568 mirror_flags |= MF_COMP_ID;
3569 result = mirror_split(fname, comp_id, mirror_flags,
3570 has_m_file ? mirror_list->m_file :
3572 } else if (layout != NULL) {
3573 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3579 } else if (foreign_mode) {
3580 result = llapi_file_create_foreign(fname, mode, type,
3587 result = llapi_file_open_param(fname,
3596 /* Save the first error encountered. */
3603 if (mode_opt != NULL)
3604 umask(previous_umask);
3607 free(migrate_mdt_param.fp_lmv_md);
3608 llapi_layout_free(layout);
3609 lfs_mirror_list_free(mirror_list);
3614 llapi_layout_free(layout);
3615 lfs_mirror_list_free(mirror_list);
3619 static int lfs_poollist(int argc, char **argv)
3624 return llapi_poollist(argv[1]);
3627 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3632 char *endptr = "AD";
3637 else if (str[0] == '-')
3643 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3644 long long val = strtoll(timebuf, &endptr, 0);
3649 unit *= 52; /* 52 weeks + 1 day below */
3650 case 'w': /* fallthrough */
3652 case '\0': /* days are default unit if none used */
3653 case 'd': /* fallthrough */
3654 unit = (unit + (*endptr == 'y')) * 24;
3655 case 'h': /* fallthrough */
3657 case 'm': /* fallthrough */
3659 case 's': /* fallthrough */
3661 /* don't need to multiply by 1 for seconds */
3664 "%s find: bad time string '%s': %s\n",
3665 progname, timebuf, strerror(EINVAL));
3669 if (param->fp_time_margin == 0 ||
3670 (*endptr && unit < param->fp_time_margin))
3671 param->fp_time_margin = unit;
3678 fprintf(stderr, "%s find: bad time '%s': too large\n",
3688 static int name2uid(unsigned int *id, const char *name)
3690 struct passwd *passwd;
3692 passwd = getpwnam(name);
3695 *id = passwd->pw_uid;
3700 static int name2gid(unsigned int *id, const char *name)
3702 struct group *group;
3704 group = getgrnam(name);
3707 *id = group->gr_gid;
3712 static inline int name2projid(unsigned int *id, const char *name)
3717 static int uid2name(char **name, unsigned int id)
3719 struct passwd *passwd;
3721 passwd = getpwuid(id);
3724 *name = passwd->pw_name;
3729 static inline int gid2name(char **name, unsigned int id)
3731 struct group *group;
3733 group = getgrgid(id);
3736 *name = group->gr_name;
3741 static int name2layout(__u32 *layout, char *name)
3743 char *ptr, *layout_name;
3746 for (ptr = name; ; ptr = NULL) {
3747 layout_name = strtok(ptr, ",");
3748 if (layout_name == NULL)
3750 if (strcmp(layout_name, "released") == 0)
3751 *layout |= LOV_PATTERN_F_RELEASED;
3752 else if (strcmp(layout_name, "raid0") == 0)
3753 *layout |= LOV_PATTERN_RAID0;
3754 else if (strcmp(layout_name, "mdt") == 0)
3755 *layout |= LOV_PATTERN_MDT;
3756 else if (strcmp(layout_name, "overstriping") == 0)
3757 *layout |= LOV_PATTERN_OVERSTRIPING;
3764 static int lfs_find(int argc, char **argv)
3769 struct find_param param = {
3772 .fp_time_margin = 24 * 60 * 60,
3774 struct option long_opts[] = {
3775 { .val = 'A', .name = "atime", .has_arg = required_argument },
3776 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3777 { .val = LFS_COMP_COUNT_OPT,
3778 .name = "comp-count", .has_arg = required_argument },
3779 { .val = LFS_COMP_COUNT_OPT,
3780 .name = "component-count",
3781 .has_arg = required_argument },
3782 { .val = LFS_COMP_FLAGS_OPT,
3783 .name = "comp-flags", .has_arg = required_argument },
3784 { .val = LFS_COMP_FLAGS_OPT,
3785 .name = "component-flags",
3786 .has_arg = required_argument },
3787 { .val = LFS_COMP_START_OPT,
3788 .name = "comp-start", .has_arg = required_argument },
3789 { .val = LFS_COMP_START_OPT,
3790 .name = "component-start",
3791 .has_arg = required_argument },
3792 { .val = LFS_MIRROR_STATE_OPT,
3793 .name = "mirror-state", .has_arg = required_argument },
3794 { .val = LFS_LAYOUT_FOREIGN_OPT,
3795 .name = "foreign", .has_arg = optional_argument},
3796 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3797 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3798 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3799 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3800 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3801 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3802 { .val = 'E', .name = "component-end",
3803 .has_arg = required_argument },
3804 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3805 { .val = LFS_LAYOUT_FOREIGN_OPT,
3806 .name = "foreign", .has_arg = optional_argument},
3807 { .val = 'g', .name = "gid", .has_arg = required_argument },
3808 { .val = 'G', .name = "group", .has_arg = required_argument },
3809 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3810 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3811 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3812 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3813 { .val = 'L', .name = "layout", .has_arg = required_argument },
3814 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3815 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3816 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3817 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3818 { .val = 'n', .name = "name", .has_arg = required_argument },
3819 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3820 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3821 { .val = 'O', .name = "obd", .has_arg = required_argument },
3822 { .val = 'O', .name = "ost", .has_arg = required_argument },
3823 /* no short option for pool yet, can be 'p' after 2.18 */
3824 { .val = LFS_POOL_OPT,
3825 .name = "pool", .has_arg = required_argument },
3826 { .val = '0', .name = "print0", .has_arg = no_argument },
3827 { .val = 'P', .name = "print", .has_arg = no_argument },
3828 { .val = LFS_PROJID_OPT,
3829 .name = "projid", .has_arg = required_argument },
3830 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3831 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3832 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3833 { .val = 's', .name = "size", .has_arg = required_argument },
3834 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3835 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3836 { .val = 't', .name = "type", .has_arg = required_argument },
3837 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3838 { .val = 'u', .name = "uid", .has_arg = required_argument },
3839 { .val = 'U', .name = "user", .has_arg = required_argument },
3840 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3841 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3853 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3854 while ((c = getopt_long_only(argc, argv,
3855 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3856 long_opts, NULL)) >= 0) {
3861 /* '!' is part of option */
3862 /* when getopt_long_only() finds a string which is not
3863 * an option nor a known option argument it returns 1
3864 * in that case if we already have found pathstart and pathend
3865 * (i.e. we have the list of pathnames),
3866 * the only supported value is "!"
3868 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3869 if (!isoption && pathend != -1) {
3870 fprintf(stderr, "err: %s: filename|dirname must either "
3871 "precede options or follow options\n",
3876 if (!isoption && pathstart == -1)
3877 pathstart = optind - 1;
3878 if (isoption && pathstart != -1 && pathend == -1)
3879 pathend = optind - 2;
3885 /* unknown; opt is "!" or path component,
3886 * checking done above.
3888 if (strcmp(optarg, "!") == 0)
3892 xtime = ¶m.fp_atime;
3893 xsign = ¶m.fp_asign;
3894 param.fp_exclude_atime = !!neg_opt;
3895 /* no break, this falls through to 'C' for ctime */
3898 xtime = ¶m.fp_ctime;
3899 xsign = ¶m.fp_csign;
3900 param.fp_exclude_ctime = !!neg_opt;
3902 /* no break, this falls through to 'M' for mtime */
3905 xtime = ¶m.fp_mtime;
3906 xsign = ¶m.fp_msign;
3907 param.fp_exclude_mtime = !!neg_opt;
3909 rc = set_time(¶m, &t, xtime, optarg);
3910 if (rc == LONG_MAX) {
3918 if (optarg[0] == '+') {
3919 param.fp_blocks_sign = -1;
3921 } else if (optarg[0] == '-') {
3922 param.fp_blocks_sign = 1;
3926 param.fp_blocks_units = 1024;
3927 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3928 ¶m.fp_blocks_units, 0);
3930 fprintf(stderr, "error: bad blocks '%s'\n",
3934 param.fp_check_blocks = 1;
3935 param.fp_exclude_blocks = !!neg_opt;
3937 case LFS_COMP_COUNT_OPT:
3938 if (optarg[0] == '+') {
3939 param.fp_comp_count_sign = -1;
3941 } else if (optarg[0] == '-') {
3942 param.fp_comp_count_sign = 1;
3946 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3947 if (*endptr != '\0') {
3948 fprintf(stderr, "error: bad component count "
3952 param.fp_check_comp_count = 1;
3953 param.fp_exclude_comp_count = !!neg_opt;
3955 case LFS_COMP_FLAGS_OPT:
3956 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3957 ¶m.fp_comp_neg_flags);
3959 fprintf(stderr, "error: bad component flags "
3963 param.fp_check_comp_flags = 1;
3965 __u32 flags = param.fp_comp_neg_flags;
3966 param.fp_comp_neg_flags = param.fp_comp_flags;
3967 param.fp_comp_flags = flags;
3970 case LFS_COMP_START_OPT:
3971 if (optarg[0] == '+') {
3972 param.fp_comp_start_sign = -1;
3974 } else if (optarg[0] == '-') {
3975 param.fp_comp_start_sign = 1;
3979 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3980 ¶m.fp_comp_start_units, 0);
3982 fprintf(stderr, "error: bad component start "
3986 param.fp_check_comp_start = 1;
3987 param.fp_exclude_comp_start = !!neg_opt;
3989 case LFS_MIRROR_STATE_OPT:
3990 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3991 ¶m.fp_mirror_neg_state);
3994 "error: bad mirrored file state '%s'\n",
3998 param.fp_check_mirror_state = 1;
4000 __u16 state = param.fp_mirror_neg_state;
4001 param.fp_mirror_neg_state =
4002 param.fp_mirror_state;
4003 param.fp_mirror_state = state;
4007 if (optarg[0] == '+') {
4008 param.fp_stripe_count_sign = -1;
4010 } else if (optarg[0] == '-') {
4011 param.fp_stripe_count_sign = 1;
4015 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4016 if (*endptr != '\0') {
4017 fprintf(stderr,"error: bad stripe_count '%s'\n",
4022 param.fp_check_stripe_count = 1;
4023 param.fp_exclude_stripe_count = !!neg_opt;
4026 param.fp_max_depth = strtol(optarg, 0, 0);
4029 if (optarg[0] == '+') {
4030 param.fp_comp_end_sign = -1;
4032 } else if (optarg[0] == '-') {
4033 param.fp_comp_end_sign = 1;
4037 if (arg_is_eof(optarg)) {
4038 param.fp_comp_end = LUSTRE_EOF;
4039 param.fp_comp_end_units = 1;
4042 rc = llapi_parse_size(optarg,
4044 ¶m.fp_comp_end_units, 0);
4047 fprintf(stderr, "error: bad component end "
4051 param.fp_check_comp_end = 1;
4052 param.fp_exclude_comp_end = !!neg_opt;
4054 case LFS_LAYOUT_FOREIGN_OPT: {
4055 /* all types by default */
4056 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4058 if (optarg != NULL) {
4059 /* check pure numeric */
4060 type = strtoul(optarg, &endptr, 0);
4063 type = check_foreign_type_name(optarg);
4064 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4066 "%s %s: unknown foreign type '%s'\n",
4073 param.fp_foreign_type = type;
4074 param.fp_check_foreign = 1;
4075 param.fp_exclude_foreign = !!neg_opt;
4080 rc = name2gid(¶m.fp_gid, optarg);
4082 param.fp_gid = strtoul(optarg, &endptr, 10);
4083 if (*endptr != '\0') {
4084 fprintf(stderr, "Group/GID: %s cannot "
4085 "be found.\n", optarg);
4090 param.fp_exclude_gid = !!neg_opt;
4091 param.fp_check_gid = 1;
4094 param.fp_hash_type = check_hashtype(optarg);
4095 if (param.fp_hash_type == 0) {
4096 fprintf(stderr, "error: bad hash_type '%s'\n",
4101 param.fp_check_hash_type = 1;
4102 param.fp_exclude_hash_type = !!neg_opt;
4105 ret = name2layout(¶m.fp_layout, optarg);
4108 param.fp_exclude_layout = !!neg_opt;
4109 param.fp_check_layout = 1;
4113 rc = name2uid(¶m.fp_uid, optarg);
4115 param.fp_uid = strtoul(optarg, &endptr, 10);
4116 if (*endptr != '\0') {
4117 fprintf(stderr, "User/UID: %s cannot "
4118 "be found.\n", optarg);
4123 param.fp_exclude_uid = !!neg_opt;
4124 param.fp_check_uid = 1;
4127 param.fp_pattern = (char *)optarg;
4128 param.fp_exclude_pattern = !!neg_opt;
4131 if (optarg[0] == '+') {
4132 param.fp_mirror_count_sign = -1;
4134 } else if (optarg[0] == '-') {
4135 param.fp_mirror_count_sign = 1;
4139 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4140 if (*endptr != '\0') {
4142 "error: bad mirror count '%s'\n",
4146 param.fp_check_mirror_count = 1;
4147 param.fp_exclude_mirror_count = !!neg_opt;
4152 char *buf, *token, *next, *p;
4156 buf = strdup(optarg);
4162 param.fp_exclude_obd = !!neg_opt;
4165 while (token && *token) {
4166 token = strchr(token, ',');
4173 param.fp_exclude_mdt = !!neg_opt;
4174 param.fp_num_alloc_mdts += len;
4175 tmp = realloc(param.fp_mdt_uuid,
4176 param.fp_num_alloc_mdts *
4177 sizeof(*param.fp_mdt_uuid));
4183 param.fp_mdt_uuid = tmp;
4185 param.fp_exclude_obd = !!neg_opt;
4186 param.fp_num_alloc_obds += len;
4187 tmp = realloc(param.fp_obd_uuid,
4188 param.fp_num_alloc_obds *
4189 sizeof(*param.fp_obd_uuid));
4195 param.fp_obd_uuid = tmp;
4197 for (token = buf; token && *token; token = next) {
4198 struct obd_uuid *puuid;
4201 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4204 ¶m.fp_obd_uuid[param.fp_num_obds++];
4206 p = strchr(token, ',');
4213 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4218 strncpy(puuid->uuid, token,
4219 sizeof(puuid->uuid));
4226 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4230 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4232 "Pool name %s is too long (max %d)\n",
4233 optarg, LOV_MAXPOOLNAME);
4238 * We do check for empty pool because empty pool
4239 * is used to find V1 LOV attributes
4241 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4242 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4243 param.fp_exclude_pool = !!neg_opt;
4244 param.fp_check_pool = 1;
4246 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4247 case 'p': /* want this for --pool, to match getstripe/find */
4249 "warning: -p deprecated, use --print0 or -0\n");
4252 param.fp_zero_end = 1;
4254 case 'P': /* we always print, this option is a no-op */
4256 case LFS_PROJID_OPT:
4257 rc = name2projid(¶m.fp_projid, optarg);
4259 param.fp_projid = strtoul(optarg, &endptr, 10);
4260 if (*endptr != '\0') {
4262 "Invalid project ID: %s",
4268 param.fp_exclude_projid = !!neg_opt;
4269 param.fp_check_projid = 1;
4272 if (optarg[0] == '+') {
4273 param.fp_size_sign = -1;
4275 } else if (optarg[0] == '-') {
4276 param.fp_size_sign = 1;
4280 ret = llapi_parse_size(optarg, ¶m.fp_size,
4281 ¶m.fp_size_units, 0);
4283 fprintf(stderr, "error: bad file size '%s'\n",
4287 param.fp_check_size = 1;
4288 param.fp_exclude_size = !!neg_opt;
4291 if (optarg[0] == '+') {
4292 param.fp_stripe_size_sign = -1;
4294 } else if (optarg[0] == '-') {
4295 param.fp_stripe_size_sign = 1;
4299 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4300 ¶m.fp_stripe_size_units, 0);
4302 fprintf(stderr, "error: bad stripe_size '%s'\n",
4306 param.fp_check_stripe_size = 1;
4307 param.fp_exclude_stripe_size = !!neg_opt;
4310 param.fp_exclude_type = !!neg_opt;
4311 switch (optarg[0]) {
4313 param.fp_type = S_IFBLK;
4316 param.fp_type = S_IFCHR;
4319 param.fp_type = S_IFDIR;
4322 param.fp_type = S_IFREG;
4325 param.fp_type = S_IFLNK;
4328 param.fp_type = S_IFIFO;
4331 param.fp_type = S_IFSOCK;
4334 fprintf(stderr, "error: %s: bad type '%s'\n",
4341 if (optarg[0] == '+') {
4342 param.fp_mdt_count_sign = -1;
4344 } else if (optarg[0] == '-') {
4345 param.fp_mdt_count_sign = 1;
4349 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4350 if (*endptr != '\0') {
4351 fprintf(stderr, "error: bad mdt_count '%s'\n",
4356 param.fp_check_mdt_count = 1;
4357 param.fp_exclude_mdt_count = !!neg_opt;
4365 if (pathstart == -1) {
4366 fprintf(stderr, "error: %s: no filename|pathname\n",
4370 } else if (pathend == -1) {
4376 rc = llapi_find(argv[pathstart], ¶m);
4377 if (rc != 0 && ret == 0)
4379 } while (++pathstart < pathend);
4382 fprintf(stderr, "error: %s failed for %s.\n",
4383 argv[0], argv[optind - 1]);
4385 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4386 free(param.fp_obd_uuid);
4388 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4389 free(param.fp_mdt_uuid);
4394 static int lfs_getstripe_internal(int argc, char **argv,
4395 struct find_param *param)
4397 struct option long_opts[] = {
4398 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4399 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4400 { .val = LFS_COMP_COUNT_OPT,
4401 .name = "comp-count", .has_arg = no_argument },
4402 { .val = LFS_COMP_COUNT_OPT,
4403 .name = "component-count", .has_arg = no_argument },
4404 { .val = LFS_COMP_FLAGS_OPT,
4405 .name = "comp-flags", .has_arg = optional_argument },
4406 { .val = LFS_COMP_FLAGS_OPT,
4407 .name = "component-flags", .has_arg = optional_argument },
4408 { .val = LFS_COMP_START_OPT,
4409 .name = "comp-start", .has_arg = optional_argument },
4410 { .val = LFS_COMP_START_OPT,
4411 .name = "component-start", .has_arg = optional_argument },
4412 { .val = LFS_MIRROR_INDEX_OPT,
4413 .name = "mirror-index", .has_arg = required_argument },
4414 { .val = LFS_MIRROR_ID_OPT,
4415 .name = "mirror-id", .has_arg = required_argument },
4416 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4417 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4418 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4419 { .val = 'd', .name = "directory", .has_arg = no_argument },
4420 { .val = 'D', .name = "default", .has_arg = no_argument },
4421 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4422 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4423 { .val = 'F', .name = "fid", .has_arg = no_argument },
4424 { .val = 'g', .name = "generation", .has_arg = no_argument },
4425 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4426 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4427 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4428 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4429 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4430 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4431 { .val = 'L', .name = "layout", .has_arg = no_argument },
4432 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4433 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4434 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4435 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4436 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4437 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4438 { .val = 'O', .name = "obd", .has_arg = required_argument },
4439 { .val = 'O', .name = "ost", .has_arg = required_argument },
4440 { .val = 'p', .name = "pool", .has_arg = no_argument },
4441 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4442 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4443 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4444 { .val = 'R', .name = "raw", .has_arg = no_argument },
4445 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4446 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4447 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4448 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4449 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4450 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4451 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4452 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4456 int pathstart = -1, pathend = -1;
4460 while ((c = getopt_long(argc, argv,
4461 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4462 long_opts, NULL)) != -1) {
4466 /* '!' is part of option */
4467 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4468 if (!isoption && pathend != -1) {
4470 "error: %s: filename|dirname must either precede options or follow options\n",
4474 if (!isoption && pathstart == -1)
4475 pathstart = optind - 1;
4476 if (isoption && pathstart != -1 && pathend == -1)
4477 pathend = optind - 2;
4481 /* unknown: opt is "!" */
4482 if (strcmp(optarg, "!") == 0)
4486 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4487 param->fp_verbose |= VERBOSE_COUNT;
4488 param->fp_max_depth = 0;
4491 case LFS_COMP_COUNT_OPT:
4492 param->fp_verbose |= VERBOSE_COMP_COUNT;
4493 param->fp_max_depth = 0;
4495 case LFS_COMP_FLAGS_OPT:
4496 if (optarg != NULL) {
4497 rc = comp_str2flags(optarg,
4498 ¶m->fp_comp_flags,
4499 ¶m->fp_comp_neg_flags);
4501 fprintf(stderr, "error: %s bad "
4502 "component flags '%s'.\n",
4506 param->fp_check_comp_flags = 1;
4508 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4509 param->fp_max_depth = 0;
4512 case LFS_COMP_START_OPT:
4513 if (optarg != NULL) {
4515 if (tmp[0] == '+') {
4516 param->fp_comp_start_sign = -1;
4518 } else if (tmp[0] == '-') {
4519 param->fp_comp_start_sign = 1;
4522 rc = llapi_parse_size(tmp,
4523 ¶m->fp_comp_start,
4524 ¶m->fp_comp_start_units, 0);
4526 fprintf(stderr, "error: %s bad "
4527 "component start '%s'.\n",
4531 param->fp_check_comp_start = 1;
4534 param->fp_verbose |= VERBOSE_COMP_START;
4535 param->fp_max_depth = 0;
4538 case LFS_MIRROR_INDEX_OPT:
4539 if (optarg[0] == '+') {
4540 param->fp_mirror_index_sign = -1;
4542 } else if (optarg[0] == '-') {
4543 param->fp_mirror_index_sign = 1;
4547 param->fp_mirror_index = strtoul(optarg, &end, 0);
4548 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4549 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4551 "%s %s: invalid mirror index '%s'\n",
4552 progname, argv[0], optarg);
4555 if (param->fp_mirror_id != 0) {
4557 "%s %s: can't specify both mirror index and mirror ID\n",
4561 param->fp_check_mirror_index = 1;
4562 param->fp_exclude_mirror_index = !!neg_opt;
4564 case LFS_MIRROR_ID_OPT:
4565 if (optarg[0] == '+') {
4566 param->fp_mirror_id_sign = -1;
4568 } else if (optarg[0] == '-') {
4569 param->fp_mirror_id_sign = 1;
4573 param->fp_mirror_id = strtoul(optarg, &end, 0);
4574 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4575 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4577 "%s %s: invalid mirror ID '%s'\n",
4578 progname, argv[0], optarg);
4581 if (param->fp_mirror_index != 0) {
4583 "%s %s: can't specify both mirror index and mirror ID\n",
4587 param->fp_check_mirror_id = 1;
4588 param->fp_exclude_mirror_id = !!neg_opt;
4591 param->fp_max_depth = 0;
4594 param->fp_get_default_lmv = 1;
4597 if (optarg != NULL) {
4599 if (tmp[0] == '+') {
4600 param->fp_comp_end_sign = -1;
4602 } else if (tmp[0] == '-') {
4603 param->fp_comp_end_sign = 1;
4607 if (arg_is_eof(tmp)) {
4608 param->fp_comp_end = LUSTRE_EOF;
4609 param->fp_comp_end_units = 1;
4612 rc = llapi_parse_size(tmp,
4613 ¶m->fp_comp_end,
4614 ¶m->fp_comp_end_units, 0);
4617 fprintf(stderr, "error: %s bad "
4618 "component end '%s'.\n",
4622 param->fp_check_comp_end = 1;
4624 param->fp_verbose |= VERBOSE_COMP_END;
4625 param->fp_max_depth = 0;
4629 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4630 param->fp_verbose |= VERBOSE_DFID;
4631 param->fp_max_depth = 0;
4635 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4636 param->fp_verbose |= VERBOSE_GENERATION;
4637 param->fp_max_depth = 0;
4641 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4642 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4643 param->fp_max_depth = 0;
4647 if (optarg != NULL) {
4648 param->fp_comp_id = strtoul(optarg, &end, 0);
4649 if (*end != '\0' || param->fp_comp_id == 0 ||
4650 param->fp_comp_id > LCME_ID_MAX) {
4651 fprintf(stderr, "error: %s bad "
4652 "component id '%s'\n",
4656 param->fp_check_comp_id = 1;
4659 param->fp_max_depth = 0;
4660 param->fp_verbose |= VERBOSE_COMP_ID;
4664 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4665 param->fp_verbose |= VERBOSE_PATTERN;
4666 param->fp_max_depth = 0;
4669 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4671 fprintf(stderr, "warning: '-M' deprecated"
4672 ", use '--mdt-index' or '-m' instead\n");
4675 if (!(param->fp_verbose & VERBOSE_DETAIL))
4676 param->fp_max_depth = 0;
4677 param->fp_verbose |= VERBOSE_MDTINDEX;
4680 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4681 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4682 param->fp_max_depth = 0;
4686 if (param->fp_obd_uuid) {
4688 "error: %s: only one obduuid allowed",
4692 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4695 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4696 param->fp_verbose |= VERBOSE_POOL;
4697 param->fp_max_depth = 0;
4704 param->fp_recursive = 1;
4710 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4711 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4712 param->fp_max_depth = 0;
4716 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4726 if (pathstart == -1) {
4727 fprintf(stderr, "error: %s: no filename|pathname\n",
4730 } else if (pathend == -1) {
4738 if (param->fp_recursive)
4739 param->fp_max_depth = -1;
4740 else if (param->fp_verbose & VERBOSE_DETAIL)
4741 param->fp_max_depth = 1;
4743 if (!param->fp_verbose)
4744 param->fp_verbose = VERBOSE_DEFAULT;
4745 if (param->fp_quiet)
4746 param->fp_verbose = VERBOSE_OBJID;
4749 rc = llapi_getstripe(argv[pathstart], param);
4750 } while (++pathstart < pathend && !rc);
4753 fprintf(stderr, "error: %s failed for %s.\n",
4754 argv[0], argv[optind - 1]);
4758 static int lfs_tgts(int argc, char **argv)
4760 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4761 struct find_param param;
4762 int index = 0, rc=0;
4767 if (argc == 2 && !realpath(argv[1], path)) {
4769 fprintf(stderr, "error: invalid path '%s': %s\n",
4770 argv[1], strerror(-rc));
4774 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4775 /* Check if we have a mount point */
4776 if (mntdir[0] == '\0')
4779 memset(¶m, 0, sizeof(param));
4780 if (!strcmp(argv[0], "mdts"))
4781 param.fp_get_lmv = 1;
4783 rc = llapi_ostlist(mntdir, ¶m);
4785 fprintf(stderr, "error: %s: failed on %s\n",
4788 if (path[0] != '\0')
4790 memset(mntdir, 0, PATH_MAX);
4796 static int lfs_getstripe(int argc, char **argv)
4798 struct find_param param = { 0 };
4800 param.fp_max_depth = 1;
4801 return lfs_getstripe_internal(argc, argv, ¶m);
4805 static int lfs_getdirstripe(int argc, char **argv)
4807 struct find_param param = { 0 };
4808 struct option long_opts[] = {
4809 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4810 { .val = 'D', .name = "default", .has_arg = no_argument },
4811 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4812 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4813 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4814 { .val = 'O', .name = "obd", .has_arg = required_argument },
4815 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4816 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4817 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4818 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4822 param.fp_get_lmv = 1;
4824 while ((c = getopt_long(argc, argv,
4825 "cDHimO:rtTvy", long_opts, NULL)) != -1)
4830 param.fp_verbose |= VERBOSE_COUNT;
4833 param.fp_get_default_lmv = 1;
4837 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4839 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4841 fprintf(stderr, "warning: '-t' deprecated, "
4842 "use '--mdt-hash' or '-H' instead\n");
4845 param.fp_verbose |= VERBOSE_HASH_TYPE;
4848 if (param.fp_obd_uuid) {
4850 "error: %s: only one obduuid allowed",
4854 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4857 param.fp_recursive = 1;
4860 param.fp_verbose |= VERBOSE_DETAIL;
4866 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4867 progname, argv[0], argv[optind - 1]);
4875 if (param.fp_recursive)
4876 param.fp_max_depth = -1;
4878 if (!param.fp_verbose)
4879 param.fp_verbose = VERBOSE_DEFAULT;
4882 rc = llapi_getstripe(argv[optind], ¶m);
4883 } while (++optind < argc && !rc);
4886 fprintf(stderr, "error: %s failed for %s.\n",
4887 argv[0], argv[optind - 1]);
4892 MNTDF_INODES = 0x0001,
4893 MNTDF_COOKED = 0x0002,
4894 MNTDF_LAZY = 0x0004,
4895 MNTDF_VERBOSE = 0x0008,
4896 MNTDF_SHOW = 0x0010,
4899 #define COOK(value) \
4902 while (value > 1024) { \
4910 #define CDF "%11llu"
4911 #define HDF "%8.1f%c"
4915 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4917 double avail, used, ratio = 0;
4920 avail = st->os_ffree;
4921 used = st->os_files - st->os_ffree;
4923 avail = st->os_bavail;
4924 used = st->os_blocks - st->os_bfree;
4926 if (avail + used > 0)
4927 ratio = used / (used + avail) * 100;
4929 /* Round up to match df(1) usage percentage */
4930 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4933 /* This is only used to reflect various problem states for lfs df, so we only
4934 * translate the flags reflecting those states.
4936 static char obd_statfs_state_names[] = {
4937 [OS_STATE_DEGRADED] = 'D',
4938 [OS_STATE_READONLY] = 'R',
4939 [OS_STATE_NOPRECREATE] = 'N',
4940 [OS_STATE_ENOSPC] = 'S',
4941 [OS_STATE_ENOINO] = 'I',
4944 static char obd_statfs_state2char(int s)
4947 if (s > ARRAY_SIZE(obd_statfs_state_names)/sizeof(char) ||
4948 obd_statfs_state_names[s] == 0)
4951 return obd_statfs_state_names[s];
4954 static int showdf(char *mntdir, struct obd_statfs *stat,
4955 char *uuid, enum mntdf_flags flags,
4956 char *type, int index, int rc)
4958 long long avail, used, total;
4960 char *suffix = "KMGTPEZY";
4961 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4962 char tbuf[3 * sizeof(__u64)];
4963 char ubuf[3 * sizeof(__u64)];
4964 char abuf[3 * sizeof(__u64)];
4965 char rbuf[3 * sizeof(__u64)];
4972 if (flags & MNTDF_INODES) {
4973 avail = stat->os_ffree;
4974 used = stat->os_files - stat->os_ffree;
4975 total = stat->os_files;
4977 int shift = flags & MNTDF_COOKED ? 0 : 10;
4979 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4980 used = ((stat->os_blocks - stat->os_bfree) *
4981 stat->os_bsize) >> shift;
4982 total = (stat->os_blocks * stat->os_bsize) >> shift;
4985 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4987 if (flags & MNTDF_COOKED) {
4991 cook_val = (double)total;
4994 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4997 snprintf(tbuf, sizeof(tbuf), CDF, total);
4999 cook_val = (double)used;
5002 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5005 snprintf(ubuf, sizeof(ubuf), CDF, used);
5007 cook_val = (double)avail;
5010 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5013 snprintf(abuf, sizeof(abuf), CDF, avail);
5015 snprintf(tbuf, sizeof(tbuf), CDF, total);
5016 snprintf(ubuf, sizeof(tbuf), CDF, used);
5017 snprintf(abuf, sizeof(tbuf), CDF, avail);
5020 sprintf(rbuf, RDF, ratio);
5021 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5022 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5024 printf("[%s:%d]", type, index);
5026 if (stat->os_state) {
5031 for (i = 0, state = stat->os_state; state != 0; i++) {
5032 uint32_t mask = 1 << i;
5033 if (!(state & mask) || mask == OS_STATE_NONROT)
5035 printf("%c", obd_statfs_state2char(mask));
5043 printf(UUF": inactive device\n", uuid);
5046 printf(UUF": %s\n", uuid, strerror(-rc));
5053 struct ll_stat_type {
5058 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5060 struct ll_statfs_data {
5062 struct obd_statfs sd_st;
5065 struct ll_statfs_buf {
5067 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5070 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5071 int ops, struct ll_statfs_buf *lsb)
5073 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5074 struct obd_uuid uuid_buf;
5075 char *poolname = NULL;
5076 struct ll_stat_type types[] = {
5077 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5078 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5079 { .st_name = NULL } };
5080 struct ll_stat_type *tp;
5081 __u64 ost_files = 0;
5082 __u64 ost_ffree = 0;
5090 poolname = strchr(pool, '.');
5091 if (poolname != NULL) {
5092 if (strncmp(fsname, pool, strlen(fsname))) {
5093 fprintf(stderr, "filesystem name incorrect\n");
5101 fd = open(mntdir, O_RDONLY);
5104 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5109 if (flags & MNTDF_SHOW) {
5110 if (flags & MNTDF_INODES)
5111 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5112 "UUID", "Inodes", "IUsed", "IFree",
5113 "IUse%", "Mounted on");
5115 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5117 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5118 "Used", "Available", "Use%", "Mounted on");
5121 for (tp = types; tp->st_name != NULL; tp++) {
5122 bool have_ost = false;
5124 if (!(tp->st_op & ops))
5127 for (index = 0; ; index++) {
5128 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5129 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5130 type = flags & MNTDF_LAZY ?
5131 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5132 rc2 = llapi_obd_fstatfs(fd, type, index,
5133 &stat_buf, &uuid_buf);
5138 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5139 if (!(flags & MNTDF_VERBOSE))
5141 } else if (rc2 < 0 && rc == 0) {
5145 /* If we have OSTs then don't report MDT block counts.
5146 * For MDT-only filesystems the expectation is that all
5147 * layouts have a DoM component. For filesystems with
5148 * OSTs, files are not necessarily going to store data
5149 * on MDTs, and MDT space is limited to a fraction of
5150 * OST space, so don't include it in the summary.
5152 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5159 if (poolname && tp->st_op == LL_STATFS_LOV &&
5160 llapi_search_ost(fsname, poolname,
5161 obd_uuid2str(&uuid_buf)) != 1)
5164 /* the llapi_obd_statfs() call may have returned with
5165 * an error, but if it filled in uuid_buf we will at
5166 * lease use that to print out a message for that OBD.
5167 * If we didn't get anything in the uuid_buf, then fill
5168 * it in so that we can print an error message. */
5169 if (uuid_buf.uuid[0] == '\0')
5170 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5171 "%s%04x", tp->st_name, index);
5173 lsb->sb_buf[lsb->sb_count].sd_index = index;
5174 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5177 if (flags & MNTDF_SHOW)
5178 showdf(mntdir, &stat_buf,
5179 obd_uuid2str(&uuid_buf), flags,
5180 tp->st_name, index, rc2);
5185 if (tp->st_op == LL_STATFS_LMV) {
5186 sum.os_ffree += stat_buf.os_ffree;
5187 sum.os_files += stat_buf.os_files;
5188 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5189 ost_files += stat_buf.os_files;
5190 ost_ffree += stat_buf.os_ffree;
5192 sum.os_blocks += stat_buf.os_blocks *
5194 sum.os_bfree += stat_buf.os_bfree *
5196 sum.os_bavail += stat_buf.os_bavail *
5203 /* If we have _some_ OSTs, but don't have as many free objects on the
5204 * OST as inodes on the MDTs, reduce the reported number of inodes
5205 * to compensate, so that the "inodes in use" number is correct.
5206 * This should be kept in sync with ll_statfs_internal().
5208 if (ost_files && ost_ffree < sum.os_ffree) {
5209 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5210 sum.os_ffree = ost_ffree;
5212 if (flags & MNTDF_SHOW) {
5214 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5221 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5223 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5225 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5227 int r1 = obd_statfs_ratio(st1, false);
5228 int r2 = obd_statfs_ratio(st2, false);
5229 int64_t result = r1 - r2;
5231 /* if both space usage are above 90, compare free inodes */
5232 if (r1 > 90 && r2 > 90)
5233 result = st2->os_ffree - st1->os_ffree;
5237 else if (result == 0)
5244 static int lfs_setdirstripe(int argc, char **argv)
5247 struct lfs_setstripe_args lsa = { 0 };
5248 struct llapi_stripe_param *param = NULL;
5249 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5252 char *mode_opt = NULL;
5253 bool default_stripe = false;
5254 bool delete = false;
5255 bool auto_distributed = false;
5256 bool foreign_mode = false;
5257 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5258 mode_t previous_mode = 0;
5259 struct ll_statfs_buf *lsb = NULL;
5260 char mntdir[PATH_MAX] = "";
5262 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5263 struct option long_opts[] = {
5264 { .val = 'c', .name = "count", .has_arg = required_argument },
5265 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5266 { .val = 'd', .name = "delete", .has_arg = no_argument },
5267 { .val = 'D', .name = "default", .has_arg = no_argument },
5268 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5269 { .val = LFS_LAYOUT_FLAGS_OPT,
5270 .name = "flags", .has_arg = required_argument },
5271 { .val = LFS_LAYOUT_FOREIGN_OPT,
5272 .name = "foreign", .has_arg = optional_argument},
5273 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5274 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5275 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5276 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5278 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5279 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5281 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5282 { .val = 'i', .name = "index", .has_arg = required_argument },
5284 { .val = 'o', .name = "mode", .has_arg = required_argument },
5285 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5286 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5288 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5289 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5290 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5294 setstripe_args_init(&lsa);
5296 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5297 long_opts, NULL)) >= 0) {
5304 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5307 "%s %s: invalid stripe count '%s'\n",
5308 progname, argv[0], optarg);
5314 default_stripe = true;
5317 default_stripe = true;
5319 case LFS_LAYOUT_FOREIGN_OPT:
5320 if (optarg != NULL) {
5321 /* check pure numeric */
5322 type = strtoul(optarg, &end, 0);
5325 type = check_foreign_type_name(optarg);
5326 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5328 "%s %s: unknown foreign type '%s'\n",
5335 foreign_mode = true;
5337 case LFS_LAYOUT_FLAGS_OPT:
5338 flags = strtoul(optarg, &end, 16);
5341 "%s %s: bad flags '%s'\n",
5342 progname, argv[0], optarg);
5346 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5348 fprintf(stderr, "warning: '--hash-type' and '-t' "
5349 "deprecated, use '--mdt-hash' or '-H' instead\n");
5352 lsa.lsa_pattern = check_hashtype(optarg);
5353 if (lsa.lsa_pattern == 0) {
5355 "%s %s: bad stripe hash type '%s'\n",
5356 progname, argv[0], optarg);
5361 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5364 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5365 if (strcmp(argv[optind - 1], "--index") == 0)
5367 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5370 lsa.lsa_nr_tgts = parse_targets(mdts,
5371 sizeof(mdts) / sizeof(__u32),
5372 lsa.lsa_nr_tgts, optarg, NULL);
5373 if (lsa.lsa_nr_tgts < 0) {
5375 "%s %s: invalid MDT target(s) '%s'\n",
5376 progname, argv[0], optarg);
5380 lsa.lsa_tgts = mdts;
5381 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5382 lsa.lsa_stripe_off = mdts[0];
5384 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5386 fprintf(stderr, "warning: '-m' is deprecated, "
5387 "use '--mode' or '-o' instead\n");
5396 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5397 progname, argv[0], argv[optind - 1]);
5402 if (optind == argc) {
5403 fprintf(stderr, "%s %s: DIR must be specified\n",
5408 if (xattr && !foreign_mode) {
5409 /* only print a warning as this is armless and will be
5413 "%s %s: xattr has been specified for non-foreign layout\n",
5415 } else if (foreign_mode && !xattr) {
5417 "%s %s: xattr must be provided in foreign mode\n",
5422 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5423 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5425 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5430 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5431 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5433 "%s %s: stripe offset and count must be specified\n",
5439 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5440 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5442 "%s %s: cannot specify -d with -c or -i options\n",
5447 if (mode_opt != NULL) {
5448 mode = strtoul(mode_opt, &end, 8);
5451 "%s %s: bad MODE '%s'\n",
5452 progname, argv[0], mode_opt);
5455 previous_mode = umask(0);
5458 /* foreign LMV/dir case */
5460 if (argc > optind + 1) {
5462 "%s %s: cannot specify multiple foreign dirs\n",
5467 dname = argv[optind];
5468 result = llapi_dir_create_foreign(dname, mode, type, flags,
5472 "%s mkdir: can't create foreign dir '%s': %s\n",
5473 progname, dname, strerror(-result));
5478 * initialize stripe parameters, in case param is converted to specific,
5479 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5481 param = calloc(1, offsetof(typeof(*param),
5482 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5483 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5484 if (param == NULL) {
5486 "%s %s: cannot allocate memory for parameters: %s\n",
5487 progname, argv[0], strerror(ENOMEM));
5491 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5492 param->lsp_stripe_count = lsa.lsa_stripe_count;
5493 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5494 param->lsp_stripe_offset = -1;
5496 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5497 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5498 param->lsp_stripe_pattern = lsa.lsa_pattern;
5500 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
5501 param->lsp_pool = lsa.lsa_pool_name;
5502 param->lsp_is_specific = false;
5503 if (lsa.lsa_nr_tgts > 1) {
5504 if (lsa.lsa_stripe_count > 0 &&
5505 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5506 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5507 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5508 "match the number of MDTs: %d\n",
5509 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5514 param->lsp_is_specific = true;
5515 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5516 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5519 if (!default_stripe && (lsa.lsa_pattern & LMV_HASH_FLAG_SPACE)) {
5520 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5526 if (param->lsp_stripe_offset != -1 &&
5527 lsa.lsa_pattern & LMV_HASH_FLAG_SPACE) {
5528 fprintf(stderr, "%s %s: can only specify -H space with -i -1\n",
5534 dname = argv[optind];
5536 if (default_stripe) {
5537 result = llapi_dir_set_default_lmv(dname, param);
5540 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5541 progname, dname, strerror(-result));
5546 * if current \a dname isn't under the same \a mntdir as the
5547 * last one, and the last one was auto-distributed, restore
5550 if (mntdir[0] != '\0' &&
5551 strncmp(dname, mntdir, strlen(mntdir)) &&
5553 param->lsp_is_specific = false;
5554 param->lsp_stripe_offset = -1;
5555 auto_distributed = false;
5559 * TODO: when MDT can allocate object with QoS (LU-9435), below
5560 * code should be removed, instead we should let LMV to allocate
5561 * the starting MDT object, and then let LOD allocate other MDT
5564 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5565 char path[PATH_MAX] = "";
5568 lsb = malloc(sizeof(*lsb));
5576 /* use mntdir for dirname() temporarily */
5577 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5578 if (!realpath(dirname(mntdir), path)) {
5581 "error: invalid path '%s': %s\n",
5582 argv[optind], strerror(errno));
5587 result = llapi_search_mounts(path, 0, mntdir, NULL);
5588 if (result < 0 || mntdir[0] == '\0') {
5590 "No suitable Lustre mount found\n");
5594 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5599 if (param->lsp_stripe_count > lsb->sb_count) {
5601 "error: stripe count %d is too big\n",
5602 param->lsp_stripe_count);
5607 qsort(lsb->sb_buf, lsb->sb_count,
5608 sizeof(struct ll_statfs_data),
5609 ll_statfs_data_comp);
5611 auto_distributed = true;
5614 if (auto_distributed) {
5616 int nr = MAX(param->lsp_stripe_count,
5619 /* don't use server whose usage is above 90% */
5620 while (nr != param->lsp_stripe_count &&
5621 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5623 nr = MAX(param->lsp_stripe_count, nr / 2);
5625 /* get \a r between [0, nr) */
5628 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5629 if (param->lsp_stripe_count > 1) {
5632 param->lsp_is_specific = true;
5633 for (; i < param->lsp_stripe_count; i++)
5634 param->lsp_tgts[(i + r) % nr] =
5635 lsb->sb_buf[i].sd_index;
5639 result = llapi_dir_create(dname, mode, param);
5642 "%s setdirstripe: cannot create dir '%s': %s\n",
5643 progname, dname, strerror(-result));
5644 } while (!result && (dname = argv[++optind]));
5646 if (mode_opt != NULL)
5647 umask(previous_mode);
5655 static int lfs_rmentry(int argc, char **argv)
5662 fprintf(stderr, "error: %s: missing dirname\n",
5668 dname = argv[index];
5669 while (dname != NULL) {
5670 result = llapi_direntry_remove(dname);
5672 fprintf(stderr, "error: %s: remove dir entry '%s' "
5673 "failed\n", argv[0], dname);
5676 dname = argv[++index];
5681 static int lfs_mv(int argc, char **argv)
5683 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5684 struct find_param param = {
5691 struct option long_opts[] = {
5692 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5693 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5694 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5697 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5699 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5701 fprintf(stderr, "warning: '-M' deprecated"
5702 ", use '--mdt-index' or '-m' instead\n");
5705 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5707 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5713 param.fp_verbose = VERBOSE_DETAIL;
5716 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5717 progname, argv[optind - 1]);
5722 if (lmu.lum_stripe_offset == -1) {
5723 fprintf(stderr, "%s mv: MDT index must be specified\n",
5728 if (optind >= argc) {
5729 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5734 /* initialize migrate mdt parameters */
5735 param.fp_lmv_md = &lmu;
5736 param.fp_migrate = 1;
5737 rc = llapi_migrate_mdt(argv[optind], ¶m);
5739 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5740 progname, argv[optind], param.fp_mdt_index,
5745 static int lfs_osts(int argc, char **argv)
5747 return lfs_tgts(argc, argv);
5750 static int lfs_mdts(int argc, char **argv)
5752 return lfs_tgts(argc, argv);
5755 static int lfs_df(int argc, char **argv)
5757 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5758 enum mntdf_flags flags = MNTDF_SHOW;
5759 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5760 int c, rc = 0, index = 0;
5761 char fsname[PATH_MAX] = "", *pool_name = NULL;
5762 struct option long_opts[] = {
5763 { .val = 'h', .name = "human-readable",
5764 .has_arg = no_argument },
5765 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5766 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5767 { .val = 'p', .name = "pool", .has_arg = required_argument },
5768 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5771 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5774 flags |= MNTDF_COOKED;
5777 flags |= MNTDF_INODES;
5780 flags |= MNTDF_LAZY;
5786 flags |= MNTDF_VERBOSE;
5792 if (optind < argc && !realpath(argv[optind], path)) {
5794 fprintf(stderr, "error: invalid path '%s': %s\n",
5795 argv[optind], strerror(-rc));
5799 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5800 /* Check if we have a mount point */
5801 if (mntdir[0] == '\0')
5804 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5805 if (rc || path[0] != '\0')
5807 fsname[0] = '\0'; /* avoid matching in next loop */
5808 mntdir[0] = '\0'; /* avoid matching in next loop */
5814 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5815 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5819 /* llapi_search_mounts() fills "fsname", but that is not called if
5820 * explicit paths are specified on the command-line
5822 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5823 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5826 fprintf(stderr, "cannot get instance for '%s': %s\n",
5827 mntdir, strerror(-rc));
5831 if (!opt_instance) {
5832 /* print only the fsname name */
5833 buf = strchr(fsname, '-');
5837 } else if (!opt_fsname) {
5838 /* print only the instance name */
5839 buf = strchr(fsname, '-');
5845 } else if (opt_fsname) {
5846 /* print only the fsname */
5850 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5855 static int lfs_getname(int argc, char **argv)
5857 struct option long_opts[] = {
5858 { .val = 'h', .name = "help", .has_arg = no_argument },
5859 { .val = 'i', .name = "instance", .has_arg = no_argument },
5860 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5862 bool opt_instance = false, opt_fsname = false;
5863 char fsname[PATH_MAX] = "";
5866 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5869 opt_instance = true;
5880 /* If neither option is given, print both instance and fsname */
5881 if (!opt_instance && !opt_fsname)
5882 opt_instance = opt_fsname = true;
5884 if (optind == argc) { /* no paths specified, get all paths. */
5885 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5888 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5889 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5890 opt_instance, opt_fsname, true);
5893 path[0] = fsname[0] = mntdir[0] = '\0';
5895 } else { /* paths specified, only attempt to search these. */
5898 /* if only one path is given, print only requested info */
5899 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5901 for (; optind < argc; optind++) {
5902 rc2 = print_instance(argv[optind], fsname,
5903 sizeof(fsname), opt_instance,
5904 opt_fsname, opt_mntdir);
5914 static int lfs_check(int argc, char **argv)
5916 char mntdir[PATH_MAX] = {'\0'};
5925 fprintf(stderr, "%s check: server type must be specified\n",
5930 obd_types[0] = obd_type1;
5931 obd_types[1] = obd_type2;
5932 obd_types[2] = obd_type3;
5934 if (strcmp(argv[1], "osts") == 0) {
5935 strcpy(obd_types[0], "osc");
5936 } else if (strcmp(argv[1], "mdts") == 0 ||
5937 strcmp(argv[1], "mds") == 0) {
5938 strcpy(obd_types[0], "mdc");
5939 } else if (strcmp(argv[1], "mgts") == 0) {
5940 strcpy(obd_types[0], "mgc");
5941 } else if (strcmp(argv[1], "all") == 0 ||
5942 strcmp(argv[1], "servers") == 0) {
5944 strcpy(obd_types[0], "osc");
5945 strcpy(obd_types[1], "mdc");
5946 strcpy(obd_types[2], "mgc");
5948 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5953 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5954 if (rc < 0 || mntdir[0] == '\0') {
5956 "%s check: cannot find mounted Lustre filesystem: %s\n",
5957 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5961 rc = llapi_target_check(num_types, obd_types, mntdir);
5963 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5964 progname, argv[1], strerror(-rc));
5970 #ifdef HAVE_SYS_QUOTA_H
5971 #define ARG2INT(nr, str, msg) \
5974 nr = strtol(str, &endp, 0); \
5975 if (*endp != '\0') { \
5976 fprintf(stderr, "%s: bad %s '%s'\n", \
5977 progname, msg, str); \
5982 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5984 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5985 * returns the value or ULONG_MAX on integer overflow or incorrect format
5987 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5988 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5989 * 3. empty integer value is interpreted as 0
5991 static unsigned long str2sec(const char* timestr)
5993 const char spec[] = "smhdw";
5994 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5995 unsigned long val = 0;
5998 if (strpbrk(timestr, spec) == NULL) {
5999 /* no specifiers inside the time string,
6000 should treat it as an integer value */
6001 val = strtoul(timestr, &tail, 10);
6002 return *tail ? ULONG_MAX : val;
6005 /* format string is XXwXXdXXhXXmXXs */
6011 v = strtoul(timestr, &tail, 10);
6012 if (v == ULONG_MAX || *tail == '\0')
6013 /* value too large (ULONG_MAX or more)
6014 or missing specifier */
6017 ptr = strchr(spec, *tail);
6019 /* unknown specifier */
6024 /* check if product will overflow the type */
6025 if (!(v < ULONG_MAX / mult[ind]))
6028 ADD_OVERFLOW(val, mult[ind] * v);
6029 if (val == ULONG_MAX)
6041 #define ARG2ULL(nr, str, def_units) \
6043 unsigned long long limit, units = def_units; \
6046 rc = llapi_parse_size(str, &limit, &units, 1); \
6048 fprintf(stderr, "%s: invalid limit '%s'\n", \
6055 static inline int has_times_option(int argc, char **argv)
6059 for (i = 1; i < argc; i++)
6060 if (!strcmp(argv[i], "-t"))
6066 int lfs_setquota_times(int argc, char **argv)
6069 struct if_quotactl qctl;
6070 char *mnt, *obd_type = (char *)qctl.obd_type;
6071 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6072 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6073 struct option long_opts[] = {
6074 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6075 { .val = 'g', .name = "group", .has_arg = no_argument },
6076 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6077 { .val = 'p', .name = "projid", .has_arg = no_argument },
6078 { .val = 't', .name = "times", .has_arg = no_argument },
6079 { .val = 'u', .name = "user", .has_arg = no_argument },
6083 memset(&qctl, 0, sizeof(qctl));
6084 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6085 qctl.qc_type = ALLQUOTA;
6087 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6088 long_opts, NULL)) != -1) {
6099 if (qctl.qc_type != ALLQUOTA) {
6100 fprintf(stderr, "error: -u/g/p can't be used "
6101 "more than once\n");
6104 qctl.qc_type = qtype;
6107 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6108 fprintf(stderr, "error: bad block-grace: %s\n",
6112 dqb->dqb_valid |= QIF_BTIME;
6115 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6116 fprintf(stderr, "error: bad inode-grace: %s\n",
6120 dqb->dqb_valid |= QIF_ITIME;
6122 case 't': /* Yes, of course! */
6124 default: /* getopt prints error message for us when opterr != 0 */
6129 if (qctl.qc_type == ALLQUOTA) {
6130 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6134 if (optind != argc - 1) {
6135 fprintf(stderr, "error: unexpected parameters encountered\n");
6139 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6140 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6141 fprintf(stderr, "error: grace time is too large\n");
6146 rc = llapi_quotactl(mnt, &qctl);
6149 fprintf(stderr, "%s %s ", obd_type,
6150 obd_uuid2str(&qctl.obd_uuid));
6151 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6158 #define BSLIMIT (1 << 0)
6159 #define BHLIMIT (1 << 1)
6160 #define ISLIMIT (1 << 2)
6161 #define IHLIMIT (1 << 3)
6163 int lfs_setquota(int argc, char **argv)
6166 struct if_quotactl qctl;
6167 char *mnt, *obd_type = (char *)qctl.obd_type;
6168 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6169 struct option long_opts[] = {
6170 { .val = 'b', .name = "block-softlimit",
6171 .has_arg = required_argument },
6172 { .val = 'B', .name = "block-hardlimit",
6173 .has_arg = required_argument },
6174 { .val = 'd', .name = "default", .has_arg = no_argument },
6175 { .val = 'g', .name = "group", .has_arg = required_argument },
6176 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6177 { .val = 'i', .name = "inode-softlimit",
6178 .has_arg = required_argument },
6179 { .val = 'I', .name = "inode-hardlimit",
6180 .has_arg = required_argument },
6181 { .val = 'p', .name = "projid", .has_arg = required_argument },
6182 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6183 { .val = 'u', .name = "user", .has_arg = required_argument },
6184 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6186 unsigned limit_mask = 0;
6188 bool use_default = false;
6191 if (has_times_option(argc, argv))
6192 return lfs_setquota_times(argc, argv);
6194 memset(&qctl, 0, sizeof(qctl));
6195 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6196 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6197 * so it can be used as a marker that qc_type
6198 * isn't reinitialized from command line */
6200 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6201 long_opts, NULL)) != -1) {
6204 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6207 goto quota_type_def;
6210 rc = name2uid(&qctl.qc_id, optarg);
6213 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6216 goto quota_type_def;
6219 rc = name2gid(&qctl.qc_id, optarg);
6222 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6225 goto quota_type_def;
6228 rc = name2projid(&qctl.qc_id, optarg);
6231 qctl.qc_id = strtoul(optarg, &endptr, 10);
6232 if (*endptr != '\0') {
6233 fprintf(stderr, "%s setquota: invalid"
6234 " id '%s'\n", progname, optarg);
6239 if (qctl.qc_id == 0) {
6240 fprintf(stderr, "%s setquota: can't set quota"
6241 " for root usr/group/project.\n",
6247 if (qctl.qc_type != ALLQUOTA) {
6249 "%s setquota: only one of -u, -U, -g,"
6250 " -G, -p or -P may be specified\n",
6254 qctl.qc_type = qtype;
6257 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6261 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6262 dqb->dqb_bsoftlimit >>= 10;
6263 limit_mask |= BSLIMIT;
6264 if (dqb->dqb_bsoftlimit &&
6265 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6267 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6268 "See '%s help setquota' or Lustre manual for details\n",
6269 progname, dqb->dqb_bsoftlimit,
6273 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6274 dqb->dqb_bhardlimit >>= 10;
6275 limit_mask |= BHLIMIT;
6276 if (dqb->dqb_bhardlimit &&
6277 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6279 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6280 "See '%s help setquota' or Lustre manual for details\n",
6281 progname, dqb->dqb_bhardlimit,
6285 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6286 limit_mask |= ISLIMIT;
6287 if (dqb->dqb_isoftlimit &&
6288 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6290 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6291 "See '%s help setquota' or Lustre manual for details\n",
6292 progname, dqb->dqb_isoftlimit,
6296 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6297 limit_mask |= IHLIMIT;
6298 if (dqb->dqb_ihardlimit &&
6299 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6301 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6302 "See '%s help setquota' or Lustre manual for details\n",
6303 progname, dqb->dqb_ihardlimit,
6308 "%s setquota: unrecognized option '%s'\n",
6309 progname, argv[optind - 1]);
6314 if (qctl.qc_type == ALLQUOTA) {
6316 "%s setquota: either -u or -g must be specified\n",
6321 if (!use_default && limit_mask == 0) {
6323 "%s setquota: at least one limit must be specified\n",
6328 if (use_default && limit_mask != 0) {
6330 "%s setquota: limits should not be specified when"
6331 " using default quota\n",
6336 if (use_default && qctl.qc_id == 0) {
6338 "%s setquota: can not set default quota for root"
6339 " user/group/project\n",
6344 if (optind != argc - 1) {
6346 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6347 progname, argv[optind]);
6354 dqb->dqb_bhardlimit = 0;
6355 dqb->dqb_bsoftlimit = 0;
6356 dqb->dqb_ihardlimit = 0;
6357 dqb->dqb_isoftlimit = 0;
6360 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6361 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6362 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6363 /* sigh, we can't just set blimits/ilimits */
6364 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6365 .qc_type = qctl.qc_type,
6366 .qc_id = qctl.qc_id};
6368 rc = llapi_quotactl(mnt, &tmp_qctl);
6372 if (!(limit_mask & BHLIMIT))
6373 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6374 if (!(limit_mask & BSLIMIT))
6375 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6376 if (!(limit_mask & IHLIMIT))
6377 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6378 if (!(limit_mask & ISLIMIT))
6379 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6381 /* Keep grace times if we have got no softlimit arguments */
6382 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6383 dqb->dqb_valid |= QIF_BTIME;
6384 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6387 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6388 dqb->dqb_valid |= QIF_ITIME;
6389 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6393 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6394 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6396 rc = llapi_quotactl(mnt, &qctl);
6400 "%s setquota: cannot quotactl '%s' '%s': %s",
6402 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6409 /* Converts seconds value into format string
6410 * result is returned in buf
6412 * 1. result is in descenting order: 1w2d3h4m5s
6413 * 2. zero fields are not filled (except for p. 3): 5d1s
6414 * 3. zero seconds value is presented as "0s"
6416 static char * __sec2str(time_t seconds, char *buf)
6418 const char spec[] = "smhdw";
6419 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6424 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6425 c = seconds / mult[i];
6427 if (c > 0 || (i == 0 && buf == tail))
6428 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6436 static void sec2str(time_t seconds, char *buf, int rc)
6443 tail = __sec2str(seconds, tail);
6445 if (rc && tail - buf < 39) {
6451 static void diff2str(time_t seconds, char *buf, time_t now)
6457 if (seconds <= now) {
6458 strcpy(buf, "none");
6461 __sec2str(seconds - now, buf);
6464 static void print_quota_title(char *name, struct if_quotactl *qctl,
6465 bool human_readable, bool show_default)
6468 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6469 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6470 "Filesystem", "bquota", "blimit", "bgrace",
6471 "iquota", "ilimit", "igrace");
6473 printf("Disk quotas for %s %s (%cid %u):\n",
6474 qtype_name(qctl->qc_type), name,
6475 *qtype_name(qctl->qc_type), qctl->qc_id);
6476 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6477 "Filesystem", human_readable ? "used" : "kbytes",
6478 "quota", "limit", "grace",
6479 "files", "quota", "limit", "grace");
6483 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6486 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6489 snprintf(buf, buflen, "%5.4gP",
6490 (double)num / ((__u64)1 << 40));
6492 snprintf(buf, buflen, "%5.4gT",
6493 (double)num / (1 << 30));
6495 snprintf(buf, buflen, "%5.4gG",
6496 (double)num / (1 << 20));
6498 snprintf(buf, buflen, "%5.4gM",
6499 (double)num / (1 << 10));
6501 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6505 #define STRBUF_LEN 32
6506 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6507 int rc, bool h, bool show_default)
6513 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6514 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6515 int bover = 0, iover = 0;
6516 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6517 char numbuf[3][STRBUF_LEN];
6519 char strbuf[STRBUF_LEN];
6521 if (dqb->dqb_bhardlimit &&
6522 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6524 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6525 if (dqb->dqb_btime > now) {
6532 if (dqb->dqb_ihardlimit &&
6533 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6535 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6536 if (dqb->dqb_itime > now) {
6544 if (strlen(mnt) > 15)
6545 printf("%s\n%15s", mnt, "");
6547 printf("%15s", mnt);
6550 diff2str(dqb->dqb_btime, timebuf, now);
6551 else if (show_default)
6552 snprintf(timebuf, sizeof(timebuf), "%llu",
6555 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6556 strbuf, sizeof(strbuf), h);
6557 if (rc == -EREMOTEIO)
6558 sprintf(numbuf[0], "%s*", strbuf);
6560 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6561 "%s" : "[%s]", strbuf);
6563 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6564 if (type == QC_GENERAL)
6565 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6566 "%s" : "[%s]", strbuf);
6568 sprintf(numbuf[1], "%s", "-");
6570 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6571 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6572 "%s" : "[%s]", strbuf);
6575 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6577 printf(" %7s%c %6s %7s %7s",
6578 numbuf[0], bover ? '*' : ' ', numbuf[1],
6579 numbuf[2], bover > 1 ? timebuf : "-");
6583 diff2str(dqb->dqb_itime, timebuf, now);
6584 else if (show_default)
6585 snprintf(timebuf, sizeof(timebuf), "%llu",
6588 snprintf(numbuf[0], sizeof(numbuf),
6589 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6590 (uintmax_t)dqb->dqb_curinodes);
6592 if (type == QC_GENERAL)
6593 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6595 (uintmax_t)dqb->dqb_isoftlimit);
6597 sprintf(numbuf[1], "%s", "-");
6599 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6600 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6603 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6604 else if (type != QC_OSTIDX)
6605 printf(" %7s%c %6s %7s %7s",
6606 numbuf[0], iover ? '*' : ' ', numbuf[1],
6607 numbuf[2], iover > 1 ? timebuf : "-");
6609 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6612 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6613 qctl->qc_cmd == Q_GETOINFO) {
6617 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6618 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6619 printf("Block grace time: %s; Inode grace time: %s\n",
6620 bgtimebuf, igtimebuf);
6624 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6625 bool h, __u64 *total)
6627 int rc = 0, rc1 = 0, count = 0;
6628 __u32 valid = qctl->qc_valid;
6630 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6632 fprintf(stderr, "can not get %s count: %s\n",
6633 is_mdt ? "mdt": "ost", strerror(-rc));
6637 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6638 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6639 rc = llapi_quotactl(mnt, qctl);
6641 /* It is remote client case. */
6642 if (rc == -EOPNOTSUPP) {
6649 fprintf(stderr, "quotactl %s%d failed.\n",
6650 is_mdt ? "mdt": "ost", qctl->qc_idx);
6654 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6655 qctl->qc_valid, 0, h, false);
6656 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6657 qctl->qc_dqblk.dqb_bhardlimit;
6660 qctl->qc_valid = valid;
6664 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6665 int verbose, int quiet, bool human_readable,
6668 int rc1 = 0, rc2 = 0, rc3 = 0;
6669 char *obd_type = (char *)qctl->obd_type;
6670 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6671 __u64 total_ialloc = 0, total_balloc = 0;
6672 bool use_default_for_blk = false;
6673 bool use_default_for_file = false;
6676 rc1 = llapi_quotactl(mnt, qctl);
6680 fprintf(stderr, "%s quotas are not enabled.\n",
6681 qtype_name(qctl->qc_type));
6684 fprintf(stderr, "Permission denied.\n");
6687 /* We already got error message. */
6690 fprintf(stderr, "Unexpected quotactl error: %s\n",
6695 if (!show_default && qctl->qc_id == 0) {
6696 qctl->qc_dqblk.dqb_bhardlimit = 0;
6697 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6698 qctl->qc_dqblk.dqb_ihardlimit = 0;
6699 qctl->qc_dqblk.dqb_isoftlimit = 0;
6700 qctl->qc_dqblk.dqb_btime = 0;
6701 qctl->qc_dqblk.dqb_itime = 0;
6702 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6705 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6706 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6707 use_default_for_blk = true;
6708 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6711 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6712 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6713 use_default_for_file = true;
6714 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6717 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6718 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6719 print_quota_title(name, qctl, human_readable, show_default);
6721 if (rc1 && *obd_type)
6722 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6724 if (qctl->qc_valid != QC_GENERAL)
6727 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6728 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6729 (QIF_LIMITS|QIF_USAGE));
6731 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6733 if (!show_default && verbose &&
6734 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6735 char strbuf[STRBUF_LEN];
6737 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6739 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6741 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6743 printf("Total allocated inode limit: %ju, total "
6744 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6748 if (use_default_for_blk)
6749 printf("%cid %u is using default block quota setting\n",
6750 *qtype_name(qctl->qc_type), qctl->qc_id);
6752 if (use_default_for_file)
6753 printf("%cid %u is using default file quota setting\n",
6754 *qtype_name(qctl->qc_type), qctl->qc_id);
6756 if (rc1 || rc2 || rc3 || inacc)
6757 printf("Some errors happened when getting quota info. "
6758 "Some devices may be not working or deactivated. "
6759 "The data in \"[]\" is inaccurate.\n");
6765 static int lfs_project(int argc, char **argv)
6767 int ret = 0, err = 0, c, i;
6768 struct project_handle_control phc = { 0 };
6769 enum lfs_project_ops_t op;
6772 phc.assign_projid = false;
6773 /* default action */
6774 op = LFS_PROJECT_LIST;
6776 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6779 if (op != LFS_PROJECT_LIST) {
6781 "%s: cannot specify '-c' '-C' '-s' together\n",
6786 op = LFS_PROJECT_CHECK;
6789 if (op != LFS_PROJECT_LIST) {
6791 "%s: cannot specify '-c' '-C' '-s' together\n",
6796 op = LFS_PROJECT_CLEAR;
6799 if (op != LFS_PROJECT_LIST) {
6801 "%s: cannot specify '-c' '-C' '-s' together\n",
6806 phc.set_inherit = true;
6807 op = LFS_PROJECT_SET;
6813 phc.keep_projid = true;
6816 phc.recursive = true;
6819 phc.projid = strtoul(optarg, NULL, 0);
6820 phc.assign_projid = true;
6824 phc.newline = false;
6827 fprintf(stderr, "%s: invalid option '%c'\n",
6833 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6834 op = LFS_PROJECT_SET;
6835 phc.set_projid = true;
6836 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6837 phc.set_projid = true;
6841 case LFS_PROJECT_CHECK:
6842 if (phc.keep_projid) {
6844 "%s: '-k' is useless together with '-c'\n",
6849 case LFS_PROJECT_CLEAR:
6852 "%s: '-0' is useless together with '-C'\n",
6856 if (phc.assign_projid) {
6858 "%s: '-p' is useless together with '-C'\n",
6863 case LFS_PROJECT_SET:
6866 "%s: '-0' is useless together with '-s'\n",
6870 if (phc.keep_projid) {
6872 "%s: '-k' is useless together with '-s'\n",
6880 "%s: '-0' is useless for list operations\n",
6890 fprintf(stderr, "%s: missing file or directory target(s)\n",
6895 for (i = 0; i < argc; i++) {
6897 case LFS_PROJECT_CHECK:
6898 err = lfs_project_check(argv[i], &phc);
6900 case LFS_PROJECT_LIST:
6901 err = lfs_project_list(argv[i], &phc);
6903 case LFS_PROJECT_CLEAR:
6904 err = lfs_project_clear(argv[i], &phc);
6906 case LFS_PROJECT_SET:
6907 err = lfs_project_set(argv[i], &phc);
6919 static int lfs_quota(int argc, char **argv)
6922 char *mnt, *name = NULL;
6923 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6924 .qc_type = ALLQUOTA };
6925 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6926 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6928 __u32 valid = QC_GENERAL, idx = 0;
6929 bool human_readable = false;
6930 bool show_default = false;
6933 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6936 show_default = true;
6941 show_default = true;
6946 show_default = true;
6950 if (qctl.qc_type != ALLQUOTA) {
6952 "%s quota: only one of -u, -g, or -p may be specified\n",
6956 qctl.qc_type = qtype;
6959 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6962 valid = qctl.qc_valid = QC_UUID;
6963 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6966 valid = qctl.qc_valid = QC_MDTIDX;
6967 idx = qctl.qc_idx = atoi(optarg);
6968 if (idx == 0 && *optarg != '0') {
6970 "%s quota: invalid MDT index '%s'\n",
6976 valid = qctl.qc_valid = QC_OSTIDX;
6977 idx = qctl.qc_idx = atoi(optarg);
6978 if (idx == 0 && *optarg != '0') {
6980 "%s quota: invalid OST index '%s'\n",
6992 human_readable = true;
6995 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6996 progname, argv[optind - 1]);
7001 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7002 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7003 optind == argc - 1 && !show_default) {
7005 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7006 qctl.qc_valid = valid;
7009 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7010 qctl.qc_type = qtype;
7011 if (qtype == USRQUOTA) {
7012 qctl.qc_id = geteuid();
7013 rc = uid2name(&name, qctl.qc_id);
7015 qctl.qc_id = getegid();
7016 rc = gid2name(&name, qctl.qc_id);
7021 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7022 human_readable, show_default);
7027 /* lfs quota -u username /path/to/lustre/mount */
7028 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7029 /* options should be followed by u/g-name and mntpoint */
7030 if ((!show_default && optind + 2 != argc) ||
7031 (show_default && optind + 1 != argc) ||
7032 qctl.qc_type == ALLQUOTA) {
7034 "%s quota: name and mount point must be specified\n",
7039 if (!show_default) {
7040 name = argv[optind++];
7041 switch (qctl.qc_type) {
7043 rc = name2uid(&qctl.qc_id, name);
7046 rc = name2gid(&qctl.qc_id, name);
7049 rc = name2projid(&qctl.qc_id, name);
7056 qctl.qc_valid = QC_GENERAL;
7057 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7062 qctl.qc_id = strtoul(name, &endptr, 10);
7063 if (*endptr != '\0') {
7064 fprintf(stderr, "%s quota: invalid id '%s'\n",
7069 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7070 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7076 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7077 human_readable, show_default);
7080 #endif /* HAVE_SYS_QUOTA_H! */
7082 static int flushctx_ioctl(char *mp)
7086 fd = open(mp, O_RDONLY);
7088 fprintf(stderr, "flushctx: error open %s: %s\n",
7089 mp, strerror(errno));
7093 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7095 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7096 mp, strerror(errno));
7102 static int lfs_flushctx(int argc, char **argv)
7104 int kdestroy = 0, c;
7105 char mntdir[PATH_MAX] = {'\0'};
7109 while ((c = getopt(argc, argv, "k")) != -1) {
7115 fprintf(stderr, "error: %s: option '-%c' "
7116 "unrecognized\n", argv[0], c);
7122 if ((rc = system("kdestroy > /dev/null")) != 0) {
7123 rc = WEXITSTATUS(rc);
7124 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7128 if (optind >= argc) {
7129 /* flush for all mounted lustre fs. */
7130 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7131 /* Check if we have a mount point */
7132 if (mntdir[0] == '\0')
7135 if (flushctx_ioctl(mntdir))
7138 mntdir[0] = '\0'; /* avoid matching in next loop */
7141 /* flush fs as specified */
7142 while (optind < argc) {
7143 if (flushctx_ioctl(argv[optind++]))
7150 static int lfs_changelog(int argc, char **argv)
7152 void *changelog_priv;
7153 struct changelog_rec *rec;
7154 long long startrec = 0, endrec = 0;
7156 struct option long_opts[] = {
7157 { .val = 'f', .name = "follow", .has_arg = no_argument },
7159 char short_opts[] = "f";
7162 while ((rc = getopt_long(argc, argv, short_opts,
7163 long_opts, NULL)) != -1) {
7170 "%s changelog: unrecognized option '%s'\n",
7171 progname, argv[optind - 1]);
7175 if (optind >= argc) {
7176 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7181 mdd = argv[optind++];
7183 startrec = strtoll(argv[optind++], NULL, 10);
7185 endrec = strtoll(argv[optind++], NULL, 10);
7187 rc = llapi_changelog_start(&changelog_priv,
7188 CHANGELOG_FLAG_BLOCK |
7189 CHANGELOG_FLAG_JOBID |
7190 CHANGELOG_FLAG_EXTRA_FLAGS |
7191 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7194 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7195 progname, strerror(errno = -rc));
7199 rc = llapi_changelog_set_xflags(changelog_priv,
7200 CHANGELOG_EXTRA_FLAG_UIDGID |
7201 CHANGELOG_EXTRA_FLAG_NID |
7202 CHANGELOG_EXTRA_FLAG_OMODE |
7203 CHANGELOG_EXTRA_FLAG_XATTR);
7206 "%s changelog: cannot set xflags for changelog: %s\n",
7207 progname, strerror(errno = -rc));
7211 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7215 if (endrec && rec->cr_index > endrec) {
7216 llapi_changelog_free(&rec);
7219 if (rec->cr_index < startrec) {
7220 llapi_changelog_free(&rec);
7224 secs = rec->cr_time >> 30;
7225 gmtime_r(&secs, &ts);
7226 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7227 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7228 changelog_type2str(rec->cr_type),
7229 ts.tm_hour, ts.tm_min, ts.tm_sec,
7230 (int)(rec->cr_time & ((1 << 30) - 1)),
7231 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7232 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7234 if (rec->cr_flags & CLF_JOBID) {
7235 struct changelog_ext_jobid *jid =
7236 changelog_rec_jobid(rec);
7238 if (jid->cr_jobid[0] != '\0')
7239 printf(" j=%s", jid->cr_jobid);
7242 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7243 struct changelog_ext_extra_flags *ef =
7244 changelog_rec_extra_flags(rec);
7246 printf(" ef=0x%llx", ef->cr_extra_flags);
7248 if (ef->cr_extra_flags & CLFE_UIDGID) {
7249 struct changelog_ext_uidgid *uidgid =
7250 changelog_rec_uidgid(rec);
7252 printf(" u=%llu:%llu",
7253 uidgid->cr_uid, uidgid->cr_gid);
7255 if (ef->cr_extra_flags & CLFE_NID) {
7256 struct changelog_ext_nid *nid =
7257 changelog_rec_nid(rec);
7260 libcfs_nid2str(nid->cr_nid));
7263 if (ef->cr_extra_flags & CLFE_OPEN) {
7264 struct changelog_ext_openmode *omd =
7265 changelog_rec_openmode(rec);
7266 char mode[] = "---";
7268 /* exec mode must be exclusive */
7269 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7272 if (omd->cr_openflags & MDS_FMODE_READ)
7274 if (omd->cr_openflags &
7281 if (strcmp(mode, "---") != 0)
7282 printf(" m=%s", mode);
7286 if (ef->cr_extra_flags & CLFE_XATTR) {
7287 struct changelog_ext_xattr *xattr =
7288 changelog_rec_xattr(rec);
7290 if (xattr->cr_xattr[0] != '\0')
7291 printf(" x=%s", xattr->cr_xattr);
7295 if (rec->cr_namelen)
7296 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7297 rec->cr_namelen, changelog_rec_name(rec));
7299 if (rec->cr_flags & CLF_RENAME) {
7300 struct changelog_ext_rename *rnm =
7301 changelog_rec_rename(rec);
7303 if (!fid_is_zero(&rnm->cr_sfid))
7304 printf(" s="DFID" sp="DFID" %.*s",
7305 PFID(&rnm->cr_sfid),
7306 PFID(&rnm->cr_spfid),
7307 (int)changelog_rec_snamelen(rec),
7308 changelog_rec_sname(rec));
7312 llapi_changelog_free(&rec);
7315 llapi_changelog_fini(&changelog_priv);
7318 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7319 progname, strerror(errno = -rc));
7321 return (rc == 1 ? 0 : rc);
7324 static int lfs_changelog_clear(int argc, char **argv)
7332 endrec = strtoll(argv[3], NULL, 10);
7334 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7337 fprintf(stderr, "%s: record out of range: %llu\n",
7339 else if (rc == -ENOENT)
7340 fprintf(stderr, "%s: no changelog user: %s\n",
7343 fprintf(stderr, "%s error: %s\n", argv[0],
7352 static int lfs_fid2path(int argc, char **argv)
7354 struct option long_opts[] = {
7355 { .val = 'c', .name = "cur", .has_arg = no_argument },
7356 { .val = 'l', .name = "link", .has_arg = required_argument },
7357 { .val = 'r', .name = "rec", .has_arg = required_argument },
7359 char short_opts[] = "cl:r:";
7360 char *device, *fid, *path;
7361 long long recno = -1;
7366 char *endptr = NULL;
7368 while ((rc = getopt_long(argc, argv, short_opts,
7369 long_opts, NULL)) != -1) {
7375 linkno = strtol(optarg, &endptr, 10);
7376 if (*endptr != '\0') {
7378 "%s fid2path: invalid linkno '%s'\n",
7384 recno = strtoll(optarg, &endptr, 10);
7385 if (*endptr != '\0') {
7387 "%s fid2path: invalid recno '%s'\n",
7394 "%s fid2path: unrecognized option '%s'\n",
7395 progname, argv[optind - 1]);
7402 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7407 device = argv[optind++];
7408 path = calloc(1, PATH_MAX);
7412 "%s fid2path: cannot allocate memory for path: %s\n",
7413 progname, strerror(-rc));
7418 while (optind < argc) {
7419 fid = argv[optind++];
7421 lnktmp = (linkno >= 0) ? linkno : 0;
7423 int oldtmp = lnktmp;
7424 long long rectmp = recno;
7426 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7430 "%s fid2path: cannot find '%s': %s\n",
7431 progname, fid, strerror(errno = -rc2));
7438 fprintf(stdout, "%lld ", rectmp);
7439 if (device[0] == '/') {
7440 fprintf(stdout, "%s", device);
7441 if (device[strlen(device) - 1] != '/')
7442 fprintf(stdout, "/");
7443 } else if (path[0] == '\0') {
7444 fprintf(stdout, "/");
7446 fprintf(stdout, "%s\n", path);
7449 /* specified linkno */
7451 if (oldtmp == lnktmp)
7461 static int lfs_path2fid(int argc, char **argv)
7463 struct option long_opts[] = {
7464 { .val = 'p', .name = "parents", .has_arg = no_argument },
7467 const char short_opts[] = "p";
7468 const char *sep = "";
7471 bool show_parents = false;
7473 while ((rc = getopt_long(argc, argv, short_opts,
7474 long_opts, NULL)) != -1) {
7477 show_parents = true;
7481 "%s path2fid: unrecognized option '%s'\n",
7482 progname, argv[optind - 1]);
7487 if (optind > argc - 1) {
7488 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7492 else if (optind < argc - 1)
7496 for (path = argv + optind; *path != NULL; path++) {
7498 if (!show_parents) {
7499 err = llapi_path2fid(*path, &fid);
7501 printf("%s%s"DFID"\n",
7502 *sep != '\0' ? *path : "", sep,
7505 char name[NAME_MAX + 1];
7506 unsigned int linkno = 0;
7508 while ((err = llapi_path2parent(*path, linkno, &fid,
7509 name, sizeof(name))) == 0) {
7510 if (*sep != '\0' && linkno == 0)
7511 printf("%s%s", *path, sep);
7513 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7518 /* err == -ENODATA is end-of-loop */
7519 if (linkno > 0 && err == -ENODATA) {
7527 "%s path2fid: cannot get %sfid for '%s': %s\n",
7528 progname, show_parents ? "parent " : "", *path,
7540 static int lfs_data_version(int argc, char **argv)
7547 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7550 fprintf(stderr, "%s data_version: FILE must be specified\n",
7555 while ((c = getopt(argc, argv, "nrw")) != -1) {
7558 data_version_flags = 0;
7561 data_version_flags |= LL_DV_RD_FLUSH;
7564 data_version_flags |= LL_DV_WR_FLUSH;
7568 "%s data_version: unrecognized option '%s'\n",
7569 progname, argv[optind - 1]);
7573 if (optind == argc) {
7574 fprintf(stderr, "%s data_version: FILE must be specified\n",
7579 path = argv[optind];
7580 fd = open(path, O_RDONLY);
7583 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7584 progname, path, strerror(-rc));
7588 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7591 "%s data_version: cannot get version for '%s': %s\n",
7592 progname, path, strerror(-rc));
7594 printf("%ju" "\n", (uintmax_t)data_version);
7600 static int lfs_hsm_state(int argc, char **argv)
7605 struct hsm_user_state hus;
7613 rc = llapi_hsm_state_get(path, &hus);
7615 fprintf(stderr, "can't get hsm state for %s: %s\n",
7616 path, strerror(errno = -rc));
7620 /* Display path name and status flags */
7621 printf("%s: (0x%08x)", path, hus.hus_states);
7623 if (hus.hus_states & HS_RELEASED)
7624 printf(" released");
7625 if (hus.hus_states & HS_EXISTS)
7627 if (hus.hus_states & HS_DIRTY)
7629 if (hus.hus_states & HS_ARCHIVED)
7630 printf(" archived");
7631 /* Display user-settable flags */
7632 if (hus.hus_states & HS_NORELEASE)
7633 printf(" never_release");
7634 if (hus.hus_states & HS_NOARCHIVE)
7635 printf(" never_archive");
7636 if (hus.hus_states & HS_LOST)
7637 printf(" lost_from_hsm");
7639 if (hus.hus_archive_id != 0)
7640 printf(", archive_id:%d", hus.hus_archive_id);
7643 } while (++i < argc);
7648 #define LFS_HSM_SET 0
7649 #define LFS_HSM_CLEAR 1
7652 * Generic function to set or clear HSM flags.
7653 * Used by hsm_set and hsm_clear.
7655 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7657 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7659 struct option long_opts[] = {
7660 { .val = 'A', .name = "archived", .has_arg = no_argument },
7661 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7662 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7663 { .val = 'e', .name = "exists", .has_arg = no_argument },
7664 { .val = 'l', .name = "lost", .has_arg = no_argument },
7665 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7666 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7668 char short_opts[] = "lraAdei:";
7672 __u32 archive_id = 0;
7678 while ((c = getopt_long(argc, argv, short_opts,
7679 long_opts, NULL)) != -1) {
7685 mask |= HS_NOARCHIVE;
7688 mask |= HS_ARCHIVED;
7691 mask |= HS_NORELEASE;
7700 archive_id = strtol(optarg, &end, 10);
7702 fprintf(stderr, "invalid archive_id: '%s'\n",
7710 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7711 argv[0], argv[optind - 1]);
7716 /* User should have specified a flag */
7720 while (optind < argc) {
7722 path = argv[optind];
7724 /* If mode == 0, this means we apply the mask. */
7725 if (mode == LFS_HSM_SET)
7726 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7728 rc = llapi_hsm_state_set(path, 0, mask, 0);
7731 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7732 path, strerror(errno = -rc));
7741 static int lfs_hsm_action(int argc, char **argv)
7746 struct hsm_current_action hca;
7747 struct hsm_extent he;
7748 enum hsm_user_action hua;
7749 enum hsm_progress_states hps;
7757 rc = llapi_hsm_current_action(path, &hca);
7759 fprintf(stderr, "can't get hsm action for %s: %s\n",
7760 path, strerror(errno = -rc));
7763 he = hca.hca_location;
7764 hua = hca.hca_action;
7765 hps = hca.hca_state;
7767 printf("%s: %s", path, hsm_user_action2name(hua));
7769 /* Skip file without action */
7770 if (hca.hca_action == HUA_NONE) {
7775 printf(" %s ", hsm_progress_state2name(hps));
7777 if ((hps == HPS_RUNNING) &&
7778 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7779 printf("(%llu bytes moved)\n",
7780 (unsigned long long)he.length);
7781 else if ((he.offset + he.length) == LUSTRE_EOF)
7782 printf("(from %llu to EOF)\n",
7783 (unsigned long long)he.offset);
7785 printf("(from %llu to %llu)\n",
7786 (unsigned long long)he.offset,
7787 (unsigned long long)(he.offset + he.length));
7789 } while (++i < argc);
7794 static int lfs_hsm_set(int argc, char **argv)
7796 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7799 static int lfs_hsm_clear(int argc, char **argv)
7801 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7805 * Check file state and return its fid, to be used by lfs_hsm_request().
7807 * \param[in] file Path to file to check
7808 * \param[in,out] fid Pointer to allocated lu_fid struct.
7809 * \param[in,out] last_dev Pointer to last device id used.
7811 * \return 0 on success.
7813 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7819 rc = lstat(file, &st);
7821 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7824 /* Checking for regular file as archiving as posix copytool
7825 * rejects archiving files other than regular files
7827 if (!S_ISREG(st.st_mode)) {
7828 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7831 /* A request should be ... */
7832 if (*last_dev != st.st_dev && *last_dev != 0) {
7833 fprintf(stderr, "All files should be "
7834 "on the same filesystem: %s\n", file);
7837 *last_dev = st.st_dev;
7839 rc = llapi_path2fid(file, fid);
7841 fprintf(stderr, "Cannot read FID of %s: %s\n",
7842 file, strerror(-rc));
7848 /* Fill an HSM HUR item with a given file name.
7850 * If mntpath is set, then the filename is actually a FID, and no
7851 * lookup on the filesystem will be performed.
7853 * \param[in] hur the user request to fill
7854 * \param[in] idx index of the item inside the HUR to fill
7855 * \param[in] mntpath mountpoint of Lustre
7856 * \param[in] fname filename (if mtnpath is NULL)
7857 * or FID (if mntpath is set)
7858 * \param[in] last_dev pointer to last device id used
7860 * \retval 0 on success
7861 * \retval CMD_HELP or a negative errno on error
7863 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7864 const char *mntpath, const char *fname,
7867 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7870 hui->hui_extent.length = -1;
7872 if (mntpath != NULL) {
7875 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7879 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7884 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7888 hur->hur_request.hr_itemcount++;
7893 static int lfs_hsm_request(int argc, char **argv, int action)
7895 struct option long_opts[] = {
7896 { .val = 'a', .name = "archive", .has_arg = required_argument },
7897 { .val = 'D', .name = "data", .has_arg = required_argument },
7898 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7899 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7902 char short_opts[] = "l:D:a:m:";
7903 struct hsm_user_request *hur, *oldhur;
7908 char *filelist = NULL;
7909 char fullpath[PATH_MAX];
7910 char *opaque = NULL;
7914 int nbfile_alloc = 0;
7915 char *some_file = NULL;
7916 char *mntpath = NULL;
7922 while ((c = getopt_long(argc, argv, short_opts,
7923 long_opts, NULL)) != -1) {
7932 if (action != HUA_ARCHIVE &&
7933 action != HUA_REMOVE) {
7935 "error: -a is supported only "
7936 "when archiving or removing\n");
7939 archive_id = atoi(optarg);
7942 if (some_file == NULL) {
7944 some_file = strdup(optarg);
7950 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7951 argv[0], argv[optind - 1]);
7956 /* All remaining args are files, so we have at least nbfile */
7957 nbfile = argc - optind;
7959 if ((nbfile == 0) && (filelist == NULL))
7963 opaque_len = strlen(opaque);
7965 /* Alloc the request structure with enough place to store all files
7966 * from command line. */
7967 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7969 fprintf(stderr, "Cannot create the request: %s\n",
7973 nbfile_alloc = nbfile;
7975 hur->hur_request.hr_action = action;
7976 hur->hur_request.hr_archive_id = archive_id;
7977 hur->hur_request.hr_flags = 0;
7979 /* All remaining args are files, add them */
7980 if (nbfile != 0 && some_file == NULL)
7981 some_file = strdup(argv[optind]);
7983 for (i = 0; i < nbfile; i++) {
7984 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7990 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7992 /* If a filelist was specified, read the filelist from it. */
7993 if (filelist != NULL) {
7994 fp = fopen(filelist, "r");
7996 fprintf(stderr, "Cannot read the file list %s: %s\n",
7997 filelist, strerror(errno));
8002 while ((rc = getline(&line, &len, fp)) != -1) {
8003 /* If allocated buffer was too small, get something
8005 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8008 nbfile_alloc = nbfile_alloc * 2 + 1;
8010 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8013 fprintf(stderr, "hsm: cannot allocate "
8014 "the request: %s\n",
8021 size = hur_len(oldhur);
8023 fprintf(stderr, "hsm: cannot allocate "
8024 "%u files + %u bytes data\n",
8025 oldhur->hur_request.hr_itemcount,
8026 oldhur->hur_request.hr_data_len);
8033 memcpy(hur, oldhur, size);
8038 if (line[strlen(line) - 1] == '\n')
8039 line[strlen(line) - 1] = '\0';
8041 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8042 mntpath, line, &last_dev);
8048 if (some_file == NULL) {
8058 /* If a --data was used, add it to the request */
8059 hur->hur_request.hr_data_len = opaque_len;
8061 memcpy(hur_data(hur), opaque, opaque_len);
8063 /* Send the HSM request */
8064 if (realpath(some_file, fullpath) == NULL) {
8065 fprintf(stderr, "Could not find path '%s': %s\n",
8066 some_file, strerror(errno));
8068 rc = llapi_hsm_request(fullpath, hur);
8070 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8071 some_file, strerror(-rc));
8081 static int lfs_hsm_archive(int argc, char **argv)
8083 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8086 static int lfs_hsm_restore(int argc, char **argv)
8088 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8091 static int lfs_hsm_release(int argc, char **argv)
8093 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8096 static int lfs_hsm_remove(int argc, char **argv)
8098 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8101 static int lfs_hsm_cancel(int argc, char **argv)
8103 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8106 static int lfs_swap_layouts(int argc, char **argv)
8111 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8112 SWAP_LAYOUTS_KEEP_MTIME |
8113 SWAP_LAYOUTS_KEEP_ATIME);
8116 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8118 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8120 int lfs_get_mode(const char *string)
8122 enum lock_mode_user mode;
8124 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8125 if (lock_mode_names[mode] == NULL)
8127 if (strcmp(string, lock_mode_names[mode]) == 0)
8134 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8136 enum lu_ladvise_type advice;
8139 advice < ARRAY_SIZE(ladvise_names); advice++) {
8140 if (ladvise_names[advice] == NULL)
8142 if (strcmp(string, ladvise_names[advice]) == 0)
8146 return LU_LADVISE_INVALID;
8149 static int lfs_ladvise(int argc, char **argv)
8151 struct option long_opts[] = {
8152 { .val = 'a', .name = "advice", .has_arg = required_argument },
8153 { .val = 'b', .name = "background", .has_arg = no_argument },
8154 { .val = 'e', .name = "end", .has_arg = required_argument },
8155 { .val = 'l', .name = "length", .has_arg = required_argument },
8156 { .val = 'm', .name = "mode", .has_arg = required_argument },
8157 { .val = 's', .name = "start", .has_arg = required_argument },
8158 { .val = 'u', .name = "unset", .has_arg = no_argument },
8160 char short_opts[] = "a:be:l:m:s:u";
8165 struct llapi_lu_ladvise advice;
8166 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8167 unsigned long long start = 0;
8168 unsigned long long end = LUSTRE_EOF;
8169 unsigned long long length = 0;
8170 unsigned long long size_units;
8171 unsigned long long flags = 0;
8175 while ((c = getopt_long(argc, argv, short_opts,
8176 long_opts, NULL)) != -1) {
8179 advice_type = lfs_get_ladvice(optarg);
8180 if (advice_type == LU_LADVISE_INVALID) {
8181 fprintf(stderr, "%s: invalid advice type "
8182 "'%s'\n", argv[0], optarg);
8183 fprintf(stderr, "Valid types:");
8185 for (advice_type = 0;
8186 advice_type < ARRAY_SIZE(ladvise_names);
8188 if (ladvise_names[advice_type] == NULL)
8190 fprintf(stderr, " %s",
8191 ladvise_names[advice_type]);
8193 fprintf(stderr, "\n");
8206 rc = llapi_parse_size(optarg, &end,
8209 fprintf(stderr, "%s: bad end offset '%s'\n",
8216 rc = llapi_parse_size(optarg, &start,
8219 fprintf(stderr, "%s: bad start offset "
8220 "'%s'\n", argv[0], optarg);
8226 rc = llapi_parse_size(optarg, &length,
8229 fprintf(stderr, "%s: bad length '%s'\n",
8235 mode = lfs_get_mode(optarg);
8237 fprintf(stderr, "%s: bad mode '%s', valid "
8238 "modes are READ or WRITE\n",
8246 fprintf(stderr, "%s: option '%s' unrecognized\n",
8247 argv[0], argv[optind - 1]);
8252 if (advice_type == LU_LADVISE_INVALID) {
8253 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8254 fprintf(stderr, "Valid types:");
8255 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8257 if (ladvise_names[advice_type] == NULL)
8259 fprintf(stderr, " %s", ladvise_names[advice_type]);
8261 fprintf(stderr, "\n");
8265 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8266 fprintf(stderr, "%s: Lock no expand advice is a per file "
8267 "descriptor advice, so when called from lfs, "
8268 "it does nothing.\n", argv[0]);
8272 if (argc <= optind) {
8273 fprintf(stderr, "%s: please give one or more file names\n",
8278 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8279 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8284 if (end == LUSTRE_EOF && length != 0)
8285 end = start + length;
8288 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8289 argv[0], start, end);
8293 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8294 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8299 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8300 fprintf(stderr, "%s: mode is required with lockahead\n",
8305 while (optind < argc) {
8308 path = argv[optind++];
8310 fd = open(path, O_RDONLY);
8312 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8313 argv[0], path, strerror(errno));
8318 advice.lla_start = start;
8319 advice.lla_end = end;
8320 advice.lla_advice = advice_type;
8321 advice.lla_value1 = 0;
8322 advice.lla_value2 = 0;
8323 advice.lla_value3 = 0;
8324 advice.lla_value4 = 0;
8325 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8326 advice.lla_lockahead_mode = mode;
8327 advice.lla_peradvice_flags = flags;
8330 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8333 fprintf(stderr, "%s: cannot give advice '%s' to file "
8334 "'%s': %s\n", argv[0],
8335 ladvise_names[advice_type],
8336 path, strerror(errno));
8342 if (rc == 0 && rc2 < 0)
8348 static const char *const heat_names[] = LU_HEAT_NAMES;
8350 static int lfs_heat_get(int argc, char **argv)
8352 struct lu_heat *heat;
8361 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8363 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8368 while (optind < argc) {
8369 path = argv[optind++];
8371 fd = open(path, O_RDONLY);
8373 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8374 argv[0], path, strerror(errno));
8379 heat->lh_count = OBD_HEAT_COUNT;
8380 rc2 = llapi_heat_get(fd, heat);
8383 fprintf(stderr, "%s: cannot get heat of file '%s'"
8384 ": %s\n", argv[0], path, strerror(errno));
8388 printf("flags: %x\n", heat->lh_flags);
8389 for (i = 0; i < heat->lh_count; i++)
8390 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8392 if (rc == 0 && rc2 < 0)
8400 static int lfs_heat_set(int argc, char **argv)
8402 struct option long_opts[] = {
8403 { .val = 'c', .name = "clear", .has_arg = no_argument },
8404 { .val = 'o', .name = "off", .has_arg = no_argument },
8405 { .val = 'O', .name = "on", .has_arg = no_argument },
8407 enum lu_heat_flag flags = 0;
8417 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8420 flags |= LU_HEAT_FLAG_CLEAR;
8423 flags |= LU_HEAT_FLAG_CLEAR;
8424 flags |= LU_HEAT_FLAG_OFF;
8427 flags &= ~LU_HEAT_FLAG_OFF;
8432 fprintf(stderr, "%s: option '%s' unrecognized\n",
8433 argv[0], argv[optind - 1]);
8438 if (argc <= optind) {
8439 fprintf(stderr, "%s: please give one or more file names\n",
8444 while (optind < argc) {
8445 path = argv[optind++];
8447 fd = open(path, O_RDONLY);
8449 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8450 argv[0], path, strerror(errno));
8455 rc2 = llapi_heat_set(fd, flags);
8458 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8459 ": %s\n", argv[0], path, strerror(errno));
8463 if (rc == 0 && rc2 < 0)
8469 /** The input string contains a comma delimited list of component ids and
8470 * ranges, for example "1,2-4,7".
8472 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8474 bool end_of_loop = false;
8482 while (!end_of_loop) {
8486 char *endptr = NULL;
8489 ptr = strchrnul(arg, ',');
8490 end_of_loop = *ptr == '\0';
8493 start_index = strtol(arg, &endptr, 0);
8494 if (endptr == arg) /* no data at all */
8496 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8498 if (start_index < 0)
8501 end_index = start_index;
8502 if (*endptr == '-') {
8503 end_index = strtol(endptr + 1, &endptr, 0);
8504 if (*endptr != '\0')
8506 if (end_index < start_index)
8510 for (i = start_index; i <= end_index && size > 0; i++) {
8513 /* remove duplicate */
8514 for (j = 0; j < nr; j++) {
8518 if (j == nr) { /* no duplicate */
8524 if (size == 0 && i < end_index)
8531 if (!end_of_loop && ptr != NULL)
8534 return rc < 0 ? rc : nr;
8538 * struct verify_mirror_id - Mirror id to be verified.
8539 * @mirror_id: A specified mirror id.
8540 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8542 struct verify_mirror_id {
8548 * compare_mirror_ids() - Compare mirror ids.
8549 * @layout: Mirror component list.
8550 * @cbdata: Callback data in verify_mirror_id structure.
8552 * This is a callback function called by llapi_layout_comp_iterate()
8553 * to compare the specified mirror id with the one in the current
8554 * component of @layout. If they are the same, then the specified
8555 * mirror id is valid.
8557 * Return: a negative error code on failure or
8558 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8559 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8562 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8564 struct verify_mirror_id *mirror_id_cbdata =
8565 (struct verify_mirror_id *)cbdata;
8569 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8573 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8574 progname, strerror(errno));
8578 if (mirror_id_cbdata->mirror_id == mirror_id) {
8579 mirror_id_cbdata->is_valid_id = true;
8580 return LLAPI_LAYOUT_ITER_STOP;
8583 return LLAPI_LAYOUT_ITER_CONT;
8587 * verify_mirror_ids() - Verify specified mirror ids.
8588 * @fname: Mirrored file name.
8589 * @mirror_ids: Specified mirror ids to be verified.
8590 * @ids_nr: Number of specified mirror ids.
8592 * This function verifies that specified @mirror_ids are valid
8593 * in the mirrored file @fname.
8595 * Return: 0 on success or a negative error code on failure.
8598 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8600 struct llapi_layout *layout = NULL;
8601 struct verify_mirror_id mirror_id_cbdata = { 0 };
8612 if (stat(fname, &stbuf) < 0) {
8613 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8614 progname, fname, strerror(errno));
8619 if (!S_ISREG(stbuf.st_mode)) {
8620 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8626 fd = open(fname, O_DIRECT | O_RDONLY);
8628 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8629 progname, fname, strerror(errno));
8634 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8636 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8637 progname, fname, strerror(errno));
8641 layout = llapi_layout_get_by_fd(fd, 0);
8642 if (layout == NULL) {
8643 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8644 progname, fname, strerror(errno));
8646 llapi_lease_release(fd);
8650 rc = llapi_layout_flags_get(layout, &flr_state);
8652 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8653 progname, fname, strerror(errno));
8658 flr_state &= LCM_FL_FLR_MASK;
8659 switch (flr_state) {
8662 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8663 progname, fname, llapi_layout_flags_string(flr_state));
8670 for (i = 0; i < ids_nr; i++) {
8671 mirror_id_cbdata.mirror_id = mirror_ids[i];
8672 mirror_id_cbdata.is_valid_id = false;
8674 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8679 "%s: '%s' failed to verify mirror id: %u.\n",
8680 progname, fname, mirror_ids[i]);
8684 if (!mirror_id_cbdata.is_valid_id) {
8687 "%s: '%s' invalid specified mirror id: %u.\n",
8688 progname, fname, mirror_ids[i]);
8694 llapi_layout_free(layout);
8695 llapi_lease_release(fd);
8703 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8704 __u16 *mirror_ids, int ids_nr)
8706 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8707 struct llapi_layout *layout;
8717 if (stat(fname, &stbuf) < 0) {
8718 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8719 progname, fname, strerror(errno));
8723 if (!S_ISREG(stbuf.st_mode)) {
8724 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8730 fd = open(fname, O_DIRECT | O_RDWR);
8732 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8733 progname, fname, strerror(errno));
8738 layout = llapi_layout_get_by_fd(fd, 0);
8739 if (layout == NULL) {
8740 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8741 progname, fname, strerror(errno));
8746 rc = llapi_layout_flags_get(layout, &flr_state);
8748 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8749 progname, fname, strerror(errno));
8754 flr_state &= LCM_FL_FLR_MASK;
8755 if (flr_state == LCM_FL_NONE) {
8757 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8762 /* get stale component info */
8763 comp_size = llapi_mirror_find_stale(layout, comp_array,
8764 ARRAY_SIZE(comp_array),
8765 mirror_ids, ids_nr);
8766 if (comp_size <= 0) {
8771 ioc->lil_mode = LL_LEASE_WRLCK;
8772 ioc->lil_flags = LL_LEASE_RESYNC;
8773 rc = llapi_lease_set(fd, ioc);
8775 if (rc == -EALREADY)
8779 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8780 progname, fname, strerror(errno));
8784 /* get the read range [start, end) */
8785 start = comp_array[0].lrc_start;
8786 end = comp_array[0].lrc_end;
8787 for (idx = 1; idx < comp_size; idx++) {
8788 if (comp_array[idx].lrc_start < start)
8789 start = comp_array[idx].lrc_start;
8790 if (end < comp_array[idx].lrc_end)
8791 end = comp_array[idx].lrc_end;
8794 rc = llapi_lease_check(fd);
8795 if (rc != LL_LEASE_WRLCK) {
8796 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8801 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8804 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8805 progname, fname, rc);
8807 /* prepare ioc for lease put */
8808 ioc->lil_mode = LL_LEASE_UNLCK;
8809 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8811 for (idx = 0; idx < comp_size; idx++) {
8812 if (comp_array[idx].lrc_synced) {
8813 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8818 rc = llapi_lease_set(fd, ioc);
8820 if (rc == 0) /* lost lease lock */
8822 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8823 progname, fname, strerror(errno));
8827 * llapi_lease_set returns lease mode when it request to unlock
8833 llapi_layout_free(layout);
8840 static inline int lfs_mirror_resync(int argc, char **argv)
8842 struct ll_ioc_lease *ioc = NULL;
8843 __u16 mirror_ids[128] = { 0 };
8848 struct option long_opts[] = {
8849 { .val = 'o', .name = "only", .has_arg = required_argument },
8852 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8855 rc = parse_mirror_ids(mirror_ids,
8856 sizeof(mirror_ids) / sizeof(__u16),
8860 "%s: bad mirror ids '%s'.\n",
8867 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8868 argv[0], argv[optind - 1]);
8874 if (argc == optind) {
8875 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8880 if (ids_nr > 0 && argc > optind + 1) {
8882 "%s: option '--only' cannot be used upon multiple files.\n",
8890 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8895 /* set the lease on the file */
8896 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8898 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8899 argv[0], strerror(errno));
8904 for (; optind < argc; optind++) {
8905 rc = lfs_mirror_resync_file(argv[optind], ioc,
8906 mirror_ids, ids_nr);
8907 /* ignore previous file's error, continue with next file */
8910 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8918 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8920 struct llapi_layout *layout;
8923 layout = llapi_layout_get_by_fd(fd, 0);
8924 if (layout == NULL) {
8925 fprintf(stderr, "could not get layout.\n");
8929 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8931 fprintf(stderr, "failed to iterate layout\n");
8932 llapi_layout_free(layout);
8935 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8936 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8937 llapi_layout_free(layout);
8941 llapi_layout_free(layout);
8947 * Check whether two files are the same file
8948 * \retval 0 same file
8949 * \retval 1 not the same file
8950 * \retval <0 error code
8952 static inline int check_same_file(const char *f1, const char *f2)
8957 if (stat(f1, &stbuf1) < 0) {
8958 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8959 progname, f1, strerror(errno));
8963 if (stat(f2, &stbuf2) < 0) {
8964 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8965 progname, f2, strerror(errno));
8969 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8970 stbuf1.st_ino == stbuf2.st_ino)
8976 static inline int lfs_mirror_read(int argc, char **argv)
8979 __u16 mirror_id = 0;
8980 const char *outfile = NULL;
8986 const size_t buflen = 4 << 20;
8988 struct option long_opts[] = {
8989 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8990 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8993 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8998 mirror_id = strtoul(optarg, &end, 0);
8999 if (*end != '\0' || mirror_id == 0) {
9001 "%s %s: invalid mirror ID '%s'\n",
9002 progname, argv[0], optarg);
9010 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9011 progname, argv[optind - 1]);
9016 if (argc == optind) {
9017 fprintf(stderr, "%s %s: no mirrored file provided\n",
9020 } else if (argc > optind + 1) {
9021 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9025 if (mirror_id == 0) {
9026 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9031 /* open mirror file */
9032 fname = argv[optind];
9035 rc = check_same_file(fname, outfile);
9038 "%s %s: output file cannot be the mirrored file\n",
9046 fd = open(fname, O_DIRECT | O_RDONLY);
9048 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9049 progname, argv[0], fname, strerror(errno));
9053 /* verify mirror id */
9054 rc = verify_mirror_id_by_fd(fd, mirror_id);
9057 "%s %s: cannot find mirror with ID %u in '%s'\n",
9058 progname, argv[0], mirror_id, fname);
9062 /* open output file */
9064 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9066 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9067 progname, argv[0], outfile, strerror(errno));
9072 outfd = STDOUT_FILENO;
9075 /* allocate buffer */
9076 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9078 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9079 progname, argv[0], rc);
9086 ssize_t written = 0;
9088 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9089 if (bytes_read < 0) {
9092 "%s %s: fail to read data from mirror %u: %s\n",
9093 progname, argv[0], mirror_id, strerror(-rc));
9098 if (bytes_read == 0)
9101 while (written < bytes_read) {
9104 written2 = write(outfd, buf + written,
9105 bytes_read - written);
9108 "%s %s: fail to write %s: %s\n",
9109 progname, argv[0], outfile ? : "STDOUT",
9114 written += written2;
9117 if (written != bytes_read) {
9119 "%s %s: written %ld bytes does not match with %ld read.\n",
9120 progname, argv[0], written, bytes_read);
9142 static inline int lfs_mirror_write(int argc, char **argv)
9145 __u16 mirror_id = 0;
9146 const char *inputfile = NULL;
9152 const size_t buflen = 4 << 20;
9154 size_t page_size = sysconf(_SC_PAGESIZE);
9155 struct ll_ioc_lease_id ioc;
9157 struct option long_opts[] = {
9158 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9159 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9162 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9167 mirror_id = strtoul(optarg, &end, 0);
9168 if (*end != '\0' || mirror_id == 0) {
9170 "%s %s: invalid mirror ID '%s'\n",
9171 progname, argv[0], optarg);
9179 fprintf(stderr, "%s: option '%s' unrecognized\n",
9180 progname, argv[optind - 1]);
9185 if (argc == optind) {
9186 fprintf(stderr, "%s %s: no mirrored file provided\n",
9189 } else if (argc > optind + 1) {
9190 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9194 if (mirror_id == 0) {
9195 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9200 /* open mirror file */
9201 fname = argv[optind];
9204 rc = check_same_file(fname, inputfile);
9207 "%s %s: input file cannot be the mirrored file\n",
9215 fd = open(fname, O_DIRECT | O_WRONLY);
9217 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9218 progname, argv[0], fname, strerror(errno));
9222 /* verify mirror id */
9223 rc = verify_mirror_id_by_fd(fd, mirror_id);
9226 "%s %s: cannot find mirror with ID %u in '%s'\n",
9227 progname, argv[0], mirror_id, fname);
9231 /* open input file */
9233 inputfd = open(inputfile, O_RDONLY, 0644);
9235 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9236 progname, argv[0], inputfile, strerror(errno));
9241 inputfd = STDIN_FILENO;
9244 /* allocate buffer */
9245 rc = posix_memalign(&buf, page_size, buflen);
9247 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9248 progname, argv[0], rc);
9252 /* prepare target mirror components instantiation */
9253 ioc.lil_mode = LL_LEASE_WRLCK;
9254 ioc.lil_flags = LL_LEASE_RESYNC;
9255 ioc.lil_mirror_id = mirror_id;
9256 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9259 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9260 progname, argv[0], fname, strerror(errno));
9270 rc = llapi_lease_check(fd);
9271 if (rc != LL_LEASE_WRLCK) {
9272 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9273 progname, argv[0], fname);
9277 bytes_read = read(inputfd, buf, buflen);
9278 if (bytes_read < 0) {
9281 "%s %s: fail to read data from '%s': %s\n",
9282 progname, argv[0], inputfile ? : "STDIN",
9289 if (bytes_read == 0)
9292 /* round up to page align to make direct IO happy. */
9293 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9295 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9300 "%s %s: fail to write to mirror %u: %s\n",
9301 progname, argv[0], mirror_id,
9309 if (pos & (page_size - 1)) {
9310 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9315 ioc.lil_mode = LL_LEASE_UNLCK;
9316 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9318 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9323 "%s %s: release lease lock of '%s' failed: %s\n",
9324 progname, argv[0], fname, strerror(errno));
9341 struct collect_ids_data {
9347 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9349 struct collect_ids_data *cid = cbdata;
9353 rc = llapi_layout_mirror_id_get(layout, &id);
9357 if ((__u16)id != cid->cid_exclude) {
9360 for (i = 0; i < cid->cid_count; i++) {
9361 /* already collected the mirror id */
9362 if (id == cid->cid_ids[i])
9363 return LLAPI_LAYOUT_ITER_CONT;
9365 cid->cid_ids[cid->cid_count] = id;
9369 return LLAPI_LAYOUT_ITER_CONT;
9372 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9374 struct llapi_layout *layout;
9375 struct collect_ids_data cid = { .cid_ids = ids,
9377 .cid_exclude = exclude_id, };
9380 layout = llapi_layout_get_by_fd(fd, 0);
9381 if (layout == NULL) {
9382 fprintf(stderr, "could not get layout\n");
9386 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9388 fprintf(stderr, "failed to iterate layout\n");
9389 llapi_layout_free(layout);
9393 llapi_layout_free(layout);
9395 return cid.cid_count;
9398 static inline int lfs_mirror_copy(int argc, char **argv)
9401 __u16 read_mirror_id = 0;
9402 __u16 ids[128] = { 0 };
9404 struct llapi_layout *layout = NULL;
9405 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9412 struct ll_ioc_lease *ioc = NULL;
9413 struct ll_ioc_lease_id *resync_ioc;
9415 struct option long_opts[] = {
9416 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9417 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9420 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9425 read_mirror_id = strtoul(optarg, &end, 0);
9426 if (*end != '\0' || read_mirror_id == 0) {
9428 "%s %s: invalid read mirror ID '%s'\n",
9429 progname, argv[0], optarg);
9434 if (!strcmp(optarg, "-1")) {
9435 /* specify all other mirrors */
9439 count = parse_mirror_ids((__u16 *)ids,
9447 fprintf(stderr, "%s: option '%s' unrecognized\n",
9448 progname, argv[optind - 1]);
9453 if (argc == optind) {
9454 fprintf(stderr, "%s %s: no mirrored file provided\n",
9457 } else if (argc > optind + 1) {
9458 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9462 if (read_mirror_id == 0) {
9464 "%s %s: no valid read mirror ID %d is provided\n",
9465 progname, argv[0], read_mirror_id);
9471 "%s %s: no write mirror ID is provided\n",
9476 for (i = 0; i < count; i++) {
9477 if (read_mirror_id == ids[i]) {
9479 "%s %s: read and write mirror ID cannot be the same\n",
9485 /* open mirror file */
9486 fname = argv[optind];
9488 fd = open(fname, O_DIRECT | O_RDWR);
9490 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9491 progname, argv[0], fname, strerror(errno));
9495 /* write to all other mirrors */
9496 if (ids[0] == (__u16)-1) {
9497 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9501 "%s %s: failed to get other mirror ids in '%s': %d\n",
9502 progname, argv[0], fname, rc);
9507 /* verify mirror id */
9508 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9511 "%s %s: cannot find mirror with ID %u in '%s'\n",
9512 progname, argv[0], read_mirror_id, fname);
9516 for (i = 0; i < count; i++) {
9517 rc = verify_mirror_id_by_fd(fd, ids[i]);
9520 "%s %s: cannot find mirror with ID %u in '%s'\n",
9521 progname, argv[0], ids[i], fname);
9526 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9529 "%s %s: cannot alloc comp id array for ioc: %s\n",
9530 progname, argv[0], strerror(errno));
9535 /* get stale component info */
9536 layout = llapi_layout_get_by_fd(fd, 0);
9537 if (layout == NULL) {
9538 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9539 progname, argv[0], fname, strerror(errno));
9543 comp_size = llapi_mirror_find_stale(layout, comp_array,
9544 ARRAY_SIZE(comp_array),
9546 llapi_layout_free(layout);
9547 if (comp_size < 0) {
9552 /* prepare target mirror components instantiation */
9553 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9554 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9555 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9557 resync_ioc->lil_mirror_id = ids[0];
9559 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9560 rc = llapi_lease_set(fd, ioc);
9563 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9564 progname, argv[0], fname, strerror(errno));
9568 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9571 fprintf(stderr, "%s %s: copy error: %d\n",
9572 progname, argv[0], rc);
9576 fprintf(stdout, "mirror copied successfully: ");
9577 for (i = 0; i < copied; i++)
9578 fprintf(stdout, "%d ", ids[i]);
9579 fprintf(stdout, "\n");
9581 ioc->lil_mode = LL_LEASE_UNLCK;
9582 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9584 for (i = 0; i < comp_size; i++) {
9587 for (j = 0; j < copied; j++) {
9588 if (comp_array[i].lrc_mirror_id != ids[j])
9591 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9595 rc = llapi_lease_set(fd, ioc);
9600 "%s %s: release lease lock of '%s' failed: %s\n",
9601 progname, argv[0], fname, strerror(errno));
9615 * struct verify_chunk - Mirror chunk to be verified.
9616 * @chunk: [start, end) of the chunk.
9617 * @mirror_count: Number of mirror ids in @mirror_id array.
9618 * @mirror_id: Array of valid mirror ids that cover the chunk.
9620 struct verify_chunk {
9621 struct lu_extent chunk;
9622 unsigned int mirror_count;
9623 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9627 * print_chunks() - Print chunk information.
9628 * @fname: Mirrored file name.
9629 * @chunks: Array of chunks.
9630 * @chunk_count: Number of chunks in @chunks array.
9632 * This function prints [start, end) of each chunk in @chunks
9633 * for mirrored file @fname, and also prints the valid mirror ids
9634 * that cover the chunk.
9639 void print_chunks(const char *fname, struct verify_chunk *chunks,
9645 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9646 for (i = 0; i < chunk_count; i++) {
9647 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9649 if (chunks[i].mirror_count == 0)
9650 fprintf(stdout, "\t[");
9652 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9653 for (j = 1; j < chunks[i].mirror_count; j++)
9654 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9656 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9658 fprintf(stdout, "\n");
9662 * print_checksums() - Print CRC-32 checksum values.
9663 * @chunk: A chunk and its corresponding valid mirror ids.
9664 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9666 * This function prints CRC-32 checksum values on @chunk for
9667 * each valid mirror that covers it.
9672 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9677 "CRC-32 checksum value for chunk "DEXT":\n",
9678 PEXT(&chunk->chunk));
9679 for (i = 0; i < chunk->mirror_count; i++)
9680 fprintf(stdout, "Mirror %u:\t%#lx\n",
9681 chunk->mirror_id[i], crc[i]);
9682 fprintf(stdout, "\n");
9686 * filter_mirror_id() - Filter specified mirror ids.
9687 * @chunks: Array of chunks.
9688 * @chunk_count: Number of chunks in @chunks array.
9689 * @mirror_ids: Specified mirror ids to be verified.
9690 * @ids_nr: Number of specified mirror ids.
9692 * This function scans valid mirror ids that cover each chunk in @chunks
9693 * and filters specified mirror ids.
9698 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9699 __u16 *mirror_ids, int ids_nr)
9704 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9705 unsigned int valid_count = 0;
9707 for (i = 0; i < chunk_count; i++) {
9708 if (chunks[i].mirror_count == 0)
9712 for (j = 0; j < ids_nr; j++) {
9713 for (k = 0; k < chunks[i].mirror_count; k++) {
9714 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9715 valid_id[valid_count] = mirror_ids[j];
9722 memcpy(chunks[i].mirror_id, valid_id,
9723 sizeof(__u16) * valid_count);
9724 chunks[i].mirror_count = valid_count;
9729 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9730 * @layout: Mirror component list.
9731 * @chunks: Array of chunks.
9732 * @chunks_size: Array size of @chunks.
9734 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9735 * to find out chunk segments and store them in @chunks array.
9737 * The @mirror_id array in each element of @chunks will store the valid
9738 * mirror ids that cover the chunk. If a mirror component covering the
9739 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9740 * will not be stored into the @mirror_id array, and the chunk for that
9741 * mirror will not be verified.
9743 * The @mirror_count in each element of @chunks will store the number of
9744 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9745 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9746 * indicates the chunk is valid in only one mirror. In both cases, the
9747 * chunk will not be verified.
9749 * Here is an example:
9752 * +------+-------------+--------------------+
9754 * +------+------+------+------+-------------+
9755 * | | S | S | | mirror2
9756 * +-------------+------+------+-------------+
9758 * prepared @chunks array will contain 5 elements:
9759 * (([0, 1M), [1, 2], 2),
9760 * ([1M, 2M), [1, 2], 2),
9761 * ([2M, 3M), [1], 1),
9762 * ([3M, 4M], [], 0),
9763 * ([4M, EOF), [2], 1))
9765 * Return: the actual array size of @chunks on success
9766 * or a negative error code on failure.
9769 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9770 struct verify_chunk *chunks,
9781 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9784 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9787 "%s: move to the first layout component: %s.\n",
9788 progname, strerror(errno));
9794 chunks[idx].chunk.e_end = LUSTRE_EOF;
9796 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9799 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9800 progname, strerror(errno));
9804 if (start > chunks[idx].chunk.e_start ||
9805 end <= chunks[idx].chunk.e_start)
9808 if (end < chunks[idx].chunk.e_end)
9809 chunks[idx].chunk.e_end = end;
9811 rc = llapi_layout_comp_flags_get(layout, &flags);
9814 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9815 progname, strerror(errno));
9819 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9822 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9825 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9826 progname, strerror(errno));
9830 chunks[idx].mirror_id[i] = mirror_id;
9832 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9834 "%s: mirror_id array is too small.\n",
9841 rc = llapi_layout_comp_use(layout,
9842 LLAPI_LAYOUT_COMP_USE_NEXT);
9845 "%s: move to the next layout component: %s.\n",
9846 progname, strerror(errno));
9849 } /* loop through all components */
9851 chunks[idx].mirror_count = i;
9853 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9857 if (idx >= chunks_size) {
9858 fprintf(stderr, "%s: chunks array is too small.\n",
9864 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9868 return rc < 0 ? rc : idx + 1;
9872 * lfs_mirror_verify_chunk() - Verify a chunk.
9873 * @fd: File descriptor of the mirrored file.
9874 * @file_size: Size of the mirrored file.
9875 * @chunk: A chunk and its corresponding valid mirror ids.
9876 * @verbose: Verbose mode.
9878 * This function verifies a @chunk contains exactly the same data
9879 * ammong the mirrors that cover it.
9881 * If @verbose is specified, then the function will print where the
9882 * differences are if the data do not match. Otherwise, it will
9883 * just return an error in that case.
9885 * Return: 0 on success or a negative error code on failure.
9888 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9889 struct verify_chunk *chunk, int verbose)
9891 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9893 size_t page_size = sysconf(_SC_PAGESIZE);
9899 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9906 rc = posix_memalign(&buf, page_size, buflen);
9907 if (rc) /* error code is returned directly */
9911 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9912 PEXT(&chunk->chunk));
9913 for (i = 0; i < chunk->mirror_count; i++)
9914 fprintf(stdout, " %u", chunk->mirror_id[i]);
9915 fprintf(stdout, "\n");
9919 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9920 pos = chunk->chunk.e_start;
9921 while (bytes_done < count) {
9922 /* compute initial CRC-32 checksum */
9923 crc = crc32(0L, Z_NULL, 0);
9924 memset(crc_array, 0, sizeof(crc_array));
9927 for (i = 0; i < chunk->mirror_count; i++) {
9928 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9930 if (bytes_read < 0) {
9933 "%s: failed to read data from mirror %u: %s.\n",
9934 progname, chunk->mirror_id[i],
9939 /* compute new CRC-32 checksum */
9940 crc_array[i] = crc32(crc, buf, bytes_read);
9944 print_checksums(chunk, crc_array);
9946 /* compare CRC-32 checksum values */
9947 for (i = 1; i < chunk->mirror_count; i++) {
9948 if (crc_array[i] != crc_array[0]) {
9952 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9953 progname, PEXT(&chunk->chunk),
9954 chunk->mirror_id[0],
9955 chunk->mirror_id[i]);
9960 bytes_done += bytes_read;
9963 if (verbose > 1 && rc == 0) {
9964 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9965 PEXT(&chunk->chunk));
9966 for (i = 0; i < chunk->mirror_count; i++)
9967 fprintf(stdout, " %u", chunk->mirror_id[i]);
9968 fprintf(stdout, " PASS\n\n");
9977 * lfs_mirror_verify_file() - Verify a mirrored file.
9978 * @fname: Mirrored file name.
9979 * @mirror_ids: Specified mirror ids to be verified.
9980 * @ids_nr: Number of specified mirror ids.
9981 * @verbose: Verbose mode.
9983 * This function verifies that each SYNC mirror of a mirrored file
9984 * specified by @fname contains exactly the same data.
9986 * If @mirror_ids is specified, then the function will verify the
9987 * mirrors specified by @mirror_ids contain exactly the same data.
9989 * If @verbose is specified, then the function will print where the
9990 * differences are if the data do not match. Otherwise, it will
9991 * just return an error in that case.
9993 * Return: 0 on success or a negative error code on failure.
9996 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9999 struct verify_chunk chunks_array[1024] = { };
10000 struct llapi_layout *layout = NULL;
10002 uint32_t flr_state;
10004 int chunk_count = 0;
10010 if (stat(fname, &stbuf) < 0) {
10011 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10012 progname, fname, strerror(errno));
10017 if (!S_ISREG(stbuf.st_mode)) {
10018 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10024 if (stbuf.st_size == 0) {
10026 fprintf(stdout, "%s: '%s' file size is 0.\n",
10032 fd = open(fname, O_DIRECT | O_RDONLY);
10034 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10035 progname, fname, strerror(errno));
10040 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10042 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10043 progname, fname, strerror(errno));
10047 layout = llapi_layout_get_by_fd(fd, 0);
10048 if (layout == NULL) {
10049 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10050 progname, fname, strerror(errno));
10052 llapi_lease_release(fd);
10056 rc = llapi_layout_flags_get(layout, &flr_state);
10058 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10059 progname, fname, strerror(errno));
10064 flr_state &= LCM_FL_FLR_MASK;
10065 switch (flr_state) {
10068 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10069 progname, fname, llapi_layout_flags_string(flr_state));
10075 /* find out mirror chunks to be verified */
10076 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10077 ARRAY_SIZE(chunks_array));
10078 if (chunk_count < 0) {
10084 /* filter specified mirror ids */
10085 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10088 print_chunks(fname, chunks_array, chunk_count);
10090 for (idx = 0; idx < chunk_count; idx++) {
10091 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10094 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10096 PEXT(&chunks_array[idx].chunk),
10097 (unsigned long long)stbuf.st_size);
10101 if (chunks_array[idx].mirror_count == 0) {
10103 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10105 PEXT(&chunks_array[idx].chunk));
10107 fprintf(stderr, "skipped\n");
10111 fprintf(stderr, "failed\n");
10115 if (chunks_array[idx].mirror_count == 1) {
10118 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10120 PEXT(&chunks_array[idx].chunk),
10121 chunks_array[idx].mirror_id[0]);
10125 rc = llapi_lease_check(fd);
10126 if (rc != LL_LEASE_RDLCK) {
10127 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10132 /* verify one chunk */
10133 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10134 &chunks_array[idx], verbose);
10148 llapi_layout_free(layout);
10149 llapi_lease_release(fd);
10157 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10158 * @argc: The count of lfs mirror verify command line arguments.
10159 * @argv: Array of strings for lfs mirror verify command line arguments.
10161 * This function parses lfs mirror verify command and verifies the
10162 * specified mirrored file(s).
10164 * Return: 0 on success or a negative error code on failure.
10166 static inline int lfs_mirror_verify(int argc, char **argv)
10168 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10174 char cmd[PATH_MAX];
10176 struct option long_opts[] = {
10177 { .val = 'o', .name = "only", .has_arg = required_argument },
10178 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10179 { .name = NULL } };
10181 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10183 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10186 rc = parse_mirror_ids(mirror_ids,
10187 ARRAY_SIZE(mirror_ids),
10191 "%s: bad mirror ids '%s'.\n",
10198 "%s: at least 2 mirror ids needed with '--only' option.\n",
10208 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10209 progname, argv[optind - 1]);
10215 if (argc == optind) {
10216 fprintf(stderr, "%s: no file name given.\n", progname);
10221 if (ids_nr > 0 && argc > optind + 1) {
10223 "%s: '--only' cannot be used upon multiple files.\n",
10231 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10237 for (; optind < argc; optind++) {
10238 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10248 * lfs_mirror() - Parse and execute lfs mirror commands.
10249 * @argc: The count of lfs mirror command line arguments.
10250 * @argv: Array of strings for lfs mirror command line arguments.
10252 * This function parses lfs mirror commands and performs the
10253 * corresponding functions specified in mirror_cmdlist[].
10255 * Return: 0 on success or an error code on failure.
10257 static int lfs_mirror(int argc, char **argv)
10259 char cmd[PATH_MAX];
10262 setlinebuf(stdout);
10264 Parser_init("lfs-mirror > ", mirror_cmdlist);
10266 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10268 program_invocation_short_name = cmd;
10270 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10272 rc = Parser_commands();
10274 return rc < 0 ? -rc : rc;
10277 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10279 #if __BYTE_ORDER == __BIG_ENDIAN
10280 __swab16s(&attrs->lsa_valid);
10281 __swab64s(&attrs->lsa_size);
10282 __swab64s(&attrs->lsa_blocks);
10286 enum lfs_som_type {
10287 LFS_SOM_SIZE = 0x1,
10288 LFS_SOM_BLOCKS = 0x2,
10289 LFS_SOM_FLAGS = 0x4,
10290 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10294 static int lfs_getsom(int argc, char **argv)
10297 struct lustre_som_attrs *attrs;
10298 char buf[sizeof(*attrs) + 64];
10299 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10302 while ((c = getopt(argc, argv, "sbf")) != -1) {
10305 type = LFS_SOM_SIZE;
10308 type = LFS_SOM_BLOCKS;
10311 type = LFS_SOM_FLAGS;
10314 fprintf(stderr, "%s: invalid option '%c'\n",
10324 fprintf(stderr, "%s: %s\n",
10325 progname, argc == 0 ? "miss file target" :
10326 "input more than 2 files");
10331 attrs = (void *)buf;
10332 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10335 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10336 argv[0], strerror(errno), errno);
10340 lustre_som_swab(attrs);
10343 case LFS_SOM_ATTR_ALL:
10344 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10345 path, attrs->lsa_size, attrs->lsa_blocks,
10349 printf("%llu\n", attrs->lsa_size);
10351 case LFS_SOM_BLOCKS:
10352 printf("%llu\n", attrs->lsa_blocks);
10354 case LFS_SOM_FLAGS:
10355 printf("%x\n", attrs->lsa_valid);
10358 fprintf(stderr, "%s: unknown option\n", progname);
10366 * lfs_mirror_list_commands() - List lfs mirror commands.
10367 * @argc: The count of command line arguments.
10368 * @argv: Array of strings for command line arguments.
10370 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10372 * Return: 0 on success.
10374 static int lfs_mirror_list_commands(int argc, char **argv)
10376 char buffer[81] = "";
10378 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10384 static int lfs_pcc_attach(int argc, char **argv)
10386 struct option long_opts[] = {
10387 { .val = 'i', .name = "id", .has_arg = required_argument },
10388 { .name = NULL } };
10391 __u32 archive_id = 0;
10394 char fullpath[PATH_MAX];
10395 enum lu_pcc_type type = LU_PCC_READWRITE;
10398 while ((c = getopt_long(argc, argv, "i:",
10399 long_opts, NULL)) != -1) {
10402 archive_id = strtoul(optarg, &end, 0);
10403 if (*end != '\0' || archive_id == 0) {
10404 fprintf(stderr, "error: %s: bad archive ID "
10405 "'%s'\n", argv[0], optarg);
10412 fprintf(stderr, "%s: option '%s' unrecognized\n",
10413 argv[0], argv[optind - 1]);
10418 if (argc <= optind) {
10419 fprintf(stderr, "%s: must specify one or more file names\n",
10424 while (optind < argc) {
10427 path = argv[optind++];
10428 if (realpath(path, fullpath) == NULL) {
10429 fprintf(stderr, "%s: could not find path '%s': %s\n",
10430 argv[0], path, strerror(errno));
10436 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10438 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10439 "with archive ID '%u': %s\n", argv[0],
10440 path, archive_id, strerror(-rc2));
10448 static int lfs_pcc_attach_fid(int argc, char **argv)
10450 struct option long_opts[] = {
10451 { .val = 'i', .name = "id", .has_arg = required_argument },
10452 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10453 { .name = NULL } };
10454 char short_opts[] = "i:m:";
10457 __u32 archive_id = 0;
10459 const char *mntpath = NULL;
10460 const char *fidstr;
10461 enum lu_pcc_type type = LU_PCC_READWRITE;
10464 while ((c = getopt_long(argc, argv, short_opts,
10465 long_opts, NULL)) != -1) {
10468 archive_id = strtoul(optarg, &end, 0);
10469 if (*end != '\0') {
10470 fprintf(stderr, "error: %s: bad archive ID "
10471 "'%s'\n", argv[0], optarg);
10481 fprintf(stderr, "%s: option '%s' unrecognized\n",
10482 argv[0], argv[optind - 1]);
10487 if (archive_id == 0) {
10488 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10492 if (mntpath == NULL) {
10493 fprintf(stderr, "%s: must specify Lustre mount point\n",
10498 if (argc <= optind) {
10499 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10503 while (optind < argc) {
10506 fidstr = argv[optind++];
10508 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10511 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10512 "with archive ID '%u': %s\n", argv[0],
10513 fidstr, mntpath, archive_id, strerror(rc2));
10515 if (rc == 0 && rc2 < 0)
10521 static int lfs_pcc_detach(int argc, char **argv)
10523 struct option long_opts[] = {
10524 { .val = 'k', .name = "keep", .has_arg = no_argument },
10525 { .name = NULL } };
10526 char short_opts[] = "k";
10530 char fullpath[PATH_MAX];
10531 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10534 while ((c = getopt_long(argc, argv, short_opts,
10535 long_opts, NULL)) != -1) {
10538 detach_opt = PCC_DETACH_OPT_NONE;
10543 fprintf(stderr, "%s: option '%s' unrecognized\n",
10544 argv[0], argv[optind - 1]);
10549 while (optind < argc) {
10552 path = argv[optind++];
10553 if (realpath(path, fullpath) == NULL) {
10554 fprintf(stderr, "%s: could not find path '%s': %s\n",
10555 argv[0], path, strerror(errno));
10561 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
10564 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
10565 "%s\n", argv[0], path, strerror(errno));
10573 static int lfs_pcc_detach_fid(int argc, char **argv)
10575 struct option long_opts[] = {
10576 { .val = 'k', .name = "keep", .has_arg = no_argument },
10577 { .name = NULL } };
10578 char short_opts[] = "k";
10582 const char *mntpath;
10583 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10586 while ((c = getopt_long(argc, argv, short_opts,
10587 long_opts, NULL)) != -1) {
10590 detach_opt = PCC_DETACH_OPT_NONE;
10595 fprintf(stderr, "%s: option '%s' unrecognized\n",
10596 argv[0], argv[optind - 1]);
10601 mntpath = argv[optind++];
10603 while (optind < argc) {
10606 fid = argv[optind++];
10608 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
10610 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
10611 "from PCC: %s\n", argv[0], fid, mntpath,
10620 static int lfs_pcc_state(int argc, char **argv)
10624 char fullpath[PATH_MAX];
10625 struct lu_pcc_state state;
10630 fprintf(stderr, "%s: must specify one or more file names\n",
10635 while (optind < argc) {
10638 path = argv[optind++];
10639 if (realpath(path, fullpath) == NULL) {
10640 fprintf(stderr, "%s: could not find path '%s': %s\n",
10641 argv[0], path, strerror(errno));
10647 rc2 = llapi_pcc_state_get(fullpath, &state);
10651 fprintf(stderr, "%s: cannot get PCC state of '%s': "
10652 "%s\n", argv[0], path, strerror(-rc2));
10656 printf("file: %s", path);
10657 printf(", type: %s", pcc_type2string(state.pccs_type));
10658 if (state.pccs_type == LU_PCC_NONE &&
10659 state.pccs_open_count == 0) {
10664 printf(", PCC file: %s", state.pccs_path);
10665 printf(", user number: %u", state.pccs_open_count);
10666 printf(", flags: %x", state.pccs_flags);
10673 * lfs_pcc_list_commands() - List lfs pcc commands.
10674 * @argc: The count of command line arguments.
10675 * @argv: Array of strings for command line arguments.
10677 * This function lists lfs pcc commands defined in pcc_cmdlist[].
10679 * Return: 0 on success.
10681 static int lfs_pcc_list_commands(int argc, char **argv)
10683 char buffer[81] = "";
10685 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
10692 * lfs_pcc() - Parse and execute lfs pcc commands.
10693 * @argc: The count of lfs pcc command line arguments.
10694 * @argv: Array of strings for lfs pcc command line arguments.
10696 * This function parses lfs pcc commands and performs the
10697 * corresponding functions specified in pcc_cmdlist[].
10699 * Return: 0 on success or an error code on failure.
10701 static int lfs_pcc(int argc, char **argv)
10703 char cmd[PATH_MAX];
10706 setlinebuf(stdout);
10708 Parser_init("lfs-pcc > ", pcc_cmdlist);
10710 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10712 program_invocation_short_name = cmd;
10714 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
10716 rc = Parser_commands();
10718 return rc < 0 ? -rc : rc;
10721 static int lfs_list_commands(int argc, char **argv)
10723 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10725 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10730 int main(int argc, char **argv)
10734 /* Ensure that liblustreapi constructor has run */
10735 if (!llapi_liblustreapi_initialized())
10736 fprintf(stderr, "liblustreapi was not properly initialized\n");
10738 setlinebuf(stdout);
10741 Parser_init("lfs > ", cmdlist);
10743 progname = program_invocation_short_name; /* Used in error messages */
10745 llapi_set_command_name(argv[1]);
10746 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10747 llapi_clear_command_name();
10749 rc = Parser_commands();
10752 return rc < 0 ? -rc : rc;
10755 #ifdef _LUSTRE_IDL_H_
10756 /* Everything we need here should be included by lustreapi.h. */
10757 # error "lfs should not depend on lustre_idl.h"
10758 #endif /* _LUSTRE_IDL_H_ */