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/
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Robert Read <rread@clusterfs.com>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
57 #include <sys/types.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.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>
78 #include <lnetconfig/cyaml.h>
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_unlink_foreign(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_rmfid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_getsom(int argc, char **argv);
119 static int lfs_heat_get(int argc, char **argv);
120 static int lfs_heat_set(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
129 static int lfs_pcc_attach(int argc, char **argv);
130 static int lfs_pcc_attach_fid(int argc, char **argv);
131 static int lfs_pcc_detach(int argc, char **argv);
132 static int lfs_pcc_detach_fid(int argc, char **argv);
133 static int lfs_pcc_state(int argc, char **argv);
134 static int lfs_pcc(int argc, char **argv);
135 static int lfs_pcc_list_commands(int argc, char **argv);
136 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
137 __u64 migration_flags,
138 struct llapi_stripe_param *param,
139 struct llapi_layout *layout);
141 struct pool_to_id_cbdata {
145 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
146 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
148 enum setstripe_origin {
158 static int lfs_setstripe_internal(int argc, char **argv,
159 enum setstripe_origin opc);
161 static inline int lfs_setstripe(int argc, char **argv)
163 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
166 static inline int lfs_setstripe_migrate(int argc, char **argv)
168 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
171 static inline int lfs_mirror_create(int argc, char **argv)
173 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
176 static inline int lfs_mirror_extend(int argc, char **argv)
178 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
181 static inline int lfs_mirror_split(int argc, char **argv)
183 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
186 static inline int lfs_mirror_delete(int argc, char **argv)
188 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
191 /* Setstripe and migrate share mostly the same parameters */
192 #define SSM_CMD_COMMON(cmd) \
193 "usage: "cmd" [--component-end|-E COMP_END]\n" \
194 " [--stripe-count|-c STRIPE_COUNT]\n" \
195 " [--overstripe-count|-C STRIPE_COUNT]\n" \
196 " [--stripe-index|-i START_OST_IDX]\n" \
197 " [--stripe-size|-S STRIPE_SIZE]\n" \
198 " [--extension-size|--ext-size|-z]\n" \
199 " [--help|-h] [--layout|-L PATTERN]\n" \
200 " [--mirror_count|-N[MIRROR_COUNT]]\n" \
201 " [--ost|-o OST_INDICES]\n" \
202 " [--pool|-p POOL_NAME]\n" \
203 " [--yaml|-y YAML_TEMPLATE_FILE]\n" \
204 " [--copy=LUSTRE_SRC]\n"
206 #define SSM_HELP_COMMON \
207 "\tSTRIPE_COUNT: Number of OSTs to stripe on (0=fs default, -1 all)\n" \
208 "\t Using -C instead of -c allows overstriping, which\n" \
209 "\t will place more than one stripe per OST if\n" \
210 "\t stripe_count is greater than the number of OSTs.\n" \
211 "\tSTART_OST_IDX: OST index of first stripe (-1=default round robin)\n"\
212 "\tSTRIPE_SIZE: Number of bytes on each OST (0=fs default)\n" \
213 "\t Optional K, M, or G suffix (for KB, MB, GB\n" \
214 "\t respectively). Must be a multiple of 64KiB.\n" \
215 "\tEXTENSION_SIZE:\n" \
216 "\t Number of bytes the previous component is extended\n" \
217 "\t each time. Optional K, M, or G suffix (for KB,\n" \
218 "\t MB, GB respectively)\n" \
219 "\tPOOL_NAME: Name of OST pool to use (default none)\n" \
220 "\tLAYOUT: stripe pattern type: raid0, mdt (default raid0)\n" \
221 "\tOST_INDICES: List of OST indices, can be repeated multiple times\n"\
222 "\t Indices be specified in a format of:\n" \
223 "\t -o OST_1,OST_I-OST_J,OST_N\n" \
225 "\t -o OST_1 -o OST_I-OST_J -o OST_N\n" \
226 "\t If --pool is set with --ost then the OSTs\n" \
227 "\t must be the members of the pool.\n" \
228 "\tCOMP_END: Extent end of component, start after previous end.\n" \
229 "\t Optional K, M, or G suffix (for KiB, MiB, GiB), or\n" \
230 "\t -1 or 'eof' for max file size). Must be a multiple\n" \
231 "\t of stripe_size and a multiple of 64KiB.\n" \
232 "\tYAML_TEMPLATE_FILE:\n" \
233 "\t YAML layout template file, can't be used with -c,\n" \
234 "\t -i, -S, -p, -o, or -E arguments.\n" \
235 "\tLUSTRE_SRC: Lustre file/dir whose layout info is used to set\n" \
236 "\t another lustre file or directory, can't used with\n" \
237 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
239 #define MIRROR_CREATE_HELP \
240 "\tMIRROR_COUNT: Number of mirrors to be created with the upcoming\n" \
241 "\t setstripe layout options\n" \
242 "\t It defaults to 1 if not specified; if specified,\n" \
243 "\t it must follow the option without a space.\n" \
244 "\t The option can also be repeated multiple times to\n" \
245 "\t separate mirrors that have different layouts.\n" \
246 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
247 "\t It can be a plain layout or a composite layout.\n" \
248 "\t If not specified, the stripe options inherited\n" \
249 "\t from the previous component will be used.\n" \
250 "\tFLAGS: set flags to the component of the current mirror.\n" \
251 "\t Only \"prefer\" flag is supported so far.\n"
253 #define MIRROR_EXTEND_HELP \
255 "\tVICTIM_FILE: The layout of victim_file will be split and used\n" \
256 "\t as a mirror added to the mirrored file.\n" \
257 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
258 "\t from victim file(s) in case the victim file(s)\n" \
259 "\t contains the same data as the original mirrored\n" \
262 #define MIRROR_EXTEND_USAGE \
263 " {--mirror-count|-N[mirror_count]}\n" \
264 " [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n" \
267 #define SETSTRIPE_USAGE \
268 SSM_CMD_COMMON("setstripe") \
269 MIRROR_EXTEND_USAGE \
270 " <directory|filename>\n" \
274 #define MIGRATE_USAGE \
275 SSM_CMD_COMMON("migrate ") \
276 " [--block|-b] [--non-block|-n]\n" \
277 " [--non-direct|-D] [--verbose|-v]\n" \
281 "\tblock: Block file access during data migration (default)\n" \
282 "\tnon-block: Abort migrations if concurrent access is detected\n" \
283 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
284 "\tverbose: Print each filename as it is migrated\n" \
286 #define SETDIRSTRIPE_USAGE \
287 " [--mdt-count|-c stripe_count>\n" \
288 " [--help|-h] [--mdt-hash|-H mdt_hash]\n" \
289 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
290 " [--default|-D] [--mode|-o mode]\n" \
291 " [--max-inherit|-X max_inherit]\n" \
292 " [--max-inherit-rr max_inherit_rr] <dir>\n" \
293 "\tstripe_count: stripe count of the striped directory\n" \
294 "\tmdt_index: MDT index of first stripe\n" \
295 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
296 " crush CRUSH hash algorithm (default)\n" \
297 " fnv_1a_64 FNV-1a hash algorithm\n" \
298 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
299 "\tdefault_stripe: set default dirstripe of the directory\n" \
300 "\tmode: the file access permission of the directory (octal)\n" \
301 "To create dir with a foreign (free format) layout :\n" \
302 "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
303 "[--mode|-o MODE] [--flags HEX] <dir>\n" \
304 "\tmode: the file access permissions of the directory\n" \
305 "\tforeign_type: none, daos, symlink, or numeric value\n"
308 * command_t mirror_cmdlist - lfs mirror commands.
310 command_t mirror_cmdlist[] = {
311 { .pc_name = "create", .pc_func = lfs_mirror_create,
312 .pc_help = "Create a mirrored file.\n"
313 "usage: lfs mirror create {--mirror-count|-N[MIRROR_COUNT]}\n"
314 "\t\t[SETSTRIPE_OPTIONS] <filename|directory> ...\n"
315 MIRROR_CREATE_HELP },
316 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
317 .pc_help = "Delete a mirror from a file.\n"
318 "usage: lfs mirror delete {--mirror-id MIRROR_ID|-p POOL|\n"
319 "\t --component-id|--comp-id|-I COMP_ID} <mirror_file>\n"
321 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
322 .pc_help = "Extend a mirrored file.\n"
323 "usage: lfs mirror extend {--mirror-count|-N[MIRROR_COUNT]}\n"
324 "\t\t[--no-verify] [SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... <filename> ...\n"
325 MIRROR_EXTEND_HELP },
326 { .pc_name = "split", .pc_func = lfs_mirror_split,
327 .pc_help = "Split a mirrored file.\n"
328 "usage: lfs mirror split [--destroy|-d] [-f NEW_FILE] [--help|-h]\n"
329 "\t\t{--mirror-id MIRROR_ID|--component-id|-I COMP_ID|-p POOL}\n"
330 "\t\t<mirrored file> ...\n"
331 "\tMIRROR_ID: The numerical unique identifier for a mirror.\n"
332 "\t It can be fetched by the 'lfs getstripe' command.\n"
333 "\tCOMP_ID: Unique component ID within a mirror.\n"
334 "\tPOOL: Components using specified pool.\n"
335 "\tNEW_FILE: This option indicates the layout of the split\n"
336 "\t mirror will be stored into. If not specified,\n"
337 "\t a new file named <mirrored_file>.mirror~MIRROR_ID\n"
338 "\t will be used.\n" },
339 { .pc_name = "read", .pc_func = lfs_mirror_read,
340 .pc_help = "Read the content of a specified mirror of a file.\n"
341 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
342 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
343 { .pc_name = "write", .pc_func = lfs_mirror_write,
344 .pc_help = "Write to a specified mirror of a file.\n"
345 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
346 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
347 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
348 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
349 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
350 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
351 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
352 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
353 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]\n"
354 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
355 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
356 .pc_help = "Verify mirrored file(s).\n"
357 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
358 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
359 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
360 .pc_help = "list commands supported by lfs mirror"},
361 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
362 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
363 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
368 * command_t pcc_cmdlist - lfs pcc commands.
370 command_t pcc_cmdlist[] = {
371 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
372 .pc_help = "Attach given files to the Persistent Client Cache.\n"
373 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
374 "\t-i: archive id for RW-PCC\n" },
375 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
376 .pc_help = "Attach given files into PCC by FID(s).\n"
377 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
378 "\t-i: archive id for RW-PCC\n"
379 "\t-m: Lustre mount point\n" },
380 { .pc_name = "state", .pc_func = lfs_pcc_state,
381 .pc_help = "Display the PCC state for given files.\n"
382 "usage: lfs pcc state <file> ...\n" },
383 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
384 .pc_help = "Detach given files from the Persistent Client Cache.\n"
385 "usage: lfs pcc detach <file> ...\n" },
386 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
387 .pc_help = "Detach given files from PCC by FID(s).\n"
388 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
389 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
390 .pc_help = "list commands supported by lfs pcc"},
391 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
392 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
393 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
397 /* all available commands */
398 command_t cmdlist[] = {
399 {"setstripe", lfs_setstripe, 0,
400 "To create a file with specified striping/composite layout, or\n"
401 "create/replace the default layout on an existing directory:\n"
402 SSM_CMD_COMMON("setstripe")
404 " <directory|filename>\n"
406 "To add component(s) to an existing composite file:\n"
407 SSM_CMD_COMMON("setstripe --component-add")
409 "To totally delete the default striping from an existing directory:\n"
410 "usage: setstripe [--delete|-d] <directory>\n"
412 "To create a mirrored file or set s default mirror layout on a directory:\n"
413 "usage: setstripe {--mirror-count|-N}[MIRROR_COUNT] [SETSTRIPE_OPTIONS] <directory|filename>\n"
415 "To delete the last component(s) from an existing composite file\n"
416 "(note that this will also delete any data in those components):\n"
417 "usage: setstripe --component-del [--component-id|-I COMP_ID]\n"
418 " [--component-flags|-F COMP_FLAGS]\n"
420 "\tCOMP_ID: Unique component ID to delete\n"
421 "\tCOMP_FLAGS: 'init' indicating all instantiated components\n"
422 "\t '^init' indicating all uninstantiated components\n"
423 "\t-I and -F cannot be specified at the same time\n"
425 "To set or clear flags on a specific component\n"
426 "(note that this command can only be applied to mirrored files:\n"
427 "usage: setstripe --comp-set {-I COMP_ID|--comp-flags=COMP_FLAGS}\n"
430 "To create a file with a foreign (free format) layout:\n"
431 "usage: setstripe --foreign[=FOREIGN_TYPE]\n"
432 " --xattr|-x LAYOUT_STRING [--flags HEX]\n"
433 " [--mode MODE] <filename>\n"},
434 {"getstripe", lfs_getstripe, 0,
435 "To list the layout pattern for a given file or files in a\n"
436 "directory or recursively for all files in a directory tree.\n"
437 "usage: getstripe [--ost|-O UUID] [--quiet|-q] [--verbose|-v]\n"
438 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
439 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
440 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
441 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
442 " [--help|-h] [--component-id|-I[=COMP_ID]]\n"
443 " [--component-flags[=COMP_FLAGS]]\n"
444 " [--component-count]\n"
445 " [--extension-size|--ext-size|-z]\n"
446 " [--component-start[=[+-]COMP_START]]\n"
447 " [--component-end[=[+-]COMP_END]|-E[[+-]comp_end]]\n"
448 " [[!] --mirror-index=[+-]INDEX |\n"
449 " [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
450 " <directory|filename> ..."},
451 {"setdirstripe", lfs_setdirstripe, 0,
452 "Create striped directory on specified MDT, same as mkdir.\n"
453 "May be restricted to root or group users, depending on settings.\n"
454 "usage: setdirstripe [OPTION] <directory>\n"
456 {"getdirstripe", lfs_getdirstripe, 0,
457 "To list the layout pattern info for a given directory\n"
458 "or recursively for all directories in a directory tree.\n"
459 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
460 " [--help|-h] [--mdt-hash|-H] [--obd|-O UUID]\n"
461 " [--recursive|-r] [--yaml|-y]\n"
462 " [--verbose|-v] [--default|-D]\n"
463 " [--max-inherit|-X]\n"
464 " [--max-inherit-rr] <dir> ..."},
465 {"mkdir", lfs_setdirstripe, 0,
466 "Create striped directory on specified MDT, same as setdirstripe.\n"
467 "usage: mkdir [OPTION] <directory>\n"
469 {"rm_entry", lfs_rmentry, 0,
470 "To remove the name entry of the remote directory. Note: This\n"
471 "command will only delete the name entry, i.e. the remote directory\n"
472 "will become inaccessable after this command. This can only be done\n"
473 "by the administrator\n"
474 "usage: rm_entry <dir>\n"},
475 {"unlink_foreign", lfs_unlink_foreign, 0,
476 "To remove the foreign file/dir.\n"
477 "Note: This is for files/dirs prevented to be removed using\n"
478 "unlink/rmdir, but works also for regular ones\n"
479 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
480 {"pool_list", lfs_poollist, 0,
481 "List pools or pool OSTs\n"
482 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
483 {"find", lfs_find, 0,
484 "find files matching given attributes recursively in directory tree.\n"
485 "usage: find <directory|filename> ...\n"
486 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
487 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
488 " [[!] --btime|--Btime|-B [+-]N[smhdwy]] [--help|-h]\n"
489 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
490 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
491 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
492 " [[!] --perm [/-]mode] [[!] --pool <pool>] [--print|-P]\n"
493 " [--print0|-0] [[!] --projid <projid>]\n"
494 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
495 " [[!] --stripe-count|-c [+-]<stripes>]\n"
496 " [[!] --stripe-index|-i <index,...>]\n"
497 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
498 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
499 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
500 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
501 " [[!] --layout|-L released,raid0,mdt]\n"
502 " [[!] --foreign[=<foreign_type>]]\n"
503 " [[!] --component-count [+-]<comp_cnt>]\n"
504 " [[!] --component-start [+-]N[kMGTPE]]\n"
505 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
506 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
507 " [[!] --mirror-count|-N [+-]<n>]\n"
508 " [[!] --mirror-state <[^]state>]\n"
509 " [[!] --mdt-count|-T [+-]<stripes>]\n"
510 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
511 " [[!] --mdt-index|-m <uuid|index,...>]\n"
512 "\t !: used before an option indicates 'NOT' requested attribute\n"
513 "\t -: used before a value indicates less than requested value\n"
514 "\t +: used before a value indicates more than requested value\n"
515 "\t ^: used before a flag indicates to exclude it\n"},
516 {"check", lfs_check, 0,
517 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
518 "or all the servers (MGTs, MDTs and OSTs).\n"
519 "usage: check {mgts|osts|mdts|all}"},
520 {"osts", lfs_osts, 0, "list OSTs connected to client "
521 "[for specified path only]\n" "usage: osts [path]"},
522 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
523 "[for specified path only]\n" "usage: mdts [path]"},
525 "report filesystem disk space usage or inodes usage "
526 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
527 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
528 " [--pool|-p <fsname>[.<pool>]] [path]"},
529 {"getname", lfs_getname, 0,
530 "list instances and specified mount points [for specified path only]\n"
531 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
532 #ifdef HAVE_SYS_QUOTA_H
533 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
534 "usage: setquota {-u|-g|-p} UNAME|UID|GNAME|GID|PROJID\n"
535 " -b BLOCK_SOFTLIMIT -B BLOCK_HARDLIMIT\n"
536 " -i INODE_SOFTLIMIT -I INODE_HARDLIMIT <filesystem>\n"
537 " setquota {-u|--user USER|UID|-g|--group GROUP|GID|-p|--projid PROJID}\n"
538 " [--block-softlimit BLOCK_SOFTLIMIT]\n"
539 " [--block-hardlimit BLOCK_HARDLIMIT]\n"
540 " [--inode-softlimit INODE_SOFTLIMIT]\n"
541 " [--inode-hardlimit INODE_HARDLIMIT] <filesystem>\n"
542 " setquota [-t] {-h|--help|-u|--user|-g|--group|-p|--projid}\n"
543 " [--block-grace 'notify'|BLOCK_GRACE]\n"
544 " [--inode-grace 'notify'|INODE_GRACE] <filesystem>\n"
545 " setquota {-U|-G|-P}\n"
546 " -b BLOCK_SOFTLIMIT -B BLOCK_HARDLIMIT\n"
547 " -i INODE_SOFTLIMIT -I INODE_HARDLIMIT <filesystem>\n"
548 " setquota {-U|--default-usr|-G|--default-grp|-P|--default-prj}\n"
549 " [--block-softlimit BLOCK_SOFTLIMIT]\n"
550 " [--block-hardlimit BLOCK_HARDLIMIT]\n"
551 " [--inode-softlimit INODE_SOFTLIMIT]\n"
552 " [--inode-hardlimit INODE_HARDLIMIT] <filesystem>\n"
553 " setquota {-u|-g|-p} UNAME|UID|GNAME|GID|PROJID\n"
555 " -b can be used instead of --block-softlimit/--block-grace\n"
556 " -B can be used instead of --block-hardlimit\n"
557 " -i can be used instead of --inode-softlimit/--inode-grace\n"
558 " -I can be used instead of --inode-hardlimit\n"
559 " -d can be used instead of --default\n\n"
560 "Note: The total quota space will be split into many qunits and\n"
561 " balanced over all server targets, the minimal qunit size is\n"
562 " 1M bytes for block space and 1K inodes for inode space.\n\n"
563 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
564 " Quota space rebalancing process will stop when this mininum\n"
565 " value is reached. As a result, quota exceeded can be returned\n"
566 " while many targets still have 1MB or 1K inodes of spare\n"
568 " When setting the grace time, 'notify' can be used as grace to\n"
569 " be notified after the quota is over soft limit but prevents\n"
570 " the soft limit from becoming the hard limit."},
571 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
572 "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
573 " [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID] <filesystem>\n"
574 " quota [-o OBD_UUID|-i MDT_IDX|-I OST_IDX] -t {-u|-g|-p} <filesystem>\n"
575 " quota [-q] [-v] [h] {-U|-G|-P} <filesystem>"},
576 {"project", lfs_project, 0,
577 "Change or list project attribute for specified file or directory.\n"
578 "usage: project [-d|-r] <file|directory...>\n"
579 " list project ID and flags on file(s) or directories\n"
580 " project [-p id] [-s] [-r] <file|directory...>\n"
581 " set project ID and/or inherit flag for specified file(s) or directories\n"
582 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
583 " check project ID and flags on file(s) or directories, print outliers\n"
584 " project -C [-r] [-k] <file|directory...>\n"
585 " clear the project inherit flag and ID on the file or directory\n"
588 {"flushctx", lfs_flushctx, 0,
589 "Flush security context for current user.\n"
590 "usage: flushctx [-k] [-r] [mountpoint...]"},
591 {"changelog", lfs_changelog, 0,
592 "Show the metadata changes on an MDT."
593 "\nusage: changelog <mdtname> [startrec [endrec]]"},
594 {"changelog_clear", lfs_changelog_clear, 0,
595 "Indicate that old changelog records up to <endrec> are no longer of "
596 "interest to consumer <id>, allowing the system to free up space.\n"
597 "An <endrec> of 0 means all records.\n"
598 "usage: changelog_clear <mdtname> <id> <endrec>"},
599 {"fid2path", lfs_fid2path, 0,
600 "Resolve the full path(s) for given FID(s). For a specific hardlink "
601 "specify link number <linkno>.\n"
602 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
603 "<fsname|root> <fid>..."},
604 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
605 "usage: path2fid [--parents] <path> ..."},
606 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
607 "usage: rmfid <fsname|rootpath> <fid> ..."},
608 {"data_version", lfs_data_version, 0, "Display file data version for "
609 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
610 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
611 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
612 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
613 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
614 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
615 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
617 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
618 "[--archived] [--lost] <file> ..."},
619 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
620 "given files.\n" "usage: hsm_action <file> ..."},
621 {"hsm_archive", lfs_hsm_archive, 0,
622 "Archive file to external storage.\n"
623 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
625 {"hsm_restore", lfs_hsm_restore, 0,
626 "Restore file from external storage.\n"
627 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
628 {"hsm_release", lfs_hsm_release, 0,
629 "Release files from Lustre.\n"
630 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
631 {"hsm_remove", lfs_hsm_remove, 0,
632 "Remove file copy from external storage.\n"
633 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
635 " (FILE [FILE ...] | "
636 "--mntpath MOUNTPATH FID [FID ...])\n"
638 "Note: To remove an archived copy of a file already deleted from a "
640 "--mntpath option and a list of FIDs must be specified"
642 {"hsm_cancel", lfs_hsm_cancel, 0,
643 "Cancel requests related to specified files.\n"
644 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
645 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
646 "usage: swap_layouts <path1> <path2>"},
647 {"migrate", lfs_setstripe_migrate, 0,
648 "migrate a directory between MDTs.\n"
649 "usage: migrate [--mdt-count|-c STRIPE_COUNT]\n"
650 " [--mdt-hash|-H HASH_TYPE]\n"
651 " [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
653 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
654 " it's the MDT index of first stripe\n"
655 "\tmdt_count: number of MDTs to stripe a directory over\n"
656 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
657 " all_char (type 1)sum of characters % MDT_COUNT\n"
658 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
659 " crush (type 3)CRUSH hash algorithm\n"
661 "migrate file objects from one OST "
662 "layout\nto another (may be not safe with concurent writes).\n"
663 "usage: migrate [--stripe-count|-c STRIPE_COUNT]\n"
664 " [--overstripe-count|-C STRIPE_COUNT]\n"
665 " [--stripe-index|-i START_OST_INDEX]\n"
666 " [--stripe-size|-S STRIPE_SIZE]\n"
667 " [--pool|-p <pool_name>] [--ost|-o OST_INDICES]\n"
668 " [--block|-b] [--non-block|-n] [--non-direct|-D]\n"
669 " <file|directory>\n"
670 "\tstripe_count: number of OSTs to stripe a file over\n"
671 "\t Using -C instead of -c allows overstriping, which\n"
672 "\t will place more than one stripe per OST if\n"
673 "\t stripe_count is greater than the number of OSTs\n"
674 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
675 "\tstripe_size: number of bytes to store before moving to the next OST\n"
676 "\tpool_name: name of the predefined pool of OSTs\n"
677 "\tost_indices: OSTs to stripe over, in order\n"
678 "\tblock: Block file access during data migration (default)\n"
679 "\tnon-block: Abort migrations if concurrent access is detected\n"
680 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
682 "To move directories between MDTs. This command is deprecated, "
683 "use \"migrate\" instead.\n"
684 "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
686 {"ladvise", lfs_ladvise, 0,
687 "Provide servers with advice about access patterns for a file.\n"
688 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
689 " [--background|-b] [--unset|-u]\n\n"
690 " {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
691 " {[--mode|-m [READ,WRITE]}\n"
693 {"mirror", lfs_mirror, mirror_cmdlist,
694 "lfs commands used to manage files with mirrored components:\n"
695 "lfs mirror create - create a mirrored file or directory\n"
696 "lfs mirror extend - add mirror(s) to an existing file\n"
697 "lfs mirror split - split a mirror from an existing mirrored file\n"
698 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
699 "lfs mirror read - read a mirror content of a mirrored file\n"
700 "lfs mirror write - write to a mirror of a mirrored file\n"
701 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
702 "lfs mirror verify - verify mirrored file(s)\n"},
703 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
704 "usage: getsom [-s] [-b] [-f] <path>\n"
705 "\t-s: Only show the size value of the SOM data for a given file\n"
706 "\t-b: Only show the blocks value of the SOM data for a given file\n"
707 "\t-f: Only show the flags value of the SOM data for a given file\n"},
708 {"heat_get", lfs_heat_get, 0,
709 "To get heat of files.\n"
710 "usage: heat_get <file> ...\n"},
711 {"heat_set", lfs_heat_set, 0,
712 "To set heat flags of files.\n"
713 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
714 "\t--clear|-c: Clear file heat for given files\n"
715 "\t--off|-o: Turn off file heat for given files\n"
716 "\t--on|-O: Turn on file heat for given files\n"},
717 {"pcc", lfs_pcc, pcc_cmdlist,
718 "lfs commands used to interact with PCC features:\n"
719 "lfs pcc attach - attach given files to Persistent Client Cache\n"
720 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
721 "lfs pcc state - display the PCC state for given files\n"
722 "lfs pcc detach - detach given files from Persistent Client Cache\n"
723 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
724 {"help", Parser_help, 0, "help"},
725 {"exit", Parser_quit, 0, "quit"},
726 {"quit", Parser_quit, 0, "quit"},
727 {"--version", Parser_version, 0,
728 "output build version of the utility and exit"},
729 {"--list-commands", lfs_list_commands, 0,
730 "list commands supported by the utility and exit"},
734 static int check_hashtype(const char *hashtype)
736 int type_num = atoi(hashtype);
739 /* numeric hash type */
740 if (hashtype && strlen(hashtype) == 1 &&
741 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
743 /* string hash type */
744 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
745 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
751 static uint32_t check_foreign_type_name(const char *foreign_type_name)
755 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
756 if (!lu_foreign_types[i].lft_name)
758 if (strcmp(foreign_type_name,
759 lu_foreign_types[i].lft_name) == 0)
760 return lu_foreign_types[i].lft_type;
763 return LU_FOREIGN_TYPE_UNKNOWN;
766 static const char *error_loc = "syserror";
769 MIGRATION_NONBLOCK = 0x0001,
770 MIGRATION_MIRROR = 0x0002,
771 MIGRATION_NONDIRECT = 0x0004,
772 MIGRATION_VERBOSE = 0x0008,
776 migrate_open_files(const char *name, __u64 migration_flags,
777 const struct llapi_stripe_param *param,
778 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
785 char parent[PATH_MAX];
786 char volatile_file[PATH_MAX];
792 if (!param && !layout) {
793 error_loc = "layout information";
797 /* search for file directory pathname */
798 if (strlen(name) > sizeof(parent) - 1) {
799 error_loc = "source file name";
803 strncpy(parent, name, sizeof(parent));
804 ptr = strrchr(parent, '/');
806 if (!getcwd(parent, sizeof(parent))) {
807 error_loc = "getcwd";
811 if (ptr == parent) /* leading '/' */
816 /* open file, direct io */
817 /* even if the file is only read, WR mode is nedeed to allow
821 if (!(migration_flags & MIGRATION_NONDIRECT))
823 fd = open(name, rflags);
826 error_loc = "cannot open source file";
830 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
832 error_loc = "cannot get MDT index";
837 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
838 mode_t open_mode = S_IRUSR | S_IWUSR;
840 random_value = random();
841 rc = snprintf(volatile_file, sizeof(volatile_file),
842 "%s/%s:%.4X:%.4X:fd=%.2d", parent,
843 LUSTRE_VOLATILE_HDR, mdt_index,
845 if (rc >= sizeof(volatile_file)) {
850 /* create, open a volatile file, use caching (ie no directio) */
852 /* Returns -1 and sets errno on error: */
853 fdv = llapi_layout_file_open(volatile_file, open_flags,
858 /* Does the right thing on error: */
859 fdv = llapi_file_open_param(volatile_file, open_flags,
862 } while (fdv < 0 && (rc = fdv) == -EEXIST);
865 error_loc = "cannot create volatile file";
870 * In case the MDT does not support creation of volatile files
871 * we should try to unlink it.
873 (void)unlink(volatile_file);
876 * Not-owner (root?) special case.
877 * Need to set owner/group of volatile file like original.
878 * This will allow to pass related check during layout_swap.
883 error_loc = "cannot stat source file";
887 rc = fstat(fdv, &stv);
890 error_loc = "cannot stat volatile";
894 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
895 rc = fchown(fdv, st.st_uid, st.st_gid);
898 error_loc = "cannot change ownwership of volatile";
917 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
919 struct llapi_layout *layout;
920 size_t buf_size = 4 * 1024 * 1024;
924 size_t page_size = sysconf(_SC_PAGESIZE);
928 layout = llapi_layout_get_by_fd(fd_src, 0);
930 uint64_t stripe_size;
932 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
934 buf_size = stripe_size;
936 llapi_layout_free(layout);
939 /* Use a page-aligned buffer for direct I/O */
940 rc = posix_memalign(&buf, page_size, buf_size);
944 sparse = llapi_file_is_sparse(fd_src);
946 rc = ftruncate(fd_dst, pos);
955 size_t to_read, to_write;
958 if (sparse && pos >= data_end) {
961 data_off = llapi_data_seek(fd_src, pos, &data_size);
963 /* Non-fatal, switch to full copy */
967 /* hole at the end of file, truncate up to it */
969 rc = ftruncate(fd_dst, data_off);
973 pos = data_off & ~(page_size - 1);
974 data_end = data_off + data_size;
975 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
976 to_read = MIN(to_read, buf_size);
982 rc = check_file(fd_src);
987 rsize = pread(fd_src, buf, to_read, pos);
997 while (to_write > 0) {
1000 written = pwrite(fd_dst, buf, to_write, pos);
1006 to_write -= written;
1008 if (rc || rsize < to_read)
1016 /* Try to avoid page cache pollution after migration. */
1017 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
1018 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1024 static int migrate_copy_timestamps(int fd, int fdv)
1028 if (fstat(fd, &st) == 0) {
1029 struct timeval tv[2] = {
1030 {.tv_sec = st.st_atime},
1031 {.tv_sec = st.st_mtime}
1034 return futimes(fdv, tv);
1040 static int migrate_block(int fd, int fdv)
1047 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1049 error_loc = "cannot get dataversion";
1058 * The grouplock blocks all concurrent accesses to the file.
1059 * It has to be taken after llapi_get_data_version as it would
1062 rc = llapi_group_lock(fd, gid);
1064 error_loc = "cannot get group lock";
1068 rc = migrate_copy_data(fd, fdv, NULL);
1070 error_loc = "data copy failed";
1074 /* Make sure we keep original atime/mtime values */
1075 rc = migrate_copy_timestamps(fd, fdv);
1077 error_loc = "timestamp copy failed";
1083 * for a migration we need to check data version on file did
1086 * Pass in gid=0 since we already own grouplock.
1088 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1089 SWAP_LAYOUTS_CHECK_DV1);
1090 if (rc == -EAGAIN) {
1091 error_loc = "file changed";
1093 } else if (rc < 0) {
1094 error_loc = "cannot swap layout";
1099 rc2 = llapi_group_unlock(fd, gid);
1100 if (rc2 < 0 && rc == 0) {
1101 error_loc = "unlock group lock";
1109 * Internal helper for migrate_copy_data(). Check lease and report error if
1112 * \param[in] fd File descriptor on which to check the lease.
1114 * \retval 0 Migration can keep on going.
1115 * \retval -errno Error occurred, abort migration.
1117 static int check_lease(int fd)
1121 rc = llapi_lease_check(fd);
1123 return 0; /* llapi_check_lease returns > 0 on success. */
1128 static int migrate_nonblock(int fd, int fdv)
1134 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1136 error_loc = "cannot get data version";
1140 rc = migrate_copy_data(fd, fdv, check_lease);
1142 error_loc = "data copy failed";
1146 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1148 error_loc = "cannot get data version";
1154 error_loc = "source file changed";
1158 /* Make sure we keep original atime/mtime values */
1159 rc = migrate_copy_timestamps(fd, fdv);
1161 error_loc = "timestamp copy failed";
1169 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1171 struct pool_to_id_cbdata data = { .pool = pool };
1172 struct llapi_layout *layout = NULL;
1175 layout = llapi_layout_get_by_path(fname, 0);
1178 "error %s: file '%s' couldn't get layout: rc=%d\n",
1179 progname, fname, errno);
1183 rc = llapi_layout_sanity(layout, fname, false, true);
1185 llapi_layout_sanity_perror(errno);
1188 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1197 llapi_layout_free(layout);
1201 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1202 __u32 flags, __u32 neg_flags)
1205 __u32 flags_array[2];
1212 "error %s: neither component id nor pool is specified\n",
1216 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1222 ids[count] = comp_id;
1223 flags_array[count] = flags;
1228 if (neg_flags & LCME_FL_STALE) {
1230 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1235 ids[count] = comp_id;
1236 flags_array[count] = neg_flags | LCME_FL_NEG;
1240 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1242 if (errno == EUCLEAN) {
1245 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1246 progname, comp_id, fname);
1249 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1250 progname, comp_id, fname, flags, neg_flags);
1257 static int lfs_component_del(char *fname, __u32 comp_id,
1258 __u32 flags, __u32 neg_flags)
1262 if (flags && neg_flags)
1265 if (!flags && neg_flags)
1266 flags = neg_flags | LCME_FL_NEG;
1268 if ((flags && comp_id) || (!flags && !comp_id))
1272 if (flags & ~LCME_KNOWN_FLAGS) {
1274 "%s setstripe: unknown flags %#x\n",
1278 } else if (comp_id > LCME_ID_MAX) {
1279 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1284 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1287 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1288 progname, comp_id, fname, strerror(errno));
1292 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1299 rc = llapi_layout_file_comp_add(fname, layout);
1301 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1302 fname, strerror(errno));
1306 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1307 struct llapi_layout *layout)
1315 fd = lstat(fname, &st);
1316 if (fd == 0 && S_ISDIR(st.st_mode))
1317 open_flags = O_DIRECTORY | O_RDONLY;
1319 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1321 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1322 S_ISDIR(st.st_mode) ?
1323 "set default composite layout for" :
1324 "create composite file",
1325 fname, strerror(errno));
1329 static int lfs_migrate(char *name, __u64 migration_flags,
1330 struct llapi_stripe_param *param,
1331 struct llapi_layout *layout)
1333 struct llapi_layout *existing;
1334 uint64_t dom_new, dom_cur;
1339 rc = migrate_open_files(name, migration_flags, param, layout,
1344 rc = llapi_layout_dom_size(layout, &dom_new);
1346 error_loc = "cannot get new layout DoM size";
1349 /* special case for migration to DOM layout*/
1350 existing = llapi_layout_get_by_fd(fd, 0);
1352 error_loc = "cannot get existing layout";
1356 rc = llapi_layout_dom_size(existing, &dom_cur);
1358 error_loc = "cannot get current layout DoM size";
1363 * if file has DoM layout already then migration is possible to
1364 * the new layout with the same DoM component via swap layout,
1365 * if new layout used bigger DOM size, then mirroring is used
1367 if (dom_new > dom_cur) {
1368 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1371 error_loc = "cannot migrate to DOM layout";
1375 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1377 * Blocking mode (forced if servers do not support file lease).
1378 * It is also the default mode, since we cannot distinguish
1379 * between a broken lease and a server that does not support
1380 * atomic swap/close (LU-6785)
1382 rc = migrate_block(fd, fdv);
1386 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1388 error_loc = "cannot get lease";
1392 rc = migrate_nonblock(fd, fdv);
1394 llapi_lease_release(fd);
1399 * Atomically put lease, swap layouts and close.
1400 * for a migration we need to check data version on file did
1403 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1405 error_loc = "cannot swap layout";
1417 fprintf(stderr, "error: %s: %s: %s: %s\n",
1418 progname, name, error_loc, strerror(-rc));
1419 else if (migration_flags & MIGRATION_VERBOSE)
1420 printf("%s\n", name);
1425 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1428 char *dup_string = NULL;
1434 if (!string || !string[0])
1437 dup_string = strdup(string);
1439 llapi_printf(LLAPI_MSG_ERROR,
1440 "%s: insufficient memory\n",
1445 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1449 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1450 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1451 const char *comp_name = comp_flags_table[i].cfn_name;
1453 if (strcmp(name, comp_name) == 0) {
1454 *flags |= comp_flag;
1456 } else if (strncmp(name, "^", 1) == 0 &&
1457 strcmp(name + 1, comp_name) == 0) {
1458 *neg_flags |= comp_flag;
1463 llapi_printf(LLAPI_MSG_ERROR,
1464 "%s: component flag '%s' not supported\n",
1471 if (!*flags && !*neg_flags)
1474 /* don't allow to set and exclude the same flag */
1475 if (*flags & *neg_flags)
1483 static int mdthash_input(char *string, __u32 *inflags,
1484 __u32 *exflags, __u32 *type)
1491 {"migrating", LMV_HASH_FLAG_MIGRATION},
1492 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1493 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1502 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1506 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1507 if (strcmp(name, mhflist[i].name) == 0 ||
1508 name[0] == mhflist[i].name[0]) {
1509 *inflags |= mhflist[i].flag;
1511 } else if (name[0] == '^' &&
1512 (strcmp(name + 1, mhflist[i].name) == 0 ||
1513 name[1] == mhflist[i].name[0])) {
1514 *exflags |= mhflist[i].flag;
1519 i = check_hashtype(name);
1524 llapi_printf(LLAPI_MSG_ERROR,
1525 "%s: invalid mdt_hash value '%s'\n",
1531 /* don't allow to include and exclude the same flag */
1532 if (*inflags & *exflags) {
1533 llapi_printf(LLAPI_MSG_ERROR,
1534 "%s: include and exclude same flag '%s'\n",
1542 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1550 if (strncmp(string, "^", 1) == 0) {
1551 *neg_state = llapi_layout_string_flags(string + 1);
1552 if (*neg_state != 0)
1555 *state = llapi_layout_string_flags(string);
1560 llapi_printf(LLAPI_MSG_ERROR,
1561 "%s: mirrored file state '%s' not supported\n",
1567 * struct mirror_args - Command-line arguments for mirror(s).
1568 * @m_count: Number of mirrors to be created with this layout.
1569 * @m_flags: Mirror level flags, only 'prefer' is supported.
1570 * @m_layout: Mirror layout.
1571 * @m_file: A victim file. Its layout will be split and used as a mirror.
1572 * @m_next: Point to the next node of the list.
1574 * Command-line arguments for mirror(s) will be parsed and stored in
1575 * a linked list that consists of this structure.
1577 struct mirror_args {
1580 struct llapi_layout *m_layout;
1582 struct mirror_args *m_next;
1587 * enum mirror_flags - Flags for extending a mirrored file.
1588 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1589 * in case the victim file(s) contains the same data as the
1590 * original mirrored file.
1591 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1592 * @MF_COMP_ID: specified component id instead of mirror id
1594 * Flags for extending a mirrored file.
1604 * mirror_create_sanity_check() - Check mirror list.
1605 * @list: A linked list that stores the mirror arguments.
1607 * This function does a sanity check on @list for creating
1610 * Return: 0 on success or a negative error code on failure.
1612 static int mirror_create_sanity_check(const char *fname,
1613 struct mirror_args *list,
1617 bool has_m_file = false;
1618 bool has_m_layout = false;
1623 if (fname && check_fname) {
1624 struct llapi_layout *layout;
1626 layout = llapi_layout_get_by_path(fname, 0);
1629 "error: %s: file '%s' couldn't get layout\n",
1634 rc = llapi_layout_sanity(layout, fname, false, true);
1636 llapi_layout_free(layout);
1639 llapi_layout_sanity_perror(rc);
1647 llapi_layout_free(list->m_layout);
1650 llapi_layout_get_by_path(list->m_file, 0);
1651 if (!list->m_layout) {
1653 "error: %s: file '%s' has no layout\n",
1654 progname, list->m_file);
1658 has_m_layout = true;
1659 if (!list->m_layout) {
1660 fprintf(stderr, "error: %s: no mirror layout\n",
1666 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1668 llapi_layout_sanity_perror(rc);
1672 list = list->m_next;
1675 if (has_m_file && has_m_layout) {
1677 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1685 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1687 __u32 mirror_flags = *(__u32 *)cbdata;
1691 rc = llapi_layout_comp_flags_get(layout, &flags);
1696 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1701 return LLAPI_LAYOUT_ITER_CONT;
1705 * mirror_create() - Create a mirrored file.
1706 * @fname: The file to be created.
1707 * @mirror_list: A linked list that stores the mirror arguments.
1709 * This function creates a mirrored file @fname with the mirror(s)
1710 * from @mirror_list.
1712 * Return: 0 on success or a negative error code on failure.
1714 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1716 struct llapi_layout *layout = NULL;
1717 struct mirror_args *cur_mirror = NULL;
1718 uint16_t mirror_count = 0;
1722 rc = mirror_create_sanity_check(fname, mirror_list, false);
1726 cur_mirror = mirror_list;
1727 while (cur_mirror) {
1728 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1730 &cur_mirror->m_flags);
1733 fprintf(stderr, "%s: failed to set mirror flags\n",
1738 for (i = 0; i < cur_mirror->m_count; i++) {
1739 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1743 "error: %s: merge layout failed: %s\n",
1744 progname, strerror(errno));
1748 mirror_count += cur_mirror->m_count;
1749 cur_mirror = cur_mirror->m_next;
1753 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1757 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1760 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1761 progname, strerror(errno));
1765 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1773 llapi_layout_free(layout);
1778 * Compare files and check lease on @fd.
1780 * \retval bytes number of bytes are the same
1782 static ssize_t mirror_file_compare(int fd, int fdv)
1784 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1786 ssize_t bytes_done = 0;
1787 ssize_t bytes_read = 0;
1789 buf = malloc(buflen * 2);
1794 if (!llapi_lease_check(fd)) {
1795 bytes_done = -EBUSY;
1799 bytes_read = read(fd, buf, buflen);
1800 if (bytes_read <= 0)
1803 if (bytes_read != read(fdv, buf + buflen, buflen))
1807 * XXX: should compute the checksum on each buffer and then
1808 * compare checksum to avoid cache collision
1810 if (memcmp(buf, buf + buflen, bytes_read))
1813 bytes_done += bytes_read;
1821 static int mirror_extend_file(const char *fname, const char *victim_file,
1822 enum mirror_flags mirror_flags)
1827 struct stat stbuf_v;
1828 struct ll_ioc_lease *data = NULL;
1831 fd = open(fname, O_RDWR);
1833 error_loc = "open source file";
1838 fdv = open(victim_file, O_RDWR);
1840 error_loc = "open target file";
1845 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1846 error_loc = "stat source or target file";
1851 if (stbuf.st_dev != stbuf_v.st_dev) {
1852 error_loc = "stat source and target file";
1857 /* mirrors should be of the same size */
1858 if (stbuf.st_size != stbuf_v.st_size) {
1859 error_loc = "file sizes don't match";
1864 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1866 error_loc = "cannot get lease";
1870 if (!(mirror_flags & MF_NO_VERIFY)) {
1872 /* mirrors should have the same contents */
1873 ret = mirror_file_compare(fd, fdv);
1874 if (ret != stbuf.st_size) {
1875 error_loc = "file busy or contents don't match";
1876 rc = ret < 0 ? ret : -EINVAL;
1881 /* Get rid of caching pages from clients */
1882 rc = llapi_file_flush(fd);
1884 error_loc = "cannot get data version";
1888 rc = llapi_file_flush(fdv);
1890 error_loc = "cannot get data version";
1894 /* Make sure we keep original atime/mtime values */
1895 rc = migrate_copy_timestamps(fd, fdv);
1897 error_loc = "cannot copy timestamp";
1901 /* Atomically put lease, merge layouts and close. */
1902 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1904 error_loc = "memory allocation";
1907 data->lil_mode = LL_LEASE_UNLCK;
1908 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1909 data->lil_count = 1;
1910 data->lil_ids[0] = fdv;
1911 rc = llapi_lease_set(fd, data);
1913 error_loc = "cannot merge layout";
1915 } else if (rc == 0) {
1917 error_loc = "lost lease lock";
1930 (void) unlink(victim_file);
1932 fprintf(stderr, "error: %s: %s: %s: %s\n",
1933 progname, fname, error_loc, strerror(-rc));
1937 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1938 bool inherit, uint32_t flags)
1940 struct llapi_layout *f_layout = NULL;
1941 struct ll_ioc_lease *data = NULL;
1947 f_layout = llapi_layout_get_by_path(name, 0);
1949 fprintf(stderr, "%s: cannot get layout\n", progname);
1952 rc = llapi_layout_get_last_init_comp(f_layout);
1954 fprintf(stderr, "%s: cannot get the last init comp\n",
1958 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1961 "%s: cannot inherit from the last init comp\n",
1966 llapi_layout_comp_flags_set(m_layout, flags);
1967 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1972 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1974 error_loc = "cannot get lease";
1978 rc = migrate_nonblock(fd, fdv);
1980 llapi_lease_release(fd);
1984 /* Atomically put lease, merge layouts and close. */
1985 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1987 error_loc = "memory allocation";
1990 data->lil_mode = LL_LEASE_UNLCK;
1991 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1992 data->lil_count = 1;
1993 data->lil_ids[0] = fdv;
1994 rc = llapi_lease_set(fd, data);
1996 error_loc = "cannot merge layout";
1998 } else if (rc == 0) {
2000 error_loc = "lost lease lock";
2013 fprintf(stderr, "error: %s: %s: %s: %s\n",
2014 progname, name, error_loc, strerror(-rc));
2018 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2019 enum mirror_flags mirror_flags)
2023 rc = mirror_create_sanity_check(fname, mirror_list, true);
2027 while (mirror_list) {
2028 if (mirror_list->m_file) {
2029 rc = mirror_extend_file(fname, mirror_list->m_file,
2032 __u32 mirror_count = mirror_list->m_count;
2034 while (mirror_count > 0) {
2035 rc = mirror_extend_layout(fname,
2036 mirror_list->m_layout,
2037 mirror_list->m_inherit,
2038 mirror_list->m_flags);
2048 mirror_list = mirror_list->m_next;
2054 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2059 rc = llapi_layout_mirror_id_get(layout, &id);
2063 if ((__u16)id == *(__u16 *)cbdata)
2064 return LLAPI_LAYOUT_ITER_STOP;
2066 return LLAPI_LAYOUT_ITER_CONT;
2069 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2074 rc = llapi_layout_comp_id_get(layout, &id);
2078 if (id == *(__u32 *)cbdata)
2079 return LLAPI_LAYOUT_ITER_STOP;
2081 return LLAPI_LAYOUT_ITER_CONT;
2084 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2086 char buf[LOV_MAXPOOLNAME + 1];
2087 struct pool_to_id_cbdata *d = (void *)cbdata;
2091 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2094 if (strcmp(d->pool, buf))
2095 return LLAPI_LAYOUT_ITER_CONT;
2097 rc = llapi_layout_mirror_id_get(layout, &id);
2102 return LLAPI_LAYOUT_ITER_STOP;
2105 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2107 char buf[LOV_MAXPOOLNAME + 1];
2108 struct pool_to_id_cbdata *d = (void *)cbdata;
2112 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2115 if (strcmp(d->pool, buf))
2116 return LLAPI_LAYOUT_ITER_CONT;
2118 rc = llapi_layout_comp_id_get(layout, &id);
2123 return LLAPI_LAYOUT_ITER_STOP;
2126 struct collect_ids_data {
2132 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2134 struct collect_ids_data *cid = cbdata;
2138 rc = llapi_layout_mirror_id_get(layout, &id);
2142 if ((__u16)id != cid->cid_exclude) {
2145 for (i = 0; i < cid->cid_count; i++) {
2146 /* already collected the mirror id */
2147 if (id == cid->cid_ids[i])
2148 return LLAPI_LAYOUT_ITER_CONT;
2150 cid->cid_ids[cid->cid_count] = id;
2154 return LLAPI_LAYOUT_ITER_CONT;
2158 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2159 * @mirror_id: Mirror id to be checked.
2160 * @layout: Mirror component list.
2162 * This function checks if a mirror with specified @mirror_id is the last
2163 * non-stale mirror of a layout @layout.
2165 * Return: true or false.
2168 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2170 __u16 mirror_ids[128] = { 0 };
2171 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2173 .cid_exclude = mirror_id, };
2176 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2178 for (i = 0; i < cid.cid_count; i++) {
2179 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2182 comp_size = llapi_mirror_find_stale(layout, comp_array,
2183 ARRAY_SIZE(comp_array),
2192 static int mirror_split(const char *fname, __u32 id, const char *pool,
2193 enum mirror_flags mflags, const char *victim_file)
2195 struct llapi_layout *layout;
2196 char parent[PATH_MAX];
2197 char victim[PATH_MAX];
2198 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2200 struct ll_ioc_lease *data;
2201 uint16_t mirror_count;
2205 bool purge = true; /* delete mirror by setting fdv=fd */
2209 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2211 "error %s: the source file '%s' and -f file are the same\n",
2216 /* check fname contains mirror with mirror_id/comp_id */
2217 layout = llapi_layout_get_by_path(fname, 0);
2220 "error %s: file '%s' couldn't get layout\n",
2225 rc = llapi_layout_sanity(layout, fname, false, true);
2227 llapi_layout_sanity_perror(rc);
2231 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2234 "error %s: file '%s' couldn't get mirror count\n",
2238 if (mirror_count < 2) {
2240 "error %s: file '%s' has %d component, cannot split\n",
2241 progname, fname, mirror_count);
2245 if (mflags & MF_COMP_POOL) {
2246 struct pool_to_id_cbdata data = { .pool = pool };
2248 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2250 mirror_id = data.id;
2251 } else if (mflags & MF_COMP_ID) {
2252 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2253 mirror_id = mirror_id_of(id);
2255 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2259 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2262 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2263 if (mflags & MF_COMP_POOL) {
2265 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2266 progname, fname, pool);
2268 } else if (mflags & MF_COMP_ID) {
2270 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2271 progname, fname, id);
2275 "error %s: file '%s' does not contain mirror with id %u\n",
2276 progname, fname, id);
2281 fd = open(fname, O_RDWR);
2284 "error %s: open file '%s' failed: %s\n",
2285 progname, fname, strerror(errno));
2289 /* get victim file directory pathname */
2290 if (strlen(fname) > sizeof(parent) - 1) {
2291 fprintf(stderr, "error %s: file name of '%s' too long\n",
2296 strncpy(parent, fname, sizeof(parent));
2297 ptr = strrchr(parent, '/');
2299 if (!getcwd(parent, sizeof(parent))) {
2300 fprintf(stderr, "error %s: getcwd failed: %s\n",
2301 progname, strerror(errno));
2311 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2313 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2318 rc = llapi_file_is_encrypted(fd);
2320 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2321 progname, fname, rc);
2328 /* use a temp file to store the splitted layout */
2329 if (mflags & MF_DESTROY) {
2330 char file_path[PATH_MAX];
2331 unsigned int rnumber;
2334 if (last_non_stale_mirror(mirror_id, layout)) {
2337 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2343 /* don't use volatile file for mirror destroy */
2347 * try the old way to delete mirror using
2352 rc = snprintf(file_path,
2354 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2358 rc >= sizeof(file_path)) {
2359 fdv = -ENAMETOOLONG;
2363 open_flags = O_RDWR |
2364 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2365 O_CREAT | O_EXCL | O_NOFOLLOW;
2366 fdv = open(file_path, open_flags,
2370 } while (fdv < 0 && rc == -EEXIST);
2376 "error %s: not permitted on encrypted file '%s': %d\n",
2377 progname, fname, rc);
2381 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2383 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2386 /* user specified victim file */
2390 "error %s: not permitted on encrypted file '%s': %d\n",
2391 progname, fname, rc);
2394 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2399 "error %s: create victim file failed: %s\n",
2400 progname, strerror(errno));
2404 /* get lease lock of fname */
2405 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2408 "error %s: cannot get lease of file '%s': %d\n",
2409 progname, fname, rc);
2413 /* Atomatically put lease, split layouts and close. */
2414 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2420 data->lil_mode = LL_LEASE_UNLCK;
2421 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2422 data->lil_count = 2;
2423 data->lil_ids[0] = fdv;
2424 data->lil_ids[1] = mirror_id;
2425 rc = llapi_lease_set(fd, data);
2427 if (rc == -EINVAL && purge) {
2428 /* could be old MDS which prohibit fd==fdv */
2433 if (rc == 0) /* lost lease lock */
2436 "error %s: cannot split '%s': %s\n",
2437 progname, fname, strerror(-rc));
2449 llapi_layout_free(layout);
2454 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2455 __u16 *mirror_ids, int ids_nr);
2457 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2458 __u64 migration_flags,
2459 struct llapi_stripe_param *param,
2460 struct llapi_layout *layout)
2462 struct ll_ioc_lease *data = NULL;
2465 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2467 error_loc = "cannot get lease";
2471 /* Atomically put lease, merge layouts, resync and close. */
2472 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2474 error_loc = "memory allocation";
2477 data->lil_mode = LL_LEASE_UNLCK;
2478 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2479 data->lil_count = 1;
2480 data->lil_ids[0] = fdv;
2481 rc = llapi_lease_set(fd, data);
2483 error_loc = "cannot merge layout";
2485 } else if (rc == 0) {
2487 error_loc = "lost lease lock";
2493 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2495 error_loc = "cannot resync file";
2499 /* delete first mirror now */
2500 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2502 error_loc = "cannot delete old layout";
2510 fprintf(stderr, "error: %s: %s: %s: %s\n",
2511 progname, name, error_loc, strerror(-rc));
2512 else if (migration_flags & MIGRATION_VERBOSE)
2513 printf("%s\n", name);
2520 * Parse a string containing an target index list into an array of integers.
2522 * The input string contains a comma delimited list of individual
2523 * indices and ranges, for example "1,2-4,7". Add the indices into the
2524 * \a tgts array and remove duplicates.
2526 * \param[out] tgts array to store indices in
2527 * \param[in] size size of \a tgts array
2528 * \param[in] offset starting index in \a tgts
2529 * \param[in] arg string containing OST index list
2530 * \param[in/out] overstriping index list may contain duplicates
2532 * \retval positive number of indices in \a tgts
2533 * \retval -EINVAL unable to parse \a arg
2535 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2536 unsigned long long *pattern)
2540 int slots = size - offset;
2542 bool overstriped = false;
2548 end_of_loop = false;
2549 while (!end_of_loop) {
2550 int start_index = 0;
2553 char *endptr = NULL;
2557 ptr = strchrnul(arg, ',');
2559 end_of_loop = *ptr == '\0';
2563 start_index = strtol(arg, &endptr, 0);
2564 if (endptr == arg) /* no data at all */
2566 if (errno != 0 || start_index < -1 ||
2567 (*endptr != '-' && *endptr != '\0'))
2570 end_index = start_index;
2571 if (*endptr == '-') {
2573 end_index = strtol(endptr + 1, &endptr, 0);
2574 if (errno != 0 || *endptr != '\0' || end_index < -1)
2576 if (end_index < start_index)
2580 for (i = start_index; i <= end_index && slots > 0; i++) {
2583 /* remove duplicate */
2584 for (j = 0; j < offset; j++) {
2585 if (tgts[j] == i && pattern &&
2586 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2588 else if (tgts[j] == i)
2594 if (j == offset) { /* check complete */
2600 if (slots == 0 && i < end_index)
2608 if (!end_of_loop && ptr)
2611 if (!overstriped && pattern)
2612 *pattern = LLAPI_LAYOUT_DEFAULT;
2614 return rc < 0 ? rc : nr;
2617 struct lfs_setstripe_args {
2618 unsigned long long lsa_comp_end;
2619 unsigned long long lsa_stripe_size;
2620 unsigned long long lsa_extension_size;
2621 long long lsa_stripe_count;
2622 long long lsa_stripe_off;
2623 __u32 lsa_comp_flags;
2624 __u32 lsa_comp_neg_flags;
2625 unsigned long long lsa_pattern;
2626 unsigned int lsa_mirror_count;
2628 bool lsa_first_comp;
2629 bool lsa_extension_comp;
2631 char *lsa_pool_name;
2634 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2636 unsigned int mirror_count = lsa->lsa_mirror_count;
2637 bool first_comp = lsa->lsa_first_comp;
2639 memset(lsa, 0, sizeof(*lsa));
2641 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2642 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2643 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2644 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2645 lsa->lsa_pool_name = NULL;
2647 lsa->lsa_mirror_count = mirror_count;
2648 lsa->lsa_first_comp = first_comp;
2652 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2653 * @lsa: Stripe options to be initialized and inherited.
2655 * This function initializes stripe options in @lsa and inherit
2656 * stripe_size, stripe_count and OST pool_name options.
2660 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2662 unsigned long long stripe_size;
2663 long long stripe_count;
2664 char *pool_name = NULL;
2666 stripe_size = lsa->lsa_stripe_size;
2667 stripe_count = lsa->lsa_stripe_count;
2668 pool_name = lsa->lsa_pool_name;
2670 setstripe_args_init(lsa);
2672 lsa->lsa_stripe_size = stripe_size;
2673 lsa->lsa_stripe_count = stripe_count;
2674 lsa->lsa_pool_name = pool_name;
2677 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2679 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2680 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2681 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2682 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2683 lsa->lsa_comp_end != 0);
2686 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2688 if (lsa->lsa_nr_tgts) {
2689 if (lsa->lsa_nr_tgts < 0 ||
2690 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2691 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2697 if (lsa->lsa_stripe_count > 0 &&
2698 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2699 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2700 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2701 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2702 lsa->lsa_stripe_count,
2714 * comp_args_to_layout() - Create or extend a composite layout.
2715 * @composite: Pointer to the composite layout.
2716 * @lsa: Stripe options for the new component.
2718 * This function creates or extends a composite layout by adding a new
2719 * component with stripe options from @lsa.
2721 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2723 * Return: 0 on success or an error code on failure.
2725 static int comp_args_to_layout(struct llapi_layout **composite,
2726 struct lfs_setstripe_args *lsa,
2729 struct llapi_layout *layout = *composite;
2730 uint64_t prev_end = 0;
2736 layout = llapi_layout_alloc();
2738 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2743 *composite = layout;
2744 lsa->lsa_first_comp = true;
2749 * Get current component extent, current component
2750 * must be the tail component.
2752 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2754 fprintf(stderr, "Get comp extent failed. %s\n",
2759 if (lsa->lsa_first_comp) {
2761 rc = llapi_layout_add_first_comp(layout);
2763 rc = llapi_layout_comp_add(layout);
2766 fprintf(stderr, "Add component failed. %s\n",
2772 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2774 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2775 lsa->lsa_comp_flags, strerror(errno));
2780 uint64_t comp_end = lsa->lsa_comp_end;
2783 * The extendable component is 0-length, so it can be removed
2784 * if there is insufficient space to extend it.
2786 if (lsa->lsa_extension_comp)
2787 comp_end = prev_end;
2789 rc = llapi_layout_comp_extent_set(layout, prev_end,
2792 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2793 prev_end, comp_end, strerror(errno));
2797 /* reset lsa_first_comp */
2798 lsa->lsa_first_comp = false;
2800 /* Data-on-MDT component setting */
2801 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2803 * In case of Data-on-MDT patterns the only extra option
2804 * applicable is stripe size option.
2806 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2808 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2809 lsa->lsa_stripe_count);
2813 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2815 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2816 lsa->lsa_stripe_size);
2820 if (lsa->lsa_nr_tgts != 0) {
2822 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2827 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2829 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2830 lsa->lsa_stripe_off);
2834 if (lsa->lsa_pool_name != 0) {
2836 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2837 lsa->lsa_pool_name);
2842 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2844 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2849 /* Data-on-MDT component has always single stripe up to end */
2850 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2851 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2852 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2854 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2861 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2862 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2864 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2865 rc = llapi_layout_extension_size_set(layout, size);
2867 rc = llapi_layout_stripe_size_set(layout, size);
2870 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2871 size, strerror(errno));
2875 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2877 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2878 lsa->lsa_stripe_count, strerror(errno));
2882 if (lsa->lsa_pool_name) {
2883 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2885 fprintf(stderr, "Set pool name: %s failed. %s\n",
2886 lsa->lsa_pool_name, strerror(errno));
2890 rc = llapi_layout_pool_name_set(layout, "");
2892 fprintf(stderr, "Clear pool name failed: %s\n",
2898 rc = lsa_args_stripe_count_check(lsa);
2902 if (lsa->lsa_nr_tgts > 0) {
2905 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2906 rc = llapi_layout_ost_index_set(layout, i,
2911 /* Make sure stripe offset is in OST list. */
2912 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2916 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2917 lsa->lsa_stripe_off);
2921 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2922 lsa->lsa_stripe_off != -1) {
2923 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2926 fprintf(stderr, "Set ost index %d failed. %s\n",
2927 i, strerror(errno));
2931 /* Create the second, virtual component of extension space */
2932 if (lsa->lsa_extension_comp) {
2933 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2934 lsa->lsa_extension_comp = false;
2941 static int build_component(struct llapi_layout **layout,
2942 struct lfs_setstripe_args *lsa, bool set_extent)
2946 rc = comp_args_to_layout(layout, lsa, set_extent);
2950 if (lsa->lsa_mirror_count > 0) {
2951 rc = llapi_layout_mirror_count_set(*layout,
2952 lsa->lsa_mirror_count);
2956 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2959 lsa->lsa_mirror_count = 0;
2965 static int build_prev_component(struct llapi_layout **layout,
2966 struct lfs_setstripe_args *prev,
2967 struct lfs_setstripe_args *lsa,
2970 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2973 if (prev->lsa_stripe_size) {
2975 prev->lsa_comp_end = lsa->lsa_comp_end;
2976 prev->lsa_extension_size = lsa->lsa_extension_size;
2977 prev->lsa_extension_comp = true;
2980 rc = build_component(layout, prev, true);
2986 * Copy lsa to previous lsa;
2987 * if this is an extension component, make the previous invalid;
2990 prev->lsa_stripe_size = 0;
2997 static int build_layout_from_yaml_node(struct cYAML *node,
2998 struct llapi_layout **layout,
2999 struct lfs_setstripe_args *lsa,
3000 struct lfs_setstripe_args *prevp)
3002 struct lfs_setstripe_args prev = { 0 };
3003 __u32 *osts = lsa->lsa_tgts;
3011 string = node->cy_string;
3013 if (node->cy_type == CYAML_TYPE_OBJECT) {
3014 /* go deep to sub blocks */
3015 if (string && !strncmp(string, "component", 9) &&
3016 strncmp(string, "component0", 10) &&
3017 strncmp(string, "components", 10)) {
3018 rc = build_prev_component(layout, prevp, lsa,
3023 /* initialize lsa. */
3024 setstripe_args_init(lsa);
3025 lsa->lsa_first_comp = false;
3026 lsa->lsa_tgts = osts;
3029 rc = build_layout_from_yaml_node(node->cy_child, layout,
3034 if (!node->cy_string)
3037 /* skip leading lmm_ if present, to simplify parsing */
3038 if (strncmp(string, "lmm_", 4) == 0)
3041 if (node->cy_type == CYAML_TYPE_STRING) {
3042 if (!strcmp(string, "lcme_extent.e_end")) {
3043 if (!strcmp(node->cy_valuestring, "EOF") ||
3044 !strcmp(node->cy_valuestring, "eof"))
3045 lsa->lsa_comp_end = LUSTRE_EOF;
3046 } else if (!strcmp(string, "pool")) {
3047 lsa->lsa_pool_name = node->cy_valuestring;
3048 } else if (!strcmp(string, "pattern")) {
3049 if (!strcmp(node->cy_valuestring, "mdt"))
3050 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3051 if (!strcmp(node->cy_valuestring,
3052 "raid0,overstriped"))
3054 LLAPI_LAYOUT_OVERSTRIPING;
3055 } else if (!strcmp(string, "lcme_flags")) {
3056 rc = comp_str2flags(node->cy_valuestring,
3057 &lsa->lsa_comp_flags,
3058 &lsa->lsa_comp_neg_flags);
3062 * Only template flags have meaning in
3063 * the layout for a new file
3065 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3067 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3068 if (!strcmp(string, "lcm_mirror_count")) {
3069 lsa->lsa_mirror_count = node->cy_valueint;
3070 } else if (!strcmp(string, "lcme_extent.e_start")) {
3071 if (node->cy_valueint == 0)
3072 lsa->lsa_first_comp = true;
3073 } else if (!strcmp(string, "lcme_extent.e_end")) {
3074 if (node->cy_valueint == -1)
3075 lsa->lsa_comp_end = LUSTRE_EOF;
3077 lsa->lsa_comp_end = node->cy_valueint;
3078 } else if (!strcmp(string, "stripe_count")) {
3079 lsa->lsa_stripe_count = node->cy_valueint;
3080 } else if (!strcmp(string, "stripe_size")) {
3081 lsa->lsa_stripe_size = node->cy_valueint;
3082 } else if (!strcmp(string, "extension_size")) {
3083 lsa->lsa_extension_size = node->cy_valueint;
3084 lsa->lsa_extension_comp = true;
3085 } else if (!strcmp(string, "stripe_offset")) {
3086 lsa->lsa_stripe_off = node->cy_valueint;
3087 } else if (!strcmp(string, "l_ost_idx")) {
3088 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3093 node = node->cy_next;
3096 if (prevp == &prev) {
3097 rc = build_prev_component(layout, prevp, lsa, true);
3101 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3102 rc = build_component(layout, lsa, *layout != NULL);
3108 static int lfs_comp_create_from_yaml(char *template,
3109 struct llapi_layout **layout,
3110 struct lfs_setstripe_args *lsa,
3113 struct cYAML *tree = NULL, *err_rc = NULL;
3116 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3118 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3119 progname, template);
3120 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3121 "can't parse", &err_rc);
3122 cYAML_print_tree2file(stderr, err_rc);
3123 cYAML_free_tree(err_rc);