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);
131 enum setstripe_origin {
139 static int lfs_setstripe_internal(int argc, char **argv,
140 enum setstripe_origin opc);
142 static inline int lfs_setstripe(int argc, char **argv)
144 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
146 static inline int lfs_setstripe_migrate(int argc, char **argv)
148 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
150 static inline int lfs_mirror_create(int argc, char **argv)
152 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
154 static inline int lfs_mirror_extend(int argc, char **argv)
156 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
158 static inline int lfs_mirror_split(int argc, char **argv)
160 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
163 /* Setstripe and migrate share mostly the same parameters */
164 #define SSM_CMD_COMMON(cmd) \
165 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
166 " [--stripe-count|-c <stripe_count>]\n" \
167 " [--stripe-index|-i <start_ost_idx>]\n" \
168 " [--stripe-size|-S <stripe_size>]\n" \
169 " [--layout|-L <pattern>]\n" \
170 " [--pool|-p <pool_name>]\n" \
171 " [--ost|-o <ost_indices>]\n" \
172 " [--yaml|-y <yaml_template_file>]\n" \
173 " [--copy=<lustre_src>]\n"
175 #define SSM_HELP_COMMON \
176 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
177 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
178 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
179 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
180 "\t respectively)\n" \
181 "\tpool_name: Name of OST pool to use (default none)\n" \
182 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
183 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
184 "\t Indices be specified in a format of:\n" \
185 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
187 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
188 "\t If --pool is set with --ost, then the OSTs\n" \
189 "\t must be the members of the pool.\n" \
190 "\tcomp_end: Extent end of component, start after previous end.\n"\
191 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
192 "\t respectively, -1 for EOF). Must be a multiple of\n"\
193 "\t stripe_size.\n" \
194 "\tyaml_template_file:\n" \
195 "\t YAML layout template file, can't be used with -c,\n" \
196 "\t -i, -S, -p, -o, or -E arguments.\n" \
197 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
198 "\t another lustre file or directory, can't used with\n" \
199 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
201 #define MIRROR_CREATE_HELP \
202 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
203 "\t setstripe layout options\n" \
204 "\t It defaults to 1 if not specified; if specified,\n" \
205 "\t it must follow the option without a space.\n" \
206 "\t The option can also be repeated multiple times to\n" \
207 "\t separate mirrors that have different layouts.\n" \
208 "\tsetstripe options: Mirror layout\n" \
209 "\t It can be a plain layout or a composite layout.\n" \
210 "\t If not specified, the stripe options inherited\n" \
211 "\t from the previous component will be used.\n" \
212 "\tflags: set flags to the component of the current mirror.\n" \
213 "\t Only \"prefer\" flag is supported so far.\n"
215 #define MIRROR_EXTEND_HELP \
217 "\tvictim_file: The layout of victim_file will be split and used\n" \
218 "\t as a mirror added to the mirrored file.\n" \
219 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
220 "\t from victim file(s) in case the victim file(s)\n" \
221 "\t contains the same data as the original mirrored\n" \
224 #define MIRROR_EXTEND_USAGE \
225 " <--mirror-count|-N[mirror_count]>\n" \
226 " [setstripe options|-f <victim_file>]\n" \
229 #define SETSTRIPE_USAGE \
230 SSM_CMD_COMMON("setstripe") \
231 MIRROR_EXTEND_USAGE \
232 " <directory|filename>\n" \
236 #define MIGRATE_USAGE \
237 SSM_CMD_COMMON("migrate ") \
238 " [--block|-b] [--non-block|-n]\n" \
239 " [--non-direct|-D] [--verbose|-v]\n" \
243 "\tblock: Block file access during data migration (default)\n" \
244 "\tnon-block: Abort migrations if concurrent access is detected\n" \
245 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
246 "\tverbose: Print each filename as it is migrated\n" \
248 #define SETDIRSTRIPE_USAGE \
249 " [--mdt-count|-c stripe_count>\n" \
250 " [--mdt-hash|-H mdt_hash]\n" \
251 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
252 " [--default|-D] [--mode|-o mode] <dir>\n" \
253 "\tstripe_count: stripe count of the striped directory\n" \
254 "\tmdt_index: MDT index of first stripe\n" \
255 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
256 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
257 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
258 "\tdefault_stripe: set default dirstripe of the directory\n" \
259 "\tmode: the file access permission of the directory (octal)\n" \
260 "To create dir with a foreign (free format) layout :\n" \
261 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
262 "[--mode|-m mode] [--flags <hex>] <dir>\n" \
263 "\tmode: the mode of the directory\n" \
264 "\tforeign_type: none or daos\n"
267 * command_t mirror_cmdlist - lfs mirror commands.
269 command_t mirror_cmdlist[] = {
270 { .pc_name = "create", .pc_func = lfs_mirror_create,
271 .pc_help = "Create a mirrored file.\n"
272 "usage: lfs mirror create "
273 "<--mirror-count|-N[mirror_count]> "
274 "[setstripe options] ... <filename|directory>\n"
275 MIRROR_CREATE_HELP },
276 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
277 .pc_help = "Extend a mirrored file.\n"
278 "usage: lfs mirror extend "
279 "<--mirror-count|-N[mirror_count]> [--no-verify] "
280 "[setstripe options|-f <victim_file>] ... <filename>\n"
281 MIRROR_EXTEND_HELP },
282 { .pc_name = "split", .pc_func = lfs_mirror_split,
283 .pc_help = "Split a mirrored file.\n"
284 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
285 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
286 "\t [-f <new_file>] <mirrored file>\n"
287 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
288 "\t can be fetched by lfs getstripe command.\n"
289 "\tcomp_id: Unique component ID within a mirror.\n"
290 "\tnew_file: This option indicates the layout of the split\n"
291 "\t mirror will be stored into. If not specified,\n"
292 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
293 "\t will be used.\n" },
294 { .pc_name = "read", .pc_func = lfs_mirror_read,
295 .pc_help = "Read the content of a specified mirror of a file.\n"
296 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
297 "[--outfile|-o <output_file>] <mirrored_file>\n" },
298 { .pc_name = "write", .pc_func = lfs_mirror_write,
299 .pc_help = "Write to a specified mirror of a file.\n"
300 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
301 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
302 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
303 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
304 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
305 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
306 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
307 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
308 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
309 "<mirrored file> [<mirrored file2>...]\n"},
310 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
311 .pc_help = "Verify mirrored file(s).\n"
312 "usage: lfs mirror verify "
313 "[--only <mirror_id,mirror_id2[,...]>] "
314 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
315 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
316 .pc_help = "list commands supported by lfs mirror"},
317 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
318 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
319 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
323 /* all available commands */
324 command_t cmdlist[] = {
325 {"setstripe", lfs_setstripe, 0,
326 "To create a file with specified striping/composite layout, or\n"
327 "create/replace the default layout on an existing directory:\n"
328 SSM_CMD_COMMON("setstripe")
330 " <directory|filename>\n"
332 "To add component(s) to an existing composite file:\n"
333 SSM_CMD_COMMON("setstripe --component-add")
335 "To totally delete the default striping from an existing directory:\n"
336 "usage: setstripe -d <directory>\n"
338 "To create a mirrored file or set s default mirror layout on a directory:\n"
339 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
341 "To delete the last component(s) from an existing composite file\n"
342 "(note that this will also delete any data in those components):\n"
343 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
344 " [--component-flags|-F <comp_flags>]\n"
346 "\tcomp_id: Unique component ID to delete\n"
347 "\tcomp_flags: 'init' indicating all instantiated components\n"
348 "\t '^init' indicating all uninstantiated components\n"
349 "\t-I and -F cannot be specified at the same time\n"
350 "To create a file with a foreign (free format) layout:\n"
351 "usage: setstripe --foreign[=<foreign_type>]\n"
352 " --xattr|-x <layout_string> [--flags <hex>]\n"
353 " [--mode <mode>] <filename>\n"},
354 {"getstripe", lfs_getstripe, 0,
355 "To list the layout pattern for a given file or files in a\n"
356 "directory or recursively for all files in a directory tree.\n"
357 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
358 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
359 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
360 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
361 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
362 " [--component-id[=comp_id]|-I[comp_id]]\n"
363 " [--component-flags[=comp_flags]]\n"
364 " [--component-count]\n"
365 " [--component-start[=[+-]comp_start]]\n"
366 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
367 " [[!] --mirror-index=[+-]<index> |\n"
368 " [!] --mirror-id=[+-]<id>]\n"
369 " <directory|filename> ..."},
370 {"setdirstripe", lfs_setdirstripe, 0,
371 "To create a striped directory on a specified MDT. This can only\n"
372 "be done on MDT0 with the right of administrator.\n"
373 "usage: setdirstripe [OPTION] <directory>\n"
375 {"getdirstripe", lfs_getdirstripe, 0,
376 "To list the layout pattern info for a given directory\n"
377 "or recursively for all directories in a directory tree.\n"
378 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
379 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
380 " [--recursive|-r] [--yaml|-y]\n"
381 " [--verbose|-v] [--default|-D] <dir> ..."},
382 {"mkdir", lfs_setdirstripe, 0,
383 "To create a striped directory on a specified MDT. This can only\n"
384 "be done on MDT0 with the right of administrator.\n"
385 "usage: mkdir [OPTION] <directory>\n"
387 {"rm_entry", lfs_rmentry, 0,
388 "To remove the name entry of the remote directory. Note: This\n"
389 "command will only delete the name entry, i.e. the remote directory\n"
390 "will become inaccessable after this command. This can only be done\n"
391 "by the administrator\n"
392 "usage: rm_entry <dir>\n"},
393 {"pool_list", lfs_poollist, 0,
394 "List pools or pool OSTs\n"
395 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
396 {"find", lfs_find, 0,
397 "find files matching given attributes recursively in directory tree.\n"
398 "usage: find <directory|filename> ...\n"
399 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
400 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
401 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
402 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
403 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
404 " [[!] --stripe-count|-c [+-]<stripes>]\n"
405 " [[!] --stripe-index|-i <index,...>]\n"
406 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
407 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
408 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
409 " [[!] --projid <projid>]\n"
410 " [[!] --foreign[=<foreign_type>]]\n"
411 " [[!] --layout|-L released,raid0,mdt]\n"
412 " [[!] --foreign[=<foreign_type>]]\n"
413 " [[!] --component-count [+-]<comp_cnt>]\n"
414 " [[!] --component-start [+-]N[kMGTPE]]\n"
415 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
416 " [[!] --component-flags <comp_flags>]\n"
417 " [[!] --mirror-count|-N [+-]<n>]\n"
418 " [[!] --mirror-state <[^]state>]\n"
419 " [[!] --mdt-count|-T [+-]<stripes>]\n"
420 " [[!] --mdt-hash|-H <hashtype>\n"
421 " [[!] --mdt-index|-m <uuid|index,...>]\n"
422 "\t !: used before an option indicates 'NOT' requested attribute\n"
423 "\t -: used before a value indicates less than requested value\n"
424 "\t +: used before a value indicates more than requested value\n"
425 "\thashtype: hash type of the striped directory.\n"
426 "\t fnv_1a_64 FNV-1a hash algorithm\n"
427 "\t all_char sum of characters % MDT_COUNT\n"},
428 {"check", lfs_check, 0,
429 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
430 "or all the servers (MGTs, MDTs and OSTs).\n"
431 "usage: check <mgts|osts|mdts|all>"},
432 {"osts", lfs_osts, 0, "list OSTs connected to client "
433 "[for specified path only]\n" "usage: osts [path]"},
434 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
435 "[for specified path only]\n" "usage: mdts [path]"},
437 "report filesystem disk space usage or inodes usage "
438 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
439 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
440 {"getname", lfs_getname, 0,
441 "list instances and specified mount points [for specified path only]\n"
442 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
443 #ifdef HAVE_SYS_QUOTA_H
444 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
445 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
446 " -b <block-softlimit> -B <block-hardlimit>\n"
447 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
448 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
449 " [--block-softlimit <block-softlimit>]\n"
450 " [--block-hardlimit <block-hardlimit>]\n"
451 " [--inode-softlimit <inode-softlimit>]\n"
452 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
453 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
454 " [--block-grace <block-grace>]\n"
455 " [--inode-grace <inode-grace>] <filesystem>\n"
456 " setquota <-U|-G|-P>\n"
457 " -b <block-softlimit> -B <block-hardlimit>\n"
458 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
459 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
460 " [--block-softlimit <block-softlimit>]\n"
461 " [--block-hardlimit <block-hardlimit>]\n"
462 " [--inode-softlimit <inode-softlimit>]\n"
463 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
464 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
466 " -b can be used instead of --block-softlimit/--block-grace\n"
467 " -B can be used instead of --block-hardlimit\n"
468 " -i can be used instead of --inode-softlimit/--inode-grace\n"
469 " -I can be used instead of --inode-hardlimit\n"
470 " -d can be used instead of --default\n\n"
471 "Note: The total quota space will be split into many qunits and\n"
472 " balanced over all server targets, the minimal qunit size is\n"
473 " 1M bytes for block space and 1K inodes for inode space.\n\n"
474 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
475 " Quota space rebalancing process will stop when this mininum\n"
476 " value is reached. As a result, quota exceeded can be returned\n"
477 " while many targets still have 1MB or 1K inodes of spare\n"
479 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
480 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
482 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
483 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
484 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
485 {"project", lfs_project, 0,
486 "Change or list project attribute for specified file or directory.\n"
487 "usage: project [-d|-r] <file|directory...>\n"
488 " list project ID and flags on file(s) or directories\n"
489 " project [-p id] [-s] [-r] <file|directory...>\n"
490 " set project ID and/or inherit flag for specified file(s) or directories\n"
491 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
492 " check project ID and flags on file(s) or directories, print outliers\n"
493 " project -C [-r] [-k] <file|directory...>\n"
494 " clear the project inherit flag and ID on the file or directory\n"
497 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
498 "usage: flushctx [-k] [mountpoint...]"},
499 {"changelog", lfs_changelog, 0,
500 "Show the metadata changes on an MDT."
501 "\nusage: changelog <mdtname> [startrec [endrec]]"},
502 {"changelog_clear", lfs_changelog_clear, 0,
503 "Indicate that old changelog records up to <endrec> are no longer of "
504 "interest to consumer <id>, allowing the system to free up space.\n"
505 "An <endrec> of 0 means all records.\n"
506 "usage: changelog_clear <mdtname> <id> <endrec>"},
507 {"fid2path", lfs_fid2path, 0,
508 "Resolve the full path(s) for given FID(s). For a specific hardlink "
509 "specify link number <linkno>.\n"
510 /* "For a historical link name, specify changelog record <recno>.\n" */
511 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
512 /* [ --rec <recno> ] */ },
513 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
514 "usage: path2fid [--parents] <path> ..."},
515 {"data_version", lfs_data_version, 0, "Display file data version for "
516 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
517 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
518 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
519 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
520 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
521 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
522 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
524 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
525 "[--archived] [--lost] <file> ..."},
526 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
527 "given files.\n" "usage: hsm_action <file> ..."},
528 {"hsm_archive", lfs_hsm_archive, 0,
529 "Archive file to external storage.\n"
530 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
532 {"hsm_restore", lfs_hsm_restore, 0,
533 "Restore file from external storage.\n"
534 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
535 {"hsm_release", lfs_hsm_release, 0,
536 "Release files from Lustre.\n"
537 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
538 {"hsm_remove", lfs_hsm_remove, 0,
539 "Remove file copy from external storage.\n"
540 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
542 " (FILE [FILE ...] | "
543 "--mntpath MOUNTPATH FID [FID ...])\n"
545 "Note: To remove an archived copy of a file already deleted from a "
547 "--mntpath option and a list of FIDs must be specified"
549 {"hsm_cancel", lfs_hsm_cancel, 0,
550 "Cancel requests related to specified files.\n"
551 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
552 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
553 "usage: swap_layouts <path1> <path2>"},
554 {"migrate", lfs_setstripe_migrate, 0,
555 "migrate a directory between MDTs.\n"
556 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
557 " [--mdt-hash|-H] <hash_type>\n"
558 " [--mdt-index|-m] <start_mdt_index>\n"
561 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
562 " it's the MDT index of first stripe\n"
563 "\tmdt_count: number of MDTs to stripe a directory over\n"
564 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
565 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
566 " all_char sum of characters % MDT_COUNT\n"
568 "migrate file objects from one OST "
569 "layout\nto another (may be not safe with concurent writes).\n"
571 "[--stripe-count|-c] <stripe_count>\n"
572 " [--stripe-index|-i] <start_ost_index>\n"
573 " [--stripe-size|-S] <stripe_size>\n"
574 " [--pool|-p] <pool_name>\n"
575 " [--ost|-o] <ost_indices>\n"
577 " [--non-block|-n]\n"
578 " [--non-direct|-D]\n"
579 " <file|directory>\n"
580 "\tstripe_count: number of OSTs to stripe a file over\n"
581 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
582 "\tstripe_size: number of bytes to store before moving to the next OST\n"
583 "\tpool_name: name of the predefined pool of OSTs\n"
584 "\tost_indices: OSTs to stripe over, in order\n"
585 "\tblock: Block file access during data migration (default)\n"
586 "\tnon-block: Abort migrations if concurrent access is detected\n"
587 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
589 "To move directories between MDTs. This command is deprecated, "
590 "use \"migrate\" instead.\n"
591 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
593 {"ladvise", lfs_ladvise, 0,
594 "Provide servers with advice about access patterns for a file.\n"
595 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
596 " [--background|-b] [--unset|-u]\n\n"
597 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
598 " {[--mode|-m [READ,WRITE]}\n"
600 {"mirror", lfs_mirror, mirror_cmdlist,
601 "lfs commands used to manage files with mirrored components:\n"
602 "lfs mirror create - create a mirrored file or directory\n"
603 "lfs mirror extend - add mirror(s) to an existing file\n"
604 "lfs mirror split - split a mirror from an existing mirrored file\n"
605 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
606 "lfs mirror read - read a mirror content of a mirrored file\n"
607 "lfs mirror write - write to a mirror of a mirrored file\n"
608 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
609 "lfs mirror verify - verify mirrored file(s)\n"},
610 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
611 "usage: getsom [-s] [-b] [-f] <path>\n"
612 "\t-s: Only show the size value of the SOM data for a given file\n"
613 "\t-b: Only show the blocks value of the SOM data for a given file\n"
614 "\t-f: Only show the flags value of the SOM data for a given file\n"},
615 {"heat_get", lfs_heat_get, 0,
616 "To get heat of files.\n"
617 "usage: heat_get <file> ...\n"},
618 {"heat_set", lfs_heat_set, 0,
619 "To set heat flags of files.\n"
620 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
621 "\t--clear|-c: Clear file heat for given files\n"
622 "\t--off|-o: Turn off file heat for given files\n"
623 "\t--on|-O: Turn on file heat for given files\n"},
624 {"help", Parser_help, 0, "help"},
625 {"exit", Parser_quit, 0, "quit"},
626 {"quit", Parser_quit, 0, "quit"},
627 {"--version", Parser_version, 0,
628 "output build version of the utility and exit"},
629 {"--list-commands", lfs_list_commands, 0,
630 "list commands supported by the utility and exit"},
635 static int check_hashtype(const char *hashtype)
639 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
640 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
646 static uint32_t check_foreign_type_name(const char *foreign_type_name)
650 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
651 if (lu_foreign_types[i].lft_name == NULL)
653 if (strcmp(foreign_type_name,
654 lu_foreign_types[i].lft_name) == 0)
655 return lu_foreign_types[i].lft_type;
658 return LU_FOREIGN_TYPE_UNKNOWN;
661 static const char *error_loc = "syserror";
664 MIGRATION_NONBLOCK = 0x0001,
665 MIGRATION_MIRROR = 0x0002,
666 MIGRATION_NONDIRECT = 0x0004,
667 MIGRATION_VERBOSE = 0x0008,
670 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
671 struct llapi_layout *layout);
674 migrate_open_files(const char *name, __u64 migration_flags,
675 const struct llapi_stripe_param *param,
676 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
683 char parent[PATH_MAX];
684 char volatile_file[PATH_MAX];
690 if (param == NULL && layout == NULL) {
691 error_loc = "layout information";
695 /* search for file directory pathname */
696 if (strlen(name) > sizeof(parent) - 1) {
697 error_loc = "source file name";
701 strncpy(parent, name, sizeof(parent));
702 ptr = strrchr(parent, '/');
704 if (getcwd(parent, sizeof(parent)) == NULL) {
705 error_loc = "getcwd";
709 if (ptr == parent) /* leading '/' */
714 /* open file, direct io */
715 /* even if the file is only read, WR mode is nedeed to allow
716 * layout swap on fd */
718 if (!(migration_flags & MIGRATION_NONDIRECT))
720 fd = open(name, rflags);
723 error_loc = "cannot open source file";
727 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
729 error_loc = "cannot get MDT index";
734 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
735 mode_t open_mode = S_IRUSR | S_IWUSR;
737 random_value = random();
738 rc = snprintf(volatile_file, sizeof(volatile_file),
739 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
740 mdt_index, random_value);
741 if (rc >= sizeof(volatile_file)) {
746 /* create, open a volatile file, use caching (ie no directio) */
748 fdv = llapi_file_open_param(volatile_file, open_flags,
751 fdv = lfs_component_create(volatile_file, open_flags,
753 } while (fdv < 0 && (rc = fdv) == -EEXIST);
756 error_loc = "cannot create volatile file";
760 /* In case the MDT does not support creation of volatile files
761 * we should try to unlink it. */
762 (void)unlink(volatile_file);
764 /* Not-owner (root?) special case.
765 * Need to set owner/group of volatile file like original.
766 * This will allow to pass related check during layout_swap.
771 error_loc = "cannot stat source file";
775 rc = fstat(fdv, &stv);
778 error_loc = "cannot stat volatile";
782 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
783 rc = fchown(fdv, st.st_uid, st.st_gid);
786 error_loc = "cannot change ownwership of volatile";
805 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
807 struct llapi_layout *layout;
808 size_t buf_size = 4 * 1024 * 1024;
817 layout = llapi_layout_get_by_fd(fd_src, 0);
818 if (layout != NULL) {
819 uint64_t stripe_size;
821 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
823 buf_size = stripe_size;
825 llapi_layout_free(layout);
828 /* Use a page-aligned buffer for direct I/O */
829 rc = posix_memalign(&buf, getpagesize(), buf_size);
834 /* read new data only if we have written all
835 * previously read data */
838 rc = check_file(fd_src);
843 rsize = read(fd_src, buf, buf_size);
855 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
874 static int migrate_copy_timestamps(int fd, int fdv)
878 if (fstat(fd, &st) == 0) {
879 struct timeval tv[2] = {
880 {.tv_sec = st.st_atime},
881 {.tv_sec = st.st_mtime}
884 return futimes(fdv, tv);
890 static int migrate_block(int fd, int fdv)
897 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
899 error_loc = "cannot get dataversion";
907 /* The grouplock blocks all concurrent accesses to the file.
908 * It has to be taken after llapi_get_data_version as it would
910 rc = llapi_group_lock(fd, gid);
912 error_loc = "cannot get group lock";
916 rc = migrate_copy_data(fd, fdv, NULL);
918 error_loc = "data copy failed";
922 /* Make sure we keep original atime/mtime values */
923 rc = migrate_copy_timestamps(fd, fdv);
925 error_loc = "timestamp copy failed";
930 * for a migration we need to check data version on file did
933 * Pass in gid=0 since we already own grouplock. */
934 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
935 SWAP_LAYOUTS_CHECK_DV1);
937 error_loc = "file changed";
940 error_loc = "cannot swap layout";
945 rc2 = llapi_group_unlock(fd, gid);
946 if (rc2 < 0 && rc == 0) {
947 error_loc = "unlock group lock";
955 * Internal helper for migrate_copy_data(). Check lease and report error if
958 * \param[in] fd File descriptor on which to check the lease.
960 * \retval 0 Migration can keep on going.
961 * \retval -errno Error occurred, abort migration.
963 static int check_lease(int fd)
967 rc = llapi_lease_check(fd);
969 return 0; /* llapi_check_lease returns > 0 on success. */
974 static int migrate_nonblock(int fd, int fdv)
980 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
982 error_loc = "cannot get data version";
986 rc = migrate_copy_data(fd, fdv, check_lease);
988 error_loc = "data copy failed";
992 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
994 error_loc = "cannot get data version";
1000 error_loc = "source file changed";
1004 /* Make sure we keep original atime/mtime values */
1005 rc = migrate_copy_timestamps(fd, fdv);
1007 error_loc = "timestamp copy failed";
1014 static int lfs_component_set(char *fname, int comp_id,
1015 __u32 flags, __u32 neg_flags)
1018 __u32 flags_array[2];
1023 ids[count] = comp_id;
1024 flags_array[count] = flags;
1029 ids[count] = comp_id;
1030 flags_array[count] = neg_flags | LCME_FL_NEG;
1034 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1037 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1038 progname, comp_id, fname, flags, neg_flags);
1043 static int lfs_component_del(char *fname, __u32 comp_id,
1044 __u32 flags, __u32 neg_flags)
1048 if (flags && neg_flags)
1051 if (!flags && neg_flags)
1052 flags = neg_flags | LCME_FL_NEG;
1054 if ((flags && comp_id) || (!flags && !comp_id))
1057 /* LCME_FL_INIT is the only supported flag in PFL */
1059 if (flags & ~LCME_KNOWN_FLAGS) {
1061 "%s setstripe: unknown flags %#x\n",
1065 } else if (comp_id > LCME_ID_MAX) {
1066 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1071 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1074 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1075 progname, comp_id, fname, strerror(errno));
1079 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1086 rc = llapi_layout_file_comp_add(fname, layout);
1088 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1089 fname, strerror(errno));
1093 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1094 struct llapi_layout *layout)
1102 fd = lstat(fname, &st);
1103 if (fd == 0 && S_ISDIR(st.st_mode))
1104 open_flags = O_DIRECTORY | O_RDONLY;
1106 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1108 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1109 S_ISDIR(st.st_mode) ?
1110 "set default composite layout for" :
1111 "create composite file",
1112 fname, strerror(errno));
1116 static int lfs_migrate(char *name, __u64 migration_flags,
1117 struct llapi_stripe_param *param,
1118 struct llapi_layout *layout)
1124 rc = migrate_open_files(name, migration_flags, param, layout,
1129 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1130 /* Blocking mode (forced if servers do not support file lease).
1131 * It is also the default mode, since we cannot distinguish
1132 * between a broken lease and a server that does not support
1133 * atomic swap/close (LU-6785) */
1134 rc = migrate_block(fd, fdv);
1138 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1140 error_loc = "cannot get lease";
1144 rc = migrate_nonblock(fd, fdv);
1146 llapi_lease_release(fd);
1150 /* Atomically put lease, swap layouts and close.
1151 * for a migration we need to check data version on file did
1153 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1155 error_loc = "cannot swap layout";
1167 fprintf(stderr, "error: %s: %s: %s: %s\n",
1168 progname, name, error_loc, strerror(-rc));
1169 else if (migration_flags & MIGRATION_VERBOSE)
1170 printf("%s\n", name);
1175 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1184 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1188 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1189 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1190 const char *comp_name = comp_flags_table[i].cfn_name;
1192 if (strcmp(name, comp_name) == 0) {
1193 *flags |= comp_flag;
1195 } else if (strncmp(name, "^", 1) == 0 &&
1196 strcmp(name + 1, comp_name) == 0) {
1197 *neg_flags |= comp_flag;
1202 llapi_printf(LLAPI_MSG_ERROR,
1203 "%s: component flag '%s' not supported\n",
1209 if (!*flags && !*neg_flags)
1212 /* don't allow to set and exclude the same flag */
1213 if (*flags & *neg_flags)
1219 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1227 if (strncmp(string, "^", 1) == 0) {
1228 *neg_state = llapi_layout_string_flags(string + 1);
1229 if (*neg_state != 0)
1232 *state = llapi_layout_string_flags(string);
1237 llapi_printf(LLAPI_MSG_ERROR,
1238 "%s: mirrored file state '%s' not supported\n",
1244 * struct mirror_args - Command-line arguments for mirror(s).
1245 * @m_count: Number of mirrors to be created with this layout.
1246 * @m_flags: Mirror level flags, only 'prefer' is supported.
1247 * @m_layout: Mirror layout.
1248 * @m_file: A victim file. Its layout will be split and used as a mirror.
1249 * @m_next: Point to the next node of the list.
1251 * Command-line arguments for mirror(s) will be parsed and stored in
1252 * a linked list that consists of this structure.
1254 struct mirror_args {
1257 struct llapi_layout *m_layout;
1259 struct mirror_args *m_next;
1262 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1267 rc = llapi_layout_comp_flags_get(layout, &flags);
1271 if (flags & LCME_FL_NEG) {
1272 fprintf(stderr, "error: %s: negative flags are not supported\n",
1277 if (flags & LCME_FL_STALE) {
1278 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1279 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1283 return LLAPI_LAYOUT_ITER_CONT;
1286 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1288 uint64_t start, end;
1292 /* LU-10112: do not support dom+flr in phase 1 */
1293 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1297 rc = llapi_layout_pattern_get(layout, &pattern);
1301 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1302 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1307 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1311 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1315 if (end != LUSTRE_EOF) {
1316 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1321 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1326 * enum mirror_flags - Flags for extending a mirrored file.
1327 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1328 * in case the victim file(s) contains the same data as the
1329 * original mirrored file.
1330 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1331 * @MF_COMP_ID: specified component id instead of mirror id
1333 * Flags for extending a mirrored file.
1342 * mirror_create_sanity_check() - Check mirror list.
1343 * @list: A linked list that stores the mirror arguments.
1345 * This function does a sanity check on @list for creating
1348 * Return: 0 on success or a negative error code on failure.
1350 static int mirror_create_sanity_check(const char *fname,
1351 struct mirror_args *list)
1354 bool has_m_file = false;
1355 bool has_m_layout = false;
1361 struct llapi_layout *layout;
1363 layout = llapi_layout_get_by_path(fname, 0);
1366 "error: %s: file '%s' couldn't get layout\n",
1371 rc = mirror_sanity_check_one(layout);
1372 llapi_layout_free(layout);
1378 while (list != NULL) {
1379 if (list->m_file != NULL) {
1381 llapi_layout_free(list->m_layout);
1384 llapi_layout_get_by_path(list->m_file, 0);
1385 if (list->m_layout == NULL) {
1387 "error: %s: file '%s' has no layout\n",
1388 progname, list->m_file);
1392 has_m_layout = true;
1393 if (list->m_layout == NULL) {
1394 fprintf(stderr, "error: %s: no mirror layout\n",
1400 rc = mirror_sanity_check_one(list->m_layout);
1404 list = list->m_next;
1407 if (has_m_file && has_m_layout) {
1409 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1417 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1419 __u32 mirror_flags = *(__u32 *)cbdata;
1423 rc = llapi_layout_comp_flags_get(layout, &flags);
1428 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1433 return LLAPI_LAYOUT_ITER_CONT;
1437 * mirror_create() - Create a mirrored file.
1438 * @fname: The file to be created.
1439 * @mirror_list: A linked list that stores the mirror arguments.
1441 * This function creates a mirrored file @fname with the mirror(s)
1442 * from @mirror_list.
1444 * Return: 0 on success or a negative error code on failure.
1446 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1448 struct llapi_layout *layout = NULL;
1449 struct mirror_args *cur_mirror = NULL;
1450 uint16_t mirror_count = 0;
1454 rc = mirror_create_sanity_check(NULL, mirror_list);
1458 cur_mirror = mirror_list;
1459 while (cur_mirror != NULL) {
1460 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1462 &cur_mirror->m_flags);
1465 fprintf(stderr, "%s: failed to set mirror flags\n",
1470 for (i = 0; i < cur_mirror->m_count; i++) {
1471 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1474 fprintf(stderr, "error: %s: "
1475 "merge layout failed: %s\n",
1476 progname, strerror(errno));
1480 mirror_count += cur_mirror->m_count;
1481 cur_mirror = cur_mirror->m_next;
1484 if (layout == NULL) {
1485 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1489 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1492 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1493 progname, strerror(errno));
1497 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1505 llapi_layout_free(layout);
1510 * Compare files and check lease on @fd.
1512 * \retval bytes number of bytes are the same
1514 static ssize_t mirror_file_compare(int fd, int fdv)
1516 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1518 ssize_t bytes_done = 0;
1519 ssize_t bytes_read = 0;
1521 buf = malloc(buflen * 2);
1526 if (!llapi_lease_check(fd)) {
1527 bytes_done = -EBUSY;
1531 bytes_read = read(fd, buf, buflen);
1532 if (bytes_read <= 0)
1535 if (bytes_read != read(fdv, buf + buflen, buflen))
1538 /* XXX: should compute the checksum on each buffer and then
1539 * compare checksum to avoid cache collision */
1540 if (memcmp(buf, buf + buflen, bytes_read))
1543 bytes_done += bytes_read;
1551 static int mirror_extend_file(const char *fname, const char *victim_file,
1552 enum mirror_flags mirror_flags)
1557 struct stat stbuf_v;
1558 struct ll_ioc_lease *data = NULL;
1561 fd = open(fname, O_RDWR);
1563 error_loc = "open source file";
1568 fdv = open(victim_file, O_RDWR);
1570 error_loc = "open target file";
1575 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1576 error_loc = "stat source or target file";
1581 if (stbuf.st_dev != stbuf_v.st_dev) {
1582 error_loc = "stat source and target file";
1587 /* mirrors should be of the same size */
1588 if (stbuf.st_size != stbuf_v.st_size) {
1589 error_loc = "file sizes don't match";
1594 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1596 error_loc = "cannot get lease";
1600 if (!(mirror_flags & MF_NO_VERIFY)) {
1602 /* mirrors should have the same contents */
1603 ret = mirror_file_compare(fd, fdv);
1604 if (ret != stbuf.st_size) {
1605 error_loc = "file busy or contents don't match";
1606 rc = ret < 0 ? ret : -EINVAL;
1611 /* Get rid of caching pages from clients */
1612 rc = llapi_file_flush(fd);
1614 error_loc = "cannot get data version";
1618 rc = llapi_file_flush(fdv);
1620 error_loc = "cannot get data version";
1625 /* Make sure we keep original atime/mtime values */
1626 rc = migrate_copy_timestamps(fd, fdv);
1628 error_loc = "cannot copy timestamp";
1632 /* Atomically put lease, merge layouts and close. */
1633 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1635 error_loc = "memory allocation";
1638 data->lil_mode = LL_LEASE_UNLCK;
1639 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1640 data->lil_count = 1;
1641 data->lil_ids[0] = fdv;
1642 rc = llapi_lease_set(fd, data);
1644 error_loc = "cannot merge layout";
1646 } else if (rc == 0) {
1648 error_loc = "lost lease lock";
1661 (void) unlink(victim_file);
1663 fprintf(stderr, "error: %s: %s: %s: %s\n",
1664 progname, fname, error_loc, strerror(-rc));
1668 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1670 struct ll_ioc_lease *data = NULL;
1675 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1679 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1681 error_loc = "cannot get lease";
1685 rc = migrate_nonblock(fd, fdv);
1687 llapi_lease_release(fd);
1691 /* Atomically put lease, merge layouts and close. */
1692 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1694 error_loc = "memory allocation";
1697 data->lil_mode = LL_LEASE_UNLCK;
1698 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1699 data->lil_count = 1;
1700 data->lil_ids[0] = fdv;
1701 rc = llapi_lease_set(fd, data);
1703 error_loc = "cannot merge layout";
1705 } else if (rc == 0) {
1707 error_loc = "lost lease lock";
1720 fprintf(stderr, "error: %s: %s: %s: %s\n",
1721 progname, name, error_loc, strerror(-rc));
1725 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1726 enum mirror_flags mirror_flags)
1730 rc = mirror_create_sanity_check(fname, mirror_list);
1734 while (mirror_list) {
1735 if (mirror_list->m_file != NULL) {
1736 rc = mirror_extend_file(fname, mirror_list->m_file,
1739 __u32 mirror_count = mirror_list->m_count;
1741 while (mirror_count > 0) {
1742 rc = mirror_extend_layout(fname,
1743 mirror_list->m_layout);
1753 mirror_list = mirror_list->m_next;
1759 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1764 rc = llapi_layout_mirror_id_get(layout, &id);
1768 if ((__u16)id == *(__u16 *)cbdata)
1769 return LLAPI_LAYOUT_ITER_STOP;
1771 return LLAPI_LAYOUT_ITER_CONT;
1774 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1779 rc = llapi_layout_comp_id_get(layout, &id);
1783 if (id == *(__u32 *)cbdata)
1784 return LLAPI_LAYOUT_ITER_STOP;
1786 return LLAPI_LAYOUT_ITER_CONT;
1788 static int mirror_split(const char *fname, __u32 id,
1789 enum mirror_flags mflags, const char *victim_file)
1791 struct llapi_layout *layout;
1792 char parent[PATH_MAX];
1793 char victim[PATH_MAX];
1794 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1796 struct ll_ioc_lease *data;
1797 uint16_t mirror_count;
1802 /* check fname contains mirror with mirror_id/comp_id */
1803 layout = llapi_layout_get_by_path(fname, 0);
1806 "error %s: file '%s' couldn't get layout\n",
1811 rc = mirror_sanity_check_one(layout);
1815 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1818 "error %s: file '%s' couldn't get mirror count\n",
1822 if (mirror_count < 2) {
1824 "error %s: file '%s' has %d component, cannot split\n",
1825 progname, fname, mirror_count);
1829 if (mflags & MF_COMP_ID) {
1830 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1831 id = mirror_id_of(id);
1833 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1836 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1839 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1841 "error %s: file '%s' does not contain mirror with id %u\n",
1842 progname, fname, id);
1846 fd = open(fname, O_RDWR);
1849 "error %s: open file '%s' failed: %s\n",
1850 progname, fname, strerror(errno));
1854 /* get victim file directory pathname */
1855 if (strlen(fname) > sizeof(parent) - 1) {
1856 fprintf(stderr, "error %s: file name of '%s' too long\n",
1861 strncpy(parent, fname, sizeof(parent));
1862 ptr = strrchr(parent, '/');
1864 if (getcwd(parent, sizeof(parent)) == NULL) {
1865 fprintf(stderr, "error %s: getcwd failed: %s\n",
1866 progname, strerror(errno));
1876 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1878 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1883 if (victim_file == NULL) {
1884 /* use a temp file to store the splitted layout */
1885 if (mflags & MF_DESTROY) {
1886 fdv = llapi_create_volatile_idx(parent, mdt_index,
1887 O_LOV_DELAY_CREATE);
1889 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1891 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1894 /* user specified victim file */
1895 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1900 "error %s: create victim file failed: %s\n",
1901 progname, strerror(errno));
1905 /* get lease lock of fname */
1906 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1909 "error %s: cannot get lease of file '%s': %d\n",
1910 progname, fname, rc);
1914 /* Atomatically put lease, split layouts and close. */
1915 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1921 data->lil_mode = LL_LEASE_UNLCK;
1922 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1923 data->lil_count = 2;
1924 data->lil_ids[0] = fdv;
1925 data->lil_ids[1] = id;
1926 rc = llapi_lease_set(fd, data);
1928 if (rc == 0) /* lost lease lock */
1931 "error %s: cannot split '%s': %s\n",
1932 progname, fname, strerror(-rc));
1943 llapi_layout_free(layout);
1948 * Parse a string containing an target index list into an array of integers.
1950 * The input string contains a comma delimited list of individual
1951 * indices and ranges, for example "1,2-4,7". Add the indices into the
1952 * \a tgts array and remove duplicates.
1954 * \param[out] tgts array to store indices in
1955 * \param[in] size size of \a tgts array
1956 * \param[in] offset starting index in \a tgts
1957 * \param[in] arg string containing OST index list
1959 * \retval positive number of indices in \a tgts
1960 * \retval -EINVAL unable to parse \a arg
1962 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1966 int slots = size - offset;
1973 end_of_loop = false;
1974 while (!end_of_loop) {
1978 char *endptr = NULL;
1982 ptr = strchrnul(arg, ',');
1984 end_of_loop = *ptr == '\0';
1987 start_index = strtol(arg, &endptr, 0);
1988 if (endptr == arg) /* no data at all */
1990 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1993 end_index = start_index;
1994 if (*endptr == '-') {
1995 end_index = strtol(endptr + 1, &endptr, 0);
1996 if (*endptr != '\0')
1998 if (end_index < start_index)
2002 for (i = start_index; i <= end_index && slots > 0; i++) {
2005 /* remove duplicate */
2006 for (j = 0; j < offset; j++) {
2010 if (j == offset) { /* no duplicate */
2015 if (slots == 0 && i < end_index)
2023 if (!end_of_loop && ptr != NULL)
2026 return rc < 0 ? rc : nr;
2029 struct lfs_setstripe_args {
2030 unsigned long long lsa_comp_end;
2031 unsigned long long lsa_stripe_size;
2032 long long lsa_stripe_count;
2033 long long lsa_stripe_off;
2034 __u32 lsa_comp_flags;
2035 __u32 lsa_comp_neg_flags;
2036 unsigned long long lsa_pattern;
2037 unsigned int lsa_mirror_count;
2039 bool lsa_first_comp;
2041 char *lsa_pool_name;
2044 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2046 unsigned int mirror_count = lsa->lsa_mirror_count;
2047 bool first_comp = lsa->lsa_first_comp;
2049 memset(lsa, 0, sizeof(*lsa));
2051 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2052 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2053 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2054 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2055 lsa->lsa_pool_name = NULL;
2057 lsa->lsa_mirror_count = mirror_count;
2058 lsa->lsa_first_comp = first_comp;
2062 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2063 * @lsa: Stripe options to be initialized and inherited.
2065 * This function initializes stripe options in @lsa and inherit
2066 * stripe_size, stripe_count and OST pool_name options.
2070 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2072 unsigned long long stripe_size;
2073 long long stripe_count;
2074 char *pool_name = NULL;
2076 stripe_size = lsa->lsa_stripe_size;
2077 stripe_count = lsa->lsa_stripe_count;
2078 pool_name = lsa->lsa_pool_name;
2080 setstripe_args_init(lsa);
2082 lsa->lsa_stripe_size = stripe_size;
2083 lsa->lsa_stripe_count = stripe_count;
2084 lsa->lsa_pool_name = pool_name;
2087 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2089 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2090 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2091 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2092 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2093 lsa->lsa_pool_name != NULL ||
2094 lsa->lsa_comp_end != 0);
2098 * comp_args_to_layout() - Create or extend a composite layout.
2099 * @composite: Pointer to the composite layout.
2100 * @lsa: Stripe options for the new component.
2102 * This function creates or extends a composite layout by adding a new
2103 * component with stripe options from @lsa.
2105 * Return: 0 on success or an error code on failure.
2107 static int comp_args_to_layout(struct llapi_layout **composite,
2108 struct lfs_setstripe_args *lsa,
2111 struct llapi_layout *layout = *composite;
2112 uint64_t prev_end = 0;
2115 if (layout == NULL) {
2116 layout = llapi_layout_alloc();
2117 if (layout == NULL) {
2118 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2122 *composite = layout;
2126 /* Get current component extent, current component
2127 * must be the tail component. */
2128 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2130 fprintf(stderr, "Get comp extent failed. %s\n",
2135 if (lsa->lsa_first_comp)
2138 if (lsa->lsa_first_comp)
2139 rc = llapi_layout_add_first_comp(layout);
2141 rc = llapi_layout_comp_add(layout);
2143 fprintf(stderr, "Add component failed. %s\n",
2148 /* reset lsa_first_comp */
2149 lsa->lsa_first_comp = false;
2152 rc = llapi_layout_comp_extent_set(layout, prev_end,
2155 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2156 prev_end, lsa->lsa_comp_end, strerror(errno));
2161 /* Data-on-MDT component setting */
2162 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2163 /* In case of Data-on-MDT patterns the only extra option
2164 * applicable is stripe size option. */
2165 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2166 fprintf(stderr, "Option 'stripe-count' can't be "
2167 "specified with Data-on-MDT component: %lld\n",
2168 lsa->lsa_stripe_count);
2171 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2172 fprintf(stderr, "Option 'stripe-size' can't be "
2173 "specified with Data-on-MDT component: %llu\n",
2174 lsa->lsa_stripe_size);
2177 if (lsa->lsa_nr_tgts != 0) {
2178 fprintf(stderr, "Option 'ost-list' can't be specified "
2179 "with Data-on-MDT component: '%i'\n",
2183 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2184 fprintf(stderr, "Option 'stripe-offset' can't be "
2185 "specified with Data-on-MDT component: %lld\n",
2186 lsa->lsa_stripe_off);
2189 if (lsa->lsa_pool_name != 0) {
2190 fprintf(stderr, "Option 'pool' can't be specified "
2191 "with Data-on-MDT component: '%s'\n",
2192 lsa->lsa_pool_name);
2196 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2198 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2199 lsa->lsa_pattern, strerror(errno));
2202 /* Data-on-MDT component has always single stripe up to end */
2203 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2206 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2208 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2209 lsa->lsa_stripe_size, strerror(errno));
2213 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2215 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2216 lsa->lsa_stripe_count, strerror(errno));
2220 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2222 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2223 lsa->lsa_comp_flags, strerror(errno));
2227 if (lsa->lsa_pool_name != NULL) {
2228 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2230 fprintf(stderr, "Set pool name: %s failed. %s\n",
2231 lsa->lsa_pool_name, strerror(errno));
2235 rc = llapi_layout_pool_name_set(layout, "");
2237 fprintf(stderr, "Clear pool name failed: %s\n",
2243 if (lsa->lsa_nr_tgts > 0) {
2244 if (lsa->lsa_stripe_count > 0 &&
2245 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2246 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2247 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2248 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2249 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2252 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2253 rc = llapi_layout_ost_index_set(layout, i,
2258 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2259 lsa->lsa_stripe_off != -1) {
2260 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2263 fprintf(stderr, "Set ost index %d failed. %s\n",
2264 i, strerror(errno));
2271 static int build_component(struct llapi_layout **layout,
2272 struct lfs_setstripe_args *lsa, bool set_extent)
2276 rc = comp_args_to_layout(layout, lsa, set_extent);
2280 if (lsa->lsa_mirror_count > 0) {
2281 rc = llapi_layout_mirror_count_set(*layout,
2282 lsa->lsa_mirror_count);
2286 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2289 lsa->lsa_mirror_count = 0;
2295 static int build_layout_from_yaml_node(struct cYAML *node,
2296 struct llapi_layout **layout,
2297 struct lfs_setstripe_args *lsa,
2304 if (node->cy_type == CYAML_TYPE_OBJECT) {
2305 /* go deep to sub blocks */
2306 rc = build_layout_from_yaml_node(node->cy_child, layout,
2311 if (node->cy_string == NULL)
2314 string = node->cy_string;
2315 /* skip leading lmm_ if present, to simplify parsing */
2316 if (strncmp(string, "lmm_", 4) == 0)
2319 if (node->cy_type == CYAML_TYPE_STRING) {
2320 if (!strcmp(string, "lcme_extent.e_end")) {
2321 if (!strcmp(node->cy_valuestring, "EOF") ||
2322 !strcmp(node->cy_valuestring, "eof"))
2323 lsa->lsa_comp_end = LUSTRE_EOF;
2324 } else if (!strcmp(string, "pool")) {
2325 lsa->lsa_pool_name = node->cy_valuestring;
2326 } else if (!strcmp(string, "pattern")) {
2327 if (!strcmp(node->cy_valuestring, "mdt"))
2328 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2329 } else if (!strcmp(string, "lcme_flags")) {
2330 rc = comp_str2flags(node->cy_valuestring,
2331 &lsa->lsa_comp_flags,
2332 &lsa->lsa_comp_neg_flags);
2335 /* Only template flags have meaning in
2336 * the layout for a new file
2338 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2340 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2341 if (!strcmp(string, "lcm_mirror_count")) {
2342 lsa->lsa_mirror_count = node->cy_valueint;
2343 } else if (!strcmp(string, "lcme_extent.e_start")) {
2344 if (node->cy_valueint != 0 || *layout != NULL) {
2345 rc = build_component(layout, lsa, true);
2350 if (node->cy_valueint == 0)
2351 lsa->lsa_first_comp = true;
2353 /* initialize lsa */
2354 setstripe_args_init(lsa);
2355 lsa->lsa_tgts = osts;
2356 } else if (!strcmp(string, "lcme_extent.e_end")) {
2357 if (node->cy_valueint == -1)
2358 lsa->lsa_comp_end = LUSTRE_EOF;
2360 lsa->lsa_comp_end = node->cy_valueint;
2361 } else if (!strcmp(string, "stripe_count")) {
2362 lsa->lsa_stripe_count = node->cy_valueint;
2363 } else if (!strcmp(string, "stripe_size")) {
2364 lsa->lsa_stripe_size = node->cy_valueint;
2365 } else if (!strcmp(string, "stripe_offset")) {
2366 lsa->lsa_stripe_off = node->cy_valueint;
2367 } else if (!strcmp(string, "l_ost_idx")) {
2368 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2373 node = node->cy_next;
2379 static int lfs_comp_create_from_yaml(char *template,
2380 struct llapi_layout **layout,
2381 struct lfs_setstripe_args *lsa,
2384 struct cYAML *tree = NULL, *err_rc = NULL;
2387 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2389 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2390 progname, template);
2391 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2392 "can't parse", &err_rc);
2393 cYAML_print_tree2file(stderr, err_rc);
2394 cYAML_free_tree(err_rc);
2399 /* initialize lsa for plain file */
2400 setstripe_args_init(lsa);
2401 lsa->lsa_tgts = osts;
2403 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2405 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2406 progname, template);
2409 rc = build_component(layout, lsa, *layout != NULL);
2411 /* clean clean lsa */
2412 setstripe_args_init(lsa);
2416 cYAML_free_tree(tree);
2420 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2421 * end of the last component in the existing file, and adjust the
2422 * first extent start of the components to be added accordingly. */
2423 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2425 struct llapi_layout *head;
2426 uint64_t start, end, stripe_size, prev_end = 0;
2429 if (layout == NULL) {
2431 "%s setstripe: layout must be specified\n",
2437 head = llapi_layout_get_by_path(fname, 0);
2440 "%s setstripe: cannot read layout from '%s': %s\n",
2441 progname, fname, strerror(errno));
2443 } else if (errno == ENODATA) {
2444 /* file without LOVEA, this component-add will be turned
2445 * into a component-create. */
2446 llapi_layout_free(head);
2448 } else if (!llapi_layout_is_composite(head)) {
2449 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2451 llapi_layout_free(head);
2455 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2457 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2458 progname, strerror(errno));
2459 llapi_layout_free(head);
2463 llapi_layout_free(head);
2465 /* Make sure we use the first component of the layout to be added. */
2466 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2469 "%s setstripe: cannot move component cursor: %s\n",
2470 progname, strerror(errno));
2474 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2476 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2477 progname, strerror(errno));
2481 if (start > prev_end || end <= prev_end) {
2483 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2484 progname, start, end, prev_end);
2488 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2490 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2491 progname, strerror(errno));
2495 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2496 (prev_end & (stripe_size - 1))) {
2498 "%s setstripe: stripe size %lu not aligned with %lu\n",
2499 progname, stripe_size, prev_end);
2503 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2506 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2507 progname, prev_end, end, strerror(errno));
2514 static inline bool arg_is_eof(char *arg)
2516 return !strncmp(arg, "-1", strlen("-1")) ||
2517 !strncmp(arg, "EOF", strlen("EOF")) ||
2518 !strncmp(arg, "eof", strlen("eof"));
2522 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2524 * Return: Valid mirror_args pointer on success and
2525 * NULL if memory allocation fails.
2527 static struct mirror_args *lfs_mirror_alloc(void)
2529 struct mirror_args *mirror = NULL;
2532 mirror = calloc(1, sizeof(*mirror));
2543 * lfs_mirror_free() - Free memory allocated for a mirror argument
2545 * @mirror: Previously allocated mirror argument structure by
2546 * lfs_mirror_alloc().
2548 * Free memory allocated for @mirror.
2552 static void lfs_mirror_free(struct mirror_args *mirror)
2554 if (mirror->m_layout != NULL)
2555 llapi_layout_free(mirror->m_layout);
2560 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2561 * @mirror_list: Previously allocated mirror list.
2563 * Free memory allocated for @mirror_list.
2567 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2569 struct mirror_args *next_mirror = NULL;
2571 while (mirror_list != NULL) {
2572 next_mirror = mirror_list->m_next;
2573 lfs_mirror_free(mirror_list);
2574 mirror_list = next_mirror;
2586 LFS_COMP_NO_VERIFY_OPT,
2588 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2590 LFS_MIRROR_STATE_OPT,
2592 LFS_MIRROR_INDEX_OPT,
2593 LFS_LAYOUT_FOREIGN_OPT,
2598 static int lfs_setstripe_internal(int argc, char **argv,
2599 enum setstripe_origin opc)
2601 struct lfs_setstripe_args lsa = { 0 };
2602 struct llapi_stripe_param *param = NULL;
2603 struct find_param migrate_mdt_param = {
2613 unsigned long long size_units = 1;
2614 bool migrate_mode = false;
2615 bool migrate_mdt_mode = false;
2616 bool setstripe_mode = false;
2617 bool migration_block = false;
2618 __u64 migration_flags = 0;
2619 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2620 int comp_del = 0, comp_set = 0;
2623 struct llapi_layout *layout = NULL;
2624 struct llapi_layout **lpp = &layout;
2625 bool mirror_mode = false;
2626 bool has_m_file = false;
2627 __u32 mirror_count = 0;
2628 enum mirror_flags mirror_flags = 0;
2629 struct mirror_args *mirror_list = NULL;
2630 struct mirror_args *new_mirror = NULL;
2631 struct mirror_args *last_mirror = NULL;
2632 __u16 mirror_id = 0;
2634 bool from_yaml = false;
2635 bool from_copy = false;
2636 char *template = NULL;
2637 bool foreign_mode = false;
2639 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
2640 char *mode_opt = NULL;
2641 mode_t previous_umask = 0;
2642 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2644 struct option long_opts[] = {
2645 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2646 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2647 /* --block is only valid in migrate mode */
2648 { .val = 'b', .name = "block", .has_arg = no_argument },
2649 { .val = LFS_COMP_ADD_OPT,
2650 .name = "comp-add", .has_arg = no_argument },
2651 { .val = LFS_COMP_ADD_OPT,
2652 .name = "component-add", .has_arg = no_argument },
2653 { .val = LFS_COMP_DEL_OPT,
2654 .name = "comp-del", .has_arg = no_argument },
2655 { .val = LFS_COMP_DEL_OPT,
2656 .name = "component-del", .has_arg = no_argument },
2657 { .val = LFS_COMP_FLAGS_OPT,
2658 .name = "comp-flags", .has_arg = required_argument },
2659 { .val = LFS_COMP_FLAGS_OPT,
2660 .name = "component-flags",
2661 .has_arg = required_argument },
2662 { .val = LFS_COMP_SET_OPT,
2663 .name = "comp-set", .has_arg = no_argument },
2664 { .val = LFS_COMP_SET_OPT,
2665 .name = "component-set",
2666 .has_arg = no_argument},
2667 { .val = LFS_COMP_NO_VERIFY_OPT,
2668 .name = "no-verify", .has_arg = no_argument},
2669 { .val = LFS_LAYOUT_FLAGS_OPT,
2670 .name = "flags", .has_arg = required_argument},
2671 { .val = LFS_LAYOUT_FOREIGN_OPT,
2672 .name = "foreign", .has_arg = optional_argument},
2673 { .val = LFS_MIRROR_ID_OPT,
2674 .name = "mirror-id", .has_arg = required_argument},
2675 { .val = LFS_MODE_OPT,
2676 .name = "mode", .has_arg = required_argument},
2677 { .val = LFS_LAYOUT_COPY,
2678 .name = "copy", .has_arg = required_argument},
2679 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2680 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2681 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2682 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2683 { .val = 'd', .name = "delete", .has_arg = no_argument},
2684 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2685 /* --non-direct is only valid in migrate mode */
2686 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2687 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2688 { .val = 'E', .name = "component-end",
2689 .has_arg = required_argument},
2690 { .val = 'f', .name = "file", .has_arg = required_argument },
2691 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2692 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2693 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2694 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2695 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2696 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2697 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2698 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2699 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2700 { .val = 'L', .name = "layout", .has_arg = required_argument },
2701 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2702 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2703 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2704 /* --non-block is only valid in migrate mode */
2705 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2706 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2707 { .val = 'o', .name = "ost", .has_arg = required_argument },
2708 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2709 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2710 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2712 { .val = 'p', .name = "pool", .has_arg = required_argument },
2713 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2714 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2715 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2716 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2717 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2718 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2719 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2720 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2721 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2722 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2723 /* --verbose is only valid in migrate mode */
2724 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2725 { .val = 'x', .name = "xattr", .has_arg = required_argument },
2726 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2729 setstripe_args_init(&lsa);
2731 migrate_mode = (opc == SO_MIGRATE);
2732 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2733 setstripe_mode = (opc == SO_SETSTRIPE);
2735 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2737 while ((c = getopt_long(argc, argv,
2738 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:",
2739 long_opts, NULL)) >= 0) {
2744 case LFS_COMP_ADD_OPT:
2747 case LFS_COMP_DEL_OPT:
2750 case LFS_COMP_FLAGS_OPT:
2751 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2752 &lsa.lsa_comp_neg_flags);
2755 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2756 fprintf(stderr, "%s: inverted flags are not supported\n",
2760 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2762 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2769 case LFS_COMP_SET_OPT:
2772 case LFS_COMP_NO_VERIFY_OPT:
2773 mirror_flags |= MF_NO_VERIFY;
2775 case LFS_MIRROR_ID_OPT:
2776 mirror_id = strtoul(optarg, &end, 0);
2777 if (*end != '\0' || mirror_id == 0) {
2779 "%s %s: invalid mirror ID '%s'\n",
2780 progname, argv[0], optarg);
2784 case LFS_LAYOUT_FLAGS_OPT: {
2787 /* check for numeric flags (foreign and mirror cases) */
2788 if (setstripe_mode && !mirror_mode && !last_mirror) {
2789 flags = strtoul(optarg, &end, 16);
2792 "%s %s: bad flags '%s'\n",
2793 progname, argv[0], optarg);
2799 if (!mirror_mode || !last_mirror) {
2800 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2805 result = comp_str2flags(optarg, &last_mirror->m_flags,
2811 fprintf(stderr, "%s: inverted flags are not supported\n",
2816 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2818 "%s: unsupported mirror flags: %s\n",
2825 case LFS_LAYOUT_FOREIGN_OPT:
2826 if (optarg != NULL) {
2827 /* check pure numeric */
2828 type = strtoul(optarg, &end, 0);
2831 type = check_foreign_type_name(optarg);
2832 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
2834 "%s %s: unrecognized foreign type '%s'\n",
2841 foreign_mode = true;
2845 if (mode_opt != NULL) {
2846 mode = strtoul(mode_opt, &end, 8);
2849 "%s %s: bad mode '%s'\n",
2850 progname, argv[0], mode_opt);
2853 previous_umask = umask(0);
2856 case LFS_LAYOUT_COPY:
2861 if (!migrate_mode) {
2863 "%s %s: -b|--block valid only for migrate command\n",
2867 migration_block = true;
2870 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2873 "%s %s: invalid stripe count '%s'\n",
2874 progname, argv[0], optarg);
2878 if (lsa.lsa_stripe_count == -1)
2879 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2882 /* delete the default striping pattern */
2884 if (opc == SO_MIRROR_SPLIT) {
2887 "%s %s: -d cannot used with -f\n",
2891 mirror_flags |= MF_DESTROY;
2895 if (!migrate_mode) {
2897 "%s %s: -D|--non-direct is valid "
2898 "only for migrate command\n",
2902 migration_flags |= MIGRATION_NONDIRECT;
2905 if (lsa.lsa_comp_end != 0) {
2906 result = comp_args_to_layout(lpp, &lsa, true);
2909 "%s %s: invalid layout\n",
2914 setstripe_args_init_inherit(&lsa);
2917 if (arg_is_eof(optarg)) {
2918 lsa.lsa_comp_end = LUSTRE_EOF;
2920 result = llapi_parse_size(optarg,
2925 "%s %s: invalid component end '%s'\n",
2926 progname, argv[0], optarg);
2932 if (!migrate_mode) {
2933 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2937 lsa.lsa_pattern = check_hashtype(optarg);
2938 if (lsa.lsa_pattern == 0) {
2940 "%s %s: bad stripe hash type '%s'\n",
2941 progname, argv[0], optarg);
2946 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2949 "%s %s: invalid stripe offset '%s'\n",
2950 progname, argv[0], optarg);
2953 if (lsa.lsa_stripe_off == -1)
2954 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2957 comp_id = strtoul(optarg, &end, 0);
2958 if (*end != '\0' || comp_id == 0 ||
2959 comp_id > LCME_ID_MAX) {
2961 "%s %s: invalid component ID '%s'\n",
2962 progname, argv[0], optarg);
2967 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2969 "error: %s: invalid option: %s\n",
2970 progname, argv[optopt + 1]);
2973 if (opc == SO_MIRROR_EXTEND) {
2974 if (last_mirror == NULL) {
2976 "error: %s: '-N' must exist in front of '%s'\n",
2977 progname, argv[optopt + 1]);
2980 last_mirror->m_file = optarg;
2981 last_mirror->m_count = 1;
2984 if (mirror_list == NULL)
2985 mirror_list = lfs_mirror_alloc();
2986 mirror_list->m_file = optarg;
2991 if (strcmp(argv[optind - 1], "mdt") == 0) {
2992 /* Can be only the first component */
2993 if (layout != NULL) {
2995 fprintf(stderr, "error: 'mdt' layout "
2996 "can be only the first one\n");
2999 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3001 fprintf(stderr, "error: 'mdt' layout "
3002 "size is too big\n");
3005 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3006 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3008 fprintf(stderr, "error: layout '%s' is "
3009 "unknown, supported layouts are: "
3010 "'mdt', 'raid0'\n", argv[optind]);
3015 if (!migrate_mode) {
3017 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3021 migrate_mdt_mode = true;
3022 lsa.lsa_nr_tgts = parse_targets(tgts,
3023 sizeof(tgts) / sizeof(__u32),
3024 lsa.lsa_nr_tgts, optarg);
3025 if (lsa.lsa_nr_tgts < 0) {
3027 "%s %s: invalid MDT target(s) '%s'\n",
3028 progname, argv[0], optarg);
3032 lsa.lsa_tgts = tgts;
3033 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3034 lsa.lsa_stripe_off = tgts[0];
3037 if (!migrate_mode) {
3039 "%s %s: -n|--non-block valid only for migrate command\n",
3043 migration_flags |= MIGRATION_NONBLOCK;
3046 if (opc == SO_SETSTRIPE) {
3047 opc = SO_MIRROR_CREATE;
3051 if (optarg != NULL) {
3052 mirror_count = strtoul(optarg, &end, 0);
3053 if (*end != '\0' || mirror_count == 0) {
3055 "error: %s: bad mirror count: %s\n",
3062 new_mirror = lfs_mirror_alloc();
3063 new_mirror->m_count = mirror_count;
3065 if (mirror_list == NULL)
3066 mirror_list = new_mirror;
3068 if (last_mirror != NULL) {
3069 /* wrap up last mirror */
3070 if (lsa.lsa_comp_end == 0)
3071 lsa.lsa_comp_end = LUSTRE_EOF;
3073 result = comp_args_to_layout(lpp, &lsa, true);
3075 lfs_mirror_free(new_mirror);
3079 setstripe_args_init_inherit(&lsa);
3081 last_mirror->m_next = new_mirror;
3084 last_mirror = new_mirror;
3085 lpp = &last_mirror->m_layout;
3088 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3089 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3090 fprintf(stderr, "warning: '--ost-list' is "
3091 "deprecated, use '--ost' instead\n");
3093 lsa.lsa_nr_tgts = parse_targets(tgts,
3094 sizeof(tgts) / sizeof(__u32),
3095 lsa.lsa_nr_tgts, optarg);
3096 if (lsa.lsa_nr_tgts < 0) {
3098 "%s %s: invalid OST target(s) '%s'\n",
3099 progname, argv[0], optarg);
3103 lsa.lsa_tgts = tgts;
3104 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3105 lsa.lsa_stripe_off = tgts[0];
3110 lsa.lsa_pool_name = optarg;
3112 if (strlen(lsa.lsa_pool_name) == 0 ||
3113 strncmp(lsa.lsa_pool_name, "none",
3114 LOV_MAXPOOLNAME) == 0)
3115 lsa.lsa_pool_name = NULL;
3118 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3122 "%s %s: invalid stripe size '%s'\n",
3123 progname, argv[0], optarg);
3128 if (!migrate_mode) {
3130 "%s %s: -v|--verbose valid only for migrate command\n",
3134 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3135 migration_flags = MIGRATION_VERBOSE;
3145 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3146 progname, argv[0], argv[optind - 1]);
3151 fname = argv[optind];
3153 if (optind == argc) {
3154 fprintf(stderr, "%s %s: FILE must be specified\n",
3159 if (xattr && !foreign_mode) {
3160 /* only print a warning as this is harmless and will be ignored
3163 "%s %s: xattr has been specified for non-foreign layout\n",
3165 } else if (foreign_mode && !xattr) {
3167 "%s %s: xattr must be provided in foreign mode\n",
3172 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3173 comp_set || comp_id || delete || from_copy ||
3174 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3177 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3182 if (mirror_mode && mirror_count == 0) {
3184 "error: %s: --mirror-count|-N option is required\n",
3191 if (lsa.lsa_comp_end == 0)
3192 lsa.lsa_comp_end = LUSTRE_EOF;
3195 if (lsa.lsa_comp_end != 0) {
3196 result = comp_args_to_layout(lpp, &lsa, true);
3201 if (mirror_flags & MF_NO_VERIFY) {
3202 if (opc != SO_MIRROR_EXTEND) {
3204 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3208 } else if (!has_m_file) {
3210 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3217 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3218 * altered by user space tool, so we don't need to support the
3219 * --component-set for this moment. */
3220 if (comp_set && !comp_id) {
3221 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3226 if ((delete + comp_set + comp_del + comp_add) > 1) {
3228 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3233 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3234 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3236 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3241 if ((comp_set || comp_del) &&
3242 (setstripe_args_specified(&lsa) || layout != NULL)) {
3244 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3249 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3251 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3256 if (comp_add || comp_del) {
3259 result = lstat(fname, &st);
3260 if (result == 0 && S_ISDIR(st.st_mode)) {
3262 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3268 fprintf(stderr, "error: %s: can't use --component-add "
3269 "or --component-del for mirror operation\n",
3276 if (layout == NULL) {
3278 "%s %s: option -E must be specified with --component-add\n",
3283 result = adjust_first_extent(fname, layout);
3284 if (result == -ENODATA)
3286 else if (result != 0)
3290 if (from_yaml && from_copy) {
3292 "%s: can't specify --yaml and --copy together\n",
3297 if ((from_yaml || from_copy) &&
3298 (setstripe_args_specified(&lsa) || layout != NULL)) {
3299 fprintf(stderr, "error: %s: can't specify --yaml with "
3300 "-c, -S, -i, -o, -p or -E options.\n",
3305 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3307 "%s %s: options --non-block and --block are mutually exclusive\n",
3312 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3315 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3320 if (migrate_mdt_mode) {
3321 struct lmv_user_md *lmu;
3323 /* initialize migrate mdt parameters */
3324 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3325 LMV_USER_MAGIC_SPECIFIC));
3328 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3329 progname, argv[0], strerror(ENOMEM));
3333 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3334 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3335 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3337 "%s %s: migrate should specify MDT index\n",
3342 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3343 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3344 lmu->lum_hash_type = lsa.lsa_pattern;
3346 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3347 if (lsa.lsa_pool_name)
3348 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3349 sizeof(lmu->lum_pool_name));
3350 if (lsa.lsa_nr_tgts > 1) {
3353 if (lsa.lsa_stripe_count > 0 &&
3354 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3355 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3357 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3358 progname, lsa.lsa_stripe_count,
3364 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3365 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3366 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3367 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3369 lmu->lum_magic = LMV_USER_MAGIC;
3372 migrate_mdt_param.fp_lmv_md = lmu;
3373 migrate_mdt_param.fp_migrate = 1;
3374 } else if (layout == NULL) {
3375 /* initialize stripe parameters */
3376 param = calloc(1, offsetof(typeof(*param),
3377 lsp_osts[lsa.lsa_nr_tgts]));
3378 if (param == NULL) {
3380 "%s %s: cannot allocate memory for parameters: %s\n",
3381 progname, argv[0], strerror(ENOMEM));
3386 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3387 param->lsp_stripe_size = lsa.lsa_stripe_size;
3388 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3389 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3390 param->lsp_stripe_count = -1;
3392 param->lsp_stripe_count = lsa.lsa_stripe_count;
3394 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3395 param->lsp_stripe_offset = -1;
3397 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3398 param->lsp_pool = lsa.lsa_pool_name;
3399 param->lsp_is_specific = false;
3400 if (lsa.lsa_nr_tgts > 0) {
3401 if (lsa.lsa_stripe_count > 0 &&
3402 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3403 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3404 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3405 fprintf(stderr, "error: %s: stripe count %lld "
3406 "doesn't match the number of OSTs: %d\n"
3407 , argv[0], lsa.lsa_stripe_count,
3413 param->lsp_is_specific = true;
3414 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3415 memcpy(param->lsp_osts, tgts,
3416 sizeof(*tgts) * lsa.lsa_nr_tgts);
3421 /* generate a layout from a YAML template */
3422 result = lfs_comp_create_from_yaml(template, &layout,
3425 fprintf(stderr, "error: %s: can't create composite "
3426 "layout from template file %s\n",
3430 } else if (from_copy) {
3431 layout = llapi_layout_get_by_path(template, 0);
3432 if (layout == NULL) {
3434 "%s: can't create composite layout from file %s.\n",
3435 progname, template);
3440 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3441 if (migrate_mdt_mode) {
3442 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3443 } else if (migrate_mode) {
3444 result = lfs_migrate(fname, migration_flags, param,
3446 } else if (comp_set != 0) {
3447 result = lfs_component_set(fname, comp_id,
3449 lsa.lsa_comp_neg_flags);
3450 } else if (comp_del != 0) {
3451 result = lfs_component_del(fname, comp_id,
3453 lsa.lsa_comp_neg_flags);
3454 } else if (comp_add != 0) {
3455 result = lfs_component_add(fname, layout);
3456 } else if (opc == SO_MIRROR_CREATE) {
3457 result = mirror_create(fname, mirror_list);
3458 } else if (opc == SO_MIRROR_EXTEND) {
3459 result = mirror_extend(fname, mirror_list,
3461 } else if (opc == SO_MIRROR_SPLIT) {
3462 if (mirror_id == 0 && comp_id == 0) {
3464 "%s %s: no mirror id or component id is specified\n",
3469 comp_id = mirror_id;
3471 mirror_flags |= MF_COMP_ID;
3472 result = mirror_split(fname, comp_id, mirror_flags,
3473 has_m_file ? mirror_list->m_file :
3475 } else if (layout != NULL) {
3476 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3482 } else if (foreign_mode) {
3483 result = llapi_file_create_foreign(fname, mode, type,
3490 result = llapi_file_open_param(fname,
3499 /* Save the first error encountered. */
3506 if (mode_opt != NULL)
3507 umask(previous_umask);
3510 free(migrate_mdt_param.fp_lmv_md);
3511 llapi_layout_free(layout);
3512 lfs_mirror_list_free(mirror_list);
3517 llapi_layout_free(layout);
3518 lfs_mirror_list_free(mirror_list);
3522 static int lfs_poollist(int argc, char **argv)
3527 return llapi_poollist(argv[1]);
3530 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3535 char *endptr = "AD";
3540 else if (str[0] == '-')
3546 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3547 long long val = strtoll(timebuf, &endptr, 0);
3552 unit *= 52; /* 52 weeks + 1 day below */
3553 case 'w': /* fallthrough */
3555 case '\0': /* days are default unit if none used */
3556 case 'd': /* fallthrough */
3557 unit = (unit + (*endptr == 'y')) * 24;
3558 case 'h': /* fallthrough */
3560 case 'm': /* fallthrough */
3562 case 's': /* fallthrough */
3564 /* don't need to multiply by 1 for seconds */
3567 "%s find: bad time string '%s': %s\n",
3568 progname, timebuf, strerror(EINVAL));
3572 if (param->fp_time_margin == 0 ||
3573 (*endptr && unit < param->fp_time_margin))
3574 param->fp_time_margin = unit;
3581 fprintf(stderr, "%s find: bad time '%s': too large\n",
3591 static int name2uid(unsigned int *id, const char *name)
3593 struct passwd *passwd;
3595 passwd = getpwnam(name);
3598 *id = passwd->pw_uid;
3603 static int name2gid(unsigned int *id, const char *name)
3605 struct group *group;
3607 group = getgrnam(name);
3610 *id = group->gr_gid;
3615 static inline int name2projid(unsigned int *id, const char *name)
3620 static int uid2name(char **name, unsigned int id)
3622 struct passwd *passwd;
3624 passwd = getpwuid(id);
3627 *name = passwd->pw_name;
3632 static inline int gid2name(char **name, unsigned int id)
3634 struct group *group;
3636 group = getgrgid(id);
3639 *name = group->gr_name;
3644 static int name2layout(__u32 *layout, char *name)
3646 char *ptr, *layout_name;
3649 for (ptr = name; ; ptr = NULL) {
3650 layout_name = strtok(ptr, ",");
3651 if (layout_name == NULL)
3653 if (strcmp(layout_name, "released") == 0)
3654 *layout |= LOV_PATTERN_F_RELEASED;
3655 else if (strcmp(layout_name, "raid0") == 0)
3656 *layout |= LOV_PATTERN_RAID0;
3657 else if (strcmp(layout_name, "mdt") == 0)
3658 *layout |= LOV_PATTERN_MDT;
3665 static int lfs_find(int argc, char **argv)
3670 struct find_param param = {
3673 .fp_time_margin = 24 * 60 * 60,
3675 struct option long_opts[] = {
3676 { .val = 'A', .name = "atime", .has_arg = required_argument },
3677 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3678 { .val = LFS_COMP_COUNT_OPT,
3679 .name = "comp-count", .has_arg = required_argument },
3680 { .val = LFS_COMP_COUNT_OPT,
3681 .name = "component-count",
3682 .has_arg = required_argument },
3683 { .val = LFS_COMP_FLAGS_OPT,
3684 .name = "comp-flags", .has_arg = required_argument },
3685 { .val = LFS_COMP_FLAGS_OPT,
3686 .name = "component-flags",
3687 .has_arg = required_argument },
3688 { .val = LFS_COMP_START_OPT,
3689 .name = "comp-start", .has_arg = required_argument },
3690 { .val = LFS_COMP_START_OPT,
3691 .name = "component-start",
3692 .has_arg = required_argument },
3693 { .val = LFS_MIRROR_STATE_OPT,
3694 .name = "mirror-state", .has_arg = required_argument },
3695 { .val = LFS_LAYOUT_FOREIGN_OPT,
3696 .name = "foreign", .has_arg = optional_argument},
3697 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3698 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3699 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3700 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3701 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3702 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3703 { .val = 'E', .name = "component-end",
3704 .has_arg = required_argument },
3705 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3706 { .val = LFS_LAYOUT_FOREIGN_OPT,
3707 .name = "foreign", .has_arg = optional_argument},
3708 { .val = 'g', .name = "gid", .has_arg = required_argument },
3709 { .val = 'G', .name = "group", .has_arg = required_argument },
3710 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3711 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3712 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3713 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3714 { .val = 'L', .name = "layout", .has_arg = required_argument },
3715 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3716 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3717 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3718 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3719 { .val = 'n', .name = "name", .has_arg = required_argument },
3720 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3721 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3722 { .val = 'O', .name = "obd", .has_arg = required_argument },
3723 { .val = 'O', .name = "ost", .has_arg = required_argument },
3724 /* no short option for pool yet, can be 'p' after 2.18 */
3725 { .val = LFS_POOL_OPT,
3726 .name = "pool", .has_arg = required_argument },
3727 { .val = '0', .name = "print0", .has_arg = no_argument },
3728 { .val = 'P', .name = "print", .has_arg = no_argument },
3729 { .val = LFS_PROJID_OPT,
3730 .name = "projid", .has_arg = required_argument },
3731 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3732 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3733 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3734 { .val = 's', .name = "size", .has_arg = required_argument },
3735 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3736 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3737 { .val = 't', .name = "type", .has_arg = required_argument },
3738 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3739 { .val = 'u', .name = "uid", .has_arg = required_argument },
3740 { .val = 'U', .name = "user", .has_arg = required_argument },
3741 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3742 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3754 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3755 while ((c = getopt_long_only(argc, argv,
3756 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3757 long_opts, NULL)) >= 0) {
3762 /* '!' is part of option */
3763 /* when getopt_long_only() finds a string which is not
3764 * an option nor a known option argument it returns 1
3765 * in that case if we already have found pathstart and pathend
3766 * (i.e. we have the list of pathnames),
3767 * the only supported value is "!"
3769 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3770 if (!isoption && pathend != -1) {
3771 fprintf(stderr, "err: %s: filename|dirname must either "
3772 "precede options or follow options\n",
3777 if (!isoption && pathstart == -1)
3778 pathstart = optind - 1;
3779 if (isoption && pathstart != -1 && pathend == -1)
3780 pathend = optind - 2;
3786 /* unknown; opt is "!" or path component,
3787 * checking done above.
3789 if (strcmp(optarg, "!") == 0)
3793 xtime = ¶m.fp_atime;
3794 xsign = ¶m.fp_asign;
3795 param.fp_exclude_atime = !!neg_opt;
3796 /* no break, this falls through to 'C' for ctime */
3799 xtime = ¶m.fp_ctime;
3800 xsign = ¶m.fp_csign;
3801 param.fp_exclude_ctime = !!neg_opt;
3803 /* no break, this falls through to 'M' for mtime */
3806 xtime = ¶m.fp_mtime;
3807 xsign = ¶m.fp_msign;
3808 param.fp_exclude_mtime = !!neg_opt;
3810 rc = set_time(¶m, &t, xtime, optarg);
3811 if (rc == LONG_MAX) {
3819 if (optarg[0] == '+') {
3820 param.fp_blocks_sign = -1;
3822 } else if (optarg[0] == '-') {
3823 param.fp_blocks_sign = 1;
3827 param.fp_blocks_units = 1024;
3828 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3829 ¶m.fp_blocks_units, 0);
3831 fprintf(stderr, "error: bad blocks '%s'\n",
3835 param.fp_check_blocks = 1;
3836 param.fp_exclude_blocks = !!neg_opt;
3838 case LFS_COMP_COUNT_OPT:
3839 if (optarg[0] == '+') {
3840 param.fp_comp_count_sign = -1;
3842 } else if (optarg[0] == '-') {
3843 param.fp_comp_count_sign = 1;
3847 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3848 if (*endptr != '\0') {
3849 fprintf(stderr, "error: bad component count "
3853 param.fp_check_comp_count = 1;
3854 param.fp_exclude_comp_count = !!neg_opt;
3856 case LFS_COMP_FLAGS_OPT:
3857 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3858 ¶m.fp_comp_neg_flags);
3860 fprintf(stderr, "error: bad component flags "
3864 param.fp_check_comp_flags = 1;
3866 __u32 flags = param.fp_comp_neg_flags;
3867 param.fp_comp_neg_flags = param.fp_comp_flags;
3868 param.fp_comp_flags = flags;
3871 case LFS_COMP_START_OPT:
3872 if (optarg[0] == '+') {
3873 param.fp_comp_start_sign = -1;
3875 } else if (optarg[0] == '-') {
3876 param.fp_comp_start_sign = 1;
3880 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3881 ¶m.fp_comp_start_units, 0);
3883 fprintf(stderr, "error: bad component start "
3887 param.fp_check_comp_start = 1;
3888 param.fp_exclude_comp_start = !!neg_opt;
3890 case LFS_MIRROR_STATE_OPT:
3891 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3892 ¶m.fp_mirror_neg_state);
3895 "error: bad mirrored file state '%s'\n",
3899 param.fp_check_mirror_state = 1;
3901 __u16 state = param.fp_mirror_neg_state;
3902 param.fp_mirror_neg_state =
3903 param.fp_mirror_state;
3904 param.fp_mirror_state = state;
3908 if (optarg[0] == '+') {
3909 param.fp_stripe_count_sign = -1;
3911 } else if (optarg[0] == '-') {
3912 param.fp_stripe_count_sign = 1;
3916 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3917 if (*endptr != '\0') {
3918 fprintf(stderr,"error: bad stripe_count '%s'\n",
3923 param.fp_check_stripe_count = 1;
3924 param.fp_exclude_stripe_count = !!neg_opt;
3927 param.fp_max_depth = strtol(optarg, 0, 0);
3930 if (optarg[0] == '+') {
3931 param.fp_comp_end_sign = -1;
3933 } else if (optarg[0] == '-') {
3934 param.fp_comp_end_sign = 1;
3938 if (arg_is_eof(optarg)) {
3939 param.fp_comp_end = LUSTRE_EOF;
3940 param.fp_comp_end_units = 1;
3943 rc = llapi_parse_size(optarg,
3945 ¶m.fp_comp_end_units, 0);
3948 fprintf(stderr, "error: bad component end "
3952 param.fp_check_comp_end = 1;
3953 param.fp_exclude_comp_end = !!neg_opt;
3955 case LFS_LAYOUT_FOREIGN_OPT: {
3956 /* all types by default */
3957 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
3959 if (optarg != NULL) {
3960 /* check pure numeric */
3961 type = strtoul(optarg, &endptr, 0);
3964 type = check_foreign_type_name(optarg);
3965 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3967 "%s %s: unknown foreign type '%s'\n",
3974 param.fp_foreign_type = type;
3975 param.fp_check_foreign = 1;
3976 param.fp_exclude_foreign = !!neg_opt;
3981 rc = name2gid(¶m.fp_gid, optarg);
3983 param.fp_gid = strtoul(optarg, &endptr, 10);
3984 if (*endptr != '\0') {
3985 fprintf(stderr, "Group/GID: %s cannot "
3986 "be found.\n", optarg);
3991 param.fp_exclude_gid = !!neg_opt;
3992 param.fp_check_gid = 1;
3995 param.fp_hash_type = check_hashtype(optarg);
3996 if (param.fp_hash_type == 0) {
3997 fprintf(stderr, "error: bad hash_type '%s'\n",
4002 param.fp_check_hash_type = 1;
4003 param.fp_exclude_hash_type = !!neg_opt;
4006 ret = name2layout(¶m.fp_layout, optarg);
4009 param.fp_exclude_layout = !!neg_opt;
4010 param.fp_check_layout = 1;
4014 rc = name2uid(¶m.fp_uid, optarg);
4016 param.fp_uid = strtoul(optarg, &endptr, 10);
4017 if (*endptr != '\0') {
4018 fprintf(stderr, "User/UID: %s cannot "
4019 "be found.\n", optarg);
4024 param.fp_exclude_uid = !!neg_opt;
4025 param.fp_check_uid = 1;
4028 param.fp_pattern = (char *)optarg;
4029 param.fp_exclude_pattern = !!neg_opt;
4032 if (optarg[0] == '+') {
4033 param.fp_mirror_count_sign = -1;
4035 } else if (optarg[0] == '-') {
4036 param.fp_mirror_count_sign = 1;
4040 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4041 if (*endptr != '\0') {
4043 "error: bad mirror count '%s'\n",
4047 param.fp_check_mirror_count = 1;
4048 param.fp_exclude_mirror_count = !!neg_opt;
4053 char *buf, *token, *next, *p;
4057 buf = strdup(optarg);
4063 param.fp_exclude_obd = !!neg_opt;
4066 while (token && *token) {
4067 token = strchr(token, ',');
4074 param.fp_exclude_mdt = !!neg_opt;
4075 param.fp_num_alloc_mdts += len;
4076 tmp = realloc(param.fp_mdt_uuid,
4077 param.fp_num_alloc_mdts *
4078 sizeof(*param.fp_mdt_uuid));
4084 param.fp_mdt_uuid = tmp;
4086 param.fp_exclude_obd = !!neg_opt;
4087 param.fp_num_alloc_obds += len;
4088 tmp = realloc(param.fp_obd_uuid,
4089 param.fp_num_alloc_obds *
4090 sizeof(*param.fp_obd_uuid));
4096 param.fp_obd_uuid = tmp;
4098 for (token = buf; token && *token; token = next) {
4099 struct obd_uuid *puuid;
4102 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4105 ¶m.fp_obd_uuid[param.fp_num_obds++];
4107 p = strchr(token, ',');
4114 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4119 strncpy(puuid->uuid, token,
4120 sizeof(puuid->uuid));
4127 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4131 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4133 "Pool name %s is too long (max %d)\n",
4134 optarg, LOV_MAXPOOLNAME);
4139 * We do check for empty pool because empty pool
4140 * is used to find V1 LOV attributes
4142 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4143 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4144 param.fp_exclude_pool = !!neg_opt;
4145 param.fp_check_pool = 1;
4147 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4148 case 'p': /* want this for --pool, to match getstripe/find */
4150 "warning: -p deprecated, use --print0 or -0\n");
4153 param.fp_zero_end = 1;
4155 case 'P': /* we always print, this option is a no-op */
4157 case LFS_PROJID_OPT:
4158 rc = name2projid(¶m.fp_projid, optarg);
4160 param.fp_projid = strtoul(optarg, &endptr, 10);
4161 if (*endptr != '\0') {
4163 "Invalid project ID: %s",
4169 param.fp_exclude_projid = !!neg_opt;
4170 param.fp_check_projid = 1;
4173 if (optarg[0] == '+') {
4174 param.fp_size_sign = -1;
4176 } else if (optarg[0] == '-') {
4177 param.fp_size_sign = 1;
4181 ret = llapi_parse_size(optarg, ¶m.fp_size,
4182 ¶m.fp_size_units, 0);
4184 fprintf(stderr, "error: bad file size '%s'\n",
4188 param.fp_check_size = 1;
4189 param.fp_exclude_size = !!neg_opt;
4192 if (optarg[0] == '+') {
4193 param.fp_stripe_size_sign = -1;
4195 } else if (optarg[0] == '-') {
4196 param.fp_stripe_size_sign = 1;
4200 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4201 ¶m.fp_stripe_size_units, 0);
4203 fprintf(stderr, "error: bad stripe_size '%s'\n",
4207 param.fp_check_stripe_size = 1;
4208 param.fp_exclude_stripe_size = !!neg_opt;
4211 param.fp_exclude_type = !!neg_opt;
4212 switch (optarg[0]) {
4214 param.fp_type = S_IFBLK;
4217 param.fp_type = S_IFCHR;
4220 param.fp_type = S_IFDIR;
4223 param.fp_type = S_IFREG;
4226 param.fp_type = S_IFLNK;
4229 param.fp_type = S_IFIFO;
4232 param.fp_type = S_IFSOCK;
4235 fprintf(stderr, "error: %s: bad type '%s'\n",
4242 if (optarg[0] == '+') {
4243 param.fp_mdt_count_sign = -1;
4245 } else if (optarg[0] == '-') {
4246 param.fp_mdt_count_sign = 1;
4250 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4251 if (*endptr != '\0') {
4252 fprintf(stderr, "error: bad mdt_count '%s'\n",
4257 param.fp_check_mdt_count = 1;
4258 param.fp_exclude_mdt_count = !!neg_opt;
4266 if (pathstart == -1) {
4267 fprintf(stderr, "error: %s: no filename|pathname\n",
4271 } else if (pathend == -1) {
4277 rc = llapi_find(argv[pathstart], ¶m);
4278 if (rc != 0 && ret == 0)
4280 } while (++pathstart < pathend);
4283 fprintf(stderr, "error: %s failed for %s.\n",
4284 argv[0], argv[optind - 1]);
4286 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4287 free(param.fp_obd_uuid);
4289 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4290 free(param.fp_mdt_uuid);
4295 static int lfs_getstripe_internal(int argc, char **argv,
4296 struct find_param *param)
4298 struct option long_opts[] = {
4299 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4300 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4301 { .val = LFS_COMP_COUNT_OPT,
4302 .name = "comp-count", .has_arg = no_argument },
4303 { .val = LFS_COMP_COUNT_OPT,
4304 .name = "component-count", .has_arg = no_argument },
4305 { .val = LFS_COMP_FLAGS_OPT,
4306 .name = "comp-flags", .has_arg = optional_argument },
4307 { .val = LFS_COMP_FLAGS_OPT,
4308 .name = "component-flags", .has_arg = optional_argument },
4309 { .val = LFS_COMP_START_OPT,
4310 .name = "comp-start", .has_arg = optional_argument },
4311 { .val = LFS_COMP_START_OPT,
4312 .name = "component-start", .has_arg = optional_argument },
4313 { .val = LFS_MIRROR_INDEX_OPT,
4314 .name = "mirror-index", .has_arg = required_argument },
4315 { .val = LFS_MIRROR_ID_OPT,
4316 .name = "mirror-id", .has_arg = required_argument },
4317 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4318 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4319 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4320 { .val = 'd', .name = "directory", .has_arg = no_argument },
4321 { .val = 'D', .name = "default", .has_arg = no_argument },
4322 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4323 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4324 { .val = 'F', .name = "fid", .has_arg = no_argument },
4325 { .val = 'g', .name = "generation", .has_arg = no_argument },
4326 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4327 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4328 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4329 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4330 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4331 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4332 { .val = 'L', .name = "layout", .has_arg = no_argument },
4333 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4334 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4335 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4336 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4337 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4338 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4339 { .val = 'O', .name = "obd", .has_arg = required_argument },
4340 { .val = 'O', .name = "ost", .has_arg = required_argument },
4341 { .val = 'p', .name = "pool", .has_arg = no_argument },
4342 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4343 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4344 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4345 { .val = 'R', .name = "raw", .has_arg = no_argument },
4346 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4347 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4348 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4349 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4350 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4351 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4352 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4353 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4357 int pathstart = -1, pathend = -1;
4361 while ((c = getopt_long(argc, argv,
4362 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4363 long_opts, NULL)) != -1) {
4367 /* '!' is part of option */
4368 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4369 if (!isoption && pathend != -1) {
4371 "error: %s: filename|dirname must either precede options or follow options\n",
4375 if (!isoption && pathstart == -1)
4376 pathstart = optind - 1;
4377 if (isoption && pathstart != -1 && pathend == -1)
4378 pathend = optind - 2;
4382 /* unknown: opt is "!" */
4383 if (strcmp(optarg, "!") == 0)
4387 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4388 param->fp_verbose |= VERBOSE_COUNT;
4389 param->fp_max_depth = 0;
4392 case LFS_COMP_COUNT_OPT:
4393 param->fp_verbose |= VERBOSE_COMP_COUNT;
4394 param->fp_max_depth = 0;
4396 case LFS_COMP_FLAGS_OPT:
4397 if (optarg != NULL) {
4398 rc = comp_str2flags(optarg,
4399 ¶m->fp_comp_flags,
4400 ¶m->fp_comp_neg_flags);
4402 fprintf(stderr, "error: %s bad "
4403 "component flags '%s'.\n",
4407 param->fp_check_comp_flags = 1;
4409 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4410 param->fp_max_depth = 0;
4413 case LFS_COMP_START_OPT:
4414 if (optarg != NULL) {
4416 if (tmp[0] == '+') {
4417 param->fp_comp_start_sign = -1;
4419 } else if (tmp[0] == '-') {
4420 param->fp_comp_start_sign = 1;
4423 rc = llapi_parse_size(tmp,
4424 ¶m->fp_comp_start,
4425 ¶m->fp_comp_start_units, 0);
4427 fprintf(stderr, "error: %s bad "
4428 "component start '%s'.\n",
4432 param->fp_check_comp_start = 1;
4435 param->fp_verbose |= VERBOSE_COMP_START;
4436 param->fp_max_depth = 0;
4439 case LFS_MIRROR_INDEX_OPT:
4440 if (optarg[0] == '+') {
4441 param->fp_mirror_index_sign = -1;
4443 } else if (optarg[0] == '-') {
4444 param->fp_mirror_index_sign = 1;
4448 param->fp_mirror_index = strtoul(optarg, &end, 0);
4449 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4450 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4452 "%s %s: invalid mirror index '%s'\n",
4453 progname, argv[0], optarg);
4456 if (param->fp_mirror_id != 0) {
4458 "%s %s: can't specify both mirror index and mirror ID\n",
4462 param->fp_check_mirror_index = 1;
4463 param->fp_exclude_mirror_index = !!neg_opt;
4465 case LFS_MIRROR_ID_OPT:
4466 if (optarg[0] == '+') {
4467 param->fp_mirror_id_sign = -1;
4469 } else if (optarg[0] == '-') {
4470 param->fp_mirror_id_sign = 1;
4474 param->fp_mirror_id = strtoul(optarg, &end, 0);
4475 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4476 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4478 "%s %s: invalid mirror ID '%s'\n",
4479 progname, argv[0], optarg);
4482 if (param->fp_mirror_index != 0) {
4484 "%s %s: can't specify both mirror index and mirror ID\n",
4488 param->fp_check_mirror_id = 1;
4489 param->fp_exclude_mirror_id = !!neg_opt;
4492 param->fp_max_depth = 0;
4495 param->fp_get_default_lmv = 1;
4498 if (optarg != NULL) {
4500 if (tmp[0] == '+') {
4501 param->fp_comp_end_sign = -1;
4503 } else if (tmp[0] == '-') {
4504 param->fp_comp_end_sign = 1;
4508 if (arg_is_eof(tmp)) {
4509 param->fp_comp_end = LUSTRE_EOF;
4510 param->fp_comp_end_units = 1;
4513 rc = llapi_parse_size(tmp,
4514 ¶m->fp_comp_end,
4515 ¶m->fp_comp_end_units, 0);
4518 fprintf(stderr, "error: %s bad "
4519 "component end '%s'.\n",
4523 param->fp_check_comp_end = 1;
4525 param->fp_verbose |= VERBOSE_COMP_END;
4526 param->fp_max_depth = 0;
4530 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4531 param->fp_verbose |= VERBOSE_DFID;
4532 param->fp_max_depth = 0;
4536 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4537 param->fp_verbose |= VERBOSE_GENERATION;
4538 param->fp_max_depth = 0;
4542 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4543 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4544 param->fp_max_depth = 0;
4548 if (optarg != NULL) {
4549 param->fp_comp_id = strtoul(optarg, &end, 0);
4550 if (*end != '\0' || param->fp_comp_id == 0 ||
4551 param->fp_comp_id > LCME_ID_MAX) {
4552 fprintf(stderr, "error: %s bad "
4553 "component id '%s'\n",
4557 param->fp_check_comp_id = 1;
4560 param->fp_max_depth = 0;
4561 param->fp_verbose |= VERBOSE_COMP_ID;
4565 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4566 param->fp_verbose |= VERBOSE_PATTERN;
4567 param->fp_max_depth = 0;
4570 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4572 fprintf(stderr, "warning: '-M' deprecated"
4573 ", use '--mdt-index' or '-m' instead\n");
4576 if (!(param->fp_verbose & VERBOSE_DETAIL))
4577 param->fp_max_depth = 0;
4578 param->fp_verbose |= VERBOSE_MDTINDEX;
4581 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4582 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4583 param->fp_max_depth = 0;
4587 if (param->fp_obd_uuid) {
4589 "error: %s: only one obduuid allowed",
4593 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4596 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4597 param->fp_verbose |= VERBOSE_POOL;
4598 param->fp_max_depth = 0;
4605 param->fp_recursive = 1;
4611 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4612 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4613 param->fp_max_depth = 0;
4617 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4627 if (pathstart == -1) {
4628 fprintf(stderr, "error: %s: no filename|pathname\n",
4631 } else if (pathend == -1) {
4639 if (param->fp_recursive)
4640 param->fp_max_depth = -1;
4641 else if (param->fp_verbose & VERBOSE_DETAIL)
4642 param->fp_max_depth = 1;
4644 if (!param->fp_verbose)
4645 param->fp_verbose = VERBOSE_DEFAULT;
4646 if (param->fp_quiet)
4647 param->fp_verbose = VERBOSE_OBJID;
4650 rc = llapi_getstripe(argv[pathstart], param);
4651 } while (++pathstart < pathend && !rc);
4654 fprintf(stderr, "error: %s failed for %s.\n",
4655 argv[0], argv[optind - 1]);
4659 static int lfs_tgts(int argc, char **argv)
4661 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4662 struct find_param param;
4663 int index = 0, rc=0;
4668 if (argc == 2 && !realpath(argv[1], path)) {
4670 fprintf(stderr, "error: invalid path '%s': %s\n",
4671 argv[1], strerror(-rc));
4675 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4676 /* Check if we have a mount point */
4677 if (mntdir[0] == '\0')
4680 memset(¶m, 0, sizeof(param));
4681 if (!strcmp(argv[0], "mdts"))
4682 param.fp_get_lmv = 1;
4684 rc = llapi_ostlist(mntdir, ¶m);
4686 fprintf(stderr, "error: %s: failed on %s\n",
4689 if (path[0] != '\0')
4691 memset(mntdir, 0, PATH_MAX);
4697 static int lfs_getstripe(int argc, char **argv)
4699 struct find_param param = { 0 };
4701 param.fp_max_depth = 1;
4702 return lfs_getstripe_internal(argc, argv, ¶m);
4706 static int lfs_getdirstripe(int argc, char **argv)
4708 struct find_param param = { 0 };
4709 struct option long_opts[] = {
4710 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4711 { .val = 'D', .name = "default", .has_arg = no_argument },
4712 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4713 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4714 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4715 { .val = 'O', .name = "obd", .has_arg = required_argument },
4716 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4717 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4718 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4719 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4723 param.fp_get_lmv = 1;
4725 while ((c = getopt_long(argc, argv,
4726 "cDHimO:rtTvy", long_opts, NULL)) != -1)
4731 param.fp_verbose |= VERBOSE_COUNT;
4734 param.fp_get_default_lmv = 1;
4738 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4740 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4742 fprintf(stderr, "warning: '-t' deprecated, "
4743 "use '--mdt-hash' or '-H' instead\n");
4746 param.fp_verbose |= VERBOSE_HASH_TYPE;
4749 if (param.fp_obd_uuid) {
4751 "error: %s: only one obduuid allowed",
4755 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4758 param.fp_recursive = 1;
4761 param.fp_verbose |= VERBOSE_DETAIL;
4767 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4768 progname, argv[0], argv[optind - 1]);
4776 if (param.fp_recursive)
4777 param.fp_max_depth = -1;
4779 if (!param.fp_verbose)
4780 param.fp_verbose = VERBOSE_DEFAULT;
4783 rc = llapi_getstripe(argv[optind], ¶m);
4784 } while (++optind < argc && !rc);
4787 fprintf(stderr, "error: %s failed for %s.\n",
4788 argv[0], argv[optind - 1]);
4793 MNTDF_INODES = 0x0001,
4794 MNTDF_COOKED = 0x0002,
4795 MNTDF_LAZY = 0x0004,
4796 MNTDF_VERBOSE = 0x0008,
4797 MNTDF_SHOW = 0x0010,
4800 #define COOK(value) \
4803 while (value > 1024) { \
4811 #define CDF "%11llu"
4812 #define HDF "%8.1f%c"
4816 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4818 double avail, used, ratio = 0;
4821 avail = st->os_ffree;
4822 used = st->os_files - st->os_ffree;
4824 avail = st->os_bavail;
4825 used = st->os_blocks - st->os_bfree;
4827 if (avail + used > 0)
4828 ratio = used / (used + avail) * 100;
4830 /* Round up to match df(1) usage percentage */
4831 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4834 static int showdf(char *mntdir, struct obd_statfs *stat,
4835 char *uuid, enum mntdf_flags flags,
4836 char *type, int index, int rc)
4838 long long avail, used, total;
4840 char *suffix = "KMGTPEZY";
4841 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4842 char tbuf[3 * sizeof(__u64)];
4843 char ubuf[3 * sizeof(__u64)];
4844 char abuf[3 * sizeof(__u64)];
4845 char rbuf[3 * sizeof(__u64)];
4852 if (flags & MNTDF_INODES) {
4853 avail = stat->os_ffree;
4854 used = stat->os_files - stat->os_ffree;
4855 total = stat->os_files;
4857 int shift = flags & MNTDF_COOKED ? 0 : 10;
4859 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4860 used = ((stat->os_blocks - stat->os_bfree) *
4861 stat->os_bsize) >> shift;
4862 total = (stat->os_blocks * stat->os_bsize) >> shift;
4865 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4867 if (flags & MNTDF_COOKED) {
4871 cook_val = (double)total;
4874 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4877 snprintf(tbuf, sizeof(tbuf), CDF, total);
4879 cook_val = (double)used;
4882 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4885 snprintf(ubuf, sizeof(ubuf), CDF, used);
4887 cook_val = (double)avail;
4890 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4893 snprintf(abuf, sizeof(abuf), CDF, avail);
4895 snprintf(tbuf, sizeof(tbuf), CDF, total);
4896 snprintf(ubuf, sizeof(tbuf), CDF, used);
4897 snprintf(abuf, sizeof(tbuf), CDF, avail);
4900 sprintf(rbuf, RDF, ratio);
4901 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4902 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4904 printf("[%s:%d]", type, index);
4906 if (stat->os_state) {
4908 * Each character represents the matching
4911 const char state_names[] = "DRSI";
4916 for (i = 0, state = stat->os_state;
4917 state && i < sizeof(state_names); i++) {
4918 if (!(state & (1 << i)))
4920 printf("%c", state_names[i]);
4928 printf(UUF": inactive device\n", uuid);
4931 printf(UUF": %s\n", uuid, strerror(-rc));
4938 struct ll_stat_type {
4943 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4945 struct ll_statfs_data {
4947 struct obd_statfs sd_st;
4950 struct ll_statfs_buf {
4952 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4955 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4956 int ops, struct ll_statfs_buf *lsb)
4958 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4959 struct obd_uuid uuid_buf;
4960 char *poolname = NULL;
4961 struct ll_stat_type types[] = {
4962 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4963 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4964 { .st_name = NULL } };
4965 struct ll_stat_type *tp;
4966 __u64 ost_ffree = 0;
4974 poolname = strchr(pool, '.');
4975 if (poolname != NULL) {
4976 if (strncmp(fsname, pool, strlen(fsname))) {
4977 fprintf(stderr, "filesystem name incorrect\n");
4985 fd = open(mntdir, O_RDONLY);
4988 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4993 if (flags & MNTDF_SHOW) {
4994 if (flags & MNTDF_INODES)
4995 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4996 "UUID", "Inodes", "IUsed", "IFree",
4997 "IUse%", "Mounted on");
4999 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5001 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5002 "Used", "Available", "Use%", "Mounted on");
5005 for (tp = types; tp->st_name != NULL; tp++) {
5006 bool have_ost = false;
5008 if (!(tp->st_op & ops))
5011 for (index = 0; ; index++) {
5012 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5013 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5014 type = flags & MNTDF_LAZY ?
5015 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5016 rc2 = llapi_obd_fstatfs(fd, type, index,
5017 &stat_buf, &uuid_buf);
5022 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5023 if (!(flags & MNTDF_VERBOSE))
5025 } else if (rc2 < 0 && rc == 0) {
5029 /* If we have OSTs then don't report MDT block counts.
5030 * For MDT-only filesystems the expectation is that all
5031 * layouts have a DoM component. For filesystems with
5032 * OSTs, files are not necessarily going to store data
5033 * on MDTs, and MDT space is limited to a fraction of
5034 * OST space, so don't include it in the summary.
5036 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5043 if (poolname && tp->st_op == LL_STATFS_LOV &&
5044 llapi_search_ost(fsname, poolname,
5045 obd_uuid2str(&uuid_buf)) != 1)
5048 /* the llapi_obd_statfs() call may have returned with
5049 * an error, but if it filled in uuid_buf we will at
5050 * lease use that to print out a message for that OBD.
5051 * If we didn't get anything in the uuid_buf, then fill
5052 * it in so that we can print an error message. */
5053 if (uuid_buf.uuid[0] == '\0')
5054 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5055 "%s%04x", tp->st_name, index);
5057 lsb->sb_buf[lsb->sb_count].sd_index = index;
5058 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5061 if (flags & MNTDF_SHOW)
5062 showdf(mntdir, &stat_buf,
5063 obd_uuid2str(&uuid_buf), flags,
5064 tp->st_name, index, rc2);
5069 if (tp->st_op == LL_STATFS_LMV) {
5070 sum.os_ffree += stat_buf.os_ffree;
5071 sum.os_files += stat_buf.os_files;
5072 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5073 ost_ffree += stat_buf.os_ffree;
5075 sum.os_blocks += stat_buf.os_blocks *
5077 sum.os_bfree += stat_buf.os_bfree *
5079 sum.os_bavail += stat_buf.os_bavail *
5086 /* If we don't have as many objects free on the OST as inodes
5087 * on the MDS, we reduce the total number of inodes to
5088 * compensate, so that the "inodes in use" number is correct.
5089 * Matches ll_statfs_internal() so the results are consistent. */
5090 if (ost_ffree < sum.os_ffree) {
5091 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5092 sum.os_ffree = ost_ffree;
5094 if (flags & MNTDF_SHOW) {
5096 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5103 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5105 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5107 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5109 int r1 = obd_statfs_ratio(st1, false);
5110 int r2 = obd_statfs_ratio(st2, false);
5111 int64_t result = r1 - r2;
5113 /* if both space usage are above 90, compare free inodes */
5114 if (r1 > 90 && r2 > 90)
5115 result = st2->os_ffree - st1->os_ffree;
5119 else if (result == 0)
5126 static int lfs_setdirstripe(int argc, char **argv)
5130 struct lfs_setstripe_args lsa = { 0 };
5131 struct llapi_stripe_param *param = NULL;
5132 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5135 char *mode_opt = NULL;
5136 bool default_stripe = false;
5137 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5138 mode_t previous_mode = 0;
5139 bool delete = false;
5140 struct ll_statfs_buf *lsb = NULL;
5141 char mntdir[PATH_MAX] = "";
5142 bool auto_distributed = false;
5143 bool foreign_mode = false;
5145 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5147 struct option long_opts[] = {
5148 { .val = 'c', .name = "count", .has_arg = required_argument },
5149 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5150 { .val = 'd', .name = "delete", .has_arg = no_argument },
5151 { .val = 'D', .name = "default", .has_arg = no_argument },
5152 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5153 { .val = LFS_LAYOUT_FLAGS_OPT,
5154 .name = "flags", .has_arg = required_argument },
5155 { .val = LFS_LAYOUT_FOREIGN_OPT,
5156 .name = "foreign", .has_arg = optional_argument},
5157 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5158 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5159 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5160 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5162 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5163 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5165 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5166 { .val = 'i', .name = "index", .has_arg = required_argument },
5168 { .val = 'o', .name = "mode", .has_arg = required_argument },
5169 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5170 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5172 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5173 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5174 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5177 setstripe_args_init(&lsa);
5179 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5180 long_opts, NULL)) >= 0) {
5187 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5190 "%s %s: invalid stripe count '%s'\n",
5191 progname, argv[0], optarg);
5197 default_stripe = true;
5200 default_stripe = true;
5202 case LFS_LAYOUT_FOREIGN_OPT:
5203 if (optarg != NULL) {
5204 /* check pure numeric */
5205 type = strtoul(optarg, &end, 0);
5208 type = check_foreign_type_name(optarg);
5209 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5211 "%s %s: unknown foreign type '%s'\n",
5218 foreign_mode = true;
5220 case LFS_LAYOUT_FLAGS_OPT:
5221 flags = strtoul(optarg, &end, 16);
5224 "%s %s: bad flags '%s'\n",
5225 progname, argv[0], optarg);
5229 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5231 fprintf(stderr, "warning: '--hash-type' and '-t' "
5232 "deprecated, use '--mdt-hash' or '-H' instead\n");
5235 lsa.lsa_pattern = check_hashtype(optarg);
5236 if (lsa.lsa_pattern == 0) {
5238 "%s %s: bad stripe hash type '%s'\n",
5239 progname, argv[0], optarg);
5244 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5247 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5248 if (strcmp(argv[optind - 1], "--index") == 0)
5250 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5253 lsa.lsa_nr_tgts = parse_targets(mdts,
5254 sizeof(mdts) / sizeof(__u32),
5255 lsa.lsa_nr_tgts, optarg);
5256 if (lsa.lsa_nr_tgts < 0) {
5258 "%s %s: invalid MDT target(s) '%s'\n",
5259 progname, argv[0], optarg);
5263 lsa.lsa_tgts = mdts;
5264 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5265 lsa.lsa_stripe_off = mdts[0];
5267 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5269 fprintf(stderr, "warning: '-m' is deprecated, "
5270 "use '--mode' or '-o' instead\n");
5279 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5280 progname, argv[0], argv[optind - 1]);
5285 if (optind == argc) {
5286 fprintf(stderr, "%s %s: DIR must be specified\n",
5291 if (xattr && !foreign_mode) {
5292 /* only print a warning as this is armless and will be
5296 "%s %s: xattr has been specified for non-foreign layout\n",
5298 } else if (foreign_mode && !xattr) {
5300 "%s %s: xattr must be provided in foreign mode\n",
5305 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5306 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5308 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5313 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5314 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5316 "%s %s: stripe offset and count must be specified\n",
5322 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5323 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5325 "%s %s: cannot specify -d with -c or -i options\n",
5330 if (mode_opt != NULL) {
5331 mode = strtoul(mode_opt, &end, 8);
5334 "%s %s: bad MODE '%s'\n",
5335 progname, argv[0], mode_opt);
5338 previous_mode = umask(0);
5341 /* foreign LMV/dir case */
5343 if (argc > optind + 1) {
5345 "%s %s: cannot specify multiple foreign dirs\n",
5350 dname = argv[optind];
5351 result = llapi_dir_create_foreign(dname, mode, type, flags,
5355 "%s mkdir: can't create foreign dir '%s': %s\n",
5356 progname, dname, strerror(-result));
5361 * initialize stripe parameters, in case param is converted to specific,
5362 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5364 param = calloc(1, offsetof(typeof(*param),
5365 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5366 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5367 if (param == NULL) {
5369 "%s %s: cannot allocate memory for parameters: %s\n",
5370 progname, argv[0], strerror(ENOMEM));
5374 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5375 param->lsp_stripe_count = lsa.lsa_stripe_count;
5376 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5377 param->lsp_stripe_offset = -1;
5379 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5380 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5381 param->lsp_stripe_pattern = lsa.lsa_pattern;
5383 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5384 param->lsp_pool = lsa.lsa_pool_name;
5385 param->lsp_is_specific = false;
5386 if (lsa.lsa_nr_tgts > 1) {
5387 if (lsa.lsa_stripe_count > 0 &&
5388 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5389 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5390 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5391 "match the number of MDTs: %d\n",
5392 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5397 param->lsp_is_specific = true;
5398 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5399 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5402 dname = argv[optind];
5404 if (default_stripe) {
5405 result = llapi_dir_set_default_lmv(dname, param);
5407 /* if current \a dname isn't under the same \a mntdir
5408 * as the last one, and the last one was
5409 * auto-distributed, restore \a param.
5411 if (mntdir[0] != '\0' &&
5412 strncmp(dname, mntdir, strlen(mntdir)) &&
5414 param->lsp_is_specific = false;
5415 param->lsp_stripe_offset = -1;
5416 auto_distributed = false;
5419 if (!param->lsp_is_specific &&
5420 param->lsp_stripe_offset == -1) {
5421 char path[PATH_MAX] = "";
5424 lsb = malloc(sizeof(*lsb));
5432 /* use mntdir for dirname() temporarily */
5433 strncpy(mntdir, dname, sizeof(mntdir));
5434 if (!realpath(dirname(mntdir), path)) {
5437 "error: invalid path '%s': %s\n",
5438 argv[optind], strerror(errno));
5443 result = llapi_search_mounts(path, 0, mntdir,
5445 if (result < 0 || mntdir[0] == '\0') {
5447 "No suitable Lustre mount found\n");
5451 result = mntdf(mntdir, NULL, NULL, 0,
5452 LL_STATFS_LMV, lsb);
5456 if (param->lsp_stripe_count > lsb->sb_count) {
5458 "error: stripe count %d is too big\n",
5459 param->lsp_stripe_count);
5464 qsort(lsb->sb_buf, lsb->sb_count,
5465 sizeof(struct ll_statfs_data),
5466 ll_statfs_data_comp);
5468 auto_distributed = true;
5471 if (auto_distributed) {
5473 int nr = MAX(param->lsp_stripe_count,
5476 /* don't use server whose usage is above 90% */
5477 while (nr != param->lsp_stripe_count &&
5478 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5480 nr = MAX(param->lsp_stripe_count,
5483 /* get \a r between [0, nr) */
5486 param->lsp_stripe_offset =
5487 lsb->sb_buf[r].sd_index;
5488 if (param->lsp_stripe_count > 1) {
5491 param->lsp_is_specific = true;
5492 for (; i < param->lsp_stripe_count; i++)
5493 param->lsp_tgts[(i + r) % nr] =
5494 lsb->sb_buf[i].sd_index;
5498 result = llapi_dir_create(dname, mode, param);
5503 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5504 progname, dname, strerror(-result));
5507 dname = argv[++optind];
5508 } while (dname != NULL);
5510 if (mode_opt != NULL)
5511 umask(previous_mode);
5519 static int lfs_rmentry(int argc, char **argv)
5526 fprintf(stderr, "error: %s: missing dirname\n",
5532 dname = argv[index];
5533 while (dname != NULL) {
5534 result = llapi_direntry_remove(dname);
5536 fprintf(stderr, "error: %s: remove dir entry '%s' "
5537 "failed\n", argv[0], dname);
5540 dname = argv[++index];
5545 static int lfs_mv(int argc, char **argv)
5547 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5548 struct find_param param = {
5555 struct option long_opts[] = {
5556 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5557 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5558 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5561 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5563 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5565 fprintf(stderr, "warning: '-M' deprecated"
5566 ", use '--mdt-index' or '-m' instead\n");
5569 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5571 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5577 param.fp_verbose = VERBOSE_DETAIL;
5580 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5581 progname, argv[optind - 1]);
5586 if (lmu.lum_stripe_offset == -1) {
5587 fprintf(stderr, "%s mv: MDT index must be specified\n",
5592 if (optind >= argc) {
5593 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5598 /* initialize migrate mdt parameters */
5599 param.fp_lmv_md = &lmu;
5600 param.fp_migrate = 1;
5601 rc = llapi_migrate_mdt(argv[optind], ¶m);
5603 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5604 progname, argv[optind], param.fp_mdt_index,
5609 static int lfs_osts(int argc, char **argv)
5611 return lfs_tgts(argc, argv);
5614 static int lfs_mdts(int argc, char **argv)
5616 return lfs_tgts(argc, argv);
5619 static int lfs_df(int argc, char **argv)
5621 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5622 enum mntdf_flags flags = MNTDF_SHOW;
5623 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5624 int c, rc = 0, index = 0;
5625 char fsname[PATH_MAX] = "", *pool_name = NULL;
5626 struct option long_opts[] = {
5627 { .val = 'h', .name = "human-readable",
5628 .has_arg = no_argument },
5629 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5630 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5631 { .val = 'p', .name = "pool", .has_arg = required_argument },
5632 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5635 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5638 flags |= MNTDF_COOKED;
5641 flags |= MNTDF_INODES;
5644 flags |= MNTDF_LAZY;
5650 flags |= MNTDF_VERBOSE;
5656 if (optind < argc && !realpath(argv[optind], path)) {
5658 fprintf(stderr, "error: invalid path '%s': %s\n",
5659 argv[optind], strerror(-rc));
5663 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5664 /* Check if we have a mount point */
5665 if (mntdir[0] == '\0')
5668 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5669 if (rc || path[0] != '\0')
5671 fsname[0] = '\0'; /* avoid matching in next loop */
5672 mntdir[0] = '\0'; /* avoid matching in next loop */
5678 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5679 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5683 /* llapi_search_mounts() fills "fsname", but that is not called if
5684 * explicit paths are specified on the command-line
5686 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5687 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5690 fprintf(stderr, "cannot get instance for '%s': %s\n",
5691 mntdir, strerror(-rc));
5695 if (!opt_instance) {
5696 /* print only the fsname name */
5697 buf = strchr(fsname, '-');
5701 } else if (!opt_fsname) {
5702 /* print only the instance name */
5703 buf = strchr(fsname, '-');
5709 } else if (opt_fsname) {
5710 /* print only the fsname */
5714 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5719 static int lfs_getname(int argc, char **argv)
5721 struct option long_opts[] = {
5722 { .val = 'h', .name = "help", .has_arg = no_argument },
5723 { .val = 'i', .name = "instance", .has_arg = no_argument },
5724 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5726 bool opt_instance = false, opt_fsname = false;
5727 char fsname[PATH_MAX] = "";
5730 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5733 opt_instance = true;
5744 /* If neither option is given, print both instance and fsname */
5745 if (!opt_instance && !opt_fsname)
5746 opt_instance = opt_fsname = true;
5748 if (optind == argc) { /* no paths specified, get all paths. */
5749 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5752 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5753 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5754 opt_instance, opt_fsname, true);
5757 path[0] = fsname[0] = mntdir[0] = '\0';
5759 } else { /* paths specified, only attempt to search these. */
5762 /* if only one path is given, print only requested info */
5763 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5765 for (; optind < argc; optind++) {
5766 rc2 = print_instance(argv[optind], fsname,
5767 sizeof(fsname), opt_instance,
5768 opt_fsname, opt_mntdir);
5778 static int lfs_check(int argc, char **argv)
5780 char mntdir[PATH_MAX] = {'\0'};
5789 fprintf(stderr, "%s check: server type must be specified\n",
5794 obd_types[0] = obd_type1;
5795 obd_types[1] = obd_type2;
5796 obd_types[2] = obd_type3;
5798 if (strcmp(argv[1], "osts") == 0) {
5799 strcpy(obd_types[0], "osc");
5800 } else if (strcmp(argv[1], "mdts") == 0 ||
5801 strcmp(argv[1], "mds") == 0) {
5802 strcpy(obd_types[0], "mdc");
5803 } else if (strcmp(argv[1], "mgts") == 0) {
5804 strcpy(obd_types[0], "mgc");
5805 } else if (strcmp(argv[1], "all") == 0 ||
5806 strcmp(argv[1], "servers") == 0) {
5808 strcpy(obd_types[0], "osc");
5809 strcpy(obd_types[1], "mdc");
5810 strcpy(obd_types[2], "mgc");
5812 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5817 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5818 if (rc < 0 || mntdir[0] == '\0') {
5820 "%s check: cannot find mounted Lustre filesystem: %s\n",
5821 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5825 rc = llapi_target_check(num_types, obd_types, mntdir);
5827 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5828 progname, argv[1], strerror(-rc));
5834 #ifdef HAVE_SYS_QUOTA_H
5835 #define ARG2INT(nr, str, msg) \
5838 nr = strtol(str, &endp, 0); \
5839 if (*endp != '\0') { \
5840 fprintf(stderr, "%s: bad %s '%s'\n", \
5841 progname, msg, str); \
5846 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5848 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5849 * returns the value or ULONG_MAX on integer overflow or incorrect format
5851 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5852 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5853 * 3. empty integer value is interpreted as 0
5855 static unsigned long str2sec(const char* timestr)
5857 const char spec[] = "smhdw";
5858 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5859 unsigned long val = 0;
5862 if (strpbrk(timestr, spec) == NULL) {
5863 /* no specifiers inside the time string,
5864 should treat it as an integer value */
5865 val = strtoul(timestr, &tail, 10);
5866 return *tail ? ULONG_MAX : val;
5869 /* format string is XXwXXdXXhXXmXXs */
5875 v = strtoul(timestr, &tail, 10);
5876 if (v == ULONG_MAX || *tail == '\0')
5877 /* value too large (ULONG_MAX or more)
5878 or missing specifier */
5881 ptr = strchr(spec, *tail);
5883 /* unknown specifier */
5888 /* check if product will overflow the type */
5889 if (!(v < ULONG_MAX / mult[ind]))
5892 ADD_OVERFLOW(val, mult[ind] * v);
5893 if (val == ULONG_MAX)
5905 #define ARG2ULL(nr, str, def_units) \
5907 unsigned long long limit, units = def_units; \
5910 rc = llapi_parse_size(str, &limit, &units, 1); \
5912 fprintf(stderr, "%s: invalid limit '%s'\n", \
5919 static inline int has_times_option(int argc, char **argv)
5923 for (i = 1; i < argc; i++)
5924 if (!strcmp(argv[i], "-t"))
5930 int lfs_setquota_times(int argc, char **argv)
5933 struct if_quotactl qctl;
5934 char *mnt, *obd_type = (char *)qctl.obd_type;
5935 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5936 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5937 struct option long_opts[] = {
5938 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5939 { .val = 'g', .name = "group", .has_arg = no_argument },
5940 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5941 { .val = 'p', .name = "projid", .has_arg = no_argument },
5942 { .val = 't', .name = "times", .has_arg = no_argument },
5943 { .val = 'u', .name = "user", .has_arg = no_argument },
5947 memset(&qctl, 0, sizeof(qctl));
5948 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5949 qctl.qc_type = ALLQUOTA;
5951 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5952 long_opts, NULL)) != -1) {
5963 if (qctl.qc_type != ALLQUOTA) {
5964 fprintf(stderr, "error: -u/g/p can't be used "
5965 "more than once\n");
5968 qctl.qc_type = qtype;
5971 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5972 fprintf(stderr, "error: bad block-grace: %s\n",
5976 dqb->dqb_valid |= QIF_BTIME;
5979 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5980 fprintf(stderr, "error: bad inode-grace: %s\n",
5984 dqb->dqb_valid |= QIF_ITIME;
5986 case 't': /* Yes, of course! */
5988 default: /* getopt prints error message for us when opterr != 0 */
5993 if (qctl.qc_type == ALLQUOTA) {
5994 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5998 if (optind != argc - 1) {
5999 fprintf(stderr, "error: unexpected parameters encountered\n");
6003 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6004 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6005 fprintf(stderr, "error: grace time is too large\n");
6010 rc = llapi_quotactl(mnt, &qctl);
6013 fprintf(stderr, "%s %s ", obd_type,
6014 obd_uuid2str(&qctl.obd_uuid));
6015 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6022 #define BSLIMIT (1 << 0)
6023 #define BHLIMIT (1 << 1)
6024 #define ISLIMIT (1 << 2)
6025 #define IHLIMIT (1 << 3)
6027 int lfs_setquota(int argc, char **argv)
6030 struct if_quotactl qctl;
6031 char *mnt, *obd_type = (char *)qctl.obd_type;
6032 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6033 struct option long_opts[] = {
6034 { .val = 'b', .name = "block-softlimit",
6035 .has_arg = required_argument },
6036 { .val = 'B', .name = "block-hardlimit",
6037 .has_arg = required_argument },
6038 { .val = 'd', .name = "default", .has_arg = no_argument },
6039 { .val = 'g', .name = "group", .has_arg = required_argument },
6040 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6041 { .val = 'i', .name = "inode-softlimit",
6042 .has_arg = required_argument },
6043 { .val = 'I', .name = "inode-hardlimit",
6044 .has_arg = required_argument },
6045 { .val = 'p', .name = "projid", .has_arg = required_argument },
6046 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6047 { .val = 'u', .name = "user", .has_arg = required_argument },
6048 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6050 unsigned limit_mask = 0;
6052 bool use_default = false;
6055 if (has_times_option(argc, argv))
6056 return lfs_setquota_times(argc, argv);
6058 memset(&qctl, 0, sizeof(qctl));
6059 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6060 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6061 * so it can be used as a marker that qc_type
6062 * isn't reinitialized from command line */
6064 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6065 long_opts, NULL)) != -1) {
6068 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6071 goto quota_type_def;
6074 rc = name2uid(&qctl.qc_id, optarg);
6077 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6080 goto quota_type_def;
6083 rc = name2gid(&qctl.qc_id, optarg);
6086 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6089 goto quota_type_def;
6092 rc = name2projid(&qctl.qc_id, optarg);
6095 qctl.qc_id = strtoul(optarg, &endptr, 10);
6096 if (*endptr != '\0') {
6097 fprintf(stderr, "%s setquota: invalid"
6098 " id '%s'\n", progname, optarg);
6103 if (qctl.qc_id == 0) {
6104 fprintf(stderr, "%s setquota: can't set quota"
6105 " for root usr/group/project.\n",
6111 if (qctl.qc_type != ALLQUOTA) {
6113 "%s setquota: only one of -u, -U, -g,"
6114 " -G, -p or -P may be specified\n",
6118 qctl.qc_type = qtype;
6121 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6125 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6126 dqb->dqb_bsoftlimit >>= 10;
6127 limit_mask |= BSLIMIT;
6128 if (dqb->dqb_bsoftlimit &&
6129 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6131 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6132 "See '%s help setquota' or Lustre manual for details\n",
6133 progname, dqb->dqb_bsoftlimit,
6137 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6138 dqb->dqb_bhardlimit >>= 10;
6139 limit_mask |= BHLIMIT;
6140 if (dqb->dqb_bhardlimit &&
6141 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6143 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6144 "See '%s help setquota' or Lustre manual for details\n",
6145 progname, dqb->dqb_bhardlimit,
6149 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6150 limit_mask |= ISLIMIT;
6151 if (dqb->dqb_isoftlimit &&
6152 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6154 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6155 "See '%s help setquota' or Lustre manual for details\n",
6156 progname, dqb->dqb_isoftlimit,
6160 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6161 limit_mask |= IHLIMIT;
6162 if (dqb->dqb_ihardlimit &&
6163 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6165 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6166 "See '%s help setquota' or Lustre manual for details\n",
6167 progname, dqb->dqb_ihardlimit,
6172 "%s setquota: unrecognized option '%s'\n",
6173 progname, argv[optind - 1]);
6178 if (qctl.qc_type == ALLQUOTA) {
6180 "%s setquota: either -u or -g must be specified\n",
6185 if (!use_default && limit_mask == 0) {
6187 "%s setquota: at least one limit must be specified\n",
6192 if (use_default && limit_mask != 0) {
6194 "%s setquota: limits should not be specified when"
6195 " using default quota\n",
6200 if (use_default && qctl.qc_id == 0) {
6202 "%s setquota: can not set default quota for root"
6203 " user/group/project\n",
6208 if (optind != argc - 1) {
6210 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6211 progname, argv[optind]);
6218 dqb->dqb_bhardlimit = 0;
6219 dqb->dqb_bsoftlimit = 0;
6220 dqb->dqb_ihardlimit = 0;
6221 dqb->dqb_isoftlimit = 0;
6224 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6225 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6226 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6227 /* sigh, we can't just set blimits/ilimits */
6228 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6229 .qc_type = qctl.qc_type,
6230 .qc_id = qctl.qc_id};
6232 rc = llapi_quotactl(mnt, &tmp_qctl);
6236 if (!(limit_mask & BHLIMIT))
6237 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6238 if (!(limit_mask & BSLIMIT))
6239 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6240 if (!(limit_mask & IHLIMIT))
6241 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6242 if (!(limit_mask & ISLIMIT))
6243 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6245 /* Keep grace times if we have got no softlimit arguments */
6246 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6247 dqb->dqb_valid |= QIF_BTIME;
6248 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6251 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6252 dqb->dqb_valid |= QIF_ITIME;
6253 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6257 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6258 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6260 rc = llapi_quotactl(mnt, &qctl);
6264 "%s setquota: cannot quotactl '%s' '%s': %s",
6266 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6273 /* Converts seconds value into format string
6274 * result is returned in buf
6276 * 1. result is in descenting order: 1w2d3h4m5s
6277 * 2. zero fields are not filled (except for p. 3): 5d1s
6278 * 3. zero seconds value is presented as "0s"
6280 static char * __sec2str(time_t seconds, char *buf)
6282 const char spec[] = "smhdw";
6283 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6288 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6289 c = seconds / mult[i];
6291 if (c > 0 || (i == 0 && buf == tail))
6292 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6300 static void sec2str(time_t seconds, char *buf, int rc)
6307 tail = __sec2str(seconds, tail);
6309 if (rc && tail - buf < 39) {
6315 static void diff2str(time_t seconds, char *buf, time_t now)
6321 if (seconds <= now) {
6322 strcpy(buf, "none");
6325 __sec2str(seconds - now, buf);
6328 static void print_quota_title(char *name, struct if_quotactl *qctl,
6329 bool human_readable, bool show_default)
6332 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6333 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6334 "Filesystem", "bquota", "blimit", "bgrace",
6335 "iquota", "ilimit", "igrace");
6337 printf("Disk quotas for %s %s (%cid %u):\n",
6338 qtype_name(qctl->qc_type), name,
6339 *qtype_name(qctl->qc_type), qctl->qc_id);
6340 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6341 "Filesystem", human_readable ? "used" : "kbytes",
6342 "quota", "limit", "grace",
6343 "files", "quota", "limit", "grace");
6347 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6350 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6353 snprintf(buf, buflen, "%5.4gP",
6354 (double)num / ((__u64)1 << 40));
6356 snprintf(buf, buflen, "%5.4gT",
6357 (double)num / (1 << 30));
6359 snprintf(buf, buflen, "%5.4gG",
6360 (double)num / (1 << 20));
6362 snprintf(buf, buflen, "%5.4gM",
6363 (double)num / (1 << 10));
6365 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6369 #define STRBUF_LEN 32
6370 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6371 int rc, bool h, bool show_default)
6377 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6378 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6379 int bover = 0, iover = 0;
6380 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6381 char numbuf[3][STRBUF_LEN];
6383 char strbuf[STRBUF_LEN];
6385 if (dqb->dqb_bhardlimit &&
6386 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6388 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6389 if (dqb->dqb_btime > now) {
6396 if (dqb->dqb_ihardlimit &&
6397 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6399 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6400 if (dqb->dqb_itime > now) {
6408 if (strlen(mnt) > 15)
6409 printf("%s\n%15s", mnt, "");
6411 printf("%15s", mnt);
6414 diff2str(dqb->dqb_btime, timebuf, now);
6415 else if (show_default)
6416 snprintf(timebuf, sizeof(timebuf), "%llu",
6419 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6420 strbuf, sizeof(strbuf), h);
6421 if (rc == -EREMOTEIO)
6422 sprintf(numbuf[0], "%s*", strbuf);
6424 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6425 "%s" : "[%s]", strbuf);
6427 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6428 if (type == QC_GENERAL)
6429 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6430 "%s" : "[%s]", strbuf);
6432 sprintf(numbuf[1], "%s", "-");
6434 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6435 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6436 "%s" : "[%s]", strbuf);
6439 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6441 printf(" %7s%c %6s %7s %7s",
6442 numbuf[0], bover ? '*' : ' ', numbuf[1],
6443 numbuf[2], bover > 1 ? timebuf : "-");
6447 diff2str(dqb->dqb_itime, timebuf, now);
6448 else if (show_default)
6449 snprintf(timebuf, sizeof(timebuf), "%llu",
6452 snprintf(numbuf[0], sizeof(numbuf),
6453 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6454 (uintmax_t)dqb->dqb_curinodes);
6456 if (type == QC_GENERAL)
6457 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6459 (uintmax_t)dqb->dqb_isoftlimit);
6461 sprintf(numbuf[1], "%s", "-");
6463 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6464 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6467 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6468 else if (type != QC_OSTIDX)
6469 printf(" %7s%c %6s %7s %7s",
6470 numbuf[0], iover ? '*' : ' ', numbuf[1],
6471 numbuf[2], iover > 1 ? timebuf : "-");
6473 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6476 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6477 qctl->qc_cmd == Q_GETOINFO) {
6481 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6482 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6483 printf("Block grace time: %s; Inode grace time: %s\n",
6484 bgtimebuf, igtimebuf);
6488 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6489 bool h, __u64 *total)
6491 int rc = 0, rc1 = 0, count = 0;
6492 __u32 valid = qctl->qc_valid;
6494 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6496 fprintf(stderr, "can not get %s count: %s\n",
6497 is_mdt ? "mdt": "ost", strerror(-rc));
6501 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6502 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6503 rc = llapi_quotactl(mnt, qctl);
6505 /* It is remote client case. */
6506 if (rc == -EOPNOTSUPP) {
6513 fprintf(stderr, "quotactl %s%d failed.\n",
6514 is_mdt ? "mdt": "ost", qctl->qc_idx);
6518 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6519 qctl->qc_valid, 0, h, false);
6520 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6521 qctl->qc_dqblk.dqb_bhardlimit;
6524 qctl->qc_valid = valid;
6528 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6529 int verbose, int quiet, bool human_readable,
6532 int rc1 = 0, rc2 = 0, rc3 = 0;
6533 char *obd_type = (char *)qctl->obd_type;
6534 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6535 __u64 total_ialloc = 0, total_balloc = 0;
6536 bool use_default_for_blk = false;
6537 bool use_default_for_file = false;
6540 rc1 = llapi_quotactl(mnt, qctl);
6544 fprintf(stderr, "%s quotas are not enabled.\n",
6545 qtype_name(qctl->qc_type));
6548 fprintf(stderr, "Permission denied.\n");
6551 /* We already got error message. */
6554 fprintf(stderr, "Unexpected quotactl error: %s\n",
6559 if (!show_default && qctl->qc_id == 0) {
6560 qctl->qc_dqblk.dqb_bhardlimit = 0;
6561 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6562 qctl->qc_dqblk.dqb_ihardlimit = 0;
6563 qctl->qc_dqblk.dqb_isoftlimit = 0;
6564 qctl->qc_dqblk.dqb_btime = 0;
6565 qctl->qc_dqblk.dqb_itime = 0;
6566 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6569 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6570 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6571 use_default_for_blk = true;
6572 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6575 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6576 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6577 use_default_for_file = true;
6578 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6581 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6582 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6583 print_quota_title(name, qctl, human_readable, show_default);
6585 if (rc1 && *obd_type)
6586 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6588 if (qctl->qc_valid != QC_GENERAL)
6591 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6592 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6593 (QIF_LIMITS|QIF_USAGE));
6595 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6597 if (!show_default && verbose &&
6598 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6599 char strbuf[STRBUF_LEN];
6601 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6603 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6605 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6607 printf("Total allocated inode limit: %ju, total "
6608 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6612 if (use_default_for_blk)
6613 printf("%cid %u is using default block quota setting\n",
6614 *qtype_name(qctl->qc_type), qctl->qc_id);
6616 if (use_default_for_file)
6617 printf("%cid %u is using default file quota setting\n",
6618 *qtype_name(qctl->qc_type), qctl->qc_id);
6620 if (rc1 || rc2 || rc3 || inacc)
6621 printf("Some errors happened when getting quota info. "
6622 "Some devices may be not working or deactivated. "
6623 "The data in \"[]\" is inaccurate.\n");
6629 static int lfs_project(int argc, char **argv)
6631 int ret = 0, err = 0, c, i;
6632 struct project_handle_control phc = { 0 };
6633 enum lfs_project_ops_t op;
6636 phc.assign_projid = false;
6637 /* default action */
6638 op = LFS_PROJECT_LIST;
6640 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6643 if (op != LFS_PROJECT_LIST) {
6645 "%s: cannot specify '-c' '-C' '-s' together\n",
6650 op = LFS_PROJECT_CHECK;
6653 if (op != LFS_PROJECT_LIST) {
6655 "%s: cannot specify '-c' '-C' '-s' together\n",
6660 op = LFS_PROJECT_CLEAR;
6663 if (op != LFS_PROJECT_LIST) {
6665 "%s: cannot specify '-c' '-C' '-s' together\n",
6670 phc.set_inherit = true;
6671 op = LFS_PROJECT_SET;
6677 phc.keep_projid = true;
6680 phc.recursive = true;
6683 phc.projid = strtoul(optarg, NULL, 0);
6684 phc.assign_projid = true;
6688 phc.newline = false;
6691 fprintf(stderr, "%s: invalid option '%c'\n",
6697 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6698 op = LFS_PROJECT_SET;
6699 phc.set_projid = true;
6700 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6701 phc.set_projid = true;
6705 case LFS_PROJECT_CHECK:
6706 if (phc.keep_projid) {
6708 "%s: '-k' is useless together with '-c'\n",
6713 case LFS_PROJECT_CLEAR:
6716 "%s: '-0' is useless together with '-C'\n",
6720 if (phc.assign_projid) {
6722 "%s: '-p' is useless together with '-C'\n",
6727 case LFS_PROJECT_SET:
6730 "%s: '-0' is useless together with '-s'\n",
6734 if (phc.keep_projid) {
6736 "%s: '-k' is useless together with '-s'\n",
6744 "%s: '-0' is useless for list operations\n",
6754 fprintf(stderr, "%s: missing file or directory target(s)\n",
6759 for (i = 0; i < argc; i++) {
6761 case LFS_PROJECT_CHECK:
6762 err = lfs_project_check(argv[i], &phc);
6764 case LFS_PROJECT_LIST:
6765 err = lfs_project_list(argv[i], &phc);
6767 case LFS_PROJECT_CLEAR:
6768 err = lfs_project_clear(argv[i], &phc);
6770 case LFS_PROJECT_SET:
6771 err = lfs_project_set(argv[i], &phc);
6783 static int lfs_quota(int argc, char **argv)
6786 char *mnt, *name = NULL;
6787 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6788 .qc_type = ALLQUOTA };
6789 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6790 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6792 __u32 valid = QC_GENERAL, idx = 0;
6793 bool human_readable = false;
6794 bool show_default = false;
6797 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6800 show_default = true;
6805 show_default = true;
6810 show_default = true;
6814 if (qctl.qc_type != ALLQUOTA) {
6816 "%s quota: only one of -u, -g, or -p may be specified\n",
6820 qctl.qc_type = qtype;
6823 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6826 valid = qctl.qc_valid = QC_UUID;
6827 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6830 valid = qctl.qc_valid = QC_MDTIDX;
6831 idx = qctl.qc_idx = atoi(optarg);
6832 if (idx == 0 && *optarg != '0') {
6834 "%s quota: invalid MDT index '%s'\n",
6840 valid = qctl.qc_valid = QC_OSTIDX;
6841 idx = qctl.qc_idx = atoi(optarg);
6842 if (idx == 0 && *optarg != '0') {
6844 "%s quota: invalid OST index '%s'\n",
6856 human_readable = true;
6859 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6860 progname, argv[optind - 1]);
6865 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6866 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6867 optind == argc - 1 && !show_default) {
6869 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6870 qctl.qc_valid = valid;
6873 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6874 qctl.qc_type = qtype;
6875 if (qtype == USRQUOTA) {
6876 qctl.qc_id = geteuid();
6877 rc = uid2name(&name, qctl.qc_id);
6879 qctl.qc_id = getegid();
6880 rc = gid2name(&name, qctl.qc_id);
6885 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6886 human_readable, show_default);
6891 /* lfs quota -u username /path/to/lustre/mount */
6892 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6893 /* options should be followed by u/g-name and mntpoint */
6894 if ((!show_default && optind + 2 != argc) ||
6895 (show_default && optind + 1 != argc) ||
6896 qctl.qc_type == ALLQUOTA) {
6898 "%s quota: name and mount point must be specified\n",
6903 if (!show_default) {
6904 name = argv[optind++];
6905 switch (qctl.qc_type) {
6907 rc = name2uid(&qctl.qc_id, name);
6910 rc = name2gid(&qctl.qc_id, name);
6913 rc = name2projid(&qctl.qc_id, name);
6920 qctl.qc_valid = QC_GENERAL;
6921 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6926 qctl.qc_id = strtoul(name, &endptr, 10);
6927 if (*endptr != '\0') {
6928 fprintf(stderr, "%s quota: invalid id '%s'\n",
6933 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6934 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6940 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6941 human_readable, show_default);
6944 #endif /* HAVE_SYS_QUOTA_H! */
6946 static int flushctx_ioctl(char *mp)
6950 fd = open(mp, O_RDONLY);
6952 fprintf(stderr, "flushctx: error open %s: %s\n",
6953 mp, strerror(errno));
6957 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6959 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6960 mp, strerror(errno));
6966 static int lfs_flushctx(int argc, char **argv)
6968 int kdestroy = 0, c;
6969 char mntdir[PATH_MAX] = {'\0'};
6973 while ((c = getopt(argc, argv, "k")) != -1) {
6979 fprintf(stderr, "error: %s: option '-%c' "
6980 "unrecognized\n", argv[0], c);
6986 if ((rc = system("kdestroy > /dev/null")) != 0) {
6987 rc = WEXITSTATUS(rc);
6988 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6992 if (optind >= argc) {
6993 /* flush for all mounted lustre fs. */
6994 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6995 /* Check if we have a mount point */
6996 if (mntdir[0] == '\0')
6999 if (flushctx_ioctl(mntdir))
7002 mntdir[0] = '\0'; /* avoid matching in next loop */
7005 /* flush fs as specified */
7006 while (optind < argc) {
7007 if (flushctx_ioctl(argv[optind++]))
7014 static int lfs_changelog(int argc, char **argv)
7016 void *changelog_priv;
7017 struct changelog_rec *rec;
7018 long long startrec = 0, endrec = 0;
7020 struct option long_opts[] = {
7021 { .val = 'f', .name = "follow", .has_arg = no_argument },
7023 char short_opts[] = "f";
7026 while ((rc = getopt_long(argc, argv, short_opts,
7027 long_opts, NULL)) != -1) {
7034 "%s changelog: unrecognized option '%s'\n",
7035 progname, argv[optind - 1]);
7039 if (optind >= argc) {
7040 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7045 mdd = argv[optind++];
7047 startrec = strtoll(argv[optind++], NULL, 10);
7049 endrec = strtoll(argv[optind++], NULL, 10);
7051 rc = llapi_changelog_start(&changelog_priv,
7052 CHANGELOG_FLAG_BLOCK |
7053 CHANGELOG_FLAG_JOBID |
7054 CHANGELOG_FLAG_EXTRA_FLAGS |
7055 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7058 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7059 progname, strerror(errno = -rc));
7063 rc = llapi_changelog_set_xflags(changelog_priv,
7064 CHANGELOG_EXTRA_FLAG_UIDGID |
7065 CHANGELOG_EXTRA_FLAG_NID |
7066 CHANGELOG_EXTRA_FLAG_OMODE |
7067 CHANGELOG_EXTRA_FLAG_XATTR);
7070 "%s changelog: cannot set xflags for changelog: %s\n",
7071 progname, strerror(errno = -rc));
7075 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7079 if (endrec && rec->cr_index > endrec) {
7080 llapi_changelog_free(&rec);
7083 if (rec->cr_index < startrec) {
7084 llapi_changelog_free(&rec);
7088 secs = rec->cr_time >> 30;
7089 gmtime_r(&secs, &ts);
7090 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7091 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7092 changelog_type2str(rec->cr_type),
7093 ts.tm_hour, ts.tm_min, ts.tm_sec,
7094 (int)(rec->cr_time & ((1 << 30) - 1)),
7095 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7096 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7098 if (rec->cr_flags & CLF_JOBID) {
7099 struct changelog_ext_jobid *jid =
7100 changelog_rec_jobid(rec);
7102 if (jid->cr_jobid[0] != '\0')
7103 printf(" j=%s", jid->cr_jobid);
7106 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7107 struct changelog_ext_extra_flags *ef =
7108 changelog_rec_extra_flags(rec);
7110 printf(" ef=0x%llx", ef->cr_extra_flags);
7112 if (ef->cr_extra_flags & CLFE_UIDGID) {
7113 struct changelog_ext_uidgid *uidgid =
7114 changelog_rec_uidgid(rec);
7116 printf(" u=%llu:%llu",
7117 uidgid->cr_uid, uidgid->cr_gid);
7119 if (ef->cr_extra_flags & CLFE_NID) {
7120 struct changelog_ext_nid *nid =
7121 changelog_rec_nid(rec);
7124 libcfs_nid2str(nid->cr_nid));
7127 if (ef->cr_extra_flags & CLFE_OPEN) {
7128 struct changelog_ext_openmode *omd =
7129 changelog_rec_openmode(rec);
7130 char mode[] = "---";
7132 /* exec mode must be exclusive */
7133 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7136 if (omd->cr_openflags & MDS_FMODE_READ)
7138 if (omd->cr_openflags &
7145 if (strcmp(mode, "---") != 0)
7146 printf(" m=%s", mode);
7150 if (ef->cr_extra_flags & CLFE_XATTR) {
7151 struct changelog_ext_xattr *xattr =
7152 changelog_rec_xattr(rec);
7154 if (xattr->cr_xattr[0] != '\0')
7155 printf(" x=%s", xattr->cr_xattr);
7159 if (rec->cr_namelen)
7160 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7161 rec->cr_namelen, changelog_rec_name(rec));
7163 if (rec->cr_flags & CLF_RENAME) {
7164 struct changelog_ext_rename *rnm =
7165 changelog_rec_rename(rec);
7167 if (!fid_is_zero(&rnm->cr_sfid))
7168 printf(" s="DFID" sp="DFID" %.*s",
7169 PFID(&rnm->cr_sfid),
7170 PFID(&rnm->cr_spfid),
7171 (int)changelog_rec_snamelen(rec),
7172 changelog_rec_sname(rec));
7176 llapi_changelog_free(&rec);
7179 llapi_changelog_fini(&changelog_priv);
7182 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7183 progname, strerror(errno = -rc));
7185 return (rc == 1 ? 0 : rc);
7188 static int lfs_changelog_clear(int argc, char **argv)
7196 endrec = strtoll(argv[3], NULL, 10);
7198 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7201 fprintf(stderr, "%s: record out of range: %llu\n",
7203 else if (rc == -ENOENT)
7204 fprintf(stderr, "%s: no changelog user: %s\n",
7207 fprintf(stderr, "%s error: %s\n", argv[0],
7216 static int lfs_fid2path(int argc, char **argv)
7218 struct option long_opts[] = {
7219 { .val = 'c', .name = "cur", .has_arg = no_argument },
7220 { .val = 'l', .name = "link", .has_arg = required_argument },
7221 { .val = 'r', .name = "rec", .has_arg = required_argument },
7223 char short_opts[] = "cl:r:";
7224 char *device, *fid, *path;
7225 long long recno = -1;
7230 char *endptr = NULL;
7232 while ((rc = getopt_long(argc, argv, short_opts,
7233 long_opts, NULL)) != -1) {
7239 linkno = strtol(optarg, &endptr, 10);
7240 if (*endptr != '\0') {
7242 "%s fid2path: invalid linkno '%s'\n",
7248 recno = strtoll(optarg, &endptr, 10);
7249 if (*endptr != '\0') {
7251 "%s fid2path: invalid recno '%s'\n",
7258 "%s fid2path: unrecognized option '%s'\n",
7259 progname, argv[optind - 1]);
7266 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7271 device = argv[optind++];
7272 path = calloc(1, PATH_MAX);
7276 "%s fid2path: cannot allocate memory for path: %s\n",
7277 progname, strerror(-rc));
7282 while (optind < argc) {
7283 fid = argv[optind++];
7285 lnktmp = (linkno >= 0) ? linkno : 0;
7287 int oldtmp = lnktmp;
7288 long long rectmp = recno;
7290 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7294 "%s fid2path: cannot find '%s': %s\n",
7295 progname, fid, strerror(errno = -rc2));
7302 fprintf(stdout, "%lld ", rectmp);
7303 if (device[0] == '/') {
7304 fprintf(stdout, "%s", device);
7305 if (device[strlen(device) - 1] != '/')
7306 fprintf(stdout, "/");
7307 } else if (path[0] == '\0') {
7308 fprintf(stdout, "/");
7310 fprintf(stdout, "%s\n", path);
7313 /* specified linkno */
7315 if (oldtmp == lnktmp)
7325 static int lfs_path2fid(int argc, char **argv)
7327 struct option long_opts[] = {
7328 { .val = 'p', .name = "parents", .has_arg = no_argument },
7331 const char short_opts[] = "p";
7332 const char *sep = "";
7335 bool show_parents = false;
7337 while ((rc = getopt_long(argc, argv, short_opts,
7338 long_opts, NULL)) != -1) {
7341 show_parents = true;
7345 "%s path2fid: unrecognized option '%s'\n",
7346 progname, argv[optind - 1]);
7351 if (optind > argc - 1) {
7352 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7356 else if (optind < argc - 1)
7360 for (path = argv + optind; *path != NULL; path++) {
7362 if (!show_parents) {
7363 err = llapi_path2fid(*path, &fid);
7365 printf("%s%s"DFID"\n",
7366 *sep != '\0' ? *path : "", sep,
7369 char name[NAME_MAX + 1];
7370 unsigned int linkno = 0;
7372 while ((err = llapi_path2parent(*path, linkno, &fid,
7373 name, sizeof(name))) == 0) {
7374 if (*sep != '\0' && linkno == 0)
7375 printf("%s%s", *path, sep);
7377 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7382 /* err == -ENODATA is end-of-loop */
7383 if (linkno > 0 && err == -ENODATA) {
7391 "%s path2fid: cannot get %sfid for '%s': %s\n",
7392 progname, show_parents ? "parent " : "", *path,
7404 static int lfs_data_version(int argc, char **argv)
7411 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7414 fprintf(stderr, "%s data_version: FILE must be specified\n",
7419 while ((c = getopt(argc, argv, "nrw")) != -1) {
7422 data_version_flags = 0;
7425 data_version_flags |= LL_DV_RD_FLUSH;
7428 data_version_flags |= LL_DV_WR_FLUSH;
7432 "%s data_version: unrecognized option '%s'\n",
7433 progname, argv[optind - 1]);
7437 if (optind == argc) {
7438 fprintf(stderr, "%s data_version: FILE must be specified\n",
7443 path = argv[optind];
7444 fd = open(path, O_RDONLY);
7447 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7448 progname, path, strerror(-rc));
7452 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7455 "%s data_version: cannot get version for '%s': %s\n",
7456 progname, path, strerror(-rc));
7458 printf("%ju" "\n", (uintmax_t)data_version);
7464 static int lfs_hsm_state(int argc, char **argv)
7469 struct hsm_user_state hus;
7477 rc = llapi_hsm_state_get(path, &hus);
7479 fprintf(stderr, "can't get hsm state for %s: %s\n",
7480 path, strerror(errno = -rc));
7484 /* Display path name and status flags */
7485 printf("%s: (0x%08x)", path, hus.hus_states);
7487 if (hus.hus_states & HS_RELEASED)
7488 printf(" released");
7489 if (hus.hus_states & HS_EXISTS)
7491 if (hus.hus_states & HS_DIRTY)
7493 if (hus.hus_states & HS_ARCHIVED)
7494 printf(" archived");
7495 /* Display user-settable flags */
7496 if (hus.hus_states & HS_NORELEASE)
7497 printf(" never_release");
7498 if (hus.hus_states & HS_NOARCHIVE)
7499 printf(" never_archive");
7500 if (hus.hus_states & HS_LOST)
7501 printf(" lost_from_hsm");
7503 if (hus.hus_archive_id != 0)
7504 printf(", archive_id:%d", hus.hus_archive_id);
7507 } while (++i < argc);
7512 #define LFS_HSM_SET 0
7513 #define LFS_HSM_CLEAR 1
7516 * Generic function to set or clear HSM flags.
7517 * Used by hsm_set and hsm_clear.
7519 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7521 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7523 struct option long_opts[] = {
7524 { .val = 'A', .name = "archived", .has_arg = no_argument },
7525 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7526 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7527 { .val = 'e', .name = "exists", .has_arg = no_argument },
7528 { .val = 'l', .name = "lost", .has_arg = no_argument },
7529 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7530 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7532 char short_opts[] = "lraAdei:";
7536 __u32 archive_id = 0;
7542 while ((c = getopt_long(argc, argv, short_opts,
7543 long_opts, NULL)) != -1) {
7549 mask |= HS_NOARCHIVE;
7552 mask |= HS_ARCHIVED;
7555 mask |= HS_NORELEASE;
7564 archive_id = strtol(optarg, &end, 10);
7566 fprintf(stderr, "invalid archive_id: '%s'\n",
7574 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7575 argv[0], argv[optind - 1]);
7580 /* User should have specified a flag */
7584 while (optind < argc) {
7586 path = argv[optind];
7588 /* If mode == 0, this means we apply the mask. */
7589 if (mode == LFS_HSM_SET)
7590 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7592 rc = llapi_hsm_state_set(path, 0, mask, 0);
7595 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7596 path, strerror(errno = -rc));
7605 static int lfs_hsm_action(int argc, char **argv)
7610 struct hsm_current_action hca;
7611 struct hsm_extent he;
7612 enum hsm_user_action hua;
7613 enum hsm_progress_states hps;
7621 rc = llapi_hsm_current_action(path, &hca);
7623 fprintf(stderr, "can't get hsm action for %s: %s\n",
7624 path, strerror(errno = -rc));
7627 he = hca.hca_location;
7628 hua = hca.hca_action;
7629 hps = hca.hca_state;
7631 printf("%s: %s", path, hsm_user_action2name(hua));
7633 /* Skip file without action */
7634 if (hca.hca_action == HUA_NONE) {
7639 printf(" %s ", hsm_progress_state2name(hps));
7641 if ((hps == HPS_RUNNING) &&
7642 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7643 printf("(%llu bytes moved)\n",
7644 (unsigned long long)he.length);
7645 else if ((he.offset + he.length) == LUSTRE_EOF)
7646 printf("(from %llu to EOF)\n",
7647 (unsigned long long)he.offset);
7649 printf("(from %llu to %llu)\n",
7650 (unsigned long long)he.offset,
7651 (unsigned long long)(he.offset + he.length));
7653 } while (++i < argc);
7658 static int lfs_hsm_set(int argc, char **argv)
7660 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7663 static int lfs_hsm_clear(int argc, char **argv)
7665 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7669 * Check file state and return its fid, to be used by lfs_hsm_request().
7671 * \param[in] file Path to file to check
7672 * \param[in,out] fid Pointer to allocated lu_fid struct.
7673 * \param[in,out] last_dev Pointer to last device id used.
7675 * \return 0 on success.
7677 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7683 rc = lstat(file, &st);
7685 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7688 /* Checking for regular file as archiving as posix copytool
7689 * rejects archiving files other than regular files
7691 if (!S_ISREG(st.st_mode)) {
7692 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7695 /* A request should be ... */
7696 if (*last_dev != st.st_dev && *last_dev != 0) {
7697 fprintf(stderr, "All files should be "
7698 "on the same filesystem: %s\n", file);
7701 *last_dev = st.st_dev;
7703 rc = llapi_path2fid(file, fid);
7705 fprintf(stderr, "Cannot read FID of %s: %s\n",
7706 file, strerror(-rc));
7712 /* Fill an HSM HUR item with a given file name.
7714 * If mntpath is set, then the filename is actually a FID, and no
7715 * lookup on the filesystem will be performed.
7717 * \param[in] hur the user request to fill
7718 * \param[in] idx index of the item inside the HUR to fill
7719 * \param[in] mntpath mountpoint of Lustre
7720 * \param[in] fname filename (if mtnpath is NULL)
7721 * or FID (if mntpath is set)
7722 * \param[in] last_dev pointer to last device id used
7724 * \retval 0 on success
7725 * \retval CMD_HELP or a negative errno on error
7727 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7728 const char *mntpath, const char *fname,
7731 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7734 hui->hui_extent.length = -1;
7736 if (mntpath != NULL) {
7739 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7743 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7748 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7752 hur->hur_request.hr_itemcount++;
7757 static int lfs_hsm_request(int argc, char **argv, int action)
7759 struct option long_opts[] = {
7760 { .val = 'a', .name = "archive", .has_arg = required_argument },
7761 { .val = 'D', .name = "data", .has_arg = required_argument },
7762 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7763 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7766 char short_opts[] = "l:D:a:m:";
7767 struct hsm_user_request *hur, *oldhur;
7772 char *filelist = NULL;
7773 char fullpath[PATH_MAX];
7774 char *opaque = NULL;
7778 int nbfile_alloc = 0;
7779 char *some_file = NULL;
7780 char *mntpath = NULL;
7786 while ((c = getopt_long(argc, argv, short_opts,
7787 long_opts, NULL)) != -1) {
7796 if (action != HUA_ARCHIVE &&
7797 action != HUA_REMOVE) {
7799 "error: -a is supported only "
7800 "when archiving or removing\n");
7803 archive_id = atoi(optarg);
7806 if (some_file == NULL) {
7808 some_file = strdup(optarg);
7814 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7815 argv[0], argv[optind - 1]);
7820 /* All remaining args are files, so we have at least nbfile */
7821 nbfile = argc - optind;
7823 if ((nbfile == 0) && (filelist == NULL))
7827 opaque_len = strlen(opaque);
7829 /* Alloc the request structure with enough place to store all files
7830 * from command line. */
7831 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7833 fprintf(stderr, "Cannot create the request: %s\n",
7837 nbfile_alloc = nbfile;
7839 hur->hur_request.hr_action = action;
7840 hur->hur_request.hr_archive_id = archive_id;
7841 hur->hur_request.hr_flags = 0;
7843 /* All remaining args are files, add them */
7844 if (nbfile != 0 && some_file == NULL)
7845 some_file = strdup(argv[optind]);
7847 for (i = 0; i < nbfile; i++) {
7848 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7854 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7856 /* If a filelist was specified, read the filelist from it. */
7857 if (filelist != NULL) {
7858 fp = fopen(filelist, "r");
7860 fprintf(stderr, "Cannot read the file list %s: %s\n",
7861 filelist, strerror(errno));
7866 while ((rc = getline(&line, &len, fp)) != -1) {
7867 /* If allocated buffer was too small, get something
7869 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7872 nbfile_alloc = nbfile_alloc * 2 + 1;
7874 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7877 fprintf(stderr, "hsm: cannot allocate "
7878 "the request: %s\n",
7885 size = hur_len(oldhur);
7887 fprintf(stderr, "hsm: cannot allocate "
7888 "%u files + %u bytes data\n",
7889 oldhur->hur_request.hr_itemcount,
7890 oldhur->hur_request.hr_data_len);
7897 memcpy(hur, oldhur, size);
7902 if (line[strlen(line) - 1] == '\n')
7903 line[strlen(line) - 1] = '\0';
7905 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7906 mntpath, line, &last_dev);
7912 if (some_file == NULL) {
7922 /* If a --data was used, add it to the request */
7923 hur->hur_request.hr_data_len = opaque_len;
7925 memcpy(hur_data(hur), opaque, opaque_len);
7927 /* Send the HSM request */
7928 if (realpath(some_file, fullpath) == NULL) {
7929 fprintf(stderr, "Could not find path '%s': %s\n",
7930 some_file, strerror(errno));
7932 rc = llapi_hsm_request(fullpath, hur);
7934 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7935 some_file, strerror(-rc));
7945 static int lfs_hsm_archive(int argc, char **argv)
7947 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7950 static int lfs_hsm_restore(int argc, char **argv)
7952 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7955 static int lfs_hsm_release(int argc, char **argv)
7957 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7960 static int lfs_hsm_remove(int argc, char **argv)
7962 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7965 static int lfs_hsm_cancel(int argc, char **argv)
7967 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7970 static int lfs_swap_layouts(int argc, char **argv)
7975 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7976 SWAP_LAYOUTS_KEEP_MTIME |
7977 SWAP_LAYOUTS_KEEP_ATIME);
7980 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7982 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7984 int lfs_get_mode(const char *string)
7986 enum lock_mode_user mode;
7988 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7989 if (lock_mode_names[mode] == NULL)
7991 if (strcmp(string, lock_mode_names[mode]) == 0)
7998 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8000 enum lu_ladvise_type advice;
8003 advice < ARRAY_SIZE(ladvise_names); advice++) {
8004 if (ladvise_names[advice] == NULL)
8006 if (strcmp(string, ladvise_names[advice]) == 0)
8010 return LU_LADVISE_INVALID;
8013 static int lfs_ladvise(int argc, char **argv)
8015 struct option long_opts[] = {
8016 { .val = 'a', .name = "advice", .has_arg = required_argument },
8017 { .val = 'b', .name = "background", .has_arg = no_argument },
8018 { .val = 'e', .name = "end", .has_arg = required_argument },
8019 { .val = 'l', .name = "length", .has_arg = required_argument },
8020 { .val = 'm', .name = "mode", .has_arg = required_argument },
8021 { .val = 's', .name = "start", .has_arg = required_argument },
8022 { .val = 'u', .name = "unset", .has_arg = no_argument },
8024 char short_opts[] = "a:be:l:m:s:u";
8029 struct llapi_lu_ladvise advice;
8030 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8031 unsigned long long start = 0;
8032 unsigned long long end = LUSTRE_EOF;
8033 unsigned long long length = 0;
8034 unsigned long long size_units;
8035 unsigned long long flags = 0;
8039 while ((c = getopt_long(argc, argv, short_opts,
8040 long_opts, NULL)) != -1) {
8043 advice_type = lfs_get_ladvice(optarg);
8044 if (advice_type == LU_LADVISE_INVALID) {
8045 fprintf(stderr, "%s: invalid advice type "
8046 "'%s'\n", argv[0], optarg);
8047 fprintf(stderr, "Valid types:");
8049 for (advice_type = 0;
8050 advice_type < ARRAY_SIZE(ladvise_names);
8052 if (ladvise_names[advice_type] == NULL)
8054 fprintf(stderr, " %s",
8055 ladvise_names[advice_type]);
8057 fprintf(stderr, "\n");
8070 rc = llapi_parse_size(optarg, &end,
8073 fprintf(stderr, "%s: bad end offset '%s'\n",
8080 rc = llapi_parse_size(optarg, &start,
8083 fprintf(stderr, "%s: bad start offset "
8084 "'%s'\n", argv[0], optarg);
8090 rc = llapi_parse_size(optarg, &length,
8093 fprintf(stderr, "%s: bad length '%s'\n",
8099 mode = lfs_get_mode(optarg);
8101 fprintf(stderr, "%s: bad mode '%s', valid "
8102 "modes are READ or WRITE\n",
8110 fprintf(stderr, "%s: option '%s' unrecognized\n",
8111 argv[0], argv[optind - 1]);
8116 if (advice_type == LU_LADVISE_INVALID) {
8117 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8118 fprintf(stderr, "Valid types:");
8119 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8121 if (ladvise_names[advice_type] == NULL)
8123 fprintf(stderr, " %s", ladvise_names[advice_type]);
8125 fprintf(stderr, "\n");
8129 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8130 fprintf(stderr, "%s: Lock no expand advice is a per file "
8131 "descriptor advice, so when called from lfs, "
8132 "it does nothing.\n", argv[0]);
8136 if (argc <= optind) {
8137 fprintf(stderr, "%s: please give one or more file names\n",
8142 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8143 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8148 if (end == LUSTRE_EOF && length != 0)
8149 end = start + length;
8152 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8153 argv[0], start, end);
8157 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8158 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8163 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8164 fprintf(stderr, "%s: mode is required with lockahead\n",
8169 while (optind < argc) {
8172 path = argv[optind++];
8174 fd = open(path, O_RDONLY);
8176 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8177 argv[0], path, strerror(errno));
8182 advice.lla_start = start;
8183 advice.lla_end = end;
8184 advice.lla_advice = advice_type;
8185 advice.lla_value1 = 0;
8186 advice.lla_value2 = 0;
8187 advice.lla_value3 = 0;
8188 advice.lla_value4 = 0;
8189 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8190 advice.lla_lockahead_mode = mode;
8191 advice.lla_peradvice_flags = flags;
8194 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8197 fprintf(stderr, "%s: cannot give advice '%s' to file "
8198 "'%s': %s\n", argv[0],
8199 ladvise_names[advice_type],
8200 path, strerror(errno));
8206 if (rc == 0 && rc2 < 0)
8212 static const char *const heat_names[] = LU_HEAT_NAMES;
8214 static int lfs_heat_get(int argc, char **argv)
8216 struct lu_heat *heat;
8225 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8227 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8232 while (optind < argc) {
8233 path = argv[optind++];
8235 fd = open(path, O_RDONLY);
8237 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8238 argv[0], path, strerror(errno));
8243 heat->lh_count = OBD_HEAT_COUNT;
8244 rc2 = llapi_heat_get(fd, heat);
8247 fprintf(stderr, "%s: cannot get heat of file '%s'"
8248 ": %s\n", argv[0], path, strerror(errno));
8252 printf("flags: %x\n", heat->lh_flags);
8253 for (i = 0; i < heat->lh_count; i++)
8254 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8256 if (rc == 0 && rc2 < 0)
8264 static int lfs_heat_set(int argc, char **argv)
8266 struct option long_opts[] = {
8267 { .val = 'c', .name = "clear", .has_arg = no_argument },
8268 { .val = 'o', .name = "off", .has_arg = no_argument },
8269 { .val = 'O', .name = "on", .has_arg = no_argument },
8271 enum lu_heat_flag flags = 0;
8281 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8284 flags |= LU_HEAT_FLAG_CLEAR;
8287 flags |= LU_HEAT_FLAG_CLEAR;
8288 flags |= LU_HEAT_FLAG_OFF;
8291 flags &= ~LU_HEAT_FLAG_OFF;
8296 fprintf(stderr, "%s: option '%s' unrecognized\n",
8297 argv[0], argv[optind - 1]);
8302 if (argc <= optind) {
8303 fprintf(stderr, "%s: please give one or more file names\n",
8308 while (optind < argc) {
8309 path = argv[optind++];
8311 fd = open(path, O_RDONLY);
8313 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8314 argv[0], path, strerror(errno));
8319 rc2 = llapi_heat_set(fd, flags);
8322 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8323 ": %s\n", argv[0], path, strerror(errno));
8327 if (rc == 0 && rc2 < 0)
8333 /** The input string contains a comma delimited list of component ids and
8334 * ranges, for example "1,2-4,7".
8336 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8338 bool end_of_loop = false;
8346 while (!end_of_loop) {
8350 char *endptr = NULL;
8353 ptr = strchrnul(arg, ',');
8354 end_of_loop = *ptr == '\0';
8357 start_index = strtol(arg, &endptr, 0);
8358 if (endptr == arg) /* no data at all */
8360 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8362 if (start_index < 0)
8365 end_index = start_index;
8366 if (*endptr == '-') {
8367 end_index = strtol(endptr + 1, &endptr, 0);
8368 if (*endptr != '\0')
8370 if (end_index < start_index)
8374 for (i = start_index; i <= end_index && size > 0; i++) {
8377 /* remove duplicate */
8378 for (j = 0; j < nr; j++) {
8382 if (j == nr) { /* no duplicate */
8388 if (size == 0 && i < end_index)
8395 if (!end_of_loop && ptr != NULL)
8398 return rc < 0 ? rc : nr;
8402 * struct verify_mirror_id - Mirror id to be verified.
8403 * @mirror_id: A specified mirror id.
8404 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8406 struct verify_mirror_id {
8412 * compare_mirror_ids() - Compare mirror ids.
8413 * @layout: Mirror component list.
8414 * @cbdata: Callback data in verify_mirror_id structure.
8416 * This is a callback function called by llapi_layout_comp_iterate()
8417 * to compare the specified mirror id with the one in the current
8418 * component of @layout. If they are the same, then the specified
8419 * mirror id is valid.
8421 * Return: a negative error code on failure or
8422 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8423 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8426 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8428 struct verify_mirror_id *mirror_id_cbdata =
8429 (struct verify_mirror_id *)cbdata;
8433 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8437 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8438 progname, strerror(errno));
8442 if (mirror_id_cbdata->mirror_id == mirror_id) {
8443 mirror_id_cbdata->is_valid_id = true;
8444 return LLAPI_LAYOUT_ITER_STOP;
8447 return LLAPI_LAYOUT_ITER_CONT;
8451 * verify_mirror_ids() - Verify specified mirror ids.
8452 * @fname: Mirrored file name.
8453 * @mirror_ids: Specified mirror ids to be verified.
8454 * @ids_nr: Number of specified mirror ids.
8456 * This function verifies that specified @mirror_ids are valid
8457 * in the mirrored file @fname.
8459 * Return: 0 on success or a negative error code on failure.
8462 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8464 struct llapi_layout *layout = NULL;
8465 struct verify_mirror_id mirror_id_cbdata = { 0 };
8476 if (stat(fname, &stbuf) < 0) {
8477 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8478 progname, fname, strerror(errno));
8483 if (!S_ISREG(stbuf.st_mode)) {
8484 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8490 fd = open(fname, O_DIRECT | O_RDONLY);
8492 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8493 progname, fname, strerror(errno));
8498 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8500 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8501 progname, fname, strerror(errno));
8505 layout = llapi_layout_get_by_fd(fd, 0);
8506 if (layout == NULL) {
8507 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8508 progname, fname, strerror(errno));
8510 llapi_lease_release(fd);
8514 rc = llapi_layout_flags_get(layout, &flr_state);
8516 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8517 progname, fname, strerror(errno));
8522 flr_state &= LCM_FL_FLR_MASK;
8523 switch (flr_state) {
8526 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8527 progname, fname, llapi_layout_flags_string(flr_state));
8534 for (i = 0; i < ids_nr; i++) {
8535 mirror_id_cbdata.mirror_id = mirror_ids[i];
8536 mirror_id_cbdata.is_valid_id = false;
8538 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8543 "%s: '%s' failed to verify mirror id: %u.\n",
8544 progname, fname, mirror_ids[i]);
8548 if (!mirror_id_cbdata.is_valid_id) {
8551 "%s: '%s' invalid specified mirror id: %u.\n",
8552 progname, fname, mirror_ids[i]);
8558 llapi_layout_free(layout);
8559 llapi_lease_release(fd);
8567 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8568 __u16 *mirror_ids, int ids_nr)
8570 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8571 struct llapi_layout *layout;
8581 if (stat(fname, &stbuf) < 0) {
8582 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8583 progname, fname, strerror(errno));
8587 if (!S_ISREG(stbuf.st_mode)) {
8588 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8594 fd = open(fname, O_DIRECT | O_RDWR);
8596 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8597 progname, fname, strerror(errno));
8602 layout = llapi_layout_get_by_fd(fd, 0);
8603 if (layout == NULL) {
8604 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8605 progname, fname, strerror(errno));
8610 rc = llapi_layout_flags_get(layout, &flr_state);
8612 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8613 progname, fname, strerror(errno));
8618 flr_state &= LCM_FL_FLR_MASK;
8619 if (flr_state == LCM_FL_NONE) {
8621 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8626 /* get stale component info */
8627 comp_size = llapi_mirror_find_stale(layout, comp_array,
8628 ARRAY_SIZE(comp_array),
8629 mirror_ids, ids_nr);
8630 if (comp_size <= 0) {
8635 ioc->lil_mode = LL_LEASE_WRLCK;
8636 ioc->lil_flags = LL_LEASE_RESYNC;
8637 rc = llapi_lease_set(fd, ioc);
8639 if (rc == -EALREADY)
8643 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8644 progname, fname, strerror(errno));
8648 /* get the read range [start, end) */
8649 start = comp_array[0].lrc_start;
8650 end = comp_array[0].lrc_end;
8651 for (idx = 1; idx < comp_size; idx++) {
8652 if (comp_array[idx].lrc_start < start)
8653 start = comp_array[idx].lrc_start;
8654 if (end < comp_array[idx].lrc_end)
8655 end = comp_array[idx].lrc_end;
8658 rc = llapi_lease_check(fd);
8659 if (rc != LL_LEASE_WRLCK) {
8660 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8665 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8668 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8669 progname, fname, rc);
8671 /* prepare ioc for lease put */
8672 ioc->lil_mode = LL_LEASE_UNLCK;
8673 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8675 for (idx = 0; idx < comp_size; idx++) {
8676 if (comp_array[idx].lrc_synced) {
8677 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8682 rc = llapi_lease_set(fd, ioc);
8684 if (rc == 0) /* lost lease lock */
8686 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8687 progname, fname, strerror(errno));
8691 * llapi_lease_set returns lease mode when it request to unlock
8697 llapi_layout_free(layout);
8704 static inline int lfs_mirror_resync(int argc, char **argv)
8706 struct ll_ioc_lease *ioc = NULL;
8707 __u16 mirror_ids[128] = { 0 };
8712 struct option long_opts[] = {
8713 { .val = 'o', .name = "only", .has_arg = required_argument },
8716 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8719 rc = parse_mirror_ids(mirror_ids,
8720 sizeof(mirror_ids) / sizeof(__u16),
8724 "%s: bad mirror ids '%s'.\n",
8731 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8732 argv[0], argv[optind - 1]);
8738 if (argc == optind) {
8739 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8744 if (ids_nr > 0 && argc > optind + 1) {
8746 "%s: option '--only' cannot be used upon multiple files.\n",
8754 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8759 /* set the lease on the file */
8760 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8762 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8763 argv[0], strerror(errno));
8768 for (; optind < argc; optind++) {
8769 rc = lfs_mirror_resync_file(argv[optind], ioc,
8770 mirror_ids, ids_nr);
8771 /* ignore previous file's error, continue with next file */
8774 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8782 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8784 struct llapi_layout *layout;
8787 layout = llapi_layout_get_by_fd(fd, 0);
8788 if (layout == NULL) {
8789 fprintf(stderr, "could not get layout.\n");
8793 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8795 fprintf(stderr, "failed to iterate layout\n");
8796 llapi_layout_free(layout);
8799 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8800 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8801 llapi_layout_free(layout);
8805 llapi_layout_free(layout);
8811 * Check whether two files are the same file
8812 * \retval 0 same file
8813 * \retval 1 not the same file
8814 * \retval <0 error code
8816 static inline int check_same_file(const char *f1, const char *f2)
8821 if (stat(f1, &stbuf1) < 0) {
8822 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8823 progname, f1, strerror(errno));
8827 if (stat(f2, &stbuf2) < 0) {
8828 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8829 progname, f2, strerror(errno));
8833 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8834 stbuf1.st_ino == stbuf2.st_ino)
8840 static inline int lfs_mirror_read(int argc, char **argv)
8843 __u16 mirror_id = 0;
8844 const char *outfile = NULL;
8850 const size_t buflen = 4 << 20;
8852 struct option long_opts[] = {
8853 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8854 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8857 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8862 mirror_id = strtoul(optarg, &end, 0);
8863 if (*end != '\0' || mirror_id == 0) {
8865 "%s %s: invalid mirror ID '%s'\n",
8866 progname, argv[0], optarg);
8874 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8875 progname, argv[optind - 1]);
8880 if (argc == optind) {
8881 fprintf(stderr, "%s %s: no mirrored file provided\n",
8884 } else if (argc > optind + 1) {
8885 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8889 if (mirror_id == 0) {
8890 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8895 /* open mirror file */
8896 fname = argv[optind];
8899 rc = check_same_file(fname, outfile);
8902 "%s %s: output file cannot be the mirrored file\n",
8910 fd = open(fname, O_DIRECT | O_RDONLY);
8912 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8913 progname, argv[0], fname, strerror(errno));
8917 /* verify mirror id */
8918 rc = verify_mirror_id_by_fd(fd, mirror_id);
8921 "%s %s: cannot find mirror with ID %u in '%s'\n",
8922 progname, argv[0], mirror_id, fname);
8926 /* open output file */
8928 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8930 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8931 progname, argv[0], outfile, strerror(errno));
8936 outfd = STDOUT_FILENO;
8939 /* allocate buffer */
8940 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8942 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8943 progname, argv[0], rc);
8950 ssize_t written = 0;
8952 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8953 if (bytes_read < 0) {
8956 "%s %s: fail to read data from mirror %u: %s\n",
8957 progname, argv[0], mirror_id, strerror(-rc));
8962 if (bytes_read == 0)
8965 while (written < bytes_read) {
8968 written2 = write(outfd, buf + written,
8969 bytes_read - written);
8972 "%s %s: fail to write %s: %s\n",
8973 progname, argv[0], outfile ? : "STDOUT",
8978 written += written2;
8981 if (written != bytes_read) {
8983 "%s %s: written %ld bytes does not match with %ld read.\n",
8984 progname, argv[0], written, bytes_read);
9006 static inline int lfs_mirror_write(int argc, char **argv)
9009 __u16 mirror_id = 0;
9010 const char *inputfile = NULL;
9016 const size_t buflen = 4 << 20;
9018 size_t page_size = sysconf(_SC_PAGESIZE);
9019 struct ll_ioc_lease_id ioc;
9021 struct option long_opts[] = {
9022 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9023 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9026 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9031 mirror_id = strtoul(optarg, &end, 0);
9032 if (*end != '\0' || mirror_id == 0) {
9034 "%s %s: invalid mirror ID '%s'\n",
9035 progname, argv[0], optarg);
9043 fprintf(stderr, "%s: option '%s' unrecognized\n",
9044 progname, argv[optind - 1]);
9049 if (argc == optind) {
9050 fprintf(stderr, "%s %s: no mirrored file provided\n",
9053 } else if (argc > optind + 1) {
9054 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9058 if (mirror_id == 0) {
9059 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9064 /* open mirror file */
9065 fname = argv[optind];
9068 rc = check_same_file(fname, inputfile);
9071 "%s %s: input file cannot be the mirrored file\n",
9079 fd = open(fname, O_DIRECT | O_WRONLY);
9081 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9082 progname, argv[0], fname, strerror(errno));
9086 /* verify mirror id */
9087 rc = verify_mirror_id_by_fd(fd, mirror_id);
9090 "%s %s: cannot find mirror with ID %u in '%s'\n",
9091 progname, argv[0], mirror_id, fname);
9095 /* open input file */
9097 inputfd = open(inputfile, O_RDONLY, 0644);
9099 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9100 progname, argv[0], inputfile, strerror(errno));
9105 inputfd = STDIN_FILENO;
9108 /* allocate buffer */
9109 rc = posix_memalign(&buf, page_size, buflen);
9111 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9112 progname, argv[0], rc);
9116 /* prepare target mirror components instantiation */
9117 ioc.lil_mode = LL_LEASE_WRLCK;
9118 ioc.lil_flags = LL_LEASE_RESYNC;
9119 ioc.lil_mirror_id = mirror_id;
9120 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9123 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9124 progname, argv[0], fname, strerror(errno));
9134 rc = llapi_lease_check(fd);
9135 if (rc != LL_LEASE_WRLCK) {
9136 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9137 progname, argv[0], fname);
9141 bytes_read = read(inputfd, buf, buflen);
9142 if (bytes_read < 0) {
9145 "%s %s: fail to read data from '%s': %s\n",
9146 progname, argv[0], inputfile ? : "STDIN",
9153 if (bytes_read == 0)
9156 /* round up to page align to make direct IO happy. */
9157 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9159 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9164 "%s %s: fail to write to mirror %u: %s\n",
9165 progname, argv[0], mirror_id,
9173 if (pos & (page_size - 1)) {
9174 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9179 ioc.lil_mode = LL_LEASE_UNLCK;
9180 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9182 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9187 "%s %s: release lease lock of '%s' failed: %s\n",
9188 progname, argv[0], fname, strerror(errno));
9205 struct collect_ids_data {
9211 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9213 struct collect_ids_data *cid = cbdata;
9217 rc = llapi_layout_mirror_id_get(layout, &id);
9221 if ((__u16)id != cid->cid_exclude) {
9224 for (i = 0; i < cid->cid_count; i++) {
9225 /* already collected the mirror id */
9226 if (id == cid->cid_ids[i])
9227 return LLAPI_LAYOUT_ITER_CONT;
9229 cid->cid_ids[cid->cid_count] = id;
9233 return LLAPI_LAYOUT_ITER_CONT;
9236 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9238 struct llapi_layout *layout;
9239 struct collect_ids_data cid = { .cid_ids = ids,
9241 .cid_exclude = exclude_id, };
9244 layout = llapi_layout_get_by_fd(fd, 0);
9245 if (layout == NULL) {
9246 fprintf(stderr, "could not get layout\n");
9250 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9252 fprintf(stderr, "failed to iterate layout\n");
9253 llapi_layout_free(layout);
9257 llapi_layout_free(layout);
9259 return cid.cid_count;
9262 static inline int lfs_mirror_copy(int argc, char **argv)
9265 __u16 read_mirror_id = 0;
9266 __u16 ids[128] = { 0 };
9268 struct llapi_layout *layout = NULL;
9269 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9276 struct ll_ioc_lease *ioc = NULL;
9277 struct ll_ioc_lease_id *resync_ioc;
9279 struct option long_opts[] = {
9280 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9281 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9284 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9289 read_mirror_id = strtoul(optarg, &end, 0);
9290 if (*end != '\0' || read_mirror_id == 0) {
9292 "%s %s: invalid read mirror ID '%s'\n",
9293 progname, argv[0], optarg);
9298 if (!strcmp(optarg, "-1")) {
9299 /* specify all other mirrors */
9303 count = parse_mirror_ids((__u16 *)ids,
9311 fprintf(stderr, "%s: option '%s' unrecognized\n",
9312 progname, argv[optind - 1]);
9317 if (argc == optind) {
9318 fprintf(stderr, "%s %s: no mirrored file provided\n",
9321 } else if (argc > optind + 1) {
9322 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9326 if (read_mirror_id == 0) {
9328 "%s %s: no valid read mirror ID %d is provided\n",
9329 progname, argv[0], read_mirror_id);
9335 "%s %s: no write mirror ID is provided\n",
9340 for (i = 0; i < count; i++) {
9341 if (read_mirror_id == ids[i]) {
9343 "%s %s: read and write mirror ID cannot be the same\n",
9349 /* open mirror file */
9350 fname = argv[optind];
9352 fd = open(fname, O_DIRECT | O_RDWR);
9354 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9355 progname, argv[0], fname, strerror(errno));
9359 /* write to all other mirrors */
9360 if (ids[0] == (__u16)-1) {
9361 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9365 "%s %s: failed to get other mirror ids in '%s': %d\n",
9366 progname, argv[0], fname, rc);
9371 /* verify mirror id */
9372 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9375 "%s %s: cannot find mirror with ID %u in '%s'\n",
9376 progname, argv[0], read_mirror_id, fname);
9380 for (i = 0; i < count; i++) {
9381 rc = verify_mirror_id_by_fd(fd, ids[i]);
9384 "%s %s: cannot find mirror with ID %u in '%s'\n",
9385 progname, argv[0], ids[i], fname);
9390 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9393 "%s %s: cannot alloc comp id array for ioc: %s\n",
9394 progname, argv[0], strerror(errno));
9399 /* get stale component info */
9400 layout = llapi_layout_get_by_fd(fd, 0);
9401 if (layout == NULL) {
9402 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9403 progname, argv[0], fname, strerror(errno));
9407 comp_size = llapi_mirror_find_stale(layout, comp_array,
9408 ARRAY_SIZE(comp_array),
9410 llapi_layout_free(layout);
9411 if (comp_size < 0) {
9416 /* prepare target mirror components instantiation */
9417 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9418 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9419 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9421 resync_ioc->lil_mirror_id = ids[0];
9423 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9424 rc = llapi_lease_set(fd, ioc);
9427 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9428 progname, argv[0], fname, strerror(errno));
9432 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9435 fprintf(stderr, "%s %s: copy error: %d\n",
9436 progname, argv[0], rc);
9440 fprintf(stdout, "mirror copied successfully: ");
9441 for (i = 0; i < copied; i++)
9442 fprintf(stdout, "%d ", ids[i]);
9443 fprintf(stdout, "\n");
9445 ioc->lil_mode = LL_LEASE_UNLCK;
9446 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9448 for (i = 0; i < comp_size; i++) {
9451 for (j = 0; j < copied; j++) {
9452 if (comp_array[i].lrc_mirror_id != ids[j])
9455 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9459 rc = llapi_lease_set(fd, ioc);
9464 "%s %s: release lease lock of '%s' failed: %s\n",
9465 progname, argv[0], fname, strerror(errno));
9479 * struct verify_chunk - Mirror chunk to be verified.
9480 * @chunk: [start, end) of the chunk.
9481 * @mirror_count: Number of mirror ids in @mirror_id array.
9482 * @mirror_id: Array of valid mirror ids that cover the chunk.
9484 struct verify_chunk {
9485 struct lu_extent chunk;
9486 unsigned int mirror_count;
9487 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9491 * print_chunks() - Print chunk information.
9492 * @fname: Mirrored file name.
9493 * @chunks: Array of chunks.
9494 * @chunk_count: Number of chunks in @chunks array.
9496 * This function prints [start, end) of each chunk in @chunks
9497 * for mirrored file @fname, and also prints the valid mirror ids
9498 * that cover the chunk.
9503 void print_chunks(const char *fname, struct verify_chunk *chunks,
9509 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9510 for (i = 0; i < chunk_count; i++) {
9511 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9513 if (chunks[i].mirror_count == 0)
9514 fprintf(stdout, "\t[");
9516 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9517 for (j = 1; j < chunks[i].mirror_count; j++)
9518 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9520 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9522 fprintf(stdout, "\n");
9526 * print_checksums() - Print CRC-32 checksum values.
9527 * @chunk: A chunk and its corresponding valid mirror ids.
9528 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9530 * This function prints CRC-32 checksum values on @chunk for
9531 * each valid mirror that covers it.
9536 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9541 "CRC-32 checksum value for chunk "DEXT":\n",
9542 PEXT(&chunk->chunk));
9543 for (i = 0; i < chunk->mirror_count; i++)
9544 fprintf(stdout, "Mirror %u:\t%#lx\n",
9545 chunk->mirror_id[i], crc[i]);
9546 fprintf(stdout, "\n");
9550 * filter_mirror_id() - Filter specified mirror ids.
9551 * @chunks: Array of chunks.
9552 * @chunk_count: Number of chunks in @chunks array.
9553 * @mirror_ids: Specified mirror ids to be verified.
9554 * @ids_nr: Number of specified mirror ids.
9556 * This function scans valid mirror ids that cover each chunk in @chunks
9557 * and filters specified mirror ids.
9562 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9563 __u16 *mirror_ids, int ids_nr)
9568 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9569 unsigned int valid_count = 0;
9571 for (i = 0; i < chunk_count; i++) {
9572 if (chunks[i].mirror_count == 0)
9576 for (j = 0; j < ids_nr; j++) {
9577 for (k = 0; k < chunks[i].mirror_count; k++) {
9578 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9579 valid_id[valid_count] = mirror_ids[j];
9586 memcpy(chunks[i].mirror_id, valid_id,
9587 sizeof(__u16) * valid_count);
9588 chunks[i].mirror_count = valid_count;
9593 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9594 * @layout: Mirror component list.
9595 * @chunks: Array of chunks.
9596 * @chunks_size: Array size of @chunks.
9598 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9599 * to find out chunk segments and store them in @chunks array.
9601 * The @mirror_id array in each element of @chunks will store the valid
9602 * mirror ids that cover the chunk. If a mirror component covering the
9603 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9604 * will not be stored into the @mirror_id array, and the chunk for that
9605 * mirror will not be verified.
9607 * The @mirror_count in each element of @chunks will store the number of
9608 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9609 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9610 * indicates the chunk is valid in only one mirror. In both cases, the
9611 * chunk will not be verified.
9613 * Here is an example:
9616 * +------+-------------+--------------------+
9618 * +------+------+------+------+-------------+
9619 * | | S | S | | mirror2
9620 * +-------------+------+------+-------------+
9622 * prepared @chunks array will contain 5 elements:
9623 * (([0, 1M), [1, 2], 2),
9624 * ([1M, 2M), [1, 2], 2),
9625 * ([2M, 3M), [1], 1),
9626 * ([3M, 4M], [], 0),
9627 * ([4M, EOF), [2], 1))
9629 * Return: the actual array size of @chunks on success
9630 * or a negative error code on failure.
9633 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9634 struct verify_chunk *chunks,
9645 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9648 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9651 "%s: move to the first layout component: %s.\n",
9652 progname, strerror(errno));
9658 chunks[idx].chunk.e_end = LUSTRE_EOF;
9660 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9663 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9664 progname, strerror(errno));
9668 if (start > chunks[idx].chunk.e_start ||
9669 end <= chunks[idx].chunk.e_start)
9672 if (end < chunks[idx].chunk.e_end)
9673 chunks[idx].chunk.e_end = end;
9675 rc = llapi_layout_comp_flags_get(layout, &flags);
9678 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9679 progname, strerror(errno));
9683 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9686 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9689 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9690 progname, strerror(errno));
9694 chunks[idx].mirror_id[i] = mirror_id;
9696 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9698 "%s: mirror_id array is too small.\n",
9705 rc = llapi_layout_comp_use(layout,
9706 LLAPI_LAYOUT_COMP_USE_NEXT);
9709 "%s: move to the next layout component: %s.\n",
9710 progname, strerror(errno));
9713 } /* loop through all components */
9715 chunks[idx].mirror_count = i;
9717 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9721 if (idx >= chunks_size) {
9722 fprintf(stderr, "%s: chunks array is too small.\n",
9728 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9732 return rc < 0 ? rc : idx + 1;
9736 * lfs_mirror_verify_chunk() - Verify a chunk.
9737 * @fd: File descriptor of the mirrored file.
9738 * @file_size: Size of the mirrored file.
9739 * @chunk: A chunk and its corresponding valid mirror ids.
9740 * @verbose: Verbose mode.
9742 * This function verifies a @chunk contains exactly the same data
9743 * ammong the mirrors that cover it.
9745 * If @verbose is specified, then the function will print where the
9746 * differences are if the data do not match. Otherwise, it will
9747 * just return an error in that case.
9749 * Return: 0 on success or a negative error code on failure.
9752 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9753 struct verify_chunk *chunk, int verbose)
9755 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9757 size_t page_size = sysconf(_SC_PAGESIZE);
9763 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9770 rc = posix_memalign(&buf, page_size, buflen);
9771 if (rc) /* error code is returned directly */
9775 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9776 PEXT(&chunk->chunk));
9777 for (i = 0; i < chunk->mirror_count; i++)
9778 fprintf(stdout, " %u", chunk->mirror_id[i]);
9779 fprintf(stdout, "\n");
9783 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9784 pos = chunk->chunk.e_start;
9785 while (bytes_done < count) {
9786 /* compute initial CRC-32 checksum */
9787 crc = crc32(0L, Z_NULL, 0);
9788 memset(crc_array, 0, sizeof(crc_array));
9791 for (i = 0; i < chunk->mirror_count; i++) {
9792 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9794 if (bytes_read < 0) {
9797 "%s: failed to read data from mirror %u: %s.\n",
9798 progname, chunk->mirror_id[i],
9803 /* compute new CRC-32 checksum */
9804 crc_array[i] = crc32(crc, buf, bytes_read);
9808 print_checksums(chunk, crc_array);
9810 /* compare CRC-32 checksum values */
9811 for (i = 1; i < chunk->mirror_count; i++) {
9812 if (crc_array[i] != crc_array[0]) {
9816 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9817 progname, PEXT(&chunk->chunk),
9818 chunk->mirror_id[0],
9819 chunk->mirror_id[i]);
9824 bytes_done += bytes_read;
9827 if (verbose > 1 && rc == 0) {
9828 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9829 PEXT(&chunk->chunk));
9830 for (i = 0; i < chunk->mirror_count; i++)
9831 fprintf(stdout, " %u", chunk->mirror_id[i]);
9832 fprintf(stdout, " PASS\n\n");
9841 * lfs_mirror_verify_file() - Verify a mirrored file.
9842 * @fname: Mirrored file name.
9843 * @mirror_ids: Specified mirror ids to be verified.
9844 * @ids_nr: Number of specified mirror ids.
9845 * @verbose: Verbose mode.
9847 * This function verifies that each SYNC mirror of a mirrored file
9848 * specified by @fname contains exactly the same data.
9850 * If @mirror_ids is specified, then the function will verify the
9851 * mirrors specified by @mirror_ids contain exactly the same data.
9853 * If @verbose is specified, then the function will print where the
9854 * differences are if the data do not match. Otherwise, it will
9855 * just return an error in that case.
9857 * Return: 0 on success or a negative error code on failure.
9860 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9863 struct verify_chunk chunks_array[1024] = { };
9864 struct llapi_layout *layout = NULL;
9868 int chunk_count = 0;
9874 if (stat(fname, &stbuf) < 0) {
9875 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9876 progname, fname, strerror(errno));
9881 if (!S_ISREG(stbuf.st_mode)) {
9882 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9888 if (stbuf.st_size == 0) {
9890 fprintf(stdout, "%s: '%s' file size is 0.\n",
9896 fd = open(fname, O_DIRECT | O_RDONLY);
9898 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9899 progname, fname, strerror(errno));
9904 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9906 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9907 progname, fname, strerror(errno));
9911 layout = llapi_layout_get_by_fd(fd, 0);
9912 if (layout == NULL) {
9913 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9914 progname, fname, strerror(errno));
9916 llapi_lease_release(fd);
9920 rc = llapi_layout_flags_get(layout, &flr_state);
9922 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9923 progname, fname, strerror(errno));
9928 flr_state &= LCM_FL_FLR_MASK;
9929 switch (flr_state) {
9932 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9933 progname, fname, llapi_layout_flags_string(flr_state));
9939 /* find out mirror chunks to be verified */
9940 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9941 ARRAY_SIZE(chunks_array));
9942 if (chunk_count < 0) {
9948 /* filter specified mirror ids */
9949 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9952 print_chunks(fname, chunks_array, chunk_count);
9954 for (idx = 0; idx < chunk_count; idx++) {
9955 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9958 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9960 PEXT(&chunks_array[idx].chunk),
9961 (unsigned long long)stbuf.st_size);
9965 if (chunks_array[idx].mirror_count == 0) {
9967 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9969 PEXT(&chunks_array[idx].chunk));
9971 fprintf(stderr, "skipped\n");
9975 fprintf(stderr, "failed\n");
9979 if (chunks_array[idx].mirror_count == 1) {
9982 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9984 PEXT(&chunks_array[idx].chunk),
9985 chunks_array[idx].mirror_id[0]);
9989 rc = llapi_lease_check(fd);
9990 if (rc != LL_LEASE_RDLCK) {
9991 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9996 /* verify one chunk */
9997 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9998 &chunks_array[idx], verbose);
10012 llapi_layout_free(layout);
10013 llapi_lease_release(fd);
10021 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10022 * @argc: The count of lfs mirror verify command line arguments.
10023 * @argv: Array of strings for lfs mirror verify command line arguments.
10025 * This function parses lfs mirror verify command and verifies the
10026 * specified mirrored file(s).
10028 * Return: 0 on success or a negative error code on failure.
10030 static inline int lfs_mirror_verify(int argc, char **argv)
10032 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10038 char cmd[PATH_MAX];
10040 struct option long_opts[] = {
10041 { .val = 'o', .name = "only", .has_arg = required_argument },
10042 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10043 { .name = NULL } };
10045 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10047 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10050 rc = parse_mirror_ids(mirror_ids,
10051 ARRAY_SIZE(mirror_ids),
10055 "%s: bad mirror ids '%s'.\n",
10062 "%s: at least 2 mirror ids needed with '--only' option.\n",
10072 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10073 progname, argv[optind - 1]);
10079 if (argc == optind) {
10080 fprintf(stderr, "%s: no file name given.\n", progname);
10085 if (ids_nr > 0 && argc > optind + 1) {
10087 "%s: '--only' cannot be used upon multiple files.\n",
10095 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10101 for (; optind < argc; optind++) {
10102 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10112 * lfs_mirror() - Parse and execute lfs mirror commands.
10113 * @argc: The count of lfs mirror command line arguments.
10114 * @argv: Array of strings for lfs mirror command line arguments.
10116 * This function parses lfs mirror commands and performs the
10117 * corresponding functions specified in mirror_cmdlist[].
10119 * Return: 0 on success or an error code on failure.
10121 static int lfs_mirror(int argc, char **argv)
10123 char cmd[PATH_MAX];
10126 setlinebuf(stdout);
10128 Parser_init("lfs-mirror > ", mirror_cmdlist);
10130 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10132 program_invocation_short_name = cmd;
10134 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10136 rc = Parser_commands();
10138 return rc < 0 ? -rc : rc;
10141 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10143 #if __BYTE_ORDER == __BIG_ENDIAN
10144 __swab16s(&attrs->lsa_valid);
10145 __swab64s(&attrs->lsa_size);
10146 __swab64s(&attrs->lsa_blocks);
10150 enum lfs_som_type {
10151 LFS_SOM_SIZE = 0x1,
10152 LFS_SOM_BLOCKS = 0x2,
10153 LFS_SOM_FLAGS = 0x4,
10154 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10158 static int lfs_getsom(int argc, char **argv)
10161 struct lustre_som_attrs *attrs;
10162 char buf[sizeof(*attrs) + 64];
10163 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10166 while ((c = getopt(argc, argv, "sbf")) != -1) {
10169 type = LFS_SOM_SIZE;
10172 type = LFS_SOM_BLOCKS;
10175 type = LFS_SOM_FLAGS;
10178 fprintf(stderr, "%s: invalid option '%c'\n",
10188 fprintf(stderr, "%s: %s\n",
10189 progname, argc == 0 ? "miss file target" :
10190 "input more than 2 files");
10195 attrs = (void *)buf;
10196 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10199 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10200 argv[0], strerror(errno), errno);
10204 lustre_som_swab(attrs);
10207 case LFS_SOM_ATTR_ALL:
10208 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10209 path, attrs->lsa_size, attrs->lsa_blocks,
10213 printf("%llu\n", attrs->lsa_size);
10215 case LFS_SOM_BLOCKS:
10216 printf("%llu\n", attrs->lsa_blocks);
10218 case LFS_SOM_FLAGS:
10219 printf("%x\n", attrs->lsa_valid);
10222 fprintf(stderr, "%s: unknown option\n", progname);
10230 * lfs_mirror_list_commands() - List lfs mirror commands.
10231 * @argc: The count of command line arguments.
10232 * @argv: Array of strings for command line arguments.
10234 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10236 * Return: 0 on success.
10238 static int lfs_mirror_list_commands(int argc, char **argv)
10240 char buffer[81] = "";
10242 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10248 static int lfs_list_commands(int argc, char **argv)
10250 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10252 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10257 int main(int argc, char **argv)
10261 /* Ensure that liblustreapi constructor has run */
10262 if (!llapi_liblustreapi_initialized())
10263 fprintf(stderr, "liblustreapi was not properly initialized\n");
10265 setlinebuf(stdout);
10268 Parser_init("lfs > ", cmdlist);
10270 progname = program_invocation_short_name; /* Used in error messages */
10272 llapi_set_command_name(argv[1]);
10273 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10274 llapi_clear_command_name();
10276 rc = Parser_commands();
10279 return rc < 0 ? -rc : rc;
10282 #ifdef _LUSTRE_IDL_H_
10283 /* Everything we need here should be included by lustreapi.h. */
10284 # error "lfs should not depend on lustre_idl.h"
10285 #endif /* _LUSTRE_IDL_H_ */