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_files = 0;
4967 __u64 ost_ffree = 0;
4975 poolname = strchr(pool, '.');
4976 if (poolname != NULL) {
4977 if (strncmp(fsname, pool, strlen(fsname))) {
4978 fprintf(stderr, "filesystem name incorrect\n");
4986 fd = open(mntdir, O_RDONLY);
4989 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4994 if (flags & MNTDF_SHOW) {
4995 if (flags & MNTDF_INODES)
4996 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4997 "UUID", "Inodes", "IUsed", "IFree",
4998 "IUse%", "Mounted on");
5000 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5002 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5003 "Used", "Available", "Use%", "Mounted on");
5006 for (tp = types; tp->st_name != NULL; tp++) {
5007 bool have_ost = false;
5009 if (!(tp->st_op & ops))
5012 for (index = 0; ; index++) {
5013 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5014 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5015 type = flags & MNTDF_LAZY ?
5016 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5017 rc2 = llapi_obd_fstatfs(fd, type, index,
5018 &stat_buf, &uuid_buf);
5023 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5024 if (!(flags & MNTDF_VERBOSE))
5026 } else if (rc2 < 0 && rc == 0) {
5030 /* If we have OSTs then don't report MDT block counts.
5031 * For MDT-only filesystems the expectation is that all
5032 * layouts have a DoM component. For filesystems with
5033 * OSTs, files are not necessarily going to store data
5034 * on MDTs, and MDT space is limited to a fraction of
5035 * OST space, so don't include it in the summary.
5037 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5044 if (poolname && tp->st_op == LL_STATFS_LOV &&
5045 llapi_search_ost(fsname, poolname,
5046 obd_uuid2str(&uuid_buf)) != 1)
5049 /* the llapi_obd_statfs() call may have returned with
5050 * an error, but if it filled in uuid_buf we will at
5051 * lease use that to print out a message for that OBD.
5052 * If we didn't get anything in the uuid_buf, then fill
5053 * it in so that we can print an error message. */
5054 if (uuid_buf.uuid[0] == '\0')
5055 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5056 "%s%04x", tp->st_name, index);
5058 lsb->sb_buf[lsb->sb_count].sd_index = index;
5059 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5062 if (flags & MNTDF_SHOW)
5063 showdf(mntdir, &stat_buf,
5064 obd_uuid2str(&uuid_buf), flags,
5065 tp->st_name, index, rc2);
5070 if (tp->st_op == LL_STATFS_LMV) {
5071 sum.os_ffree += stat_buf.os_ffree;
5072 sum.os_files += stat_buf.os_files;
5073 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5074 ost_files += stat_buf.os_files;
5075 ost_ffree += stat_buf.os_ffree;
5077 sum.os_blocks += stat_buf.os_blocks *
5079 sum.os_bfree += stat_buf.os_bfree *
5081 sum.os_bavail += stat_buf.os_bavail *
5088 /* If we have _some_ OSTs, but don't have as many free objects on the
5089 * OST as inodes on the MDTs, reduce the reported number of inodes
5090 * to compensate, so that the "inodes in use" number is correct.
5091 * This should be kept in sync with ll_statfs_internal().
5093 if (ost_files && ost_ffree < sum.os_ffree) {
5094 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5095 sum.os_ffree = ost_ffree;
5097 if (flags & MNTDF_SHOW) {
5099 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5106 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5108 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5110 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5112 int r1 = obd_statfs_ratio(st1, false);
5113 int r2 = obd_statfs_ratio(st2, false);
5114 int64_t result = r1 - r2;
5116 /* if both space usage are above 90, compare free inodes */
5117 if (r1 > 90 && r2 > 90)
5118 result = st2->os_ffree - st1->os_ffree;
5122 else if (result == 0)
5129 static int lfs_setdirstripe(int argc, char **argv)
5133 struct lfs_setstripe_args lsa = { 0 };
5134 struct llapi_stripe_param *param = NULL;
5135 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5138 char *mode_opt = NULL;
5139 bool default_stripe = false;
5140 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5141 mode_t previous_mode = 0;
5142 bool delete = false;
5143 struct ll_statfs_buf *lsb = NULL;
5144 char mntdir[PATH_MAX] = "";
5145 bool auto_distributed = false;
5146 bool foreign_mode = false;
5148 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5150 struct option long_opts[] = {
5151 { .val = 'c', .name = "count", .has_arg = required_argument },
5152 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5153 { .val = 'd', .name = "delete", .has_arg = no_argument },
5154 { .val = 'D', .name = "default", .has_arg = no_argument },
5155 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5156 { .val = LFS_LAYOUT_FLAGS_OPT,
5157 .name = "flags", .has_arg = required_argument },
5158 { .val = LFS_LAYOUT_FOREIGN_OPT,
5159 .name = "foreign", .has_arg = optional_argument},
5160 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5161 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5162 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5163 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5165 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5166 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5168 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5169 { .val = 'i', .name = "index", .has_arg = required_argument },
5171 { .val = 'o', .name = "mode", .has_arg = required_argument },
5172 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5173 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5175 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5176 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5177 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5180 setstripe_args_init(&lsa);
5182 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5183 long_opts, NULL)) >= 0) {
5190 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5193 "%s %s: invalid stripe count '%s'\n",
5194 progname, argv[0], optarg);
5200 default_stripe = true;
5203 default_stripe = true;
5205 case LFS_LAYOUT_FOREIGN_OPT:
5206 if (optarg != NULL) {
5207 /* check pure numeric */
5208 type = strtoul(optarg, &end, 0);
5211 type = check_foreign_type_name(optarg);
5212 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5214 "%s %s: unknown foreign type '%s'\n",
5221 foreign_mode = true;
5223 case LFS_LAYOUT_FLAGS_OPT:
5224 flags = strtoul(optarg, &end, 16);
5227 "%s %s: bad flags '%s'\n",
5228 progname, argv[0], optarg);
5232 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5234 fprintf(stderr, "warning: '--hash-type' and '-t' "
5235 "deprecated, use '--mdt-hash' or '-H' instead\n");
5238 lsa.lsa_pattern = check_hashtype(optarg);
5239 if (lsa.lsa_pattern == 0) {
5241 "%s %s: bad stripe hash type '%s'\n",
5242 progname, argv[0], optarg);
5247 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5250 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5251 if (strcmp(argv[optind - 1], "--index") == 0)
5253 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5256 lsa.lsa_nr_tgts = parse_targets(mdts,
5257 sizeof(mdts) / sizeof(__u32),
5258 lsa.lsa_nr_tgts, optarg);
5259 if (lsa.lsa_nr_tgts < 0) {
5261 "%s %s: invalid MDT target(s) '%s'\n",
5262 progname, argv[0], optarg);
5266 lsa.lsa_tgts = mdts;
5267 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5268 lsa.lsa_stripe_off = mdts[0];
5270 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5272 fprintf(stderr, "warning: '-m' is deprecated, "
5273 "use '--mode' or '-o' instead\n");
5282 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5283 progname, argv[0], argv[optind - 1]);
5288 if (optind == argc) {
5289 fprintf(stderr, "%s %s: DIR must be specified\n",
5294 if (xattr && !foreign_mode) {
5295 /* only print a warning as this is armless and will be
5299 "%s %s: xattr has been specified for non-foreign layout\n",
5301 } else if (foreign_mode && !xattr) {
5303 "%s %s: xattr must be provided in foreign mode\n",
5308 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5309 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5311 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5316 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5317 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5319 "%s %s: stripe offset and count must be specified\n",
5325 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5326 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5328 "%s %s: cannot specify -d with -c or -i options\n",
5333 if (mode_opt != NULL) {
5334 mode = strtoul(mode_opt, &end, 8);
5337 "%s %s: bad MODE '%s'\n",
5338 progname, argv[0], mode_opt);
5341 previous_mode = umask(0);
5344 /* foreign LMV/dir case */
5346 if (argc > optind + 1) {
5348 "%s %s: cannot specify multiple foreign dirs\n",
5353 dname = argv[optind];
5354 result = llapi_dir_create_foreign(dname, mode, type, flags,
5358 "%s mkdir: can't create foreign dir '%s': %s\n",
5359 progname, dname, strerror(-result));
5364 * initialize stripe parameters, in case param is converted to specific,
5365 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5367 param = calloc(1, offsetof(typeof(*param),
5368 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5369 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5370 if (param == NULL) {
5372 "%s %s: cannot allocate memory for parameters: %s\n",
5373 progname, argv[0], strerror(ENOMEM));
5377 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5378 param->lsp_stripe_count = lsa.lsa_stripe_count;
5379 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5380 param->lsp_stripe_offset = -1;
5382 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5383 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5384 param->lsp_stripe_pattern = lsa.lsa_pattern;
5386 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5387 param->lsp_pool = lsa.lsa_pool_name;
5388 param->lsp_is_specific = false;
5389 if (lsa.lsa_nr_tgts > 1) {
5390 if (lsa.lsa_stripe_count > 0 &&
5391 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5392 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5393 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5394 "match the number of MDTs: %d\n",
5395 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5400 param->lsp_is_specific = true;
5401 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5402 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5405 dname = argv[optind];
5407 if (default_stripe) {
5408 result = llapi_dir_set_default_lmv(dname, param);
5410 /* if current \a dname isn't under the same \a mntdir
5411 * as the last one, and the last one was
5412 * auto-distributed, restore \a param.
5414 if (mntdir[0] != '\0' &&
5415 strncmp(dname, mntdir, strlen(mntdir)) &&
5417 param->lsp_is_specific = false;
5418 param->lsp_stripe_offset = -1;
5419 auto_distributed = false;
5422 if (!param->lsp_is_specific &&
5423 param->lsp_stripe_offset == -1) {
5424 char path[PATH_MAX] = "";
5427 lsb = malloc(sizeof(*lsb));
5435 /* use mntdir for dirname() temporarily */
5436 strncpy(mntdir, dname, sizeof(mntdir));
5437 if (!realpath(dirname(mntdir), path)) {
5440 "error: invalid path '%s': %s\n",
5441 argv[optind], strerror(errno));
5446 result = llapi_search_mounts(path, 0, mntdir,
5448 if (result < 0 || mntdir[0] == '\0') {
5450 "No suitable Lustre mount found\n");
5454 result = mntdf(mntdir, NULL, NULL, 0,
5455 LL_STATFS_LMV, lsb);
5459 if (param->lsp_stripe_count > lsb->sb_count) {
5461 "error: stripe count %d is too big\n",
5462 param->lsp_stripe_count);
5467 qsort(lsb->sb_buf, lsb->sb_count,
5468 sizeof(struct ll_statfs_data),
5469 ll_statfs_data_comp);
5471 auto_distributed = true;
5474 if (auto_distributed) {
5476 int nr = MAX(param->lsp_stripe_count,
5479 /* don't use server whose usage is above 90% */
5480 while (nr != param->lsp_stripe_count &&
5481 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5483 nr = MAX(param->lsp_stripe_count,
5486 /* get \a r between [0, nr) */
5489 param->lsp_stripe_offset =
5490 lsb->sb_buf[r].sd_index;
5491 if (param->lsp_stripe_count > 1) {
5494 param->lsp_is_specific = true;
5495 for (; i < param->lsp_stripe_count; i++)
5496 param->lsp_tgts[(i + r) % nr] =
5497 lsb->sb_buf[i].sd_index;
5501 result = llapi_dir_create(dname, mode, param);
5506 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5507 progname, dname, strerror(-result));
5510 dname = argv[++optind];
5511 } while (dname != NULL);
5513 if (mode_opt != NULL)
5514 umask(previous_mode);
5522 static int lfs_rmentry(int argc, char **argv)
5529 fprintf(stderr, "error: %s: missing dirname\n",
5535 dname = argv[index];
5536 while (dname != NULL) {
5537 result = llapi_direntry_remove(dname);
5539 fprintf(stderr, "error: %s: remove dir entry '%s' "
5540 "failed\n", argv[0], dname);
5543 dname = argv[++index];
5548 static int lfs_mv(int argc, char **argv)
5550 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5551 struct find_param param = {
5558 struct option long_opts[] = {
5559 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5560 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5561 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5564 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5566 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5568 fprintf(stderr, "warning: '-M' deprecated"
5569 ", use '--mdt-index' or '-m' instead\n");
5572 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5574 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5580 param.fp_verbose = VERBOSE_DETAIL;
5583 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5584 progname, argv[optind - 1]);
5589 if (lmu.lum_stripe_offset == -1) {
5590 fprintf(stderr, "%s mv: MDT index must be specified\n",
5595 if (optind >= argc) {
5596 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5601 /* initialize migrate mdt parameters */
5602 param.fp_lmv_md = &lmu;
5603 param.fp_migrate = 1;
5604 rc = llapi_migrate_mdt(argv[optind], ¶m);
5606 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5607 progname, argv[optind], param.fp_mdt_index,
5612 static int lfs_osts(int argc, char **argv)
5614 return lfs_tgts(argc, argv);
5617 static int lfs_mdts(int argc, char **argv)
5619 return lfs_tgts(argc, argv);
5622 static int lfs_df(int argc, char **argv)
5624 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5625 enum mntdf_flags flags = MNTDF_SHOW;
5626 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5627 int c, rc = 0, index = 0;
5628 char fsname[PATH_MAX] = "", *pool_name = NULL;
5629 struct option long_opts[] = {
5630 { .val = 'h', .name = "human-readable",
5631 .has_arg = no_argument },
5632 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5633 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5634 { .val = 'p', .name = "pool", .has_arg = required_argument },
5635 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5638 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5641 flags |= MNTDF_COOKED;
5644 flags |= MNTDF_INODES;
5647 flags |= MNTDF_LAZY;
5653 flags |= MNTDF_VERBOSE;
5659 if (optind < argc && !realpath(argv[optind], path)) {
5661 fprintf(stderr, "error: invalid path '%s': %s\n",
5662 argv[optind], strerror(-rc));
5666 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5667 /* Check if we have a mount point */
5668 if (mntdir[0] == '\0')
5671 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5672 if (rc || path[0] != '\0')
5674 fsname[0] = '\0'; /* avoid matching in next loop */
5675 mntdir[0] = '\0'; /* avoid matching in next loop */
5681 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5682 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5686 /* llapi_search_mounts() fills "fsname", but that is not called if
5687 * explicit paths are specified on the command-line
5689 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5690 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5693 fprintf(stderr, "cannot get instance for '%s': %s\n",
5694 mntdir, strerror(-rc));
5698 if (!opt_instance) {
5699 /* print only the fsname name */
5700 buf = strchr(fsname, '-');
5704 } else if (!opt_fsname) {
5705 /* print only the instance name */
5706 buf = strchr(fsname, '-');
5712 } else if (opt_fsname) {
5713 /* print only the fsname */
5717 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5722 static int lfs_getname(int argc, char **argv)
5724 struct option long_opts[] = {
5725 { .val = 'h', .name = "help", .has_arg = no_argument },
5726 { .val = 'i', .name = "instance", .has_arg = no_argument },
5727 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5729 bool opt_instance = false, opt_fsname = false;
5730 char fsname[PATH_MAX] = "";
5733 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5736 opt_instance = true;
5747 /* If neither option is given, print both instance and fsname */
5748 if (!opt_instance && !opt_fsname)
5749 opt_instance = opt_fsname = true;
5751 if (optind == argc) { /* no paths specified, get all paths. */
5752 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5755 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5756 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5757 opt_instance, opt_fsname, true);
5760 path[0] = fsname[0] = mntdir[0] = '\0';
5762 } else { /* paths specified, only attempt to search these. */
5765 /* if only one path is given, print only requested info */
5766 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5768 for (; optind < argc; optind++) {
5769 rc2 = print_instance(argv[optind], fsname,
5770 sizeof(fsname), opt_instance,
5771 opt_fsname, opt_mntdir);
5781 static int lfs_check(int argc, char **argv)
5783 char mntdir[PATH_MAX] = {'\0'};
5792 fprintf(stderr, "%s check: server type must be specified\n",
5797 obd_types[0] = obd_type1;
5798 obd_types[1] = obd_type2;
5799 obd_types[2] = obd_type3;
5801 if (strcmp(argv[1], "osts") == 0) {
5802 strcpy(obd_types[0], "osc");
5803 } else if (strcmp(argv[1], "mdts") == 0 ||
5804 strcmp(argv[1], "mds") == 0) {
5805 strcpy(obd_types[0], "mdc");
5806 } else if (strcmp(argv[1], "mgts") == 0) {
5807 strcpy(obd_types[0], "mgc");
5808 } else if (strcmp(argv[1], "all") == 0 ||
5809 strcmp(argv[1], "servers") == 0) {
5811 strcpy(obd_types[0], "osc");
5812 strcpy(obd_types[1], "mdc");
5813 strcpy(obd_types[2], "mgc");
5815 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5820 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5821 if (rc < 0 || mntdir[0] == '\0') {
5823 "%s check: cannot find mounted Lustre filesystem: %s\n",
5824 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5828 rc = llapi_target_check(num_types, obd_types, mntdir);
5830 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5831 progname, argv[1], strerror(-rc));
5837 #ifdef HAVE_SYS_QUOTA_H
5838 #define ARG2INT(nr, str, msg) \
5841 nr = strtol(str, &endp, 0); \
5842 if (*endp != '\0') { \
5843 fprintf(stderr, "%s: bad %s '%s'\n", \
5844 progname, msg, str); \
5849 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5851 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5852 * returns the value or ULONG_MAX on integer overflow or incorrect format
5854 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5855 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5856 * 3. empty integer value is interpreted as 0
5858 static unsigned long str2sec(const char* timestr)
5860 const char spec[] = "smhdw";
5861 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5862 unsigned long val = 0;
5865 if (strpbrk(timestr, spec) == NULL) {
5866 /* no specifiers inside the time string,
5867 should treat it as an integer value */
5868 val = strtoul(timestr, &tail, 10);
5869 return *tail ? ULONG_MAX : val;
5872 /* format string is XXwXXdXXhXXmXXs */
5878 v = strtoul(timestr, &tail, 10);
5879 if (v == ULONG_MAX || *tail == '\0')
5880 /* value too large (ULONG_MAX or more)
5881 or missing specifier */
5884 ptr = strchr(spec, *tail);
5886 /* unknown specifier */
5891 /* check if product will overflow the type */
5892 if (!(v < ULONG_MAX / mult[ind]))
5895 ADD_OVERFLOW(val, mult[ind] * v);
5896 if (val == ULONG_MAX)
5908 #define ARG2ULL(nr, str, def_units) \
5910 unsigned long long limit, units = def_units; \
5913 rc = llapi_parse_size(str, &limit, &units, 1); \
5915 fprintf(stderr, "%s: invalid limit '%s'\n", \
5922 static inline int has_times_option(int argc, char **argv)
5926 for (i = 1; i < argc; i++)
5927 if (!strcmp(argv[i], "-t"))
5933 int lfs_setquota_times(int argc, char **argv)
5936 struct if_quotactl qctl;
5937 char *mnt, *obd_type = (char *)qctl.obd_type;
5938 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5939 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5940 struct option long_opts[] = {
5941 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5942 { .val = 'g', .name = "group", .has_arg = no_argument },
5943 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5944 { .val = 'p', .name = "projid", .has_arg = no_argument },
5945 { .val = 't', .name = "times", .has_arg = no_argument },
5946 { .val = 'u', .name = "user", .has_arg = no_argument },
5950 memset(&qctl, 0, sizeof(qctl));
5951 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5952 qctl.qc_type = ALLQUOTA;
5954 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5955 long_opts, NULL)) != -1) {
5966 if (qctl.qc_type != ALLQUOTA) {
5967 fprintf(stderr, "error: -u/g/p can't be used "
5968 "more than once\n");
5971 qctl.qc_type = qtype;
5974 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5975 fprintf(stderr, "error: bad block-grace: %s\n",
5979 dqb->dqb_valid |= QIF_BTIME;
5982 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5983 fprintf(stderr, "error: bad inode-grace: %s\n",
5987 dqb->dqb_valid |= QIF_ITIME;
5989 case 't': /* Yes, of course! */
5991 default: /* getopt prints error message for us when opterr != 0 */
5996 if (qctl.qc_type == ALLQUOTA) {
5997 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6001 if (optind != argc - 1) {
6002 fprintf(stderr, "error: unexpected parameters encountered\n");
6006 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6007 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6008 fprintf(stderr, "error: grace time is too large\n");
6013 rc = llapi_quotactl(mnt, &qctl);
6016 fprintf(stderr, "%s %s ", obd_type,
6017 obd_uuid2str(&qctl.obd_uuid));
6018 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6025 #define BSLIMIT (1 << 0)
6026 #define BHLIMIT (1 << 1)
6027 #define ISLIMIT (1 << 2)
6028 #define IHLIMIT (1 << 3)
6030 int lfs_setquota(int argc, char **argv)
6033 struct if_quotactl qctl;
6034 char *mnt, *obd_type = (char *)qctl.obd_type;
6035 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6036 struct option long_opts[] = {
6037 { .val = 'b', .name = "block-softlimit",
6038 .has_arg = required_argument },
6039 { .val = 'B', .name = "block-hardlimit",
6040 .has_arg = required_argument },
6041 { .val = 'd', .name = "default", .has_arg = no_argument },
6042 { .val = 'g', .name = "group", .has_arg = required_argument },
6043 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6044 { .val = 'i', .name = "inode-softlimit",
6045 .has_arg = required_argument },
6046 { .val = 'I', .name = "inode-hardlimit",
6047 .has_arg = required_argument },
6048 { .val = 'p', .name = "projid", .has_arg = required_argument },
6049 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6050 { .val = 'u', .name = "user", .has_arg = required_argument },
6051 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6053 unsigned limit_mask = 0;
6055 bool use_default = false;
6058 if (has_times_option(argc, argv))
6059 return lfs_setquota_times(argc, argv);
6061 memset(&qctl, 0, sizeof(qctl));
6062 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6063 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6064 * so it can be used as a marker that qc_type
6065 * isn't reinitialized from command line */
6067 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6068 long_opts, NULL)) != -1) {
6071 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6074 goto quota_type_def;
6077 rc = name2uid(&qctl.qc_id, optarg);
6080 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6083 goto quota_type_def;
6086 rc = name2gid(&qctl.qc_id, optarg);
6089 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6092 goto quota_type_def;
6095 rc = name2projid(&qctl.qc_id, optarg);
6098 qctl.qc_id = strtoul(optarg, &endptr, 10);
6099 if (*endptr != '\0') {
6100 fprintf(stderr, "%s setquota: invalid"
6101 " id '%s'\n", progname, optarg);
6106 if (qctl.qc_id == 0) {
6107 fprintf(stderr, "%s setquota: can't set quota"
6108 " for root usr/group/project.\n",
6114 if (qctl.qc_type != ALLQUOTA) {
6116 "%s setquota: only one of -u, -U, -g,"
6117 " -G, -p or -P may be specified\n",
6121 qctl.qc_type = qtype;
6124 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6128 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6129 dqb->dqb_bsoftlimit >>= 10;
6130 limit_mask |= BSLIMIT;
6131 if (dqb->dqb_bsoftlimit &&
6132 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6134 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6135 "See '%s help setquota' or Lustre manual for details\n",
6136 progname, dqb->dqb_bsoftlimit,
6140 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6141 dqb->dqb_bhardlimit >>= 10;
6142 limit_mask |= BHLIMIT;
6143 if (dqb->dqb_bhardlimit &&
6144 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6146 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6147 "See '%s help setquota' or Lustre manual for details\n",
6148 progname, dqb->dqb_bhardlimit,
6152 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6153 limit_mask |= ISLIMIT;
6154 if (dqb->dqb_isoftlimit &&
6155 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6157 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6158 "See '%s help setquota' or Lustre manual for details\n",
6159 progname, dqb->dqb_isoftlimit,
6163 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6164 limit_mask |= IHLIMIT;
6165 if (dqb->dqb_ihardlimit &&
6166 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6168 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6169 "See '%s help setquota' or Lustre manual for details\n",
6170 progname, dqb->dqb_ihardlimit,
6175 "%s setquota: unrecognized option '%s'\n",
6176 progname, argv[optind - 1]);
6181 if (qctl.qc_type == ALLQUOTA) {
6183 "%s setquota: either -u or -g must be specified\n",
6188 if (!use_default && limit_mask == 0) {
6190 "%s setquota: at least one limit must be specified\n",
6195 if (use_default && limit_mask != 0) {
6197 "%s setquota: limits should not be specified when"
6198 " using default quota\n",
6203 if (use_default && qctl.qc_id == 0) {
6205 "%s setquota: can not set default quota for root"
6206 " user/group/project\n",
6211 if (optind != argc - 1) {
6213 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6214 progname, argv[optind]);
6221 dqb->dqb_bhardlimit = 0;
6222 dqb->dqb_bsoftlimit = 0;
6223 dqb->dqb_ihardlimit = 0;
6224 dqb->dqb_isoftlimit = 0;
6227 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6228 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6229 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6230 /* sigh, we can't just set blimits/ilimits */
6231 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6232 .qc_type = qctl.qc_type,
6233 .qc_id = qctl.qc_id};
6235 rc = llapi_quotactl(mnt, &tmp_qctl);
6239 if (!(limit_mask & BHLIMIT))
6240 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6241 if (!(limit_mask & BSLIMIT))
6242 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6243 if (!(limit_mask & IHLIMIT))
6244 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6245 if (!(limit_mask & ISLIMIT))
6246 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6248 /* Keep grace times if we have got no softlimit arguments */
6249 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6250 dqb->dqb_valid |= QIF_BTIME;
6251 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6254 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6255 dqb->dqb_valid |= QIF_ITIME;
6256 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6260 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6261 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6263 rc = llapi_quotactl(mnt, &qctl);
6267 "%s setquota: cannot quotactl '%s' '%s': %s",
6269 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6276 /* Converts seconds value into format string
6277 * result is returned in buf
6279 * 1. result is in descenting order: 1w2d3h4m5s
6280 * 2. zero fields are not filled (except for p. 3): 5d1s
6281 * 3. zero seconds value is presented as "0s"
6283 static char * __sec2str(time_t seconds, char *buf)
6285 const char spec[] = "smhdw";
6286 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6291 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6292 c = seconds / mult[i];
6294 if (c > 0 || (i == 0 && buf == tail))
6295 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6303 static void sec2str(time_t seconds, char *buf, int rc)
6310 tail = __sec2str(seconds, tail);
6312 if (rc && tail - buf < 39) {
6318 static void diff2str(time_t seconds, char *buf, time_t now)
6324 if (seconds <= now) {
6325 strcpy(buf, "none");
6328 __sec2str(seconds - now, buf);
6331 static void print_quota_title(char *name, struct if_quotactl *qctl,
6332 bool human_readable, bool show_default)
6335 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6336 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6337 "Filesystem", "bquota", "blimit", "bgrace",
6338 "iquota", "ilimit", "igrace");
6340 printf("Disk quotas for %s %s (%cid %u):\n",
6341 qtype_name(qctl->qc_type), name,
6342 *qtype_name(qctl->qc_type), qctl->qc_id);
6343 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6344 "Filesystem", human_readable ? "used" : "kbytes",
6345 "quota", "limit", "grace",
6346 "files", "quota", "limit", "grace");
6350 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6353 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6356 snprintf(buf, buflen, "%5.4gP",
6357 (double)num / ((__u64)1 << 40));
6359 snprintf(buf, buflen, "%5.4gT",
6360 (double)num / (1 << 30));
6362 snprintf(buf, buflen, "%5.4gG",
6363 (double)num / (1 << 20));
6365 snprintf(buf, buflen, "%5.4gM",
6366 (double)num / (1 << 10));
6368 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6372 #define STRBUF_LEN 32
6373 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6374 int rc, bool h, bool show_default)
6380 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6381 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6382 int bover = 0, iover = 0;
6383 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6384 char numbuf[3][STRBUF_LEN];
6386 char strbuf[STRBUF_LEN];
6388 if (dqb->dqb_bhardlimit &&
6389 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6391 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6392 if (dqb->dqb_btime > now) {
6399 if (dqb->dqb_ihardlimit &&
6400 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6402 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6403 if (dqb->dqb_itime > now) {
6411 if (strlen(mnt) > 15)
6412 printf("%s\n%15s", mnt, "");
6414 printf("%15s", mnt);
6417 diff2str(dqb->dqb_btime, timebuf, now);
6418 else if (show_default)
6419 snprintf(timebuf, sizeof(timebuf), "%llu",
6422 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6423 strbuf, sizeof(strbuf), h);
6424 if (rc == -EREMOTEIO)
6425 sprintf(numbuf[0], "%s*", strbuf);
6427 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6428 "%s" : "[%s]", strbuf);
6430 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6431 if (type == QC_GENERAL)
6432 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6433 "%s" : "[%s]", strbuf);
6435 sprintf(numbuf[1], "%s", "-");
6437 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6438 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6439 "%s" : "[%s]", strbuf);
6442 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6444 printf(" %7s%c %6s %7s %7s",
6445 numbuf[0], bover ? '*' : ' ', numbuf[1],
6446 numbuf[2], bover > 1 ? timebuf : "-");
6450 diff2str(dqb->dqb_itime, timebuf, now);
6451 else if (show_default)
6452 snprintf(timebuf, sizeof(timebuf), "%llu",
6455 snprintf(numbuf[0], sizeof(numbuf),
6456 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6457 (uintmax_t)dqb->dqb_curinodes);
6459 if (type == QC_GENERAL)
6460 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6462 (uintmax_t)dqb->dqb_isoftlimit);
6464 sprintf(numbuf[1], "%s", "-");
6466 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6467 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6470 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6471 else if (type != QC_OSTIDX)
6472 printf(" %7s%c %6s %7s %7s",
6473 numbuf[0], iover ? '*' : ' ', numbuf[1],
6474 numbuf[2], iover > 1 ? timebuf : "-");
6476 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6479 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6480 qctl->qc_cmd == Q_GETOINFO) {
6484 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6485 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6486 printf("Block grace time: %s; Inode grace time: %s\n",
6487 bgtimebuf, igtimebuf);
6491 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6492 bool h, __u64 *total)
6494 int rc = 0, rc1 = 0, count = 0;
6495 __u32 valid = qctl->qc_valid;
6497 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6499 fprintf(stderr, "can not get %s count: %s\n",
6500 is_mdt ? "mdt": "ost", strerror(-rc));
6504 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6505 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6506 rc = llapi_quotactl(mnt, qctl);
6508 /* It is remote client case. */
6509 if (rc == -EOPNOTSUPP) {
6516 fprintf(stderr, "quotactl %s%d failed.\n",
6517 is_mdt ? "mdt": "ost", qctl->qc_idx);
6521 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6522 qctl->qc_valid, 0, h, false);
6523 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6524 qctl->qc_dqblk.dqb_bhardlimit;
6527 qctl->qc_valid = valid;
6531 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6532 int verbose, int quiet, bool human_readable,
6535 int rc1 = 0, rc2 = 0, rc3 = 0;
6536 char *obd_type = (char *)qctl->obd_type;
6537 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6538 __u64 total_ialloc = 0, total_balloc = 0;
6539 bool use_default_for_blk = false;
6540 bool use_default_for_file = false;
6543 rc1 = llapi_quotactl(mnt, qctl);
6547 fprintf(stderr, "%s quotas are not enabled.\n",
6548 qtype_name(qctl->qc_type));
6551 fprintf(stderr, "Permission denied.\n");
6554 /* We already got error message. */
6557 fprintf(stderr, "Unexpected quotactl error: %s\n",
6562 if (!show_default && qctl->qc_id == 0) {
6563 qctl->qc_dqblk.dqb_bhardlimit = 0;
6564 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6565 qctl->qc_dqblk.dqb_ihardlimit = 0;
6566 qctl->qc_dqblk.dqb_isoftlimit = 0;
6567 qctl->qc_dqblk.dqb_btime = 0;
6568 qctl->qc_dqblk.dqb_itime = 0;
6569 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6572 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6573 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6574 use_default_for_blk = true;
6575 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6578 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6579 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6580 use_default_for_file = true;
6581 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6584 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6585 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6586 print_quota_title(name, qctl, human_readable, show_default);
6588 if (rc1 && *obd_type)
6589 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6591 if (qctl->qc_valid != QC_GENERAL)
6594 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6595 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6596 (QIF_LIMITS|QIF_USAGE));
6598 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6600 if (!show_default && verbose &&
6601 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6602 char strbuf[STRBUF_LEN];
6604 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6606 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6608 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6610 printf("Total allocated inode limit: %ju, total "
6611 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6615 if (use_default_for_blk)
6616 printf("%cid %u is using default block quota setting\n",
6617 *qtype_name(qctl->qc_type), qctl->qc_id);
6619 if (use_default_for_file)
6620 printf("%cid %u is using default file quota setting\n",
6621 *qtype_name(qctl->qc_type), qctl->qc_id);
6623 if (rc1 || rc2 || rc3 || inacc)
6624 printf("Some errors happened when getting quota info. "
6625 "Some devices may be not working or deactivated. "
6626 "The data in \"[]\" is inaccurate.\n");
6632 static int lfs_project(int argc, char **argv)
6634 int ret = 0, err = 0, c, i;
6635 struct project_handle_control phc = { 0 };
6636 enum lfs_project_ops_t op;
6639 phc.assign_projid = false;
6640 /* default action */
6641 op = LFS_PROJECT_LIST;
6643 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6646 if (op != LFS_PROJECT_LIST) {
6648 "%s: cannot specify '-c' '-C' '-s' together\n",
6653 op = LFS_PROJECT_CHECK;
6656 if (op != LFS_PROJECT_LIST) {
6658 "%s: cannot specify '-c' '-C' '-s' together\n",
6663 op = LFS_PROJECT_CLEAR;
6666 if (op != LFS_PROJECT_LIST) {
6668 "%s: cannot specify '-c' '-C' '-s' together\n",
6673 phc.set_inherit = true;
6674 op = LFS_PROJECT_SET;
6680 phc.keep_projid = true;
6683 phc.recursive = true;
6686 phc.projid = strtoul(optarg, NULL, 0);
6687 phc.assign_projid = true;
6691 phc.newline = false;
6694 fprintf(stderr, "%s: invalid option '%c'\n",
6700 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6701 op = LFS_PROJECT_SET;
6702 phc.set_projid = true;
6703 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6704 phc.set_projid = true;
6708 case LFS_PROJECT_CHECK:
6709 if (phc.keep_projid) {
6711 "%s: '-k' is useless together with '-c'\n",
6716 case LFS_PROJECT_CLEAR:
6719 "%s: '-0' is useless together with '-C'\n",
6723 if (phc.assign_projid) {
6725 "%s: '-p' is useless together with '-C'\n",
6730 case LFS_PROJECT_SET:
6733 "%s: '-0' is useless together with '-s'\n",
6737 if (phc.keep_projid) {
6739 "%s: '-k' is useless together with '-s'\n",
6747 "%s: '-0' is useless for list operations\n",
6757 fprintf(stderr, "%s: missing file or directory target(s)\n",
6762 for (i = 0; i < argc; i++) {
6764 case LFS_PROJECT_CHECK:
6765 err = lfs_project_check(argv[i], &phc);
6767 case LFS_PROJECT_LIST:
6768 err = lfs_project_list(argv[i], &phc);
6770 case LFS_PROJECT_CLEAR:
6771 err = lfs_project_clear(argv[i], &phc);
6773 case LFS_PROJECT_SET:
6774 err = lfs_project_set(argv[i], &phc);
6786 static int lfs_quota(int argc, char **argv)
6789 char *mnt, *name = NULL;
6790 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6791 .qc_type = ALLQUOTA };
6792 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6793 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6795 __u32 valid = QC_GENERAL, idx = 0;
6796 bool human_readable = false;
6797 bool show_default = false;
6800 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6803 show_default = true;
6808 show_default = true;
6813 show_default = true;
6817 if (qctl.qc_type != ALLQUOTA) {
6819 "%s quota: only one of -u, -g, or -p may be specified\n",
6823 qctl.qc_type = qtype;
6826 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6829 valid = qctl.qc_valid = QC_UUID;
6830 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6833 valid = qctl.qc_valid = QC_MDTIDX;
6834 idx = qctl.qc_idx = atoi(optarg);
6835 if (idx == 0 && *optarg != '0') {
6837 "%s quota: invalid MDT index '%s'\n",
6843 valid = qctl.qc_valid = QC_OSTIDX;
6844 idx = qctl.qc_idx = atoi(optarg);
6845 if (idx == 0 && *optarg != '0') {
6847 "%s quota: invalid OST index '%s'\n",
6859 human_readable = true;
6862 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6863 progname, argv[optind - 1]);
6868 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6869 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6870 optind == argc - 1 && !show_default) {
6872 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6873 qctl.qc_valid = valid;
6876 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6877 qctl.qc_type = qtype;
6878 if (qtype == USRQUOTA) {
6879 qctl.qc_id = geteuid();
6880 rc = uid2name(&name, qctl.qc_id);
6882 qctl.qc_id = getegid();
6883 rc = gid2name(&name, qctl.qc_id);
6888 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6889 human_readable, show_default);
6894 /* lfs quota -u username /path/to/lustre/mount */
6895 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6896 /* options should be followed by u/g-name and mntpoint */
6897 if ((!show_default && optind + 2 != argc) ||
6898 (show_default && optind + 1 != argc) ||
6899 qctl.qc_type == ALLQUOTA) {
6901 "%s quota: name and mount point must be specified\n",
6906 if (!show_default) {
6907 name = argv[optind++];
6908 switch (qctl.qc_type) {
6910 rc = name2uid(&qctl.qc_id, name);
6913 rc = name2gid(&qctl.qc_id, name);
6916 rc = name2projid(&qctl.qc_id, name);
6923 qctl.qc_valid = QC_GENERAL;
6924 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6929 qctl.qc_id = strtoul(name, &endptr, 10);
6930 if (*endptr != '\0') {
6931 fprintf(stderr, "%s quota: invalid id '%s'\n",
6936 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6937 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6943 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6944 human_readable, show_default);
6947 #endif /* HAVE_SYS_QUOTA_H! */
6949 static int flushctx_ioctl(char *mp)
6953 fd = open(mp, O_RDONLY);
6955 fprintf(stderr, "flushctx: error open %s: %s\n",
6956 mp, strerror(errno));
6960 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6962 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6963 mp, strerror(errno));
6969 static int lfs_flushctx(int argc, char **argv)
6971 int kdestroy = 0, c;
6972 char mntdir[PATH_MAX] = {'\0'};
6976 while ((c = getopt(argc, argv, "k")) != -1) {
6982 fprintf(stderr, "error: %s: option '-%c' "
6983 "unrecognized\n", argv[0], c);
6989 if ((rc = system("kdestroy > /dev/null")) != 0) {
6990 rc = WEXITSTATUS(rc);
6991 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6995 if (optind >= argc) {
6996 /* flush for all mounted lustre fs. */
6997 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6998 /* Check if we have a mount point */
6999 if (mntdir[0] == '\0')
7002 if (flushctx_ioctl(mntdir))
7005 mntdir[0] = '\0'; /* avoid matching in next loop */
7008 /* flush fs as specified */
7009 while (optind < argc) {
7010 if (flushctx_ioctl(argv[optind++]))
7017 static int lfs_changelog(int argc, char **argv)
7019 void *changelog_priv;
7020 struct changelog_rec *rec;
7021 long long startrec = 0, endrec = 0;
7023 struct option long_opts[] = {
7024 { .val = 'f', .name = "follow", .has_arg = no_argument },
7026 char short_opts[] = "f";
7029 while ((rc = getopt_long(argc, argv, short_opts,
7030 long_opts, NULL)) != -1) {
7037 "%s changelog: unrecognized option '%s'\n",
7038 progname, argv[optind - 1]);
7042 if (optind >= argc) {
7043 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7048 mdd = argv[optind++];
7050 startrec = strtoll(argv[optind++], NULL, 10);
7052 endrec = strtoll(argv[optind++], NULL, 10);
7054 rc = llapi_changelog_start(&changelog_priv,
7055 CHANGELOG_FLAG_BLOCK |
7056 CHANGELOG_FLAG_JOBID |
7057 CHANGELOG_FLAG_EXTRA_FLAGS |
7058 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7061 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7062 progname, strerror(errno = -rc));
7066 rc = llapi_changelog_set_xflags(changelog_priv,
7067 CHANGELOG_EXTRA_FLAG_UIDGID |
7068 CHANGELOG_EXTRA_FLAG_NID |
7069 CHANGELOG_EXTRA_FLAG_OMODE |
7070 CHANGELOG_EXTRA_FLAG_XATTR);
7073 "%s changelog: cannot set xflags for changelog: %s\n",
7074 progname, strerror(errno = -rc));
7078 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7082 if (endrec && rec->cr_index > endrec) {
7083 llapi_changelog_free(&rec);
7086 if (rec->cr_index < startrec) {
7087 llapi_changelog_free(&rec);
7091 secs = rec->cr_time >> 30;
7092 gmtime_r(&secs, &ts);
7093 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7094 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7095 changelog_type2str(rec->cr_type),
7096 ts.tm_hour, ts.tm_min, ts.tm_sec,
7097 (int)(rec->cr_time & ((1 << 30) - 1)),
7098 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7099 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7101 if (rec->cr_flags & CLF_JOBID) {
7102 struct changelog_ext_jobid *jid =
7103 changelog_rec_jobid(rec);
7105 if (jid->cr_jobid[0] != '\0')
7106 printf(" j=%s", jid->cr_jobid);
7109 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7110 struct changelog_ext_extra_flags *ef =
7111 changelog_rec_extra_flags(rec);
7113 printf(" ef=0x%llx", ef->cr_extra_flags);
7115 if (ef->cr_extra_flags & CLFE_UIDGID) {
7116 struct changelog_ext_uidgid *uidgid =
7117 changelog_rec_uidgid(rec);
7119 printf(" u=%llu:%llu",
7120 uidgid->cr_uid, uidgid->cr_gid);
7122 if (ef->cr_extra_flags & CLFE_NID) {
7123 struct changelog_ext_nid *nid =
7124 changelog_rec_nid(rec);
7127 libcfs_nid2str(nid->cr_nid));
7130 if (ef->cr_extra_flags & CLFE_OPEN) {
7131 struct changelog_ext_openmode *omd =
7132 changelog_rec_openmode(rec);
7133 char mode[] = "---";
7135 /* exec mode must be exclusive */
7136 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7139 if (omd->cr_openflags & MDS_FMODE_READ)
7141 if (omd->cr_openflags &
7148 if (strcmp(mode, "---") != 0)
7149 printf(" m=%s", mode);
7153 if (ef->cr_extra_flags & CLFE_XATTR) {
7154 struct changelog_ext_xattr *xattr =
7155 changelog_rec_xattr(rec);
7157 if (xattr->cr_xattr[0] != '\0')
7158 printf(" x=%s", xattr->cr_xattr);
7162 if (rec->cr_namelen)
7163 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7164 rec->cr_namelen, changelog_rec_name(rec));
7166 if (rec->cr_flags & CLF_RENAME) {
7167 struct changelog_ext_rename *rnm =
7168 changelog_rec_rename(rec);
7170 if (!fid_is_zero(&rnm->cr_sfid))
7171 printf(" s="DFID" sp="DFID" %.*s",
7172 PFID(&rnm->cr_sfid),
7173 PFID(&rnm->cr_spfid),
7174 (int)changelog_rec_snamelen(rec),
7175 changelog_rec_sname(rec));
7179 llapi_changelog_free(&rec);
7182 llapi_changelog_fini(&changelog_priv);
7185 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7186 progname, strerror(errno = -rc));
7188 return (rc == 1 ? 0 : rc);
7191 static int lfs_changelog_clear(int argc, char **argv)
7199 endrec = strtoll(argv[3], NULL, 10);
7201 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7204 fprintf(stderr, "%s: record out of range: %llu\n",
7206 else if (rc == -ENOENT)
7207 fprintf(stderr, "%s: no changelog user: %s\n",
7210 fprintf(stderr, "%s error: %s\n", argv[0],
7219 static int lfs_fid2path(int argc, char **argv)
7221 struct option long_opts[] = {
7222 { .val = 'c', .name = "cur", .has_arg = no_argument },
7223 { .val = 'l', .name = "link", .has_arg = required_argument },
7224 { .val = 'r', .name = "rec", .has_arg = required_argument },
7226 char short_opts[] = "cl:r:";
7227 char *device, *fid, *path;
7228 long long recno = -1;
7233 char *endptr = NULL;
7235 while ((rc = getopt_long(argc, argv, short_opts,
7236 long_opts, NULL)) != -1) {
7242 linkno = strtol(optarg, &endptr, 10);
7243 if (*endptr != '\0') {
7245 "%s fid2path: invalid linkno '%s'\n",
7251 recno = strtoll(optarg, &endptr, 10);
7252 if (*endptr != '\0') {
7254 "%s fid2path: invalid recno '%s'\n",
7261 "%s fid2path: unrecognized option '%s'\n",
7262 progname, argv[optind - 1]);
7269 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7274 device = argv[optind++];
7275 path = calloc(1, PATH_MAX);
7279 "%s fid2path: cannot allocate memory for path: %s\n",
7280 progname, strerror(-rc));
7285 while (optind < argc) {
7286 fid = argv[optind++];
7288 lnktmp = (linkno >= 0) ? linkno : 0;
7290 int oldtmp = lnktmp;
7291 long long rectmp = recno;
7293 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7297 "%s fid2path: cannot find '%s': %s\n",
7298 progname, fid, strerror(errno = -rc2));
7305 fprintf(stdout, "%lld ", rectmp);
7306 if (device[0] == '/') {
7307 fprintf(stdout, "%s", device);
7308 if (device[strlen(device) - 1] != '/')
7309 fprintf(stdout, "/");
7310 } else if (path[0] == '\0') {
7311 fprintf(stdout, "/");
7313 fprintf(stdout, "%s\n", path);
7316 /* specified linkno */
7318 if (oldtmp == lnktmp)
7328 static int lfs_path2fid(int argc, char **argv)
7330 struct option long_opts[] = {
7331 { .val = 'p', .name = "parents", .has_arg = no_argument },
7334 const char short_opts[] = "p";
7335 const char *sep = "";
7338 bool show_parents = false;
7340 while ((rc = getopt_long(argc, argv, short_opts,
7341 long_opts, NULL)) != -1) {
7344 show_parents = true;
7348 "%s path2fid: unrecognized option '%s'\n",
7349 progname, argv[optind - 1]);
7354 if (optind > argc - 1) {
7355 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7359 else if (optind < argc - 1)
7363 for (path = argv + optind; *path != NULL; path++) {
7365 if (!show_parents) {
7366 err = llapi_path2fid(*path, &fid);
7368 printf("%s%s"DFID"\n",
7369 *sep != '\0' ? *path : "", sep,
7372 char name[NAME_MAX + 1];
7373 unsigned int linkno = 0;
7375 while ((err = llapi_path2parent(*path, linkno, &fid,
7376 name, sizeof(name))) == 0) {
7377 if (*sep != '\0' && linkno == 0)
7378 printf("%s%s", *path, sep);
7380 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7385 /* err == -ENODATA is end-of-loop */
7386 if (linkno > 0 && err == -ENODATA) {
7394 "%s path2fid: cannot get %sfid for '%s': %s\n",
7395 progname, show_parents ? "parent " : "", *path,
7407 static int lfs_data_version(int argc, char **argv)
7414 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7417 fprintf(stderr, "%s data_version: FILE must be specified\n",
7422 while ((c = getopt(argc, argv, "nrw")) != -1) {
7425 data_version_flags = 0;
7428 data_version_flags |= LL_DV_RD_FLUSH;
7431 data_version_flags |= LL_DV_WR_FLUSH;
7435 "%s data_version: unrecognized option '%s'\n",
7436 progname, argv[optind - 1]);
7440 if (optind == argc) {
7441 fprintf(stderr, "%s data_version: FILE must be specified\n",
7446 path = argv[optind];
7447 fd = open(path, O_RDONLY);
7450 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7451 progname, path, strerror(-rc));
7455 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7458 "%s data_version: cannot get version for '%s': %s\n",
7459 progname, path, strerror(-rc));
7461 printf("%ju" "\n", (uintmax_t)data_version);
7467 static int lfs_hsm_state(int argc, char **argv)
7472 struct hsm_user_state hus;
7480 rc = llapi_hsm_state_get(path, &hus);
7482 fprintf(stderr, "can't get hsm state for %s: %s\n",
7483 path, strerror(errno = -rc));
7487 /* Display path name and status flags */
7488 printf("%s: (0x%08x)", path, hus.hus_states);
7490 if (hus.hus_states & HS_RELEASED)
7491 printf(" released");
7492 if (hus.hus_states & HS_EXISTS)
7494 if (hus.hus_states & HS_DIRTY)
7496 if (hus.hus_states & HS_ARCHIVED)
7497 printf(" archived");
7498 /* Display user-settable flags */
7499 if (hus.hus_states & HS_NORELEASE)
7500 printf(" never_release");
7501 if (hus.hus_states & HS_NOARCHIVE)
7502 printf(" never_archive");
7503 if (hus.hus_states & HS_LOST)
7504 printf(" lost_from_hsm");
7506 if (hus.hus_archive_id != 0)
7507 printf(", archive_id:%d", hus.hus_archive_id);
7510 } while (++i < argc);
7515 #define LFS_HSM_SET 0
7516 #define LFS_HSM_CLEAR 1
7519 * Generic function to set or clear HSM flags.
7520 * Used by hsm_set and hsm_clear.
7522 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7524 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7526 struct option long_opts[] = {
7527 { .val = 'A', .name = "archived", .has_arg = no_argument },
7528 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7529 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7530 { .val = 'e', .name = "exists", .has_arg = no_argument },
7531 { .val = 'l', .name = "lost", .has_arg = no_argument },
7532 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7533 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7535 char short_opts[] = "lraAdei:";
7539 __u32 archive_id = 0;
7545 while ((c = getopt_long(argc, argv, short_opts,
7546 long_opts, NULL)) != -1) {
7552 mask |= HS_NOARCHIVE;
7555 mask |= HS_ARCHIVED;
7558 mask |= HS_NORELEASE;
7567 archive_id = strtol(optarg, &end, 10);
7569 fprintf(stderr, "invalid archive_id: '%s'\n",
7577 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7578 argv[0], argv[optind - 1]);
7583 /* User should have specified a flag */
7587 while (optind < argc) {
7589 path = argv[optind];
7591 /* If mode == 0, this means we apply the mask. */
7592 if (mode == LFS_HSM_SET)
7593 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7595 rc = llapi_hsm_state_set(path, 0, mask, 0);
7598 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7599 path, strerror(errno = -rc));
7608 static int lfs_hsm_action(int argc, char **argv)
7613 struct hsm_current_action hca;
7614 struct hsm_extent he;
7615 enum hsm_user_action hua;
7616 enum hsm_progress_states hps;
7624 rc = llapi_hsm_current_action(path, &hca);
7626 fprintf(stderr, "can't get hsm action for %s: %s\n",
7627 path, strerror(errno = -rc));
7630 he = hca.hca_location;
7631 hua = hca.hca_action;
7632 hps = hca.hca_state;
7634 printf("%s: %s", path, hsm_user_action2name(hua));
7636 /* Skip file without action */
7637 if (hca.hca_action == HUA_NONE) {
7642 printf(" %s ", hsm_progress_state2name(hps));
7644 if ((hps == HPS_RUNNING) &&
7645 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7646 printf("(%llu bytes moved)\n",
7647 (unsigned long long)he.length);
7648 else if ((he.offset + he.length) == LUSTRE_EOF)
7649 printf("(from %llu to EOF)\n",
7650 (unsigned long long)he.offset);
7652 printf("(from %llu to %llu)\n",
7653 (unsigned long long)he.offset,
7654 (unsigned long long)(he.offset + he.length));
7656 } while (++i < argc);
7661 static int lfs_hsm_set(int argc, char **argv)
7663 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7666 static int lfs_hsm_clear(int argc, char **argv)
7668 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7672 * Check file state and return its fid, to be used by lfs_hsm_request().
7674 * \param[in] file Path to file to check
7675 * \param[in,out] fid Pointer to allocated lu_fid struct.
7676 * \param[in,out] last_dev Pointer to last device id used.
7678 * \return 0 on success.
7680 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7686 rc = lstat(file, &st);
7688 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7691 /* Checking for regular file as archiving as posix copytool
7692 * rejects archiving files other than regular files
7694 if (!S_ISREG(st.st_mode)) {
7695 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7698 /* A request should be ... */
7699 if (*last_dev != st.st_dev && *last_dev != 0) {
7700 fprintf(stderr, "All files should be "
7701 "on the same filesystem: %s\n", file);
7704 *last_dev = st.st_dev;
7706 rc = llapi_path2fid(file, fid);
7708 fprintf(stderr, "Cannot read FID of %s: %s\n",
7709 file, strerror(-rc));
7715 /* Fill an HSM HUR item with a given file name.
7717 * If mntpath is set, then the filename is actually a FID, and no
7718 * lookup on the filesystem will be performed.
7720 * \param[in] hur the user request to fill
7721 * \param[in] idx index of the item inside the HUR to fill
7722 * \param[in] mntpath mountpoint of Lustre
7723 * \param[in] fname filename (if mtnpath is NULL)
7724 * or FID (if mntpath is set)
7725 * \param[in] last_dev pointer to last device id used
7727 * \retval 0 on success
7728 * \retval CMD_HELP or a negative errno on error
7730 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7731 const char *mntpath, const char *fname,
7734 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7737 hui->hui_extent.length = -1;
7739 if (mntpath != NULL) {
7742 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7746 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7751 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7755 hur->hur_request.hr_itemcount++;
7760 static int lfs_hsm_request(int argc, char **argv, int action)
7762 struct option long_opts[] = {
7763 { .val = 'a', .name = "archive", .has_arg = required_argument },
7764 { .val = 'D', .name = "data", .has_arg = required_argument },
7765 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7766 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7769 char short_opts[] = "l:D:a:m:";
7770 struct hsm_user_request *hur, *oldhur;
7775 char *filelist = NULL;
7776 char fullpath[PATH_MAX];
7777 char *opaque = NULL;
7781 int nbfile_alloc = 0;
7782 char *some_file = NULL;
7783 char *mntpath = NULL;
7789 while ((c = getopt_long(argc, argv, short_opts,
7790 long_opts, NULL)) != -1) {
7799 if (action != HUA_ARCHIVE &&
7800 action != HUA_REMOVE) {
7802 "error: -a is supported only "
7803 "when archiving or removing\n");
7806 archive_id = atoi(optarg);
7809 if (some_file == NULL) {
7811 some_file = strdup(optarg);
7817 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7818 argv[0], argv[optind - 1]);
7823 /* All remaining args are files, so we have at least nbfile */
7824 nbfile = argc - optind;
7826 if ((nbfile == 0) && (filelist == NULL))
7830 opaque_len = strlen(opaque);
7832 /* Alloc the request structure with enough place to store all files
7833 * from command line. */
7834 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7836 fprintf(stderr, "Cannot create the request: %s\n",
7840 nbfile_alloc = nbfile;
7842 hur->hur_request.hr_action = action;
7843 hur->hur_request.hr_archive_id = archive_id;
7844 hur->hur_request.hr_flags = 0;
7846 /* All remaining args are files, add them */
7847 if (nbfile != 0 && some_file == NULL)
7848 some_file = strdup(argv[optind]);
7850 for (i = 0; i < nbfile; i++) {
7851 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7857 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7859 /* If a filelist was specified, read the filelist from it. */
7860 if (filelist != NULL) {
7861 fp = fopen(filelist, "r");
7863 fprintf(stderr, "Cannot read the file list %s: %s\n",
7864 filelist, strerror(errno));
7869 while ((rc = getline(&line, &len, fp)) != -1) {
7870 /* If allocated buffer was too small, get something
7872 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7875 nbfile_alloc = nbfile_alloc * 2 + 1;
7877 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7880 fprintf(stderr, "hsm: cannot allocate "
7881 "the request: %s\n",
7888 size = hur_len(oldhur);
7890 fprintf(stderr, "hsm: cannot allocate "
7891 "%u files + %u bytes data\n",
7892 oldhur->hur_request.hr_itemcount,
7893 oldhur->hur_request.hr_data_len);
7900 memcpy(hur, oldhur, size);
7905 if (line[strlen(line) - 1] == '\n')
7906 line[strlen(line) - 1] = '\0';
7908 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7909 mntpath, line, &last_dev);
7915 if (some_file == NULL) {
7925 /* If a --data was used, add it to the request */
7926 hur->hur_request.hr_data_len = opaque_len;
7928 memcpy(hur_data(hur), opaque, opaque_len);
7930 /* Send the HSM request */
7931 if (realpath(some_file, fullpath) == NULL) {
7932 fprintf(stderr, "Could not find path '%s': %s\n",
7933 some_file, strerror(errno));
7935 rc = llapi_hsm_request(fullpath, hur);
7937 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7938 some_file, strerror(-rc));
7948 static int lfs_hsm_archive(int argc, char **argv)
7950 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7953 static int lfs_hsm_restore(int argc, char **argv)
7955 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7958 static int lfs_hsm_release(int argc, char **argv)
7960 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7963 static int lfs_hsm_remove(int argc, char **argv)
7965 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7968 static int lfs_hsm_cancel(int argc, char **argv)
7970 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7973 static int lfs_swap_layouts(int argc, char **argv)
7978 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7979 SWAP_LAYOUTS_KEEP_MTIME |
7980 SWAP_LAYOUTS_KEEP_ATIME);
7983 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7985 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7987 int lfs_get_mode(const char *string)
7989 enum lock_mode_user mode;
7991 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7992 if (lock_mode_names[mode] == NULL)
7994 if (strcmp(string, lock_mode_names[mode]) == 0)
8001 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8003 enum lu_ladvise_type advice;
8006 advice < ARRAY_SIZE(ladvise_names); advice++) {
8007 if (ladvise_names[advice] == NULL)
8009 if (strcmp(string, ladvise_names[advice]) == 0)
8013 return LU_LADVISE_INVALID;
8016 static int lfs_ladvise(int argc, char **argv)
8018 struct option long_opts[] = {
8019 { .val = 'a', .name = "advice", .has_arg = required_argument },
8020 { .val = 'b', .name = "background", .has_arg = no_argument },
8021 { .val = 'e', .name = "end", .has_arg = required_argument },
8022 { .val = 'l', .name = "length", .has_arg = required_argument },
8023 { .val = 'm', .name = "mode", .has_arg = required_argument },
8024 { .val = 's', .name = "start", .has_arg = required_argument },
8025 { .val = 'u', .name = "unset", .has_arg = no_argument },
8027 char short_opts[] = "a:be:l:m:s:u";
8032 struct llapi_lu_ladvise advice;
8033 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8034 unsigned long long start = 0;
8035 unsigned long long end = LUSTRE_EOF;
8036 unsigned long long length = 0;
8037 unsigned long long size_units;
8038 unsigned long long flags = 0;
8042 while ((c = getopt_long(argc, argv, short_opts,
8043 long_opts, NULL)) != -1) {
8046 advice_type = lfs_get_ladvice(optarg);
8047 if (advice_type == LU_LADVISE_INVALID) {
8048 fprintf(stderr, "%s: invalid advice type "
8049 "'%s'\n", argv[0], optarg);
8050 fprintf(stderr, "Valid types:");
8052 for (advice_type = 0;
8053 advice_type < ARRAY_SIZE(ladvise_names);
8055 if (ladvise_names[advice_type] == NULL)
8057 fprintf(stderr, " %s",
8058 ladvise_names[advice_type]);
8060 fprintf(stderr, "\n");
8073 rc = llapi_parse_size(optarg, &end,
8076 fprintf(stderr, "%s: bad end offset '%s'\n",
8083 rc = llapi_parse_size(optarg, &start,
8086 fprintf(stderr, "%s: bad start offset "
8087 "'%s'\n", argv[0], optarg);
8093 rc = llapi_parse_size(optarg, &length,
8096 fprintf(stderr, "%s: bad length '%s'\n",
8102 mode = lfs_get_mode(optarg);
8104 fprintf(stderr, "%s: bad mode '%s', valid "
8105 "modes are READ or WRITE\n",
8113 fprintf(stderr, "%s: option '%s' unrecognized\n",
8114 argv[0], argv[optind - 1]);
8119 if (advice_type == LU_LADVISE_INVALID) {
8120 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8121 fprintf(stderr, "Valid types:");
8122 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8124 if (ladvise_names[advice_type] == NULL)
8126 fprintf(stderr, " %s", ladvise_names[advice_type]);
8128 fprintf(stderr, "\n");
8132 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8133 fprintf(stderr, "%s: Lock no expand advice is a per file "
8134 "descriptor advice, so when called from lfs, "
8135 "it does nothing.\n", argv[0]);
8139 if (argc <= optind) {
8140 fprintf(stderr, "%s: please give one or more file names\n",
8145 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8146 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8151 if (end == LUSTRE_EOF && length != 0)
8152 end = start + length;
8155 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8156 argv[0], start, end);
8160 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8161 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8166 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8167 fprintf(stderr, "%s: mode is required with lockahead\n",
8172 while (optind < argc) {
8175 path = argv[optind++];
8177 fd = open(path, O_RDONLY);
8179 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8180 argv[0], path, strerror(errno));
8185 advice.lla_start = start;
8186 advice.lla_end = end;
8187 advice.lla_advice = advice_type;
8188 advice.lla_value1 = 0;
8189 advice.lla_value2 = 0;
8190 advice.lla_value3 = 0;
8191 advice.lla_value4 = 0;
8192 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8193 advice.lla_lockahead_mode = mode;
8194 advice.lla_peradvice_flags = flags;
8197 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8200 fprintf(stderr, "%s: cannot give advice '%s' to file "
8201 "'%s': %s\n", argv[0],
8202 ladvise_names[advice_type],
8203 path, strerror(errno));
8209 if (rc == 0 && rc2 < 0)
8215 static const char *const heat_names[] = LU_HEAT_NAMES;
8217 static int lfs_heat_get(int argc, char **argv)
8219 struct lu_heat *heat;
8228 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8230 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8235 while (optind < argc) {
8236 path = argv[optind++];
8238 fd = open(path, O_RDONLY);
8240 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8241 argv[0], path, strerror(errno));
8246 heat->lh_count = OBD_HEAT_COUNT;
8247 rc2 = llapi_heat_get(fd, heat);
8250 fprintf(stderr, "%s: cannot get heat of file '%s'"
8251 ": %s\n", argv[0], path, strerror(errno));
8255 printf("flags: %x\n", heat->lh_flags);
8256 for (i = 0; i < heat->lh_count; i++)
8257 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8259 if (rc == 0 && rc2 < 0)
8267 static int lfs_heat_set(int argc, char **argv)
8269 struct option long_opts[] = {
8270 { .val = 'c', .name = "clear", .has_arg = no_argument },
8271 { .val = 'o', .name = "off", .has_arg = no_argument },
8272 { .val = 'O', .name = "on", .has_arg = no_argument },
8274 enum lu_heat_flag flags = 0;
8284 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8287 flags |= LU_HEAT_FLAG_CLEAR;
8290 flags |= LU_HEAT_FLAG_CLEAR;
8291 flags |= LU_HEAT_FLAG_OFF;
8294 flags &= ~LU_HEAT_FLAG_OFF;
8299 fprintf(stderr, "%s: option '%s' unrecognized\n",
8300 argv[0], argv[optind - 1]);
8305 if (argc <= optind) {
8306 fprintf(stderr, "%s: please give one or more file names\n",
8311 while (optind < argc) {
8312 path = argv[optind++];
8314 fd = open(path, O_RDONLY);
8316 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8317 argv[0], path, strerror(errno));
8322 rc2 = llapi_heat_set(fd, flags);
8325 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8326 ": %s\n", argv[0], path, strerror(errno));
8330 if (rc == 0 && rc2 < 0)
8336 /** The input string contains a comma delimited list of component ids and
8337 * ranges, for example "1,2-4,7".
8339 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8341 bool end_of_loop = false;
8349 while (!end_of_loop) {
8353 char *endptr = NULL;
8356 ptr = strchrnul(arg, ',');
8357 end_of_loop = *ptr == '\0';
8360 start_index = strtol(arg, &endptr, 0);
8361 if (endptr == arg) /* no data at all */
8363 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8365 if (start_index < 0)
8368 end_index = start_index;
8369 if (*endptr == '-') {
8370 end_index = strtol(endptr + 1, &endptr, 0);
8371 if (*endptr != '\0')
8373 if (end_index < start_index)
8377 for (i = start_index; i <= end_index && size > 0; i++) {
8380 /* remove duplicate */
8381 for (j = 0; j < nr; j++) {
8385 if (j == nr) { /* no duplicate */
8391 if (size == 0 && i < end_index)
8398 if (!end_of_loop && ptr != NULL)
8401 return rc < 0 ? rc : nr;
8405 * struct verify_mirror_id - Mirror id to be verified.
8406 * @mirror_id: A specified mirror id.
8407 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8409 struct verify_mirror_id {
8415 * compare_mirror_ids() - Compare mirror ids.
8416 * @layout: Mirror component list.
8417 * @cbdata: Callback data in verify_mirror_id structure.
8419 * This is a callback function called by llapi_layout_comp_iterate()
8420 * to compare the specified mirror id with the one in the current
8421 * component of @layout. If they are the same, then the specified
8422 * mirror id is valid.
8424 * Return: a negative error code on failure or
8425 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8426 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8429 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8431 struct verify_mirror_id *mirror_id_cbdata =
8432 (struct verify_mirror_id *)cbdata;
8436 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8440 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8441 progname, strerror(errno));
8445 if (mirror_id_cbdata->mirror_id == mirror_id) {
8446 mirror_id_cbdata->is_valid_id = true;
8447 return LLAPI_LAYOUT_ITER_STOP;
8450 return LLAPI_LAYOUT_ITER_CONT;
8454 * verify_mirror_ids() - Verify specified mirror ids.
8455 * @fname: Mirrored file name.
8456 * @mirror_ids: Specified mirror ids to be verified.
8457 * @ids_nr: Number of specified mirror ids.
8459 * This function verifies that specified @mirror_ids are valid
8460 * in the mirrored file @fname.
8462 * Return: 0 on success or a negative error code on failure.
8465 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8467 struct llapi_layout *layout = NULL;
8468 struct verify_mirror_id mirror_id_cbdata = { 0 };
8479 if (stat(fname, &stbuf) < 0) {
8480 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8481 progname, fname, strerror(errno));
8486 if (!S_ISREG(stbuf.st_mode)) {
8487 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8493 fd = open(fname, O_DIRECT | O_RDONLY);
8495 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8496 progname, fname, strerror(errno));
8501 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8503 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8504 progname, fname, strerror(errno));
8508 layout = llapi_layout_get_by_fd(fd, 0);
8509 if (layout == NULL) {
8510 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8511 progname, fname, strerror(errno));
8513 llapi_lease_release(fd);
8517 rc = llapi_layout_flags_get(layout, &flr_state);
8519 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8520 progname, fname, strerror(errno));
8525 flr_state &= LCM_FL_FLR_MASK;
8526 switch (flr_state) {
8529 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8530 progname, fname, llapi_layout_flags_string(flr_state));
8537 for (i = 0; i < ids_nr; i++) {
8538 mirror_id_cbdata.mirror_id = mirror_ids[i];
8539 mirror_id_cbdata.is_valid_id = false;
8541 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8546 "%s: '%s' failed to verify mirror id: %u.\n",
8547 progname, fname, mirror_ids[i]);
8551 if (!mirror_id_cbdata.is_valid_id) {
8554 "%s: '%s' invalid specified mirror id: %u.\n",
8555 progname, fname, mirror_ids[i]);
8561 llapi_layout_free(layout);
8562 llapi_lease_release(fd);
8570 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8571 __u16 *mirror_ids, int ids_nr)
8573 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8574 struct llapi_layout *layout;
8584 if (stat(fname, &stbuf) < 0) {
8585 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8586 progname, fname, strerror(errno));
8590 if (!S_ISREG(stbuf.st_mode)) {
8591 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8597 fd = open(fname, O_DIRECT | O_RDWR);
8599 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8600 progname, fname, strerror(errno));
8605 layout = llapi_layout_get_by_fd(fd, 0);
8606 if (layout == NULL) {
8607 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8608 progname, fname, strerror(errno));
8613 rc = llapi_layout_flags_get(layout, &flr_state);
8615 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8616 progname, fname, strerror(errno));
8621 flr_state &= LCM_FL_FLR_MASK;
8622 if (flr_state == LCM_FL_NONE) {
8624 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8629 /* get stale component info */
8630 comp_size = llapi_mirror_find_stale(layout, comp_array,
8631 ARRAY_SIZE(comp_array),
8632 mirror_ids, ids_nr);
8633 if (comp_size <= 0) {
8638 ioc->lil_mode = LL_LEASE_WRLCK;
8639 ioc->lil_flags = LL_LEASE_RESYNC;
8640 rc = llapi_lease_set(fd, ioc);
8642 if (rc == -EALREADY)
8646 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8647 progname, fname, strerror(errno));
8651 /* get the read range [start, end) */
8652 start = comp_array[0].lrc_start;
8653 end = comp_array[0].lrc_end;
8654 for (idx = 1; idx < comp_size; idx++) {
8655 if (comp_array[idx].lrc_start < start)
8656 start = comp_array[idx].lrc_start;
8657 if (end < comp_array[idx].lrc_end)
8658 end = comp_array[idx].lrc_end;
8661 rc = llapi_lease_check(fd);
8662 if (rc != LL_LEASE_WRLCK) {
8663 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8668 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8671 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8672 progname, fname, rc);
8674 /* prepare ioc for lease put */
8675 ioc->lil_mode = LL_LEASE_UNLCK;
8676 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8678 for (idx = 0; idx < comp_size; idx++) {
8679 if (comp_array[idx].lrc_synced) {
8680 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8685 rc = llapi_lease_set(fd, ioc);
8687 if (rc == 0) /* lost lease lock */
8689 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8690 progname, fname, strerror(errno));
8694 * llapi_lease_set returns lease mode when it request to unlock
8700 llapi_layout_free(layout);
8707 static inline int lfs_mirror_resync(int argc, char **argv)
8709 struct ll_ioc_lease *ioc = NULL;
8710 __u16 mirror_ids[128] = { 0 };
8715 struct option long_opts[] = {
8716 { .val = 'o', .name = "only", .has_arg = required_argument },
8719 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8722 rc = parse_mirror_ids(mirror_ids,
8723 sizeof(mirror_ids) / sizeof(__u16),
8727 "%s: bad mirror ids '%s'.\n",
8734 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8735 argv[0], argv[optind - 1]);
8741 if (argc == optind) {
8742 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8747 if (ids_nr > 0 && argc > optind + 1) {
8749 "%s: option '--only' cannot be used upon multiple files.\n",
8757 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8762 /* set the lease on the file */
8763 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8765 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8766 argv[0], strerror(errno));
8771 for (; optind < argc; optind++) {
8772 rc = lfs_mirror_resync_file(argv[optind], ioc,
8773 mirror_ids, ids_nr);
8774 /* ignore previous file's error, continue with next file */
8777 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8785 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8787 struct llapi_layout *layout;
8790 layout = llapi_layout_get_by_fd(fd, 0);
8791 if (layout == NULL) {
8792 fprintf(stderr, "could not get layout.\n");
8796 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8798 fprintf(stderr, "failed to iterate layout\n");
8799 llapi_layout_free(layout);
8802 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8803 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8804 llapi_layout_free(layout);
8808 llapi_layout_free(layout);
8814 * Check whether two files are the same file
8815 * \retval 0 same file
8816 * \retval 1 not the same file
8817 * \retval <0 error code
8819 static inline int check_same_file(const char *f1, const char *f2)
8824 if (stat(f1, &stbuf1) < 0) {
8825 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8826 progname, f1, strerror(errno));
8830 if (stat(f2, &stbuf2) < 0) {
8831 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8832 progname, f2, strerror(errno));
8836 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8837 stbuf1.st_ino == stbuf2.st_ino)
8843 static inline int lfs_mirror_read(int argc, char **argv)
8846 __u16 mirror_id = 0;
8847 const char *outfile = NULL;
8853 const size_t buflen = 4 << 20;
8855 struct option long_opts[] = {
8856 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8857 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8860 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8865 mirror_id = strtoul(optarg, &end, 0);
8866 if (*end != '\0' || mirror_id == 0) {
8868 "%s %s: invalid mirror ID '%s'\n",
8869 progname, argv[0], optarg);
8877 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8878 progname, argv[optind - 1]);
8883 if (argc == optind) {
8884 fprintf(stderr, "%s %s: no mirrored file provided\n",
8887 } else if (argc > optind + 1) {
8888 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8892 if (mirror_id == 0) {
8893 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8898 /* open mirror file */
8899 fname = argv[optind];
8902 rc = check_same_file(fname, outfile);
8905 "%s %s: output file cannot be the mirrored file\n",
8913 fd = open(fname, O_DIRECT | O_RDONLY);
8915 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8916 progname, argv[0], fname, strerror(errno));
8920 /* verify mirror id */
8921 rc = verify_mirror_id_by_fd(fd, mirror_id);
8924 "%s %s: cannot find mirror with ID %u in '%s'\n",
8925 progname, argv[0], mirror_id, fname);
8929 /* open output file */
8931 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8933 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8934 progname, argv[0], outfile, strerror(errno));
8939 outfd = STDOUT_FILENO;
8942 /* allocate buffer */
8943 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8945 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8946 progname, argv[0], rc);
8953 ssize_t written = 0;
8955 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8956 if (bytes_read < 0) {
8959 "%s %s: fail to read data from mirror %u: %s\n",
8960 progname, argv[0], mirror_id, strerror(-rc));
8965 if (bytes_read == 0)
8968 while (written < bytes_read) {
8971 written2 = write(outfd, buf + written,
8972 bytes_read - written);
8975 "%s %s: fail to write %s: %s\n",
8976 progname, argv[0], outfile ? : "STDOUT",
8981 written += written2;
8984 if (written != bytes_read) {
8986 "%s %s: written %ld bytes does not match with %ld read.\n",
8987 progname, argv[0], written, bytes_read);
9009 static inline int lfs_mirror_write(int argc, char **argv)
9012 __u16 mirror_id = 0;
9013 const char *inputfile = NULL;
9019 const size_t buflen = 4 << 20;
9021 size_t page_size = sysconf(_SC_PAGESIZE);
9022 struct ll_ioc_lease_id ioc;
9024 struct option long_opts[] = {
9025 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9026 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9029 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9034 mirror_id = strtoul(optarg, &end, 0);
9035 if (*end != '\0' || mirror_id == 0) {
9037 "%s %s: invalid mirror ID '%s'\n",
9038 progname, argv[0], optarg);
9046 fprintf(stderr, "%s: option '%s' unrecognized\n",
9047 progname, argv[optind - 1]);
9052 if (argc == optind) {
9053 fprintf(stderr, "%s %s: no mirrored file provided\n",
9056 } else if (argc > optind + 1) {
9057 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9061 if (mirror_id == 0) {
9062 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9067 /* open mirror file */
9068 fname = argv[optind];
9071 rc = check_same_file(fname, inputfile);
9074 "%s %s: input file cannot be the mirrored file\n",
9082 fd = open(fname, O_DIRECT | O_WRONLY);
9084 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9085 progname, argv[0], fname, strerror(errno));
9089 /* verify mirror id */
9090 rc = verify_mirror_id_by_fd(fd, mirror_id);
9093 "%s %s: cannot find mirror with ID %u in '%s'\n",
9094 progname, argv[0], mirror_id, fname);
9098 /* open input file */
9100 inputfd = open(inputfile, O_RDONLY, 0644);
9102 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9103 progname, argv[0], inputfile, strerror(errno));
9108 inputfd = STDIN_FILENO;
9111 /* allocate buffer */
9112 rc = posix_memalign(&buf, page_size, buflen);
9114 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9115 progname, argv[0], rc);
9119 /* prepare target mirror components instantiation */
9120 ioc.lil_mode = LL_LEASE_WRLCK;
9121 ioc.lil_flags = LL_LEASE_RESYNC;
9122 ioc.lil_mirror_id = mirror_id;
9123 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9126 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9127 progname, argv[0], fname, strerror(errno));
9137 rc = llapi_lease_check(fd);
9138 if (rc != LL_LEASE_WRLCK) {
9139 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9140 progname, argv[0], fname);
9144 bytes_read = read(inputfd, buf, buflen);
9145 if (bytes_read < 0) {
9148 "%s %s: fail to read data from '%s': %s\n",
9149 progname, argv[0], inputfile ? : "STDIN",
9156 if (bytes_read == 0)
9159 /* round up to page align to make direct IO happy. */
9160 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9162 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9167 "%s %s: fail to write to mirror %u: %s\n",
9168 progname, argv[0], mirror_id,
9176 if (pos & (page_size - 1)) {
9177 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9182 ioc.lil_mode = LL_LEASE_UNLCK;
9183 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9185 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9190 "%s %s: release lease lock of '%s' failed: %s\n",
9191 progname, argv[0], fname, strerror(errno));
9208 struct collect_ids_data {
9214 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9216 struct collect_ids_data *cid = cbdata;
9220 rc = llapi_layout_mirror_id_get(layout, &id);
9224 if ((__u16)id != cid->cid_exclude) {
9227 for (i = 0; i < cid->cid_count; i++) {
9228 /* already collected the mirror id */
9229 if (id == cid->cid_ids[i])
9230 return LLAPI_LAYOUT_ITER_CONT;
9232 cid->cid_ids[cid->cid_count] = id;
9236 return LLAPI_LAYOUT_ITER_CONT;
9239 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9241 struct llapi_layout *layout;
9242 struct collect_ids_data cid = { .cid_ids = ids,
9244 .cid_exclude = exclude_id, };
9247 layout = llapi_layout_get_by_fd(fd, 0);
9248 if (layout == NULL) {
9249 fprintf(stderr, "could not get layout\n");
9253 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9255 fprintf(stderr, "failed to iterate layout\n");
9256 llapi_layout_free(layout);
9260 llapi_layout_free(layout);
9262 return cid.cid_count;
9265 static inline int lfs_mirror_copy(int argc, char **argv)
9268 __u16 read_mirror_id = 0;
9269 __u16 ids[128] = { 0 };
9271 struct llapi_layout *layout = NULL;
9272 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9279 struct ll_ioc_lease *ioc = NULL;
9280 struct ll_ioc_lease_id *resync_ioc;
9282 struct option long_opts[] = {
9283 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9284 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9287 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9292 read_mirror_id = strtoul(optarg, &end, 0);
9293 if (*end != '\0' || read_mirror_id == 0) {
9295 "%s %s: invalid read mirror ID '%s'\n",
9296 progname, argv[0], optarg);
9301 if (!strcmp(optarg, "-1")) {
9302 /* specify all other mirrors */
9306 count = parse_mirror_ids((__u16 *)ids,
9314 fprintf(stderr, "%s: option '%s' unrecognized\n",
9315 progname, argv[optind - 1]);
9320 if (argc == optind) {
9321 fprintf(stderr, "%s %s: no mirrored file provided\n",
9324 } else if (argc > optind + 1) {
9325 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9329 if (read_mirror_id == 0) {
9331 "%s %s: no valid read mirror ID %d is provided\n",
9332 progname, argv[0], read_mirror_id);
9338 "%s %s: no write mirror ID is provided\n",
9343 for (i = 0; i < count; i++) {
9344 if (read_mirror_id == ids[i]) {
9346 "%s %s: read and write mirror ID cannot be the same\n",
9352 /* open mirror file */
9353 fname = argv[optind];
9355 fd = open(fname, O_DIRECT | O_RDWR);
9357 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9358 progname, argv[0], fname, strerror(errno));
9362 /* write to all other mirrors */
9363 if (ids[0] == (__u16)-1) {
9364 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9368 "%s %s: failed to get other mirror ids in '%s': %d\n",
9369 progname, argv[0], fname, rc);
9374 /* verify mirror id */
9375 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9378 "%s %s: cannot find mirror with ID %u in '%s'\n",
9379 progname, argv[0], read_mirror_id, fname);
9383 for (i = 0; i < count; i++) {
9384 rc = verify_mirror_id_by_fd(fd, ids[i]);
9387 "%s %s: cannot find mirror with ID %u in '%s'\n",
9388 progname, argv[0], ids[i], fname);
9393 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9396 "%s %s: cannot alloc comp id array for ioc: %s\n",
9397 progname, argv[0], strerror(errno));
9402 /* get stale component info */
9403 layout = llapi_layout_get_by_fd(fd, 0);
9404 if (layout == NULL) {
9405 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9406 progname, argv[0], fname, strerror(errno));
9410 comp_size = llapi_mirror_find_stale(layout, comp_array,
9411 ARRAY_SIZE(comp_array),
9413 llapi_layout_free(layout);
9414 if (comp_size < 0) {
9419 /* prepare target mirror components instantiation */
9420 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9421 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9422 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9424 resync_ioc->lil_mirror_id = ids[0];
9426 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9427 rc = llapi_lease_set(fd, ioc);
9430 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9431 progname, argv[0], fname, strerror(errno));
9435 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9438 fprintf(stderr, "%s %s: copy error: %d\n",
9439 progname, argv[0], rc);
9443 fprintf(stdout, "mirror copied successfully: ");
9444 for (i = 0; i < copied; i++)
9445 fprintf(stdout, "%d ", ids[i]);
9446 fprintf(stdout, "\n");
9448 ioc->lil_mode = LL_LEASE_UNLCK;
9449 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9451 for (i = 0; i < comp_size; i++) {
9454 for (j = 0; j < copied; j++) {
9455 if (comp_array[i].lrc_mirror_id != ids[j])
9458 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9462 rc = llapi_lease_set(fd, ioc);
9467 "%s %s: release lease lock of '%s' failed: %s\n",
9468 progname, argv[0], fname, strerror(errno));
9482 * struct verify_chunk - Mirror chunk to be verified.
9483 * @chunk: [start, end) of the chunk.
9484 * @mirror_count: Number of mirror ids in @mirror_id array.
9485 * @mirror_id: Array of valid mirror ids that cover the chunk.
9487 struct verify_chunk {
9488 struct lu_extent chunk;
9489 unsigned int mirror_count;
9490 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9494 * print_chunks() - Print chunk information.
9495 * @fname: Mirrored file name.
9496 * @chunks: Array of chunks.
9497 * @chunk_count: Number of chunks in @chunks array.
9499 * This function prints [start, end) of each chunk in @chunks
9500 * for mirrored file @fname, and also prints the valid mirror ids
9501 * that cover the chunk.
9506 void print_chunks(const char *fname, struct verify_chunk *chunks,
9512 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9513 for (i = 0; i < chunk_count; i++) {
9514 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9516 if (chunks[i].mirror_count == 0)
9517 fprintf(stdout, "\t[");
9519 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9520 for (j = 1; j < chunks[i].mirror_count; j++)
9521 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9523 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9525 fprintf(stdout, "\n");
9529 * print_checksums() - Print CRC-32 checksum values.
9530 * @chunk: A chunk and its corresponding valid mirror ids.
9531 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9533 * This function prints CRC-32 checksum values on @chunk for
9534 * each valid mirror that covers it.
9539 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9544 "CRC-32 checksum value for chunk "DEXT":\n",
9545 PEXT(&chunk->chunk));
9546 for (i = 0; i < chunk->mirror_count; i++)
9547 fprintf(stdout, "Mirror %u:\t%#lx\n",
9548 chunk->mirror_id[i], crc[i]);
9549 fprintf(stdout, "\n");
9553 * filter_mirror_id() - Filter specified mirror ids.
9554 * @chunks: Array of chunks.
9555 * @chunk_count: Number of chunks in @chunks array.
9556 * @mirror_ids: Specified mirror ids to be verified.
9557 * @ids_nr: Number of specified mirror ids.
9559 * This function scans valid mirror ids that cover each chunk in @chunks
9560 * and filters specified mirror ids.
9565 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9566 __u16 *mirror_ids, int ids_nr)
9571 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9572 unsigned int valid_count = 0;
9574 for (i = 0; i < chunk_count; i++) {
9575 if (chunks[i].mirror_count == 0)
9579 for (j = 0; j < ids_nr; j++) {
9580 for (k = 0; k < chunks[i].mirror_count; k++) {
9581 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9582 valid_id[valid_count] = mirror_ids[j];
9589 memcpy(chunks[i].mirror_id, valid_id,
9590 sizeof(__u16) * valid_count);
9591 chunks[i].mirror_count = valid_count;
9596 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9597 * @layout: Mirror component list.
9598 * @chunks: Array of chunks.
9599 * @chunks_size: Array size of @chunks.
9601 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9602 * to find out chunk segments and store them in @chunks array.
9604 * The @mirror_id array in each element of @chunks will store the valid
9605 * mirror ids that cover the chunk. If a mirror component covering the
9606 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9607 * will not be stored into the @mirror_id array, and the chunk for that
9608 * mirror will not be verified.
9610 * The @mirror_count in each element of @chunks will store the number of
9611 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9612 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9613 * indicates the chunk is valid in only one mirror. In both cases, the
9614 * chunk will not be verified.
9616 * Here is an example:
9619 * +------+-------------+--------------------+
9621 * +------+------+------+------+-------------+
9622 * | | S | S | | mirror2
9623 * +-------------+------+------+-------------+
9625 * prepared @chunks array will contain 5 elements:
9626 * (([0, 1M), [1, 2], 2),
9627 * ([1M, 2M), [1, 2], 2),
9628 * ([2M, 3M), [1], 1),
9629 * ([3M, 4M], [], 0),
9630 * ([4M, EOF), [2], 1))
9632 * Return: the actual array size of @chunks on success
9633 * or a negative error code on failure.
9636 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9637 struct verify_chunk *chunks,
9648 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9651 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9654 "%s: move to the first layout component: %s.\n",
9655 progname, strerror(errno));
9661 chunks[idx].chunk.e_end = LUSTRE_EOF;
9663 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9666 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9667 progname, strerror(errno));
9671 if (start > chunks[idx].chunk.e_start ||
9672 end <= chunks[idx].chunk.e_start)
9675 if (end < chunks[idx].chunk.e_end)
9676 chunks[idx].chunk.e_end = end;
9678 rc = llapi_layout_comp_flags_get(layout, &flags);
9681 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9682 progname, strerror(errno));
9686 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9689 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9692 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9693 progname, strerror(errno));
9697 chunks[idx].mirror_id[i] = mirror_id;
9699 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9701 "%s: mirror_id array is too small.\n",
9708 rc = llapi_layout_comp_use(layout,
9709 LLAPI_LAYOUT_COMP_USE_NEXT);
9712 "%s: move to the next layout component: %s.\n",
9713 progname, strerror(errno));
9716 } /* loop through all components */
9718 chunks[idx].mirror_count = i;
9720 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9724 if (idx >= chunks_size) {
9725 fprintf(stderr, "%s: chunks array is too small.\n",
9731 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9735 return rc < 0 ? rc : idx + 1;
9739 * lfs_mirror_verify_chunk() - Verify a chunk.
9740 * @fd: File descriptor of the mirrored file.
9741 * @file_size: Size of the mirrored file.
9742 * @chunk: A chunk and its corresponding valid mirror ids.
9743 * @verbose: Verbose mode.
9745 * This function verifies a @chunk contains exactly the same data
9746 * ammong the mirrors that cover it.
9748 * If @verbose is specified, then the function will print where the
9749 * differences are if the data do not match. Otherwise, it will
9750 * just return an error in that case.
9752 * Return: 0 on success or a negative error code on failure.
9755 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9756 struct verify_chunk *chunk, int verbose)
9758 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9760 size_t page_size = sysconf(_SC_PAGESIZE);
9766 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9773 rc = posix_memalign(&buf, page_size, buflen);
9774 if (rc) /* error code is returned directly */
9778 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9779 PEXT(&chunk->chunk));
9780 for (i = 0; i < chunk->mirror_count; i++)
9781 fprintf(stdout, " %u", chunk->mirror_id[i]);
9782 fprintf(stdout, "\n");
9786 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9787 pos = chunk->chunk.e_start;
9788 while (bytes_done < count) {
9789 /* compute initial CRC-32 checksum */
9790 crc = crc32(0L, Z_NULL, 0);
9791 memset(crc_array, 0, sizeof(crc_array));
9794 for (i = 0; i < chunk->mirror_count; i++) {
9795 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9797 if (bytes_read < 0) {
9800 "%s: failed to read data from mirror %u: %s.\n",
9801 progname, chunk->mirror_id[i],
9806 /* compute new CRC-32 checksum */
9807 crc_array[i] = crc32(crc, buf, bytes_read);
9811 print_checksums(chunk, crc_array);
9813 /* compare CRC-32 checksum values */
9814 for (i = 1; i < chunk->mirror_count; i++) {
9815 if (crc_array[i] != crc_array[0]) {
9819 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9820 progname, PEXT(&chunk->chunk),
9821 chunk->mirror_id[0],
9822 chunk->mirror_id[i]);
9827 bytes_done += bytes_read;
9830 if (verbose > 1 && rc == 0) {
9831 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9832 PEXT(&chunk->chunk));
9833 for (i = 0; i < chunk->mirror_count; i++)
9834 fprintf(stdout, " %u", chunk->mirror_id[i]);
9835 fprintf(stdout, " PASS\n\n");
9844 * lfs_mirror_verify_file() - Verify a mirrored file.
9845 * @fname: Mirrored file name.
9846 * @mirror_ids: Specified mirror ids to be verified.
9847 * @ids_nr: Number of specified mirror ids.
9848 * @verbose: Verbose mode.
9850 * This function verifies that each SYNC mirror of a mirrored file
9851 * specified by @fname contains exactly the same data.
9853 * If @mirror_ids is specified, then the function will verify the
9854 * mirrors specified by @mirror_ids contain exactly the same data.
9856 * If @verbose is specified, then the function will print where the
9857 * differences are if the data do not match. Otherwise, it will
9858 * just return an error in that case.
9860 * Return: 0 on success or a negative error code on failure.
9863 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9866 struct verify_chunk chunks_array[1024] = { };
9867 struct llapi_layout *layout = NULL;
9871 int chunk_count = 0;
9877 if (stat(fname, &stbuf) < 0) {
9878 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9879 progname, fname, strerror(errno));
9884 if (!S_ISREG(stbuf.st_mode)) {
9885 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9891 if (stbuf.st_size == 0) {
9893 fprintf(stdout, "%s: '%s' file size is 0.\n",
9899 fd = open(fname, O_DIRECT | O_RDONLY);
9901 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9902 progname, fname, strerror(errno));
9907 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9909 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9910 progname, fname, strerror(errno));
9914 layout = llapi_layout_get_by_fd(fd, 0);
9915 if (layout == NULL) {
9916 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9917 progname, fname, strerror(errno));
9919 llapi_lease_release(fd);
9923 rc = llapi_layout_flags_get(layout, &flr_state);
9925 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9926 progname, fname, strerror(errno));
9931 flr_state &= LCM_FL_FLR_MASK;
9932 switch (flr_state) {
9935 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9936 progname, fname, llapi_layout_flags_string(flr_state));
9942 /* find out mirror chunks to be verified */
9943 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9944 ARRAY_SIZE(chunks_array));
9945 if (chunk_count < 0) {
9951 /* filter specified mirror ids */
9952 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9955 print_chunks(fname, chunks_array, chunk_count);
9957 for (idx = 0; idx < chunk_count; idx++) {
9958 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9961 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9963 PEXT(&chunks_array[idx].chunk),
9964 (unsigned long long)stbuf.st_size);
9968 if (chunks_array[idx].mirror_count == 0) {
9970 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9972 PEXT(&chunks_array[idx].chunk));
9974 fprintf(stderr, "skipped\n");
9978 fprintf(stderr, "failed\n");
9982 if (chunks_array[idx].mirror_count == 1) {
9985 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9987 PEXT(&chunks_array[idx].chunk),
9988 chunks_array[idx].mirror_id[0]);
9992 rc = llapi_lease_check(fd);
9993 if (rc != LL_LEASE_RDLCK) {
9994 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9999 /* verify one chunk */
10000 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10001 &chunks_array[idx], verbose);
10015 llapi_layout_free(layout);
10016 llapi_lease_release(fd);
10024 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10025 * @argc: The count of lfs mirror verify command line arguments.
10026 * @argv: Array of strings for lfs mirror verify command line arguments.
10028 * This function parses lfs mirror verify command and verifies the
10029 * specified mirrored file(s).
10031 * Return: 0 on success or a negative error code on failure.
10033 static inline int lfs_mirror_verify(int argc, char **argv)
10035 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10041 char cmd[PATH_MAX];
10043 struct option long_opts[] = {
10044 { .val = 'o', .name = "only", .has_arg = required_argument },
10045 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10046 { .name = NULL } };
10048 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10050 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10053 rc = parse_mirror_ids(mirror_ids,
10054 ARRAY_SIZE(mirror_ids),
10058 "%s: bad mirror ids '%s'.\n",
10065 "%s: at least 2 mirror ids needed with '--only' option.\n",
10075 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10076 progname, argv[optind - 1]);
10082 if (argc == optind) {
10083 fprintf(stderr, "%s: no file name given.\n", progname);
10088 if (ids_nr > 0 && argc > optind + 1) {
10090 "%s: '--only' cannot be used upon multiple files.\n",
10098 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10104 for (; optind < argc; optind++) {
10105 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10115 * lfs_mirror() - Parse and execute lfs mirror commands.
10116 * @argc: The count of lfs mirror command line arguments.
10117 * @argv: Array of strings for lfs mirror command line arguments.
10119 * This function parses lfs mirror commands and performs the
10120 * corresponding functions specified in mirror_cmdlist[].
10122 * Return: 0 on success or an error code on failure.
10124 static int lfs_mirror(int argc, char **argv)
10126 char cmd[PATH_MAX];
10129 setlinebuf(stdout);
10131 Parser_init("lfs-mirror > ", mirror_cmdlist);
10133 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10135 program_invocation_short_name = cmd;
10137 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10139 rc = Parser_commands();
10141 return rc < 0 ? -rc : rc;
10144 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10146 #if __BYTE_ORDER == __BIG_ENDIAN
10147 __swab16s(&attrs->lsa_valid);
10148 __swab64s(&attrs->lsa_size);
10149 __swab64s(&attrs->lsa_blocks);
10153 enum lfs_som_type {
10154 LFS_SOM_SIZE = 0x1,
10155 LFS_SOM_BLOCKS = 0x2,
10156 LFS_SOM_FLAGS = 0x4,
10157 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10161 static int lfs_getsom(int argc, char **argv)
10164 struct lustre_som_attrs *attrs;
10165 char buf[sizeof(*attrs) + 64];
10166 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10169 while ((c = getopt(argc, argv, "sbf")) != -1) {
10172 type = LFS_SOM_SIZE;
10175 type = LFS_SOM_BLOCKS;
10178 type = LFS_SOM_FLAGS;
10181 fprintf(stderr, "%s: invalid option '%c'\n",
10191 fprintf(stderr, "%s: %s\n",
10192 progname, argc == 0 ? "miss file target" :
10193 "input more than 2 files");
10198 attrs = (void *)buf;
10199 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10202 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10203 argv[0], strerror(errno), errno);
10207 lustre_som_swab(attrs);
10210 case LFS_SOM_ATTR_ALL:
10211 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10212 path, attrs->lsa_size, attrs->lsa_blocks,
10216 printf("%llu\n", attrs->lsa_size);
10218 case LFS_SOM_BLOCKS:
10219 printf("%llu\n", attrs->lsa_blocks);
10221 case LFS_SOM_FLAGS:
10222 printf("%x\n", attrs->lsa_valid);
10225 fprintf(stderr, "%s: unknown option\n", progname);
10233 * lfs_mirror_list_commands() - List lfs mirror commands.
10234 * @argc: The count of command line arguments.
10235 * @argv: Array of strings for command line arguments.
10237 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10239 * Return: 0 on success.
10241 static int lfs_mirror_list_commands(int argc, char **argv)
10243 char buffer[81] = "";
10245 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10251 static int lfs_list_commands(int argc, char **argv)
10253 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10255 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10260 int main(int argc, char **argv)
10264 /* Ensure that liblustreapi constructor has run */
10265 if (!llapi_liblustreapi_initialized())
10266 fprintf(stderr, "liblustreapi was not properly initialized\n");
10268 setlinebuf(stdout);
10271 Parser_init("lfs > ", cmdlist);
10273 progname = program_invocation_short_name; /* Used in error messages */
10275 llapi_set_command_name(argv[1]);
10276 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10277 llapi_clear_command_name();
10279 rc = Parser_commands();
10282 return rc < 0 ? -rc : rc;
10285 #ifdef _LUSTRE_IDL_H_
10286 /* Everything we need here should be included by lustreapi.h. */
10287 # error "lfs should not depend on lustre_idl.h"
10288 #endif /* _LUSTRE_IDL_H_ */