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_rmfid(int argc, char **argv);
107 static int lfs_data_version(int argc, char **argv);
108 static int lfs_hsm_state(int argc, char **argv);
109 static int lfs_hsm_set(int argc, char **argv);
110 static int lfs_hsm_clear(int argc, char **argv);
111 static int lfs_hsm_action(int argc, char **argv);
112 static int lfs_hsm_archive(int argc, char **argv);
113 static int lfs_hsm_restore(int argc, char **argv);
114 static int lfs_hsm_release(int argc, char **argv);
115 static int lfs_hsm_remove(int argc, char **argv);
116 static int lfs_hsm_cancel(int argc, char **argv);
117 static int lfs_swap_layouts(int argc, char **argv);
118 static int lfs_mv(int argc, char **argv);
119 static int lfs_ladvise(int argc, char **argv);
120 static int lfs_getsom(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
130 enum setstripe_origin {
138 static int lfs_setstripe_internal(int argc, char **argv,
139 enum setstripe_origin opc);
141 static inline int lfs_setstripe(int argc, char **argv)
143 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
145 static inline int lfs_setstripe_migrate(int argc, char **argv)
147 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
149 static inline int lfs_mirror_create(int argc, char **argv)
151 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
153 static inline int lfs_mirror_extend(int argc, char **argv)
155 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
157 static inline int lfs_mirror_split(int argc, char **argv)
159 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
162 /* Setstripe and migrate share mostly the same parameters */
163 #define SSM_CMD_COMMON(cmd) \
164 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
165 " [--stripe-count|-c <stripe_count>]\n" \
166 " [--stripe-index|-i <start_ost_idx>]\n" \
167 " [--stripe-size|-S <stripe_size>]\n" \
168 " [--layout|-L <pattern>]\n" \
169 " [--pool|-p <pool_name>]\n" \
170 " [--ost|-o <ost_indices>]\n" \
171 " [--yaml|-y <yaml_template_file>]\n" \
172 " [--copy=<lustre_src>]\n"
174 #define SSM_HELP_COMMON \
175 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
176 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
177 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
178 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
179 "\t respectively)\n" \
180 "\tpool_name: Name of OST pool to use (default none)\n" \
181 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
182 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
183 "\t Indices be specified in a format of:\n" \
184 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
186 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
187 "\t If --pool is set with --ost, then the OSTs\n" \
188 "\t must be the members of the pool.\n" \
189 "\tcomp_end: Extent end of component, start after previous end.\n"\
190 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
191 "\t respectively, -1 for EOF). Must be a multiple of\n"\
192 "\t stripe_size.\n" \
193 "\tyaml_template_file:\n" \
194 "\t YAML layout template file, can't be used with -c,\n" \
195 "\t -i, -S, -p, -o, or -E arguments.\n" \
196 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
197 "\t another lustre file or directory, can't used with\n" \
198 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
200 #define MIRROR_CREATE_HELP \
201 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
202 "\t setstripe layout options\n" \
203 "\t It defaults to 1 if not specified; if specified,\n" \
204 "\t it must follow the option without a space.\n" \
205 "\t The option can also be repeated multiple times to\n" \
206 "\t separate mirrors that have different layouts.\n" \
207 "\tsetstripe options: Mirror layout\n" \
208 "\t It can be a plain layout or a composite layout.\n" \
209 "\t If not specified, the stripe options inherited\n" \
210 "\t from the previous component will be used.\n" \
211 "\tflags: set flags to the component of the current mirror.\n" \
212 "\t Only \"prefer\" flag is supported so far.\n"
214 #define MIRROR_EXTEND_HELP \
216 "\tvictim_file: The layout of victim_file will be split and used\n" \
217 "\t as a mirror added to the mirrored file.\n" \
218 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
219 "\t from victim file(s) in case the victim file(s)\n" \
220 "\t contains the same data as the original mirrored\n" \
223 #define MIRROR_EXTEND_USAGE \
224 " <--mirror-count|-N[mirror_count]>\n" \
225 " [setstripe options|-f <victim_file>]\n" \
228 #define SETSTRIPE_USAGE \
229 SSM_CMD_COMMON("setstripe") \
230 MIRROR_EXTEND_USAGE \
231 " <directory|filename>\n" \
235 #define MIGRATE_USAGE \
236 SSM_CMD_COMMON("migrate ") \
238 " [--non-block|-n]\n" \
239 " [--non-direct|-D]\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" \
247 #define SETDIRSTRIPE_USAGE \
248 " [--mdt-count|-c stripe_count>\n" \
249 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
250 " [--mdt-hash|-H mdt_hash]\n" \
251 " [--default|-D] [--mode|-m mode] <dir>\n" \
252 "\tstripe_count: stripe count of the striped directory\n" \
253 "\tmdt_index: MDT index of first stripe\n" \
254 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
255 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
256 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
257 "\tdefault_stripe: set default dirstripe of the directory\n" \
258 "\tmode: the mode of the directory\n"
261 * command_t mirror_cmdlist - lfs mirror commands.
263 command_t mirror_cmdlist[] = {
264 { .pc_name = "create", .pc_func = lfs_mirror_create,
265 .pc_help = "Create a mirrored file.\n"
266 "usage: lfs mirror create "
267 "<--mirror-count|-N[mirror_count]> "
268 "[setstripe options] ... <filename|directory>\n"
269 MIRROR_CREATE_HELP },
270 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
271 .pc_help = "Extend a mirrored file.\n"
272 "usage: lfs mirror extend "
273 "<--mirror-count|-N[mirror_count]> [--no-verify] "
274 "[setstripe options|-f <victim_file>] ... <filename>\n"
275 MIRROR_EXTEND_HELP },
276 { .pc_name = "split", .pc_func = lfs_mirror_split,
277 .pc_help = "Split a mirrored file.\n"
278 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
279 "\t <--component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
280 "\t [-f <new_file>] <mirrored file>\n"
281 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
282 "\t can be fetched by lfs getstripe command.\n"
283 "\tcomp_id: Unique component ID within a mirror.\n"
284 "\tpool: Components on specified pool.\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")
324 " <directory|filename>\n"
326 "To add component(s) to an existing composite file:\n"
327 SSM_CMD_COMMON("setstripe --component-add")
329 "To totally delete the default striping from an existing directory:\n"
330 "usage: setstripe -d <directory>\n"
332 "To create a mirrored file or set s default mirror layout on a directory:\n"
333 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
335 "To delete the last component(s) from an existing composite file\n"
336 "(note that this will also delete any data in those components):\n"
337 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
338 " [--component-flags|-F <comp_flags>]\n"
340 "\tcomp_id: Unique component ID to delete\n"
341 "\tcomp_flags: 'init' indicating all instantiated components\n"
342 "\t '^init' indicating all uninstantiated components\n"
343 "\t-I and -F cannot be specified at the same time\n"},
344 {"getstripe", lfs_getstripe, 0,
345 "To list the layout pattern for a given file or files in a\n"
346 "directory or recursively for all files in a directory tree.\n"
347 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
348 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
349 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
350 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
351 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
352 " [--component-id[=comp_id]|-I[comp_id]]\n"
353 " [--component-flags[=comp_flags]]\n"
354 " [--component-count]\n"
355 " [--component-start[=[+-]comp_start]]\n"
356 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
357 " [[!] --mirror-index=[+-]<index> |\n"
358 " [!] --mirror-id=[+-]<id>]\n"
359 " <directory|filename> ..."},
360 {"setdirstripe", lfs_setdirstripe, 0,
361 "To create a striped directory on a specified MDT. This can only\n"
362 "be done on MDT0 with the right of administrator.\n"
363 "usage: setdirstripe [OPTION] <directory>\n"
365 {"getdirstripe", lfs_getdirstripe, 0,
366 "To list the layout pattern info for a given directory\n"
367 "or recursively for all directories in a directory tree.\n"
368 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
369 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
370 " [--recursive|-r] [--yaml|-y]\n"
371 " [--default|-D] <dir> ..."},
372 {"mkdir", lfs_setdirstripe, 0,
373 "To create a striped directory on a specified MDT. This can only\n"
374 "be done on MDT0 with the right of administrator.\n"
375 "usage: mkdir [OPTION] <directory>\n"
377 {"rm_entry", lfs_rmentry, 0,
378 "To remove the name entry of the remote directory. Note: This\n"
379 "command will only delete the name entry, i.e. the remote directory\n"
380 "will become inaccessable after this command. This can only be done\n"
381 "by the administrator\n"
382 "usage: rm_entry <dir>\n"},
383 {"pool_list", lfs_poollist, 0,
384 "List pools or pool OSTs\n"
385 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
386 {"find", lfs_find, 0,
387 "find files matching given attributes recursively in directory tree.\n"
388 "usage: find <directory|filename> ...\n"
389 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
390 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
391 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
392 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
393 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
394 " [[!] --stripe-count|-c [+-]<stripes>]\n"
395 " [[!] --stripe-index|-i <index,...>]\n"
396 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
397 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
398 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
399 " [[!] --projid <projid>]\n"
400 " [[!] --layout|-L released,raid0,mdt]\n"
401 " [[!] --component-count [+-]<comp_cnt>]\n"
402 " [[!] --component-start [+-]N[kMGTPE]]\n"
403 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
404 " [[!] --component-flags <comp_flags>]\n"
405 " [[!] --mirror-count|-N [+-]<n>]\n"
406 " [[!] --mirror-state <[^]state>]\n"
407 " [[!] --mdt-count|-T [+-]<stripes>]\n"
408 " [[!] --mdt-hash|-H <hashtype>\n"
409 "\t !: used before an option indicates 'NOT' requested attribute\n"
410 "\t -: used before a value indicates less than requested value\n"
411 "\t +: used before a value indicates more than requested value\n"
412 "\thashtype: hash type of the striped directory.\n"
413 "\t fnv_1a_64 FNV-1a hash algorithm\n"
414 "\t all_char sum of characters % MDT_COUNT\n"},
415 {"check", lfs_check, 0,
416 "Display the status of MDS or OSTs (as specified in the command)\n"
417 "or all the servers (MDS and OSTs).\n"
418 "usage: check <osts|mds|servers>"},
419 {"osts", lfs_osts, 0, "list OSTs connected to client "
420 "[for specified path only]\n" "usage: osts [path]"},
421 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
422 "[for specified path only]\n" "usage: mdts [path]"},
424 "report filesystem disk space usage or inodes usage"
425 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
426 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
427 {"getname", lfs_getname, 0, "list instances and specified mount points "
428 "[for specified path only]\n"
429 "Usage: getname [-h]|[path ...] "},
430 #ifdef HAVE_SYS_QUOTA_H
431 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
432 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
433 " -b <block-softlimit> -B <block-hardlimit>\n"
434 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
435 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
436 " [--block-softlimit <block-softlimit>]\n"
437 " [--block-hardlimit <block-hardlimit>]\n"
438 " [--inode-softlimit <inode-softlimit>]\n"
439 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
440 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
441 " [--block-grace <block-grace>]\n"
442 " [--inode-grace <inode-grace>] <filesystem>\n"
443 " setquota <-U|-G|-P>\n"
444 " -b <block-softlimit> -B <block-hardlimit>\n"
445 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
446 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
447 " [--block-softlimit <block-softlimit>]\n"
448 " [--block-hardlimit <block-hardlimit>]\n"
449 " [--inode-softlimit <inode-softlimit>]\n"
450 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
451 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
453 " -b can be used instead of --block-softlimit/--block-grace\n"
454 " -B can be used instead of --block-hardlimit\n"
455 " -i can be used instead of --inode-softlimit/--inode-grace\n"
456 " -I can be used instead of --inode-hardlimit\n"
457 " -d can be used instead of --default\n\n"
458 "Note: The total quota space will be split into many qunits and\n"
459 " balanced over all server targets, the minimal qunit size is\n"
460 " 1M bytes for block space and 1K inodes for inode space.\n\n"
461 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
462 " Quota space rebalancing process will stop when this mininum\n"
463 " value is reached. As a result, quota exceeded can be returned\n"
464 " while many targets still have 1MB or 1K inodes of spare\n"
466 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
467 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
469 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
470 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
471 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
472 {"project", lfs_project, 0,
473 "Change or list project attribute for specified file or directory.\n"
474 "usage: project [-d|-r] <file|directory...>\n"
475 " list project ID and flags on file(s) or directories\n"
476 " project [-p id] [-s] [-r] <file|directory...>\n"
477 " set project ID and/or inherit flag for specified file(s) or directories\n"
478 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
479 " check project ID and flags on file(s) or directories, print outliers\n"
480 " project -C [-r] [-k] <file|directory...>\n"
481 " clear the project inherit flag and ID on the file or directory\n"
484 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
485 "usage: flushctx [-k] [mountpoint...]"},
486 {"changelog", lfs_changelog, 0,
487 "Show the metadata changes on an MDT."
488 "\nusage: changelog <mdtname> [startrec [endrec]]"},
489 {"changelog_clear", lfs_changelog_clear, 0,
490 "Indicate that old changelog records up to <endrec> are no longer of "
491 "interest to consumer <id>, allowing the system to free up space.\n"
492 "An <endrec> of 0 means all records.\n"
493 "usage: changelog_clear <mdtname> <id> <endrec>"},
494 {"fid2path", lfs_fid2path, 0,
495 "Resolve the full path(s) for given FID(s). For a specific hardlink "
496 "specify link number <linkno>.\n"
497 /* "For a historical link name, specify changelog record <recno>.\n" */
498 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
499 /* [ --rec <recno> ] */ },
500 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
501 "usage: path2fid [--parents] <path> ..."},
502 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
503 "usage: rmfid <fsname|rootpath> <fid> ..."},
504 {"data_version", lfs_data_version, 0, "Display file data version for "
505 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
506 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
507 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
508 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
509 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
510 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
511 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
513 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
514 "[--archived] [--lost] <file> ..."},
515 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
516 "given files.\n" "usage: hsm_action <file> ..."},
517 {"hsm_archive", lfs_hsm_archive, 0,
518 "Archive file to external storage.\n"
519 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
521 {"hsm_restore", lfs_hsm_restore, 0,
522 "Restore file from external storage.\n"
523 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
524 {"hsm_release", lfs_hsm_release, 0,
525 "Release files from Lustre.\n"
526 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
527 {"hsm_remove", lfs_hsm_remove, 0,
528 "Remove file copy from external storage.\n"
529 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
531 " (FILE [FILE ...] | "
532 "--mntpath MOUNTPATH FID [FID ...])\n"
534 "Note: To remove an archived copy of a file already deleted from a "
536 "--mntpath option and a list of FIDs must be specified"
538 {"hsm_cancel", lfs_hsm_cancel, 0,
539 "Cancel requests related to specified files.\n"
540 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
541 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
542 "usage: swap_layouts <path1> <path2>"},
543 {"migrate", lfs_setstripe_migrate, 0,
544 "migrate a directory between MDTs.\n"
545 "usage: migrate [--mdt|-m] <start_mdt_index>\n"
546 " [--mdt-count|-c] <stripe_count>\n"
547 " [--mdt-hash|-H] <hash_type>\n"
550 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
551 " it's the MDT index of first stripe\n"
552 "\tmdt_count: number of MDTs to stripe a directory over\n"
553 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
554 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
555 " all_char sum of characters % MDT_COUNT\n"
557 "migrate file objects from one OST "
558 "layout\nto another (may be not safe with concurent writes).\n"
560 "[--stripe-count|-c] <stripe_count>\n"
561 " [--stripe-index|-i] <start_ost_index>\n"
562 " [--stripe-size|-S] <stripe_size>\n"
563 " [--pool|-p] <pool_name>\n"
564 " [--ost|-o] <ost_indices>\n"
566 " [--non-block|-n]\n"
567 " [--non-direct|-D]\n"
568 " <file|directory>\n"
569 "\tstripe_count: number of OSTs to stripe a file over\n"
570 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
571 "\tstripe_size: number of bytes to store before moving to the next OST\n"
572 "\tpool_name: name of the predefined pool of OSTs\n"
573 "\tost_indices: OSTs to stripe over, in order\n"
574 "\tblock: wait for the operation to return before continuing\n"
575 "\tnon-block: do not wait for the operation to return\n"
576 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
578 "To move directories between MDTs. This command is deprecated, "
579 "use \"migrate\" instead.\n"
580 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
582 {"ladvise", lfs_ladvise, 0,
583 "Provide servers with advice about access patterns for a file.\n"
584 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
585 " [--background|-b] [--unset|-u]\n\n"
586 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
587 " {[--mode|-m [READ,WRITE]}\n"
589 {"mirror", lfs_mirror, mirror_cmdlist,
590 "lfs commands used to manage files with mirrored components:\n"
591 "lfs mirror create - create a mirrored file or directory\n"
592 "lfs mirror extend - add mirror(s) to an existing file\n"
593 "lfs mirror split - split a mirror from an existing mirrored file\n"
594 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
595 "lfs mirror read - read a mirror content of a mirrored file\n"
596 "lfs mirror write - write to a mirror of a mirrored file\n"
597 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
598 "lfs mirror verify - verify mirrored file(s)\n"},
599 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
600 "usage: getsom [-s] [-b] [-f] <path>\n"
601 "\t-s: Only show the size value of the SOM data for a given file\n"
602 "\t-b: Only show the blocks value of the SOM data for a given file\n"
603 "\t-f: Only show the flags value of the SOM data for a given file\n"},
604 {"help", Parser_help, 0, "help"},
605 {"exit", Parser_quit, 0, "quit"},
606 {"quit", Parser_quit, 0, "quit"},
607 {"--version", Parser_version, 0,
608 "output build version of the utility and exit"},
609 {"--list-commands", lfs_list_commands, 0,
610 "list commands supported by the utility and exit"},
615 static int check_hashtype(const char *hashtype)
619 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
620 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
627 static const char *error_loc = "syserror";
630 MIGRATION_NONBLOCK = 1 << 0,
631 MIGRATION_MIRROR = 1 << 1,
632 MIGRATION_NONDIRECT = 1 << 2,
635 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
636 struct llapi_layout *layout);
639 migrate_open_files(const char *name, __u64 migration_flags,
640 const struct llapi_stripe_param *param,
641 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
648 char parent[PATH_MAX];
649 char volatile_file[PATH_MAX];
655 if (param == NULL && layout == NULL) {
656 error_loc = "layout information";
660 /* search for file directory pathname */
661 if (strlen(name) > sizeof(parent) - 1) {
662 error_loc = "source file name";
666 strncpy(parent, name, sizeof(parent));
667 ptr = strrchr(parent, '/');
669 if (getcwd(parent, sizeof(parent)) == NULL) {
670 error_loc = "getcwd";
674 if (ptr == parent) /* leading '/' */
679 /* open file, direct io */
680 /* even if the file is only read, WR mode is nedeed to allow
681 * layout swap on fd */
683 if (!(migration_flags & MIGRATION_NONDIRECT))
685 fd = open(name, rflags);
688 error_loc = "cannot open source file";
692 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
694 error_loc = "cannot get MDT index";
699 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
700 mode_t open_mode = S_IRUSR | S_IWUSR;
702 random_value = random();
703 rc = snprintf(volatile_file, sizeof(volatile_file),
704 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
705 mdt_index, random_value);
706 if (rc >= sizeof(volatile_file)) {
711 /* create, open a volatile file, use caching (ie no directio) */
713 fdv = llapi_file_open_param(volatile_file, open_flags,
716 fdv = lfs_component_create(volatile_file, open_flags,
718 } while (fdv < 0 && (rc = fdv) == -EEXIST);
721 error_loc = "cannot create volatile file";
725 /* In case the MDT does not support creation of volatile files
726 * we should try to unlink it. */
727 (void)unlink(volatile_file);
729 /* Not-owner (root?) special case.
730 * Need to set owner/group of volatile file like original.
731 * This will allow to pass related check during layout_swap.
736 error_loc = "cannot stat source file";
740 rc = fstat(fdv, &stv);
743 error_loc = "cannot stat volatile";
747 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
748 rc = fchown(fdv, st.st_uid, st.st_gid);
751 error_loc = "cannot change ownwership of volatile";
770 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
772 struct llapi_layout *layout;
773 size_t buf_size = 4 * 1024 * 1024;
782 layout = llapi_layout_get_by_fd(fd_src, 0);
783 if (layout != NULL) {
784 uint64_t stripe_size;
786 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
788 buf_size = stripe_size;
790 llapi_layout_free(layout);
793 /* Use a page-aligned buffer for direct I/O */
794 rc = posix_memalign(&buf, getpagesize(), buf_size);
799 /* read new data only if we have written all
800 * previously read data */
803 rc = check_file(fd_src);
808 rsize = read(fd_src, buf, buf_size);
820 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
839 static int migrate_copy_timestamps(int fd, int fdv)
843 if (fstat(fd, &st) == 0) {
844 struct timeval tv[2] = {
845 {.tv_sec = st.st_atime},
846 {.tv_sec = st.st_mtime}
849 return futimes(fdv, tv);
855 static int migrate_block(int fd, int fdv)
862 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
864 error_loc = "cannot get dataversion";
872 /* The grouplock blocks all concurrent accesses to the file.
873 * It has to be taken after llapi_get_data_version as it would
875 rc = llapi_group_lock(fd, gid);
877 error_loc = "cannot get group lock";
881 rc = migrate_copy_data(fd, fdv, NULL);
883 error_loc = "data copy failed";
887 /* Make sure we keep original atime/mtime values */
888 rc = migrate_copy_timestamps(fd, fdv);
890 error_loc = "timestamp copy failed";
895 * for a migration we need to check data version on file did
898 * Pass in gid=0 since we already own grouplock. */
899 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
900 SWAP_LAYOUTS_CHECK_DV1);
902 error_loc = "file changed";
905 error_loc = "cannot swap layout";
910 rc2 = llapi_group_unlock(fd, gid);
911 if (rc2 < 0 && rc == 0) {
912 error_loc = "unlock group lock";
920 * Internal helper for migrate_copy_data(). Check lease and report error if
923 * \param[in] fd File descriptor on which to check the lease.
925 * \retval 0 Migration can keep on going.
926 * \retval -errno Error occurred, abort migration.
928 static int check_lease(int fd)
932 rc = llapi_lease_check(fd);
934 return 0; /* llapi_check_lease returns > 0 on success. */
939 static int migrate_nonblock(int fd, int fdv)
945 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
947 error_loc = "cannot get data version";
951 rc = migrate_copy_data(fd, fdv, check_lease);
953 error_loc = "data copy failed";
957 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
959 error_loc = "cannot get data version";
965 error_loc = "source file changed";
969 /* Make sure we keep original atime/mtime values */
970 rc = migrate_copy_timestamps(fd, fdv);
972 error_loc = "timestamp copy failed";
979 static int lfs_component_set(char *fname, int comp_id,
980 __u32 flags, __u32 neg_flags)
983 __u32 flags_array[2];
988 ids[count] = comp_id;
989 flags_array[count] = flags;
994 ids[count] = comp_id;
995 flags_array[count] = neg_flags | LCME_FL_NEG;
999 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1001 if (errno == EUCLEAN) {
1004 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1005 progname, comp_id, fname);
1008 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1009 progname, comp_id, fname, flags, neg_flags);
1016 static int lfs_component_del(char *fname, __u32 comp_id,
1017 __u32 flags, __u32 neg_flags)
1021 if (flags && neg_flags)
1024 if (!flags && neg_flags)
1025 flags = neg_flags | LCME_FL_NEG;
1027 if ((flags && comp_id) || (!flags && !comp_id))
1030 /* LCME_FL_INIT is the only supported flag in PFL */
1032 if (flags & ~LCME_KNOWN_FLAGS) {
1034 "%s setstripe: unknown flags %#x\n",
1038 } else if (comp_id > LCME_ID_MAX) {
1039 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1044 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1047 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1048 progname, comp_id, fname, strerror(errno));
1052 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1059 rc = llapi_layout_file_comp_add(fname, layout);
1061 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1062 fname, strerror(errno));
1066 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1067 struct llapi_layout *layout)
1075 fd = lstat(fname, &st);
1076 if (fd == 0 && S_ISDIR(st.st_mode))
1077 open_flags = O_DIRECTORY | O_RDONLY;
1079 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1081 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1082 S_ISDIR(st.st_mode) ?
1083 "set default composite layout for" :
1084 "create composite file",
1085 fname, strerror(errno));
1089 static int lfs_migrate(char *name, __u64 migration_flags,
1090 struct llapi_stripe_param *param,
1091 struct llapi_layout *layout)
1097 rc = migrate_open_files(name, migration_flags, param, layout,
1102 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1103 /* Blocking mode (forced if servers do not support file lease).
1104 * It is also the default mode, since we cannot distinguish
1105 * between a broken lease and a server that does not support
1106 * atomic swap/close (LU-6785) */
1107 rc = migrate_block(fd, fdv);
1111 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1113 error_loc = "cannot get lease";
1117 rc = migrate_nonblock(fd, fdv);
1119 llapi_lease_release(fd);
1123 /* Atomically put lease, swap layouts and close.
1124 * for a migration we need to check data version on file did
1126 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1128 error_loc = "cannot swap layout";
1140 fprintf(stderr, "error: %s: %s: %s: %s\n",
1141 progname, name, error_loc, strerror(-rc));
1145 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1154 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1158 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1159 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1160 const char *comp_name = comp_flags_table[i].cfn_name;
1162 if (strcmp(name, comp_name) == 0) {
1163 *flags |= comp_flag;
1165 } else if (strncmp(name, "^", 1) == 0 &&
1166 strcmp(name + 1, comp_name) == 0) {
1167 *neg_flags |= comp_flag;
1172 llapi_printf(LLAPI_MSG_ERROR,
1173 "%s: component flag '%s' not supported\n",
1179 if (!*flags && !*neg_flags)
1182 /* don't allow to set and exclude the same flag */
1183 if (*flags & *neg_flags)
1189 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1197 if (strncmp(string, "^", 1) == 0) {
1198 *neg_state = llapi_layout_string_flags(string + 1);
1199 if (*neg_state != 0)
1202 *state = llapi_layout_string_flags(string);
1207 llapi_printf(LLAPI_MSG_ERROR,
1208 "%s: mirrored file state '%s' not supported\n",
1214 * struct mirror_args - Command-line arguments for mirror(s).
1215 * @m_count: Number of mirrors to be created with this layout.
1216 * @m_flags: Mirror level flags, only 'prefer' is supported.
1217 * @m_layout: Mirror layout.
1218 * @m_file: A victim file. Its layout will be split and used as a mirror.
1219 * @m_next: Point to the next node of the list.
1221 * Command-line arguments for mirror(s) will be parsed and stored in
1222 * a linked list that consists of this structure.
1224 struct mirror_args {
1227 struct llapi_layout *m_layout;
1229 struct mirror_args *m_next;
1232 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1237 rc = llapi_layout_comp_flags_get(layout, &flags);
1241 if (flags & LCME_FL_NEG) {
1242 fprintf(stderr, "error: %s: negative flags are not supported\n",
1247 if (flags & LCME_FL_STALE) {
1248 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1249 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1253 return LLAPI_LAYOUT_ITER_CONT;
1256 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1258 uint64_t start, end;
1262 /* LU-10112: do not support dom+flr in phase 1 */
1263 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1267 rc = llapi_layout_pattern_get(layout, &pattern);
1271 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1272 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1277 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1281 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1285 if (end != LUSTRE_EOF) {
1286 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1291 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1296 * enum mirror_flags - Flags for extending a mirrored file.
1297 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1298 * in case the victim file(s) contains the same data as the
1299 * original mirrored file.
1300 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1301 * @MF_COMP_ID: specified component id instead of mirror id
1303 * Flags for extending a mirrored file.
1313 * mirror_create_sanity_check() - Check mirror list.
1314 * @list: A linked list that stores the mirror arguments.
1316 * This function does a sanity check on @list for creating
1319 * Return: 0 on success or a negative error code on failure.
1321 static int mirror_create_sanity_check(const char *fname,
1322 struct mirror_args *list)
1325 bool has_m_file = false;
1326 bool has_m_layout = false;
1332 struct llapi_layout *layout;
1334 layout = llapi_layout_get_by_path(fname, 0);
1337 "error: %s: file '%s' couldn't get layout\n",
1342 rc = mirror_sanity_check_one(layout);
1343 llapi_layout_free(layout);
1349 while (list != NULL) {
1350 if (list->m_file != NULL) {
1352 llapi_layout_free(list->m_layout);
1355 llapi_layout_get_by_path(list->m_file, 0);
1356 if (list->m_layout == NULL) {
1358 "error: %s: file '%s' has no layout\n",
1359 progname, list->m_file);
1363 has_m_layout = true;
1364 if (list->m_layout == NULL) {
1365 fprintf(stderr, "error: %s: no mirror layout\n",
1371 rc = mirror_sanity_check_one(list->m_layout);
1375 list = list->m_next;
1378 if (has_m_file && has_m_layout) {
1380 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1388 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1390 __u32 mirror_flags = *(__u32 *)cbdata;
1394 rc = llapi_layout_comp_flags_get(layout, &flags);
1399 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1404 return LLAPI_LAYOUT_ITER_CONT;
1408 * mirror_create() - Create a mirrored file.
1409 * @fname: The file to be created.
1410 * @mirror_list: A linked list that stores the mirror arguments.
1412 * This function creates a mirrored file @fname with the mirror(s)
1413 * from @mirror_list.
1415 * Return: 0 on success or a negative error code on failure.
1417 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1419 struct llapi_layout *layout = NULL;
1420 struct mirror_args *cur_mirror = NULL;
1421 uint16_t mirror_count = 0;
1425 rc = mirror_create_sanity_check(NULL, mirror_list);
1429 cur_mirror = mirror_list;
1430 while (cur_mirror != NULL) {
1431 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1433 &cur_mirror->m_flags);
1436 fprintf(stderr, "%s: failed to set mirror flags\n",
1441 for (i = 0; i < cur_mirror->m_count; i++) {
1442 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1445 fprintf(stderr, "error: %s: "
1446 "merge layout failed: %s\n",
1447 progname, strerror(errno));
1451 mirror_count += cur_mirror->m_count;
1452 cur_mirror = cur_mirror->m_next;
1455 if (layout == NULL) {
1456 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1460 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1463 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1464 progname, strerror(errno));
1468 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1476 llapi_layout_free(layout);
1481 * Compare files and check lease on @fd.
1483 * \retval bytes number of bytes are the same
1485 static ssize_t mirror_file_compare(int fd, int fdv)
1487 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1489 ssize_t bytes_done = 0;
1490 ssize_t bytes_read = 0;
1492 buf = malloc(buflen * 2);
1497 if (!llapi_lease_check(fd)) {
1498 bytes_done = -EBUSY;
1502 bytes_read = read(fd, buf, buflen);
1503 if (bytes_read <= 0)
1506 if (bytes_read != read(fdv, buf + buflen, buflen))
1509 /* XXX: should compute the checksum on each buffer and then
1510 * compare checksum to avoid cache collision */
1511 if (memcmp(buf, buf + buflen, bytes_read))
1514 bytes_done += bytes_read;
1522 static int mirror_extend_file(const char *fname, const char *victim_file,
1523 enum mirror_flags mirror_flags)
1528 struct stat stbuf_v;
1529 struct ll_ioc_lease *data = NULL;
1532 fd = open(fname, O_RDWR);
1534 error_loc = "open source file";
1539 fdv = open(victim_file, O_RDWR);
1541 error_loc = "open target file";
1546 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1547 error_loc = "stat source or target file";
1552 if (stbuf.st_dev != stbuf_v.st_dev) {
1553 error_loc = "stat source and target file";
1558 /* mirrors should be of the same size */
1559 if (stbuf.st_size != stbuf_v.st_size) {
1560 error_loc = "file sizes don't match";
1565 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1567 error_loc = "cannot get lease";
1571 if (!(mirror_flags & MF_NO_VERIFY)) {
1573 /* mirrors should have the same contents */
1574 ret = mirror_file_compare(fd, fdv);
1575 if (ret != stbuf.st_size) {
1576 error_loc = "file busy or contents don't match";
1577 rc = ret < 0 ? ret : -EINVAL;
1582 /* Get rid of caching pages from clients */
1583 rc = llapi_file_flush(fd);
1585 error_loc = "cannot get data version";
1589 rc = llapi_file_flush(fdv);
1591 error_loc = "cannot get data version";
1596 /* Make sure we keep original atime/mtime values */
1597 rc = migrate_copy_timestamps(fd, fdv);
1599 error_loc = "cannot copy timestamp";
1603 /* Atomically put lease, merge layouts and close. */
1604 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1606 error_loc = "memory allocation";
1609 data->lil_mode = LL_LEASE_UNLCK;
1610 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1611 data->lil_count = 1;
1612 data->lil_ids[0] = fdv;
1613 rc = llapi_lease_set(fd, data);
1615 error_loc = "cannot merge layout";
1617 } else if (rc == 0) {
1619 error_loc = "lost lease lock";
1632 (void) unlink(victim_file);
1634 fprintf(stderr, "error: %s: %s: %s: %s\n",
1635 progname, fname, error_loc, strerror(-rc));
1639 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1641 struct ll_ioc_lease *data = NULL;
1646 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1650 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1652 error_loc = "cannot get lease";
1656 rc = migrate_nonblock(fd, fdv);
1658 llapi_lease_release(fd);
1662 /* Atomically put lease, merge layouts and close. */
1663 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1665 error_loc = "memory allocation";
1668 data->lil_mode = LL_LEASE_UNLCK;
1669 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1670 data->lil_count = 1;
1671 data->lil_ids[0] = fdv;
1672 rc = llapi_lease_set(fd, data);
1674 error_loc = "cannot merge layout";
1676 } else if (rc == 0) {
1678 error_loc = "lost lease lock";
1691 fprintf(stderr, "error: %s: %s: %s: %s\n",
1692 progname, name, error_loc, strerror(-rc));
1696 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1697 enum mirror_flags mirror_flags)
1701 rc = mirror_create_sanity_check(fname, mirror_list);
1705 while (mirror_list) {
1706 if (mirror_list->m_file != NULL) {
1707 rc = mirror_extend_file(fname, mirror_list->m_file,
1710 __u32 mirror_count = mirror_list->m_count;
1712 while (mirror_count > 0) {
1713 rc = mirror_extend_layout(fname,
1714 mirror_list->m_layout);
1724 mirror_list = mirror_list->m_next;
1730 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1735 rc = llapi_layout_mirror_id_get(layout, &id);
1739 if ((__u16)id == *(__u16 *)cbdata)
1740 return LLAPI_LAYOUT_ITER_STOP;
1742 return LLAPI_LAYOUT_ITER_CONT;
1745 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1750 rc = llapi_layout_comp_id_get(layout, &id);
1754 if (id == *(__u32 *)cbdata)
1755 return LLAPI_LAYOUT_ITER_STOP;
1757 return LLAPI_LAYOUT_ITER_CONT;
1760 struct pool_to_id_cbdata {
1764 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1766 char buf[LOV_MAXPOOLNAME + 1];
1767 struct pool_to_id_cbdata *d = (void *)cbdata;
1771 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1774 if (strcmp(d->pool, buf))
1775 return LLAPI_LAYOUT_ITER_CONT;
1777 rc = llapi_layout_mirror_id_get(layout, &id);
1782 return LLAPI_LAYOUT_ITER_STOP;
1785 struct collect_ids_data {
1791 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1793 struct collect_ids_data *cid = cbdata;
1797 rc = llapi_layout_mirror_id_get(layout, &id);
1801 if ((__u16)id != cid->cid_exclude) {
1804 for (i = 0; i < cid->cid_count; i++) {
1805 /* already collected the mirror id */
1806 if (id == cid->cid_ids[i])
1807 return LLAPI_LAYOUT_ITER_CONT;
1809 cid->cid_ids[cid->cid_count] = id;
1813 return LLAPI_LAYOUT_ITER_CONT;
1817 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1818 * @mirror_id: Mirror id to be checked.
1819 * @layout: Mirror component list.
1821 * This function checks if a mirror with specified @mirror_id is the last
1822 * non-stale mirror of a layout @layout.
1824 * Return: true or false.
1827 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1829 __u16 mirror_ids[128] = { 0 };
1830 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1832 .cid_exclude = mirror_id, };
1835 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1837 for (i = 0; i < cid.cid_count; i++) {
1838 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1841 comp_size = llapi_mirror_find_stale(layout, comp_array,
1842 ARRAY_SIZE(comp_array),
1851 static int mirror_split(const char *fname, __u32 id, const char *pool,
1852 enum mirror_flags mflags, const char *victim_file)
1854 struct llapi_layout *layout;
1855 char parent[PATH_MAX];
1856 char victim[PATH_MAX];
1857 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1859 struct ll_ioc_lease *data;
1860 uint16_t mirror_count;
1865 /* check fname contains mirror with mirror_id/comp_id */
1866 layout = llapi_layout_get_by_path(fname, 0);
1869 "error %s: file '%s' couldn't get layout\n",
1874 rc = mirror_sanity_check_one(layout);
1878 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1881 "error %s: file '%s' couldn't get mirror count\n",
1885 if (mirror_count < 2) {
1887 "error %s: file '%s' has %d component, cannot split\n",
1888 progname, fname, mirror_count);
1892 if (mflags & MF_COMP_POOL) {
1893 struct pool_to_id_cbdata data = { .pool = pool };
1895 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
1898 } else if (mflags & MF_COMP_ID) {
1899 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1900 id = mirror_id_of(id);
1902 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1905 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1908 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1910 "error %s: file '%s' does not contain mirror with id %u\n",
1911 progname, fname, id);
1915 fd = open(fname, O_RDWR);
1918 "error %s: open file '%s' failed: %s\n",
1919 progname, fname, strerror(errno));
1923 /* get victim file directory pathname */
1924 if (strlen(fname) > sizeof(parent) - 1) {
1925 fprintf(stderr, "error %s: file name of '%s' too long\n",
1930 strncpy(parent, fname, sizeof(parent));
1931 ptr = strrchr(parent, '/');
1933 if (getcwd(parent, sizeof(parent)) == NULL) {
1934 fprintf(stderr, "error %s: getcwd failed: %s\n",
1935 progname, strerror(errno));
1945 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1947 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1952 if (victim_file == NULL) {
1953 /* use a temp file to store the splitted layout */
1954 if (mflags & MF_DESTROY) {
1955 if (last_non_stale_mirror(id, layout)) {
1958 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
1963 fdv = llapi_create_volatile_idx(parent, mdt_index,
1964 O_LOV_DELAY_CREATE);
1966 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1968 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1971 /* user specified victim file */
1972 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1977 "error %s: create victim file failed: %s\n",
1978 progname, strerror(errno));
1982 /* get lease lock of fname */
1983 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1986 "error %s: cannot get lease of file '%s': %d\n",
1987 progname, fname, rc);
1991 /* Atomatically put lease, split layouts and close. */
1992 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1998 data->lil_mode = LL_LEASE_UNLCK;
1999 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2000 data->lil_count = 2;
2001 data->lil_ids[0] = fdv;
2002 data->lil_ids[1] = id;
2003 rc = llapi_lease_set(fd, data);
2005 if (rc == 0) /* lost lease lock */
2008 "error %s: cannot split '%s': %s\n",
2009 progname, fname, strerror(-rc));
2020 llapi_layout_free(layout);
2025 * Parse a string containing an target index list into an array of integers.
2027 * The input string contains a comma delimited list of individual
2028 * indices and ranges, for example "1,2-4,7". Add the indices into the
2029 * \a tgts array and remove duplicates.
2031 * \param[out] tgts array to store indices in
2032 * \param[in] size size of \a tgts array
2033 * \param[in] offset starting index in \a tgts
2034 * \param[in] arg string containing OST index list
2036 * \retval positive number of indices in \a tgts
2037 * \retval -EINVAL unable to parse \a arg
2039 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
2043 int slots = size - offset;
2050 end_of_loop = false;
2051 while (!end_of_loop) {
2055 char *endptr = NULL;
2059 ptr = strchrnul(arg, ',');
2061 end_of_loop = *ptr == '\0';
2064 start_index = strtol(arg, &endptr, 0);
2065 if (endptr == arg) /* no data at all */
2067 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2070 end_index = start_index;
2071 if (*endptr == '-') {
2072 end_index = strtol(endptr + 1, &endptr, 0);
2073 if (*endptr != '\0')
2075 if (end_index < start_index)
2079 for (i = start_index; i <= end_index && slots > 0; i++) {
2082 /* remove duplicate */
2083 for (j = 0; j < offset; j++) {
2087 if (j == offset) { /* no duplicate */
2092 if (slots == 0 && i < end_index)
2100 if (!end_of_loop && ptr != NULL)
2103 return rc < 0 ? rc : nr;
2106 struct lfs_setstripe_args {
2107 unsigned long long lsa_comp_end;
2108 unsigned long long lsa_stripe_size;
2109 long long lsa_stripe_count;
2110 long long lsa_stripe_off;
2111 __u32 lsa_comp_flags;
2112 __u32 lsa_comp_neg_flags;
2113 unsigned long long lsa_pattern;
2114 unsigned int lsa_mirror_count;
2116 bool lsa_first_comp;
2118 char *lsa_pool_name;
2121 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2123 unsigned int mirror_count = lsa->lsa_mirror_count;
2124 bool first_comp = lsa->lsa_first_comp;
2126 memset(lsa, 0, sizeof(*lsa));
2128 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2129 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2130 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2131 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2132 lsa->lsa_pool_name = NULL;
2134 lsa->lsa_mirror_count = mirror_count;
2135 lsa->lsa_first_comp = first_comp;
2139 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2140 * @lsa: Stripe options to be initialized and inherited.
2142 * This function initializes stripe options in @lsa and inherit
2143 * stripe_size, stripe_count and OST pool_name options.
2147 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2149 unsigned long long stripe_size;
2150 long long stripe_count;
2151 char *pool_name = NULL;
2153 stripe_size = lsa->lsa_stripe_size;
2154 stripe_count = lsa->lsa_stripe_count;
2155 pool_name = lsa->lsa_pool_name;
2157 setstripe_args_init(lsa);
2159 lsa->lsa_stripe_size = stripe_size;
2160 lsa->lsa_stripe_count = stripe_count;
2161 lsa->lsa_pool_name = pool_name;
2164 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2166 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2167 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2168 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2169 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2170 lsa->lsa_comp_end != 0);
2174 * comp_args_to_layout() - Create or extend a composite layout.
2175 * @composite: Pointer to the composite layout.
2176 * @lsa: Stripe options for the new component.
2178 * This function creates or extends a composite layout by adding a new
2179 * component with stripe options from @lsa.
2181 * Return: 0 on success or an error code on failure.
2183 static int comp_args_to_layout(struct llapi_layout **composite,
2184 struct lfs_setstripe_args *lsa,
2187 struct llapi_layout *layout = *composite;
2188 uint64_t prev_end = 0;
2191 if (layout == NULL) {
2192 layout = llapi_layout_alloc();
2193 if (layout == NULL) {
2194 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2198 *composite = layout;
2202 /* Get current component extent, current component
2203 * must be the tail component. */
2204 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2206 fprintf(stderr, "Get comp extent failed. %s\n",
2211 if (lsa->lsa_first_comp)
2214 if (lsa->lsa_first_comp)
2215 rc = llapi_layout_add_first_comp(layout);
2217 rc = llapi_layout_comp_add(layout);
2219 fprintf(stderr, "Add component failed. %s\n",
2224 /* reset lsa_first_comp */
2225 lsa->lsa_first_comp = false;
2228 rc = llapi_layout_comp_extent_set(layout, prev_end,
2231 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2232 prev_end, lsa->lsa_comp_end, strerror(errno));
2237 /* Data-on-MDT component setting */
2238 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2239 /* In case of Data-on-MDT patterns the only extra option
2240 * applicable is stripe size option. */
2241 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2242 fprintf(stderr, "Option 'stripe-count' can't be "
2243 "specified with Data-on-MDT component: %lld\n",
2244 lsa->lsa_stripe_count);
2247 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2248 fprintf(stderr, "Option 'stripe-size' can't be "
2249 "specified with Data-on-MDT component: %llu\n",
2250 lsa->lsa_stripe_size);
2253 if (lsa->lsa_nr_tgts != 0) {
2254 fprintf(stderr, "Option 'ost-list' can't be specified "
2255 "with Data-on-MDT component: '%i'\n",
2259 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2260 fprintf(stderr, "Option 'stripe-offset' can't be "
2261 "specified with Data-on-MDT component: %lld\n",
2262 lsa->lsa_stripe_off);
2265 if (lsa->lsa_pool_name != 0) {
2266 fprintf(stderr, "Option 'pool' can't be specified "
2267 "with Data-on-MDT component: '%s'\n",
2268 lsa->lsa_pool_name);
2272 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2274 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2279 /* Data-on-MDT component has always single stripe up to end */
2280 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2283 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2285 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2286 lsa->lsa_stripe_size, strerror(errno));
2290 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2292 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2293 lsa->lsa_stripe_count, strerror(errno));
2297 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2299 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2300 lsa->lsa_comp_flags, strerror(errno));
2304 if (lsa->lsa_pool_name != NULL) {
2305 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2307 fprintf(stderr, "Set pool name: %s failed. %s\n",
2308 lsa->lsa_pool_name, strerror(errno));
2312 rc = llapi_layout_pool_name_set(layout, "");
2314 fprintf(stderr, "Clear pool name failed: %s\n",
2320 if (lsa->lsa_nr_tgts > 0) {
2321 if (lsa->lsa_stripe_count > 0 &&
2322 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2323 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2324 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2325 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2326 lsa->lsa_stripe_count,
2330 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2331 rc = llapi_layout_ost_index_set(layout, i,
2336 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2337 lsa->lsa_stripe_off != -1) {
2338 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2341 fprintf(stderr, "Set ost index %d failed. %s\n",
2342 i, strerror(errno));
2349 static int build_component(struct llapi_layout **layout,
2350 struct lfs_setstripe_args *lsa, bool set_extent)
2354 rc = comp_args_to_layout(layout, lsa, set_extent);
2358 if (lsa->lsa_mirror_count > 0) {
2359 rc = llapi_layout_mirror_count_set(*layout,
2360 lsa->lsa_mirror_count);
2364 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2367 lsa->lsa_mirror_count = 0;
2373 static int build_layout_from_yaml_node(struct cYAML *node,
2374 struct llapi_layout **layout,
2375 struct lfs_setstripe_args *lsa,
2382 if (node->cy_type == CYAML_TYPE_OBJECT) {
2383 /* go deep to sub blocks */
2384 rc = build_layout_from_yaml_node(node->cy_child, layout,
2389 if (node->cy_string == NULL)
2392 string = node->cy_string;
2393 /* skip leading lmm_ if present, to simplify parsing */
2394 if (strncmp(string, "lmm_", 4) == 0)
2397 if (node->cy_type == CYAML_TYPE_STRING) {
2398 if (!strcmp(string, "lcme_extent.e_end")) {
2399 if (!strcmp(node->cy_valuestring, "EOF") ||
2400 !strcmp(node->cy_valuestring, "eof"))
2401 lsa->lsa_comp_end = LUSTRE_EOF;
2402 } else if (!strcmp(string, "pool")) {
2403 lsa->lsa_pool_name = node->cy_valuestring;
2404 } else if (!strcmp(string, "pattern")) {
2405 if (!strcmp(node->cy_valuestring, "mdt"))
2406 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2407 } else if (!strcmp(string, "lcme_flags")) {
2408 rc = comp_str2flags(node->cy_valuestring,
2409 &lsa->lsa_comp_flags,
2410 &lsa->lsa_comp_neg_flags);
2413 /* Only template flags have meaning in
2414 * the layout for a new file
2416 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2418 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2419 if (!strcmp(string, "lcm_mirror_count")) {
2420 lsa->lsa_mirror_count = node->cy_valueint;
2421 } else if (!strcmp(string, "lcme_extent.e_start")) {
2422 if (node->cy_valueint != 0 || *layout != NULL) {
2423 rc = build_component(layout, lsa, true);
2428 if (node->cy_valueint == 0)
2429 lsa->lsa_first_comp = true;
2431 /* initialize lsa */
2432 setstripe_args_init(lsa);
2433 lsa->lsa_tgts = osts;
2434 } else if (!strcmp(string, "lcme_extent.e_end")) {
2435 if (node->cy_valueint == -1)
2436 lsa->lsa_comp_end = LUSTRE_EOF;
2438 lsa->lsa_comp_end = node->cy_valueint;
2439 } else if (!strcmp(string, "stripe_count")) {
2440 lsa->lsa_stripe_count = node->cy_valueint;
2441 } else if (!strcmp(string, "stripe_size")) {
2442 lsa->lsa_stripe_size = node->cy_valueint;
2443 } else if (!strcmp(string, "stripe_offset")) {
2444 lsa->lsa_stripe_off = node->cy_valueint;
2445 } else if (!strcmp(string, "l_ost_idx")) {
2446 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2451 node = node->cy_next;
2457 static int lfs_comp_create_from_yaml(char *template,
2458 struct llapi_layout **layout,
2459 struct lfs_setstripe_args *lsa,
2462 struct cYAML *tree = NULL, *err_rc = NULL;
2465 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2467 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2468 progname, template);
2469 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2470 "can't parse", &err_rc);
2471 cYAML_print_tree2file(stderr, err_rc);
2472 cYAML_free_tree(err_rc);
2477 /* initialize lsa for plain file */
2478 setstripe_args_init(lsa);
2479 lsa->lsa_tgts = osts;
2481 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2483 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2484 progname, template);
2487 rc = build_component(layout, lsa, *layout != NULL);
2489 /* clean clean lsa */
2490 setstripe_args_init(lsa);
2494 cYAML_free_tree(tree);
2498 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2499 * end of the last component in the existing file, and adjust the
2500 * first extent start of the components to be added accordingly. */
2501 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2503 struct llapi_layout *head;
2504 uint64_t start, end, stripe_size, prev_end = 0;
2507 if (layout == NULL) {
2509 "%s setstripe: layout must be specified\n",
2515 head = llapi_layout_get_by_path(fname, 0);
2518 "%s setstripe: cannot read layout from '%s': %s\n",
2519 progname, fname, strerror(errno));
2521 } else if (errno == ENODATA) {
2522 /* file without LOVEA, this component-add will be turned
2523 * into a component-create. */
2524 llapi_layout_free(head);
2526 } else if (!llapi_layout_is_composite(head)) {
2527 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2529 llapi_layout_free(head);
2533 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2535 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2536 progname, strerror(errno));
2537 llapi_layout_free(head);
2541 llapi_layout_free(head);
2543 /* Make sure we use the first component of the layout to be added. */
2544 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2547 "%s setstripe: cannot move component cursor: %s\n",
2548 progname, strerror(errno));
2552 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2554 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2555 progname, strerror(errno));
2559 if (start > prev_end || end <= prev_end) {
2561 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2562 progname, start, end, prev_end);
2566 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2568 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2569 progname, strerror(errno));
2573 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2574 (prev_end & (stripe_size - 1))) {
2576 "%s setstripe: stripe size %lu not aligned with %lu\n",
2577 progname, stripe_size, prev_end);
2581 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2584 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2585 progname, prev_end, end, strerror(errno));
2592 static inline bool arg_is_eof(char *arg)
2594 return !strncmp(arg, "-1", strlen("-1")) ||
2595 !strncmp(arg, "EOF", strlen("EOF")) ||
2596 !strncmp(arg, "eof", strlen("eof"));
2600 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2602 * Return: Valid mirror_args pointer on success and
2603 * NULL if memory allocation fails.
2605 static struct mirror_args *lfs_mirror_alloc(void)
2607 struct mirror_args *mirror = NULL;
2610 mirror = calloc(1, sizeof(*mirror));
2621 * lfs_mirror_free() - Free memory allocated for a mirror argument
2623 * @mirror: Previously allocated mirror argument structure by
2624 * lfs_mirror_alloc().
2626 * Free memory allocated for @mirror.
2630 static void lfs_mirror_free(struct mirror_args *mirror)
2632 if (mirror->m_layout != NULL)
2633 llapi_layout_free(mirror->m_layout);
2638 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2639 * @mirror_list: Previously allocated mirror list.
2641 * Free memory allocated for @mirror_list.
2645 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2647 struct mirror_args *next_mirror = NULL;
2649 while (mirror_list != NULL) {
2650 next_mirror = mirror_list->m_next;
2651 lfs_mirror_free(mirror_list);
2652 mirror_list = next_mirror;
2664 LFS_COMP_NO_VERIFY_OPT,
2666 LFS_MIRROR_FLAGS_OPT,
2668 LFS_MIRROR_STATE_OPT,
2670 LFS_MIRROR_INDEX_OPT,
2674 static int lfs_setstripe_internal(int argc, char **argv,
2675 enum setstripe_origin opc)
2677 struct lfs_setstripe_args lsa = { 0 };
2678 struct llapi_stripe_param *param = NULL;
2679 struct find_param migrate_mdt_param = {
2689 unsigned long long size_units = 1;
2690 bool migrate_mode = false;
2691 bool migrate_mdt_mode = false;
2692 bool migration_block = false;
2693 __u64 migration_flags = 0;
2694 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2695 int comp_del = 0, comp_set = 0;
2698 struct llapi_layout *layout = NULL;
2699 struct llapi_layout **lpp = &layout;
2700 bool mirror_mode = false;
2701 bool has_m_file = false;
2702 __u32 mirror_count = 0;
2703 enum mirror_flags mirror_flags = 0;
2704 struct mirror_args *mirror_list = NULL;
2705 struct mirror_args *new_mirror = NULL;
2706 struct mirror_args *last_mirror = NULL;
2707 __u16 mirror_id = 0;
2709 bool from_yaml = false;
2710 bool from_copy = false;
2711 char *template = NULL;
2713 struct option long_opts[] = {
2714 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2715 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2716 /* --block is only valid in migrate mode */
2717 { .val = 'b', .name = "block", .has_arg = no_argument },
2718 { .val = LFS_COMP_ADD_OPT,
2719 .name = "comp-add", .has_arg = no_argument },
2720 { .val = LFS_COMP_ADD_OPT,
2721 .name = "component-add", .has_arg = no_argument },
2722 { .val = LFS_COMP_DEL_OPT,
2723 .name = "comp-del", .has_arg = no_argument },
2724 { .val = LFS_COMP_DEL_OPT,
2725 .name = "component-del", .has_arg = no_argument },
2726 { .val = LFS_COMP_FLAGS_OPT,
2727 .name = "comp-flags", .has_arg = required_argument },
2728 { .val = LFS_COMP_FLAGS_OPT,
2729 .name = "component-flags",
2730 .has_arg = required_argument },
2731 { .val = LFS_COMP_SET_OPT,
2732 .name = "comp-set", .has_arg = no_argument },
2733 { .val = LFS_COMP_SET_OPT,
2734 .name = "component-set",
2735 .has_arg = no_argument},
2736 { .val = LFS_COMP_NO_VERIFY_OPT,
2737 .name = "no-verify", .has_arg = no_argument},
2738 { .val = LFS_MIRROR_FLAGS_OPT,
2739 .name = "flags", .has_arg = required_argument},
2740 { .val = LFS_MIRROR_ID_OPT,
2741 .name = "mirror-id", .has_arg = required_argument},
2742 { .val = LFS_LAYOUT_COPY,
2743 .name = "copy", .has_arg = required_argument},
2744 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2745 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2746 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2747 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2748 { .val = 'd', .name = "delete", .has_arg = no_argument},
2749 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2750 /* --non-direct is only valid in migrate mode */
2751 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2752 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2753 { .val = 'E', .name = "component-end",
2754 .has_arg = required_argument},
2755 { .val = 'f', .name = "file", .has_arg = required_argument },
2756 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2757 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2758 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2759 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2760 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2761 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2762 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2763 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2764 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2765 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
2766 { .val = 'L', .name = "layout", .has_arg = required_argument },
2767 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2768 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2769 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2770 /* --non-block is only valid in migrate mode */
2771 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2772 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2773 { .val = 'o', .name = "ost", .has_arg = required_argument },
2774 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2775 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2776 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2778 { .val = 'p', .name = "pool", .has_arg = required_argument },
2779 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2780 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2781 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2782 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2783 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2784 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2785 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2786 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2787 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2788 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2789 /* --verbose is only valid in migrate mode */
2790 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2791 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2794 setstripe_args_init(&lsa);
2796 migrate_mode = (opc == SO_MIGRATE);
2797 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2799 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2801 while ((c = getopt_long(argc, argv,
2802 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2808 case LFS_COMP_ADD_OPT:
2811 case LFS_COMP_DEL_OPT:
2814 case LFS_COMP_FLAGS_OPT:
2815 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2816 &lsa.lsa_comp_neg_flags);
2819 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2820 fprintf(stderr, "%s: inverted flags are not supported\n",
2824 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2826 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2833 case LFS_COMP_SET_OPT:
2836 case LFS_COMP_NO_VERIFY_OPT:
2837 mirror_flags |= MF_NO_VERIFY;
2839 case LFS_MIRROR_ID_OPT:
2840 mirror_id = strtoul(optarg, &end, 0);
2841 if (*end != '\0' || mirror_id == 0) {
2843 "%s %s: invalid mirror ID '%s'\n",
2844 progname, argv[0], optarg);
2848 case LFS_MIRROR_FLAGS_OPT: {
2851 if (!mirror_mode || !last_mirror) {
2852 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2857 result = comp_str2flags(optarg, &last_mirror->m_flags,
2863 fprintf(stderr, "%s: inverted flags are not supported\n",
2868 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2870 "%s: unsupported mirror flags: %s\n",
2877 case LFS_LAYOUT_COPY:
2882 if (!migrate_mode) {
2884 "%s %s: -b|--block valid only for migrate command\n",
2888 migration_block = true;
2891 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2894 "%s %s: invalid stripe count '%s'\n",
2895 progname, argv[0], optarg);
2899 if (lsa.lsa_stripe_count == -1)
2900 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2903 /* delete the default striping pattern */
2905 if (opc == SO_MIRROR_SPLIT) {
2908 "%s %s: -d cannot used with -f\n",
2912 mirror_flags |= MF_DESTROY;
2916 if (!migrate_mode) {
2918 "%s %s: -D|--non-direct is valid "
2919 "only for migrate command\n",
2923 migration_flags |= MIGRATION_NONDIRECT;
2926 if (lsa.lsa_comp_end != 0) {
2927 result = comp_args_to_layout(lpp, &lsa, true);
2930 "%s %s: invalid layout\n",
2935 setstripe_args_init_inherit(&lsa);
2938 if (arg_is_eof(optarg)) {
2939 lsa.lsa_comp_end = LUSTRE_EOF;
2941 result = llapi_parse_size(optarg,
2946 "%s %s: invalid component end '%s'\n",
2947 progname, argv[0], optarg);
2953 if (!migrate_mode) {
2954 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2958 lsa.lsa_pattern = check_hashtype(optarg);
2959 if (lsa.lsa_pattern == 0) {
2961 "%s %s: bad stripe hash type '%s'\n",
2962 progname, argv[0], optarg);
2967 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2970 "%s %s: invalid stripe offset '%s'\n",
2971 progname, argv[0], optarg);
2974 if (lsa.lsa_stripe_off == -1)
2975 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2978 comp_id = strtoul(optarg, &end, 0);
2979 if (*end != '\0' || comp_id == 0 ||
2980 comp_id > LCME_ID_MAX) {
2982 "%s %s: invalid component ID '%s'\n",
2983 progname, argv[0], optarg);
2988 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2990 "error: %s: invalid option: %s\n",
2991 progname, argv[optopt + 1]);
2994 if (opc == SO_MIRROR_EXTEND) {
2995 if (last_mirror == NULL) {
2997 "error: %s: '-N' must exist in front of '%s'\n",
2998 progname, argv[optopt + 1]);
3001 last_mirror->m_file = optarg;
3002 last_mirror->m_count = 1;
3005 if (mirror_list == NULL)
3006 mirror_list = lfs_mirror_alloc();
3007 mirror_list->m_file = optarg;
3012 if (strcmp(argv[optind - 1], "mdt") == 0) {
3013 /* Can be only the first component */
3014 if (layout != NULL) {
3016 fprintf(stderr, "error: 'mdt' layout "
3017 "can be only the first one\n");
3020 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3022 fprintf(stderr, "error: 'mdt' layout "
3023 "size is too big\n");
3026 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3027 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3029 fprintf(stderr, "error: layout '%s' is "
3030 "unknown, supported layouts are: "
3031 "'mdt', 'raid0'\n", argv[optind]);
3036 if (!migrate_mode) {
3038 "%s %s: -m|--mdt is valid only for migrate command\n",
3042 migrate_mdt_mode = true;
3043 lsa.lsa_nr_tgts = parse_targets(tgts,
3044 sizeof(tgts) / sizeof(__u32),
3045 lsa.lsa_nr_tgts, optarg);
3046 if (lsa.lsa_nr_tgts < 0) {
3048 "%s %s: invalid MDT target(s) '%s'\n",
3049 progname, argv[0], optarg);
3053 lsa.lsa_tgts = tgts;
3054 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3055 lsa.lsa_stripe_off = tgts[0];
3058 if (!migrate_mode) {
3060 "%s %s: -n|--non-block valid only for migrate command\n",
3064 migration_flags |= MIGRATION_NONBLOCK;
3067 if (opc == SO_SETSTRIPE) {
3068 opc = SO_MIRROR_CREATE;
3072 if (optarg != NULL) {
3073 mirror_count = strtoul(optarg, &end, 0);
3074 if (*end != '\0' || mirror_count == 0) {
3076 "error: %s: bad mirror count: %s\n",
3083 new_mirror = lfs_mirror_alloc();
3084 new_mirror->m_count = mirror_count;
3086 if (mirror_list == NULL)
3087 mirror_list = new_mirror;
3089 if (last_mirror != NULL) {
3090 /* wrap up last mirror */
3091 if (lsa.lsa_comp_end == 0)
3092 lsa.lsa_comp_end = LUSTRE_EOF;
3094 result = comp_args_to_layout(lpp, &lsa, true);
3096 lfs_mirror_free(new_mirror);
3100 setstripe_args_init_inherit(&lsa);
3102 last_mirror->m_next = new_mirror;
3105 last_mirror = new_mirror;
3106 lpp = &last_mirror->m_layout;
3109 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3110 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3111 fprintf(stderr, "warning: '--ost-list' is "
3112 "deprecated, use '--ost' instead\n");
3114 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3116 "%s %s: -o|--ost incompatible with DoM layout\n",
3120 lsa.lsa_nr_tgts = parse_targets(tgts,
3121 sizeof(tgts) / sizeof(__u32),
3122 lsa.lsa_nr_tgts, optarg);
3123 if (lsa.lsa_nr_tgts < 0) {
3125 "%s %s: invalid OST target(s) '%s'\n",
3126 progname, argv[0], optarg);
3130 lsa.lsa_tgts = tgts;
3131 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3132 lsa.lsa_stripe_off = tgts[0];
3137 lsa.lsa_pool_name = optarg;
3139 if (strlen(lsa.lsa_pool_name) == 0 ||
3140 strncmp(lsa.lsa_pool_name, "none",
3141 LOV_MAXPOOLNAME) == 0)
3142 lsa.lsa_pool_name = NULL;
3145 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3149 "%s %s: invalid stripe size '%s'\n",
3150 progname, argv[0], optarg);
3155 if (!migrate_mode) {
3157 "%s %s: -v|--verbose valid only for migrate command\n",
3161 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3168 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3169 progname, argv[0], argv[optind - 1]);
3174 fname = argv[optind];
3176 if (optind == argc) {
3177 fprintf(stderr, "%s %s: FILE must be specified\n",
3182 if (mirror_mode && mirror_count == 0) {
3184 "error: %s: --mirror-count|-N option is required\n",
3191 if (lsa.lsa_comp_end == 0)
3192 lsa.lsa_comp_end = LUSTRE_EOF;
3195 if (lsa.lsa_comp_end != 0) {
3196 result = comp_args_to_layout(lpp, &lsa, true);
3201 if (mirror_flags & MF_NO_VERIFY) {
3202 if (opc != SO_MIRROR_EXTEND) {
3204 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3208 } else if (!has_m_file) {
3210 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3217 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3218 * altered by user space tool, so we don't need to support the
3219 * --component-set for this moment. */
3220 if (comp_set && !comp_id) {
3221 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3226 if ((delete + comp_set + comp_del + comp_add) > 1) {
3228 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3233 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3234 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3236 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3241 if ((comp_set || comp_del) &&
3242 (setstripe_args_specified(&lsa) || layout != NULL)) {
3244 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3249 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3251 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3256 if (comp_add || comp_del) {
3259 result = lstat(fname, &st);
3260 if (result == 0 && S_ISDIR(st.st_mode)) {
3262 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3268 fprintf(stderr, "error: %s: can't use --component-add "
3269 "or --component-del for mirror operation\n",
3276 if (layout == NULL) {
3278 "%s %s: option -E must be specified with --component-add\n",
3283 result = adjust_first_extent(fname, layout);
3284 if (result == -ENODATA)
3286 else if (result != 0)
3290 if (from_yaml && from_copy) {
3292 "%s: can't specify --yaml and --copy together\n",
3297 if ((from_yaml || from_copy) &&
3298 (setstripe_args_specified(&lsa) || layout != NULL)) {
3299 fprintf(stderr, "error: %s: can't specify --yaml with "
3300 "-c, -S, -i, -o, -p or -E options.\n",
3305 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3307 "%s %s: options --non-block and --block are mutually exclusive\n",
3312 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3315 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3320 if (migrate_mdt_mode) {
3321 struct lmv_user_md *lmu;
3323 /* initialize migrate mdt parameters */
3324 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3325 LMV_USER_MAGIC_SPECIFIC));
3328 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3329 progname, argv[0], strerror(ENOMEM));
3333 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3334 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3335 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3337 "%s %s: migrate should specify MDT index\n",
3342 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3343 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3344 lmu->lum_hash_type = lsa.lsa_pattern;
3346 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3347 if (lsa.lsa_pool_name) {
3348 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3349 sizeof(lmu->lum_pool_name) - 1);
3350 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3352 if (lsa.lsa_nr_tgts > 1) {
3355 if (lsa.lsa_stripe_count > 0 &&
3356 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3357 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3359 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3360 progname, lsa.lsa_stripe_count,
3366 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3367 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3368 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3369 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3371 lmu->lum_magic = LMV_USER_MAGIC;
3374 migrate_mdt_param.fp_lmv_md = lmu;
3375 migrate_mdt_param.fp_migrate = 1;
3376 } else if (layout == NULL) {
3377 /* initialize stripe parameters */
3378 param = calloc(1, offsetof(typeof(*param),
3379 lsp_osts[lsa.lsa_nr_tgts]));
3380 if (param == NULL) {
3382 "%s %s: cannot allocate memory for parameters: %s\n",
3383 progname, argv[0], strerror(ENOMEM));
3388 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3389 param->lsp_stripe_size = lsa.lsa_stripe_size;
3390 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3391 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3392 param->lsp_stripe_count = -1;
3394 param->lsp_stripe_count = lsa.lsa_stripe_count;
3396 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3397 param->lsp_stripe_offset = -1;
3399 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3400 param->lsp_pool = lsa.lsa_pool_name;
3401 param->lsp_is_specific = false;
3402 if (lsa.lsa_nr_tgts > 0) {
3403 if (lsa.lsa_stripe_count > 0 &&
3404 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3405 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3406 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3408 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3409 argv[0], lsa.lsa_stripe_count,
3415 param->lsp_is_specific = true;
3416 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3417 memcpy(param->lsp_osts, tgts,
3418 sizeof(*tgts) * lsa.lsa_nr_tgts);
3423 /* generate a layout from a YAML template */
3424 result = lfs_comp_create_from_yaml(template, &layout,
3427 fprintf(stderr, "error: %s: can't create composite "
3428 "layout from template file %s\n",
3432 } else if (from_copy) {
3433 layout = llapi_layout_get_by_path(template, 0);
3434 if (layout == NULL) {
3436 "%s: can't create composite layout from file %s.\n",
3437 progname, template);
3442 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3443 if (migrate_mdt_mode) {
3444 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3445 } else if (migrate_mode) {
3446 result = lfs_migrate(fname, migration_flags, param,
3448 } else if (comp_set != 0) {
3449 result = lfs_component_set(fname, comp_id,
3451 lsa.lsa_comp_neg_flags);
3452 } else if (comp_del != 0) {
3453 result = lfs_component_del(fname, comp_id,
3455 lsa.lsa_comp_neg_flags);
3456 } else if (comp_add != 0) {
3457 result = lfs_component_add(fname, layout);
3458 } else if (opc == SO_MIRROR_CREATE) {
3459 result = mirror_create(fname, mirror_list);
3460 } else if (opc == SO_MIRROR_EXTEND) {
3461 result = mirror_extend(fname, mirror_list,
3463 } else if (opc == SO_MIRROR_SPLIT) {
3464 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3466 "%s %s: no mirror id or component id or pool name"
3467 " is specified\n", progname, argv[0]);
3470 if (lsa.lsa_pool_name)
3471 mirror_flags |= MF_COMP_POOL;
3472 else if (mirror_id != 0)
3473 comp_id = mirror_id;
3475 mirror_flags |= MF_COMP_ID;
3476 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3478 has_m_file ? mirror_list->m_file :
3480 } else if (layout != NULL) {
3481 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3488 result = llapi_file_open_param(fname,
3497 /* Save the first error encountered. */
3505 free(migrate_mdt_param.fp_lmv_md);
3506 llapi_layout_free(layout);
3507 lfs_mirror_list_free(mirror_list);
3512 llapi_layout_free(layout);
3513 lfs_mirror_list_free(mirror_list);
3517 static int lfs_poollist(int argc, char **argv)
3522 return llapi_poollist(argv[1]);
3525 static int set_time(time_t *time, time_t *set, char *str)
3533 else if (str[0] == '-')
3539 t = strtol(str, &endptr, 0);
3540 if (*endptr != '\0') {
3542 "%s find: bad time '%s': %s\n",
3543 progname, str, strerror(EINVAL));
3546 if (*time < t * 24 * 60 * 60) {
3550 "%s find: bad time '%s': too large\n",
3555 *set = *time - t * 24 * 60 * 60;
3559 static int name2uid(unsigned int *id, const char *name)
3561 struct passwd *passwd;
3563 passwd = getpwnam(name);
3566 *id = passwd->pw_uid;
3571 static int name2gid(unsigned int *id, const char *name)
3573 struct group *group;
3575 group = getgrnam(name);
3578 *id = group->gr_gid;
3583 static inline int name2projid(unsigned int *id, const char *name)
3588 static int uid2name(char **name, unsigned int id)
3590 struct passwd *passwd;
3592 passwd = getpwuid(id);
3595 *name = passwd->pw_name;
3600 static inline int gid2name(char **name, unsigned int id)
3602 struct group *group;
3604 group = getgrgid(id);
3607 *name = group->gr_name;
3612 static int name2layout(__u32 *layout, char *name)
3614 char *ptr, *layout_name;
3617 for (ptr = name; ; ptr = NULL) {
3618 layout_name = strtok(ptr, ",");
3619 if (layout_name == NULL)
3621 if (strcmp(layout_name, "released") == 0)
3622 *layout |= LOV_PATTERN_F_RELEASED;
3623 else if (strcmp(layout_name, "raid0") == 0)
3624 *layout |= LOV_PATTERN_RAID0;
3625 else if (strcmp(layout_name, "mdt") == 0)
3626 *layout |= LOV_PATTERN_MDT;
3633 static int lfs_find(int argc, char **argv)
3638 struct find_param param = {
3642 struct option long_opts[] = {
3643 { .val = 'A', .name = "atime", .has_arg = required_argument },
3644 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3645 { .val = LFS_COMP_COUNT_OPT,
3646 .name = "comp-count", .has_arg = required_argument },
3647 { .val = LFS_COMP_COUNT_OPT,
3648 .name = "component-count",
3649 .has_arg = required_argument },
3650 { .val = LFS_COMP_FLAGS_OPT,
3651 .name = "comp-flags", .has_arg = required_argument },
3652 { .val = LFS_COMP_FLAGS_OPT,
3653 .name = "component-flags",
3654 .has_arg = required_argument },
3655 { .val = LFS_COMP_START_OPT,
3656 .name = "comp-start", .has_arg = required_argument },
3657 { .val = LFS_COMP_START_OPT,
3658 .name = "component-start",
3659 .has_arg = required_argument },
3660 { .val = LFS_MIRROR_STATE_OPT,
3661 .name = "mirror-state", .has_arg = required_argument },
3662 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3663 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3664 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3665 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3666 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3667 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3668 { .val = 'E', .name = "component-end",
3669 .has_arg = required_argument },
3670 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3671 { .val = 'g', .name = "gid", .has_arg = required_argument },
3672 { .val = 'G', .name = "group", .has_arg = required_argument },
3673 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3674 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3675 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3676 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3677 { .val = 'l', .name = "lazy", .has_arg = no_argument },
3678 { .val = 'L', .name = "layout", .has_arg = required_argument },
3679 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3680 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3681 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3682 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3683 { .val = 'n', .name = "name", .has_arg = required_argument },
3684 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3685 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3686 { .val = 'O', .name = "obd", .has_arg = required_argument },
3687 { .val = 'O', .name = "ost", .has_arg = required_argument },
3688 /* no short option for pool yet, can be 'p' after 2.18 */
3689 { .val = LFS_POOL_OPT,
3690 .name = "pool", .has_arg = required_argument },
3691 { .val = '0', .name = "print0", .has_arg = no_argument },
3692 { .val = 'P', .name = "print", .has_arg = no_argument },
3693 { .val = LFS_PROJID_OPT,
3694 .name = "projid", .has_arg = required_argument },
3695 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3696 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3697 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3698 { .val = 's', .name = "size", .has_arg = required_argument },
3699 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3700 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3701 { .val = 't', .name = "type", .has_arg = required_argument },
3702 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3703 { .val = 'u', .name = "uid", .has_arg = required_argument },
3704 { .val = 'U', .name = "user", .has_arg = required_argument },
3705 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3706 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3718 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3719 while ((c = getopt_long_only(argc, argv,
3720 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3721 long_opts, NULL)) >= 0) {
3726 /* '!' is part of option */
3727 /* when getopt_long_only() finds a string which is not
3728 * an option nor a known option argument it returns 1
3729 * in that case if we already have found pathstart and pathend
3730 * (i.e. we have the list of pathnames),
3731 * the only supported value is "!"
3733 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3734 if (!isoption && pathend != -1) {
3735 fprintf(stderr, "err: %s: filename|dirname must either "
3736 "precede options or follow options\n",
3741 if (!isoption && pathstart == -1)
3742 pathstart = optind - 1;
3743 if (isoption && pathstart != -1 && pathend == -1)
3744 pathend = optind - 2;
3750 /* unknown; opt is "!" or path component,
3751 * checking done above.
3753 if (strcmp(optarg, "!") == 0)
3757 xtime = ¶m.fp_atime;
3758 xsign = ¶m.fp_asign;
3759 param.fp_exclude_atime = !!neg_opt;
3760 /* no break, this falls through to 'C' for ctime */
3763 xtime = ¶m.fp_ctime;
3764 xsign = ¶m.fp_csign;
3765 param.fp_exclude_ctime = !!neg_opt;
3767 /* no break, this falls through to 'M' for mtime */
3770 xtime = ¶m.fp_mtime;
3771 xsign = ¶m.fp_msign;
3772 param.fp_exclude_mtime = !!neg_opt;
3774 rc = set_time(&t, xtime, optarg);
3775 if (rc == INT_MAX) {
3783 if (optarg[0] == '+') {
3784 param.fp_blocks_sign = -1;
3786 } else if (optarg[0] == '-') {
3787 param.fp_blocks_sign = 1;
3791 param.fp_blocks_units = 1024;
3792 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3793 ¶m.fp_blocks_units, 0);
3795 fprintf(stderr, "error: bad blocks '%s'\n",
3799 param.fp_check_blocks = 1;
3800 param.fp_exclude_blocks = !!neg_opt;
3802 case LFS_COMP_COUNT_OPT:
3803 if (optarg[0] == '+') {
3804 param.fp_comp_count_sign = -1;
3806 } else if (optarg[0] == '-') {
3807 param.fp_comp_count_sign = 1;
3811 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3812 if (*endptr != '\0') {
3813 fprintf(stderr, "error: bad component count "
3817 param.fp_check_comp_count = 1;
3818 param.fp_exclude_comp_count = !!neg_opt;
3820 case LFS_COMP_FLAGS_OPT:
3821 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3822 ¶m.fp_comp_neg_flags);
3824 fprintf(stderr, "error: bad component flags "
3828 param.fp_check_comp_flags = 1;
3830 __u32 flags = param.fp_comp_neg_flags;
3831 param.fp_comp_neg_flags = param.fp_comp_flags;
3832 param.fp_comp_flags = flags;
3835 case LFS_COMP_START_OPT:
3836 if (optarg[0] == '+') {
3837 param.fp_comp_start_sign = -1;
3839 } else if (optarg[0] == '-') {
3840 param.fp_comp_start_sign = 1;
3844 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3845 ¶m.fp_comp_start_units, 0);
3847 fprintf(stderr, "error: bad component start "
3851 param.fp_check_comp_start = 1;
3852 param.fp_exclude_comp_start = !!neg_opt;
3854 case LFS_MIRROR_STATE_OPT:
3855 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3856 ¶m.fp_mirror_neg_state);
3859 "error: bad mirrored file state '%s'\n",
3863 param.fp_check_mirror_state = 1;
3865 __u16 state = param.fp_mirror_neg_state;
3866 param.fp_mirror_neg_state =
3867 param.fp_mirror_state;
3868 param.fp_mirror_state = state;
3872 if (optarg[0] == '+') {
3873 param.fp_stripe_count_sign = -1;
3875 } else if (optarg[0] == '-') {
3876 param.fp_stripe_count_sign = 1;
3880 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3881 if (*endptr != '\0') {
3882 fprintf(stderr,"error: bad stripe_count '%s'\n",
3887 param.fp_check_stripe_count = 1;
3888 param.fp_exclude_stripe_count = !!neg_opt;
3891 param.fp_max_depth = strtol(optarg, 0, 0);
3894 if (optarg[0] == '+') {
3895 param.fp_comp_end_sign = -1;
3897 } else if (optarg[0] == '-') {
3898 param.fp_comp_end_sign = 1;
3902 if (arg_is_eof(optarg)) {
3903 param.fp_comp_end = LUSTRE_EOF;
3904 param.fp_comp_end_units = 1;
3907 rc = llapi_parse_size(optarg,
3909 ¶m.fp_comp_end_units, 0);
3912 fprintf(stderr, "error: bad component end "
3916 param.fp_check_comp_end = 1;
3917 param.fp_exclude_comp_end = !!neg_opt;
3921 rc = name2gid(¶m.fp_gid, optarg);
3923 param.fp_gid = strtoul(optarg, &endptr, 10);
3924 if (*endptr != '\0') {
3925 fprintf(stderr, "Group/GID: %s cannot "
3926 "be found.\n", optarg);
3931 param.fp_exclude_gid = !!neg_opt;
3932 param.fp_check_gid = 1;
3935 param.fp_hash_type = check_hashtype(optarg);
3936 if (param.fp_hash_type == 0) {
3937 fprintf(stderr, "error: bad hash_type '%s'\n",
3942 param.fp_check_hash_type = 1;
3943 param.fp_exclude_hash_type = !!neg_opt;
3949 ret = name2layout(¶m.fp_layout, optarg);
3952 param.fp_exclude_layout = !!neg_opt;
3953 param.fp_check_layout = 1;
3957 rc = name2uid(¶m.fp_uid, optarg);
3959 param.fp_uid = strtoul(optarg, &endptr, 10);
3960 if (*endptr != '\0') {
3961 fprintf(stderr, "User/UID: %s cannot "
3962 "be found.\n", optarg);
3967 param.fp_exclude_uid = !!neg_opt;
3968 param.fp_check_uid = 1;
3971 param.fp_pattern = (char *)optarg;
3972 param.fp_exclude_pattern = !!neg_opt;
3975 if (optarg[0] == '+') {
3976 param.fp_mirror_count_sign = -1;
3978 } else if (optarg[0] == '-') {
3979 param.fp_mirror_count_sign = 1;
3983 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3984 if (*endptr != '\0') {
3986 "error: bad mirror count '%s'\n",
3990 param.fp_check_mirror_count = 1;
3991 param.fp_exclude_mirror_count = !!neg_opt;
3996 char *buf, *token, *next, *p;
4000 buf = strdup(optarg);
4006 param.fp_exclude_obd = !!neg_opt;
4009 while (token && *token) {
4010 token = strchr(token, ',');
4017 param.fp_exclude_mdt = !!neg_opt;
4018 param.fp_num_alloc_mdts += len;
4019 tmp = realloc(param.fp_mdt_uuid,
4020 param.fp_num_alloc_mdts *
4021 sizeof(*param.fp_mdt_uuid));
4027 param.fp_mdt_uuid = tmp;
4029 param.fp_exclude_obd = !!neg_opt;
4030 param.fp_num_alloc_obds += len;
4031 tmp = realloc(param.fp_obd_uuid,
4032 param.fp_num_alloc_obds *
4033 sizeof(*param.fp_obd_uuid));
4039 param.fp_obd_uuid = tmp;
4041 for (token = buf; token && *token; token = next) {
4042 struct obd_uuid *puuid;
4045 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4048 ¶m.fp_obd_uuid[param.fp_num_obds++];
4050 p = strchr(token, ',');
4057 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4062 strncpy(puuid->uuid, token,
4063 sizeof(puuid->uuid));
4070 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4074 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4076 "Pool name %s is too long (max %d)\n",
4077 optarg, LOV_MAXPOOLNAME);
4082 * We do check for empty pool because empty pool
4083 * is used to find V1 LOV attributes
4085 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4086 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4087 param.fp_exclude_pool = !!neg_opt;
4088 param.fp_check_pool = 1;
4090 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4091 case 'p': /* want this for --pool, to match getstripe/find */
4093 "warning: -p deprecated, use --print0 or -0\n");
4096 param.fp_zero_end = 1;
4098 case 'P': /* we always print, this option is a no-op */
4100 case LFS_PROJID_OPT:
4101 rc = name2projid(¶m.fp_projid, optarg);
4103 param.fp_projid = strtoul(optarg, &endptr, 10);
4104 if (*endptr != '\0') {
4106 "Invalid project ID: %s",
4112 param.fp_exclude_projid = !!neg_opt;
4113 param.fp_check_projid = 1;
4116 if (optarg[0] == '+') {
4117 param.fp_size_sign = -1;
4119 } else if (optarg[0] == '-') {
4120 param.fp_size_sign = 1;
4124 ret = llapi_parse_size(optarg, ¶m.fp_size,
4125 ¶m.fp_size_units, 0);
4127 fprintf(stderr, "error: bad file size '%s'\n",
4131 param.fp_check_size = 1;
4132 param.fp_exclude_size = !!neg_opt;
4135 if (optarg[0] == '+') {
4136 param.fp_stripe_size_sign = -1;
4138 } else if (optarg[0] == '-') {
4139 param.fp_stripe_size_sign = 1;
4143 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4144 ¶m.fp_stripe_size_units, 0);
4146 fprintf(stderr, "error: bad stripe_size '%s'\n",
4150 param.fp_check_stripe_size = 1;
4151 param.fp_exclude_stripe_size = !!neg_opt;
4154 param.fp_exclude_type = !!neg_opt;
4155 switch (optarg[0]) {
4157 param.fp_type = S_IFBLK;
4160 param.fp_type = S_IFCHR;
4163 param.fp_type = S_IFDIR;
4166 param.fp_type = S_IFREG;
4169 param.fp_type = S_IFLNK;
4172 param.fp_type = S_IFIFO;
4175 param.fp_type = S_IFSOCK;
4178 fprintf(stderr, "error: %s: bad type '%s'\n",
4185 if (optarg[0] == '+') {
4186 param.fp_mdt_count_sign = -1;
4188 } else if (optarg[0] == '-') {
4189 param.fp_mdt_count_sign = 1;
4193 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4194 if (*endptr != '\0') {
4195 fprintf(stderr, "error: bad mdt_count '%s'\n",
4200 param.fp_check_mdt_count = 1;
4201 param.fp_exclude_mdt_count = !!neg_opt;
4209 if (pathstart == -1) {
4210 fprintf(stderr, "error: %s: no filename|pathname\n",
4214 } else if (pathend == -1) {
4220 rc = llapi_find(argv[pathstart], ¶m);
4221 if (rc != 0 && ret == 0)
4223 } while (++pathstart < pathend);
4226 fprintf(stderr, "error: %s failed for %s.\n",
4227 argv[0], argv[optind - 1]);
4229 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4230 free(param.fp_obd_uuid);
4232 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4233 free(param.fp_mdt_uuid);
4238 static int lfs_getstripe_internal(int argc, char **argv,
4239 struct find_param *param)
4241 struct option long_opts[] = {
4242 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4243 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4244 { .val = LFS_COMP_COUNT_OPT,
4245 .name = "comp-count", .has_arg = no_argument },
4246 { .val = LFS_COMP_COUNT_OPT,
4247 .name = "component-count", .has_arg = no_argument },
4248 { .val = LFS_COMP_FLAGS_OPT,
4249 .name = "comp-flags", .has_arg = optional_argument },
4250 { .val = LFS_COMP_FLAGS_OPT,
4251 .name = "component-flags", .has_arg = optional_argument },
4252 { .val = LFS_COMP_START_OPT,
4253 .name = "comp-start", .has_arg = optional_argument },
4254 { .val = LFS_COMP_START_OPT,
4255 .name = "component-start", .has_arg = optional_argument },
4256 { .val = LFS_MIRROR_INDEX_OPT,
4257 .name = "mirror-index", .has_arg = required_argument },
4258 { .val = LFS_MIRROR_ID_OPT,
4259 .name = "mirror-id", .has_arg = required_argument },
4260 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4261 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4262 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4263 { .val = 'd', .name = "directory", .has_arg = no_argument },
4264 { .val = 'D', .name = "default", .has_arg = no_argument },
4265 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4266 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4267 { .val = 'F', .name = "fid", .has_arg = no_argument },
4268 { .val = 'g', .name = "generation", .has_arg = no_argument },
4269 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4270 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4271 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4272 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4273 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4274 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4275 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
4276 { .val = 'L', .name = "layout", .has_arg = no_argument },
4277 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4278 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4279 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4280 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4281 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4282 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4283 { .val = 'O', .name = "obd", .has_arg = required_argument },
4284 { .val = 'O', .name = "ost", .has_arg = required_argument },
4285 { .val = 'p', .name = "pool", .has_arg = no_argument },
4286 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4287 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4288 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4289 { .val = 'R', .name = "raw", .has_arg = no_argument },
4290 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4291 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4292 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4293 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4294 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4295 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4296 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4297 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4301 int pathstart = -1, pathend = -1;
4305 while ((c = getopt_long(argc, argv,
4306 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4307 long_opts, NULL)) != -1) {
4311 /* '!' is part of option */
4312 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4313 if (!isoption && pathend != -1) {
4315 "error: %s: filename|dirname must either precede options or follow options\n",
4319 if (!isoption && pathstart == -1)
4320 pathstart = optind - 1;
4321 if (isoption && pathstart != -1 && pathend == -1)
4322 pathend = optind - 2;
4326 /* unknown: opt is "!" */
4327 if (strcmp(optarg, "!") == 0)
4331 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4332 param->fp_verbose |= VERBOSE_COUNT;
4333 param->fp_max_depth = 0;
4336 case LFS_COMP_COUNT_OPT:
4337 param->fp_verbose |= VERBOSE_COMP_COUNT;
4338 param->fp_max_depth = 0;
4340 case LFS_COMP_FLAGS_OPT:
4341 if (optarg != NULL) {
4342 rc = comp_str2flags(optarg,
4343 ¶m->fp_comp_flags,
4344 ¶m->fp_comp_neg_flags);
4346 fprintf(stderr, "error: %s bad "
4347 "component flags '%s'.\n",
4351 param->fp_check_comp_flags = 1;
4353 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4354 param->fp_max_depth = 0;
4357 case LFS_COMP_START_OPT:
4358 if (optarg != NULL) {
4360 if (tmp[0] == '+') {
4361 param->fp_comp_start_sign = -1;
4363 } else if (tmp[0] == '-') {
4364 param->fp_comp_start_sign = 1;
4367 rc = llapi_parse_size(tmp,
4368 ¶m->fp_comp_start,
4369 ¶m->fp_comp_start_units, 0);
4371 fprintf(stderr, "error: %s bad "
4372 "component start '%s'.\n",
4376 param->fp_check_comp_start = 1;
4379 param->fp_verbose |= VERBOSE_COMP_START;
4380 param->fp_max_depth = 0;
4383 case LFS_MIRROR_INDEX_OPT:
4384 if (optarg[0] == '+') {
4385 param->fp_mirror_index_sign = -1;
4387 } else if (optarg[0] == '-') {
4388 param->fp_mirror_index_sign = 1;
4392 param->fp_mirror_index = strtoul(optarg, &end, 0);
4393 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4394 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4396 "%s %s: invalid mirror index '%s'\n",
4397 progname, argv[0], optarg);
4400 if (param->fp_mirror_id != 0) {
4402 "%s %s: can't specify both mirror index and mirror ID\n",
4406 param->fp_check_mirror_index = 1;
4407 param->fp_exclude_mirror_index = !!neg_opt;
4409 case LFS_MIRROR_ID_OPT:
4410 if (optarg[0] == '+') {
4411 param->fp_mirror_id_sign = -1;
4413 } else if (optarg[0] == '-') {
4414 param->fp_mirror_id_sign = 1;
4418 param->fp_mirror_id = strtoul(optarg, &end, 0);
4419 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4420 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4422 "%s %s: invalid mirror ID '%s'\n",
4423 progname, argv[0], optarg);
4426 if (param->fp_mirror_index != 0) {
4428 "%s %s: can't specify both mirror index and mirror ID\n",
4432 param->fp_check_mirror_id = 1;
4433 param->fp_exclude_mirror_id = !!neg_opt;
4436 param->fp_max_depth = 0;
4439 param->fp_get_default_lmv = 1;
4442 if (optarg != NULL) {
4444 if (tmp[0] == '+') {
4445 param->fp_comp_end_sign = -1;
4447 } else if (tmp[0] == '-') {
4448 param->fp_comp_end_sign = 1;
4452 if (arg_is_eof(tmp)) {
4453 param->fp_comp_end = LUSTRE_EOF;
4454 param->fp_comp_end_units = 1;
4457 rc = llapi_parse_size(tmp,
4458 ¶m->fp_comp_end,
4459 ¶m->fp_comp_end_units, 0);
4462 fprintf(stderr, "error: %s bad "
4463 "component end '%s'.\n",
4467 param->fp_check_comp_end = 1;
4469 param->fp_verbose |= VERBOSE_COMP_END;
4470 param->fp_max_depth = 0;
4474 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4475 param->fp_verbose |= VERBOSE_DFID;
4476 param->fp_max_depth = 0;
4480 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4481 param->fp_verbose |= VERBOSE_GENERATION;
4482 param->fp_max_depth = 0;
4486 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4487 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4488 param->fp_max_depth = 0;
4492 if (optarg != NULL) {
4493 param->fp_comp_id = strtoul(optarg, &end, 0);
4494 if (*end != '\0' || param->fp_comp_id == 0 ||
4495 param->fp_comp_id > LCME_ID_MAX) {
4496 fprintf(stderr, "error: %s bad "
4497 "component id '%s'\n",
4501 param->fp_check_comp_id = 1;
4504 param->fp_max_depth = 0;
4505 param->fp_verbose |= VERBOSE_COMP_ID;
4509 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4510 param->fp_verbose |= VERBOSE_PATTERN;
4511 param->fp_max_depth = 0;
4514 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4516 fprintf(stderr, "warning: '-M' deprecated"
4517 ", use '--mdt-index' or '-m' instead\n");
4520 if (!(param->fp_verbose & VERBOSE_DETAIL))
4521 param->fp_max_depth = 0;
4522 param->fp_verbose |= VERBOSE_MDTINDEX;
4525 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4526 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4527 param->fp_max_depth = 0;
4531 if (param->fp_obd_uuid) {
4533 "error: %s: only one obduuid allowed",
4537 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4540 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4541 param->fp_verbose |= VERBOSE_POOL;
4542 param->fp_max_depth = 0;
4549 param->fp_recursive = 1;
4555 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4556 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4557 param->fp_max_depth = 0;
4561 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4571 if (pathstart == -1) {
4572 fprintf(stderr, "error: %s: no filename|pathname\n",
4575 } else if (pathend == -1) {
4583 if (param->fp_recursive)
4584 param->fp_max_depth = -1;
4585 else if (param->fp_verbose & VERBOSE_DETAIL)
4586 param->fp_max_depth = 1;
4588 if (!param->fp_verbose)
4589 param->fp_verbose = VERBOSE_DEFAULT;
4590 if (param->fp_quiet)
4591 param->fp_verbose = VERBOSE_OBJID;
4594 rc = llapi_getstripe(argv[pathstart], param);
4595 } while (++pathstart < pathend && !rc);
4598 fprintf(stderr, "error: %s failed for %s.\n",
4599 argv[0], argv[optind - 1]);
4603 static int lfs_tgts(int argc, char **argv)
4605 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4606 struct find_param param;
4607 int index = 0, rc=0;
4612 if (argc == 2 && !realpath(argv[1], path)) {
4614 fprintf(stderr, "error: invalid path '%s': %s\n",
4615 argv[1], strerror(-rc));
4619 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4620 /* Check if we have a mount point */
4621 if (mntdir[0] == '\0')
4624 memset(¶m, 0, sizeof(param));
4625 if (!strcmp(argv[0], "mdts"))
4626 param.fp_get_lmv = 1;
4628 rc = llapi_ostlist(mntdir, ¶m);
4630 fprintf(stderr, "error: %s: failed on %s\n",
4633 if (path[0] != '\0')
4635 memset(mntdir, 0, PATH_MAX);
4641 static int lfs_getstripe(int argc, char **argv)
4643 struct find_param param = { 0 };
4645 param.fp_max_depth = 1;
4646 return lfs_getstripe_internal(argc, argv, ¶m);
4650 static int lfs_getdirstripe(int argc, char **argv)
4652 struct find_param param = { 0 };
4653 struct option long_opts[] = {
4654 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4655 { .val = 'D', .name = "default", .has_arg = no_argument },
4656 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4657 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4658 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4659 { .val = 'O', .name = "obd", .has_arg = required_argument },
4660 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4661 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4662 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4666 param.fp_get_lmv = 1;
4668 while ((c = getopt_long(argc, argv,
4669 "cDHimO:rtTy", long_opts, NULL)) != -1)
4674 param.fp_verbose |= VERBOSE_COUNT;
4677 param.fp_get_default_lmv = 1;
4681 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4683 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4685 fprintf(stderr, "warning: '-t' deprecated, "
4686 "use '--mdt-hash' or '-H' instead\n");
4689 param.fp_verbose |= VERBOSE_HASH_TYPE;
4692 if (param.fp_obd_uuid) {
4694 "error: %s: only one obduuid allowed",
4698 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4701 param.fp_recursive = 1;
4714 if (param.fp_recursive)
4715 param.fp_max_depth = -1;
4717 if (!param.fp_verbose)
4718 param.fp_verbose = VERBOSE_DEFAULT;
4721 rc = llapi_getstripe(argv[optind], ¶m);
4722 } while (++optind < argc && !rc);
4725 fprintf(stderr, "error: %s failed for %s.\n",
4726 argv[0], argv[optind - 1]);
4731 MNTDF_INODES = 0x0001,
4732 MNTDF_COOKED = 0x0002,
4733 MNTDF_LAZY = 0x0004,
4734 MNTDF_VERBOSE = 0x0008,
4735 MNTDF_SHOW = 0x0010,
4738 #define COOK(value) \
4741 while (value > 1024) { \
4749 #define CDF "%11llu"
4750 #define HDF "%8.1f%c"
4754 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4756 double avail, used, ratio = 0;
4759 avail = st->os_ffree;
4760 used = st->os_files - st->os_ffree;
4762 avail = st->os_bavail;
4763 used = st->os_blocks - st->os_bfree;
4765 if (avail + used > 0)
4766 ratio = used / (used + avail) * 100;
4768 /* Round up to match df(1) usage percentage */
4769 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4772 /* This is to identify various problem states for "lfs df" if .osn_err = true,
4773 * so only show flags reflecting those states by default. Informational states
4774 * are only shown with "-v" and use lower-case names to distinguish them.
4775 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
4777 static struct obd_statfs_state_names {
4778 enum obd_statfs_state osn_state;
4779 const char osn_name;
4782 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
4783 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
4784 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
4785 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
4786 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
4787 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
4788 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
4789 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
4790 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
4793 static int showdf(char *mntdir, struct obd_statfs *stat,
4794 char *uuid, enum mntdf_flags flags,
4795 char *type, int index, int rc)
4797 long long avail, used, total;
4799 char *suffix = "KMGTPEZY";
4800 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4801 char tbuf[3 * sizeof(__u64)];
4802 char ubuf[3 * sizeof(__u64)];
4803 char abuf[3 * sizeof(__u64)];
4804 char rbuf[3 * sizeof(__u64)];
4811 if (flags & MNTDF_INODES) {
4812 avail = stat->os_ffree;
4813 used = stat->os_files - stat->os_ffree;
4814 total = stat->os_files;
4816 int shift = flags & MNTDF_COOKED ? 0 : 10;
4818 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4819 used = ((stat->os_blocks - stat->os_bfree) *
4820 stat->os_bsize) >> shift;
4821 total = (stat->os_blocks * stat->os_bsize) >> shift;
4824 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4826 if (flags & MNTDF_COOKED) {
4830 cook_val = (double)total;
4833 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4836 snprintf(tbuf, sizeof(tbuf), CDF, total);
4838 cook_val = (double)used;
4841 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4844 snprintf(ubuf, sizeof(ubuf), CDF, used);
4846 cook_val = (double)avail;
4849 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4852 snprintf(abuf, sizeof(abuf), CDF, avail);
4854 snprintf(tbuf, sizeof(tbuf), CDF, total);
4855 snprintf(ubuf, sizeof(tbuf), CDF, used);
4856 snprintf(abuf, sizeof(tbuf), CDF, avail);
4859 sprintf(rbuf, RDF, ratio);
4860 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4861 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4863 printf("[%s:%d]", type, index);
4865 if (stat->os_state) {
4869 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
4870 if (oss_names[i].osn_state & stat->os_state &&
4871 (oss_names[i].osn_err ||
4872 flags & MNTDF_VERBOSE))
4873 printf("%c", oss_names[i].osn_name);
4880 printf(UUF": inactive device\n", uuid);
4883 printf(UUF": %s\n", uuid, strerror(-rc));
4890 struct ll_stat_type {
4895 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4897 struct ll_statfs_data {
4899 struct obd_statfs sd_st;
4902 struct ll_statfs_buf {
4904 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4907 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4908 int ops, struct ll_statfs_buf *lsb)
4910 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4911 struct obd_uuid uuid_buf;
4912 char *poolname = NULL;
4913 struct ll_stat_type types[] = {
4914 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4915 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4916 { .st_name = NULL } };
4917 struct ll_stat_type *tp;
4918 __u64 ost_files = 0;
4919 __u64 ost_ffree = 0;
4927 poolname = strchr(pool, '.');
4928 if (poolname != NULL) {
4929 if (strncmp(fsname, pool, strlen(fsname))) {
4930 fprintf(stderr, "filesystem name incorrect\n");
4938 fd = open(mntdir, O_RDONLY);
4941 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4946 if (flags & MNTDF_SHOW) {
4947 if (flags & MNTDF_INODES)
4948 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4949 "UUID", "Inodes", "IUsed", "IFree",
4950 "IUse%", "Mounted on");
4952 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4954 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4955 "Used", "Available", "Use%", "Mounted on");
4958 for (tp = types; tp->st_name != NULL; tp++) {
4959 bool have_ost = false;
4961 if (!(tp->st_op & ops))
4964 for (index = 0; ; index++) {
4965 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4966 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4967 type = flags & MNTDF_LAZY ?
4968 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4969 rc2 = llapi_obd_fstatfs(fd, type, index,
4970 &stat_buf, &uuid_buf);
4975 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4976 if (!(flags & MNTDF_VERBOSE))
4978 } else if (rc2 < 0 && rc == 0) {
4982 /* If we have OSTs then don't report MDT block counts.
4983 * For MDT-only filesystems the expectation is that all
4984 * layouts have a DoM component. For filesystems with
4985 * OSTs, files are not necessarily going to store data
4986 * on MDTs, and MDT space is limited to a fraction of
4987 * OST space, so don't include it in the summary.
4989 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
4996 if (poolname && tp->st_op == LL_STATFS_LOV &&
4997 llapi_search_ost(fsname, poolname,
4998 obd_uuid2str(&uuid_buf)) != 1)
5001 /* the llapi_obd_statfs() call may have returned with
5002 * an error, but if it filled in uuid_buf we will at
5003 * lease use that to print out a message for that OBD.
5004 * If we didn't get anything in the uuid_buf, then fill
5005 * it in so that we can print an error message. */
5006 if (uuid_buf.uuid[0] == '\0')
5007 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5008 "%s%04x", tp->st_name, index);
5010 lsb->sb_buf[lsb->sb_count].sd_index = index;
5011 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5014 if (flags & MNTDF_SHOW)
5015 showdf(mntdir, &stat_buf,
5016 obd_uuid2str(&uuid_buf), flags,
5017 tp->st_name, index, rc2);
5022 if (tp->st_op == LL_STATFS_LMV) {
5023 sum.os_ffree += stat_buf.os_ffree;
5024 sum.os_files += stat_buf.os_files;
5025 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5026 ost_files += stat_buf.os_files;
5027 ost_ffree += stat_buf.os_ffree;
5029 sum.os_blocks += stat_buf.os_blocks *
5031 sum.os_bfree += stat_buf.os_bfree *
5033 sum.os_bavail += stat_buf.os_bavail *
5040 /* If we have _some_ OSTs, but don't have as many free objects on the
5041 * OST as inodes on the MDTs, reduce the reported number of inodes
5042 * to compensate, so that the "inodes in use" number is correct.
5043 * This should be kept in sync with ll_statfs_internal().
5045 if (ost_files && ost_ffree < sum.os_ffree) {
5046 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5047 sum.os_ffree = ost_ffree;
5049 if (flags & MNTDF_SHOW) {
5051 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5058 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5060 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5062 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5064 int r1 = obd_statfs_ratio(st1, false);
5065 int r2 = obd_statfs_ratio(st2, false);
5066 int64_t result = r1 - r2;
5068 /* if both space usage are above 90, compare free inodes */
5069 if (r1 > 90 && r2 > 90)
5070 result = st2->os_ffree - st1->os_ffree;
5074 else if (result == 0)
5081 static int lfs_setdirstripe(int argc, char **argv)
5085 struct lfs_setstripe_args lsa = { 0 };
5086 struct llapi_stripe_param *param = NULL;
5087 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5090 char *mode_opt = NULL;
5091 bool default_stripe = false;
5092 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5093 mode_t previous_mode = 0;
5094 bool delete = false;
5095 struct ll_statfs_buf *lsb = NULL;
5096 char mntdir[PATH_MAX] = "";
5097 bool auto_distributed = false;
5099 struct option long_opts[] = {
5100 { .val = 'c', .name = "count", .has_arg = required_argument },
5101 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5102 { .val = 'd', .name = "delete", .has_arg = no_argument },
5103 { .val = 'D', .name = "default", .has_arg = no_argument },
5104 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5105 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5106 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5107 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5108 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5109 { .val = 'i', .name = "index", .has_arg = required_argument },
5111 { .val = 'o', .name = "mode", .has_arg = required_argument },
5112 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5113 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5115 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5116 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5119 setstripe_args_init(&lsa);
5121 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
5129 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5132 "%s %s: invalid stripe count '%s'\n",
5133 progname, argv[0], optarg);
5139 default_stripe = true;
5142 default_stripe = true;
5144 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5146 fprintf(stderr, "warning: '--hash-type' and '-t' "
5147 "deprecated, use '--mdt-hash' or '-H' instead\n");
5150 lsa.lsa_pattern = check_hashtype(optarg);
5151 if (lsa.lsa_pattern == 0) {
5153 "%s %s: bad stripe hash type '%s'\n",
5154 progname, argv[0], optarg);
5159 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5160 if (strcmp(argv[optind - 1], "--index") == 0)
5162 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5165 lsa.lsa_nr_tgts = parse_targets(mdts,
5166 sizeof(mdts) / sizeof(__u32),
5167 lsa.lsa_nr_tgts, optarg);
5168 if (lsa.lsa_nr_tgts < 0) {
5170 "%s %s: invalid MDT target(s) '%s'\n",
5171 progname, argv[0], optarg);
5175 lsa.lsa_tgts = mdts;
5176 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5177 lsa.lsa_stripe_off = mdts[0];
5179 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
5181 fprintf(stderr, "warning: '-m' is deprecated, "
5182 "use '--mode' or '-o' instead\n");
5188 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5189 progname, argv[0], argv[optind - 1]);
5194 if (optind == argc) {
5195 fprintf(stderr, "%s %s: DIR must be specified\n",
5200 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5201 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5203 "%s %s: stripe offset and count must be specified\n",
5209 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5210 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5212 "%s %s: cannot specify -d with -c or -i options\n",
5217 if (mode_opt != NULL) {
5218 mode = strtoul(mode_opt, &end, 8);
5221 "%s %s: bad MODE '%s'\n",
5222 progname, argv[0], mode_opt);
5225 previous_mode = umask(0);
5229 * initialize stripe parameters, in case param is converted to specific,
5230 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5232 param = calloc(1, offsetof(typeof(*param),
5233 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5234 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5235 if (param == NULL) {
5237 "%s %s: cannot allocate memory for parameters: %s\n",
5238 progname, argv[0], strerror(ENOMEM));
5242 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5243 param->lsp_stripe_count = lsa.lsa_stripe_count;
5244 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5245 param->lsp_stripe_offset = -1;
5247 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5248 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5249 param->lsp_stripe_pattern = lsa.lsa_pattern;
5251 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5252 param->lsp_pool = lsa.lsa_pool_name;
5253 param->lsp_is_specific = false;
5254 if (lsa.lsa_nr_tgts > 1) {
5255 if (lsa.lsa_stripe_count > 0 &&
5256 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5257 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5259 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
5260 argv[0], lsa.lsa_stripe_count,
5266 param->lsp_is_specific = true;
5267 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5268 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5271 dname = argv[optind];
5273 if (default_stripe) {
5274 result = llapi_dir_set_default_lmv(dname, param);
5276 /* if current \a dname isn't under the same \a mntdir
5277 * as the last one, and the last one was
5278 * auto-distributed, restore \a param.
5280 if (mntdir[0] != '\0' &&
5281 strncmp(dname, mntdir, strlen(mntdir)) &&
5283 param->lsp_is_specific = false;
5284 param->lsp_stripe_offset = -1;
5285 auto_distributed = false;
5288 if (!param->lsp_is_specific &&
5289 param->lsp_stripe_offset == -1) {
5290 char path[PATH_MAX] = "";
5293 lsb = malloc(sizeof(*lsb));
5301 /* use mntdir for dirname() temporarily */
5302 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5303 if (!realpath(dirname(mntdir), path)) {
5306 "error: invalid path '%s': %s\n",
5307 argv[optind], strerror(errno));
5312 result = llapi_search_mounts(path, 0, mntdir,
5314 if (result < 0 || mntdir[0] == '\0') {
5316 "No suitable Lustre mount found\n");
5320 result = mntdf(mntdir, NULL, NULL, 0,
5321 LL_STATFS_LMV, lsb);
5325 if (param->lsp_stripe_count > lsb->sb_count) {
5327 "error: stripe count %d is too big\n",
5328 param->lsp_stripe_count);
5333 qsort(lsb->sb_buf, lsb->sb_count,
5334 sizeof(struct ll_statfs_data),
5335 ll_statfs_data_comp);
5337 auto_distributed = true;
5340 if (auto_distributed) {
5342 int nr = MAX(param->lsp_stripe_count,
5345 /* don't use server whose usage is above 90% */
5346 while (nr != param->lsp_stripe_count &&
5347 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5349 nr = MAX(param->lsp_stripe_count,
5352 /* get \a r between [0, nr) */
5355 param->lsp_stripe_offset =
5356 lsb->sb_buf[r].sd_index;
5357 if (param->lsp_stripe_count > 1) {
5360 param->lsp_is_specific = true;
5361 for (; i < param->lsp_stripe_count; i++)
5362 param->lsp_tgts[(i + r) % nr] =
5363 lsb->sb_buf[i].sd_index;
5367 result = llapi_dir_create(dname, mode, param);
5372 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5373 progname, dname, strerror(-result));
5376 dname = argv[++optind];
5377 } while (dname != NULL);
5379 if (mode_opt != NULL)
5380 umask(previous_mode);
5388 static int lfs_rmentry(int argc, char **argv)
5395 fprintf(stderr, "error: %s: missing dirname\n",
5401 dname = argv[index];
5402 while (dname != NULL) {
5403 result = llapi_direntry_remove(dname);
5405 fprintf(stderr, "error: %s: remove dir entry '%s' "
5406 "failed\n", argv[0], dname);
5409 dname = argv[++index];
5414 static int lfs_mv(int argc, char **argv)
5416 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5417 struct find_param param = {
5424 struct option long_opts[] = {
5425 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5426 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5429 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5431 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5433 fprintf(stderr, "warning: '-M' deprecated"
5434 ", use '--mdt-index' or '-m' instead\n");
5437 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5439 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5445 param.fp_verbose = VERBOSE_DETAIL;
5448 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5449 progname, argv[optind - 1]);
5454 if (lmu.lum_stripe_offset == -1) {
5455 fprintf(stderr, "%s mv: MDT index must be specified\n",
5460 if (optind >= argc) {
5461 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5466 /* initialize migrate mdt parameters */
5467 param.fp_lmv_md = &lmu;
5468 param.fp_migrate = 1;
5469 rc = llapi_migrate_mdt(argv[optind], ¶m);
5471 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5472 progname, argv[optind], param.fp_mdt_index,
5477 static int lfs_osts(int argc, char **argv)
5479 return lfs_tgts(argc, argv);
5482 static int lfs_mdts(int argc, char **argv)
5484 return lfs_tgts(argc, argv);
5487 static int lfs_df(int argc, char **argv)
5489 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5490 enum mntdf_flags flags = MNTDF_SHOW;
5491 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5492 int c, rc = 0, index = 0;
5493 char fsname[PATH_MAX] = "", *pool_name = NULL;
5494 struct option long_opts[] = {
5495 { .val = 'h', .name = "human-readable",
5496 .has_arg = no_argument },
5497 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5498 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5499 { .val = 'p', .name = "pool", .has_arg = required_argument },
5500 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5503 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5506 flags |= MNTDF_COOKED;
5509 flags |= MNTDF_INODES;
5512 flags |= MNTDF_LAZY;
5518 flags |= MNTDF_VERBOSE;
5524 if (optind < argc && !realpath(argv[optind], path)) {
5526 fprintf(stderr, "error: invalid path '%s': %s\n",
5527 argv[optind], strerror(-rc));
5531 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5532 /* Check if we have a mount point */
5533 if (mntdir[0] == '\0')
5536 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5537 if (rc || path[0] != '\0')
5539 fsname[0] = '\0'; /* avoid matching in next loop */
5540 mntdir[0] = '\0'; /* avoid matching in next loop */
5546 static int lfs_getname(int argc, char **argv)
5548 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5549 int rc = 0, index = 0, c;
5550 char buf[sizeof(struct obd_uuid)];
5552 while ((c = getopt(argc, argv, "h")) != -1)
5555 if (optind == argc) { /* no paths specified, get all paths. */
5556 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5557 rc = llapi_getname(mntdir, buf, sizeof(buf));
5560 "cannot get name for `%s': %s\n",
5561 mntdir, strerror(-rc));
5565 printf("%s %s\n", buf, mntdir);
5567 path[0] = fsname[0] = mntdir[0] = 0;
5569 } else { /* paths specified, only attempt to search these. */
5570 for (; optind < argc; optind++) {
5571 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5574 "cannot get name for `%s': %s\n",
5575 argv[optind], strerror(-rc));
5579 printf("%s %s\n", buf, argv[optind]);
5585 static int lfs_check(int argc, char **argv)
5588 char mntdir[PATH_MAX] = {'\0'};
5595 fprintf(stderr, "%s check: server type must be specified\n",
5600 obd_types[0] = obd_type1;
5601 obd_types[1] = obd_type2;
5603 if (strcmp(argv[1], "osts") == 0) {
5604 strcpy(obd_types[0], "osc");
5605 } else if (strcmp(argv[1], "mds") == 0) {
5606 strcpy(obd_types[0], "mdc");
5607 } else if (strcmp(argv[1], "servers") == 0) {
5609 strcpy(obd_types[0], "osc");
5610 strcpy(obd_types[1], "mdc");
5612 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5617 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5618 if (rc < 0 || mntdir[0] == '\0') {
5620 "%s check: cannot find mounted Lustre filesystem: %s\n",
5621 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5625 rc = llapi_target_check(num_types, obd_types, mntdir);
5627 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5628 progname, argv[1], strerror(-rc));
5634 #ifdef HAVE_SYS_QUOTA_H
5635 #define ARG2INT(nr, str, msg) \
5638 nr = strtol(str, &endp, 0); \
5639 if (*endp != '\0') { \
5640 fprintf(stderr, "%s: bad %s '%s'\n", \
5641 progname, msg, str); \
5646 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5648 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5649 * returns the value or ULONG_MAX on integer overflow or incorrect format
5651 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5652 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5653 * 3. empty integer value is interpreted as 0
5655 static unsigned long str2sec(const char* timestr)
5657 const char spec[] = "smhdw";
5658 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5659 unsigned long val = 0;
5662 if (strpbrk(timestr, spec) == NULL) {
5663 /* no specifiers inside the time string,
5664 should treat it as an integer value */
5665 val = strtoul(timestr, &tail, 10);
5666 return *tail ? ULONG_MAX : val;
5669 /* format string is XXwXXdXXhXXmXXs */
5675 v = strtoul(timestr, &tail, 10);
5676 if (v == ULONG_MAX || *tail == '\0')
5677 /* value too large (ULONG_MAX or more)
5678 or missing specifier */
5681 ptr = strchr(spec, *tail);
5683 /* unknown specifier */
5688 /* check if product will overflow the type */
5689 if (!(v < ULONG_MAX / mult[ind]))
5692 ADD_OVERFLOW(val, mult[ind] * v);
5693 if (val == ULONG_MAX)
5705 #define ARG2ULL(nr, str, def_units) \
5707 unsigned long long limit, units = def_units; \
5710 rc = llapi_parse_size(str, &limit, &units, 1); \
5712 fprintf(stderr, "%s: invalid limit '%s'\n", \
5719 static inline int has_times_option(int argc, char **argv)
5723 for (i = 1; i < argc; i++)
5724 if (!strcmp(argv[i], "-t"))
5730 int lfs_setquota_times(int argc, char **argv)
5733 struct if_quotactl qctl;
5734 char *mnt, *obd_type = (char *)qctl.obd_type;
5735 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5736 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5737 struct option long_opts[] = {
5738 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5739 { .val = 'g', .name = "group", .has_arg = no_argument },
5740 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5741 { .val = 'p', .name = "projid", .has_arg = no_argument },
5742 { .val = 't', .name = "times", .has_arg = no_argument },
5743 { .val = 'u', .name = "user", .has_arg = no_argument },
5747 memset(&qctl, 0, sizeof(qctl));
5748 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5749 qctl.qc_type = ALLQUOTA;
5751 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5752 long_opts, NULL)) != -1) {
5763 if (qctl.qc_type != ALLQUOTA) {
5764 fprintf(stderr, "error: -u/g/p can't be used "
5765 "more than once\n");
5768 qctl.qc_type = qtype;
5771 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5772 fprintf(stderr, "error: bad block-grace: %s\n",
5776 dqb->dqb_valid |= QIF_BTIME;
5779 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5780 fprintf(stderr, "error: bad inode-grace: %s\n",
5784 dqb->dqb_valid |= QIF_ITIME;
5786 case 't': /* Yes, of course! */
5788 default: /* getopt prints error message for us when opterr != 0 */
5793 if (qctl.qc_type == ALLQUOTA) {
5794 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5798 if (optind != argc - 1) {
5799 fprintf(stderr, "error: unexpected parameters encountered\n");
5803 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5804 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5805 fprintf(stderr, "error: grace time is too large\n");
5810 rc = llapi_quotactl(mnt, &qctl);
5813 fprintf(stderr, "%s %s ", obd_type,
5814 obd_uuid2str(&qctl.obd_uuid));
5815 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5822 #define BSLIMIT (1 << 0)
5823 #define BHLIMIT (1 << 1)
5824 #define ISLIMIT (1 << 2)
5825 #define IHLIMIT (1 << 3)
5827 int lfs_setquota(int argc, char **argv)
5830 struct if_quotactl qctl;
5831 char *mnt, *obd_type = (char *)qctl.obd_type;
5832 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5833 struct option long_opts[] = {
5834 { .val = 'b', .name = "block-softlimit",
5835 .has_arg = required_argument },
5836 { .val = 'B', .name = "block-hardlimit",
5837 .has_arg = required_argument },
5838 { .val = 'd', .name = "default", .has_arg = no_argument },
5839 { .val = 'g', .name = "group", .has_arg = required_argument },
5840 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5841 { .val = 'i', .name = "inode-softlimit",
5842 .has_arg = required_argument },
5843 { .val = 'I', .name = "inode-hardlimit",
5844 .has_arg = required_argument },
5845 { .val = 'p', .name = "projid", .has_arg = required_argument },
5846 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5847 { .val = 'u', .name = "user", .has_arg = required_argument },
5848 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5850 unsigned limit_mask = 0;
5852 bool use_default = false;
5855 if (has_times_option(argc, argv))
5856 return lfs_setquota_times(argc, argv);
5858 memset(&qctl, 0, sizeof(qctl));
5859 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5860 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5861 * so it can be used as a marker that qc_type
5862 * isn't reinitialized from command line */
5864 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5865 long_opts, NULL)) != -1) {
5868 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5871 goto quota_type_def;
5874 rc = name2uid(&qctl.qc_id, optarg);
5877 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5880 goto quota_type_def;
5883 rc = name2gid(&qctl.qc_id, optarg);
5886 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5889 goto quota_type_def;
5892 rc = name2projid(&qctl.qc_id, optarg);
5895 qctl.qc_id = strtoul(optarg, &endptr, 10);
5896 if (*endptr != '\0') {
5897 fprintf(stderr, "%s setquota: invalid"
5898 " id '%s'\n", progname, optarg);
5903 if (qctl.qc_id == 0) {
5904 fprintf(stderr, "%s setquota: can't set quota"
5905 " for root usr/group/project.\n",
5911 if (qctl.qc_type != ALLQUOTA) {
5913 "%s setquota: only one of -u, -U, -g,"
5914 " -G, -p or -P may be specified\n",
5918 qctl.qc_type = qtype;
5921 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5925 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5926 dqb->dqb_bsoftlimit >>= 10;
5927 limit_mask |= BSLIMIT;
5928 if (dqb->dqb_bsoftlimit &&
5929 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5931 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5932 "See '%s help setquota' or Lustre manual for details\n",
5934 (unsigned long long)dqb->dqb_bsoftlimit,
5938 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5939 dqb->dqb_bhardlimit >>= 10;
5940 limit_mask |= BHLIMIT;
5941 if (dqb->dqb_bhardlimit &&
5942 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5944 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5945 "See '%s help setquota' or Lustre manual for details\n",
5947 (unsigned long long)dqb->dqb_bhardlimit,
5951 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5952 limit_mask |= ISLIMIT;
5953 if (dqb->dqb_isoftlimit &&
5954 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5956 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5957 "See '%s help setquota' or Lustre manual for details\n",
5959 (unsigned long long)dqb->dqb_isoftlimit,
5963 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5964 limit_mask |= IHLIMIT;
5965 if (dqb->dqb_ihardlimit &&
5966 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5968 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5969 "See '%s help setquota' or Lustre manual for details\n",
5971 (unsigned long long)dqb->dqb_ihardlimit,
5976 "%s setquota: unrecognized option '%s'\n",
5977 progname, argv[optind - 1]);
5982 if (qctl.qc_type == ALLQUOTA) {
5984 "%s setquota: either -u or -g must be specified\n",
5989 if (!use_default && limit_mask == 0) {
5991 "%s setquota: at least one limit must be specified\n",
5996 if (use_default && limit_mask != 0) {
5998 "%s setquota: limits should not be specified when"
5999 " using default quota\n",
6004 if (use_default && qctl.qc_id == 0) {
6006 "%s setquota: can not set default quota for root"
6007 " user/group/project\n",
6012 if (optind != argc - 1) {
6014 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6015 progname, argv[optind]);
6022 dqb->dqb_bhardlimit = 0;
6023 dqb->dqb_bsoftlimit = 0;
6024 dqb->dqb_ihardlimit = 0;
6025 dqb->dqb_isoftlimit = 0;
6028 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6029 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6030 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6031 /* sigh, we can't just set blimits/ilimits */
6032 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6033 .qc_type = qctl.qc_type,
6034 .qc_id = qctl.qc_id};
6036 rc = llapi_quotactl(mnt, &tmp_qctl);
6040 if (!(limit_mask & BHLIMIT))
6041 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6042 if (!(limit_mask & BSLIMIT))
6043 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6044 if (!(limit_mask & IHLIMIT))
6045 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6046 if (!(limit_mask & ISLIMIT))
6047 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6049 /* Keep grace times if we have got no softlimit arguments */
6050 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6051 dqb->dqb_valid |= QIF_BTIME;
6052 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6055 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6056 dqb->dqb_valid |= QIF_ITIME;
6057 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6061 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6062 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6064 rc = llapi_quotactl(mnt, &qctl);
6068 "%s setquota: cannot quotactl '%s' '%s': %s",
6070 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6077 /* Converts seconds value into format string
6078 * result is returned in buf
6080 * 1. result is in descenting order: 1w2d3h4m5s
6081 * 2. zero fields are not filled (except for p. 3): 5d1s
6082 * 3. zero seconds value is presented as "0s"
6084 static char * __sec2str(time_t seconds, char *buf)
6086 const char spec[] = "smhdw";
6087 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6092 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6093 c = seconds / mult[i];
6095 if (c > 0 || (i == 0 && buf == tail))
6096 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6104 static void sec2str(time_t seconds, char *buf, int rc)
6111 tail = __sec2str(seconds, tail);
6113 if (rc && tail - buf < 39) {
6119 static void diff2str(time_t seconds, char *buf, time_t now)
6125 if (seconds <= now) {
6126 strcpy(buf, "none");
6129 __sec2str(seconds - now, buf);
6132 static void print_quota_title(char *name, struct if_quotactl *qctl,
6133 bool human_readable, bool show_default)
6136 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6137 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6138 "Filesystem", "bquota", "blimit", "bgrace",
6139 "iquota", "ilimit", "igrace");
6141 printf("Disk quotas for %s %s (%cid %u):\n",
6142 qtype_name(qctl->qc_type), name,
6143 *qtype_name(qctl->qc_type), qctl->qc_id);
6144 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6145 "Filesystem", human_readable ? "used" : "kbytes",
6146 "quota", "limit", "grace",
6147 "files", "quota", "limit", "grace");
6151 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6154 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6157 snprintf(buf, buflen, "%5.4gP",
6158 (double)num / ((__u64)1 << 40));
6160 snprintf(buf, buflen, "%5.4gT",
6161 (double)num / (1 << 30));
6163 snprintf(buf, buflen, "%5.4gG",
6164 (double)num / (1 << 20));
6166 snprintf(buf, buflen, "%5.4gM",
6167 (double)num / (1 << 10));
6169 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6173 #define STRBUF_LEN 32
6174 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6175 int rc, bool h, bool show_default)
6181 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6182 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6183 int bover = 0, iover = 0;
6184 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6185 char numbuf[3][STRBUF_LEN];
6187 char strbuf[STRBUF_LEN];
6189 if (dqb->dqb_bhardlimit &&
6190 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6192 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6193 if (dqb->dqb_btime > now) {
6200 if (dqb->dqb_ihardlimit &&
6201 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6203 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6204 if (dqb->dqb_itime > now) {
6212 if (strlen(mnt) > 15)
6213 printf("%s\n%15s", mnt, "");
6215 printf("%15s", mnt);
6218 diff2str(dqb->dqb_btime, timebuf, now);
6219 else if (show_default)
6220 snprintf(timebuf, sizeof(timebuf), "%llu",
6221 (unsigned long long)dqb->dqb_btime);
6223 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6224 strbuf, sizeof(strbuf), h);
6225 if (rc == -EREMOTEIO)
6226 sprintf(numbuf[0], "%s*", strbuf);
6228 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6229 "%s" : "[%s]", strbuf);
6231 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6232 if (type == QC_GENERAL)
6233 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6234 "%s" : "[%s]", strbuf);
6236 sprintf(numbuf[1], "%s", "-");
6238 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6239 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6240 "%s" : "[%s]", strbuf);
6243 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6245 printf(" %7s%c %6s %7s %7s",
6246 numbuf[0], bover ? '*' : ' ', numbuf[1],
6247 numbuf[2], bover > 1 ? timebuf : "-");
6251 diff2str(dqb->dqb_itime, timebuf, now);
6252 else if (show_default)
6253 snprintf(timebuf, sizeof(timebuf), "%llu",
6254 (unsigned long long)dqb->dqb_itime);
6256 snprintf(numbuf[0], sizeof(numbuf),
6257 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6258 (uintmax_t)dqb->dqb_curinodes);
6260 if (type == QC_GENERAL)
6261 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6263 (uintmax_t)dqb->dqb_isoftlimit);
6265 sprintf(numbuf[1], "%s", "-");
6267 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6268 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6271 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6272 else if (type != QC_OSTIDX)
6273 printf(" %7s%c %6s %7s %7s",
6274 numbuf[0], iover ? '*' : ' ', numbuf[1],
6275 numbuf[2], iover > 1 ? timebuf : "-");
6277 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6280 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6281 qctl->qc_cmd == Q_GETOINFO) {
6285 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6286 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6287 printf("Block grace time: %s; Inode grace time: %s\n",
6288 bgtimebuf, igtimebuf);
6292 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6293 bool h, __u64 *total)
6295 int rc = 0, rc1 = 0, count = 0;
6296 __u32 valid = qctl->qc_valid;
6298 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6300 fprintf(stderr, "can not get %s count: %s\n",
6301 is_mdt ? "mdt": "ost", strerror(-rc));
6305 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6306 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6307 rc = llapi_quotactl(mnt, qctl);
6309 /* It is remote client case. */
6310 if (rc == -EOPNOTSUPP) {
6317 fprintf(stderr, "quotactl %s%d failed.\n",
6318 is_mdt ? "mdt": "ost", qctl->qc_idx);
6322 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6323 qctl->qc_valid, 0, h, false);
6324 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6325 qctl->qc_dqblk.dqb_bhardlimit;
6328 qctl->qc_valid = valid;
6332 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6333 int verbose, int quiet, bool human_readable,
6336 int rc1 = 0, rc2 = 0, rc3 = 0;
6337 char *obd_type = (char *)qctl->obd_type;
6338 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6339 __u64 total_ialloc = 0, total_balloc = 0;
6340 bool use_default_for_blk = false;
6341 bool use_default_for_file = false;
6344 rc1 = llapi_quotactl(mnt, qctl);
6348 fprintf(stderr, "%s quotas are not enabled.\n",
6349 qtype_name(qctl->qc_type));
6352 fprintf(stderr, "Permission denied.\n");
6355 /* We already got error message. */
6358 fprintf(stderr, "Unexpected quotactl error: %s\n",
6363 if (!show_default && qctl->qc_id == 0) {
6364 qctl->qc_dqblk.dqb_bhardlimit = 0;
6365 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6366 qctl->qc_dqblk.dqb_ihardlimit = 0;
6367 qctl->qc_dqblk.dqb_isoftlimit = 0;
6368 qctl->qc_dqblk.dqb_btime = 0;
6369 qctl->qc_dqblk.dqb_itime = 0;
6370 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6373 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6374 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6375 use_default_for_blk = true;
6376 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6379 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6380 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6381 use_default_for_file = true;
6382 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6385 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6386 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6387 print_quota_title(name, qctl, human_readable, show_default);
6389 if (rc1 && *obd_type)
6390 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6392 if (qctl->qc_valid != QC_GENERAL)
6395 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6396 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6397 (QIF_LIMITS|QIF_USAGE));
6399 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6401 if (!show_default && verbose &&
6402 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6403 char strbuf[STRBUF_LEN];
6405 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6407 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6409 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6411 printf("Total allocated inode limit: %ju, total "
6412 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6416 if (use_default_for_blk)
6417 printf("%cid %u is using default block quota setting\n",
6418 *qtype_name(qctl->qc_type), qctl->qc_id);
6420 if (use_default_for_file)
6421 printf("%cid %u is using default file quota setting\n",
6422 *qtype_name(qctl->qc_type), qctl->qc_id);
6424 if (rc1 || rc2 || rc3 || inacc)
6425 printf("Some errors happened when getting quota info. "
6426 "Some devices may be not working or deactivated. "
6427 "The data in \"[]\" is inaccurate.\n");
6433 static int lfs_project(int argc, char **argv)
6435 int ret = 0, err = 0, c, i;
6436 struct project_handle_control phc = { 0 };
6437 enum lfs_project_ops_t op;
6440 phc.assign_projid = false;
6441 /* default action */
6442 op = LFS_PROJECT_LIST;
6444 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6447 if (op != LFS_PROJECT_LIST) {
6449 "%s: cannot specify '-c' '-C' '-s' together\n",
6454 op = LFS_PROJECT_CHECK;
6457 if (op != LFS_PROJECT_LIST) {
6459 "%s: cannot specify '-c' '-C' '-s' together\n",
6464 op = LFS_PROJECT_CLEAR;
6467 if (op != LFS_PROJECT_LIST) {
6469 "%s: cannot specify '-c' '-C' '-s' together\n",
6474 phc.set_inherit = true;
6475 op = LFS_PROJECT_SET;
6481 phc.keep_projid = true;
6484 phc.recursive = true;
6487 phc.projid = strtoul(optarg, NULL, 0);
6488 phc.assign_projid = true;
6492 phc.newline = false;
6495 fprintf(stderr, "%s: invalid option '%c'\n",
6501 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6502 op = LFS_PROJECT_SET;
6503 phc.set_projid = true;
6504 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6505 phc.set_projid = true;
6509 case LFS_PROJECT_CHECK:
6510 if (phc.keep_projid) {
6512 "%s: '-k' is useless together with '-c'\n",
6517 case LFS_PROJECT_CLEAR:
6520 "%s: '-0' is useless together with '-C'\n",
6524 if (phc.assign_projid) {
6526 "%s: '-p' is useless together with '-C'\n",
6531 case LFS_PROJECT_SET:
6534 "%s: '-0' is useless together with '-s'\n",
6538 if (phc.keep_projid) {
6540 "%s: '-k' is useless together with '-s'\n",
6548 "%s: '-0' is useless for list operations\n",
6558 fprintf(stderr, "%s: missing file or directory target(s)\n",
6563 for (i = 0; i < argc; i++) {
6565 case LFS_PROJECT_CHECK:
6566 err = lfs_project_check(argv[i], &phc);
6568 case LFS_PROJECT_LIST:
6569 err = lfs_project_list(argv[i], &phc);
6571 case LFS_PROJECT_CLEAR:
6572 err = lfs_project_clear(argv[i], &phc);
6574 case LFS_PROJECT_SET:
6575 err = lfs_project_set(argv[i], &phc);
6587 static int lfs_quota(int argc, char **argv)
6590 char *mnt, *name = NULL;
6591 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6592 .qc_type = ALLQUOTA };
6593 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6594 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6596 __u32 valid = QC_GENERAL, idx = 0;
6597 bool human_readable = false;
6598 bool show_default = false;
6601 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6604 show_default = true;
6609 show_default = true;
6614 show_default = true;
6618 if (qctl.qc_type != ALLQUOTA) {
6620 "%s quota: only one of -u, -g, or -p may be specified\n",
6624 qctl.qc_type = qtype;
6627 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6630 valid = qctl.qc_valid = QC_UUID;
6631 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6634 valid = qctl.qc_valid = QC_MDTIDX;
6635 idx = qctl.qc_idx = atoi(optarg);
6636 if (idx == 0 && *optarg != '0') {
6638 "%s quota: invalid MDT index '%s'\n",
6644 valid = qctl.qc_valid = QC_OSTIDX;
6645 idx = qctl.qc_idx = atoi(optarg);
6646 if (idx == 0 && *optarg != '0') {
6648 "%s quota: invalid OST index '%s'\n",
6660 human_readable = true;
6663 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6664 progname, argv[optind - 1]);
6669 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6670 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6671 optind == argc - 1 && !show_default) {
6673 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6674 qctl.qc_valid = valid;
6677 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6678 qctl.qc_type = qtype;
6679 if (qtype == USRQUOTA) {
6680 qctl.qc_id = geteuid();
6681 rc = uid2name(&name, qctl.qc_id);
6683 qctl.qc_id = getegid();
6684 rc = gid2name(&name, qctl.qc_id);
6689 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6690 human_readable, show_default);
6695 /* lfs quota -u username /path/to/lustre/mount */
6696 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6697 /* options should be followed by u/g-name and mntpoint */
6698 if ((!show_default && optind + 2 != argc) ||
6699 (show_default && optind + 1 != argc) ||
6700 qctl.qc_type == ALLQUOTA) {
6702 "%s quota: name and mount point must be specified\n",
6707 if (!show_default) {
6708 name = argv[optind++];
6709 switch (qctl.qc_type) {
6711 rc = name2uid(&qctl.qc_id, name);
6714 rc = name2gid(&qctl.qc_id, name);
6717 rc = name2projid(&qctl.qc_id, name);
6724 qctl.qc_valid = QC_GENERAL;
6725 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6730 qctl.qc_id = strtoul(name, &endptr, 10);
6731 if (*endptr != '\0') {
6732 fprintf(stderr, "%s quota: invalid id '%s'\n",
6737 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6738 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6744 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6745 human_readable, show_default);
6748 #endif /* HAVE_SYS_QUOTA_H! */
6750 static int flushctx_ioctl(char *mp)
6754 fd = open(mp, O_RDONLY);
6756 fprintf(stderr, "flushctx: error open %s: %s\n",
6757 mp, strerror(errno));
6761 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6763 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6764 mp, strerror(errno));
6770 static int lfs_flushctx(int argc, char **argv)
6772 int kdestroy = 0, c;
6773 char mntdir[PATH_MAX] = {'\0'};
6777 while ((c = getopt(argc, argv, "k")) != -1) {
6783 fprintf(stderr, "error: %s: option '-%c' "
6784 "unrecognized\n", argv[0], c);
6790 if ((rc = system("kdestroy > /dev/null")) != 0) {
6791 rc = WEXITSTATUS(rc);
6792 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6796 if (optind >= argc) {
6797 /* flush for all mounted lustre fs. */
6798 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6799 /* Check if we have a mount point */
6800 if (mntdir[0] == '\0')
6803 if (flushctx_ioctl(mntdir))
6806 mntdir[0] = '\0'; /* avoid matching in next loop */
6809 /* flush fs as specified */
6810 while (optind < argc) {
6811 if (flushctx_ioctl(argv[optind++]))
6818 static int lfs_changelog(int argc, char **argv)
6820 void *changelog_priv;
6821 struct changelog_rec *rec;
6822 long long startrec = 0, endrec = 0;
6824 struct option long_opts[] = {
6825 { .val = 'f', .name = "follow", .has_arg = no_argument },
6827 char short_opts[] = "f";
6830 while ((rc = getopt_long(argc, argv, short_opts,
6831 long_opts, NULL)) != -1) {
6838 "%s changelog: unrecognized option '%s'\n",
6839 progname, argv[optind - 1]);
6843 if (optind >= argc) {
6844 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6849 mdd = argv[optind++];
6851 startrec = strtoll(argv[optind++], NULL, 10);
6853 endrec = strtoll(argv[optind++], NULL, 10);
6855 rc = llapi_changelog_start(&changelog_priv,
6856 CHANGELOG_FLAG_BLOCK |
6857 CHANGELOG_FLAG_JOBID |
6858 CHANGELOG_FLAG_EXTRA_FLAGS |
6859 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6862 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6863 progname, strerror(errno = -rc));
6867 rc = llapi_changelog_set_xflags(changelog_priv,
6868 CHANGELOG_EXTRA_FLAG_UIDGID |
6869 CHANGELOG_EXTRA_FLAG_NID |
6870 CHANGELOG_EXTRA_FLAG_OMODE |
6871 CHANGELOG_EXTRA_FLAG_XATTR);
6874 "%s changelog: cannot set xflags for changelog: %s\n",
6875 progname, strerror(errno = -rc));
6879 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6883 if (endrec && rec->cr_index > endrec) {
6884 llapi_changelog_free(&rec);
6887 if (rec->cr_index < startrec) {
6888 llapi_changelog_free(&rec);
6892 secs = rec->cr_time >> 30;
6893 gmtime_r(&secs, &ts);
6894 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6895 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6896 changelog_type2str(rec->cr_type),
6897 ts.tm_hour, ts.tm_min, ts.tm_sec,
6898 (int)(rec->cr_time & ((1 << 30) - 1)),
6899 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6900 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6902 if (rec->cr_flags & CLF_JOBID) {
6903 struct changelog_ext_jobid *jid =
6904 changelog_rec_jobid(rec);
6906 if (jid->cr_jobid[0] != '\0')
6907 printf(" j=%s", jid->cr_jobid);
6910 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6911 struct changelog_ext_extra_flags *ef =
6912 changelog_rec_extra_flags(rec);
6914 printf(" ef=0x%llx",
6915 (unsigned long long)ef->cr_extra_flags);
6917 if (ef->cr_extra_flags & CLFE_UIDGID) {
6918 struct changelog_ext_uidgid *uidgid =
6919 changelog_rec_uidgid(rec);
6921 printf(" u=%llu:%llu",
6922 (unsigned long long)uidgid->cr_uid,
6923 (unsigned long long)uidgid->cr_gid);
6925 if (ef->cr_extra_flags & CLFE_NID) {
6926 struct changelog_ext_nid *nid =
6927 changelog_rec_nid(rec);
6930 libcfs_nid2str(nid->cr_nid));
6933 if (ef->cr_extra_flags & CLFE_OPEN) {
6934 struct changelog_ext_openmode *omd =
6935 changelog_rec_openmode(rec);
6936 char mode[] = "---";
6938 /* exec mode must be exclusive */
6939 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6942 if (omd->cr_openflags & MDS_FMODE_READ)
6944 if (omd->cr_openflags &
6951 if (strcmp(mode, "---") != 0)
6952 printf(" m=%s", mode);
6956 if (ef->cr_extra_flags & CLFE_XATTR) {
6957 struct changelog_ext_xattr *xattr =
6958 changelog_rec_xattr(rec);
6960 if (xattr->cr_xattr[0] != '\0')
6961 printf(" x=%s", xattr->cr_xattr);
6965 if (rec->cr_namelen)
6966 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6967 rec->cr_namelen, changelog_rec_name(rec));
6969 if (rec->cr_flags & CLF_RENAME) {
6970 struct changelog_ext_rename *rnm =
6971 changelog_rec_rename(rec);
6973 if (!fid_is_zero(&rnm->cr_sfid))
6974 printf(" s="DFID" sp="DFID" %.*s",
6975 PFID(&rnm->cr_sfid),
6976 PFID(&rnm->cr_spfid),
6977 (int)changelog_rec_snamelen(rec),
6978 changelog_rec_sname(rec));
6982 llapi_changelog_free(&rec);
6985 llapi_changelog_fini(&changelog_priv);
6988 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6989 progname, strerror(errno = -rc));
6991 return (rc == 1 ? 0 : rc);
6994 static int lfs_changelog_clear(int argc, char **argv)
7002 endrec = strtoll(argv[3], NULL, 10);
7004 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7007 fprintf(stderr, "%s: record out of range: %llu\n",
7009 else if (rc == -ENOENT)
7010 fprintf(stderr, "%s: no changelog user: %s\n",
7013 fprintf(stderr, "%s error: %s\n", argv[0],
7022 static int lfs_fid2path(int argc, char **argv)
7024 struct option long_opts[] = {
7025 { .val = 'c', .name = "cur", .has_arg = no_argument },
7026 { .val = 'l', .name = "link", .has_arg = required_argument },
7027 { .val = 'r', .name = "rec", .has_arg = required_argument },
7029 char short_opts[] = "cl:r:";
7030 char *device, *fid, *path;
7031 long long recno = -1;
7036 char *endptr = NULL;
7038 while ((rc = getopt_long(argc, argv, short_opts,
7039 long_opts, NULL)) != -1) {
7045 linkno = strtol(optarg, &endptr, 10);
7046 if (*endptr != '\0') {
7048 "%s fid2path: invalid linkno '%s'\n",
7054 recno = strtoll(optarg, &endptr, 10);
7055 if (*endptr != '\0') {
7057 "%s fid2path: invalid recno '%s'\n",
7064 "%s fid2path: unrecognized option '%s'\n",
7065 progname, argv[optind - 1]);
7072 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7077 device = argv[optind++];
7078 path = calloc(1, PATH_MAX);
7082 "%s fid2path: cannot allocate memory for path: %s\n",
7083 progname, strerror(-rc));
7088 while (optind < argc) {
7089 fid = argv[optind++];
7091 lnktmp = (linkno >= 0) ? linkno : 0;
7093 int oldtmp = lnktmp;
7094 long long rectmp = recno;
7096 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7100 "%s fid2path: cannot find '%s': %s\n",
7101 progname, fid, strerror(errno = -rc2));
7108 fprintf(stdout, "%lld ", rectmp);
7109 if (device[0] == '/') {
7110 fprintf(stdout, "%s", device);
7111 if (device[strlen(device) - 1] != '/')
7112 fprintf(stdout, "/");
7113 } else if (path[0] == '\0') {
7114 fprintf(stdout, "/");
7116 fprintf(stdout, "%s\n", path);
7119 /* specified linkno */
7121 if (oldtmp == lnktmp)
7131 static int lfs_path2fid(int argc, char **argv)
7133 struct option long_opts[] = {
7134 { .val = 'p', .name = "parents", .has_arg = no_argument },
7137 const char short_opts[] = "p";
7138 const char *sep = "";
7141 bool show_parents = false;
7143 while ((rc = getopt_long(argc, argv, short_opts,
7144 long_opts, NULL)) != -1) {
7147 show_parents = true;
7151 "%s path2fid: unrecognized option '%s'\n",
7152 progname, argv[optind - 1]);
7157 if (optind > argc - 1) {
7158 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7162 else if (optind < argc - 1)
7166 for (path = argv + optind; *path != NULL; path++) {
7168 if (!show_parents) {
7169 err = llapi_path2fid(*path, &fid);
7171 printf("%s%s"DFID"\n",
7172 *sep != '\0' ? *path : "", sep,
7175 char name[NAME_MAX + 1];
7176 unsigned int linkno = 0;
7178 while ((err = llapi_path2parent(*path, linkno, &fid,
7179 name, sizeof(name))) == 0) {
7180 if (*sep != '\0' && linkno == 0)
7181 printf("%s%s", *path, sep);
7183 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7188 /* err == -ENODATA is end-of-loop */
7189 if (linkno > 0 && err == -ENODATA) {
7197 "%s path2fid: cannot get %sfid for '%s': %s\n",
7198 progname, show_parents ? "parent " : "", *path,
7210 #define MAX_ERRNO 4095
7211 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
7213 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
7217 rc = llapi_rmfid(device, fa);
7219 fprintf(stderr, "rmfid(): rc = %d\n", rc);
7223 for (k = 0; k < fa->fa_nr; k++) {
7224 rc = (__s32)fa->fa_fids[k].f_ver;
7225 if (!IS_ERR_VALUE(rc))
7231 fa->fa_fids[k].f_ver = 0;
7232 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
7233 PFID(&fa->fa_fids[k]), rc);
7239 static int lfs_rmfid(int argc, char **argv)
7241 char *fidstr, *device;
7242 int rc = 0, rc2, nr;
7243 struct fid_array *fa;
7245 if (optind > argc - 1) {
7246 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
7250 device = argv[optind++];
7253 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
7259 while (optind < argc) {
7262 fidstr = argv[optind++];
7263 while (*fidstr == '[')
7265 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
7267 fprintf(stderr, "unrecognized FID: %s\n",
7272 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
7273 /* start another batch */
7274 rc2 = lfs_rmfid_and_show_errors(device, fa);
7281 rc2 = lfs_rmfid_and_show_errors(device, fa);
7289 static int lfs_data_version(int argc, char **argv)
7296 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7299 fprintf(stderr, "%s data_version: FILE must be specified\n",
7304 while ((c = getopt(argc, argv, "nrw")) != -1) {
7307 data_version_flags = 0;
7310 data_version_flags |= LL_DV_RD_FLUSH;
7313 data_version_flags |= LL_DV_WR_FLUSH;
7317 "%s data_version: unrecognized option '%s'\n",
7318 progname, argv[optind - 1]);
7322 if (optind == argc) {
7323 fprintf(stderr, "%s data_version: FILE must be specified\n",
7328 path = argv[optind];
7329 fd = open(path, O_RDONLY);
7332 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7333 progname, path, strerror(-rc));
7337 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7340 "%s data_version: cannot get version for '%s': %s\n",
7341 progname, path, strerror(-rc));
7343 printf("%ju" "\n", (uintmax_t)data_version);
7349 static int lfs_hsm_state(int argc, char **argv)
7354 struct hsm_user_state hus;
7362 rc = llapi_hsm_state_get(path, &hus);
7364 fprintf(stderr, "can't get hsm state for %s: %s\n",
7365 path, strerror(errno = -rc));
7369 /* Display path name and status flags */
7370 printf("%s: (0x%08x)", path, hus.hus_states);
7372 if (hus.hus_states & HS_RELEASED)
7373 printf(" released");
7374 if (hus.hus_states & HS_EXISTS)
7376 if (hus.hus_states & HS_DIRTY)
7378 if (hus.hus_states & HS_ARCHIVED)
7379 printf(" archived");
7380 /* Display user-settable flags */
7381 if (hus.hus_states & HS_NORELEASE)
7382 printf(" never_release");
7383 if (hus.hus_states & HS_NOARCHIVE)
7384 printf(" never_archive");
7385 if (hus.hus_states & HS_LOST)
7386 printf(" lost_from_hsm");
7388 if (hus.hus_archive_id != 0)
7389 printf(", archive_id:%d", hus.hus_archive_id);
7392 } while (++i < argc);
7397 #define LFS_HSM_SET 0
7398 #define LFS_HSM_CLEAR 1
7401 * Generic function to set or clear HSM flags.
7402 * Used by hsm_set and hsm_clear.
7404 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7406 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7408 struct option long_opts[] = {
7409 { .val = 'A', .name = "archived", .has_arg = no_argument },
7410 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7411 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7412 { .val = 'e', .name = "exists", .has_arg = no_argument },
7413 { .val = 'l', .name = "lost", .has_arg = no_argument },
7414 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7415 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7417 char short_opts[] = "lraAdei:";
7421 __u32 archive_id = 0;
7427 while ((c = getopt_long(argc, argv, short_opts,
7428 long_opts, NULL)) != -1) {
7434 mask |= HS_NOARCHIVE;
7437 mask |= HS_ARCHIVED;
7440 mask |= HS_NORELEASE;
7449 archive_id = strtol(optarg, &end, 10);
7451 fprintf(stderr, "invalid archive_id: '%s'\n",
7459 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7460 argv[0], argv[optind - 1]);
7465 /* User should have specified a flag */
7469 while (optind < argc) {
7471 path = argv[optind];
7473 /* If mode == 0, this means we apply the mask. */
7474 if (mode == LFS_HSM_SET)
7475 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7477 rc = llapi_hsm_state_set(path, 0, mask, 0);
7480 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7481 path, strerror(errno = -rc));
7490 static int lfs_hsm_action(int argc, char **argv)
7495 struct hsm_current_action hca;
7496 struct hsm_extent he;
7497 enum hsm_user_action hua;
7498 enum hsm_progress_states hps;
7506 rc = llapi_hsm_current_action(path, &hca);
7508 fprintf(stderr, "can't get hsm action for %s: %s\n",
7509 path, strerror(errno = -rc));
7512 he = hca.hca_location;
7513 hua = hca.hca_action;
7514 hps = hca.hca_state;
7516 printf("%s: %s", path, hsm_user_action2name(hua));
7518 /* Skip file without action */
7519 if (hca.hca_action == HUA_NONE) {
7524 printf(" %s ", hsm_progress_state2name(hps));
7526 if ((hps == HPS_RUNNING) &&
7527 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7528 printf("(%llu bytes moved)\n",
7529 (unsigned long long)he.length);
7530 else if ((he.offset + he.length) == LUSTRE_EOF)
7531 printf("(from %llu to EOF)\n",
7532 (unsigned long long)he.offset);
7534 printf("(from %llu to %llu)\n",
7535 (unsigned long long)he.offset,
7536 (unsigned long long)(he.offset + he.length));
7538 } while (++i < argc);
7543 static int lfs_hsm_set(int argc, char **argv)
7545 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7548 static int lfs_hsm_clear(int argc, char **argv)
7550 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7554 * Check file state and return its fid, to be used by lfs_hsm_request().
7556 * \param[in] file Path to file to check
7557 * \param[in,out] fid Pointer to allocated lu_fid struct.
7558 * \param[in,out] last_dev Pointer to last device id used.
7560 * \return 0 on success.
7562 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7568 rc = lstat(file, &st);
7570 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7573 /* Checking for regular file as archiving as posix copytool
7574 * rejects archiving files other than regular files
7576 if (!S_ISREG(st.st_mode)) {
7577 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7580 /* A request should be ... */
7581 if (*last_dev != st.st_dev && *last_dev != 0) {
7582 fprintf(stderr, "All files should be "
7583 "on the same filesystem: %s\n", file);
7586 *last_dev = st.st_dev;
7588 rc = llapi_path2fid(file, fid);
7590 fprintf(stderr, "Cannot read FID of %s: %s\n",
7591 file, strerror(-rc));
7597 /* Fill an HSM HUR item with a given file name.
7599 * If mntpath is set, then the filename is actually a FID, and no
7600 * lookup on the filesystem will be performed.
7602 * \param[in] hur the user request to fill
7603 * \param[in] idx index of the item inside the HUR to fill
7604 * \param[in] mntpath mountpoint of Lustre
7605 * \param[in] fname filename (if mtnpath is NULL)
7606 * or FID (if mntpath is set)
7607 * \param[in] last_dev pointer to last device id used
7609 * \retval 0 on success
7610 * \retval CMD_HELP or a negative errno on error
7612 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7613 const char *mntpath, const char *fname,
7616 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7619 hui->hui_extent.length = -1;
7621 if (mntpath != NULL) {
7624 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7628 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7633 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7637 hur->hur_request.hr_itemcount++;
7642 static int lfs_hsm_request(int argc, char **argv, int action)
7644 struct option long_opts[] = {
7645 { .val = 'a', .name = "archive", .has_arg = required_argument },
7646 { .val = 'D', .name = "data", .has_arg = required_argument },
7647 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7648 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7651 char short_opts[] = "l:D:a:m:";
7652 struct hsm_user_request *hur, *oldhur;
7657 char *filelist = NULL;
7658 char fullpath[PATH_MAX];
7659 char *opaque = NULL;
7663 int nbfile_alloc = 0;
7664 char *some_file = NULL;
7665 char *mntpath = NULL;
7671 while ((c = getopt_long(argc, argv, short_opts,
7672 long_opts, NULL)) != -1) {
7681 if (action != HUA_ARCHIVE &&
7682 action != HUA_REMOVE) {
7684 "error: -a is supported only "
7685 "when archiving or removing\n");
7688 archive_id = atoi(optarg);
7691 if (some_file == NULL) {
7693 some_file = strdup(optarg);
7699 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7700 argv[0], argv[optind - 1]);
7705 /* All remaining args are files, so we have at least nbfile */
7706 nbfile = argc - optind;
7708 if ((nbfile == 0) && (filelist == NULL))
7712 opaque_len = strlen(opaque);
7714 /* Alloc the request structure with enough place to store all files
7715 * from command line. */
7716 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7718 fprintf(stderr, "Cannot create the request: %s\n",
7722 nbfile_alloc = nbfile;
7724 hur->hur_request.hr_action = action;
7725 hur->hur_request.hr_archive_id = archive_id;
7726 hur->hur_request.hr_flags = 0;
7728 /* All remaining args are files, add them */
7729 if (nbfile != 0 && some_file == NULL)
7730 some_file = strdup(argv[optind]);
7732 for (i = 0; i < nbfile; i++) {
7733 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7739 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7741 /* If a filelist was specified, read the filelist from it. */
7742 if (filelist != NULL) {
7743 fp = fopen(filelist, "r");
7745 fprintf(stderr, "Cannot read the file list %s: %s\n",
7746 filelist, strerror(errno));
7751 while ((rc = getline(&line, &len, fp)) != -1) {
7752 /* If allocated buffer was too small, get something
7754 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7757 nbfile_alloc = nbfile_alloc * 2 + 1;
7759 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7762 fprintf(stderr, "hsm: cannot allocate "
7763 "the request: %s\n",
7770 size = hur_len(oldhur);
7772 fprintf(stderr, "hsm: cannot allocate "
7773 "%u files + %u bytes data\n",
7774 oldhur->hur_request.hr_itemcount,
7775 oldhur->hur_request.hr_data_len);
7782 memcpy(hur, oldhur, size);
7787 if (line[strlen(line) - 1] == '\n')
7788 line[strlen(line) - 1] = '\0';
7790 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7791 mntpath, line, &last_dev);
7797 if (some_file == NULL) {
7807 /* If a --data was used, add it to the request */
7808 hur->hur_request.hr_data_len = opaque_len;
7810 memcpy(hur_data(hur), opaque, opaque_len);
7812 /* Send the HSM request */
7813 if (realpath(some_file, fullpath) == NULL) {
7814 fprintf(stderr, "Could not find path '%s': %s\n",
7815 some_file, strerror(errno));
7817 rc = llapi_hsm_request(fullpath, hur);
7819 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7820 some_file, strerror(-rc));
7830 static int lfs_hsm_archive(int argc, char **argv)
7832 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7835 static int lfs_hsm_restore(int argc, char **argv)
7837 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7840 static int lfs_hsm_release(int argc, char **argv)
7842 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7845 static int lfs_hsm_remove(int argc, char **argv)
7847 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7850 static int lfs_hsm_cancel(int argc, char **argv)
7852 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7855 static int lfs_swap_layouts(int argc, char **argv)
7860 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7861 SWAP_LAYOUTS_KEEP_MTIME |
7862 SWAP_LAYOUTS_KEEP_ATIME);
7865 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7867 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7869 int lfs_get_mode(const char *string)
7871 enum lock_mode_user mode;
7873 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7874 if (lock_mode_names[mode] == NULL)
7876 if (strcmp(string, lock_mode_names[mode]) == 0)
7883 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7885 enum lu_ladvise_type advice;
7888 advice < ARRAY_SIZE(ladvise_names); advice++) {
7889 if (ladvise_names[advice] == NULL)
7891 if (strcmp(string, ladvise_names[advice]) == 0)
7895 return LU_LADVISE_INVALID;
7898 static int lfs_ladvise(int argc, char **argv)
7900 struct option long_opts[] = {
7901 { .val = 'a', .name = "advice", .has_arg = required_argument },
7902 { .val = 'b', .name = "background", .has_arg = no_argument },
7903 { .val = 'e', .name = "end", .has_arg = required_argument },
7904 { .val = 'l', .name = "length", .has_arg = required_argument },
7905 { .val = 'm', .name = "mode", .has_arg = required_argument },
7906 { .val = 's', .name = "start", .has_arg = required_argument },
7907 { .val = 'u', .name = "unset", .has_arg = no_argument },
7909 char short_opts[] = "a:be:l:m:s:u";
7914 struct llapi_lu_ladvise advice;
7915 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7916 unsigned long long start = 0;
7917 unsigned long long end = LUSTRE_EOF;
7918 unsigned long long length = 0;
7919 unsigned long long size_units;
7920 unsigned long long flags = 0;
7924 while ((c = getopt_long(argc, argv, short_opts,
7925 long_opts, NULL)) != -1) {
7928 advice_type = lfs_get_ladvice(optarg);
7929 if (advice_type == LU_LADVISE_INVALID) {
7930 fprintf(stderr, "%s: invalid advice type "
7931 "'%s'\n", argv[0], optarg);
7932 fprintf(stderr, "Valid types:");
7934 for (advice_type = 0;
7935 advice_type < ARRAY_SIZE(ladvise_names);
7937 if (ladvise_names[advice_type] == NULL)
7939 fprintf(stderr, " %s",
7940 ladvise_names[advice_type]);
7942 fprintf(stderr, "\n");
7955 rc = llapi_parse_size(optarg, &end,
7958 fprintf(stderr, "%s: bad end offset '%s'\n",
7965 rc = llapi_parse_size(optarg, &start,
7968 fprintf(stderr, "%s: bad start offset "
7969 "'%s'\n", argv[0], optarg);
7975 rc = llapi_parse_size(optarg, &length,
7978 fprintf(stderr, "%s: bad length '%s'\n",
7984 mode = lfs_get_mode(optarg);
7986 fprintf(stderr, "%s: bad mode '%s', valid "
7987 "modes are READ or WRITE\n",
7995 fprintf(stderr, "%s: option '%s' unrecognized\n",
7996 argv[0], argv[optind - 1]);
8001 if (advice_type == LU_LADVISE_INVALID) {
8002 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8003 fprintf(stderr, "Valid types:");
8004 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8006 if (ladvise_names[advice_type] == NULL)
8008 fprintf(stderr, " %s", ladvise_names[advice_type]);
8010 fprintf(stderr, "\n");
8014 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8015 fprintf(stderr, "%s: Lock no expand advice is a per file "
8016 "descriptor advice, so when called from lfs, "
8017 "it does nothing.\n", argv[0]);
8021 if (argc <= optind) {
8022 fprintf(stderr, "%s: please give one or more file names\n",
8027 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8028 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8033 if (end == LUSTRE_EOF && length != 0)
8034 end = start + length;
8037 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8038 argv[0], start, end);
8042 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8043 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8048 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8049 fprintf(stderr, "%s: mode is required with lockahead\n",
8054 while (optind < argc) {
8057 path = argv[optind++];
8059 fd = open(path, O_RDONLY);
8061 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8062 argv[0], path, strerror(errno));
8067 advice.lla_start = start;
8068 advice.lla_end = end;
8069 advice.lla_advice = advice_type;
8070 advice.lla_value1 = 0;
8071 advice.lla_value2 = 0;
8072 advice.lla_value3 = 0;
8073 advice.lla_value4 = 0;
8074 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8075 advice.lla_lockahead_mode = mode;
8076 advice.lla_peradvice_flags = flags;
8079 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8082 fprintf(stderr, "%s: cannot give advice '%s' to file "
8083 "'%s': %s\n", argv[0],
8084 ladvise_names[advice_type],
8085 path, strerror(errno));
8091 if (rc == 0 && rc2 < 0)
8097 /** The input string contains a comma delimited list of component ids and
8098 * ranges, for example "1,2-4,7".
8100 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8102 bool end_of_loop = false;
8110 while (!end_of_loop) {
8114 char *endptr = NULL;
8117 ptr = strchrnul(arg, ',');
8118 end_of_loop = *ptr == '\0';
8121 start_index = strtol(arg, &endptr, 0);
8122 if (endptr == arg) /* no data at all */
8124 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8126 if (start_index < 0)
8129 end_index = start_index;
8130 if (*endptr == '-') {
8131 end_index = strtol(endptr + 1, &endptr, 0);
8132 if (*endptr != '\0')
8134 if (end_index < start_index)
8138 for (i = start_index; i <= end_index && size > 0; i++) {
8141 /* remove duplicate */
8142 for (j = 0; j < nr; j++) {
8146 if (j == nr) { /* no duplicate */
8152 if (size == 0 && i < end_index)
8159 if (!end_of_loop && ptr != NULL)
8162 return rc < 0 ? rc : nr;
8166 * struct verify_mirror_id - Mirror id to be verified.
8167 * @mirror_id: A specified mirror id.
8168 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8170 struct verify_mirror_id {
8176 * compare_mirror_ids() - Compare mirror ids.
8177 * @layout: Mirror component list.
8178 * @cbdata: Callback data in verify_mirror_id structure.
8180 * This is a callback function called by llapi_layout_comp_iterate()
8181 * to compare the specified mirror id with the one in the current
8182 * component of @layout. If they are the same, then the specified
8183 * mirror id is valid.
8185 * Return: a negative error code on failure or
8186 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8187 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8190 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8192 struct verify_mirror_id *mirror_id_cbdata =
8193 (struct verify_mirror_id *)cbdata;
8197 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8201 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8202 progname, strerror(errno));
8206 if (mirror_id_cbdata->mirror_id == mirror_id) {
8207 mirror_id_cbdata->is_valid_id = true;
8208 return LLAPI_LAYOUT_ITER_STOP;
8211 return LLAPI_LAYOUT_ITER_CONT;
8215 * verify_mirror_ids() - Verify specified mirror ids.
8216 * @fname: Mirrored file name.
8217 * @mirror_ids: Specified mirror ids to be verified.
8218 * @ids_nr: Number of specified mirror ids.
8220 * This function verifies that specified @mirror_ids are valid
8221 * in the mirrored file @fname.
8223 * Return: 0 on success or a negative error code on failure.
8226 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8228 struct llapi_layout *layout = NULL;
8229 struct verify_mirror_id mirror_id_cbdata = { 0 };
8240 if (stat(fname, &stbuf) < 0) {
8241 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8242 progname, fname, strerror(errno));
8247 if (!S_ISREG(stbuf.st_mode)) {
8248 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8254 fd = open(fname, O_DIRECT | O_RDONLY);
8256 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8257 progname, fname, strerror(errno));
8262 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8264 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8265 progname, fname, strerror(errno));
8269 layout = llapi_layout_get_by_fd(fd, 0);
8270 if (layout == NULL) {
8271 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8272 progname, fname, strerror(errno));
8274 llapi_lease_release(fd);
8278 rc = llapi_layout_flags_get(layout, &flr_state);
8280 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8281 progname, fname, strerror(errno));
8286 flr_state &= LCM_FL_FLR_MASK;
8287 switch (flr_state) {
8290 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8291 progname, fname, llapi_layout_flags_string(flr_state));
8298 for (i = 0; i < ids_nr; i++) {
8299 mirror_id_cbdata.mirror_id = mirror_ids[i];
8300 mirror_id_cbdata.is_valid_id = false;
8302 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8307 "%s: '%s' failed to verify mirror id: %u.\n",
8308 progname, fname, mirror_ids[i]);
8312 if (!mirror_id_cbdata.is_valid_id) {
8315 "%s: '%s' invalid specified mirror id: %u.\n",
8316 progname, fname, mirror_ids[i]);
8322 llapi_layout_free(layout);
8323 llapi_lease_release(fd);
8331 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8332 __u16 *mirror_ids, int ids_nr)
8334 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8335 struct llapi_layout *layout;
8346 if (stat(fname, &stbuf) < 0) {
8347 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8348 progname, fname, strerror(errno));
8352 if (!S_ISREG(stbuf.st_mode)) {
8353 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8359 fd = open(fname, O_DIRECT | O_RDWR);
8361 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8362 progname, fname, strerror(errno));
8367 layout = llapi_layout_get_by_fd(fd, 0);
8368 if (layout == NULL) {
8369 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8370 progname, fname, strerror(errno));
8375 rc = llapi_layout_flags_get(layout, &flr_state);
8377 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8378 progname, fname, strerror(errno));
8383 flr_state &= LCM_FL_FLR_MASK;
8384 if (flr_state == LCM_FL_NONE) {
8386 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8391 /* get stale component info */
8392 comp_size = llapi_mirror_find_stale(layout, comp_array,
8393 ARRAY_SIZE(comp_array),
8394 mirror_ids, ids_nr);
8395 if (comp_size <= 0) {
8400 ioc->lil_mode = LL_LEASE_WRLCK;
8401 ioc->lil_flags = LL_LEASE_RESYNC;
8402 rc = llapi_lease_set(fd, ioc);
8404 if (rc == -EALREADY)
8408 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8409 progname, fname, strerror(-rc));
8413 /* get the read range [start, end) */
8414 start = comp_array[0].lrc_start;
8415 end = comp_array[0].lrc_end;
8416 for (idx = 1; idx < comp_size; idx++) {
8417 if (comp_array[idx].lrc_start < start)
8418 start = comp_array[idx].lrc_start;
8419 if (end < comp_array[idx].lrc_end)
8420 end = comp_array[idx].lrc_end;
8423 rc = llapi_lease_check(fd);
8424 if (rc != LL_LEASE_WRLCK) {
8425 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8430 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8433 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
8434 progname, fname, strerror(-rc));
8436 /* need to do the lease unlock even resync fails */
8437 ioc->lil_mode = LL_LEASE_UNLCK;
8438 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8440 for (idx = 0; idx < comp_size; idx++) {
8441 if (comp_array[idx].lrc_synced) {
8442 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8447 rc2 = llapi_lease_set(fd, ioc);
8449 * llapi_lease_set returns lease mode when it request to unlock
8453 /* rc2 == 0 means lost lease lock */
8454 if (rc2 == 0 && rc == 0)
8456 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8458 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
8462 llapi_layout_free(layout);
8469 static inline int lfs_mirror_resync(int argc, char **argv)
8471 struct ll_ioc_lease *ioc = NULL;
8472 __u16 mirror_ids[128] = { 0 };
8477 struct option long_opts[] = {
8478 { .val = 'o', .name = "only", .has_arg = required_argument },
8481 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8484 rc = parse_mirror_ids(mirror_ids,
8485 sizeof(mirror_ids) / sizeof(__u16),
8489 "%s: bad mirror ids '%s'.\n",
8496 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8497 argv[0], argv[optind - 1]);
8503 if (argc == optind) {
8504 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8509 if (ids_nr > 0 && argc > optind + 1) {
8511 "%s: option '--only' cannot be used upon multiple files.\n",
8519 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8524 /* set the lease on the file */
8525 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8527 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8528 argv[0], strerror(errno));
8533 for (; optind < argc; optind++) {
8534 rc = lfs_mirror_resync_file(argv[optind], ioc,
8535 mirror_ids, ids_nr);
8536 /* ignore previous file's error, continue with next file */
8539 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8547 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8549 struct llapi_layout *layout;
8552 layout = llapi_layout_get_by_fd(fd, 0);
8553 if (layout == NULL) {
8554 fprintf(stderr, "could not get layout.\n");
8558 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8560 fprintf(stderr, "failed to iterate layout\n");
8561 llapi_layout_free(layout);
8564 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8565 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8566 llapi_layout_free(layout);
8570 llapi_layout_free(layout);
8576 * Check whether two files are the same file
8577 * \retval 0 same file
8578 * \retval 1 not the same file
8579 * \retval <0 error code
8581 static inline int check_same_file(const char *f1, const char *f2)
8586 if (stat(f1, &stbuf1) < 0) {
8587 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8588 progname, f1, strerror(errno));
8592 if (stat(f2, &stbuf2) < 0) {
8593 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8594 progname, f2, strerror(errno));
8598 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8599 stbuf1.st_ino == stbuf2.st_ino)
8605 static inline int lfs_mirror_read(int argc, char **argv)
8608 __u16 mirror_id = 0;
8609 const char *outfile = NULL;
8615 const size_t buflen = 4 << 20;
8617 struct option long_opts[] = {
8618 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8619 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8622 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8627 mirror_id = strtoul(optarg, &end, 0);
8628 if (*end != '\0' || mirror_id == 0) {
8630 "%s %s: invalid mirror ID '%s'\n",
8631 progname, argv[0], optarg);
8639 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8640 progname, argv[optind - 1]);
8645 if (argc == optind) {
8646 fprintf(stderr, "%s %s: no mirrored file provided\n",
8649 } else if (argc > optind + 1) {
8650 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8654 if (mirror_id == 0) {
8655 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8660 /* open mirror file */
8661 fname = argv[optind];
8664 rc = check_same_file(fname, outfile);
8667 "%s %s: output file cannot be the mirrored file\n",
8675 fd = open(fname, O_DIRECT | O_RDONLY);
8677 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8678 progname, argv[0], fname, strerror(errno));
8682 /* verify mirror id */
8683 rc = verify_mirror_id_by_fd(fd, mirror_id);
8686 "%s %s: cannot find mirror with ID %u in '%s'\n",
8687 progname, argv[0], mirror_id, fname);
8691 /* open output file */
8693 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8695 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8696 progname, argv[0], outfile, strerror(errno));
8701 outfd = STDOUT_FILENO;
8704 /* allocate buffer */
8705 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8707 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8708 progname, argv[0], rc);
8715 ssize_t written = 0;
8717 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8718 if (bytes_read < 0) {
8721 "%s %s: fail to read data from mirror %u: %s\n",
8722 progname, argv[0], mirror_id, strerror(-rc));
8727 if (bytes_read == 0)
8730 while (written < bytes_read) {
8733 written2 = write(outfd, buf + written,
8734 bytes_read - written);
8737 "%s %s: fail to write %s: %s\n",
8738 progname, argv[0], outfile ? : "STDOUT",
8743 written += written2;
8746 if (written != bytes_read) {
8748 "%s %s: written %ld bytes does not match with %ld read.\n",
8749 progname, argv[0], written, bytes_read);
8771 static inline int lfs_mirror_write(int argc, char **argv)
8774 __u16 mirror_id = 0;
8775 const char *inputfile = NULL;
8781 const size_t buflen = 4 << 20;
8783 size_t page_size = sysconf(_SC_PAGESIZE);
8784 struct ll_ioc_lease_id ioc;
8786 struct option long_opts[] = {
8787 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8788 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8791 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8796 mirror_id = strtoul(optarg, &end, 0);
8797 if (*end != '\0' || mirror_id == 0) {
8799 "%s %s: invalid mirror ID '%s'\n",
8800 progname, argv[0], optarg);
8808 fprintf(stderr, "%s: option '%s' unrecognized\n",
8809 progname, argv[optind - 1]);
8814 if (argc == optind) {
8815 fprintf(stderr, "%s %s: no mirrored file provided\n",
8818 } else if (argc > optind + 1) {
8819 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8823 if (mirror_id == 0) {
8824 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8829 /* open mirror file */
8830 fname = argv[optind];
8833 rc = check_same_file(fname, inputfile);
8836 "%s %s: input file cannot be the mirrored file\n",
8844 fd = open(fname, O_DIRECT | O_WRONLY);
8846 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8847 progname, argv[0], fname, strerror(errno));
8851 /* verify mirror id */
8852 rc = verify_mirror_id_by_fd(fd, mirror_id);
8855 "%s %s: cannot find mirror with ID %u in '%s'\n",
8856 progname, argv[0], mirror_id, fname);
8860 /* open input file */
8862 inputfd = open(inputfile, O_RDONLY, 0644);
8864 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
8865 progname, argv[0], inputfile, strerror(errno));
8870 inputfd = STDIN_FILENO;
8873 /* allocate buffer */
8874 rc = posix_memalign(&buf, page_size, buflen);
8876 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8877 progname, argv[0], rc);
8881 /* prepare target mirror components instantiation */
8882 ioc.lil_mode = LL_LEASE_WRLCK;
8883 ioc.lil_flags = LL_LEASE_RESYNC;
8884 ioc.lil_mirror_id = mirror_id;
8885 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8888 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8889 progname, argv[0], fname, strerror(errno));
8899 rc = llapi_lease_check(fd);
8900 if (rc != LL_LEASE_WRLCK) {
8901 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
8902 progname, argv[0], fname);
8906 bytes_read = read(inputfd, buf, buflen);
8907 if (bytes_read < 0) {
8910 "%s %s: fail to read data from '%s': %s\n",
8911 progname, argv[0], inputfile ? : "STDIN",
8918 if (bytes_read == 0)
8921 /* round up to page align to make direct IO happy. */
8922 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
8924 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
8929 "%s %s: fail to write to mirror %u: %s\n",
8930 progname, argv[0], mirror_id,
8938 if (pos & (page_size - 1)) {
8939 rc = llapi_mirror_truncate(fd, mirror_id, pos);
8944 ioc.lil_mode = LL_LEASE_UNLCK;
8945 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
8947 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8952 "%s %s: release lease lock of '%s' failed: %s\n",
8953 progname, argv[0], fname, strerror(errno));
8970 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
8972 struct llapi_layout *layout;
8973 struct collect_ids_data cid = { .cid_ids = ids,
8975 .cid_exclude = exclude_id, };
8978 layout = llapi_layout_get_by_fd(fd, 0);
8979 if (layout == NULL) {
8980 fprintf(stderr, "could not get layout\n");
8984 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
8986 fprintf(stderr, "failed to iterate layout\n");
8987 llapi_layout_free(layout);
8991 llapi_layout_free(layout);
8993 return cid.cid_count;
8996 static inline int lfs_mirror_copy(int argc, char **argv)
8999 __u16 read_mirror_id = 0;
9000 __u16 ids[128] = { 0 };
9002 struct llapi_layout *layout = NULL;
9003 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9010 struct ll_ioc_lease *ioc = NULL;
9011 struct ll_ioc_lease_id *resync_ioc;
9013 struct option long_opts[] = {
9014 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9015 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9018 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9023 read_mirror_id = strtoul(optarg, &end, 0);
9024 if (*end != '\0' || read_mirror_id == 0) {
9026 "%s %s: invalid read mirror ID '%s'\n",
9027 progname, argv[0], optarg);
9032 if (!strcmp(optarg, "-1")) {
9033 /* specify all other mirrors */
9037 count = parse_mirror_ids((__u16 *)ids,
9045 fprintf(stderr, "%s: option '%s' unrecognized\n",
9046 progname, argv[optind - 1]);
9051 if (argc == optind) {
9052 fprintf(stderr, "%s %s: no mirrored file provided\n",
9055 } else if (argc > optind + 1) {
9056 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9060 if (read_mirror_id == 0) {
9062 "%s %s: no valid read mirror ID %d is provided\n",
9063 progname, argv[0], read_mirror_id);
9069 "%s %s: no write mirror ID is provided\n",
9074 for (i = 0; i < count; i++) {
9075 if (read_mirror_id == ids[i]) {
9077 "%s %s: read and write mirror ID cannot be the same\n",
9083 /* open mirror file */
9084 fname = argv[optind];
9086 fd = open(fname, O_DIRECT | O_RDWR);
9088 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9089 progname, argv[0], fname, strerror(errno));
9093 /* write to all other mirrors */
9094 if (ids[0] == (__u16)-1) {
9095 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9099 "%s %s: failed to get other mirror ids in '%s': %d\n",
9100 progname, argv[0], fname, rc);
9105 /* verify mirror id */
9106 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9109 "%s %s: cannot find mirror with ID %u in '%s'\n",
9110 progname, argv[0], read_mirror_id, fname);
9114 for (i = 0; i < count; i++) {
9115 rc = verify_mirror_id_by_fd(fd, ids[i]);
9118 "%s %s: cannot find mirror with ID %u in '%s'\n",
9119 progname, argv[0], ids[i], fname);
9124 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9127 "%s %s: cannot alloc comp id array for ioc: %s\n",
9128 progname, argv[0], strerror(errno));
9133 /* get stale component info */
9134 layout = llapi_layout_get_by_fd(fd, 0);
9135 if (layout == NULL) {
9136 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9137 progname, argv[0], fname, strerror(errno));
9141 comp_size = llapi_mirror_find_stale(layout, comp_array,
9142 ARRAY_SIZE(comp_array),
9144 llapi_layout_free(layout);
9145 if (comp_size < 0) {
9150 /* prepare target mirror components instantiation */
9151 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9152 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9153 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9155 resync_ioc->lil_mirror_id = ids[0];
9157 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9158 rc = llapi_lease_set(fd, ioc);
9161 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9162 progname, argv[0], fname, strerror(errno));
9166 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9169 fprintf(stderr, "%s %s: copy error: %d\n",
9170 progname, argv[0], rc);
9174 fprintf(stdout, "mirror copied successfully: ");
9175 for (i = 0; i < copied; i++)
9176 fprintf(stdout, "%d ", ids[i]);
9177 fprintf(stdout, "\n");
9179 ioc->lil_mode = LL_LEASE_UNLCK;
9180 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9182 for (i = 0; i < comp_size; i++) {
9185 for (j = 0; j < copied; j++) {
9186 if (comp_array[i].lrc_mirror_id != ids[j])
9189 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9193 rc = llapi_lease_set(fd, ioc);
9198 "%s %s: release lease lock of '%s' failed: %s\n",
9199 progname, argv[0], fname, strerror(errno));
9213 * struct verify_chunk - Mirror chunk to be verified.
9214 * @chunk: [start, end) of the chunk.
9215 * @mirror_count: Number of mirror ids in @mirror_id array.
9216 * @mirror_id: Array of valid mirror ids that cover the chunk.
9218 struct verify_chunk {
9219 struct lu_extent chunk;
9220 unsigned int mirror_count;
9221 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9225 * print_chunks() - Print chunk information.
9226 * @fname: Mirrored file name.
9227 * @chunks: Array of chunks.
9228 * @chunk_count: Number of chunks in @chunks array.
9230 * This function prints [start, end) of each chunk in @chunks
9231 * for mirrored file @fname, and also prints the valid mirror ids
9232 * that cover the chunk.
9237 void print_chunks(const char *fname, struct verify_chunk *chunks,
9243 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9244 for (i = 0; i < chunk_count; i++) {
9245 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9247 if (chunks[i].mirror_count == 0)
9248 fprintf(stdout, "\t[");
9250 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9251 for (j = 1; j < chunks[i].mirror_count; j++)
9252 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9254 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9256 fprintf(stdout, "\n");
9260 * print_checksums() - Print CRC-32 checksum values.
9261 * @chunk: A chunk and its corresponding valid mirror ids.
9262 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9264 * This function prints CRC-32 checksum values on @chunk for
9265 * each valid mirror that covers it.
9270 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9275 "CRC-32 checksum value for chunk "DEXT":\n",
9276 PEXT(&chunk->chunk));
9277 for (i = 0; i < chunk->mirror_count; i++)
9278 fprintf(stdout, "Mirror %u:\t%#lx\n",
9279 chunk->mirror_id[i], crc[i]);
9280 fprintf(stdout, "\n");
9284 * filter_mirror_id() - Filter specified mirror ids.
9285 * @chunks: Array of chunks.
9286 * @chunk_count: Number of chunks in @chunks array.
9287 * @mirror_ids: Specified mirror ids to be verified.
9288 * @ids_nr: Number of specified mirror ids.
9290 * This function scans valid mirror ids that cover each chunk in @chunks
9291 * and filters specified mirror ids.
9296 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9297 __u16 *mirror_ids, int ids_nr)
9302 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9303 unsigned int valid_count = 0;
9305 for (i = 0; i < chunk_count; i++) {
9306 if (chunks[i].mirror_count == 0)
9310 for (j = 0; j < ids_nr; j++) {
9311 for (k = 0; k < chunks[i].mirror_count; k++) {
9312 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9313 valid_id[valid_count] = mirror_ids[j];
9320 memcpy(chunks[i].mirror_id, valid_id,
9321 sizeof(__u16) * valid_count);
9322 chunks[i].mirror_count = valid_count;
9327 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9328 * @layout: Mirror component list.
9329 * @chunks: Array of chunks.
9330 * @chunks_size: Array size of @chunks.
9332 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9333 * to find out chunk segments and store them in @chunks array.
9335 * The @mirror_id array in each element of @chunks will store the valid
9336 * mirror ids that cover the chunk. If a mirror component covering the
9337 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9338 * will not be stored into the @mirror_id array, and the chunk for that
9339 * mirror will not be verified.
9341 * The @mirror_count in each element of @chunks will store the number of
9342 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9343 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9344 * indicates the chunk is valid in only one mirror. In both cases, the
9345 * chunk will not be verified.
9347 * Here is an example:
9350 * +------+-------------+--------------------+
9352 * +------+------+------+------+-------------+
9353 * | | S | S | | mirror2
9354 * +-------------+------+------+-------------+
9356 * prepared @chunks array will contain 5 elements:
9357 * (([0, 1M), [1, 2], 2),
9358 * ([1M, 2M), [1, 2], 2),
9359 * ([2M, 3M), [1], 1),
9360 * ([3M, 4M], [], 0),
9361 * ([4M, EOF), [2], 1))
9363 * Return: the actual array size of @chunks on success
9364 * or a negative error code on failure.
9367 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9368 struct verify_chunk *chunks,
9379 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9382 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9385 "%s: move to the first layout component: %s.\n",
9386 progname, strerror(errno));
9392 chunks[idx].chunk.e_end = LUSTRE_EOF;
9394 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9397 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9398 progname, strerror(errno));
9402 if (start > chunks[idx].chunk.e_start ||
9403 end <= chunks[idx].chunk.e_start)
9406 if (end < chunks[idx].chunk.e_end)
9407 chunks[idx].chunk.e_end = end;
9409 rc = llapi_layout_comp_flags_get(layout, &flags);
9412 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9413 progname, strerror(errno));
9417 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9420 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9423 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9424 progname, strerror(errno));
9428 chunks[idx].mirror_id[i] = mirror_id;
9430 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9432 "%s: mirror_id array is too small.\n",
9439 rc = llapi_layout_comp_use(layout,
9440 LLAPI_LAYOUT_COMP_USE_NEXT);
9443 "%s: move to the next layout component: %s.\n",
9444 progname, strerror(errno));
9447 } /* loop through all components */
9449 chunks[idx].mirror_count = i;
9451 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9455 if (idx >= chunks_size) {
9456 fprintf(stderr, "%s: chunks array is too small.\n",
9462 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9466 return rc < 0 ? rc : idx + 1;
9470 * lfs_mirror_verify_chunk() - Verify a chunk.
9471 * @fd: File descriptor of the mirrored file.
9472 * @file_size: Size of the mirrored file.
9473 * @chunk: A chunk and its corresponding valid mirror ids.
9474 * @verbose: Verbose mode.
9476 * This function verifies a @chunk contains exactly the same data
9477 * ammong the mirrors that cover it.
9479 * If @verbose is specified, then the function will print where the
9480 * differences are if the data do not match. Otherwise, it will
9481 * just return an error in that case.
9483 * Return: 0 on success or a negative error code on failure.
9486 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9487 struct verify_chunk *chunk, int verbose)
9489 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9491 size_t page_size = sysconf(_SC_PAGESIZE);
9497 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9504 rc = posix_memalign(&buf, page_size, buflen);
9505 if (rc) /* error code is returned directly */
9509 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9510 PEXT(&chunk->chunk));
9511 for (i = 0; i < chunk->mirror_count; i++)
9512 fprintf(stdout, " %u", chunk->mirror_id[i]);
9513 fprintf(stdout, "\n");
9517 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9518 pos = chunk->chunk.e_start;
9519 while (bytes_done < count) {
9520 /* compute initial CRC-32 checksum */
9521 crc = crc32(0L, Z_NULL, 0);
9522 memset(crc_array, 0, sizeof(crc_array));
9525 for (i = 0; i < chunk->mirror_count; i++) {
9526 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9528 if (bytes_read < 0) {
9531 "%s: failed to read data from mirror %u: %s.\n",
9532 progname, chunk->mirror_id[i],
9537 /* compute new CRC-32 checksum */
9538 crc_array[i] = crc32(crc, buf, bytes_read);
9542 print_checksums(chunk, crc_array);
9544 /* compare CRC-32 checksum values */
9545 for (i = 1; i < chunk->mirror_count; i++) {
9546 if (crc_array[i] != crc_array[0]) {
9550 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9551 progname, PEXT(&chunk->chunk),
9552 chunk->mirror_id[0],
9553 chunk->mirror_id[i]);
9558 bytes_done += bytes_read;
9561 if (verbose > 1 && rc == 0) {
9562 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9563 PEXT(&chunk->chunk));
9564 for (i = 0; i < chunk->mirror_count; i++)
9565 fprintf(stdout, " %u", chunk->mirror_id[i]);
9566 fprintf(stdout, " PASS\n\n");
9575 * lfs_mirror_verify_file() - Verify a mirrored file.
9576 * @fname: Mirrored file name.
9577 * @mirror_ids: Specified mirror ids to be verified.
9578 * @ids_nr: Number of specified mirror ids.
9579 * @verbose: Verbose mode.
9581 * This function verifies that each SYNC mirror of a mirrored file
9582 * specified by @fname contains exactly the same data.
9584 * If @mirror_ids is specified, then the function will verify the
9585 * mirrors specified by @mirror_ids contain exactly the same data.
9587 * If @verbose is specified, then the function will print where the
9588 * differences are if the data do not match. Otherwise, it will
9589 * just return an error in that case.
9591 * Return: 0 on success or a negative error code on failure.
9594 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9597 struct verify_chunk chunks_array[1024] = { };
9598 struct llapi_layout *layout = NULL;
9602 int chunk_count = 0;
9608 if (stat(fname, &stbuf) < 0) {
9609 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9610 progname, fname, strerror(errno));
9615 if (!S_ISREG(stbuf.st_mode)) {
9616 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9622 if (stbuf.st_size == 0) {
9624 fprintf(stdout, "%s: '%s' file size is 0.\n",
9630 fd = open(fname, O_DIRECT | O_RDONLY);
9632 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9633 progname, fname, strerror(errno));
9638 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9640 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9641 progname, fname, strerror(errno));
9645 layout = llapi_layout_get_by_fd(fd, 0);
9646 if (layout == NULL) {
9647 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9648 progname, fname, strerror(errno));
9650 llapi_lease_release(fd);
9654 rc = llapi_layout_flags_get(layout, &flr_state);
9656 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9657 progname, fname, strerror(errno));
9662 flr_state &= LCM_FL_FLR_MASK;
9663 switch (flr_state) {
9666 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9667 progname, fname, llapi_layout_flags_string(flr_state));
9673 /* find out mirror chunks to be verified */
9674 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9675 ARRAY_SIZE(chunks_array));
9676 if (chunk_count < 0) {
9682 /* filter specified mirror ids */
9683 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9686 print_chunks(fname, chunks_array, chunk_count);
9688 for (idx = 0; idx < chunk_count; idx++) {
9689 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9692 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9694 PEXT(&chunks_array[idx].chunk),
9695 (unsigned long long)stbuf.st_size);
9699 if (chunks_array[idx].mirror_count == 0) {
9701 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9703 PEXT(&chunks_array[idx].chunk));
9705 fprintf(stderr, "skipped\n");
9709 fprintf(stderr, "failed\n");
9713 if (chunks_array[idx].mirror_count == 1) {
9716 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9718 PEXT(&chunks_array[idx].chunk),
9719 chunks_array[idx].mirror_id[0]);
9723 rc = llapi_lease_check(fd);
9724 if (rc != LL_LEASE_RDLCK) {
9725 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9730 /* verify one chunk */
9731 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9732 &chunks_array[idx], verbose);
9746 llapi_layout_free(layout);
9747 llapi_lease_release(fd);
9755 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9756 * @argc: The count of lfs mirror verify command line arguments.
9757 * @argv: Array of strings for lfs mirror verify command line arguments.
9759 * This function parses lfs mirror verify command and verifies the
9760 * specified mirrored file(s).
9762 * Return: 0 on success or a negative error code on failure.
9764 static inline int lfs_mirror_verify(int argc, char **argv)
9766 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9774 struct option long_opts[] = {
9775 { .val = 'o', .name = "only", .has_arg = required_argument },
9776 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9779 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9781 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9784 rc = parse_mirror_ids(mirror_ids,
9785 ARRAY_SIZE(mirror_ids),
9789 "%s: bad mirror ids '%s'.\n",
9796 "%s: at least 2 mirror ids needed with '--only' option.\n",
9806 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9807 progname, argv[optind - 1]);
9813 if (argc == optind) {
9814 fprintf(stderr, "%s: no file name given.\n", progname);
9819 if (ids_nr > 0 && argc > optind + 1) {
9821 "%s: '--only' cannot be used upon multiple files.\n",
9829 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9835 for (; optind < argc; optind++) {
9836 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9846 * lfs_mirror() - Parse and execute lfs mirror commands.
9847 * @argc: The count of lfs mirror command line arguments.
9848 * @argv: Array of strings for lfs mirror command line arguments.
9850 * This function parses lfs mirror commands and performs the
9851 * corresponding functions specified in mirror_cmdlist[].
9853 * Return: 0 on success or an error code on failure.
9855 static int lfs_mirror(int argc, char **argv)
9862 Parser_init("lfs-mirror > ", mirror_cmdlist);
9864 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9866 program_invocation_short_name = cmd;
9868 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9870 rc = Parser_commands();
9872 return rc < 0 ? -rc : rc;
9875 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9877 #if __BYTE_ORDER == __BIG_ENDIAN
9878 __swab16s(&attrs->lsa_valid);
9879 __swab64s(&attrs->lsa_size);
9880 __swab64s(&attrs->lsa_blocks);
9886 LFS_SOM_BLOCKS = 0x2,
9887 LFS_SOM_FLAGS = 0x4,
9888 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9892 static int lfs_getsom(int argc, char **argv)
9895 struct lustre_som_attrs *attrs;
9896 char buf[sizeof(*attrs) + 64];
9897 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9900 while ((c = getopt(argc, argv, "sbf")) != -1) {
9903 type = LFS_SOM_SIZE;
9906 type = LFS_SOM_BLOCKS;
9909 type = LFS_SOM_FLAGS;
9912 fprintf(stderr, "%s: invalid option '%c'\n",
9922 fprintf(stderr, "%s: %s\n",
9923 progname, argc == 0 ? "miss file target" :
9924 "input more than 2 files");
9929 attrs = (void *)buf;
9930 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9933 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
9934 argv[0], strerror(errno), errno);
9938 lustre_som_swab(attrs);
9941 case LFS_SOM_ATTR_ALL:
9942 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9943 path, (unsigned long long)attrs->lsa_size,
9944 (unsigned long long)attrs->lsa_blocks,
9948 printf("%llu\n", (unsigned long long)attrs->lsa_size);
9950 case LFS_SOM_BLOCKS:
9951 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
9954 printf("%x\n", attrs->lsa_valid);
9957 fprintf(stderr, "%s: unknown option\n", progname);
9965 * lfs_mirror_list_commands() - List lfs mirror commands.
9966 * @argc: The count of command line arguments.
9967 * @argv: Array of strings for command line arguments.
9969 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9971 * Return: 0 on success.
9973 static int lfs_mirror_list_commands(int argc, char **argv)
9975 char buffer[81] = "";
9977 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9983 static int lfs_list_commands(int argc, char **argv)
9985 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9987 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9992 int main(int argc, char **argv)
9996 /* Ensure that liblustreapi constructor has run */
9997 if (!llapi_liblustreapi_initialized())
9998 fprintf(stderr, "liblustreapi was not properly initialized\n");
10000 setlinebuf(stdout);
10003 Parser_init("lfs > ", cmdlist);
10005 progname = program_invocation_short_name; /* Used in error messages */
10007 llapi_set_command_name(argv[1]);
10008 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10009 llapi_clear_command_name();
10011 rc = Parser_commands();
10014 return rc < 0 ? -rc : rc;
10017 #ifdef _LUSTRE_IDL_H_
10018 /* Everything we need here should be included by lustreapi.h. */
10019 # error "lfs should not depend on lustre_idl.h"
10020 #endif /* _LUSTRE_IDL_H_ */