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;
2294 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2295 if (!strcmp(string, "lcm_mirror_count")) {
2296 lsa->lsa_mirror_count = node->cy_valueint;
2297 } else if (!strcmp(string, "lcme_extent.e_start")) {
2298 if (node->cy_valueint != 0 || *layout != NULL) {
2299 rc = build_component(layout, lsa, true);
2304 if (node->cy_valueint == 0)
2305 lsa->lsa_first_comp = true;
2307 /* initialize lsa */
2308 setstripe_args_init(lsa);
2309 lsa->lsa_tgts = osts;
2310 } else if (!strcmp(string, "lcme_extent.e_end")) {
2311 if (node->cy_valueint == -1)
2312 lsa->lsa_comp_end = LUSTRE_EOF;
2314 lsa->lsa_comp_end = node->cy_valueint;
2315 } else if (!strcmp(string, "stripe_count")) {
2316 lsa->lsa_stripe_count = node->cy_valueint;
2317 } else if (!strcmp(string, "stripe_size")) {
2318 lsa->lsa_stripe_size = node->cy_valueint;
2319 } else if (!strcmp(string, "stripe_offset")) {
2320 lsa->lsa_stripe_off = node->cy_valueint;
2321 } else if (!strcmp(string, "l_ost_idx")) {
2322 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2327 node = node->cy_next;
2333 static int lfs_comp_create_from_yaml(char *template,
2334 struct llapi_layout **layout,
2335 struct lfs_setstripe_args *lsa,
2338 struct cYAML *tree = NULL, *err_rc = NULL;
2341 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2343 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2344 progname, template);
2345 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2346 "can't parse", &err_rc);
2347 cYAML_print_tree2file(stderr, err_rc);
2348 cYAML_free_tree(err_rc);
2353 /* initialize lsa for plain file */
2354 setstripe_args_init(lsa);
2355 lsa->lsa_tgts = osts;
2357 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2359 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2360 progname, template);
2363 rc = build_component(layout, lsa, *layout != NULL);
2365 /* clean clean lsa */
2366 setstripe_args_init(lsa);
2370 cYAML_free_tree(tree);
2374 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2375 * end of the last component in the existing file, and adjust the
2376 * first extent start of the components to be added accordingly. */
2377 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2379 struct llapi_layout *head;
2380 uint64_t start, end, stripe_size, prev_end = 0;
2383 if (layout == NULL) {
2385 "%s setstripe: layout must be specified\n",
2391 head = llapi_layout_get_by_path(fname, 0);
2394 "%s setstripe: cannot read layout from '%s': %s\n",
2395 progname, fname, strerror(errno));
2397 } else if (errno == ENODATA) {
2398 /* file without LOVEA, this component-add will be turned
2399 * into a component-create. */
2400 llapi_layout_free(head);
2402 } else if (!llapi_layout_is_composite(head)) {
2403 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2405 llapi_layout_free(head);
2409 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2411 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2412 progname, strerror(errno));
2413 llapi_layout_free(head);
2417 llapi_layout_free(head);
2419 /* Make sure we use the first component of the layout to be added. */
2420 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2423 "%s setstripe: cannot move component cursor: %s\n",
2424 progname, strerror(errno));
2428 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2430 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2431 progname, strerror(errno));
2435 if (start > prev_end || end <= prev_end) {
2437 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2438 progname, start, end, prev_end);
2442 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2444 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2445 progname, strerror(errno));
2449 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2450 (prev_end & (stripe_size - 1))) {
2452 "%s setstripe: stripe size %lu not aligned with %lu\n",
2453 progname, stripe_size, prev_end);
2457 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2460 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2461 progname, prev_end, end, strerror(errno));
2468 static inline bool arg_is_eof(char *arg)
2470 return !strncmp(arg, "-1", strlen("-1")) ||
2471 !strncmp(arg, "EOF", strlen("EOF")) ||
2472 !strncmp(arg, "eof", strlen("eof"));
2476 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2478 * Return: Valid mirror_args pointer on success and
2479 * NULL if memory allocation fails.
2481 static struct mirror_args *lfs_mirror_alloc(void)
2483 struct mirror_args *mirror = NULL;
2486 mirror = calloc(1, sizeof(*mirror));
2497 * lfs_mirror_free() - Free memory allocated for a mirror argument
2499 * @mirror: Previously allocated mirror argument structure by
2500 * lfs_mirror_alloc().
2502 * Free memory allocated for @mirror.
2506 static void lfs_mirror_free(struct mirror_args *mirror)
2508 if (mirror->m_layout != NULL)
2509 llapi_layout_free(mirror->m_layout);
2514 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2515 * @mirror_list: Previously allocated mirror list.
2517 * Free memory allocated for @mirror_list.
2521 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2523 struct mirror_args *next_mirror = NULL;
2525 while (mirror_list != NULL) {
2526 next_mirror = mirror_list->m_next;
2527 lfs_mirror_free(mirror_list);
2528 mirror_list = next_mirror;
2540 LFS_COMP_NO_VERIFY_OPT,
2542 LFS_MIRROR_FLAGS_OPT,
2544 LFS_MIRROR_STATE_OPT,
2546 LFS_MIRROR_INDEX_OPT,
2550 static int lfs_setstripe_internal(int argc, char **argv,
2551 enum setstripe_origin opc)
2553 struct lfs_setstripe_args lsa = { 0 };
2554 struct llapi_stripe_param *param = NULL;
2555 struct find_param migrate_mdt_param = {
2565 unsigned long long size_units = 1;
2566 bool migrate_mode = false;
2567 bool migrate_mdt_mode = false;
2568 bool migration_block = false;
2569 __u64 migration_flags = 0;
2570 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2571 int comp_del = 0, comp_set = 0;
2574 struct llapi_layout *layout = NULL;
2575 struct llapi_layout **lpp = &layout;
2576 bool mirror_mode = false;
2577 bool has_m_file = false;
2578 __u32 mirror_count = 0;
2579 enum mirror_flags mirror_flags = 0;
2580 struct mirror_args *mirror_list = NULL;
2581 struct mirror_args *new_mirror = NULL;
2582 struct mirror_args *last_mirror = NULL;
2583 __u16 mirror_id = 0;
2585 bool from_yaml = false;
2586 bool from_copy = false;
2587 char *template = NULL;
2589 struct option long_opts[] = {
2590 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2591 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2592 /* --block is only valid in migrate mode */
2593 { .val = 'b', .name = "block", .has_arg = no_argument },
2594 { .val = LFS_COMP_ADD_OPT,
2595 .name = "comp-add", .has_arg = no_argument },
2596 { .val = LFS_COMP_ADD_OPT,
2597 .name = "component-add", .has_arg = no_argument },
2598 { .val = LFS_COMP_DEL_OPT,
2599 .name = "comp-del", .has_arg = no_argument },
2600 { .val = LFS_COMP_DEL_OPT,
2601 .name = "component-del", .has_arg = no_argument },
2602 { .val = LFS_COMP_FLAGS_OPT,
2603 .name = "comp-flags", .has_arg = required_argument },
2604 { .val = LFS_COMP_FLAGS_OPT,
2605 .name = "component-flags",
2606 .has_arg = required_argument },
2607 { .val = LFS_COMP_SET_OPT,
2608 .name = "comp-set", .has_arg = no_argument },
2609 { .val = LFS_COMP_SET_OPT,
2610 .name = "component-set",
2611 .has_arg = no_argument},
2612 { .val = LFS_COMP_NO_VERIFY_OPT,
2613 .name = "no-verify", .has_arg = no_argument},
2614 { .val = LFS_MIRROR_FLAGS_OPT,
2615 .name = "flags", .has_arg = required_argument},
2616 { .val = LFS_MIRROR_ID_OPT,
2617 .name = "mirror-id", .has_arg = required_argument},
2618 { .val = LFS_LAYOUT_COPY,
2619 .name = "copy", .has_arg = required_argument},
2620 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2621 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2622 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2623 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2624 { .val = 'd', .name = "delete", .has_arg = no_argument},
2625 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2626 /* --non-direct is only valid in migrate mode */
2627 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2628 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2629 { .val = 'E', .name = "component-end",
2630 .has_arg = required_argument},
2631 { .val = 'f', .name = "file", .has_arg = required_argument },
2632 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2633 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2634 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2635 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2636 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2637 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2638 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2639 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2640 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2641 { .val = 'L', .name = "layout", .has_arg = required_argument },
2642 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2643 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2644 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2645 /* --non-block is only valid in migrate mode */
2646 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2647 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2648 { .val = 'o', .name = "ost", .has_arg = required_argument },
2649 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2650 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2651 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2653 { .val = 'p', .name = "pool", .has_arg = required_argument },
2654 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2655 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2656 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2657 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2658 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2659 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2660 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2661 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2662 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2663 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2664 /* --verbose is only valid in migrate mode */
2665 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2666 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2669 setstripe_args_init(&lsa);
2671 migrate_mode = (opc == SO_MIGRATE);
2672 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2674 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2676 while ((c = getopt_long(argc, argv,
2677 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2683 case LFS_COMP_ADD_OPT:
2686 case LFS_COMP_DEL_OPT:
2689 case LFS_COMP_FLAGS_OPT:
2690 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2691 &lsa.lsa_comp_neg_flags);
2694 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2695 fprintf(stderr, "%s: inverted flags are not supported\n",
2699 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2701 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2708 case LFS_COMP_SET_OPT:
2711 case LFS_COMP_NO_VERIFY_OPT:
2712 mirror_flags |= MF_NO_VERIFY;
2714 case LFS_MIRROR_ID_OPT:
2715 mirror_id = strtoul(optarg, &end, 0);
2716 if (*end != '\0' || mirror_id == 0) {
2718 "%s %s: invalid mirror ID '%s'\n",
2719 progname, argv[0], optarg);
2723 case LFS_MIRROR_FLAGS_OPT: {
2726 if (!mirror_mode || !last_mirror) {
2727 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2732 result = comp_str2flags(optarg, &last_mirror->m_flags,
2738 fprintf(stderr, "%s: inverted flags are not supported\n",
2743 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2745 "%s: unsupported mirror flags: %s\n",
2752 case LFS_LAYOUT_COPY:
2757 if (!migrate_mode) {
2759 "%s %s: -b|--block valid only for migrate command\n",
2763 migration_block = true;
2766 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2769 "%s %s: invalid stripe count '%s'\n",
2770 progname, argv[0], optarg);
2774 if (lsa.lsa_stripe_count == -1)
2775 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2778 /* delete the default striping pattern */
2780 if (opc == SO_MIRROR_SPLIT) {
2783 "%s %s: -d cannot used with -f\n",
2787 mirror_flags |= MF_DESTROY;
2791 if (!migrate_mode) {
2793 "%s %s: -D|--non-direct is valid "
2794 "only for migrate command\n",
2798 migration_flags |= MIGRATION_NONDIRECT;
2801 if (lsa.lsa_comp_end != 0) {
2802 result = comp_args_to_layout(lpp, &lsa, true);
2805 "%s %s: invalid layout\n",
2810 setstripe_args_init_inherit(&lsa);
2813 if (arg_is_eof(optarg)) {
2814 lsa.lsa_comp_end = LUSTRE_EOF;
2816 result = llapi_parse_size(optarg,
2821 "%s %s: invalid component end '%s'\n",
2822 progname, argv[0], optarg);
2828 if (!migrate_mode) {
2829 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2833 lsa.lsa_pattern = check_hashtype(optarg);
2834 if (lsa.lsa_pattern == 0) {
2836 "%s %s: bad stripe hash type '%s'\n",
2837 progname, argv[0], optarg);
2842 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2845 "%s %s: invalid stripe offset '%s'\n",
2846 progname, argv[0], optarg);
2849 if (lsa.lsa_stripe_off == -1)
2850 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2853 comp_id = strtoul(optarg, &end, 0);
2854 if (*end != '\0' || comp_id == 0 ||
2855 comp_id > LCME_ID_MAX) {
2857 "%s %s: invalid component ID '%s'\n",
2858 progname, argv[0], optarg);
2863 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2865 "error: %s: invalid option: %s\n",
2866 progname, argv[optopt + 1]);
2869 if (opc == SO_MIRROR_EXTEND) {
2870 if (last_mirror == NULL) {
2872 "error: %s: '-N' must exist in front of '%s'\n",
2873 progname, argv[optopt + 1]);
2876 last_mirror->m_file = optarg;
2877 last_mirror->m_count = 1;
2880 if (mirror_list == NULL)
2881 mirror_list = lfs_mirror_alloc();
2882 mirror_list->m_file = optarg;
2887 if (strcmp(argv[optind - 1], "mdt") == 0) {
2888 /* Can be only the first component */
2889 if (layout != NULL) {
2891 fprintf(stderr, "error: 'mdt' layout "
2892 "can be only the first one\n");
2895 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2897 fprintf(stderr, "error: 'mdt' layout "
2898 "size is too big\n");
2901 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2902 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2904 fprintf(stderr, "error: layout '%s' is "
2905 "unknown, supported layouts are: "
2906 "'mdt', 'raid0'\n", argv[optind]);
2911 if (!migrate_mode) {
2913 "%s %s: -m|--mdt is valid only for migrate command\n",
2917 migrate_mdt_mode = true;
2918 lsa.lsa_nr_tgts = parse_targets(tgts,
2919 sizeof(tgts) / sizeof(__u32),
2920 lsa.lsa_nr_tgts, optarg);
2921 if (lsa.lsa_nr_tgts < 0) {
2923 "%s %s: invalid MDT target(s) '%s'\n",
2924 progname, argv[0], optarg);
2928 lsa.lsa_tgts = tgts;
2929 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2930 lsa.lsa_stripe_off = tgts[0];
2933 if (!migrate_mode) {
2935 "%s %s: -n|--non-block valid only for migrate command\n",
2939 migration_flags |= MIGRATION_NONBLOCK;
2942 if (opc == SO_SETSTRIPE) {
2943 opc = SO_MIRROR_CREATE;
2947 if (optarg != NULL) {
2948 mirror_count = strtoul(optarg, &end, 0);
2949 if (*end != '\0' || mirror_count == 0) {
2951 "error: %s: bad mirror count: %s\n",
2958 new_mirror = lfs_mirror_alloc();
2959 new_mirror->m_count = mirror_count;
2961 if (mirror_list == NULL)
2962 mirror_list = new_mirror;
2964 if (last_mirror != NULL) {
2965 /* wrap up last mirror */
2966 if (lsa.lsa_comp_end == 0)
2967 lsa.lsa_comp_end = LUSTRE_EOF;
2969 result = comp_args_to_layout(lpp, &lsa, true);
2971 lfs_mirror_free(new_mirror);
2975 setstripe_args_init_inherit(&lsa);
2977 last_mirror->m_next = new_mirror;
2980 last_mirror = new_mirror;
2981 lpp = &last_mirror->m_layout;
2984 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2985 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2986 fprintf(stderr, "warning: '--ost-list' is "
2987 "deprecated, use '--ost' instead\n");
2989 lsa.lsa_nr_tgts = parse_targets(tgts,
2990 sizeof(tgts) / sizeof(__u32),
2991 lsa.lsa_nr_tgts, optarg);
2992 if (lsa.lsa_nr_tgts < 0) {
2994 "%s %s: invalid OST target(s) '%s'\n",
2995 progname, argv[0], optarg);
2999 lsa.lsa_tgts = tgts;
3000 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3001 lsa.lsa_stripe_off = tgts[0];
3006 lsa.lsa_pool_name = optarg;
3008 if (strlen(lsa.lsa_pool_name) == 0 ||
3009 strncmp(lsa.lsa_pool_name, "none",
3010 LOV_MAXPOOLNAME) == 0)
3011 lsa.lsa_pool_name = NULL;
3014 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3018 "%s %s: invalid stripe size '%s'\n",
3019 progname, argv[0], optarg);
3024 if (!migrate_mode) {
3026 "%s %s: -v|--verbose valid only for migrate command\n",
3030 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3031 migration_flags = MIGRATION_VERBOSE;
3038 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3039 progname, argv[0], argv[optind - 1]);
3044 fname = argv[optind];
3046 if (optind == argc) {
3047 fprintf(stderr, "%s %s: FILE must be specified\n",
3052 if (mirror_mode && mirror_count == 0) {
3054 "error: %s: --mirror-count|-N option is required\n",
3061 if (lsa.lsa_comp_end == 0)
3062 lsa.lsa_comp_end = LUSTRE_EOF;