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 ") \
237 " [--block|-b] [--non-block|-n]\n" \
238 " [--non-direct|-D] [--verbose|-v]\n" \
242 "\tblock: Block file access during data migration (default)\n" \
243 "\tnon-block: Abort migrations if concurrent access is detected\n" \
244 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
245 "\tverbose: Print each filename as it is migrated\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 = 0x0001,
629 MIGRATION_MIRROR = 0x0002,
630 MIGRATION_NONDIRECT = 0x0004,
631 MIGRATION_VERBOSE = 0x0008,
634 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
635 struct llapi_layout *layout);
638 migrate_open_files(const char *name, __u64 migration_flags,
639 const struct llapi_stripe_param *param,
640 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
647 char parent[PATH_MAX];
648 char volatile_file[PATH_MAX];
654 if (param == NULL && layout == NULL) {
655 error_loc = "layout information";
659 /* search for file directory pathname */
660 if (strlen(name) > sizeof(parent) - 1) {
661 error_loc = "source file name";
665 strncpy(parent, name, sizeof(parent));
666 ptr = strrchr(parent, '/');
668 if (getcwd(parent, sizeof(parent)) == NULL) {
669 error_loc = "getcwd";
673 if (ptr == parent) /* leading '/' */
678 /* open file, direct io */
679 /* even if the file is only read, WR mode is nedeed to allow
680 * layout swap on fd */
682 if (!(migration_flags & MIGRATION_NONDIRECT))
684 fd = open(name, rflags);
687 error_loc = "cannot open source file";
691 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
693 error_loc = "cannot get MDT index";
698 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
699 mode_t open_mode = S_IRUSR | S_IWUSR;
701 random_value = random();
702 rc = snprintf(volatile_file, sizeof(volatile_file),
703 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
704 mdt_index, random_value);
705 if (rc >= sizeof(volatile_file)) {
710 /* create, open a volatile file, use caching (ie no directio) */
712 fdv = llapi_file_open_param(volatile_file, open_flags,
715 fdv = lfs_component_create(volatile_file, open_flags,
717 } while (fdv < 0 && (rc = fdv) == -EEXIST);
720 error_loc = "cannot create volatile file";
724 /* In case the MDT does not support creation of volatile files
725 * we should try to unlink it. */
726 (void)unlink(volatile_file);
728 /* Not-owner (root?) special case.
729 * Need to set owner/group of volatile file like original.
730 * This will allow to pass related check during layout_swap.
735 error_loc = "cannot stat source file";
739 rc = fstat(fdv, &stv);
742 error_loc = "cannot stat volatile";
746 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
747 rc = fchown(fdv, st.st_uid, st.st_gid);
750 error_loc = "cannot change ownwership of volatile";
769 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
771 struct llapi_layout *layout;
772 size_t buf_size = 4 * 1024 * 1024;
781 layout = llapi_layout_get_by_fd(fd_src, 0);
782 if (layout != NULL) {
783 uint64_t stripe_size;
785 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
787 buf_size = stripe_size;
789 llapi_layout_free(layout);
792 /* Use a page-aligned buffer for direct I/O */
793 rc = posix_memalign(&buf, getpagesize(), buf_size);
798 /* read new data only if we have written all
799 * previously read data */
802 rc = check_file(fd_src);
807 rsize = read(fd_src, buf, buf_size);
819 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
838 static int migrate_copy_timestamps(int fd, int fdv)
842 if (fstat(fd, &st) == 0) {
843 struct timeval tv[2] = {
844 {.tv_sec = st.st_atime},
845 {.tv_sec = st.st_mtime}
848 return futimes(fdv, tv);
854 static int migrate_block(int fd, int fdv)
861 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
863 error_loc = "cannot get dataversion";
871 /* The grouplock blocks all concurrent accesses to the file.
872 * It has to be taken after llapi_get_data_version as it would
874 rc = llapi_group_lock(fd, gid);
876 error_loc = "cannot get group lock";
880 rc = migrate_copy_data(fd, fdv, NULL);
882 error_loc = "data copy failed";
886 /* Make sure we keep original atime/mtime values */
887 rc = migrate_copy_timestamps(fd, fdv);
889 error_loc = "timestamp copy failed";
894 * for a migration we need to check data version on file did
897 * Pass in gid=0 since we already own grouplock. */
898 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
899 SWAP_LAYOUTS_CHECK_DV1);
901 error_loc = "file changed";
904 error_loc = "cannot swap layout";
909 rc2 = llapi_group_unlock(fd, gid);
910 if (rc2 < 0 && rc == 0) {
911 error_loc = "unlock group lock";
919 * Internal helper for migrate_copy_data(). Check lease and report error if
922 * \param[in] fd File descriptor on which to check the lease.
924 * \retval 0 Migration can keep on going.
925 * \retval -errno Error occurred, abort migration.
927 static int check_lease(int fd)
931 rc = llapi_lease_check(fd);
933 return 0; /* llapi_check_lease returns > 0 on success. */
938 static int migrate_nonblock(int fd, int fdv)
944 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
946 error_loc = "cannot get data version";
950 rc = migrate_copy_data(fd, fdv, check_lease);
952 error_loc = "data copy failed";
956 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
958 error_loc = "cannot get data version";
964 error_loc = "source file changed";
968 /* Make sure we keep original atime/mtime values */
969 rc = migrate_copy_timestamps(fd, fdv);
971 error_loc = "timestamp copy failed";
978 static int lfs_component_set(char *fname, int comp_id,
979 __u32 flags, __u32 neg_flags)
982 __u32 flags_array[2];
987 ids[count] = comp_id;
988 flags_array[count] = flags;
993 ids[count] = comp_id;
994 flags_array[count] = neg_flags | LCME_FL_NEG;
998 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1001 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1002 progname, comp_id, fname, flags, neg_flags);
1007 static int lfs_component_del(char *fname, __u32 comp_id,
1008 __u32 flags, __u32 neg_flags)
1012 if (flags && neg_flags)
1015 if (!flags && neg_flags)
1016 flags = neg_flags | LCME_FL_NEG;
1018 if ((flags && comp_id) || (!flags && !comp_id))
1021 /* LCME_FL_INIT is the only supported flag in PFL */
1023 if (flags & ~LCME_KNOWN_FLAGS) {
1025 "%s setstripe: unknown flags %#x\n",
1029 } else if (comp_id > LCME_ID_MAX) {
1030 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1035 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1038 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1039 progname, comp_id, fname, strerror(errno));
1043 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1050 rc = llapi_layout_file_comp_add(fname, layout);
1052 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1053 fname, strerror(errno));
1057 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1058 struct llapi_layout *layout)
1066 fd = lstat(fname, &st);
1067 if (fd == 0 && S_ISDIR(st.st_mode))
1068 open_flags = O_DIRECTORY | O_RDONLY;
1070 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1072 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1073 S_ISDIR(st.st_mode) ?
1074 "set default composite layout for" :
1075 "create composite file",
1076 fname, strerror(errno));
1080 static int lfs_migrate(char *name, __u64 migration_flags,
1081 struct llapi_stripe_param *param,
1082 struct llapi_layout *layout)
1088 rc = migrate_open_files(name, migration_flags, param, layout,
1093 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1094 /* Blocking mode (forced if servers do not support file lease).
1095 * It is also the default mode, since we cannot distinguish
1096 * between a broken lease and a server that does not support
1097 * atomic swap/close (LU-6785) */
1098 rc = migrate_block(fd, fdv);
1102 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1104 error_loc = "cannot get lease";
1108 rc = migrate_nonblock(fd, fdv);
1110 llapi_lease_release(fd);
1114 /* Atomically put lease, swap layouts and close.
1115 * for a migration we need to check data version on file did
1117 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1119 error_loc = "cannot swap layout";
1131 fprintf(stderr, "error: %s: %s: %s: %s\n",
1132 progname, name, error_loc, strerror(-rc));
1133 else if (migration_flags & MIGRATION_VERBOSE)
1134 printf("%s\n", name);
1139 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1148 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1152 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1153 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1154 const char *comp_name = comp_flags_table[i].cfn_name;
1156 if (strcmp(name, comp_name) == 0) {
1157 *flags |= comp_flag;
1159 } else if (strncmp(name, "^", 1) == 0 &&
1160 strcmp(name + 1, comp_name) == 0) {
1161 *neg_flags |= comp_flag;
1166 llapi_printf(LLAPI_MSG_ERROR,
1167 "%s: component flag '%s' not supported\n",
1173 if (!*flags && !*neg_flags)
1176 /* don't allow to set and exclude the same flag */
1177 if (*flags & *neg_flags)
1183 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1191 if (strncmp(string, "^", 1) == 0) {
1192 *neg_state = llapi_layout_string_flags(string + 1);
1193 if (*neg_state != 0)
1196 *state = llapi_layout_string_flags(string);
1201 llapi_printf(LLAPI_MSG_ERROR,
1202 "%s: mirrored file state '%s' not supported\n",
1208 * struct mirror_args - Command-line arguments for mirror(s).
1209 * @m_count: Number of mirrors to be created with this layout.
1210 * @m_flags: Mirror level flags, only 'prefer' is supported.
1211 * @m_layout: Mirror layout.
1212 * @m_file: A victim file. Its layout will be split and used as a mirror.
1213 * @m_next: Point to the next node of the list.
1215 * Command-line arguments for mirror(s) will be parsed and stored in
1216 * a linked list that consists of this structure.
1218 struct mirror_args {
1221 struct llapi_layout *m_layout;
1223 struct mirror_args *m_next;
1226 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1231 rc = llapi_layout_comp_flags_get(layout, &flags);
1235 if (flags & LCME_FL_NEG) {
1236 fprintf(stderr, "error: %s: negative flags are not supported\n",
1241 if (flags & LCME_FL_STALE) {
1242 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1243 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1247 return LLAPI_LAYOUT_ITER_CONT;
1250 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1252 uint64_t start, end;
1256 /* LU-10112: do not support dom+flr in phase 1 */
1257 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1261 rc = llapi_layout_pattern_get(layout, &pattern);
1265 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1266 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1271 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1275 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1279 if (end != LUSTRE_EOF) {
1280 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1285 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1290 * enum mirror_flags - Flags for extending a mirrored file.
1291 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1292 * in case the victim file(s) contains the same data as the
1293 * original mirrored file.
1294 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1295 * @MF_COMP_ID: specified component id instead of mirror id
1297 * Flags for extending a mirrored file.
1306 * mirror_create_sanity_check() - Check mirror list.
1307 * @list: A linked list that stores the mirror arguments.
1309 * This function does a sanity check on @list for creating
1312 * Return: 0 on success or a negative error code on failure.
1314 static int mirror_create_sanity_check(const char *fname,
1315 struct mirror_args *list)
1318 bool has_m_file = false;
1319 bool has_m_layout = false;
1325 struct llapi_layout *layout;
1327 layout = llapi_layout_get_by_path(fname, 0);
1330 "error: %s: file '%s' couldn't get layout\n",
1335 rc = mirror_sanity_check_one(layout);
1336 llapi_layout_free(layout);
1342 while (list != NULL) {
1343 if (list->m_file != NULL) {
1345 llapi_layout_free(list->m_layout);
1348 llapi_layout_get_by_path(list->m_file, 0);
1349 if (list->m_layout == NULL) {
1351 "error: %s: file '%s' has no layout\n",
1352 progname, list->m_file);
1356 has_m_layout = true;
1357 if (list->m_layout == NULL) {
1358 fprintf(stderr, "error: %s: no mirror layout\n",
1364 rc = mirror_sanity_check_one(list->m_layout);
1368 list = list->m_next;
1371 if (has_m_file && has_m_layout) {
1373 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1381 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1383 __u32 mirror_flags = *(__u32 *)cbdata;
1387 rc = llapi_layout_comp_flags_get(layout, &flags);
1392 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1397 return LLAPI_LAYOUT_ITER_CONT;
1401 * mirror_create() - Create a mirrored file.
1402 * @fname: The file to be created.
1403 * @mirror_list: A linked list that stores the mirror arguments.
1405 * This function creates a mirrored file @fname with the mirror(s)
1406 * from @mirror_list.
1408 * Return: 0 on success or a negative error code on failure.
1410 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1412 struct llapi_layout *layout = NULL;
1413 struct mirror_args *cur_mirror = NULL;
1414 uint16_t mirror_count = 0;
1418 rc = mirror_create_sanity_check(NULL, mirror_list);
1422 cur_mirror = mirror_list;
1423 while (cur_mirror != NULL) {
1424 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1426 &cur_mirror->m_flags);
1429 fprintf(stderr, "%s: failed to set mirror flags\n",
1434 for (i = 0; i < cur_mirror->m_count; i++) {
1435 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1438 fprintf(stderr, "error: %s: "
1439 "merge layout failed: %s\n",
1440 progname, strerror(errno));
1444 mirror_count += cur_mirror->m_count;
1445 cur_mirror = cur_mirror->m_next;
1448 if (layout == NULL) {
1449 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1453 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1456 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1457 progname, strerror(errno));
1461 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1469 llapi_layout_free(layout);
1474 * Compare files and check lease on @fd.
1476 * \retval bytes number of bytes are the same
1478 static ssize_t mirror_file_compare(int fd, int fdv)
1480 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1482 ssize_t bytes_done = 0;
1483 ssize_t bytes_read = 0;
1485 buf = malloc(buflen * 2);
1490 if (!llapi_lease_check(fd)) {
1491 bytes_done = -EBUSY;
1495 bytes_read = read(fd, buf, buflen);
1496 if (bytes_read <= 0)
1499 if (bytes_read != read(fdv, buf + buflen, buflen))
1502 /* XXX: should compute the checksum on each buffer and then
1503 * compare checksum to avoid cache collision */
1504 if (memcmp(buf, buf + buflen, bytes_read))
1507 bytes_done += bytes_read;
1515 static int mirror_extend_file(const char *fname, const char *victim_file,
1516 enum mirror_flags mirror_flags)
1521 struct stat stbuf_v;
1522 struct ll_ioc_lease *data = NULL;
1525 fd = open(fname, O_RDWR);
1527 error_loc = "open source file";
1532 fdv = open(victim_file, O_RDWR);
1534 error_loc = "open target file";
1539 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1540 error_loc = "stat source or target file";
1545 if (stbuf.st_dev != stbuf_v.st_dev) {
1546 error_loc = "stat source and target file";
1551 /* mirrors should be of the same size */
1552 if (stbuf.st_size != stbuf_v.st_size) {
1553 error_loc = "file sizes don't match";
1558 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1560 error_loc = "cannot get lease";
1564 if (!(mirror_flags & MF_NO_VERIFY)) {
1566 /* mirrors should have the same contents */
1567 ret = mirror_file_compare(fd, fdv);
1568 if (ret != stbuf.st_size) {
1569 error_loc = "file busy or contents don't match";
1570 rc = ret < 0 ? ret : -EINVAL;
1575 /* Get rid of caching pages from clients */
1576 rc = llapi_file_flush(fd);
1578 error_loc = "cannot get data version";
1582 rc = llapi_file_flush(fdv);
1584 error_loc = "cannot get data version";
1589 /* Make sure we keep original atime/mtime values */
1590 rc = migrate_copy_timestamps(fd, fdv);
1592 error_loc = "cannot copy timestamp";
1596 /* Atomically put lease, merge layouts and close. */
1597 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1599 error_loc = "memory allocation";
1602 data->lil_mode = LL_LEASE_UNLCK;
1603 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1604 data->lil_count = 1;
1605 data->lil_ids[0] = fdv;
1606 rc = llapi_lease_set(fd, data);
1608 error_loc = "cannot merge layout";
1610 } else if (rc == 0) {
1612 error_loc = "lost lease lock";
1625 (void) unlink(victim_file);
1627 fprintf(stderr, "error: %s: %s: %s: %s\n",
1628 progname, fname, error_loc, strerror(-rc));
1632 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1634 struct ll_ioc_lease *data = NULL;
1639 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1643 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1645 error_loc = "cannot get lease";
1649 rc = migrate_nonblock(fd, fdv);
1651 llapi_lease_release(fd);
1655 /* Atomically put lease, merge layouts and close. */
1656 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1658 error_loc = "memory allocation";
1661 data->lil_mode = LL_LEASE_UNLCK;
1662 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1663 data->lil_count = 1;
1664 data->lil_ids[0] = fdv;
1665 rc = llapi_lease_set(fd, data);
1667 error_loc = "cannot merge layout";
1669 } else if (rc == 0) {
1671 error_loc = "lost lease lock";
1684 fprintf(stderr, "error: %s: %s: %s: %s\n",
1685 progname, name, error_loc, strerror(-rc));
1689 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1690 enum mirror_flags mirror_flags)
1694 rc = mirror_create_sanity_check(fname, mirror_list);
1698 while (mirror_list) {
1699 if (mirror_list->m_file != NULL) {
1700 rc = mirror_extend_file(fname, mirror_list->m_file,
1703 __u32 mirror_count = mirror_list->m_count;
1705 while (mirror_count > 0) {
1706 rc = mirror_extend_layout(fname,
1707 mirror_list->m_layout);
1717 mirror_list = mirror_list->m_next;
1723 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1728 rc = llapi_layout_mirror_id_get(layout, &id);
1732 if ((__u16)id == *(__u16 *)cbdata)
1733 return LLAPI_LAYOUT_ITER_STOP;
1735 return LLAPI_LAYOUT_ITER_CONT;
1738 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1743 rc = llapi_layout_comp_id_get(layout, &id);
1747 if (id == *(__u32 *)cbdata)
1748 return LLAPI_LAYOUT_ITER_STOP;
1750 return LLAPI_LAYOUT_ITER_CONT;
1752 static int mirror_split(const char *fname, __u32 id,
1753 enum mirror_flags mflags, const char *victim_file)
1755 struct llapi_layout *layout;
1756 char parent[PATH_MAX];
1757 char victim[PATH_MAX];
1758 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1760 struct ll_ioc_lease *data;
1761 uint16_t mirror_count;
1766 /* check fname contains mirror with mirror_id/comp_id */
1767 layout = llapi_layout_get_by_path(fname, 0);
1770 "error %s: file '%s' couldn't get layout\n",
1775 rc = mirror_sanity_check_one(layout);
1779 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1782 "error %s: file '%s' couldn't get mirror count\n",
1786 if (mirror_count < 2) {
1788 "error %s: file '%s' has %d component, cannot split\n",
1789 progname, fname, mirror_count);
1793 if (mflags & MF_COMP_ID) {
1794 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1795 id = mirror_id_of(id);
1797 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1800 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1803 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1805 "error %s: file '%s' does not contain mirror with id %u\n",
1806 progname, fname, id);
1810 fd = open(fname, O_RDWR);
1813 "error %s: open file '%s' failed: %s\n",
1814 progname, fname, strerror(errno));
1818 /* get victim file directory pathname */
1819 if (strlen(fname) > sizeof(parent) - 1) {
1820 fprintf(stderr, "error %s: file name of '%s' too long\n",
1825 strncpy(parent, fname, sizeof(parent));
1826 ptr = strrchr(parent, '/');
1828 if (getcwd(parent, sizeof(parent)) == NULL) {
1829 fprintf(stderr, "error %s: getcwd failed: %s\n",
1830 progname, strerror(errno));
1840 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1842 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1847 if (victim_file == NULL) {
1848 /* use a temp file to store the splitted layout */
1849 if (mflags & MF_DESTROY) {
1850 fdv = llapi_create_volatile_idx(parent, mdt_index,
1851 O_LOV_DELAY_CREATE);
1853 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1855 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1858 /* user specified victim file */
1859 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1864 "error %s: create victim file failed: %s\n",
1865 progname, strerror(errno));
1869 /* get lease lock of fname */
1870 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1873 "error %s: cannot get lease of file '%s': %d\n",
1874 progname, fname, rc);
1878 /* Atomatically put lease, split layouts and close. */
1879 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1885 data->lil_mode = LL_LEASE_UNLCK;
1886 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1887 data->lil_count = 2;
1888 data->lil_ids[0] = fdv;
1889 data->lil_ids[1] = id;
1890 rc = llapi_lease_set(fd, data);
1892 if (rc == 0) /* lost lease lock */
1895 "error %s: cannot split '%s': %s\n",
1896 progname, fname, strerror(-rc));
1907 llapi_layout_free(layout);
1912 * Parse a string containing an target index list into an array of integers.
1914 * The input string contains a comma delimited list of individual
1915 * indices and ranges, for example "1,2-4,7". Add the indices into the
1916 * \a tgts array and remove duplicates.
1918 * \param[out] tgts array to store indices in
1919 * \param[in] size size of \a tgts array
1920 * \param[in] offset starting index in \a tgts
1921 * \param[in] arg string containing OST index list
1923 * \retval positive number of indices in \a tgts
1924 * \retval -EINVAL unable to parse \a arg
1926 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1930 int slots = size - offset;
1937 end_of_loop = false;
1938 while (!end_of_loop) {
1942 char *endptr = NULL;
1946 ptr = strchrnul(arg, ',');
1948 end_of_loop = *ptr == '\0';
1951 start_index = strtol(arg, &endptr, 0);
1952 if (endptr == arg) /* no data at all */
1954 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1957 end_index = start_index;
1958 if (*endptr == '-') {
1959 end_index = strtol(endptr + 1, &endptr, 0);
1960 if (*endptr != '\0')
1962 if (end_index < start_index)
1966 for (i = start_index; i <= end_index && slots > 0; i++) {
1969 /* remove duplicate */
1970 for (j = 0; j < offset; j++) {
1974 if (j == offset) { /* no duplicate */
1979 if (slots == 0 && i < end_index)
1987 if (!end_of_loop && ptr != NULL)
1990 return rc < 0 ? rc : nr;
1993 struct lfs_setstripe_args {
1994 unsigned long long lsa_comp_end;
1995 unsigned long long lsa_stripe_size;
1996 long long lsa_stripe_count;
1997 long long lsa_stripe_off;
1998 __u32 lsa_comp_flags;
1999 __u32 lsa_comp_neg_flags;
2000 unsigned long long lsa_pattern;
2001 unsigned int lsa_mirror_count;
2003 bool lsa_first_comp;
2005 char *lsa_pool_name;
2008 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2010 unsigned int mirror_count = lsa->lsa_mirror_count;
2011 bool first_comp = lsa->lsa_first_comp;
2013 memset(lsa, 0, sizeof(*lsa));
2015 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2016 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2017 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2018 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2019 lsa->lsa_pool_name = NULL;
2021 lsa->lsa_mirror_count = mirror_count;
2022 lsa->lsa_first_comp = first_comp;
2026 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2027 * @lsa: Stripe options to be initialized and inherited.
2029 * This function initializes stripe options in @lsa and inherit
2030 * stripe_size, stripe_count and OST pool_name options.
2034 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2036 unsigned long long stripe_size;
2037 long long stripe_count;
2038 char *pool_name = NULL;
2040 stripe_size = lsa->lsa_stripe_size;
2041 stripe_count = lsa->lsa_stripe_count;
2042 pool_name = lsa->lsa_pool_name;
2044 setstripe_args_init(lsa);
2046 lsa->lsa_stripe_size = stripe_size;
2047 lsa->lsa_stripe_count = stripe_count;
2048 lsa->lsa_pool_name = pool_name;
2051 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2053 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2054 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2055 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2056 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2057 lsa->lsa_pool_name != NULL ||
2058 lsa->lsa_comp_end != 0);
2062 * comp_args_to_layout() - Create or extend a composite layout.
2063 * @composite: Pointer to the composite layout.
2064 * @lsa: Stripe options for the new component.
2066 * This function creates or extends a composite layout by adding a new
2067 * component with stripe options from @lsa.
2069 * Return: 0 on success or an error code on failure.
2071 static int comp_args_to_layout(struct llapi_layout **composite,
2072 struct lfs_setstripe_args *lsa,
2075 struct llapi_layout *layout = *composite;
2076 uint64_t prev_end = 0;
2079 if (layout == NULL) {
2080 layout = llapi_layout_alloc();
2081 if (layout == NULL) {
2082 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2086 *composite = layout;
2090 /* Get current component extent, current component
2091 * must be the tail component. */
2092 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2094 fprintf(stderr, "Get comp extent failed. %s\n",
2099 if (lsa->lsa_first_comp)
2102 if (lsa->lsa_first_comp)
2103 rc = llapi_layout_add_first_comp(layout);
2105 rc = llapi_layout_comp_add(layout);
2107 fprintf(stderr, "Add component failed. %s\n",
2112 /* reset lsa_first_comp */
2113 lsa->lsa_first_comp = false;
2116 rc = llapi_layout_comp_extent_set(layout, prev_end,
2119 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2120 prev_end, lsa->lsa_comp_end, strerror(errno));
2125 /* Data-on-MDT component setting */
2126 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2127 /* In case of Data-on-MDT patterns the only extra option
2128 * applicable is stripe size option. */
2129 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2130 fprintf(stderr, "Option 'stripe-count' can't be "
2131 "specified with Data-on-MDT component: %lld\n",
2132 lsa->lsa_stripe_count);
2135 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2136 fprintf(stderr, "Option 'stripe-size' can't be "
2137 "specified with Data-on-MDT component: %llu\n",
2138 lsa->lsa_stripe_size);
2141 if (lsa->lsa_nr_tgts != 0) {
2142 fprintf(stderr, "Option 'ost-list' can't be specified "
2143 "with Data-on-MDT component: '%i'\n",
2147 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2148 fprintf(stderr, "Option 'stripe-offset' can't be "
2149 "specified with Data-on-MDT component: %lld\n",
2150 lsa->lsa_stripe_off);
2153 if (lsa->lsa_pool_name != 0) {
2154 fprintf(stderr, "Option 'pool' can't be specified "
2155 "with Data-on-MDT component: '%s'\n",
2156 lsa->lsa_pool_name);
2160 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2162 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2163 lsa->lsa_pattern, strerror(errno));
2166 /* Data-on-MDT component has always single stripe up to end */
2167 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2170 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2172 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2173 lsa->lsa_stripe_size, strerror(errno));
2177 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2179 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2180 lsa->lsa_stripe_count, strerror(errno));
2184 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2186 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2187 lsa->lsa_comp_flags, strerror(errno));
2191 if (lsa->lsa_pool_name != NULL) {
2192 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2194 fprintf(stderr, "Set pool name: %s failed. %s\n",
2195 lsa->lsa_pool_name, strerror(errno));
2199 rc = llapi_layout_pool_name_set(layout, "");
2201 fprintf(stderr, "Clear pool name failed: %s\n",
2207 if (lsa->lsa_nr_tgts > 0) {
2208 if (lsa->lsa_stripe_count > 0 &&
2209 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2210 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2211 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2212 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2213 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2216 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2217 rc = llapi_layout_ost_index_set(layout, i,
2222 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2223 lsa->lsa_stripe_off != -1) {
2224 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2227 fprintf(stderr, "Set ost index %d failed. %s\n",
2228 i, strerror(errno));
2235 static int build_component(struct llapi_layout **layout,
2236 struct lfs_setstripe_args *lsa, bool set_extent)
2240 rc = comp_args_to_layout(layout, lsa, set_extent);
2244 if (lsa->lsa_mirror_count > 0) {
2245 rc = llapi_layout_mirror_count_set(*layout,
2246 lsa->lsa_mirror_count);
2250 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2253 lsa->lsa_mirror_count = 0;
2259 static int build_layout_from_yaml_node(struct cYAML *node,
2260 struct llapi_layout **layout,
2261 struct lfs_setstripe_args *lsa,
2268 if (node->cy_type == CYAML_TYPE_OBJECT) {
2269 /* go deep to sub blocks */
2270 rc = build_layout_from_yaml_node(node->cy_child, layout,
2275 if (node->cy_string == NULL)
2278 string = node->cy_string;
2279 /* skip leading lmm_ if present, to simplify parsing */
2280 if (strncmp(string, "lmm_", 4) == 0)
2283 if (node->cy_type == CYAML_TYPE_STRING) {
2284 if (!strcmp(string, "lcme_extent.e_end")) {
2285 if (!strcmp(node->cy_valuestring, "EOF") ||
2286 !strcmp(node->cy_valuestring, "eof"))
2287 lsa->lsa_comp_end = LUSTRE_EOF;
2288 } else if (!strcmp(string, "pool")) {
2289 lsa->lsa_pool_name = node->cy_valuestring;
2290 } else if (!strcmp(string, "pattern")) {
2291 if (!strcmp(node->cy_valuestring, "mdt"))
2292 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2293 } else if (!strcmp(string, "lcme_flags")) {
2294 rc = comp_str2flags(node->cy_valuestring,
2295 &lsa->lsa_comp_flags,
2296 &lsa->lsa_comp_neg_flags);
2299 /* Only template flags have meaning in
2300 * the layout for a new file
2302 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2304 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2305 if (!strcmp(string, "lcm_mirror_count")) {
2306 lsa->lsa_mirror_count = node->cy_valueint;
2307 } else if (!strcmp(string, "lcme_extent.e_start")) {
2308 if (node->cy_valueint != 0 || *layout != NULL) {
2309 rc = build_component(layout, lsa, true);
2314 if (node->cy_valueint == 0)
2315 lsa->lsa_first_comp = true;
2317 /* initialize lsa */
2318 setstripe_args_init(lsa);
2319 lsa->lsa_tgts = osts;
2320 } else if (!strcmp(string, "lcme_extent.e_end")) {
2321 if (node->cy_valueint == -1)
2322 lsa->lsa_comp_end = LUSTRE_EOF;
2324 lsa->lsa_comp_end = node->cy_valueint;
2325 } else if (!strcmp(string, "stripe_count")) {
2326 lsa->lsa_stripe_count = node->cy_valueint;
2327 } else if (!strcmp(string, "stripe_size")) {
2328 lsa->lsa_stripe_size = node->cy_valueint;
2329 } else if (!strcmp(string, "stripe_offset")) {
2330 lsa->lsa_stripe_off = node->cy_valueint;
2331 } else if (!strcmp(string, "l_ost_idx")) {
2332 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2337 node = node->cy_next;
2343 static int lfs_comp_create_from_yaml(char *template,
2344 struct llapi_layout **layout,
2345 struct lfs_setstripe_args *lsa,
2348 struct cYAML *tree = NULL, *err_rc = NULL;
2351 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2353 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2354 progname, template);
2355 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2356 "can't parse", &err_rc);
2357 cYAML_print_tree2file(stderr, err_rc);
2358 cYAML_free_tree(err_rc);
2363 /* initialize lsa for plain file */
2364 setstripe_args_init(lsa);
2365 lsa->lsa_tgts = osts;
2367 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2369 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2370 progname, template);
2373 rc = build_component(layout, lsa, *layout != NULL);
2375 /* clean clean lsa */
2376 setstripe_args_init(lsa);
2380 cYAML_free_tree(tree);
2384 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2385 * end of the last component in the existing file, and adjust the
2386 * first extent start of the components to be added accordingly. */
2387 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2389 struct llapi_layout *head;
2390 uint64_t start, end, stripe_size, prev_end = 0;
2393 if (layout == NULL) {
2395 "%s setstripe: layout must be specified\n",
2401 head = llapi_layout_get_by_path(fname, 0);
2404 "%s setstripe: cannot read layout from '%s': %s\n",
2405 progname, fname, strerror(errno));
2407 } else if (errno == ENODATA) {
2408 /* file without LOVEA, this component-add will be turned
2409 * into a component-create. */
2410 llapi_layout_free(head);
2412 } else if (!llapi_layout_is_composite(head)) {
2413 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2415 llapi_layout_free(head);
2419 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2421 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2422 progname, strerror(errno));
2423 llapi_layout_free(head);
2427 llapi_layout_free(head);
2429 /* Make sure we use the first component of the layout to be added. */
2430 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2433 "%s setstripe: cannot move component cursor: %s\n",
2434 progname, strerror(errno));
2438 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2440 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2441 progname, strerror(errno));
2445 if (start > prev_end || end <= prev_end) {
2447 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2448 progname, start, end, prev_end);
2452 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2454 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2455 progname, strerror(errno));
2459 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2460 (prev_end & (stripe_size - 1))) {
2462 "%s setstripe: stripe size %lu not aligned with %lu\n",
2463 progname, stripe_size, prev_end);
2467 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2470 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2471 progname, prev_end, end, strerror(errno));
2478 static inline bool arg_is_eof(char *arg)
2480 return !strncmp(arg, "-1", strlen("-1")) ||
2481 !strncmp(arg, "EOF", strlen("EOF")) ||
2482 !strncmp(arg, "eof", strlen("eof"));
2486 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2488 * Return: Valid mirror_args pointer on success and
2489 * NULL if memory allocation fails.
2491 static struct mirror_args *lfs_mirror_alloc(void)
2493 struct mirror_args *mirror = NULL;
2496 mirror = calloc(1, sizeof(*mirror));
2507 * lfs_mirror_free() - Free memory allocated for a mirror argument
2509 * @mirror: Previously allocated mirror argument structure by
2510 * lfs_mirror_alloc().
2512 * Free memory allocated for @mirror.
2516 static void lfs_mirror_free(struct mirror_args *mirror)
2518 if (mirror->m_layout != NULL)
2519 llapi_layout_free(mirror->m_layout);
2524 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2525 * @mirror_list: Previously allocated mirror list.
2527 * Free memory allocated for @mirror_list.
2531 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2533 struct mirror_args *next_mirror = NULL;
2535 while (mirror_list != NULL) {
2536 next_mirror = mirror_list->m_next;
2537 lfs_mirror_free(mirror_list);
2538 mirror_list = next_mirror;
2550 LFS_COMP_NO_VERIFY_OPT,
2552 LFS_MIRROR_FLAGS_OPT,
2554 LFS_MIRROR_STATE_OPT,
2556 LFS_MIRROR_INDEX_OPT,
2560 static int lfs_setstripe_internal(int argc, char **argv,
2561 enum setstripe_origin opc)
2563 struct lfs_setstripe_args lsa = { 0 };
2564 struct llapi_stripe_param *param = NULL;
2565 struct find_param migrate_mdt_param = {
2575 unsigned long long size_units = 1;
2576 bool migrate_mode = false;
2577 bool migrate_mdt_mode = false;
2578 bool migration_block = false;
2579 __u64 migration_flags = 0;
2580 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2581 int comp_del = 0, comp_set = 0;
2584 struct llapi_layout *layout = NULL;
2585 struct llapi_layout **lpp = &layout;
2586 bool mirror_mode = false;
2587 bool has_m_file = false;
2588 __u32 mirror_count = 0;
2589 enum mirror_flags mirror_flags = 0;
2590 struct mirror_args *mirror_list = NULL;
2591 struct mirror_args *new_mirror = NULL;
2592 struct mirror_args *last_mirror = NULL;
2593 __u16 mirror_id = 0;
2595 bool from_yaml = false;
2596 bool from_copy = false;
2597 char *template = NULL;
2599 struct option long_opts[] = {
2600 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2601 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2602 /* --block is only valid in migrate mode */
2603 { .val = 'b', .name = "block", .has_arg = no_argument },
2604 { .val = LFS_COMP_ADD_OPT,
2605 .name = "comp-add", .has_arg = no_argument },
2606 { .val = LFS_COMP_ADD_OPT,
2607 .name = "component-add", .has_arg = no_argument },
2608 { .val = LFS_COMP_DEL_OPT,
2609 .name = "comp-del", .has_arg = no_argument },
2610 { .val = LFS_COMP_DEL_OPT,
2611 .name = "component-del", .has_arg = no_argument },
2612 { .val = LFS_COMP_FLAGS_OPT,
2613 .name = "comp-flags", .has_arg = required_argument },
2614 { .val = LFS_COMP_FLAGS_OPT,
2615 .name = "component-flags",
2616 .has_arg = required_argument },
2617 { .val = LFS_COMP_SET_OPT,
2618 .name = "comp-set", .has_arg = no_argument },
2619 { .val = LFS_COMP_SET_OPT,
2620 .name = "component-set",
2621 .has_arg = no_argument},
2622 { .val = LFS_COMP_NO_VERIFY_OPT,
2623 .name = "no-verify", .has_arg = no_argument},
2624 { .val = LFS_MIRROR_FLAGS_OPT,
2625 .name = "flags", .has_arg = required_argument},
2626 { .val = LFS_MIRROR_ID_OPT,
2627 .name = "mirror-id", .has_arg = required_argument},
2628 { .val = LFS_LAYOUT_COPY,
2629 .name = "copy", .has_arg = required_argument},
2630 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2631 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2632 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2633 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2634 { .val = 'd', .name = "delete", .has_arg = no_argument},
2635 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2636 /* --non-direct is only valid in migrate mode */
2637 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2638 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2639 { .val = 'E', .name = "component-end",
2640 .has_arg = required_argument},
2641 { .val = 'f', .name = "file", .has_arg = required_argument },
2642 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2643 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2644 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2645 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2646 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2647 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2648 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2649 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2650 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2651 { .val = 'L', .name = "layout", .has_arg = required_argument },
2652 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2653 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2654 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2655 /* --non-block is only valid in migrate mode */
2656 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2657 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2658 { .val = 'o', .name = "ost", .has_arg = required_argument },
2659 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2660 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2661 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2663 { .val = 'p', .name = "pool", .has_arg = required_argument },
2664 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2665 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2666 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2667 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2668 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2669 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2670 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2671 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2672 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2673 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2674 /* --verbose is only valid in migrate mode */
2675 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2676 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2679 setstripe_args_init(&lsa);
2681 migrate_mode = (opc == SO_MIGRATE);
2682 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2684 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2686 while ((c = getopt_long(argc, argv,
2687 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2693 case LFS_COMP_ADD_OPT:
2696 case LFS_COMP_DEL_OPT:
2699 case LFS_COMP_FLAGS_OPT:
2700 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2701 &lsa.lsa_comp_neg_flags);
2704 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2705 fprintf(stderr, "%s: inverted flags are not supported\n",
2709 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2711 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2718 case LFS_COMP_SET_OPT:
2721 case LFS_COMP_NO_VERIFY_OPT:
2722 mirror_flags |= MF_NO_VERIFY;
2724 case LFS_MIRROR_ID_OPT:
2725 mirror_id = strtoul(optarg, &end, 0);
2726 if (*end != '\0' || mirror_id == 0) {
2728 "%s %s: invalid mirror ID '%s'\n",
2729 progname, argv[0], optarg);
2733 case LFS_MIRROR_FLAGS_OPT: {
2736 if (!mirror_mode || !last_mirror) {
2737 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2742 result = comp_str2flags(optarg, &last_mirror->m_flags,
2748 fprintf(stderr, "%s: inverted flags are not supported\n",
2753 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2755 "%s: unsupported mirror flags: %s\n",
2762 case LFS_LAYOUT_COPY:
2767 if (!migrate_mode) {
2769 "%s %s: -b|--block valid only for migrate command\n",
2773 migration_block = true;
2776 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2779 "%s %s: invalid stripe count '%s'\n",
2780 progname, argv[0], optarg);
2784 if (lsa.lsa_stripe_count == -1)
2785 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2788 /* delete the default striping pattern */
2790 if (opc == SO_MIRROR_SPLIT) {
2793 "%s %s: -d cannot used with -f\n",
2797 mirror_flags |= MF_DESTROY;
2801 if (!migrate_mode) {
2803 "%s %s: -D|--non-direct is valid "
2804 "only for migrate command\n",
2808 migration_flags |= MIGRATION_NONDIRECT;
2811 if (lsa.lsa_comp_end != 0) {
2812 result = comp_args_to_layout(lpp, &lsa, true);
2815 "%s %s: invalid layout\n",
2820 setstripe_args_init_inherit(&lsa);
2823 if (arg_is_eof(optarg)) {
2824 lsa.lsa_comp_end = LUSTRE_EOF;
2826 result = llapi_parse_size(optarg,
2831 "%s %s: invalid component end '%s'\n",
2832 progname, argv[0], optarg);
2838 if (!migrate_mode) {
2839 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2843 lsa.lsa_pattern = check_hashtype(optarg);
2844 if (lsa.lsa_pattern == 0) {
2846 "%s %s: bad stripe hash type '%s'\n",
2847 progname, argv[0], optarg);
2852 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2855 "%s %s: invalid stripe offset '%s'\n",
2856 progname, argv[0], optarg);
2859 if (lsa.lsa_stripe_off == -1)
2860 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2863 comp_id = strtoul(optarg, &end, 0);
2864 if (*end != '\0' || comp_id == 0 ||
2865 comp_id > LCME_ID_MAX) {
2867 "%s %s: invalid component ID '%s'\n",
2868 progname, argv[0], optarg);
2873 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2875 "error: %s: invalid option: %s\n",
2876 progname, argv[optopt + 1]);
2879 if (opc == SO_MIRROR_EXTEND) {
2880 if (last_mirror == NULL) {
2882 "error: %s: '-N' must exist in front of '%s'\n",
2883 progname, argv[optopt + 1]);
2886 last_mirror->m_file = optarg;
2887 last_mirror->m_count = 1;
2890 if (mirror_list == NULL)
2891 mirror_list = lfs_mirror_alloc();
2892 mirror_list->m_file = optarg;
2897 if (strcmp(argv[optind - 1], "mdt") == 0) {
2898 /* Can be only the first component */
2899 if (layout != NULL) {
2901 fprintf(stderr, "error: 'mdt' layout "
2902 "can be only the first one\n");
2905 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2907 fprintf(stderr, "error: 'mdt' layout "
2908 "size is too big\n");
2911 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2912 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2914 fprintf(stderr, "error: layout '%s' is "
2915 "unknown, supported layouts are: "
2916 "'mdt', 'raid0'\n", argv[optind]);
2921 if (!migrate_mode) {
2923 "%s %s: -m|--mdt is valid only for migrate command\n",
2927 migrate_mdt_mode = true;
2928 lsa.lsa_nr_tgts = parse_targets(tgts,
2929 sizeof(tgts) / sizeof(__u32),
2930 lsa.lsa_nr_tgts, optarg);
2931 if (lsa.lsa_nr_tgts < 0) {
2933 "%s %s: invalid MDT target(s) '%s'\n",
2934 progname, argv[0], optarg);
2938 lsa.lsa_tgts = tgts;
2939 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2940 lsa.lsa_stripe_off = tgts[0];
2943 if (!migrate_mode) {
2945 "%s %s: -n|--non-block valid only for migrate command\n",
2949 migration_flags |= MIGRATION_NONBLOCK;
2952 if (opc == SO_SETSTRIPE) {
2953 opc = SO_MIRROR_CREATE;
2957 if (optarg != NULL) {
2958 mirror_count = strtoul(optarg, &end, 0);
2959 if (*end != '\0' || mirror_count == 0) {
2961 "error: %s: bad mirror count: %s\n",
2968 new_mirror = lfs_mirror_alloc();
2969 new_mirror->m_count = mirror_count;
2971 if (mirror_list == NULL)
2972 mirror_list = new_mirror;
2974 if (last_mirror != NULL) {
2975 /* wrap up last mirror */
2976 if (lsa.lsa_comp_end == 0)
2977 lsa.lsa_comp_end = LUSTRE_EOF;
2979 result = comp_args_to_layout(lpp, &lsa, true);
2981 lfs_mirror_free(new_mirror);
2985 setstripe_args_init_inherit(&lsa);
2987 last_mirror->m_next = new_mirror;
2990 last_mirror = new_mirror;
2991 lpp = &last_mirror->m_layout;
2994 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2995 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2996 fprintf(stderr, "warning: '--ost-list' is "
2997 "deprecated, use '--ost' instead\n");
2999 lsa.lsa_nr_tgts = parse_targets(tgts,
3000 sizeof(tgts) / sizeof(__u32),
3001 lsa.lsa_nr_tgts, optarg);
3002 if (lsa.lsa_nr_tgts < 0) {
3004 "%s %s: invalid OST target(s) '%s'\n",
3005 progname, argv[0], optarg);
3009 lsa.lsa_tgts = tgts;
3010 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3011 lsa.lsa_stripe_off = tgts[0];
3016 lsa.lsa_pool_name = optarg;
3018 if (strlen(lsa.lsa_pool_name) == 0 ||
3019 strncmp(lsa.lsa_pool_name, "none",
3020 LOV_MAXPOOLNAME) == 0)
3021 lsa.lsa_pool_name = NULL;
3024 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3028 "%s %s: invalid stripe size '%s'\n",
3029 progname, argv[0], optarg);
3034 if (!migrate_mode) {
3036 "%s %s: -v|--verbose valid only for migrate command\n",
3040 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3041 migration_flags = MIGRATION_VERBOSE;
3048 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3049 progname, argv[0], argv[optind - 1]);
3054 fname = argv[optind];
3056 if (optind == argc) {
3057 fprintf(stderr, "%s %s: FILE must be specified\n",
3062 if (mirror_mode && mirror_count == 0) {
3064 "error: %s: --mirror-count|-N option is required\n",
3071 if (lsa.lsa_comp_end == 0)
3072 lsa.lsa_comp_end = LUSTRE_EOF;
3075 if (lsa.lsa_comp_end != 0) {
3076 result = comp_args_to_layout(lpp, &lsa, true);
3081 if (mirror_flags & MF_NO_VERIFY) {
3082 if (opc != SO_MIRROR_EXTEND) {
3084 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3088 } else if (!has_m_file) {
3090 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3097 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3098 * altered by user space tool, so we don't need to support the
3099 * --component-set for this moment. */
3100 if (comp_set && !comp_id) {
3101 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3106 if ((delete + comp_set + comp_del + comp_add) > 1) {
3108 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3113 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3114 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3116 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3121 if ((comp_set || comp_del) &&
3122 (setstripe_args_specified(&lsa) || layout != NULL)) {
3124 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3129 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3131 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3136 if (comp_add || comp_del) {
3139 result = lstat(fname, &st);
3140 if (result == 0 && S_ISDIR(st.st_mode)) {
3142 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3148 fprintf(stderr, "error: %s: can't use --component-add "
3149 "or --component-del for mirror operation\n",
3156 if (layout == NULL) {
3158 "%s %s: option -E must be specified with --component-add\n",
3163 result = adjust_first_extent(fname, layout);
3164 if (result == -ENODATA)
3166 else if (result != 0)
3170 if (from_yaml && from_copy) {
3172 "%s: can't specify --yaml and --copy together\n",
3177 if ((from_yaml || from_copy) &&
3178 (setstripe_args_specified(&lsa) || layout != NULL)) {
3179 fprintf(stderr, "error: %s: can't specify --yaml with "
3180 "-c, -S, -i, -o, -p or -E options.\n",
3185 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3187 "%s %s: options --non-block and --block are mutually exclusive\n",
3192 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3195 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3200 if (migrate_mdt_mode) {
3201 struct lmv_user_md *lmu;
3203 /* initialize migrate mdt parameters */
3204 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3205 LMV_USER_MAGIC_SPECIFIC));
3208 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3209 progname, argv[0], strerror(ENOMEM));
3213 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3214 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3215 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3217 "%s %s: migrate should specify MDT index\n",
3222 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3223 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3224 lmu->lum_hash_type = lsa.lsa_pattern;
3226 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3227 if (lsa.lsa_pool_name)
3228 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3229 sizeof(lmu->lum_pool_name));
3230 if (lsa.lsa_nr_tgts > 1) {
3233 if (lsa.lsa_stripe_count > 0 &&
3234 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3235 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3237 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3238 progname, lsa.lsa_stripe_count,
3244 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3245 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3246 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3247 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3249 lmu->lum_magic = LMV_USER_MAGIC;
3252 migrate_mdt_param.fp_lmv_md = lmu;
3253 migrate_mdt_param.fp_migrate = 1;
3254 } else if (layout == NULL) {
3255 /* initialize stripe parameters */
3256 param = calloc(1, offsetof(typeof(*param),
3257 lsp_osts[lsa.lsa_nr_tgts]));
3258 if (param == NULL) {
3260 "%s %s: cannot allocate memory for parameters: %s\n",
3261 progname, argv[0], strerror(ENOMEM));
3266 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3267 param->lsp_stripe_size = lsa.lsa_stripe_size;
3268 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3269 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3270 param->lsp_stripe_count = -1;
3272 param->lsp_stripe_count = lsa.lsa_stripe_count;
3274 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3275 param->lsp_stripe_offset = -1;
3277 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3278 param->lsp_pool = lsa.lsa_pool_name;
3279 param->lsp_is_specific = false;
3280 if (lsa.lsa_nr_tgts > 0) {
3281 if (lsa.lsa_stripe_count > 0 &&
3282 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3283 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3284 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3285 fprintf(stderr, "error: %s: stripe count %lld "
3286 "doesn't match the number of OSTs: %d\n"
3287 , argv[0], lsa.lsa_stripe_count,
3293 param->lsp_is_specific = true;
3294 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3295 memcpy(param->lsp_osts, tgts,
3296 sizeof(*tgts) * lsa.lsa_nr_tgts);
3301 /* generate a layout from a YAML template */
3302 result = lfs_comp_create_from_yaml(template, &layout,
3305 fprintf(stderr, "error: %s: can't create composite "
3306 "layout from template file %s\n",
3310 } else if (from_copy) {
3311 layout = llapi_layout_get_by_path(template, 0);
3312 if (layout == NULL) {
3314 "%s: can't create composite layout from file %s.\n",
3315 progname, template);
3320 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3321 if (migrate_mdt_mode) {
3322 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3323 } else if (migrate_mode) {
3324 result = lfs_migrate(fname, migration_flags, param,
3326 } else if (comp_set != 0) {
3327 result = lfs_component_set(fname, comp_id,
3329 lsa.lsa_comp_neg_flags);
3330 } else if (comp_del != 0) {
3331 result = lfs_component_del(fname, comp_id,
3333 lsa.lsa_comp_neg_flags);
3334 } else if (comp_add != 0) {
3335 result = lfs_component_add(fname, layout);
3336 } else if (opc == SO_MIRROR_CREATE) {
3337 result = mirror_create(fname, mirror_list);
3338 } else if (opc == SO_MIRROR_EXTEND) {
3339 result = mirror_extend(fname, mirror_list,
3341 } else if (opc == SO_MIRROR_SPLIT) {
3342 if (mirror_id == 0 && comp_id == 0) {
3344 "%s %s: no mirror id or component id is specified\n",
3349 comp_id = mirror_id;
3351 mirror_flags |= MF_COMP_ID;
3352 result = mirror_split(fname, comp_id, mirror_flags,
3353 has_m_file ? mirror_list->m_file :
3355 } else if (layout != NULL) {
3356 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3363 result = llapi_file_open_param(fname,
3372 /* Save the first error encountered. */
3380 free(migrate_mdt_param.fp_lmv_md);
3381 llapi_layout_free(layout);
3382 lfs_mirror_list_free(mirror_list);
3387 llapi_layout_free(layout);
3388 lfs_mirror_list_free(mirror_list);
3392 static int lfs_poollist(int argc, char **argv)
3397 return llapi_poollist(argv[1]);
3400 static int set_time(time_t *time, time_t *set, char *str)
3408 else if (str[0] == '-')
3414 t = strtol(str, &endptr, 0);
3415 if (*endptr != '\0') {
3417 "%s find: bad time '%s': %s\n",
3418 progname, str, strerror(EINVAL));
3421 if (*time < t * 24 * 60 * 60) {
3425 "%s find: bad time '%s': too large\n",
3430 *set = *time - t * 24 * 60 * 60;
3434 static int name2uid(unsigned int *id, const char *name)
3436 struct passwd *passwd;
3438 passwd = getpwnam(name);
3441 *id = passwd->pw_uid;
3446 static int name2gid(unsigned int *id, const char *name)
3448 struct group *group;
3450 group = getgrnam(name);
3453 *id = group->gr_gid;
3458 static inline int name2projid(unsigned int *id, const char *name)
3463 static int uid2name(char **name, unsigned int id)
3465 struct passwd *passwd;
3467 passwd = getpwuid(id);
3470 *name = passwd->pw_name;
3475 static inline int gid2name(char **name, unsigned int id)
3477 struct group *group;
3479 group = getgrgid(id);
3482 *name = group->gr_name;
3487 static int name2layout(__u32 *layout, char *name)
3489 char *ptr, *layout_name;
3492 for (ptr = name; ; ptr = NULL) {
3493 layout_name = strtok(ptr, ",");
3494 if (layout_name == NULL)
3496 if (strcmp(layout_name, "released") == 0)
3497 *layout |= LOV_PATTERN_F_RELEASED;
3498 else if (strcmp(layout_name, "raid0") == 0)
3499 *layout |= LOV_PATTERN_RAID0;
3500 else if (strcmp(layout_name, "mdt") == 0)
3501 *layout |= LOV_PATTERN_MDT;
3508 static int lfs_find(int argc, char **argv)
3513 struct find_param param = {
3517 struct option long_opts[] = {
3518 { .val = 'A', .name = "atime", .has_arg = required_argument },
3519 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3520 { .val = LFS_COMP_COUNT_OPT,
3521 .name = "comp-count", .has_arg = required_argument },
3522 { .val = LFS_COMP_COUNT_OPT,
3523 .name = "component-count",
3524 .has_arg = required_argument },
3525 { .val = LFS_COMP_FLAGS_OPT,
3526 .name = "comp-flags", .has_arg = required_argument },
3527 { .val = LFS_COMP_FLAGS_OPT,
3528 .name = "component-flags",
3529 .has_arg = required_argument },
3530 { .val = LFS_COMP_START_OPT,
3531 .name = "comp-start", .has_arg = required_argument },
3532 { .val = LFS_COMP_START_OPT,
3533 .name = "component-start",
3534 .has_arg = required_argument },
3535 { .val = LFS_MIRROR_STATE_OPT,
3536 .name = "mirror-state", .has_arg = required_argument },
3537 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3538 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3539 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3540 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3541 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3542 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3543 { .val = 'E', .name = "component-end",
3544 .has_arg = required_argument },
3545 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3546 { .val = 'g', .name = "gid", .has_arg = required_argument },
3547 { .val = 'G', .name = "group", .has_arg = required_argument },
3548 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3549 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3550 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3551 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3552 { .val = 'L', .name = "layout", .has_arg = required_argument },
3553 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3554 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3555 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3556 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3557 { .val = 'n', .name = "name", .has_arg = required_argument },
3558 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3559 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3560 { .val = 'O', .name = "obd", .has_arg = required_argument },
3561 { .val = 'O', .name = "ost", .has_arg = required_argument },
3562 /* no short option for pool yet, can be 'p' after 2.18 */
3563 { .val = LFS_POOL_OPT,
3564 .name = "pool", .has_arg = required_argument },
3565 { .val = '0', .name = "print0", .has_arg = no_argument },
3566 { .val = 'P', .name = "print", .has_arg = no_argument },
3567 { .val = LFS_PROJID_OPT,
3568 .name = "projid", .has_arg = required_argument },
3569 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3570 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3571 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3572 { .val = 's', .name = "size", .has_arg = required_argument },
3573 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3574 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3575 { .val = 't', .name = "type", .has_arg = required_argument },
3576 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3577 { .val = 'u', .name = "uid", .has_arg = required_argument },
3578 { .val = 'U', .name = "user", .has_arg = required_argument },
3579 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3580 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3592 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3593 while ((c = getopt_long_only(argc, argv,
3594 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3595 long_opts, NULL)) >= 0) {
3600 /* '!' is part of option */
3601 /* when getopt_long_only() finds a string which is not
3602 * an option nor a known option argument it returns 1
3603 * in that case if we already have found pathstart and pathend
3604 * (i.e. we have the list of pathnames),
3605 * the only supported value is "!"
3607 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3608 if (!isoption && pathend != -1) {
3609 fprintf(stderr, "err: %s: filename|dirname must either "
3610 "precede options or follow options\n",
3615 if (!isoption && pathstart == -1)
3616 pathstart = optind - 1;
3617 if (isoption && pathstart != -1 && pathend == -1)
3618 pathend = optind - 2;
3624 /* unknown; opt is "!" or path component,
3625 * checking done above.
3627 if (strcmp(optarg, "!") == 0)
3631 xtime = ¶m.fp_atime;
3632 xsign = ¶m.fp_asign;
3633 param.fp_exclude_atime = !!neg_opt;
3634 /* no break, this falls through to 'C' for ctime */
3637 xtime = ¶m.fp_ctime;
3638 xsign = ¶m.fp_csign;
3639 param.fp_exclude_ctime = !!neg_opt;
3641 /* no break, this falls through to 'M' for mtime */
3644 xtime = ¶m.fp_mtime;
3645 xsign = ¶m.fp_msign;
3646 param.fp_exclude_mtime = !!neg_opt;
3648 rc = set_time(&t, xtime, optarg);
3649 if (rc == INT_MAX) {
3657 if (optarg[0] == '+') {
3658 param.fp_blocks_sign = -1;
3660 } else if (optarg[0] == '-') {
3661 param.fp_blocks_sign = 1;
3665 param.fp_blocks_units = 1024;
3666 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3667 ¶m.fp_blocks_units, 0);
3669 fprintf(stderr, "error: bad blocks '%s'\n",
3673 param.fp_check_blocks = 1;
3674 param.fp_exclude_blocks = !!neg_opt;
3676 case LFS_COMP_COUNT_OPT:
3677 if (optarg[0] == '+') {
3678 param.fp_comp_count_sign = -1;
3680 } else if (optarg[0] == '-') {
3681 param.fp_comp_count_sign = 1;
3685 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3686 if (*endptr != '\0') {
3687 fprintf(stderr, "error: bad component count "
3691 param.fp_check_comp_count = 1;
3692 param.fp_exclude_comp_count = !!neg_opt;
3694 case LFS_COMP_FLAGS_OPT:
3695 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3696 ¶m.fp_comp_neg_flags);
3698 fprintf(stderr, "error: bad component flags "
3702 param.fp_check_comp_flags = 1;
3704 __u32 flags = param.fp_comp_neg_flags;
3705 param.fp_comp_neg_flags = param.fp_comp_flags;
3706 param.fp_comp_flags = flags;
3709 case LFS_COMP_START_OPT:
3710 if (optarg[0] == '+') {
3711 param.fp_comp_start_sign = -1;
3713 } else if (optarg[0] == '-') {
3714 param.fp_comp_start_sign = 1;
3718 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3719 ¶m.fp_comp_start_units, 0);
3721 fprintf(stderr, "error: bad component start "
3725 param.fp_check_comp_start = 1;
3726 param.fp_exclude_comp_start = !!neg_opt;
3728 case LFS_MIRROR_STATE_OPT:
3729 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3730 ¶m.fp_mirror_neg_state);
3733 "error: bad mirrored file state '%s'\n",
3737 param.fp_check_mirror_state = 1;
3739 __u16 state = param.fp_mirror_neg_state;
3740 param.fp_mirror_neg_state =
3741 param.fp_mirror_state;
3742 param.fp_mirror_state = state;
3746 if (optarg[0] == '+') {
3747 param.fp_stripe_count_sign = -1;
3749 } else if (optarg[0] == '-') {
3750 param.fp_stripe_count_sign = 1;
3754 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3755 if (*endptr != '\0') {
3756 fprintf(stderr,"error: bad stripe_count '%s'\n",
3761 param.fp_check_stripe_count = 1;
3762 param.fp_exclude_stripe_count = !!neg_opt;
3765 param.fp_max_depth = strtol(optarg, 0, 0);
3768 if (optarg[0] == '+') {
3769 param.fp_comp_end_sign = -1;
3771 } else if (optarg[0] == '-') {
3772 param.fp_comp_end_sign = 1;
3776 if (arg_is_eof(optarg)) {
3777 param.fp_comp_end = LUSTRE_EOF;
3778 param.fp_comp_end_units = 1;
3781 rc = llapi_parse_size(optarg,
3783 ¶m.fp_comp_end_units, 0);
3786 fprintf(stderr, "error: bad component end "
3790 param.fp_check_comp_end = 1;
3791 param.fp_exclude_comp_end = !!neg_opt;
3795 rc = name2gid(¶m.fp_gid, optarg);
3797 param.fp_gid = strtoul(optarg, &endptr, 10);
3798 if (*endptr != '\0') {
3799 fprintf(stderr, "Group/GID: %s cannot "
3800 "be found.\n", optarg);
3805 param.fp_exclude_gid = !!neg_opt;
3806 param.fp_check_gid = 1;
3809 param.fp_hash_type = check_hashtype(optarg);
3810 if (param.fp_hash_type == 0) {
3811 fprintf(stderr, "error: bad hash_type '%s'\n",
3816 param.fp_check_hash_type = 1;
3817 param.fp_exclude_hash_type = !!neg_opt;
3820 ret = name2layout(¶m.fp_layout, optarg);
3823 param.fp_exclude_layout = !!neg_opt;
3824 param.fp_check_layout = 1;
3828 rc = name2uid(¶m.fp_uid, optarg);
3830 param.fp_uid = strtoul(optarg, &endptr, 10);
3831 if (*endptr != '\0') {
3832 fprintf(stderr, "User/UID: %s cannot "
3833 "be found.\n", optarg);
3838 param.fp_exclude_uid = !!neg_opt;
3839 param.fp_check_uid = 1;
3842 param.fp_pattern = (char *)optarg;
3843 param.fp_exclude_pattern = !!neg_opt;
3846 if (optarg[0] == '+') {
3847 param.fp_mirror_count_sign = -1;
3849 } else if (optarg[0] == '-') {
3850 param.fp_mirror_count_sign = 1;
3854 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3855 if (*endptr != '\0') {
3857 "error: bad mirror count '%s'\n",
3861 param.fp_check_mirror_count = 1;
3862 param.fp_exclude_mirror_count = !!neg_opt;
3867 char *buf, *token, *next, *p;
3871 buf = strdup(optarg);
3877 param.fp_exclude_obd = !!neg_opt;
3880 while (token && *token) {
3881 token = strchr(token, ',');
3888 param.fp_exclude_mdt = !!neg_opt;
3889 param.fp_num_alloc_mdts += len;
3890 tmp = realloc(param.fp_mdt_uuid,
3891 param.fp_num_alloc_mdts *
3892 sizeof(*param.fp_mdt_uuid));
3898 param.fp_mdt_uuid = tmp;
3900 param.fp_exclude_obd = !!neg_opt;
3901 param.fp_num_alloc_obds += len;
3902 tmp = realloc(param.fp_obd_uuid,
3903 param.fp_num_alloc_obds *
3904 sizeof(*param.fp_obd_uuid));
3910 param.fp_obd_uuid = tmp;
3912 for (token = buf; token && *token; token = next) {
3913 struct obd_uuid *puuid;
3916 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3919 ¶m.fp_obd_uuid[param.fp_num_obds++];
3921 p = strchr(token, ',');
3928 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3933 strncpy(puuid->uuid, token,
3934 sizeof(puuid->uuid));
3941 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3945 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3947 "Pool name %s is too long (max %d)\n",
3948 optarg, LOV_MAXPOOLNAME);
3953 * We do check for empty pool because empty pool
3954 * is used to find V1 LOV attributes
3956 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3957 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3958 param.fp_exclude_pool = !!neg_opt;
3959 param.fp_check_pool = 1;
3961 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3962 case 'p': /* want this for --pool, to match getstripe/find */
3964 "warning: -p deprecated, use --print0 or -0\n");
3967 param.fp_zero_end = 1;
3969 case 'P': /* we always print, this option is a no-op */
3971 case LFS_PROJID_OPT:
3972 rc = name2projid(¶m.fp_projid, optarg);
3974 param.fp_projid = strtoul(optarg, &endptr, 10);
3975 if (*endptr != '\0') {
3977 "Invalid project ID: %s",
3983 param.fp_exclude_projid = !!neg_opt;
3984 param.fp_check_projid = 1;
3987 if (optarg[0] == '+') {
3988 param.fp_size_sign = -1;
3990 } else if (optarg[0] == '-') {
3991 param.fp_size_sign = 1;
3995 ret = llapi_parse_size(optarg, ¶m.fp_size,
3996 ¶m.fp_size_units, 0);
3998 fprintf(stderr, "error: bad file size '%s'\n",
4002 param.fp_check_size = 1;
4003 param.fp_exclude_size = !!neg_opt;
4006 if (optarg[0] == '+') {
4007 param.fp_stripe_size_sign = -1;
4009 } else if (optarg[0] == '-') {
4010 param.fp_stripe_size_sign = 1;
4014 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4015 ¶m.fp_stripe_size_units, 0);
4017 fprintf(stderr, "error: bad stripe_size '%s'\n",
4021 param.fp_check_stripe_size = 1;
4022 param.fp_exclude_stripe_size = !!neg_opt;
4025 param.fp_exclude_type = !!neg_opt;
4026 switch (optarg[0]) {
4028 param.fp_type = S_IFBLK;
4031 param.fp_type = S_IFCHR;
4034 param.fp_type = S_IFDIR;
4037 param.fp_type = S_IFREG;
4040 param.fp_type = S_IFLNK;
4043 param.fp_type = S_IFIFO;
4046 param.fp_type = S_IFSOCK;
4049 fprintf(stderr, "error: %s: bad type '%s'\n",
4056 if (optarg[0] == '+') {
4057 param.fp_mdt_count_sign = -1;
4059 } else if (optarg[0] == '-') {
4060 param.fp_mdt_count_sign = 1;
4064 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4065 if (*endptr != '\0') {
4066 fprintf(stderr, "error: bad mdt_count '%s'\n",
4071 param.fp_check_mdt_count = 1;
4072 param.fp_exclude_mdt_count = !!neg_opt;
4080 if (pathstart == -1) {
4081 fprintf(stderr, "error: %s: no filename|pathname\n",
4085 } else if (pathend == -1) {
4091 rc = llapi_find(argv[pathstart], ¶m);
4092 if (rc != 0 && ret == 0)
4094 } while (++pathstart < pathend);
4097 fprintf(stderr, "error: %s failed for %s.\n",
4098 argv[0], argv[optind - 1]);
4100 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4101 free(param.fp_obd_uuid);
4103 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4104 free(param.fp_mdt_uuid);
4109 static int lfs_getstripe_internal(int argc, char **argv,
4110 struct find_param *param)
4112 struct option long_opts[] = {
4113 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4114 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4115 { .val = LFS_COMP_COUNT_OPT,
4116 .name = "comp-count", .has_arg = no_argument },
4117 { .val = LFS_COMP_COUNT_OPT,
4118 .name = "component-count", .has_arg = no_argument },
4119 { .val = LFS_COMP_FLAGS_OPT,
4120 .name = "comp-flags", .has_arg = optional_argument },
4121 { .val = LFS_COMP_FLAGS_OPT,
4122 .name = "component-flags", .has_arg = optional_argument },
4123 { .val = LFS_COMP_START_OPT,
4124 .name = "comp-start", .has_arg = optional_argument },
4125 { .val = LFS_COMP_START_OPT,
4126 .name = "component-start", .has_arg = optional_argument },
4127 { .val = LFS_MIRROR_INDEX_OPT,
4128 .name = "mirror-index", .has_arg = required_argument },
4129 { .val = LFS_MIRROR_ID_OPT,
4130 .name = "mirror-id", .has_arg = required_argument },
4131 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4132 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4133 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4134 { .val = 'd', .name = "directory", .has_arg = no_argument },
4135 { .val = 'D', .name = "default", .has_arg = no_argument },
4136 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4137 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4138 { .val = 'F', .name = "fid", .has_arg = no_argument },
4139 { .val = 'g', .name = "generation", .has_arg = no_argument },
4140 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4141 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4142 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4143 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4144 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4145 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4146 { .val = 'L', .name = "layout", .has_arg = no_argument },
4147 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4148 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4149 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4150 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4151 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4152 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4153 { .val = 'O', .name = "obd", .has_arg = required_argument },
4154 { .val = 'O', .name = "ost", .has_arg = required_argument },
4155 { .val = 'p', .name = "pool", .has_arg = no_argument },
4156 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4157 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4158 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4159 { .val = 'R', .name = "raw", .has_arg = no_argument },
4160 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4161 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4162 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4163 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4164 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4165 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4166 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4167 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4171 int pathstart = -1, pathend = -1;
4175 while ((c = getopt_long(argc, argv,
4176 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4177 long_opts, NULL)) != -1) {
4181 /* '!' is part of option */
4182 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4183 if (!isoption && pathend != -1) {
4185 "error: %s: filename|dirname must either precede options or follow options\n",
4189 if (!isoption && pathstart == -1)
4190 pathstart = optind - 1;
4191 if (isoption && pathstart != -1 && pathend == -1)
4192 pathend = optind - 2;
4196 /* unknown: opt is "!" */
4197 if (strcmp(optarg, "!") == 0)
4201 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4202 param->fp_verbose |= VERBOSE_COUNT;
4203 param->fp_max_depth = 0;
4206 case LFS_COMP_COUNT_OPT:
4207 param->fp_verbose |= VERBOSE_COMP_COUNT;
4208 param->fp_max_depth = 0;
4210 case LFS_COMP_FLAGS_OPT:
4211 if (optarg != NULL) {
4212 rc = comp_str2flags(optarg,
4213 ¶m->fp_comp_flags,
4214 ¶m->fp_comp_neg_flags);
4216 fprintf(stderr, "error: %s bad "
4217 "component flags '%s'.\n",
4221 param->fp_check_comp_flags = 1;
4223 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4224 param->fp_max_depth = 0;
4227 case LFS_COMP_START_OPT:
4228 if (optarg != NULL) {
4230 if (tmp[0] == '+') {
4231 param->fp_comp_start_sign = -1;
4233 } else if (tmp[0] == '-') {
4234 param->fp_comp_start_sign = 1;
4237 rc = llapi_parse_size(tmp,
4238 ¶m->fp_comp_start,
4239 ¶m->fp_comp_start_units, 0);
4241 fprintf(stderr, "error: %s bad "
4242 "component start '%s'.\n",
4246 param->fp_check_comp_start = 1;
4249 param->fp_verbose |= VERBOSE_COMP_START;
4250 param->fp_max_depth = 0;
4253 case LFS_MIRROR_INDEX_OPT:
4254 if (optarg[0] == '+') {
4255 param->fp_mirror_index_sign = -1;
4257 } else if (optarg[0] == '-') {
4258 param->fp_mirror_index_sign = 1;
4262 param->fp_mirror_index = strtoul(optarg, &end, 0);
4263 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4264 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4266 "%s %s: invalid mirror index '%s'\n",
4267 progname, argv[0], optarg);
4270 if (param->fp_mirror_id != 0) {
4272 "%s %s: can't specify both mirror index and mirror ID\n",
4276 param->fp_check_mirror_index = 1;
4277 param->fp_exclude_mirror_index = !!neg_opt;
4279 case LFS_MIRROR_ID_OPT:
4280 if (optarg[0] == '+') {
4281 param->fp_mirror_id_sign = -1;
4283 } else if (optarg[0] == '-') {
4284 param->fp_mirror_id_sign = 1;
4288 param->fp_mirror_id = strtoul(optarg, &end, 0);
4289 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4290 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4292 "%s %s: invalid mirror ID '%s'\n",
4293 progname, argv[0], optarg);
4296 if (param->fp_mirror_index != 0) {
4298 "%s %s: can't specify both mirror index and mirror ID\n",
4302 param->fp_check_mirror_id = 1;
4303 param->fp_exclude_mirror_id = !!neg_opt;
4306 param->fp_max_depth = 0;
4309 param->fp_get_default_lmv = 1;
4312 if (optarg != NULL) {
4314 if (tmp[0] == '+') {
4315 param->fp_comp_end_sign = -1;
4317 } else if (tmp[0] == '-') {
4318 param->fp_comp_end_sign = 1;
4322 if (arg_is_eof(tmp)) {
4323 param->fp_comp_end = LUSTRE_EOF;
4324 param->fp_comp_end_units = 1;
4327 rc = llapi_parse_size(tmp,
4328 ¶m->fp_comp_end,
4329 ¶m->fp_comp_end_units, 0);
4332 fprintf(stderr, "error: %s bad "
4333 "component end '%s'.\n",
4337 param->fp_check_comp_end = 1;
4339 param->fp_verbose |= VERBOSE_COMP_END;
4340 param->fp_max_depth = 0;
4344 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4345 param->fp_verbose |= VERBOSE_DFID;
4346 param->fp_max_depth = 0;
4350 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4351 param->fp_verbose |= VERBOSE_GENERATION;
4352 param->fp_max_depth = 0;
4356 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4357 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4358 param->fp_max_depth = 0;
4362 if (optarg != NULL) {
4363 param->fp_comp_id = strtoul(optarg, &end, 0);
4364 if (*end != '\0' || param->fp_comp_id == 0 ||
4365 param->fp_comp_id > LCME_ID_MAX) {
4366 fprintf(stderr, "error: %s bad "
4367 "component id '%s'\n",
4371 param->fp_check_comp_id = 1;
4374 param->fp_max_depth = 0;
4375 param->fp_verbose |= VERBOSE_COMP_ID;
4379 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4380 param->fp_verbose |= VERBOSE_PATTERN;
4381 param->fp_max_depth = 0;
4384 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4386 fprintf(stderr, "warning: '-M' deprecated"
4387 ", use '--mdt-index' or '-m' instead\n");
4390 if (!(param->fp_verbose & VERBOSE_DETAIL))
4391 param->fp_max_depth = 0;
4392 param->fp_verbose |= VERBOSE_MDTINDEX;
4395 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4396 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4397 param->fp_max_depth = 0;
4401 if (param->fp_obd_uuid) {
4403 "error: %s: only one obduuid allowed",
4407 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4410 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4411 param->fp_verbose |= VERBOSE_POOL;
4412 param->fp_max_depth = 0;
4419 param->fp_recursive = 1;
4425 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4426 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4427 param->fp_max_depth = 0;
4431 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4441 if (pathstart == -1) {
4442 fprintf(stderr, "error: %s: no filename|pathname\n",
4445 } else if (pathend == -1) {
4453 if (param->fp_recursive)
4454 param->fp_max_depth = -1;
4455 else if (param->fp_verbose & VERBOSE_DETAIL)
4456 param->fp_max_depth = 1;
4458 if (!param->fp_verbose)
4459 param->fp_verbose = VERBOSE_DEFAULT;
4460 if (param->fp_quiet)
4461 param->fp_verbose = VERBOSE_OBJID;
4464 rc = llapi_getstripe(argv[pathstart], param);
4465 } while (++pathstart < pathend && !rc);
4468 fprintf(stderr, "error: %s failed for %s.\n",
4469 argv[0], argv[optind - 1]);
4473 static int lfs_tgts(int argc, char **argv)
4475 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4476 struct find_param param;
4477 int index = 0, rc=0;
4482 if (argc == 2 && !realpath(argv[1], path)) {
4484 fprintf(stderr, "error: invalid path '%s': %s\n",
4485 argv[1], strerror(-rc));
4489 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4490 /* Check if we have a mount point */
4491 if (mntdir[0] == '\0')
4494 memset(¶m, 0, sizeof(param));
4495 if (!strcmp(argv[0], "mdts"))
4496 param.fp_get_lmv = 1;
4498 rc = llapi_ostlist(mntdir, ¶m);
4500 fprintf(stderr, "error: %s: failed on %s\n",
4503 if (path[0] != '\0')
4505 memset(mntdir, 0, PATH_MAX);
4511 static int lfs_getstripe(int argc, char **argv)
4513 struct find_param param = { 0 };
4515 param.fp_max_depth = 1;
4516 return lfs_getstripe_internal(argc, argv, ¶m);
4520 static int lfs_getdirstripe(int argc, char **argv)
4522 struct find_param param = { 0 };
4523 struct option long_opts[] = {
4524 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4525 { .val = 'D', .name = "default", .has_arg = no_argument },
4526 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4527 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4528 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4529 { .val = 'O', .name = "obd", .has_arg = required_argument },
4530 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4531 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4532 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4536 param.fp_get_lmv = 1;
4538 while ((c = getopt_long(argc, argv,
4539 "cDHimO:rtTy", long_opts, NULL)) != -1)
4544 param.fp_verbose |= VERBOSE_COUNT;
4547 param.fp_get_default_lmv = 1;
4551 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4553 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4555 fprintf(stderr, "warning: '-t' deprecated, "
4556 "use '--mdt-hash' or '-H' instead\n");
4559 param.fp_verbose |= VERBOSE_HASH_TYPE;
4562 if (param.fp_obd_uuid) {
4564 "error: %s: only one obduuid allowed",
4568 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4571 param.fp_recursive = 1;
4584 if (param.fp_recursive)
4585 param.fp_max_depth = -1;
4587 if (!param.fp_verbose)
4588 param.fp_verbose = VERBOSE_DEFAULT;
4591 rc = llapi_getstripe(argv[optind], ¶m);
4592 } while (++optind < argc && !rc);
4595 fprintf(stderr, "error: %s failed for %s.\n",
4596 argv[0], argv[optind - 1]);
4601 MNTDF_INODES = 0x0001,
4602 MNTDF_COOKED = 0x0002,
4603 MNTDF_LAZY = 0x0004,
4604 MNTDF_VERBOSE = 0x0008,
4605 MNTDF_SHOW = 0x0010,
4608 #define COOK(value) \
4611 while (value > 1024) { \
4619 #define CDF "%11llu"
4620 #define HDF "%8.1f%c"
4624 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4626 double avail, used, ratio = 0;
4629 avail = st->os_ffree;
4630 used = st->os_files - st->os_ffree;
4632 avail = st->os_bavail;
4633 used = st->os_blocks - st->os_bfree;
4635 if (avail + used > 0)
4636 ratio = used / (used + avail) * 100;
4638 /* Round up to match df(1) usage percentage */
4639 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4642 static int showdf(char *mntdir, struct obd_statfs *stat,
4643 char *uuid, enum mntdf_flags flags,
4644 char *type, int index, int rc)
4646 long long avail, used, total;
4648 char *suffix = "KMGTPEZY";
4649 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4650 char tbuf[3 * sizeof(__u64)];
4651 char ubuf[3 * sizeof(__u64)];
4652 char abuf[3 * sizeof(__u64)];
4653 char rbuf[3 * sizeof(__u64)];
4660 if (flags & MNTDF_INODES) {
4661 avail = stat->os_ffree;
4662 used = stat->os_files - stat->os_ffree;
4663 total = stat->os_files;
4665 int shift = flags & MNTDF_COOKED ? 0 : 10;
4667 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4668 used = ((stat->os_blocks - stat->os_bfree) *
4669 stat->os_bsize) >> shift;
4670 total = (stat->os_blocks * stat->os_bsize) >> shift;
4673 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4675 if (flags & MNTDF_COOKED) {
4679 cook_val = (double)total;
4682 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4685 snprintf(tbuf, sizeof(tbuf), CDF, total);
4687 cook_val = (double)used;
4690 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4693 snprintf(ubuf, sizeof(ubuf), CDF, used);
4695 cook_val = (double)avail;
4698 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4701 snprintf(abuf, sizeof(abuf), CDF, avail);
4703 snprintf(tbuf, sizeof(tbuf), CDF, total);
4704 snprintf(ubuf, sizeof(tbuf), CDF, used);
4705 snprintf(abuf, sizeof(tbuf), CDF, avail);
4708 sprintf(rbuf, RDF, ratio);
4709 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4710 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4712 printf("[%s:%d]", type, index);
4714 if (stat->os_state) {
4716 * Each character represents the matching
4719 const char state_names[] = "DRSI";
4724 for (i = 0, state = stat->os_state;
4725 state && i < sizeof(state_names); i++) {
4726 if (!(state & (1 << i)))
4728 printf("%c", state_names[i]);
4736 printf(UUF": inactive device\n", uuid);
4739 printf(UUF": %s\n", uuid, strerror(-rc));
4746 struct ll_stat_type {
4751 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4753 struct ll_statfs_data {
4755 struct obd_statfs sd_st;
4758 struct ll_statfs_buf {
4760 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4763 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4764 int ops, struct ll_statfs_buf *lsb)
4766 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4767 struct obd_uuid uuid_buf;
4768 char *poolname = NULL;
4769 struct ll_stat_type types[] = {
4770 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4771 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4772 { .st_name = NULL } };
4773 struct ll_stat_type *tp;
4774 __u64 ost_ffree = 0;
4782 poolname = strchr(pool, '.');
4783 if (poolname != NULL) {
4784 if (strncmp(fsname, pool, strlen(fsname))) {
4785 fprintf(stderr, "filesystem name incorrect\n");
4793 fd = open(mntdir, O_RDONLY);
4796 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4801 if (flags & MNTDF_SHOW) {
4802 if (flags & MNTDF_INODES)
4803 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4804 "UUID", "Inodes", "IUsed", "IFree",
4805 "IUse%", "Mounted on");
4807 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4809 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4810 "Used", "Available", "Use%", "Mounted on");
4813 for (tp = types; tp->st_name != NULL; tp++) {
4814 bool have_ost = false;
4816 if (!(tp->st_op & ops))
4819 for (index = 0; ; index++) {
4820 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4821 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4822 type = flags & MNTDF_LAZY ?
4823 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4824 rc2 = llapi_obd_fstatfs(fd, type, index,
4825 &stat_buf, &uuid_buf);
4830 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4831 if (!(flags & MNTDF_VERBOSE))
4833 } else if (rc2 < 0 && rc == 0) {
4837 /* If we have OSTs then don't report MDT block counts.
4838 * For MDT-only filesystems the expectation is that all
4839 * layouts have a DoM component. For filesystems with
4840 * OSTs, files are not necessarily going to store data
4841 * on MDTs, and MDT space is limited to a fraction of
4842 * OST space, so don't include it in the summary.
4844 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
4851 if (poolname && tp->st_op == LL_STATFS_LOV &&
4852 llapi_search_ost(fsname, poolname,
4853 obd_uuid2str(&uuid_buf)) != 1)
4856 /* the llapi_obd_statfs() call may have returned with
4857 * an error, but if it filled in uuid_buf we will at
4858 * lease use that to print out a message for that OBD.
4859 * If we didn't get anything in the uuid_buf, then fill
4860 * it in so that we can print an error message. */
4861 if (uuid_buf.uuid[0] == '\0')
4862 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4863 "%s%04x", tp->st_name, index);
4865 lsb->sb_buf[lsb->sb_count].sd_index = index;
4866 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4869 if (flags & MNTDF_SHOW)
4870 showdf(mntdir, &stat_buf,
4871 obd_uuid2str(&uuid_buf), flags,
4872 tp->st_name, index, rc2);
4877 if (tp->st_op == LL_STATFS_LMV) {
4878 sum.os_ffree += stat_buf.os_ffree;
4879 sum.os_files += stat_buf.os_files;
4880 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4881 ost_ffree += stat_buf.os_ffree;
4883 sum.os_blocks += stat_buf.os_blocks *
4885 sum.os_bfree += stat_buf.os_bfree *
4887 sum.os_bavail += stat_buf.os_bavail *
4894 /* If we don't have as many objects free on the OST as inodes
4895 * on the MDS, we reduce the total number of inodes to
4896 * compensate, so that the "inodes in use" number is correct.
4897 * Matches ll_statfs_internal() so the results are consistent. */
4898 if (ost_ffree < sum.os_ffree) {
4899 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4900 sum.os_ffree = ost_ffree;
4902 if (flags & MNTDF_SHOW) {
4904 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4911 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4913 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4915 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4917 int r1 = obd_statfs_ratio(st1, false);
4918 int r2 = obd_statfs_ratio(st2, false);
4919 int64_t result = r1 - r2;
4921 /* if both space usage are above 90, compare free inodes */
4922 if (r1 > 90 && r2 > 90)
4923 result = st2->os_ffree - st1->os_ffree;
4927 else if (result == 0)
4934 static int lfs_setdirstripe(int argc, char **argv)
4938 struct lfs_setstripe_args lsa = { 0 };
4939 struct llapi_stripe_param *param = NULL;
4940 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4943 char *mode_opt = NULL;
4944 bool default_stripe = false;
4945 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4946 mode_t previous_mode = 0;
4947 bool delete = false;
4948 struct ll_statfs_buf *lsb = NULL;
4949 char mntdir[PATH_MAX] = "";
4950 bool auto_distributed = false;
4952 struct option long_opts[] = {
4953 { .val = 'c', .name = "count", .has_arg = required_argument },
4954 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4955 { .val = 'd', .name = "delete", .has_arg = no_argument },
4956 { .val = 'D', .name = "default", .has_arg = no_argument },
4957 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4958 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4959 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4960 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4961 { .val = 'i', .name = "index", .has_arg = required_argument },
4963 { .val = 'o', .name = "mode", .has_arg = required_argument },
4964 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4965 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4967 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4968 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4971 setstripe_args_init(&lsa);
4973 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4981 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4984 "%s %s: invalid stripe count '%s'\n",
4985 progname, argv[0], optarg);
4991 default_stripe = true;
4994 default_stripe = true;
4996 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4998 fprintf(stderr, "warning: '--hash-type' and '-t' "
4999 "deprecated, use '--mdt-hash' or '-H' instead\n");
5002 lsa.lsa_pattern = check_hashtype(optarg);
5003 if (lsa.lsa_pattern == 0) {
5005 "%s %s: bad stripe hash type '%s'\n",
5006 progname, argv[0], optarg);
5011 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5012 if (strcmp(argv[optind - 1], "--index") == 0)
5014 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5017 lsa.lsa_nr_tgts = parse_targets(mdts,
5018 sizeof(mdts) / sizeof(__u32),
5019 lsa.lsa_nr_tgts, optarg);
5020 if (lsa.lsa_nr_tgts < 0) {
5022 "%s %s: invalid MDT target(s) '%s'\n",
5023 progname, argv[0], optarg);
5027 lsa.lsa_tgts = mdts;
5028 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5029 lsa.lsa_stripe_off = mdts[0];
5031 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
5033 fprintf(stderr, "warning: '-m' is deprecated, "
5034 "use '--mode' or '-o' instead\n");
5040 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5041 progname, argv[0], argv[optind - 1]);
5046 if (optind == argc) {
5047 fprintf(stderr, "%s %s: DIR must be specified\n",
5052 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5053 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5055 "%s %s: stripe offset and count must be specified\n",
5061 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5062 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5064 "%s %s: cannot specify -d with -c or -i options\n",
5069 if (mode_opt != NULL) {
5070 mode = strtoul(mode_opt, &end, 8);
5073 "%s %s: bad MODE '%s'\n",
5074 progname, argv[0], mode_opt);
5077 previous_mode = umask(0);
5081 * initialize stripe parameters, in case param is converted to specific,
5082 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5084 param = calloc(1, offsetof(typeof(*param),
5085 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5086 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5087 if (param == NULL) {
5089 "%s %s: cannot allocate memory for parameters: %s\n",
5090 progname, argv[0], strerror(ENOMEM));
5094 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5095 param->lsp_stripe_count = lsa.lsa_stripe_count;
5096 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5097 param->lsp_stripe_offset = -1;
5099 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5100 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5101 param->lsp_stripe_pattern = lsa.lsa_pattern;
5103 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5104 param->lsp_pool = lsa.lsa_pool_name;
5105 param->lsp_is_specific = false;
5106 if (lsa.lsa_nr_tgts > 1) {
5107 if (lsa.lsa_stripe_count > 0 &&
5108 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5109 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5110 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5111 "match the number of MDTs: %d\n",
5112 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5117 param->lsp_is_specific = true;
5118 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5119 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5122 dname = argv[optind];
5124 if (default_stripe) {
5125 result = llapi_dir_set_default_lmv(dname, param);
5127 /* if current \a dname isn't under the same \a mntdir
5128 * as the last one, and the last one was
5129 * auto-distributed, restore \a param.
5131 if (mntdir[0] != '\0' &&
5132 strncmp(dname, mntdir, strlen(mntdir)) &&
5134 param->lsp_is_specific = false;
5135 param->lsp_stripe_offset = -1;
5136 auto_distributed = false;
5139 if (!param->lsp_is_specific &&
5140 param->lsp_stripe_offset == -1) {
5141 char path[PATH_MAX] = "";
5144 lsb = malloc(sizeof(*lsb));
5152 /* use mntdir for dirname() temporarily */
5153 strncpy(mntdir, dname, sizeof(mntdir));
5154 if (!realpath(dirname(mntdir), path)) {
5157 "error: invalid path '%s': %s\n",
5158 argv[optind], strerror(errno));
5163 result = llapi_search_mounts(path, 0, mntdir,
5165 if (result < 0 || mntdir[0] == '\0') {
5167 "No suitable Lustre mount found\n");
5171 result = mntdf(mntdir, NULL, NULL, 0,
5172 LL_STATFS_LMV, lsb);
5176 if (param->lsp_stripe_count > lsb->sb_count) {
5178 "error: stripe count %d is too big\n",
5179 param->lsp_stripe_count);
5184 qsort(lsb->sb_buf, lsb->sb_count,
5185 sizeof(struct ll_statfs_data),
5186 ll_statfs_data_comp);
5188 auto_distributed = true;
5191 if (auto_distributed) {
5193 int nr = MAX(param->lsp_stripe_count,
5196 /* don't use server whose usage is above 90% */
5197 while (nr != param->lsp_stripe_count &&
5198 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5200 nr = MAX(param->lsp_stripe_count,
5203 /* get \a r between [0, nr) */
5206 param->lsp_stripe_offset =
5207 lsb->sb_buf[r].sd_index;
5208 if (param->lsp_stripe_count > 1) {
5211 param->lsp_is_specific = true;
5212 for (; i < param->lsp_stripe_count; i++)
5213 param->lsp_tgts[(i + r) % nr] =
5214 lsb->sb_buf[i].sd_index;
5218 result = llapi_dir_create(dname, mode, param);
5223 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5224 progname, dname, strerror(-result));
5227 dname = argv[++optind];
5228 } while (dname != NULL);
5230 if (mode_opt != NULL)
5231 umask(previous_mode);
5239 static int lfs_rmentry(int argc, char **argv)
5246 fprintf(stderr, "error: %s: missing dirname\n",
5252 dname = argv[index];
5253 while (dname != NULL) {
5254 result = llapi_direntry_remove(dname);
5256 fprintf(stderr, "error: %s: remove dir entry '%s' "
5257 "failed\n", argv[0], dname);
5260 dname = argv[++index];
5265 static int lfs_mv(int argc, char **argv)
5267 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5268 struct find_param param = {
5275 struct option long_opts[] = {
5276 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5277 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5280 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5282 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5284 fprintf(stderr, "warning: '-M' deprecated"
5285 ", use '--mdt-index' or '-m' instead\n");
5288 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5290 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5296 param.fp_verbose = VERBOSE_DETAIL;
5299 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5300 progname, argv[optind - 1]);
5305 if (lmu.lum_stripe_offset == -1) {
5306 fprintf(stderr, "%s mv: MDT index must be specified\n",
5311 if (optind >= argc) {
5312 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5317 /* initialize migrate mdt parameters */
5318 param.fp_lmv_md = &lmu;
5319 param.fp_migrate = 1;
5320 rc = llapi_migrate_mdt(argv[optind], ¶m);
5322 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5323 progname, argv[optind], param.fp_mdt_index,
5328 static int lfs_osts(int argc, char **argv)
5330 return lfs_tgts(argc, argv);
5333 static int lfs_mdts(int argc, char **argv)
5335 return lfs_tgts(argc, argv);
5338 static int lfs_df(int argc, char **argv)
5340 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5341 enum mntdf_flags flags = MNTDF_SHOW;
5342 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5343 int c, rc = 0, index = 0;
5344 char fsname[PATH_MAX] = "", *pool_name = NULL;
5345 struct option long_opts[] = {
5346 { .val = 'h', .name = "human-readable",
5347 .has_arg = no_argument },
5348 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5349 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5350 { .val = 'p', .name = "pool", .has_arg = required_argument },
5351 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5354 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5357 flags |= MNTDF_COOKED;
5360 flags |= MNTDF_INODES;
5363 flags |= MNTDF_LAZY;
5369 flags |= MNTDF_VERBOSE;
5375 if (optind < argc && !realpath(argv[optind], path)) {
5377 fprintf(stderr, "error: invalid path '%s': %s\n",
5378 argv[optind], strerror(-rc));
5382 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5383 /* Check if we have a mount point */
5384 if (mntdir[0] == '\0')
5387 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5388 if (rc || path[0] != '\0')
5390 fsname[0] = '\0'; /* avoid matching in next loop */
5391 mntdir[0] = '\0'; /* avoid matching in next loop */
5397 static int lfs_getname(int argc, char **argv)
5399 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5400 int rc = 0, index = 0, c;
5401 char buf[sizeof(struct obd_uuid)];
5403 while ((c = getopt(argc, argv, "h")) != -1)
5406 if (optind == argc) { /* no paths specified, get all paths. */
5407 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5408 rc = llapi_getname(mntdir, buf, sizeof(buf));
5411 "cannot get name for `%s': %s\n",
5412 mntdir, strerror(-rc));
5416 printf("%s %s\n", buf, mntdir);
5418 path[0] = fsname[0] = mntdir[0] = 0;
5420 } else { /* paths specified, only attempt to search these. */
5421 for (; optind < argc; optind++) {
5422 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5425 "cannot get name for `%s': %s\n",
5426 argv[optind], strerror(-rc));
5430 printf("%s %s\n", buf, argv[optind]);
5436 static int lfs_check(int argc, char **argv)
5438 char mntdir[PATH_MAX] = {'\0'};
5447 fprintf(stderr, "%s check: server type must be specified\n",
5452 obd_types[0] = obd_type1;
5453 obd_types[1] = obd_type2;
5454 obd_types[2] = obd_type3;
5456 if (strcmp(argv[1], "osts") == 0) {
5457 strcpy(obd_types[0], "osc");
5458 } else if (strcmp(argv[1], "mdts") == 0 ||
5459 strcmp(argv[1], "mds") == 0) {
5460 strcpy(obd_types[0], "mdc");
5461 } else if (strcmp(argv[1], "mgts") == 0) {
5462 strcpy(obd_types[0], "mgc");
5463 } else if (strcmp(argv[1], "all") == 0 ||
5464 strcmp(argv[1], "servers") == 0) {
5466 strcpy(obd_types[0], "osc");
5467 strcpy(obd_types[1], "mdc");
5468 strcpy(obd_types[2], "mgc");
5470 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5475 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5476 if (rc < 0 || mntdir[0] == '\0') {
5478 "%s check: cannot find mounted Lustre filesystem: %s\n",
5479 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5483 rc = llapi_target_check(num_types, obd_types, mntdir);
5485 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5486 progname, argv[1], strerror(-rc));
5492 #ifdef HAVE_SYS_QUOTA_H
5493 #define ARG2INT(nr, str, msg) \
5496 nr = strtol(str, &endp, 0); \
5497 if (*endp != '\0') { \
5498 fprintf(stderr, "%s: bad %s '%s'\n", \
5499 progname, msg, str); \
5504 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5506 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5507 * returns the value or ULONG_MAX on integer overflow or incorrect format
5509 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5510 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5511 * 3. empty integer value is interpreted as 0
5513 static unsigned long str2sec(const char* timestr)
5515 const char spec[] = "smhdw";
5516 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5517 unsigned long val = 0;
5520 if (strpbrk(timestr, spec) == NULL) {
5521 /* no specifiers inside the time string,
5522 should treat it as an integer value */
5523 val = strtoul(timestr, &tail, 10);
5524 return *tail ? ULONG_MAX : val;
5527 /* format string is XXwXXdXXhXXmXXs */
5533 v = strtoul(timestr, &tail, 10);
5534 if (v == ULONG_MAX || *tail == '\0')
5535 /* value too large (ULONG_MAX or more)
5536 or missing specifier */
5539 ptr = strchr(spec, *tail);
5541 /* unknown specifier */
5546 /* check if product will overflow the type */
5547 if (!(v < ULONG_MAX / mult[ind]))
5550 ADD_OVERFLOW(val, mult[ind] * v);
5551 if (val == ULONG_MAX)
5563 #define ARG2ULL(nr, str, def_units) \
5565 unsigned long long limit, units = def_units; \
5568 rc = llapi_parse_size(str, &limit, &units, 1); \
5570 fprintf(stderr, "%s: invalid limit '%s'\n", \
5577 static inline int has_times_option(int argc, char **argv)
5581 for (i = 1; i < argc; i++)
5582 if (!strcmp(argv[i], "-t"))
5588 int lfs_setquota_times(int argc, char **argv)
5591 struct if_quotactl qctl;
5592 char *mnt, *obd_type = (char *)qctl.obd_type;
5593 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5594 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5595 struct option long_opts[] = {
5596 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5597 { .val = 'g', .name = "group", .has_arg = no_argument },
5598 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5599 { .val = 'p', .name = "projid", .has_arg = no_argument },
5600 { .val = 't', .name = "times", .has_arg = no_argument },
5601 { .val = 'u', .name = "user", .has_arg = no_argument },
5605 memset(&qctl, 0, sizeof(qctl));
5606 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5607 qctl.qc_type = ALLQUOTA;
5609 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5610 long_opts, NULL)) != -1) {
5621 if (qctl.qc_type != ALLQUOTA) {
5622 fprintf(stderr, "error: -u/g/p can't be used "
5623 "more than once\n");
5626 qctl.qc_type = qtype;
5629 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5630 fprintf(stderr, "error: bad block-grace: %s\n",
5634 dqb->dqb_valid |= QIF_BTIME;
5637 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5638 fprintf(stderr, "error: bad inode-grace: %s\n",
5642 dqb->dqb_valid |= QIF_ITIME;
5644 case 't': /* Yes, of course! */
5646 default: /* getopt prints error message for us when opterr != 0 */
5651 if (qctl.qc_type == ALLQUOTA) {
5652 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5656 if (optind != argc - 1) {
5657 fprintf(stderr, "error: unexpected parameters encountered\n");
5661 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5662 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5663 fprintf(stderr, "error: grace time is too large\n");
5668 rc = llapi_quotactl(mnt, &qctl);
5671 fprintf(stderr, "%s %s ", obd_type,
5672 obd_uuid2str(&qctl.obd_uuid));
5673 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5680 #define BSLIMIT (1 << 0)
5681 #define BHLIMIT (1 << 1)
5682 #define ISLIMIT (1 << 2)
5683 #define IHLIMIT (1 << 3)
5685 int lfs_setquota(int argc, char **argv)
5688 struct if_quotactl qctl;
5689 char *mnt, *obd_type = (char *)qctl.obd_type;
5690 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5691 struct option long_opts[] = {
5692 { .val = 'b', .name = "block-softlimit",
5693 .has_arg = required_argument },
5694 { .val = 'B', .name = "block-hardlimit",
5695 .has_arg = required_argument },
5696 { .val = 'd', .name = "default", .has_arg = no_argument },
5697 { .val = 'g', .name = "group", .has_arg = required_argument },
5698 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5699 { .val = 'i', .name = "inode-softlimit",
5700 .has_arg = required_argument },
5701 { .val = 'I', .name = "inode-hardlimit",
5702 .has_arg = required_argument },
5703 { .val = 'p', .name = "projid", .has_arg = required_argument },
5704 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5705 { .val = 'u', .name = "user", .has_arg = required_argument },
5706 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5708 unsigned limit_mask = 0;
5710 bool use_default = false;
5713 if (has_times_option(argc, argv))
5714 return lfs_setquota_times(argc, argv);
5716 memset(&qctl, 0, sizeof(qctl));
5717 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5718 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5719 * so it can be used as a marker that qc_type
5720 * isn't reinitialized from command line */
5722 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5723 long_opts, NULL)) != -1) {
5726 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5729 goto quota_type_def;
5732 rc = name2uid(&qctl.qc_id, optarg);
5735 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5738 goto quota_type_def;
5741 rc = name2gid(&qctl.qc_id, optarg);
5744 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5747 goto quota_type_def;
5750 rc = name2projid(&qctl.qc_id, optarg);
5753 qctl.qc_id = strtoul(optarg, &endptr, 10);
5754 if (*endptr != '\0') {
5755 fprintf(stderr, "%s setquota: invalid"
5756 " id '%s'\n", progname, optarg);
5761 if (qctl.qc_id == 0) {
5762 fprintf(stderr, "%s setquota: can't set quota"
5763 " for root usr/group/project.\n",
5769 if (qctl.qc_type != ALLQUOTA) {
5771 "%s setquota: only one of -u, -U, -g,"
5772 " -G, -p or -P may be specified\n",
5776 qctl.qc_type = qtype;
5779 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5783 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5784 dqb->dqb_bsoftlimit >>= 10;
5785 limit_mask |= BSLIMIT;
5786 if (dqb->dqb_bsoftlimit &&
5787 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5789 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5790 "See '%s help setquota' or Lustre manual for details\n",
5791 progname, dqb->dqb_bsoftlimit,
5795 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5796 dqb->dqb_bhardlimit >>= 10;
5797 limit_mask |= BHLIMIT;
5798 if (dqb->dqb_bhardlimit &&
5799 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5801 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5802 "See '%s help setquota' or Lustre manual for details\n",
5803 progname, dqb->dqb_bhardlimit,
5807 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5808 limit_mask |= ISLIMIT;
5809 if (dqb->dqb_isoftlimit &&
5810 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5812 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5813 "See '%s help setquota' or Lustre manual for details\n",
5814 progname, dqb->dqb_isoftlimit,
5818 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5819 limit_mask |= IHLIMIT;
5820 if (dqb->dqb_ihardlimit &&
5821 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5823 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5824 "See '%s help setquota' or Lustre manual for details\n",
5825 progname, dqb->dqb_ihardlimit,
5830 "%s setquota: unrecognized option '%s'\n",
5831 progname, argv[optind - 1]);
5836 if (qctl.qc_type == ALLQUOTA) {
5838 "%s setquota: either -u or -g must be specified\n",
5843 if (!use_default && limit_mask == 0) {
5845 "%s setquota: at least one limit must be specified\n",
5850 if (use_default && limit_mask != 0) {
5852 "%s setquota: limits should not be specified when"
5853 " using default quota\n",
5858 if (use_default && qctl.qc_id == 0) {
5860 "%s setquota: can not set default quota for root"
5861 " user/group/project\n",
5866 if (optind != argc - 1) {
5868 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5869 progname, argv[optind]);
5876 dqb->dqb_bhardlimit = 0;
5877 dqb->dqb_bsoftlimit = 0;
5878 dqb->dqb_ihardlimit = 0;
5879 dqb->dqb_isoftlimit = 0;
5882 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5883 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5884 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5885 /* sigh, we can't just set blimits/ilimits */
5886 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5887 .qc_type = qctl.qc_type,
5888 .qc_id = qctl.qc_id};
5890 rc = llapi_quotactl(mnt, &tmp_qctl);
5894 if (!(limit_mask & BHLIMIT))
5895 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5896 if (!(limit_mask & BSLIMIT))
5897 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5898 if (!(limit_mask & IHLIMIT))
5899 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5900 if (!(limit_mask & ISLIMIT))
5901 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5903 /* Keep grace times if we have got no softlimit arguments */
5904 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5905 dqb->dqb_valid |= QIF_BTIME;
5906 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5909 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5910 dqb->dqb_valid |= QIF_ITIME;
5911 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5915 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5916 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5918 rc = llapi_quotactl(mnt, &qctl);
5922 "%s setquota: cannot quotactl '%s' '%s': %s",
5924 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5931 /* Converts seconds value into format string
5932 * result is returned in buf
5934 * 1. result is in descenting order: 1w2d3h4m5s
5935 * 2. zero fields are not filled (except for p. 3): 5d1s
5936 * 3. zero seconds value is presented as "0s"
5938 static char * __sec2str(time_t seconds, char *buf)
5940 const char spec[] = "smhdw";
5941 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5946 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5947 c = seconds / mult[i];
5949 if (c > 0 || (i == 0 && buf == tail))
5950 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5958 static void sec2str(time_t seconds, char *buf, int rc)
5965 tail = __sec2str(seconds, tail);
5967 if (rc && tail - buf < 39) {
5973 static void diff2str(time_t seconds, char *buf, time_t now)
5979 if (seconds <= now) {
5980 strcpy(buf, "none");
5983 __sec2str(seconds - now, buf);
5986 static void print_quota_title(char *name, struct if_quotactl *qctl,
5987 bool human_readable, bool show_default)
5990 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
5991 printf("%15s %8s%8s%8s %8s%8s%8s\n",
5992 "Filesystem", "bquota", "blimit", "bgrace",
5993 "iquota", "ilimit", "igrace");
5995 printf("Disk quotas for %s %s (%cid %u):\n",
5996 qtype_name(qctl->qc_type), name,
5997 *qtype_name(qctl->qc_type), qctl->qc_id);
5998 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5999 "Filesystem", human_readable ? "used" : "kbytes",
6000 "quota", "limit", "grace",
6001 "files", "quota", "limit", "grace");
6005 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6008 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6011 snprintf(buf, buflen, "%5.4gP",
6012 (double)num / ((__u64)1 << 40));
6014 snprintf(buf, buflen, "%5.4gT",
6015 (double)num / (1 << 30));
6017 snprintf(buf, buflen, "%5.4gG",
6018 (double)num / (1 << 20));
6020 snprintf(buf, buflen, "%5.4gM",
6021 (double)num / (1 << 10));
6023 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6027 #define STRBUF_LEN 32
6028 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6029 int rc, bool h, bool show_default)
6035 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6036 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6037 int bover = 0, iover = 0;
6038 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6039 char numbuf[3][STRBUF_LEN];
6041 char strbuf[STRBUF_LEN];
6043 if (dqb->dqb_bhardlimit &&
6044 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6046 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6047 if (dqb->dqb_btime > now) {
6054 if (dqb->dqb_ihardlimit &&
6055 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6057 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6058 if (dqb->dqb_itime > now) {
6066 if (strlen(mnt) > 15)
6067 printf("%s\n%15s", mnt, "");
6069 printf("%15s", mnt);
6072 diff2str(dqb->dqb_btime, timebuf, now);
6073 else if (show_default)
6074 snprintf(timebuf, sizeof(timebuf), "%llu",
6077 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6078 strbuf, sizeof(strbuf), h);
6079 if (rc == -EREMOTEIO)
6080 sprintf(numbuf[0], "%s*", strbuf);
6082 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6083 "%s" : "[%s]", strbuf);
6085 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6086 if (type == QC_GENERAL)
6087 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6088 "%s" : "[%s]", strbuf);
6090 sprintf(numbuf[1], "%s", "-");
6092 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6093 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6094 "%s" : "[%s]", strbuf);
6097 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6099 printf(" %7s%c %6s %7s %7s",
6100 numbuf[0], bover ? '*' : ' ', numbuf[1],
6101 numbuf[2], bover > 1 ? timebuf : "-");
6105 diff2str(dqb->dqb_itime, timebuf, now);
6106 else if (show_default)
6107 snprintf(timebuf, sizeof(timebuf), "%llu",
6110 snprintf(numbuf[0], sizeof(numbuf),
6111 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6112 (uintmax_t)dqb->dqb_curinodes);
6114 if (type == QC_GENERAL)
6115 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6117 (uintmax_t)dqb->dqb_isoftlimit);
6119 sprintf(numbuf[1], "%s", "-");
6121 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6122 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6125 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6126 else if (type != QC_OSTIDX)
6127 printf(" %7s%c %6s %7s %7s",
6128 numbuf[0], iover ? '*' : ' ', numbuf[1],
6129 numbuf[2], iover > 1 ? timebuf : "-");
6131 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6134 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6135 qctl->qc_cmd == Q_GETOINFO) {
6139 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6140 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6141 printf("Block grace time: %s; Inode grace time: %s\n",
6142 bgtimebuf, igtimebuf);
6146 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6147 bool h, __u64 *total)
6149 int rc = 0, rc1 = 0, count = 0;
6150 __u32 valid = qctl->qc_valid;
6152 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6154 fprintf(stderr, "can not get %s count: %s\n",
6155 is_mdt ? "mdt": "ost", strerror(-rc));
6159 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6160 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6161 rc = llapi_quotactl(mnt, qctl);
6163 /* It is remote client case. */
6164 if (rc == -EOPNOTSUPP) {
6171 fprintf(stderr, "quotactl %s%d failed.\n",
6172 is_mdt ? "mdt": "ost", qctl->qc_idx);
6176 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6177 qctl->qc_valid, 0, h, false);
6178 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6179 qctl->qc_dqblk.dqb_bhardlimit;
6182 qctl->qc_valid = valid;
6186 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6187 int verbose, int quiet, bool human_readable,
6190 int rc1 = 0, rc2 = 0, rc3 = 0;
6191 char *obd_type = (char *)qctl->obd_type;
6192 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6193 __u64 total_ialloc = 0, total_balloc = 0;
6194 bool use_default_for_blk = false;
6195 bool use_default_for_file = false;
6198 rc1 = llapi_quotactl(mnt, qctl);
6202 fprintf(stderr, "%s quotas are not enabled.\n",
6203 qtype_name(qctl->qc_type));
6206 fprintf(stderr, "Permission denied.\n");
6209 /* We already got error message. */
6212 fprintf(stderr, "Unexpected quotactl error: %s\n",
6217 if (!show_default && qctl->qc_id == 0) {
6218 qctl->qc_dqblk.dqb_bhardlimit = 0;
6219 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6220 qctl->qc_dqblk.dqb_ihardlimit = 0;
6221 qctl->qc_dqblk.dqb_isoftlimit = 0;
6222 qctl->qc_dqblk.dqb_btime = 0;
6223 qctl->qc_dqblk.dqb_itime = 0;
6224 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6227 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6228 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6229 use_default_for_blk = true;
6230 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6233 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6234 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6235 use_default_for_file = true;
6236 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6239 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6240 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6241 print_quota_title(name, qctl, human_readable, show_default);
6243 if (rc1 && *obd_type)
6244 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6246 if (qctl->qc_valid != QC_GENERAL)
6249 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6250 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6251 (QIF_LIMITS|QIF_USAGE));
6253 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6255 if (!show_default && verbose &&
6256 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6257 char strbuf[STRBUF_LEN];
6259 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6261 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6263 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6265 printf("Total allocated inode limit: %ju, total "
6266 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6270 if (use_default_for_blk)
6271 printf("%cid %u is using default block quota setting\n",
6272 *qtype_name(qctl->qc_type), qctl->qc_id);
6274 if (use_default_for_file)
6275 printf("%cid %u is using default file quota setting\n",
6276 *qtype_name(qctl->qc_type), qctl->qc_id);
6278 if (rc1 || rc2 || rc3 || inacc)
6279 printf("Some errors happened when getting quota info. "
6280 "Some devices may be not working or deactivated. "
6281 "The data in \"[]\" is inaccurate.\n");
6287 static int lfs_project(int argc, char **argv)
6289 int ret = 0, err = 0, c, i;
6290 struct project_handle_control phc = { 0 };
6291 enum lfs_project_ops_t op;
6294 phc.assign_projid = false;
6295 /* default action */
6296 op = LFS_PROJECT_LIST;
6298 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6301 if (op != LFS_PROJECT_LIST) {
6303 "%s: cannot specify '-c' '-C' '-s' together\n",
6308 op = LFS_PROJECT_CHECK;
6311 if (op != LFS_PROJECT_LIST) {
6313 "%s: cannot specify '-c' '-C' '-s' together\n",
6318 op = LFS_PROJECT_CLEAR;
6321 if (op != LFS_PROJECT_LIST) {
6323 "%s: cannot specify '-c' '-C' '-s' together\n",
6328 phc.set_inherit = true;
6329 op = LFS_PROJECT_SET;
6335 phc.keep_projid = true;
6338 phc.recursive = true;
6341 phc.projid = strtoul(optarg, NULL, 0);
6342 phc.assign_projid = true;
6346 phc.newline = false;
6349 fprintf(stderr, "%s: invalid option '%c'\n",
6355 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6356 op = LFS_PROJECT_SET;
6357 phc.set_projid = true;
6358 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6359 phc.set_projid = true;
6363 case LFS_PROJECT_CHECK:
6364 if (phc.keep_projid) {
6366 "%s: '-k' is useless together with '-c'\n",
6371 case LFS_PROJECT_CLEAR:
6374 "%s: '-0' is useless together with '-C'\n",
6378 if (phc.assign_projid) {
6380 "%s: '-p' is useless together with '-C'\n",
6385 case LFS_PROJECT_SET:
6388 "%s: '-0' is useless together with '-s'\n",
6392 if (phc.keep_projid) {
6394 "%s: '-k' is useless together with '-s'\n",
6402 "%s: '-0' is useless for list operations\n",
6412 fprintf(stderr, "%s: missing file or directory target(s)\n",
6417 for (i = 0; i < argc; i++) {
6419 case LFS_PROJECT_CHECK:
6420 err = lfs_project_check(argv[i], &phc);
6422 case LFS_PROJECT_LIST:
6423 err = lfs_project_list(argv[i], &phc);
6425 case LFS_PROJECT_CLEAR:
6426 err = lfs_project_clear(argv[i], &phc);
6428 case LFS_PROJECT_SET:
6429 err = lfs_project_set(argv[i], &phc);
6441 static int lfs_quota(int argc, char **argv)
6444 char *mnt, *name = NULL;
6445 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6446 .qc_type = ALLQUOTA };
6447 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6448 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6450 __u32 valid = QC_GENERAL, idx = 0;
6451 bool human_readable = false;
6452 bool show_default = false;
6455 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6458 show_default = true;
6463 show_default = true;
6468 show_default = true;
6472 if (qctl.qc_type != ALLQUOTA) {
6474 "%s quota: only one of -u, -g, or -p may be specified\n",
6478 qctl.qc_type = qtype;
6481 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6484 valid = qctl.qc_valid = QC_UUID;
6485 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6488 valid = qctl.qc_valid = QC_MDTIDX;
6489 idx = qctl.qc_idx = atoi(optarg);
6490 if (idx == 0 && *optarg != '0') {
6492 "%s quota: invalid MDT index '%s'\n",
6498 valid = qctl.qc_valid = QC_OSTIDX;
6499 idx = qctl.qc_idx = atoi(optarg);
6500 if (idx == 0 && *optarg != '0') {
6502 "%s quota: invalid OST index '%s'\n",
6514 human_readable = true;
6517 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6518 progname, argv[optind - 1]);
6523 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6524 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6525 optind == argc - 1 && !show_default) {
6527 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6528 qctl.qc_valid = valid;
6531 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6532 qctl.qc_type = qtype;
6533 if (qtype == USRQUOTA) {
6534 qctl.qc_id = geteuid();
6535 rc = uid2name(&name, qctl.qc_id);
6537 qctl.qc_id = getegid();
6538 rc = gid2name(&name, qctl.qc_id);
6543 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6544 human_readable, show_default);
6549 /* lfs quota -u username /path/to/lustre/mount */
6550 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6551 /* options should be followed by u/g-name and mntpoint */
6552 if ((!show_default && optind + 2 != argc) ||
6553 (show_default && optind + 1 != argc) ||
6554 qctl.qc_type == ALLQUOTA) {
6556 "%s quota: name and mount point must be specified\n",
6561 if (!show_default) {
6562 name = argv[optind++];
6563 switch (qctl.qc_type) {
6565 rc = name2uid(&qctl.qc_id, name);
6568 rc = name2gid(&qctl.qc_id, name);
6571 rc = name2projid(&qctl.qc_id, name);
6578 qctl.qc_valid = QC_GENERAL;
6579 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6584 qctl.qc_id = strtoul(name, &endptr, 10);
6585 if (*endptr != '\0') {
6586 fprintf(stderr, "%s quota: invalid id '%s'\n",
6591 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6592 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6598 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6599 human_readable, show_default);
6602 #endif /* HAVE_SYS_QUOTA_H! */
6604 static int flushctx_ioctl(char *mp)
6608 fd = open(mp, O_RDONLY);
6610 fprintf(stderr, "flushctx: error open %s: %s\n",
6611 mp, strerror(errno));
6615 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6617 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6618 mp, strerror(errno));
6624 static int lfs_flushctx(int argc, char **argv)
6626 int kdestroy = 0, c;
6627 char mntdir[PATH_MAX] = {'\0'};
6631 while ((c = getopt(argc, argv, "k")) != -1) {
6637 fprintf(stderr, "error: %s: option '-%c' "
6638 "unrecognized\n", argv[0], c);
6644 if ((rc = system("kdestroy > /dev/null")) != 0) {
6645 rc = WEXITSTATUS(rc);
6646 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6650 if (optind >= argc) {
6651 /* flush for all mounted lustre fs. */
6652 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6653 /* Check if we have a mount point */
6654 if (mntdir[0] == '\0')
6657 if (flushctx_ioctl(mntdir))
6660 mntdir[0] = '\0'; /* avoid matching in next loop */
6663 /* flush fs as specified */
6664 while (optind < argc) {
6665 if (flushctx_ioctl(argv[optind++]))
6672 static int lfs_cp(int argc, char **argv)
6674 fprintf(stderr, "remote client copy file(s).\n"
6675 "obsolete, does not support it anymore.\n");
6679 static int lfs_ls(int argc, char **argv)
6681 fprintf(stderr, "remote client lists directory contents.\n"
6682 "obsolete, does not support it anymore.\n");
6686 static int lfs_changelog(int argc, char **argv)
6688 void *changelog_priv;
6689 struct changelog_rec *rec;
6690 long long startrec = 0, endrec = 0;
6692 struct option long_opts[] = {
6693 { .val = 'f', .name = "follow", .has_arg = no_argument },
6695 char short_opts[] = "f";
6698 while ((rc = getopt_long(argc, argv, short_opts,
6699 long_opts, NULL)) != -1) {
6706 "%s changelog: unrecognized option '%s'\n",
6707 progname, argv[optind - 1]);
6711 if (optind >= argc) {
6712 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6717 mdd = argv[optind++];
6719 startrec = strtoll(argv[optind++], NULL, 10);
6721 endrec = strtoll(argv[optind++], NULL, 10);
6723 rc = llapi_changelog_start(&changelog_priv,
6724 CHANGELOG_FLAG_BLOCK |
6725 CHANGELOG_FLAG_JOBID |
6726 CHANGELOG_FLAG_EXTRA_FLAGS |
6727 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6730 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6731 progname, strerror(errno = -rc));
6735 rc = llapi_changelog_set_xflags(changelog_priv,
6736 CHANGELOG_EXTRA_FLAG_UIDGID |
6737 CHANGELOG_EXTRA_FLAG_NID |
6738 CHANGELOG_EXTRA_FLAG_OMODE |
6739 CHANGELOG_EXTRA_FLAG_XATTR);
6742 "%s changelog: cannot set xflags for changelog: %s\n",
6743 progname, strerror(errno = -rc));
6747 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6751 if (endrec && rec->cr_index > endrec) {
6752 llapi_changelog_free(&rec);
6755 if (rec->cr_index < startrec) {
6756 llapi_changelog_free(&rec);
6760 secs = rec->cr_time >> 30;
6761 gmtime_r(&secs, &ts);
6762 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6763 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6764 changelog_type2str(rec->cr_type),
6765 ts.tm_hour, ts.tm_min, ts.tm_sec,
6766 (int)(rec->cr_time & ((1 << 30) - 1)),
6767 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6768 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6770 if (rec->cr_flags & CLF_JOBID) {
6771 struct changelog_ext_jobid *jid =
6772 changelog_rec_jobid(rec);
6774 if (jid->cr_jobid[0] != '\0')
6775 printf(" j=%s", jid->cr_jobid);
6778 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6779 struct changelog_ext_extra_flags *ef =
6780 changelog_rec_extra_flags(rec);
6782 printf(" ef=0x%llx", ef->cr_extra_flags);
6784 if (ef->cr_extra_flags & CLFE_UIDGID) {
6785 struct changelog_ext_uidgid *uidgid =
6786 changelog_rec_uidgid(rec);
6788 printf(" u=%llu:%llu",
6789 uidgid->cr_uid, uidgid->cr_gid);
6791 if (ef->cr_extra_flags & CLFE_NID) {
6792 struct changelog_ext_nid *nid =
6793 changelog_rec_nid(rec);
6796 libcfs_nid2str(nid->cr_nid));
6799 if (ef->cr_extra_flags & CLFE_OPEN) {
6800 struct changelog_ext_openmode *omd =
6801 changelog_rec_openmode(rec);
6802 char mode[] = "---";
6804 /* exec mode must be exclusive */
6805 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6808 if (omd->cr_openflags & MDS_FMODE_READ)
6810 if (omd->cr_openflags &
6817 if (strcmp(mode, "---") != 0)
6818 printf(" m=%s", mode);
6822 if (ef->cr_extra_flags & CLFE_XATTR) {
6823 struct changelog_ext_xattr *xattr =
6824 changelog_rec_xattr(rec);
6826 if (xattr->cr_xattr[0] != '\0')
6827 printf(" x=%s", xattr->cr_xattr);
6831 if (rec->cr_namelen)
6832 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6833 rec->cr_namelen, changelog_rec_name(rec));
6835 if (rec->cr_flags & CLF_RENAME) {
6836 struct changelog_ext_rename *rnm =
6837 changelog_rec_rename(rec);
6839 if (!fid_is_zero(&rnm->cr_sfid))
6840 printf(" s="DFID" sp="DFID" %.*s",
6841 PFID(&rnm->cr_sfid),
6842 PFID(&rnm->cr_spfid),
6843 (int)changelog_rec_snamelen(rec),
6844 changelog_rec_sname(rec));
6848 llapi_changelog_free(&rec);
6851 llapi_changelog_fini(&changelog_priv);
6854 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6855 progname, strerror(errno = -rc));
6857 return (rc == 1 ? 0 : rc);
6860 static int lfs_changelog_clear(int argc, char **argv)
6868 endrec = strtoll(argv[3], NULL, 10);
6870 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6873 fprintf(stderr, "%s: record out of range: %llu\n",
6875 else if (rc == -ENOENT)
6876 fprintf(stderr, "%s: no changelog user: %s\n",
6879 fprintf(stderr, "%s error: %s\n", argv[0],
6888 static int lfs_fid2path(int argc, char **argv)
6890 struct option long_opts[] = {
6891 { .val = 'c', .name = "cur", .has_arg = no_argument },
6892 { .val = 'l', .name = "link", .has_arg = required_argument },
6893 { .val = 'r', .name = "rec", .has_arg = required_argument },
6895 char short_opts[] = "cl:r:";
6896 char *device, *fid, *path;
6897 long long recno = -1;
6902 char *endptr = NULL;
6904 while ((rc = getopt_long(argc, argv, short_opts,
6905 long_opts, NULL)) != -1) {
6911 linkno = strtol(optarg, &endptr, 10);
6912 if (*endptr != '\0') {
6914 "%s fid2path: invalid linkno '%s'\n",
6920 recno = strtoll(optarg, &endptr, 10);
6921 if (*endptr != '\0') {
6923 "%s fid2path: invalid recno '%s'\n",
6930 "%s fid2path: unrecognized option '%s'\n",
6931 progname, argv[optind - 1]);
6938 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6943 device = argv[optind++];
6944 path = calloc(1, PATH_MAX);
6948 "%s fid2path: cannot allocate memory for path: %s\n",
6949 progname, strerror(-rc));
6954 while (optind < argc) {
6955 fid = argv[optind++];
6957 lnktmp = (linkno >= 0) ? linkno : 0;
6959 int oldtmp = lnktmp;
6960 long long rectmp = recno;
6962 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6966 "%s fid2path: cannot find '%s': %s\n",
6967 progname, fid, strerror(errno = -rc2));
6974 fprintf(stdout, "%lld ", rectmp);
6975 if (device[0] == '/') {
6976 fprintf(stdout, "%s", device);
6977 if (device[strlen(device) - 1] != '/')
6978 fprintf(stdout, "/");
6979 } else if (path[0] == '\0') {
6980 fprintf(stdout, "/");
6982 fprintf(stdout, "%s\n", path);
6985 /* specified linkno */
6987 if (oldtmp == lnktmp)
6997 static int lfs_path2fid(int argc, char **argv)
6999 struct option long_opts[] = {
7000 { .val = 'p', .name = "parents", .has_arg = no_argument },
7003 const char short_opts[] = "p";
7004 const char *sep = "";
7007 bool show_parents = false;
7009 while ((rc = getopt_long(argc, argv, short_opts,
7010 long_opts, NULL)) != -1) {
7013 show_parents = true;
7017 "%s path2fid: unrecognized option '%s'\n",
7018 progname, argv[optind - 1]);
7023 if (optind > argc - 1) {
7024 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7028 else if (optind < argc - 1)
7032 for (path = argv + optind; *path != NULL; path++) {
7034 if (!show_parents) {
7035 err = llapi_path2fid(*path, &fid);
7037 printf("%s%s"DFID"\n",
7038 *sep != '\0' ? *path : "", sep,
7041 char name[NAME_MAX + 1];
7042 unsigned int linkno = 0;
7044 while ((err = llapi_path2parent(*path, linkno, &fid,
7045 name, sizeof(name))) == 0) {
7046 if (*sep != '\0' && linkno == 0)
7047 printf("%s%s", *path, sep);
7049 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7054 /* err == -ENODATA is end-of-loop */
7055 if (linkno > 0 && err == -ENODATA) {
7063 "%s path2fid: cannot get %sfid for '%s': %s\n",
7064 progname, show_parents ? "parent " : "", *path,
7076 static int lfs_data_version(int argc, char **argv)
7083 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7086 fprintf(stderr, "%s data_version: FILE must be specified\n",
7091 while ((c = getopt(argc, argv, "nrw")) != -1) {
7094 data_version_flags = 0;
7097 data_version_flags |= LL_DV_RD_FLUSH;
7100 data_version_flags |= LL_DV_WR_FLUSH;
7104 "%s data_version: unrecognized option '%s'\n",
7105 progname, argv[optind - 1]);
7109 if (optind == argc) {
7110 fprintf(stderr, "%s data_version: FILE must be specified\n",
7115 path = argv[optind];
7116 fd = open(path, O_RDONLY);
7119 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7120 progname, path, strerror(-rc));
7124 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7127 "%s data_version: cannot get version for '%s': %s\n",
7128 progname, path, strerror(-rc));
7130 printf("%ju" "\n", (uintmax_t)data_version);
7136 static int lfs_hsm_state(int argc, char **argv)
7141 struct hsm_user_state hus;
7149 rc = llapi_hsm_state_get(path, &hus);
7151 fprintf(stderr, "can't get hsm state for %s: %s\n",
7152 path, strerror(errno = -rc));
7156 /* Display path name and status flags */
7157 printf("%s: (0x%08x)", path, hus.hus_states);
7159 if (hus.hus_states & HS_RELEASED)
7160 printf(" released");
7161 if (hus.hus_states & HS_EXISTS)
7163 if (hus.hus_states & HS_DIRTY)
7165 if (hus.hus_states & HS_ARCHIVED)
7166 printf(" archived");
7167 /* Display user-settable flags */
7168 if (hus.hus_states & HS_NORELEASE)
7169 printf(" never_release");
7170 if (hus.hus_states & HS_NOARCHIVE)
7171 printf(" never_archive");
7172 if (hus.hus_states & HS_LOST)
7173 printf(" lost_from_hsm");
7175 if (hus.hus_archive_id != 0)
7176 printf(", archive_id:%d", hus.hus_archive_id);
7179 } while (++i < argc);
7184 #define LFS_HSM_SET 0
7185 #define LFS_HSM_CLEAR 1
7188 * Generic function to set or clear HSM flags.
7189 * Used by hsm_set and hsm_clear.
7191 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7193 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7195 struct option long_opts[] = {
7196 { .val = 'A', .name = "archived", .has_arg = no_argument },
7197 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7198 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7199 { .val = 'e', .name = "exists", .has_arg = no_argument },
7200 { .val = 'l', .name = "lost", .has_arg = no_argument },
7201 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7202 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7204 char short_opts[] = "lraAdei:";
7208 __u32 archive_id = 0;
7214 while ((c = getopt_long(argc, argv, short_opts,
7215 long_opts, NULL)) != -1) {
7221 mask |= HS_NOARCHIVE;
7224 mask |= HS_ARCHIVED;
7227 mask |= HS_NORELEASE;
7236 archive_id = strtol(optarg, &end, 10);
7238 fprintf(stderr, "invalid archive_id: '%s'\n",
7246 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7247 argv[0], argv[optind - 1]);
7252 /* User should have specified a flag */
7256 while (optind < argc) {
7258 path = argv[optind];
7260 /* If mode == 0, this means we apply the mask. */
7261 if (mode == LFS_HSM_SET)
7262 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7264 rc = llapi_hsm_state_set(path, 0, mask, 0);
7267 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7268 path, strerror(errno = -rc));
7277 static int lfs_hsm_action(int argc, char **argv)
7282 struct hsm_current_action hca;
7283 struct hsm_extent he;
7284 enum hsm_user_action hua;
7285 enum hsm_progress_states hps;
7293 rc = llapi_hsm_current_action(path, &hca);
7295 fprintf(stderr, "can't get hsm action for %s: %s\n",
7296 path, strerror(errno = -rc));
7299 he = hca.hca_location;
7300 hua = hca.hca_action;
7301 hps = hca.hca_state;
7303 printf("%s: %s", path, hsm_user_action2name(hua));
7305 /* Skip file without action */
7306 if (hca.hca_action == HUA_NONE) {
7311 printf(" %s ", hsm_progress_state2name(hps));
7313 if ((hps == HPS_RUNNING) &&
7314 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7315 printf("(%llu bytes moved)\n",
7316 (unsigned long long)he.length);
7317 else if ((he.offset + he.length) == LUSTRE_EOF)
7318 printf("(from %llu to EOF)\n",
7319 (unsigned long long)he.offset);
7321 printf("(from %llu to %llu)\n",
7322 (unsigned long long)he.offset,
7323 (unsigned long long)(he.offset + he.length));
7325 } while (++i < argc);
7330 static int lfs_hsm_set(int argc, char **argv)
7332 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7335 static int lfs_hsm_clear(int argc, char **argv)
7337 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7341 * Check file state and return its fid, to be used by lfs_hsm_request().
7343 * \param[in] file Path to file to check
7344 * \param[in,out] fid Pointer to allocated lu_fid struct.
7345 * \param[in,out] last_dev Pointer to last device id used.
7347 * \return 0 on success.
7349 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7355 rc = lstat(file, &st);
7357 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7360 /* Checking for regular file as archiving as posix copytool
7361 * rejects archiving files other than regular files
7363 if (!S_ISREG(st.st_mode)) {
7364 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7367 /* A request should be ... */
7368 if (*last_dev != st.st_dev && *last_dev != 0) {
7369 fprintf(stderr, "All files should be "
7370 "on the same filesystem: %s\n", file);
7373 *last_dev = st.st_dev;
7375 rc = llapi_path2fid(file, fid);
7377 fprintf(stderr, "Cannot read FID of %s: %s\n",
7378 file, strerror(-rc));
7384 /* Fill an HSM HUR item with a given file name.
7386 * If mntpath is set, then the filename is actually a FID, and no
7387 * lookup on the filesystem will be performed.
7389 * \param[in] hur the user request to fill
7390 * \param[in] idx index of the item inside the HUR to fill
7391 * \param[in] mntpath mountpoint of Lustre
7392 * \param[in] fname filename (if mtnpath is NULL)
7393 * or FID (if mntpath is set)
7394 * \param[in] last_dev pointer to last device id used
7396 * \retval 0 on success
7397 * \retval CMD_HELP or a negative errno on error
7399 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7400 const char *mntpath, const char *fname,
7403 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7406 hui->hui_extent.length = -1;
7408 if (mntpath != NULL) {
7411 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7415 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7420 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7424 hur->hur_request.hr_itemcount++;
7429 static int lfs_hsm_request(int argc, char **argv, int action)
7431 struct option long_opts[] = {
7432 { .val = 'a', .name = "archive", .has_arg = required_argument },
7433 { .val = 'D', .name = "data", .has_arg = required_argument },
7434 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7435 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7438 char short_opts[] = "l:D:a:m:";
7439 struct hsm_user_request *hur, *oldhur;
7444 char *filelist = NULL;
7445 char fullpath[PATH_MAX];
7446 char *opaque = NULL;
7450 int nbfile_alloc = 0;
7451 char *some_file = NULL;
7452 char *mntpath = NULL;
7458 while ((c = getopt_long(argc, argv, short_opts,
7459 long_opts, NULL)) != -1) {
7468 if (action != HUA_ARCHIVE &&
7469 action != HUA_REMOVE) {
7471 "error: -a is supported only "
7472 "when archiving or removing\n");
7475 archive_id = atoi(optarg);
7478 if (some_file == NULL) {
7480 some_file = strdup(optarg);
7486 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7487 argv[0], argv[optind - 1]);
7492 /* All remaining args are files, so we have at least nbfile */
7493 nbfile = argc - optind;
7495 if ((nbfile == 0) && (filelist == NULL))
7499 opaque_len = strlen(opaque);
7501 /* Alloc the request structure with enough place to store all files
7502 * from command line. */
7503 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7505 fprintf(stderr, "Cannot create the request: %s\n",
7509 nbfile_alloc = nbfile;
7511 hur->hur_request.hr_action = action;
7512 hur->hur_request.hr_archive_id = archive_id;
7513 hur->hur_request.hr_flags = 0;
7515 /* All remaining args are files, add them */
7516 if (nbfile != 0 && some_file == NULL)
7517 some_file = strdup(argv[optind]);
7519 for (i = 0; i < nbfile; i++) {
7520 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7526 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7528 /* If a filelist was specified, read the filelist from it. */
7529 if (filelist != NULL) {
7530 fp = fopen(filelist, "r");
7532 fprintf(stderr, "Cannot read the file list %s: %s\n",
7533 filelist, strerror(errno));
7538 while ((rc = getline(&line, &len, fp)) != -1) {
7539 /* If allocated buffer was too small, get something
7541 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7544 nbfile_alloc = nbfile_alloc * 2 + 1;
7546 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7549 fprintf(stderr, "hsm: cannot allocate "
7550 "the request: %s\n",
7557 size = hur_len(oldhur);
7559 fprintf(stderr, "hsm: cannot allocate "
7560 "%u files + %u bytes data\n",
7561 oldhur->hur_request.hr_itemcount,
7562 oldhur->hur_request.hr_data_len);
7569 memcpy(hur, oldhur, size);
7574 if (line[strlen(line) - 1] == '\n')
7575 line[strlen(line) - 1] = '\0';
7577 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7578 mntpath, line, &last_dev);
7584 if (some_file == NULL) {
7594 /* If a --data was used, add it to the request */
7595 hur->hur_request.hr_data_len = opaque_len;
7597 memcpy(hur_data(hur), opaque, opaque_len);
7599 /* Send the HSM request */
7600 if (realpath(some_file, fullpath) == NULL) {
7601 fprintf(stderr, "Could not find path '%s': %s\n",
7602 some_file, strerror(errno));
7604 rc = llapi_hsm_request(fullpath, hur);
7606 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7607 some_file, strerror(-rc));
7617 static int lfs_hsm_archive(int argc, char **argv)
7619 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7622 static int lfs_hsm_restore(int argc, char **argv)
7624 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7627 static int lfs_hsm_release(int argc, char **argv)
7629 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7632 static int lfs_hsm_remove(int argc, char **argv)
7634 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7637 static int lfs_hsm_cancel(int argc, char **argv)
7639 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7642 static int lfs_swap_layouts(int argc, char **argv)
7647 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7648 SWAP_LAYOUTS_KEEP_MTIME |
7649 SWAP_LAYOUTS_KEEP_ATIME);
7652 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7654 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7656 int lfs_get_mode(const char *string)
7658 enum lock_mode_user mode;
7660 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7661 if (lock_mode_names[mode] == NULL)
7663 if (strcmp(string, lock_mode_names[mode]) == 0)
7670 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7672 enum lu_ladvise_type advice;
7675 advice < ARRAY_SIZE(ladvise_names); advice++) {
7676 if (ladvise_names[advice] == NULL)
7678 if (strcmp(string, ladvise_names[advice]) == 0)
7682 return LU_LADVISE_INVALID;
7685 static int lfs_ladvise(int argc, char **argv)
7687 struct option long_opts[] = {
7688 { .val = 'a', .name = "advice", .has_arg = required_argument },
7689 { .val = 'b', .name = "background", .has_arg = no_argument },
7690 { .val = 'e', .name = "end", .has_arg = required_argument },
7691 { .val = 'l', .name = "length", .has_arg = required_argument },
7692 { .val = 'm', .name = "mode", .has_arg = required_argument },
7693 { .val = 's', .name = "start", .has_arg = required_argument },
7694 { .val = 'u', .name = "unset", .has_arg = no_argument },
7696 char short_opts[] = "a:be:l:m:s:u";
7701 struct llapi_lu_ladvise advice;
7702 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7703 unsigned long long start = 0;
7704 unsigned long long end = LUSTRE_EOF;
7705 unsigned long long length = 0;
7706 unsigned long long size_units;
7707 unsigned long long flags = 0;
7711 while ((c = getopt_long(argc, argv, short_opts,
7712 long_opts, NULL)) != -1) {
7715 advice_type = lfs_get_ladvice(optarg);
7716 if (advice_type == LU_LADVISE_INVALID) {
7717 fprintf(stderr, "%s: invalid advice type "
7718 "'%s'\n", argv[0], optarg);
7719 fprintf(stderr, "Valid types:");
7721 for (advice_type = 0;
7722 advice_type < ARRAY_SIZE(ladvise_names);
7724 if (ladvise_names[advice_type] == NULL)
7726 fprintf(stderr, " %s",
7727 ladvise_names[advice_type]);
7729 fprintf(stderr, "\n");
7742 rc = llapi_parse_size(optarg, &end,
7745 fprintf(stderr, "%s: bad end offset '%s'\n",
7752 rc = llapi_parse_size(optarg, &start,
7755 fprintf(stderr, "%s: bad start offset "
7756 "'%s'\n", argv[0], optarg);
7762 rc = llapi_parse_size(optarg, &length,
7765 fprintf(stderr, "%s: bad length '%s'\n",
7771 mode = lfs_get_mode(optarg);
7773 fprintf(stderr, "%s: bad mode '%s', valid "
7774 "modes are READ or WRITE\n",
7782 fprintf(stderr, "%s: option '%s' unrecognized\n",
7783 argv[0], argv[optind - 1]);
7788 if (advice_type == LU_LADVISE_INVALID) {
7789 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7790 fprintf(stderr, "Valid types:");
7791 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7793 if (ladvise_names[advice_type] == NULL)
7795 fprintf(stderr, " %s", ladvise_names[advice_type]);
7797 fprintf(stderr, "\n");
7801 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7802 fprintf(stderr, "%s: Lock no expand advice is a per file "
7803 "descriptor advice, so when called from lfs, "
7804 "it does nothing.\n", argv[0]);
7808 if (argc <= optind) {
7809 fprintf(stderr, "%s: please give one or more file names\n",
7814 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7815 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7820 if (end == LUSTRE_EOF && length != 0)
7821 end = start + length;
7824 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7825 argv[0], start, end);
7829 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7830 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7835 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7836 fprintf(stderr, "%s: mode is required with lockahead\n",
7841 while (optind < argc) {
7844 path = argv[optind++];
7846 fd = open(path, O_RDONLY);
7848 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7849 argv[0], path, strerror(errno));
7854 advice.lla_start = start;
7855 advice.lla_end = end;
7856 advice.lla_advice = advice_type;
7857 advice.lla_value1 = 0;
7858 advice.lla_value2 = 0;
7859 advice.lla_value3 = 0;
7860 advice.lla_value4 = 0;
7861 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7862 advice.lla_lockahead_mode = mode;
7863 advice.lla_peradvice_flags = flags;
7866 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7869 fprintf(stderr, "%s: cannot give advice '%s' to file "
7870 "'%s': %s\n", argv[0],
7871 ladvise_names[advice_type],
7872 path, strerror(errno));
7878 if (rc == 0 && rc2 < 0)
7884 /** The input string contains a comma delimited list of component ids and
7885 * ranges, for example "1,2-4,7".
7887 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7889 bool end_of_loop = false;
7897 while (!end_of_loop) {
7901 char *endptr = NULL;
7904 ptr = strchrnul(arg, ',');
7905 end_of_loop = *ptr == '\0';
7908 start_index = strtol(arg, &endptr, 0);
7909 if (endptr == arg) /* no data at all */
7911 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7913 if (start_index < 0)
7916 end_index = start_index;
7917 if (*endptr == '-') {
7918 end_index = strtol(endptr + 1, &endptr, 0);
7919 if (*endptr != '\0')
7921 if (end_index < start_index)
7925 for (i = start_index; i <= end_index && size > 0; i++) {
7928 /* remove duplicate */
7929 for (j = 0; j < nr; j++) {
7933 if (j == nr) { /* no duplicate */
7939 if (size == 0 && i < end_index)
7946 if (!end_of_loop && ptr != NULL)
7949 return rc < 0 ? rc : nr;
7953 * struct verify_mirror_id - Mirror id to be verified.
7954 * @mirror_id: A specified mirror id.
7955 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7957 struct verify_mirror_id {
7963 * compare_mirror_ids() - Compare mirror ids.
7964 * @layout: Mirror component list.
7965 * @cbdata: Callback data in verify_mirror_id structure.
7967 * This is a callback function called by llapi_layout_comp_iterate()
7968 * to compare the specified mirror id with the one in the current
7969 * component of @layout. If they are the same, then the specified
7970 * mirror id is valid.
7972 * Return: a negative error code on failure or
7973 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7974 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7977 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7979 struct verify_mirror_id *mirror_id_cbdata =
7980 (struct verify_mirror_id *)cbdata;
7984 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7988 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7989 progname, strerror(errno));
7993 if (mirror_id_cbdata->mirror_id == mirror_id) {
7994 mirror_id_cbdata->is_valid_id = true;
7995 return LLAPI_LAYOUT_ITER_STOP;
7998 return LLAPI_LAYOUT_ITER_CONT;
8002 * verify_mirror_ids() - Verify specified mirror ids.
8003 * @fname: Mirrored file name.
8004 * @mirror_ids: Specified mirror ids to be verified.
8005 * @ids_nr: Number of specified mirror ids.
8007 * This function verifies that specified @mirror_ids are valid
8008 * in the mirrored file @fname.
8010 * Return: 0 on success or a negative error code on failure.
8013 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8015 struct llapi_layout *layout = NULL;
8016 struct verify_mirror_id mirror_id_cbdata = { 0 };
8027 if (stat(fname, &stbuf) < 0) {
8028 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8029 progname, fname, strerror(errno));
8034 if (!S_ISREG(stbuf.st_mode)) {
8035 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8041 fd = open(fname, O_DIRECT | O_RDONLY);
8043 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8044 progname, fname, strerror(errno));
8049 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8051 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8052 progname, fname, strerror(errno));
8056 layout = llapi_layout_get_by_fd(fd, 0);
8057 if (layout == NULL) {
8058 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8059 progname, fname, strerror(errno));
8061 llapi_lease_release(fd);
8065 rc = llapi_layout_flags_get(layout, &flr_state);
8067 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8068 progname, fname, strerror(errno));
8073 flr_state &= LCM_FL_FLR_MASK;
8074 switch (flr_state) {
8077 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8078 progname, fname, llapi_layout_flags_string(flr_state));
8085 for (i = 0; i < ids_nr; i++) {
8086 mirror_id_cbdata.mirror_id = mirror_ids[i];
8087 mirror_id_cbdata.is_valid_id = false;
8089 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8094 "%s: '%s' failed to verify mirror id: %u.\n",
8095 progname, fname, mirror_ids[i]);
8099 if (!mirror_id_cbdata.is_valid_id) {
8102 "%s: '%s' invalid specified mirror id: %u.\n",
8103 progname, fname, mirror_ids[i]);
8109 llapi_layout_free(layout);
8110 llapi_lease_release(fd);
8118 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8119 __u16 *mirror_ids, int ids_nr)
8121 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8122 struct llapi_layout *layout;
8132 if (stat(fname, &stbuf) < 0) {
8133 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8134 progname, fname, strerror(errno));
8138 if (!S_ISREG(stbuf.st_mode)) {
8139 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8145 fd = open(fname, O_DIRECT | O_RDWR);
8147 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8148 progname, fname, strerror(errno));
8153 layout = llapi_layout_get_by_fd(fd, 0);
8154 if (layout == NULL) {
8155 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8156 progname, fname, strerror(errno));
8161 rc = llapi_layout_flags_get(layout, &flr_state);
8163 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8164 progname, fname, strerror(errno));
8169 flr_state &= LCM_FL_FLR_MASK;
8170 if (flr_state == LCM_FL_NONE) {
8172 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8177 /* get stale component info */
8178 comp_size = llapi_mirror_find_stale(layout, comp_array,
8179 ARRAY_SIZE(comp_array),
8180 mirror_ids, ids_nr);
8181 if (comp_size <= 0) {
8186 ioc->lil_mode = LL_LEASE_WRLCK;
8187 ioc->lil_flags = LL_LEASE_RESYNC;
8188 rc = llapi_lease_set(fd, ioc);
8190 if (rc == -EALREADY)
8194 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8195 progname, fname, strerror(errno));
8199 /* get the read range [start, end) */
8200 start = comp_array[0].lrc_start;
8201 end = comp_array[0].lrc_end;
8202 for (idx = 1; idx < comp_size; idx++) {
8203 if (comp_array[idx].lrc_start < start)
8204 start = comp_array[idx].lrc_start;
8205 if (end < comp_array[idx].lrc_end)
8206 end = comp_array[idx].lrc_end;
8209 rc = llapi_lease_check(fd);
8210 if (rc != LL_LEASE_WRLCK) {
8211 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8216 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8219 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8220 progname, fname, rc);
8222 /* prepare ioc for lease put */
8223 ioc->lil_mode = LL_LEASE_UNLCK;
8224 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8226 for (idx = 0; idx < comp_size; idx++) {
8227 if (comp_array[idx].lrc_synced) {
8228 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8233 rc = llapi_lease_set(fd, ioc);
8235 if (rc == 0) /* lost lease lock */
8237 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8238 progname, fname, strerror(errno));
8242 * llapi_lease_set returns lease mode when it request to unlock
8248 llapi_layout_free(layout);
8255 static inline int lfs_mirror_resync(int argc, char **argv)
8257 struct ll_ioc_lease *ioc = NULL;
8258 __u16 mirror_ids[128] = { 0 };
8263 struct option long_opts[] = {
8264 { .val = 'o', .name = "only", .has_arg = required_argument },
8267 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8270 rc = parse_mirror_ids(mirror_ids,
8271 sizeof(mirror_ids) / sizeof(__u16),
8275 "%s: bad mirror ids '%s'.\n",
8282 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8283 argv[0], argv[optind - 1]);
8289 if (argc == optind) {
8290 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8295 if (ids_nr > 0 && argc > optind + 1) {
8297 "%s: option '--only' cannot be used upon multiple files.\n",
8305 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8310 /* set the lease on the file */
8311 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8313 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8314 argv[0], strerror(errno));
8319 for (; optind < argc; optind++) {
8320 rc = lfs_mirror_resync_file(argv[optind], ioc,
8321 mirror_ids, ids_nr);
8322 /* ignore previous file's error, continue with next file */
8325 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8333 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8335 struct llapi_layout *layout;
8338 layout = llapi_layout_get_by_fd(fd, 0);
8339 if (layout == NULL) {
8340 fprintf(stderr, "could not get layout.\n");
8344 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8346 fprintf(stderr, "failed to iterate layout\n");
8347 llapi_layout_free(layout);
8350 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8351 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8352 llapi_layout_free(layout);
8356 llapi_layout_free(layout);
8362 * Check whether two files are the same file
8363 * \retval 0 same file
8364 * \retval 1 not the same file
8365 * \retval <0 error code
8367 static inline int check_same_file(const char *f1, const char *f2)
8372 if (stat(f1, &stbuf1) < 0) {
8373 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8374 progname, f1, strerror(errno));
8378 if (stat(f2, &stbuf2) < 0) {
8379 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8380 progname, f2, strerror(errno));
8384 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8385 stbuf1.st_ino == stbuf2.st_ino)
8391 static inline int lfs_mirror_read(int argc, char **argv)
8394 __u16 mirror_id = 0;
8395 const char *outfile = NULL;
8401 const size_t buflen = 4 << 20;
8403 struct option long_opts[] = {
8404 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8405 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8408 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8413 mirror_id = strtoul(optarg, &end, 0);
8414 if (*end != '\0' || mirror_id == 0) {
8416 "%s %s: invalid mirror ID '%s'\n",
8417 progname, argv[0], optarg);
8425 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8426 progname, argv[optind - 1]);
8431 if (argc == optind) {
8432 fprintf(stderr, "%s %s: no mirrored file provided\n",
8435 } else if (argc > optind + 1) {
8436 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8440 if (mirror_id == 0) {
8441 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8446 /* open mirror file */
8447 fname = argv[optind];
8450 rc = check_same_file(fname, outfile);
8453 "%s %s: output file cannot be the mirrored file\n",
8461 fd = open(fname, O_DIRECT | O_RDONLY);
8463 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8464 progname, argv[0], fname, strerror(errno));
8468 /* verify mirror id */
8469 rc = verify_mirror_id_by_fd(fd, mirror_id);
8472 "%s %s: cannot find mirror with ID %u in '%s'\n",
8473 progname, argv[0], mirror_id, fname);
8477 /* open output file */
8479 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8481 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8482 progname, argv[0], outfile, strerror(errno));
8487 outfd = STDOUT_FILENO;
8490 /* allocate buffer */
8491 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8493 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8494 progname, argv[0], rc);
8501 ssize_t written = 0;
8503 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8504 if (bytes_read < 0) {
8507 "%s %s: fail to read data from mirror %u: %s\n",
8508 progname, argv[0], mirror_id, strerror(-rc));
8513 if (bytes_read == 0)
8516 while (written < bytes_read) {
8519 written2 = write(outfd, buf + written,
8520 bytes_read - written);
8523 "%s %s: fail to write %s: %s\n",
8524 progname, argv[0], outfile ? : "STDOUT",
8529 written += written2;
8532 if (written != bytes_read) {
8534 "%s %s: written %ld bytes does not match with %ld read.\n",
8535 progname, argv[0], written, bytes_read);
8557 static inline int lfs_mirror_write(int argc, char **argv)
8560 __u16 mirror_id = 0;
8561 const char *inputfile = NULL;
8567 const size_t buflen = 4 << 20;
8569 size_t page_size = sysconf(_SC_PAGESIZE);
8570 struct ll_ioc_lease_id ioc;
8572 struct option long_opts[] = {
8573 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8574 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8577 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8582 mirror_id = strtoul(optarg, &end, 0);
8583 if (*end != '\0' || mirror_id == 0) {
8585 "%s %s: invalid mirror ID '%s'\n",
8586 progname, argv[0], optarg);
8594 fprintf(stderr, "%s: option '%s' unrecognized\n",
8595 progname, argv[optind - 1]);
8600 if (argc == optind) {
8601 fprintf(stderr, "%s %s: no mirrored file provided\n",
8604 } else if (argc > optind + 1) {
8605 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8609 if (mirror_id == 0) {
8610 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8615 /* open mirror file */
8616 fname = argv[optind];
8619 rc = check_same_file(fname, inputfile);
8622 "%s %s: input file cannot be the mirrored file\n",
8630 fd = open(fname, O_DIRECT | O_WRONLY);
8632 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8633 progname, argv[0], fname, strerror(errno));
8637 /* verify mirror id */
8638 rc = verify_mirror_id_by_fd(fd, mirror_id);
8641 "%s %s: cannot find mirror with ID %u in '%s'\n",
8642 progname, argv[0], mirror_id, fname);
8646 /* open input file */
8648 inputfd = open(inputfile, O_RDONLY, 0644);
8650 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
8651 progname, argv[0], inputfile, strerror(errno));
8656 inputfd = STDIN_FILENO;
8659 /* allocate buffer */
8660 rc = posix_memalign(&buf, page_size, buflen);
8662 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8663 progname, argv[0], rc);
8667 /* prepare target mirror components instantiation */
8668 ioc.lil_mode = LL_LEASE_WRLCK;
8669 ioc.lil_flags = LL_LEASE_RESYNC;
8670 ioc.lil_mirror_id = mirror_id;
8671 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8674 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8675 progname, argv[0], fname, strerror(errno));
8685 rc = llapi_lease_check(fd);
8686 if (rc != LL_LEASE_WRLCK) {
8687 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
8688 progname, argv[0], fname);
8692 bytes_read = read(inputfd, buf, buflen);
8693 if (bytes_read < 0) {
8696 "%s %s: fail to read data from '%s': %s\n",
8697 progname, argv[0], inputfile ? : "STDIN",
8704 if (bytes_read == 0)
8707 /* round up to page align to make direct IO happy. */
8708 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
8710 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
8715 "%s %s: fail to write to mirror %u: %s\n",
8716 progname, argv[0], mirror_id,
8724 if (pos & (page_size - 1)) {
8725 rc = llapi_mirror_truncate(fd, mirror_id, pos);
8730 ioc.lil_mode = LL_LEASE_UNLCK;
8731 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
8733 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8738 "%s %s: release lease lock of '%s' failed: %s\n",
8739 progname, argv[0], fname, strerror(errno));
8757 * struct verify_chunk - Mirror chunk to be verified.
8758 * @chunk: [start, end) of the chunk.
8759 * @mirror_count: Number of mirror ids in @mirror_id array.
8760 * @mirror_id: Array of valid mirror ids that cover the chunk.
8762 struct verify_chunk {
8763 struct lu_extent chunk;
8764 unsigned int mirror_count;
8765 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
8769 * print_chunks() - Print chunk information.
8770 * @fname: Mirrored file name.
8771 * @chunks: Array of chunks.
8772 * @chunk_count: Number of chunks in @chunks array.
8774 * This function prints [start, end) of each chunk in @chunks
8775 * for mirrored file @fname, and also prints the valid mirror ids
8776 * that cover the chunk.
8781 void print_chunks(const char *fname, struct verify_chunk *chunks,
8787 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
8788 for (i = 0; i < chunk_count; i++) {
8789 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
8791 if (chunks[i].mirror_count == 0)
8792 fprintf(stdout, "\t[");
8794 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
8795 for (j = 1; j < chunks[i].mirror_count; j++)
8796 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
8798 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
8800 fprintf(stdout, "\n");
8804 * print_checksums() - Print CRC-32 checksum values.
8805 * @chunk: A chunk and its corresponding valid mirror ids.
8806 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
8808 * This function prints CRC-32 checksum values on @chunk for
8809 * each valid mirror that covers it.
8814 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
8819 "CRC-32 checksum value for chunk "DEXT":\n",
8820 PEXT(&chunk->chunk));
8821 for (i = 0; i < chunk->mirror_count; i++)
8822 fprintf(stdout, "Mirror %u:\t%#lx\n",
8823 chunk->mirror_id[i], crc[i]);
8824 fprintf(stdout, "\n");
8828 * filter_mirror_id() - Filter specified mirror ids.
8829 * @chunks: Array of chunks.
8830 * @chunk_count: Number of chunks in @chunks array.
8831 * @mirror_ids: Specified mirror ids to be verified.
8832 * @ids_nr: Number of specified mirror ids.
8834 * This function scans valid mirror ids that cover each chunk in @chunks
8835 * and filters specified mirror ids.
8840 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
8841 __u16 *mirror_ids, int ids_nr)
8846 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8847 unsigned int valid_count = 0;
8849 for (i = 0; i < chunk_count; i++) {
8850 if (chunks[i].mirror_count == 0)
8854 for (j = 0; j < ids_nr; j++) {
8855 for (k = 0; k < chunks[i].mirror_count; k++) {
8856 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
8857 valid_id[valid_count] = mirror_ids[j];
8864 memcpy(chunks[i].mirror_id, valid_id,
8865 sizeof(__u16) * valid_count);
8866 chunks[i].mirror_count = valid_count;
8871 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
8872 * @layout: Mirror component list.
8873 * @chunks: Array of chunks.
8874 * @chunks_size: Array size of @chunks.
8876 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
8877 * to find out chunk segments and store them in @chunks array.
8879 * The @mirror_id array in each element of @chunks will store the valid
8880 * mirror ids that cover the chunk. If a mirror component covering the
8881 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
8882 * will not be stored into the @mirror_id array, and the chunk for that
8883 * mirror will not be verified.
8885 * The @mirror_count in each element of @chunks will store the number of
8886 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
8887 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
8888 * indicates the chunk is valid in only one mirror. In both cases, the
8889 * chunk will not be verified.
8891 * Here is an example:
8894 * +------+-------------+--------------------+
8896 * +------+------+------+------+-------------+
8897 * | | S | S | | mirror2
8898 * +-------------+------+------+-------------+
8900 * prepared @chunks array will contain 5 elements:
8901 * (([0, 1M), [1, 2], 2),
8902 * ([1M, 2M), [1, 2], 2),
8903 * ([2M, 3M), [1], 1),
8904 * ([3M, 4M], [], 0),
8905 * ([4M, EOF), [2], 1))
8907 * Return: the actual array size of @chunks on success
8908 * or a negative error code on failure.
8911 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
8912 struct verify_chunk *chunks,
8923 memset(chunks, 0, sizeof(*chunks) * chunks_size);
8926 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
8929 "%s: move to the first layout component: %s.\n",
8930 progname, strerror(errno));
8936 chunks[idx].chunk.e_end = LUSTRE_EOF;
8938 rc = llapi_layout_comp_extent_get(layout, &start, &end);
8941 "%s: llapi_layout_comp_extent_get failed: %s.\n",
8942 progname, strerror(errno));
8946 if (start > chunks[idx].chunk.e_start ||
8947 end <= chunks[idx].chunk.e_start)
8950 if (end < chunks[idx].chunk.e_end)
8951 chunks[idx].chunk.e_end = end;
8953 rc = llapi_layout_comp_flags_get(layout, &flags);
8956 "%s: llapi_layout_comp_flags_get failed: %s.\n",
8957 progname, strerror(errno));
8961 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8964 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8967 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8968 progname, strerror(errno));
8972 chunks[idx].mirror_id[i] = mirror_id;
8974 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8976 "%s: mirror_id array is too small.\n",
8983 rc = llapi_layout_comp_use(layout,
8984 LLAPI_LAYOUT_COMP_USE_NEXT);
8987 "%s: move to the next layout component: %s.\n",
8988 progname, strerror(errno));
8991 } /* loop through all components */
8993 chunks[idx].mirror_count = i;
8995 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8999 if (idx >= chunks_size) {
9000 fprintf(stderr, "%s: chunks array is too small.\n",
9006 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9010 return rc < 0 ? rc : idx + 1;
9014 * lfs_mirror_verify_chunk() - Verify a chunk.
9015 * @fd: File descriptor of the mirrored file.
9016 * @file_size: Size of the mirrored file.
9017 * @chunk: A chunk and its corresponding valid mirror ids.
9018 * @verbose: Verbose mode.
9020 * This function verifies a @chunk contains exactly the same data
9021 * ammong the mirrors that cover it.
9023 * If @verbose is specified, then the function will print where the
9024 * differences are if the data do not match. Otherwise, it will
9025 * just return an error in that case.
9027 * Return: 0 on success or a negative error code on failure.
9030 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9031 struct verify_chunk *chunk, int verbose)
9033 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9035 size_t page_size = sysconf(_SC_PAGESIZE);
9041 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9048 rc = posix_memalign(&buf, page_size, buflen);
9049 if (rc) /* error code is returned directly */
9053 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9054 PEXT(&chunk->chunk));
9055 for (i = 0; i < chunk->mirror_count; i++)
9056 fprintf(stdout, " %u", chunk->mirror_id[i]);
9057 fprintf(stdout, "\n");
9061 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9062 pos = chunk->chunk.e_start;
9063 while (bytes_done < count) {
9064 /* compute initial CRC-32 checksum */
9065 crc = crc32(0L, Z_NULL, 0);
9066 memset(crc_array, 0, sizeof(crc_array));
9069 for (i = 0; i < chunk->mirror_count; i++) {
9070 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9072 if (bytes_read < 0) {
9075 "%s: failed to read data from mirror %u: %s.\n",
9076 progname, chunk->mirror_id[i],
9081 /* compute new CRC-32 checksum */
9082 crc_array[i] = crc32(crc, buf, bytes_read);
9086 print_checksums(chunk, crc_array);
9088 /* compare CRC-32 checksum values */
9089 for (i = 1; i < chunk->mirror_count; i++) {
9090 if (crc_array[i] != crc_array[0]) {
9096 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9097 progname, PEXT(&chunk->chunk),
9098 chunk->mirror_id[0],
9099 chunk->mirror_id[i]);
9104 bytes_done += bytes_read;
9107 if (verbose > 1 && rc == 0) {
9108 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9109 PEXT(&chunk->chunk));
9110 for (i = 0; i < chunk->mirror_count; i++)
9111 fprintf(stdout, " %u", chunk->mirror_id[i]);
9112 fprintf(stdout, " PASS\n\n");
9121 * lfs_mirror_verify_file() - Verify a mirrored file.
9122 * @fname: Mirrored file name.
9123 * @mirror_ids: Specified mirror ids to be verified.
9124 * @ids_nr: Number of specified mirror ids.
9125 * @verbose: Verbose mode.
9127 * This function verifies that each SYNC mirror of a mirrored file
9128 * specified by @fname contains exactly the same data.
9130 * If @mirror_ids is specified, then the function will verify the
9131 * mirrors specified by @mirror_ids contain exactly the same data.
9133 * If @verbose is specified, then the function will print where the
9134 * differences are if the data do not match. Otherwise, it will
9135 * just return an error in that case.
9137 * Return: 0 on success or a negative error code on failure.
9140 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9143 struct verify_chunk chunks_array[1024] = { };
9144 struct llapi_layout *layout = NULL;
9148 int chunk_count = 0;
9154 if (stat(fname, &stbuf) < 0) {
9155 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9156 progname, fname, strerror(errno));
9161 if (!S_ISREG(stbuf.st_mode)) {
9162 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9168 if (stbuf.st_size == 0) {
9170 fprintf(stdout, "%s: '%s' file size is 0.\n",
9176 fd = open(fname, O_DIRECT | O_RDONLY);
9178 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9179 progname, fname, strerror(errno));
9184 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9186 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9187 progname, fname, strerror(errno));
9191 layout = llapi_layout_get_by_fd(fd, 0);
9192 if (layout == NULL) {
9193 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9194 progname, fname, strerror(errno));
9196 llapi_lease_release(fd);
9200 rc = llapi_layout_flags_get(layout, &flr_state);
9202 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9203 progname, fname, strerror(errno));
9208 flr_state &= LCM_FL_FLR_MASK;
9209 switch (flr_state) {
9212 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9213 progname, fname, llapi_layout_flags_string(flr_state));
9219 /* find out mirror chunks to be verified */
9220 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9221 ARRAY_SIZE(chunks_array));
9222 if (chunk_count < 0) {
9228 /* filter specified mirror ids */
9229 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9232 print_chunks(fname, chunks_array, chunk_count);
9234 for (idx = 0; idx < chunk_count; idx++) {
9235 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9238 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9240 PEXT(&chunks_array[idx].chunk),
9241 (unsigned long long)stbuf.st_size);
9245 if (chunks_array[idx].mirror_count == 0) {
9247 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9249 PEXT(&chunks_array[idx].chunk));
9251 fprintf(stderr, "skipped\n");
9255 fprintf(stderr, "failed\n");
9259 if (chunks_array[idx].mirror_count == 1) {
9262 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9264 PEXT(&chunks_array[idx].chunk),
9265 chunks_array[idx].mirror_id[0]);
9269 rc = llapi_lease_check(fd);
9270 if (rc != LL_LEASE_RDLCK) {
9271 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9276 /* verify one chunk */
9277 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9278 &chunks_array[idx], verbose);
9292 llapi_layout_free(layout);
9293 llapi_lease_release(fd);
9301 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9302 * @argc: The count of lfs mirror verify command line arguments.
9303 * @argv: Array of strings for lfs mirror verify command line arguments.
9305 * This function parses lfs mirror verify command and verifies the
9306 * specified mirrored file(s).
9308 * Return: 0 on success or a negative error code on failure.
9310 static inline int lfs_mirror_verify(int argc, char **argv)
9312 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9320 struct option long_opts[] = {
9321 { .val = 'o', .name = "only", .has_arg = required_argument },
9322 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9325 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9327 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9330 rc = parse_mirror_ids(mirror_ids,
9331 ARRAY_SIZE(mirror_ids),
9335 "%s: bad mirror ids '%s'.\n",
9342 "%s: at least 2 mirror ids needed with '--only' option.\n",
9352 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9353 progname, argv[optind - 1]);
9359 if (argc == optind) {
9360 fprintf(stderr, "%s: no file name given.\n", progname);
9365 if (ids_nr > 0 && argc > optind + 1) {
9367 "%s: '--only' cannot be used upon multiple files.\n",
9375 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9381 for (; optind < argc; optind++) {
9382 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9392 * lfs_mirror() - Parse and execute lfs mirror commands.
9393 * @argc: The count of lfs mirror command line arguments.
9394 * @argv: Array of strings for lfs mirror command line arguments.
9396 * This function parses lfs mirror commands and performs the
9397 * corresponding functions specified in mirror_cmdlist[].
9399 * Return: 0 on success or an error code on failure.
9401 static int lfs_mirror(int argc, char **argv)
9408 Parser_init("lfs-mirror > ", mirror_cmdlist);
9410 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9412 program_invocation_short_name = cmd;
9414 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9416 rc = Parser_commands();
9418 return rc < 0 ? -rc : rc;
9421 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9423 #if __BYTE_ORDER == __BIG_ENDIAN
9424 __swab16s(&attrs->lsa_valid);
9425 __swab64s(&attrs->lsa_size);
9426 __swab64s(&attrs->lsa_blocks);
9432 LFS_SOM_BLOCKS = 0x2,
9433 LFS_SOM_FLAGS = 0x4,
9434 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9438 static int lfs_getsom(int argc, char **argv)
9441 struct lustre_som_attrs *attrs;
9442 char buf[sizeof(*attrs) + 64];
9443 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9446 while ((c = getopt(argc, argv, "sbf")) != -1) {
9449 type = LFS_SOM_SIZE;
9452 type = LFS_SOM_BLOCKS;
9455 type = LFS_SOM_FLAGS;
9458 fprintf(stderr, "%s: invalid option '%c'\n",
9468 fprintf(stderr, "%s: %s\n",
9469 progname, argc == 0 ? "miss file target" :
9470 "input more than 2 files");
9475 attrs = (void *)buf;
9476 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9479 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
9480 argv[0], strerror(errno), errno);
9484 lustre_som_swab(attrs);
9487 case LFS_SOM_ATTR_ALL:
9488 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9489 path, attrs->lsa_size, attrs->lsa_blocks,
9493 printf("%llu\n", attrs->lsa_size);
9495 case LFS_SOM_BLOCKS:
9496 printf("%llu\n", attrs->lsa_blocks);
9499 printf("%x\n", attrs->lsa_valid);
9502 fprintf(stderr, "%s: unknown option\n", progname);
9510 * lfs_mirror_list_commands() - List lfs mirror commands.
9511 * @argc: The count of command line arguments.
9512 * @argv: Array of strings for command line arguments.
9514 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9516 * Return: 0 on success.
9518 static int lfs_mirror_list_commands(int argc, char **argv)
9520 char buffer[81] = "";
9522 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9528 static int lfs_list_commands(int argc, char **argv)
9530 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9532 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9537 int main(int argc, char **argv)
9541 /* Ensure that liblustreapi constructor has run */
9542 if (!llapi_liblustreapi_initialized())
9543 fprintf(stderr, "liblustreapi was not properly initialized\n");
9548 Parser_init("lfs > ", cmdlist);
9550 progname = program_invocation_short_name; /* Used in error messages */
9552 llapi_set_command_name(argv[1]);
9553 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9554 llapi_clear_command_name();
9556 rc = Parser_commands();
9559 return rc < 0 ? -rc : rc;
9562 #ifdef _LUSTRE_IDL_H_
9563 /* Everything we need here should be included by lustreapi.h. */
9564 # error "lfs should not depend on lustre_idl.h"
9565 #endif /* _LUSTRE_IDL_H_ */