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"
262 * command_t mirror_cmdlist - lfs mirror commands.
264 command_t mirror_cmdlist[] = {
265 { .pc_name = "create", .pc_func = lfs_mirror_create,
266 .pc_help = "Create a mirrored file.\n"
267 "usage: lfs mirror create "
268 "<--mirror-count|-N[mirror_count]> "
269 "[setstripe options] ... <filename|directory>\n"
270 MIRROR_CREATE_HELP },
271 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
272 .pc_help = "Extend a mirrored file.\n"
273 "usage: lfs mirror extend "
274 "<--mirror-count|-N[mirror_count]> [--no-verify] "
275 "[setstripe options|-f <victim_file>] ... <filename>\n"
276 MIRROR_EXTEND_HELP },
277 { .pc_name = "split", .pc_func = lfs_mirror_split,
278 .pc_help = "Split a mirrored file.\n"
279 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
280 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
281 "\t [-f <new_file>] <mirrored file>\n"
282 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
283 "\t can be fetched by lfs getstripe command.\n"
284 "\tcomp_id: Unique component ID within a mirror.\n"
285 "\tnew_file: This option indicates the layout of the split\n"
286 "\t mirror will be stored into. If not specified,\n"
287 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
288 "\t will be used.\n" },
289 { .pc_name = "read", .pc_func = lfs_mirror_read,
290 .pc_help = "Read the content of a specified mirror of a file.\n"
291 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
292 "[--outfile|-o <output_file>] <mirrored_file>\n" },
293 { .pc_name = "write", .pc_func = lfs_mirror_write,
294 .pc_help = "Write to a specified mirror of a file.\n"
295 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
296 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
297 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
298 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
299 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
300 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
301 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
302 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
303 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
304 "<mirrored file> [<mirrored file2>...]\n"},
305 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
306 .pc_help = "Verify mirrored file(s).\n"
307 "usage: lfs mirror verify "
308 "[--only <mirror_id,mirror_id2[,...]>] "
309 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
310 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
311 .pc_help = "list commands supported by lfs mirror"},
312 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
313 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
314 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
318 /* all available commands */
319 command_t cmdlist[] = {
320 {"setstripe", lfs_setstripe, 0,
321 "To create a file with specified striping/composite layout, or\n"
322 "create/replace the default layout on an existing directory:\n"
323 SSM_CMD_COMMON("setstripe")
325 " <directory|filename>\n"
327 "To add component(s) to an existing composite file:\n"
328 SSM_CMD_COMMON("setstripe --component-add")
330 "To totally delete the default striping from an existing directory:\n"
331 "usage: setstripe -d <directory>\n"
333 "To create a mirrored file or set s default mirror layout on a directory:\n"
334 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
336 "To delete the last component(s) from an existing composite file\n"
337 "(note that this will also delete any data in those components):\n"
338 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
339 " [--component-flags|-F <comp_flags>]\n"
341 "\tcomp_id: Unique component ID to delete\n"
342 "\tcomp_flags: 'init' indicating all instantiated components\n"
343 "\t '^init' indicating all uninstantiated components\n"
344 "\t-I and -F cannot be specified at the same time\n"
345 "To create a file with a foreign (free format) layout:\n"
346 "usage: setstripe --foreign[=<foreign_type>]\n"
347 " --xattr|-x <layout_string> [--flags <hex>]\n"
348 " [--mode <mode>] <filename>\n"},
349 {"getstripe", lfs_getstripe, 0,
350 "To list the layout pattern for a given file or files in a\n"
351 "directory or recursively for all files in a directory tree.\n"
352 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
353 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
354 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
355 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
356 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
357 " [--component-id[=comp_id]|-I[comp_id]]\n"
358 " [--component-flags[=comp_flags]]\n"
359 " [--component-count]\n"
360 " [--component-start[=[+-]comp_start]]\n"
361 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
362 " [[!] --mirror-index=[+-]<index> |\n"
363 " [!] --mirror-id=[+-]<id>]\n"
364 " <directory|filename> ..."},
365 {"setdirstripe", lfs_setdirstripe, 0,
366 "To create a striped directory on a specified MDT. This can only\n"
367 "be done on MDT0 with the right of administrator.\n"
368 "usage: setdirstripe [OPTION] <directory>\n"
370 {"getdirstripe", lfs_getdirstripe, 0,
371 "To list the layout pattern info for a given directory\n"
372 "or recursively for all directories in a directory tree.\n"
373 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
374 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
375 " [--recursive|-r] [--yaml|-y]\n"
376 " [--default|-D] <dir> ..."},
377 {"mkdir", lfs_setdirstripe, 0,
378 "To create a striped directory on a specified MDT. This can only\n"
379 "be done on MDT0 with the right of administrator.\n"
380 "usage: mkdir [OPTION] <directory>\n"
382 {"rm_entry", lfs_rmentry, 0,
383 "To remove the name entry of the remote directory. Note: This\n"
384 "command will only delete the name entry, i.e. the remote directory\n"
385 "will become inaccessable after this command. This can only be done\n"
386 "by the administrator\n"
387 "usage: rm_entry <dir>\n"},
388 {"pool_list", lfs_poollist, 0,
389 "List pools or pool OSTs\n"
390 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
391 {"find", lfs_find, 0,
392 "find files matching given attributes recursively in directory tree.\n"
393 "usage: find <directory|filename> ...\n"
394 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
395 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
396 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
397 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
398 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
399 " [[!] --stripe-count|-c [+-]<stripes>]\n"
400 " [[!] --stripe-index|-i <index,...>]\n"
401 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
402 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
403 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
404 " [[!] --projid <projid>]\n"
405 " [[!] --layout|-L released,raid0,mdt]\n"
406 " [[!] --foreign[=<foreign_type>]]\n"
407 " [[!] --component-count [+-]<comp_cnt>]\n"
408 " [[!] --component-start [+-]N[kMGTPE]]\n"
409 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
410 " [[!] --component-flags <comp_flags>]\n"
411 " [[!] --mirror-count|-N [+-]<n>]\n"
412 " [[!] --mirror-state <[^]state>]\n"
413 " [[!] --mdt-count|-T [+-]<stripes>]\n"
414 " [[!] --mdt-hash|-H <hashtype>\n"
415 " [[!] --mdt-index|-m <uuid|index,...>]\n"
416 "\t !: used before an option indicates 'NOT' requested attribute\n"
417 "\t -: used before a value indicates less than requested value\n"
418 "\t +: used before a value indicates more than requested value\n"
419 "\thashtype: hash type of the striped directory.\n"
420 "\t fnv_1a_64 FNV-1a hash algorithm\n"
421 "\t all_char sum of characters % MDT_COUNT\n"},
422 {"check", lfs_check, 0,
423 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
424 "or all the servers (MGTs, MDTs and OSTs).\n"
425 "usage: check <mgts|osts|mdts|all>"},
426 {"osts", lfs_osts, 0, "list OSTs connected to client "
427 "[for specified path only]\n" "usage: osts [path]"},
428 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
429 "[for specified path only]\n" "usage: mdts [path]"},
431 "report filesystem disk space usage or inodes usage "
432 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
433 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
434 {"getname", lfs_getname, 0,
435 "list instances and specified mount points [for specified path only]\n"
436 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
437 #ifdef HAVE_SYS_QUOTA_H
438 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
439 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
440 " -b <block-softlimit> -B <block-hardlimit>\n"
441 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
442 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
443 " [--block-softlimit <block-softlimit>]\n"
444 " [--block-hardlimit <block-hardlimit>]\n"
445 " [--inode-softlimit <inode-softlimit>]\n"
446 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
447 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
448 " [--block-grace <block-grace>]\n"
449 " [--inode-grace <inode-grace>] <filesystem>\n"
450 " setquota <-U|-G|-P>\n"
451 " -b <block-softlimit> -B <block-hardlimit>\n"
452 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
453 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
454 " [--block-softlimit <block-softlimit>]\n"
455 " [--block-hardlimit <block-hardlimit>]\n"
456 " [--inode-softlimit <inode-softlimit>]\n"
457 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
458 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
460 " -b can be used instead of --block-softlimit/--block-grace\n"
461 " -B can be used instead of --block-hardlimit\n"
462 " -i can be used instead of --inode-softlimit/--inode-grace\n"
463 " -I can be used instead of --inode-hardlimit\n"
464 " -d can be used instead of --default\n\n"
465 "Note: The total quota space will be split into many qunits and\n"
466 " balanced over all server targets, the minimal qunit size is\n"
467 " 1M bytes for block space and 1K inodes for inode space.\n\n"
468 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
469 " Quota space rebalancing process will stop when this mininum\n"
470 " value is reached. As a result, quota exceeded can be returned\n"
471 " while many targets still have 1MB or 1K inodes of spare\n"
473 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
474 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
476 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
477 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
478 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
479 {"project", lfs_project, 0,
480 "Change or list project attribute for specified file or directory.\n"
481 "usage: project [-d|-r] <file|directory...>\n"
482 " list project ID and flags on file(s) or directories\n"
483 " project [-p id] [-s] [-r] <file|directory...>\n"
484 " set project ID and/or inherit flag for specified file(s) or directories\n"
485 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
486 " check project ID and flags on file(s) or directories, print outliers\n"
487 " project -C [-r] [-k] <file|directory...>\n"
488 " clear the project inherit flag and ID on the file or directory\n"
491 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
492 "usage: flushctx [-k] [mountpoint...]"},
493 {"changelog", lfs_changelog, 0,
494 "Show the metadata changes on an MDT."
495 "\nusage: changelog <mdtname> [startrec [endrec]]"},
496 {"changelog_clear", lfs_changelog_clear, 0,
497 "Indicate that old changelog records up to <endrec> are no longer of "
498 "interest to consumer <id>, allowing the system to free up space.\n"
499 "An <endrec> of 0 means all records.\n"
500 "usage: changelog_clear <mdtname> <id> <endrec>"},
501 {"fid2path", lfs_fid2path, 0,
502 "Resolve the full path(s) for given FID(s). For a specific hardlink "
503 "specify link number <linkno>.\n"
504 /* "For a historical link name, specify changelog record <recno>.\n" */
505 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
506 /* [ --rec <recno> ] */ },
507 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
508 "usage: path2fid [--parents] <path> ..."},
509 {"data_version", lfs_data_version, 0, "Display file data version for "
510 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
511 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
512 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
513 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
514 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
515 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
516 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
518 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
519 "[--archived] [--lost] <file> ..."},
520 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
521 "given files.\n" "usage: hsm_action <file> ..."},
522 {"hsm_archive", lfs_hsm_archive, 0,
523 "Archive file to external storage.\n"
524 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
526 {"hsm_restore", lfs_hsm_restore, 0,
527 "Restore file from external storage.\n"
528 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
529 {"hsm_release", lfs_hsm_release, 0,
530 "Release files from Lustre.\n"
531 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
532 {"hsm_remove", lfs_hsm_remove, 0,
533 "Remove file copy from external storage.\n"
534 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
536 " (FILE [FILE ...] | "
537 "--mntpath MOUNTPATH FID [FID ...])\n"
539 "Note: To remove an archived copy of a file already deleted from a "
541 "--mntpath option and a list of FIDs must be specified"
543 {"hsm_cancel", lfs_hsm_cancel, 0,
544 "Cancel requests related to specified files.\n"
545 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
546 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
547 "usage: swap_layouts <path1> <path2>"},
548 {"migrate", lfs_setstripe_migrate, 0,
549 "migrate a directory between MDTs.\n"
550 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
551 " [--mdt-hash|-H] <hash_type>\n"
552 " [--mdt-index|-m] <start_mdt_index>\n"
555 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
556 " it's the MDT index of first stripe\n"
557 "\tmdt_count: number of MDTs to stripe a directory over\n"
558 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
559 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
560 " all_char sum of characters % MDT_COUNT\n"
562 "migrate file objects from one OST "
563 "layout\nto another (may be not safe with concurent writes).\n"
565 "[--stripe-count|-c] <stripe_count>\n"
566 " [--stripe-index|-i] <start_ost_index>\n"
567 " [--stripe-size|-S] <stripe_size>\n"
568 " [--pool|-p] <pool_name>\n"
569 " [--ost|-o] <ost_indices>\n"
571 " [--non-block|-n]\n"
572 " [--non-direct|-D]\n"
573 " <file|directory>\n"
574 "\tstripe_count: number of OSTs to stripe a file over\n"
575 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
576 "\tstripe_size: number of bytes to store before moving to the next OST\n"
577 "\tpool_name: name of the predefined pool of OSTs\n"
578 "\tost_indices: OSTs to stripe over, in order\n"
579 "\tblock: Block file access during data migration (default)\n"
580 "\tnon-block: Abort migrations if concurrent access is detected\n"
581 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
583 "To move directories between MDTs. This command is deprecated, "
584 "use \"migrate\" instead.\n"
585 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
587 {"ladvise", lfs_ladvise, 0,
588 "Provide servers with advice about access patterns for a file.\n"
589 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
590 " [--background|-b] [--unset|-u]\n\n"
591 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
592 " {[--mode|-m [READ,WRITE]}\n"
594 {"mirror", lfs_mirror, mirror_cmdlist,
595 "lfs commands used to manage files with mirrored components:\n"
596 "lfs mirror create - create a mirrored file or directory\n"
597 "lfs mirror extend - add mirror(s) to an existing file\n"
598 "lfs mirror split - split a mirror from an existing mirrored file\n"
599 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
600 "lfs mirror read - read a mirror content of a mirrored file\n"
601 "lfs mirror write - write to a mirror of a mirrored file\n"
602 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
603 "lfs mirror verify - verify mirrored file(s)\n"},
604 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
605 "usage: getsom [-s] [-b] [-f] <path>\n"
606 "\t-s: Only show the size value of the SOM data for a given file\n"
607 "\t-b: Only show the blocks value of the SOM data for a given file\n"
608 "\t-f: Only show the flags value of the SOM data for a given file\n"},
609 {"heat_get", lfs_heat_get, 0,
610 "To get heat of files.\n"
611 "usage: heat_get <file> ...\n"},
612 {"heat_set", lfs_heat_set, 0,
613 "To set heat flags of files.\n"
614 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
615 "\t--clear|-c: Clear file heat for given files\n"
616 "\t--off|-o: Turn off file heat for given files\n"
617 "\t--on|-O: Turn on file heat for given files\n"},
618 {"help", Parser_help, 0, "help"},
619 {"exit", Parser_quit, 0, "quit"},
620 {"quit", Parser_quit, 0, "quit"},
621 {"--version", Parser_version, 0,
622 "output build version of the utility and exit"},
623 {"--list-commands", lfs_list_commands, 0,
624 "list commands supported by the utility and exit"},
629 static int check_hashtype(const char *hashtype)
633 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
634 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
640 static uint32_t lov_check_foreign_type_name(const char *foreign_type_name)
644 for (i = 0; i < LOV_FOREIGN_TYPE_UNKNOWN; i++) {
645 if (lov_foreign_type[i].lft_name == NULL)
647 if (strcmp(foreign_type_name,
648 lov_foreign_type[i].lft_name) == 0)
649 return lov_foreign_type[i].lft_type;
652 return LOV_FOREIGN_TYPE_UNKNOWN;
655 static const char *error_loc = "syserror";
658 MIGRATION_NONBLOCK = 0x0001,
659 MIGRATION_MIRROR = 0x0002,
660 MIGRATION_NONDIRECT = 0x0004,
661 MIGRATION_VERBOSE = 0x0008,
664 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
665 struct llapi_layout *layout);
668 migrate_open_files(const char *name, __u64 migration_flags,
669 const struct llapi_stripe_param *param,
670 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
677 char parent[PATH_MAX];
678 char volatile_file[PATH_MAX];
684 if (param == NULL && layout == NULL) {
685 error_loc = "layout information";
689 /* search for file directory pathname */
690 if (strlen(name) > sizeof(parent) - 1) {
691 error_loc = "source file name";
695 strncpy(parent, name, sizeof(parent));
696 ptr = strrchr(parent, '/');
698 if (getcwd(parent, sizeof(parent)) == NULL) {
699 error_loc = "getcwd";
703 if (ptr == parent) /* leading '/' */
708 /* open file, direct io */
709 /* even if the file is only read, WR mode is nedeed to allow
710 * layout swap on fd */
712 if (!(migration_flags & MIGRATION_NONDIRECT))
714 fd = open(name, rflags);
717 error_loc = "cannot open source file";
721 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
723 error_loc = "cannot get MDT index";
728 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
729 mode_t open_mode = S_IRUSR | S_IWUSR;
731 random_value = random();
732 rc = snprintf(volatile_file, sizeof(volatile_file),
733 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
734 mdt_index, random_value);
735 if (rc >= sizeof(volatile_file)) {
740 /* create, open a volatile file, use caching (ie no directio) */
742 fdv = llapi_file_open_param(volatile_file, open_flags,
745 fdv = lfs_component_create(volatile_file, open_flags,
747 } while (fdv < 0 && (rc = fdv) == -EEXIST);
750 error_loc = "cannot create volatile file";
754 /* In case the MDT does not support creation of volatile files
755 * we should try to unlink it. */
756 (void)unlink(volatile_file);
758 /* Not-owner (root?) special case.
759 * Need to set owner/group of volatile file like original.
760 * This will allow to pass related check during layout_swap.
765 error_loc = "cannot stat source file";
769 rc = fstat(fdv, &stv);
772 error_loc = "cannot stat volatile";
776 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
777 rc = fchown(fdv, st.st_uid, st.st_gid);
780 error_loc = "cannot change ownwership of volatile";
799 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
801 struct llapi_layout *layout;
802 size_t buf_size = 4 * 1024 * 1024;
811 layout = llapi_layout_get_by_fd(fd_src, 0);
812 if (layout != NULL) {
813 uint64_t stripe_size;
815 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
817 buf_size = stripe_size;
819 llapi_layout_free(layout);
822 /* Use a page-aligned buffer for direct I/O */
823 rc = posix_memalign(&buf, getpagesize(), buf_size);
828 /* read new data only if we have written all
829 * previously read data */
832 rc = check_file(fd_src);
837 rsize = read(fd_src, buf, buf_size);
849 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
868 static int migrate_copy_timestamps(int fd, int fdv)
872 if (fstat(fd, &st) == 0) {
873 struct timeval tv[2] = {
874 {.tv_sec = st.st_atime},
875 {.tv_sec = st.st_mtime}
878 return futimes(fdv, tv);
884 static int migrate_block(int fd, int fdv)
891 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
893 error_loc = "cannot get dataversion";
901 /* The grouplock blocks all concurrent accesses to the file.
902 * It has to be taken after llapi_get_data_version as it would
904 rc = llapi_group_lock(fd, gid);
906 error_loc = "cannot get group lock";
910 rc = migrate_copy_data(fd, fdv, NULL);
912 error_loc = "data copy failed";
916 /* Make sure we keep original atime/mtime values */
917 rc = migrate_copy_timestamps(fd, fdv);
919 error_loc = "timestamp copy failed";
924 * for a migration we need to check data version on file did
927 * Pass in gid=0 since we already own grouplock. */
928 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
929 SWAP_LAYOUTS_CHECK_DV1);
931 error_loc = "file changed";
934 error_loc = "cannot swap layout";
939 rc2 = llapi_group_unlock(fd, gid);
940 if (rc2 < 0 && rc == 0) {
941 error_loc = "unlock group lock";
949 * Internal helper for migrate_copy_data(). Check lease and report error if
952 * \param[in] fd File descriptor on which to check the lease.
954 * \retval 0 Migration can keep on going.
955 * \retval -errno Error occurred, abort migration.
957 static int check_lease(int fd)
961 rc = llapi_lease_check(fd);
963 return 0; /* llapi_check_lease returns > 0 on success. */
968 static int migrate_nonblock(int fd, int fdv)
974 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
976 error_loc = "cannot get data version";
980 rc = migrate_copy_data(fd, fdv, check_lease);
982 error_loc = "data copy failed";
986 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
988 error_loc = "cannot get data version";
994 error_loc = "source file changed";
998 /* Make sure we keep original atime/mtime values */
999 rc = migrate_copy_timestamps(fd, fdv);
1001 error_loc = "timestamp copy failed";
1008 static int lfs_component_set(char *fname, int comp_id,
1009 __u32 flags, __u32 neg_flags)
1012 __u32 flags_array[2];
1017 ids[count] = comp_id;
1018 flags_array[count] = flags;
1023 ids[count] = comp_id;
1024 flags_array[count] = neg_flags | LCME_FL_NEG;
1028 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1031 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1032 progname, comp_id, fname, flags, neg_flags);
1037 static int lfs_component_del(char *fname, __u32 comp_id,
1038 __u32 flags, __u32 neg_flags)
1042 if (flags && neg_flags)
1045 if (!flags && neg_flags)
1046 flags = neg_flags | LCME_FL_NEG;
1048 if ((flags && comp_id) || (!flags && !comp_id))
1051 /* LCME_FL_INIT is the only supported flag in PFL */
1053 if (flags & ~LCME_KNOWN_FLAGS) {
1055 "%s setstripe: unknown flags %#x\n",
1059 } else if (comp_id > LCME_ID_MAX) {
1060 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1065 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1068 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1069 progname, comp_id, fname, strerror(errno));
1073 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1080 rc = llapi_layout_file_comp_add(fname, layout);
1082 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1083 fname, strerror(errno));
1087 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1088 struct llapi_layout *layout)
1096 fd = lstat(fname, &st);
1097 if (fd == 0 && S_ISDIR(st.st_mode))
1098 open_flags = O_DIRECTORY | O_RDONLY;
1100 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1102 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1103 S_ISDIR(st.st_mode) ?
1104 "set default composite layout for" :
1105 "create composite file",
1106 fname, strerror(errno));
1110 static int lfs_migrate(char *name, __u64 migration_flags,
1111 struct llapi_stripe_param *param,
1112 struct llapi_layout *layout)
1118 rc = migrate_open_files(name, migration_flags, param, layout,
1123 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1124 /* Blocking mode (forced if servers do not support file lease).
1125 * It is also the default mode, since we cannot distinguish
1126 * between a broken lease and a server that does not support
1127 * atomic swap/close (LU-6785) */
1128 rc = migrate_block(fd, fdv);
1132 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1134 error_loc = "cannot get lease";
1138 rc = migrate_nonblock(fd, fdv);
1140 llapi_lease_release(fd);
1144 /* Atomically put lease, swap layouts and close.
1145 * for a migration we need to check data version on file did
1147 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1149 error_loc = "cannot swap layout";
1161 fprintf(stderr, "error: %s: %s: %s: %s\n",
1162 progname, name, error_loc, strerror(-rc));
1163 else if (migration_flags & MIGRATION_VERBOSE)
1164 printf("%s\n", name);
1169 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1178 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1182 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1183 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1184 const char *comp_name = comp_flags_table[i].cfn_name;
1186 if (strcmp(name, comp_name) == 0) {
1187 *flags |= comp_flag;
1189 } else if (strncmp(name, "^", 1) == 0 &&
1190 strcmp(name + 1, comp_name) == 0) {
1191 *neg_flags |= comp_flag;
1196 llapi_printf(LLAPI_MSG_ERROR,
1197 "%s: component flag '%s' not supported\n",
1203 if (!*flags && !*neg_flags)
1206 /* don't allow to set and exclude the same flag */
1207 if (*flags & *neg_flags)
1213 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1221 if (strncmp(string, "^", 1) == 0) {
1222 *neg_state = llapi_layout_string_flags(string + 1);
1223 if (*neg_state != 0)
1226 *state = llapi_layout_string_flags(string);
1231 llapi_printf(LLAPI_MSG_ERROR,
1232 "%s: mirrored file state '%s' not supported\n",
1238 * struct mirror_args - Command-line arguments for mirror(s).
1239 * @m_count: Number of mirrors to be created with this layout.
1240 * @m_flags: Mirror level flags, only 'prefer' is supported.
1241 * @m_layout: Mirror layout.
1242 * @m_file: A victim file. Its layout will be split and used as a mirror.
1243 * @m_next: Point to the next node of the list.
1245 * Command-line arguments for mirror(s) will be parsed and stored in
1246 * a linked list that consists of this structure.
1248 struct mirror_args {
1251 struct llapi_layout *m_layout;
1253 struct mirror_args *m_next;
1256 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1261 rc = llapi_layout_comp_flags_get(layout, &flags);
1265 if (flags & LCME_FL_NEG) {
1266 fprintf(stderr, "error: %s: negative flags are not supported\n",
1271 if (flags & LCME_FL_STALE) {
1272 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1273 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1277 return LLAPI_LAYOUT_ITER_CONT;
1280 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1282 uint64_t start, end;
1286 /* LU-10112: do not support dom+flr in phase 1 */
1287 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1291 rc = llapi_layout_pattern_get(layout, &pattern);
1295 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1296 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1301 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1305 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1309 if (end != LUSTRE_EOF) {
1310 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1315 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1320 * enum mirror_flags - Flags for extending a mirrored file.
1321 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1322 * in case the victim file(s) contains the same data as the
1323 * original mirrored file.
1324 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1325 * @MF_COMP_ID: specified component id instead of mirror id
1327 * Flags for extending a mirrored file.
1336 * mirror_create_sanity_check() - Check mirror list.
1337 * @list: A linked list that stores the mirror arguments.
1339 * This function does a sanity check on @list for creating
1342 * Return: 0 on success or a negative error code on failure.
1344 static int mirror_create_sanity_check(const char *fname,
1345 struct mirror_args *list)
1348 bool has_m_file = false;
1349 bool has_m_layout = false;
1355 struct llapi_layout *layout;
1357 layout = llapi_layout_get_by_path(fname, 0);
1360 "error: %s: file '%s' couldn't get layout\n",
1365 rc = mirror_sanity_check_one(layout);
1366 llapi_layout_free(layout);
1372 while (list != NULL) {
1373 if (list->m_file != NULL) {
1375 llapi_layout_free(list->m_layout);
1378 llapi_layout_get_by_path(list->m_file, 0);
1379 if (list->m_layout == NULL) {
1381 "error: %s: file '%s' has no layout\n",
1382 progname, list->m_file);
1386 has_m_layout = true;
1387 if (list->m_layout == NULL) {
1388 fprintf(stderr, "error: %s: no mirror layout\n",
1394 rc = mirror_sanity_check_one(list->m_layout);
1398 list = list->m_next;
1401 if (has_m_file && has_m_layout) {
1403 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1411 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1413 __u32 mirror_flags = *(__u32 *)cbdata;
1417 rc = llapi_layout_comp_flags_get(layout, &flags);
1422 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1427 return LLAPI_LAYOUT_ITER_CONT;
1431 * mirror_create() - Create a mirrored file.
1432 * @fname: The file to be created.
1433 * @mirror_list: A linked list that stores the mirror arguments.
1435 * This function creates a mirrored file @fname with the mirror(s)
1436 * from @mirror_list.
1438 * Return: 0 on success or a negative error code on failure.
1440 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1442 struct llapi_layout *layout = NULL;
1443 struct mirror_args *cur_mirror = NULL;
1444 uint16_t mirror_count = 0;
1448 rc = mirror_create_sanity_check(NULL, mirror_list);
1452 cur_mirror = mirror_list;
1453 while (cur_mirror != NULL) {
1454 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1456 &cur_mirror->m_flags);
1459 fprintf(stderr, "%s: failed to set mirror flags\n",
1464 for (i = 0; i < cur_mirror->m_count; i++) {
1465 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1468 fprintf(stderr, "error: %s: "
1469 "merge layout failed: %s\n",
1470 progname, strerror(errno));
1474 mirror_count += cur_mirror->m_count;
1475 cur_mirror = cur_mirror->m_next;
1478 if (layout == NULL) {
1479 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1483 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1486 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1487 progname, strerror(errno));
1491 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1499 llapi_layout_free(layout);
1504 * Compare files and check lease on @fd.
1506 * \retval bytes number of bytes are the same
1508 static ssize_t mirror_file_compare(int fd, int fdv)
1510 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1512 ssize_t bytes_done = 0;
1513 ssize_t bytes_read = 0;
1515 buf = malloc(buflen * 2);
1520 if (!llapi_lease_check(fd)) {
1521 bytes_done = -EBUSY;
1525 bytes_read = read(fd, buf, buflen);
1526 if (bytes_read <= 0)
1529 if (bytes_read != read(fdv, buf + buflen, buflen))
1532 /* XXX: should compute the checksum on each buffer and then
1533 * compare checksum to avoid cache collision */
1534 if (memcmp(buf, buf + buflen, bytes_read))
1537 bytes_done += bytes_read;
1545 static int mirror_extend_file(const char *fname, const char *victim_file,
1546 enum mirror_flags mirror_flags)
1551 struct stat stbuf_v;
1552 struct ll_ioc_lease *data = NULL;
1555 fd = open(fname, O_RDWR);
1557 error_loc = "open source file";
1562 fdv = open(victim_file, O_RDWR);
1564 error_loc = "open target file";
1569 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1570 error_loc = "stat source or target file";
1575 if (stbuf.st_dev != stbuf_v.st_dev) {
1576 error_loc = "stat source and target file";
1581 /* mirrors should be of the same size */
1582 if (stbuf.st_size != stbuf_v.st_size) {
1583 error_loc = "file sizes don't match";
1588 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1590 error_loc = "cannot get lease";
1594 if (!(mirror_flags & MF_NO_VERIFY)) {
1596 /* mirrors should have the same contents */
1597 ret = mirror_file_compare(fd, fdv);
1598 if (ret != stbuf.st_size) {
1599 error_loc = "file busy or contents don't match";
1600 rc = ret < 0 ? ret : -EINVAL;
1605 /* Get rid of caching pages from clients */
1606 rc = llapi_file_flush(fd);
1608 error_loc = "cannot get data version";
1612 rc = llapi_file_flush(fdv);
1614 error_loc = "cannot get data version";
1619 /* Make sure we keep original atime/mtime values */
1620 rc = migrate_copy_timestamps(fd, fdv);
1622 error_loc = "cannot copy timestamp";
1626 /* Atomically put lease, merge layouts and close. */
1627 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1629 error_loc = "memory allocation";
1632 data->lil_mode = LL_LEASE_UNLCK;
1633 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1634 data->lil_count = 1;
1635 data->lil_ids[0] = fdv;
1636 rc = llapi_lease_set(fd, data);
1638 error_loc = "cannot merge layout";
1640 } else if (rc == 0) {
1642 error_loc = "lost lease lock";
1655 (void) unlink(victim_file);
1657 fprintf(stderr, "error: %s: %s: %s: %s\n",
1658 progname, fname, error_loc, strerror(-rc));
1662 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1664 struct ll_ioc_lease *data = NULL;
1669 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1673 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1675 error_loc = "cannot get lease";
1679 rc = migrate_nonblock(fd, fdv);
1681 llapi_lease_release(fd);
1685 /* Atomically put lease, merge layouts and close. */
1686 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1688 error_loc = "memory allocation";
1691 data->lil_mode = LL_LEASE_UNLCK;
1692 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1693 data->lil_count = 1;
1694 data->lil_ids[0] = fdv;
1695 rc = llapi_lease_set(fd, data);
1697 error_loc = "cannot merge layout";
1699 } else if (rc == 0) {
1701 error_loc = "lost lease lock";
1714 fprintf(stderr, "error: %s: %s: %s: %s\n",
1715 progname, name, error_loc, strerror(-rc));
1719 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1720 enum mirror_flags mirror_flags)
1724 rc = mirror_create_sanity_check(fname, mirror_list);
1728 while (mirror_list) {
1729 if (mirror_list->m_file != NULL) {
1730 rc = mirror_extend_file(fname, mirror_list->m_file,
1733 __u32 mirror_count = mirror_list->m_count;
1735 while (mirror_count > 0) {
1736 rc = mirror_extend_layout(fname,
1737 mirror_list->m_layout);
1747 mirror_list = mirror_list->m_next;
1753 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1758 rc = llapi_layout_mirror_id_get(layout, &id);
1762 if ((__u16)id == *(__u16 *)cbdata)
1763 return LLAPI_LAYOUT_ITER_STOP;
1765 return LLAPI_LAYOUT_ITER_CONT;
1768 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1773 rc = llapi_layout_comp_id_get(layout, &id);
1777 if (id == *(__u32 *)cbdata)
1778 return LLAPI_LAYOUT_ITER_STOP;
1780 return LLAPI_LAYOUT_ITER_CONT;
1782 static int mirror_split(const char *fname, __u32 id,
1783 enum mirror_flags mflags, const char *victim_file)
1785 struct llapi_layout *layout;
1786 char parent[PATH_MAX];
1787 char victim[PATH_MAX];
1788 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1790 struct ll_ioc_lease *data;
1791 uint16_t mirror_count;
1796 /* check fname contains mirror with mirror_id/comp_id */
1797 layout = llapi_layout_get_by_path(fname, 0);
1800 "error %s: file '%s' couldn't get layout\n",
1805 rc = mirror_sanity_check_one(layout);
1809 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1812 "error %s: file '%s' couldn't get mirror count\n",
1816 if (mirror_count < 2) {
1818 "error %s: file '%s' has %d component, cannot split\n",
1819 progname, fname, mirror_count);
1823 if (mflags & MF_COMP_ID) {
1824 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1825 id = mirror_id_of(id);
1827 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1830 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1833 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1835 "error %s: file '%s' does not contain mirror with id %u\n",
1836 progname, fname, id);
1840 fd = open(fname, O_RDWR);
1843 "error %s: open file '%s' failed: %s\n",
1844 progname, fname, strerror(errno));
1848 /* get victim file directory pathname */
1849 if (strlen(fname) > sizeof(parent) - 1) {
1850 fprintf(stderr, "error %s: file name of '%s' too long\n",
1855 strncpy(parent, fname, sizeof(parent));
1856 ptr = strrchr(parent, '/');
1858 if (getcwd(parent, sizeof(parent)) == NULL) {
1859 fprintf(stderr, "error %s: getcwd failed: %s\n",
1860 progname, strerror(errno));
1870 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1872 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1877 if (victim_file == NULL) {
1878 /* use a temp file to store the splitted layout */
1879 if (mflags & MF_DESTROY) {
1880 fdv = llapi_create_volatile_idx(parent, mdt_index,
1881 O_LOV_DELAY_CREATE);
1883 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1885 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1888 /* user specified victim file */
1889 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1894 "error %s: create victim file failed: %s\n",
1895 progname, strerror(errno));
1899 /* get lease lock of fname */
1900 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1903 "error %s: cannot get lease of file '%s': %d\n",
1904 progname, fname, rc);
1908 /* Atomatically put lease, split layouts and close. */
1909 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1915 data->lil_mode = LL_LEASE_UNLCK;
1916 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1917 data->lil_count = 2;
1918 data->lil_ids[0] = fdv;
1919 data->lil_ids[1] = id;
1920 rc = llapi_lease_set(fd, data);
1922 if (rc == 0) /* lost lease lock */
1925 "error %s: cannot split '%s': %s\n",
1926 progname, fname, strerror(-rc));
1937 llapi_layout_free(layout);
1942 * Parse a string containing an target index list into an array of integers.
1944 * The input string contains a comma delimited list of individual
1945 * indices and ranges, for example "1,2-4,7". Add the indices into the
1946 * \a tgts array and remove duplicates.
1948 * \param[out] tgts array to store indices in
1949 * \param[in] size size of \a tgts array
1950 * \param[in] offset starting index in \a tgts
1951 * \param[in] arg string containing OST index list
1953 * \retval positive number of indices in \a tgts
1954 * \retval -EINVAL unable to parse \a arg
1956 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1960 int slots = size - offset;
1967 end_of_loop = false;
1968 while (!end_of_loop) {
1972 char *endptr = NULL;
1976 ptr = strchrnul(arg, ',');
1978 end_of_loop = *ptr == '\0';
1981 start_index = strtol(arg, &endptr, 0);
1982 if (endptr == arg) /* no data at all */
1984 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1987 end_index = start_index;
1988 if (*endptr == '-') {
1989 end_index = strtol(endptr + 1, &endptr, 0);
1990 if (*endptr != '\0')
1992 if (end_index < start_index)
1996 for (i = start_index; i <= end_index && slots > 0; i++) {
1999 /* remove duplicate */
2000 for (j = 0; j < offset; j++) {
2004 if (j == offset) { /* no duplicate */
2009 if (slots == 0 && i < end_index)
2017 if (!end_of_loop && ptr != NULL)
2020 return rc < 0 ? rc : nr;
2023 struct lfs_setstripe_args {
2024 unsigned long long lsa_comp_end;
2025 unsigned long long lsa_stripe_size;
2026 long long lsa_stripe_count;
2027 long long lsa_stripe_off;
2028 __u32 lsa_comp_flags;
2029 __u32 lsa_comp_neg_flags;
2030 unsigned long long lsa_pattern;
2031 unsigned int lsa_mirror_count;
2033 bool lsa_first_comp;
2035 char *lsa_pool_name;
2038 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2040 unsigned int mirror_count = lsa->lsa_mirror_count;
2041 bool first_comp = lsa->lsa_first_comp;
2043 memset(lsa, 0, sizeof(*lsa));
2045 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2046 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2047 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2048 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2049 lsa->lsa_pool_name = NULL;
2051 lsa->lsa_mirror_count = mirror_count;
2052 lsa->lsa_first_comp = first_comp;
2056 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2057 * @lsa: Stripe options to be initialized and inherited.
2059 * This function initializes stripe options in @lsa and inherit
2060 * stripe_size, stripe_count and OST pool_name options.
2064 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2066 unsigned long long stripe_size;
2067 long long stripe_count;
2068 char *pool_name = NULL;
2070 stripe_size = lsa->lsa_stripe_size;
2071 stripe_count = lsa->lsa_stripe_count;
2072 pool_name = lsa->lsa_pool_name;
2074 setstripe_args_init(lsa);
2076 lsa->lsa_stripe_size = stripe_size;
2077 lsa->lsa_stripe_count = stripe_count;
2078 lsa->lsa_pool_name = pool_name;
2081 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2083 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2084 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2085 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2086 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2087 lsa->lsa_pool_name != NULL ||
2088 lsa->lsa_comp_end != 0);
2092 * comp_args_to_layout() - Create or extend a composite layout.
2093 * @composite: Pointer to the composite layout.
2094 * @lsa: Stripe options for the new component.
2096 * This function creates or extends a composite layout by adding a new
2097 * component with stripe options from @lsa.
2099 * Return: 0 on success or an error code on failure.
2101 static int comp_args_to_layout(struct llapi_layout **composite,
2102 struct lfs_setstripe_args *lsa,
2105 struct llapi_layout *layout = *composite;
2106 uint64_t prev_end = 0;
2109 if (layout == NULL) {
2110 layout = llapi_layout_alloc();
2111 if (layout == NULL) {
2112 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2116 *composite = layout;
2120 /* Get current component extent, current component
2121 * must be the tail component. */
2122 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2124 fprintf(stderr, "Get comp extent failed. %s\n",
2129 if (lsa->lsa_first_comp)
2132 if (lsa->lsa_first_comp)
2133 rc = llapi_layout_add_first_comp(layout);
2135 rc = llapi_layout_comp_add(layout);
2137 fprintf(stderr, "Add component failed. %s\n",
2142 /* reset lsa_first_comp */
2143 lsa->lsa_first_comp = false;
2146 rc = llapi_layout_comp_extent_set(layout, prev_end,
2149 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2150 prev_end, lsa->lsa_comp_end, strerror(errno));
2155 /* Data-on-MDT component setting */
2156 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2157 /* In case of Data-on-MDT patterns the only extra option
2158 * applicable is stripe size option. */
2159 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2160 fprintf(stderr, "Option 'stripe-count' can't be "
2161 "specified with Data-on-MDT component: %lld\n",
2162 lsa->lsa_stripe_count);
2165 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2166 fprintf(stderr, "Option 'stripe-size' can't be "
2167 "specified with Data-on-MDT component: %llu\n",
2168 lsa->lsa_stripe_size);
2171 if (lsa->lsa_nr_tgts != 0) {
2172 fprintf(stderr, "Option 'ost-list' can't be specified "
2173 "with Data-on-MDT component: '%i'\n",
2177 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2178 fprintf(stderr, "Option 'stripe-offset' can't be "
2179 "specified with Data-on-MDT component: %lld\n",
2180 lsa->lsa_stripe_off);
2183 if (lsa->lsa_pool_name != 0) {
2184 fprintf(stderr, "Option 'pool' can't be specified "
2185 "with Data-on-MDT component: '%s'\n",
2186 lsa->lsa_pool_name);
2190 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2192 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2193 lsa->lsa_pattern, strerror(errno));
2196 /* Data-on-MDT component has always single stripe up to end */
2197 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2200 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2202 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2203 lsa->lsa_stripe_size, strerror(errno));
2207 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2209 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2210 lsa->lsa_stripe_count, strerror(errno));
2214 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2216 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2217 lsa->lsa_comp_flags, strerror(errno));
2221 if (lsa->lsa_pool_name != NULL) {
2222 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2224 fprintf(stderr, "Set pool name: %s failed. %s\n",
2225 lsa->lsa_pool_name, strerror(errno));
2229 rc = llapi_layout_pool_name_set(layout, "");
2231 fprintf(stderr, "Clear pool name failed: %s\n",
2237 if (lsa->lsa_nr_tgts > 0) {
2238 if (lsa->lsa_stripe_count > 0 &&
2239 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2240 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2241 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2242 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2243 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2246 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2247 rc = llapi_layout_ost_index_set(layout, i,
2252 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2253 lsa->lsa_stripe_off != -1) {
2254 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2257 fprintf(stderr, "Set ost index %d failed. %s\n",
2258 i, strerror(errno));
2265 static int build_component(struct llapi_layout **layout,
2266 struct lfs_setstripe_args *lsa, bool set_extent)
2270 rc = comp_args_to_layout(layout, lsa, set_extent);
2274 if (lsa->lsa_mirror_count > 0) {
2275 rc = llapi_layout_mirror_count_set(*layout,
2276 lsa->lsa_mirror_count);
2280 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2283 lsa->lsa_mirror_count = 0;
2289 static int build_layout_from_yaml_node(struct cYAML *node,
2290 struct llapi_layout **layout,
2291 struct lfs_setstripe_args *lsa,
2298 if (node->cy_type == CYAML_TYPE_OBJECT) {
2299 /* go deep to sub blocks */
2300 rc = build_layout_from_yaml_node(node->cy_child, layout,
2305 if (node->cy_string == NULL)
2308 string = node->cy_string;
2309 /* skip leading lmm_ if present, to simplify parsing */
2310 if (strncmp(string, "lmm_", 4) == 0)
2313 if (node->cy_type == CYAML_TYPE_STRING) {
2314 if (!strcmp(string, "lcme_extent.e_end")) {
2315 if (!strcmp(node->cy_valuestring, "EOF") ||
2316 !strcmp(node->cy_valuestring, "eof"))
2317 lsa->lsa_comp_end = LUSTRE_EOF;
2318 } else if (!strcmp(string, "pool")) {
2319 lsa->lsa_pool_name = node->cy_valuestring;
2320 } else if (!strcmp(string, "pattern")) {
2321 if (!strcmp(node->cy_valuestring, "mdt"))
2322 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2323 } else if (!strcmp(string, "lcme_flags")) {
2324 rc = comp_str2flags(node->cy_valuestring,
2325 &lsa->lsa_comp_flags,
2326 &lsa->lsa_comp_neg_flags);
2329 /* Only template flags have meaning in
2330 * the layout for a new file
2332 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2334 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2335 if (!strcmp(string, "lcm_mirror_count")) {
2336 lsa->lsa_mirror_count = node->cy_valueint;
2337 } else if (!strcmp(string, "lcme_extent.e_start")) {
2338 if (node->cy_valueint != 0 || *layout != NULL) {
2339 rc = build_component(layout, lsa, true);
2344 if (node->cy_valueint == 0)
2345 lsa->lsa_first_comp = true;
2347 /* initialize lsa */
2348 setstripe_args_init(lsa);
2349 lsa->lsa_tgts = osts;
2350 } else if (!strcmp(string, "lcme_extent.e_end")) {
2351 if (node->cy_valueint == -1)
2352 lsa->lsa_comp_end = LUSTRE_EOF;
2354 lsa->lsa_comp_end = node->cy_valueint;
2355 } else if (!strcmp(string, "stripe_count")) {
2356 lsa->lsa_stripe_count = node->cy_valueint;
2357 } else if (!strcmp(string, "stripe_size")) {
2358 lsa->lsa_stripe_size = node->cy_valueint;
2359 } else if (!strcmp(string, "stripe_offset")) {
2360 lsa->lsa_stripe_off = node->cy_valueint;
2361 } else if (!strcmp(string, "l_ost_idx")) {
2362 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2367 node = node->cy_next;
2373 static int lfs_comp_create_from_yaml(char *template,
2374 struct llapi_layout **layout,
2375 struct lfs_setstripe_args *lsa,
2378 struct cYAML *tree = NULL, *err_rc = NULL;
2381 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2383 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2384 progname, template);
2385 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2386 "can't parse", &err_rc);
2387 cYAML_print_tree2file(stderr, err_rc);
2388 cYAML_free_tree(err_rc);
2393 /* initialize lsa for plain file */
2394 setstripe_args_init(lsa);
2395 lsa->lsa_tgts = osts;
2397 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2399 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2400 progname, template);
2403 rc = build_component(layout, lsa, *layout != NULL);
2405 /* clean clean lsa */
2406 setstripe_args_init(lsa);
2410 cYAML_free_tree(tree);
2414 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2415 * end of the last component in the existing file, and adjust the
2416 * first extent start of the components to be added accordingly. */
2417 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2419 struct llapi_layout *head;
2420 uint64_t start, end, stripe_size, prev_end = 0;
2423 if (layout == NULL) {
2425 "%s setstripe: layout must be specified\n",
2431 head = llapi_layout_get_by_path(fname, 0);
2434 "%s setstripe: cannot read layout from '%s': %s\n",
2435 progname, fname, strerror(errno));
2437 } else if (errno == ENODATA) {
2438 /* file without LOVEA, this component-add will be turned
2439 * into a component-create. */
2440 llapi_layout_free(head);
2442 } else if (!llapi_layout_is_composite(head)) {
2443 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2445 llapi_layout_free(head);
2449 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2451 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2452 progname, strerror(errno));
2453 llapi_layout_free(head);
2457 llapi_layout_free(head);
2459 /* Make sure we use the first component of the layout to be added. */
2460 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2463 "%s setstripe: cannot move component cursor: %s\n",
2464 progname, strerror(errno));
2468 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2470 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2471 progname, strerror(errno));
2475 if (start > prev_end || end <= prev_end) {
2477 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2478 progname, start, end, prev_end);
2482 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2484 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2485 progname, strerror(errno));
2489 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2490 (prev_end & (stripe_size - 1))) {
2492 "%s setstripe: stripe size %lu not aligned with %lu\n",
2493 progname, stripe_size, prev_end);
2497 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2500 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2501 progname, prev_end, end, strerror(errno));
2508 static inline bool arg_is_eof(char *arg)
2510 return !strncmp(arg, "-1", strlen("-1")) ||
2511 !strncmp(arg, "EOF", strlen("EOF")) ||
2512 !strncmp(arg, "eof", strlen("eof"));
2516 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2518 * Return: Valid mirror_args pointer on success and
2519 * NULL if memory allocation fails.
2521 static struct mirror_args *lfs_mirror_alloc(void)
2523 struct mirror_args *mirror = NULL;
2526 mirror = calloc(1, sizeof(*mirror));
2537 * lfs_mirror_free() - Free memory allocated for a mirror argument
2539 * @mirror: Previously allocated mirror argument structure by
2540 * lfs_mirror_alloc().
2542 * Free memory allocated for @mirror.
2546 static void lfs_mirror_free(struct mirror_args *mirror)
2548 if (mirror->m_layout != NULL)
2549 llapi_layout_free(mirror->m_layout);
2554 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2555 * @mirror_list: Previously allocated mirror list.
2557 * Free memory allocated for @mirror_list.
2561 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2563 struct mirror_args *next_mirror = NULL;
2565 while (mirror_list != NULL) {
2566 next_mirror = mirror_list->m_next;
2567 lfs_mirror_free(mirror_list);
2568 mirror_list = next_mirror;
2580 LFS_COMP_NO_VERIFY_OPT,
2582 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2584 LFS_MIRROR_STATE_OPT,
2586 LFS_MIRROR_INDEX_OPT,
2587 LFS_LAYOUT_FOREIGN_OPT,
2592 static int lfs_setstripe_internal(int argc, char **argv,
2593 enum setstripe_origin opc)
2595 struct lfs_setstripe_args lsa = { 0 };
2596 struct llapi_stripe_param *param = NULL;
2597 struct find_param migrate_mdt_param = {
2607 unsigned long long size_units = 1;
2608 bool migrate_mode = false;
2609 bool migrate_mdt_mode = false;
2610 bool setstripe_mode = false;
2611 bool migration_block = false;
2612 __u64 migration_flags = 0;
2613 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2614 int comp_del = 0, comp_set = 0;
2617 struct llapi_layout *layout = NULL;
2618 struct llapi_layout **lpp = &layout;
2619 bool mirror_mode = false;
2620 bool has_m_file = false;
2621 __u32 mirror_count = 0;
2622 enum mirror_flags mirror_flags = 0;
2623 struct mirror_args *mirror_list = NULL;
2624 struct mirror_args *new_mirror = NULL;
2625 struct mirror_args *last_mirror = NULL;
2626 __u16 mirror_id = 0;
2628 bool from_yaml = false;
2629 bool from_copy = false;
2630 char *template = NULL;
2631 bool foreign_mode = false;
2633 uint32_t type = LOV_FOREIGN_TYPE_NONE, flags = 0;
2634 char *mode_opt = NULL;
2635 mode_t previous_umask = 0;
2636 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2638 struct option long_opts[] = {
2639 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2640 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2641 /* --block is only valid in migrate mode */
2642 { .val = 'b', .name = "block", .has_arg = no_argument },
2643 { .val = LFS_COMP_ADD_OPT,
2644 .name = "comp-add", .has_arg = no_argument },
2645 { .val = LFS_COMP_ADD_OPT,
2646 .name = "component-add", .has_arg = no_argument },
2647 { .val = LFS_COMP_DEL_OPT,
2648 .name = "comp-del", .has_arg = no_argument },
2649 { .val = LFS_COMP_DEL_OPT,
2650 .name = "component-del", .has_arg = no_argument },
2651 { .val = LFS_COMP_FLAGS_OPT,
2652 .name = "comp-flags", .has_arg = required_argument },
2653 { .val = LFS_COMP_FLAGS_OPT,
2654 .name = "component-flags",
2655 .has_arg = required_argument },
2656 { .val = LFS_COMP_SET_OPT,
2657 .name = "comp-set", .has_arg = no_argument },
2658 { .val = LFS_COMP_SET_OPT,
2659 .name = "component-set",
2660 .has_arg = no_argument},
2661 { .val = LFS_COMP_NO_VERIFY_OPT,
2662 .name = "no-verify", .has_arg = no_argument},
2663 { .val = LFS_LAYOUT_FLAGS_OPT,
2664 .name = "flags", .has_arg = required_argument},
2665 { .val = LFS_LAYOUT_FOREIGN_OPT,
2666 .name = "foreign", .has_arg = optional_argument},
2667 { .val = LFS_MIRROR_ID_OPT,
2668 .name = "mirror-id", .has_arg = required_argument},
2669 { .val = LFS_MODE_OPT,
2670 .name = "mode", .has_arg = required_argument},
2671 { .val = LFS_LAYOUT_COPY,
2672 .name = "copy", .has_arg = required_argument},
2673 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2674 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2675 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2676 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2677 { .val = 'd', .name = "delete", .has_arg = no_argument},
2678 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2679 /* --non-direct is only valid in migrate mode */
2680 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2681 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2682 { .val = 'E', .name = "component-end",
2683 .has_arg = required_argument},
2684 { .val = 'f', .name = "file", .has_arg = required_argument },
2685 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2686 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2687 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2688 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2689 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2690 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2691 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2692 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2693 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2694 { .val = 'L', .name = "layout", .has_arg = required_argument },
2695 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2696 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2697 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2698 /* --non-block is only valid in migrate mode */
2699 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2700 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2701 { .val = 'o', .name = "ost", .has_arg = required_argument },
2702 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2703 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2704 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2706 { .val = 'p', .name = "pool", .has_arg = required_argument },
2707 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2708 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2709 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2710 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2711 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2712 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2713 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2714 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2715 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2716 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2717 /* --verbose is only valid in migrate mode */
2718 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2719 { .val = 'x', .name = "xattr", .has_arg = required_argument },
2720 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2723 setstripe_args_init(&lsa);
2725 migrate_mode = (opc == SO_MIGRATE);
2726 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2727 setstripe_mode = (opc == SO_SETSTRIPE);
2729 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2731 while ((c = getopt_long(argc, argv,
2732 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:",
2733 long_opts, NULL)) >= 0) {
2738 case LFS_COMP_ADD_OPT:
2741 case LFS_COMP_DEL_OPT:
2744 case LFS_COMP_FLAGS_OPT:
2745 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2746 &lsa.lsa_comp_neg_flags);
2749 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2750 fprintf(stderr, "%s: inverted flags are not supported\n",
2754 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2756 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2763 case LFS_COMP_SET_OPT:
2766 case LFS_COMP_NO_VERIFY_OPT:
2767 mirror_flags |= MF_NO_VERIFY;
2769 case LFS_MIRROR_ID_OPT:
2770 mirror_id = strtoul(optarg, &end, 0);
2771 if (*end != '\0' || mirror_id == 0) {
2773 "%s %s: invalid mirror ID '%s'\n",
2774 progname, argv[0], optarg);
2778 case LFS_LAYOUT_FLAGS_OPT: {
2781 /* check for numeric flags (foreign and mirror cases) */
2782 if (setstripe_mode && !mirror_mode && !last_mirror) {
2783 flags = strtoul(optarg, &end, 16);
2786 "%s %s: bad flags '%s'\n",
2787 progname, argv[0], optarg);
2793 if (!mirror_mode || !last_mirror) {
2794 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2799 result = comp_str2flags(optarg, &last_mirror->m_flags,
2805 fprintf(stderr, "%s: inverted flags are not supported\n",
2810 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2812 "%s: unsupported mirror flags: %s\n",
2819 case LFS_LAYOUT_FOREIGN_OPT:
2820 if (optarg != NULL) {
2821 /* check pure numeric */
2822 type = strtoul(optarg, &end, 0);
2825 type = lov_check_foreign_type_name(optarg);
2826 if (type == LOV_FOREIGN_TYPE_UNKNOWN) {
2828 "%s %s: unrecognized foreign type '%s'\n",
2835 foreign_mode = true;
2839 if (mode_opt != NULL) {
2840 mode = strtoul(mode_opt, &end, 8);
2843 "%s %s: bad MODE '%s'\n",
2844 progname, argv[0], mode_opt);
2847 previous_umask = umask(0);
2850 case LFS_LAYOUT_COPY:
2855 if (!migrate_mode) {
2857 "%s %s: -b|--block valid only for migrate command\n",
2861 migration_block = true;
2864 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2867 "%s %s: invalid stripe count '%s'\n",
2868 progname, argv[0], optarg);
2872 if (lsa.lsa_stripe_count == -1)
2873 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2876 /* delete the default striping pattern */
2878 if (opc == SO_MIRROR_SPLIT) {
2881 "%s %s: -d cannot used with -f\n",
2885 mirror_flags |= MF_DESTROY;
2889 if (!migrate_mode) {
2891 "%s %s: -D|--non-direct is valid "
2892 "only for migrate command\n",
2896 migration_flags |= MIGRATION_NONDIRECT;
2899 if (lsa.lsa_comp_end != 0) {
2900 result = comp_args_to_layout(lpp, &lsa, true);
2903 "%s %s: invalid layout\n",
2908 setstripe_args_init_inherit(&lsa);
2911 if (arg_is_eof(optarg)) {
2912 lsa.lsa_comp_end = LUSTRE_EOF;
2914 result = llapi_parse_size(optarg,
2919 "%s %s: invalid component end '%s'\n",
2920 progname, argv[0], optarg);
2926 if (!migrate_mode) {
2927 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2931 lsa.lsa_pattern = check_hashtype(optarg);
2932 if (lsa.lsa_pattern == 0) {
2934 "%s %s: bad stripe hash type '%s'\n",
2935 progname, argv[0], optarg);
2940 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2943 "%s %s: invalid stripe offset '%s'\n",
2944 progname, argv[0], optarg);
2947 if (lsa.lsa_stripe_off == -1)
2948 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2951 comp_id = strtoul(optarg, &end, 0);
2952 if (*end != '\0' || comp_id == 0 ||
2953 comp_id > LCME_ID_MAX) {
2955 "%s %s: invalid component ID '%s'\n",
2956 progname, argv[0], optarg);
2961 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2963 "error: %s: invalid option: %s\n",
2964 progname, argv[optopt + 1]);
2967 if (opc == SO_MIRROR_EXTEND) {
2968 if (last_mirror == NULL) {
2970 "error: %s: '-N' must exist in front of '%s'\n",
2971 progname, argv[optopt + 1]);
2974 last_mirror->m_file = optarg;
2975 last_mirror->m_count = 1;
2978 if (mirror_list == NULL)
2979 mirror_list = lfs_mirror_alloc();
2980 mirror_list->m_file = optarg;
2985 if (strcmp(argv[optind - 1], "mdt") == 0) {
2986 /* Can be only the first component */
2987 if (layout != NULL) {
2989 fprintf(stderr, "error: 'mdt' layout "
2990 "can be only the first one\n");
2993 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2995 fprintf(stderr, "error: 'mdt' layout "
2996 "size is too big\n");
2999 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3000 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3002 fprintf(stderr, "error: layout '%s' is "
3003 "unknown, supported layouts are: "
3004 "'mdt', 'raid0'\n", argv[optind]);
3009 if (!migrate_mode) {
3011 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3015 migrate_mdt_mode = true;
3016 lsa.lsa_nr_tgts = parse_targets(tgts,
3017 sizeof(tgts) / sizeof(__u32),
3018 lsa.lsa_nr_tgts, optarg);
3019 if (lsa.lsa_nr_tgts < 0) {
3021 "%s %s: invalid MDT target(s) '%s'\n",
3022 progname, argv[0], optarg);
3026 lsa.lsa_tgts = tgts;
3027 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3028 lsa.lsa_stripe_off = tgts[0];
3031 if (!migrate_mode) {
3033 "%s %s: -n|--non-block valid only for migrate command\n",
3037 migration_flags |= MIGRATION_NONBLOCK;
3040 if (opc == SO_SETSTRIPE) {
3041 opc = SO_MIRROR_CREATE;
3045 if (optarg != NULL) {
3046 mirror_count = strtoul(optarg, &end, 0);
3047 if (*end != '\0' || mirror_count == 0) {
3049 "error: %s: bad mirror count: %s\n",
3056 new_mirror = lfs_mirror_alloc();
3057 new_mirror->m_count = mirror_count;
3059 if (mirror_list == NULL)
3060 mirror_list = new_mirror;
3062 if (last_mirror != NULL) {
3063 /* wrap up last mirror */
3064 if (lsa.lsa_comp_end == 0)
3065 lsa.lsa_comp_end = LUSTRE_EOF;
3067 result = comp_args_to_layout(lpp, &lsa, true);
3069 lfs_mirror_free(new_mirror);
3073 setstripe_args_init_inherit(&lsa);
3075 last_mirror->m_next = new_mirror;
3078 last_mirror = new_mirror;
3079 lpp = &last_mirror->m_layout;
3082 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3083 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3084 fprintf(stderr, "warning: '--ost-list' is "
3085 "deprecated, use '--ost' instead\n");
3087 lsa.lsa_nr_tgts = parse_targets(tgts,
3088 sizeof(tgts) / sizeof(__u32),
3089 lsa.lsa_nr_tgts, optarg);
3090 if (lsa.lsa_nr_tgts < 0) {
3092 "%s %s: invalid OST target(s) '%s'\n",
3093 progname, argv[0], optarg);
3097 lsa.lsa_tgts = tgts;
3098 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3099 lsa.lsa_stripe_off = tgts[0];
3104 lsa.lsa_pool_name = optarg;
3106 if (strlen(lsa.lsa_pool_name) == 0 ||
3107 strncmp(lsa.lsa_pool_name, "none",
3108 LOV_MAXPOOLNAME) == 0)
3109 lsa.lsa_pool_name = NULL;
3112 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3116 "%s %s: invalid stripe size '%s'\n",
3117 progname, argv[0], optarg);
3122 if (!migrate_mode) {
3124 "%s %s: -v|--verbose valid only for migrate command\n",
3128 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3129 migration_flags = MIGRATION_VERBOSE;
3139 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3140 progname, argv[0], argv[optind - 1]);
3145 fname = argv[optind];
3147 if (optind == argc) {
3148 fprintf(stderr, "%s %s: FILE must be specified\n",
3153 if (xattr && !foreign_mode) {
3154 /* only print a warning as this is harmless and will be ignored
3157 "%s %s: xattr has been specified for non-foreign layout\n",
3159 } else if (foreign_mode && !xattr) {
3161 "%s %s: xattr must be provided in foreign mode\n",
3166 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3167 comp_set || comp_id || delete || from_copy ||
3168 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3171 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3176 if (mirror_mode && mirror_count == 0) {
3178 "error: %s: --mirror-count|-N option is required\n",
3185 if (lsa.lsa_comp_end == 0)
3186 lsa.lsa_comp_end = LUSTRE_EOF;
3189 if (lsa.lsa_comp_end != 0) {
3190 result = comp_args_to_layout(lpp, &lsa, true);
3195 if (mirror_flags & MF_NO_VERIFY) {
3196 if (opc != SO_MIRROR_EXTEND) {
3198 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3202 } else if (!has_m_file) {
3204 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3211 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3212 * altered by user space tool, so we don't need to support the
3213 * --component-set for this moment. */
3214 if (comp_set && !comp_id) {
3215 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3220 if ((delete + comp_set + comp_del + comp_add) > 1) {
3222 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3227 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3228 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3230 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3235 if ((comp_set || comp_del) &&
3236 (setstripe_args_specified(&lsa) || layout != NULL)) {
3238 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3243 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3245 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3250 if (comp_add || comp_del) {
3253 result = lstat(fname, &st);
3254 if (result == 0 && S_ISDIR(st.st_mode)) {
3256 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3262 fprintf(stderr, "error: %s: can't use --component-add "
3263 "or --component-del for mirror operation\n",
3270 if (layout == NULL) {
3272 "%s %s: option -E must be specified with --component-add\n",
3277 result = adjust_first_extent(fname, layout);
3278 if (result == -ENODATA)
3280 else if (result != 0)
3284 if (from_yaml && from_copy) {
3286 "%s: can't specify --yaml and --copy together\n",
3291 if ((from_yaml || from_copy) &&
3292 (setstripe_args_specified(&lsa) || layout != NULL)) {
3293 fprintf(stderr, "error: %s: can't specify --yaml with "
3294 "-c, -S, -i, -o, -p or -E options.\n",
3299 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3301 "%s %s: options --non-block and --block are mutually exclusive\n",
3306 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3309 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3314 if (migrate_mdt_mode) {
3315 struct lmv_user_md *lmu;
3317 /* initialize migrate mdt parameters */
3318 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3319 LMV_USER_MAGIC_SPECIFIC));
3322 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3323 progname, argv[0], strerror(ENOMEM));
3327 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3328 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3329 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3331 "%s %s: migrate should specify MDT index\n",
3336 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3337 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3338 lmu->lum_hash_type = lsa.lsa_pattern;
3340 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3341 if (lsa.lsa_pool_name)
3342 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3343 sizeof(lmu->lum_pool_name));
3344 if (lsa.lsa_nr_tgts > 1) {
3347 if (lsa.lsa_stripe_count > 0 &&
3348 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3349 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3351 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3352 progname, lsa.lsa_stripe_count,
3358 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3359 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3360 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3361 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3363 lmu->lum_magic = LMV_USER_MAGIC;
3366 migrate_mdt_param.fp_lmv_md = lmu;
3367 migrate_mdt_param.fp_migrate = 1;
3368 } else if (layout == NULL) {
3369 /* initialize stripe parameters */
3370 param = calloc(1, offsetof(typeof(*param),
3371 lsp_osts[lsa.lsa_nr_tgts]));
3372 if (param == NULL) {
3374 "%s %s: cannot allocate memory for parameters: %s\n",
3375 progname, argv[0], strerror(ENOMEM));
3380 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3381 param->lsp_stripe_size = lsa.lsa_stripe_size;
3382 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3383 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3384 param->lsp_stripe_count = -1;
3386 param->lsp_stripe_count = lsa.lsa_stripe_count;
3388 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3389 param->lsp_stripe_offset = -1;
3391 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3392 param->lsp_pool = lsa.lsa_pool_name;
3393 param->lsp_is_specific = false;
3394 if (lsa.lsa_nr_tgts > 0) {
3395 if (lsa.lsa_stripe_count > 0 &&
3396 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3397 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3398 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3399 fprintf(stderr, "error: %s: stripe count %lld "
3400 "doesn't match the number of OSTs: %d\n"
3401 , argv[0], lsa.lsa_stripe_count,
3407 param->lsp_is_specific = true;
3408 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3409 memcpy(param->lsp_osts, tgts,
3410 sizeof(*tgts) * lsa.lsa_nr_tgts);
3415 /* generate a layout from a YAML template */
3416 result = lfs_comp_create_from_yaml(template, &layout,
3419 fprintf(stderr, "error: %s: can't create composite "
3420 "layout from template file %s\n",
3424 } else if (from_copy) {
3425 layout = llapi_layout_get_by_path(template, 0);
3426 if (layout == NULL) {
3428 "%s: can't create composite layout from file %s.\n",
3429 progname, template);
3434 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3435 if (migrate_mdt_mode) {
3436 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3437 } else if (migrate_mode) {
3438 result = lfs_migrate(fname, migration_flags, param,
3440 } else if (comp_set != 0) {
3441 result = lfs_component_set(fname, comp_id,
3443 lsa.lsa_comp_neg_flags);
3444 } else if (comp_del != 0) {
3445 result = lfs_component_del(fname, comp_id,
3447 lsa.lsa_comp_neg_flags);
3448 } else if (comp_add != 0) {
3449 result = lfs_component_add(fname, layout);
3450 } else if (opc == SO_MIRROR_CREATE) {
3451 result = mirror_create(fname, mirror_list);
3452 } else if (opc == SO_MIRROR_EXTEND) {
3453 result = mirror_extend(fname, mirror_list,
3455 } else if (opc == SO_MIRROR_SPLIT) {
3456 if (mirror_id == 0 && comp_id == 0) {
3458 "%s %s: no mirror id or component id is specified\n",
3463 comp_id = mirror_id;
3465 mirror_flags |= MF_COMP_ID;
3466 result = mirror_split(fname, comp_id, mirror_flags,
3467 has_m_file ? mirror_list->m_file :
3469 } else if (layout != NULL) {
3470 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3476 } else if (foreign_mode) {
3477 result = llapi_file_create_foreign(fname, mode, type,
3484 result = llapi_file_open_param(fname,
3493 /* Save the first error encountered. */
3500 if (mode_opt != NULL && previous_umask != 0)
3501 umask(previous_umask);
3504 free(migrate_mdt_param.fp_lmv_md);
3505 llapi_layout_free(layout);
3506 lfs_mirror_list_free(mirror_list);
3511 llapi_layout_free(layout);
3512 lfs_mirror_list_free(mirror_list);
3516 static int lfs_poollist(int argc, char **argv)
3521 return llapi_poollist(argv[1]);
3524 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3529 char *endptr = "AD";
3534 else if (str[0] == '-')
3540 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3541 long long val = strtoll(timebuf, &endptr, 0);
3546 unit *= 52; /* 52 weeks + 1 day below */
3547 case 'w': /* fallthrough */
3549 case '\0': /* days are default unit if none used */
3550 case 'd': /* fallthrough */
3551 unit = (unit + (*endptr == 'y')) * 24;
3552 case 'h': /* fallthrough */
3554 case 'm': /* fallthrough */
3556 case 's': /* fallthrough */
3558 /* don't need to multiply by 1 for seconds */
3561 "%s find: bad time string '%s': %s\n",
3562 progname, timebuf, strerror(EINVAL));
3566 if (param->fp_time_margin == 0 ||
3567 (*endptr && unit < param->fp_time_margin))
3568 param->fp_time_margin = unit;
3575 fprintf(stderr, "%s find: bad time '%s': too large\n",
3585 static int name2uid(unsigned int *id, const char *name)
3587 struct passwd *passwd;
3589 passwd = getpwnam(name);
3592 *id = passwd->pw_uid;
3597 static int name2gid(unsigned int *id, const char *name)
3599 struct group *group;
3601 group = getgrnam(name);
3604 *id = group->gr_gid;
3609 static inline int name2projid(unsigned int *id, const char *name)
3614 static int uid2name(char **name, unsigned int id)
3616 struct passwd *passwd;
3618 passwd = getpwuid(id);
3621 *name = passwd->pw_name;
3626 static inline int gid2name(char **name, unsigned int id)
3628 struct group *group;
3630 group = getgrgid(id);
3633 *name = group->gr_name;
3638 static int name2layout(__u32 *layout, char *name)
3640 char *ptr, *layout_name;
3643 for (ptr = name; ; ptr = NULL) {
3644 layout_name = strtok(ptr, ",");
3645 if (layout_name == NULL)
3647 if (strcmp(layout_name, "released") == 0)
3648 *layout |= LOV_PATTERN_F_RELEASED;
3649 else if (strcmp(layout_name, "raid0") == 0)
3650 *layout |= LOV_PATTERN_RAID0;
3651 else if (strcmp(layout_name, "mdt") == 0)
3652 *layout |= LOV_PATTERN_MDT;
3659 static int lfs_find(int argc, char **argv)
3664 struct find_param param = {
3667 .fp_time_margin = 24 * 60 * 60,
3669 struct option long_opts[] = {
3670 { .val = 'A', .name = "atime", .has_arg = required_argument },
3671 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3672 { .val = LFS_COMP_COUNT_OPT,
3673 .name = "comp-count", .has_arg = required_argument },
3674 { .val = LFS_COMP_COUNT_OPT,
3675 .name = "component-count",
3676 .has_arg = required_argument },
3677 { .val = LFS_COMP_FLAGS_OPT,
3678 .name = "comp-flags", .has_arg = required_argument },
3679 { .val = LFS_COMP_FLAGS_OPT,
3680 .name = "component-flags",
3681 .has_arg = required_argument },
3682 { .val = LFS_COMP_START_OPT,
3683 .name = "comp-start", .has_arg = required_argument },
3684 { .val = LFS_COMP_START_OPT,
3685 .name = "component-start",
3686 .has_arg = required_argument },
3687 { .val = LFS_MIRROR_STATE_OPT,
3688 .name = "mirror-state", .has_arg = required_argument },
3689 { .val = LFS_LAYOUT_FOREIGN_OPT,
3690 .name = "foreign", .has_arg = optional_argument},
3691 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3692 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3693 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3694 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3695 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3696 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3697 { .val = 'E', .name = "component-end",
3698 .has_arg = required_argument },
3699 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3700 { .val = 'g', .name = "gid", .has_arg = required_argument },
3701 { .val = 'G', .name = "group", .has_arg = required_argument },
3702 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3703 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3704 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3705 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3706 { .val = 'L', .name = "layout", .has_arg = required_argument },
3707 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3708 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3709 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3710 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3711 { .val = 'n', .name = "name", .has_arg = required_argument },
3712 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3713 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3714 { .val = 'O', .name = "obd", .has_arg = required_argument },
3715 { .val = 'O', .name = "ost", .has_arg = required_argument },
3716 /* no short option for pool yet, can be 'p' after 2.18 */
3717 { .val = LFS_POOL_OPT,
3718 .name = "pool", .has_arg = required_argument },
3719 { .val = '0', .name = "print0", .has_arg = no_argument },
3720 { .val = 'P', .name = "print", .has_arg = no_argument },
3721 { .val = LFS_PROJID_OPT,
3722 .name = "projid", .has_arg = required_argument },
3723 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3724 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3725 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3726 { .val = 's', .name = "size", .has_arg = required_argument },
3727 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3728 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3729 { .val = 't', .name = "type", .has_arg = required_argument },
3730 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3731 { .val = 'u', .name = "uid", .has_arg = required_argument },
3732 { .val = 'U', .name = "user", .has_arg = required_argument },
3733 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3734 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3746 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3747 while ((c = getopt_long_only(argc, argv,
3748 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3749 long_opts, NULL)) >= 0) {
3754 /* '!' is part of option */
3755 /* when getopt_long_only() finds a string which is not
3756 * an option nor a known option argument it returns 1
3757 * in that case if we already have found pathstart and pathend
3758 * (i.e. we have the list of pathnames),
3759 * the only supported value is "!"
3761 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3762 if (!isoption && pathend != -1) {
3763 fprintf(stderr, "err: %s: filename|dirname must either "
3764 "precede options or follow options\n",
3769 if (!isoption && pathstart == -1)
3770 pathstart = optind - 1;
3771 if (isoption && pathstart != -1 && pathend == -1)
3772 pathend = optind - 2;
3778 /* unknown; opt is "!" or path component,
3779 * checking done above.
3781 if (strcmp(optarg, "!") == 0)
3785 xtime = ¶m.fp_atime;
3786 xsign = ¶m.fp_asign;
3787 param.fp_exclude_atime = !!neg_opt;
3788 /* no break, this falls through to 'C' for ctime */
3791 xtime = ¶m.fp_ctime;
3792 xsign = ¶m.fp_csign;
3793 param.fp_exclude_ctime = !!neg_opt;
3795 /* no break, this falls through to 'M' for mtime */
3798 xtime = ¶m.fp_mtime;
3799 xsign = ¶m.fp_msign;
3800 param.fp_exclude_mtime = !!neg_opt;
3802 rc = set_time(¶m, &t, xtime, optarg);
3803 if (rc == LONG_MAX) {
3811 if (optarg[0] == '+') {
3812 param.fp_blocks_sign = -1;
3814 } else if (optarg[0] == '-') {
3815 param.fp_blocks_sign = 1;
3819 param.fp_blocks_units = 1024;
3820 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3821 ¶m.fp_blocks_units, 0);
3823 fprintf(stderr, "error: bad blocks '%s'\n",
3827 param.fp_check_blocks = 1;
3828 param.fp_exclude_blocks = !!neg_opt;
3830 case LFS_COMP_COUNT_OPT:
3831 if (optarg[0] == '+') {
3832 param.fp_comp_count_sign = -1;
3834 } else if (optarg[0] == '-') {
3835 param.fp_comp_count_sign = 1;
3839 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3840 if (*endptr != '\0') {
3841 fprintf(stderr, "error: bad component count "
3845 param.fp_check_comp_count = 1;
3846 param.fp_exclude_comp_count = !!neg_opt;
3848 case LFS_COMP_FLAGS_OPT:
3849 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3850 ¶m.fp_comp_neg_flags);
3852 fprintf(stderr, "error: bad component flags "
3856 param.fp_check_comp_flags = 1;
3858 __u32 flags = param.fp_comp_neg_flags;
3859 param.fp_comp_neg_flags = param.fp_comp_flags;
3860 param.fp_comp_flags = flags;
3863 case LFS_COMP_START_OPT:
3864 if (optarg[0] == '+') {
3865 param.fp_comp_start_sign = -1;
3867 } else if (optarg[0] == '-') {
3868 param.fp_comp_start_sign = 1;
3872 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3873 ¶m.fp_comp_start_units, 0);
3875 fprintf(stderr, "error: bad component start "
3879 param.fp_check_comp_start = 1;
3880 param.fp_exclude_comp_start = !!neg_opt;
3882 case LFS_MIRROR_STATE_OPT:
3883 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3884 ¶m.fp_mirror_neg_state);
3887 "error: bad mirrored file state '%s'\n",
3891 param.fp_check_mirror_state = 1;
3893 __u16 state = param.fp_mirror_neg_state;
3894 param.fp_mirror_neg_state =
3895 param.fp_mirror_state;
3896 param.fp_mirror_state = state;
3899 case LFS_LAYOUT_FOREIGN_OPT: {
3900 /* all types by default */
3901 uint32_t type = LOV_FOREIGN_TYPE_UNKNOWN;
3903 if (optarg != NULL) {
3904 /* check pure numeric */
3905 type = strtoul(optarg, &endptr, 0);
3908 type = lov_check_foreign_type_name(optarg);
3909 if (type == LOV_FOREIGN_TYPE_UNKNOWN) {
3911 "%s %s: unrecognized foreign type '%s'\n",
3918 param.fp_foreign_type = type;
3919 param.fp_check_foreign = 1;
3920 param.fp_exclude_foreign = !!neg_opt;
3924 if (optarg[0] == '+') {
3925 param.fp_stripe_count_sign = -1;
3927 } else if (optarg[0] == '-') {
3928 param.fp_stripe_count_sign = 1;
3932 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3933 if (*endptr != '\0') {
3934 fprintf(stderr,"error: bad stripe_count '%s'\n",
3939 param.fp_check_stripe_count = 1;
3940 param.fp_exclude_stripe_count = !!neg_opt;
3943 param.fp_max_depth = strtol(optarg, 0, 0);
3946 if (optarg[0] == '+') {
3947 param.fp_comp_end_sign = -1;
3949 } else if (optarg[0] == '-') {
3950 param.fp_comp_end_sign = 1;
3954 if (arg_is_eof(optarg)) {
3955 param.fp_comp_end = LUSTRE_EOF;
3956 param.fp_comp_end_units = 1;
3959 rc = llapi_parse_size(optarg,
3961 ¶m.fp_comp_end_units, 0);
3964 fprintf(stderr, "error: bad component end "
3968 param.fp_check_comp_end = 1;
3969 param.fp_exclude_comp_end = !!neg_opt;
3973 rc = name2gid(¶m.fp_gid, optarg);
3975 param.fp_gid = strtoul(optarg, &endptr, 10);
3976 if (*endptr != '\0') {
3977 fprintf(stderr, "Group/GID: %s cannot "
3978 "be found.\n", optarg);
3983 param.fp_exclude_gid = !!neg_opt;
3984 param.fp_check_gid = 1;
3987 param.fp_hash_type = check_hashtype(optarg);
3988 if (param.fp_hash_type == 0) {
3989 fprintf(stderr, "error: bad hash_type '%s'\n",
3994 param.fp_check_hash_type = 1;
3995 param.fp_exclude_hash_type = !!neg_opt;
3998 ret = name2layout(¶m.fp_layout, optarg);
4001 param.fp_exclude_layout = !!neg_opt;
4002 param.fp_check_layout = 1;
4006 rc = name2uid(¶m.fp_uid, optarg);
4008 param.fp_uid = strtoul(optarg, &endptr, 10);
4009 if (*endptr != '\0') {
4010 fprintf(stderr, "User/UID: %s cannot "
4011 "be found.\n", optarg);
4016 param.fp_exclude_uid = !!neg_opt;
4017 param.fp_check_uid = 1;
4020 param.fp_pattern = (char *)optarg;
4021 param.fp_exclude_pattern = !!neg_opt;
4024 if (optarg[0] == '+') {
4025 param.fp_mirror_count_sign = -1;
4027 } else if (optarg[0] == '-') {
4028 param.fp_mirror_count_sign = 1;
4032 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4033 if (*endptr != '\0') {
4035 "error: bad mirror count '%s'\n",
4039 param.fp_check_mirror_count = 1;
4040 param.fp_exclude_mirror_count = !!neg_opt;
4045 char *buf, *token, *next, *p;
4049 buf = strdup(optarg);
4055 param.fp_exclude_obd = !!neg_opt;
4058 while (token && *token) {
4059 token = strchr(token, ',');
4066 param.fp_exclude_mdt = !!neg_opt;
4067 param.fp_num_alloc_mdts += len;
4068 tmp = realloc(param.fp_mdt_uuid,
4069 param.fp_num_alloc_mdts *
4070 sizeof(*param.fp_mdt_uuid));
4076 param.fp_mdt_uuid = tmp;
4078 param.fp_exclude_obd = !!neg_opt;
4079 param.fp_num_alloc_obds += len;
4080 tmp = realloc(param.fp_obd_uuid,
4081 param.fp_num_alloc_obds *
4082 sizeof(*param.fp_obd_uuid));
4088 param.fp_obd_uuid = tmp;
4090 for (token = buf; token && *token; token = next) {
4091 struct obd_uuid *puuid;
4094 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4097 ¶m.fp_obd_uuid[param.fp_num_obds++];
4099 p = strchr(token, ',');
4106 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4111 strncpy(puuid->uuid, token,
4112 sizeof(puuid->uuid));
4119 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4123 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4125 "Pool name %s is too long (max %d)\n",
4126 optarg, LOV_MAXPOOLNAME);
4131 * We do check for empty pool because empty pool
4132 * is used to find V1 LOV attributes
4134 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4135 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4136 param.fp_exclude_pool = !!neg_opt;
4137 param.fp_check_pool = 1;
4139 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4140 case 'p': /* want this for --pool, to match getstripe/find */
4142 "warning: -p deprecated, use --print0 or -0\n");
4145 param.fp_zero_end = 1;
4147 case 'P': /* we always print, this option is a no-op */
4149 case LFS_PROJID_OPT:
4150 rc = name2projid(¶m.fp_projid, optarg);
4152 param.fp_projid = strtoul(optarg, &endptr, 10);
4153 if (*endptr != '\0') {
4155 "Invalid project ID: %s",
4161 param.fp_exclude_projid = !!neg_opt;
4162 param.fp_check_projid = 1;
4165 if (optarg[0] == '+') {
4166 param.fp_size_sign = -1;
4168 } else if (optarg[0] == '-') {
4169 param.fp_size_sign = 1;
4173 ret = llapi_parse_size(optarg, ¶m.fp_size,
4174 ¶m.fp_size_units, 0);
4176 fprintf(stderr, "error: bad file size '%s'\n",
4180 param.fp_check_size = 1;
4181 param.fp_exclude_size = !!neg_opt;
4184 if (optarg[0] == '+') {
4185 param.fp_stripe_size_sign = -1;
4187 } else if (optarg[0] == '-') {
4188 param.fp_stripe_size_sign = 1;
4192 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4193 ¶m.fp_stripe_size_units, 0);
4195 fprintf(stderr, "error: bad stripe_size '%s'\n",
4199 param.fp_check_stripe_size = 1;
4200 param.fp_exclude_stripe_size = !!neg_opt;
4203 param.fp_exclude_type = !!neg_opt;
4204 switch (optarg[0]) {
4206 param.fp_type = S_IFBLK;
4209 param.fp_type = S_IFCHR;
4212 param.fp_type = S_IFDIR;
4215 param.fp_type = S_IFREG;
4218 param.fp_type = S_IFLNK;
4221 param.fp_type = S_IFIFO;
4224 param.fp_type = S_IFSOCK;
4227 fprintf(stderr, "error: %s: bad type '%s'\n",
4234 if (optarg[0] == '+') {
4235 param.fp_mdt_count_sign = -1;
4237 } else if (optarg[0] == '-') {
4238 param.fp_mdt_count_sign = 1;
4242 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4243 if (*endptr != '\0') {
4244 fprintf(stderr, "error: bad mdt_count '%s'\n",
4249 param.fp_check_mdt_count = 1;
4250 param.fp_exclude_mdt_count = !!neg_opt;
4258 if (pathstart == -1) {
4259 fprintf(stderr, "error: %s: no filename|pathname\n",
4263 } else if (pathend == -1) {
4269 rc = llapi_find(argv[pathstart], ¶m);
4270 if (rc != 0 && ret == 0)
4272 } while (++pathstart < pathend);
4275 fprintf(stderr, "error: %s failed for %s.\n",
4276 argv[0], argv[optind - 1]);
4278 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4279 free(param.fp_obd_uuid);
4281 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4282 free(param.fp_mdt_uuid);
4287 static int lfs_getstripe_internal(int argc, char **argv,
4288 struct find_param *param)
4290 struct option long_opts[] = {
4291 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4292 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4293 { .val = LFS_COMP_COUNT_OPT,
4294 .name = "comp-count", .has_arg = no_argument },
4295 { .val = LFS_COMP_COUNT_OPT,
4296 .name = "component-count", .has_arg = no_argument },
4297 { .val = LFS_COMP_FLAGS_OPT,
4298 .name = "comp-flags", .has_arg = optional_argument },
4299 { .val = LFS_COMP_FLAGS_OPT,
4300 .name = "component-flags", .has_arg = optional_argument },
4301 { .val = LFS_COMP_START_OPT,
4302 .name = "comp-start", .has_arg = optional_argument },
4303 { .val = LFS_COMP_START_OPT,
4304 .name = "component-start", .has_arg = optional_argument },
4305 { .val = LFS_MIRROR_INDEX_OPT,
4306 .name = "mirror-index", .has_arg = required_argument },
4307 { .val = LFS_MIRROR_ID_OPT,
4308 .name = "mirror-id", .has_arg = required_argument },
4309 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4310 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4311 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4312 { .val = 'd', .name = "directory", .has_arg = no_argument },
4313 { .val = 'D', .name = "default", .has_arg = no_argument },
4314 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4315 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4316 { .val = 'F', .name = "fid", .has_arg = no_argument },
4317 { .val = 'g', .name = "generation", .has_arg = no_argument },
4318 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4319 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4320 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4321 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4322 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4323 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4324 { .val = 'L', .name = "layout", .has_arg = no_argument },
4325 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4326 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4327 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4328 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4329 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4330 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4331 { .val = 'O', .name = "obd", .has_arg = required_argument },
4332 { .val = 'O', .name = "ost", .has_arg = required_argument },
4333 { .val = 'p', .name = "pool", .has_arg = no_argument },
4334 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4335 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4336 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4337 { .val = 'R', .name = "raw", .has_arg = no_argument },
4338 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4339 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4340 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4341 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4342 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4343 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4344 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4345 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4349 int pathstart = -1, pathend = -1;
4353 while ((c = getopt_long(argc, argv,
4354 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4355 long_opts, NULL)) != -1) {
4359 /* '!' is part of option */
4360 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4361 if (!isoption && pathend != -1) {
4363 "error: %s: filename|dirname must either precede options or follow options\n",
4367 if (!isoption && pathstart == -1)
4368 pathstart = optind - 1;
4369 if (isoption && pathstart != -1 && pathend == -1)
4370 pathend = optind - 2;
4374 /* unknown: opt is "!" */
4375 if (strcmp(optarg, "!") == 0)
4379 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4380 param->fp_verbose |= VERBOSE_COUNT;
4381 param->fp_max_depth = 0;
4384 case LFS_COMP_COUNT_OPT:
4385 param->fp_verbose |= VERBOSE_COMP_COUNT;
4386 param->fp_max_depth = 0;
4388 case LFS_COMP_FLAGS_OPT:
4389 if (optarg != NULL) {
4390 rc = comp_str2flags(optarg,
4391 ¶m->fp_comp_flags,
4392 ¶m->fp_comp_neg_flags);
4394 fprintf(stderr, "error: %s bad "
4395 "component flags '%s'.\n",
4399 param->fp_check_comp_flags = 1;
4401 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4402 param->fp_max_depth = 0;
4405 case LFS_COMP_START_OPT:
4406 if (optarg != NULL) {
4408 if (tmp[0] == '+') {
4409 param->fp_comp_start_sign = -1;
4411 } else if (tmp[0] == '-') {
4412 param->fp_comp_start_sign = 1;
4415 rc = llapi_parse_size(tmp,
4416 ¶m->fp_comp_start,
4417 ¶m->fp_comp_start_units, 0);
4419 fprintf(stderr, "error: %s bad "
4420 "component start '%s'.\n",
4424 param->fp_check_comp_start = 1;
4427 param->fp_verbose |= VERBOSE_COMP_START;
4428 param->fp_max_depth = 0;
4431 case LFS_MIRROR_INDEX_OPT:
4432 if (optarg[0] == '+') {
4433 param->fp_mirror_index_sign = -1;
4435 } else if (optarg[0] == '-') {
4436 param->fp_mirror_index_sign = 1;
4440 param->fp_mirror_index = strtoul(optarg, &end, 0);
4441 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4442 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4444 "%s %s: invalid mirror index '%s'\n",
4445 progname, argv[0], optarg);
4448 if (param->fp_mirror_id != 0) {
4450 "%s %s: can't specify both mirror index and mirror ID\n",
4454 param->fp_check_mirror_index = 1;
4455 param->fp_exclude_mirror_index = !!neg_opt;
4457 case LFS_MIRROR_ID_OPT:
4458 if (optarg[0] == '+') {
4459 param->fp_mirror_id_sign = -1;
4461 } else if (optarg[0] == '-') {
4462 param->fp_mirror_id_sign = 1;
4466 param->fp_mirror_id = strtoul(optarg, &end, 0);
4467 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4468 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4470 "%s %s: invalid mirror ID '%s'\n",
4471 progname, argv[0], optarg);
4474 if (param->fp_mirror_index != 0) {
4476 "%s %s: can't specify both mirror index and mirror ID\n",
4480 param->fp_check_mirror_id = 1;
4481 param->fp_exclude_mirror_id = !!neg_opt;
4484 param->fp_max_depth = 0;
4487 param->fp_get_default_lmv = 1;
4490 if (optarg != NULL) {
4492 if (tmp[0] == '+') {
4493 param->fp_comp_end_sign = -1;
4495 } else if (tmp[0] == '-') {
4496 param->fp_comp_end_sign = 1;
4500 if (arg_is_eof(tmp)) {
4501 param->fp_comp_end = LUSTRE_EOF;
4502 param->fp_comp_end_units = 1;
4505 rc = llapi_parse_size(tmp,
4506 ¶m->fp_comp_end,
4507 ¶m->fp_comp_end_units, 0);
4510 fprintf(stderr, "error: %s bad "
4511 "component end '%s'.\n",
4515 param->fp_check_comp_end = 1;
4517 param->fp_verbose |= VERBOSE_COMP_END;
4518 param->fp_max_depth = 0;
4522 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4523 param->fp_verbose |= VERBOSE_DFID;
4524 param->fp_max_depth = 0;
4528 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4529 param->fp_verbose |= VERBOSE_GENERATION;
4530 param->fp_max_depth = 0;
4534 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4535 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4536 param->fp_max_depth = 0;
4540 if (optarg != NULL) {
4541 param->fp_comp_id = strtoul(optarg, &end, 0);
4542 if (*end != '\0' || param->fp_comp_id == 0 ||
4543 param->fp_comp_id > LCME_ID_MAX) {
4544 fprintf(stderr, "error: %s bad "
4545 "component id '%s'\n",
4549 param->fp_check_comp_id = 1;
4552 param->fp_max_depth = 0;
4553 param->fp_verbose |= VERBOSE_COMP_ID;
4557 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4558 param->fp_verbose |= VERBOSE_PATTERN;
4559 param->fp_max_depth = 0;
4562 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4564 fprintf(stderr, "warning: '-M' deprecated"
4565 ", use '--mdt-index' or '-m' instead\n");
4568 if (!(param->fp_verbose & VERBOSE_DETAIL))
4569 param->fp_max_depth = 0;
4570 param->fp_verbose |= VERBOSE_MDTINDEX;
4573 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4574 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4575 param->fp_max_depth = 0;
4579 if (param->fp_obd_uuid) {
4581 "error: %s: only one obduuid allowed",
4585 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4588 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4589 param->fp_verbose |= VERBOSE_POOL;
4590 param->fp_max_depth = 0;
4597 param->fp_recursive = 1;
4603 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4604 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4605 param->fp_max_depth = 0;
4609 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4619 if (pathstart == -1) {
4620 fprintf(stderr, "error: %s: no filename|pathname\n",
4623 } else if (pathend == -1) {
4631 if (param->fp_recursive)
4632 param->fp_max_depth = -1;
4633 else if (param->fp_verbose & VERBOSE_DETAIL)
4634 param->fp_max_depth = 1;
4636 if (!param->fp_verbose)
4637 param->fp_verbose = VERBOSE_DEFAULT;
4638 if (param->fp_quiet)
4639 param->fp_verbose = VERBOSE_OBJID;
4642 rc = llapi_getstripe(argv[pathstart], param);
4643 } while (++pathstart < pathend && !rc);
4646 fprintf(stderr, "error: %s failed for %s.\n",
4647 argv[0], argv[optind - 1]);
4651 static int lfs_tgts(int argc, char **argv)
4653 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4654 struct find_param param;
4655 int index = 0, rc=0;
4660 if (argc == 2 && !realpath(argv[1], path)) {
4662 fprintf(stderr, "error: invalid path '%s': %s\n",
4663 argv[1], strerror(-rc));
4667 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4668 /* Check if we have a mount point */
4669 if (mntdir[0] == '\0')
4672 memset(¶m, 0, sizeof(param));
4673 if (!strcmp(argv[0], "mdts"))
4674 param.fp_get_lmv = 1;
4676 rc = llapi_ostlist(mntdir, ¶m);
4678 fprintf(stderr, "error: %s: failed on %s\n",
4681 if (path[0] != '\0')
4683 memset(mntdir, 0, PATH_MAX);
4689 static int lfs_getstripe(int argc, char **argv)
4691 struct find_param param = { 0 };
4693 param.fp_max_depth = 1;
4694 return lfs_getstripe_internal(argc, argv, ¶m);
4698 static int lfs_getdirstripe(int argc, char **argv)
4700 struct find_param param = { 0 };
4701 struct option long_opts[] = {
4702 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4703 { .val = 'D', .name = "default", .has_arg = no_argument },
4704 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4705 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4706 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4707 { .val = 'O', .name = "obd", .has_arg = required_argument },
4708 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4709 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4710 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4714 param.fp_get_lmv = 1;
4716 while ((c = getopt_long(argc, argv,
4717 "cDHimO:rtTy", long_opts, NULL)) != -1)
4722 param.fp_verbose |= VERBOSE_COUNT;
4725 param.fp_get_default_lmv = 1;
4729 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4731 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4733 fprintf(stderr, "warning: '-t' deprecated, "
4734 "use '--mdt-hash' or '-H' instead\n");
4737 param.fp_verbose |= VERBOSE_HASH_TYPE;
4740 if (param.fp_obd_uuid) {
4742 "error: %s: only one obduuid allowed",
4746 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4749 param.fp_recursive = 1;
4762 if (param.fp_recursive)
4763 param.fp_max_depth = -1;
4765 if (!param.fp_verbose)
4766 param.fp_verbose = VERBOSE_DEFAULT;
4769 rc = llapi_getstripe(argv[optind], ¶m);
4770 } while (++optind < argc && !rc);
4773 fprintf(stderr, "error: %s failed for %s.\n",
4774 argv[0], argv[optind - 1]);
4779 MNTDF_INODES = 0x0001,
4780 MNTDF_COOKED = 0x0002,
4781 MNTDF_LAZY = 0x0004,
4782 MNTDF_VERBOSE = 0x0008,
4783 MNTDF_SHOW = 0x0010,
4786 #define COOK(value) \
4789 while (value > 1024) { \
4797 #define CDF "%11llu"
4798 #define HDF "%8.1f%c"
4802 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4804 double avail, used, ratio = 0;
4807 avail = st->os_ffree;
4808 used = st->os_files - st->os_ffree;
4810 avail = st->os_bavail;
4811 used = st->os_blocks - st->os_bfree;
4813 if (avail + used > 0)
4814 ratio = used / (used + avail) * 100;
4816 /* Round up to match df(1) usage percentage */
4817 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4820 static int showdf(char *mntdir, struct obd_statfs *stat,
4821 char *uuid, enum mntdf_flags flags,
4822 char *type, int index, int rc)
4824 long long avail, used, total;
4826 char *suffix = "KMGTPEZY";
4827 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4828 char tbuf[3 * sizeof(__u64)];
4829 char ubuf[3 * sizeof(__u64)];
4830 char abuf[3 * sizeof(__u64)];
4831 char rbuf[3 * sizeof(__u64)];
4838 if (flags & MNTDF_INODES) {
4839 avail = stat->os_ffree;
4840 used = stat->os_files - stat->os_ffree;
4841 total = stat->os_files;
4843 int shift = flags & MNTDF_COOKED ? 0 : 10;
4845 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4846 used = ((stat->os_blocks - stat->os_bfree) *
4847 stat->os_bsize) >> shift;
4848 total = (stat->os_blocks * stat->os_bsize) >> shift;
4851 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4853 if (flags & MNTDF_COOKED) {
4857 cook_val = (double)total;
4860 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4863 snprintf(tbuf, sizeof(tbuf), CDF, total);
4865 cook_val = (double)used;
4868 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4871 snprintf(ubuf, sizeof(ubuf), CDF, used);
4873 cook_val = (double)avail;
4876 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4879 snprintf(abuf, sizeof(abuf), CDF, avail);
4881 snprintf(tbuf, sizeof(tbuf), CDF, total);
4882 snprintf(ubuf, sizeof(tbuf), CDF, used);
4883 snprintf(abuf, sizeof(tbuf), CDF, avail);
4886 sprintf(rbuf, RDF, ratio);
4887 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4888 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4890 printf("[%s:%d]", type, index);
4892 if (stat->os_state) {
4894 * Each character represents the matching
4897 const char state_names[] = "DRSI";
4902 for (i = 0, state = stat->os_state;
4903 state && i < sizeof(state_names); i++) {
4904 if (!(state & (1 << i)))
4906 printf("%c", state_names[i]);
4914 printf(UUF": inactive device\n", uuid);
4917 printf(UUF": %s\n", uuid, strerror(-rc));
4924 struct ll_stat_type {
4929 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4931 struct ll_statfs_data {
4933 struct obd_statfs sd_st;
4936 struct ll_statfs_buf {
4938 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4941 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4942 int ops, struct ll_statfs_buf *lsb)
4944 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4945 struct obd_uuid uuid_buf;
4946 char *poolname = NULL;
4947 struct ll_stat_type types[] = {
4948 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4949 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4950 { .st_name = NULL } };
4951 struct ll_stat_type *tp;
4952 __u64 ost_ffree = 0;
4960 poolname = strchr(pool, '.');
4961 if (poolname != NULL) {
4962 if (strncmp(fsname, pool, strlen(fsname))) {
4963 fprintf(stderr, "filesystem name incorrect\n");
4971 fd = open(mntdir, O_RDONLY);
4974 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4979 if (flags & MNTDF_SHOW) {
4980 if (flags & MNTDF_INODES)
4981 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4982 "UUID", "Inodes", "IUsed", "IFree",
4983 "IUse%", "Mounted on");
4985 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4987 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4988 "Used", "Available", "Use%", "Mounted on");
4991 for (tp = types; tp->st_name != NULL; tp++) {
4992 bool have_ost = false;
4994 if (!(tp->st_op & ops))
4997 for (index = 0; ; index++) {
4998 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4999 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5000 type = flags & MNTDF_LAZY ?
5001 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5002 rc2 = llapi_obd_fstatfs(fd, type, index,
5003 &stat_buf, &uuid_buf);
5008 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5009 if (!(flags & MNTDF_VERBOSE))
5011 } else if (rc2 < 0 && rc == 0) {
5015 /* If we have OSTs then don't report MDT block counts.
5016 * For MDT-only filesystems the expectation is that all
5017 * layouts have a DoM component. For filesystems with
5018 * OSTs, files are not necessarily going to store data
5019 * on MDTs, and MDT space is limited to a fraction of
5020 * OST space, so don't include it in the summary.
5022 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5029 if (poolname && tp->st_op == LL_STATFS_LOV &&
5030 llapi_search_ost(fsname, poolname,
5031 obd_uuid2str(&uuid_buf)) != 1)
5034 /* the llapi_obd_statfs() call may have returned with
5035 * an error, but if it filled in uuid_buf we will at
5036 * lease use that to print out a message for that OBD.
5037 * If we didn't get anything in the uuid_buf, then fill
5038 * it in so that we can print an error message. */
5039 if (uuid_buf.uuid[0] == '\0')
5040 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5041 "%s%04x", tp->st_name, index);
5043 lsb->sb_buf[lsb->sb_count].sd_index = index;
5044 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5047 if (flags & MNTDF_SHOW)
5048 showdf(mntdir, &stat_buf,
5049 obd_uuid2str(&uuid_buf), flags,
5050 tp->st_name, index, rc2);
5055 if (tp->st_op == LL_STATFS_LMV) {
5056 sum.os_ffree += stat_buf.os_ffree;
5057 sum.os_files += stat_buf.os_files;
5058 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5059 ost_ffree += stat_buf.os_ffree;
5061 sum.os_blocks += stat_buf.os_blocks *
5063 sum.os_bfree += stat_buf.os_bfree *
5065 sum.os_bavail += stat_buf.os_bavail *
5072 /* If we don't have as many objects free on the OST as inodes
5073 * on the MDS, we reduce the total number of inodes to
5074 * compensate, so that the "inodes in use" number is correct.
5075 * Matches ll_statfs_internal() so the results are consistent. */
5076 if (ost_ffree < sum.os_ffree) {
5077 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5078 sum.os_ffree = ost_ffree;
5080 if (flags & MNTDF_SHOW) {
5082 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5089 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5091 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5093 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5095 int r1 = obd_statfs_ratio(st1, false);
5096 int r2 = obd_statfs_ratio(st2, false);
5097 int64_t result = r1 - r2;
5099 /* if both space usage are above 90, compare free inodes */
5100 if (r1 > 90 && r2 > 90)
5101 result = st2->os_ffree - st1->os_ffree;
5105 else if (result == 0)
5112 static int lfs_setdirstripe(int argc, char **argv)
5116 struct lfs_setstripe_args lsa = { 0 };
5117 struct llapi_stripe_param *param = NULL;
5118 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5121 char *mode_opt = NULL;
5122 bool default_stripe = false;
5123 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5124 mode_t previous_mode = 0;
5125 bool delete = false;
5126 struct ll_statfs_buf *lsb = NULL;
5127 char mntdir[PATH_MAX] = "";
5128 bool auto_distributed = false;
5130 struct option long_opts[] = {
5131 { .val = 'c', .name = "count", .has_arg = required_argument },
5132 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5133 { .val = 'd', .name = "delete", .has_arg = no_argument },
5134 { .val = 'D', .name = "default", .has_arg = no_argument },
5135 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5136 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5137 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5138 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5139 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5141 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5142 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5144 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5145 { .val = 'i', .name = "index", .has_arg = required_argument },
5147 { .val = 'o', .name = "mode", .has_arg = required_argument },
5148 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5149 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5151 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5152 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5155 setstripe_args_init(&lsa);
5157 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
5165 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5168 "%s %s: invalid stripe count '%s'\n",
5169 progname, argv[0], optarg);
5175 default_stripe = true;
5178 default_stripe = true;
5180 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5182 fprintf(stderr, "warning: '--hash-type' and '-t' "
5183 "deprecated, use '--mdt-hash' or '-H' instead\n");
5186 lsa.lsa_pattern = check_hashtype(optarg);
5187 if (lsa.lsa_pattern == 0) {
5189 "%s %s: bad stripe hash type '%s'\n",
5190 progname, argv[0], optarg);
5195 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5198 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5199 if (strcmp(argv[optind - 1], "--index") == 0)
5201 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5204 lsa.lsa_nr_tgts = parse_targets(mdts,
5205 sizeof(mdts) / sizeof(__u32),
5206 lsa.lsa_nr_tgts, optarg);
5207 if (lsa.lsa_nr_tgts < 0) {
5209 "%s %s: invalid MDT target(s) '%s'\n",
5210 progname, argv[0], optarg);
5214 lsa.lsa_tgts = mdts;
5215 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5216 lsa.lsa_stripe_off = mdts[0];
5218 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5220 fprintf(stderr, "warning: '-m' is deprecated, "
5221 "use '--mode' or '-o' instead\n");
5227 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5228 progname, argv[0], argv[optind - 1]);
5233 if (optind == argc) {
5234 fprintf(stderr, "%s %s: DIR must be specified\n",
5239 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5240 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5242 "%s %s: stripe offset and count must be specified\n",
5248 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5249 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5251 "%s %s: cannot specify -d with -c or -i options\n",
5256 if (mode_opt != NULL) {
5257 mode = strtoul(mode_opt, &end, 8);
5260 "%s %s: bad MODE '%s'\n",
5261 progname, argv[0], mode_opt);
5264 previous_mode = umask(0);
5268 * initialize stripe parameters, in case param is converted to specific,
5269 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5271 param = calloc(1, offsetof(typeof(*param),
5272 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5273 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5274 if (param == NULL) {
5276 "%s %s: cannot allocate memory for parameters: %s\n",
5277 progname, argv[0], strerror(ENOMEM));
5281 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5282 param->lsp_stripe_count = lsa.lsa_stripe_count;
5283 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5284 param->lsp_stripe_offset = -1;
5286 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5287 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5288 param->lsp_stripe_pattern = lsa.lsa_pattern;
5290 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5291 param->lsp_pool = lsa.lsa_pool_name;
5292 param->lsp_is_specific = false;
5293 if (lsa.lsa_nr_tgts > 1) {
5294 if (lsa.lsa_stripe_count > 0 &&
5295 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5296 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5297 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5298 "match the number of MDTs: %d\n",
5299 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5304 param->lsp_is_specific = true;
5305 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5306 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5309 dname = argv[optind];
5311 if (default_stripe) {
5312 result = llapi_dir_set_default_lmv(dname, param);
5314 /* if current \a dname isn't under the same \a mntdir
5315 * as the last one, and the last one was
5316 * auto-distributed, restore \a param.
5318 if (mntdir[0] != '\0' &&
5319 strncmp(dname, mntdir, strlen(mntdir)) &&
5321 param->lsp_is_specific = false;
5322 param->lsp_stripe_offset = -1;
5323 auto_distributed = false;
5326 if (!param->lsp_is_specific &&
5327 param->lsp_stripe_offset == -1) {
5328 char path[PATH_MAX] = "";
5331 lsb = malloc(sizeof(*lsb));
5339 /* use mntdir for dirname() temporarily */
5340 strncpy(mntdir, dname, sizeof(mntdir));
5341 if (!realpath(dirname(mntdir), path)) {
5344 "error: invalid path '%s': %s\n",
5345 argv[optind], strerror(errno));
5350 result = llapi_search_mounts(path, 0, mntdir,
5352 if (result < 0 || mntdir[0] == '\0') {
5354 "No suitable Lustre mount found\n");
5358 result = mntdf(mntdir, NULL, NULL, 0,
5359 LL_STATFS_LMV, lsb);
5363 if (param->lsp_stripe_count > lsb->sb_count) {
5365 "error: stripe count %d is too big\n",
5366 param->lsp_stripe_count);
5371 qsort(lsb->sb_buf, lsb->sb_count,
5372 sizeof(struct ll_statfs_data),
5373 ll_statfs_data_comp);
5375 auto_distributed = true;
5378 if (auto_distributed) {
5380 int nr = MAX(param->lsp_stripe_count,
5383 /* don't use server whose usage is above 90% */
5384 while (nr != param->lsp_stripe_count &&
5385 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5387 nr = MAX(param->lsp_stripe_count,
5390 /* get \a r between [0, nr) */
5393 param->lsp_stripe_offset =
5394 lsb->sb_buf[r].sd_index;
5395 if (param->lsp_stripe_count > 1) {
5398 param->lsp_is_specific = true;
5399 for (; i < param->lsp_stripe_count; i++)
5400 param->lsp_tgts[(i + r) % nr] =
5401 lsb->sb_buf[i].sd_index;
5405 result = llapi_dir_create(dname, mode, param);
5410 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5411 progname, dname, strerror(-result));
5414 dname = argv[++optind];
5415 } while (dname != NULL);
5417 if (mode_opt != NULL)
5418 umask(previous_mode);
5426 static int lfs_rmentry(int argc, char **argv)
5433 fprintf(stderr, "error: %s: missing dirname\n",
5439 dname = argv[index];
5440 while (dname != NULL) {
5441 result = llapi_direntry_remove(dname);
5443 fprintf(stderr, "error: %s: remove dir entry '%s' "
5444 "failed\n", argv[0], dname);
5447 dname = argv[++index];
5452 static int lfs_mv(int argc, char **argv)
5454 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5455 struct find_param param = {
5462 struct option long_opts[] = {
5463 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5464 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5465 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5468 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5470 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5472 fprintf(stderr, "warning: '-M' deprecated"
5473 ", use '--mdt-index' or '-m' instead\n");
5476 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5478 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5484 param.fp_verbose = VERBOSE_DETAIL;
5487 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5488 progname, argv[optind - 1]);
5493 if (lmu.lum_stripe_offset == -1) {
5494 fprintf(stderr, "%s mv: MDT index must be specified\n",
5499 if (optind >= argc) {
5500 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5505 /* initialize migrate mdt parameters */
5506 param.fp_lmv_md = &lmu;
5507 param.fp_migrate = 1;
5508 rc = llapi_migrate_mdt(argv[optind], ¶m);
5510 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5511 progname, argv[optind], param.fp_mdt_index,
5516 static int lfs_osts(int argc, char **argv)
5518 return lfs_tgts(argc, argv);
5521 static int lfs_mdts(int argc, char **argv)
5523 return lfs_tgts(argc, argv);
5526 static int lfs_df(int argc, char **argv)
5528 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5529 enum mntdf_flags flags = MNTDF_SHOW;
5530 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5531 int c, rc = 0, index = 0;
5532 char fsname[PATH_MAX] = "", *pool_name = NULL;
5533 struct option long_opts[] = {
5534 { .val = 'h', .name = "human-readable",
5535 .has_arg = no_argument },
5536 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5537 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5538 { .val = 'p', .name = "pool", .has_arg = required_argument },
5539 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5542 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5545 flags |= MNTDF_COOKED;
5548 flags |= MNTDF_INODES;
5551 flags |= MNTDF_LAZY;
5557 flags |= MNTDF_VERBOSE;
5563 if (optind < argc && !realpath(argv[optind], path)) {
5565 fprintf(stderr, "error: invalid path '%s': %s\n",
5566 argv[optind], strerror(-rc));
5570 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5571 /* Check if we have a mount point */
5572 if (mntdir[0] == '\0')
5575 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5576 if (rc || path[0] != '\0')
5578 fsname[0] = '\0'; /* avoid matching in next loop */
5579 mntdir[0] = '\0'; /* avoid matching in next loop */
5585 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5586 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5590 /* llapi_search_mounts() fills "fsname", but that is not called if
5591 * explicit paths are specified on the command-line
5593 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5594 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5597 fprintf(stderr, "cannot get instance for '%s': %s\n",
5598 mntdir, strerror(-rc));
5602 if (!opt_instance) {
5603 /* print only the fsname name */
5604 buf = strchr(fsname, '-');
5608 } else if (!opt_fsname) {
5609 /* print only the instance name */
5610 buf = strchr(fsname, '-');
5616 } else if (opt_fsname) {
5617 /* print only the fsname */
5621 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5626 static int lfs_getname(int argc, char **argv)
5628 struct option long_opts[] = {
5629 { .val = 'h', .name = "help", .has_arg = no_argument },
5630 { .val = 'i', .name = "instance", .has_arg = no_argument },
5631 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5633 bool opt_instance = false, opt_fsname = false;
5634 char fsname[PATH_MAX] = "";
5637 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5640 opt_instance = true;
5651 /* If neither option is given, print both instance and fsname */
5652 if (!opt_instance && !opt_fsname)
5653 opt_instance = opt_fsname = true;
5655 if (optind == argc) { /* no paths specified, get all paths. */
5656 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5659 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5660 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5661 opt_instance, opt_fsname, true);
5664 path[0] = fsname[0] = mntdir[0] = '\0';
5666 } else { /* paths specified, only attempt to search these. */
5669 /* if only one path is given, print only requested info */
5670 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5672 for (; optind < argc; optind++) {
5673 rc2 = print_instance(argv[optind], fsname,
5674 sizeof(fsname), opt_instance,
5675 opt_fsname, opt_mntdir);
5685 static int lfs_check(int argc, char **argv)
5687 char mntdir[PATH_MAX] = {'\0'};
5696 fprintf(stderr, "%s check: server type must be specified\n",
5701 obd_types[0] = obd_type1;
5702 obd_types[1] = obd_type2;
5703 obd_types[2] = obd_type3;
5705 if (strcmp(argv[1], "osts") == 0) {
5706 strcpy(obd_types[0], "osc");
5707 } else if (strcmp(argv[1], "mdts") == 0 ||
5708 strcmp(argv[1], "mds") == 0) {
5709 strcpy(obd_types[0], "mdc");
5710 } else if (strcmp(argv[1], "mgts") == 0) {
5711 strcpy(obd_types[0], "mgc");
5712 } else if (strcmp(argv[1], "all") == 0 ||
5713 strcmp(argv[1], "servers") == 0) {
5715 strcpy(obd_types[0], "osc");
5716 strcpy(obd_types[1], "mdc");
5717 strcpy(obd_types[2], "mgc");
5719 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5724 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5725 if (rc < 0 || mntdir[0] == '\0') {
5727 "%s check: cannot find mounted Lustre filesystem: %s\n",
5728 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5732 rc = llapi_target_check(num_types, obd_types, mntdir);
5734 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5735 progname, argv[1], strerror(-rc));
5741 #ifdef HAVE_SYS_QUOTA_H
5742 #define ARG2INT(nr, str, msg) \
5745 nr = strtol(str, &endp, 0); \
5746 if (*endp != '\0') { \
5747 fprintf(stderr, "%s: bad %s '%s'\n", \
5748 progname, msg, str); \
5753 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5755 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5756 * returns the value or ULONG_MAX on integer overflow or incorrect format
5758 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5759 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5760 * 3. empty integer value is interpreted as 0
5762 static unsigned long str2sec(const char* timestr)
5764 const char spec[] = "smhdw";
5765 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5766 unsigned long val = 0;
5769 if (strpbrk(timestr, spec) == NULL) {
5770 /* no specifiers inside the time string,
5771 should treat it as an integer value */
5772 val = strtoul(timestr, &tail, 10);
5773 return *tail ? ULONG_MAX : val;
5776 /* format string is XXwXXdXXhXXmXXs */
5782 v = strtoul(timestr, &tail, 10);
5783 if (v == ULONG_MAX || *tail == '\0')
5784 /* value too large (ULONG_MAX or more)
5785 or missing specifier */
5788 ptr = strchr(spec, *tail);
5790 /* unknown specifier */
5795 /* check if product will overflow the type */
5796 if (!(v < ULONG_MAX / mult[ind]))
5799 ADD_OVERFLOW(val, mult[ind] * v);
5800 if (val == ULONG_MAX)
5812 #define ARG2ULL(nr, str, def_units) \
5814 unsigned long long limit, units = def_units; \
5817 rc = llapi_parse_size(str, &limit, &units, 1); \
5819 fprintf(stderr, "%s: invalid limit '%s'\n", \
5826 static inline int has_times_option(int argc, char **argv)
5830 for (i = 1; i < argc; i++)
5831 if (!strcmp(argv[i], "-t"))
5837 int lfs_setquota_times(int argc, char **argv)
5840 struct if_quotactl qctl;
5841 char *mnt, *obd_type = (char *)qctl.obd_type;
5842 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5843 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5844 struct option long_opts[] = {
5845 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5846 { .val = 'g', .name = "group", .has_arg = no_argument },
5847 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5848 { .val = 'p', .name = "projid", .has_arg = no_argument },
5849 { .val = 't', .name = "times", .has_arg = no_argument },
5850 { .val = 'u', .name = "user", .has_arg = no_argument },
5854 memset(&qctl, 0, sizeof(qctl));
5855 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5856 qctl.qc_type = ALLQUOTA;
5858 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5859 long_opts, NULL)) != -1) {
5870 if (qctl.qc_type != ALLQUOTA) {
5871 fprintf(stderr, "error: -u/g/p can't be used "
5872 "more than once\n");
5875 qctl.qc_type = qtype;
5878 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5879 fprintf(stderr, "error: bad block-grace: %s\n",
5883 dqb->dqb_valid |= QIF_BTIME;
5886 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5887 fprintf(stderr, "error: bad inode-grace: %s\n",
5891 dqb->dqb_valid |= QIF_ITIME;
5893 case 't': /* Yes, of course! */
5895 default: /* getopt prints error message for us when opterr != 0 */
5900 if (qctl.qc_type == ALLQUOTA) {
5901 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5905 if (optind != argc - 1) {
5906 fprintf(stderr, "error: unexpected parameters encountered\n");
5910 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5911 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5912 fprintf(stderr, "error: grace time is too large\n");
5917 rc = llapi_quotactl(mnt, &qctl);
5920 fprintf(stderr, "%s %s ", obd_type,
5921 obd_uuid2str(&qctl.obd_uuid));
5922 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5929 #define BSLIMIT (1 << 0)
5930 #define BHLIMIT (1 << 1)
5931 #define ISLIMIT (1 << 2)
5932 #define IHLIMIT (1 << 3)
5934 int lfs_setquota(int argc, char **argv)
5937 struct if_quotactl qctl;
5938 char *mnt, *obd_type = (char *)qctl.obd_type;
5939 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5940 struct option long_opts[] = {
5941 { .val = 'b', .name = "block-softlimit",
5942 .has_arg = required_argument },
5943 { .val = 'B', .name = "block-hardlimit",
5944 .has_arg = required_argument },
5945 { .val = 'd', .name = "default", .has_arg = no_argument },
5946 { .val = 'g', .name = "group", .has_arg = required_argument },
5947 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5948 { .val = 'i', .name = "inode-softlimit",
5949 .has_arg = required_argument },
5950 { .val = 'I', .name = "inode-hardlimit",
5951 .has_arg = required_argument },
5952 { .val = 'p', .name = "projid", .has_arg = required_argument },
5953 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5954 { .val = 'u', .name = "user", .has_arg = required_argument },
5955 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5957 unsigned limit_mask = 0;
5959 bool use_default = false;
5962 if (has_times_option(argc, argv))
5963 return lfs_setquota_times(argc, argv);
5965 memset(&qctl, 0, sizeof(qctl));
5966 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5967 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5968 * so it can be used as a marker that qc_type
5969 * isn't reinitialized from command line */
5971 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5972 long_opts, NULL)) != -1) {
5975 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5978 goto quota_type_def;
5981 rc = name2uid(&qctl.qc_id, optarg);
5984 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5987 goto quota_type_def;
5990 rc = name2gid(&qctl.qc_id, optarg);
5993 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5996 goto quota_type_def;
5999 rc = name2projid(&qctl.qc_id, optarg);
6002 qctl.qc_id = strtoul(optarg, &endptr, 10);
6003 if (*endptr != '\0') {
6004 fprintf(stderr, "%s setquota: invalid"
6005 " id '%s'\n", progname, optarg);
6010 if (qctl.qc_id == 0) {
6011 fprintf(stderr, "%s setquota: can't set quota"
6012 " for root usr/group/project.\n",
6018 if (qctl.qc_type != ALLQUOTA) {
6020 "%s setquota: only one of -u, -U, -g,"
6021 " -G, -p or -P may be specified\n",
6025 qctl.qc_type = qtype;
6028 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6032 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6033 dqb->dqb_bsoftlimit >>= 10;
6034 limit_mask |= BSLIMIT;
6035 if (dqb->dqb_bsoftlimit &&
6036 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6038 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6039 "See '%s help setquota' or Lustre manual for details\n",
6040 progname, dqb->dqb_bsoftlimit,
6044 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6045 dqb->dqb_bhardlimit >>= 10;
6046 limit_mask |= BHLIMIT;
6047 if (dqb->dqb_bhardlimit &&
6048 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6050 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6051 "See '%s help setquota' or Lustre manual for details\n",
6052 progname, dqb->dqb_bhardlimit,
6056 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6057 limit_mask |= ISLIMIT;
6058 if (dqb->dqb_isoftlimit &&
6059 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6061 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6062 "See '%s help setquota' or Lustre manual for details\n",
6063 progname, dqb->dqb_isoftlimit,
6067 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6068 limit_mask |= IHLIMIT;
6069 if (dqb->dqb_ihardlimit &&
6070 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6072 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6073 "See '%s help setquota' or Lustre manual for details\n",
6074 progname, dqb->dqb_ihardlimit,
6079 "%s setquota: unrecognized option '%s'\n",
6080 progname, argv[optind - 1]);
6085 if (qctl.qc_type == ALLQUOTA) {
6087 "%s setquota: either -u or -g must be specified\n",
6092 if (!use_default && limit_mask == 0) {
6094 "%s setquota: at least one limit must be specified\n",
6099 if (use_default && limit_mask != 0) {
6101 "%s setquota: limits should not be specified when"
6102 " using default quota\n",
6107 if (use_default && qctl.qc_id == 0) {
6109 "%s setquota: can not set default quota for root"
6110 " user/group/project\n",
6115 if (optind != argc - 1) {
6117 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6118 progname, argv[optind]);
6125 dqb->dqb_bhardlimit = 0;
6126 dqb->dqb_bsoftlimit = 0;
6127 dqb->dqb_ihardlimit = 0;
6128 dqb->dqb_isoftlimit = 0;
6131 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6132 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6133 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6134 /* sigh, we can't just set blimits/ilimits */
6135 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6136 .qc_type = qctl.qc_type,
6137 .qc_id = qctl.qc_id};
6139 rc = llapi_quotactl(mnt, &tmp_qctl);
6143 if (!(limit_mask & BHLIMIT))
6144 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6145 if (!(limit_mask & BSLIMIT))
6146 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6147 if (!(limit_mask & IHLIMIT))
6148 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6149 if (!(limit_mask & ISLIMIT))
6150 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6152 /* Keep grace times if we have got no softlimit arguments */
6153 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6154 dqb->dqb_valid |= QIF_BTIME;
6155 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6158 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6159 dqb->dqb_valid |= QIF_ITIME;
6160 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6164 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6165 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6167 rc = llapi_quotactl(mnt, &qctl);
6171 "%s setquota: cannot quotactl '%s' '%s': %s",
6173 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6180 /* Converts seconds value into format string
6181 * result is returned in buf
6183 * 1. result is in descenting order: 1w2d3h4m5s
6184 * 2. zero fields are not filled (except for p. 3): 5d1s
6185 * 3. zero seconds value is presented as "0s"
6187 static char * __sec2str(time_t seconds, char *buf)
6189 const char spec[] = "smhdw";
6190 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6195 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6196 c = seconds / mult[i];
6198 if (c > 0 || (i == 0 && buf == tail))
6199 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6207 static void sec2str(time_t seconds, char *buf, int rc)
6214 tail = __sec2str(seconds, tail);
6216 if (rc && tail - buf < 39) {
6222 static void diff2str(time_t seconds, char *buf, time_t now)
6228 if (seconds <= now) {
6229 strcpy(buf, "none");
6232 __sec2str(seconds - now, buf);
6235 static void print_quota_title(char *name, struct if_quotactl *qctl,
6236 bool human_readable, bool show_default)
6239 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6240 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6241 "Filesystem", "bquota", "blimit", "bgrace",
6242 "iquota", "ilimit", "igrace");
6244 printf("Disk quotas for %s %s (%cid %u):\n",
6245 qtype_name(qctl->qc_type), name,
6246 *qtype_name(qctl->qc_type), qctl->qc_id);
6247 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6248 "Filesystem", human_readable ? "used" : "kbytes",
6249 "quota", "limit", "grace",
6250 "files", "quota", "limit", "grace");
6254 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6257 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6260 snprintf(buf, buflen, "%5.4gP",
6261 (double)num / ((__u64)1 << 40));
6263 snprintf(buf, buflen, "%5.4gT",
6264 (double)num / (1 << 30));
6266 snprintf(buf, buflen, "%5.4gG",
6267 (double)num / (1 << 20));
6269 snprintf(buf, buflen, "%5.4gM",
6270 (double)num / (1 << 10));
6272 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6276 #define STRBUF_LEN 32
6277 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6278 int rc, bool h, bool show_default)
6284 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6285 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6286 int bover = 0, iover = 0;
6287 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6288 char numbuf[3][STRBUF_LEN];
6290 char strbuf[STRBUF_LEN];
6292 if (dqb->dqb_bhardlimit &&
6293 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6295 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6296 if (dqb->dqb_btime > now) {
6303 if (dqb->dqb_ihardlimit &&
6304 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6306 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6307 if (dqb->dqb_itime > now) {
6315 if (strlen(mnt) > 15)
6316 printf("%s\n%15s", mnt, "");
6318 printf("%15s", mnt);
6321 diff2str(dqb->dqb_btime, timebuf, now);
6322 else if (show_default)
6323 snprintf(timebuf, sizeof(timebuf), "%llu",
6326 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6327 strbuf, sizeof(strbuf), h);
6328 if (rc == -EREMOTEIO)
6329 sprintf(numbuf[0], "%s*", strbuf);
6331 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6332 "%s" : "[%s]", strbuf);
6334 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6335 if (type == QC_GENERAL)
6336 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6337 "%s" : "[%s]", strbuf);
6339 sprintf(numbuf[1], "%s", "-");
6341 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6342 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6343 "%s" : "[%s]", strbuf);
6346 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6348 printf(" %7s%c %6s %7s %7s",
6349 numbuf[0], bover ? '*' : ' ', numbuf[1],
6350 numbuf[2], bover > 1 ? timebuf : "-");
6354 diff2str(dqb->dqb_itime, timebuf, now);
6355 else if (show_default)
6356 snprintf(timebuf, sizeof(timebuf), "%llu",
6359 snprintf(numbuf[0], sizeof(numbuf),
6360 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6361 (uintmax_t)dqb->dqb_curinodes);
6363 if (type == QC_GENERAL)
6364 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6366 (uintmax_t)dqb->dqb_isoftlimit);
6368 sprintf(numbuf[1], "%s", "-");
6370 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6371 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6374 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6375 else if (type != QC_OSTIDX)
6376 printf(" %7s%c %6s %7s %7s",
6377 numbuf[0], iover ? '*' : ' ', numbuf[1],
6378 numbuf[2], iover > 1 ? timebuf : "-");
6380 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6383 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6384 qctl->qc_cmd == Q_GETOINFO) {
6388 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6389 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6390 printf("Block grace time: %s; Inode grace time: %s\n",
6391 bgtimebuf, igtimebuf);
6395 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6396 bool h, __u64 *total)
6398 int rc = 0, rc1 = 0, count = 0;
6399 __u32 valid = qctl->qc_valid;
6401 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6403 fprintf(stderr, "can not get %s count: %s\n",
6404 is_mdt ? "mdt": "ost", strerror(-rc));
6408 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6409 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6410 rc = llapi_quotactl(mnt, qctl);
6412 /* It is remote client case. */
6413 if (rc == -EOPNOTSUPP) {
6420 fprintf(stderr, "quotactl %s%d failed.\n",
6421 is_mdt ? "mdt": "ost", qctl->qc_idx);
6425 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6426 qctl->qc_valid, 0, h, false);
6427 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6428 qctl->qc_dqblk.dqb_bhardlimit;
6431 qctl->qc_valid = valid;
6435 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6436 int verbose, int quiet, bool human_readable,
6439 int rc1 = 0, rc2 = 0, rc3 = 0;
6440 char *obd_type = (char *)qctl->obd_type;
6441 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6442 __u64 total_ialloc = 0, total_balloc = 0;
6443 bool use_default_for_blk = false;
6444 bool use_default_for_file = false;
6447 rc1 = llapi_quotactl(mnt, qctl);
6451 fprintf(stderr, "%s quotas are not enabled.\n",
6452 qtype_name(qctl->qc_type));
6455 fprintf(stderr, "Permission denied.\n");
6458 /* We already got error message. */
6461 fprintf(stderr, "Unexpected quotactl error: %s\n",
6466 if (!show_default && qctl->qc_id == 0) {
6467 qctl->qc_dqblk.dqb_bhardlimit = 0;
6468 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6469 qctl->qc_dqblk.dqb_ihardlimit = 0;
6470 qctl->qc_dqblk.dqb_isoftlimit = 0;
6471 qctl->qc_dqblk.dqb_btime = 0;
6472 qctl->qc_dqblk.dqb_itime = 0;
6473 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6476 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6477 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6478 use_default_for_blk = true;
6479 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6482 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6483 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6484 use_default_for_file = true;
6485 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6488 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6489 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6490 print_quota_title(name, qctl, human_readable, show_default);
6492 if (rc1 && *obd_type)
6493 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6495 if (qctl->qc_valid != QC_GENERAL)
6498 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6499 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6500 (QIF_LIMITS|QIF_USAGE));
6502 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6504 if (!show_default && verbose &&
6505 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6506 char strbuf[STRBUF_LEN];
6508 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6510 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6512 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6514 printf("Total allocated inode limit: %ju, total "
6515 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6519 if (use_default_for_blk)
6520 printf("%cid %u is using default block quota setting\n",
6521 *qtype_name(qctl->qc_type), qctl->qc_id);
6523 if (use_default_for_file)
6524 printf("%cid %u is using default file quota setting\n",
6525 *qtype_name(qctl->qc_type), qctl->qc_id);
6527 if (rc1 || rc2 || rc3 || inacc)
6528 printf("Some errors happened when getting quota info. "
6529 "Some devices may be not working or deactivated. "
6530 "The data in \"[]\" is inaccurate.\n");
6536 static int lfs_project(int argc, char **argv)
6538 int ret = 0, err = 0, c, i;
6539 struct project_handle_control phc = { 0 };
6540 enum lfs_project_ops_t op;
6543 phc.assign_projid = false;
6544 /* default action */
6545 op = LFS_PROJECT_LIST;
6547 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6550 if (op != LFS_PROJECT_LIST) {
6552 "%s: cannot specify '-c' '-C' '-s' together\n",
6557 op = LFS_PROJECT_CHECK;
6560 if (op != LFS_PROJECT_LIST) {
6562 "%s: cannot specify '-c' '-C' '-s' together\n",
6567 op = LFS_PROJECT_CLEAR;
6570 if (op != LFS_PROJECT_LIST) {
6572 "%s: cannot specify '-c' '-C' '-s' together\n",
6577 phc.set_inherit = true;
6578 op = LFS_PROJECT_SET;
6584 phc.keep_projid = true;
6587 phc.recursive = true;
6590 phc.projid = strtoul(optarg, NULL, 0);
6591 phc.assign_projid = true;
6595 phc.newline = false;
6598 fprintf(stderr, "%s: invalid option '%c'\n",
6604 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6605 op = LFS_PROJECT_SET;
6606 phc.set_projid = true;
6607 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6608 phc.set_projid = true;
6612 case LFS_PROJECT_CHECK:
6613 if (phc.keep_projid) {
6615 "%s: '-k' is useless together with '-c'\n",
6620 case LFS_PROJECT_CLEAR:
6623 "%s: '-0' is useless together with '-C'\n",
6627 if (phc.assign_projid) {
6629 "%s: '-p' is useless together with '-C'\n",
6634 case LFS_PROJECT_SET:
6637 "%s: '-0' is useless together with '-s'\n",
6641 if (phc.keep_projid) {
6643 "%s: '-k' is useless together with '-s'\n",
6651 "%s: '-0' is useless for list operations\n",
6661 fprintf(stderr, "%s: missing file or directory target(s)\n",
6666 for (i = 0; i < argc; i++) {
6668 case LFS_PROJECT_CHECK:
6669 err = lfs_project_check(argv[i], &phc);
6671 case LFS_PROJECT_LIST:
6672 err = lfs_project_list(argv[i], &phc);
6674 case LFS_PROJECT_CLEAR:
6675 err = lfs_project_clear(argv[i], &phc);
6677 case LFS_PROJECT_SET:
6678 err = lfs_project_set(argv[i], &phc);
6690 static int lfs_quota(int argc, char **argv)
6693 char *mnt, *name = NULL;
6694 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6695 .qc_type = ALLQUOTA };
6696 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6697 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6699 __u32 valid = QC_GENERAL, idx = 0;
6700 bool human_readable = false;
6701 bool show_default = false;
6704 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6707 show_default = true;
6712 show_default = true;
6717 show_default = true;
6721 if (qctl.qc_type != ALLQUOTA) {
6723 "%s quota: only one of -u, -g, or -p may be specified\n",
6727 qctl.qc_type = qtype;
6730 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6733 valid = qctl.qc_valid = QC_UUID;
6734 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6737 valid = qctl.qc_valid = QC_MDTIDX;
6738 idx = qctl.qc_idx = atoi(optarg);
6739 if (idx == 0 && *optarg != '0') {
6741 "%s quota: invalid MDT index '%s'\n",
6747 valid = qctl.qc_valid = QC_OSTIDX;
6748 idx = qctl.qc_idx = atoi(optarg);
6749 if (idx == 0 && *optarg != '0') {
6751 "%s quota: invalid OST index '%s'\n",
6763 human_readable = true;
6766 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6767 progname, argv[optind - 1]);
6772 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6773 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6774 optind == argc - 1 && !show_default) {
6776 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6777 qctl.qc_valid = valid;
6780 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6781 qctl.qc_type = qtype;
6782 if (qtype == USRQUOTA) {
6783 qctl.qc_id = geteuid();
6784 rc = uid2name(&name, qctl.qc_id);
6786 qctl.qc_id = getegid();
6787 rc = gid2name(&name, qctl.qc_id);
6792 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6793 human_readable, show_default);
6798 /* lfs quota -u username /path/to/lustre/mount */
6799 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6800 /* options should be followed by u/g-name and mntpoint */
6801 if ((!show_default && optind + 2 != argc) ||
6802 (show_default && optind + 1 != argc) ||
6803 qctl.qc_type == ALLQUOTA) {
6805 "%s quota: name and mount point must be specified\n",
6810 if (!show_default) {
6811 name = argv[optind++];
6812 switch (qctl.qc_type) {
6814 rc = name2uid(&qctl.qc_id, name);
6817 rc = name2gid(&qctl.qc_id, name);
6820 rc = name2projid(&qctl.qc_id, name);
6827 qctl.qc_valid = QC_GENERAL;
6828 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6833 qctl.qc_id = strtoul(name, &endptr, 10);
6834 if (*endptr != '\0') {
6835 fprintf(stderr, "%s quota: invalid id '%s'\n",
6840 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6841 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6847 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6848 human_readable, show_default);
6851 #endif /* HAVE_SYS_QUOTA_H! */
6853 static int flushctx_ioctl(char *mp)
6857 fd = open(mp, O_RDONLY);
6859 fprintf(stderr, "flushctx: error open %s: %s\n",
6860 mp, strerror(errno));
6864 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6866 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6867 mp, strerror(errno));
6873 static int lfs_flushctx(int argc, char **argv)
6875 int kdestroy = 0, c;
6876 char mntdir[PATH_MAX] = {'\0'};
6880 while ((c = getopt(argc, argv, "k")) != -1) {
6886 fprintf(stderr, "error: %s: option '-%c' "
6887 "unrecognized\n", argv[0], c);
6893 if ((rc = system("kdestroy > /dev/null")) != 0) {
6894 rc = WEXITSTATUS(rc);
6895 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6899 if (optind >= argc) {
6900 /* flush for all mounted lustre fs. */
6901 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6902 /* Check if we have a mount point */
6903 if (mntdir[0] == '\0')
6906 if (flushctx_ioctl(mntdir))
6909 mntdir[0] = '\0'; /* avoid matching in next loop */
6912 /* flush fs as specified */
6913 while (optind < argc) {
6914 if (flushctx_ioctl(argv[optind++]))
6921 static int lfs_changelog(int argc, char **argv)
6923 void *changelog_priv;
6924 struct changelog_rec *rec;
6925 long long startrec = 0, endrec = 0;
6927 struct option long_opts[] = {
6928 { .val = 'f', .name = "follow", .has_arg = no_argument },
6930 char short_opts[] = "f";
6933 while ((rc = getopt_long(argc, argv, short_opts,
6934 long_opts, NULL)) != -1) {
6941 "%s changelog: unrecognized option '%s'\n",
6942 progname, argv[optind - 1]);
6946 if (optind >= argc) {
6947 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6952 mdd = argv[optind++];
6954 startrec = strtoll(argv[optind++], NULL, 10);
6956 endrec = strtoll(argv[optind++], NULL, 10);
6958 rc = llapi_changelog_start(&changelog_priv,
6959 CHANGELOG_FLAG_BLOCK |
6960 CHANGELOG_FLAG_JOBID |
6961 CHANGELOG_FLAG_EXTRA_FLAGS |
6962 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6965 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6966 progname, strerror(errno = -rc));
6970 rc = llapi_changelog_set_xflags(changelog_priv,
6971 CHANGELOG_EXTRA_FLAG_UIDGID |
6972 CHANGELOG_EXTRA_FLAG_NID |
6973 CHANGELOG_EXTRA_FLAG_OMODE |
6974 CHANGELOG_EXTRA_FLAG_XATTR);
6977 "%s changelog: cannot set xflags for changelog: %s\n",
6978 progname, strerror(errno = -rc));
6982 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6986 if (endrec && rec->cr_index > endrec) {
6987 llapi_changelog_free(&rec);
6990 if (rec->cr_index < startrec) {
6991 llapi_changelog_free(&rec);
6995 secs = rec->cr_time >> 30;
6996 gmtime_r(&secs, &ts);
6997 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6998 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6999 changelog_type2str(rec->cr_type),
7000 ts.tm_hour, ts.tm_min, ts.tm_sec,
7001 (int)(rec->cr_time & ((1 << 30) - 1)),
7002 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7003 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7005 if (rec->cr_flags & CLF_JOBID) {
7006 struct changelog_ext_jobid *jid =
7007 changelog_rec_jobid(rec);
7009 if (jid->cr_jobid[0] != '\0')
7010 printf(" j=%s", jid->cr_jobid);
7013 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7014 struct changelog_ext_extra_flags *ef =
7015 changelog_rec_extra_flags(rec);
7017 printf(" ef=0x%llx", ef->cr_extra_flags);
7019 if (ef->cr_extra_flags & CLFE_UIDGID) {
7020 struct changelog_ext_uidgid *uidgid =
7021 changelog_rec_uidgid(rec);
7023 printf(" u=%llu:%llu",
7024 uidgid->cr_uid, uidgid->cr_gid);
7026 if (ef->cr_extra_flags & CLFE_NID) {
7027 struct changelog_ext_nid *nid =
7028 changelog_rec_nid(rec);
7031 libcfs_nid2str(nid->cr_nid));
7034 if (ef->cr_extra_flags & CLFE_OPEN) {
7035 struct changelog_ext_openmode *omd =
7036 changelog_rec_openmode(rec);
7037 char mode[] = "---";
7039 /* exec mode must be exclusive */
7040 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7043 if (omd->cr_openflags & MDS_FMODE_READ)
7045 if (omd->cr_openflags &
7052 if (strcmp(mode, "---") != 0)
7053 printf(" m=%s", mode);
7057 if (ef->cr_extra_flags & CLFE_XATTR) {
7058 struct changelog_ext_xattr *xattr =
7059 changelog_rec_xattr(rec);
7061 if (xattr->cr_xattr[0] != '\0')
7062 printf(" x=%s", xattr->cr_xattr);
7066 if (rec->cr_namelen)
7067 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7068 rec->cr_namelen, changelog_rec_name(rec));
7070 if (rec->cr_flags & CLF_RENAME) {
7071 struct changelog_ext_rename *rnm =
7072 changelog_rec_rename(rec);
7074 if (!fid_is_zero(&rnm->cr_sfid))
7075 printf(" s="DFID" sp="DFID" %.*s",
7076 PFID(&rnm->cr_sfid),
7077 PFID(&rnm->cr_spfid),
7078 (int)changelog_rec_snamelen(rec),
7079 changelog_rec_sname(rec));
7083 llapi_changelog_free(&rec);
7086 llapi_changelog_fini(&changelog_priv);
7089 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7090 progname, strerror(errno = -rc));
7092 return (rc == 1 ? 0 : rc);
7095 static int lfs_changelog_clear(int argc, char **argv)
7103 endrec = strtoll(argv[3], NULL, 10);
7105 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7108 fprintf(stderr, "%s: record out of range: %llu\n",
7110 else if (rc == -ENOENT)
7111 fprintf(stderr, "%s: no changelog user: %s\n",
7114 fprintf(stderr, "%s error: %s\n", argv[0],
7123 static int lfs_fid2path(int argc, char **argv)
7125 struct option long_opts[] = {
7126 { .val = 'c', .name = "cur", .has_arg = no_argument },
7127 { .val = 'l', .name = "link", .has_arg = required_argument },
7128 { .val = 'r', .name = "rec", .has_arg = required_argument },
7130 char short_opts[] = "cl:r:";
7131 char *device, *fid, *path;
7132 long long recno = -1;
7137 char *endptr = NULL;
7139 while ((rc = getopt_long(argc, argv, short_opts,
7140 long_opts, NULL)) != -1) {
7146 linkno = strtol(optarg, &endptr, 10);
7147 if (*endptr != '\0') {
7149 "%s fid2path: invalid linkno '%s'\n",
7155 recno = strtoll(optarg, &endptr, 10);
7156 if (*endptr != '\0') {
7158 "%s fid2path: invalid recno '%s'\n",
7165 "%s fid2path: unrecognized option '%s'\n",
7166 progname, argv[optind - 1]);
7173 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7178 device = argv[optind++];
7179 path = calloc(1, PATH_MAX);
7183 "%s fid2path: cannot allocate memory for path: %s\n",
7184 progname, strerror(-rc));
7189 while (optind < argc) {
7190 fid = argv[optind++];
7192 lnktmp = (linkno >= 0) ? linkno : 0;
7194 int oldtmp = lnktmp;
7195 long long rectmp = recno;
7197 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7201 "%s fid2path: cannot find '%s': %s\n",
7202 progname, fid, strerror(errno = -rc2));
7209 fprintf(stdout, "%lld ", rectmp);
7210 if (device[0] == '/') {
7211 fprintf(stdout, "%s", device);
7212 if (device[strlen(device) - 1] != '/')
7213 fprintf(stdout, "/");
7214 } else if (path[0] == '\0') {
7215 fprintf(stdout, "/");
7217 fprintf(stdout, "%s\n", path);
7220 /* specified linkno */
7222 if (oldtmp == lnktmp)
7232 static int lfs_path2fid(int argc, char **argv)
7234 struct option long_opts[] = {
7235 { .val = 'p', .name = "parents", .has_arg = no_argument },
7238 const char short_opts[] = "p";
7239 const char *sep = "";
7242 bool show_parents = false;
7244 while ((rc = getopt_long(argc, argv, short_opts,
7245 long_opts, NULL)) != -1) {
7248 show_parents = true;
7252 "%s path2fid: unrecognized option '%s'\n",
7253 progname, argv[optind - 1]);
7258 if (optind > argc - 1) {
7259 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7263 else if (optind < argc - 1)
7267 for (path = argv + optind; *path != NULL; path++) {
7269 if (!show_parents) {
7270 err = llapi_path2fid(*path, &fid);
7272 printf("%s%s"DFID"\n",
7273 *sep != '\0' ? *path : "", sep,
7276 char name[NAME_MAX + 1];
7277 unsigned int linkno = 0;
7279 while ((err = llapi_path2parent(*path, linkno, &fid,
7280 name, sizeof(name))) == 0) {
7281 if (*sep != '\0' && linkno == 0)
7282 printf("%s%s", *path, sep);
7284 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7289 /* err == -ENODATA is end-of-loop */
7290 if (linkno > 0 && err == -ENODATA) {
7298 "%s path2fid: cannot get %sfid for '%s': %s\n",
7299 progname, show_parents ? "parent " : "", *path,
7311 static int lfs_data_version(int argc, char **argv)
7318 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7321 fprintf(stderr, "%s data_version: FILE must be specified\n",
7326 while ((c = getopt(argc, argv, "nrw")) != -1) {
7329 data_version_flags = 0;
7332 data_version_flags |= LL_DV_RD_FLUSH;
7335 data_version_flags |= LL_DV_WR_FLUSH;
7339 "%s data_version: unrecognized option '%s'\n",
7340 progname, argv[optind - 1]);
7344 if (optind == argc) {
7345 fprintf(stderr, "%s data_version: FILE must be specified\n",
7350 path = argv[optind];
7351 fd = open(path, O_RDONLY);
7354 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7355 progname, path, strerror(-rc));
7359 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7362 "%s data_version: cannot get version for '%s': %s\n",
7363 progname, path, strerror(-rc));
7365 printf("%ju" "\n", (uintmax_t)data_version);
7371 static int lfs_hsm_state(int argc, char **argv)
7376 struct hsm_user_state hus;
7384 rc = llapi_hsm_state_get(path, &hus);
7386 fprintf(stderr, "can't get hsm state for %s: %s\n",
7387 path, strerror(errno = -rc));
7391 /* Display path name and status flags */
7392 printf("%s: (0x%08x)", path, hus.hus_states);
7394 if (hus.hus_states & HS_RELEASED)
7395 printf(" released");
7396 if (hus.hus_states & HS_EXISTS)
7398 if (hus.hus_states & HS_DIRTY)
7400 if (hus.hus_states & HS_ARCHIVED)
7401 printf(" archived");
7402 /* Display user-settable flags */
7403 if (hus.hus_states & HS_NORELEASE)
7404 printf(" never_release");
7405 if (hus.hus_states & HS_NOARCHIVE)
7406 printf(" never_archive");
7407 if (hus.hus_states & HS_LOST)
7408 printf(" lost_from_hsm");
7410 if (hus.hus_archive_id != 0)
7411 printf(", archive_id:%d", hus.hus_archive_id);
7414 } while (++i < argc);
7419 #define LFS_HSM_SET 0
7420 #define LFS_HSM_CLEAR 1
7423 * Generic function to set or clear HSM flags.
7424 * Used by hsm_set and hsm_clear.
7426 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7428 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7430 struct option long_opts[] = {
7431 { .val = 'A', .name = "archived", .has_arg = no_argument },
7432 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7433 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7434 { .val = 'e', .name = "exists", .has_arg = no_argument },
7435 { .val = 'l', .name = "lost", .has_arg = no_argument },
7436 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7437 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7439 char short_opts[] = "lraAdei:";
7443 __u32 archive_id = 0;
7449 while ((c = getopt_long(argc, argv, short_opts,
7450 long_opts, NULL)) != -1) {
7456 mask |= HS_NOARCHIVE;
7459 mask |= HS_ARCHIVED;
7462 mask |= HS_NORELEASE;
7471 archive_id = strtol(optarg, &end, 10);
7473 fprintf(stderr, "invalid archive_id: '%s'\n",
7481 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7482 argv[0], argv[optind - 1]);
7487 /* User should have specified a flag */
7491 while (optind < argc) {
7493 path = argv[optind];
7495 /* If mode == 0, this means we apply the mask. */
7496 if (mode == LFS_HSM_SET)
7497 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7499 rc = llapi_hsm_state_set(path, 0, mask, 0);
7502 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7503 path, strerror(errno = -rc));
7512 static int lfs_hsm_action(int argc, char **argv)
7517 struct hsm_current_action hca;
7518 struct hsm_extent he;
7519 enum hsm_user_action hua;
7520 enum hsm_progress_states hps;
7528 rc = llapi_hsm_current_action(path, &hca);
7530 fprintf(stderr, "can't get hsm action for %s: %s\n",
7531 path, strerror(errno = -rc));
7534 he = hca.hca_location;
7535 hua = hca.hca_action;
7536 hps = hca.hca_state;
7538 printf("%s: %s", path, hsm_user_action2name(hua));
7540 /* Skip file without action */
7541 if (hca.hca_action == HUA_NONE) {
7546 printf(" %s ", hsm_progress_state2name(hps));
7548 if ((hps == HPS_RUNNING) &&
7549 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7550 printf("(%llu bytes moved)\n",
7551 (unsigned long long)he.length);
7552 else if ((he.offset + he.length) == LUSTRE_EOF)
7553 printf("(from %llu to EOF)\n",
7554 (unsigned long long)he.offset);
7556 printf("(from %llu to %llu)\n",
7557 (unsigned long long)he.offset,
7558 (unsigned long long)(he.offset + he.length));
7560 } while (++i < argc);
7565 static int lfs_hsm_set(int argc, char **argv)
7567 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7570 static int lfs_hsm_clear(int argc, char **argv)
7572 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7576 * Check file state and return its fid, to be used by lfs_hsm_request().
7578 * \param[in] file Path to file to check
7579 * \param[in,out] fid Pointer to allocated lu_fid struct.
7580 * \param[in,out] last_dev Pointer to last device id used.
7582 * \return 0 on success.
7584 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7590 rc = lstat(file, &st);
7592 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7595 /* Checking for regular file as archiving as posix copytool
7596 * rejects archiving files other than regular files
7598 if (!S_ISREG(st.st_mode)) {
7599 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7602 /* A request should be ... */
7603 if (*last_dev != st.st_dev && *last_dev != 0) {
7604 fprintf(stderr, "All files should be "
7605 "on the same filesystem: %s\n", file);
7608 *last_dev = st.st_dev;
7610 rc = llapi_path2fid(file, fid);
7612 fprintf(stderr, "Cannot read FID of %s: %s\n",
7613 file, strerror(-rc));
7619 /* Fill an HSM HUR item with a given file name.
7621 * If mntpath is set, then the filename is actually a FID, and no
7622 * lookup on the filesystem will be performed.
7624 * \param[in] hur the user request to fill
7625 * \param[in] idx index of the item inside the HUR to fill
7626 * \param[in] mntpath mountpoint of Lustre
7627 * \param[in] fname filename (if mtnpath is NULL)
7628 * or FID (if mntpath is set)
7629 * \param[in] last_dev pointer to last device id used
7631 * \retval 0 on success
7632 * \retval CMD_HELP or a negative errno on error
7634 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7635 const char *mntpath, const char *fname,
7638 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7641 hui->hui_extent.length = -1;
7643 if (mntpath != NULL) {
7646 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7650 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7655 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7659 hur->hur_request.hr_itemcount++;
7664 static int lfs_hsm_request(int argc, char **argv, int action)
7666 struct option long_opts[] = {
7667 { .val = 'a', .name = "archive", .has_arg = required_argument },
7668 { .val = 'D', .name = "data", .has_arg = required_argument },
7669 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7670 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7673 char short_opts[] = "l:D:a:m:";
7674 struct hsm_user_request *hur, *oldhur;
7679 char *filelist = NULL;
7680 char fullpath[PATH_MAX];
7681 char *opaque = NULL;
7685 int nbfile_alloc = 0;
7686 char *some_file = NULL;
7687 char *mntpath = NULL;
7693 while ((c = getopt_long(argc, argv, short_opts,
7694 long_opts, NULL)) != -1) {
7703 if (action != HUA_ARCHIVE &&
7704 action != HUA_REMOVE) {
7706 "error: -a is supported only "
7707 "when archiving or removing\n");
7710 archive_id = atoi(optarg);
7713 if (some_file == NULL) {
7715 some_file = strdup(optarg);
7721 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7722 argv[0], argv[optind - 1]);
7727 /* All remaining args are files, so we have at least nbfile */
7728 nbfile = argc - optind;
7730 if ((nbfile == 0) && (filelist == NULL))
7734 opaque_len = strlen(opaque);
7736 /* Alloc the request structure with enough place to store all files
7737 * from command line. */
7738 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7740 fprintf(stderr, "Cannot create the request: %s\n",
7744 nbfile_alloc = nbfile;
7746 hur->hur_request.hr_action = action;
7747 hur->hur_request.hr_archive_id = archive_id;
7748 hur->hur_request.hr_flags = 0;
7750 /* All remaining args are files, add them */
7751 if (nbfile != 0 && some_file == NULL)
7752 some_file = strdup(argv[optind]);
7754 for (i = 0; i < nbfile; i++) {
7755 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7761 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7763 /* If a filelist was specified, read the filelist from it. */
7764 if (filelist != NULL) {
7765 fp = fopen(filelist, "r");
7767 fprintf(stderr, "Cannot read the file list %s: %s\n",
7768 filelist, strerror(errno));
7773 while ((rc = getline(&line, &len, fp)) != -1) {
7774 /* If allocated buffer was too small, get something
7776 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7779 nbfile_alloc = nbfile_alloc * 2 + 1;
7781 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7784 fprintf(stderr, "hsm: cannot allocate "
7785 "the request: %s\n",
7792 size = hur_len(oldhur);
7794 fprintf(stderr, "hsm: cannot allocate "
7795 "%u files + %u bytes data\n",
7796 oldhur->hur_request.hr_itemcount,
7797 oldhur->hur_request.hr_data_len);
7804 memcpy(hur, oldhur, size);
7809 if (line[strlen(line) - 1] == '\n')
7810 line[strlen(line) - 1] = '\0';
7812 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7813 mntpath, line, &last_dev);
7819 if (some_file == NULL) {
7829 /* If a --data was used, add it to the request */
7830 hur->hur_request.hr_data_len = opaque_len;
7832 memcpy(hur_data(hur), opaque, opaque_len);
7834 /* Send the HSM request */
7835 if (realpath(some_file, fullpath) == NULL) {
7836 fprintf(stderr, "Could not find path '%s': %s\n",
7837 some_file, strerror(errno));
7839 rc = llapi_hsm_request(fullpath, hur);
7841 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7842 some_file, strerror(-rc));
7852 static int lfs_hsm_archive(int argc, char **argv)
7854 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7857 static int lfs_hsm_restore(int argc, char **argv)
7859 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7862 static int lfs_hsm_release(int argc, char **argv)
7864 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7867 static int lfs_hsm_remove(int argc, char **argv)
7869 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7872 static int lfs_hsm_cancel(int argc, char **argv)
7874 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7877 static int lfs_swap_layouts(int argc, char **argv)
7882 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7883 SWAP_LAYOUTS_KEEP_MTIME |
7884 SWAP_LAYOUTS_KEEP_ATIME);
7887 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7889 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7891 int lfs_get_mode(const char *string)
7893 enum lock_mode_user mode;
7895 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7896 if (lock_mode_names[mode] == NULL)
7898 if (strcmp(string, lock_mode_names[mode]) == 0)
7905 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7907 enum lu_ladvise_type advice;
7910 advice < ARRAY_SIZE(ladvise_names); advice++) {
7911 if (ladvise_names[advice] == NULL)
7913 if (strcmp(string, ladvise_names[advice]) == 0)
7917 return LU_LADVISE_INVALID;
7920 static int lfs_ladvise(int argc, char **argv)
7922 struct option long_opts[] = {
7923 { .val = 'a', .name = "advice", .has_arg = required_argument },
7924 { .val = 'b', .name = "background", .has_arg = no_argument },
7925 { .val = 'e', .name = "end", .has_arg = required_argument },
7926 { .val = 'l', .name = "length", .has_arg = required_argument },
7927 { .val = 'm', .name = "mode", .has_arg = required_argument },
7928 { .val = 's', .name = "start", .has_arg = required_argument },
7929 { .val = 'u', .name = "unset", .has_arg = no_argument },
7931 char short_opts[] = "a:be:l:m:s:u";
7936 struct llapi_lu_ladvise advice;
7937 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7938 unsigned long long start = 0;
7939 unsigned long long end = LUSTRE_EOF;
7940 unsigned long long length = 0;
7941 unsigned long long size_units;
7942 unsigned long long flags = 0;
7946 while ((c = getopt_long(argc, argv, short_opts,
7947 long_opts, NULL)) != -1) {
7950 advice_type = lfs_get_ladvice(optarg);
7951 if (advice_type == LU_LADVISE_INVALID) {
7952 fprintf(stderr, "%s: invalid advice type "
7953 "'%s'\n", argv[0], optarg);
7954 fprintf(stderr, "Valid types:");
7956 for (advice_type = 0;
7957 advice_type < ARRAY_SIZE(ladvise_names);
7959 if (ladvise_names[advice_type] == NULL)
7961 fprintf(stderr, " %s",
7962 ladvise_names[advice_type]);
7964 fprintf(stderr, "\n");
7977 rc = llapi_parse_size(optarg, &end,
7980 fprintf(stderr, "%s: bad end offset '%s'\n",
7987 rc = llapi_parse_size(optarg, &start,
7990 fprintf(stderr, "%s: bad start offset "
7991 "'%s'\n", argv[0], optarg);
7997 rc = llapi_parse_size(optarg, &length,
8000 fprintf(stderr, "%s: bad length '%s'\n",
8006 mode = lfs_get_mode(optarg);
8008 fprintf(stderr, "%s: bad mode '%s', valid "
8009 "modes are READ or WRITE\n",
8017 fprintf(stderr, "%s: option '%s' unrecognized\n",
8018 argv[0], argv[optind - 1]);
8023 if (advice_type == LU_LADVISE_INVALID) {
8024 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8025 fprintf(stderr, "Valid types:");
8026 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8028 if (ladvise_names[advice_type] == NULL)
8030 fprintf(stderr, " %s", ladvise_names[advice_type]);
8032 fprintf(stderr, "\n");
8036 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8037 fprintf(stderr, "%s: Lock no expand advice is a per file "
8038 "descriptor advice, so when called from lfs, "
8039 "it does nothing.\n", argv[0]);
8043 if (argc <= optind) {
8044 fprintf(stderr, "%s: please give one or more file names\n",
8049 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8050 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8055 if (end == LUSTRE_EOF && length != 0)
8056 end = start + length;
8059 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8060 argv[0], start, end);
8064 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8065 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8070 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8071 fprintf(stderr, "%s: mode is required with lockahead\n",
8076 while (optind < argc) {
8079 path = argv[optind++];
8081 fd = open(path, O_RDONLY);
8083 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8084 argv[0], path, strerror(errno));
8089 advice.lla_start = start;
8090 advice.lla_end = end;
8091 advice.lla_advice = advice_type;
8092 advice.lla_value1 = 0;
8093 advice.lla_value2 = 0;
8094 advice.lla_value3 = 0;
8095 advice.lla_value4 = 0;
8096 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8097 advice.lla_lockahead_mode = mode;
8098 advice.lla_peradvice_flags = flags;
8101 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8104 fprintf(stderr, "%s: cannot give advice '%s' to file "
8105 "'%s': %s\n", argv[0],
8106 ladvise_names[advice_type],
8107 path, strerror(errno));
8113 if (rc == 0 && rc2 < 0)
8120 static const char *const heat_names[] = LU_HEAT_NAMES;
8122 static int lfs_heat_get(int argc, char **argv)
8124 struct lu_heat *heat;
8133 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8135 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8140 while (optind < argc) {
8141 path = argv[optind++];
8143 fd = open(path, O_RDONLY);
8145 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8146 argv[0], path, strerror(errno));
8151 heat->lh_count = OBD_HEAT_COUNT;
8152 rc2 = llapi_heat_get(fd, heat);
8155 fprintf(stderr, "%s: cannot get heat of file '%s'"
8156 ": %s\n", argv[0], path, strerror(errno));
8160 printf("flags: %x\n", heat->lh_flags);
8161 for (i = 0; i < heat->lh_count; i++)
8162 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8164 if (rc == 0 && rc2 < 0)
8172 static int lfs_heat_set(int argc, char **argv)
8174 struct option long_opts[] = {
8175 {"clear", no_argument, 0, 'c'},
8176 {"off", no_argument, 0, 'o'},
8177 {"on", no_argument, 0, 'O'},
8180 char short_opts[] = "coO";
8191 while ((c = getopt_long(argc, argv, short_opts,
8192 long_opts, NULL)) != -1) {
8195 flags |= LU_HEAT_FLAG_CLEAR;
8198 flags |= LU_HEAT_FLAG_CLEAR;
8199 flags |= LU_HEAT_FLAG_OFF;
8202 flags &= ~LU_HEAT_FLAG_OFF;
8207 fprintf(stderr, "%s: option '%s' unrecognized\n",
8208 argv[0], argv[optind - 1]);
8213 if (argc <= optind) {
8214 fprintf(stderr, "%s: please give one or more file names\n",
8219 while (optind < argc) {
8220 path = argv[optind++];
8222 fd = open(path, O_RDONLY);
8224 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8225 argv[0], path, strerror(errno));
8230 rc2 = llapi_heat_set(fd, flags);
8233 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8234 ": %s\n", argv[0], path, strerror(errno));
8238 if (rc == 0 && rc2 < 0)
8244 /** The input string contains a comma delimited list of component ids and
8245 * ranges, for example "1,2-4,7".
8247 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8249 bool end_of_loop = false;
8257 while (!end_of_loop) {
8261 char *endptr = NULL;
8264 ptr = strchrnul(arg, ',');
8265 end_of_loop = *ptr == '\0';
8268 start_index = strtol(arg, &endptr, 0);
8269 if (endptr == arg) /* no data at all */
8271 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8273 if (start_index < 0)
8276 end_index = start_index;
8277 if (*endptr == '-') {
8278 end_index = strtol(endptr + 1, &endptr, 0);
8279 if (*endptr != '\0')
8281 if (end_index < start_index)
8285 for (i = start_index; i <= end_index && size > 0; i++) {
8288 /* remove duplicate */
8289 for (j = 0; j < nr; j++) {
8293 if (j == nr) { /* no duplicate */
8299 if (size == 0 && i < end_index)
8306 if (!end_of_loop && ptr != NULL)
8309 return rc < 0 ? rc : nr;
8313 * struct verify_mirror_id - Mirror id to be verified.
8314 * @mirror_id: A specified mirror id.
8315 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8317 struct verify_mirror_id {
8323 * compare_mirror_ids() - Compare mirror ids.
8324 * @layout: Mirror component list.
8325 * @cbdata: Callback data in verify_mirror_id structure.
8327 * This is a callback function called by llapi_layout_comp_iterate()
8328 * to compare the specified mirror id with the one in the current
8329 * component of @layout. If they are the same, then the specified
8330 * mirror id is valid.
8332 * Return: a negative error code on failure or
8333 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8334 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8337 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8339 struct verify_mirror_id *mirror_id_cbdata =
8340 (struct verify_mirror_id *)cbdata;
8344 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8348 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8349 progname, strerror(errno));
8353 if (mirror_id_cbdata->mirror_id == mirror_id) {
8354 mirror_id_cbdata->is_valid_id = true;
8355 return LLAPI_LAYOUT_ITER_STOP;
8358 return LLAPI_LAYOUT_ITER_CONT;
8362 * verify_mirror_ids() - Verify specified mirror ids.
8363 * @fname: Mirrored file name.
8364 * @mirror_ids: Specified mirror ids to be verified.
8365 * @ids_nr: Number of specified mirror ids.
8367 * This function verifies that specified @mirror_ids are valid
8368 * in the mirrored file @fname.
8370 * Return: 0 on success or a negative error code on failure.
8373 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8375 struct llapi_layout *layout = NULL;
8376 struct verify_mirror_id mirror_id_cbdata = { 0 };
8387 if (stat(fname, &stbuf) < 0) {
8388 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8389 progname, fname, strerror(errno));
8394 if (!S_ISREG(stbuf.st_mode)) {
8395 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8401 fd = open(fname, O_DIRECT | O_RDONLY);
8403 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8404 progname, fname, strerror(errno));
8409 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8411 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8412 progname, fname, strerror(errno));
8416 layout = llapi_layout_get_by_fd(fd, 0);
8417 if (layout == NULL) {
8418 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8419 progname, fname, strerror(errno));
8421 llapi_lease_release(fd);
8425 rc = llapi_layout_flags_get(layout, &flr_state);
8427 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8428 progname, fname, strerror(errno));
8433 flr_state &= LCM_FL_FLR_MASK;
8434 switch (flr_state) {
8437 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8438 progname, fname, llapi_layout_flags_string(flr_state));
8445 for (i = 0; i < ids_nr; i++) {
8446 mirror_id_cbdata.mirror_id = mirror_ids[i];
8447 mirror_id_cbdata.is_valid_id = false;
8449 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8454 "%s: '%s' failed to verify mirror id: %u.\n",
8455 progname, fname, mirror_ids[i]);
8459 if (!mirror_id_cbdata.is_valid_id) {
8462 "%s: '%s' invalid specified mirror id: %u.\n",
8463 progname, fname, mirror_ids[i]);
8469 llapi_layout_free(layout);
8470 llapi_lease_release(fd);
8478 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8479 __u16 *mirror_ids, int ids_nr)
8481 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8482 struct llapi_layout *layout;
8492 if (stat(fname, &stbuf) < 0) {
8493 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8494 progname, fname, strerror(errno));
8498 if (!S_ISREG(stbuf.st_mode)) {
8499 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8505 fd = open(fname, O_DIRECT | O_RDWR);
8507 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8508 progname, fname, strerror(errno));
8513 layout = llapi_layout_get_by_fd(fd, 0);
8514 if (layout == NULL) {
8515 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8516 progname, fname, strerror(errno));
8521 rc = llapi_layout_flags_get(layout, &flr_state);
8523 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8524 progname, fname, strerror(errno));
8529 flr_state &= LCM_FL_FLR_MASK;
8530 if (flr_state == LCM_FL_NONE) {
8532 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8537 /* get stale component info */
8538 comp_size = llapi_mirror_find_stale(layout, comp_array,
8539 ARRAY_SIZE(comp_array),
8540 mirror_ids, ids_nr);
8541 if (comp_size <= 0) {
8546 ioc->lil_mode = LL_LEASE_WRLCK;
8547 ioc->lil_flags = LL_LEASE_RESYNC;
8548 rc = llapi_lease_set(fd, ioc);
8550 if (rc == -EALREADY)
8554 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8555 progname, fname, strerror(errno));
8559 /* get the read range [start, end) */
8560 start = comp_array[0].lrc_start;
8561 end = comp_array[0].lrc_end;
8562 for (idx = 1; idx < comp_size; idx++) {
8563 if (comp_array[idx].lrc_start < start)
8564 start = comp_array[idx].lrc_start;
8565 if (end < comp_array[idx].lrc_end)
8566 end = comp_array[idx].lrc_end;
8569 rc = llapi_lease_check(fd);
8570 if (rc != LL_LEASE_WRLCK) {
8571 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8576 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8579 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8580 progname, fname, rc);
8582 /* prepare ioc for lease put */
8583 ioc->lil_mode = LL_LEASE_UNLCK;
8584 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8586 for (idx = 0; idx < comp_size; idx++) {
8587 if (comp_array[idx].lrc_synced) {
8588 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8593 rc = llapi_lease_set(fd, ioc);
8595 if (rc == 0) /* lost lease lock */
8597 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8598 progname, fname, strerror(errno));
8602 * llapi_lease_set returns lease mode when it request to unlock
8608 llapi_layout_free(layout);
8615 static inline int lfs_mirror_resync(int argc, char **argv)
8617 struct ll_ioc_lease *ioc = NULL;
8618 __u16 mirror_ids[128] = { 0 };
8623 struct option long_opts[] = {
8624 { .val = 'o', .name = "only", .has_arg = required_argument },
8627 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8630 rc = parse_mirror_ids(mirror_ids,
8631 sizeof(mirror_ids) / sizeof(__u16),
8635 "%s: bad mirror ids '%s'.\n",
8642 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8643 argv[0], argv[optind - 1]);
8649 if (argc == optind) {
8650 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8655 if (ids_nr > 0 && argc > optind + 1) {
8657 "%s: option '--only' cannot be used upon multiple files.\n",
8665 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8670 /* set the lease on the file */
8671 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8673 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8674 argv[0], strerror(errno));
8679 for (; optind < argc; optind++) {
8680 rc = lfs_mirror_resync_file(argv[optind], ioc,
8681 mirror_ids, ids_nr);
8682 /* ignore previous file's error, continue with next file */
8685 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8693 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8695 struct llapi_layout *layout;
8698 layout = llapi_layout_get_by_fd(fd, 0);
8699 if (layout == NULL) {
8700 fprintf(stderr, "could not get layout.\n");
8704 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8706 fprintf(stderr, "failed to iterate layout\n");
8707 llapi_layout_free(layout);
8710 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8711 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8712 llapi_layout_free(layout);
8716 llapi_layout_free(layout);
8722 * Check whether two files are the same file
8723 * \retval 0 same file
8724 * \retval 1 not the same file
8725 * \retval <0 error code
8727 static inline int check_same_file(const char *f1, const char *f2)
8732 if (stat(f1, &stbuf1) < 0) {
8733 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8734 progname, f1, strerror(errno));
8738 if (stat(f2, &stbuf2) < 0) {
8739 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8740 progname, f2, strerror(errno));
8744 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8745 stbuf1.st_ino == stbuf2.st_ino)
8751 static inline int lfs_mirror_read(int argc, char **argv)
8754 __u16 mirror_id = 0;
8755 const char *outfile = NULL;
8761 const size_t buflen = 4 << 20;
8763 struct option long_opts[] = {
8764 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8765 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8768 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8773 mirror_id = strtoul(optarg, &end, 0);
8774 if (*end != '\0' || mirror_id == 0) {
8776 "%s %s: invalid mirror ID '%s'\n",
8777 progname, argv[0], optarg);
8785 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8786 progname, argv[optind - 1]);
8791 if (argc == optind) {
8792 fprintf(stderr, "%s %s: no mirrored file provided\n",
8795 } else if (argc > optind + 1) {
8796 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8800 if (mirror_id == 0) {
8801 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8806 /* open mirror file */
8807 fname = argv[optind];
8810 rc = check_same_file(fname, outfile);
8813 "%s %s: output file cannot be the mirrored file\n",
8821 fd = open(fname, O_DIRECT | O_RDONLY);
8823 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8824 progname, argv[0], fname, strerror(errno));
8828 /* verify mirror id */
8829 rc = verify_mirror_id_by_fd(fd, mirror_id);
8832 "%s %s: cannot find mirror with ID %u in '%s'\n",
8833 progname, argv[0], mirror_id, fname);
8837 /* open output file */
8839 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8841 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8842 progname, argv[0], outfile, strerror(errno));
8847 outfd = STDOUT_FILENO;
8850 /* allocate buffer */
8851 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8853 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8854 progname, argv[0], rc);
8861 ssize_t written = 0;
8863 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8864 if (bytes_read < 0) {
8867 "%s %s: fail to read data from mirror %u: %s\n",
8868 progname, argv[0], mirror_id, strerror(-rc));
8873 if (bytes_read == 0)
8876 while (written < bytes_read) {
8879 written2 = write(outfd, buf + written,
8880 bytes_read - written);
8883 "%s %s: fail to write %s: %s\n",
8884 progname, argv[0], outfile ? : "STDOUT",
8889 written += written2;
8892 if (written != bytes_read) {
8894 "%s %s: written %ld bytes does not match with %ld read.\n",
8895 progname, argv[0], written, bytes_read);
8917 static inline int lfs_mirror_write(int argc, char **argv)
8920 __u16 mirror_id = 0;
8921 const char *inputfile = NULL;
8927 const size_t buflen = 4 << 20;
8929 size_t page_size = sysconf(_SC_PAGESIZE);
8930 struct ll_ioc_lease_id ioc;
8932 struct option long_opts[] = {
8933 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8934 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8937 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8942 mirror_id = strtoul(optarg, &end, 0);
8943 if (*end != '\0' || mirror_id == 0) {
8945 "%s %s: invalid mirror ID '%s'\n",
8946 progname, argv[0], optarg);
8954 fprintf(stderr, "%s: option '%s' unrecognized\n",
8955 progname, argv[optind - 1]);
8960 if (argc == optind) {
8961 fprintf(stderr, "%s %s: no mirrored file provided\n",
8964 } else if (argc > optind + 1) {
8965 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8969 if (mirror_id == 0) {
8970 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8975 /* open mirror file */
8976 fname = argv[optind];
8979 rc = check_same_file(fname, inputfile);
8982 "%s %s: input file cannot be the mirrored file\n",
8990 fd = open(fname, O_DIRECT | O_WRONLY);
8992 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8993 progname, argv[0], fname, strerror(errno));
8997 /* verify mirror id */
8998 rc = verify_mirror_id_by_fd(fd, mirror_id);
9001 "%s %s: cannot find mirror with ID %u in '%s'\n",
9002 progname, argv[0], mirror_id, fname);
9006 /* open input file */
9008 inputfd = open(inputfile, O_RDONLY, 0644);
9010 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9011 progname, argv[0], inputfile, strerror(errno));
9016 inputfd = STDIN_FILENO;
9019 /* allocate buffer */
9020 rc = posix_memalign(&buf, page_size, buflen);
9022 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9023 progname, argv[0], rc);
9027 /* prepare target mirror components instantiation */
9028 ioc.lil_mode = LL_LEASE_WRLCK;
9029 ioc.lil_flags = LL_LEASE_RESYNC;
9030 ioc.lil_mirror_id = mirror_id;
9031 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9034 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9035 progname, argv[0], fname, strerror(errno));
9045 rc = llapi_lease_check(fd);
9046 if (rc != LL_LEASE_WRLCK) {
9047 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9048 progname, argv[0], fname);
9052 bytes_read = read(inputfd, buf, buflen);
9053 if (bytes_read < 0) {
9056 "%s %s: fail to read data from '%s': %s\n",
9057 progname, argv[0], inputfile ? : "STDIN",
9064 if (bytes_read == 0)
9067 /* round up to page align to make direct IO happy. */
9068 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9070 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9075 "%s %s: fail to write to mirror %u: %s\n",
9076 progname, argv[0], mirror_id,
9084 if (pos & (page_size - 1)) {
9085 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9090 ioc.lil_mode = LL_LEASE_UNLCK;
9091 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9093 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9098 "%s %s: release lease lock of '%s' failed: %s\n",
9099 progname, argv[0], fname, strerror(errno));
9116 struct collect_ids_data {
9122 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9124 struct collect_ids_data *cid = cbdata;
9128 rc = llapi_layout_mirror_id_get(layout, &id);
9132 if ((__u16)id != cid->cid_exclude) {
9135 for (i = 0; i < cid->cid_count; i++) {
9136 /* already collected the mirror id */
9137 if (id == cid->cid_ids[i])
9138 return LLAPI_LAYOUT_ITER_CONT;
9140 cid->cid_ids[cid->cid_count] = id;
9144 return LLAPI_LAYOUT_ITER_CONT;
9147 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9149 struct llapi_layout *layout;
9150 struct collect_ids_data cid = { .cid_ids = ids,
9152 .cid_exclude = exclude_id, };
9155 layout = llapi_layout_get_by_fd(fd, 0);
9156 if (layout == NULL) {
9157 fprintf(stderr, "could not get layout\n");
9161 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9163 fprintf(stderr, "failed to iterate layout\n");
9164 llapi_layout_free(layout);
9168 llapi_layout_free(layout);
9170 return cid.cid_count;
9173 static inline int lfs_mirror_copy(int argc, char **argv)
9176 __u16 read_mirror_id = 0;
9177 __u16 ids[128] = { 0 };
9179 struct llapi_layout *layout = NULL;
9180 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9187 struct ll_ioc_lease *ioc = NULL;
9188 struct ll_ioc_lease_id *resync_ioc;
9190 struct option long_opts[] = {
9191 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9192 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9195 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9200 read_mirror_id = strtoul(optarg, &end, 0);
9201 if (*end != '\0' || read_mirror_id == 0) {
9203 "%s %s: invalid read mirror ID '%s'\n",
9204 progname, argv[0], optarg);
9209 if (!strcmp(optarg, "-1")) {
9210 /* specify all other mirrors */
9214 count = parse_mirror_ids((__u16 *)ids,
9222 fprintf(stderr, "%s: option '%s' unrecognized\n",
9223 progname, argv[optind - 1]);
9228 if (argc == optind) {
9229 fprintf(stderr, "%s %s: no mirrored file provided\n",
9232 } else if (argc > optind + 1) {
9233 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9237 if (read_mirror_id == 0) {
9239 "%s %s: no valid read mirror ID %d is provided\n",
9240 progname, argv[0], read_mirror_id);
9246 "%s %s: no write mirror ID is provided\n",
9251 for (i = 0; i < count; i++) {
9252 if (read_mirror_id == ids[i]) {
9254 "%s %s: read and write mirror ID cannot be the same\n",
9260 /* open mirror file */
9261 fname = argv[optind];
9263 fd = open(fname, O_DIRECT | O_RDWR);
9265 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9266 progname, argv[0], fname, strerror(errno));
9270 /* write to all other mirrors */
9271 if (ids[0] == (__u16)-1) {
9272 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9276 "%s %s: failed to get other mirror ids in '%s': %d\n",
9277 progname, argv[0], fname, rc);
9282 /* verify mirror id */
9283 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9286 "%s %s: cannot find mirror with ID %u in '%s'\n",
9287 progname, argv[0], read_mirror_id, fname);
9291 for (i = 0; i < count; i++) {
9292 rc = verify_mirror_id_by_fd(fd, ids[i]);
9295 "%s %s: cannot find mirror with ID %u in '%s'\n",
9296 progname, argv[0], ids[i], fname);
9301 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9304 "%s %s: cannot alloc comp id array for ioc: %s\n",
9305 progname, argv[0], strerror(errno));
9310 /* get stale component info */
9311 layout = llapi_layout_get_by_fd(fd, 0);
9312 if (layout == NULL) {
9313 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9314 progname, argv[0], fname, strerror(errno));
9318 comp_size = llapi_mirror_find_stale(layout, comp_array,
9319 ARRAY_SIZE(comp_array),
9321 llapi_layout_free(layout);
9322 if (comp_size < 0) {
9327 /* prepare target mirror components instantiation */
9328 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9329 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9330 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9332 resync_ioc->lil_mirror_id = ids[0];
9334 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9335 rc = llapi_lease_set(fd, ioc);
9338 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9339 progname, argv[0], fname, strerror(errno));
9343 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9346 fprintf(stderr, "%s %s: copy error: %d\n",
9347 progname, argv[0], rc);
9351 fprintf(stdout, "mirror copied successfully: ");
9352 for (i = 0; i < copied; i++)
9353 fprintf(stdout, "%d ", ids[i]);
9354 fprintf(stdout, "\n");
9356 ioc->lil_mode = LL_LEASE_UNLCK;
9357 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9359 for (i = 0; i < comp_size; i++) {
9362 for (j = 0; j < copied; j++) {
9363 if (comp_array[i].lrc_mirror_id != ids[j])
9366 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9370 rc = llapi_lease_set(fd, ioc);
9375 "%s %s: release lease lock of '%s' failed: %s\n",
9376 progname, argv[0], fname, strerror(errno));
9390 * struct verify_chunk - Mirror chunk to be verified.
9391 * @chunk: [start, end) of the chunk.
9392 * @mirror_count: Number of mirror ids in @mirror_id array.
9393 * @mirror_id: Array of valid mirror ids that cover the chunk.
9395 struct verify_chunk {
9396 struct lu_extent chunk;
9397 unsigned int mirror_count;
9398 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9402 * print_chunks() - Print chunk information.
9403 * @fname: Mirrored file name.
9404 * @chunks: Array of chunks.
9405 * @chunk_count: Number of chunks in @chunks array.
9407 * This function prints [start, end) of each chunk in @chunks
9408 * for mirrored file @fname, and also prints the valid mirror ids
9409 * that cover the chunk.
9414 void print_chunks(const char *fname, struct verify_chunk *chunks,
9420 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9421 for (i = 0; i < chunk_count; i++) {
9422 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9424 if (chunks[i].mirror_count == 0)
9425 fprintf(stdout, "\t[");
9427 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9428 for (j = 1; j < chunks[i].mirror_count; j++)
9429 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9431 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9433 fprintf(stdout, "\n");
9437 * print_checksums() - Print CRC-32 checksum values.
9438 * @chunk: A chunk and its corresponding valid mirror ids.
9439 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9441 * This function prints CRC-32 checksum values on @chunk for
9442 * each valid mirror that covers it.
9447 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9452 "CRC-32 checksum value for chunk "DEXT":\n",
9453 PEXT(&chunk->chunk));
9454 for (i = 0; i < chunk->mirror_count; i++)
9455 fprintf(stdout, "Mirror %u:\t%#lx\n",
9456 chunk->mirror_id[i], crc[i]);
9457 fprintf(stdout, "\n");
9461 * filter_mirror_id() - Filter specified mirror ids.
9462 * @chunks: Array of chunks.
9463 * @chunk_count: Number of chunks in @chunks array.
9464 * @mirror_ids: Specified mirror ids to be verified.
9465 * @ids_nr: Number of specified mirror ids.
9467 * This function scans valid mirror ids that cover each chunk in @chunks
9468 * and filters specified mirror ids.
9473 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9474 __u16 *mirror_ids, int ids_nr)
9479 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9480 unsigned int valid_count = 0;
9482 for (i = 0; i < chunk_count; i++) {
9483 if (chunks[i].mirror_count == 0)
9487 for (j = 0; j < ids_nr; j++) {
9488 for (k = 0; k < chunks[i].mirror_count; k++) {
9489 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9490 valid_id[valid_count] = mirror_ids[j];
9497 memcpy(chunks[i].mirror_id, valid_id,
9498 sizeof(__u16) * valid_count);
9499 chunks[i].mirror_count = valid_count;
9504 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9505 * @layout: Mirror component list.
9506 * @chunks: Array of chunks.
9507 * @chunks_size: Array size of @chunks.
9509 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9510 * to find out chunk segments and store them in @chunks array.
9512 * The @mirror_id array in each element of @chunks will store the valid
9513 * mirror ids that cover the chunk. If a mirror component covering the
9514 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9515 * will not be stored into the @mirror_id array, and the chunk for that
9516 * mirror will not be verified.
9518 * The @mirror_count in each element of @chunks will store the number of
9519 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9520 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9521 * indicates the chunk is valid in only one mirror. In both cases, the
9522 * chunk will not be verified.
9524 * Here is an example:
9527 * +------+-------------+--------------------+
9529 * +------+------+------+------+-------------+
9530 * | | S | S | | mirror2
9531 * +-------------+------+------+-------------+
9533 * prepared @chunks array will contain 5 elements:
9534 * (([0, 1M), [1, 2], 2),
9535 * ([1M, 2M), [1, 2], 2),
9536 * ([2M, 3M), [1], 1),
9537 * ([3M, 4M], [], 0),
9538 * ([4M, EOF), [2], 1))
9540 * Return: the actual array size of @chunks on success
9541 * or a negative error code on failure.
9544 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9545 struct verify_chunk *chunks,
9556 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9559 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9562 "%s: move to the first layout component: %s.\n",
9563 progname, strerror(errno));
9569 chunks[idx].chunk.e_end = LUSTRE_EOF;
9571 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9574 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9575 progname, strerror(errno));
9579 if (start > chunks[idx].chunk.e_start ||
9580 end <= chunks[idx].chunk.e_start)
9583 if (end < chunks[idx].chunk.e_end)
9584 chunks[idx].chunk.e_end = end;
9586 rc = llapi_layout_comp_flags_get(layout, &flags);
9589 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9590 progname, strerror(errno));
9594 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9597 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9600 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9601 progname, strerror(errno));
9605 chunks[idx].mirror_id[i] = mirror_id;
9607 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9609 "%s: mirror_id array is too small.\n",
9616 rc = llapi_layout_comp_use(layout,
9617 LLAPI_LAYOUT_COMP_USE_NEXT);
9620 "%s: move to the next layout component: %s.\n",
9621 progname, strerror(errno));
9624 } /* loop through all components */
9626 chunks[idx].mirror_count = i;
9628 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9632 if (idx >= chunks_size) {
9633 fprintf(stderr, "%s: chunks array is too small.\n",
9639 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9643 return rc < 0 ? rc : idx + 1;
9647 * lfs_mirror_verify_chunk() - Verify a chunk.
9648 * @fd: File descriptor of the mirrored file.
9649 * @file_size: Size of the mirrored file.
9650 * @chunk: A chunk and its corresponding valid mirror ids.
9651 * @verbose: Verbose mode.
9653 * This function verifies a @chunk contains exactly the same data
9654 * ammong the mirrors that cover it.
9656 * If @verbose is specified, then the function will print where the
9657 * differences are if the data do not match. Otherwise, it will
9658 * just return an error in that case.
9660 * Return: 0 on success or a negative error code on failure.
9663 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9664 struct verify_chunk *chunk, int verbose)
9666 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9668 size_t page_size = sysconf(_SC_PAGESIZE);
9674 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9681 rc = posix_memalign(&buf, page_size, buflen);
9682 if (rc) /* error code is returned directly */
9686 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9687 PEXT(&chunk->chunk));
9688 for (i = 0; i < chunk->mirror_count; i++)
9689 fprintf(stdout, " %u", chunk->mirror_id[i]);
9690 fprintf(stdout, "\n");
9694 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9695 pos = chunk->chunk.e_start;
9696 while (bytes_done < count) {
9697 /* compute initial CRC-32 checksum */
9698 crc = crc32(0L, Z_NULL, 0);
9699 memset(crc_array, 0, sizeof(crc_array));
9702 for (i = 0; i < chunk->mirror_count; i++) {
9703 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9705 if (bytes_read < 0) {
9708 "%s: failed to read data from mirror %u: %s.\n",
9709 progname, chunk->mirror_id[i],
9714 /* compute new CRC-32 checksum */
9715 crc_array[i] = crc32(crc, buf, bytes_read);
9719 print_checksums(chunk, crc_array);
9721 /* compare CRC-32 checksum values */
9722 for (i = 1; i < chunk->mirror_count; i++) {
9723 if (crc_array[i] != crc_array[0]) {
9727 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9728 progname, PEXT(&chunk->chunk),
9729 chunk->mirror_id[0],
9730 chunk->mirror_id[i]);
9735 bytes_done += bytes_read;
9738 if (verbose > 1 && rc == 0) {
9739 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9740 PEXT(&chunk->chunk));
9741 for (i = 0; i < chunk->mirror_count; i++)
9742 fprintf(stdout, " %u", chunk->mirror_id[i]);
9743 fprintf(stdout, " PASS\n\n");
9752 * lfs_mirror_verify_file() - Verify a mirrored file.
9753 * @fname: Mirrored file name.
9754 * @mirror_ids: Specified mirror ids to be verified.
9755 * @ids_nr: Number of specified mirror ids.
9756 * @verbose: Verbose mode.
9758 * This function verifies that each SYNC mirror of a mirrored file
9759 * specified by @fname contains exactly the same data.
9761 * If @mirror_ids is specified, then the function will verify the
9762 * mirrors specified by @mirror_ids contain exactly the same data.
9764 * If @verbose is specified, then the function will print where the
9765 * differences are if the data do not match. Otherwise, it will
9766 * just return an error in that case.
9768 * Return: 0 on success or a negative error code on failure.
9771 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9774 struct verify_chunk chunks_array[1024] = { };
9775 struct llapi_layout *layout = NULL;
9779 int chunk_count = 0;
9785 if (stat(fname, &stbuf) < 0) {
9786 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9787 progname, fname, strerror(errno));
9792 if (!S_ISREG(stbuf.st_mode)) {
9793 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9799 if (stbuf.st_size == 0) {
9801 fprintf(stdout, "%s: '%s' file size is 0.\n",
9807 fd = open(fname, O_DIRECT | O_RDONLY);
9809 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9810 progname, fname, strerror(errno));
9815 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9817 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9818 progname, fname, strerror(errno));
9822 layout = llapi_layout_get_by_fd(fd, 0);
9823 if (layout == NULL) {
9824 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9825 progname, fname, strerror(errno));
9827 llapi_lease_release(fd);
9831 rc = llapi_layout_flags_get(layout, &flr_state);
9833 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9834 progname, fname, strerror(errno));
9839 flr_state &= LCM_FL_FLR_MASK;
9840 switch (flr_state) {
9843 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9844 progname, fname, llapi_layout_flags_string(flr_state));
9850 /* find out mirror chunks to be verified */
9851 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9852 ARRAY_SIZE(chunks_array));
9853 if (chunk_count < 0) {
9859 /* filter specified mirror ids */
9860 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9863 print_chunks(fname, chunks_array, chunk_count);
9865 for (idx = 0; idx < chunk_count; idx++) {
9866 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9869 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9871 PEXT(&chunks_array[idx].chunk),
9872 (unsigned long long)stbuf.st_size);
9876 if (chunks_array[idx].mirror_count == 0) {
9878 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9880 PEXT(&chunks_array[idx].chunk));
9882 fprintf(stderr, "skipped\n");
9886 fprintf(stderr, "failed\n");
9890 if (chunks_array[idx].mirror_count == 1) {
9893 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9895 PEXT(&chunks_array[idx].chunk),
9896 chunks_array[idx].mirror_id[0]);
9900 rc = llapi_lease_check(fd);
9901 if (rc != LL_LEASE_RDLCK) {
9902 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9907 /* verify one chunk */
9908 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9909 &chunks_array[idx], verbose);
9923 llapi_layout_free(layout);
9924 llapi_lease_release(fd);
9932 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9933 * @argc: The count of lfs mirror verify command line arguments.
9934 * @argv: Array of strings for lfs mirror verify command line arguments.
9936 * This function parses lfs mirror verify command and verifies the
9937 * specified mirrored file(s).
9939 * Return: 0 on success or a negative error code on failure.
9941 static inline int lfs_mirror_verify(int argc, char **argv)
9943 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9951 struct option long_opts[] = {
9952 { .val = 'o', .name = "only", .has_arg = required_argument },
9953 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9956 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9958 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9961 rc = parse_mirror_ids(mirror_ids,
9962 ARRAY_SIZE(mirror_ids),
9966 "%s: bad mirror ids '%s'.\n",
9973 "%s: at least 2 mirror ids needed with '--only' option.\n",
9983 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9984 progname, argv[optind - 1]);
9990 if (argc == optind) {
9991 fprintf(stderr, "%s: no file name given.\n", progname);
9996 if (ids_nr > 0 && argc > optind + 1) {
9998 "%s: '--only' cannot be used upon multiple files.\n",
10006 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10012 for (; optind < argc; optind++) {
10013 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10023 * lfs_mirror() - Parse and execute lfs mirror commands.
10024 * @argc: The count of lfs mirror command line arguments.
10025 * @argv: Array of strings for lfs mirror command line arguments.
10027 * This function parses lfs mirror commands and performs the
10028 * corresponding functions specified in mirror_cmdlist[].
10030 * Return: 0 on success or an error code on failure.
10032 static int lfs_mirror(int argc, char **argv)
10034 char cmd[PATH_MAX];
10037 setlinebuf(stdout);
10039 Parser_init("lfs-mirror > ", mirror_cmdlist);
10041 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10043 program_invocation_short_name = cmd;
10045 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10047 rc = Parser_commands();
10049 return rc < 0 ? -rc : rc;
10052 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10054 #if __BYTE_ORDER == __BIG_ENDIAN
10055 __swab16s(&attrs->lsa_valid);
10056 __swab64s(&attrs->lsa_size);
10057 __swab64s(&attrs->lsa_blocks);
10061 enum lfs_som_type {
10062 LFS_SOM_SIZE = 0x1,
10063 LFS_SOM_BLOCKS = 0x2,
10064 LFS_SOM_FLAGS = 0x4,
10065 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10069 static int lfs_getsom(int argc, char **argv)
10072 struct lustre_som_attrs *attrs;
10073 char buf[sizeof(*attrs) + 64];
10074 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10077 while ((c = getopt(argc, argv, "sbf")) != -1) {
10080 type = LFS_SOM_SIZE;
10083 type = LFS_SOM_BLOCKS;
10086 type = LFS_SOM_FLAGS;
10089 fprintf(stderr, "%s: invalid option '%c'\n",
10099 fprintf(stderr, "%s: %s\n",
10100 progname, argc == 0 ? "miss file target" :
10101 "input more than 2 files");
10106 attrs = (void *)buf;
10107 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10110 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10111 argv[0], strerror(errno), errno);
10115 lustre_som_swab(attrs);
10118 case LFS_SOM_ATTR_ALL:
10119 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10120 path, attrs->lsa_size, attrs->lsa_blocks,
10124 printf("%llu\n", attrs->lsa_size);
10126 case LFS_SOM_BLOCKS:
10127 printf("%llu\n", attrs->lsa_blocks);
10129 case LFS_SOM_FLAGS:
10130 printf("%x\n", attrs->lsa_valid);
10133 fprintf(stderr, "%s: unknown option\n", progname);
10141 * lfs_mirror_list_commands() - List lfs mirror commands.
10142 * @argc: The count of command line arguments.
10143 * @argv: Array of strings for command line arguments.
10145 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10147 * Return: 0 on success.
10149 static int lfs_mirror_list_commands(int argc, char **argv)
10151 char buffer[81] = "";
10153 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10159 static int lfs_list_commands(int argc, char **argv)
10161 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10163 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10168 int main(int argc, char **argv)
10172 /* Ensure that liblustreapi constructor has run */
10173 if (!llapi_liblustreapi_initialized())
10174 fprintf(stderr, "liblustreapi was not properly initialized\n");
10176 setlinebuf(stdout);
10179 Parser_init("lfs > ", cmdlist);
10181 progname = program_invocation_short_name; /* Used in error messages */
10183 llapi_set_command_name(argv[1]);
10184 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10185 llapi_clear_command_name();
10187 rc = Parser_commands();
10190 return rc < 0 ? -rc : rc;
10193 #ifdef _LUSTRE_IDL_H_
10194 /* Everything we need here should be included by lustreapi.h. */
10195 # error "lfs should not depend on lustre_idl.h"
10196 #endif /* _LUSTRE_IDL_H_ */