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_cp(int argc, char **argv);
102 static int lfs_ls(int argc, char **argv);
103 static int lfs_poollist(int argc, char **argv);
104 static int lfs_changelog(int argc, char **argv);
105 static int lfs_changelog_clear(int argc, char **argv);
106 static int lfs_fid2path(int argc, char **argv);
107 static int lfs_path2fid(int argc, char **argv);
108 static int lfs_data_version(int argc, char **argv);
109 static int lfs_hsm_state(int argc, char **argv);
110 static int lfs_hsm_set(int argc, char **argv);
111 static int lfs_hsm_clear(int argc, char **argv);
112 static int lfs_hsm_action(int argc, char **argv);
113 static int lfs_hsm_archive(int argc, char **argv);
114 static int lfs_hsm_restore(int argc, char **argv);
115 static int lfs_hsm_release(int argc, char **argv);
116 static int lfs_hsm_remove(int argc, char **argv);
117 static int lfs_hsm_cancel(int argc, char **argv);
118 static int lfs_swap_layouts(int argc, char **argv);
119 static int lfs_mv(int argc, char **argv);
120 static int lfs_ladvise(int argc, char **argv);
121 static int lfs_getsom(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_read(int argc, char **argv);
128 static inline int lfs_mirror_write(int argc, char **argv);
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>> [--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 "\tnew_file: This option indicates the layout of the split\n"
285 "\t mirror will be stored into. If not specified,\n"
286 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
287 "\t will be used.\n" },
288 { .pc_name = "read", .pc_func = lfs_mirror_read,
289 .pc_help = "Read the content of a specified mirror of a file.\n"
290 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
291 "[--outfile|-o <output_file>] <mirrored_file>\n" },
292 { .pc_name = "write", .pc_func = lfs_mirror_write,
293 .pc_help = "Write to a specified mirror of a file.\n"
294 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
295 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
296 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
297 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
298 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
299 "<mirrored file> [<mirrored file2>...]\n"},
300 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
301 .pc_help = "Verify mirrored file(s).\n"
302 "usage: lfs mirror verify "
303 "[--only <mirror_id,mirror_id2[,...]>] "
304 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
305 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
306 .pc_help = "list commands supported by lfs mirror"},
307 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
308 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
309 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
313 /* all available commands */
314 command_t cmdlist[] = {
315 {"setstripe", lfs_setstripe, 0,
316 "To create a file with specified striping/composite layout, or\n"
317 "create/replace the default layout on an existing directory:\n"
318 SSM_CMD_COMMON("setstripe")
319 " <directory|filename>\n"
321 "To add component(s) to an existing composite file:\n"
322 SSM_CMD_COMMON("setstripe --component-add")
324 "To totally delete the default striping from an existing directory:\n"
325 "usage: setstripe -d <directory>\n"
327 "To create a mirrored file or set s default mirror layout on a directory:\n"
328 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
330 "To delete the last component(s) from an existing composite file\n"
331 "(note that this will also delete any data in those components):\n"
332 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
333 " [--component-flags|-F <comp_flags>]\n"
335 "\tcomp_id: Unique component ID to delete\n"
336 "\tcomp_flags: 'init' indicating all instantiated components\n"
337 "\t '^init' indicating all uninstantiated components\n"
338 "\t-I and -F cannot be specified at the same time\n"},
339 {"getstripe", lfs_getstripe, 0,
340 "To list the layout pattern for a given file or files in a\n"
341 "directory or recursively for all files in a directory tree.\n"
342 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
343 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
344 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
345 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
346 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
347 " [--component-id[=comp_id]|-I[comp_id]]\n"
348 " [--component-flags[=comp_flags]]\n"
349 " [--component-count]\n"
350 " [--component-start[=[+-]comp_start]]\n"
351 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
352 " [[!] --mirror-index=[+-]<index> |\n"
353 " [!] --mirror-id=[+-]<id>]\n"
354 " <directory|filename> ..."},
355 {"setdirstripe", lfs_setdirstripe, 0,
356 "To create a striped directory on a specified MDT. This can only\n"
357 "be done on MDT0 with the right of administrator.\n"
358 "usage: setdirstripe [OPTION] <directory>\n"
360 {"getdirstripe", lfs_getdirstripe, 0,
361 "To list the layout pattern info for a given directory\n"
362 "or recursively for all directories in a directory tree.\n"
363 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
364 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
365 " [--recursive|-r] [--yaml|-y]\n"
366 " [--default|-D] <dir> ..."},
367 {"mkdir", lfs_setdirstripe, 0,
368 "To create a striped directory on a specified MDT. This can only\n"
369 "be done on MDT0 with the right of administrator.\n"
370 "usage: mkdir [OPTION] <directory>\n"
372 {"rm_entry", lfs_rmentry, 0,
373 "To remove the name entry of the remote directory. Note: This\n"
374 "command will only delete the name entry, i.e. the remote directory\n"
375 "will become inaccessable after this command. This can only be done\n"
376 "by the administrator\n"
377 "usage: rm_entry <dir>\n"},
378 {"pool_list", lfs_poollist, 0,
379 "List pools or pool OSTs\n"
380 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
381 {"find", lfs_find, 0,
382 "find files matching given attributes recursively in directory tree.\n"
383 "usage: find <directory|filename> ...\n"
384 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
385 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
386 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
387 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
388 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
389 " [[!] --stripe-count|-c [+-]<stripes>]\n"
390 " [[!] --stripe-index|-i <index,...>]\n"
391 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
392 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
393 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
394 " [[!] --projid <projid>]\n"
395 " [[!] --layout|-L released,raid0,mdt]\n"
396 " [[!] --component-count [+-]<comp_cnt>]\n"
397 " [[!] --component-start [+-]N[kMGTPE]]\n"
398 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
399 " [[!] --component-flags <comp_flags>]\n"
400 " [[!] --mirror-count|-N [+-]<n>]\n"
401 " [[!] --mirror-state <[^]state>]\n"
402 " [[!] --mdt-count|-T [+-]<stripes>]\n"
403 " [[!] --mdt-hash|-H <hashtype>\n"
404 "\t !: used before an option indicates 'NOT' requested attribute\n"
405 "\t -: used before a value indicates less than requested value\n"
406 "\t +: used before a value indicates more than requested value\n"
407 "\thashtype: hash type of the striped directory.\n"
408 "\t fnv_1a_64 FNV-1a hash algorithm\n"
409 "\t all_char sum of characters % MDT_COUNT\n"},
410 {"check", lfs_check, 0,
411 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
412 "or all the servers (MGTs, MDTs and OSTs).\n"
413 "usage: check <mgts|osts|mdts|all>"},
414 {"osts", lfs_osts, 0, "list OSTs connected to client "
415 "[for specified path only]\n" "usage: osts [path]"},
416 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
417 "[for specified path only]\n" "usage: mdts [path]"},
419 "report filesystem disk space usage or inodes usage"
420 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
421 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
422 {"getname", lfs_getname, 0, "list instances and specified mount points "
423 "[for specified path only]\n"
424 "Usage: getname [-h]|[path ...] "},
425 #ifdef HAVE_SYS_QUOTA_H
426 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
427 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
428 " -b <block-softlimit> -B <block-hardlimit>\n"
429 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
430 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
431 " [--block-softlimit <block-softlimit>]\n"
432 " [--block-hardlimit <block-hardlimit>]\n"
433 " [--inode-softlimit <inode-softlimit>]\n"
434 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
435 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
436 " [--block-grace <block-grace>]\n"
437 " [--inode-grace <inode-grace>] <filesystem>\n"
438 " setquota <-U|-G|-P>\n"
439 " -b <block-softlimit> -B <block-hardlimit>\n"
440 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
441 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
442 " [--block-softlimit <block-softlimit>]\n"
443 " [--block-hardlimit <block-hardlimit>]\n"
444 " [--inode-softlimit <inode-softlimit>]\n"
445 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
446 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
448 " -b can be used instead of --block-softlimit/--block-grace\n"
449 " -B can be used instead of --block-hardlimit\n"
450 " -i can be used instead of --inode-softlimit/--inode-grace\n"
451 " -I can be used instead of --inode-hardlimit\n"
452 " -d can be used instead of --default\n\n"
453 "Note: The total quota space will be split into many qunits and\n"
454 " balanced over all server targets, the minimal qunit size is\n"
455 " 1M bytes for block space and 1K inodes for inode space.\n\n"
456 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
457 " Quota space rebalancing process will stop when this mininum\n"
458 " value is reached. As a result, quota exceeded can be returned\n"
459 " while many targets still have 1MB or 1K inodes of spare\n"
461 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
462 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
464 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
465 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
466 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
467 {"project", lfs_project, 0,
468 "Change or list project attribute for specified file or directory.\n"
469 "usage: project [-d|-r] <file|directory...>\n"
470 " list project ID and flags on file(s) or directories\n"
471 " project [-p id] [-s] [-r] <file|directory...>\n"
472 " set project ID and/or inherit flag for specified file(s) or directories\n"
473 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
474 " check project ID and flags on file(s) or directories, print outliers\n"
475 " project -C [-r] [-k] <file|directory...>\n"
476 " clear the project inherit flag and ID on the file or directory\n"
479 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
480 "usage: flushctx [-k] [mountpoint...]"},
482 "Remote user copy files and directories.\n"
483 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
485 "Remote user list directory contents.\n"
486 "usage: ls [OPTION]... [FILE]..."},
487 {"changelog", lfs_changelog, 0,
488 "Show the metadata changes on an MDT."
489 "\nusage: changelog <mdtname> [startrec [endrec]]"},
490 {"changelog_clear", lfs_changelog_clear, 0,
491 "Indicate that old changelog records up to <endrec> are no longer of "
492 "interest to consumer <id>, allowing the system to free up space.\n"
493 "An <endrec> of 0 means all records.\n"
494 "usage: changelog_clear <mdtname> <id> <endrec>"},
495 {"fid2path", lfs_fid2path, 0,
496 "Resolve the full path(s) for given FID(s). For a specific hardlink "
497 "specify link number <linkno>.\n"
498 /* "For a historical link name, specify changelog record <recno>.\n" */
499 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
500 /* [ --rec <recno> ] */ },
501 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
502 "usage: path2fid [--parents] <path> ..."},
503 {"data_version", lfs_data_version, 0, "Display file data version for "
504 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
505 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
506 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
507 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
508 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
509 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
510 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
512 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
513 "[--archived] [--lost] <file> ..."},
514 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
515 "given files.\n" "usage: hsm_action <file> ..."},
516 {"hsm_archive", lfs_hsm_archive, 0,
517 "Archive file to external storage.\n"
518 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
520 {"hsm_restore", lfs_hsm_restore, 0,
521 "Restore file from external storage.\n"
522 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
523 {"hsm_release", lfs_hsm_release, 0,
524 "Release files from Lustre.\n"
525 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
526 {"hsm_remove", lfs_hsm_remove, 0,
527 "Remove file copy from external storage.\n"
528 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
530 " (FILE [FILE ...] | "
531 "--mntpath MOUNTPATH FID [FID ...])\n"
533 "Note: To remove an archived copy of a file already deleted from a "
535 "--mntpath option and a list of FIDs must be specified"
537 {"hsm_cancel", lfs_hsm_cancel, 0,
538 "Cancel requests related to specified files.\n"
539 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
540 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
541 "usage: swap_layouts <path1> <path2>"},
542 {"migrate", lfs_setstripe_migrate, 0,
543 "migrate a directory between MDTs.\n"
544 "usage: migrate [--mdt|-m] <start_mdt_index>\n"
545 " [--mdt-count|-c] <stripe_count>\n"
546 " [--mdt-hash|-H] <hash_type>\n"
549 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
550 " it's the MDT index of first stripe\n"
551 "\tmdt_count: number of MDTs to stripe a directory over\n"
552 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
553 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
554 " all_char sum of characters % MDT_COUNT\n"
556 "migrate file objects from one OST "
557 "layout\nto another (may be not safe with concurent writes).\n"
559 "[--stripe-count|-c] <stripe_count>\n"
560 " [--stripe-index|-i] <start_ost_index>\n"
561 " [--stripe-size|-S] <stripe_size>\n"
562 " [--pool|-p] <pool_name>\n"
563 " [--ost|-o] <ost_indices>\n"
565 " [--non-block|-n]\n"
566 " [--non-direct|-D]\n"
567 " <file|directory>\n"
568 "\tstripe_count: number of OSTs to stripe a file over\n"
569 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
570 "\tstripe_size: number of bytes to store before moving to the next OST\n"
571 "\tpool_name: name of the predefined pool of OSTs\n"
572 "\tost_indices: OSTs to stripe over, in order\n"
573 "\tblock: wait for the operation to return before continuing\n"
574 "\tnon-block: do not wait for the operation to return\n"
575 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
577 "To move directories between MDTs. This command is deprecated, "
578 "use \"migrate\" instead.\n"
579 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
581 {"ladvise", lfs_ladvise, 0,
582 "Provide servers with advice about access patterns for a file.\n"
583 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
584 " [--background|-b] [--unset|-u]\n\n"
585 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
586 " {[--mode|-m [READ,WRITE]}\n"
588 {"mirror", lfs_mirror, mirror_cmdlist,
589 "lfs commands used to manage files with mirrored components:\n"
590 "lfs mirror create - create a mirrored file or directory\n"
591 "lfs mirror extend - add mirror(s) to an existing file\n"
592 "lfs mirror split - split a mirror from an existing mirrored file\n"
593 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
594 "lfs mirror read - read a mirror content of a mirrored file\n"
595 "lfs mirror write - write to a mirror of a mirrored file\n"
596 "lfs mirror verify - verify mirrored file(s)\n"},
597 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
598 "usage: getsom [-s] [-b] [-f] <path>\n"
599 "\t-s: Only show the size value of the SOM data for a given file\n"
600 "\t-b: Only show the blocks value of the SOM data for a given file\n"
601 "\t-f: Only show the flags value of the SOM data for a given file\n"},
602 {"help", Parser_help, 0, "help"},
603 {"exit", Parser_quit, 0, "quit"},
604 {"quit", Parser_quit, 0, "quit"},
605 {"--version", Parser_version, 0,
606 "output build version of the utility and exit"},
607 {"--list-commands", lfs_list_commands, 0,
608 "list commands supported by the utility and exit"},
613 static int check_hashtype(const char *hashtype)
617 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
618 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
625 static const char *error_loc = "syserror";
628 MIGRATION_NONBLOCK = 1 << 0,
629 MIGRATION_MIRROR = 1 << 1,
630 MIGRATION_NONDIRECT = 1 << 2,
633 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
634 struct llapi_layout *layout);
637 migrate_open_files(const char *name, __u64 migration_flags,
638 const struct llapi_stripe_param *param,
639 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
646 char parent[PATH_MAX];
647 char volatile_file[PATH_MAX];
653 if (param == NULL && layout == NULL) {
654 error_loc = "layout information";
658 /* search for file directory pathname */
659 if (strlen(name) > sizeof(parent) - 1) {
660 error_loc = "source file name";
664 strncpy(parent, name, sizeof(parent));
665 ptr = strrchr(parent, '/');
667 if (getcwd(parent, sizeof(parent)) == NULL) {
668 error_loc = "getcwd";
672 if (ptr == parent) /* leading '/' */
677 /* open file, direct io */
678 /* even if the file is only read, WR mode is nedeed to allow
679 * layout swap on fd */
681 if (!(migration_flags & MIGRATION_NONDIRECT))
683 fd = open(name, rflags);
686 error_loc = "cannot open source file";
690 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
692 error_loc = "cannot get MDT index";
697 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
698 mode_t open_mode = S_IRUSR | S_IWUSR;
700 random_value = random();
701 rc = snprintf(volatile_file, sizeof(volatile_file),
702 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
703 mdt_index, random_value);
704 if (rc >= sizeof(volatile_file)) {
709 /* create, open a volatile file, use caching (ie no directio) */
711 fdv = llapi_file_open_param(volatile_file, open_flags,
714 fdv = lfs_component_create(volatile_file, open_flags,
716 } while (fdv < 0 && (rc = fdv) == -EEXIST);
719 error_loc = "cannot create volatile file";
723 /* In case the MDT does not support creation of volatile files
724 * we should try to unlink it. */
725 (void)unlink(volatile_file);
727 /* Not-owner (root?) special case.
728 * Need to set owner/group of volatile file like original.
729 * This will allow to pass related check during layout_swap.
734 error_loc = "cannot stat source file";
738 rc = fstat(fdv, &stv);
741 error_loc = "cannot stat volatile";
745 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
746 rc = fchown(fdv, st.st_uid, st.st_gid);
749 error_loc = "cannot change ownwership of volatile";
768 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
770 struct llapi_layout *layout;
771 size_t buf_size = 4 * 1024 * 1024;
780 layout = llapi_layout_get_by_fd(fd_src, 0);
781 if (layout != NULL) {
782 uint64_t stripe_size;
784 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
786 buf_size = stripe_size;
788 llapi_layout_free(layout);
791 /* Use a page-aligned buffer for direct I/O */
792 rc = posix_memalign(&buf, getpagesize(), buf_size);
797 /* read new data only if we have written all
798 * previously read data */
801 rc = check_file(fd_src);
806 rsize = read(fd_src, buf, buf_size);
818 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
837 static int migrate_copy_timestamps(int fd, int fdv)
841 if (fstat(fd, &st) == 0) {
842 struct timeval tv[2] = {
843 {.tv_sec = st.st_atime},
844 {.tv_sec = st.st_mtime}
847 return futimes(fdv, tv);
853 static int migrate_block(int fd, int fdv)
860 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
862 error_loc = "cannot get dataversion";
870 /* The grouplock blocks all concurrent accesses to the file.
871 * It has to be taken after llapi_get_data_version as it would
873 rc = llapi_group_lock(fd, gid);
875 error_loc = "cannot get group lock";
879 rc = migrate_copy_data(fd, fdv, NULL);
881 error_loc = "data copy failed";
885 /* Make sure we keep original atime/mtime values */
886 rc = migrate_copy_timestamps(fd, fdv);
888 error_loc = "timestamp copy failed";
893 * for a migration we need to check data version on file did
896 * Pass in gid=0 since we already own grouplock. */
897 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
898 SWAP_LAYOUTS_CHECK_DV1);
900 error_loc = "file changed";
903 error_loc = "cannot swap layout";
908 rc2 = llapi_group_unlock(fd, gid);
909 if (rc2 < 0 && rc == 0) {
910 error_loc = "unlock group lock";
918 * Internal helper for migrate_copy_data(). Check lease and report error if
921 * \param[in] fd File descriptor on which to check the lease.
923 * \retval 0 Migration can keep on going.
924 * \retval -errno Error occurred, abort migration.
926 static int check_lease(int fd)
930 rc = llapi_lease_check(fd);
932 return 0; /* llapi_check_lease returns > 0 on success. */
937 static int migrate_nonblock(int fd, int fdv)
943 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
945 error_loc = "cannot get data version";
949 rc = migrate_copy_data(fd, fdv, check_lease);
951 error_loc = "data copy failed";
955 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
957 error_loc = "cannot get data version";
963 error_loc = "source file changed";
967 /* Make sure we keep original atime/mtime values */
968 rc = migrate_copy_timestamps(fd, fdv);
970 error_loc = "timestamp copy failed";
977 static int lfs_component_set(char *fname, int comp_id,
978 __u32 flags, __u32 neg_flags)
981 __u32 flags_array[2];
986 ids[count] = comp_id;
987 flags_array[count] = flags;
992 ids[count] = comp_id;
993 flags_array[count] = neg_flags | LCME_FL_NEG;
997 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1000 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1001 progname, comp_id, fname, flags, neg_flags);
1006 static int lfs_component_del(char *fname, __u32 comp_id,
1007 __u32 flags, __u32 neg_flags)
1011 if (flags && neg_flags)
1014 if (!flags && neg_flags)
1015 flags = neg_flags | LCME_FL_NEG;
1017 if ((flags && comp_id) || (!flags && !comp_id))
1020 /* LCME_FL_INIT is the only supported flag in PFL */
1022 if (flags & ~LCME_KNOWN_FLAGS) {
1024 "%s setstripe: unknown flags %#x\n",
1028 } else if (comp_id > LCME_ID_MAX) {
1029 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1034 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1037 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1038 progname, comp_id, fname, strerror(errno));
1042 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1049 rc = llapi_layout_file_comp_add(fname, layout);
1051 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1052 fname, strerror(errno));
1056 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1057 struct llapi_layout *layout)
1065 fd = lstat(fname, &st);
1066 if (fd == 0 && S_ISDIR(st.st_mode))
1067 open_flags = O_DIRECTORY | O_RDONLY;
1069 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1071 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1072 S_ISDIR(st.st_mode) ?
1073 "set default composite layout for" :
1074 "create composite file",
1075 fname, strerror(errno));
1079 static int lfs_migrate(char *name, __u64 migration_flags,
1080 struct llapi_stripe_param *param,
1081 struct llapi_layout *layout)
1087 rc = migrate_open_files(name, migration_flags, param, layout,
1092 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1093 /* Blocking mode (forced if servers do not support file lease).
1094 * It is also the default mode, since we cannot distinguish
1095 * between a broken lease and a server that does not support
1096 * atomic swap/close (LU-6785) */
1097 rc = migrate_block(fd, fdv);
1101 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1103 error_loc = "cannot get lease";
1107 rc = migrate_nonblock(fd, fdv);
1109 llapi_lease_release(fd);
1113 /* Atomically put lease, swap layouts and close.
1114 * for a migration we need to check data version on file did
1116 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1118 error_loc = "cannot swap layout";
1130 fprintf(stderr, "error: %s: %s: %s: %s\n",
1131 progname, name, error_loc, strerror(-rc));
1135 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1144 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1148 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1149 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1150 const char *comp_name = comp_flags_table[i].cfn_name;
1152 if (strcmp(name, comp_name) == 0) {
1153 *flags |= comp_flag;
1155 } else if (strncmp(name, "^", 1) == 0 &&
1156 strcmp(name + 1, comp_name) == 0) {
1157 *neg_flags |= comp_flag;
1162 llapi_printf(LLAPI_MSG_ERROR,
1163 "%s: component flag '%s' not supported\n",
1169 if (!*flags && !*neg_flags)
1172 /* don't allow to set and exclude the same flag */
1173 if (*flags & *neg_flags)
1179 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1187 if (strncmp(string, "^", 1) == 0) {
1188 *neg_state = llapi_layout_string_flags(string + 1);
1189 if (*neg_state != 0)
1192 *state = llapi_layout_string_flags(string);
1197 llapi_printf(LLAPI_MSG_ERROR,
1198 "%s: mirrored file state '%s' not supported\n",
1204 * struct mirror_args - Command-line arguments for mirror(s).
1205 * @m_count: Number of mirrors to be created with this layout.
1206 * @m_flags: Mirror level flags, only 'prefer' is supported.
1207 * @m_layout: Mirror layout.
1208 * @m_file: A victim file. Its layout will be split and used as a mirror.
1209 * @m_next: Point to the next node of the list.
1211 * Command-line arguments for mirror(s) will be parsed and stored in
1212 * a linked list that consists of this structure.
1214 struct mirror_args {
1217 struct llapi_layout *m_layout;
1219 struct mirror_args *m_next;
1222 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1227 rc = llapi_layout_comp_flags_get(layout, &flags);
1231 if (flags & LCME_FL_NEG) {
1232 fprintf(stderr, "error: %s: negative flags are not supported\n",
1237 if (flags & LCME_FL_STALE) {
1238 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1239 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1243 return LLAPI_LAYOUT_ITER_CONT;
1246 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1248 uint64_t start, end;
1252 /* LU-10112: do not support dom+flr in phase 1 */
1253 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1257 rc = llapi_layout_pattern_get(layout, &pattern);
1261 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1262 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1267 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1271 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1275 if (end != LUSTRE_EOF) {
1276 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1281 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1286 * enum mirror_flags - Flags for extending a mirrored file.
1287 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1288 * in case the victim file(s) contains the same data as the
1289 * original mirrored file.
1290 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1291 * @MF_COMP_ID: specified component id instead of mirror id
1293 * Flags for extending a mirrored file.
1302 * mirror_create_sanity_check() - Check mirror list.
1303 * @list: A linked list that stores the mirror arguments.
1305 * This function does a sanity check on @list for creating
1308 * Return: 0 on success or a negative error code on failure.
1310 static int mirror_create_sanity_check(const char *fname,
1311 struct mirror_args *list)
1314 bool has_m_file = false;
1315 bool has_m_layout = false;
1321 struct llapi_layout *layout;
1323 layout = llapi_layout_get_by_path(fname, 0);
1326 "error: %s: file '%s' couldn't get layout\n",
1331 rc = mirror_sanity_check_one(layout);
1332 llapi_layout_free(layout);
1338 while (list != NULL) {
1339 if (list->m_file != NULL) {
1341 llapi_layout_free(list->m_layout);
1344 llapi_layout_get_by_path(list->m_file, 0);
1345 if (list->m_layout == NULL) {
1347 "error: %s: file '%s' has no layout\n",
1348 progname, list->m_file);
1352 has_m_layout = true;
1353 if (list->m_layout == NULL) {
1354 fprintf(stderr, "error: %s: no mirror layout\n",
1360 rc = mirror_sanity_check_one(list->m_layout);
1364 list = list->m_next;
1367 if (has_m_file && has_m_layout) {
1369 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1377 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1379 __u32 mirror_flags = *(__u32 *)cbdata;
1383 rc = llapi_layout_comp_flags_get(layout, &flags);
1388 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1393 return LLAPI_LAYOUT_ITER_CONT;
1397 * mirror_create() - Create a mirrored file.
1398 * @fname: The file to be created.
1399 * @mirror_list: A linked list that stores the mirror arguments.
1401 * This function creates a mirrored file @fname with the mirror(s)
1402 * from @mirror_list.
1404 * Return: 0 on success or a negative error code on failure.
1406 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1408 struct llapi_layout *layout = NULL;
1409 struct mirror_args *cur_mirror = NULL;
1410 uint16_t mirror_count = 0;
1414 rc = mirror_create_sanity_check(NULL, mirror_list);
1418 cur_mirror = mirror_list;
1419 while (cur_mirror != NULL) {
1420 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1422 &cur_mirror->m_flags);
1425 fprintf(stderr, "%s: failed to set mirror flags\n",
1430 for (i = 0; i < cur_mirror->m_count; i++) {
1431 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1434 fprintf(stderr, "error: %s: "
1435 "merge layout failed: %s\n",
1436 progname, strerror(errno));
1440 mirror_count += cur_mirror->m_count;
1441 cur_mirror = cur_mirror->m_next;
1444 if (layout == NULL) {
1445 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1449 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1452 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1453 progname, strerror(errno));
1457 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1465 llapi_layout_free(layout);
1470 * Compare files and check lease on @fd.
1472 * \retval bytes number of bytes are the same
1474 static ssize_t mirror_file_compare(int fd, int fdv)
1476 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1478 ssize_t bytes_done = 0;
1479 ssize_t bytes_read = 0;
1481 buf = malloc(buflen * 2);
1486 if (!llapi_lease_check(fd)) {
1487 bytes_done = -EBUSY;
1491 bytes_read = read(fd, buf, buflen);
1492 if (bytes_read <= 0)
1495 if (bytes_read != read(fdv, buf + buflen, buflen))
1498 /* XXX: should compute the checksum on each buffer and then
1499 * compare checksum to avoid cache collision */
1500 if (memcmp(buf, buf + buflen, bytes_read))
1503 bytes_done += bytes_read;
1511 static int mirror_extend_file(const char *fname, const char *victim_file,
1512 enum mirror_flags mirror_flags)
1517 struct stat stbuf_v;
1518 struct ll_ioc_lease *data = NULL;
1521 fd = open(fname, O_RDWR);
1523 error_loc = "open source file";
1528 fdv = open(victim_file, O_RDWR);
1530 error_loc = "open target file";
1535 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1536 error_loc = "stat source or target file";
1541 if (stbuf.st_dev != stbuf_v.st_dev) {
1542 error_loc = "stat source and target file";
1547 /* mirrors should be of the same size */
1548 if (stbuf.st_size != stbuf_v.st_size) {
1549 error_loc = "file sizes don't match";
1554 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1556 error_loc = "cannot get lease";
1560 if (!(mirror_flags & MF_NO_VERIFY)) {
1562 /* mirrors should have the same contents */
1563 ret = mirror_file_compare(fd, fdv);
1564 if (ret != stbuf.st_size) {
1565 error_loc = "file busy or contents don't match";
1566 rc = ret < 0 ? ret : -EINVAL;
1571 /* Get rid of caching pages from clients */
1572 rc = llapi_file_flush(fd);
1574 error_loc = "cannot get data version";
1578 rc = llapi_file_flush(fdv);
1580 error_loc = "cannot get data version";
1585 /* Make sure we keep original atime/mtime values */
1586 rc = migrate_copy_timestamps(fd, fdv);
1588 error_loc = "cannot copy timestamp";
1592 /* Atomically put lease, merge layouts and close. */
1593 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1595 error_loc = "memory allocation";
1598 data->lil_mode = LL_LEASE_UNLCK;
1599 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1600 data->lil_count = 1;
1601 data->lil_ids[0] = fdv;
1602 rc = llapi_lease_set(fd, data);
1604 error_loc = "cannot merge layout";
1606 } else if (rc == 0) {
1608 error_loc = "lost lease lock";
1621 (void) unlink(victim_file);
1623 fprintf(stderr, "error: %s: %s: %s: %s\n",
1624 progname, fname, error_loc, strerror(-rc));
1628 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1630 struct ll_ioc_lease *data = NULL;
1635 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1639 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1641 error_loc = "cannot get lease";
1645 rc = migrate_nonblock(fd, fdv);
1647 llapi_lease_release(fd);
1651 /* Atomically put lease, merge layouts and close. */
1652 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1654 error_loc = "memory allocation";
1657 data->lil_mode = LL_LEASE_UNLCK;
1658 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1659 data->lil_count = 1;
1660 data->lil_ids[0] = fdv;
1661 rc = llapi_lease_set(fd, data);
1663 error_loc = "cannot merge layout";
1665 } else if (rc == 0) {
1667 error_loc = "lost lease lock";
1680 fprintf(stderr, "error: %s: %s: %s: %s\n",
1681 progname, name, error_loc, strerror(-rc));
1685 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1686 enum mirror_flags mirror_flags)
1690 rc = mirror_create_sanity_check(fname, mirror_list);
1694 while (mirror_list) {
1695 if (mirror_list->m_file != NULL) {
1696 rc = mirror_extend_file(fname, mirror_list->m_file,
1699 __u32 mirror_count = mirror_list->m_count;
1701 while (mirror_count > 0) {
1702 rc = mirror_extend_layout(fname,
1703 mirror_list->m_layout);
1713 mirror_list = mirror_list->m_next;
1719 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1724 rc = llapi_layout_mirror_id_get(layout, &id);
1728 if ((__u16)id == *(__u16 *)cbdata)
1729 return LLAPI_LAYOUT_ITER_STOP;
1731 return LLAPI_LAYOUT_ITER_CONT;
1734 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1739 rc = llapi_layout_comp_id_get(layout, &id);
1743 if (id == *(__u32 *)cbdata)
1744 return LLAPI_LAYOUT_ITER_STOP;
1746 return LLAPI_LAYOUT_ITER_CONT;
1748 static int mirror_split(const char *fname, __u32 id,
1749 enum mirror_flags mflags, const char *victim_file)
1751 struct llapi_layout *layout;
1752 char parent[PATH_MAX];
1753 char victim[PATH_MAX];
1754 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1756 struct ll_ioc_lease *data;
1757 uint16_t mirror_count;
1762 /* check fname contains mirror with mirror_id/comp_id */
1763 layout = llapi_layout_get_by_path(fname, 0);
1766 "error %s: file '%s' couldn't get layout\n",
1771 rc = mirror_sanity_check_one(layout);
1775 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1778 "error %s: file '%s' couldn't get mirror count\n",
1782 if (mirror_count < 2) {
1784 "error %s: file '%s' has %d component, cannot split\n",
1785 progname, fname, mirror_count);
1789 if (mflags & MF_COMP_ID) {
1790 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1791 id = mirror_id_of(id);
1793 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1796 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1799 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1801 "error %s: file '%s' does not contain mirror with id %u\n",
1802 progname, fname, id);
1806 fd = open(fname, O_RDWR);
1809 "error %s: open file '%s' failed: %s\n",
1810 progname, fname, strerror(errno));
1814 /* get victim file directory pathname */
1815 if (strlen(fname) > sizeof(parent) - 1) {
1816 fprintf(stderr, "error %s: file name of '%s' too long\n",
1821 strncpy(parent, fname, sizeof(parent));
1822 ptr = strrchr(parent, '/');
1824 if (getcwd(parent, sizeof(parent)) == NULL) {
1825 fprintf(stderr, "error %s: getcwd failed: %s\n",
1826 progname, strerror(errno));
1836 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1838 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1843 if (victim_file == NULL) {
1844 /* use a temp file to store the splitted layout */
1845 if (mflags & MF_DESTROY) {
1846 fdv = llapi_create_volatile_idx(parent, mdt_index,
1847 O_LOV_DELAY_CREATE);
1849 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1851 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1854 /* user specified victim file */
1855 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1860 "error %s: create victim file failed: %s\n",
1861 progname, strerror(errno));
1865 /* get lease lock of fname */
1866 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1869 "error %s: cannot get lease of file '%s': %d\n",
1870 progname, fname, rc);
1874 /* Atomatically put lease, split layouts and close. */
1875 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1881 data->lil_mode = LL_LEASE_UNLCK;
1882 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1883 data->lil_count = 2;
1884 data->lil_ids[0] = fdv;
1885 data->lil_ids[1] = id;
1886 rc = llapi_lease_set(fd, data);
1888 if (rc == 0) /* lost lease lock */
1891 "error %s: cannot split '%s': %s\n",
1892 progname, fname, strerror(-rc));
1903 llapi_layout_free(layout);
1908 * Parse a string containing an target index list into an array of integers.
1910 * The input string contains a comma delimited list of individual
1911 * indices and ranges, for example "1,2-4,7". Add the indices into the
1912 * \a tgts array and remove duplicates.
1914 * \param[out] tgts array to store indices in
1915 * \param[in] size size of \a tgts array
1916 * \param[in] offset starting index in \a tgts
1917 * \param[in] arg string containing OST index list
1919 * \retval positive number of indices in \a tgts
1920 * \retval -EINVAL unable to parse \a arg
1922 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1926 int slots = size - offset;
1933 end_of_loop = false;
1934 while (!end_of_loop) {
1938 char *endptr = NULL;
1942 ptr = strchrnul(arg, ',');
1944 end_of_loop = *ptr == '\0';
1947 start_index = strtol(arg, &endptr, 0);
1948 if (endptr == arg) /* no data at all */
1950 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1953 end_index = start_index;
1954 if (*endptr == '-') {
1955 end_index = strtol(endptr + 1, &endptr, 0);
1956 if (*endptr != '\0')
1958 if (end_index < start_index)
1962 for (i = start_index; i <= end_index && slots > 0; i++) {
1965 /* remove duplicate */
1966 for (j = 0; j < offset; j++) {
1970 if (j == offset) { /* no duplicate */
1975 if (slots == 0 && i < end_index)
1983 if (!end_of_loop && ptr != NULL)
1986 return rc < 0 ? rc : nr;
1989 struct lfs_setstripe_args {
1990 unsigned long long lsa_comp_end;
1991 unsigned long long lsa_stripe_size;
1992 long long lsa_stripe_count;
1993 long long lsa_stripe_off;
1994 __u32 lsa_comp_flags;
1995 __u32 lsa_comp_neg_flags;
1996 unsigned long long lsa_pattern;
1997 unsigned int lsa_mirror_count;
1999 bool lsa_first_comp;
2001 char *lsa_pool_name;
2004 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2006 unsigned int mirror_count = lsa->lsa_mirror_count;
2007 bool first_comp = lsa->lsa_first_comp;
2009 memset(lsa, 0, sizeof(*lsa));
2011 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2012 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2013 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2014 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2015 lsa->lsa_pool_name = NULL;
2017 lsa->lsa_mirror_count = mirror_count;
2018 lsa->lsa_first_comp = first_comp;
2022 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2023 * @lsa: Stripe options to be initialized and inherited.
2025 * This function initializes stripe options in @lsa and inherit
2026 * stripe_size, stripe_count and OST pool_name options.
2030 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2032 unsigned long long stripe_size;
2033 long long stripe_count;
2034 char *pool_name = NULL;
2036 stripe_size = lsa->lsa_stripe_size;
2037 stripe_count = lsa->lsa_stripe_count;
2038 pool_name = lsa->lsa_pool_name;
2040 setstripe_args_init(lsa);
2042 lsa->lsa_stripe_size = stripe_size;
2043 lsa->lsa_stripe_count = stripe_count;
2044 lsa->lsa_pool_name = pool_name;
2047 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2049 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2050 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2051 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2052 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2053 lsa->lsa_pool_name != NULL ||
2054 lsa->lsa_comp_end != 0);
2058 * comp_args_to_layout() - Create or extend a composite layout.
2059 * @composite: Pointer to the composite layout.
2060 * @lsa: Stripe options for the new component.
2062 * This function creates or extends a composite layout by adding a new
2063 * component with stripe options from @lsa.
2065 * Return: 0 on success or an error code on failure.
2067 static int comp_args_to_layout(struct llapi_layout **composite,
2068 struct lfs_setstripe_args *lsa,
2071 struct llapi_layout *layout = *composite;
2072 uint64_t prev_end = 0;
2075 if (layout == NULL) {
2076 layout = llapi_layout_alloc();
2077 if (layout == NULL) {
2078 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2082 *composite = layout;
2086 /* Get current component extent, current component
2087 * must be the tail component. */
2088 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2090 fprintf(stderr, "Get comp extent failed. %s\n",
2095 if (lsa->lsa_first_comp)
2098 if (lsa->lsa_first_comp)
2099 rc = llapi_layout_add_first_comp(layout);
2101 rc = llapi_layout_comp_add(layout);
2103 fprintf(stderr, "Add component failed. %s\n",
2108 /* reset lsa_first_comp */
2109 lsa->lsa_first_comp = false;
2112 rc = llapi_layout_comp_extent_set(layout, prev_end,
2115 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2116 prev_end, lsa->lsa_comp_end, strerror(errno));
2121 /* Data-on-MDT component setting */
2122 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2123 /* In case of Data-on-MDT patterns the only extra option
2124 * applicable is stripe size option. */
2125 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2126 fprintf(stderr, "Option 'stripe-count' can't be "
2127 "specified with Data-on-MDT component: %lld\n",
2128 lsa->lsa_stripe_count);
2131 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2132 fprintf(stderr, "Option 'stripe-size' can't be "
2133 "specified with Data-on-MDT component: %llu\n",
2134 lsa->lsa_stripe_size);
2137 if (lsa->lsa_nr_tgts != 0) {
2138 fprintf(stderr, "Option 'ost-list' can't be specified "
2139 "with Data-on-MDT component: '%i'\n",
2143 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2144 fprintf(stderr, "Option 'stripe-offset' can't be "
2145 "specified with Data-on-MDT component: %lld\n",
2146 lsa->lsa_stripe_off);
2149 if (lsa->lsa_pool_name != 0) {
2150 fprintf(stderr, "Option 'pool' can't be specified "
2151 "with Data-on-MDT component: '%s'\n",
2152 lsa->lsa_pool_name);
2156 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2158 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2159 lsa->lsa_pattern, strerror(errno));
2162 /* Data-on-MDT component has always single stripe up to end */
2163 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2166 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2168 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2169 lsa->lsa_stripe_size, strerror(errno));
2173 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2175 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2176 lsa->lsa_stripe_count, strerror(errno));
2180 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2182 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2183 lsa->lsa_comp_flags, strerror(errno));
2187 if (lsa->lsa_pool_name != NULL) {
2188 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2190 fprintf(stderr, "Set pool name: %s failed. %s\n",
2191 lsa->lsa_pool_name, strerror(errno));
2195 rc = llapi_layout_pool_name_set(layout, "");
2197 fprintf(stderr, "Clear pool name failed: %s\n",
2203 if (lsa->lsa_nr_tgts > 0) {
2204 if (lsa->lsa_stripe_count > 0 &&
2205 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2206 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2207 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2208 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2209 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2212 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2213 rc = llapi_layout_ost_index_set(layout, i,
2218 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2219 lsa->lsa_stripe_off != -1) {
2220 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2223 fprintf(stderr, "Set ost index %d failed. %s\n",
2224 i, strerror(errno));
2231 static int build_component(struct llapi_layout **layout,
2232 struct lfs_setstripe_args *lsa, bool set_extent)
2236 rc = comp_args_to_layout(layout, lsa, set_extent);
2240 if (lsa->lsa_mirror_count > 0) {
2241 rc = llapi_layout_mirror_count_set(*layout,
2242 lsa->lsa_mirror_count);
2246 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2249 lsa->lsa_mirror_count = 0;
2255 static int build_layout_from_yaml_node(struct cYAML *node,
2256 struct llapi_layout **layout,
2257 struct lfs_setstripe_args *lsa,
2264 if (node->cy_type == CYAML_TYPE_OBJECT) {
2265 /* go deep to sub blocks */
2266 rc = build_layout_from_yaml_node(node->cy_child, layout,
2271 if (node->cy_string == NULL)
2274 string = node->cy_string;
2275 /* skip leading lmm_ if present, to simplify parsing */
2276 if (strncmp(string, "lmm_", 4) == 0)
2279 if (node->cy_type == CYAML_TYPE_STRING) {
2280 if (!strcmp(string, "lcme_extent.e_end")) {
2281 if (!strcmp(node->cy_valuestring, "EOF") ||
2282 !strcmp(node->cy_valuestring, "eof"))
2283 lsa->lsa_comp_end = LUSTRE_EOF;
2284 } else if (!strcmp(string, "pool")) {
2285 lsa->lsa_pool_name = node->cy_valuestring;
2286 } else if (!strcmp(string, "pattern")) {
2287 if (!strcmp(node->cy_valuestring, "mdt"))
2288 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2290 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2291 if (!strcmp(string, "lcm_mirror_count")) {
2292 lsa->lsa_mirror_count = node->cy_valueint;
2293 } else if (!strcmp(string, "lcme_extent.e_start")) {
2294 if (node->cy_valueint != 0 || *layout != NULL) {
2295 rc = build_component(layout, lsa, true);
2300 if (node->cy_valueint == 0)
2301 lsa->lsa_first_comp = true;
2303 /* initialize lsa */
2304 setstripe_args_init(lsa);
2305 lsa->lsa_tgts = osts;
2306 } else if (!strcmp(string, "lcme_extent.e_end")) {
2307 if (node->cy_valueint == -1)
2308 lsa->lsa_comp_end = LUSTRE_EOF;
2310 lsa->lsa_comp_end = node->cy_valueint;
2311 } else if (!strcmp(string, "stripe_count")) {
2312 lsa->lsa_stripe_count = node->cy_valueint;
2313 } else if (!strcmp(string, "stripe_size")) {
2314 lsa->lsa_stripe_size = node->cy_valueint;
2315 } else if (!strcmp(string, "stripe_offset")) {
2316 lsa->lsa_stripe_off = node->cy_valueint;
2317 } else if (!strcmp(string, "l_ost_idx")) {
2318 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2323 node = node->cy_next;
2329 static int lfs_comp_create_from_yaml(char *template,
2330 struct llapi_layout **layout,
2331 struct lfs_setstripe_args *lsa,
2334 struct cYAML *tree = NULL, *err_rc = NULL;
2337 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2339 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2340 progname, template);
2341 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2342 "can't parse", &err_rc);
2343 cYAML_print_tree2file(stderr, err_rc);
2344 cYAML_free_tree(err_rc);
2349 /* initialize lsa for plain file */
2350 setstripe_args_init(lsa);
2351 lsa->lsa_tgts = osts;
2353 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2355 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2356 progname, template);
2359 rc = build_component(layout, lsa, *layout != NULL);
2361 /* clean clean lsa */
2362 setstripe_args_init(lsa);
2366 cYAML_free_tree(tree);
2370 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2371 * end of the last component in the existing file, and adjust the
2372 * first extent start of the components to be added accordingly. */
2373 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2375 struct llapi_layout *head;
2376 uint64_t start, end, stripe_size, prev_end = 0;
2379 if (layout == NULL) {
2381 "%s setstripe: layout must be specified\n",
2387 head = llapi_layout_get_by_path(fname, 0);
2390 "%s setstripe: cannot read layout from '%s': %s\n",
2391 progname, fname, strerror(errno));
2393 } else if (errno == ENODATA) {
2394 /* file without LOVEA, this component-add will be turned
2395 * into a component-create. */
2396 llapi_layout_free(head);
2398 } else if (!llapi_layout_is_composite(head)) {
2399 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2401 llapi_layout_free(head);
2405 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2407 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2408 progname, strerror(errno));
2409 llapi_layout_free(head);
2413 llapi_layout_free(head);
2415 /* Make sure we use the first component of the layout to be added. */
2416 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2419 "%s setstripe: cannot move component cursor: %s\n",
2420 progname, strerror(errno));
2424 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2426 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2427 progname, strerror(errno));
2431 if (start > prev_end || end <= prev_end) {
2433 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2434 progname, start, end, prev_end);
2438 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2440 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2441 progname, strerror(errno));
2445 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2446 (prev_end & (stripe_size - 1))) {
2448 "%s setstripe: stripe size %lu not aligned with %lu\n",
2449 progname, stripe_size, prev_end);
2453 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2456 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2457 progname, prev_end, end, strerror(errno));
2464 static inline bool arg_is_eof(char *arg)
2466 return !strncmp(arg, "-1", strlen("-1")) ||
2467 !strncmp(arg, "EOF", strlen("EOF")) ||
2468 !strncmp(arg, "eof", strlen("eof"));
2472 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2474 * Return: Valid mirror_args pointer on success and
2475 * NULL if memory allocation fails.
2477 static struct mirror_args *lfs_mirror_alloc(void)
2479 struct mirror_args *mirror = NULL;
2482 mirror = calloc(1, sizeof(*mirror));
2493 * lfs_mirror_free() - Free memory allocated for a mirror argument
2495 * @mirror: Previously allocated mirror argument structure by
2496 * lfs_mirror_alloc().
2498 * Free memory allocated for @mirror.
2502 static void lfs_mirror_free(struct mirror_args *mirror)
2504 if (mirror->m_layout != NULL)
2505 llapi_layout_free(mirror->m_layout);
2510 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2511 * @mirror_list: Previously allocated mirror list.
2513 * Free memory allocated for @mirror_list.
2517 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2519 struct mirror_args *next_mirror = NULL;
2521 while (mirror_list != NULL) {
2522 next_mirror = mirror_list->m_next;
2523 lfs_mirror_free(mirror_list);
2524 mirror_list = next_mirror;
2536 LFS_COMP_NO_VERIFY_OPT,
2538 LFS_MIRROR_FLAGS_OPT,
2540 LFS_MIRROR_STATE_OPT,
2542 LFS_MIRROR_INDEX_OPT,
2546 static int lfs_setstripe_internal(int argc, char **argv,
2547 enum setstripe_origin opc)
2549 struct lfs_setstripe_args lsa = { 0 };
2550 struct llapi_stripe_param *param = NULL;
2551 struct find_param migrate_mdt_param = {
2561 unsigned long long size_units = 1;
2562 bool migrate_mode = false;
2563 bool migrate_mdt_mode = false;
2564 bool migration_block = false;
2565 __u64 migration_flags = 0;
2566 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2567 int comp_del = 0, comp_set = 0;
2570 struct llapi_layout *layout = NULL;
2571 struct llapi_layout **lpp = &layout;
2572 bool mirror_mode = false;
2573 bool has_m_file = false;
2574 __u32 mirror_count = 0;
2575 enum mirror_flags mirror_flags = 0;
2576 struct mirror_args *mirror_list = NULL;
2577 struct mirror_args *new_mirror = NULL;
2578 struct mirror_args *last_mirror = NULL;
2579 __u16 mirror_id = 0;
2581 bool from_yaml = false;
2582 bool from_copy = false;
2583 char *template = NULL;
2585 struct option long_opts[] = {
2586 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2587 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2588 /* --block is only valid in migrate mode */
2589 { .val = 'b', .name = "block", .has_arg = no_argument },
2590 { .val = LFS_COMP_ADD_OPT,
2591 .name = "comp-add", .has_arg = no_argument },
2592 { .val = LFS_COMP_ADD_OPT,
2593 .name = "component-add", .has_arg = no_argument },
2594 { .val = LFS_COMP_DEL_OPT,
2595 .name = "comp-del", .has_arg = no_argument },
2596 { .val = LFS_COMP_DEL_OPT,
2597 .name = "component-del", .has_arg = no_argument },
2598 { .val = LFS_COMP_FLAGS_OPT,
2599 .name = "comp-flags", .has_arg = required_argument },
2600 { .val = LFS_COMP_FLAGS_OPT,
2601 .name = "component-flags",
2602 .has_arg = required_argument },
2603 { .val = LFS_COMP_SET_OPT,
2604 .name = "comp-set", .has_arg = no_argument },
2605 { .val = LFS_COMP_SET_OPT,
2606 .name = "component-set",
2607 .has_arg = no_argument},
2608 { .val = LFS_COMP_NO_VERIFY_OPT,
2609 .name = "no-verify", .has_arg = no_argument},
2610 { .val = LFS_MIRROR_FLAGS_OPT,
2611 .name = "flags", .has_arg = required_argument},
2612 { .val = LFS_MIRROR_ID_OPT,
2613 .name = "mirror-id", .has_arg = required_argument},
2614 { .val = LFS_LAYOUT_COPY,
2615 .name = "copy", .has_arg = required_argument},
2616 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2617 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2618 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2619 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2620 { .val = 'd', .name = "delete", .has_arg = no_argument},
2621 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2622 /* --non-direct is only valid in migrate mode */
2623 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2624 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2625 { .val = 'E', .name = "component-end",
2626 .has_arg = required_argument},
2627 { .val = 'f', .name = "file", .has_arg = required_argument },
2628 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2629 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2630 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2631 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2632 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2633 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2634 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2635 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2636 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2637 { .val = 'L', .name = "layout", .has_arg = required_argument },
2638 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2639 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2640 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2641 /* --non-block is only valid in migrate mode */
2642 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2643 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2644 { .val = 'o', .name = "ost", .has_arg = required_argument },
2645 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2646 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2647 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2649 { .val = 'p', .name = "pool", .has_arg = required_argument },
2650 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2651 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2652 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2653 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2654 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2655 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2656 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2657 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2658 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2659 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2660 /* --verbose is only valid in migrate mode */
2661 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2662 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2665 setstripe_args_init(&lsa);
2667 migrate_mode = (opc == SO_MIGRATE);
2668 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2670 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2672 while ((c = getopt_long(argc, argv,
2673 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2679 case LFS_COMP_ADD_OPT:
2682 case LFS_COMP_DEL_OPT:
2685 case LFS_COMP_FLAGS_OPT:
2686 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2687 &lsa.lsa_comp_neg_flags);
2690 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2691 fprintf(stderr, "%s: inverted flags are not supported\n",
2695 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2697 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2704 case LFS_COMP_SET_OPT:
2707 case LFS_COMP_NO_VERIFY_OPT:
2708 mirror_flags |= MF_NO_VERIFY;
2710 case LFS_MIRROR_ID_OPT:
2711 mirror_id = strtoul(optarg, &end, 0);
2712 if (*end != '\0' || mirror_id == 0) {
2714 "%s %s: invalid mirror ID '%s'\n",
2715 progname, argv[0], optarg);
2719 case LFS_MIRROR_FLAGS_OPT: {
2722 if (!mirror_mode || !last_mirror) {
2723 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2728 result = comp_str2flags(optarg, &last_mirror->m_flags,
2734 fprintf(stderr, "%s: inverted flags are not supported\n",
2739 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2741 "%s: unsupported mirror flags: %s\n",
2748 case LFS_LAYOUT_COPY:
2753 if (!migrate_mode) {
2755 "%s %s: -b|--block valid only for migrate command\n",
2759 migration_block = true;
2762 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2765 "%s %s: invalid stripe count '%s'\n",
2766 progname, argv[0], optarg);
2770 if (lsa.lsa_stripe_count == -1)
2771 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2774 /* delete the default striping pattern */
2776 if (opc == SO_MIRROR_SPLIT) {
2779 "%s %s: -d cannot used with -f\n",
2783 mirror_flags |= MF_DESTROY;
2787 if (!migrate_mode) {
2789 "%s %s: -D|--non-direct is valid "
2790 "only for migrate command\n",
2794 migration_flags |= MIGRATION_NONDIRECT;
2797 if (lsa.lsa_comp_end != 0) {
2798 result = comp_args_to_layout(lpp, &lsa, true);
2801 "%s %s: invalid layout\n",
2806 setstripe_args_init_inherit(&lsa);
2809 if (arg_is_eof(optarg)) {
2810 lsa.lsa_comp_end = LUSTRE_EOF;
2812 result = llapi_parse_size(optarg,
2817 "%s %s: invalid component end '%s'\n",
2818 progname, argv[0], optarg);
2824 if (!migrate_mode) {
2825 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2829 lsa.lsa_pattern = check_hashtype(optarg);
2830 if (lsa.lsa_pattern == 0) {
2832 "%s %s: bad stripe hash type '%s'\n",
2833 progname, argv[0], optarg);
2838 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2841 "%s %s: invalid stripe offset '%s'\n",
2842 progname, argv[0], optarg);
2845 if (lsa.lsa_stripe_off == -1)
2846 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2849 comp_id = strtoul(optarg, &end, 0);
2850 if (*end != '\0' || comp_id == 0 ||
2851 comp_id > LCME_ID_MAX) {
2853 "%s %s: invalid component ID '%s'\n",
2854 progname, argv[0], optarg);
2859 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2861 "error: %s: invalid option: %s\n",
2862 progname, argv[optopt + 1]);
2865 if (opc == SO_MIRROR_EXTEND) {
2866 if (last_mirror == NULL) {
2868 "error: %s: '-N' must exist in front of '%s'\n",
2869 progname, argv[optopt + 1]);
2872 last_mirror->m_file = optarg;
2873 last_mirror->m_count = 1;
2876 if (mirror_list == NULL)
2877 mirror_list = lfs_mirror_alloc();
2878 mirror_list->m_file = optarg;
2883 if (strcmp(argv[optind - 1], "mdt") == 0) {
2884 /* Can be only the first component */
2885 if (layout != NULL) {
2887 fprintf(stderr, "error: 'mdt' layout "
2888 "can be only the first one\n");
2891 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2893 fprintf(stderr, "error: 'mdt' layout "
2894 "size is too big\n");
2897 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2898 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2900 fprintf(stderr, "error: layout '%s' is "
2901 "unknown, supported layouts are: "
2902 "'mdt', 'raid0'\n", argv[optind]);
2907 if (!migrate_mode) {
2909 "%s %s: -m|--mdt is valid only for migrate command\n",
2913 migrate_mdt_mode = true;
2914 lsa.lsa_nr_tgts = parse_targets(tgts,
2915 sizeof(tgts) / sizeof(__u32),
2916 lsa.lsa_nr_tgts, optarg);
2917 if (lsa.lsa_nr_tgts < 0) {
2919 "%s %s: invalid MDT target(s) '%s'\n",
2920 progname, argv[0], optarg);
2924 lsa.lsa_tgts = tgts;
2925 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2926 lsa.lsa_stripe_off = tgts[0];
2929 if (!migrate_mode) {
2931 "%s %s: -n|--non-block valid only for migrate command\n",
2935 migration_flags |= MIGRATION_NONBLOCK;
2938 if (opc == SO_SETSTRIPE) {
2939 opc = SO_MIRROR_CREATE;
2943 if (optarg != NULL) {
2944 mirror_count = strtoul(optarg, &end, 0);
2945 if (*end != '\0' || mirror_count == 0) {
2947 "error: %s: bad mirror count: %s\n",
2954 new_mirror = lfs_mirror_alloc();
2955 new_mirror->m_count = mirror_count;
2957 if (mirror_list == NULL)
2958 mirror_list = new_mirror;
2960 if (last_mirror != NULL) {
2961 /* wrap up last mirror */
2962 if (lsa.lsa_comp_end == 0)
2963 lsa.lsa_comp_end = LUSTRE_EOF;
2965 result = comp_args_to_layout(lpp, &lsa, true);
2967 lfs_mirror_free(new_mirror);
2971 setstripe_args_init_inherit(&lsa);
2973 last_mirror->m_next = new_mirror;
2976 last_mirror = new_mirror;
2977 lpp = &last_mirror->m_layout;
2980 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2981 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2982 fprintf(stderr, "warning: '--ost-list' is "
2983 "deprecated, use '--ost' instead\n");
2985 lsa.lsa_nr_tgts = parse_targets(tgts,
2986 sizeof(tgts) / sizeof(__u32),
2987 lsa.lsa_nr_tgts, optarg);
2988 if (lsa.lsa_nr_tgts < 0) {
2990 "%s %s: invalid OST target(s) '%s'\n",
2991 progname, argv[0], optarg);
2995 lsa.lsa_tgts = tgts;
2996 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2997 lsa.lsa_stripe_off = tgts[0];
3002 lsa.lsa_pool_name = optarg;
3004 if (strlen(lsa.lsa_pool_name) == 0 ||
3005 strncmp(lsa.lsa_pool_name, "none",
3006 LOV_MAXPOOLNAME) == 0)
3007 lsa.lsa_pool_name = NULL;
3010 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3014 "%s %s: invalid stripe size '%s'\n",
3015 progname, argv[0], optarg);
3020 if (!migrate_mode) {
3022 "%s %s: -v|--verbose valid only for migrate command\n",
3026 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3033 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3034 progname, argv[0], argv[optind - 1]);
3039 fname = argv[optind];
3041 if (optind == argc) {
3042 fprintf(stderr, "%s %s: FILE must be specified\n",
3047 if (mirror_mode && mirror_count == 0) {
3049 "error: %s: --mirror-count|-N option is required\n",
3056 if (lsa.lsa_comp_end == 0)
3057 lsa.lsa_comp_end = LUSTRE_EOF;
3060 if (lsa.lsa_comp_end != 0) {
3061 result = comp_args_to_layout(lpp, &lsa, true);
3066 if (mirror_flags & MF_NO_VERIFY) {
3067 if (opc != SO_MIRROR_EXTEND) {
3069 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3073 } else if (!has_m_file) {
3075 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3082 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3083 * altered by user space tool, so we don't need to support the
3084 * --component-set for this moment. */
3085 if (comp_set && !comp_id) {
3086 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3091 if ((delete + comp_set + comp_del + comp_add) > 1) {
3093 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3098 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3099 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3101 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3106 if ((comp_set || comp_del) &&
3107 (setstripe_args_specified(&lsa) || layout != NULL)) {
3109 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3114 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3116 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3121 if (comp_add || comp_del) {
3124 result = lstat(fname, &st);
3125 if (result == 0 && S_ISDIR(st.st_mode)) {
3127 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3133 fprintf(stderr, "error: %s: can't use --component-add "
3134 "or --component-del for mirror operation\n",
3141 if (layout == NULL) {
3143 "%s %s: option -E must be specified with --component-add\n",
3148 result = adjust_first_extent(fname, layout);
3149 if (result == -ENODATA)
3151 else if (result != 0)
3155 if (from_yaml && from_copy) {
3157 "%s: can't specify --yaml and --copy together\n",
3162 if ((from_yaml || from_copy) &&
3163 (setstripe_args_specified(&lsa) || layout != NULL)) {
3164 fprintf(stderr, "error: %s: can't specify --yaml with "
3165 "-c, -S, -i, -o, -p or -E options.\n",
3170 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3172 "%s %s: options --non-block and --block are mutually exclusive\n",
3177 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3180 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3185 if (migrate_mdt_mode) {
3186 struct lmv_user_md *lmu;
3188 /* initialize migrate mdt parameters */
3189 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3190 LMV_USER_MAGIC_SPECIFIC));
3193 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3194 progname, argv[0], strerror(ENOMEM));
3198 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3199 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3200 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3202 "%s %s: migrate should specify MDT index\n",
3207 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3208 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3209 lmu->lum_hash_type = lsa.lsa_pattern;
3211 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3212 if (lsa.lsa_pool_name)
3213 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3214 sizeof(lmu->lum_pool_name));
3215 if (lsa.lsa_nr_tgts > 1) {
3218 if (lsa.lsa_stripe_count > 0 &&
3219 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3220 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3222 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3223 progname, lsa.lsa_stripe_count,
3229 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3230 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3231 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3232 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3234 lmu->lum_magic = LMV_USER_MAGIC;
3237 migrate_mdt_param.fp_lmv_md = lmu;
3238 migrate_mdt_param.fp_migrate = 1;
3239 } else if (layout == NULL) {
3240 /* initialize stripe parameters */
3241 param = calloc(1, offsetof(typeof(*param),
3242 lsp_osts[lsa.lsa_nr_tgts]));
3243 if (param == NULL) {
3245 "%s %s: cannot allocate memory for parameters: %s\n",
3246 progname, argv[0], strerror(ENOMEM));
3251 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3252 param->lsp_stripe_size = lsa.lsa_stripe_size;
3253 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3254 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3255 param->lsp_stripe_count = -1;
3257 param->lsp_stripe_count = lsa.lsa_stripe_count;
3259 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3260 param->lsp_stripe_offset = -1;
3262 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3263 param->lsp_pool = lsa.lsa_pool_name;
3264 param->lsp_is_specific = false;
3265 if (lsa.lsa_nr_tgts > 0) {
3266 if (lsa.lsa_stripe_count > 0 &&
3267 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3268 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3269 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3270 fprintf(stderr, "error: %s: stripe count %lld "
3271 "doesn't match the number of OSTs: %d\n"
3272 , argv[0], lsa.lsa_stripe_count,
3278 param->lsp_is_specific = true;
3279 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3280 memcpy(param->lsp_osts, tgts,
3281 sizeof(*tgts) * lsa.lsa_nr_tgts);
3286 /* generate a layout from a YAML template */
3287 result = lfs_comp_create_from_yaml(template, &layout,
3290 fprintf(stderr, "error: %s: can't create composite "
3291 "layout from template file %s\n",
3295 } else if (from_copy) {
3296 layout = llapi_layout_get_by_path(template, 0);
3297 if (layout == NULL) {
3299 "%s: can't create composite layout from file %s.\n",
3300 progname, template);
3305 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3306 if (migrate_mdt_mode) {
3307 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3308 } else if (migrate_mode) {
3309 result = lfs_migrate(fname, migration_flags, param,
3311 } else if (comp_set != 0) {
3312 result = lfs_component_set(fname, comp_id,
3314 lsa.lsa_comp_neg_flags);
3315 } else if (comp_del != 0) {
3316 result = lfs_component_del(fname, comp_id,
3318 lsa.lsa_comp_neg_flags);
3319 } else if (comp_add != 0) {
3320 result = lfs_component_add(fname, layout);
3321 } else if (opc == SO_MIRROR_CREATE) {
3322 result = mirror_create(fname, mirror_list);
3323 } else if (opc == SO_MIRROR_EXTEND) {
3324 result = mirror_extend(fname, mirror_list,
3326 } else if (opc == SO_MIRROR_SPLIT) {
3327 if (mirror_id == 0 && comp_id == 0) {
3329 "%s %s: no mirror id or component id is specified\n",
3334 comp_id = mirror_id;
3336 mirror_flags |= MF_COMP_ID;
3337 result = mirror_split(fname, comp_id, mirror_flags,
3338 has_m_file ? mirror_list->m_file :
3340 } else if (layout != NULL) {
3341 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3348 result = llapi_file_open_param(fname,
3357 /* Save the first error encountered. */
3365 free(migrate_mdt_param.fp_lmv_md);
3366 llapi_layout_free(layout);
3367 lfs_mirror_list_free(mirror_list);
3372 llapi_layout_free(layout);
3373 lfs_mirror_list_free(mirror_list);
3377 static int lfs_poollist(int argc, char **argv)
3382 return llapi_poollist(argv[1]);
3385 static int set_time(time_t *time, time_t *set, char *str)
3393 else if (str[0] == '-')
3399 t = strtol(str, &endptr, 0);
3400 if (*endptr != '\0') {
3402 "%s find: bad time '%s': %s\n",
3403 progname, str, strerror(EINVAL));
3406 if (*time < t * 24 * 60 * 60) {
3410 "%s find: bad time '%s': too large\n",
3415 *set = *time - t * 24 * 60 * 60;
3419 static int name2uid(unsigned int *id, const char *name)
3421 struct passwd *passwd;
3423 passwd = getpwnam(name);
3426 *id = passwd->pw_uid;
3431 static int name2gid(unsigned int *id, const char *name)
3433 struct group *group;
3435 group = getgrnam(name);
3438 *id = group->gr_gid;
3443 static inline int name2projid(unsigned int *id, const char *name)
3448 static int uid2name(char **name, unsigned int id)
3450 struct passwd *passwd;
3452 passwd = getpwuid(id);
3455 *name = passwd->pw_name;
3460 static inline int gid2name(char **name, unsigned int id)
3462 struct group *group;
3464 group = getgrgid(id);
3467 *name = group->gr_name;
3472 static int name2layout(__u32 *layout, char *name)
3474 char *ptr, *layout_name;
3477 for (ptr = name; ; ptr = NULL) {
3478 layout_name = strtok(ptr, ",");
3479 if (layout_name == NULL)
3481 if (strcmp(layout_name, "released") == 0)
3482 *layout |= LOV_PATTERN_F_RELEASED;
3483 else if (strcmp(layout_name, "raid0") == 0)
3484 *layout |= LOV_PATTERN_RAID0;
3485 else if (strcmp(layout_name, "mdt") == 0)
3486 *layout |= LOV_PATTERN_MDT;
3493 static int lfs_find(int argc, char **argv)
3498 struct find_param param = {
3502 struct option long_opts[] = {
3503 { .val = 'A', .name = "atime", .has_arg = required_argument },
3504 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3505 { .val = LFS_COMP_COUNT_OPT,
3506 .name = "comp-count", .has_arg = required_argument },
3507 { .val = LFS_COMP_COUNT_OPT,
3508 .name = "component-count",
3509 .has_arg = required_argument },
3510 { .val = LFS_COMP_FLAGS_OPT,
3511 .name = "comp-flags", .has_arg = required_argument },
3512 { .val = LFS_COMP_FLAGS_OPT,
3513 .name = "component-flags",
3514 .has_arg = required_argument },
3515 { .val = LFS_COMP_START_OPT,
3516 .name = "comp-start", .has_arg = required_argument },
3517 { .val = LFS_COMP_START_OPT,
3518 .name = "component-start",
3519 .has_arg = required_argument },
3520 { .val = LFS_MIRROR_STATE_OPT,
3521 .name = "mirror-state", .has_arg = required_argument },
3522 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3523 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3524 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3525 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3526 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3527 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3528 { .val = 'E', .name = "component-end",
3529 .has_arg = required_argument },
3530 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3531 { .val = 'g', .name = "gid", .has_arg = required_argument },
3532 { .val = 'G', .name = "group", .has_arg = required_argument },
3533 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3534 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3535 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3536 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3537 { .val = 'L', .name = "layout", .has_arg = required_argument },
3538 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3539 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3540 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3541 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3542 { .val = 'n', .name = "name", .has_arg = required_argument },
3543 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3544 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3545 { .val = 'O', .name = "obd", .has_arg = required_argument },
3546 { .val = 'O', .name = "ost", .has_arg = required_argument },
3547 /* no short option for pool yet, can be 'p' after 2.18 */
3548 { .val = LFS_POOL_OPT,
3549 .name = "pool", .has_arg = required_argument },
3550 { .val = '0', .name = "print0", .has_arg = no_argument },
3551 { .val = 'P', .name = "print", .has_arg = no_argument },
3552 { .val = LFS_PROJID_OPT,
3553 .name = "projid", .has_arg = required_argument },
3554 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3555 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3556 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3557 { .val = 's', .name = "size", .has_arg = required_argument },
3558 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3559 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3560 { .val = 't', .name = "type", .has_arg = required_argument },
3561 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3562 { .val = 'u', .name = "uid", .has_arg = required_argument },
3563 { .val = 'U', .name = "user", .has_arg = required_argument },
3564 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3565 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3577 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3578 while ((c = getopt_long_only(argc, argv,
3579 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3580 long_opts, NULL)) >= 0) {
3585 /* '!' is part of option */
3586 /* when getopt_long_only() finds a string which is not
3587 * an option nor a known option argument it returns 1
3588 * in that case if we already have found pathstart and pathend
3589 * (i.e. we have the list of pathnames),
3590 * the only supported value is "!"
3592 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3593 if (!isoption && pathend != -1) {
3594 fprintf(stderr, "err: %s: filename|dirname must either "
3595 "precede options or follow options\n",
3600 if (!isoption && pathstart == -1)
3601 pathstart = optind - 1;
3602 if (isoption && pathstart != -1 && pathend == -1)
3603 pathend = optind - 2;
3609 /* unknown; opt is "!" or path component,
3610 * checking done above.
3612 if (strcmp(optarg, "!") == 0)
3616 xtime = ¶m.fp_atime;
3617 xsign = ¶m.fp_asign;
3618 param.fp_exclude_atime = !!neg_opt;
3619 /* no break, this falls through to 'C' for ctime */
3622 xtime = ¶m.fp_ctime;
3623 xsign = ¶m.fp_csign;
3624 param.fp_exclude_ctime = !!neg_opt;
3626 /* no break, this falls through to 'M' for mtime */
3629 xtime = ¶m.fp_mtime;
3630 xsign = ¶m.fp_msign;
3631 param.fp_exclude_mtime = !!neg_opt;
3633 rc = set_time(&t, xtime, optarg);
3634 if (rc == INT_MAX) {
3642 if (optarg[0] == '+') {
3643 param.fp_blocks_sign = -1;
3645 } else if (optarg[0] == '-') {
3646 param.fp_blocks_sign = 1;
3650 param.fp_blocks_units = 1024;
3651 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3652 ¶m.fp_blocks_units, 0);
3654 fprintf(stderr, "error: bad blocks '%s'\n",
3658 param.fp_check_blocks = 1;
3659 param.fp_exclude_blocks = !!neg_opt;
3661 case LFS_COMP_COUNT_OPT:
3662 if (optarg[0] == '+') {
3663 param.fp_comp_count_sign = -1;
3665 } else if (optarg[0] == '-') {
3666 param.fp_comp_count_sign = 1;
3670 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3671 if (*endptr != '\0') {
3672 fprintf(stderr, "error: bad component count "
3676 param.fp_check_comp_count = 1;
3677 param.fp_exclude_comp_count = !!neg_opt;
3679 case LFS_COMP_FLAGS_OPT:
3680 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3681 ¶m.fp_comp_neg_flags);
3683 fprintf(stderr, "error: bad component flags "
3687 param.fp_check_comp_flags = 1;
3689 __u32 flags = param.fp_comp_neg_flags;
3690 param.fp_comp_neg_flags = param.fp_comp_flags;
3691 param.fp_comp_flags = flags;
3694 case LFS_COMP_START_OPT:
3695 if (optarg[0] == '+') {
3696 param.fp_comp_start_sign = -1;
3698 } else if (optarg[0] == '-') {
3699 param.fp_comp_start_sign = 1;
3703 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3704 ¶m.fp_comp_start_units, 0);
3706 fprintf(stderr, "error: bad component start "
3710 param.fp_check_comp_start = 1;
3711 param.fp_exclude_comp_start = !!neg_opt;
3713 case LFS_MIRROR_STATE_OPT:
3714 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3715 ¶m.fp_mirror_neg_state);
3718 "error: bad mirrored file state '%s'\n",
3722 param.fp_check_mirror_state = 1;
3724 __u16 state = param.fp_mirror_neg_state;
3725 param.fp_mirror_neg_state =
3726 param.fp_mirror_state;
3727 param.fp_mirror_state = state;
3731 if (optarg[0] == '+') {
3732 param.fp_stripe_count_sign = -1;
3734 } else if (optarg[0] == '-') {
3735 param.fp_stripe_count_sign = 1;
3739 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3740 if (*endptr != '\0') {
3741 fprintf(stderr,"error: bad stripe_count '%s'\n",
3746 param.fp_check_stripe_count = 1;
3747 param.fp_exclude_stripe_count = !!neg_opt;
3750 param.fp_max_depth = strtol(optarg, 0, 0);
3753 if (optarg[0] == '+') {
3754 param.fp_comp_end_sign = -1;
3756 } else if (optarg[0] == '-') {
3757 param.fp_comp_end_sign = 1;
3761 if (arg_is_eof(optarg)) {
3762 param.fp_comp_end = LUSTRE_EOF;
3763 param.fp_comp_end_units = 1;
3766 rc = llapi_parse_size(optarg,
3768 ¶m.fp_comp_end_units, 0);
3771 fprintf(stderr, "error: bad component end "
3775 param.fp_check_comp_end = 1;
3776 param.fp_exclude_comp_end = !!neg_opt;
3780 rc = name2gid(¶m.fp_gid, optarg);
3782 param.fp_gid = strtoul(optarg, &endptr, 10);
3783 if (*endptr != '\0') {
3784 fprintf(stderr, "Group/GID: %s cannot "
3785 "be found.\n", optarg);
3790 param.fp_exclude_gid = !!neg_opt;
3791 param.fp_check_gid = 1;
3794 param.fp_hash_type = check_hashtype(optarg);
3795 if (param.fp_hash_type == 0) {
3796 fprintf(stderr, "error: bad hash_type '%s'\n",
3801 param.fp_check_hash_type = 1;
3802 param.fp_exclude_hash_type = !!neg_opt;
3805 ret = name2layout(¶m.fp_layout, optarg);
3808 param.fp_exclude_layout = !!neg_opt;
3809 param.fp_check_layout = 1;
3813 rc = name2uid(¶m.fp_uid, optarg);
3815 param.fp_uid = strtoul(optarg, &endptr, 10);
3816 if (*endptr != '\0') {
3817 fprintf(stderr, "User/UID: %s cannot "
3818 "be found.\n", optarg);
3823 param.fp_exclude_uid = !!neg_opt;
3824 param.fp_check_uid = 1;
3827 param.fp_pattern = (char *)optarg;
3828 param.fp_exclude_pattern = !!neg_opt;
3831 if (optarg[0] == '+') {
3832 param.fp_mirror_count_sign = -1;
3834 } else if (optarg[0] == '-') {
3835 param.fp_mirror_count_sign = 1;
3839 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3840 if (*endptr != '\0') {
3842 "error: bad mirror count '%s'\n",
3846 param.fp_check_mirror_count = 1;
3847 param.fp_exclude_mirror_count = !!neg_opt;
3852 char *buf, *token, *next, *p;
3856 buf = strdup(optarg);
3862 param.fp_exclude_obd = !!neg_opt;
3865 while (token && *token) {
3866 token = strchr(token, ',');
3873 param.fp_exclude_mdt = !!neg_opt;
3874 param.fp_num_alloc_mdts += len;
3875 tmp = realloc(param.fp_mdt_uuid,
3876 param.fp_num_alloc_mdts *
3877 sizeof(*param.fp_mdt_uuid));
3883 param.fp_mdt_uuid = tmp;
3885 param.fp_exclude_obd = !!neg_opt;
3886 param.fp_num_alloc_obds += len;
3887 tmp = realloc(param.fp_obd_uuid,
3888 param.fp_num_alloc_obds *
3889 sizeof(*param.fp_obd_uuid));
3895 param.fp_obd_uuid = tmp;
3897 for (token = buf; token && *token; token = next) {
3898 struct obd_uuid *puuid;
3901 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3904 ¶m.fp_obd_uuid[param.fp_num_obds++];
3906 p = strchr(token, ',');
3913 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3918 strncpy(puuid->uuid, token,
3919 sizeof(puuid->uuid));
3926 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3930 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3932 "Pool name %s is too long (max %d)\n",
3933 optarg, LOV_MAXPOOLNAME);
3938 * We do check for empty pool because empty pool
3939 * is used to find V1 LOV attributes
3941 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3942 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3943 param.fp_exclude_pool = !!neg_opt;
3944 param.fp_check_pool = 1;
3946 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3947 case 'p': /* want this for --pool, to match getstripe/find */
3949 "warning: -p deprecated, use --print0 or -0\n");
3952 param.fp_zero_end = 1;
3954 case 'P': /* we always print, this option is a no-op */
3956 case LFS_PROJID_OPT:
3957 rc = name2projid(¶m.fp_projid, optarg);
3959 param.fp_projid = strtoul(optarg, &endptr, 10);
3960 if (*endptr != '\0') {
3962 "Invalid project ID: %s",
3968 param.fp_exclude_projid = !!neg_opt;
3969 param.fp_check_projid = 1;
3972 if (optarg[0] == '+') {
3973 param.fp_size_sign = -1;
3975 } else if (optarg[0] == '-') {
3976 param.fp_size_sign = 1;
3980 ret = llapi_parse_size(optarg, ¶m.fp_size,
3981 ¶m.fp_size_units, 0);
3983 fprintf(stderr, "error: bad file size '%s'\n",
3987 param.fp_check_size = 1;
3988 param.fp_exclude_size = !!neg_opt;
3991 if (optarg[0] == '+') {
3992 param.fp_stripe_size_sign = -1;
3994 } else if (optarg[0] == '-') {
3995 param.fp_stripe_size_sign = 1;
3999 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4000 ¶m.fp_stripe_size_units, 0);
4002 fprintf(stderr, "error: bad stripe_size '%s'\n",
4006 param.fp_check_stripe_size = 1;
4007 param.fp_exclude_stripe_size = !!neg_opt;
4010 param.fp_exclude_type = !!neg_opt;
4011 switch (optarg[0]) {
4013 param.fp_type = S_IFBLK;
4016 param.fp_type = S_IFCHR;
4019 param.fp_type = S_IFDIR;
4022 param.fp_type = S_IFREG;
4025 param.fp_type = S_IFLNK;
4028 param.fp_type = S_IFIFO;
4031 param.fp_type = S_IFSOCK;
4034 fprintf(stderr, "error: %s: bad type '%s'\n",
4041 if (optarg[0] == '+') {
4042 param.fp_mdt_count_sign = -1;
4044 } else if (optarg[0] == '-') {
4045 param.fp_mdt_count_sign = 1;
4049 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4050 if (*endptr != '\0') {
4051 fprintf(stderr, "error: bad mdt_count '%s'\n",
4056 param.fp_check_mdt_count = 1;
4057 param.fp_exclude_mdt_count = !!neg_opt;
4065 if (pathstart == -1) {
4066 fprintf(stderr, "error: %s: no filename|pathname\n",
4070 } else if (pathend == -1) {
4076 rc = llapi_find(argv[pathstart], ¶m);
4077 if (rc != 0 && ret == 0)
4079 } while (++pathstart < pathend);
4082 fprintf(stderr, "error: %s failed for %s.\n",
4083 argv[0], argv[optind - 1]);
4085 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4086 free(param.fp_obd_uuid);
4088 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4089 free(param.fp_mdt_uuid);
4094 static int lfs_getstripe_internal(int argc, char **argv,
4095 struct find_param *param)
4097 struct option long_opts[] = {
4098 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4099 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4100 { .val = LFS_COMP_COUNT_OPT,
4101 .name = "comp-count", .has_arg = no_argument },
4102 { .val = LFS_COMP_COUNT_OPT,
4103 .name = "component-count", .has_arg = no_argument },
4104 { .val = LFS_COMP_FLAGS_OPT,
4105 .name = "comp-flags", .has_arg = optional_argument },
4106 { .val = LFS_COMP_FLAGS_OPT,
4107 .name = "component-flags", .has_arg = optional_argument },
4108 { .val = LFS_COMP_START_OPT,
4109 .name = "comp-start", .has_arg = optional_argument },
4110 { .val = LFS_COMP_START_OPT,
4111 .name = "component-start", .has_arg = optional_argument },
4112 { .val = LFS_MIRROR_INDEX_OPT,
4113 .name = "mirror-index", .has_arg = required_argument },
4114 { .val = LFS_MIRROR_ID_OPT,
4115 .name = "mirror-id", .has_arg = required_argument },
4116 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4117 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4118 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4119 { .val = 'd', .name = "directory", .has_arg = no_argument },
4120 { .val = 'D', .name = "default", .has_arg = no_argument },
4121 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4122 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4123 { .val = 'F', .name = "fid", .has_arg = no_argument },
4124 { .val = 'g', .name = "generation", .has_arg = no_argument },
4125 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4126 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4127 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4128 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4129 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4130 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4131 { .val = 'L', .name = "layout", .has_arg = no_argument },
4132 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4133 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4134 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4135 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4136 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4137 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4138 { .val = 'O', .name = "obd", .has_arg = required_argument },
4139 { .val = 'O', .name = "ost", .has_arg = required_argument },
4140 { .val = 'p', .name = "pool", .has_arg = no_argument },
4141 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4142 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4143 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4144 { .val = 'R', .name = "raw", .has_arg = no_argument },
4145 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4146 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4147 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4148 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4149 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4150 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4151 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4152 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4156 int pathstart = -1, pathend = -1;
4160 while ((c = getopt_long(argc, argv,
4161 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4162 long_opts, NULL)) != -1) {
4166 /* '!' is part of option */
4167 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4168 if (!isoption && pathend != -1) {
4170 "error: %s: filename|dirname must either precede options or follow options\n",
4174 if (!isoption && pathstart == -1)
4175 pathstart = optind - 1;
4176 if (isoption && pathstart != -1 && pathend == -1)
4177 pathend = optind - 2;
4181 /* unknown: opt is "!" */
4182 if (strcmp(optarg, "!") == 0)
4186 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4187 param->fp_verbose |= VERBOSE_COUNT;
4188 param->fp_max_depth = 0;
4191 case LFS_COMP_COUNT_OPT:
4192 param->fp_verbose |= VERBOSE_COMP_COUNT;
4193 param->fp_max_depth = 0;
4195 case LFS_COMP_FLAGS_OPT:
4196 if (optarg != NULL) {
4197 rc = comp_str2flags(optarg,
4198 ¶m->fp_comp_flags,
4199 ¶m->fp_comp_neg_flags);
4201 fprintf(stderr, "error: %s bad "
4202 "component flags '%s'.\n",
4206 param->fp_check_comp_flags = 1;
4208 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4209 param->fp_max_depth = 0;
4212 case LFS_COMP_START_OPT:
4213 if (optarg != NULL) {
4215 if (tmp[0] == '+') {
4216 param->fp_comp_start_sign = -1;
4218 } else if (tmp[0] == '-') {
4219 param->fp_comp_start_sign = 1;
4222 rc = llapi_parse_size(tmp,
4223 ¶m->fp_comp_start,
4224 ¶m->fp_comp_start_units, 0);
4226 fprintf(stderr, "error: %s bad "
4227 "component start '%s'.\n",
4231 param->fp_check_comp_start = 1;
4234 param->fp_verbose |= VERBOSE_COMP_START;
4235 param->fp_max_depth = 0;
4238 case LFS_MIRROR_INDEX_OPT:
4239 if (optarg[0] == '+') {
4240 param->fp_mirror_index_sign = -1;
4242 } else if (optarg[0] == '-') {
4243 param->fp_mirror_index_sign = 1;
4247 param->fp_mirror_index = strtoul(optarg, &end, 0);
4248 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4249 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4251 "%s %s: invalid mirror index '%s'\n",
4252 progname, argv[0], optarg);
4255 if (param->fp_mirror_id != 0) {
4257 "%s %s: can't specify both mirror index and mirror ID\n",
4261 param->fp_check_mirror_index = 1;
4262 param->fp_exclude_mirror_index = !!neg_opt;
4264 case LFS_MIRROR_ID_OPT:
4265 if (optarg[0] == '+') {
4266 param->fp_mirror_id_sign = -1;
4268 } else if (optarg[0] == '-') {
4269 param->fp_mirror_id_sign = 1;
4273 param->fp_mirror_id = strtoul(optarg, &end, 0);
4274 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4275 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4277 "%s %s: invalid mirror ID '%s'\n",
4278 progname, argv[0], optarg);
4281 if (param->fp_mirror_index != 0) {
4283 "%s %s: can't specify both mirror index and mirror ID\n",
4287 param->fp_check_mirror_id = 1;
4288 param->fp_exclude_mirror_id = !!neg_opt;
4291 param->fp_max_depth = 0;
4294 param->fp_get_default_lmv = 1;
4297 if (optarg != NULL) {
4299 if (tmp[0] == '+') {
4300 param->fp_comp_end_sign = -1;
4302 } else if (tmp[0] == '-') {
4303 param->fp_comp_end_sign = 1;
4307 if (arg_is_eof(tmp)) {
4308 param->fp_comp_end = LUSTRE_EOF;
4309 param->fp_comp_end_units = 1;
4312 rc = llapi_parse_size(tmp,
4313 ¶m->fp_comp_end,
4314 ¶m->fp_comp_end_units, 0);
4317 fprintf(stderr, "error: %s bad "
4318 "component end '%s'.\n",
4322 param->fp_check_comp_end = 1;
4324 param->fp_verbose |= VERBOSE_COMP_END;
4325 param->fp_max_depth = 0;
4329 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4330 param->fp_verbose |= VERBOSE_DFID;
4331 param->fp_max_depth = 0;
4335 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4336 param->fp_verbose |= VERBOSE_GENERATION;
4337 param->fp_max_depth = 0;
4341 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4342 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4343 param->fp_max_depth = 0;
4347 if (optarg != NULL) {
4348 param->fp_comp_id = strtoul(optarg, &end, 0);
4349 if (*end != '\0' || param->fp_comp_id == 0 ||
4350 param->fp_comp_id > LCME_ID_MAX) {
4351 fprintf(stderr, "error: %s bad "
4352 "component id '%s'\n",
4356 param->fp_check_comp_id = 1;
4359 param->fp_max_depth = 0;
4360 param->fp_verbose |= VERBOSE_COMP_ID;
4364 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4365 param->fp_verbose |= VERBOSE_PATTERN;
4366 param->fp_max_depth = 0;
4369 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4371 fprintf(stderr, "warning: '-M' deprecated"
4372 ", use '--mdt-index' or '-m' instead\n");
4375 if (!(param->fp_verbose & VERBOSE_DETAIL))
4376 param->fp_max_depth = 0;
4377 param->fp_verbose |= VERBOSE_MDTINDEX;
4380 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4381 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4382 param->fp_max_depth = 0;
4386 if (param->fp_obd_uuid) {
4388 "error: %s: only one obduuid allowed",
4392 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4395 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4396 param->fp_verbose |= VERBOSE_POOL;
4397 param->fp_max_depth = 0;
4404 param->fp_recursive = 1;
4410 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4411 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4412 param->fp_max_depth = 0;
4416 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4426 if (pathstart == -1) {
4427 fprintf(stderr, "error: %s: no filename|pathname\n",
4430 } else if (pathend == -1) {
4438 if (param->fp_recursive)
4439 param->fp_max_depth = -1;
4440 else if (param->fp_verbose & VERBOSE_DETAIL)
4441 param->fp_max_depth = 1;
4443 if (!param->fp_verbose)
4444 param->fp_verbose = VERBOSE_DEFAULT;
4445 if (param->fp_quiet)
4446 param->fp_verbose = VERBOSE_OBJID;
4449 rc = llapi_getstripe(argv[pathstart], param);
4450 } while (++pathstart < pathend && !rc);
4453 fprintf(stderr, "error: %s failed for %s.\n",
4454 argv[0], argv[optind - 1]);
4458 static int lfs_tgts(int argc, char **argv)
4460 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4461 struct find_param param;
4462 int index = 0, rc=0;
4467 if (argc == 2 && !realpath(argv[1], path)) {
4469 fprintf(stderr, "error: invalid path '%s': %s\n",
4470 argv[1], strerror(-rc));
4474 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4475 /* Check if we have a mount point */
4476 if (mntdir[0] == '\0')
4479 memset(¶m, 0, sizeof(param));
4480 if (!strcmp(argv[0], "mdts"))
4481 param.fp_get_lmv = 1;
4483 rc = llapi_ostlist(mntdir, ¶m);
4485 fprintf(stderr, "error: %s: failed on %s\n",
4488 if (path[0] != '\0')
4490 memset(mntdir, 0, PATH_MAX);
4496 static int lfs_getstripe(int argc, char **argv)
4498 struct find_param param = { 0 };
4500 param.fp_max_depth = 1;
4501 return lfs_getstripe_internal(argc, argv, ¶m);
4505 static int lfs_getdirstripe(int argc, char **argv)
4507 struct find_param param = { 0 };
4508 struct option long_opts[] = {
4509 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4510 { .val = 'D', .name = "default", .has_arg = no_argument },
4511 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4512 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4513 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4514 { .val = 'O', .name = "obd", .has_arg = required_argument },
4515 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4516 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4517 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4521 param.fp_get_lmv = 1;
4523 while ((c = getopt_long(argc, argv,
4524 "cDHimO:rtTy", long_opts, NULL)) != -1)
4529 param.fp_verbose |= VERBOSE_COUNT;
4532 param.fp_get_default_lmv = 1;
4536 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4538 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4540 fprintf(stderr, "warning: '-t' deprecated, "
4541 "use '--mdt-hash' or '-H' instead\n");
4544 param.fp_verbose |= VERBOSE_HASH_TYPE;
4547 if (param.fp_obd_uuid) {
4549 "error: %s: only one obduuid allowed",
4553 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4556 param.fp_recursive = 1;
4569 if (param.fp_recursive)
4570 param.fp_max_depth = -1;
4572 if (!param.fp_verbose)
4573 param.fp_verbose = VERBOSE_DEFAULT;
4576 rc = llapi_getstripe(argv[optind], ¶m);
4577 } while (++optind < argc && !rc);
4580 fprintf(stderr, "error: %s failed for %s.\n",
4581 argv[0], argv[optind - 1]);
4586 MNTDF_INODES = 0x0001,
4587 MNTDF_COOKED = 0x0002,
4588 MNTDF_LAZY = 0x0004,
4589 MNTDF_VERBOSE = 0x0008,
4590 MNTDF_SHOW = 0x0010,
4593 #define COOK(value) \
4596 while (value > 1024) { \
4604 #define CDF "%11llu"
4605 #define HDF "%8.1f%c"
4609 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4611 double avail, used, ratio = 0;
4613 avail = st->os_bavail;
4614 used = st->os_blocks - st->os_bfree;
4615 if (avail + used > 0)
4616 ratio = used / (used + avail) * 100 + 0.5;
4621 static int showdf(char *mntdir, struct obd_statfs *stat,
4622 char *uuid, enum mntdf_flags flags,
4623 char *type, int index, int rc)
4625 long long avail, used, total;
4627 char *suffix = "KMGTPEZY";
4628 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4629 char tbuf[3 * sizeof(__u64)];
4630 char ubuf[3 * sizeof(__u64)];
4631 char abuf[3 * sizeof(__u64)];
4632 char rbuf[3 * sizeof(__u64)];
4639 if (flags & MNTDF_INODES) {
4640 avail = stat->os_ffree;
4641 used = stat->os_files - stat->os_ffree;
4642 total = stat->os_files;
4644 int shift = flags & MNTDF_COOKED ? 0 : 10;
4646 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4647 used = ((stat->os_blocks - stat->os_bfree) *
4648 stat->os_bsize) >> shift;
4649 total = (stat->os_blocks * stat->os_bsize) >> shift;
4652 ratio = obd_statfs_ratio(stat);
4654 if (flags & MNTDF_COOKED) {
4658 cook_val = (double)total;
4661 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4664 snprintf(tbuf, sizeof(tbuf), CDF, total);
4666 cook_val = (double)used;
4669 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4672 snprintf(ubuf, sizeof(ubuf), CDF, used);
4674 cook_val = (double)avail;
4677 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4680 snprintf(abuf, sizeof(abuf), CDF, avail);
4682 snprintf(tbuf, sizeof(tbuf), CDF, total);
4683 snprintf(ubuf, sizeof(tbuf), CDF, used);
4684 snprintf(abuf, sizeof(tbuf), CDF, avail);
4687 sprintf(rbuf, RDF, ratio);
4688 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4689 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4691 printf("[%s:%d]", type, index);
4693 if (stat->os_state) {
4695 * Each character represents the matching
4698 const char state_names[] = "DRSI";
4703 for (i = 0, state = stat->os_state;
4704 state && i < sizeof(state_names); i++) {
4705 if (!(state & (1 << i)))
4707 printf("%c", state_names[i]);
4715 printf(UUF": inactive device\n", uuid);
4718 printf(UUF": %s\n", uuid, strerror(-rc));
4725 struct ll_stat_type {
4730 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4732 struct ll_statfs_data {
4734 struct obd_statfs sd_st;
4737 struct ll_statfs_buf {
4739 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4742 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4743 int ops, struct ll_statfs_buf *lsb)
4745 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4746 struct obd_uuid uuid_buf;
4747 char *poolname = NULL;
4748 struct ll_stat_type types[] = {
4749 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4750 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4751 { .st_name = NULL } };
4752 struct ll_stat_type *tp;
4753 __u64 ost_ffree = 0;
4761 poolname = strchr(pool, '.');
4762 if (poolname != NULL) {
4763 if (strncmp(fsname, pool, strlen(fsname))) {
4764 fprintf(stderr, "filesystem name incorrect\n");
4772 fd = open(mntdir, O_RDONLY);
4775 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4780 if (flags & MNTDF_SHOW) {
4781 if (flags & MNTDF_INODES)
4782 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4783 "UUID", "Inodes", "IUsed", "IFree",
4784 "IUse%", "Mounted on");
4786 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4788 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4789 "Used", "Available", "Use%", "Mounted on");
4792 for (tp = types; tp->st_name != NULL; tp++) {
4793 bool have_ost = false;
4795 if (!(tp->st_op & ops))
4798 for (index = 0; ; index++) {
4799 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4800 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4801 type = flags & MNTDF_LAZY ?
4802 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4803 rc2 = llapi_obd_fstatfs(fd, type, index,
4804 &stat_buf, &uuid_buf);
4809 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4810 if (!(flags & MNTDF_VERBOSE))
4812 } else if (rc2 < 0 && rc == 0) {
4816 /* If we have OSTs then don't report MDT block counts.
4817 * For MDT-only filesystems the expectation is that all
4818 * layouts have a DoM component. For filesystems with
4819 * OSTs, files are not necessarily going to store data
4820 * on MDTs, and MDT space is limited to a fraction of
4821 * OST space, so don't include it in the summary.
4823 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
4830 if (poolname && tp->st_op == LL_STATFS_LOV &&
4831 llapi_search_ost(fsname, poolname,
4832 obd_uuid2str(&uuid_buf)) != 1)
4835 /* the llapi_obd_statfs() call may have returned with
4836 * an error, but if it filled in uuid_buf we will at
4837 * lease use that to print out a message for that OBD.
4838 * If we didn't get anything in the uuid_buf, then fill
4839 * it in so that we can print an error message. */
4840 if (uuid_buf.uuid[0] == '\0')
4841 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4842 "%s%04x", tp->st_name, index);
4844 lsb->sb_buf[lsb->sb_count].sd_index = index;
4845 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4848 if (flags & MNTDF_SHOW)
4849 showdf(mntdir, &stat_buf,
4850 obd_uuid2str(&uuid_buf), flags,
4851 tp->st_name, index, rc2);
4856 if (tp->st_op == LL_STATFS_LMV) {
4857 sum.os_ffree += stat_buf.os_ffree;
4858 sum.os_files += stat_buf.os_files;
4859 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4860 ost_ffree += stat_buf.os_ffree;
4862 sum.os_blocks += stat_buf.os_blocks *
4864 sum.os_bfree += stat_buf.os_bfree *
4866 sum.os_bavail += stat_buf.os_bavail *
4873 /* If we don't have as many objects free on the OST as inodes
4874 * on the MDS, we reduce the total number of inodes to
4875 * compensate, so that the "inodes in use" number is correct.
4876 * Matches ll_statfs_internal() so the results are consistent. */
4877 if (ost_ffree < sum.os_ffree) {
4878 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4879 sum.os_ffree = ost_ffree;
4881 if (flags & MNTDF_SHOW) {
4883 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4890 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4892 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4894 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4896 int r1 = obd_statfs_ratio(st1);
4897 int r2 = obd_statfs_ratio(st2);
4898 int64_t result = r1 - r2;
4900 /* if both space usage are above 90, compare free inodes */
4901 if (r1 > 90 && r2 > 90)
4902 result = st2->os_ffree - st1->os_ffree;
4906 else if (result == 0)
4913 static int lfs_setdirstripe(int argc, char **argv)
4917 struct lfs_setstripe_args lsa = { 0 };
4918 struct llapi_stripe_param *param = NULL;
4919 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4922 char *mode_opt = NULL;
4923 bool default_stripe = false;
4924 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4925 mode_t previous_mode = 0;
4926 bool delete = false;
4927 struct ll_statfs_buf *lsb = NULL;
4928 char mntdir[PATH_MAX] = "";
4929 bool auto_distributed = false;
4931 struct option long_opts[] = {
4932 { .val = 'c', .name = "count", .has_arg = required_argument },
4933 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4934 { .val = 'd', .name = "delete", .has_arg = no_argument },
4935 { .val = 'D', .name = "default", .has_arg = no_argument },
4936 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4937 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4938 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4939 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4940 { .val = 'i', .name = "index", .has_arg = required_argument },
4942 { .val = 'o', .name = "mode", .has_arg = required_argument },
4943 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4944 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4946 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4947 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4950 setstripe_args_init(&lsa);
4952 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4960 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4963 "%s %s: invalid stripe count '%s'\n",
4964 progname, argv[0], optarg);
4970 default_stripe = true;
4973 default_stripe = true;
4975 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4977 fprintf(stderr, "warning: '--hash-type' and '-t' "
4978 "deprecated, use '--mdt-hash' or '-H' instead\n");
4981 lsa.lsa_pattern = check_hashtype(optarg);
4982 if (lsa.lsa_pattern == 0) {
4984 "%s %s: bad stripe hash type '%s'\n",
4985 progname, argv[0], optarg);
4990 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4991 if (strcmp(argv[optind - 1], "--index") == 0)
4993 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4996 lsa.lsa_nr_tgts = parse_targets(mdts,
4997 sizeof(mdts) / sizeof(__u32),
4998 lsa.lsa_nr_tgts, optarg);
4999 if (lsa.lsa_nr_tgts < 0) {
5001 "%s %s: invalid MDT target(s) '%s'\n",
5002 progname, argv[0], optarg);
5006 lsa.lsa_tgts = mdts;
5007 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5008 lsa.lsa_stripe_off = mdts[0];
5010 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
5012 fprintf(stderr, "warning: '-m' is deprecated, "
5013 "use '--mode' or '-o' instead\n");
5019 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5020 progname, argv[0], argv[optind - 1]);
5025 if (optind == argc) {
5026 fprintf(stderr, "%s %s: DIR must be specified\n",
5031 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5032 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5034 "%s %s: stripe offset and count must be specified\n",
5040 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5041 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5043 "%s %s: cannot specify -d with -c or -i options\n",
5048 if (mode_opt != NULL) {
5049 mode = strtoul(mode_opt, &end, 8);
5052 "%s %s: bad MODE '%s'\n",
5053 progname, argv[0], mode_opt);
5056 previous_mode = umask(0);
5060 * initialize stripe parameters, in case param is converted to specific,
5061 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5063 param = calloc(1, offsetof(typeof(*param),
5064 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5065 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5066 if (param == NULL) {
5068 "%s %s: cannot allocate memory for parameters: %s\n",
5069 progname, argv[0], strerror(ENOMEM));
5073 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5074 param->lsp_stripe_count = lsa.lsa_stripe_count;
5075 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5076 param->lsp_stripe_offset = -1;
5078 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5079 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5080 param->lsp_stripe_pattern = lsa.lsa_pattern;
5082 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5083 param->lsp_pool = lsa.lsa_pool_name;
5084 param->lsp_is_specific = false;
5085 if (lsa.lsa_nr_tgts > 1) {
5086 if (lsa.lsa_stripe_count > 0 &&
5087 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5088 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5089 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5090 "match the number of MDTs: %d\n",
5091 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5096 param->lsp_is_specific = true;
5097 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5098 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5101 dname = argv[optind];
5103 if (default_stripe) {
5104 result = llapi_dir_set_default_lmv(dname, param);
5106 /* if current \a dname isn't under the same \a mntdir
5107 * as the last one, and the last one was
5108 * auto-distributed, restore \a param.
5110 if (mntdir[0] != '\0' &&
5111 strncmp(dname, mntdir, strlen(mntdir)) &&
5113 param->lsp_is_specific = false;
5114 param->lsp_stripe_offset = -1;
5115 auto_distributed = false;
5118 if (!param->lsp_is_specific &&
5119 param->lsp_stripe_offset == -1) {
5120 char path[PATH_MAX] = "";
5123 lsb = malloc(sizeof(*lsb));
5131 /* use mntdir for dirname() temporarily */
5132 strncpy(mntdir, dname, sizeof(mntdir));
5133 if (!realpath(dirname(mntdir), path)) {
5136 "error: invalid path '%s': %s\n",
5137 argv[optind], strerror(errno));
5142 result = llapi_search_mounts(path, 0, mntdir,
5144 if (result < 0 || mntdir[0] == '\0') {
5146 "No suitable Lustre mount found\n");
5150 result = mntdf(mntdir, NULL, NULL, 0,
5151 LL_STATFS_LMV, lsb);
5155 if (param->lsp_stripe_count > lsb->sb_count) {
5157 "error: stripe count %d is too big\n",
5158 param->lsp_stripe_count);
5163 qsort(lsb->sb_buf, lsb->sb_count,
5164 sizeof(struct ll_statfs_data),
5165 ll_statfs_data_comp);
5167 auto_distributed = true;
5170 if (auto_distributed) {
5172 int nr = MAX(param->lsp_stripe_count,
5175 /* don't use server whose usage is above 90% */
5176 while (nr != param->lsp_stripe_count &&
5177 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
5179 nr = MAX(param->lsp_stripe_count,
5182 /* get \a r between [0, nr) */
5185 param->lsp_stripe_offset =
5186 lsb->sb_buf[r].sd_index;
5187 if (param->lsp_stripe_count > 1) {
5190 param->lsp_is_specific = true;
5191 for (; i < param->lsp_stripe_count; i++)
5192 param->lsp_tgts[(i + r) % nr] =
5193 lsb->sb_buf[i].sd_index;
5197 result = llapi_dir_create(dname, mode, param);
5202 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5203 progname, dname, strerror(-result));
5206 dname = argv[++optind];
5207 } while (dname != NULL);
5209 if (mode_opt != NULL)
5210 umask(previous_mode);
5218 static int lfs_rmentry(int argc, char **argv)
5225 fprintf(stderr, "error: %s: missing dirname\n",
5231 dname = argv[index];
5232 while (dname != NULL) {
5233 result = llapi_direntry_remove(dname);
5235 fprintf(stderr, "error: %s: remove dir entry '%s' "
5236 "failed\n", argv[0], dname);
5239 dname = argv[++index];
5244 static int lfs_mv(int argc, char **argv)
5246 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5247 struct find_param param = {
5254 struct option long_opts[] = {
5255 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5256 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5259 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5261 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5263 fprintf(stderr, "warning: '-M' deprecated"
5264 ", use '--mdt-index' or '-m' instead\n");
5267 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5269 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5275 param.fp_verbose = VERBOSE_DETAIL;
5278 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5279 progname, argv[optind - 1]);
5284 if (lmu.lum_stripe_offset == -1) {
5285 fprintf(stderr, "%s mv: MDT index must be specified\n",
5290 if (optind >= argc) {
5291 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5296 /* initialize migrate mdt parameters */
5297 param.fp_lmv_md = &lmu;
5298 param.fp_migrate = 1;
5299 rc = llapi_migrate_mdt(argv[optind], ¶m);
5301 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5302 progname, argv[optind], param.fp_mdt_index,
5307 static int lfs_osts(int argc, char **argv)
5309 return lfs_tgts(argc, argv);
5312 static int lfs_mdts(int argc, char **argv)
5314 return lfs_tgts(argc, argv);
5317 static int lfs_df(int argc, char **argv)
5319 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5320 enum mntdf_flags flags = MNTDF_SHOW;
5321 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5322 int c, rc = 0, index = 0;
5323 char fsname[PATH_MAX] = "", *pool_name = NULL;
5324 struct option long_opts[] = {
5325 { .val = 'h', .name = "human-readable",
5326 .has_arg = no_argument },
5327 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5328 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5329 { .val = 'p', .name = "pool", .has_arg = required_argument },
5330 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5333 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5336 flags |= MNTDF_COOKED;
5339 flags |= MNTDF_INODES;
5342 flags |= MNTDF_LAZY;
5348 flags |= MNTDF_VERBOSE;
5354 if (optind < argc && !realpath(argv[optind], path)) {
5356 fprintf(stderr, "error: invalid path '%s': %s\n",
5357 argv[optind], strerror(-rc));
5361 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5362 /* Check if we have a mount point */
5363 if (mntdir[0] == '\0')
5366 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5367 if (rc || path[0] != '\0')
5369 fsname[0] = '\0'; /* avoid matching in next loop */
5370 mntdir[0] = '\0'; /* avoid matching in next loop */
5376 static int lfs_getname(int argc, char **argv)
5378 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5379 int rc = 0, index = 0, c;
5380 char buf[sizeof(struct obd_uuid)];
5382 while ((c = getopt(argc, argv, "h")) != -1)
5385 if (optind == argc) { /* no paths specified, get all paths. */
5386 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5387 rc = llapi_getname(mntdir, buf, sizeof(buf));
5390 "cannot get name for `%s': %s\n",
5391 mntdir, strerror(-rc));
5395 printf("%s %s\n", buf, mntdir);
5397 path[0] = fsname[0] = mntdir[0] = 0;
5399 } else { /* paths specified, only attempt to search these. */
5400 for (; optind < argc; optind++) {
5401 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5404 "cannot get name for `%s': %s\n",
5405 argv[optind], strerror(-rc));
5409 printf("%s %s\n", buf, argv[optind]);
5415 static int lfs_check(int argc, char **argv)
5417 char mntdir[PATH_MAX] = {'\0'};
5426 fprintf(stderr, "%s check: server type must be specified\n",
5431 obd_types[0] = obd_type1;
5432 obd_types[1] = obd_type2;
5433 obd_types[2] = obd_type3;
5435 if (strcmp(argv[1], "osts") == 0) {
5436 strcpy(obd_types[0], "osc");
5437 } else if (strcmp(argv[1], "mdts") == 0 ||
5438 strcmp(argv[1], "mds") == 0) {
5439 strcpy(obd_types[0], "mdc");
5440 } else if (strcmp(argv[1], "mgts") == 0) {
5441 strcpy(obd_types[0], "mgc");
5442 } else if (strcmp(argv[1], "all") == 0 ||
5443 strcmp(argv[1], "servers") == 0) {
5445 strcpy(obd_types[0], "osc");
5446 strcpy(obd_types[1], "mdc");
5447 strcpy(obd_types[2], "mgc");
5449 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5454 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5455 if (rc < 0 || mntdir[0] == '\0') {
5457 "%s check: cannot find mounted Lustre filesystem: %s\n",
5458 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5462 rc = llapi_target_check(num_types, obd_types, mntdir);
5464 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5465 progname, argv[1], strerror(-rc));
5471 #ifdef HAVE_SYS_QUOTA_H
5472 #define ARG2INT(nr, str, msg) \
5475 nr = strtol(str, &endp, 0); \
5476 if (*endp != '\0') { \
5477 fprintf(stderr, "%s: bad %s '%s'\n", \
5478 progname, msg, str); \
5483 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5485 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5486 * returns the value or ULONG_MAX on integer overflow or incorrect format
5488 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5489 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5490 * 3. empty integer value is interpreted as 0
5492 static unsigned long str2sec(const char* timestr)
5494 const char spec[] = "smhdw";
5495 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5496 unsigned long val = 0;
5499 if (strpbrk(timestr, spec) == NULL) {
5500 /* no specifiers inside the time string,
5501 should treat it as an integer value */
5502 val = strtoul(timestr, &tail, 10);
5503 return *tail ? ULONG_MAX : val;
5506 /* format string is XXwXXdXXhXXmXXs */
5512 v = strtoul(timestr, &tail, 10);
5513 if (v == ULONG_MAX || *tail == '\0')
5514 /* value too large (ULONG_MAX or more)
5515 or missing specifier */
5518 ptr = strchr(spec, *tail);
5520 /* unknown specifier */
5525 /* check if product will overflow the type */
5526 if (!(v < ULONG_MAX / mult[ind]))
5529 ADD_OVERFLOW(val, mult[ind] * v);
5530 if (val == ULONG_MAX)
5542 #define ARG2ULL(nr, str, def_units) \
5544 unsigned long long limit, units = def_units; \
5547 rc = llapi_parse_size(str, &limit, &units, 1); \
5549 fprintf(stderr, "%s: invalid limit '%s'\n", \
5556 static inline int has_times_option(int argc, char **argv)
5560 for (i = 1; i < argc; i++)
5561 if (!strcmp(argv[i], "-t"))
5567 int lfs_setquota_times(int argc, char **argv)
5570 struct if_quotactl qctl;
5571 char *mnt, *obd_type = (char *)qctl.obd_type;
5572 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5573 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5574 struct option long_opts[] = {
5575 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5576 { .val = 'g', .name = "group", .has_arg = no_argument },
5577 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5578 { .val = 'p', .name = "projid", .has_arg = no_argument },
5579 { .val = 't', .name = "times", .has_arg = no_argument },
5580 { .val = 'u', .name = "user", .has_arg = no_argument },
5584 memset(&qctl, 0, sizeof(qctl));
5585 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5586 qctl.qc_type = ALLQUOTA;
5588 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5589 long_opts, NULL)) != -1) {
5600 if (qctl.qc_type != ALLQUOTA) {
5601 fprintf(stderr, "error: -u/g/p can't be used "
5602 "more than once\n");
5605 qctl.qc_type = qtype;
5608 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5609 fprintf(stderr, "error: bad block-grace: %s\n",
5613 dqb->dqb_valid |= QIF_BTIME;
5616 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5617 fprintf(stderr, "error: bad inode-grace: %s\n",
5621 dqb->dqb_valid |= QIF_ITIME;
5623 case 't': /* Yes, of course! */
5625 default: /* getopt prints error message for us when opterr != 0 */
5630 if (qctl.qc_type == ALLQUOTA) {
5631 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5635 if (optind != argc - 1) {
5636 fprintf(stderr, "error: unexpected parameters encountered\n");
5640 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5641 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5642 fprintf(stderr, "error: grace time is too large\n");
5647 rc = llapi_quotactl(mnt, &qctl);
5650 fprintf(stderr, "%s %s ", obd_type,
5651 obd_uuid2str(&qctl.obd_uuid));
5652 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5659 #define BSLIMIT (1 << 0)
5660 #define BHLIMIT (1 << 1)
5661 #define ISLIMIT (1 << 2)
5662 #define IHLIMIT (1 << 3)
5664 int lfs_setquota(int argc, char **argv)
5667 struct if_quotactl qctl;
5668 char *mnt, *obd_type = (char *)qctl.obd_type;
5669 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5670 struct option long_opts[] = {
5671 { .val = 'b', .name = "block-softlimit",
5672 .has_arg = required_argument },
5673 { .val = 'B', .name = "block-hardlimit",
5674 .has_arg = required_argument },
5675 { .val = 'd', .name = "default", .has_arg = no_argument },
5676 { .val = 'g', .name = "group", .has_arg = required_argument },
5677 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5678 { .val = 'i', .name = "inode-softlimit",
5679 .has_arg = required_argument },
5680 { .val = 'I', .name = "inode-hardlimit",
5681 .has_arg = required_argument },
5682 { .val = 'p', .name = "projid", .has_arg = required_argument },
5683 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5684 { .val = 'u', .name = "user", .has_arg = required_argument },
5685 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5687 unsigned limit_mask = 0;
5689 bool use_default = false;
5692 if (has_times_option(argc, argv))
5693 return lfs_setquota_times(argc, argv);
5695 memset(&qctl, 0, sizeof(qctl));
5696 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5697 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5698 * so it can be used as a marker that qc_type
5699 * isn't reinitialized from command line */
5701 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5702 long_opts, NULL)) != -1) {
5705 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5708 goto quota_type_def;
5711 rc = name2uid(&qctl.qc_id, optarg);
5714 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5717 goto quota_type_def;
5720 rc = name2gid(&qctl.qc_id, optarg);
5723 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5726 goto quota_type_def;
5729 rc = name2projid(&qctl.qc_id, optarg);
5732 qctl.qc_id = strtoul(optarg, &endptr, 10);
5733 if (*endptr != '\0') {
5734 fprintf(stderr, "%s setquota: invalid"
5735 " id '%s'\n", progname, optarg);
5740 if (qctl.qc_id == 0) {
5741 fprintf(stderr, "%s setquota: can't set quota"
5742 " for root usr/group/project.\n",
5748 if (qctl.qc_type != ALLQUOTA) {
5750 "%s setquota: only one of -u, -U, -g,"
5751 " -G, -p or -P may be specified\n",
5755 qctl.qc_type = qtype;
5758 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5762 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5763 dqb->dqb_bsoftlimit >>= 10;
5764 limit_mask |= BSLIMIT;
5765 if (dqb->dqb_bsoftlimit &&
5766 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5768 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5769 "See '%s help setquota' or Lustre manual for details\n",
5770 progname, dqb->dqb_bsoftlimit,
5774 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5775 dqb->dqb_bhardlimit >>= 10;
5776 limit_mask |= BHLIMIT;
5777 if (dqb->dqb_bhardlimit &&
5778 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5780 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5781 "See '%s help setquota' or Lustre manual for details\n",
5782 progname, dqb->dqb_bhardlimit,
5786 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5787 limit_mask |= ISLIMIT;
5788 if (dqb->dqb_isoftlimit &&
5789 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5791 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5792 "See '%s help setquota' or Lustre manual for details\n",
5793 progname, dqb->dqb_isoftlimit,
5797 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5798 limit_mask |= IHLIMIT;
5799 if (dqb->dqb_ihardlimit &&
5800 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5802 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5803 "See '%s help setquota' or Lustre manual for details\n",
5804 progname, dqb->dqb_ihardlimit,
5809 "%s setquota: unrecognized option '%s'\n",
5810 progname, argv[optind - 1]);
5815 if (qctl.qc_type == ALLQUOTA) {
5817 "%s setquota: either -u or -g must be specified\n",
5822 if (!use_default && limit_mask == 0) {
5824 "%s setquota: at least one limit must be specified\n",
5829 if (use_default && limit_mask != 0) {
5831 "%s setquota: limits should not be specified when"
5832 " using default quota\n",
5837 if (use_default && qctl.qc_id == 0) {
5839 "%s setquota: can not set default quota for root"
5840 " user/group/project\n",
5845 if (optind != argc - 1) {
5847 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5848 progname, argv[optind]);
5855 dqb->dqb_bhardlimit = 0;
5856 dqb->dqb_bsoftlimit = 0;
5857 dqb->dqb_ihardlimit = 0;
5858 dqb->dqb_isoftlimit = 0;
5861 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5862 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5863 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5864 /* sigh, we can't just set blimits/ilimits */
5865 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5866 .qc_type = qctl.qc_type,
5867 .qc_id = qctl.qc_id};
5869 rc = llapi_quotactl(mnt, &tmp_qctl);
5873 if (!(limit_mask & BHLIMIT))
5874 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5875 if (!(limit_mask & BSLIMIT))
5876 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5877 if (!(limit_mask & IHLIMIT))
5878 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5879 if (!(limit_mask & ISLIMIT))
5880 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5882 /* Keep grace times if we have got no softlimit arguments */
5883 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5884 dqb->dqb_valid |= QIF_BTIME;
5885 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5888 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5889 dqb->dqb_valid |= QIF_ITIME;
5890 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5894 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5895 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5897 rc = llapi_quotactl(mnt, &qctl);
5901 "%s setquota: cannot quotactl '%s' '%s': %s",
5903 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5910 /* Converts seconds value into format string
5911 * result is returned in buf
5913 * 1. result is in descenting order: 1w2d3h4m5s
5914 * 2. zero fields are not filled (except for p. 3): 5d1s
5915 * 3. zero seconds value is presented as "0s"
5917 static char * __sec2str(time_t seconds, char *buf)
5919 const char spec[] = "smhdw";
5920 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5925 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5926 c = seconds / mult[i];
5928 if (c > 0 || (i == 0 && buf == tail))
5929 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5937 static void sec2str(time_t seconds, char *buf, int rc)
5944 tail = __sec2str(seconds, tail);
5946 if (rc && tail - buf < 39) {
5952 static void diff2str(time_t seconds, char *buf, time_t now)
5958 if (seconds <= now) {
5959 strcpy(buf, "none");
5962 __sec2str(seconds - now, buf);
5965 static void print_quota_title(char *name, struct if_quotactl *qctl,
5966 bool human_readable, bool show_default)
5969 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
5970 printf("%15s %8s%8s%8s %8s%8s%8s\n",
5971 "Filesystem", "bquota", "blimit", "bgrace",
5972 "iquota", "ilimit", "igrace");
5974 printf("Disk quotas for %s %s (%cid %u):\n",
5975 qtype_name(qctl->qc_type), name,
5976 *qtype_name(qctl->qc_type), qctl->qc_id);
5977 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5978 "Filesystem", human_readable ? "used" : "kbytes",
5979 "quota", "limit", "grace",
5980 "files", "quota", "limit", "grace");
5984 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5987 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5990 snprintf(buf, buflen, "%5.4gP",
5991 (double)num / ((__u64)1 << 40));
5993 snprintf(buf, buflen, "%5.4gT",
5994 (double)num / (1 << 30));
5996 snprintf(buf, buflen, "%5.4gG",
5997 (double)num / (1 << 20));
5999 snprintf(buf, buflen, "%5.4gM",
6000 (double)num / (1 << 10));
6002 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6006 #define STRBUF_LEN 32
6007 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6008 int rc, bool h, bool show_default)
6014 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6015 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6016 int bover = 0, iover = 0;
6017 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6018 char numbuf[3][STRBUF_LEN];
6020 char strbuf[STRBUF_LEN];
6022 if (dqb->dqb_bhardlimit &&
6023 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6025 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6026 if (dqb->dqb_btime > now) {
6033 if (dqb->dqb_ihardlimit &&
6034 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6036 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6037 if (dqb->dqb_itime > now) {
6045 if (strlen(mnt) > 15)
6046 printf("%s\n%15s", mnt, "");
6048 printf("%15s", mnt);
6051 diff2str(dqb->dqb_btime, timebuf, now);
6052 else if (show_default)
6053 snprintf(timebuf, sizeof(timebuf), "%llu",
6056 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6057 strbuf, sizeof(strbuf), h);
6058 if (rc == -EREMOTEIO)
6059 sprintf(numbuf[0], "%s*", strbuf);
6061 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6062 "%s" : "[%s]", strbuf);
6064 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6065 if (type == QC_GENERAL)
6066 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6067 "%s" : "[%s]", strbuf);
6069 sprintf(numbuf[1], "%s", "-");
6071 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6072 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6073 "%s" : "[%s]", strbuf);
6076 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6078 printf(" %7s%c %6s %7s %7s",
6079 numbuf[0], bover ? '*' : ' ', numbuf[1],
6080 numbuf[2], bover > 1 ? timebuf : "-");
6084 diff2str(dqb->dqb_itime, timebuf, now);
6085 else if (show_default)
6086 snprintf(timebuf, sizeof(timebuf), "%llu",
6089 snprintf(numbuf[0], sizeof(numbuf),
6090 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6091 (uintmax_t)dqb->dqb_curinodes);
6093 if (type == QC_GENERAL)
6094 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6096 (uintmax_t)dqb->dqb_isoftlimit);
6098 sprintf(numbuf[1], "%s", "-");
6100 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6101 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6104 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6105 else if (type != QC_OSTIDX)
6106 printf(" %7s%c %6s %7s %7s",
6107 numbuf[0], iover ? '*' : ' ', numbuf[1],
6108 numbuf[2], iover > 1 ? timebuf : "-");
6110 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6113 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6114 qctl->qc_cmd == Q_GETOINFO) {
6118 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6119 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6120 printf("Block grace time: %s; Inode grace time: %s\n",
6121 bgtimebuf, igtimebuf);
6125 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6126 bool h, __u64 *total)
6128 int rc = 0, rc1 = 0, count = 0;
6129 __u32 valid = qctl->qc_valid;
6131 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6133 fprintf(stderr, "can not get %s count: %s\n",
6134 is_mdt ? "mdt": "ost", strerror(-rc));
6138 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6139 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6140 rc = llapi_quotactl(mnt, qctl);
6142 /* It is remote client case. */
6143 if (rc == -EOPNOTSUPP) {
6150 fprintf(stderr, "quotactl %s%d failed.\n",
6151 is_mdt ? "mdt": "ost", qctl->qc_idx);
6155 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6156 qctl->qc_valid, 0, h, false);
6157 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6158 qctl->qc_dqblk.dqb_bhardlimit;
6161 qctl->qc_valid = valid;
6165 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6166 int verbose, int quiet, bool human_readable,
6169 int rc1 = 0, rc2 = 0, rc3 = 0;
6170 char *obd_type = (char *)qctl->obd_type;
6171 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6172 __u64 total_ialloc = 0, total_balloc = 0;
6173 bool use_default_for_blk = false;
6174 bool use_default_for_file = false;
6177 rc1 = llapi_quotactl(mnt, qctl);
6181 fprintf(stderr, "%s quotas are not enabled.\n",
6182 qtype_name(qctl->qc_type));
6185 fprintf(stderr, "Permission denied.\n");
6188 /* We already got error message. */
6191 fprintf(stderr, "Unexpected quotactl error: %s\n",
6196 if (!show_default && qctl->qc_id == 0) {
6197 qctl->qc_dqblk.dqb_bhardlimit = 0;
6198 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6199 qctl->qc_dqblk.dqb_ihardlimit = 0;
6200 qctl->qc_dqblk.dqb_isoftlimit = 0;
6201 qctl->qc_dqblk.dqb_btime = 0;
6202 qctl->qc_dqblk.dqb_itime = 0;
6203 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6206 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6207 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6208 use_default_for_blk = true;
6209 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6212 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6213 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6214 use_default_for_file = true;
6215 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6218 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6219 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6220 print_quota_title(name, qctl, human_readable, show_default);
6222 if (rc1 && *obd_type)
6223 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6225 if (qctl->qc_valid != QC_GENERAL)
6228 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6229 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6230 (QIF_LIMITS|QIF_USAGE));
6232 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6234 if (!show_default && verbose &&
6235 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6236 char strbuf[STRBUF_LEN];
6238 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6240 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6242 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6244 printf("Total allocated inode limit: %ju, total "
6245 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6249 if (use_default_for_blk)
6250 printf("%cid %u is using default block quota setting\n",
6251 *qtype_name(qctl->qc_type), qctl->qc_id);
6253 if (use_default_for_file)
6254 printf("%cid %u is using default file quota setting\n",
6255 *qtype_name(qctl->qc_type), qctl->qc_id);
6257 if (rc1 || rc2 || rc3 || inacc)
6258 printf("Some errors happened when getting quota info. "
6259 "Some devices may be not working or deactivated. "
6260 "The data in \"[]\" is inaccurate.\n");
6266 static int lfs_project(int argc, char **argv)
6268 int ret = 0, err = 0, c, i;
6269 struct project_handle_control phc = { 0 };
6270 enum lfs_project_ops_t op;
6273 phc.assign_projid = false;
6274 /* default action */
6275 op = LFS_PROJECT_LIST;
6277 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6280 if (op != LFS_PROJECT_LIST) {
6282 "%s: cannot specify '-c' '-C' '-s' together\n",
6287 op = LFS_PROJECT_CHECK;
6290 if (op != LFS_PROJECT_LIST) {
6292 "%s: cannot specify '-c' '-C' '-s' together\n",
6297 op = LFS_PROJECT_CLEAR;
6300 if (op != LFS_PROJECT_LIST) {
6302 "%s: cannot specify '-c' '-C' '-s' together\n",
6307 phc.set_inherit = true;
6308 op = LFS_PROJECT_SET;
6314 phc.keep_projid = true;
6317 phc.recursive = true;
6320 phc.projid = strtoul(optarg, NULL, 0);
6321 phc.assign_projid = true;
6325 phc.newline = false;
6328 fprintf(stderr, "%s: invalid option '%c'\n",
6334 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6335 op = LFS_PROJECT_SET;
6336 phc.set_projid = true;
6337 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6338 phc.set_projid = true;
6342 case LFS_PROJECT_CHECK:
6343 if (phc.keep_projid) {
6345 "%s: '-k' is useless together with '-c'\n",
6350 case LFS_PROJECT_CLEAR:
6353 "%s: '-0' is useless together with '-C'\n",
6357 if (phc.assign_projid) {
6359 "%s: '-p' is useless together with '-C'\n",
6364 case LFS_PROJECT_SET:
6367 "%s: '-0' is useless together with '-s'\n",
6371 if (phc.keep_projid) {
6373 "%s: '-k' is useless together with '-s'\n",
6381 "%s: '-0' is useless for list operations\n",
6391 fprintf(stderr, "%s: missing file or directory target(s)\n",
6396 for (i = 0; i < argc; i++) {
6398 case LFS_PROJECT_CHECK:
6399 err = lfs_project_check(argv[i], &phc);
6401 case LFS_PROJECT_LIST:
6402 err = lfs_project_list(argv[i], &phc);
6404 case LFS_PROJECT_CLEAR:
6405 err = lfs_project_clear(argv[i], &phc);
6407 case LFS_PROJECT_SET:
6408 err = lfs_project_set(argv[i], &phc);
6420 static int lfs_quota(int argc, char **argv)
6423 char *mnt, *name = NULL;
6424 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6425 .qc_type = ALLQUOTA };
6426 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6427 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6429 __u32 valid = QC_GENERAL, idx = 0;
6430 bool human_readable = false;
6431 bool show_default = false;
6434 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6437 show_default = true;
6442 show_default = true;
6447 show_default = true;
6451 if (qctl.qc_type != ALLQUOTA) {
6453 "%s quota: only one of -u, -g, or -p may be specified\n",
6457 qctl.qc_type = qtype;
6460 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6463 valid = qctl.qc_valid = QC_UUID;
6464 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6467 valid = qctl.qc_valid = QC_MDTIDX;
6468 idx = qctl.qc_idx = atoi(optarg);
6469 if (idx == 0 && *optarg != '0') {
6471 "%s quota: invalid MDT index '%s'\n",
6477 valid = qctl.qc_valid = QC_OSTIDX;
6478 idx = qctl.qc_idx = atoi(optarg);
6479 if (idx == 0 && *optarg != '0') {
6481 "%s quota: invalid OST index '%s'\n",
6493 human_readable = true;
6496 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6497 progname, argv[optind - 1]);
6502 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6503 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6504 optind == argc - 1 && !show_default) {
6506 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6507 qctl.qc_valid = valid;
6510 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6511 qctl.qc_type = qtype;
6512 if (qtype == USRQUOTA) {
6513 qctl.qc_id = geteuid();
6514 rc = uid2name(&name, qctl.qc_id);
6516 qctl.qc_id = getegid();
6517 rc = gid2name(&name, qctl.qc_id);
6522 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6523 human_readable, show_default);
6528 /* lfs quota -u username /path/to/lustre/mount */
6529 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6530 /* options should be followed by u/g-name and mntpoint */
6531 if ((!show_default && optind + 2 != argc) ||
6532 (show_default && optind + 1 != argc) ||
6533 qctl.qc_type == ALLQUOTA) {
6535 "%s quota: name and mount point must be specified\n",
6540 if (!show_default) {
6541 name = argv[optind++];
6542 switch (qctl.qc_type) {
6544 rc = name2uid(&qctl.qc_id, name);
6547 rc = name2gid(&qctl.qc_id, name);
6550 rc = name2projid(&qctl.qc_id, name);
6557 qctl.qc_valid = QC_GENERAL;
6558 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6563 qctl.qc_id = strtoul(name, &endptr, 10);
6564 if (*endptr != '\0') {
6565 fprintf(stderr, "%s quota: invalid id '%s'\n",
6570 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6571 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6577 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6578 human_readable, show_default);
6581 #endif /* HAVE_SYS_QUOTA_H! */
6583 static int flushctx_ioctl(char *mp)
6587 fd = open(mp, O_RDONLY);
6589 fprintf(stderr, "flushctx: error open %s: %s\n",
6590 mp, strerror(errno));
6594 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6596 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6597 mp, strerror(errno));
6603 static int lfs_flushctx(int argc, char **argv)
6605 int kdestroy = 0, c;
6606 char mntdir[PATH_MAX] = {'\0'};
6610 while ((c = getopt(argc, argv, "k")) != -1) {
6616 fprintf(stderr, "error: %s: option '-%c' "
6617 "unrecognized\n", argv[0], c);
6623 if ((rc = system("kdestroy > /dev/null")) != 0) {
6624 rc = WEXITSTATUS(rc);
6625 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6629 if (optind >= argc) {
6630 /* flush for all mounted lustre fs. */
6631 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6632 /* Check if we have a mount point */
6633 if (mntdir[0] == '\0')
6636 if (flushctx_ioctl(mntdir))
6639 mntdir[0] = '\0'; /* avoid matching in next loop */
6642 /* flush fs as specified */
6643 while (optind < argc) {
6644 if (flushctx_ioctl(argv[optind++]))
6651 static int lfs_cp(int argc, char **argv)
6653 fprintf(stderr, "remote client copy file(s).\n"
6654 "obsolete, does not support it anymore.\n");
6658 static int lfs_ls(int argc, char **argv)
6660 fprintf(stderr, "remote client lists directory contents.\n"
6661 "obsolete, does not support it anymore.\n");
6665 static int lfs_changelog(int argc, char **argv)
6667 void *changelog_priv;
6668 struct changelog_rec *rec;
6669 long long startrec = 0, endrec = 0;
6671 struct option long_opts[] = {
6672 { .val = 'f', .name = "follow", .has_arg = no_argument },
6674 char short_opts[] = "f";
6677 while ((rc = getopt_long(argc, argv, short_opts,
6678 long_opts, NULL)) != -1) {
6685 "%s changelog: unrecognized option '%s'\n",
6686 progname, argv[optind - 1]);
6690 if (optind >= argc) {
6691 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6696 mdd = argv[optind++];
6698 startrec = strtoll(argv[optind++], NULL, 10);
6700 endrec = strtoll(argv[optind++], NULL, 10);
6702 rc = llapi_changelog_start(&changelog_priv,
6703 CHANGELOG_FLAG_BLOCK |
6704 CHANGELOG_FLAG_JOBID |
6705 CHANGELOG_FLAG_EXTRA_FLAGS |
6706 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6709 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6710 progname, strerror(errno = -rc));
6714 rc = llapi_changelog_set_xflags(changelog_priv,
6715 CHANGELOG_EXTRA_FLAG_UIDGID |
6716 CHANGELOG_EXTRA_FLAG_NID |
6717 CHANGELOG_EXTRA_FLAG_OMODE |
6718 CHANGELOG_EXTRA_FLAG_XATTR);
6721 "%s changelog: cannot set xflags for changelog: %s\n",
6722 progname, strerror(errno = -rc));
6726 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6730 if (endrec && rec->cr_index > endrec) {
6731 llapi_changelog_free(&rec);
6734 if (rec->cr_index < startrec) {
6735 llapi_changelog_free(&rec);
6739 secs = rec->cr_time >> 30;
6740 gmtime_r(&secs, &ts);
6741 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6742 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6743 changelog_type2str(rec->cr_type),
6744 ts.tm_hour, ts.tm_min, ts.tm_sec,
6745 (int)(rec->cr_time & ((1 << 30) - 1)),
6746 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6747 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6749 if (rec->cr_flags & CLF_JOBID) {
6750 struct changelog_ext_jobid *jid =
6751 changelog_rec_jobid(rec);
6753 if (jid->cr_jobid[0] != '\0')
6754 printf(" j=%s", jid->cr_jobid);
6757 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6758 struct changelog_ext_extra_flags *ef =
6759 changelog_rec_extra_flags(rec);
6761 printf(" ef=0x%llx", ef->cr_extra_flags);
6763 if (ef->cr_extra_flags & CLFE_UIDGID) {
6764 struct changelog_ext_uidgid *uidgid =
6765 changelog_rec_uidgid(rec);
6767 printf(" u=%llu:%llu",
6768 uidgid->cr_uid, uidgid->cr_gid);
6770 if (ef->cr_extra_flags & CLFE_NID) {
6771 struct changelog_ext_nid *nid =
6772 changelog_rec_nid(rec);
6775 libcfs_nid2str(nid->cr_nid));
6778 if (ef->cr_extra_flags & CLFE_OPEN) {
6779 struct changelog_ext_openmode *omd =
6780 changelog_rec_openmode(rec);
6781 char mode[] = "---";
6783 /* exec mode must be exclusive */
6784 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6787 if (omd->cr_openflags & MDS_FMODE_READ)
6789 if (omd->cr_openflags &
6796 if (strcmp(mode, "---") != 0)
6797 printf(" m=%s", mode);
6801 if (ef->cr_extra_flags & CLFE_XATTR) {
6802 struct changelog_ext_xattr *xattr =
6803 changelog_rec_xattr(rec);
6805 if (xattr->cr_xattr[0] != '\0')
6806 printf(" x=%s", xattr->cr_xattr);
6810 if (rec->cr_namelen)
6811 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6812 rec->cr_namelen, changelog_rec_name(rec));
6814 if (rec->cr_flags & CLF_RENAME) {
6815 struct changelog_ext_rename *rnm =
6816 changelog_rec_rename(rec);
6818 if (!fid_is_zero(&rnm->cr_sfid))
6819 printf(" s="DFID" sp="DFID" %.*s",
6820 PFID(&rnm->cr_sfid),
6821 PFID(&rnm->cr_spfid),
6822 (int)changelog_rec_snamelen(rec),
6823 changelog_rec_sname(rec));
6827 llapi_changelog_free(&rec);
6830 llapi_changelog_fini(&changelog_priv);
6833 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6834 progname, strerror(errno = -rc));
6836 return (rc == 1 ? 0 : rc);
6839 static int lfs_changelog_clear(int argc, char **argv)
6847 endrec = strtoll(argv[3], NULL, 10);
6849 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6852 fprintf(stderr, "%s: record out of range: %llu\n",
6854 else if (rc == -ENOENT)
6855 fprintf(stderr, "%s: no changelog user: %s\n",
6858 fprintf(stderr, "%s error: %s\n", argv[0],
6867 static int lfs_fid2path(int argc, char **argv)
6869 struct option long_opts[] = {
6870 { .val = 'c', .name = "cur", .has_arg = no_argument },
6871 { .val = 'l', .name = "link", .has_arg = required_argument },
6872 { .val = 'r', .name = "rec", .has_arg = required_argument },
6874 char short_opts[] = "cl:r:";
6875 char *device, *fid, *path;
6876 long long recno = -1;
6881 char *endptr = NULL;
6883 while ((rc = getopt_long(argc, argv, short_opts,
6884 long_opts, NULL)) != -1) {
6890 linkno = strtol(optarg, &endptr, 10);
6891 if (*endptr != '\0') {
6893 "%s fid2path: invalid linkno '%s'\n",
6899 recno = strtoll(optarg, &endptr, 10);
6900 if (*endptr != '\0') {
6902 "%s fid2path: invalid recno '%s'\n",
6909 "%s fid2path: unrecognized option '%s'\n",
6910 progname, argv[optind - 1]);
6917 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6922 device = argv[optind++];
6923 path = calloc(1, PATH_MAX);
6927 "%s fid2path: cannot allocate memory for path: %s\n",
6928 progname, strerror(-rc));
6933 while (optind < argc) {
6934 fid = argv[optind++];
6936 lnktmp = (linkno >= 0) ? linkno : 0;
6938 int oldtmp = lnktmp;
6939 long long rectmp = recno;
6941 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6945 "%s fid2path: cannot find '%s': %s\n",
6946 progname, fid, strerror(errno = -rc2));
6953 fprintf(stdout, "%lld ", rectmp);
6954 if (device[0] == '/') {
6955 fprintf(stdout, "%s", device);
6956 if (device[strlen(device) - 1] != '/')
6957 fprintf(stdout, "/");
6958 } else if (path[0] == '\0') {
6959 fprintf(stdout, "/");
6961 fprintf(stdout, "%s\n", path);
6964 /* specified linkno */
6966 if (oldtmp == lnktmp)
6976 static int lfs_path2fid(int argc, char **argv)
6978 struct option long_opts[] = {
6979 { .val = 'p', .name = "parents", .has_arg = no_argument },
6982 const char short_opts[] = "p";
6983 const char *sep = "";
6986 bool show_parents = false;
6988 while ((rc = getopt_long(argc, argv, short_opts,
6989 long_opts, NULL)) != -1) {
6992 show_parents = true;
6996 "%s path2fid: unrecognized option '%s'\n",
6997 progname, argv[optind - 1]);
7002 if (optind > argc - 1) {
7003 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7007 else if (optind < argc - 1)
7011 for (path = argv + optind; *path != NULL; path++) {
7013 if (!show_parents) {
7014 err = llapi_path2fid(*path, &fid);
7016 printf("%s%s"DFID"\n",
7017 *sep != '\0' ? *path : "", sep,
7020 char name[NAME_MAX + 1];
7021 unsigned int linkno = 0;
7023 while ((err = llapi_path2parent(*path, linkno, &fid,
7024 name, sizeof(name))) == 0) {
7025 if (*sep != '\0' && linkno == 0)
7026 printf("%s%s", *path, sep);
7028 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7033 /* err == -ENODATA is end-of-loop */
7034 if (linkno > 0 && err == -ENODATA) {
7042 "%s path2fid: cannot get %sfid for '%s': %s\n",
7043 progname, show_parents ? "parent " : "", *path,
7055 static int lfs_data_version(int argc, char **argv)
7062 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7065 fprintf(stderr, "%s data_version: FILE must be specified\n",
7070 while ((c = getopt(argc, argv, "nrw")) != -1) {
7073 data_version_flags = 0;
7076 data_version_flags |= LL_DV_RD_FLUSH;
7079 data_version_flags |= LL_DV_WR_FLUSH;
7083 "%s data_version: unrecognized option '%s'\n",
7084 progname, argv[optind - 1]);
7088 if (optind == argc) {
7089 fprintf(stderr, "%s data_version: FILE must be specified\n",
7094 path = argv[optind];
7095 fd = open(path, O_RDONLY);
7098 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7099 progname, path, strerror(-rc));
7103 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7106 "%s data_version: cannot get version for '%s': %s\n",
7107 progname, path, strerror(-rc));
7109 printf("%ju" "\n", (uintmax_t)data_version);
7115 static int lfs_hsm_state(int argc, char **argv)
7120 struct hsm_user_state hus;
7128 rc = llapi_hsm_state_get(path, &hus);
7130 fprintf(stderr, "can't get hsm state for %s: %s\n",
7131 path, strerror(errno = -rc));
7135 /* Display path name and status flags */
7136 printf("%s: (0x%08x)", path, hus.hus_states);
7138 if (hus.hus_states & HS_RELEASED)
7139 printf(" released");
7140 if (hus.hus_states & HS_EXISTS)
7142 if (hus.hus_states & HS_DIRTY)
7144 if (hus.hus_states & HS_ARCHIVED)
7145 printf(" archived");
7146 /* Display user-settable flags */
7147 if (hus.hus_states & HS_NORELEASE)
7148 printf(" never_release");
7149 if (hus.hus_states & HS_NOARCHIVE)
7150 printf(" never_archive");
7151 if (hus.hus_states & HS_LOST)
7152 printf(" lost_from_hsm");
7154 if (hus.hus_archive_id != 0)
7155 printf(", archive_id:%d", hus.hus_archive_id);
7158 } while (++i < argc);
7163 #define LFS_HSM_SET 0
7164 #define LFS_HSM_CLEAR 1
7167 * Generic function to set or clear HSM flags.
7168 * Used by hsm_set and hsm_clear.
7170 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7172 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7174 struct option long_opts[] = {
7175 { .val = 'A', .name = "archived", .has_arg = no_argument },
7176 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7177 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7178 { .val = 'e', .name = "exists", .has_arg = no_argument },
7179 { .val = 'l', .name = "lost", .has_arg = no_argument },
7180 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7181 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7183 char short_opts[] = "lraAdei:";
7187 __u32 archive_id = 0;
7193 while ((c = getopt_long(argc, argv, short_opts,
7194 long_opts, NULL)) != -1) {
7200 mask |= HS_NOARCHIVE;
7203 mask |= HS_ARCHIVED;
7206 mask |= HS_NORELEASE;
7215 archive_id = strtol(optarg, &end, 10);
7217 fprintf(stderr, "invalid archive_id: '%s'\n",
7225 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7226 argv[0], argv[optind - 1]);
7231 /* User should have specified a flag */
7235 while (optind < argc) {
7237 path = argv[optind];
7239 /* If mode == 0, this means we apply the mask. */
7240 if (mode == LFS_HSM_SET)
7241 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7243 rc = llapi_hsm_state_set(path, 0, mask, 0);
7246 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7247 path, strerror(errno = -rc));
7256 static int lfs_hsm_action(int argc, char **argv)
7261 struct hsm_current_action hca;
7262 struct hsm_extent he;
7263 enum hsm_user_action hua;
7264 enum hsm_progress_states hps;
7272 rc = llapi_hsm_current_action(path, &hca);
7274 fprintf(stderr, "can't get hsm action for %s: %s\n",
7275 path, strerror(errno = -rc));
7278 he = hca.hca_location;
7279 hua = hca.hca_action;
7280 hps = hca.hca_state;
7282 printf("%s: %s", path, hsm_user_action2name(hua));
7284 /* Skip file without action */
7285 if (hca.hca_action == HUA_NONE) {
7290 printf(" %s ", hsm_progress_state2name(hps));
7292 if ((hps == HPS_RUNNING) &&
7293 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7294 printf("(%llu bytes moved)\n",
7295 (unsigned long long)he.length);
7296 else if ((he.offset + he.length) == LUSTRE_EOF)
7297 printf("(from %llu to EOF)\n",
7298 (unsigned long long)he.offset);
7300 printf("(from %llu to %llu)\n",
7301 (unsigned long long)he.offset,
7302 (unsigned long long)(he.offset + he.length));
7304 } while (++i < argc);
7309 static int lfs_hsm_set(int argc, char **argv)
7311 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7314 static int lfs_hsm_clear(int argc, char **argv)
7316 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7320 * Check file state and return its fid, to be used by lfs_hsm_request().
7322 * \param[in] file Path to file to check
7323 * \param[in,out] fid Pointer to allocated lu_fid struct.
7324 * \param[in,out] last_dev Pointer to last device id used.
7326 * \return 0 on success.
7328 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7334 rc = lstat(file, &st);
7336 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7339 /* Checking for regular file as archiving as posix copytool
7340 * rejects archiving files other than regular files
7342 if (!S_ISREG(st.st_mode)) {
7343 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7346 /* A request should be ... */
7347 if (*last_dev != st.st_dev && *last_dev != 0) {
7348 fprintf(stderr, "All files should be "
7349 "on the same filesystem: %s\n", file);
7352 *last_dev = st.st_dev;
7354 rc = llapi_path2fid(file, fid);
7356 fprintf(stderr, "Cannot read FID of %s: %s\n",
7357 file, strerror(-rc));
7363 /* Fill an HSM HUR item with a given file name.
7365 * If mntpath is set, then the filename is actually a FID, and no
7366 * lookup on the filesystem will be performed.
7368 * \param[in] hur the user request to fill
7369 * \param[in] idx index of the item inside the HUR to fill
7370 * \param[in] mntpath mountpoint of Lustre
7371 * \param[in] fname filename (if mtnpath is NULL)
7372 * or FID (if mntpath is set)
7373 * \param[in] last_dev pointer to last device id used
7375 * \retval 0 on success
7376 * \retval CMD_HELP or a negative errno on error
7378 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7379 const char *mntpath, const char *fname,
7382 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7385 hui->hui_extent.length = -1;
7387 if (mntpath != NULL) {
7390 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7394 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7399 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7403 hur->hur_request.hr_itemcount++;
7408 static int lfs_hsm_request(int argc, char **argv, int action)
7410 struct option long_opts[] = {
7411 { .val = 'a', .name = "archive", .has_arg = required_argument },
7412 { .val = 'D', .name = "data", .has_arg = required_argument },
7413 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7414 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7417 char short_opts[] = "l:D:a:m:";
7418 struct hsm_user_request *hur, *oldhur;
7423 char *filelist = NULL;
7424 char fullpath[PATH_MAX];
7425 char *opaque = NULL;
7429 int nbfile_alloc = 0;
7430 char *some_file = NULL;
7431 char *mntpath = NULL;
7437 while ((c = getopt_long(argc, argv, short_opts,
7438 long_opts, NULL)) != -1) {
7447 if (action != HUA_ARCHIVE &&
7448 action != HUA_REMOVE) {
7450 "error: -a is supported only "
7451 "when archiving or removing\n");
7454 archive_id = atoi(optarg);
7457 if (some_file == NULL) {
7459 some_file = strdup(optarg);
7465 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7466 argv[0], argv[optind - 1]);
7471 /* All remaining args are files, so we have at least nbfile */
7472 nbfile = argc - optind;
7474 if ((nbfile == 0) && (filelist == NULL))
7478 opaque_len = strlen(opaque);
7480 /* Alloc the request structure with enough place to store all files
7481 * from command line. */
7482 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7484 fprintf(stderr, "Cannot create the request: %s\n",
7488 nbfile_alloc = nbfile;
7490 hur->hur_request.hr_action = action;
7491 hur->hur_request.hr_archive_id = archive_id;
7492 hur->hur_request.hr_flags = 0;
7494 /* All remaining args are files, add them */
7495 if (nbfile != 0 && some_file == NULL)
7496 some_file = strdup(argv[optind]);
7498 for (i = 0; i < nbfile; i++) {
7499 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7505 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7507 /* If a filelist was specified, read the filelist from it. */
7508 if (filelist != NULL) {
7509 fp = fopen(filelist, "r");
7511 fprintf(stderr, "Cannot read the file list %s: %s\n",
7512 filelist, strerror(errno));
7517 while ((rc = getline(&line, &len, fp)) != -1) {
7518 /* If allocated buffer was too small, get something
7520 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7523 nbfile_alloc = nbfile_alloc * 2 + 1;
7525 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7528 fprintf(stderr, "hsm: cannot allocate "
7529 "the request: %s\n",
7536 size = hur_len(oldhur);
7538 fprintf(stderr, "hsm: cannot allocate "
7539 "%u files + %u bytes data\n",
7540 oldhur->hur_request.hr_itemcount,
7541 oldhur->hur_request.hr_data_len);
7548 memcpy(hur, oldhur, size);
7553 if (line[strlen(line) - 1] == '\n')
7554 line[strlen(line) - 1] = '\0';
7556 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7557 mntpath, line, &last_dev);
7563 if (some_file == NULL) {
7573 /* If a --data was used, add it to the request */
7574 hur->hur_request.hr_data_len = opaque_len;
7576 memcpy(hur_data(hur), opaque, opaque_len);
7578 /* Send the HSM request */
7579 if (realpath(some_file, fullpath) == NULL) {
7580 fprintf(stderr, "Could not find path '%s': %s\n",
7581 some_file, strerror(errno));
7583 rc = llapi_hsm_request(fullpath, hur);
7585 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7586 some_file, strerror(-rc));
7596 static int lfs_hsm_archive(int argc, char **argv)
7598 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7601 static int lfs_hsm_restore(int argc, char **argv)
7603 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7606 static int lfs_hsm_release(int argc, char **argv)
7608 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7611 static int lfs_hsm_remove(int argc, char **argv)
7613 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7616 static int lfs_hsm_cancel(int argc, char **argv)
7618 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7621 static int lfs_swap_layouts(int argc, char **argv)
7626 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7627 SWAP_LAYOUTS_KEEP_MTIME |
7628 SWAP_LAYOUTS_KEEP_ATIME);
7631 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7633 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7635 int lfs_get_mode(const char *string)
7637 enum lock_mode_user mode;
7639 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7640 if (lock_mode_names[mode] == NULL)
7642 if (strcmp(string, lock_mode_names[mode]) == 0)
7649 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7651 enum lu_ladvise_type advice;
7654 advice < ARRAY_SIZE(ladvise_names); advice++) {
7655 if (ladvise_names[advice] == NULL)
7657 if (strcmp(string, ladvise_names[advice]) == 0)
7661 return LU_LADVISE_INVALID;
7664 static int lfs_ladvise(int argc, char **argv)
7666 struct option long_opts[] = {
7667 { .val = 'a', .name = "advice", .has_arg = required_argument },
7668 { .val = 'b', .name = "background", .has_arg = no_argument },
7669 { .val = 'e', .name = "end", .has_arg = required_argument },
7670 { .val = 'l', .name = "length", .has_arg = required_argument },
7671 { .val = 'm', .name = "mode", .has_arg = required_argument },
7672 { .val = 's', .name = "start", .has_arg = required_argument },
7673 { .val = 'u', .name = "unset", .has_arg = no_argument },
7675 char short_opts[] = "a:be:l:m:s:u";
7680 struct llapi_lu_ladvise advice;
7681 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7682 unsigned long long start = 0;
7683 unsigned long long end = LUSTRE_EOF;
7684 unsigned long long length = 0;
7685 unsigned long long size_units;
7686 unsigned long long flags = 0;
7690 while ((c = getopt_long(argc, argv, short_opts,
7691 long_opts, NULL)) != -1) {
7694 advice_type = lfs_get_ladvice(optarg);
7695 if (advice_type == LU_LADVISE_INVALID) {
7696 fprintf(stderr, "%s: invalid advice type "
7697 "'%s'\n", argv[0], optarg);
7698 fprintf(stderr, "Valid types:");
7700 for (advice_type = 0;
7701 advice_type < ARRAY_SIZE(ladvise_names);
7703 if (ladvise_names[advice_type] == NULL)
7705 fprintf(stderr, " %s",
7706 ladvise_names[advice_type]);
7708 fprintf(stderr, "\n");
7721 rc = llapi_parse_size(optarg, &end,
7724 fprintf(stderr, "%s: bad end offset '%s'\n",
7731 rc = llapi_parse_size(optarg, &start,
7734 fprintf(stderr, "%s: bad start offset "
7735 "'%s'\n", argv[0], optarg);
7741 rc = llapi_parse_size(optarg, &length,
7744 fprintf(stderr, "%s: bad length '%s'\n",
7750 mode = lfs_get_mode(optarg);
7752 fprintf(stderr, "%s: bad mode '%s', valid "
7753 "modes are READ or WRITE\n",
7761 fprintf(stderr, "%s: option '%s' unrecognized\n",
7762 argv[0], argv[optind - 1]);
7767 if (advice_type == LU_LADVISE_INVALID) {
7768 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7769 fprintf(stderr, "Valid types:");
7770 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7772 if (ladvise_names[advice_type] == NULL)
7774 fprintf(stderr, " %s", ladvise_names[advice_type]);
7776 fprintf(stderr, "\n");
7780 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7781 fprintf(stderr, "%s: Lock no expand advice is a per file "
7782 "descriptor advice, so when called from lfs, "
7783 "it does nothing.\n", argv[0]);
7787 if (argc <= optind) {
7788 fprintf(stderr, "%s: please give one or more file names\n",
7793 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7794 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7799 if (end == LUSTRE_EOF && length != 0)
7800 end = start + length;
7803 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7804 argv[0], start, end);
7808 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7809 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7814 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7815 fprintf(stderr, "%s: mode is required with lockahead\n",
7820 while (optind < argc) {
7823 path = argv[optind++];
7825 fd = open(path, O_RDONLY);
7827 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7828 argv[0], path, strerror(errno));
7833 advice.lla_start = start;
7834 advice.lla_end = end;
7835 advice.lla_advice = advice_type;
7836 advice.lla_value1 = 0;
7837 advice.lla_value2 = 0;
7838 advice.lla_value3 = 0;
7839 advice.lla_value4 = 0;
7840 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7841 advice.lla_lockahead_mode = mode;
7842 advice.lla_peradvice_flags = flags;
7845 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7848 fprintf(stderr, "%s: cannot give advice '%s' to file "
7849 "'%s': %s\n", argv[0],
7850 ladvise_names[advice_type],
7851 path, strerror(errno));
7857 if (rc == 0 && rc2 < 0)
7863 /** The input string contains a comma delimited list of component ids and
7864 * ranges, for example "1,2-4,7".
7866 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7868 bool end_of_loop = false;
7876 while (!end_of_loop) {
7880 char *endptr = NULL;
7883 ptr = strchrnul(arg, ',');
7884 end_of_loop = *ptr == '\0';
7887 start_index = strtol(arg, &endptr, 0);
7888 if (endptr == arg) /* no data at all */
7890 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7892 if (start_index < 0)
7895 end_index = start_index;
7896 if (*endptr == '-') {
7897 end_index = strtol(endptr + 1, &endptr, 0);
7898 if (*endptr != '\0')
7900 if (end_index < start_index)
7904 for (i = start_index; i <= end_index && size > 0; i++) {
7907 /* remove duplicate */
7908 for (j = 0; j < nr; j++) {
7912 if (j == nr) { /* no duplicate */
7918 if (size == 0 && i < end_index)
7925 if (!end_of_loop && ptr != NULL)
7928 return rc < 0 ? rc : nr;
7932 * struct verify_mirror_id - Mirror id to be verified.
7933 * @mirror_id: A specified mirror id.
7934 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7936 struct verify_mirror_id {
7942 * compare_mirror_ids() - Compare mirror ids.
7943 * @layout: Mirror component list.
7944 * @cbdata: Callback data in verify_mirror_id structure.
7946 * This is a callback function called by llapi_layout_comp_iterate()
7947 * to compare the specified mirror id with the one in the current
7948 * component of @layout. If they are the same, then the specified
7949 * mirror id is valid.
7951 * Return: a negative error code on failure or
7952 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7953 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7956 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7958 struct verify_mirror_id *mirror_id_cbdata =
7959 (struct verify_mirror_id *)cbdata;
7963 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7967 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7968 progname, strerror(errno));
7972 if (mirror_id_cbdata->mirror_id == mirror_id) {
7973 mirror_id_cbdata->is_valid_id = true;
7974 return LLAPI_LAYOUT_ITER_STOP;
7977 return LLAPI_LAYOUT_ITER_CONT;
7981 * verify_mirror_ids() - Verify specified mirror ids.
7982 * @fname: Mirrored file name.
7983 * @mirror_ids: Specified mirror ids to be verified.
7984 * @ids_nr: Number of specified mirror ids.
7986 * This function verifies that specified @mirror_ids are valid
7987 * in the mirrored file @fname.
7989 * Return: 0 on success or a negative error code on failure.
7992 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7994 struct llapi_layout *layout = NULL;
7995 struct verify_mirror_id mirror_id_cbdata = { 0 };
8006 if (stat(fname, &stbuf) < 0) {
8007 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8008 progname, fname, strerror(errno));
8013 if (!S_ISREG(stbuf.st_mode)) {
8014 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8020 fd = open(fname, O_DIRECT | O_RDONLY);
8022 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8023 progname, fname, strerror(errno));
8028 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8030 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8031 progname, fname, strerror(errno));
8035 layout = llapi_layout_get_by_fd(fd, 0);
8036 if (layout == NULL) {
8037 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8038 progname, fname, strerror(errno));
8040 llapi_lease_release(fd);
8044 rc = llapi_layout_flags_get(layout, &flr_state);
8046 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8047 progname, fname, strerror(errno));
8052 flr_state &= LCM_FL_FLR_MASK;
8053 switch (flr_state) {
8056 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8057 progname, fname, llapi_layout_flags_string(flr_state));
8064 for (i = 0; i < ids_nr; i++) {
8065 mirror_id_cbdata.mirror_id = mirror_ids[i];
8066 mirror_id_cbdata.is_valid_id = false;
8068 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8073 "%s: '%s' failed to verify mirror id: %u.\n",
8074 progname, fname, mirror_ids[i]);
8078 if (!mirror_id_cbdata.is_valid_id) {
8081 "%s: '%s' invalid specified mirror id: %u.\n",
8082 progname, fname, mirror_ids[i]);
8088 llapi_layout_free(layout);
8089 llapi_lease_release(fd);
8097 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8098 __u16 *mirror_ids, int ids_nr)
8100 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8101 struct llapi_layout *layout;
8111 if (stat(fname, &stbuf) < 0) {
8112 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8113 progname, fname, strerror(errno));
8117 if (!S_ISREG(stbuf.st_mode)) {
8118 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8124 fd = open(fname, O_DIRECT | O_RDWR);
8126 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8127 progname, fname, strerror(errno));
8132 layout = llapi_layout_get_by_fd(fd, 0);
8133 if (layout == NULL) {
8134 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8135 progname, fname, strerror(errno));
8140 rc = llapi_layout_flags_get(layout, &flr_state);
8142 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8143 progname, fname, strerror(errno));
8148 flr_state &= LCM_FL_FLR_MASK;
8149 if (flr_state == LCM_FL_NONE) {
8151 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8156 /* get stale component info */
8157 comp_size = llapi_mirror_find_stale(layout, comp_array,
8158 ARRAY_SIZE(comp_array),
8159 mirror_ids, ids_nr);
8160 if (comp_size <= 0) {
8165 ioc->lil_mode = LL_LEASE_WRLCK;
8166 ioc->lil_flags = LL_LEASE_RESYNC;
8167 rc = llapi_lease_set(fd, ioc);
8169 if (rc == -EALREADY)
8173 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8174 progname, fname, strerror(errno));
8178 /* get the read range [start, end) */
8179 start = comp_array[0].lrc_start;
8180 end = comp_array[0].lrc_end;
8181 for (idx = 1; idx < comp_size; idx++) {
8182 if (comp_array[idx].lrc_start < start)
8183 start = comp_array[idx].lrc_start;
8184 if (end < comp_array[idx].lrc_end)
8185 end = comp_array[idx].lrc_end;
8188 rc = llapi_lease_check(fd);
8189 if (rc != LL_LEASE_WRLCK) {
8190 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8195 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8198 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8199 progname, fname, rc);
8201 /* prepare ioc for lease put */
8202 ioc->lil_mode = LL_LEASE_UNLCK;
8203 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8205 for (idx = 0; idx < comp_size; idx++) {
8206 if (comp_array[idx].lrc_synced) {
8207 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8212 rc = llapi_lease_set(fd, ioc);
8214 if (rc == 0) /* lost lease lock */
8216 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8217 progname, fname, strerror(errno));
8221 * llapi_lease_set returns lease mode when it request to unlock
8227 llapi_layout_free(layout);
8234 static inline int lfs_mirror_resync(int argc, char **argv)
8236 struct ll_ioc_lease *ioc = NULL;
8237 __u16 mirror_ids[128] = { 0 };
8242 struct option long_opts[] = {
8243 { .val = 'o', .name = "only", .has_arg = required_argument },
8246 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8249 rc = parse_mirror_ids(mirror_ids,
8250 sizeof(mirror_ids) / sizeof(__u16),
8254 "%s: bad mirror ids '%s'.\n",
8261 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8262 argv[0], argv[optind - 1]);
8268 if (argc == optind) {
8269 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8274 if (ids_nr > 0 && argc > optind + 1) {
8276 "%s: option '--only' cannot be used upon multiple files.\n",
8284 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8289 /* set the lease on the file */
8290 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8292 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8293 argv[0], strerror(errno));
8298 for (; optind < argc; optind++) {
8299 rc = lfs_mirror_resync_file(argv[optind], ioc,
8300 mirror_ids, ids_nr);
8301 /* ignore previous file's error, continue with next file */
8304 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8312 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8314 struct llapi_layout *layout;
8317 layout = llapi_layout_get_by_fd(fd, 0);
8318 if (layout == NULL) {
8319 fprintf(stderr, "could not get layout.\n");
8323 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8325 fprintf(stderr, "failed to iterate layout\n");
8326 llapi_layout_free(layout);
8329 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8330 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8331 llapi_layout_free(layout);
8335 llapi_layout_free(layout);
8341 * Check whether two files are the same file
8342 * \retval 0 same file
8343 * \retval 1 not the same file
8344 * \retval <0 error code
8346 static inline int check_same_file(const char *f1, const char *f2)
8351 if (stat(f1, &stbuf1) < 0) {
8352 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8353 progname, f1, strerror(errno));
8357 if (stat(f2, &stbuf2) < 0) {
8358 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8359 progname, f2, strerror(errno));
8363 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8364 stbuf1.st_ino == stbuf2.st_ino)
8370 static inline int lfs_mirror_read(int argc, char **argv)
8373 __u16 mirror_id = 0;
8374 const char *outfile = NULL;
8380 const size_t buflen = 4 << 20;
8382 struct option long_opts[] = {
8383 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8384 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8387 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8392 mirror_id = strtoul(optarg, &end, 0);
8393 if (*end != '\0' || mirror_id == 0) {
8395 "%s %s: invalid mirror ID '%s'\n",
8396 progname, argv[0], optarg);
8404 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8405 progname, argv[optind - 1]);
8410 if (argc == optind) {
8411 fprintf(stderr, "%s %s: no mirrored file provided\n",
8414 } else if (argc > optind + 1) {
8415 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8419 if (mirror_id == 0) {
8420 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8425 /* open mirror file */
8426 fname = argv[optind];
8429 rc = check_same_file(fname, outfile);
8432 "%s %s: output file cannot be the mirrored file\n",
8440 fd = open(fname, O_DIRECT | O_RDONLY);
8442 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8443 progname, argv[0], fname, strerror(errno));
8447 /* verify mirror id */
8448 rc = verify_mirror_id_by_fd(fd, mirror_id);
8451 "%s %s: cannot find mirror with ID %u in '%s'\n",
8452 progname, argv[0], mirror_id, fname);
8456 /* open output file */
8458 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8460 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8461 progname, argv[0], outfile, strerror(errno));
8466 outfd = STDOUT_FILENO;
8469 /* allocate buffer */
8470 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8472 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8473 progname, argv[0], rc);
8480 ssize_t written = 0;
8482 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8483 if (bytes_read < 0) {
8486 "%s %s: fail to read data from mirror %u: %s\n",
8487 progname, argv[0], mirror_id, strerror(-rc));
8492 if (bytes_read == 0)
8495 while (written < bytes_read) {
8498 written2 = write(outfd, buf + written,
8499 bytes_read - written);
8502 "%s %s: fail to write %s: %s\n",
8503 progname, argv[0], outfile ? : "STDOUT",
8508 written += written2;
8511 if (written != bytes_read) {
8513 "%s %s: written %ld bytes does not match with %ld read.\n",
8514 progname, argv[0], written, bytes_read);
8536 static inline int lfs_mirror_write(int argc, char **argv)
8539 __u16 mirror_id = 0;
8540 const char *inputfile = NULL;
8546 const size_t buflen = 4 << 20;
8548 size_t page_size = sysconf(_SC_PAGESIZE);
8549 struct ll_ioc_lease_id ioc;
8551 struct option long_opts[] = {
8552 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8553 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8556 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8561 mirror_id = strtoul(optarg, &end, 0);
8562 if (*end != '\0' || mirror_id == 0) {
8564 "%s %s: invalid mirror ID '%s'\n",
8565 progname, argv[0], optarg);
8573 fprintf(stderr, "%s: option '%s' unrecognized\n",
8574 progname, argv[optind - 1]);
8579 if (argc == optind) {
8580 fprintf(stderr, "%s %s: no mirrored file provided\n",
8583 } else if (argc > optind + 1) {
8584 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8588 if (mirror_id == 0) {
8589 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8594 /* open mirror file */
8595 fname = argv[optind];
8598 rc = check_same_file(fname, inputfile);
8601 "%s %s: input file cannot be the mirrored file\n",
8609 fd = open(fname, O_DIRECT | O_WRONLY);
8611 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8612 progname, argv[0], fname, strerror(errno));
8616 /* verify mirror id */
8617 rc = verify_mirror_id_by_fd(fd, mirror_id);
8620 "%s %s: cannot find mirror with ID %u in '%s'\n",
8621 progname, argv[0], mirror_id, fname);
8625 /* open input file */
8627 inputfd = open(inputfile, O_RDONLY, 0644);
8629 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
8630 progname, argv[0], inputfile, strerror(errno));
8635 inputfd = STDIN_FILENO;
8638 /* allocate buffer */
8639 rc = posix_memalign(&buf, page_size, buflen);
8641 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8642 progname, argv[0], rc);
8646 /* prepare target mirror components instantiation */
8647 ioc.lil_mode = LL_LEASE_WRLCK;
8648 ioc.lil_flags = LL_LEASE_RESYNC;
8649 ioc.lil_mirror_id = mirror_id;
8650 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8653 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8654 progname, argv[0], fname, strerror(errno));
8664 rc = llapi_lease_check(fd);
8665 if (rc != LL_LEASE_WRLCK) {
8666 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
8667 progname, argv[0], fname);
8671 bytes_read = read(inputfd, buf, buflen);
8672 if (bytes_read < 0) {
8675 "%s %s: fail to read data from '%s': %s\n",
8676 progname, argv[0], inputfile ? : "STDIN",
8683 if (bytes_read == 0)
8686 /* round up to page align to make direct IO happy. */
8687 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
8689 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
8694 "%s %s: fail to write to mirror %u: %s\n",
8695 progname, argv[0], mirror_id,
8703 if (pos & (page_size - 1)) {
8704 rc = llapi_mirror_truncate(fd, mirror_id, pos);
8709 ioc.lil_mode = LL_LEASE_UNLCK;
8710 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
8712 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8717 "%s %s: release lease lock of '%s' failed: %s\n",
8718 progname, argv[0], fname, strerror(errno));
8736 * struct verify_chunk - Mirror chunk to be verified.
8737 * @chunk: [start, end) of the chunk.
8738 * @mirror_count: Number of mirror ids in @mirror_id array.
8739 * @mirror_id: Array of valid mirror ids that cover the chunk.
8741 struct verify_chunk {
8742 struct lu_extent chunk;
8743 unsigned int mirror_count;
8744 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
8748 * print_chunks() - Print chunk information.
8749 * @fname: Mirrored file name.
8750 * @chunks: Array of chunks.
8751 * @chunk_count: Number of chunks in @chunks array.
8753 * This function prints [start, end) of each chunk in @chunks
8754 * for mirrored file @fname, and also prints the valid mirror ids
8755 * that cover the chunk.
8760 void print_chunks(const char *fname, struct verify_chunk *chunks,
8766 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
8767 for (i = 0; i < chunk_count; i++) {
8768 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
8770 if (chunks[i].mirror_count == 0)
8771 fprintf(stdout, "\t[");
8773 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
8774 for (j = 1; j < chunks[i].mirror_count; j++)
8775 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
8777 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
8779 fprintf(stdout, "\n");
8783 * print_checksums() - Print CRC-32 checksum values.
8784 * @chunk: A chunk and its corresponding valid mirror ids.
8785 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
8787 * This function prints CRC-32 checksum values on @chunk for
8788 * each valid mirror that covers it.
8793 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
8798 "CRC-32 checksum value for chunk "DEXT":\n",
8799 PEXT(&chunk->chunk));
8800 for (i = 0; i < chunk->mirror_count; i++)
8801 fprintf(stdout, "Mirror %u:\t%#lx\n",
8802 chunk->mirror_id[i], crc[i]);
8803 fprintf(stdout, "\n");
8807 * filter_mirror_id() - Filter specified mirror ids.
8808 * @chunks: Array of chunks.
8809 * @chunk_count: Number of chunks in @chunks array.
8810 * @mirror_ids: Specified mirror ids to be verified.
8811 * @ids_nr: Number of specified mirror ids.
8813 * This function scans valid mirror ids that cover each chunk in @chunks
8814 * and filters specified mirror ids.
8819 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
8820 __u16 *mirror_ids, int ids_nr)
8825 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8826 unsigned int valid_count = 0;
8828 for (i = 0; i < chunk_count; i++) {
8829 if (chunks[i].mirror_count == 0)
8833 for (j = 0; j < ids_nr; j++) {
8834 for (k = 0; k < chunks[i].mirror_count; k++) {
8835 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
8836 valid_id[valid_count] = mirror_ids[j];
8843 memcpy(chunks[i].mirror_id, valid_id,
8844 sizeof(__u16) * valid_count);
8845 chunks[i].mirror_count = valid_count;
8850 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
8851 * @layout: Mirror component list.
8852 * @chunks: Array of chunks.
8853 * @chunks_size: Array size of @chunks.
8855 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
8856 * to find out chunk segments and store them in @chunks array.
8858 * The @mirror_id array in each element of @chunks will store the valid
8859 * mirror ids that cover the chunk. If a mirror component covering the
8860 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
8861 * will not be stored into the @mirror_id array, and the chunk for that
8862 * mirror will not be verified.
8864 * The @mirror_count in each element of @chunks will store the number of
8865 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
8866 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
8867 * indicates the chunk is valid in only one mirror. In both cases, the
8868 * chunk will not be verified.
8870 * Here is an example:
8873 * +------+-------------+--------------------+
8875 * +------+------+------+------+-------------+
8876 * | | S | S | | mirror2
8877 * +-------------+------+------+-------------+
8879 * prepared @chunks array will contain 5 elements:
8880 * (([0, 1M), [1, 2], 2),
8881 * ([1M, 2M), [1, 2], 2),
8882 * ([2M, 3M), [1], 1),
8883 * ([3M, 4M], [], 0),
8884 * ([4M, EOF), [2], 1))
8886 * Return: the actual array size of @chunks on success
8887 * or a negative error code on failure.
8890 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
8891 struct verify_chunk *chunks,
8902 memset(chunks, 0, sizeof(*chunks) * chunks_size);
8905 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
8908 "%s: move to the first layout component: %s.\n",
8909 progname, strerror(errno));
8915 chunks[idx].chunk.e_end = LUSTRE_EOF;
8917 rc = llapi_layout_comp_extent_get(layout, &start, &end);
8920 "%s: llapi_layout_comp_extent_get failed: %s.\n",
8921 progname, strerror(errno));
8925 if (start > chunks[idx].chunk.e_start ||
8926 end <= chunks[idx].chunk.e_start)
8929 if (end < chunks[idx].chunk.e_end)
8930 chunks[idx].chunk.e_end = end;
8932 rc = llapi_layout_comp_flags_get(layout, &flags);
8935 "%s: llapi_layout_comp_flags_get failed: %s.\n",
8936 progname, strerror(errno));
8940 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8943 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8946 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8947 progname, strerror(errno));
8951 chunks[idx].mirror_id[i] = mirror_id;
8953 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8955 "%s: mirror_id array is too small.\n",
8962 rc = llapi_layout_comp_use(layout,
8963 LLAPI_LAYOUT_COMP_USE_NEXT);
8966 "%s: move to the next layout component: %s.\n",
8967 progname, strerror(errno));
8970 } /* loop through all components */
8972 chunks[idx].mirror_count = i;
8974 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8978 if (idx >= chunks_size) {
8979 fprintf(stderr, "%s: chunks array is too small.\n",
8985 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
8989 return rc < 0 ? rc : idx + 1;
8993 * lfs_mirror_verify_chunk() - Verify a chunk.
8994 * @fd: File descriptor of the mirrored file.
8995 * @file_size: Size of the mirrored file.
8996 * @chunk: A chunk and its corresponding valid mirror ids.
8997 * @verbose: Verbose mode.
8999 * This function verifies a @chunk contains exactly the same data
9000 * ammong the mirrors that cover it.
9002 * If @verbose is specified, then the function will print where the
9003 * differences are if the data do not match. Otherwise, it will
9004 * just return an error in that case.
9006 * Return: 0 on success or a negative error code on failure.
9009 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9010 struct verify_chunk *chunk, int verbose)
9012 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9014 size_t page_size = sysconf(_SC_PAGESIZE);
9020 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9027 rc = posix_memalign(&buf, page_size, buflen);
9028 if (rc) /* error code is returned directly */
9032 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9033 PEXT(&chunk->chunk));
9034 for (i = 0; i < chunk->mirror_count; i++)
9035 fprintf(stdout, " %u", chunk->mirror_id[i]);
9036 fprintf(stdout, "\n");
9040 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9041 pos = chunk->chunk.e_start;
9042 while (bytes_done < count) {
9043 /* compute initial CRC-32 checksum */
9044 crc = crc32(0L, Z_NULL, 0);
9045 memset(crc_array, 0, sizeof(crc_array));
9048 for (i = 0; i < chunk->mirror_count; i++) {
9049 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9051 if (bytes_read < 0) {
9054 "%s: failed to read data from mirror %u: %s.\n",
9055 progname, chunk->mirror_id[i],
9060 /* compute new CRC-32 checksum */
9061 crc_array[i] = crc32(crc, buf, bytes_read);
9065 print_checksums(chunk, crc_array);
9067 /* compare CRC-32 checksum values */
9068 for (i = 1; i < chunk->mirror_count; i++) {
9069 if (crc_array[i] != crc_array[0]) {
9075 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9076 progname, PEXT(&chunk->chunk),
9077 chunk->mirror_id[0],
9078 chunk->mirror_id[i]);
9083 bytes_done += bytes_read;
9086 if (verbose > 1 && rc == 0) {
9087 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9088 PEXT(&chunk->chunk));
9089 for (i = 0; i < chunk->mirror_count; i++)
9090 fprintf(stdout, " %u", chunk->mirror_id[i]);
9091 fprintf(stdout, " PASS\n\n");
9100 * lfs_mirror_verify_file() - Verify a mirrored file.
9101 * @fname: Mirrored file name.
9102 * @mirror_ids: Specified mirror ids to be verified.
9103 * @ids_nr: Number of specified mirror ids.
9104 * @verbose: Verbose mode.
9106 * This function verifies that each SYNC mirror of a mirrored file
9107 * specified by @fname contains exactly the same data.
9109 * If @mirror_ids is specified, then the function will verify the
9110 * mirrors specified by @mirror_ids contain exactly the same data.
9112 * If @verbose is specified, then the function will print where the
9113 * differences are if the data do not match. Otherwise, it will
9114 * just return an error in that case.
9116 * Return: 0 on success or a negative error code on failure.
9119 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9122 struct verify_chunk chunks_array[1024] = { };
9123 struct llapi_layout *layout = NULL;
9127 int chunk_count = 0;
9133 if (stat(fname, &stbuf) < 0) {
9134 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9135 progname, fname, strerror(errno));
9140 if (!S_ISREG(stbuf.st_mode)) {
9141 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9147 if (stbuf.st_size == 0) {
9149 fprintf(stdout, "%s: '%s' file size is 0.\n",
9155 fd = open(fname, O_DIRECT | O_RDONLY);
9157 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9158 progname, fname, strerror(errno));
9163 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9165 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9166 progname, fname, strerror(errno));
9170 layout = llapi_layout_get_by_fd(fd, 0);
9171 if (layout == NULL) {
9172 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9173 progname, fname, strerror(errno));
9175 llapi_lease_release(fd);
9179 rc = llapi_layout_flags_get(layout, &flr_state);
9181 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9182 progname, fname, strerror(errno));
9187 flr_state &= LCM_FL_FLR_MASK;
9188 switch (flr_state) {
9191 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9192 progname, fname, llapi_layout_flags_string(flr_state));
9198 /* find out mirror chunks to be verified */
9199 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9200 ARRAY_SIZE(chunks_array));
9201 if (chunk_count < 0) {
9207 /* filter specified mirror ids */
9208 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9211 print_chunks(fname, chunks_array, chunk_count);
9213 for (idx = 0; idx < chunk_count; idx++) {
9214 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9217 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9219 PEXT(&chunks_array[idx].chunk),
9220 (unsigned long long)stbuf.st_size);
9224 if (chunks_array[idx].mirror_count == 0) {
9226 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9228 PEXT(&chunks_array[idx].chunk));
9230 fprintf(stderr, "skipped\n");
9234 fprintf(stderr, "failed\n");
9238 if (chunks_array[idx].mirror_count == 1) {
9241 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9243 PEXT(&chunks_array[idx].chunk),
9244 chunks_array[idx].mirror_id[0]);
9248 rc = llapi_lease_check(fd);
9249 if (rc != LL_LEASE_RDLCK) {
9250 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9255 /* verify one chunk */
9256 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9257 &chunks_array[idx], verbose);
9271 llapi_layout_free(layout);
9272 llapi_lease_release(fd);
9280 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9281 * @argc: The count of lfs mirror verify command line arguments.
9282 * @argv: Array of strings for lfs mirror verify command line arguments.
9284 * This function parses lfs mirror verify command and verifies the
9285 * specified mirrored file(s).
9287 * Return: 0 on success or a negative error code on failure.
9289 static inline int lfs_mirror_verify(int argc, char **argv)
9291 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9299 struct option long_opts[] = {
9300 { .val = 'o', .name = "only", .has_arg = required_argument },
9301 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9304 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9306 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9309 rc = parse_mirror_ids(mirror_ids,
9310 ARRAY_SIZE(mirror_ids),
9314 "%s: bad mirror ids '%s'.\n",
9321 "%s: at least 2 mirror ids needed with '--only' option.\n",
9331 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9332 progname, argv[optind - 1]);
9338 if (argc == optind) {
9339 fprintf(stderr, "%s: no file name given.\n", progname);
9344 if (ids_nr > 0 && argc > optind + 1) {
9346 "%s: '--only' cannot be used upon multiple files.\n",
9354 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9360 for (; optind < argc; optind++) {
9361 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9371 * lfs_mirror() - Parse and execute lfs mirror commands.
9372 * @argc: The count of lfs mirror command line arguments.
9373 * @argv: Array of strings for lfs mirror command line arguments.
9375 * This function parses lfs mirror commands and performs the
9376 * corresponding functions specified in mirror_cmdlist[].
9378 * Return: 0 on success or an error code on failure.
9380 static int lfs_mirror(int argc, char **argv)
9387 Parser_init("lfs-mirror > ", mirror_cmdlist);
9389 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9391 program_invocation_short_name = cmd;
9393 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9395 rc = Parser_commands();
9397 return rc < 0 ? -rc : rc;
9400 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9402 #if __BYTE_ORDER == __BIG_ENDIAN
9403 __swab16s(&attrs->lsa_valid);
9404 __swab64s(&attrs->lsa_size);
9405 __swab64s(&attrs->lsa_blocks);
9411 LFS_SOM_BLOCKS = 0x2,
9412 LFS_SOM_FLAGS = 0x4,
9413 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9417 static int lfs_getsom(int argc, char **argv)
9420 struct lustre_som_attrs *attrs;
9421 char buf[sizeof(*attrs) + 64];
9422 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9425 while ((c = getopt(argc, argv, "sbf")) != -1) {
9428 type = LFS_SOM_SIZE;
9431 type = LFS_SOM_BLOCKS;
9434 type = LFS_SOM_FLAGS;
9437 fprintf(stderr, "%s: invalid option '%c'\n",
9447 fprintf(stderr, "%s: %s\n",
9448 progname, argc == 0 ? "miss file target" :
9449 "input more than 2 files");
9454 attrs = (void *)buf;
9455 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9458 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
9459 argv[0], strerror(errno), errno);
9463 lustre_som_swab(attrs);
9466 case LFS_SOM_ATTR_ALL:
9467 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9468 path, attrs->lsa_size, attrs->lsa_blocks,
9472 printf("%llu\n", attrs->lsa_size);
9474 case LFS_SOM_BLOCKS:
9475 printf("%llu\n", attrs->lsa_blocks);
9478 printf("%x\n", attrs->lsa_valid);
9481 fprintf(stderr, "%s: unknown option\n", progname);
9489 * lfs_mirror_list_commands() - List lfs mirror commands.
9490 * @argc: The count of command line arguments.
9491 * @argv: Array of strings for command line arguments.
9493 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9495 * Return: 0 on success.
9497 static int lfs_mirror_list_commands(int argc, char **argv)
9499 char buffer[81] = "";
9501 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9507 static int lfs_list_commands(int argc, char **argv)
9509 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9511 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9516 int main(int argc, char **argv)
9520 /* Ensure that liblustreapi constructor has run */
9521 if (!llapi_liblustreapi_initialized())
9522 fprintf(stderr, "liblustreapi was not properly initialized\n");
9527 Parser_init("lfs > ", cmdlist);
9529 progname = program_invocation_short_name; /* Used in error messages */
9531 llapi_set_command_name(argv[1]);
9532 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9533 llapi_clear_command_name();
9535 rc = Parser_commands();
9538 return rc < 0 ? -rc : rc;
9541 #ifdef _LUSTRE_IDL_H_
9542 /* Everything we need here should be included by lustreapi.h. */
9543 # error "lfs should not depend on lustre_idl.h"
9544 #endif /* _LUSTRE_IDL_H_ */