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 lsa.lsa_nr_tgts = parse_targets(tgts,
3115 sizeof(tgts) / sizeof(__u32),
3116 lsa.lsa_nr_tgts, optarg);
3117 if (lsa.lsa_nr_tgts < 0) {
3119 "%s %s: invalid OST target(s) '%s'\n",
3120 progname, argv[0], optarg);
3124 lsa.lsa_tgts = tgts;
3125 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3126 lsa.lsa_stripe_off = tgts[0];
3131 lsa.lsa_pool_name = optarg;
3133 if (strlen(lsa.lsa_pool_name) == 0 ||
3134 strncmp(lsa.lsa_pool_name, "none",
3135 LOV_MAXPOOLNAME) == 0)
3136 lsa.lsa_pool_name = NULL;
3139 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3143 "%s %s: invalid stripe size '%s'\n",
3144 progname, argv[0], optarg);
3149 if (!migrate_mode) {
3151 "%s %s: -v|--verbose valid only for migrate command\n",
3155 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3162 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3163 progname, argv[0], argv[optind - 1]);
3168 fname = argv[optind];
3170 if (optind == argc) {
3171 fprintf(stderr, "%s %s: FILE must be specified\n",
3176 if (mirror_mode && mirror_count == 0) {
3178 "error: %s: --mirror-count|-N option is required\n",
3185 if (lsa.lsa_comp_end == 0)
3186 lsa.lsa_comp_end = LUSTRE_EOF;
3189 if (lsa.lsa_comp_end != 0) {
3190 result = comp_args_to_layout(lpp, &lsa, true);
3195 if (mirror_flags & MF_NO_VERIFY) {
3196 if (opc != SO_MIRROR_EXTEND) {
3198 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3202 } else if (!has_m_file) {
3204 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3211 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3212 * altered by user space tool, so we don't need to support the
3213 * --component-set for this moment. */
3214 if (comp_set && !comp_id) {
3215 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3220 if ((delete + comp_set + comp_del + comp_add) > 1) {
3222 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3227 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3228 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3230 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3235 if ((comp_set || comp_del) &&
3236 (setstripe_args_specified(&lsa) || layout != NULL)) {
3238 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3243 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3245 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3250 if (comp_add || comp_del) {
3253 result = lstat(fname, &st);
3254 if (result == 0 && S_ISDIR(st.st_mode)) {
3256 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3262 fprintf(stderr, "error: %s: can't use --component-add "
3263 "or --component-del for mirror operation\n",
3270 if (layout == NULL) {
3272 "%s %s: option -E must be specified with --component-add\n",
3277 result = adjust_first_extent(fname, layout);
3278 if (result == -ENODATA)
3280 else if (result != 0)
3284 if (from_yaml && from_copy) {
3286 "%s: can't specify --yaml and --copy together\n",
3291 if ((from_yaml || from_copy) &&
3292 (setstripe_args_specified(&lsa) || layout != NULL)) {
3293 fprintf(stderr, "error: %s: can't specify --yaml with "
3294 "-c, -S, -i, -o, -p or -E options.\n",
3299 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3301 "%s %s: options --non-block and --block are mutually exclusive\n",
3306 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3309 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3314 if (migrate_mdt_mode) {
3315 struct lmv_user_md *lmu;
3317 /* initialize migrate mdt parameters */
3318 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3319 LMV_USER_MAGIC_SPECIFIC));
3322 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3323 progname, argv[0], strerror(ENOMEM));
3327 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3328 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3329 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3331 "%s %s: migrate should specify MDT index\n",
3336 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3337 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3338 lmu->lum_hash_type = lsa.lsa_pattern;
3340 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3341 if (lsa.lsa_pool_name) {
3342 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3343 sizeof(lmu->lum_pool_name) - 1);
3344 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3346 if (lsa.lsa_nr_tgts > 1) {
3349 if (lsa.lsa_stripe_count > 0 &&
3350 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3351 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3353 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3354 progname, lsa.lsa_stripe_count,
3360 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3361 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3362 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3363 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3365 lmu->lum_magic = LMV_USER_MAGIC;
3368 migrate_mdt_param.fp_lmv_md = lmu;
3369 migrate_mdt_param.fp_migrate = 1;
3370 } else if (layout == NULL) {
3371 /* initialize stripe parameters */
3372 param = calloc(1, offsetof(typeof(*param),
3373 lsp_osts[lsa.lsa_nr_tgts]));
3374 if (param == NULL) {
3376 "%s %s: cannot allocate memory for parameters: %s\n",
3377 progname, argv[0], strerror(ENOMEM));
3382 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3383 param->lsp_stripe_size = lsa.lsa_stripe_size;
3384 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3385 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3386 param->lsp_stripe_count = -1;
3388 param->lsp_stripe_count = lsa.lsa_stripe_count;
3390 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3391 param->lsp_stripe_offset = -1;
3393 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3394 param->lsp_pool = lsa.lsa_pool_name;
3395 param->lsp_is_specific = false;
3396 if (lsa.lsa_nr_tgts > 0) {
3397 if (lsa.lsa_stripe_count > 0 &&
3398 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3399 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3400 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3402 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3403 argv[0], lsa.lsa_stripe_count,
3409 param->lsp_is_specific = true;
3410 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3411 memcpy(param->lsp_osts, tgts,
3412 sizeof(*tgts) * lsa.lsa_nr_tgts);
3417 /* generate a layout from a YAML template */
3418 result = lfs_comp_create_from_yaml(template, &layout,
3421 fprintf(stderr, "error: %s: can't create composite "
3422 "layout from template file %s\n",
3426 } else if (from_copy) {
3427 layout = llapi_layout_get_by_path(template, 0);
3428 if (layout == NULL) {
3430 "%s: can't create composite layout from file %s.\n",
3431 progname, template);
3436 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3437 if (migrate_mdt_mode) {
3438 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3439 } else if (migrate_mode) {
3440 result = lfs_migrate(fname, migration_flags, param,
3442 } else if (comp_set != 0) {
3443 result = lfs_component_set(fname, comp_id,
3445 lsa.lsa_comp_neg_flags);
3446 } else if (comp_del != 0) {
3447 result = lfs_component_del(fname, comp_id,
3449 lsa.lsa_comp_neg_flags);
3450 } else if (comp_add != 0) {
3451 result = lfs_component_add(fname, layout);
3452 } else if (opc == SO_MIRROR_CREATE) {
3453 result = mirror_create(fname, mirror_list);
3454 } else if (opc == SO_MIRROR_EXTEND) {
3455 result = mirror_extend(fname, mirror_list,
3457 } else if (opc == SO_MIRROR_SPLIT) {
3458 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3460 "%s %s: no mirror id or component id or pool name"
3461 " is specified\n", progname, argv[0]);
3464 if (lsa.lsa_pool_name)
3465 mirror_flags |= MF_COMP_POOL;
3466 else if (mirror_id != 0)
3467 comp_id = mirror_id;
3469 mirror_flags |= MF_COMP_ID;
3470 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3472 has_m_file ? mirror_list->m_file :
3474 } else if (layout != NULL) {
3475 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3482 result = llapi_file_open_param(fname,
3491 /* Save the first error encountered. */
3499 free(migrate_mdt_param.fp_lmv_md);
3500 llapi_layout_free(layout);
3501 lfs_mirror_list_free(mirror_list);
3506 llapi_layout_free(layout);
3507 lfs_mirror_list_free(mirror_list);
3511 static int lfs_poollist(int argc, char **argv)
3516 return llapi_poollist(argv[1]);
3519 static int set_time(time_t *time, time_t *set, char *str)
3527 else if (str[0] == '-')
3533 t = strtol(str, &endptr, 0);
3534 if (*endptr != '\0') {
3536 "%s find: bad time '%s': %s\n",
3537 progname, str, strerror(EINVAL));
3540 if (*time < t * 24 * 60 * 60) {
3544 "%s find: bad time '%s': too large\n",
3549 *set = *time - t * 24 * 60 * 60;
3553 static int name2uid(unsigned int *id, const char *name)
3555 struct passwd *passwd;
3557 passwd = getpwnam(name);
3560 *id = passwd->pw_uid;
3565 static int name2gid(unsigned int *id, const char *name)
3567 struct group *group;
3569 group = getgrnam(name);
3572 *id = group->gr_gid;
3577 static inline int name2projid(unsigned int *id, const char *name)
3582 static int uid2name(char **name, unsigned int id)
3584 struct passwd *passwd;
3586 passwd = getpwuid(id);
3589 *name = passwd->pw_name;
3594 static inline int gid2name(char **name, unsigned int id)
3596 struct group *group;
3598 group = getgrgid(id);
3601 *name = group->gr_name;
3606 static int name2layout(__u32 *layout, char *name)
3608 char *ptr, *layout_name;
3611 for (ptr = name; ; ptr = NULL) {
3612 layout_name = strtok(ptr, ",");
3613 if (layout_name == NULL)
3615 if (strcmp(layout_name, "released") == 0)
3616 *layout |= LOV_PATTERN_F_RELEASED;
3617 else if (strcmp(layout_name, "raid0") == 0)
3618 *layout |= LOV_PATTERN_RAID0;
3619 else if (strcmp(layout_name, "mdt") == 0)
3620 *layout |= LOV_PATTERN_MDT;
3627 static int lfs_find(int argc, char **argv)
3632 struct find_param param = {
3636 struct option long_opts[] = {
3637 { .val = 'A', .name = "atime", .has_arg = required_argument },
3638 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3639 { .val = LFS_COMP_COUNT_OPT,
3640 .name = "comp-count", .has_arg = required_argument },
3641 { .val = LFS_COMP_COUNT_OPT,
3642 .name = "component-count",
3643 .has_arg = required_argument },
3644 { .val = LFS_COMP_FLAGS_OPT,
3645 .name = "comp-flags", .has_arg = required_argument },
3646 { .val = LFS_COMP_FLAGS_OPT,
3647 .name = "component-flags",
3648 .has_arg = required_argument },
3649 { .val = LFS_COMP_START_OPT,
3650 .name = "comp-start", .has_arg = required_argument },
3651 { .val = LFS_COMP_START_OPT,
3652 .name = "component-start",
3653 .has_arg = required_argument },
3654 { .val = LFS_MIRROR_STATE_OPT,
3655 .name = "mirror-state", .has_arg = required_argument },
3656 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3657 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3658 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3659 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3660 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3661 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3662 { .val = 'E', .name = "component-end",
3663 .has_arg = required_argument },
3664 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3665 { .val = 'g', .name = "gid", .has_arg = required_argument },
3666 { .val = 'G', .name = "group", .has_arg = required_argument },
3667 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3668 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3669 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3670 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3671 { .val = 'l', .name = "lazy", .has_arg = no_argument },
3672 { .val = 'L', .name = "layout", .has_arg = required_argument },
3673 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3674 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3675 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3676 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3677 { .val = 'n', .name = "name", .has_arg = required_argument },
3678 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3679 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3680 { .val = 'O', .name = "obd", .has_arg = required_argument },
3681 { .val = 'O', .name = "ost", .has_arg = required_argument },
3682 /* no short option for pool yet, can be 'p' after 2.18 */
3683 { .val = LFS_POOL_OPT,
3684 .name = "pool", .has_arg = required_argument },
3685 { .val = '0', .name = "print0", .has_arg = no_argument },
3686 { .val = 'P', .name = "print", .has_arg = no_argument },
3687 { .val = LFS_PROJID_OPT,
3688 .name = "projid", .has_arg = required_argument },
3689 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3690 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3691 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3692 { .val = 's', .name = "size", .has_arg = required_argument },
3693 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3694 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3695 { .val = 't', .name = "type", .has_arg = required_argument },
3696 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3697 { .val = 'u', .name = "uid", .has_arg = required_argument },
3698 { .val = 'U', .name = "user", .has_arg = required_argument },
3699 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3700 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3712 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3713 while ((c = getopt_long_only(argc, argv,
3714 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3715 long_opts, NULL)) >= 0) {
3720 /* '!' is part of option */
3721 /* when getopt_long_only() finds a string which is not
3722 * an option nor a known option argument it returns 1
3723 * in that case if we already have found pathstart and pathend
3724 * (i.e. we have the list of pathnames),
3725 * the only supported value is "!"
3727 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3728 if (!isoption && pathend != -1) {
3729 fprintf(stderr, "err: %s: filename|dirname must either "
3730 "precede options or follow options\n",
3735 if (!isoption && pathstart == -1)
3736 pathstart = optind - 1;
3737 if (isoption && pathstart != -1 && pathend == -1)
3738 pathend = optind - 2;
3744 /* unknown; opt is "!" or path component,
3745 * checking done above.
3747 if (strcmp(optarg, "!") == 0)
3751 xtime = ¶m.fp_atime;
3752 xsign = ¶m.fp_asign;
3753 param.fp_exclude_atime = !!neg_opt;
3754 /* no break, this falls through to 'C' for ctime */
3757 xtime = ¶m.fp_ctime;
3758 xsign = ¶m.fp_csign;
3759 param.fp_exclude_ctime = !!neg_opt;
3761 /* no break, this falls through to 'M' for mtime */
3764 xtime = ¶m.fp_mtime;
3765 xsign = ¶m.fp_msign;
3766 param.fp_exclude_mtime = !!neg_opt;
3768 rc = set_time(&t, xtime, optarg);
3769 if (rc == INT_MAX) {
3777 if (optarg[0] == '+') {
3778 param.fp_blocks_sign = -1;
3780 } else if (optarg[0] == '-') {
3781 param.fp_blocks_sign = 1;
3785 param.fp_blocks_units = 1024;
3786 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3787 ¶m.fp_blocks_units, 0);
3789 fprintf(stderr, "error: bad blocks '%s'\n",
3793 param.fp_check_blocks = 1;
3794 param.fp_exclude_blocks = !!neg_opt;
3796 case LFS_COMP_COUNT_OPT:
3797 if (optarg[0] == '+') {
3798 param.fp_comp_count_sign = -1;
3800 } else if (optarg[0] == '-') {
3801 param.fp_comp_count_sign = 1;
3805 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3806 if (*endptr != '\0') {
3807 fprintf(stderr, "error: bad component count "
3811 param.fp_check_comp_count = 1;
3812 param.fp_exclude_comp_count = !!neg_opt;
3814 case LFS_COMP_FLAGS_OPT:
3815 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3816 ¶m.fp_comp_neg_flags);
3818 fprintf(stderr, "error: bad component flags "
3822 param.fp_check_comp_flags = 1;
3824 __u32 flags = param.fp_comp_neg_flags;
3825 param.fp_comp_neg_flags = param.fp_comp_flags;
3826 param.fp_comp_flags = flags;
3829 case LFS_COMP_START_OPT:
3830 if (optarg[0] == '+') {
3831 param.fp_comp_start_sign = -1;
3833 } else if (optarg[0] == '-') {
3834 param.fp_comp_start_sign = 1;
3838 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3839 ¶m.fp_comp_start_units, 0);
3841 fprintf(stderr, "error: bad component start "
3845 param.fp_check_comp_start = 1;
3846 param.fp_exclude_comp_start = !!neg_opt;
3848 case LFS_MIRROR_STATE_OPT:
3849 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3850 ¶m.fp_mirror_neg_state);
3853 "error: bad mirrored file state '%s'\n",
3857 param.fp_check_mirror_state = 1;
3859 __u16 state = param.fp_mirror_neg_state;
3860 param.fp_mirror_neg_state =
3861 param.fp_mirror_state;
3862 param.fp_mirror_state = state;
3866 if (optarg[0] == '+') {
3867 param.fp_stripe_count_sign = -1;
3869 } else if (optarg[0] == '-') {
3870 param.fp_stripe_count_sign = 1;
3874 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3875 if (*endptr != '\0') {
3876 fprintf(stderr,"error: bad stripe_count '%s'\n",
3881 param.fp_check_stripe_count = 1;
3882 param.fp_exclude_stripe_count = !!neg_opt;
3885 param.fp_max_depth = strtol(optarg, 0, 0);
3888 if (optarg[0] == '+') {
3889 param.fp_comp_end_sign = -1;
3891 } else if (optarg[0] == '-') {
3892 param.fp_comp_end_sign = 1;
3896 if (arg_is_eof(optarg)) {
3897 param.fp_comp_end = LUSTRE_EOF;
3898 param.fp_comp_end_units = 1;
3901 rc = llapi_parse_size(optarg,
3903 ¶m.fp_comp_end_units, 0);
3906 fprintf(stderr, "error: bad component end "
3910 param.fp_check_comp_end = 1;
3911 param.fp_exclude_comp_end = !!neg_opt;
3915 rc = name2gid(¶m.fp_gid, optarg);
3917 param.fp_gid = strtoul(optarg, &endptr, 10);
3918 if (*endptr != '\0') {
3919 fprintf(stderr, "Group/GID: %s cannot "
3920 "be found.\n", optarg);
3925 param.fp_exclude_gid = !!neg_opt;
3926 param.fp_check_gid = 1;
3929 param.fp_hash_type = check_hashtype(optarg);
3930 if (param.fp_hash_type == 0) {
3931 fprintf(stderr, "error: bad hash_type '%s'\n",
3936 param.fp_check_hash_type = 1;
3937 param.fp_exclude_hash_type = !!neg_opt;
3943 ret = name2layout(¶m.fp_layout, optarg);
3946 param.fp_exclude_layout = !!neg_opt;
3947 param.fp_check_layout = 1;
3951 rc = name2uid(¶m.fp_uid, optarg);
3953 param.fp_uid = strtoul(optarg, &endptr, 10);
3954 if (*endptr != '\0') {
3955 fprintf(stderr, "User/UID: %s cannot "
3956 "be found.\n", optarg);
3961 param.fp_exclude_uid = !!neg_opt;
3962 param.fp_check_uid = 1;
3965 param.fp_pattern = (char *)optarg;
3966 param.fp_exclude_pattern = !!neg_opt;
3969 if (optarg[0] == '+') {
3970 param.fp_mirror_count_sign = -1;
3972 } else if (optarg[0] == '-') {
3973 param.fp_mirror_count_sign = 1;
3977 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3978 if (*endptr != '\0') {
3980 "error: bad mirror count '%s'\n",
3984 param.fp_check_mirror_count = 1;
3985 param.fp_exclude_mirror_count = !!neg_opt;
3990 char *buf, *token, *next, *p;
3994 buf = strdup(optarg);
4000 param.fp_exclude_obd = !!neg_opt;
4003 while (token && *token) {
4004 token = strchr(token, ',');
4011 param.fp_exclude_mdt = !!neg_opt;
4012 param.fp_num_alloc_mdts += len;
4013 tmp = realloc(param.fp_mdt_uuid,
4014 param.fp_num_alloc_mdts *
4015 sizeof(*param.fp_mdt_uuid));
4021 param.fp_mdt_uuid = tmp;
4023 param.fp_exclude_obd = !!neg_opt;
4024 param.fp_num_alloc_obds += len;
4025 tmp = realloc(param.fp_obd_uuid,
4026 param.fp_num_alloc_obds *
4027 sizeof(*param.fp_obd_uuid));
4033 param.fp_obd_uuid = tmp;
4035 for (token = buf; token && *token; token = next) {
4036 struct obd_uuid *puuid;
4039 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4042 ¶m.fp_obd_uuid[param.fp_num_obds++];
4044 p = strchr(token, ',');
4051 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4056 strncpy(puuid->uuid, token,
4057 sizeof(puuid->uuid));
4064 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4068 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4070 "Pool name %s is too long (max %d)\n",
4071 optarg, LOV_MAXPOOLNAME);
4076 * We do check for empty pool because empty pool
4077 * is used to find V1 LOV attributes
4079 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4080 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4081 param.fp_exclude_pool = !!neg_opt;
4082 param.fp_check_pool = 1;
4084 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4085 case 'p': /* want this for --pool, to match getstripe/find */
4087 "warning: -p deprecated, use --print0 or -0\n");
4090 param.fp_zero_end = 1;
4092 case 'P': /* we always print, this option is a no-op */
4094 case LFS_PROJID_OPT:
4095 rc = name2projid(¶m.fp_projid, optarg);
4097 param.fp_projid = strtoul(optarg, &endptr, 10);
4098 if (*endptr != '\0') {
4100 "Invalid project ID: %s",
4106 param.fp_exclude_projid = !!neg_opt;
4107 param.fp_check_projid = 1;
4110 if (optarg[0] == '+') {
4111 param.fp_size_sign = -1;
4113 } else if (optarg[0] == '-') {
4114 param.fp_size_sign = 1;
4118 ret = llapi_parse_size(optarg, ¶m.fp_size,
4119 ¶m.fp_size_units, 0);
4121 fprintf(stderr, "error: bad file size '%s'\n",
4125 param.fp_check_size = 1;
4126 param.fp_exclude_size = !!neg_opt;
4129 if (optarg[0] == '+') {
4130 param.fp_stripe_size_sign = -1;
4132 } else if (optarg[0] == '-') {
4133 param.fp_stripe_size_sign = 1;
4137 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4138 ¶m.fp_stripe_size_units, 0);
4140 fprintf(stderr, "error: bad stripe_size '%s'\n",
4144 param.fp_check_stripe_size = 1;
4145 param.fp_exclude_stripe_size = !!neg_opt;
4148 param.fp_exclude_type = !!neg_opt;
4149 switch (optarg[0]) {
4151 param.fp_type = S_IFBLK;
4154 param.fp_type = S_IFCHR;
4157 param.fp_type = S_IFDIR;
4160 param.fp_type = S_IFREG;
4163 param.fp_type = S_IFLNK;
4166 param.fp_type = S_IFIFO;
4169 param.fp_type = S_IFSOCK;
4172 fprintf(stderr, "error: %s: bad type '%s'\n",
4179 if (optarg[0] == '+') {
4180 param.fp_mdt_count_sign = -1;
4182 } else if (optarg[0] == '-') {
4183 param.fp_mdt_count_sign = 1;
4187 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4188 if (*endptr != '\0') {
4189 fprintf(stderr, "error: bad mdt_count '%s'\n",
4194 param.fp_check_mdt_count = 1;
4195 param.fp_exclude_mdt_count = !!neg_opt;
4203 if (pathstart == -1) {
4204 fprintf(stderr, "error: %s: no filename|pathname\n",
4208 } else if (pathend == -1) {
4214 rc = llapi_find(argv[pathstart], ¶m);
4215 if (rc != 0 && ret == 0)
4217 } while (++pathstart < pathend);
4220 fprintf(stderr, "error: %s failed for %s.\n",
4221 argv[0], argv[optind - 1]);
4223 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4224 free(param.fp_obd_uuid);
4226 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4227 free(param.fp_mdt_uuid);
4232 static int lfs_getstripe_internal(int argc, char **argv,
4233 struct find_param *param)
4235 struct option long_opts[] = {
4236 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4237 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4238 { .val = LFS_COMP_COUNT_OPT,
4239 .name = "comp-count", .has_arg = no_argument },
4240 { .val = LFS_COMP_COUNT_OPT,
4241 .name = "component-count", .has_arg = no_argument },
4242 { .val = LFS_COMP_FLAGS_OPT,
4243 .name = "comp-flags", .has_arg = optional_argument },
4244 { .val = LFS_COMP_FLAGS_OPT,
4245 .name = "component-flags", .has_arg = optional_argument },
4246 { .val = LFS_COMP_START_OPT,
4247 .name = "comp-start", .has_arg = optional_argument },
4248 { .val = LFS_COMP_START_OPT,
4249 .name = "component-start", .has_arg = optional_argument },
4250 { .val = LFS_MIRROR_INDEX_OPT,
4251 .name = "mirror-index", .has_arg = required_argument },
4252 { .val = LFS_MIRROR_ID_OPT,
4253 .name = "mirror-id", .has_arg = required_argument },
4254 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4255 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4256 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4257 { .val = 'd', .name = "directory", .has_arg = no_argument },
4258 { .val = 'D', .name = "default", .has_arg = no_argument },
4259 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4260 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4261 { .val = 'F', .name = "fid", .has_arg = no_argument },
4262 { .val = 'g', .name = "generation", .has_arg = no_argument },
4263 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4264 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4265 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4266 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4267 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4268 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4269 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
4270 { .val = 'L', .name = "layout", .has_arg = no_argument },
4271 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4272 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4273 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4274 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4275 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4276 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4277 { .val = 'O', .name = "obd", .has_arg = required_argument },
4278 { .val = 'O', .name = "ost", .has_arg = required_argument },
4279 { .val = 'p', .name = "pool", .has_arg = no_argument },
4280 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4281 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4282 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4283 { .val = 'R', .name = "raw", .has_arg = no_argument },
4284 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4285 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4286 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4287 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4288 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4289 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4290 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4291 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4295 int pathstart = -1, pathend = -1;
4299 while ((c = getopt_long(argc, argv,
4300 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4301 long_opts, NULL)) != -1) {
4305 /* '!' is part of option */
4306 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4307 if (!isoption && pathend != -1) {
4309 "error: %s: filename|dirname must either precede options or follow options\n",
4313 if (!isoption && pathstart == -1)
4314 pathstart = optind - 1;
4315 if (isoption && pathstart != -1 && pathend == -1)
4316 pathend = optind - 2;
4320 /* unknown: opt is "!" */
4321 if (strcmp(optarg, "!") == 0)
4325 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4326 param->fp_verbose |= VERBOSE_COUNT;
4327 param->fp_max_depth = 0;
4330 case LFS_COMP_COUNT_OPT:
4331 param->fp_verbose |= VERBOSE_COMP_COUNT;
4332 param->fp_max_depth = 0;
4334 case LFS_COMP_FLAGS_OPT:
4335 if (optarg != NULL) {
4336 rc = comp_str2flags(optarg,
4337 ¶m->fp_comp_flags,
4338 ¶m->fp_comp_neg_flags);
4340 fprintf(stderr, "error: %s bad "
4341 "component flags '%s'.\n",
4345 param->fp_check_comp_flags = 1;
4347 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4348 param->fp_max_depth = 0;
4351 case LFS_COMP_START_OPT:
4352 if (optarg != NULL) {
4354 if (tmp[0] == '+') {
4355 param->fp_comp_start_sign = -1;
4357 } else if (tmp[0] == '-') {
4358 param->fp_comp_start_sign = 1;
4361 rc = llapi_parse_size(tmp,
4362 ¶m->fp_comp_start,
4363 ¶m->fp_comp_start_units, 0);
4365 fprintf(stderr, "error: %s bad "
4366 "component start '%s'.\n",
4370 param->fp_check_comp_start = 1;
4373 param->fp_verbose |= VERBOSE_COMP_START;
4374 param->fp_max_depth = 0;
4377 case LFS_MIRROR_INDEX_OPT:
4378 if (optarg[0] == '+') {
4379 param->fp_mirror_index_sign = -1;
4381 } else if (optarg[0] == '-') {
4382 param->fp_mirror_index_sign = 1;
4386 param->fp_mirror_index = strtoul(optarg, &end, 0);
4387 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4388 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4390 "%s %s: invalid mirror index '%s'\n",
4391 progname, argv[0], optarg);
4394 if (param->fp_mirror_id != 0) {
4396 "%s %s: can't specify both mirror index and mirror ID\n",
4400 param->fp_check_mirror_index = 1;
4401 param->fp_exclude_mirror_index = !!neg_opt;
4403 case LFS_MIRROR_ID_OPT:
4404 if (optarg[0] == '+') {
4405 param->fp_mirror_id_sign = -1;
4407 } else if (optarg[0] == '-') {
4408 param->fp_mirror_id_sign = 1;
4412 param->fp_mirror_id = strtoul(optarg, &end, 0);
4413 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4414 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4416 "%s %s: invalid mirror ID '%s'\n",
4417 progname, argv[0], optarg);
4420 if (param->fp_mirror_index != 0) {
4422 "%s %s: can't specify both mirror index and mirror ID\n",
4426 param->fp_check_mirror_id = 1;
4427 param->fp_exclude_mirror_id = !!neg_opt;
4430 param->fp_max_depth = 0;
4433 param->fp_get_default_lmv = 1;
4436 if (optarg != NULL) {
4438 if (tmp[0] == '+') {
4439 param->fp_comp_end_sign = -1;
4441 } else if (tmp[0] == '-') {
4442 param->fp_comp_end_sign = 1;
4446 if (arg_is_eof(tmp)) {
4447 param->fp_comp_end = LUSTRE_EOF;
4448 param->fp_comp_end_units = 1;
4451 rc = llapi_parse_size(tmp,
4452 ¶m->fp_comp_end,
4453 ¶m->fp_comp_end_units, 0);
4456 fprintf(stderr, "error: %s bad "
4457 "component end '%s'.\n",
4461 param->fp_check_comp_end = 1;
4463 param->fp_verbose |= VERBOSE_COMP_END;
4464 param->fp_max_depth = 0;
4468 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4469 param->fp_verbose |= VERBOSE_DFID;
4470 param->fp_max_depth = 0;
4474 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4475 param->fp_verbose |= VERBOSE_GENERATION;
4476 param->fp_max_depth = 0;
4480 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4481 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4482 param->fp_max_depth = 0;
4486 if (optarg != NULL) {
4487 param->fp_comp_id = strtoul(optarg, &end, 0);
4488 if (*end != '\0' || param->fp_comp_id == 0 ||
4489 param->fp_comp_id > LCME_ID_MAX) {
4490 fprintf(stderr, "error: %s bad "
4491 "component id '%s'\n",
4495 param->fp_check_comp_id = 1;
4498 param->fp_max_depth = 0;
4499 param->fp_verbose |= VERBOSE_COMP_ID;
4503 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4504 param->fp_verbose |= VERBOSE_PATTERN;
4505 param->fp_max_depth = 0;
4508 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4510 fprintf(stderr, "warning: '-M' deprecated"
4511 ", use '--mdt-index' or '-m' instead\n");
4514 if (!(param->fp_verbose & VERBOSE_DETAIL))
4515 param->fp_max_depth = 0;
4516 param->fp_verbose |= VERBOSE_MDTINDEX;
4519 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4520 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4521 param->fp_max_depth = 0;
4525 if (param->fp_obd_uuid) {
4527 "error: %s: only one obduuid allowed",
4531 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4534 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4535 param->fp_verbose |= VERBOSE_POOL;
4536 param->fp_max_depth = 0;
4543 param->fp_recursive = 1;
4549 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4550 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4551 param->fp_max_depth = 0;
4555 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4565 if (pathstart == -1) {
4566 fprintf(stderr, "error: %s: no filename|pathname\n",
4569 } else if (pathend == -1) {
4577 if (param->fp_recursive)
4578 param->fp_max_depth = -1;
4579 else if (param->fp_verbose & VERBOSE_DETAIL)
4580 param->fp_max_depth = 1;
4582 if (!param->fp_verbose)
4583 param->fp_verbose = VERBOSE_DEFAULT;
4584 if (param->fp_quiet)
4585 param->fp_verbose = VERBOSE_OBJID;
4588 rc = llapi_getstripe(argv[pathstart], param);
4589 } while (++pathstart < pathend && !rc);
4592 fprintf(stderr, "error: %s failed for %s.\n",
4593 argv[0], argv[optind - 1]);
4597 static int lfs_tgts(int argc, char **argv)
4599 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4600 struct find_param param;
4601 int index = 0, rc=0;
4606 if (argc == 2 && !realpath(argv[1], path)) {
4608 fprintf(stderr, "error: invalid path '%s': %s\n",
4609 argv[1], strerror(-rc));
4613 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4614 /* Check if we have a mount point */
4615 if (mntdir[0] == '\0')
4618 memset(¶m, 0, sizeof(param));
4619 if (!strcmp(argv[0], "mdts"))
4620 param.fp_get_lmv = 1;
4622 rc = llapi_ostlist(mntdir, ¶m);
4624 fprintf(stderr, "error: %s: failed on %s\n",
4627 if (path[0] != '\0')
4629 memset(mntdir, 0, PATH_MAX);
4635 static int lfs_getstripe(int argc, char **argv)
4637 struct find_param param = { 0 };
4639 param.fp_max_depth = 1;
4640 return lfs_getstripe_internal(argc, argv, ¶m);
4644 static int lfs_getdirstripe(int argc, char **argv)
4646 struct find_param param = { 0 };
4647 struct option long_opts[] = {
4648 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4649 { .val = 'D', .name = "default", .has_arg = no_argument },
4650 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4651 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4652 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4653 { .val = 'O', .name = "obd", .has_arg = required_argument },
4654 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4655 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4656 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4660 param.fp_get_lmv = 1;
4662 while ((c = getopt_long(argc, argv,
4663 "cDHimO:rtTy", long_opts, NULL)) != -1)
4668 param.fp_verbose |= VERBOSE_COUNT;
4671 param.fp_get_default_lmv = 1;
4675 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4677 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4679 fprintf(stderr, "warning: '-t' deprecated, "
4680 "use '--mdt-hash' or '-H' instead\n");
4683 param.fp_verbose |= VERBOSE_HASH_TYPE;
4686 if (param.fp_obd_uuid) {
4688 "error: %s: only one obduuid allowed",
4692 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4695 param.fp_recursive = 1;
4708 if (param.fp_recursive)
4709 param.fp_max_depth = -1;
4711 if (!param.fp_verbose)
4712 param.fp_verbose = VERBOSE_DEFAULT;
4715 rc = llapi_getstripe(argv[optind], ¶m);
4716 } while (++optind < argc && !rc);
4719 fprintf(stderr, "error: %s failed for %s.\n",
4720 argv[0], argv[optind - 1]);
4725 MNTDF_INODES = 0x0001,
4726 MNTDF_COOKED = 0x0002,
4727 MNTDF_LAZY = 0x0004,
4728 MNTDF_VERBOSE = 0x0008,
4729 MNTDF_SHOW = 0x0010,
4732 #define COOK(value) \
4735 while (value > 1024) { \
4743 #define CDF "%11llu"
4744 #define HDF "%8.1f%c"
4748 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4750 double avail, used, ratio = 0;
4753 avail = st->os_ffree;
4754 used = st->os_files - st->os_ffree;
4756 avail = st->os_bavail;
4757 used = st->os_blocks - st->os_bfree;
4759 if (avail + used > 0)
4760 ratio = used / (used + avail) * 100;
4762 /* Round up to match df(1) usage percentage */
4763 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4766 /* This is to identify various problem states for "lfs df" if .osn_err = true,
4767 * so only show flags reflecting those states by default. Informational states
4768 * are only shown with "-v" and use lower-case names to distinguish them.
4769 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
4771 static struct obd_statfs_state_names {
4772 enum obd_statfs_state osn_state;
4773 const char osn_name;
4776 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
4777 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
4778 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
4779 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
4780 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
4781 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
4782 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
4783 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
4784 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
4787 static int showdf(char *mntdir, struct obd_statfs *stat,
4788 char *uuid, enum mntdf_flags flags,
4789 char *type, int index, int rc)
4791 long long avail, used, total;
4793 char *suffix = "KMGTPEZY";
4794 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4795 char tbuf[3 * sizeof(__u64)];
4796 char ubuf[3 * sizeof(__u64)];
4797 char abuf[3 * sizeof(__u64)];
4798 char rbuf[3 * sizeof(__u64)];
4805 if (flags & MNTDF_INODES) {
4806 avail = stat->os_ffree;
4807 used = stat->os_files - stat->os_ffree;
4808 total = stat->os_files;
4810 int shift = flags & MNTDF_COOKED ? 0 : 10;
4812 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4813 used = ((stat->os_blocks - stat->os_bfree) *
4814 stat->os_bsize) >> shift;
4815 total = (stat->os_blocks * stat->os_bsize) >> shift;
4818 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4820 if (flags & MNTDF_COOKED) {
4824 cook_val = (double)total;
4827 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4830 snprintf(tbuf, sizeof(tbuf), CDF, total);
4832 cook_val = (double)used;
4835 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4838 snprintf(ubuf, sizeof(ubuf), CDF, used);
4840 cook_val = (double)avail;
4843 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4846 snprintf(abuf, sizeof(abuf), CDF, avail);
4848 snprintf(tbuf, sizeof(tbuf), CDF, total);
4849 snprintf(ubuf, sizeof(tbuf), CDF, used);
4850 snprintf(abuf, sizeof(tbuf), CDF, avail);
4853 sprintf(rbuf, RDF, ratio);
4854 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4855 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4857 printf("[%s:%d]", type, index);
4859 if (stat->os_state) {
4863 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
4864 if (oss_names[i].osn_state & stat->os_state &&
4865 (oss_names[i].osn_err ||
4866 flags & MNTDF_VERBOSE))
4867 printf("%c", oss_names[i].osn_name);
4874 printf(UUF": inactive device\n", uuid);
4877 printf(UUF": %s\n", uuid, strerror(-rc));
4884 struct ll_stat_type {
4889 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4891 struct ll_statfs_data {
4893 struct obd_statfs sd_st;
4896 struct ll_statfs_buf {
4898 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4901 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4902 int ops, struct ll_statfs_buf *lsb)
4904 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4905 struct obd_uuid uuid_buf;
4906 char *poolname = NULL;
4907 struct ll_stat_type types[] = {
4908 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4909 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4910 { .st_name = NULL } };
4911 struct ll_stat_type *tp;
4912 __u64 ost_files = 0;
4913 __u64 ost_ffree = 0;
4921 poolname = strchr(pool, '.');
4922 if (poolname != NULL) {
4923 if (strncmp(fsname, pool, strlen(fsname))) {
4924 fprintf(stderr, "filesystem name incorrect\n");
4932 fd = open(mntdir, O_RDONLY);
4935 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4940 if (flags & MNTDF_SHOW) {
4941 if (flags & MNTDF_INODES)
4942 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4943 "UUID", "Inodes", "IUsed", "IFree",
4944 "IUse%", "Mounted on");
4946 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4948 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4949 "Used", "Available", "Use%", "Mounted on");
4952 for (tp = types; tp->st_name != NULL; tp++) {
4953 bool have_ost = false;
4955 if (!(tp->st_op & ops))
4958 for (index = 0; ; index++) {
4959 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4960 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4961 type = flags & MNTDF_LAZY ?
4962 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4963 rc2 = llapi_obd_fstatfs(fd, type, index,
4964 &stat_buf, &uuid_buf);
4969 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4970 if (!(flags & MNTDF_VERBOSE))
4972 } else if (rc2 < 0 && rc == 0) {
4976 /* If we have OSTs then don't report MDT block counts.
4977 * For MDT-only filesystems the expectation is that all
4978 * layouts have a DoM component. For filesystems with
4979 * OSTs, files are not necessarily going to store data
4980 * on MDTs, and MDT space is limited to a fraction of
4981 * OST space, so don't include it in the summary.
4983 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
4990 if (poolname && tp->st_op == LL_STATFS_LOV &&
4991 llapi_search_ost(fsname, poolname,
4992 obd_uuid2str(&uuid_buf)) != 1)
4995 /* the llapi_obd_statfs() call may have returned with
4996 * an error, but if it filled in uuid_buf we will at
4997 * lease use that to print out a message for that OBD.
4998 * If we didn't get anything in the uuid_buf, then fill
4999 * it in so that we can print an error message. */
5000 if (uuid_buf.uuid[0] == '\0')
5001 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5002 "%s%04x", tp->st_name, index);
5004 lsb->sb_buf[lsb->sb_count].sd_index = index;
5005 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5008 if (flags & MNTDF_SHOW)
5009 showdf(mntdir, &stat_buf,
5010 obd_uuid2str(&uuid_buf), flags,
5011 tp->st_name, index, rc2);
5016 if (tp->st_op == LL_STATFS_LMV) {
5017 sum.os_ffree += stat_buf.os_ffree;
5018 sum.os_files += stat_buf.os_files;
5019 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5020 ost_files += stat_buf.os_files;
5021 ost_ffree += stat_buf.os_ffree;
5023 sum.os_blocks += stat_buf.os_blocks *
5025 sum.os_bfree += stat_buf.os_bfree *
5027 sum.os_bavail += stat_buf.os_bavail *
5034 /* If we have _some_ OSTs, but don't have as many free objects on the
5035 * OST as inodes on the MDTs, reduce the reported number of inodes
5036 * to compensate, so that the "inodes in use" number is correct.
5037 * This should be kept in sync with ll_statfs_internal().
5039 if (ost_files && ost_ffree < sum.os_ffree) {
5040 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5041 sum.os_ffree = ost_ffree;
5043 if (flags & MNTDF_SHOW) {
5045 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5052 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5054 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5056 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5058 int r1 = obd_statfs_ratio(st1, false);
5059 int r2 = obd_statfs_ratio(st2, false);
5060 int64_t result = r1 - r2;
5062 /* if both space usage are above 90, compare free inodes */
5063 if (r1 > 90 && r2 > 90)
5064 result = st2->os_ffree - st1->os_ffree;
5068 else if (result == 0)
5075 static int lfs_setdirstripe(int argc, char **argv)
5079 struct lfs_setstripe_args lsa = { 0 };
5080 struct llapi_stripe_param *param = NULL;
5081 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5084 char *mode_opt = NULL;
5085 bool default_stripe = false;
5086 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5087 mode_t previous_mode = 0;
5088 bool delete = false;
5089 struct ll_statfs_buf *lsb = NULL;
5090 char mntdir[PATH_MAX] = "";
5091 bool auto_distributed = false;
5093 struct option long_opts[] = {
5094 { .val = 'c', .name = "count", .has_arg = required_argument },
5095 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5096 { .val = 'd', .name = "delete", .has_arg = no_argument },
5097 { .val = 'D', .name = "default", .has_arg = no_argument },
5098 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5099 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5100 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5101 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5102 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5103 { .val = 'i', .name = "index", .has_arg = required_argument },
5105 { .val = 'o', .name = "mode", .has_arg = required_argument },
5106 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5107 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5109 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5110 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5113 setstripe_args_init(&lsa);
5115 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
5123 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5126 "%s %s: invalid stripe count '%s'\n",
5127 progname, argv[0], optarg);
5133 default_stripe = true;
5136 default_stripe = true;
5138 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5140 fprintf(stderr, "warning: '--hash-type' and '-t' "
5141 "deprecated, use '--mdt-hash' or '-H' instead\n");
5144 lsa.lsa_pattern = check_hashtype(optarg);
5145 if (lsa.lsa_pattern == 0) {
5147 "%s %s: bad stripe hash type '%s'\n",
5148 progname, argv[0], optarg);
5153 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5154 if (strcmp(argv[optind - 1], "--index") == 0)
5156 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5159 lsa.lsa_nr_tgts = parse_targets(mdts,
5160 sizeof(mdts) / sizeof(__u32),
5161 lsa.lsa_nr_tgts, optarg);
5162 if (lsa.lsa_nr_tgts < 0) {
5164 "%s %s: invalid MDT target(s) '%s'\n",
5165 progname, argv[0], optarg);
5169 lsa.lsa_tgts = mdts;
5170 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5171 lsa.lsa_stripe_off = mdts[0];
5173 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
5175 fprintf(stderr, "warning: '-m' is deprecated, "
5176 "use '--mode' or '-o' instead\n");
5182 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5183 progname, argv[0], argv[optind - 1]);
5188 if (optind == argc) {
5189 fprintf(stderr, "%s %s: DIR must be specified\n",
5194 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5195 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5197 "%s %s: stripe offset and count must be specified\n",
5203 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5204 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5206 "%s %s: cannot specify -d with -c or -i options\n",
5211 if (mode_opt != NULL) {
5212 mode = strtoul(mode_opt, &end, 8);
5215 "%s %s: bad MODE '%s'\n",
5216 progname, argv[0], mode_opt);
5219 previous_mode = umask(0);
5223 * initialize stripe parameters, in case param is converted to specific,
5224 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5226 param = calloc(1, offsetof(typeof(*param),
5227 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5228 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5229 if (param == NULL) {
5231 "%s %s: cannot allocate memory for parameters: %s\n",
5232 progname, argv[0], strerror(ENOMEM));
5236 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5237 param->lsp_stripe_count = lsa.lsa_stripe_count;
5238 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5239 param->lsp_stripe_offset = -1;
5241 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5242 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5243 param->lsp_stripe_pattern = lsa.lsa_pattern;
5245 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5246 param->lsp_pool = lsa.lsa_pool_name;
5247 param->lsp_is_specific = false;
5248 if (lsa.lsa_nr_tgts > 1) {
5249 if (lsa.lsa_stripe_count > 0 &&
5250 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5251 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5253 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
5254 argv[0], lsa.lsa_stripe_count,
5260 param->lsp_is_specific = true;
5261 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5262 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5265 dname = argv[optind];
5267 if (default_stripe) {
5268 result = llapi_dir_set_default_lmv(dname, param);
5270 /* if current \a dname isn't under the same \a mntdir
5271 * as the last one, and the last one was
5272 * auto-distributed, restore \a param.
5274 if (mntdir[0] != '\0' &&
5275 strncmp(dname, mntdir, strlen(mntdir)) &&
5277 param->lsp_is_specific = false;
5278 param->lsp_stripe_offset = -1;
5279 auto_distributed = false;
5282 if (!param->lsp_is_specific &&
5283 param->lsp_stripe_offset == -1) {
5284 char path[PATH_MAX] = "";
5287 lsb = malloc(sizeof(*lsb));
5295 /* use mntdir for dirname() temporarily */
5296 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5297 if (!realpath(dirname(mntdir), path)) {
5300 "error: invalid path '%s': %s\n",
5301 argv[optind], strerror(errno));
5306 result = llapi_search_mounts(path, 0, mntdir,
5308 if (result < 0 || mntdir[0] == '\0') {
5310 "No suitable Lustre mount found\n");
5314 result = mntdf(mntdir, NULL, NULL, 0,
5315 LL_STATFS_LMV, lsb);
5319 if (param->lsp_stripe_count > lsb->sb_count) {
5321 "error: stripe count %d is too big\n",
5322 param->lsp_stripe_count);
5327 qsort(lsb->sb_buf, lsb->sb_count,
5328 sizeof(struct ll_statfs_data),
5329 ll_statfs_data_comp);
5331 auto_distributed = true;
5334 if (auto_distributed) {
5336 int nr = MAX(param->lsp_stripe_count,
5339 /* don't use server whose usage is above 90% */
5340 while (nr != param->lsp_stripe_count &&
5341 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5343 nr = MAX(param->lsp_stripe_count,
5346 /* get \a r between [0, nr) */
5349 param->lsp_stripe_offset =
5350 lsb->sb_buf[r].sd_index;
5351 if (param->lsp_stripe_count > 1) {
5354 param->lsp_is_specific = true;
5355 for (; i < param->lsp_stripe_count; i++)
5356 param->lsp_tgts[(i + r) % nr] =
5357 lsb->sb_buf[i].sd_index;
5361 result = llapi_dir_create(dname, mode, param);
5366 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5367 progname, dname, strerror(-result));
5370 dname = argv[++optind];
5371 } while (dname != NULL);
5373 if (mode_opt != NULL)
5374 umask(previous_mode);
5382 static int lfs_rmentry(int argc, char **argv)
5389 fprintf(stderr, "error: %s: missing dirname\n",
5395 dname = argv[index];
5396 while (dname != NULL) {
5397 result = llapi_direntry_remove(dname);
5399 fprintf(stderr, "error: %s: remove dir entry '%s' "
5400 "failed\n", argv[0], dname);
5403 dname = argv[++index];
5408 static int lfs_mv(int argc, char **argv)
5410 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5411 struct find_param param = {
5418 struct option long_opts[] = {
5419 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5420 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5423 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5425 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5427 fprintf(stderr, "warning: '-M' deprecated"
5428 ", use '--mdt-index' or '-m' instead\n");
5431 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5433 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5439 param.fp_verbose = VERBOSE_DETAIL;
5442 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5443 progname, argv[optind - 1]);
5448 if (lmu.lum_stripe_offset == -1) {
5449 fprintf(stderr, "%s mv: MDT index must be specified\n",
5454 if (optind >= argc) {
5455 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5460 /* initialize migrate mdt parameters */
5461 param.fp_lmv_md = &lmu;
5462 param.fp_migrate = 1;
5463 rc = llapi_migrate_mdt(argv[optind], ¶m);
5465 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5466 progname, argv[optind], param.fp_mdt_index,
5471 static int lfs_osts(int argc, char **argv)
5473 return lfs_tgts(argc, argv);
5476 static int lfs_mdts(int argc, char **argv)
5478 return lfs_tgts(argc, argv);
5481 static int lfs_df(int argc, char **argv)
5483 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5484 enum mntdf_flags flags = MNTDF_SHOW;
5485 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5486 int c, rc = 0, index = 0;
5487 char fsname[PATH_MAX] = "", *pool_name = NULL;
5488 struct option long_opts[] = {
5489 { .val = 'h', .name = "human-readable",
5490 .has_arg = no_argument },
5491 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5492 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5493 { .val = 'p', .name = "pool", .has_arg = required_argument },
5494 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5497 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5500 flags |= MNTDF_COOKED;
5503 flags |= MNTDF_INODES;
5506 flags |= MNTDF_LAZY;
5512 flags |= MNTDF_VERBOSE;
5518 if (optind < argc && !realpath(argv[optind], path)) {
5520 fprintf(stderr, "error: invalid path '%s': %s\n",
5521 argv[optind], strerror(-rc));
5525 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5526 /* Check if we have a mount point */
5527 if (mntdir[0] == '\0')
5530 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5531 if (rc || path[0] != '\0')
5533 fsname[0] = '\0'; /* avoid matching in next loop */
5534 mntdir[0] = '\0'; /* avoid matching in next loop */
5540 static int lfs_getname(int argc, char **argv)
5542 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5543 int rc = 0, index = 0, c;
5544 char buf[sizeof(struct obd_uuid)];
5546 while ((c = getopt(argc, argv, "h")) != -1)
5549 if (optind == argc) { /* no paths specified, get all paths. */
5550 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5551 rc = llapi_getname(mntdir, buf, sizeof(buf));
5554 "cannot get name for `%s': %s\n",
5555 mntdir, strerror(-rc));
5559 printf("%s %s\n", buf, mntdir);
5561 path[0] = fsname[0] = mntdir[0] = 0;
5563 } else { /* paths specified, only attempt to search these. */
5564 for (; optind < argc; optind++) {
5565 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5568 "cannot get name for `%s': %s\n",
5569 argv[optind], strerror(-rc));
5573 printf("%s %s\n", buf, argv[optind]);
5579 static int lfs_check(int argc, char **argv)
5582 char mntdir[PATH_MAX] = {'\0'};
5589 fprintf(stderr, "%s check: server type must be specified\n",
5594 obd_types[0] = obd_type1;
5595 obd_types[1] = obd_type2;
5597 if (strcmp(argv[1], "osts") == 0) {
5598 strcpy(obd_types[0], "osc");
5599 } else if (strcmp(argv[1], "mds") == 0) {
5600 strcpy(obd_types[0], "mdc");
5601 } else if (strcmp(argv[1], "servers") == 0) {
5603 strcpy(obd_types[0], "osc");
5604 strcpy(obd_types[1], "mdc");
5606 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5611 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5612 if (rc < 0 || mntdir[0] == '\0') {
5614 "%s check: cannot find mounted Lustre filesystem: %s\n",
5615 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5619 rc = llapi_target_check(num_types, obd_types, mntdir);
5621 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5622 progname, argv[1], strerror(-rc));
5628 #ifdef HAVE_SYS_QUOTA_H
5629 #define ARG2INT(nr, str, msg) \
5632 nr = strtol(str, &endp, 0); \
5633 if (*endp != '\0') { \
5634 fprintf(stderr, "%s: bad %s '%s'\n", \
5635 progname, msg, str); \
5640 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5642 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5643 * returns the value or ULONG_MAX on integer overflow or incorrect format
5645 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5646 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5647 * 3. empty integer value is interpreted as 0
5649 static unsigned long str2sec(const char* timestr)
5651 const char spec[] = "smhdw";
5652 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5653 unsigned long val = 0;
5656 if (strpbrk(timestr, spec) == NULL) {
5657 /* no specifiers inside the time string,
5658 should treat it as an integer value */
5659 val = strtoul(timestr, &tail, 10);
5660 return *tail ? ULONG_MAX : val;
5663 /* format string is XXwXXdXXhXXmXXs */
5669 v = strtoul(timestr, &tail, 10);
5670 if (v == ULONG_MAX || *tail == '\0')
5671 /* value too large (ULONG_MAX or more)
5672 or missing specifier */
5675 ptr = strchr(spec, *tail);
5677 /* unknown specifier */
5682 /* check if product will overflow the type */
5683 if (!(v < ULONG_MAX / mult[ind]))
5686 ADD_OVERFLOW(val, mult[ind] * v);
5687 if (val == ULONG_MAX)
5699 #define ARG2ULL(nr, str, def_units) \
5701 unsigned long long limit, units = def_units; \
5704 rc = llapi_parse_size(str, &limit, &units, 1); \
5706 fprintf(stderr, "%s: invalid limit '%s'\n", \
5713 static inline int has_times_option(int argc, char **argv)
5717 for (i = 1; i < argc; i++)
5718 if (!strcmp(argv[i], "-t"))
5724 int lfs_setquota_times(int argc, char **argv)
5727 struct if_quotactl qctl;
5728 char *mnt, *obd_type = (char *)qctl.obd_type;
5729 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5730 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5731 struct option long_opts[] = {
5732 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5733 { .val = 'g', .name = "group", .has_arg = no_argument },
5734 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5735 { .val = 'p', .name = "projid", .has_arg = no_argument },
5736 { .val = 't', .name = "times", .has_arg = no_argument },
5737 { .val = 'u', .name = "user", .has_arg = no_argument },
5741 memset(&qctl, 0, sizeof(qctl));
5742 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5743 qctl.qc_type = ALLQUOTA;
5745 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5746 long_opts, NULL)) != -1) {
5757 if (qctl.qc_type != ALLQUOTA) {
5758 fprintf(stderr, "error: -u/g/p can't be used "
5759 "more than once\n");
5762 qctl.qc_type = qtype;
5765 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5766 fprintf(stderr, "error: bad block-grace: %s\n",
5770 dqb->dqb_valid |= QIF_BTIME;
5773 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5774 fprintf(stderr, "error: bad inode-grace: %s\n",
5778 dqb->dqb_valid |= QIF_ITIME;
5780 case 't': /* Yes, of course! */
5782 default: /* getopt prints error message for us when opterr != 0 */
5787 if (qctl.qc_type == ALLQUOTA) {
5788 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5792 if (optind != argc - 1) {
5793 fprintf(stderr, "error: unexpected parameters encountered\n");
5797 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5798 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5799 fprintf(stderr, "error: grace time is too large\n");
5804 rc = llapi_quotactl(mnt, &qctl);
5807 fprintf(stderr, "%s %s ", obd_type,
5808 obd_uuid2str(&qctl.obd_uuid));
5809 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5816 #define BSLIMIT (1 << 0)
5817 #define BHLIMIT (1 << 1)
5818 #define ISLIMIT (1 << 2)
5819 #define IHLIMIT (1 << 3)
5821 int lfs_setquota(int argc, char **argv)
5824 struct if_quotactl qctl;
5825 char *mnt, *obd_type = (char *)qctl.obd_type;
5826 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5827 struct option long_opts[] = {
5828 { .val = 'b', .name = "block-softlimit",
5829 .has_arg = required_argument },
5830 { .val = 'B', .name = "block-hardlimit",
5831 .has_arg = required_argument },
5832 { .val = 'd', .name = "default", .has_arg = no_argument },
5833 { .val = 'g', .name = "group", .has_arg = required_argument },
5834 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5835 { .val = 'i', .name = "inode-softlimit",
5836 .has_arg = required_argument },
5837 { .val = 'I', .name = "inode-hardlimit",
5838 .has_arg = required_argument },
5839 { .val = 'p', .name = "projid", .has_arg = required_argument },
5840 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5841 { .val = 'u', .name = "user", .has_arg = required_argument },
5842 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5844 unsigned limit_mask = 0;
5846 bool use_default = false;
5849 if (has_times_option(argc, argv))
5850 return lfs_setquota_times(argc, argv);
5852 memset(&qctl, 0, sizeof(qctl));
5853 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5854 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5855 * so it can be used as a marker that qc_type
5856 * isn't reinitialized from command line */
5858 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5859 long_opts, NULL)) != -1) {
5862 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5865 goto quota_type_def;
5868 rc = name2uid(&qctl.qc_id, optarg);
5871 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5874 goto quota_type_def;
5877 rc = name2gid(&qctl.qc_id, optarg);
5880 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5883 goto quota_type_def;
5886 rc = name2projid(&qctl.qc_id, optarg);
5889 qctl.qc_id = strtoul(optarg, &endptr, 10);
5890 if (*endptr != '\0') {
5891 fprintf(stderr, "%s setquota: invalid"
5892 " id '%s'\n", progname, optarg);
5897 if (qctl.qc_id == 0) {
5898 fprintf(stderr, "%s setquota: can't set quota"
5899 " for root usr/group/project.\n",
5905 if (qctl.qc_type != ALLQUOTA) {
5907 "%s setquota: only one of -u, -U, -g,"
5908 " -G, -p or -P may be specified\n",
5912 qctl.qc_type = qtype;
5915 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5919 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5920 dqb->dqb_bsoftlimit >>= 10;
5921 limit_mask |= BSLIMIT;
5922 if (dqb->dqb_bsoftlimit &&
5923 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5925 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5926 "See '%s help setquota' or Lustre manual for details\n",
5928 (unsigned long long)dqb->dqb_bsoftlimit,
5932 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5933 dqb->dqb_bhardlimit >>= 10;
5934 limit_mask |= BHLIMIT;
5935 if (dqb->dqb_bhardlimit &&
5936 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5938 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5939 "See '%s help setquota' or Lustre manual for details\n",
5941 (unsigned long long)dqb->dqb_bhardlimit,
5945 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5946 limit_mask |= ISLIMIT;
5947 if (dqb->dqb_isoftlimit &&
5948 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5950 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5951 "See '%s help setquota' or Lustre manual for details\n",
5953 (unsigned long long)dqb->dqb_isoftlimit,
5957 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5958 limit_mask |= IHLIMIT;
5959 if (dqb->dqb_ihardlimit &&
5960 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5962 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5963 "See '%s help setquota' or Lustre manual for details\n",
5965 (unsigned long long)dqb->dqb_ihardlimit,
5970 "%s setquota: unrecognized option '%s'\n",
5971 progname, argv[optind - 1]);
5976 if (qctl.qc_type == ALLQUOTA) {
5978 "%s setquota: either -u or -g must be specified\n",
5983 if (!use_default && limit_mask == 0) {
5985 "%s setquota: at least one limit must be specified\n",
5990 if (use_default && limit_mask != 0) {
5992 "%s setquota: limits should not be specified when"
5993 " using default quota\n",
5998 if (use_default && qctl.qc_id == 0) {
6000 "%s setquota: can not set default quota for root"
6001 " user/group/project\n",
6006 if (optind != argc - 1) {
6008 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6009 progname, argv[optind]);
6016 dqb->dqb_bhardlimit = 0;
6017 dqb->dqb_bsoftlimit = 0;
6018 dqb->dqb_ihardlimit = 0;
6019 dqb->dqb_isoftlimit = 0;
6022 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6023 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6024 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6025 /* sigh, we can't just set blimits/ilimits */
6026 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6027 .qc_type = qctl.qc_type,
6028 .qc_id = qctl.qc_id};
6030 rc = llapi_quotactl(mnt, &tmp_qctl);
6034 if (!(limit_mask & BHLIMIT))
6035 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6036 if (!(limit_mask & BSLIMIT))
6037 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6038 if (!(limit_mask & IHLIMIT))
6039 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6040 if (!(limit_mask & ISLIMIT))
6041 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6043 /* Keep grace times if we have got no softlimit arguments */
6044 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6045 dqb->dqb_valid |= QIF_BTIME;
6046 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6049 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6050 dqb->dqb_valid |= QIF_ITIME;
6051 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6055 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6056 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6058 rc = llapi_quotactl(mnt, &qctl);
6062 "%s setquota: cannot quotactl '%s' '%s': %s",
6064 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6071 /* Converts seconds value into format string
6072 * result is returned in buf
6074 * 1. result is in descenting order: 1w2d3h4m5s
6075 * 2. zero fields are not filled (except for p. 3): 5d1s
6076 * 3. zero seconds value is presented as "0s"
6078 static char * __sec2str(time_t seconds, char *buf)
6080 const char spec[] = "smhdw";
6081 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6086 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6087 c = seconds / mult[i];
6089 if (c > 0 || (i == 0 && buf == tail))
6090 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6098 static void sec2str(time_t seconds, char *buf, int rc)
6105 tail = __sec2str(seconds, tail);
6107 if (rc && tail - buf < 39) {
6113 static void diff2str(time_t seconds, char *buf, time_t now)
6119 if (seconds <= now) {
6120 strcpy(buf, "none");
6123 __sec2str(seconds - now, buf);
6126 static void print_quota_title(char *name, struct if_quotactl *qctl,
6127 bool human_readable, bool show_default)
6130 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6131 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6132 "Filesystem", "bquota", "blimit", "bgrace",
6133 "iquota", "ilimit", "igrace");
6135 printf("Disk quotas for %s %s (%cid %u):\n",
6136 qtype_name(qctl->qc_type), name,
6137 *qtype_name(qctl->qc_type), qctl->qc_id);
6138 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6139 "Filesystem", human_readable ? "used" : "kbytes",
6140 "quota", "limit", "grace",
6141 "files", "quota", "limit", "grace");
6145 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6148 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6151 snprintf(buf, buflen, "%5.4gP",
6152 (double)num / ((__u64)1 << 40));
6154 snprintf(buf, buflen, "%5.4gT",
6155 (double)num / (1 << 30));
6157 snprintf(buf, buflen, "%5.4gG",
6158 (double)num / (1 << 20));
6160 snprintf(buf, buflen, "%5.4gM",
6161 (double)num / (1 << 10));
6163 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6167 #define STRBUF_LEN 32
6168 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6169 int rc, bool h, bool show_default)
6175 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6176 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6177 int bover = 0, iover = 0;
6178 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6179 char numbuf[3][STRBUF_LEN];
6181 char strbuf[STRBUF_LEN];
6183 if (dqb->dqb_bhardlimit &&
6184 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6186 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6187 if (dqb->dqb_btime > now) {
6194 if (dqb->dqb_ihardlimit &&
6195 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6197 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6198 if (dqb->dqb_itime > now) {
6206 if (strlen(mnt) > 15)
6207 printf("%s\n%15s", mnt, "");
6209 printf("%15s", mnt);
6212 diff2str(dqb->dqb_btime, timebuf, now);
6213 else if (show_default)
6214 snprintf(timebuf, sizeof(timebuf), "%llu",
6215 (unsigned long long)dqb->dqb_btime);
6217 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6218 strbuf, sizeof(strbuf), h);
6219 if (rc == -EREMOTEIO)
6220 sprintf(numbuf[0], "%s*", strbuf);
6222 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6223 "%s" : "[%s]", strbuf);
6225 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6226 if (type == QC_GENERAL)
6227 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6228 "%s" : "[%s]", strbuf);
6230 sprintf(numbuf[1], "%s", "-");
6232 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6233 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6234 "%s" : "[%s]", strbuf);
6237 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6239 printf(" %7s%c %6s %7s %7s",
6240 numbuf[0], bover ? '*' : ' ', numbuf[1],
6241 numbuf[2], bover > 1 ? timebuf : "-");
6245 diff2str(dqb->dqb_itime, timebuf, now);
6246 else if (show_default)
6247 snprintf(timebuf, sizeof(timebuf), "%llu",
6248 (unsigned long long)dqb->dqb_itime);
6250 snprintf(numbuf[0], sizeof(numbuf),
6251 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6252 (uintmax_t)dqb->dqb_curinodes);
6254 if (type == QC_GENERAL)
6255 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6257 (uintmax_t)dqb->dqb_isoftlimit);
6259 sprintf(numbuf[1], "%s", "-");
6261 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6262 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6265 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6266 else if (type != QC_OSTIDX)
6267 printf(" %7s%c %6s %7s %7s",
6268 numbuf[0], iover ? '*' : ' ', numbuf[1],
6269 numbuf[2], iover > 1 ? timebuf : "-");
6271 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6274 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6275 qctl->qc_cmd == Q_GETOINFO) {
6279 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6280 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6281 printf("Block grace time: %s; Inode grace time: %s\n",
6282 bgtimebuf, igtimebuf);
6286 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6287 bool h, __u64 *total)
6289 int rc = 0, rc1 = 0, count = 0;
6290 __u32 valid = qctl->qc_valid;
6292 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6294 fprintf(stderr, "can not get %s count: %s\n",
6295 is_mdt ? "mdt": "ost", strerror(-rc));
6299 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6300 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6301 rc = llapi_quotactl(mnt, qctl);
6303 /* It is remote client case. */
6304 if (rc == -EOPNOTSUPP) {
6311 fprintf(stderr, "quotactl %s%d failed.\n",
6312 is_mdt ? "mdt": "ost", qctl->qc_idx);
6316 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6317 qctl->qc_valid, 0, h, false);
6318 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6319 qctl->qc_dqblk.dqb_bhardlimit;
6322 qctl->qc_valid = valid;
6326 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6327 int verbose, int quiet, bool human_readable,
6330 int rc1 = 0, rc2 = 0, rc3 = 0;
6331 char *obd_type = (char *)qctl->obd_type;
6332 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6333 __u64 total_ialloc = 0, total_balloc = 0;
6334 bool use_default_for_blk = false;
6335 bool use_default_for_file = false;
6338 rc1 = llapi_quotactl(mnt, qctl);
6342 fprintf(stderr, "%s quotas are not enabled.\n",
6343 qtype_name(qctl->qc_type));
6346 fprintf(stderr, "Permission denied.\n");
6349 /* We already got error message. */
6352 fprintf(stderr, "Unexpected quotactl error: %s\n",
6357 if (!show_default && qctl->qc_id == 0) {
6358 qctl->qc_dqblk.dqb_bhardlimit = 0;
6359 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6360 qctl->qc_dqblk.dqb_ihardlimit = 0;
6361 qctl->qc_dqblk.dqb_isoftlimit = 0;
6362 qctl->qc_dqblk.dqb_btime = 0;
6363 qctl->qc_dqblk.dqb_itime = 0;
6364 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6367 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6368 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6369 use_default_for_blk = true;
6370 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6373 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6374 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6375 use_default_for_file = true;
6376 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6379 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6380 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6381 print_quota_title(name, qctl, human_readable, show_default);
6383 if (rc1 && *obd_type)
6384 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6386 if (qctl->qc_valid != QC_GENERAL)
6389 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6390 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6391 (QIF_LIMITS|QIF_USAGE));
6393 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6395 if (!show_default && verbose &&
6396 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6397 char strbuf[STRBUF_LEN];
6399 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6401 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6403 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6405 printf("Total allocated inode limit: %ju, total "
6406 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6410 if (use_default_for_blk)
6411 printf("%cid %u is using default block quota setting\n",
6412 *qtype_name(qctl->qc_type), qctl->qc_id);
6414 if (use_default_for_file)
6415 printf("%cid %u is using default file quota setting\n",
6416 *qtype_name(qctl->qc_type), qctl->qc_id);
6418 if (rc1 || rc2 || rc3 || inacc)
6419 printf("Some errors happened when getting quota info. "
6420 "Some devices may be not working or deactivated. "
6421 "The data in \"[]\" is inaccurate.\n");
6427 static int lfs_project(int argc, char **argv)
6429 int ret = 0, err = 0, c, i;
6430 struct project_handle_control phc = { 0 };
6431 enum lfs_project_ops_t op;
6434 phc.assign_projid = false;
6435 /* default action */
6436 op = LFS_PROJECT_LIST;
6438 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6441 if (op != LFS_PROJECT_LIST) {
6443 "%s: cannot specify '-c' '-C' '-s' together\n",
6448 op = LFS_PROJECT_CHECK;
6451 if (op != LFS_PROJECT_LIST) {
6453 "%s: cannot specify '-c' '-C' '-s' together\n",
6458 op = LFS_PROJECT_CLEAR;
6461 if (op != LFS_PROJECT_LIST) {
6463 "%s: cannot specify '-c' '-C' '-s' together\n",
6468 phc.set_inherit = true;
6469 op = LFS_PROJECT_SET;
6475 phc.keep_projid = true;
6478 phc.recursive = true;
6481 phc.projid = strtoul(optarg, NULL, 0);
6482 phc.assign_projid = true;
6486 phc.newline = false;
6489 fprintf(stderr, "%s: invalid option '%c'\n",
6495 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6496 op = LFS_PROJECT_SET;
6497 phc.set_projid = true;
6498 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6499 phc.set_projid = true;
6503 case LFS_PROJECT_CHECK:
6504 if (phc.keep_projid) {
6506 "%s: '-k' is useless together with '-c'\n",
6511 case LFS_PROJECT_CLEAR:
6514 "%s: '-0' is useless together with '-C'\n",
6518 if (phc.assign_projid) {
6520 "%s: '-p' is useless together with '-C'\n",
6525 case LFS_PROJECT_SET:
6528 "%s: '-0' is useless together with '-s'\n",
6532 if (phc.keep_projid) {
6534 "%s: '-k' is useless together with '-s'\n",
6542 "%s: '-0' is useless for list operations\n",
6552 fprintf(stderr, "%s: missing file or directory target(s)\n",
6557 for (i = 0; i < argc; i++) {
6559 case LFS_PROJECT_CHECK:
6560 err = lfs_project_check(argv[i], &phc);
6562 case LFS_PROJECT_LIST:
6563 err = lfs_project_list(argv[i], &phc);
6565 case LFS_PROJECT_CLEAR:
6566 err = lfs_project_clear(argv[i], &phc);
6568 case LFS_PROJECT_SET:
6569 err = lfs_project_set(argv[i], &phc);
6581 static int lfs_quota(int argc, char **argv)
6584 char *mnt, *name = NULL;
6585 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6586 .qc_type = ALLQUOTA };
6587 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6588 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6590 __u32 valid = QC_GENERAL, idx = 0;
6591 bool human_readable = false;
6592 bool show_default = false;
6595 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6598 show_default = true;
6603 show_default = true;
6608 show_default = true;
6612 if (qctl.qc_type != ALLQUOTA) {
6614 "%s quota: only one of -u, -g, or -p may be specified\n",
6618 qctl.qc_type = qtype;
6621 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6624 valid = qctl.qc_valid = QC_UUID;
6625 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6628 valid = qctl.qc_valid = QC_MDTIDX;
6629 idx = qctl.qc_idx = atoi(optarg);
6630 if (idx == 0 && *optarg != '0') {
6632 "%s quota: invalid MDT index '%s'\n",
6638 valid = qctl.qc_valid = QC_OSTIDX;
6639 idx = qctl.qc_idx = atoi(optarg);
6640 if (idx == 0 && *optarg != '0') {
6642 "%s quota: invalid OST index '%s'\n",
6654 human_readable = true;
6657 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6658 progname, argv[optind - 1]);
6663 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6664 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6665 optind == argc - 1 && !show_default) {
6667 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6668 qctl.qc_valid = valid;
6671 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6672 qctl.qc_type = qtype;
6673 if (qtype == USRQUOTA) {
6674 qctl.qc_id = geteuid();
6675 rc = uid2name(&name, qctl.qc_id);
6677 qctl.qc_id = getegid();
6678 rc = gid2name(&name, qctl.qc_id);
6683 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6684 human_readable, show_default);
6689 /* lfs quota -u username /path/to/lustre/mount */
6690 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6691 /* options should be followed by u/g-name and mntpoint */
6692 if ((!show_default && optind + 2 != argc) ||
6693 (show_default && optind + 1 != argc) ||
6694 qctl.qc_type == ALLQUOTA) {
6696 "%s quota: name and mount point must be specified\n",
6701 if (!show_default) {
6702 name = argv[optind++];
6703 switch (qctl.qc_type) {
6705 rc = name2uid(&qctl.qc_id, name);
6708 rc = name2gid(&qctl.qc_id, name);
6711 rc = name2projid(&qctl.qc_id, name);
6718 qctl.qc_valid = QC_GENERAL;
6719 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6724 qctl.qc_id = strtoul(name, &endptr, 10);
6725 if (*endptr != '\0') {
6726 fprintf(stderr, "%s quota: invalid id '%s'\n",
6731 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6732 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6738 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6739 human_readable, show_default);
6742 #endif /* HAVE_SYS_QUOTA_H! */
6744 static int flushctx_ioctl(char *mp)
6748 fd = open(mp, O_RDONLY);
6750 fprintf(stderr, "flushctx: error open %s: %s\n",
6751 mp, strerror(errno));
6755 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6757 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6758 mp, strerror(errno));
6764 static int lfs_flushctx(int argc, char **argv)
6766 int kdestroy = 0, c;
6767 char mntdir[PATH_MAX] = {'\0'};
6771 while ((c = getopt(argc, argv, "k")) != -1) {
6777 fprintf(stderr, "error: %s: option '-%c' "
6778 "unrecognized\n", argv[0], c);
6784 if ((rc = system("kdestroy > /dev/null")) != 0) {
6785 rc = WEXITSTATUS(rc);
6786 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6790 if (optind >= argc) {
6791 /* flush for all mounted lustre fs. */
6792 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6793 /* Check if we have a mount point */
6794 if (mntdir[0] == '\0')
6797 if (flushctx_ioctl(mntdir))
6800 mntdir[0] = '\0'; /* avoid matching in next loop */
6803 /* flush fs as specified */
6804 while (optind < argc) {
6805 if (flushctx_ioctl(argv[optind++]))
6812 static int lfs_changelog(int argc, char **argv)
6814 void *changelog_priv;
6815 struct changelog_rec *rec;
6816 long long startrec = 0, endrec = 0;
6818 struct option long_opts[] = {
6819 { .val = 'f', .name = "follow", .has_arg = no_argument },
6821 char short_opts[] = "f";
6824 while ((rc = getopt_long(argc, argv, short_opts,
6825 long_opts, NULL)) != -1) {
6832 "%s changelog: unrecognized option '%s'\n",
6833 progname, argv[optind - 1]);
6837 if (optind >= argc) {
6838 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6843 mdd = argv[optind++];
6845 startrec = strtoll(argv[optind++], NULL, 10);
6847 endrec = strtoll(argv[optind++], NULL, 10);
6849 rc = llapi_changelog_start(&changelog_priv,
6850 CHANGELOG_FLAG_BLOCK |
6851 CHANGELOG_FLAG_JOBID |
6852 CHANGELOG_FLAG_EXTRA_FLAGS |
6853 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6856 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6857 progname, strerror(errno = -rc));
6861 rc = llapi_changelog_set_xflags(changelog_priv,
6862 CHANGELOG_EXTRA_FLAG_UIDGID |
6863 CHANGELOG_EXTRA_FLAG_NID |
6864 CHANGELOG_EXTRA_FLAG_OMODE |
6865 CHANGELOG_EXTRA_FLAG_XATTR);
6868 "%s changelog: cannot set xflags for changelog: %s\n",
6869 progname, strerror(errno = -rc));
6873 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6877 if (endrec && rec->cr_index > endrec) {
6878 llapi_changelog_free(&rec);
6881 if (rec->cr_index < startrec) {
6882 llapi_changelog_free(&rec);
6886 secs = rec->cr_time >> 30;
6887 gmtime_r(&secs, &ts);
6888 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6889 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6890 changelog_type2str(rec->cr_type),
6891 ts.tm_hour, ts.tm_min, ts.tm_sec,
6892 (int)(rec->cr_time & ((1 << 30) - 1)),
6893 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6894 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6896 if (rec->cr_flags & CLF_JOBID) {
6897 struct changelog_ext_jobid *jid =
6898 changelog_rec_jobid(rec);
6900 if (jid->cr_jobid[0] != '\0')
6901 printf(" j=%s", jid->cr_jobid);
6904 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6905 struct changelog_ext_extra_flags *ef =
6906 changelog_rec_extra_flags(rec);
6908 printf(" ef=0x%llx",
6909 (unsigned long long)ef->cr_extra_flags);
6911 if (ef->cr_extra_flags & CLFE_UIDGID) {
6912 struct changelog_ext_uidgid *uidgid =
6913 changelog_rec_uidgid(rec);
6915 printf(" u=%llu:%llu",
6916 (unsigned long long)uidgid->cr_uid,
6917 (unsigned long long)uidgid->cr_gid);
6919 if (ef->cr_extra_flags & CLFE_NID) {
6920 struct changelog_ext_nid *nid =
6921 changelog_rec_nid(rec);
6924 libcfs_nid2str(nid->cr_nid));
6927 if (ef->cr_extra_flags & CLFE_OPEN) {
6928 struct changelog_ext_openmode *omd =
6929 changelog_rec_openmode(rec);
6930 char mode[] = "---";
6932 /* exec mode must be exclusive */
6933 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6936 if (omd->cr_openflags & MDS_FMODE_READ)
6938 if (omd->cr_openflags &
6945 if (strcmp(mode, "---") != 0)
6946 printf(" m=%s", mode);
6950 if (ef->cr_extra_flags & CLFE_XATTR) {
6951 struct changelog_ext_xattr *xattr =
6952 changelog_rec_xattr(rec);
6954 if (xattr->cr_xattr[0] != '\0')
6955 printf(" x=%s", xattr->cr_xattr);
6959 if (rec->cr_namelen)
6960 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6961 rec->cr_namelen, changelog_rec_name(rec));
6963 if (rec->cr_flags & CLF_RENAME) {
6964 struct changelog_ext_rename *rnm =
6965 changelog_rec_rename(rec);
6967 if (!fid_is_zero(&rnm->cr_sfid))
6968 printf(" s="DFID" sp="DFID" %.*s",
6969 PFID(&rnm->cr_sfid),
6970 PFID(&rnm->cr_spfid),
6971 (int)changelog_rec_snamelen(rec),
6972 changelog_rec_sname(rec));
6976 llapi_changelog_free(&rec);
6979 llapi_changelog_fini(&changelog_priv);
6982 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6983 progname, strerror(errno = -rc));
6985 return (rc == 1 ? 0 : rc);
6988 static int lfs_changelog_clear(int argc, char **argv)
6996 endrec = strtoll(argv[3], NULL, 10);
6998 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7001 fprintf(stderr, "%s: record out of range: %llu\n",
7003 else if (rc == -ENOENT)
7004 fprintf(stderr, "%s: no changelog user: %s\n",
7007 fprintf(stderr, "%s error: %s\n", argv[0],
7016 static int lfs_fid2path(int argc, char **argv)
7018 struct option long_opts[] = {
7019 { .val = 'c', .name = "cur", .has_arg = no_argument },
7020 { .val = 'l', .name = "link", .has_arg = required_argument },
7021 { .val = 'r', .name = "rec", .has_arg = required_argument },
7023 char short_opts[] = "cl:r:";
7024 char *device, *fid, *path;
7025 long long recno = -1;
7030 char *endptr = NULL;
7032 while ((rc = getopt_long(argc, argv, short_opts,
7033 long_opts, NULL)) != -1) {
7039 linkno = strtol(optarg, &endptr, 10);
7040 if (*endptr != '\0') {
7042 "%s fid2path: invalid linkno '%s'\n",
7048 recno = strtoll(optarg, &endptr, 10);
7049 if (*endptr != '\0') {
7051 "%s fid2path: invalid recno '%s'\n",
7058 "%s fid2path: unrecognized option '%s'\n",
7059 progname, argv[optind - 1]);
7066 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7071 device = argv[optind++];
7072 path = calloc(1, PATH_MAX);
7076 "%s fid2path: cannot allocate memory for path: %s\n",
7077 progname, strerror(-rc));
7082 while (optind < argc) {
7083 fid = argv[optind++];
7085 lnktmp = (linkno >= 0) ? linkno : 0;
7087 int oldtmp = lnktmp;
7088 long long rectmp = recno;
7090 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7094 "%s fid2path: cannot find '%s': %s\n",
7095 progname, fid, strerror(errno = -rc2));
7102 fprintf(stdout, "%lld ", rectmp);
7103 if (device[0] == '/') {
7104 fprintf(stdout, "%s", device);
7105 if (device[strlen(device) - 1] != '/')
7106 fprintf(stdout, "/");
7107 } else if (path[0] == '\0') {
7108 fprintf(stdout, "/");
7110 fprintf(stdout, "%s\n", path);
7113 /* specified linkno */
7115 if (oldtmp == lnktmp)
7125 static int lfs_path2fid(int argc, char **argv)
7127 struct option long_opts[] = {
7128 { .val = 'p', .name = "parents", .has_arg = no_argument },
7131 const char short_opts[] = "p";
7132 const char *sep = "";
7135 bool show_parents = false;
7137 while ((rc = getopt_long(argc, argv, short_opts,
7138 long_opts, NULL)) != -1) {
7141 show_parents = true;
7145 "%s path2fid: unrecognized option '%s'\n",
7146 progname, argv[optind - 1]);
7151 if (optind > argc - 1) {
7152 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7156 else if (optind < argc - 1)
7160 for (path = argv + optind; *path != NULL; path++) {
7162 if (!show_parents) {
7163 err = llapi_path2fid(*path, &fid);
7165 printf("%s%s"DFID"\n",
7166 *sep != '\0' ? *path : "", sep,
7169 char name[NAME_MAX + 1];
7170 unsigned int linkno = 0;
7172 while ((err = llapi_path2parent(*path, linkno, &fid,
7173 name, sizeof(name))) == 0) {
7174 if (*sep != '\0' && linkno == 0)
7175 printf("%s%s", *path, sep);
7177 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7182 /* err == -ENODATA is end-of-loop */
7183 if (linkno > 0 && err == -ENODATA) {
7191 "%s path2fid: cannot get %sfid for '%s': %s\n",
7192 progname, show_parents ? "parent " : "", *path,
7204 #define MAX_ERRNO 4095
7205 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
7207 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
7211 rc = llapi_rmfid(device, fa);
7213 fprintf(stderr, "rmfid(): rc = %d\n", rc);
7217 for (k = 0; k < fa->fa_nr; k++) {
7218 rc = (__s32)fa->fa_fids[k].f_ver;
7219 if (!IS_ERR_VALUE(rc))
7225 fa->fa_fids[k].f_ver = 0;
7226 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
7227 PFID(&fa->fa_fids[k]), rc);
7233 static int lfs_rmfid(int argc, char **argv)
7235 char *fidstr, *device;
7236 int rc = 0, rc2, nr;
7237 struct fid_array *fa;
7239 if (optind > argc - 1) {
7240 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
7244 device = argv[optind++];
7247 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
7253 while (optind < argc) {
7256 fidstr = argv[optind++];
7257 while (*fidstr == '[')
7259 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
7261 fprintf(stderr, "unrecognized FID: %s\n",
7266 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
7267 /* start another batch */
7268 rc2 = lfs_rmfid_and_show_errors(device, fa);
7275 rc2 = lfs_rmfid_and_show_errors(device, fa);
7283 static int lfs_data_version(int argc, char **argv)
7290 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7293 fprintf(stderr, "%s data_version: FILE must be specified\n",
7298 while ((c = getopt(argc, argv, "nrw")) != -1) {
7301 data_version_flags = 0;
7304 data_version_flags |= LL_DV_RD_FLUSH;
7307 data_version_flags |= LL_DV_WR_FLUSH;
7311 "%s data_version: unrecognized option '%s'\n",
7312 progname, argv[optind - 1]);
7316 if (optind == argc) {
7317 fprintf(stderr, "%s data_version: FILE must be specified\n",
7322 path = argv[optind];
7323 fd = open(path, O_RDONLY);
7326 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7327 progname, path, strerror(-rc));
7331 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7334 "%s data_version: cannot get version for '%s': %s\n",
7335 progname, path, strerror(-rc));
7337 printf("%ju" "\n", (uintmax_t)data_version);
7343 static int lfs_hsm_state(int argc, char **argv)
7348 struct hsm_user_state hus;
7356 rc = llapi_hsm_state_get(path, &hus);
7358 fprintf(stderr, "can't get hsm state for %s: %s\n",
7359 path, strerror(errno = -rc));
7363 /* Display path name and status flags */
7364 printf("%s: (0x%08x)", path, hus.hus_states);
7366 if (hus.hus_states & HS_RELEASED)
7367 printf(" released");
7368 if (hus.hus_states & HS_EXISTS)
7370 if (hus.hus_states & HS_DIRTY)
7372 if (hus.hus_states & HS_ARCHIVED)
7373 printf(" archived");
7374 /* Display user-settable flags */
7375 if (hus.hus_states & HS_NORELEASE)
7376 printf(" never_release");
7377 if (hus.hus_states & HS_NOARCHIVE)
7378 printf(" never_archive");
7379 if (hus.hus_states & HS_LOST)
7380 printf(" lost_from_hsm");
7382 if (hus.hus_archive_id != 0)
7383 printf(", archive_id:%d", hus.hus_archive_id);
7386 } while (++i < argc);
7391 #define LFS_HSM_SET 0
7392 #define LFS_HSM_CLEAR 1
7395 * Generic function to set or clear HSM flags.
7396 * Used by hsm_set and hsm_clear.
7398 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7400 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7402 struct option long_opts[] = {
7403 { .val = 'A', .name = "archived", .has_arg = no_argument },
7404 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7405 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7406 { .val = 'e', .name = "exists", .has_arg = no_argument },
7407 { .val = 'l', .name = "lost", .has_arg = no_argument },
7408 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7409 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7411 char short_opts[] = "lraAdei:";
7415 __u32 archive_id = 0;
7421 while ((c = getopt_long(argc, argv, short_opts,
7422 long_opts, NULL)) != -1) {
7428 mask |= HS_NOARCHIVE;
7431 mask |= HS_ARCHIVED;
7434 mask |= HS_NORELEASE;
7443 archive_id = strtol(optarg, &end, 10);
7445 fprintf(stderr, "invalid archive_id: '%s'\n",
7453 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7454 argv[0], argv[optind - 1]);
7459 /* User should have specified a flag */
7463 while (optind < argc) {
7465 path = argv[optind];
7467 /* If mode == 0, this means we apply the mask. */
7468 if (mode == LFS_HSM_SET)
7469 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7471 rc = llapi_hsm_state_set(path, 0, mask, 0);
7474 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7475 path, strerror(errno = -rc));
7484 static int lfs_hsm_action(int argc, char **argv)
7489 struct hsm_current_action hca;
7490 struct hsm_extent he;
7491 enum hsm_user_action hua;
7492 enum hsm_progress_states hps;
7500 rc = llapi_hsm_current_action(path, &hca);
7502 fprintf(stderr, "can't get hsm action for %s: %s\n",
7503 path, strerror(errno = -rc));
7506 he = hca.hca_location;
7507 hua = hca.hca_action;
7508 hps = hca.hca_state;
7510 printf("%s: %s", path, hsm_user_action2name(hua));
7512 /* Skip file without action */
7513 if (hca.hca_action == HUA_NONE) {
7518 printf(" %s ", hsm_progress_state2name(hps));
7520 if ((hps == HPS_RUNNING) &&
7521 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7522 printf("(%llu bytes moved)\n",
7523 (unsigned long long)he.length);
7524 else if ((he.offset + he.length) == LUSTRE_EOF)
7525 printf("(from %llu to EOF)\n",
7526 (unsigned long long)he.offset);
7528 printf("(from %llu to %llu)\n",
7529 (unsigned long long)he.offset,
7530 (unsigned long long)(he.offset + he.length));
7532 } while (++i < argc);
7537 static int lfs_hsm_set(int argc, char **argv)
7539 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7542 static int lfs_hsm_clear(int argc, char **argv)
7544 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7548 * Check file state and return its fid, to be used by lfs_hsm_request().
7550 * \param[in] file Path to file to check
7551 * \param[in,out] fid Pointer to allocated lu_fid struct.
7552 * \param[in,out] last_dev Pointer to last device id used.
7554 * \return 0 on success.
7556 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7562 rc = lstat(file, &st);
7564 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7567 /* Checking for regular file as archiving as posix copytool
7568 * rejects archiving files other than regular files
7570 if (!S_ISREG(st.st_mode)) {
7571 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7574 /* A request should be ... */
7575 if (*last_dev != st.st_dev && *last_dev != 0) {
7576 fprintf(stderr, "All files should be "
7577 "on the same filesystem: %s\n", file);
7580 *last_dev = st.st_dev;
7582 rc = llapi_path2fid(file, fid);
7584 fprintf(stderr, "Cannot read FID of %s: %s\n",
7585 file, strerror(-rc));
7591 /* Fill an HSM HUR item with a given file name.
7593 * If mntpath is set, then the filename is actually a FID, and no
7594 * lookup on the filesystem will be performed.
7596 * \param[in] hur the user request to fill
7597 * \param[in] idx index of the item inside the HUR to fill
7598 * \param[in] mntpath mountpoint of Lustre
7599 * \param[in] fname filename (if mtnpath is NULL)
7600 * or FID (if mntpath is set)
7601 * \param[in] last_dev pointer to last device id used
7603 * \retval 0 on success
7604 * \retval CMD_HELP or a negative errno on error
7606 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7607 const char *mntpath, const char *fname,
7610 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7613 hui->hui_extent.length = -1;
7615 if (mntpath != NULL) {
7618 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7622 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7627 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7631 hur->hur_request.hr_itemcount++;
7636 static int lfs_hsm_request(int argc, char **argv, int action)
7638 struct option long_opts[] = {
7639 { .val = 'a', .name = "archive", .has_arg = required_argument },
7640 { .val = 'D', .name = "data", .has_arg = required_argument },
7641 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7642 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7645 char short_opts[] = "l:D:a:m:";
7646 struct hsm_user_request *hur, *oldhur;
7651 char *filelist = NULL;
7652 char fullpath[PATH_MAX];
7653 char *opaque = NULL;
7657 int nbfile_alloc = 0;
7658 char *some_file = NULL;
7659 char *mntpath = NULL;
7665 while ((c = getopt_long(argc, argv, short_opts,
7666 long_opts, NULL)) != -1) {
7675 if (action != HUA_ARCHIVE &&
7676 action != HUA_REMOVE) {
7678 "error: -a is supported only "
7679 "when archiving or removing\n");
7682 archive_id = atoi(optarg);
7685 if (some_file == NULL) {
7687 some_file = strdup(optarg);
7693 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7694 argv[0], argv[optind - 1]);
7699 /* All remaining args are files, so we have at least nbfile */
7700 nbfile = argc - optind;
7702 if ((nbfile == 0) && (filelist == NULL))
7706 opaque_len = strlen(opaque);
7708 /* Alloc the request structure with enough place to store all files
7709 * from command line. */
7710 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7712 fprintf(stderr, "Cannot create the request: %s\n",
7716 nbfile_alloc = nbfile;
7718 hur->hur_request.hr_action = action;
7719 hur->hur_request.hr_archive_id = archive_id;
7720 hur->hur_request.hr_flags = 0;
7722 /* All remaining args are files, add them */
7723 if (nbfile != 0 && some_file == NULL)
7724 some_file = strdup(argv[optind]);
7726 for (i = 0; i < nbfile; i++) {
7727 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7733 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7735 /* If a filelist was specified, read the filelist from it. */
7736 if (filelist != NULL) {
7737 fp = fopen(filelist, "r");
7739 fprintf(stderr, "Cannot read the file list %s: %s\n",
7740 filelist, strerror(errno));
7745 while ((rc = getline(&line, &len, fp)) != -1) {
7746 /* If allocated buffer was too small, get something
7748 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7751 nbfile_alloc = nbfile_alloc * 2 + 1;
7753 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7756 fprintf(stderr, "hsm: cannot allocate "
7757 "the request: %s\n",
7764 size = hur_len(oldhur);
7766 fprintf(stderr, "hsm: cannot allocate "
7767 "%u files + %u bytes data\n",
7768 oldhur->hur_request.hr_itemcount,
7769 oldhur->hur_request.hr_data_len);
7776 memcpy(hur, oldhur, size);
7781 if (line[strlen(line) - 1] == '\n')
7782 line[strlen(line) - 1] = '\0';
7784 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7785 mntpath, line, &last_dev);
7791 if (some_file == NULL) {
7801 /* If a --data was used, add it to the request */
7802 hur->hur_request.hr_data_len = opaque_len;
7804 memcpy(hur_data(hur), opaque, opaque_len);
7806 /* Send the HSM request */
7807 if (realpath(some_file, fullpath) == NULL) {
7808 fprintf(stderr, "Could not find path '%s': %s\n",
7809 some_file, strerror(errno));
7811 rc = llapi_hsm_request(fullpath, hur);
7813 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7814 some_file, strerror(-rc));
7824 static int lfs_hsm_archive(int argc, char **argv)
7826 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7829 static int lfs_hsm_restore(int argc, char **argv)
7831 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7834 static int lfs_hsm_release(int argc, char **argv)
7836 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7839 static int lfs_hsm_remove(int argc, char **argv)
7841 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7844 static int lfs_hsm_cancel(int argc, char **argv)
7846 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7849 static int lfs_swap_layouts(int argc, char **argv)
7854 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7855 SWAP_LAYOUTS_KEEP_MTIME |
7856 SWAP_LAYOUTS_KEEP_ATIME);
7859 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7861 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7863 int lfs_get_mode(const char *string)
7865 enum lock_mode_user mode;
7867 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7868 if (lock_mode_names[mode] == NULL)
7870 if (strcmp(string, lock_mode_names[mode]) == 0)
7877 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7879 enum lu_ladvise_type advice;
7882 advice < ARRAY_SIZE(ladvise_names); advice++) {
7883 if (ladvise_names[advice] == NULL)
7885 if (strcmp(string, ladvise_names[advice]) == 0)
7889 return LU_LADVISE_INVALID;
7892 static int lfs_ladvise(int argc, char **argv)
7894 struct option long_opts[] = {
7895 { .val = 'a', .name = "advice", .has_arg = required_argument },
7896 { .val = 'b', .name = "background", .has_arg = no_argument },
7897 { .val = 'e', .name = "end", .has_arg = required_argument },
7898 { .val = 'l', .name = "length", .has_arg = required_argument },
7899 { .val = 'm', .name = "mode", .has_arg = required_argument },
7900 { .val = 's', .name = "start", .has_arg = required_argument },
7901 { .val = 'u', .name = "unset", .has_arg = no_argument },
7903 char short_opts[] = "a:be:l:m:s:u";
7908 struct llapi_lu_ladvise advice;
7909 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7910 unsigned long long start = 0;
7911 unsigned long long end = LUSTRE_EOF;
7912 unsigned long long length = 0;
7913 unsigned long long size_units;
7914 unsigned long long flags = 0;
7918 while ((c = getopt_long(argc, argv, short_opts,
7919 long_opts, NULL)) != -1) {
7922 advice_type = lfs_get_ladvice(optarg);
7923 if (advice_type == LU_LADVISE_INVALID) {
7924 fprintf(stderr, "%s: invalid advice type "
7925 "'%s'\n", argv[0], optarg);
7926 fprintf(stderr, "Valid types:");
7928 for (advice_type = 0;
7929 advice_type < ARRAY_SIZE(ladvise_names);
7931 if (ladvise_names[advice_type] == NULL)
7933 fprintf(stderr, " %s",
7934 ladvise_names[advice_type]);
7936 fprintf(stderr, "\n");
7949 rc = llapi_parse_size(optarg, &end,
7952 fprintf(stderr, "%s: bad end offset '%s'\n",
7959 rc = llapi_parse_size(optarg, &start,
7962 fprintf(stderr, "%s: bad start offset "
7963 "'%s'\n", argv[0], optarg);
7969 rc = llapi_parse_size(optarg, &length,
7972 fprintf(stderr, "%s: bad length '%s'\n",
7978 mode = lfs_get_mode(optarg);
7980 fprintf(stderr, "%s: bad mode '%s', valid "
7981 "modes are READ or WRITE\n",
7989 fprintf(stderr, "%s: option '%s' unrecognized\n",
7990 argv[0], argv[optind - 1]);
7995 if (advice_type == LU_LADVISE_INVALID) {
7996 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7997 fprintf(stderr, "Valid types:");
7998 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8000 if (ladvise_names[advice_type] == NULL)
8002 fprintf(stderr, " %s", ladvise_names[advice_type]);
8004 fprintf(stderr, "\n");
8008 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8009 fprintf(stderr, "%s: Lock no expand advice is a per file "
8010 "descriptor advice, so when called from lfs, "
8011 "it does nothing.\n", argv[0]);
8015 if (argc <= optind) {
8016 fprintf(stderr, "%s: please give one or more file names\n",
8021 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8022 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8027 if (end == LUSTRE_EOF && length != 0)
8028 end = start + length;
8031 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8032 argv[0], start, end);
8036 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8037 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8042 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8043 fprintf(stderr, "%s: mode is required with lockahead\n",
8048 while (optind < argc) {
8051 path = argv[optind++];
8053 fd = open(path, O_RDONLY);
8055 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8056 argv[0], path, strerror(errno));
8061 advice.lla_start = start;
8062 advice.lla_end = end;
8063 advice.lla_advice = advice_type;
8064 advice.lla_value1 = 0;
8065 advice.lla_value2 = 0;
8066 advice.lla_value3 = 0;
8067 advice.lla_value4 = 0;
8068 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8069 advice.lla_lockahead_mode = mode;
8070 advice.lla_peradvice_flags = flags;
8073 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8076 fprintf(stderr, "%s: cannot give advice '%s' to file "
8077 "'%s': %s\n", argv[0],
8078 ladvise_names[advice_type],
8079 path, strerror(errno));
8085 if (rc == 0 && rc2 < 0)
8091 /** The input string contains a comma delimited list of component ids and
8092 * ranges, for example "1,2-4,7".
8094 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8096 bool end_of_loop = false;
8104 while (!end_of_loop) {
8108 char *endptr = NULL;
8111 ptr = strchrnul(arg, ',');
8112 end_of_loop = *ptr == '\0';
8115 start_index = strtol(arg, &endptr, 0);
8116 if (endptr == arg) /* no data at all */
8118 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8120 if (start_index < 0)
8123 end_index = start_index;
8124 if (*endptr == '-') {
8125 end_index = strtol(endptr + 1, &endptr, 0);
8126 if (*endptr != '\0')
8128 if (end_index < start_index)
8132 for (i = start_index; i <= end_index && size > 0; i++) {
8135 /* remove duplicate */
8136 for (j = 0; j < nr; j++) {
8140 if (j == nr) { /* no duplicate */
8146 if (size == 0 && i < end_index)
8153 if (!end_of_loop && ptr != NULL)
8156 return rc < 0 ? rc : nr;
8160 * struct verify_mirror_id - Mirror id to be verified.
8161 * @mirror_id: A specified mirror id.
8162 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8164 struct verify_mirror_id {
8170 * compare_mirror_ids() - Compare mirror ids.
8171 * @layout: Mirror component list.
8172 * @cbdata: Callback data in verify_mirror_id structure.
8174 * This is a callback function called by llapi_layout_comp_iterate()
8175 * to compare the specified mirror id with the one in the current
8176 * component of @layout. If they are the same, then the specified
8177 * mirror id is valid.
8179 * Return: a negative error code on failure or
8180 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8181 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8184 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8186 struct verify_mirror_id *mirror_id_cbdata =
8187 (struct verify_mirror_id *)cbdata;
8191 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8195 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8196 progname, strerror(errno));
8200 if (mirror_id_cbdata->mirror_id == mirror_id) {
8201 mirror_id_cbdata->is_valid_id = true;
8202 return LLAPI_LAYOUT_ITER_STOP;
8205 return LLAPI_LAYOUT_ITER_CONT;
8209 * verify_mirror_ids() - Verify specified mirror ids.
8210 * @fname: Mirrored file name.
8211 * @mirror_ids: Specified mirror ids to be verified.
8212 * @ids_nr: Number of specified mirror ids.
8214 * This function verifies that specified @mirror_ids are valid
8215 * in the mirrored file @fname.
8217 * Return: 0 on success or a negative error code on failure.
8220 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8222 struct llapi_layout *layout = NULL;
8223 struct verify_mirror_id mirror_id_cbdata = { 0 };
8234 if (stat(fname, &stbuf) < 0) {
8235 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8236 progname, fname, strerror(errno));
8241 if (!S_ISREG(stbuf.st_mode)) {
8242 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8248 fd = open(fname, O_DIRECT | O_RDONLY);
8250 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8251 progname, fname, strerror(errno));
8256 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8258 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8259 progname, fname, strerror(errno));
8263 layout = llapi_layout_get_by_fd(fd, 0);
8264 if (layout == NULL) {
8265 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8266 progname, fname, strerror(errno));
8268 llapi_lease_release(fd);
8272 rc = llapi_layout_flags_get(layout, &flr_state);
8274 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8275 progname, fname, strerror(errno));
8280 flr_state &= LCM_FL_FLR_MASK;
8281 switch (flr_state) {
8284 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8285 progname, fname, llapi_layout_flags_string(flr_state));
8292 for (i = 0; i < ids_nr; i++) {
8293 mirror_id_cbdata.mirror_id = mirror_ids[i];
8294 mirror_id_cbdata.is_valid_id = false;
8296 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8301 "%s: '%s' failed to verify mirror id: %u.\n",
8302 progname, fname, mirror_ids[i]);
8306 if (!mirror_id_cbdata.is_valid_id) {
8309 "%s: '%s' invalid specified mirror id: %u.\n",
8310 progname, fname, mirror_ids[i]);
8316 llapi_layout_free(layout);
8317 llapi_lease_release(fd);
8325 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8326 __u16 *mirror_ids, int ids_nr)
8328 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8329 struct llapi_layout *layout;
8340 if (stat(fname, &stbuf) < 0) {
8341 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8342 progname, fname, strerror(errno));
8346 if (!S_ISREG(stbuf.st_mode)) {
8347 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8353 fd = open(fname, O_DIRECT | O_RDWR);
8355 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8356 progname, fname, strerror(errno));
8361 layout = llapi_layout_get_by_fd(fd, 0);
8362 if (layout == NULL) {
8363 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8364 progname, fname, strerror(errno));
8369 rc = llapi_layout_flags_get(layout, &flr_state);
8371 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8372 progname, fname, strerror(errno));
8377 flr_state &= LCM_FL_FLR_MASK;
8378 if (flr_state == LCM_FL_NONE) {
8380 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8385 /* get stale component info */
8386 comp_size = llapi_mirror_find_stale(layout, comp_array,
8387 ARRAY_SIZE(comp_array),
8388 mirror_ids, ids_nr);
8389 if (comp_size <= 0) {
8394 ioc->lil_mode = LL_LEASE_WRLCK;
8395 ioc->lil_flags = LL_LEASE_RESYNC;
8396 rc = llapi_lease_set(fd, ioc);
8398 if (rc == -EALREADY)
8402 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8403 progname, fname, strerror(-rc));
8407 /* get the read range [start, end) */
8408 start = comp_array[0].lrc_start;
8409 end = comp_array[0].lrc_end;
8410 for (idx = 1; idx < comp_size; idx++) {
8411 if (comp_array[idx].lrc_start < start)
8412 start = comp_array[idx].lrc_start;
8413 if (end < comp_array[idx].lrc_end)
8414 end = comp_array[idx].lrc_end;
8417 rc = llapi_lease_check(fd);
8418 if (rc != LL_LEASE_WRLCK) {
8419 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8424 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8427 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
8428 progname, fname, strerror(-rc));
8430 /* need to do the lease unlock even resync fails */
8431 ioc->lil_mode = LL_LEASE_UNLCK;
8432 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8434 for (idx = 0; idx < comp_size; idx++) {
8435 if (comp_array[idx].lrc_synced) {
8436 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8441 rc2 = llapi_lease_set(fd, ioc);
8443 * llapi_lease_set returns lease mode when it request to unlock
8447 /* rc2 == 0 means lost lease lock */
8448 if (rc2 == 0 && rc == 0)
8450 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8452 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
8456 llapi_layout_free(layout);
8463 static inline int lfs_mirror_resync(int argc, char **argv)
8465 struct ll_ioc_lease *ioc = NULL;
8466 __u16 mirror_ids[128] = { 0 };
8471 struct option long_opts[] = {
8472 { .val = 'o', .name = "only", .has_arg = required_argument },
8475 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8478 rc = parse_mirror_ids(mirror_ids,
8479 sizeof(mirror_ids) / sizeof(__u16),
8483 "%s: bad mirror ids '%s'.\n",
8490 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8491 argv[0], argv[optind - 1]);
8497 if (argc == optind) {
8498 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8503 if (ids_nr > 0 && argc > optind + 1) {
8505 "%s: option '--only' cannot be used upon multiple files.\n",
8513 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8518 /* set the lease on the file */
8519 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8521 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8522 argv[0], strerror(errno));
8527 for (; optind < argc; optind++) {
8528 rc = lfs_mirror_resync_file(argv[optind], ioc,
8529 mirror_ids, ids_nr);
8530 /* ignore previous file's error, continue with next file */
8533 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8541 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8543 struct llapi_layout *layout;
8546 layout = llapi_layout_get_by_fd(fd, 0);
8547 if (layout == NULL) {
8548 fprintf(stderr, "could not get layout.\n");
8552 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8554 fprintf(stderr, "failed to iterate layout\n");
8555 llapi_layout_free(layout);
8558 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8559 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8560 llapi_layout_free(layout);
8564 llapi_layout_free(layout);
8570 * Check whether two files are the same file
8571 * \retval 0 same file
8572 * \retval 1 not the same file
8573 * \retval <0 error code
8575 static inline int check_same_file(const char *f1, const char *f2)
8580 if (stat(f1, &stbuf1) < 0) {
8581 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8582 progname, f1, strerror(errno));
8586 if (stat(f2, &stbuf2) < 0) {
8587 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8588 progname, f2, strerror(errno));
8592 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8593 stbuf1.st_ino == stbuf2.st_ino)
8599 static inline int lfs_mirror_read(int argc, char **argv)
8602 __u16 mirror_id = 0;
8603 const char *outfile = NULL;
8609 const size_t buflen = 4 << 20;
8611 struct option long_opts[] = {
8612 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8613 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8616 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8621 mirror_id = strtoul(optarg, &end, 0);
8622 if (*end != '\0' || mirror_id == 0) {
8624 "%s %s: invalid mirror ID '%s'\n",
8625 progname, argv[0], optarg);
8633 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8634 progname, argv[optind - 1]);
8639 if (argc == optind) {
8640 fprintf(stderr, "%s %s: no mirrored file provided\n",
8643 } else if (argc > optind + 1) {
8644 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8648 if (mirror_id == 0) {
8649 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8654 /* open mirror file */
8655 fname = argv[optind];
8658 rc = check_same_file(fname, outfile);
8661 "%s %s: output file cannot be the mirrored file\n",
8669 fd = open(fname, O_DIRECT | O_RDONLY);
8671 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8672 progname, argv[0], fname, strerror(errno));
8676 /* verify mirror id */
8677 rc = verify_mirror_id_by_fd(fd, mirror_id);
8680 "%s %s: cannot find mirror with ID %u in '%s'\n",
8681 progname, argv[0], mirror_id, fname);
8685 /* open output file */
8687 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8689 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8690 progname, argv[0], outfile, strerror(errno));
8695 outfd = STDOUT_FILENO;
8698 /* allocate buffer */
8699 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8701 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8702 progname, argv[0], rc);
8709 ssize_t written = 0;
8711 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8712 if (bytes_read < 0) {
8715 "%s %s: fail to read data from mirror %u: %s\n",
8716 progname, argv[0], mirror_id, strerror(-rc));
8721 if (bytes_read == 0)
8724 while (written < bytes_read) {
8727 written2 = write(outfd, buf + written,
8728 bytes_read - written);
8731 "%s %s: fail to write %s: %s\n",
8732 progname, argv[0], outfile ? : "STDOUT",
8737 written += written2;
8740 if (written != bytes_read) {
8742 "%s %s: written %ld bytes does not match with %ld read.\n",
8743 progname, argv[0], written, bytes_read);
8765 static inline int lfs_mirror_write(int argc, char **argv)
8768 __u16 mirror_id = 0;
8769 const char *inputfile = NULL;
8775 const size_t buflen = 4 << 20;
8777 size_t page_size = sysconf(_SC_PAGESIZE);
8778 struct ll_ioc_lease_id ioc;
8780 struct option long_opts[] = {
8781 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8782 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8785 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8790 mirror_id = strtoul(optarg, &end, 0);
8791 if (*end != '\0' || mirror_id == 0) {
8793 "%s %s: invalid mirror ID '%s'\n",
8794 progname, argv[0], optarg);
8802 fprintf(stderr, "%s: option '%s' unrecognized\n",
8803 progname, argv[optind - 1]);
8808 if (argc == optind) {
8809 fprintf(stderr, "%s %s: no mirrored file provided\n",
8812 } else if (argc > optind + 1) {
8813 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8817 if (mirror_id == 0) {
8818 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8823 /* open mirror file */
8824 fname = argv[optind];
8827 rc = check_same_file(fname, inputfile);
8830 "%s %s: input file cannot be the mirrored file\n",
8838 fd = open(fname, O_DIRECT | O_WRONLY);
8840 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8841 progname, argv[0], fname, strerror(errno));
8845 /* verify mirror id */
8846 rc = verify_mirror_id_by_fd(fd, mirror_id);
8849 "%s %s: cannot find mirror with ID %u in '%s'\n",
8850 progname, argv[0], mirror_id, fname);
8854 /* open input file */
8856 inputfd = open(inputfile, O_RDONLY, 0644);
8858 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
8859 progname, argv[0], inputfile, strerror(errno));
8864 inputfd = STDIN_FILENO;
8867 /* allocate buffer */
8868 rc = posix_memalign(&buf, page_size, buflen);
8870 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8871 progname, argv[0], rc);
8875 /* prepare target mirror components instantiation */
8876 ioc.lil_mode = LL_LEASE_WRLCK;
8877 ioc.lil_flags = LL_LEASE_RESYNC;
8878 ioc.lil_mirror_id = mirror_id;
8879 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8882 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8883 progname, argv[0], fname, strerror(errno));
8893 rc = llapi_lease_check(fd);
8894 if (rc != LL_LEASE_WRLCK) {
8895 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
8896 progname, argv[0], fname);
8900 bytes_read = read(inputfd, buf, buflen);
8901 if (bytes_read < 0) {
8904 "%s %s: fail to read data from '%s': %s\n",
8905 progname, argv[0], inputfile ? : "STDIN",
8912 if (bytes_read == 0)
8915 /* round up to page align to make direct IO happy. */
8916 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
8918 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
8923 "%s %s: fail to write to mirror %u: %s\n",
8924 progname, argv[0], mirror_id,
8932 if (pos & (page_size - 1)) {
8933 rc = llapi_mirror_truncate(fd, mirror_id, pos);
8938 ioc.lil_mode = LL_LEASE_UNLCK;
8939 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
8941 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8946 "%s %s: release lease lock of '%s' failed: %s\n",
8947 progname, argv[0], fname, strerror(errno));
8964 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
8966 struct llapi_layout *layout;
8967 struct collect_ids_data cid = { .cid_ids = ids,
8969 .cid_exclude = exclude_id, };
8972 layout = llapi_layout_get_by_fd(fd, 0);
8973 if (layout == NULL) {
8974 fprintf(stderr, "could not get layout\n");
8978 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
8980 fprintf(stderr, "failed to iterate layout\n");
8981 llapi_layout_free(layout);
8985 llapi_layout_free(layout);
8987 return cid.cid_count;
8990 static inline int lfs_mirror_copy(int argc, char **argv)
8993 __u16 read_mirror_id = 0;
8994 __u16 ids[128] = { 0 };
8996 struct llapi_layout *layout = NULL;
8997 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9004 struct ll_ioc_lease *ioc = NULL;
9005 struct ll_ioc_lease_id *resync_ioc;
9007 struct option long_opts[] = {
9008 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9009 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9012 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9017 read_mirror_id = strtoul(optarg, &end, 0);
9018 if (*end != '\0' || read_mirror_id == 0) {
9020 "%s %s: invalid read mirror ID '%s'\n",
9021 progname, argv[0], optarg);
9026 if (!strcmp(optarg, "-1")) {
9027 /* specify all other mirrors */
9031 count = parse_mirror_ids((__u16 *)ids,
9039 fprintf(stderr, "%s: option '%s' unrecognized\n",
9040 progname, argv[optind - 1]);
9045 if (argc == optind) {
9046 fprintf(stderr, "%s %s: no mirrored file provided\n",
9049 } else if (argc > optind + 1) {
9050 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9054 if (read_mirror_id == 0) {
9056 "%s %s: no valid read mirror ID %d is provided\n",
9057 progname, argv[0], read_mirror_id);
9063 "%s %s: no write mirror ID is provided\n",
9068 for (i = 0; i < count; i++) {
9069 if (read_mirror_id == ids[i]) {
9071 "%s %s: read and write mirror ID cannot be the same\n",
9077 /* open mirror file */
9078 fname = argv[optind];
9080 fd = open(fname, O_DIRECT | O_RDWR);
9082 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9083 progname, argv[0], fname, strerror(errno));
9087 /* write to all other mirrors */
9088 if (ids[0] == (__u16)-1) {
9089 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9093 "%s %s: failed to get other mirror ids in '%s': %d\n",
9094 progname, argv[0], fname, rc);
9099 /* verify mirror id */
9100 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9103 "%s %s: cannot find mirror with ID %u in '%s'\n",
9104 progname, argv[0], read_mirror_id, fname);
9108 for (i = 0; i < count; i++) {
9109 rc = verify_mirror_id_by_fd(fd, ids[i]);
9112 "%s %s: cannot find mirror with ID %u in '%s'\n",
9113 progname, argv[0], ids[i], fname);
9118 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9121 "%s %s: cannot alloc comp id array for ioc: %s\n",
9122 progname, argv[0], strerror(errno));
9127 /* get stale component info */
9128 layout = llapi_layout_get_by_fd(fd, 0);
9129 if (layout == NULL) {
9130 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9131 progname, argv[0], fname, strerror(errno));
9135 comp_size = llapi_mirror_find_stale(layout, comp_array,
9136 ARRAY_SIZE(comp_array),
9138 llapi_layout_free(layout);
9139 if (comp_size < 0) {
9144 /* prepare target mirror components instantiation */
9145 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9146 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9147 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9149 resync_ioc->lil_mirror_id = ids[0];
9151 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9152 rc = llapi_lease_set(fd, ioc);
9155 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9156 progname, argv[0], fname, strerror(errno));
9160 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9163 fprintf(stderr, "%s %s: copy error: %d\n",
9164 progname, argv[0], rc);
9168 fprintf(stdout, "mirror copied successfully: ");
9169 for (i = 0; i < copied; i++)
9170 fprintf(stdout, "%d ", ids[i]);
9171 fprintf(stdout, "\n");
9173 ioc->lil_mode = LL_LEASE_UNLCK;
9174 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9176 for (i = 0; i < comp_size; i++) {
9179 for (j = 0; j < copied; j++) {
9180 if (comp_array[i].lrc_mirror_id != ids[j])
9183 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9187 rc = llapi_lease_set(fd, ioc);
9192 "%s %s: release lease lock of '%s' failed: %s\n",
9193 progname, argv[0], fname, strerror(errno));
9207 * struct verify_chunk - Mirror chunk to be verified.
9208 * @chunk: [start, end) of the chunk.
9209 * @mirror_count: Number of mirror ids in @mirror_id array.
9210 * @mirror_id: Array of valid mirror ids that cover the chunk.
9212 struct verify_chunk {
9213 struct lu_extent chunk;
9214 unsigned int mirror_count;
9215 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9219 * print_chunks() - Print chunk information.
9220 * @fname: Mirrored file name.
9221 * @chunks: Array of chunks.
9222 * @chunk_count: Number of chunks in @chunks array.
9224 * This function prints [start, end) of each chunk in @chunks
9225 * for mirrored file @fname, and also prints the valid mirror ids
9226 * that cover the chunk.
9231 void print_chunks(const char *fname, struct verify_chunk *chunks,
9237 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9238 for (i = 0; i < chunk_count; i++) {
9239 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9241 if (chunks[i].mirror_count == 0)
9242 fprintf(stdout, "\t[");
9244 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9245 for (j = 1; j < chunks[i].mirror_count; j++)
9246 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9248 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9250 fprintf(stdout, "\n");
9254 * print_checksums() - Print CRC-32 checksum values.
9255 * @chunk: A chunk and its corresponding valid mirror ids.
9256 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9258 * This function prints CRC-32 checksum values on @chunk for
9259 * each valid mirror that covers it.
9264 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9269 "CRC-32 checksum value for chunk "DEXT":\n",
9270 PEXT(&chunk->chunk));
9271 for (i = 0; i < chunk->mirror_count; i++)
9272 fprintf(stdout, "Mirror %u:\t%#lx\n",
9273 chunk->mirror_id[i], crc[i]);
9274 fprintf(stdout, "\n");
9278 * filter_mirror_id() - Filter specified mirror ids.
9279 * @chunks: Array of chunks.
9280 * @chunk_count: Number of chunks in @chunks array.
9281 * @mirror_ids: Specified mirror ids to be verified.
9282 * @ids_nr: Number of specified mirror ids.
9284 * This function scans valid mirror ids that cover each chunk in @chunks
9285 * and filters specified mirror ids.
9290 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9291 __u16 *mirror_ids, int ids_nr)
9296 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9297 unsigned int valid_count = 0;
9299 for (i = 0; i < chunk_count; i++) {
9300 if (chunks[i].mirror_count == 0)
9304 for (j = 0; j < ids_nr; j++) {
9305 for (k = 0; k < chunks[i].mirror_count; k++) {
9306 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9307 valid_id[valid_count] = mirror_ids[j];
9314 memcpy(chunks[i].mirror_id, valid_id,
9315 sizeof(__u16) * valid_count);
9316 chunks[i].mirror_count = valid_count;
9321 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9322 * @layout: Mirror component list.
9323 * @chunks: Array of chunks.
9324 * @chunks_size: Array size of @chunks.
9326 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9327 * to find out chunk segments and store them in @chunks array.
9329 * The @mirror_id array in each element of @chunks will store the valid
9330 * mirror ids that cover the chunk. If a mirror component covering the
9331 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9332 * will not be stored into the @mirror_id array, and the chunk for that
9333 * mirror will not be verified.
9335 * The @mirror_count in each element of @chunks will store the number of
9336 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9337 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9338 * indicates the chunk is valid in only one mirror. In both cases, the
9339 * chunk will not be verified.
9341 * Here is an example:
9344 * +------+-------------+--------------------+
9346 * +------+------+------+------+-------------+
9347 * | | S | S | | mirror2
9348 * +-------------+------+------+-------------+
9350 * prepared @chunks array will contain 5 elements:
9351 * (([0, 1M), [1, 2], 2),
9352 * ([1M, 2M), [1, 2], 2),
9353 * ([2M, 3M), [1], 1),
9354 * ([3M, 4M], [], 0),
9355 * ([4M, EOF), [2], 1))
9357 * Return: the actual array size of @chunks on success
9358 * or a negative error code on failure.
9361 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9362 struct verify_chunk *chunks,
9373 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9376 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9379 "%s: move to the first layout component: %s.\n",
9380 progname, strerror(errno));
9386 chunks[idx].chunk.e_end = LUSTRE_EOF;
9388 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9391 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9392 progname, strerror(errno));
9396 if (start > chunks[idx].chunk.e_start ||
9397 end <= chunks[idx].chunk.e_start)
9400 if (end < chunks[idx].chunk.e_end)
9401 chunks[idx].chunk.e_end = end;
9403 rc = llapi_layout_comp_flags_get(layout, &flags);
9406 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9407 progname, strerror(errno));
9411 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9414 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9417 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9418 progname, strerror(errno));
9422 chunks[idx].mirror_id[i] = mirror_id;
9424 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9426 "%s: mirror_id array is too small.\n",
9433 rc = llapi_layout_comp_use(layout,
9434 LLAPI_LAYOUT_COMP_USE_NEXT);
9437 "%s: move to the next layout component: %s.\n",
9438 progname, strerror(errno));
9441 } /* loop through all components */
9443 chunks[idx].mirror_count = i;
9445 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9449 if (idx >= chunks_size) {
9450 fprintf(stderr, "%s: chunks array is too small.\n",
9456 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9460 return rc < 0 ? rc : idx + 1;
9464 * lfs_mirror_verify_chunk() - Verify a chunk.
9465 * @fd: File descriptor of the mirrored file.
9466 * @file_size: Size of the mirrored file.
9467 * @chunk: A chunk and its corresponding valid mirror ids.
9468 * @verbose: Verbose mode.
9470 * This function verifies a @chunk contains exactly the same data
9471 * ammong the mirrors that cover it.
9473 * If @verbose is specified, then the function will print where the
9474 * differences are if the data do not match. Otherwise, it will
9475 * just return an error in that case.
9477 * Return: 0 on success or a negative error code on failure.
9480 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9481 struct verify_chunk *chunk, int verbose)
9483 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9485 size_t page_size = sysconf(_SC_PAGESIZE);
9491 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9498 rc = posix_memalign(&buf, page_size, buflen);
9499 if (rc) /* error code is returned directly */
9503 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9504 PEXT(&chunk->chunk));
9505 for (i = 0; i < chunk->mirror_count; i++)
9506 fprintf(stdout, " %u", chunk->mirror_id[i]);
9507 fprintf(stdout, "\n");
9511 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9512 pos = chunk->chunk.e_start;
9513 while (bytes_done < count) {
9514 /* compute initial CRC-32 checksum */
9515 crc = crc32(0L, Z_NULL, 0);
9516 memset(crc_array, 0, sizeof(crc_array));
9519 for (i = 0; i < chunk->mirror_count; i++) {
9520 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9522 if (bytes_read < 0) {
9525 "%s: failed to read data from mirror %u: %s.\n",
9526 progname, chunk->mirror_id[i],
9531 /* compute new CRC-32 checksum */
9532 crc_array[i] = crc32(crc, buf, bytes_read);
9536 print_checksums(chunk, crc_array);
9538 /* compare CRC-32 checksum values */
9539 for (i = 1; i < chunk->mirror_count; i++) {
9540 if (crc_array[i] != crc_array[0]) {
9544 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9545 progname, PEXT(&chunk->chunk),
9546 chunk->mirror_id[0],
9547 chunk->mirror_id[i]);
9552 bytes_done += bytes_read;
9555 if (verbose > 1 && rc == 0) {
9556 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9557 PEXT(&chunk->chunk));
9558 for (i = 0; i < chunk->mirror_count; i++)
9559 fprintf(stdout, " %u", chunk->mirror_id[i]);
9560 fprintf(stdout, " PASS\n\n");
9569 * lfs_mirror_verify_file() - Verify a mirrored file.
9570 * @fname: Mirrored file name.
9571 * @mirror_ids: Specified mirror ids to be verified.
9572 * @ids_nr: Number of specified mirror ids.
9573 * @verbose: Verbose mode.
9575 * This function verifies that each SYNC mirror of a mirrored file
9576 * specified by @fname contains exactly the same data.
9578 * If @mirror_ids is specified, then the function will verify the
9579 * mirrors specified by @mirror_ids contain exactly the same data.
9581 * If @verbose is specified, then the function will print where the
9582 * differences are if the data do not match. Otherwise, it will
9583 * just return an error in that case.
9585 * Return: 0 on success or a negative error code on failure.
9588 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9591 struct verify_chunk chunks_array[1024] = { };
9592 struct llapi_layout *layout = NULL;
9596 int chunk_count = 0;
9602 if (stat(fname, &stbuf) < 0) {
9603 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9604 progname, fname, strerror(errno));
9609 if (!S_ISREG(stbuf.st_mode)) {
9610 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9616 if (stbuf.st_size == 0) {
9618 fprintf(stdout, "%s: '%s' file size is 0.\n",
9624 fd = open(fname, O_DIRECT | O_RDONLY);
9626 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9627 progname, fname, strerror(errno));
9632 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9634 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9635 progname, fname, strerror(errno));
9639 layout = llapi_layout_get_by_fd(fd, 0);
9640 if (layout == NULL) {
9641 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9642 progname, fname, strerror(errno));
9644 llapi_lease_release(fd);
9648 rc = llapi_layout_flags_get(layout, &flr_state);
9650 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9651 progname, fname, strerror(errno));
9656 flr_state &= LCM_FL_FLR_MASK;
9657 switch (flr_state) {
9660 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9661 progname, fname, llapi_layout_flags_string(flr_state));
9667 /* find out mirror chunks to be verified */
9668 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9669 ARRAY_SIZE(chunks_array));
9670 if (chunk_count < 0) {
9676 /* filter specified mirror ids */
9677 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9680 print_chunks(fname, chunks_array, chunk_count);
9682 for (idx = 0; idx < chunk_count; idx++) {
9683 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9686 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9688 PEXT(&chunks_array[idx].chunk),
9689 (unsigned long long)stbuf.st_size);
9693 if (chunks_array[idx].mirror_count == 0) {
9695 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9697 PEXT(&chunks_array[idx].chunk));
9699 fprintf(stderr, "skipped\n");
9703 fprintf(stderr, "failed\n");
9707 if (chunks_array[idx].mirror_count == 1) {
9710 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9712 PEXT(&chunks_array[idx].chunk),
9713 chunks_array[idx].mirror_id[0]);
9717 rc = llapi_lease_check(fd);
9718 if (rc != LL_LEASE_RDLCK) {
9719 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9724 /* verify one chunk */
9725 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9726 &chunks_array[idx], verbose);
9740 llapi_layout_free(layout);
9741 llapi_lease_release(fd);
9749 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9750 * @argc: The count of lfs mirror verify command line arguments.
9751 * @argv: Array of strings for lfs mirror verify command line arguments.
9753 * This function parses lfs mirror verify command and verifies the
9754 * specified mirrored file(s).
9756 * Return: 0 on success or a negative error code on failure.
9758 static inline int lfs_mirror_verify(int argc, char **argv)
9760 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9768 struct option long_opts[] = {
9769 { .val = 'o', .name = "only", .has_arg = required_argument },
9770 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9773 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9775 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9778 rc = parse_mirror_ids(mirror_ids,
9779 ARRAY_SIZE(mirror_ids),
9783 "%s: bad mirror ids '%s'.\n",
9790 "%s: at least 2 mirror ids needed with '--only' option.\n",
9800 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9801 progname, argv[optind - 1]);
9807 if (argc == optind) {
9808 fprintf(stderr, "%s: no file name given.\n", progname);
9813 if (ids_nr > 0 && argc > optind + 1) {
9815 "%s: '--only' cannot be used upon multiple files.\n",
9823 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9829 for (; optind < argc; optind++) {
9830 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9840 * lfs_mirror() - Parse and execute lfs mirror commands.
9841 * @argc: The count of lfs mirror command line arguments.
9842 * @argv: Array of strings for lfs mirror command line arguments.
9844 * This function parses lfs mirror commands and performs the
9845 * corresponding functions specified in mirror_cmdlist[].
9847 * Return: 0 on success or an error code on failure.
9849 static int lfs_mirror(int argc, char **argv)
9856 Parser_init("lfs-mirror > ", mirror_cmdlist);
9858 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9860 program_invocation_short_name = cmd;
9862 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9864 rc = Parser_commands();
9866 return rc < 0 ? -rc : rc;
9869 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9871 #if __BYTE_ORDER == __BIG_ENDIAN
9872 __swab16s(&attrs->lsa_valid);
9873 __swab64s(&attrs->lsa_size);
9874 __swab64s(&attrs->lsa_blocks);
9880 LFS_SOM_BLOCKS = 0x2,
9881 LFS_SOM_FLAGS = 0x4,
9882 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9886 static int lfs_getsom(int argc, char **argv)
9889 struct lustre_som_attrs *attrs;
9890 char buf[sizeof(*attrs) + 64];
9891 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9894 while ((c = getopt(argc, argv, "sbf")) != -1) {
9897 type = LFS_SOM_SIZE;
9900 type = LFS_SOM_BLOCKS;
9903 type = LFS_SOM_FLAGS;
9906 fprintf(stderr, "%s: invalid option '%c'\n",
9916 fprintf(stderr, "%s: %s\n",
9917 progname, argc == 0 ? "miss file target" :
9918 "input more than 2 files");
9923 attrs = (void *)buf;
9924 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9927 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
9928 argv[0], strerror(errno), errno);
9932 lustre_som_swab(attrs);
9935 case LFS_SOM_ATTR_ALL:
9936 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9937 path, (unsigned long long)attrs->lsa_size,
9938 (unsigned long long)attrs->lsa_blocks,
9942 printf("%llu\n", (unsigned long long)attrs->lsa_size);
9944 case LFS_SOM_BLOCKS:
9945 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
9948 printf("%x\n", attrs->lsa_valid);
9951 fprintf(stderr, "%s: unknown option\n", progname);
9959 * lfs_mirror_list_commands() - List lfs mirror commands.
9960 * @argc: The count of command line arguments.
9961 * @argv: Array of strings for command line arguments.
9963 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9965 * Return: 0 on success.
9967 static int lfs_mirror_list_commands(int argc, char **argv)
9969 char buffer[81] = "";
9971 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9977 static int lfs_list_commands(int argc, char **argv)
9979 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9981 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9986 int main(int argc, char **argv)
9990 /* Ensure that liblustreapi constructor has run */
9991 if (!llapi_liblustreapi_initialized())
9992 fprintf(stderr, "liblustreapi was not properly initialized\n");
9997 Parser_init("lfs > ", cmdlist);
9999 progname = program_invocation_short_name; /* Used in error messages */
10001 llapi_set_command_name(argv[1]);
10002 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10003 llapi_clear_command_name();
10005 rc = Parser_commands();
10008 return rc < 0 ? -rc : rc;
10011 #ifdef _LUSTRE_IDL_H_
10012 /* Everything we need here should be included by lustreapi.h. */
10013 # error "lfs should not depend on lustre_idl.h"
10014 #endif /* _LUSTRE_IDL_H_ */