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)
700 static uint32_t check_foreign_type_name(const char *foreign_type_name)
704 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
705 if (lu_foreign_types[i].lft_name == NULL)
707 if (strcmp(foreign_type_name,
708 lu_foreign_types[i].lft_name) == 0)
709 return lu_foreign_types[i].lft_type;
712 return LU_FOREIGN_TYPE_UNKNOWN;
715 static const char *error_loc = "syserror";
718 MIGRATION_NONBLOCK = 0x0001,
719 MIGRATION_MIRROR = 0x0002,
720 MIGRATION_NONDIRECT = 0x0004,
721 MIGRATION_VERBOSE = 0x0008,
724 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
725 struct llapi_layout *layout);
728 migrate_open_files(const char *name, __u64 migration_flags,
729 const struct llapi_stripe_param *param,
730 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
737 char parent[PATH_MAX];
738 char volatile_file[PATH_MAX];
744 if (param == NULL && layout == NULL) {
745 error_loc = "layout information";
749 /* search for file directory pathname */
750 if (strlen(name) > sizeof(parent) - 1) {
751 error_loc = "source file name";
755 strncpy(parent, name, sizeof(parent));
756 ptr = strrchr(parent, '/');
758 if (getcwd(parent, sizeof(parent)) == NULL) {
759 error_loc = "getcwd";
763 if (ptr == parent) /* leading '/' */
768 /* open file, direct io */
769 /* even if the file is only read, WR mode is nedeed to allow
770 * layout swap on fd */
772 if (!(migration_flags & MIGRATION_NONDIRECT))
774 fd = open(name, rflags);
777 error_loc = "cannot open source file";
781 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
783 error_loc = "cannot get MDT index";
788 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
789 mode_t open_mode = S_IRUSR | S_IWUSR;
791 random_value = random();
792 rc = snprintf(volatile_file, sizeof(volatile_file),
793 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
794 mdt_index, random_value);
795 if (rc >= sizeof(volatile_file)) {
800 /* create, open a volatile file, use caching (ie no directio) */
802 fdv = llapi_file_open_param(volatile_file, open_flags,
805 fdv = lfs_component_create(volatile_file, open_flags,
807 } while (fdv < 0 && (rc = fdv) == -EEXIST);
810 error_loc = "cannot create volatile file";
814 /* In case the MDT does not support creation of volatile files
815 * we should try to unlink it. */
816 (void)unlink(volatile_file);
818 /* Not-owner (root?) special case.
819 * Need to set owner/group of volatile file like original.
820 * This will allow to pass related check during layout_swap.
825 error_loc = "cannot stat source file";
829 rc = fstat(fdv, &stv);
832 error_loc = "cannot stat volatile";
836 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
837 rc = fchown(fdv, st.st_uid, st.st_gid);
840 error_loc = "cannot change ownwership of volatile";
859 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
861 struct llapi_layout *layout;
862 size_t buf_size = 4 * 1024 * 1024;
871 layout = llapi_layout_get_by_fd(fd_src, 0);
872 if (layout != NULL) {
873 uint64_t stripe_size;
875 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
877 buf_size = stripe_size;
879 llapi_layout_free(layout);
882 /* Use a page-aligned buffer for direct I/O */
883 rc = posix_memalign(&buf, getpagesize(), buf_size);
888 /* read new data only if we have written all
889 * previously read data */
892 rc = check_file(fd_src);
897 rsize = read(fd_src, buf, buf_size);
909 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
928 static int migrate_copy_timestamps(int fd, int fdv)
932 if (fstat(fd, &st) == 0) {
933 struct timeval tv[2] = {
934 {.tv_sec = st.st_atime},
935 {.tv_sec = st.st_mtime}
938 return futimes(fdv, tv);
944 static int migrate_block(int fd, int fdv)
951 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
953 error_loc = "cannot get dataversion";
961 /* The grouplock blocks all concurrent accesses to the file.
962 * It has to be taken after llapi_get_data_version as it would
964 rc = llapi_group_lock(fd, gid);
966 error_loc = "cannot get group lock";
970 rc = migrate_copy_data(fd, fdv, NULL);
972 error_loc = "data copy failed";
976 /* Make sure we keep original atime/mtime values */
977 rc = migrate_copy_timestamps(fd, fdv);
979 error_loc = "timestamp copy failed";
984 * for a migration we need to check data version on file did
987 * Pass in gid=0 since we already own grouplock. */
988 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
989 SWAP_LAYOUTS_CHECK_DV1);
991 error_loc = "file changed";
994 error_loc = "cannot swap layout";
999 rc2 = llapi_group_unlock(fd, gid);
1000 if (rc2 < 0 && rc == 0) {
1001 error_loc = "unlock group lock";
1009 * Internal helper for migrate_copy_data(). Check lease and report error if
1012 * \param[in] fd File descriptor on which to check the lease.
1014 * \retval 0 Migration can keep on going.
1015 * \retval -errno Error occurred, abort migration.
1017 static int check_lease(int fd)
1021 rc = llapi_lease_check(fd);
1023 return 0; /* llapi_check_lease returns > 0 on success. */
1028 static int migrate_nonblock(int fd, int fdv)
1034 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1036 error_loc = "cannot get data version";
1040 rc = migrate_copy_data(fd, fdv, check_lease);
1042 error_loc = "data copy failed";
1046 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1048 error_loc = "cannot get data version";
1054 error_loc = "source file changed";
1058 /* Make sure we keep original atime/mtime values */
1059 rc = migrate_copy_timestamps(fd, fdv);
1061 error_loc = "timestamp copy failed";
1068 static int lfs_component_set(char *fname, int comp_id,
1069 __u32 flags, __u32 neg_flags)
1072 __u32 flags_array[2];
1077 ids[count] = comp_id;
1078 flags_array[count] = flags;
1083 ids[count] = comp_id;
1084 flags_array[count] = neg_flags | LCME_FL_NEG;
1088 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1091 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1092 progname, comp_id, fname, flags, neg_flags);
1097 static int lfs_component_del(char *fname, __u32 comp_id,
1098 __u32 flags, __u32 neg_flags)
1102 if (flags && neg_flags)
1105 if (!flags && neg_flags)
1106 flags = neg_flags | LCME_FL_NEG;
1108 if ((flags && comp_id) || (!flags && !comp_id))
1111 /* LCME_FL_INIT is the only supported flag in PFL */
1113 if (flags & ~LCME_KNOWN_FLAGS) {
1115 "%s setstripe: unknown flags %#x\n",
1119 } else if (comp_id > LCME_ID_MAX) {
1120 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1125 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1128 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1129 progname, comp_id, fname, strerror(errno));
1133 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1140 rc = llapi_layout_file_comp_add(fname, layout);
1142 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1143 fname, strerror(errno));
1147 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1148 struct llapi_layout *layout)
1156 fd = lstat(fname, &st);
1157 if (fd == 0 && S_ISDIR(st.st_mode))
1158 open_flags = O_DIRECTORY | O_RDONLY;
1160 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1162 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1163 S_ISDIR(st.st_mode) ?
1164 "set default composite layout for" :
1165 "create composite file",
1166 fname, strerror(errno));
1170 static int lfs_migrate(char *name, __u64 migration_flags,
1171 struct llapi_stripe_param *param,
1172 struct llapi_layout *layout)
1178 rc = migrate_open_files(name, migration_flags, param, layout,
1183 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1184 /* Blocking mode (forced if servers do not support file lease).
1185 * It is also the default mode, since we cannot distinguish
1186 * between a broken lease and a server that does not support
1187 * atomic swap/close (LU-6785) */
1188 rc = migrate_block(fd, fdv);
1192 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1194 error_loc = "cannot get lease";
1198 rc = migrate_nonblock(fd, fdv);
1200 llapi_lease_release(fd);
1204 /* Atomically put lease, swap layouts and close.
1205 * for a migration we need to check data version on file did
1207 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1209 error_loc = "cannot swap layout";
1221 fprintf(stderr, "error: %s: %s: %s: %s\n",
1222 progname, name, error_loc, strerror(-rc));
1223 else if (migration_flags & MIGRATION_VERBOSE)
1224 printf("%s\n", name);
1229 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1238 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1242 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1243 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1244 const char *comp_name = comp_flags_table[i].cfn_name;
1246 if (strcmp(name, comp_name) == 0) {
1247 *flags |= comp_flag;
1249 } else if (strncmp(name, "^", 1) == 0 &&
1250 strcmp(name + 1, comp_name) == 0) {
1251 *neg_flags |= comp_flag;
1256 llapi_printf(LLAPI_MSG_ERROR,
1257 "%s: component flag '%s' not supported\n",
1263 if (!*flags && !*neg_flags)
1266 /* don't allow to set and exclude the same flag */
1267 if (*flags & *neg_flags)
1273 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1281 if (strncmp(string, "^", 1) == 0) {
1282 *neg_state = llapi_layout_string_flags(string + 1);
1283 if (*neg_state != 0)
1286 *state = llapi_layout_string_flags(string);
1291 llapi_printf(LLAPI_MSG_ERROR,
1292 "%s: mirrored file state '%s' not supported\n",
1298 * struct mirror_args - Command-line arguments for mirror(s).
1299 * @m_count: Number of mirrors to be created with this layout.
1300 * @m_flags: Mirror level flags, only 'prefer' is supported.
1301 * @m_layout: Mirror layout.
1302 * @m_file: A victim file. Its layout will be split and used as a mirror.
1303 * @m_next: Point to the next node of the list.
1305 * Command-line arguments for mirror(s) will be parsed and stored in
1306 * a linked list that consists of this structure.
1308 struct mirror_args {
1311 struct llapi_layout *m_layout;
1313 struct mirror_args *m_next;
1316 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1321 rc = llapi_layout_comp_flags_get(layout, &flags);
1325 if (flags & LCME_FL_NEG) {
1326 fprintf(stderr, "error: %s: negative flags are not supported\n",
1331 if (flags & LCME_FL_STALE) {
1332 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1333 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1337 return LLAPI_LAYOUT_ITER_CONT;
1340 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1342 uint64_t start, end;
1346 /* LU-10112: do not support dom+flr in phase 1 */
1347 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1351 rc = llapi_layout_pattern_get(layout, &pattern);
1355 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1356 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1361 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1365 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1369 if (end != LUSTRE_EOF) {
1370 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1375 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1380 * enum mirror_flags - Flags for extending a mirrored file.
1381 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1382 * in case the victim file(s) contains the same data as the
1383 * original mirrored file.
1384 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1385 * @MF_COMP_ID: specified component id instead of mirror id
1387 * Flags for extending a mirrored file.
1396 * mirror_create_sanity_check() - Check mirror list.
1397 * @list: A linked list that stores the mirror arguments.
1399 * This function does a sanity check on @list for creating
1402 * Return: 0 on success or a negative error code on failure.
1404 static int mirror_create_sanity_check(const char *fname,
1405 struct mirror_args *list)
1408 bool has_m_file = false;
1409 bool has_m_layout = false;
1415 struct llapi_layout *layout;
1417 layout = llapi_layout_get_by_path(fname, 0);
1420 "error: %s: file '%s' couldn't get layout\n",
1425 rc = mirror_sanity_check_one(layout);
1426 llapi_layout_free(layout);
1432 while (list != NULL) {
1433 if (list->m_file != NULL) {
1435 llapi_layout_free(list->m_layout);
1438 llapi_layout_get_by_path(list->m_file, 0);
1439 if (list->m_layout == NULL) {
1441 "error: %s: file '%s' has no layout\n",
1442 progname, list->m_file);
1446 has_m_layout = true;
1447 if (list->m_layout == NULL) {
1448 fprintf(stderr, "error: %s: no mirror layout\n",
1454 rc = mirror_sanity_check_one(list->m_layout);
1458 list = list->m_next;
1461 if (has_m_file && has_m_layout) {
1463 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1471 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1473 __u32 mirror_flags = *(__u32 *)cbdata;
1477 rc = llapi_layout_comp_flags_get(layout, &flags);
1482 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1487 return LLAPI_LAYOUT_ITER_CONT;
1491 * mirror_create() - Create a mirrored file.
1492 * @fname: The file to be created.
1493 * @mirror_list: A linked list that stores the mirror arguments.
1495 * This function creates a mirrored file @fname with the mirror(s)
1496 * from @mirror_list.
1498 * Return: 0 on success or a negative error code on failure.
1500 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1502 struct llapi_layout *layout = NULL;
1503 struct mirror_args *cur_mirror = NULL;
1504 uint16_t mirror_count = 0;
1508 rc = mirror_create_sanity_check(NULL, mirror_list);
1512 cur_mirror = mirror_list;
1513 while (cur_mirror != NULL) {
1514 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1516 &cur_mirror->m_flags);
1519 fprintf(stderr, "%s: failed to set mirror flags\n",
1524 for (i = 0; i < cur_mirror->m_count; i++) {
1525 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1528 fprintf(stderr, "error: %s: "
1529 "merge layout failed: %s\n",
1530 progname, strerror(errno));
1534 mirror_count += cur_mirror->m_count;
1535 cur_mirror = cur_mirror->m_next;
1538 if (layout == NULL) {
1539 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1543 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1546 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1547 progname, strerror(errno));
1551 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1559 llapi_layout_free(layout);
1564 * Compare files and check lease on @fd.
1566 * \retval bytes number of bytes are the same
1568 static ssize_t mirror_file_compare(int fd, int fdv)
1570 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1572 ssize_t bytes_done = 0;
1573 ssize_t bytes_read = 0;
1575 buf = malloc(buflen * 2);
1580 if (!llapi_lease_check(fd)) {
1581 bytes_done = -EBUSY;
1585 bytes_read = read(fd, buf, buflen);
1586 if (bytes_read <= 0)
1589 if (bytes_read != read(fdv, buf + buflen, buflen))
1592 /* XXX: should compute the checksum on each buffer and then
1593 * compare checksum to avoid cache collision */
1594 if (memcmp(buf, buf + buflen, bytes_read))
1597 bytes_done += bytes_read;
1605 static int mirror_extend_file(const char *fname, const char *victim_file,
1606 enum mirror_flags mirror_flags)
1611 struct stat stbuf_v;
1612 struct ll_ioc_lease *data = NULL;
1615 fd = open(fname, O_RDWR);
1617 error_loc = "open source file";
1622 fdv = open(victim_file, O_RDWR);
1624 error_loc = "open target file";
1629 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1630 error_loc = "stat source or target file";
1635 if (stbuf.st_dev != stbuf_v.st_dev) {
1636 error_loc = "stat source and target file";
1641 /* mirrors should be of the same size */
1642 if (stbuf.st_size != stbuf_v.st_size) {
1643 error_loc = "file sizes don't match";
1648 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1650 error_loc = "cannot get lease";
1654 if (!(mirror_flags & MF_NO_VERIFY)) {
1656 /* mirrors should have the same contents */
1657 ret = mirror_file_compare(fd, fdv);
1658 if (ret != stbuf.st_size) {
1659 error_loc = "file busy or contents don't match";
1660 rc = ret < 0 ? ret : -EINVAL;
1665 /* Get rid of caching pages from clients */
1666 rc = llapi_file_flush(fd);
1668 error_loc = "cannot get data version";
1672 rc = llapi_file_flush(fdv);
1674 error_loc = "cannot get data version";
1679 /* Make sure we keep original atime/mtime values */
1680 rc = migrate_copy_timestamps(fd, fdv);
1682 error_loc = "cannot copy timestamp";
1686 /* Atomically put lease, merge layouts and close. */
1687 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1689 error_loc = "memory allocation";
1692 data->lil_mode = LL_LEASE_UNLCK;
1693 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1694 data->lil_count = 1;
1695 data->lil_ids[0] = fdv;
1696 rc = llapi_lease_set(fd, data);
1698 error_loc = "cannot merge layout";
1700 } else if (rc == 0) {
1702 error_loc = "lost lease lock";
1715 (void) unlink(victim_file);
1717 fprintf(stderr, "error: %s: %s: %s: %s\n",
1718 progname, fname, error_loc, strerror(-rc));
1722 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1724 struct ll_ioc_lease *data = NULL;
1729 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1733 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1735 error_loc = "cannot get lease";
1739 rc = migrate_nonblock(fd, fdv);
1741 llapi_lease_release(fd);
1745 /* Atomically put lease, merge layouts and close. */
1746 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1748 error_loc = "memory allocation";
1751 data->lil_mode = LL_LEASE_UNLCK;
1752 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1753 data->lil_count = 1;
1754 data->lil_ids[0] = fdv;
1755 rc = llapi_lease_set(fd, data);
1757 error_loc = "cannot merge layout";
1759 } else if (rc == 0) {
1761 error_loc = "lost lease lock";
1774 fprintf(stderr, "error: %s: %s: %s: %s\n",
1775 progname, name, error_loc, strerror(-rc));
1779 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1780 enum mirror_flags mirror_flags)
1784 rc = mirror_create_sanity_check(fname, mirror_list);
1788 while (mirror_list) {
1789 if (mirror_list->m_file != NULL) {
1790 rc = mirror_extend_file(fname, mirror_list->m_file,
1793 __u32 mirror_count = mirror_list->m_count;
1795 while (mirror_count > 0) {
1796 rc = mirror_extend_layout(fname,
1797 mirror_list->m_layout);
1807 mirror_list = mirror_list->m_next;
1813 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1818 rc = llapi_layout_mirror_id_get(layout, &id);
1822 if ((__u16)id == *(__u16 *)cbdata)
1823 return LLAPI_LAYOUT_ITER_STOP;
1825 return LLAPI_LAYOUT_ITER_CONT;
1828 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1833 rc = llapi_layout_comp_id_get(layout, &id);
1837 if (id == *(__u32 *)cbdata)
1838 return LLAPI_LAYOUT_ITER_STOP;
1840 return LLAPI_LAYOUT_ITER_CONT;
1842 static int mirror_split(const char *fname, __u32 id,
1843 enum mirror_flags mflags, const char *victim_file)
1845 struct llapi_layout *layout;
1846 char parent[PATH_MAX];
1847 char victim[PATH_MAX];
1848 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1850 struct ll_ioc_lease *data;
1851 uint16_t mirror_count;
1856 /* check fname contains mirror with mirror_id/comp_id */
1857 layout = llapi_layout_get_by_path(fname, 0);
1860 "error %s: file '%s' couldn't get layout\n",
1865 rc = mirror_sanity_check_one(layout);
1869 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1872 "error %s: file '%s' couldn't get mirror count\n",
1876 if (mirror_count < 2) {
1878 "error %s: file '%s' has %d component, cannot split\n",
1879 progname, fname, mirror_count);
1883 if (mflags & MF_COMP_ID) {
1884 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1885 id = mirror_id_of(id);
1887 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1890 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1893 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1895 "error %s: file '%s' does not contain mirror with id %u\n",
1896 progname, fname, id);
1900 fd = open(fname, O_RDWR);
1903 "error %s: open file '%s' failed: %s\n",
1904 progname, fname, strerror(errno));
1908 /* get victim file directory pathname */
1909 if (strlen(fname) > sizeof(parent) - 1) {
1910 fprintf(stderr, "error %s: file name of '%s' too long\n",
1915 strncpy(parent, fname, sizeof(parent));
1916 ptr = strrchr(parent, '/');
1918 if (getcwd(parent, sizeof(parent)) == NULL) {
1919 fprintf(stderr, "error %s: getcwd failed: %s\n",
1920 progname, strerror(errno));
1930 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1932 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1937 if (victim_file == NULL) {
1938 /* use a temp file to store the splitted layout */
1939 if (mflags & MF_DESTROY) {
1940 fdv = llapi_create_volatile_idx(parent, mdt_index,
1941 O_LOV_DELAY_CREATE);
1943 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1945 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1948 /* user specified victim file */
1949 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1954 "error %s: create victim file failed: %s\n",
1955 progname, strerror(errno));
1959 /* get lease lock of fname */
1960 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1963 "error %s: cannot get lease of file '%s': %d\n",
1964 progname, fname, rc);
1968 /* Atomatically put lease, split layouts and close. */
1969 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1975 data->lil_mode = LL_LEASE_UNLCK;
1976 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1977 data->lil_count = 2;
1978 data->lil_ids[0] = fdv;
1979 data->lil_ids[1] = id;
1980 rc = llapi_lease_set(fd, data);
1982 if (rc == 0) /* lost lease lock */
1985 "error %s: cannot split '%s': %s\n",
1986 progname, fname, strerror(-rc));
1997 llapi_layout_free(layout);
2002 * Parse a string containing an target index list into an array of integers.
2004 * The input string contains a comma delimited list of individual
2005 * indices and ranges, for example "1,2-4,7". Add the indices into the
2006 * \a tgts array and remove duplicates.
2008 * \param[out] tgts array to store indices in
2009 * \param[in] size size of \a tgts array
2010 * \param[in] offset starting index in \a tgts
2011 * \param[in] arg string containing OST index list
2012 * \param[in/out] overstriping index list may contain duplicates
2014 * \retval positive number of indices in \a tgts
2015 * \retval -EINVAL unable to parse \a arg
2017 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2018 unsigned long long *pattern)
2022 int slots = size - offset;
2024 bool overstriped = false;
2030 end_of_loop = false;
2031 while (!end_of_loop) {
2032 int start_index = 0;
2035 char *endptr = NULL;
2039 ptr = strchrnul(arg, ',');
2041 end_of_loop = *ptr == '\0';
2044 start_index = strtol(arg, &endptr, 0);
2045 if (endptr == arg) /* no data at all */
2047 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2050 end_index = start_index;
2051 if (*endptr == '-') {
2052 end_index = strtol(endptr + 1, &endptr, 0);
2053 if (*endptr != '\0')
2055 if (end_index < start_index)
2059 for (i = start_index; i <= end_index && slots > 0; i++) {
2062 /* remove duplicate */
2063 for (j = 0; j < offset; j++) {
2064 if (tgts[j] == i && pattern &&
2065 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2067 else if (tgts[j] == i)
2073 if (j == offset) { /* check complete */
2079 if (slots == 0 && i < end_index)
2087 if (!end_of_loop && ptr != NULL)
2090 if (!overstriped && pattern)
2091 *pattern = LLAPI_LAYOUT_DEFAULT;
2093 return rc < 0 ? rc : nr;
2096 struct lfs_setstripe_args {
2097 unsigned long long lsa_comp_end;
2098 unsigned long long lsa_stripe_size;
2099 long long lsa_stripe_count;
2100 long long lsa_stripe_off;
2101 __u32 lsa_comp_flags;
2102 __u32 lsa_comp_neg_flags;
2103 unsigned long long lsa_pattern;
2104 unsigned int lsa_mirror_count;
2106 bool lsa_first_comp;
2108 char *lsa_pool_name;
2111 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2113 unsigned int mirror_count = lsa->lsa_mirror_count;
2114 bool first_comp = lsa->lsa_first_comp;
2116 memset(lsa, 0, sizeof(*lsa));
2118 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2119 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2120 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2121 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2122 lsa->lsa_pool_name = NULL;
2124 lsa->lsa_mirror_count = mirror_count;
2125 lsa->lsa_first_comp = first_comp;
2129 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2130 * @lsa: Stripe options to be initialized and inherited.
2132 * This function initializes stripe options in @lsa and inherit
2133 * stripe_size, stripe_count and OST pool_name options.
2137 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2139 unsigned long long stripe_size;
2140 long long stripe_count;
2141 char *pool_name = NULL;
2143 stripe_size = lsa->lsa_stripe_size;
2144 stripe_count = lsa->lsa_stripe_count;
2145 pool_name = lsa->lsa_pool_name;
2147 setstripe_args_init(lsa);
2149 lsa->lsa_stripe_size = stripe_size;
2150 lsa->lsa_stripe_count = stripe_count;
2151 lsa->lsa_pool_name = pool_name;
2154 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2156 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2157 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2158 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2159 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2160 lsa->lsa_pool_name != NULL ||
2161 lsa->lsa_comp_end != 0);
2165 * comp_args_to_layout() - Create or extend a composite layout.
2166 * @composite: Pointer to the composite layout.
2167 * @lsa: Stripe options for the new component.
2169 * This function creates or extends a composite layout by adding a new
2170 * component with stripe options from @lsa.
2172 * Return: 0 on success or an error code on failure.
2174 static int comp_args_to_layout(struct llapi_layout **composite,
2175 struct lfs_setstripe_args *lsa,
2178 struct llapi_layout *layout = *composite;
2179 uint64_t prev_end = 0;
2182 if (layout == NULL) {
2183 layout = llapi_layout_alloc();
2184 if (layout == NULL) {
2185 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2189 *composite = layout;
2193 /* Get current component extent, current component
2194 * must be the tail component. */
2195 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2197 fprintf(stderr, "Get comp extent failed. %s\n",
2202 if (lsa->lsa_first_comp)
2205 if (lsa->lsa_first_comp)
2206 rc = llapi_layout_add_first_comp(layout);
2208 rc = llapi_layout_comp_add(layout);
2210 fprintf(stderr, "Add component failed. %s\n",
2215 /* reset lsa_first_comp */
2216 lsa->lsa_first_comp = false;
2219 rc = llapi_layout_comp_extent_set(layout, prev_end,
2222 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2223 prev_end, lsa->lsa_comp_end, strerror(errno));
2228 /* Data-on-MDT component setting */
2229 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2230 /* In case of Data-on-MDT patterns the only extra option
2231 * applicable is stripe size option. */
2232 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2233 fprintf(stderr, "Option 'stripe-count' can't be "
2234 "specified with Data-on-MDT component: %lld\n",
2235 lsa->lsa_stripe_count);
2238 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2239 fprintf(stderr, "Option 'stripe-size' can't be "
2240 "specified with Data-on-MDT component: %llu\n",
2241 lsa->lsa_stripe_size);
2244 if (lsa->lsa_nr_tgts != 0) {
2245 fprintf(stderr, "Option 'ost-list' can't be specified "
2246 "with Data-on-MDT component: '%i'\n",
2250 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2251 fprintf(stderr, "Option 'stripe-offset' can't be "
2252 "specified with Data-on-MDT component: %lld\n",
2253 lsa->lsa_stripe_off);
2256 if (lsa->lsa_pool_name != 0) {
2257 fprintf(stderr, "Option 'pool' can't be specified "
2258 "with Data-on-MDT component: '%s'\n",
2259 lsa->lsa_pool_name);
2263 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2265 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2266 lsa->lsa_pattern, strerror(errno));
2269 /* Data-on-MDT component has always single stripe up to end */
2270 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2271 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2272 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2274 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2275 lsa->lsa_pattern, strerror(errno));
2280 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2282 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2283 lsa->lsa_stripe_size, strerror(errno));
2287 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2289 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2290 lsa->lsa_stripe_count, strerror(errno));
2294 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2296 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2297 lsa->lsa_comp_flags, strerror(errno));
2301 if (lsa->lsa_pool_name != NULL) {
2302 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2304 fprintf(stderr, "Set pool name: %s failed. %s\n",
2305 lsa->lsa_pool_name, strerror(errno));
2309 rc = llapi_layout_pool_name_set(layout, "");
2311 fprintf(stderr, "Clear pool name failed: %s\n",
2317 if (lsa->lsa_nr_tgts > 0) {
2318 if (lsa->lsa_stripe_count > 0 &&
2319 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2320 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2321 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2322 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2323 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2326 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2327 rc = llapi_layout_ost_index_set(layout, i,
2332 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2333 lsa->lsa_stripe_off != -1) {
2334 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2337 fprintf(stderr, "Set ost index %d failed. %s\n",
2338 i, strerror(errno));
2345 static int build_component(struct llapi_layout **layout,
2346 struct lfs_setstripe_args *lsa, bool set_extent)
2350 rc = comp_args_to_layout(layout, lsa, set_extent);
2354 if (lsa->lsa_mirror_count > 0) {
2355 rc = llapi_layout_mirror_count_set(*layout,
2356 lsa->lsa_mirror_count);
2360 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2363 lsa->lsa_mirror_count = 0;
2369 static int build_layout_from_yaml_node(struct cYAML *node,
2370 struct llapi_layout **layout,
2371 struct lfs_setstripe_args *lsa,
2378 if (node->cy_type == CYAML_TYPE_OBJECT) {
2379 /* go deep to sub blocks */
2380 rc = build_layout_from_yaml_node(node->cy_child, layout,
2385 if (node->cy_string == NULL)
2388 string = node->cy_string;
2389 /* skip leading lmm_ if present, to simplify parsing */
2390 if (strncmp(string, "lmm_", 4) == 0)
2393 if (node->cy_type == CYAML_TYPE_STRING) {
2394 if (!strcmp(string, "lcme_extent.e_end")) {
2395 if (!strcmp(node->cy_valuestring, "EOF") ||
2396 !strcmp(node->cy_valuestring, "eof"))
2397 lsa->lsa_comp_end = LUSTRE_EOF;
2398 } else if (!strcmp(string, "pool")) {
2399 lsa->lsa_pool_name = node->cy_valuestring;
2400 } else if (!strcmp(string, "pattern")) {
2401 if (!strcmp(node->cy_valuestring, "mdt"))
2402 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2403 if (!strcmp(node->cy_valuestring,
2404 "raid0,overstriped"))
2406 LLAPI_LAYOUT_OVERSTRIPING;
2407 } else if (!strcmp(string, "lcme_flags")) {
2408 rc = comp_str2flags(node->cy_valuestring,
2409 &lsa->lsa_comp_flags,
2410 &lsa->lsa_comp_neg_flags);
2413 /* Only template flags have meaning in
2414 * the layout for a new file
2416 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2418 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2419 if (!strcmp(string, "lcm_mirror_count")) {
2420 lsa->lsa_mirror_count = node->cy_valueint;
2421 } else if (!strcmp(string, "lcme_extent.e_start")) {
2422 if (node->cy_valueint != 0 || *layout != NULL) {
2423 rc = build_component(layout, lsa, true);
2428 if (node->cy_valueint == 0)
2429 lsa->lsa_first_comp = true;
2431 /* initialize lsa */
2432 setstripe_args_init(lsa);
2433 lsa->lsa_tgts = osts;
2434 } else if (!strcmp(string, "lcme_extent.e_end")) {
2435 if (node->cy_valueint == -1)
2436 lsa->lsa_comp_end = LUSTRE_EOF;
2438 lsa->lsa_comp_end = node->cy_valueint;
2439 } else if (!strcmp(string, "stripe_count")) {
2440 lsa->lsa_stripe_count = node->cy_valueint;
2441 } else if (!strcmp(string, "stripe_size")) {
2442 lsa->lsa_stripe_size = node->cy_valueint;
2443 } else if (!strcmp(string, "stripe_offset")) {
2444 lsa->lsa_stripe_off = node->cy_valueint;
2445 } else if (!strcmp(string, "l_ost_idx")) {
2446 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2451 node = node->cy_next;
2457 static int lfs_comp_create_from_yaml(char *template,
2458 struct llapi_layout **layout,
2459 struct lfs_setstripe_args *lsa,
2462 struct cYAML *tree = NULL, *err_rc = NULL;
2465 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2467 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2468 progname, template);
2469 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2470 "can't parse", &err_rc);
2471 cYAML_print_tree2file(stderr, err_rc);
2472 cYAML_free_tree(err_rc);
2477 /* initialize lsa for plain file */
2478 setstripe_args_init(lsa);
2479 lsa->lsa_tgts = osts;
2481 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2483 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2484 progname, template);
2487 rc = build_component(layout, lsa, *layout != NULL);
2489 /* clean clean lsa */
2490 setstripe_args_init(lsa);
2494 cYAML_free_tree(tree);
2498 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2499 * end of the last component in the existing file, and adjust the
2500 * first extent start of the components to be added accordingly. */
2501 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2503 struct llapi_layout *head;
2504 uint64_t start, end, stripe_size, prev_end = 0;
2507 if (layout == NULL) {
2509 "%s setstripe: layout must be specified\n",
2515 head = llapi_layout_get_by_path(fname, 0);
2518 "%s setstripe: cannot read layout from '%s': %s\n",
2519 progname, fname, strerror(errno));
2521 } else if (errno == ENODATA) {
2522 /* file without LOVEA, this component-add will be turned
2523 * into a component-create. */
2524 llapi_layout_free(head);
2526 } else if (!llapi_layout_is_composite(head)) {
2527 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2529 llapi_layout_free(head);
2533 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2535 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2536 progname, strerror(errno));
2537 llapi_layout_free(head);
2541 llapi_layout_free(head);
2543 /* Make sure we use the first component of the layout to be added. */
2544 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2547 "%s setstripe: cannot move component cursor: %s\n",
2548 progname, strerror(errno));
2552 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2554 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2555 progname, strerror(errno));
2559 if (start > prev_end || end <= prev_end) {
2561 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2562 progname, start, end, prev_end);
2566 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2568 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2569 progname, strerror(errno));
2573 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2574 (prev_end & (stripe_size - 1))) {
2576 "%s setstripe: stripe size %lu not aligned with %lu\n",
2577 progname, stripe_size, prev_end);
2581 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2584 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2585 progname, prev_end, end, strerror(errno));
2592 static inline bool arg_is_eof(char *arg)
2594 return !strncmp(arg, "-1", strlen("-1")) ||
2595 !strncmp(arg, "EOF", strlen("EOF")) ||
2596 !strncmp(arg, "eof", strlen("eof"));
2600 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2602 * Return: Valid mirror_args pointer on success and
2603 * NULL if memory allocation fails.
2605 static struct mirror_args *lfs_mirror_alloc(void)
2607 struct mirror_args *mirror = NULL;
2610 mirror = calloc(1, sizeof(*mirror));
2621 * lfs_mirror_free() - Free memory allocated for a mirror argument
2623 * @mirror: Previously allocated mirror argument structure by
2624 * lfs_mirror_alloc().
2626 * Free memory allocated for @mirror.
2630 static void lfs_mirror_free(struct mirror_args *mirror)
2632 if (mirror->m_layout != NULL)
2633 llapi_layout_free(mirror->m_layout);
2638 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2639 * @mirror_list: Previously allocated mirror list.
2641 * Free memory allocated for @mirror_list.
2645 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2647 struct mirror_args *next_mirror = NULL;
2649 while (mirror_list != NULL) {
2650 next_mirror = mirror_list->m_next;
2651 lfs_mirror_free(mirror_list);
2652 mirror_list = next_mirror;
2664 LFS_COMP_NO_VERIFY_OPT,
2666 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2668 LFS_MIRROR_STATE_OPT,
2670 LFS_MIRROR_INDEX_OPT,
2671 LFS_LAYOUT_FOREIGN_OPT,
2676 static int lfs_setstripe_internal(int argc, char **argv,
2677 enum setstripe_origin opc)
2679 struct lfs_setstripe_args lsa = { 0 };
2680 struct llapi_stripe_param *param = NULL;
2681 struct find_param migrate_mdt_param = {
2691 unsigned long long size_units = 1;
2692 bool migrate_mode = false;
2693 bool migrate_mdt_mode = false;
2694 bool setstripe_mode = false;
2695 bool migration_block = false;
2696 __u64 migration_flags = 0;
2697 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2698 int comp_del = 0, comp_set = 0;
2701 struct llapi_layout *layout = NULL;
2702 struct llapi_layout **lpp = &layout;
2703 bool mirror_mode = false;
2704 bool has_m_file = false;
2705 __u32 mirror_count = 0;
2706 enum mirror_flags mirror_flags = 0;
2707 struct mirror_args *mirror_list = NULL;
2708 struct mirror_args *new_mirror = NULL;
2709 struct mirror_args *last_mirror = NULL;
2710 __u16 mirror_id = 0;
2712 bool from_yaml = false;
2713 bool from_copy = false;
2714 char *template = NULL;
2715 bool foreign_mode = false;
2717 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
2718 char *mode_opt = NULL;
2719 mode_t previous_umask = 0;
2720 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2722 struct option long_opts[] = {
2723 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2724 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2725 /* --block is only valid in migrate mode */
2726 { .val = 'b', .name = "block", .has_arg = no_argument },
2727 { .val = LFS_COMP_ADD_OPT,
2728 .name = "comp-add", .has_arg = no_argument },
2729 { .val = LFS_COMP_ADD_OPT,
2730 .name = "component-add", .has_arg = no_argument },
2731 { .val = LFS_COMP_DEL_OPT,
2732 .name = "comp-del", .has_arg = no_argument },
2733 { .val = LFS_COMP_DEL_OPT,
2734 .name = "component-del", .has_arg = no_argument },
2735 { .val = LFS_COMP_FLAGS_OPT,
2736 .name = "comp-flags", .has_arg = required_argument },
2737 { .val = LFS_COMP_FLAGS_OPT,
2738 .name = "component-flags",
2739 .has_arg = required_argument },
2740 { .val = LFS_COMP_SET_OPT,
2741 .name = "comp-set", .has_arg = no_argument },
2742 { .val = LFS_COMP_SET_OPT,
2743 .name = "component-set",
2744 .has_arg = no_argument},
2745 { .val = LFS_COMP_NO_VERIFY_OPT,
2746 .name = "no-verify", .has_arg = no_argument},
2747 { .val = LFS_LAYOUT_FLAGS_OPT,
2748 .name = "flags", .has_arg = required_argument},
2749 { .val = LFS_LAYOUT_FOREIGN_OPT,
2750 .name = "foreign", .has_arg = optional_argument},
2751 { .val = LFS_MIRROR_ID_OPT,
2752 .name = "mirror-id", .has_arg = required_argument},
2753 { .val = LFS_MODE_OPT,
2754 .name = "mode", .has_arg = required_argument},
2755 { .val = LFS_LAYOUT_COPY,
2756 .name = "copy", .has_arg = required_argument},
2757 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2758 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2759 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2760 { .val = 'C', .name = "overstripe-count",
2761 .has_arg = required_argument},
2762 { .val = 'd', .name = "delete", .has_arg = no_argument},
2763 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2764 /* --non-direct is only valid in migrate mode */
2765 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2766 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2767 { .val = 'E', .name = "component-end",
2768 .has_arg = required_argument},
2769 { .val = 'f', .name = "file", .has_arg = required_argument },
2770 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2771 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2772 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2773 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2774 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2775 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2776 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2777 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2778 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2779 { .val = 'L', .name = "layout", .has_arg = required_argument },
2780 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2781 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2782 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2783 /* --non-block is only valid in migrate mode */
2784 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2785 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2786 { .val = 'o', .name = "ost", .has_arg = required_argument },
2787 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2788 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2789 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2791 { .val = 'p', .name = "pool", .has_arg = required_argument },
2792 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2793 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2794 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2795 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2796 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2797 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2798 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2799 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2800 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2801 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2802 /* --verbose is only valid in migrate mode */
2803 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2804 { .val = 'x', .name = "xattr", .has_arg = required_argument },
2805 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2808 setstripe_args_init(&lsa);
2810 migrate_mode = (opc == SO_MIGRATE);
2811 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2812 setstripe_mode = (opc == SO_SETSTRIPE);
2814 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2816 while ((c = getopt_long(argc, argv,
2817 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:",
2818 long_opts, NULL)) >= 0) {
2823 case LFS_COMP_ADD_OPT:
2826 case LFS_COMP_DEL_OPT:
2829 case LFS_COMP_FLAGS_OPT:
2830 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2831 &lsa.lsa_comp_neg_flags);
2834 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2835 fprintf(stderr, "%s: inverted flags are not supported\n",
2839 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2841 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2848 case LFS_COMP_SET_OPT:
2851 case LFS_COMP_NO_VERIFY_OPT:
2852 mirror_flags |= MF_NO_VERIFY;
2854 case LFS_MIRROR_ID_OPT:
2855 mirror_id = strtoul(optarg, &end, 0);
2856 if (*end != '\0' || mirror_id == 0) {
2858 "%s %s: invalid mirror ID '%s'\n",
2859 progname, argv[0], optarg);
2863 case LFS_LAYOUT_FLAGS_OPT: {
2866 /* check for numeric flags (foreign and mirror cases) */
2867 if (setstripe_mode && !mirror_mode && !last_mirror) {
2868 flags = strtoul(optarg, &end, 16);
2871 "%s %s: bad flags '%s'\n",
2872 progname, argv[0], optarg);
2878 if (!mirror_mode || !last_mirror) {
2879 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2884 result = comp_str2flags(optarg, &last_mirror->m_flags,
2890 fprintf(stderr, "%s: inverted flags are not supported\n",
2895 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2897 "%s: unsupported mirror flags: %s\n",
2904 case LFS_LAYOUT_FOREIGN_OPT:
2905 if (optarg != NULL) {
2906 /* check pure numeric */
2907 type = strtoul(optarg, &end, 0);
2910 type = check_foreign_type_name(optarg);
2911 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
2913 "%s %s: unrecognized foreign type '%s'\n",
2920 foreign_mode = true;
2924 if (mode_opt != NULL) {
2925 mode = strtoul(mode_opt, &end, 8);
2928 "%s %s: bad mode '%s'\n",
2929 progname, argv[0], mode_opt);
2932 previous_umask = umask(0);
2935 case LFS_LAYOUT_COPY:
2940 if (!migrate_mode) {
2942 "%s %s: -b|--block valid only for migrate command\n",
2946 migration_block = true;
2949 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
2952 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2955 "%s %s: invalid stripe count '%s'\n",
2956 progname, argv[0], optarg);
2960 if (lsa.lsa_stripe_count == -1)
2961 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2964 /* delete the default striping pattern */
2966 if (opc == SO_MIRROR_SPLIT) {
2969 "%s %s: -d cannot used with -f\n",
2973 mirror_flags |= MF_DESTROY;
2977 if (!migrate_mode) {
2979 "%s %s: -D|--non-direct is valid "
2980 "only for migrate command\n",
2984 migration_flags |= MIGRATION_NONDIRECT;
2987 if (lsa.lsa_comp_end != 0) {
2988 result = comp_args_to_layout(lpp, &lsa, true);
2991 "%s %s: invalid layout\n",
2996 setstripe_args_init_inherit(&lsa);
2999 if (arg_is_eof(optarg)) {
3000 lsa.lsa_comp_end = LUSTRE_EOF;
3002 result = llapi_parse_size(optarg,
3007 "%s %s: invalid component end '%s'\n",
3008 progname, argv[0], optarg);
3014 if (!migrate_mode) {
3015 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3019 lsa.lsa_pattern = check_hashtype(optarg);
3020 if (lsa.lsa_pattern == 0) {
3022 "%s %s: bad stripe hash type '%s'\n",
3023 progname, argv[0], optarg);
3028 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3031 "%s %s: invalid stripe offset '%s'\n",
3032 progname, argv[0], optarg);
3035 if (lsa.lsa_stripe_off == -1)
3036 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3039 comp_id = strtoul(optarg, &end, 0);
3040 if (*end != '\0' || comp_id == 0 ||
3041 comp_id > LCME_ID_MAX) {
3043 "%s %s: invalid component ID '%s'\n",
3044 progname, argv[0], optarg);
3049 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3051 "error: %s: invalid option: %s\n",
3052 progname, argv[optopt + 1]);
3055 if (opc == SO_MIRROR_EXTEND) {
3056 if (last_mirror == NULL) {
3058 "error: %s: '-N' must exist in front of '%s'\n",
3059 progname, argv[optopt + 1]);
3062 last_mirror->m_file = optarg;
3063 last_mirror->m_count = 1;
3066 if (mirror_list == NULL)
3067 mirror_list = lfs_mirror_alloc();
3068 mirror_list->m_file = optarg;
3073 if (strcmp(argv[optind - 1], "mdt") == 0) {
3074 /* Can be only the first component */
3075 if (layout != NULL) {
3077 fprintf(stderr, "error: 'mdt' layout "
3078 "can be only the first one\n");
3081 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3083 fprintf(stderr, "error: 'mdt' layout "
3084 "size is too big\n");
3087 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3088 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3090 fprintf(stderr, "error: layout '%s' is "
3091 "unknown, supported layouts are: "
3092 "'mdt', 'raid0'\n", argv[optind]);
3097 if (!migrate_mode) {
3099 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3103 migrate_mdt_mode = true;
3104 lsa.lsa_nr_tgts = parse_targets(tgts,
3105 sizeof(tgts) / sizeof(__u32),
3106 lsa.lsa_nr_tgts, optarg, NULL);
3107 if (lsa.lsa_nr_tgts < 0) {
3109 "%s %s: invalid MDT target(s) '%s'\n",
3110 progname, argv[0], optarg);
3114 lsa.lsa_tgts = tgts;
3115 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3116 lsa.lsa_stripe_off = tgts[0];
3119 if (!migrate_mode) {
3121 "%s %s: -n|--non-block valid only for migrate command\n",
3125 migration_flags |= MIGRATION_NONBLOCK;
3128 if (opc == SO_SETSTRIPE) {
3129 opc = SO_MIRROR_CREATE;
3133 if (optarg != NULL) {
3134 mirror_count = strtoul(optarg, &end, 0);
3135 if (*end != '\0' || mirror_count == 0) {
3137 "error: %s: bad mirror count: %s\n",
3144 new_mirror = lfs_mirror_alloc();
3145 new_mirror->m_count = mirror_count;
3147 if (mirror_list == NULL)
3148 mirror_list = new_mirror;
3150 if (last_mirror != NULL) {
3151 /* wrap up last mirror */
3152 if (lsa.lsa_comp_end == 0)
3153 lsa.lsa_comp_end = LUSTRE_EOF;
3155 result = comp_args_to_layout(lpp, &lsa, true);
3157 lfs_mirror_free(new_mirror);
3161 setstripe_args_init_inherit(&lsa);
3163 last_mirror->m_next = new_mirror;
3166 last_mirror = new_mirror;
3167 lpp = &last_mirror->m_layout;
3170 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3171 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3172 fprintf(stderr, "warning: '--ost-list' is "
3173 "deprecated, use '--ost' instead\n");
3175 /* -o allows overstriping, and must note it because
3176 * parse_targets is shared with MDT striping, which
3177 * does not allow duplicates
3179 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3180 lsa.lsa_nr_tgts = parse_targets(tgts,
3181 sizeof(tgts) / sizeof(__u32),
3182 lsa.lsa_nr_tgts, optarg,
3184 if (lsa.lsa_nr_tgts < 0) {
3186 "%s %s: invalid OST target(s) '%s'\n",
3187 progname, argv[0], optarg);
3191 lsa.lsa_tgts = tgts;
3192 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3193 lsa.lsa_stripe_off = tgts[0];
3198 lsa.lsa_pool_name = optarg;
3200 if (strlen(lsa.lsa_pool_name) == 0 ||
3201 strncmp(lsa.lsa_pool_name, "none",
3202 LOV_MAXPOOLNAME) == 0)
3203 lsa.lsa_pool_name = NULL;
3206 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3210 "%s %s: invalid stripe size '%s'\n",
3211 progname, argv[0], optarg);
3216 if (!migrate_mode) {
3218 "%s %s: -v|--verbose valid only for migrate command\n",
3222 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3223 migration_flags = MIGRATION_VERBOSE;
3233 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3234 progname, argv[0], argv[optind - 1]);
3239 fname = argv[optind];
3241 if (optind == argc) {
3242 fprintf(stderr, "%s %s: FILE must be specified\n",
3247 if (xattr && !foreign_mode) {
3248 /* only print a warning as this is harmless and will be ignored
3251 "%s %s: xattr has been specified for non-foreign layout\n",
3253 } else if (foreign_mode && !xattr) {
3255 "%s %s: xattr must be provided in foreign mode\n",
3260 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3261 comp_set || comp_id || delete || from_copy ||
3262 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3265 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3270 if (mirror_mode && mirror_count == 0) {
3272 "error: %s: --mirror-count|-N option is required\n",
3279 if (lsa.lsa_comp_end == 0)
3280 lsa.lsa_comp_end = LUSTRE_EOF;
3283 if (lsa.lsa_comp_end != 0) {
3284 result = comp_args_to_layout(lpp, &lsa, true);
3289 if (mirror_flags & MF_NO_VERIFY) {
3290 if (opc != SO_MIRROR_EXTEND) {
3292 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3296 } else if (!has_m_file) {
3298 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3305 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3306 * altered by user space tool, so we don't need to support the
3307 * --component-set for this moment. */
3308 if (comp_set && !comp_id) {
3309 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3314 if ((delete + comp_set + comp_del + comp_add) > 1) {
3316 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3321 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3322 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3324 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3329 if ((comp_set || comp_del) &&
3330 (setstripe_args_specified(&lsa) || layout != NULL)) {
3332 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3337 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3339 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3344 if (comp_add || comp_del) {
3347 result = lstat(fname, &st);
3348 if (result == 0 && S_ISDIR(st.st_mode)) {
3350 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3356 fprintf(stderr, "error: %s: can't use --component-add "
3357 "or --component-del for mirror operation\n",
3364 if (layout == NULL) {
3366 "%s %s: option -E must be specified with --component-add\n",
3371 result = adjust_first_extent(fname, layout);
3372 if (result == -ENODATA)
3374 else if (result != 0)
3378 if (from_yaml && from_copy) {
3380 "%s: can't specify --yaml and --copy together\n",
3385 if ((from_yaml || from_copy) &&
3386 (setstripe_args_specified(&lsa) || layout != NULL)) {
3387 fprintf(stderr, "error: %s: can't specify --yaml with "
3388 "-c, -S, -i, -o, -p or -E options.\n",
3393 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3395 "%s %s: options --non-block and --block are mutually exclusive\n",
3400 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3403 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3408 if (migrate_mdt_mode) {
3409 struct lmv_user_md *lmu;
3411 /* initialize migrate mdt parameters */
3412 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3413 LMV_USER_MAGIC_SPECIFIC));
3416 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3417 progname, argv[0], strerror(ENOMEM));
3421 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3422 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3423 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3425 "%s %s: migrate should specify MDT index\n",
3430 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3431 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3432 lmu->lum_hash_type = lsa.lsa_pattern;
3434 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3435 if (lsa.lsa_pool_name)
3436 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3437 sizeof(lmu->lum_pool_name) - 1);
3438 if (lsa.lsa_nr_tgts > 1) {
3441 if (lsa.lsa_stripe_count > 0 &&
3442 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3443 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3445 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3446 progname, lsa.lsa_stripe_count,
3452 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3453 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3454 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3455 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3457 lmu->lum_magic = LMV_USER_MAGIC;
3460 migrate_mdt_param.fp_lmv_md = lmu;
3461 migrate_mdt_param.fp_migrate = 1;
3462 } else if (layout == NULL) {
3463 /* initialize stripe parameters */
3464 param = calloc(1, offsetof(typeof(*param),
3465 lsp_osts[lsa.lsa_nr_tgts]));
3466 if (param == NULL) {
3468 "%s %s: cannot allocate memory for parameters: %s\n",
3469 progname, argv[0], strerror(ENOMEM));
3474 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3475 param->lsp_stripe_size = lsa.lsa_stripe_size;
3476 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3477 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3478 param->lsp_stripe_count = -1;
3480 param->lsp_stripe_count = lsa.lsa_stripe_count;
3482 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3483 param->lsp_stripe_offset = -1;
3485 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3486 param->lsp_stripe_pattern =
3487 llapi_pattern_to_lov(lsa.lsa_pattern);
3488 if (param->lsp_stripe_pattern == EINVAL) {
3489 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3494 param->lsp_pool = lsa.lsa_pool_name;
3495 param->lsp_is_specific = false;
3496 if (lsa.lsa_nr_tgts > 0) {
3497 if (lsa.lsa_stripe_count > 0 &&
3498 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3499 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3500 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3501 fprintf(stderr, "error: %s: stripe count %lld "
3502 "doesn't match the number of OSTs: %d\n"
3503 , argv[0], lsa.lsa_stripe_count,
3509 param->lsp_is_specific = true;
3510 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3511 memcpy(param->lsp_osts, tgts,
3512 sizeof(*tgts) * lsa.lsa_nr_tgts);
3517 /* generate a layout from a YAML template */
3518 result = lfs_comp_create_from_yaml(template, &layout,
3521 fprintf(stderr, "error: %s: can't create composite "
3522 "layout from template file %s\n",
3526 } else if (from_copy) {
3527 layout = llapi_layout_get_by_path(template, 0);
3528 if (layout == NULL) {
3530 "%s: can't create composite layout from file %s.\n",
3531 progname, template);
3536 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3537 if (migrate_mdt_mode) {
3538 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3539 } else if (migrate_mode) {
3540 result = lfs_migrate(fname, migration_flags, param,
3542 } else if (comp_set != 0) {
3543 result = lfs_component_set(fname, comp_id,
3545 lsa.lsa_comp_neg_flags);
3546 } else if (comp_del != 0) {
3547 result = lfs_component_del(fname, comp_id,
3549 lsa.lsa_comp_neg_flags);
3550 } else if (comp_add != 0) {
3551 result = lfs_component_add(fname, layout);
3552 } else if (opc == SO_MIRROR_CREATE) {
3553 result = mirror_create(fname, mirror_list);
3554 } else if (opc == SO_MIRROR_EXTEND) {
3555 result = mirror_extend(fname, mirror_list,
3557 } else if (opc == SO_MIRROR_SPLIT) {
3558 if (mirror_id == 0 && comp_id == 0) {
3560 "%s %s: no mirror id or component id is specified\n",
3565 comp_id = mirror_id;
3567 mirror_flags |= MF_COMP_ID;
3568 result = mirror_split(fname, comp_id, mirror_flags,
3569 has_m_file ? mirror_list->m_file :
3571 } else if (layout != NULL) {
3572 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3578 } else if (foreign_mode) {
3579 result = llapi_file_create_foreign(fname, mode, type,
3586 result = llapi_file_open_param(fname,
3595 /* Save the first error encountered. */
3602 if (mode_opt != NULL)
3603 umask(previous_umask);
3606 free(migrate_mdt_param.fp_lmv_md);
3607 llapi_layout_free(layout);
3608 lfs_mirror_list_free(mirror_list);
3613 llapi_layout_free(layout);
3614 lfs_mirror_list_free(mirror_list);
3618 static int lfs_poollist(int argc, char **argv)
3623 return llapi_poollist(argv[1]);
3626 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3631 char *endptr = "AD";
3636 else if (str[0] == '-')
3642 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3643 long long val = strtoll(timebuf, &endptr, 0);
3648 unit *= 52; /* 52 weeks + 1 day below */
3649 case 'w': /* fallthrough */
3651 case '\0': /* days are default unit if none used */
3652 case 'd': /* fallthrough */
3653 unit = (unit + (*endptr == 'y')) * 24;
3654 case 'h': /* fallthrough */
3656 case 'm': /* fallthrough */
3658 case 's': /* fallthrough */
3660 /* don't need to multiply by 1 for seconds */
3663 "%s find: bad time string '%s': %s\n",
3664 progname, timebuf, strerror(EINVAL));
3668 if (param->fp_time_margin == 0 ||
3669 (*endptr && unit < param->fp_time_margin))
3670 param->fp_time_margin = unit;
3677 fprintf(stderr, "%s find: bad time '%s': too large\n",
3687 static int name2uid(unsigned int *id, const char *name)
3689 struct passwd *passwd;
3691 passwd = getpwnam(name);
3694 *id = passwd->pw_uid;
3699 static int name2gid(unsigned int *id, const char *name)
3701 struct group *group;
3703 group = getgrnam(name);
3706 *id = group->gr_gid;
3711 static inline int name2projid(unsigned int *id, const char *name)
3716 static int uid2name(char **name, unsigned int id)
3718 struct passwd *passwd;
3720 passwd = getpwuid(id);
3723 *name = passwd->pw_name;
3728 static inline int gid2name(char **name, unsigned int id)
3730 struct group *group;
3732 group = getgrgid(id);
3735 *name = group->gr_name;
3740 static int name2layout(__u32 *layout, char *name)
3742 char *ptr, *layout_name;
3745 for (ptr = name; ; ptr = NULL) {
3746 layout_name = strtok(ptr, ",");
3747 if (layout_name == NULL)
3749 if (strcmp(layout_name, "released") == 0)
3750 *layout |= LOV_PATTERN_F_RELEASED;
3751 else if (strcmp(layout_name, "raid0") == 0)
3752 *layout |= LOV_PATTERN_RAID0;
3753 else if (strcmp(layout_name, "mdt") == 0)
3754 *layout |= LOV_PATTERN_MDT;
3755 else if (strcmp(layout_name, "overstriping") == 0)
3756 *layout |= LOV_PATTERN_OVERSTRIPING;
3763 static int lfs_find(int argc, char **argv)
3768 struct find_param param = {
3771 .fp_time_margin = 24 * 60 * 60,
3773 struct option long_opts[] = {
3774 { .val = 'A', .name = "atime", .has_arg = required_argument },
3775 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3776 { .val = LFS_COMP_COUNT_OPT,
3777 .name = "comp-count", .has_arg = required_argument },
3778 { .val = LFS_COMP_COUNT_OPT,
3779 .name = "component-count",
3780 .has_arg = required_argument },
3781 { .val = LFS_COMP_FLAGS_OPT,
3782 .name = "comp-flags", .has_arg = required_argument },
3783 { .val = LFS_COMP_FLAGS_OPT,
3784 .name = "component-flags",
3785 .has_arg = required_argument },
3786 { .val = LFS_COMP_START_OPT,
3787 .name = "comp-start", .has_arg = required_argument },
3788 { .val = LFS_COMP_START_OPT,
3789 .name = "component-start",
3790 .has_arg = required_argument },
3791 { .val = LFS_MIRROR_STATE_OPT,
3792 .name = "mirror-state", .has_arg = required_argument },
3793 { .val = LFS_LAYOUT_FOREIGN_OPT,
3794 .name = "foreign", .has_arg = optional_argument},
3795 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3796 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3797 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3798 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3799 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3800 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3801 { .val = 'E', .name = "component-end",
3802 .has_arg = required_argument },
3803 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3804 { .val = LFS_LAYOUT_FOREIGN_OPT,
3805 .name = "foreign", .has_arg = optional_argument},
3806 { .val = 'g', .name = "gid", .has_arg = required_argument },
3807 { .val = 'G', .name = "group", .has_arg = required_argument },
3808 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3809 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3810 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3811 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3812 { .val = 'L', .name = "layout", .has_arg = required_argument },
3813 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3814 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3815 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3816 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3817 { .val = 'n', .name = "name", .has_arg = required_argument },
3818 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3819 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3820 { .val = 'O', .name = "obd", .has_arg = required_argument },
3821 { .val = 'O', .name = "ost", .has_arg = required_argument },
3822 /* no short option for pool yet, can be 'p' after 2.18 */
3823 { .val = LFS_POOL_OPT,
3824 .name = "pool", .has_arg = required_argument },
3825 { .val = '0', .name = "print0", .has_arg = no_argument },
3826 { .val = 'P', .name = "print", .has_arg = no_argument },
3827 { .val = LFS_PROJID_OPT,
3828 .name = "projid", .has_arg = required_argument },
3829 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3830 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3831 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3832 { .val = 's', .name = "size", .has_arg = required_argument },
3833 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3834 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3835 { .val = 't', .name = "type", .has_arg = required_argument },
3836 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3837 { .val = 'u', .name = "uid", .has_arg = required_argument },
3838 { .val = 'U', .name = "user", .has_arg = required_argument },
3839 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3840 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3852 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3853 while ((c = getopt_long_only(argc, argv,
3854 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3855 long_opts, NULL)) >= 0) {
3860 /* '!' is part of option */
3861 /* when getopt_long_only() finds a string which is not
3862 * an option nor a known option argument it returns 1
3863 * in that case if we already have found pathstart and pathend
3864 * (i.e. we have the list of pathnames),
3865 * the only supported value is "!"
3867 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3868 if (!isoption && pathend != -1) {
3869 fprintf(stderr, "err: %s: filename|dirname must either "
3870 "precede options or follow options\n",
3875 if (!isoption && pathstart == -1)
3876 pathstart = optind - 1;
3877 if (isoption && pathstart != -1 && pathend == -1)
3878 pathend = optind - 2;
3884 /* unknown; opt is "!" or path component,
3885 * checking done above.
3887 if (strcmp(optarg, "!") == 0)
3891 xtime = ¶m.fp_atime;
3892 xsign = ¶m.fp_asign;
3893 param.fp_exclude_atime = !!neg_opt;
3894 /* no break, this falls through to 'C' for ctime */
3897 xtime = ¶m.fp_ctime;
3898 xsign = ¶m.fp_csign;
3899 param.fp_exclude_ctime = !!neg_opt;
3901 /* no break, this falls through to 'M' for mtime */
3904 xtime = ¶m.fp_mtime;
3905 xsign = ¶m.fp_msign;
3906 param.fp_exclude_mtime = !!neg_opt;
3908 rc = set_time(¶m, &t, xtime, optarg);
3909 if (rc == LONG_MAX) {
3917 if (optarg[0] == '+') {
3918 param.fp_blocks_sign = -1;
3920 } else if (optarg[0] == '-') {
3921 param.fp_blocks_sign = 1;
3925 param.fp_blocks_units = 1024;
3926 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3927 ¶m.fp_blocks_units, 0);
3929 fprintf(stderr, "error: bad blocks '%s'\n",
3933 param.fp_check_blocks = 1;
3934 param.fp_exclude_blocks = !!neg_opt;
3936 case LFS_COMP_COUNT_OPT:
3937 if (optarg[0] == '+') {
3938 param.fp_comp_count_sign = -1;
3940 } else if (optarg[0] == '-') {
3941 param.fp_comp_count_sign = 1;
3945 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3946 if (*endptr != '\0') {
3947 fprintf(stderr, "error: bad component count "
3951 param.fp_check_comp_count = 1;
3952 param.fp_exclude_comp_count = !!neg_opt;
3954 case LFS_COMP_FLAGS_OPT:
3955 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3956 ¶m.fp_comp_neg_flags);
3958 fprintf(stderr, "error: bad component flags "
3962 param.fp_check_comp_flags = 1;
3964 __u32 flags = param.fp_comp_neg_flags;
3965 param.fp_comp_neg_flags = param.fp_comp_flags;
3966 param.fp_comp_flags = flags;
3969 case LFS_COMP_START_OPT:
3970 if (optarg[0] == '+') {
3971 param.fp_comp_start_sign = -1;
3973 } else if (optarg[0] == '-') {
3974 param.fp_comp_start_sign = 1;
3978 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3979 ¶m.fp_comp_start_units, 0);
3981 fprintf(stderr, "error: bad component start "
3985 param.fp_check_comp_start = 1;
3986 param.fp_exclude_comp_start = !!neg_opt;
3988 case LFS_MIRROR_STATE_OPT:
3989 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3990 ¶m.fp_mirror_neg_state);
3993 "error: bad mirrored file state '%s'\n",
3997 param.fp_check_mirror_state = 1;
3999 __u16 state = param.fp_mirror_neg_state;
4000 param.fp_mirror_neg_state =
4001 param.fp_mirror_state;
4002 param.fp_mirror_state = state;
4006 if (optarg[0] == '+') {
4007 param.fp_stripe_count_sign = -1;
4009 } else if (optarg[0] == '-') {
4010 param.fp_stripe_count_sign = 1;
4014 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4015 if (*endptr != '\0') {
4016 fprintf(stderr,"error: bad stripe_count '%s'\n",
4021 param.fp_check_stripe_count = 1;
4022 param.fp_exclude_stripe_count = !!neg_opt;
4025 param.fp_max_depth = strtol(optarg, 0, 0);
4028 if (optarg[0] == '+') {
4029 param.fp_comp_end_sign = -1;
4031 } else if (optarg[0] == '-') {
4032 param.fp_comp_end_sign = 1;
4036 if (arg_is_eof(optarg)) {
4037 param.fp_comp_end = LUSTRE_EOF;
4038 param.fp_comp_end_units = 1;
4041 rc = llapi_parse_size(optarg,
4043 ¶m.fp_comp_end_units, 0);
4046 fprintf(stderr, "error: bad component end "
4050 param.fp_check_comp_end = 1;
4051 param.fp_exclude_comp_end = !!neg_opt;
4053 case LFS_LAYOUT_FOREIGN_OPT: {
4054 /* all types by default */
4055 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4057 if (optarg != NULL) {
4058 /* check pure numeric */
4059 type = strtoul(optarg, &endptr, 0);
4062 type = check_foreign_type_name(optarg);
4063 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4065 "%s %s: unknown foreign type '%s'\n",
4072 param.fp_foreign_type = type;
4073 param.fp_check_foreign = 1;
4074 param.fp_exclude_foreign = !!neg_opt;
4079 rc = name2gid(¶m.fp_gid, optarg);
4081 param.fp_gid = strtoul(optarg, &endptr, 10);
4082 if (*endptr != '\0') {
4083 fprintf(stderr, "Group/GID: %s cannot "
4084 "be found.\n", optarg);
4089 param.fp_exclude_gid = !!neg_opt;
4090 param.fp_check_gid = 1;
4093 param.fp_hash_type = check_hashtype(optarg);
4094 if (param.fp_hash_type == 0) {
4095 fprintf(stderr, "error: bad hash_type '%s'\n",
4100 param.fp_check_hash_type = 1;
4101 param.fp_exclude_hash_type = !!neg_opt;
4104 ret = name2layout(¶m.fp_layout, optarg);
4107 param.fp_exclude_layout = !!neg_opt;
4108 param.fp_check_layout = 1;
4112 rc = name2uid(¶m.fp_uid, optarg);
4114 param.fp_uid = strtoul(optarg, &endptr, 10);
4115 if (*endptr != '\0') {
4116 fprintf(stderr, "User/UID: %s cannot "
4117 "be found.\n", optarg);
4122 param.fp_exclude_uid = !!neg_opt;
4123 param.fp_check_uid = 1;
4126 param.fp_pattern = (char *)optarg;
4127 param.fp_exclude_pattern = !!neg_opt;
4130 if (optarg[0] == '+') {
4131 param.fp_mirror_count_sign = -1;
4133 } else if (optarg[0] == '-') {
4134 param.fp_mirror_count_sign = 1;
4138 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4139 if (*endptr != '\0') {
4141 "error: bad mirror count '%s'\n",
4145 param.fp_check_mirror_count = 1;
4146 param.fp_exclude_mirror_count = !!neg_opt;
4151 char *buf, *token, *next, *p;
4155 buf = strdup(optarg);
4161 param.fp_exclude_obd = !!neg_opt;
4164 while (token && *token) {
4165 token = strchr(token, ',');
4172 param.fp_exclude_mdt = !!neg_opt;
4173 param.fp_num_alloc_mdts += len;
4174 tmp = realloc(param.fp_mdt_uuid,
4175 param.fp_num_alloc_mdts *
4176 sizeof(*param.fp_mdt_uuid));
4182 param.fp_mdt_uuid = tmp;
4184 param.fp_exclude_obd = !!neg_opt;
4185 param.fp_num_alloc_obds += len;
4186 tmp = realloc(param.fp_obd_uuid,
4187 param.fp_num_alloc_obds *
4188 sizeof(*param.fp_obd_uuid));
4194 param.fp_obd_uuid = tmp;
4196 for (token = buf; token && *token; token = next) {
4197 struct obd_uuid *puuid;
4200 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4203 ¶m.fp_obd_uuid[param.fp_num_obds++];
4205 p = strchr(token, ',');
4212 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4217 strncpy(puuid->uuid, token,
4218 sizeof(puuid->uuid));
4225 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4229 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4231 "Pool name %s is too long (max %d)\n",
4232 optarg, LOV_MAXPOOLNAME);
4237 * We do check for empty pool because empty pool
4238 * is used to find V1 LOV attributes
4240 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4241 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4242 param.fp_exclude_pool = !!neg_opt;
4243 param.fp_check_pool = 1;
4245 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4246 case 'p': /* want this for --pool, to match getstripe/find */
4248 "warning: -p deprecated, use --print0 or -0\n");
4251 param.fp_zero_end = 1;
4253 case 'P': /* we always print, this option is a no-op */
4255 case LFS_PROJID_OPT:
4256 rc = name2projid(¶m.fp_projid, optarg);
4258 param.fp_projid = strtoul(optarg, &endptr, 10);
4259 if (*endptr != '\0') {
4261 "Invalid project ID: %s",
4267 param.fp_exclude_projid = !!neg_opt;
4268 param.fp_check_projid = 1;
4271 if (optarg[0] == '+') {
4272 param.fp_size_sign = -1;
4274 } else if (optarg[0] == '-') {
4275 param.fp_size_sign = 1;
4279 ret = llapi_parse_size(optarg, ¶m.fp_size,
4280 ¶m.fp_size_units, 0);
4282 fprintf(stderr, "error: bad file size '%s'\n",
4286 param.fp_check_size = 1;
4287 param.fp_exclude_size = !!neg_opt;
4290 if (optarg[0] == '+') {
4291 param.fp_stripe_size_sign = -1;
4293 } else if (optarg[0] == '-') {
4294 param.fp_stripe_size_sign = 1;
4298 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4299 ¶m.fp_stripe_size_units, 0);
4301 fprintf(stderr, "error: bad stripe_size '%s'\n",
4305 param.fp_check_stripe_size = 1;
4306 param.fp_exclude_stripe_size = !!neg_opt;
4309 param.fp_exclude_type = !!neg_opt;
4310 switch (optarg[0]) {
4312 param.fp_type = S_IFBLK;
4315 param.fp_type = S_IFCHR;
4318 param.fp_type = S_IFDIR;
4321 param.fp_type = S_IFREG;
4324 param.fp_type = S_IFLNK;
4327 param.fp_type = S_IFIFO;
4330 param.fp_type = S_IFSOCK;
4333 fprintf(stderr, "error: %s: bad type '%s'\n",
4340 if (optarg[0] == '+') {
4341 param.fp_mdt_count_sign = -1;
4343 } else if (optarg[0] == '-') {
4344 param.fp_mdt_count_sign = 1;
4348 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4349 if (*endptr != '\0') {
4350 fprintf(stderr, "error: bad mdt_count '%s'\n",
4355 param.fp_check_mdt_count = 1;
4356 param.fp_exclude_mdt_count = !!neg_opt;
4364 if (pathstart == -1) {
4365 fprintf(stderr, "error: %s: no filename|pathname\n",
4369 } else if (pathend == -1) {
4375 rc = llapi_find(argv[pathstart], ¶m);
4376 if (rc != 0 && ret == 0)
4378 } while (++pathstart < pathend);
4381 fprintf(stderr, "error: %s failed for %s.\n",
4382 argv[0], argv[optind - 1]);
4384 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4385 free(param.fp_obd_uuid);
4387 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4388 free(param.fp_mdt_uuid);
4393 static int lfs_getstripe_internal(int argc, char **argv,
4394 struct find_param *param)
4396 struct option long_opts[] = {
4397 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4398 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4399 { .val = LFS_COMP_COUNT_OPT,
4400 .name = "comp-count", .has_arg = no_argument },
4401 { .val = LFS_COMP_COUNT_OPT,
4402 .name = "component-count", .has_arg = no_argument },
4403 { .val = LFS_COMP_FLAGS_OPT,
4404 .name = "comp-flags", .has_arg = optional_argument },
4405 { .val = LFS_COMP_FLAGS_OPT,
4406 .name = "component-flags", .has_arg = optional_argument },
4407 { .val = LFS_COMP_START_OPT,
4408 .name = "comp-start", .has_arg = optional_argument },
4409 { .val = LFS_COMP_START_OPT,
4410 .name = "component-start", .has_arg = optional_argument },
4411 { .val = LFS_MIRROR_INDEX_OPT,
4412 .name = "mirror-index", .has_arg = required_argument },
4413 { .val = LFS_MIRROR_ID_OPT,
4414 .name = "mirror-id", .has_arg = required_argument },
4415 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4416 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4417 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4418 { .val = 'd', .name = "directory", .has_arg = no_argument },
4419 { .val = 'D', .name = "default", .has_arg = no_argument },
4420 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4421 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4422 { .val = 'F', .name = "fid", .has_arg = no_argument },
4423 { .val = 'g', .name = "generation", .has_arg = no_argument },
4424 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4425 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4426 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4427 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4428 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4429 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4430 { .val = 'L', .name = "layout", .has_arg = no_argument },
4431 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4432 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4433 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4434 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4435 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4436 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4437 { .val = 'O', .name = "obd", .has_arg = required_argument },
4438 { .val = 'O', .name = "ost", .has_arg = required_argument },
4439 { .val = 'p', .name = "pool", .has_arg = no_argument },
4440 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4441 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4442 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4443 { .val = 'R', .name = "raw", .has_arg = no_argument },
4444 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4445 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4446 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4447 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4448 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4449 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4450 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4451 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4455 int pathstart = -1, pathend = -1;
4459 while ((c = getopt_long(argc, argv,
4460 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4461 long_opts, NULL)) != -1) {
4465 /* '!' is part of option */
4466 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4467 if (!isoption && pathend != -1) {
4469 "error: %s: filename|dirname must either precede options or follow options\n",
4473 if (!isoption && pathstart == -1)
4474 pathstart = optind - 1;
4475 if (isoption && pathstart != -1 && pathend == -1)
4476 pathend = optind - 2;
4480 /* unknown: opt is "!" */
4481 if (strcmp(optarg, "!") == 0)
4485 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4486 param->fp_verbose |= VERBOSE_COUNT;
4487 param->fp_max_depth = 0;
4490 case LFS_COMP_COUNT_OPT:
4491 param->fp_verbose |= VERBOSE_COMP_COUNT;
4492 param->fp_max_depth = 0;
4494 case LFS_COMP_FLAGS_OPT:
4495 if (optarg != NULL) {
4496 rc = comp_str2flags(optarg,
4497 ¶m->fp_comp_flags,
4498 ¶m->fp_comp_neg_flags);
4500 fprintf(stderr, "error: %s bad "
4501 "component flags '%s'.\n",
4505 param->fp_check_comp_flags = 1;
4507 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4508 param->fp_max_depth = 0;
4511 case LFS_COMP_START_OPT:
4512 if (optarg != NULL) {
4514 if (tmp[0] == '+') {
4515 param->fp_comp_start_sign = -1;
4517 } else if (tmp[0] == '-') {
4518 param->fp_comp_start_sign = 1;
4521 rc = llapi_parse_size(tmp,
4522 ¶m->fp_comp_start,
4523 ¶m->fp_comp_start_units, 0);
4525 fprintf(stderr, "error: %s bad "
4526 "component start '%s'.\n",
4530 param->fp_check_comp_start = 1;
4533 param->fp_verbose |= VERBOSE_COMP_START;
4534 param->fp_max_depth = 0;
4537 case LFS_MIRROR_INDEX_OPT:
4538 if (optarg[0] == '+') {
4539 param->fp_mirror_index_sign = -1;
4541 } else if (optarg[0] == '-') {
4542 param->fp_mirror_index_sign = 1;
4546 param->fp_mirror_index = strtoul(optarg, &end, 0);
4547 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4548 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4550 "%s %s: invalid mirror index '%s'\n",
4551 progname, argv[0], optarg);
4554 if (param->fp_mirror_id != 0) {
4556 "%s %s: can't specify both mirror index and mirror ID\n",
4560 param->fp_check_mirror_index = 1;
4561 param->fp_exclude_mirror_index = !!neg_opt;
4563 case LFS_MIRROR_ID_OPT:
4564 if (optarg[0] == '+') {
4565 param->fp_mirror_id_sign = -1;
4567 } else if (optarg[0] == '-') {
4568 param->fp_mirror_id_sign = 1;
4572 param->fp_mirror_id = strtoul(optarg, &end, 0);
4573 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4574 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4576 "%s %s: invalid mirror ID '%s'\n",
4577 progname, argv[0], optarg);
4580 if (param->fp_mirror_index != 0) {
4582 "%s %s: can't specify both mirror index and mirror ID\n",
4586 param->fp_check_mirror_id = 1;
4587 param->fp_exclude_mirror_id = !!neg_opt;
4590 param->fp_max_depth = 0;
4593 param->fp_get_default_lmv = 1;
4596 if (optarg != NULL) {
4598 if (tmp[0] == '+') {
4599 param->fp_comp_end_sign = -1;
4601 } else if (tmp[0] == '-') {
4602 param->fp_comp_end_sign = 1;
4606 if (arg_is_eof(tmp)) {
4607 param->fp_comp_end = LUSTRE_EOF;
4608 param->fp_comp_end_units = 1;
4611 rc = llapi_parse_size(tmp,
4612 ¶m->fp_comp_end,
4613 ¶m->fp_comp_end_units, 0);
4616 fprintf(stderr, "error: %s bad "
4617 "component end '%s'.\n",
4621 param->fp_check_comp_end = 1;
4623 param->fp_verbose |= VERBOSE_COMP_END;
4624 param->fp_max_depth = 0;
4628 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4629 param->fp_verbose |= VERBOSE_DFID;
4630 param->fp_max_depth = 0;
4634 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4635 param->fp_verbose |= VERBOSE_GENERATION;
4636 param->fp_max_depth = 0;
4640 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4641 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4642 param->fp_max_depth = 0;
4646 if (optarg != NULL) {
4647 param->fp_comp_id = strtoul(optarg, &end, 0);
4648 if (*end != '\0' || param->fp_comp_id == 0 ||
4649 param->fp_comp_id > LCME_ID_MAX) {
4650 fprintf(stderr, "error: %s bad "
4651 "component id '%s'\n",
4655 param->fp_check_comp_id = 1;
4658 param->fp_max_depth = 0;
4659 param->fp_verbose |= VERBOSE_COMP_ID;
4663 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4664 param->fp_verbose |= VERBOSE_PATTERN;
4665 param->fp_max_depth = 0;
4668 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4670 fprintf(stderr, "warning: '-M' deprecated"
4671 ", use '--mdt-index' or '-m' instead\n");
4674 if (!(param->fp_verbose & VERBOSE_DETAIL))
4675 param->fp_max_depth = 0;
4676 param->fp_verbose |= VERBOSE_MDTINDEX;
4679 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4680 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4681 param->fp_max_depth = 0;
4685 if (param->fp_obd_uuid) {
4687 "error: %s: only one obduuid allowed",
4691 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4694 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4695 param->fp_verbose |= VERBOSE_POOL;
4696 param->fp_max_depth = 0;
4703 param->fp_recursive = 1;
4709 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4710 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4711 param->fp_max_depth = 0;
4715 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4725 if (pathstart == -1) {
4726 fprintf(stderr, "error: %s: no filename|pathname\n",
4729 } else if (pathend == -1) {
4737 if (param->fp_recursive)
4738 param->fp_max_depth = -1;
4739 else if (param->fp_verbose & VERBOSE_DETAIL)
4740 param->fp_max_depth = 1;
4742 if (!param->fp_verbose)
4743 param->fp_verbose = VERBOSE_DEFAULT;
4744 if (param->fp_quiet)
4745 param->fp_verbose = VERBOSE_OBJID;
4748 rc = llapi_getstripe(argv[pathstart], param);
4749 } while (++pathstart < pathend && !rc);
4752 fprintf(stderr, "error: %s failed for %s.\n",
4753 argv[0], argv[optind - 1]);
4757 static int lfs_tgts(int argc, char **argv)
4759 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4760 struct find_param param;
4761 int index = 0, rc=0;
4766 if (argc == 2 && !realpath(argv[1], path)) {
4768 fprintf(stderr, "error: invalid path '%s': %s\n",
4769 argv[1], strerror(-rc));
4773 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4774 /* Check if we have a mount point */
4775 if (mntdir[0] == '\0')
4778 memset(¶m, 0, sizeof(param));
4779 if (!strcmp(argv[0], "mdts"))
4780 param.fp_get_lmv = 1;
4782 rc = llapi_ostlist(mntdir, ¶m);
4784 fprintf(stderr, "error: %s: failed on %s\n",
4787 if (path[0] != '\0')
4789 memset(mntdir, 0, PATH_MAX);
4795 static int lfs_getstripe(int argc, char **argv)
4797 struct find_param param = { 0 };
4799 param.fp_max_depth = 1;
4800 return lfs_getstripe_internal(argc, argv, ¶m);
4804 static int lfs_getdirstripe(int argc, char **argv)
4806 struct find_param param = { 0 };
4807 struct option long_opts[] = {
4808 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4809 { .val = 'D', .name = "default", .has_arg = no_argument },
4810 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4811 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4812 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4813 { .val = 'O', .name = "obd", .has_arg = required_argument },
4814 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4815 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4816 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4817 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4821 param.fp_get_lmv = 1;
4823 while ((c = getopt_long(argc, argv,
4824 "cDHimO:rtTvy", long_opts, NULL)) != -1)
4829 param.fp_verbose |= VERBOSE_COUNT;
4832 param.fp_get_default_lmv = 1;
4836 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4838 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4840 fprintf(stderr, "warning: '-t' deprecated, "
4841 "use '--mdt-hash' or '-H' instead\n");
4844 param.fp_verbose |= VERBOSE_HASH_TYPE;
4847 if (param.fp_obd_uuid) {
4849 "error: %s: only one obduuid allowed",
4853 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4856 param.fp_recursive = 1;
4859 param.fp_verbose |= VERBOSE_DETAIL;
4865 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4866 progname, argv[0], argv[optind - 1]);
4874 if (param.fp_recursive)
4875 param.fp_max_depth = -1;
4877 if (!param.fp_verbose)
4878 param.fp_verbose = VERBOSE_DEFAULT;
4881 rc = llapi_getstripe(argv[optind], ¶m);
4882 } while (++optind < argc && !rc);
4885 fprintf(stderr, "error: %s failed for %s.\n",
4886 argv[0], argv[optind - 1]);
4891 MNTDF_INODES = 0x0001,
4892 MNTDF_COOKED = 0x0002,
4893 MNTDF_LAZY = 0x0004,
4894 MNTDF_VERBOSE = 0x0008,
4895 MNTDF_SHOW = 0x0010,
4898 #define COOK(value) \
4901 while (value > 1024) { \
4909 #define CDF "%11llu"
4910 #define HDF "%8.1f%c"
4914 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4916 double avail, used, ratio = 0;
4919 avail = st->os_ffree;
4920 used = st->os_files - st->os_ffree;
4922 avail = st->os_bavail;
4923 used = st->os_blocks - st->os_bfree;
4925 if (avail + used > 0)
4926 ratio = used / (used + avail) * 100;
4928 /* Round up to match df(1) usage percentage */
4929 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4932 /* This is only used to reflect various problem states for lfs df, so we only
4933 * translate the flags reflecting those states.
4935 static char obd_statfs_state_names[] = {
4936 [OS_STATE_DEGRADED] = 'D',
4937 [OS_STATE_READONLY] = 'R',
4938 [OS_STATE_NOPRECREATE] = 'N',
4939 [OS_STATE_ENOSPC] = 'S',
4940 [OS_STATE_ENOINO] = 'I',
4943 static char obd_statfs_state2char(int s)
4946 if (s > ARRAY_SIZE(obd_statfs_state_names)/sizeof(char) ||
4947 obd_statfs_state_names[s] == 0)
4950 return obd_statfs_state_names[s];
4953 static int showdf(char *mntdir, struct obd_statfs *stat,
4954 char *uuid, enum mntdf_flags flags,
4955 char *type, int index, int rc)
4957 long long avail, used, total;
4959 char *suffix = "KMGTPEZY";
4960 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4961 char tbuf[3 * sizeof(__u64)];
4962 char ubuf[3 * sizeof(__u64)];
4963 char abuf[3 * sizeof(__u64)];
4964 char rbuf[3 * sizeof(__u64)];
4971 if (flags & MNTDF_INODES) {
4972 avail = stat->os_ffree;
4973 used = stat->os_files - stat->os_ffree;
4974 total = stat->os_files;
4976 int shift = flags & MNTDF_COOKED ? 0 : 10;
4978 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4979 used = ((stat->os_blocks - stat->os_bfree) *
4980 stat->os_bsize) >> shift;
4981 total = (stat->os_blocks * stat->os_bsize) >> shift;
4984 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4986 if (flags & MNTDF_COOKED) {
4990 cook_val = (double)total;
4993 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4996 snprintf(tbuf, sizeof(tbuf), CDF, total);
4998 cook_val = (double)used;
5001 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5004 snprintf(ubuf, sizeof(ubuf), CDF, used);
5006 cook_val = (double)avail;
5009 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5012 snprintf(abuf, sizeof(abuf), CDF, avail);
5014 snprintf(tbuf, sizeof(tbuf), CDF, total);
5015 snprintf(ubuf, sizeof(tbuf), CDF, used);
5016 snprintf(abuf, sizeof(tbuf), CDF, avail);
5019 sprintf(rbuf, RDF, ratio);
5020 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5021 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5023 printf("[%s:%d]", type, index);
5025 if (stat->os_state) {
5030 for (i = 0, state = stat->os_state; state != 0; i++) {
5031 uint32_t mask = 1 << i;
5032 if (!(state & mask) || mask == OS_STATE_NONROT)
5034 printf("%c", obd_statfs_state2char(mask));
5042 printf(UUF": inactive device\n", uuid);
5045 printf(UUF": %s\n", uuid, strerror(-rc));
5052 struct ll_stat_type {
5057 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5059 struct ll_statfs_data {
5061 struct obd_statfs sd_st;
5064 struct ll_statfs_buf {
5066 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5069 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5070 int ops, struct ll_statfs_buf *lsb)
5072 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5073 struct obd_uuid uuid_buf;
5074 char *poolname = NULL;
5075 struct ll_stat_type types[] = {
5076 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5077 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5078 { .st_name = NULL } };
5079 struct ll_stat_type *tp;
5080 __u64 ost_files = 0;
5081 __u64 ost_ffree = 0;
5089 poolname = strchr(pool, '.');
5090 if (poolname != NULL) {
5091 if (strncmp(fsname, pool, strlen(fsname))) {
5092 fprintf(stderr, "filesystem name incorrect\n");
5100 fd = open(mntdir, O_RDONLY);
5103 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5108 if (flags & MNTDF_SHOW) {
5109 if (flags & MNTDF_INODES)
5110 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5111 "UUID", "Inodes", "IUsed", "IFree",
5112 "IUse%", "Mounted on");
5114 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5116 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5117 "Used", "Available", "Use%", "Mounted on");
5120 for (tp = types; tp->st_name != NULL; tp++) {
5121 bool have_ost = false;
5123 if (!(tp->st_op & ops))
5126 for (index = 0; ; index++) {
5127 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5128 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5129 type = flags & MNTDF_LAZY ?
5130 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5131 rc2 = llapi_obd_fstatfs(fd, type, index,
5132 &stat_buf, &uuid_buf);
5137 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5138 if (!(flags & MNTDF_VERBOSE))
5140 } else if (rc2 < 0 && rc == 0) {
5144 /* If we have OSTs then don't report MDT block counts.
5145 * For MDT-only filesystems the expectation is that all
5146 * layouts have a DoM component. For filesystems with
5147 * OSTs, files are not necessarily going to store data
5148 * on MDTs, and MDT space is limited to a fraction of
5149 * OST space, so don't include it in the summary.
5151 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5158 if (poolname && tp->st_op == LL_STATFS_LOV &&
5159 llapi_search_ost(fsname, poolname,
5160 obd_uuid2str(&uuid_buf)) != 1)
5163 /* the llapi_obd_statfs() call may have returned with
5164 * an error, but if it filled in uuid_buf we will at
5165 * lease use that to print out a message for that OBD.
5166 * If we didn't get anything in the uuid_buf, then fill
5167 * it in so that we can print an error message. */
5168 if (uuid_buf.uuid[0] == '\0')
5169 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5170 "%s%04x", tp->st_name, index);
5172 lsb->sb_buf[lsb->sb_count].sd_index = index;
5173 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5176 if (flags & MNTDF_SHOW)
5177 showdf(mntdir, &stat_buf,
5178 obd_uuid2str(&uuid_buf), flags,
5179 tp->st_name, index, rc2);
5184 if (tp->st_op == LL_STATFS_LMV) {
5185 sum.os_ffree += stat_buf.os_ffree;
5186 sum.os_files += stat_buf.os_files;
5187 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5188 ost_files += stat_buf.os_files;
5189 ost_ffree += stat_buf.os_ffree;
5191 sum.os_blocks += stat_buf.os_blocks *
5193 sum.os_bfree += stat_buf.os_bfree *
5195 sum.os_bavail += stat_buf.os_bavail *
5202 /* If we have _some_ OSTs, but don't have as many free objects on the
5203 * OST as inodes on the MDTs, reduce the reported number of inodes
5204 * to compensate, so that the "inodes in use" number is correct.
5205 * This should be kept in sync with ll_statfs_internal().
5207 if (ost_files && ost_ffree < sum.os_ffree) {
5208 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5209 sum.os_ffree = ost_ffree;
5211 if (flags & MNTDF_SHOW) {
5213 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5220 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5222 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5224 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5226 int r1 = obd_statfs_ratio(st1, false);
5227 int r2 = obd_statfs_ratio(st2, false);
5228 int64_t result = r1 - r2;
5230 /* if both space usage are above 90, compare free inodes */
5231 if (r1 > 90 && r2 > 90)
5232 result = st2->os_ffree - st1->os_ffree;
5236 else if (result == 0)
5243 static int lfs_setdirstripe(int argc, char **argv)
5246 struct lfs_setstripe_args lsa = { 0 };
5247 struct llapi_stripe_param *param = NULL;
5248 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5251 char *mode_opt = NULL;
5252 bool default_stripe = false;
5253 bool delete = false;
5254 bool auto_distributed = false;
5255 bool foreign_mode = false;
5256 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5257 mode_t previous_mode = 0;
5258 struct ll_statfs_buf *lsb = NULL;
5259 char mntdir[PATH_MAX] = "";
5261 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5262 struct option long_opts[] = {
5263 { .val = 'c', .name = "count", .has_arg = required_argument },
5264 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5265 { .val = 'd', .name = "delete", .has_arg = no_argument },
5266 { .val = 'D', .name = "default", .has_arg = no_argument },
5267 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5268 { .val = LFS_LAYOUT_FLAGS_OPT,
5269 .name = "flags", .has_arg = required_argument },
5270 { .val = LFS_LAYOUT_FOREIGN_OPT,
5271 .name = "foreign", .has_arg = optional_argument},
5272 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5273 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5274 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5275 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5277 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5278 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5280 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5281 { .val = 'i', .name = "index", .has_arg = required_argument },
5283 { .val = 'o', .name = "mode", .has_arg = required_argument },
5284 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5285 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5287 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5288 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5289 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5293 setstripe_args_init(&lsa);
5295 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5296 long_opts, NULL)) >= 0) {
5303 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5306 "%s %s: invalid stripe count '%s'\n",
5307 progname, argv[0], optarg);
5313 default_stripe = true;
5316 default_stripe = true;
5318 case LFS_LAYOUT_FOREIGN_OPT:
5319 if (optarg != NULL) {
5320 /* check pure numeric */
5321 type = strtoul(optarg, &end, 0);
5324 type = check_foreign_type_name(optarg);
5325 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5327 "%s %s: unknown foreign type '%s'\n",
5334 foreign_mode = true;
5336 case LFS_LAYOUT_FLAGS_OPT:
5337 flags = strtoul(optarg, &end, 16);
5340 "%s %s: bad flags '%s'\n",
5341 progname, argv[0], optarg);
5345 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5347 fprintf(stderr, "warning: '--hash-type' and '-t' "
5348 "deprecated, use '--mdt-hash' or '-H' instead\n");
5351 lsa.lsa_pattern = check_hashtype(optarg);
5352 if (lsa.lsa_pattern == 0) {
5354 "%s %s: bad stripe hash type '%s'\n",
5355 progname, argv[0], optarg);
5360 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5363 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5364 if (strcmp(argv[optind - 1], "--index") == 0)
5366 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5369 lsa.lsa_nr_tgts = parse_targets(mdts,
5370 sizeof(mdts) / sizeof(__u32),
5371 lsa.lsa_nr_tgts, optarg, NULL);
5372 if (lsa.lsa_nr_tgts < 0) {
5374 "%s %s: invalid MDT target(s) '%s'\n",
5375 progname, argv[0], optarg);
5379 lsa.lsa_tgts = mdts;
5380 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5381 lsa.lsa_stripe_off = mdts[0];
5383 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5385 fprintf(stderr, "warning: '-m' is deprecated, "
5386 "use '--mode' or '-o' instead\n");
5395 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5396 progname, argv[0], argv[optind - 1]);
5401 if (optind == argc) {
5402 fprintf(stderr, "%s %s: DIR must be specified\n",
5407 if (xattr && !foreign_mode) {
5408 /* only print a warning as this is armless and will be
5412 "%s %s: xattr has been specified for non-foreign layout\n",
5414 } else if (foreign_mode && !xattr) {
5416 "%s %s: xattr must be provided in foreign mode\n",
5421 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5422 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5424 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5429 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5430 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5432 "%s %s: stripe offset and count must be specified\n",
5438 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5439 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5441 "%s %s: cannot specify -d with -c or -i options\n",
5446 if (mode_opt != NULL) {
5447 mode = strtoul(mode_opt, &end, 8);
5450 "%s %s: bad MODE '%s'\n",
5451 progname, argv[0], mode_opt);
5454 previous_mode = umask(0);
5457 /* foreign LMV/dir case */
5459 if (argc > optind + 1) {
5461 "%s %s: cannot specify multiple foreign dirs\n",
5466 dname = argv[optind];
5467 result = llapi_dir_create_foreign(dname, mode, type, flags,
5471 "%s mkdir: can't create foreign dir '%s': %s\n",
5472 progname, dname, strerror(-result));
5477 * initialize stripe parameters, in case param is converted to specific,
5478 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5480 param = calloc(1, offsetof(typeof(*param),
5481 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5482 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5483 if (param == NULL) {
5485 "%s %s: cannot allocate memory for parameters: %s\n",
5486 progname, argv[0], strerror(ENOMEM));
5490 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5491 param->lsp_stripe_count = lsa.lsa_stripe_count;
5492 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5493 param->lsp_stripe_offset = -1;
5495 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5496 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5497 param->lsp_stripe_pattern = lsa.lsa_pattern;
5499 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5500 param->lsp_pool = lsa.lsa_pool_name;
5501 param->lsp_is_specific = false;
5502 if (lsa.lsa_nr_tgts > 1) {
5503 if (lsa.lsa_stripe_count > 0 &&
5504 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5505 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5506 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5507 "match the number of MDTs: %d\n",
5508 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5513 param->lsp_is_specific = true;
5514 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5515 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5518 if (!default_stripe && lsa.lsa_pattern == LMV_HASH_TYPE_SPACE) {
5519 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5525 dname = argv[optind];
5527 if (default_stripe) {
5528 result = llapi_dir_set_default_lmv(dname, param);
5531 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5532 progname, dname, strerror(-result));
5537 * if current \a dname isn't under the same \a mntdir as the
5538 * last one, and the last one was auto-distributed, restore
5541 if (mntdir[0] != '\0' &&
5542 strncmp(dname, mntdir, strlen(mntdir)) &&
5544 param->lsp_is_specific = false;
5545 param->lsp_stripe_offset = -1;
5546 auto_distributed = false;
5550 * TODO: when MDT can allocate object with QoS (LU-9435), below
5551 * code should be removed, instead we should let LMV to allocate
5552 * the starting MDT object, and then let LOD allocate other MDT
5555 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5556 char path[PATH_MAX] = "";
5559 lsb = malloc(sizeof(*lsb));
5567 /* use mntdir for dirname() temporarily */
5568 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5569 if (!realpath(dirname(mntdir), path)) {
5572 "error: invalid path '%s': %s\n",
5573 argv[optind], strerror(errno));
5578 result = llapi_search_mounts(path, 0, mntdir, NULL);
5579 if (result < 0 || mntdir[0] == '\0') {
5581 "No suitable Lustre mount found\n");
5585 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5590 if (param->lsp_stripe_count > lsb->sb_count) {
5592 "error: stripe count %d is too big\n",
5593 param->lsp_stripe_count);
5598 qsort(lsb->sb_buf, lsb->sb_count,
5599 sizeof(struct ll_statfs_data),
5600 ll_statfs_data_comp);
5602 auto_distributed = true;
5605 if (auto_distributed) {
5607 int nr = MAX(param->lsp_stripe_count,
5610 /* don't use server whose usage is above 90% */
5611 while (nr != param->lsp_stripe_count &&
5612 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5614 nr = MAX(param->lsp_stripe_count, nr / 2);
5616 /* get \a r between [0, nr) */
5619 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5620 if (param->lsp_stripe_count > 1) {
5623 param->lsp_is_specific = true;
5624 for (; i < param->lsp_stripe_count; i++)
5625 param->lsp_tgts[(i + r) % nr] =
5626 lsb->sb_buf[i].sd_index;
5630 result = llapi_dir_create(dname, mode, param);
5633 "%s setdirstripe: cannot create dir '%s': %s\n",
5634 progname, dname, strerror(-result));
5635 } while (!result && (dname = argv[++optind]));
5637 if (mode_opt != NULL)
5638 umask(previous_mode);
5646 static int lfs_rmentry(int argc, char **argv)
5653 fprintf(stderr, "error: %s: missing dirname\n",
5659 dname = argv[index];
5660 while (dname != NULL) {
5661 result = llapi_direntry_remove(dname);
5663 fprintf(stderr, "error: %s: remove dir entry '%s' "
5664 "failed\n", argv[0], dname);
5667 dname = argv[++index];
5672 static int lfs_mv(int argc, char **argv)
5674 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5675 struct find_param param = {
5682 struct option long_opts[] = {
5683 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5684 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5685 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5688 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5690 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5692 fprintf(stderr, "warning: '-M' deprecated"
5693 ", use '--mdt-index' or '-m' instead\n");
5696 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5698 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5704 param.fp_verbose = VERBOSE_DETAIL;
5707 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5708 progname, argv[optind - 1]);
5713 if (lmu.lum_stripe_offset == -1) {
5714 fprintf(stderr, "%s mv: MDT index must be specified\n",
5719 if (optind >= argc) {
5720 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5725 /* initialize migrate mdt parameters */
5726 param.fp_lmv_md = &lmu;
5727 param.fp_migrate = 1;
5728 rc = llapi_migrate_mdt(argv[optind], ¶m);
5730 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5731 progname, argv[optind], param.fp_mdt_index,
5736 static int lfs_osts(int argc, char **argv)
5738 return lfs_tgts(argc, argv);
5741 static int lfs_mdts(int argc, char **argv)
5743 return lfs_tgts(argc, argv);
5746 static int lfs_df(int argc, char **argv)
5748 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5749 enum mntdf_flags flags = MNTDF_SHOW;
5750 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5751 int c, rc = 0, index = 0;
5752 char fsname[PATH_MAX] = "", *pool_name = NULL;
5753 struct option long_opts[] = {
5754 { .val = 'h', .name = "human-readable",
5755 .has_arg = no_argument },
5756 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5757 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5758 { .val = 'p', .name = "pool", .has_arg = required_argument },
5759 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5762 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5765 flags |= MNTDF_COOKED;
5768 flags |= MNTDF_INODES;
5771 flags |= MNTDF_LAZY;
5777 flags |= MNTDF_VERBOSE;
5783 if (optind < argc && !realpath(argv[optind], path)) {
5785 fprintf(stderr, "error: invalid path '%s': %s\n",
5786 argv[optind], strerror(-rc));
5790 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5791 /* Check if we have a mount point */
5792 if (mntdir[0] == '\0')
5795 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5796 if (rc || path[0] != '\0')
5798 fsname[0] = '\0'; /* avoid matching in next loop */
5799 mntdir[0] = '\0'; /* avoid matching in next loop */
5805 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5806 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5810 /* llapi_search_mounts() fills "fsname", but that is not called if
5811 * explicit paths are specified on the command-line
5813 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5814 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5817 fprintf(stderr, "cannot get instance for '%s': %s\n",
5818 mntdir, strerror(-rc));
5822 if (!opt_instance) {
5823 /* print only the fsname name */
5824 buf = strchr(fsname, '-');
5828 } else if (!opt_fsname) {
5829 /* print only the instance name */
5830 buf = strchr(fsname, '-');
5836 } else if (opt_fsname) {
5837 /* print only the fsname */
5841 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5846 static int lfs_getname(int argc, char **argv)
5848 struct option long_opts[] = {
5849 { .val = 'h', .name = "help", .has_arg = no_argument },
5850 { .val = 'i', .name = "instance", .has_arg = no_argument },
5851 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5853 bool opt_instance = false, opt_fsname = false;
5854 char fsname[PATH_MAX] = "";
5857 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5860 opt_instance = true;
5871 /* If neither option is given, print both instance and fsname */
5872 if (!opt_instance && !opt_fsname)
5873 opt_instance = opt_fsname = true;
5875 if (optind == argc) { /* no paths specified, get all paths. */
5876 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5879 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5880 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5881 opt_instance, opt_fsname, true);
5884 path[0] = fsname[0] = mntdir[0] = '\0';
5886 } else { /* paths specified, only attempt to search these. */
5889 /* if only one path is given, print only requested info */
5890 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5892 for (; optind < argc; optind++) {
5893 rc2 = print_instance(argv[optind], fsname,
5894 sizeof(fsname), opt_instance,
5895 opt_fsname, opt_mntdir);
5905 static int lfs_check(int argc, char **argv)
5907 char mntdir[PATH_MAX] = {'\0'};
5916 fprintf(stderr, "%s check: server type must be specified\n",
5921 obd_types[0] = obd_type1;
5922 obd_types[1] = obd_type2;
5923 obd_types[2] = obd_type3;
5925 if (strcmp(argv[1], "osts") == 0) {
5926 strcpy(obd_types[0], "osc");
5927 } else if (strcmp(argv[1], "mdts") == 0 ||
5928 strcmp(argv[1], "mds") == 0) {
5929 strcpy(obd_types[0], "mdc");
5930 } else if (strcmp(argv[1], "mgts") == 0) {
5931 strcpy(obd_types[0], "mgc");
5932 } else if (strcmp(argv[1], "all") == 0 ||
5933 strcmp(argv[1], "servers") == 0) {
5935 strcpy(obd_types[0], "osc");
5936 strcpy(obd_types[1], "mdc");
5937 strcpy(obd_types[2], "mgc");
5939 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5944 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5945 if (rc < 0 || mntdir[0] == '\0') {
5947 "%s check: cannot find mounted Lustre filesystem: %s\n",
5948 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5952 rc = llapi_target_check(num_types, obd_types, mntdir);
5954 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5955 progname, argv[1], strerror(-rc));
5961 #ifdef HAVE_SYS_QUOTA_H
5962 #define ARG2INT(nr, str, msg) \
5965 nr = strtol(str, &endp, 0); \
5966 if (*endp != '\0') { \
5967 fprintf(stderr, "%s: bad %s '%s'\n", \
5968 progname, msg, str); \
5973 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5975 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5976 * returns the value or ULONG_MAX on integer overflow or incorrect format
5978 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5979 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5980 * 3. empty integer value is interpreted as 0
5982 static unsigned long str2sec(const char* timestr)
5984 const char spec[] = "smhdw";
5985 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5986 unsigned long val = 0;
5989 if (strpbrk(timestr, spec) == NULL) {
5990 /* no specifiers inside the time string,
5991 should treat it as an integer value */
5992 val = strtoul(timestr, &tail, 10);
5993 return *tail ? ULONG_MAX : val;
5996 /* format string is XXwXXdXXhXXmXXs */
6002 v = strtoul(timestr, &tail, 10);
6003 if (v == ULONG_MAX || *tail == '\0')
6004 /* value too large (ULONG_MAX or more)
6005 or missing specifier */
6008 ptr = strchr(spec, *tail);
6010 /* unknown specifier */
6015 /* check if product will overflow the type */
6016 if (!(v < ULONG_MAX / mult[ind]))
6019 ADD_OVERFLOW(val, mult[ind] * v);
6020 if (val == ULONG_MAX)
6032 #define ARG2ULL(nr, str, def_units) \
6034 unsigned long long limit, units = def_units; \
6037 rc = llapi_parse_size(str, &limit, &units, 1); \
6039 fprintf(stderr, "%s: invalid limit '%s'\n", \
6046 static inline int has_times_option(int argc, char **argv)
6050 for (i = 1; i < argc; i++)
6051 if (!strcmp(argv[i], "-t"))
6057 int lfs_setquota_times(int argc, char **argv)
6060 struct if_quotactl qctl;
6061 char *mnt, *obd_type = (char *)qctl.obd_type;
6062 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6063 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6064 struct option long_opts[] = {
6065 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6066 { .val = 'g', .name = "group", .has_arg = no_argument },
6067 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6068 { .val = 'p', .name = "projid", .has_arg = no_argument },
6069 { .val = 't', .name = "times", .has_arg = no_argument },
6070 { .val = 'u', .name = "user", .has_arg = no_argument },
6074 memset(&qctl, 0, sizeof(qctl));
6075 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6076 qctl.qc_type = ALLQUOTA;
6078 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6079 long_opts, NULL)) != -1) {
6090 if (qctl.qc_type != ALLQUOTA) {
6091 fprintf(stderr, "error: -u/g/p can't be used "
6092 "more than once\n");
6095 qctl.qc_type = qtype;
6098 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6099 fprintf(stderr, "error: bad block-grace: %s\n",
6103 dqb->dqb_valid |= QIF_BTIME;
6106 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6107 fprintf(stderr, "error: bad inode-grace: %s\n",
6111 dqb->dqb_valid |= QIF_ITIME;
6113 case 't': /* Yes, of course! */
6115 default: /* getopt prints error message for us when opterr != 0 */
6120 if (qctl.qc_type == ALLQUOTA) {
6121 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6125 if (optind != argc - 1) {
6126 fprintf(stderr, "error: unexpected parameters encountered\n");
6130 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6131 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6132 fprintf(stderr, "error: grace time is too large\n");
6137 rc = llapi_quotactl(mnt, &qctl);
6140 fprintf(stderr, "%s %s ", obd_type,
6141 obd_uuid2str(&qctl.obd_uuid));
6142 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6149 #define BSLIMIT (1 << 0)
6150 #define BHLIMIT (1 << 1)
6151 #define ISLIMIT (1 << 2)
6152 #define IHLIMIT (1 << 3)
6154 int lfs_setquota(int argc, char **argv)
6157 struct if_quotactl qctl;
6158 char *mnt, *obd_type = (char *)qctl.obd_type;
6159 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6160 struct option long_opts[] = {
6161 { .val = 'b', .name = "block-softlimit",
6162 .has_arg = required_argument },
6163 { .val = 'B', .name = "block-hardlimit",
6164 .has_arg = required_argument },
6165 { .val = 'd', .name = "default", .has_arg = no_argument },
6166 { .val = 'g', .name = "group", .has_arg = required_argument },
6167 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6168 { .val = 'i', .name = "inode-softlimit",
6169 .has_arg = required_argument },
6170 { .val = 'I', .name = "inode-hardlimit",
6171 .has_arg = required_argument },
6172 { .val = 'p', .name = "projid", .has_arg = required_argument },
6173 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6174 { .val = 'u', .name = "user", .has_arg = required_argument },
6175 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6177 unsigned limit_mask = 0;
6179 bool use_default = false;
6182 if (has_times_option(argc, argv))
6183 return lfs_setquota_times(argc, argv);
6185 memset(&qctl, 0, sizeof(qctl));
6186 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6187 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6188 * so it can be used as a marker that qc_type
6189 * isn't reinitialized from command line */
6191 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6192 long_opts, NULL)) != -1) {
6195 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6198 goto quota_type_def;
6201 rc = name2uid(&qctl.qc_id, optarg);
6204 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6207 goto quota_type_def;
6210 rc = name2gid(&qctl.qc_id, optarg);
6213 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6216 goto quota_type_def;
6219 rc = name2projid(&qctl.qc_id, optarg);
6222 qctl.qc_id = strtoul(optarg, &endptr, 10);
6223 if (*endptr != '\0') {
6224 fprintf(stderr, "%s setquota: invalid"
6225 " id '%s'\n", progname, optarg);
6230 if (qctl.qc_id == 0) {
6231 fprintf(stderr, "%s setquota: can't set quota"
6232 " for root usr/group/project.\n",
6238 if (qctl.qc_type != ALLQUOTA) {
6240 "%s setquota: only one of -u, -U, -g,"
6241 " -G, -p or -P may be specified\n",
6245 qctl.qc_type = qtype;
6248 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6252 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6253 dqb->dqb_bsoftlimit >>= 10;
6254 limit_mask |= BSLIMIT;
6255 if (dqb->dqb_bsoftlimit &&
6256 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6258 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6259 "See '%s help setquota' or Lustre manual for details\n",
6260 progname, dqb->dqb_bsoftlimit,
6264 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6265 dqb->dqb_bhardlimit >>= 10;
6266 limit_mask |= BHLIMIT;
6267 if (dqb->dqb_bhardlimit &&
6268 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6270 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6271 "See '%s help setquota' or Lustre manual for details\n",
6272 progname, dqb->dqb_bhardlimit,
6276 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6277 limit_mask |= ISLIMIT;
6278 if (dqb->dqb_isoftlimit &&
6279 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6281 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6282 "See '%s help setquota' or Lustre manual for details\n",
6283 progname, dqb->dqb_isoftlimit,
6287 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6288 limit_mask |= IHLIMIT;
6289 if (dqb->dqb_ihardlimit &&
6290 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6292 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6293 "See '%s help setquota' or Lustre manual for details\n",
6294 progname, dqb->dqb_ihardlimit,
6299 "%s setquota: unrecognized option '%s'\n",
6300 progname, argv[optind - 1]);
6305 if (qctl.qc_type == ALLQUOTA) {
6307 "%s setquota: either -u or -g must be specified\n",
6312 if (!use_default && limit_mask == 0) {
6314 "%s setquota: at least one limit must be specified\n",
6319 if (use_default && limit_mask != 0) {
6321 "%s setquota: limits should not be specified when"
6322 " using default quota\n",
6327 if (use_default && qctl.qc_id == 0) {
6329 "%s setquota: can not set default quota for root"
6330 " user/group/project\n",
6335 if (optind != argc - 1) {
6337 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6338 progname, argv[optind]);
6345 dqb->dqb_bhardlimit = 0;
6346 dqb->dqb_bsoftlimit = 0;
6347 dqb->dqb_ihardlimit = 0;
6348 dqb->dqb_isoftlimit = 0;
6351 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6352 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6353 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6354 /* sigh, we can't just set blimits/ilimits */
6355 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6356 .qc_type = qctl.qc_type,
6357 .qc_id = qctl.qc_id};
6359 rc = llapi_quotactl(mnt, &tmp_qctl);
6363 if (!(limit_mask & BHLIMIT))
6364 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6365 if (!(limit_mask & BSLIMIT))
6366 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6367 if (!(limit_mask & IHLIMIT))
6368 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6369 if (!(limit_mask & ISLIMIT))
6370 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6372 /* Keep grace times if we have got no softlimit arguments */
6373 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6374 dqb->dqb_valid |= QIF_BTIME;
6375 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6378 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6379 dqb->dqb_valid |= QIF_ITIME;
6380 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6384 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6385 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6387 rc = llapi_quotactl(mnt, &qctl);
6391 "%s setquota: cannot quotactl '%s' '%s': %s",
6393 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6400 /* Converts seconds value into format string
6401 * result is returned in buf
6403 * 1. result is in descenting order: 1w2d3h4m5s
6404 * 2. zero fields are not filled (except for p. 3): 5d1s
6405 * 3. zero seconds value is presented as "0s"
6407 static char * __sec2str(time_t seconds, char *buf)
6409 const char spec[] = "smhdw";
6410 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6415 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6416 c = seconds / mult[i];
6418 if (c > 0 || (i == 0 && buf == tail))
6419 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6427 static void sec2str(time_t seconds, char *buf, int rc)
6434 tail = __sec2str(seconds, tail);
6436 if (rc && tail - buf < 39) {
6442 static void diff2str(time_t seconds, char *buf, time_t now)
6448 if (seconds <= now) {
6449 strcpy(buf, "none");
6452 __sec2str(seconds - now, buf);
6455 static void print_quota_title(char *name, struct if_quotactl *qctl,
6456 bool human_readable, bool show_default)
6459 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6460 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6461 "Filesystem", "bquota", "blimit", "bgrace",
6462 "iquota", "ilimit", "igrace");
6464 printf("Disk quotas for %s %s (%cid %u):\n",
6465 qtype_name(qctl->qc_type), name,
6466 *qtype_name(qctl->qc_type), qctl->qc_id);
6467 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6468 "Filesystem", human_readable ? "used" : "kbytes",
6469 "quota", "limit", "grace",
6470 "files", "quota", "limit", "grace");
6474 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6477 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6480 snprintf(buf, buflen, "%5.4gP",
6481 (double)num / ((__u64)1 << 40));
6483 snprintf(buf, buflen, "%5.4gT",
6484 (double)num / (1 << 30));
6486 snprintf(buf, buflen, "%5.4gG",
6487 (double)num / (1 << 20));
6489 snprintf(buf, buflen, "%5.4gM",
6490 (double)num / (1 << 10));
6492 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6496 #define STRBUF_LEN 32
6497 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6498 int rc, bool h, bool show_default)
6504 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6505 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6506 int bover = 0, iover = 0;
6507 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6508 char numbuf[3][STRBUF_LEN];
6510 char strbuf[STRBUF_LEN];
6512 if (dqb->dqb_bhardlimit &&
6513 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6515 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6516 if (dqb->dqb_btime > now) {
6523 if (dqb->dqb_ihardlimit &&
6524 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6526 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6527 if (dqb->dqb_itime > now) {
6535 if (strlen(mnt) > 15)
6536 printf("%s\n%15s", mnt, "");
6538 printf("%15s", mnt);
6541 diff2str(dqb->dqb_btime, timebuf, now);
6542 else if (show_default)
6543 snprintf(timebuf, sizeof(timebuf), "%llu",
6546 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6547 strbuf, sizeof(strbuf), h);
6548 if (rc == -EREMOTEIO)
6549 sprintf(numbuf[0], "%s*", strbuf);
6551 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6552 "%s" : "[%s]", strbuf);
6554 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6555 if (type == QC_GENERAL)
6556 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6557 "%s" : "[%s]", strbuf);
6559 sprintf(numbuf[1], "%s", "-");
6561 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6562 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6563 "%s" : "[%s]", strbuf);
6566 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6568 printf(" %7s%c %6s %7s %7s",
6569 numbuf[0], bover ? '*' : ' ', numbuf[1],
6570 numbuf[2], bover > 1 ? timebuf : "-");
6574 diff2str(dqb->dqb_itime, timebuf, now);
6575 else if (show_default)
6576 snprintf(timebuf, sizeof(timebuf), "%llu",
6579 snprintf(numbuf[0], sizeof(numbuf),
6580 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6581 (uintmax_t)dqb->dqb_curinodes);
6583 if (type == QC_GENERAL)
6584 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6586 (uintmax_t)dqb->dqb_isoftlimit);
6588 sprintf(numbuf[1], "%s", "-");
6590 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6591 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6594 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6595 else if (type != QC_OSTIDX)
6596 printf(" %7s%c %6s %7s %7s",
6597 numbuf[0], iover ? '*' : ' ', numbuf[1],
6598 numbuf[2], iover > 1 ? timebuf : "-");
6600 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6603 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6604 qctl->qc_cmd == Q_GETOINFO) {
6608 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6609 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6610 printf("Block grace time: %s; Inode grace time: %s\n",
6611 bgtimebuf, igtimebuf);
6615 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6616 bool h, __u64 *total)
6618 int rc = 0, rc1 = 0, count = 0;
6619 __u32 valid = qctl->qc_valid;
6621 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6623 fprintf(stderr, "can not get %s count: %s\n",
6624 is_mdt ? "mdt": "ost", strerror(-rc));
6628 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6629 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6630 rc = llapi_quotactl(mnt, qctl);
6632 /* It is remote client case. */
6633 if (rc == -EOPNOTSUPP) {
6640 fprintf(stderr, "quotactl %s%d failed.\n",
6641 is_mdt ? "mdt": "ost", qctl->qc_idx);
6645 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6646 qctl->qc_valid, 0, h, false);
6647 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6648 qctl->qc_dqblk.dqb_bhardlimit;
6651 qctl->qc_valid = valid;
6655 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6656 int verbose, int quiet, bool human_readable,
6659 int rc1 = 0, rc2 = 0, rc3 = 0;
6660 char *obd_type = (char *)qctl->obd_type;
6661 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6662 __u64 total_ialloc = 0, total_balloc = 0;
6663 bool use_default_for_blk = false;
6664 bool use_default_for_file = false;
6667 rc1 = llapi_quotactl(mnt, qctl);
6671 fprintf(stderr, "%s quotas are not enabled.\n",
6672 qtype_name(qctl->qc_type));
6675 fprintf(stderr, "Permission denied.\n");
6678 /* We already got error message. */
6681 fprintf(stderr, "Unexpected quotactl error: %s\n",
6686 if (!show_default && qctl->qc_id == 0) {
6687 qctl->qc_dqblk.dqb_bhardlimit = 0;
6688 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6689 qctl->qc_dqblk.dqb_ihardlimit = 0;
6690 qctl->qc_dqblk.dqb_isoftlimit = 0;
6691 qctl->qc_dqblk.dqb_btime = 0;
6692 qctl->qc_dqblk.dqb_itime = 0;
6693 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6696 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6697 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6698 use_default_for_blk = true;
6699 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6702 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6703 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6704 use_default_for_file = true;
6705 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6708 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6709 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6710 print_quota_title(name, qctl, human_readable, show_default);
6712 if (rc1 && *obd_type)
6713 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6715 if (qctl->qc_valid != QC_GENERAL)
6718 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6719 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6720 (QIF_LIMITS|QIF_USAGE));
6722 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6724 if (!show_default && verbose &&
6725 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6726 char strbuf[STRBUF_LEN];
6728 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6730 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6732 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6734 printf("Total allocated inode limit: %ju, total "
6735 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6739 if (use_default_for_blk)
6740 printf("%cid %u is using default block quota setting\n",
6741 *qtype_name(qctl->qc_type), qctl->qc_id);
6743 if (use_default_for_file)
6744 printf("%cid %u is using default file quota setting\n",
6745 *qtype_name(qctl->qc_type), qctl->qc_id);
6747 if (rc1 || rc2 || rc3 || inacc)
6748 printf("Some errors happened when getting quota info. "
6749 "Some devices may be not working or deactivated. "
6750 "The data in \"[]\" is inaccurate.\n");
6756 static int lfs_project(int argc, char **argv)
6758 int ret = 0, err = 0, c, i;
6759 struct project_handle_control phc = { 0 };
6760 enum lfs_project_ops_t op;
6763 phc.assign_projid = false;
6764 /* default action */
6765 op = LFS_PROJECT_LIST;
6767 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6770 if (op != LFS_PROJECT_LIST) {
6772 "%s: cannot specify '-c' '-C' '-s' together\n",
6777 op = LFS_PROJECT_CHECK;
6780 if (op != LFS_PROJECT_LIST) {
6782 "%s: cannot specify '-c' '-C' '-s' together\n",
6787 op = LFS_PROJECT_CLEAR;
6790 if (op != LFS_PROJECT_LIST) {
6792 "%s: cannot specify '-c' '-C' '-s' together\n",
6797 phc.set_inherit = true;
6798 op = LFS_PROJECT_SET;
6804 phc.keep_projid = true;
6807 phc.recursive = true;
6810 phc.projid = strtoul(optarg, NULL, 0);
6811 phc.assign_projid = true;
6815 phc.newline = false;
6818 fprintf(stderr, "%s: invalid option '%c'\n",
6824 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6825 op = LFS_PROJECT_SET;
6826 phc.set_projid = true;
6827 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6828 phc.set_projid = true;
6832 case LFS_PROJECT_CHECK:
6833 if (phc.keep_projid) {
6835 "%s: '-k' is useless together with '-c'\n",
6840 case LFS_PROJECT_CLEAR:
6843 "%s: '-0' is useless together with '-C'\n",
6847 if (phc.assign_projid) {
6849 "%s: '-p' is useless together with '-C'\n",
6854 case LFS_PROJECT_SET:
6857 "%s: '-0' is useless together with '-s'\n",
6861 if (phc.keep_projid) {
6863 "%s: '-k' is useless together with '-s'\n",
6871 "%s: '-0' is useless for list operations\n",
6881 fprintf(stderr, "%s: missing file or directory target(s)\n",
6886 for (i = 0; i < argc; i++) {
6888 case LFS_PROJECT_CHECK:
6889 err = lfs_project_check(argv[i], &phc);
6891 case LFS_PROJECT_LIST:
6892 err = lfs_project_list(argv[i], &phc);
6894 case LFS_PROJECT_CLEAR:
6895 err = lfs_project_clear(argv[i], &phc);
6897 case LFS_PROJECT_SET:
6898 err = lfs_project_set(argv[i], &phc);
6910 static int lfs_quota(int argc, char **argv)
6913 char *mnt, *name = NULL;
6914 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6915 .qc_type = ALLQUOTA };
6916 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6917 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6919 __u32 valid = QC_GENERAL, idx = 0;
6920 bool human_readable = false;
6921 bool show_default = false;
6924 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6927 show_default = true;
6932 show_default = true;
6937 show_default = true;
6941 if (qctl.qc_type != ALLQUOTA) {
6943 "%s quota: only one of -u, -g, or -p may be specified\n",
6947 qctl.qc_type = qtype;
6950 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6953 valid = qctl.qc_valid = QC_UUID;
6954 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6957 valid = qctl.qc_valid = QC_MDTIDX;
6958 idx = qctl.qc_idx = atoi(optarg);
6959 if (idx == 0 && *optarg != '0') {
6961 "%s quota: invalid MDT index '%s'\n",
6967 valid = qctl.qc_valid = QC_OSTIDX;
6968 idx = qctl.qc_idx = atoi(optarg);
6969 if (idx == 0 && *optarg != '0') {
6971 "%s quota: invalid OST index '%s'\n",
6983 human_readable = true;
6986 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6987 progname, argv[optind - 1]);
6992 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6993 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6994 optind == argc - 1 && !show_default) {
6996 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6997 qctl.qc_valid = valid;
7000 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7001 qctl.qc_type = qtype;
7002 if (qtype == USRQUOTA) {
7003 qctl.qc_id = geteuid();
7004 rc = uid2name(&name, qctl.qc_id);
7006 qctl.qc_id = getegid();
7007 rc = gid2name(&name, qctl.qc_id);
7012 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7013 human_readable, show_default);
7018 /* lfs quota -u username /path/to/lustre/mount */
7019 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7020 /* options should be followed by u/g-name and mntpoint */
7021 if ((!show_default && optind + 2 != argc) ||
7022 (show_default && optind + 1 != argc) ||
7023 qctl.qc_type == ALLQUOTA) {
7025 "%s quota: name and mount point must be specified\n",
7030 if (!show_default) {
7031 name = argv[optind++];
7032 switch (qctl.qc_type) {
7034 rc = name2uid(&qctl.qc_id, name);
7037 rc = name2gid(&qctl.qc_id, name);
7040 rc = name2projid(&qctl.qc_id, name);
7047 qctl.qc_valid = QC_GENERAL;
7048 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7053 qctl.qc_id = strtoul(name, &endptr, 10);
7054 if (*endptr != '\0') {
7055 fprintf(stderr, "%s quota: invalid id '%s'\n",
7060 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7061 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7067 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7068 human_readable, show_default);
7071 #endif /* HAVE_SYS_QUOTA_H! */
7073 static int flushctx_ioctl(char *mp)
7077 fd = open(mp, O_RDONLY);
7079 fprintf(stderr, "flushctx: error open %s: %s\n",
7080 mp, strerror(errno));
7084 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7086 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7087 mp, strerror(errno));
7093 static int lfs_flushctx(int argc, char **argv)
7095 int kdestroy = 0, c;
7096 char mntdir[PATH_MAX] = {'\0'};
7100 while ((c = getopt(argc, argv, "k")) != -1) {
7106 fprintf(stderr, "error: %s: option '-%c' "
7107 "unrecognized\n", argv[0], c);
7113 if ((rc = system("kdestroy > /dev/null")) != 0) {
7114 rc = WEXITSTATUS(rc);
7115 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7119 if (optind >= argc) {
7120 /* flush for all mounted lustre fs. */
7121 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7122 /* Check if we have a mount point */
7123 if (mntdir[0] == '\0')
7126 if (flushctx_ioctl(mntdir))
7129 mntdir[0] = '\0'; /* avoid matching in next loop */
7132 /* flush fs as specified */
7133 while (optind < argc) {
7134 if (flushctx_ioctl(argv[optind++]))
7141 static int lfs_changelog(int argc, char **argv)
7143 void *changelog_priv;
7144 struct changelog_rec *rec;
7145 long long startrec = 0, endrec = 0;
7147 struct option long_opts[] = {
7148 { .val = 'f', .name = "follow", .has_arg = no_argument },
7150 char short_opts[] = "f";
7153 while ((rc = getopt_long(argc, argv, short_opts,
7154 long_opts, NULL)) != -1) {
7161 "%s changelog: unrecognized option '%s'\n",
7162 progname, argv[optind - 1]);
7166 if (optind >= argc) {
7167 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7172 mdd = argv[optind++];
7174 startrec = strtoll(argv[optind++], NULL, 10);
7176 endrec = strtoll(argv[optind++], NULL, 10);
7178 rc = llapi_changelog_start(&changelog_priv,
7179 CHANGELOG_FLAG_BLOCK |
7180 CHANGELOG_FLAG_JOBID |
7181 CHANGELOG_FLAG_EXTRA_FLAGS |
7182 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7185 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7186 progname, strerror(errno = -rc));
7190 rc = llapi_changelog_set_xflags(changelog_priv,
7191 CHANGELOG_EXTRA_FLAG_UIDGID |
7192 CHANGELOG_EXTRA_FLAG_NID |
7193 CHANGELOG_EXTRA_FLAG_OMODE |
7194 CHANGELOG_EXTRA_FLAG_XATTR);
7197 "%s changelog: cannot set xflags for changelog: %s\n",
7198 progname, strerror(errno = -rc));
7202 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7206 if (endrec && rec->cr_index > endrec) {
7207 llapi_changelog_free(&rec);
7210 if (rec->cr_index < startrec) {
7211 llapi_changelog_free(&rec);
7215 secs = rec->cr_time >> 30;
7216 gmtime_r(&secs, &ts);
7217 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7218 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7219 changelog_type2str(rec->cr_type),
7220 ts.tm_hour, ts.tm_min, ts.tm_sec,
7221 (int)(rec->cr_time & ((1 << 30) - 1)),
7222 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7223 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7225 if (rec->cr_flags & CLF_JOBID) {
7226 struct changelog_ext_jobid *jid =
7227 changelog_rec_jobid(rec);
7229 if (jid->cr_jobid[0] != '\0')
7230 printf(" j=%s", jid->cr_jobid);
7233 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7234 struct changelog_ext_extra_flags *ef =
7235 changelog_rec_extra_flags(rec);
7237 printf(" ef=0x%llx", ef->cr_extra_flags);
7239 if (ef->cr_extra_flags & CLFE_UIDGID) {
7240 struct changelog_ext_uidgid *uidgid =
7241 changelog_rec_uidgid(rec);
7243 printf(" u=%llu:%llu",
7244 uidgid->cr_uid, uidgid->cr_gid);
7246 if (ef->cr_extra_flags & CLFE_NID) {
7247 struct changelog_ext_nid *nid =
7248 changelog_rec_nid(rec);
7251 libcfs_nid2str(nid->cr_nid));
7254 if (ef->cr_extra_flags & CLFE_OPEN) {
7255 struct changelog_ext_openmode *omd =
7256 changelog_rec_openmode(rec);
7257 char mode[] = "---";
7259 /* exec mode must be exclusive */
7260 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7263 if (omd->cr_openflags & MDS_FMODE_READ)
7265 if (omd->cr_openflags &
7272 if (strcmp(mode, "---") != 0)
7273 printf(" m=%s", mode);
7277 if (ef->cr_extra_flags & CLFE_XATTR) {
7278 struct changelog_ext_xattr *xattr =
7279 changelog_rec_xattr(rec);
7281 if (xattr->cr_xattr[0] != '\0')
7282 printf(" x=%s", xattr->cr_xattr);
7286 if (rec->cr_namelen)
7287 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7288 rec->cr_namelen, changelog_rec_name(rec));
7290 if (rec->cr_flags & CLF_RENAME) {
7291 struct changelog_ext_rename *rnm =
7292 changelog_rec_rename(rec);
7294 if (!fid_is_zero(&rnm->cr_sfid))
7295 printf(" s="DFID" sp="DFID" %.*s",
7296 PFID(&rnm->cr_sfid),
7297 PFID(&rnm->cr_spfid),
7298 (int)changelog_rec_snamelen(rec),
7299 changelog_rec_sname(rec));
7303 llapi_changelog_free(&rec);
7306 llapi_changelog_fini(&changelog_priv);
7309 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7310 progname, strerror(errno = -rc));
7312 return (rc == 1 ? 0 : rc);
7315 static int lfs_changelog_clear(int argc, char **argv)
7323 endrec = strtoll(argv[3], NULL, 10);
7325 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7328 fprintf(stderr, "%s: record out of range: %llu\n",
7330 else if (rc == -ENOENT)
7331 fprintf(stderr, "%s: no changelog user: %s\n",
7334 fprintf(stderr, "%s error: %s\n", argv[0],
7343 static int lfs_fid2path(int argc, char **argv)
7345 struct option long_opts[] = {
7346 { .val = 'c', .name = "cur", .has_arg = no_argument },
7347 { .val = 'l', .name = "link", .has_arg = required_argument },
7348 { .val = 'r', .name = "rec", .has_arg = required_argument },
7350 char short_opts[] = "cl:r:";
7351 char *device, *fid, *path;
7352 long long recno = -1;
7357 char *endptr = NULL;
7359 while ((rc = getopt_long(argc, argv, short_opts,
7360 long_opts, NULL)) != -1) {
7366 linkno = strtol(optarg, &endptr, 10);
7367 if (*endptr != '\0') {
7369 "%s fid2path: invalid linkno '%s'\n",
7375 recno = strtoll(optarg, &endptr, 10);
7376 if (*endptr != '\0') {
7378 "%s fid2path: invalid recno '%s'\n",
7385 "%s fid2path: unrecognized option '%s'\n",
7386 progname, argv[optind - 1]);
7393 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7398 device = argv[optind++];
7399 path = calloc(1, PATH_MAX);
7403 "%s fid2path: cannot allocate memory for path: %s\n",
7404 progname, strerror(-rc));
7409 while (optind < argc) {
7410 fid = argv[optind++];
7412 lnktmp = (linkno >= 0) ? linkno : 0;
7414 int oldtmp = lnktmp;
7415 long long rectmp = recno;
7417 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7421 "%s fid2path: cannot find '%s': %s\n",
7422 progname, fid, strerror(errno = -rc2));
7429 fprintf(stdout, "%lld ", rectmp);
7430 if (device[0] == '/') {
7431 fprintf(stdout, "%s", device);
7432 if (device[strlen(device) - 1] != '/')
7433 fprintf(stdout, "/");
7434 } else if (path[0] == '\0') {
7435 fprintf(stdout, "/");
7437 fprintf(stdout, "%s\n", path);
7440 /* specified linkno */
7442 if (oldtmp == lnktmp)
7452 static int lfs_path2fid(int argc, char **argv)
7454 struct option long_opts[] = {
7455 { .val = 'p', .name = "parents", .has_arg = no_argument },
7458 const char short_opts[] = "p";
7459 const char *sep = "";
7462 bool show_parents = false;
7464 while ((rc = getopt_long(argc, argv, short_opts,
7465 long_opts, NULL)) != -1) {
7468 show_parents = true;
7472 "%s path2fid: unrecognized option '%s'\n",
7473 progname, argv[optind - 1]);
7478 if (optind > argc - 1) {
7479 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7483 else if (optind < argc - 1)
7487 for (path = argv + optind; *path != NULL; path++) {
7489 if (!show_parents) {
7490 err = llapi_path2fid(*path, &fid);
7492 printf("%s%s"DFID"\n",
7493 *sep != '\0' ? *path : "", sep,
7496 char name[NAME_MAX + 1];
7497 unsigned int linkno = 0;
7499 while ((err = llapi_path2parent(*path, linkno, &fid,
7500 name, sizeof(name))) == 0) {
7501 if (*sep != '\0' && linkno == 0)
7502 printf("%s%s", *path, sep);
7504 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7509 /* err == -ENODATA is end-of-loop */
7510 if (linkno > 0 && err == -ENODATA) {
7518 "%s path2fid: cannot get %sfid for '%s': %s\n",
7519 progname, show_parents ? "parent " : "", *path,
7531 static int lfs_data_version(int argc, char **argv)
7538 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7541 fprintf(stderr, "%s data_version: FILE must be specified\n",
7546 while ((c = getopt(argc, argv, "nrw")) != -1) {
7549 data_version_flags = 0;
7552 data_version_flags |= LL_DV_RD_FLUSH;
7555 data_version_flags |= LL_DV_WR_FLUSH;
7559 "%s data_version: unrecognized option '%s'\n",
7560 progname, argv[optind - 1]);
7564 if (optind == argc) {
7565 fprintf(stderr, "%s data_version: FILE must be specified\n",
7570 path = argv[optind];
7571 fd = open(path, O_RDONLY);
7574 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7575 progname, path, strerror(-rc));
7579 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7582 "%s data_version: cannot get version for '%s': %s\n",
7583 progname, path, strerror(-rc));
7585 printf("%ju" "\n", (uintmax_t)data_version);
7591 static int lfs_hsm_state(int argc, char **argv)
7596 struct hsm_user_state hus;
7604 rc = llapi_hsm_state_get(path, &hus);
7606 fprintf(stderr, "can't get hsm state for %s: %s\n",
7607 path, strerror(errno = -rc));
7611 /* Display path name and status flags */
7612 printf("%s: (0x%08x)", path, hus.hus_states);
7614 if (hus.hus_states & HS_RELEASED)
7615 printf(" released");
7616 if (hus.hus_states & HS_EXISTS)
7618 if (hus.hus_states & HS_DIRTY)
7620 if (hus.hus_states & HS_ARCHIVED)
7621 printf(" archived");
7622 /* Display user-settable flags */
7623 if (hus.hus_states & HS_NORELEASE)
7624 printf(" never_release");
7625 if (hus.hus_states & HS_NOARCHIVE)
7626 printf(" never_archive");
7627 if (hus.hus_states & HS_LOST)
7628 printf(" lost_from_hsm");
7630 if (hus.hus_archive_id != 0)
7631 printf(", archive_id:%d", hus.hus_archive_id);
7634 } while (++i < argc);
7639 #define LFS_HSM_SET 0
7640 #define LFS_HSM_CLEAR 1
7643 * Generic function to set or clear HSM flags.
7644 * Used by hsm_set and hsm_clear.
7646 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7648 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7650 struct option long_opts[] = {
7651 { .val = 'A', .name = "archived", .has_arg = no_argument },
7652 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7653 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7654 { .val = 'e', .name = "exists", .has_arg = no_argument },
7655 { .val = 'l', .name = "lost", .has_arg = no_argument },
7656 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7657 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7659 char short_opts[] = "lraAdei:";
7663 __u32 archive_id = 0;
7669 while ((c = getopt_long(argc, argv, short_opts,
7670 long_opts, NULL)) != -1) {
7676 mask |= HS_NOARCHIVE;
7679 mask |= HS_ARCHIVED;
7682 mask |= HS_NORELEASE;
7691 archive_id = strtol(optarg, &end, 10);
7693 fprintf(stderr, "invalid archive_id: '%s'\n",
7701 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7702 argv[0], argv[optind - 1]);
7707 /* User should have specified a flag */
7711 while (optind < argc) {
7713 path = argv[optind];
7715 /* If mode == 0, this means we apply the mask. */
7716 if (mode == LFS_HSM_SET)
7717 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7719 rc = llapi_hsm_state_set(path, 0, mask, 0);
7722 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7723 path, strerror(errno = -rc));
7732 static int lfs_hsm_action(int argc, char **argv)
7737 struct hsm_current_action hca;
7738 struct hsm_extent he;
7739 enum hsm_user_action hua;
7740 enum hsm_progress_states hps;
7748 rc = llapi_hsm_current_action(path, &hca);
7750 fprintf(stderr, "can't get hsm action for %s: %s\n",
7751 path, strerror(errno = -rc));
7754 he = hca.hca_location;
7755 hua = hca.hca_action;
7756 hps = hca.hca_state;
7758 printf("%s: %s", path, hsm_user_action2name(hua));
7760 /* Skip file without action */
7761 if (hca.hca_action == HUA_NONE) {
7766 printf(" %s ", hsm_progress_state2name(hps));
7768 if ((hps == HPS_RUNNING) &&
7769 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7770 printf("(%llu bytes moved)\n",
7771 (unsigned long long)he.length);
7772 else if ((he.offset + he.length) == LUSTRE_EOF)
7773 printf("(from %llu to EOF)\n",
7774 (unsigned long long)he.offset);
7776 printf("(from %llu to %llu)\n",
7777 (unsigned long long)he.offset,
7778 (unsigned long long)(he.offset + he.length));
7780 } while (++i < argc);
7785 static int lfs_hsm_set(int argc, char **argv)
7787 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7790 static int lfs_hsm_clear(int argc, char **argv)
7792 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7796 * Check file state and return its fid, to be used by lfs_hsm_request().
7798 * \param[in] file Path to file to check
7799 * \param[in,out] fid Pointer to allocated lu_fid struct.
7800 * \param[in,out] last_dev Pointer to last device id used.
7802 * \return 0 on success.
7804 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7810 rc = lstat(file, &st);
7812 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7815 /* Checking for regular file as archiving as posix copytool
7816 * rejects archiving files other than regular files
7818 if (!S_ISREG(st.st_mode)) {
7819 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7822 /* A request should be ... */
7823 if (*last_dev != st.st_dev && *last_dev != 0) {
7824 fprintf(stderr, "All files should be "
7825 "on the same filesystem: %s\n", file);
7828 *last_dev = st.st_dev;
7830 rc = llapi_path2fid(file, fid);
7832 fprintf(stderr, "Cannot read FID of %s: %s\n",
7833 file, strerror(-rc));
7839 /* Fill an HSM HUR item with a given file name.
7841 * If mntpath is set, then the filename is actually a FID, and no
7842 * lookup on the filesystem will be performed.
7844 * \param[in] hur the user request to fill
7845 * \param[in] idx index of the item inside the HUR to fill
7846 * \param[in] mntpath mountpoint of Lustre
7847 * \param[in] fname filename (if mtnpath is NULL)
7848 * or FID (if mntpath is set)
7849 * \param[in] last_dev pointer to last device id used
7851 * \retval 0 on success
7852 * \retval CMD_HELP or a negative errno on error
7854 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7855 const char *mntpath, const char *fname,
7858 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7861 hui->hui_extent.length = -1;
7863 if (mntpath != NULL) {
7866 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7870 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7875 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7879 hur->hur_request.hr_itemcount++;
7884 static int lfs_hsm_request(int argc, char **argv, int action)
7886 struct option long_opts[] = {
7887 { .val = 'a', .name = "archive", .has_arg = required_argument },
7888 { .val = 'D', .name = "data", .has_arg = required_argument },
7889 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7890 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7893 char short_opts[] = "l:D:a:m:";
7894 struct hsm_user_request *hur, *oldhur;
7899 char *filelist = NULL;
7900 char fullpath[PATH_MAX];
7901 char *opaque = NULL;
7905 int nbfile_alloc = 0;
7906 char *some_file = NULL;
7907 char *mntpath = NULL;
7913 while ((c = getopt_long(argc, argv, short_opts,
7914 long_opts, NULL)) != -1) {
7923 if (action != HUA_ARCHIVE &&
7924 action != HUA_REMOVE) {
7926 "error: -a is supported only "
7927 "when archiving or removing\n");
7930 archive_id = atoi(optarg);
7933 if (some_file == NULL) {
7935 some_file = strdup(optarg);
7941 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7942 argv[0], argv[optind - 1]);
7947 /* All remaining args are files, so we have at least nbfile */
7948 nbfile = argc - optind;
7950 if ((nbfile == 0) && (filelist == NULL))
7954 opaque_len = strlen(opaque);
7956 /* Alloc the request structure with enough place to store all files
7957 * from command line. */
7958 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7960 fprintf(stderr, "Cannot create the request: %s\n",
7964 nbfile_alloc = nbfile;
7966 hur->hur_request.hr_action = action;
7967 hur->hur_request.hr_archive_id = archive_id;
7968 hur->hur_request.hr_flags = 0;
7970 /* All remaining args are files, add them */
7971 if (nbfile != 0 && some_file == NULL)
7972 some_file = strdup(argv[optind]);
7974 for (i = 0; i < nbfile; i++) {
7975 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7981 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7983 /* If a filelist was specified, read the filelist from it. */
7984 if (filelist != NULL) {
7985 fp = fopen(filelist, "r");
7987 fprintf(stderr, "Cannot read the file list %s: %s\n",
7988 filelist, strerror(errno));
7993 while ((rc = getline(&line, &len, fp)) != -1) {
7994 /* If allocated buffer was too small, get something
7996 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7999 nbfile_alloc = nbfile_alloc * 2 + 1;
8001 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8004 fprintf(stderr, "hsm: cannot allocate "
8005 "the request: %s\n",
8012 size = hur_len(oldhur);
8014 fprintf(stderr, "hsm: cannot allocate "
8015 "%u files + %u bytes data\n",
8016 oldhur->hur_request.hr_itemcount,
8017 oldhur->hur_request.hr_data_len);
8024 memcpy(hur, oldhur, size);
8029 if (line[strlen(line) - 1] == '\n')
8030 line[strlen(line) - 1] = '\0';
8032 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8033 mntpath, line, &last_dev);
8039 if (some_file == NULL) {
8049 /* If a --data was used, add it to the request */
8050 hur->hur_request.hr_data_len = opaque_len;
8052 memcpy(hur_data(hur), opaque, opaque_len);
8054 /* Send the HSM request */
8055 if (realpath(some_file, fullpath) == NULL) {
8056 fprintf(stderr, "Could not find path '%s': %s\n",
8057 some_file, strerror(errno));
8059 rc = llapi_hsm_request(fullpath, hur);
8061 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8062 some_file, strerror(-rc));
8072 static int lfs_hsm_archive(int argc, char **argv)
8074 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8077 static int lfs_hsm_restore(int argc, char **argv)
8079 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8082 static int lfs_hsm_release(int argc, char **argv)
8084 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8087 static int lfs_hsm_remove(int argc, char **argv)
8089 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8092 static int lfs_hsm_cancel(int argc, char **argv)
8094 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8097 static int lfs_swap_layouts(int argc, char **argv)
8102 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8103 SWAP_LAYOUTS_KEEP_MTIME |
8104 SWAP_LAYOUTS_KEEP_ATIME);
8107 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8109 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8111 int lfs_get_mode(const char *string)
8113 enum lock_mode_user mode;
8115 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8116 if (lock_mode_names[mode] == NULL)
8118 if (strcmp(string, lock_mode_names[mode]) == 0)
8125 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8127 enum lu_ladvise_type advice;
8130 advice < ARRAY_SIZE(ladvise_names); advice++) {
8131 if (ladvise_names[advice] == NULL)
8133 if (strcmp(string, ladvise_names[advice]) == 0)
8137 return LU_LADVISE_INVALID;
8140 static int lfs_ladvise(int argc, char **argv)
8142 struct option long_opts[] = {
8143 { .val = 'a', .name = "advice", .has_arg = required_argument },
8144 { .val = 'b', .name = "background", .has_arg = no_argument },
8145 { .val = 'e', .name = "end", .has_arg = required_argument },
8146 { .val = 'l', .name = "length", .has_arg = required_argument },
8147 { .val = 'm', .name = "mode", .has_arg = required_argument },
8148 { .val = 's', .name = "start", .has_arg = required_argument },
8149 { .val = 'u', .name = "unset", .has_arg = no_argument },
8151 char short_opts[] = "a:be:l:m:s:u";
8156 struct llapi_lu_ladvise advice;
8157 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8158 unsigned long long start = 0;
8159 unsigned long long end = LUSTRE_EOF;
8160 unsigned long long length = 0;
8161 unsigned long long size_units;
8162 unsigned long long flags = 0;
8166 while ((c = getopt_long(argc, argv, short_opts,
8167 long_opts, NULL)) != -1) {
8170 advice_type = lfs_get_ladvice(optarg);
8171 if (advice_type == LU_LADVISE_INVALID) {
8172 fprintf(stderr, "%s: invalid advice type "
8173 "'%s'\n", argv[0], optarg);
8174 fprintf(stderr, "Valid types:");
8176 for (advice_type = 0;
8177 advice_type < ARRAY_SIZE(ladvise_names);
8179 if (ladvise_names[advice_type] == NULL)
8181 fprintf(stderr, " %s",
8182 ladvise_names[advice_type]);
8184 fprintf(stderr, "\n");
8197 rc = llapi_parse_size(optarg, &end,
8200 fprintf(stderr, "%s: bad end offset '%s'\n",
8207 rc = llapi_parse_size(optarg, &start,
8210 fprintf(stderr, "%s: bad start offset "
8211 "'%s'\n", argv[0], optarg);
8217 rc = llapi_parse_size(optarg, &length,
8220 fprintf(stderr, "%s: bad length '%s'\n",
8226 mode = lfs_get_mode(optarg);
8228 fprintf(stderr, "%s: bad mode '%s', valid "
8229 "modes are READ or WRITE\n",
8237 fprintf(stderr, "%s: option '%s' unrecognized\n",
8238 argv[0], argv[optind - 1]);
8243 if (advice_type == LU_LADVISE_INVALID) {
8244 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8245 fprintf(stderr, "Valid types:");
8246 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8248 if (ladvise_names[advice_type] == NULL)
8250 fprintf(stderr, " %s", ladvise_names[advice_type]);
8252 fprintf(stderr, "\n");
8256 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8257 fprintf(stderr, "%s: Lock no expand advice is a per file "
8258 "descriptor advice, so when called from lfs, "
8259 "it does nothing.\n", argv[0]);
8263 if (argc <= optind) {
8264 fprintf(stderr, "%s: please give one or more file names\n",
8269 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8270 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8275 if (end == LUSTRE_EOF && length != 0)
8276 end = start + length;
8279 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8280 argv[0], start, end);
8284 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8285 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8290 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8291 fprintf(stderr, "%s: mode is required with lockahead\n",
8296 while (optind < argc) {
8299 path = argv[optind++];
8301 fd = open(path, O_RDONLY);
8303 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8304 argv[0], path, strerror(errno));
8309 advice.lla_start = start;
8310 advice.lla_end = end;
8311 advice.lla_advice = advice_type;
8312 advice.lla_value1 = 0;
8313 advice.lla_value2 = 0;
8314 advice.lla_value3 = 0;
8315 advice.lla_value4 = 0;
8316 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8317 advice.lla_lockahead_mode = mode;
8318 advice.lla_peradvice_flags = flags;
8321 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8324 fprintf(stderr, "%s: cannot give advice '%s' to file "
8325 "'%s': %s\n", argv[0],
8326 ladvise_names[advice_type],
8327 path, strerror(errno));
8333 if (rc == 0 && rc2 < 0)
8339 static const char *const heat_names[] = LU_HEAT_NAMES;
8341 static int lfs_heat_get(int argc, char **argv)
8343 struct lu_heat *heat;
8352 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8354 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8359 while (optind < argc) {
8360 path = argv[optind++];
8362 fd = open(path, O_RDONLY);
8364 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8365 argv[0], path, strerror(errno));
8370 heat->lh_count = OBD_HEAT_COUNT;
8371 rc2 = llapi_heat_get(fd, heat);
8374 fprintf(stderr, "%s: cannot get heat of file '%s'"
8375 ": %s\n", argv[0], path, strerror(errno));
8379 printf("flags: %x\n", heat->lh_flags);
8380 for (i = 0; i < heat->lh_count; i++)
8381 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8383 if (rc == 0 && rc2 < 0)
8391 static int lfs_heat_set(int argc, char **argv)
8393 struct option long_opts[] = {
8394 { .val = 'c', .name = "clear", .has_arg = no_argument },
8395 { .val = 'o', .name = "off", .has_arg = no_argument },
8396 { .val = 'O', .name = "on", .has_arg = no_argument },
8398 enum lu_heat_flag flags = 0;
8408 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8411 flags |= LU_HEAT_FLAG_CLEAR;
8414 flags |= LU_HEAT_FLAG_CLEAR;
8415 flags |= LU_HEAT_FLAG_OFF;
8418 flags &= ~LU_HEAT_FLAG_OFF;
8423 fprintf(stderr, "%s: option '%s' unrecognized\n",
8424 argv[0], argv[optind - 1]);
8429 if (argc <= optind) {
8430 fprintf(stderr, "%s: please give one or more file names\n",
8435 while (optind < argc) {
8436 path = argv[optind++];
8438 fd = open(path, O_RDONLY);
8440 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8441 argv[0], path, strerror(errno));
8446 rc2 = llapi_heat_set(fd, flags);
8449 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8450 ": %s\n", argv[0], path, strerror(errno));
8454 if (rc == 0 && rc2 < 0)
8460 /** The input string contains a comma delimited list of component ids and
8461 * ranges, for example "1,2-4,7".
8463 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8465 bool end_of_loop = false;
8473 while (!end_of_loop) {
8477 char *endptr = NULL;
8480 ptr = strchrnul(arg, ',');
8481 end_of_loop = *ptr == '\0';
8484 start_index = strtol(arg, &endptr, 0);
8485 if (endptr == arg) /* no data at all */
8487 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8489 if (start_index < 0)
8492 end_index = start_index;
8493 if (*endptr == '-') {
8494 end_index = strtol(endptr + 1, &endptr, 0);
8495 if (*endptr != '\0')
8497 if (end_index < start_index)
8501 for (i = start_index; i <= end_index && size > 0; i++) {
8504 /* remove duplicate */
8505 for (j = 0; j < nr; j++) {
8509 if (j == nr) { /* no duplicate */
8515 if (size == 0 && i < end_index)
8522 if (!end_of_loop && ptr != NULL)
8525 return rc < 0 ? rc : nr;
8529 * struct verify_mirror_id - Mirror id to be verified.
8530 * @mirror_id: A specified mirror id.
8531 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8533 struct verify_mirror_id {
8539 * compare_mirror_ids() - Compare mirror ids.
8540 * @layout: Mirror component list.
8541 * @cbdata: Callback data in verify_mirror_id structure.
8543 * This is a callback function called by llapi_layout_comp_iterate()
8544 * to compare the specified mirror id with the one in the current
8545 * component of @layout. If they are the same, then the specified
8546 * mirror id is valid.
8548 * Return: a negative error code on failure or
8549 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8550 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8553 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8555 struct verify_mirror_id *mirror_id_cbdata =
8556 (struct verify_mirror_id *)cbdata;
8560 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8564 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8565 progname, strerror(errno));
8569 if (mirror_id_cbdata->mirror_id == mirror_id) {
8570 mirror_id_cbdata->is_valid_id = true;
8571 return LLAPI_LAYOUT_ITER_STOP;
8574 return LLAPI_LAYOUT_ITER_CONT;
8578 * verify_mirror_ids() - Verify specified mirror ids.
8579 * @fname: Mirrored file name.
8580 * @mirror_ids: Specified mirror ids to be verified.
8581 * @ids_nr: Number of specified mirror ids.
8583 * This function verifies that specified @mirror_ids are valid
8584 * in the mirrored file @fname.
8586 * Return: 0 on success or a negative error code on failure.
8589 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8591 struct llapi_layout *layout = NULL;
8592 struct verify_mirror_id mirror_id_cbdata = { 0 };
8603 if (stat(fname, &stbuf) < 0) {
8604 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8605 progname, fname, strerror(errno));
8610 if (!S_ISREG(stbuf.st_mode)) {
8611 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8617 fd = open(fname, O_DIRECT | O_RDONLY);
8619 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8620 progname, fname, strerror(errno));
8625 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8627 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8628 progname, fname, strerror(errno));
8632 layout = llapi_layout_get_by_fd(fd, 0);
8633 if (layout == NULL) {
8634 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8635 progname, fname, strerror(errno));
8637 llapi_lease_release(fd);
8641 rc = llapi_layout_flags_get(layout, &flr_state);
8643 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8644 progname, fname, strerror(errno));
8649 flr_state &= LCM_FL_FLR_MASK;
8650 switch (flr_state) {
8653 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8654 progname, fname, llapi_layout_flags_string(flr_state));
8661 for (i = 0; i < ids_nr; i++) {
8662 mirror_id_cbdata.mirror_id = mirror_ids[i];
8663 mirror_id_cbdata.is_valid_id = false;
8665 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8670 "%s: '%s' failed to verify mirror id: %u.\n",
8671 progname, fname, mirror_ids[i]);
8675 if (!mirror_id_cbdata.is_valid_id) {
8678 "%s: '%s' invalid specified mirror id: %u.\n",
8679 progname, fname, mirror_ids[i]);
8685 llapi_layout_free(layout);
8686 llapi_lease_release(fd);
8694 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8695 __u16 *mirror_ids, int ids_nr)
8697 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8698 struct llapi_layout *layout;
8708 if (stat(fname, &stbuf) < 0) {
8709 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8710 progname, fname, strerror(errno));
8714 if (!S_ISREG(stbuf.st_mode)) {
8715 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8721 fd = open(fname, O_DIRECT | O_RDWR);
8723 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8724 progname, fname, strerror(errno));
8729 layout = llapi_layout_get_by_fd(fd, 0);
8730 if (layout == NULL) {
8731 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8732 progname, fname, strerror(errno));
8737 rc = llapi_layout_flags_get(layout, &flr_state);
8739 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8740 progname, fname, strerror(errno));
8745 flr_state &= LCM_FL_FLR_MASK;
8746 if (flr_state == LCM_FL_NONE) {
8748 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8753 /* get stale component info */
8754 comp_size = llapi_mirror_find_stale(layout, comp_array,
8755 ARRAY_SIZE(comp_array),
8756 mirror_ids, ids_nr);
8757 if (comp_size <= 0) {
8762 ioc->lil_mode = LL_LEASE_WRLCK;
8763 ioc->lil_flags = LL_LEASE_RESYNC;
8764 rc = llapi_lease_set(fd, ioc);
8766 if (rc == -EALREADY)
8770 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8771 progname, fname, strerror(errno));
8775 /* get the read range [start, end) */
8776 start = comp_array[0].lrc_start;
8777 end = comp_array[0].lrc_end;
8778 for (idx = 1; idx < comp_size; idx++) {
8779 if (comp_array[idx].lrc_start < start)
8780 start = comp_array[idx].lrc_start;
8781 if (end < comp_array[idx].lrc_end)
8782 end = comp_array[idx].lrc_end;
8785 rc = llapi_lease_check(fd);
8786 if (rc != LL_LEASE_WRLCK) {
8787 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8792 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8795 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8796 progname, fname, rc);
8798 /* prepare ioc for lease put */
8799 ioc->lil_mode = LL_LEASE_UNLCK;
8800 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8802 for (idx = 0; idx < comp_size; idx++) {
8803 if (comp_array[idx].lrc_synced) {
8804 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8809 rc = llapi_lease_set(fd, ioc);
8811 if (rc == 0) /* lost lease lock */
8813 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8814 progname, fname, strerror(errno));
8818 * llapi_lease_set returns lease mode when it request to unlock
8824 llapi_layout_free(layout);
8831 static inline int lfs_mirror_resync(int argc, char **argv)
8833 struct ll_ioc_lease *ioc = NULL;
8834 __u16 mirror_ids[128] = { 0 };
8839 struct option long_opts[] = {
8840 { .val = 'o', .name = "only", .has_arg = required_argument },
8843 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8846 rc = parse_mirror_ids(mirror_ids,
8847 sizeof(mirror_ids) / sizeof(__u16),
8851 "%s: bad mirror ids '%s'.\n",
8858 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8859 argv[0], argv[optind - 1]);
8865 if (argc == optind) {
8866 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8871 if (ids_nr > 0 && argc > optind + 1) {
8873 "%s: option '--only' cannot be used upon multiple files.\n",
8881 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8886 /* set the lease on the file */
8887 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8889 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8890 argv[0], strerror(errno));
8895 for (; optind < argc; optind++) {
8896 rc = lfs_mirror_resync_file(argv[optind], ioc,
8897 mirror_ids, ids_nr);
8898 /* ignore previous file's error, continue with next file */
8901 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8909 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8911 struct llapi_layout *layout;
8914 layout = llapi_layout_get_by_fd(fd, 0);
8915 if (layout == NULL) {
8916 fprintf(stderr, "could not get layout.\n");
8920 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8922 fprintf(stderr, "failed to iterate layout\n");
8923 llapi_layout_free(layout);
8926 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8927 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8928 llapi_layout_free(layout);
8932 llapi_layout_free(layout);
8938 * Check whether two files are the same file
8939 * \retval 0 same file
8940 * \retval 1 not the same file
8941 * \retval <0 error code
8943 static inline int check_same_file(const char *f1, const char *f2)
8948 if (stat(f1, &stbuf1) < 0) {
8949 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8950 progname, f1, strerror(errno));
8954 if (stat(f2, &stbuf2) < 0) {
8955 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8956 progname, f2, strerror(errno));
8960 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8961 stbuf1.st_ino == stbuf2.st_ino)
8967 static inline int lfs_mirror_read(int argc, char **argv)
8970 __u16 mirror_id = 0;
8971 const char *outfile = NULL;
8977 const size_t buflen = 4 << 20;
8979 struct option long_opts[] = {
8980 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8981 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8984 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8989 mirror_id = strtoul(optarg, &end, 0);
8990 if (*end != '\0' || mirror_id == 0) {
8992 "%s %s: invalid mirror ID '%s'\n",
8993 progname, argv[0], optarg);
9001 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9002 progname, argv[optind - 1]);
9007 if (argc == optind) {
9008 fprintf(stderr, "%s %s: no mirrored file provided\n",
9011 } else if (argc > optind + 1) {
9012 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9016 if (mirror_id == 0) {
9017 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9022 /* open mirror file */
9023 fname = argv[optind];
9026 rc = check_same_file(fname, outfile);
9029 "%s %s: output file cannot be the mirrored file\n",
9037 fd = open(fname, O_DIRECT | O_RDONLY);
9039 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9040 progname, argv[0], fname, strerror(errno));
9044 /* verify mirror id */
9045 rc = verify_mirror_id_by_fd(fd, mirror_id);
9048 "%s %s: cannot find mirror with ID %u in '%s'\n",
9049 progname, argv[0], mirror_id, fname);
9053 /* open output file */
9055 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9057 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9058 progname, argv[0], outfile, strerror(errno));
9063 outfd = STDOUT_FILENO;
9066 /* allocate buffer */
9067 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9069 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9070 progname, argv[0], rc);
9077 ssize_t written = 0;
9079 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9080 if (bytes_read < 0) {
9083 "%s %s: fail to read data from mirror %u: %s\n",
9084 progname, argv[0], mirror_id, strerror(-rc));
9089 if (bytes_read == 0)
9092 while (written < bytes_read) {
9095 written2 = write(outfd, buf + written,
9096 bytes_read - written);
9099 "%s %s: fail to write %s: %s\n",
9100 progname, argv[0], outfile ? : "STDOUT",
9105 written += written2;
9108 if (written != bytes_read) {
9110 "%s %s: written %ld bytes does not match with %ld read.\n",
9111 progname, argv[0], written, bytes_read);
9133 static inline int lfs_mirror_write(int argc, char **argv)
9136 __u16 mirror_id = 0;
9137 const char *inputfile = NULL;
9143 const size_t buflen = 4 << 20;
9145 size_t page_size = sysconf(_SC_PAGESIZE);
9146 struct ll_ioc_lease_id ioc;
9148 struct option long_opts[] = {
9149 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9150 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9153 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9158 mirror_id = strtoul(optarg, &end, 0);
9159 if (*end != '\0' || mirror_id == 0) {
9161 "%s %s: invalid mirror ID '%s'\n",
9162 progname, argv[0], optarg);
9170 fprintf(stderr, "%s: option '%s' unrecognized\n",
9171 progname, argv[optind - 1]);
9176 if (argc == optind) {
9177 fprintf(stderr, "%s %s: no mirrored file provided\n",
9180 } else if (argc > optind + 1) {
9181 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9185 if (mirror_id == 0) {
9186 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9191 /* open mirror file */
9192 fname = argv[optind];
9195 rc = check_same_file(fname, inputfile);
9198 "%s %s: input file cannot be the mirrored file\n",
9206 fd = open(fname, O_DIRECT | O_WRONLY);
9208 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9209 progname, argv[0], fname, strerror(errno));
9213 /* verify mirror id */
9214 rc = verify_mirror_id_by_fd(fd, mirror_id);
9217 "%s %s: cannot find mirror with ID %u in '%s'\n",
9218 progname, argv[0], mirror_id, fname);
9222 /* open input file */
9224 inputfd = open(inputfile, O_RDONLY, 0644);
9226 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9227 progname, argv[0], inputfile, strerror(errno));
9232 inputfd = STDIN_FILENO;
9235 /* allocate buffer */
9236 rc = posix_memalign(&buf, page_size, buflen);
9238 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9239 progname, argv[0], rc);
9243 /* prepare target mirror components instantiation */
9244 ioc.lil_mode = LL_LEASE_WRLCK;
9245 ioc.lil_flags = LL_LEASE_RESYNC;
9246 ioc.lil_mirror_id = mirror_id;
9247 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9250 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9251 progname, argv[0], fname, strerror(errno));
9261 rc = llapi_lease_check(fd);
9262 if (rc != LL_LEASE_WRLCK) {
9263 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9264 progname, argv[0], fname);
9268 bytes_read = read(inputfd, buf, buflen);
9269 if (bytes_read < 0) {
9272 "%s %s: fail to read data from '%s': %s\n",
9273 progname, argv[0], inputfile ? : "STDIN",
9280 if (bytes_read == 0)
9283 /* round up to page align to make direct IO happy. */
9284 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9286 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9291 "%s %s: fail to write to mirror %u: %s\n",
9292 progname, argv[0], mirror_id,
9300 if (pos & (page_size - 1)) {
9301 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9306 ioc.lil_mode = LL_LEASE_UNLCK;
9307 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9309 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9314 "%s %s: release lease lock of '%s' failed: %s\n",
9315 progname, argv[0], fname, strerror(errno));
9332 struct collect_ids_data {
9338 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9340 struct collect_ids_data *cid = cbdata;
9344 rc = llapi_layout_mirror_id_get(layout, &id);
9348 if ((__u16)id != cid->cid_exclude) {
9351 for (i = 0; i < cid->cid_count; i++) {
9352 /* already collected the mirror id */
9353 if (id == cid->cid_ids[i])
9354 return LLAPI_LAYOUT_ITER_CONT;
9356 cid->cid_ids[cid->cid_count] = id;
9360 return LLAPI_LAYOUT_ITER_CONT;
9363 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9365 struct llapi_layout *layout;
9366 struct collect_ids_data cid = { .cid_ids = ids,
9368 .cid_exclude = exclude_id, };
9371 layout = llapi_layout_get_by_fd(fd, 0);
9372 if (layout == NULL) {
9373 fprintf(stderr, "could not get layout\n");
9377 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9379 fprintf(stderr, "failed to iterate layout\n");
9380 llapi_layout_free(layout);
9384 llapi_layout_free(layout);
9386 return cid.cid_count;
9389 static inline int lfs_mirror_copy(int argc, char **argv)
9392 __u16 read_mirror_id = 0;
9393 __u16 ids[128] = { 0 };
9395 struct llapi_layout *layout = NULL;
9396 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9403 struct ll_ioc_lease *ioc = NULL;
9404 struct ll_ioc_lease_id *resync_ioc;
9406 struct option long_opts[] = {
9407 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9408 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9411 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9416 read_mirror_id = strtoul(optarg, &end, 0);
9417 if (*end != '\0' || read_mirror_id == 0) {
9419 "%s %s: invalid read mirror ID '%s'\n",
9420 progname, argv[0], optarg);
9425 if (!strcmp(optarg, "-1")) {
9426 /* specify all other mirrors */
9430 count = parse_mirror_ids((__u16 *)ids,
9438 fprintf(stderr, "%s: option '%s' unrecognized\n",
9439 progname, argv[optind - 1]);
9444 if (argc == optind) {
9445 fprintf(stderr, "%s %s: no mirrored file provided\n",
9448 } else if (argc > optind + 1) {
9449 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9453 if (read_mirror_id == 0) {
9455 "%s %s: no valid read mirror ID %d is provided\n",
9456 progname, argv[0], read_mirror_id);
9462 "%s %s: no write mirror ID is provided\n",
9467 for (i = 0; i < count; i++) {
9468 if (read_mirror_id == ids[i]) {
9470 "%s %s: read and write mirror ID cannot be the same\n",
9476 /* open mirror file */
9477 fname = argv[optind];
9479 fd = open(fname, O_DIRECT | O_RDWR);
9481 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9482 progname, argv[0], fname, strerror(errno));
9486 /* write to all other mirrors */
9487 if (ids[0] == (__u16)-1) {
9488 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9492 "%s %s: failed to get other mirror ids in '%s': %d\n",
9493 progname, argv[0], fname, rc);
9498 /* verify mirror id */
9499 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9502 "%s %s: cannot find mirror with ID %u in '%s'\n",
9503 progname, argv[0], read_mirror_id, fname);
9507 for (i = 0; i < count; i++) {
9508 rc = verify_mirror_id_by_fd(fd, ids[i]);
9511 "%s %s: cannot find mirror with ID %u in '%s'\n",
9512 progname, argv[0], ids[i], fname);
9517 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9520 "%s %s: cannot alloc comp id array for ioc: %s\n",
9521 progname, argv[0], strerror(errno));
9526 /* get stale component info */
9527 layout = llapi_layout_get_by_fd(fd, 0);
9528 if (layout == NULL) {
9529 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9530 progname, argv[0], fname, strerror(errno));
9534 comp_size = llapi_mirror_find_stale(layout, comp_array,
9535 ARRAY_SIZE(comp_array),
9537 llapi_layout_free(layout);
9538 if (comp_size < 0) {
9543 /* prepare target mirror components instantiation */
9544 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9545 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9546 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9548 resync_ioc->lil_mirror_id = ids[0];
9550 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9551 rc = llapi_lease_set(fd, ioc);
9554 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9555 progname, argv[0], fname, strerror(errno));
9559 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9562 fprintf(stderr, "%s %s: copy error: %d\n",
9563 progname, argv[0], rc);
9567 fprintf(stdout, "mirror copied successfully: ");
9568 for (i = 0; i < copied; i++)
9569 fprintf(stdout, "%d ", ids[i]);
9570 fprintf(stdout, "\n");
9572 ioc->lil_mode = LL_LEASE_UNLCK;
9573 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9575 for (i = 0; i < comp_size; i++) {
9578 for (j = 0; j < copied; j++) {
9579 if (comp_array[i].lrc_mirror_id != ids[j])
9582 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9586 rc = llapi_lease_set(fd, ioc);
9591 "%s %s: release lease lock of '%s' failed: %s\n",
9592 progname, argv[0], fname, strerror(errno));
9606 * struct verify_chunk - Mirror chunk to be verified.
9607 * @chunk: [start, end) of the chunk.
9608 * @mirror_count: Number of mirror ids in @mirror_id array.
9609 * @mirror_id: Array of valid mirror ids that cover the chunk.
9611 struct verify_chunk {
9612 struct lu_extent chunk;
9613 unsigned int mirror_count;
9614 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9618 * print_chunks() - Print chunk information.
9619 * @fname: Mirrored file name.
9620 * @chunks: Array of chunks.
9621 * @chunk_count: Number of chunks in @chunks array.
9623 * This function prints [start, end) of each chunk in @chunks
9624 * for mirrored file @fname, and also prints the valid mirror ids
9625 * that cover the chunk.
9630 void print_chunks(const char *fname, struct verify_chunk *chunks,
9636 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9637 for (i = 0; i < chunk_count; i++) {
9638 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9640 if (chunks[i].mirror_count == 0)
9641 fprintf(stdout, "\t[");
9643 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9644 for (j = 1; j < chunks[i].mirror_count; j++)
9645 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9647 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9649 fprintf(stdout, "\n");
9653 * print_checksums() - Print CRC-32 checksum values.
9654 * @chunk: A chunk and its corresponding valid mirror ids.
9655 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9657 * This function prints CRC-32 checksum values on @chunk for
9658 * each valid mirror that covers it.
9663 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9668 "CRC-32 checksum value for chunk "DEXT":\n",
9669 PEXT(&chunk->chunk));
9670 for (i = 0; i < chunk->mirror_count; i++)
9671 fprintf(stdout, "Mirror %u:\t%#lx\n",
9672 chunk->mirror_id[i], crc[i]);
9673 fprintf(stdout, "\n");
9677 * filter_mirror_id() - Filter specified mirror ids.
9678 * @chunks: Array of chunks.
9679 * @chunk_count: Number of chunks in @chunks array.
9680 * @mirror_ids: Specified mirror ids to be verified.
9681 * @ids_nr: Number of specified mirror ids.
9683 * This function scans valid mirror ids that cover each chunk in @chunks
9684 * and filters specified mirror ids.
9689 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9690 __u16 *mirror_ids, int ids_nr)
9695 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9696 unsigned int valid_count = 0;
9698 for (i = 0; i < chunk_count; i++) {
9699 if (chunks[i].mirror_count == 0)
9703 for (j = 0; j < ids_nr; j++) {
9704 for (k = 0; k < chunks[i].mirror_count; k++) {
9705 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9706 valid_id[valid_count] = mirror_ids[j];
9713 memcpy(chunks[i].mirror_id, valid_id,
9714 sizeof(__u16) * valid_count);
9715 chunks[i].mirror_count = valid_count;
9720 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9721 * @layout: Mirror component list.
9722 * @chunks: Array of chunks.
9723 * @chunks_size: Array size of @chunks.
9725 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9726 * to find out chunk segments and store them in @chunks array.
9728 * The @mirror_id array in each element of @chunks will store the valid
9729 * mirror ids that cover the chunk. If a mirror component covering the
9730 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9731 * will not be stored into the @mirror_id array, and the chunk for that
9732 * mirror will not be verified.
9734 * The @mirror_count in each element of @chunks will store the number of
9735 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9736 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9737 * indicates the chunk is valid in only one mirror. In both cases, the
9738 * chunk will not be verified.
9740 * Here is an example:
9743 * +------+-------------+--------------------+
9745 * +------+------+------+------+-------------+
9746 * | | S | S | | mirror2
9747 * +-------------+------+------+-------------+
9749 * prepared @chunks array will contain 5 elements:
9750 * (([0, 1M), [1, 2], 2),
9751 * ([1M, 2M), [1, 2], 2),
9752 * ([2M, 3M), [1], 1),
9753 * ([3M, 4M], [], 0),
9754 * ([4M, EOF), [2], 1))
9756 * Return: the actual array size of @chunks on success
9757 * or a negative error code on failure.
9760 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9761 struct verify_chunk *chunks,
9772 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9775 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9778 "%s: move to the first layout component: %s.\n",
9779 progname, strerror(errno));
9785 chunks[idx].chunk.e_end = LUSTRE_EOF;
9787 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9790 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9791 progname, strerror(errno));
9795 if (start > chunks[idx].chunk.e_start ||
9796 end <= chunks[idx].chunk.e_start)
9799 if (end < chunks[idx].chunk.e_end)
9800 chunks[idx].chunk.e_end = end;
9802 rc = llapi_layout_comp_flags_get(layout, &flags);
9805 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9806 progname, strerror(errno));
9810 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9813 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9816 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9817 progname, strerror(errno));
9821 chunks[idx].mirror_id[i] = mirror_id;
9823 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9825 "%s: mirror_id array is too small.\n",
9832 rc = llapi_layout_comp_use(layout,
9833 LLAPI_LAYOUT_COMP_USE_NEXT);
9836 "%s: move to the next layout component: %s.\n",
9837 progname, strerror(errno));
9840 } /* loop through all components */
9842 chunks[idx].mirror_count = i;
9844 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9848 if (idx >= chunks_size) {
9849 fprintf(stderr, "%s: chunks array is too small.\n",
9855 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9859 return rc < 0 ? rc : idx + 1;
9863 * lfs_mirror_verify_chunk() - Verify a chunk.
9864 * @fd: File descriptor of the mirrored file.
9865 * @file_size: Size of the mirrored file.
9866 * @chunk: A chunk and its corresponding valid mirror ids.
9867 * @verbose: Verbose mode.
9869 * This function verifies a @chunk contains exactly the same data
9870 * ammong the mirrors that cover it.
9872 * If @verbose is specified, then the function will print where the
9873 * differences are if the data do not match. Otherwise, it will
9874 * just return an error in that case.
9876 * Return: 0 on success or a negative error code on failure.
9879 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9880 struct verify_chunk *chunk, int verbose)
9882 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9884 size_t page_size = sysconf(_SC_PAGESIZE);
9890 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9897 rc = posix_memalign(&buf, page_size, buflen);
9898 if (rc) /* error code is returned directly */
9902 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9903 PEXT(&chunk->chunk));
9904 for (i = 0; i < chunk->mirror_count; i++)
9905 fprintf(stdout, " %u", chunk->mirror_id[i]);
9906 fprintf(stdout, "\n");
9910 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9911 pos = chunk->chunk.e_start;
9912 while (bytes_done < count) {
9913 /* compute initial CRC-32 checksum */
9914 crc = crc32(0L, Z_NULL, 0);
9915 memset(crc_array, 0, sizeof(crc_array));
9918 for (i = 0; i < chunk->mirror_count; i++) {
9919 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9921 if (bytes_read < 0) {
9924 "%s: failed to read data from mirror %u: %s.\n",
9925 progname, chunk->mirror_id[i],
9930 /* compute new CRC-32 checksum */
9931 crc_array[i] = crc32(crc, buf, bytes_read);
9935 print_checksums(chunk, crc_array);
9937 /* compare CRC-32 checksum values */
9938 for (i = 1; i < chunk->mirror_count; i++) {
9939 if (crc_array[i] != crc_array[0]) {
9943 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9944 progname, PEXT(&chunk->chunk),
9945 chunk->mirror_id[0],
9946 chunk->mirror_id[i]);
9951 bytes_done += bytes_read;
9954 if (verbose > 1 && rc == 0) {
9955 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9956 PEXT(&chunk->chunk));
9957 for (i = 0; i < chunk->mirror_count; i++)
9958 fprintf(stdout, " %u", chunk->mirror_id[i]);
9959 fprintf(stdout, " PASS\n\n");
9968 * lfs_mirror_verify_file() - Verify a mirrored file.
9969 * @fname: Mirrored file name.
9970 * @mirror_ids: Specified mirror ids to be verified.
9971 * @ids_nr: Number of specified mirror ids.
9972 * @verbose: Verbose mode.
9974 * This function verifies that each SYNC mirror of a mirrored file
9975 * specified by @fname contains exactly the same data.
9977 * If @mirror_ids is specified, then the function will verify the
9978 * mirrors specified by @mirror_ids contain exactly the same data.
9980 * If @verbose is specified, then the function will print where the
9981 * differences are if the data do not match. Otherwise, it will
9982 * just return an error in that case.
9984 * Return: 0 on success or a negative error code on failure.
9987 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9990 struct verify_chunk chunks_array[1024] = { };
9991 struct llapi_layout *layout = NULL;
9995 int chunk_count = 0;
10001 if (stat(fname, &stbuf) < 0) {
10002 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10003 progname, fname, strerror(errno));
10008 if (!S_ISREG(stbuf.st_mode)) {
10009 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10015 if (stbuf.st_size == 0) {
10017 fprintf(stdout, "%s: '%s' file size is 0.\n",
10023 fd = open(fname, O_DIRECT | O_RDONLY);
10025 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10026 progname, fname, strerror(errno));
10031 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10033 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10034 progname, fname, strerror(errno));
10038 layout = llapi_layout_get_by_fd(fd, 0);
10039 if (layout == NULL) {
10040 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10041 progname, fname, strerror(errno));
10043 llapi_lease_release(fd);
10047 rc = llapi_layout_flags_get(layout, &flr_state);
10049 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10050 progname, fname, strerror(errno));
10055 flr_state &= LCM_FL_FLR_MASK;
10056 switch (flr_state) {
10059 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10060 progname, fname, llapi_layout_flags_string(flr_state));
10066 /* find out mirror chunks to be verified */
10067 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10068 ARRAY_SIZE(chunks_array));
10069 if (chunk_count < 0) {
10075 /* filter specified mirror ids */
10076 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10079 print_chunks(fname, chunks_array, chunk_count);
10081 for (idx = 0; idx < chunk_count; idx++) {
10082 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10085 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10087 PEXT(&chunks_array[idx].chunk),
10088 (unsigned long long)stbuf.st_size);
10092 if (chunks_array[idx].mirror_count == 0) {
10094 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10096 PEXT(&chunks_array[idx].chunk));
10098 fprintf(stderr, "skipped\n");
10102 fprintf(stderr, "failed\n");
10106 if (chunks_array[idx].mirror_count == 1) {
10109 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10111 PEXT(&chunks_array[idx].chunk),
10112 chunks_array[idx].mirror_id[0]);
10116 rc = llapi_lease_check(fd);
10117 if (rc != LL_LEASE_RDLCK) {
10118 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10123 /* verify one chunk */
10124 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10125 &chunks_array[idx], verbose);
10139 llapi_layout_free(layout);
10140 llapi_lease_release(fd);
10148 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10149 * @argc: The count of lfs mirror verify command line arguments.
10150 * @argv: Array of strings for lfs mirror verify command line arguments.
10152 * This function parses lfs mirror verify command and verifies the
10153 * specified mirrored file(s).
10155 * Return: 0 on success or a negative error code on failure.
10157 static inline int lfs_mirror_verify(int argc, char **argv)
10159 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10165 char cmd[PATH_MAX];
10167 struct option long_opts[] = {
10168 { .val = 'o', .name = "only", .has_arg = required_argument },
10169 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10170 { .name = NULL } };
10172 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10174 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10177 rc = parse_mirror_ids(mirror_ids,
10178 ARRAY_SIZE(mirror_ids),
10182 "%s: bad mirror ids '%s'.\n",
10189 "%s: at least 2 mirror ids needed with '--only' option.\n",
10199 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10200 progname, argv[optind - 1]);
10206 if (argc == optind) {
10207 fprintf(stderr, "%s: no file name given.\n", progname);
10212 if (ids_nr > 0 && argc > optind + 1) {
10214 "%s: '--only' cannot be used upon multiple files.\n",
10222 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10228 for (; optind < argc; optind++) {
10229 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10239 * lfs_mirror() - Parse and execute lfs mirror commands.
10240 * @argc: The count of lfs mirror command line arguments.
10241 * @argv: Array of strings for lfs mirror command line arguments.
10243 * This function parses lfs mirror commands and performs the
10244 * corresponding functions specified in mirror_cmdlist[].
10246 * Return: 0 on success or an error code on failure.
10248 static int lfs_mirror(int argc, char **argv)
10250 char cmd[PATH_MAX];
10253 setlinebuf(stdout);
10255 Parser_init("lfs-mirror > ", mirror_cmdlist);
10257 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10259 program_invocation_short_name = cmd;
10261 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10263 rc = Parser_commands();
10265 return rc < 0 ? -rc : rc;
10268 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10270 #if __BYTE_ORDER == __BIG_ENDIAN
10271 __swab16s(&attrs->lsa_valid);
10272 __swab64s(&attrs->lsa_size);
10273 __swab64s(&attrs->lsa_blocks);
10277 enum lfs_som_type {
10278 LFS_SOM_SIZE = 0x1,
10279 LFS_SOM_BLOCKS = 0x2,
10280 LFS_SOM_FLAGS = 0x4,
10281 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10285 static int lfs_getsom(int argc, char **argv)
10288 struct lustre_som_attrs *attrs;
10289 char buf[sizeof(*attrs) + 64];
10290 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10293 while ((c = getopt(argc, argv, "sbf")) != -1) {
10296 type = LFS_SOM_SIZE;
10299 type = LFS_SOM_BLOCKS;
10302 type = LFS_SOM_FLAGS;
10305 fprintf(stderr, "%s: invalid option '%c'\n",
10315 fprintf(stderr, "%s: %s\n",
10316 progname, argc == 0 ? "miss file target" :
10317 "input more than 2 files");
10322 attrs = (void *)buf;
10323 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10326 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10327 argv[0], strerror(errno), errno);
10331 lustre_som_swab(attrs);
10334 case LFS_SOM_ATTR_ALL:
10335 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10336 path, attrs->lsa_size, attrs->lsa_blocks,
10340 printf("%llu\n", attrs->lsa_size);
10342 case LFS_SOM_BLOCKS:
10343 printf("%llu\n", attrs->lsa_blocks);
10345 case LFS_SOM_FLAGS:
10346 printf("%x\n", attrs->lsa_valid);
10349 fprintf(stderr, "%s: unknown option\n", progname);
10357 * lfs_mirror_list_commands() - List lfs mirror commands.
10358 * @argc: The count of command line arguments.
10359 * @argv: Array of strings for command line arguments.
10361 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10363 * Return: 0 on success.
10365 static int lfs_mirror_list_commands(int argc, char **argv)
10367 char buffer[81] = "";
10369 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10375 static int lfs_pcc_attach(int argc, char **argv)
10377 struct option long_opts[] = {
10378 { .val = 'i', .name = "id", .has_arg = required_argument },
10379 { .name = NULL } };
10382 __u32 archive_id = 0;
10385 char fullpath[PATH_MAX];
10386 enum lu_pcc_type type = LU_PCC_READWRITE;
10389 while ((c = getopt_long(argc, argv, "i:",
10390 long_opts, NULL)) != -1) {
10393 archive_id = strtoul(optarg, &end, 0);
10394 if (*end != '\0' || archive_id == 0) {
10395 fprintf(stderr, "error: %s: bad archive ID "
10396 "'%s'\n", argv[0], optarg);
10403 fprintf(stderr, "%s: option '%s' unrecognized\n",
10404 argv[0], argv[optind - 1]);
10409 if (argc <= optind) {
10410 fprintf(stderr, "%s: must specify one or more file names\n",
10415 while (optind < argc) {
10418 path = argv[optind++];
10419 if (realpath(path, fullpath) == NULL) {
10420 fprintf(stderr, "%s: could not find path '%s': %s\n",
10421 argv[0], path, strerror(errno));
10427 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10429 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10430 "with archive ID '%u': %s\n", argv[0],
10431 path, archive_id, strerror(-rc2));
10439 static int lfs_pcc_attach_fid(int argc, char **argv)
10441 struct option long_opts[] = {
10442 { .val = 'i', .name = "id", .has_arg = required_argument },
10443 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10444 { .name = NULL } };
10445 char short_opts[] = "i:m:";
10448 __u32 archive_id = 0;
10450 const char *mntpath = NULL;
10451 const char *fidstr;
10452 enum lu_pcc_type type = LU_PCC_READWRITE;
10455 while ((c = getopt_long(argc, argv, short_opts,
10456 long_opts, NULL)) != -1) {
10459 archive_id = strtoul(optarg, &end, 0);
10460 if (*end != '\0') {
10461 fprintf(stderr, "error: %s: bad archive ID "
10462 "'%s'\n", argv[0], optarg);
10472 fprintf(stderr, "%s: option '%s' unrecognized\n",
10473 argv[0], argv[optind - 1]);
10478 if (archive_id == 0) {
10479 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10483 if (mntpath == NULL) {
10484 fprintf(stderr, "%s: must specify Lustre mount point\n",
10489 if (argc <= optind) {
10490 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10494 while (optind < argc) {
10497 fidstr = argv[optind++];
10499 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10502 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10503 "with archive ID '%u': %s\n", argv[0],
10504 fidstr, mntpath, archive_id, strerror(rc2));
10506 if (rc == 0 && rc2 < 0)
10512 static int lfs_pcc_detach(int argc, char **argv)
10514 struct option long_opts[] = {
10515 { .val = 'k', .name = "keep", .has_arg = no_argument },
10516 { .name = NULL } };
10517 char short_opts[] = "k";
10521 char fullpath[PATH_MAX];
10522 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10525 while ((c = getopt_long(argc, argv, short_opts,
10526 long_opts, NULL)) != -1) {
10529 detach_opt = PCC_DETACH_OPT_NONE;
10534 fprintf(stderr, "%s: option '%s' unrecognized\n",
10535 argv[0], argv[optind - 1]);
10540 while (optind < argc) {
10543 path = argv[optind++];
10544 if (realpath(path, fullpath) == NULL) {
10545 fprintf(stderr, "%s: could not find path '%s': %s\n",
10546 argv[0], path, strerror(errno));
10552 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
10555 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
10556 "%s\n", argv[0], path, strerror(errno));
10564 static int lfs_pcc_detach_fid(int argc, char **argv)
10566 struct option long_opts[] = {
10567 { .val = 'k', .name = "keep", .has_arg = no_argument },
10568 { .name = NULL } };
10569 char short_opts[] = "k";
10573 const char *mntpath;
10574 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10577 while ((c = getopt_long(argc, argv, short_opts,
10578 long_opts, NULL)) != -1) {
10581 detach_opt = PCC_DETACH_OPT_NONE;
10586 fprintf(stderr, "%s: option '%s' unrecognized\n",
10587 argv[0], argv[optind - 1]);
10592 mntpath = argv[optind++];
10594 while (optind < argc) {
10597 fid = argv[optind++];
10599 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
10601 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
10602 "from PCC: %s\n", argv[0], fid, mntpath,
10611 static int lfs_pcc_state(int argc, char **argv)
10615 char fullpath[PATH_MAX];
10616 struct lu_pcc_state state;
10621 fprintf(stderr, "%s: must specify one or more file names\n",
10626 while (optind < argc) {
10629 path = argv[optind++];
10630 if (realpath(path, fullpath) == NULL) {
10631 fprintf(stderr, "%s: could not find path '%s': %s\n",
10632 argv[0], path, strerror(errno));
10638 rc2 = llapi_pcc_state_get(fullpath, &state);
10642 fprintf(stderr, "%s: cannot get PCC state of '%s': "
10643 "%s\n", argv[0], path, strerror(-rc2));
10647 printf("file: %s", path);
10648 printf(", type: %s", pcc_type2string(state.pccs_type));
10649 if (state.pccs_type == LU_PCC_NONE &&
10650 state.pccs_open_count == 0) {
10655 printf(", PCC file: %s", state.pccs_path);
10656 printf(", user number: %u", state.pccs_open_count);
10657 printf(", flags: %x", state.pccs_flags);
10664 * lfs_pcc_list_commands() - List lfs pcc commands.
10665 * @argc: The count of command line arguments.
10666 * @argv: Array of strings for command line arguments.
10668 * This function lists lfs pcc commands defined in pcc_cmdlist[].
10670 * Return: 0 on success.
10672 static int lfs_pcc_list_commands(int argc, char **argv)
10674 char buffer[81] = "";
10676 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
10683 * lfs_pcc() - Parse and execute lfs pcc commands.
10684 * @argc: The count of lfs pcc command line arguments.
10685 * @argv: Array of strings for lfs pcc command line arguments.
10687 * This function parses lfs pcc commands and performs the
10688 * corresponding functions specified in pcc_cmdlist[].
10690 * Return: 0 on success or an error code on failure.
10692 static int lfs_pcc(int argc, char **argv)
10694 char cmd[PATH_MAX];
10697 setlinebuf(stdout);
10699 Parser_init("lfs-pcc > ", pcc_cmdlist);
10701 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10703 program_invocation_short_name = cmd;
10705 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
10707 rc = Parser_commands();
10709 return rc < 0 ? -rc : rc;
10712 static int lfs_list_commands(int argc, char **argv)
10714 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10716 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10721 int main(int argc, char **argv)
10725 /* Ensure that liblustreapi constructor has run */
10726 if (!llapi_liblustreapi_initialized())
10727 fprintf(stderr, "liblustreapi was not properly initialized\n");
10729 setlinebuf(stdout);
10732 Parser_init("lfs > ", cmdlist);
10734 progname = program_invocation_short_name; /* Used in error messages */
10736 llapi_set_command_name(argv[1]);
10737 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10738 llapi_clear_command_name();
10740 rc = Parser_commands();
10743 return rc < 0 ? -rc : rc;
10746 #ifdef _LUSTRE_IDL_H_
10747 /* Everything we need here should be included by lustreapi.h. */
10748 # error "lfs should not depend on lustre_idl.h"
10749 #endif /* _LUSTRE_IDL_H_ */