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
820 rflags = O_RDWR | O_NOATIME;
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_set_timestamps(int fd, const struct stat *st)
1026 struct timeval tv[2] = {
1027 {.tv_sec = st->st_atime},
1028 {.tv_sec = st->st_mtime}
1031 return futimes(fd, tv);
1034 static int migrate_block(int fd, int fdv)
1042 rc = fstat(fd, &st);
1044 error_loc = "cannot stat source file";
1048 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1050 error_loc = "cannot get dataversion";
1059 * The grouplock blocks all concurrent accesses to the file.
1060 * It has to be taken after llapi_get_data_version as it would
1063 rc = llapi_group_lock(fd, gid);
1065 error_loc = "cannot get group lock";
1069 rc = migrate_copy_data(fd, fdv, NULL);
1071 error_loc = "data copy failed";
1075 /* Make sure we keep original atime/mtime values */
1076 rc = migrate_set_timestamps(fdv, &st);
1078 error_loc = "set target file timestamp failed";
1084 * for a migration we need to check data version on file did
1087 * Pass in gid=0 since we already own grouplock.
1089 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1090 SWAP_LAYOUTS_CHECK_DV1);
1091 if (rc == -EAGAIN) {
1092 error_loc = "file changed";
1094 } else if (rc < 0) {
1095 error_loc = "cannot swap layout";
1100 rc2 = llapi_group_unlock(fd, gid);
1101 if (rc2 < 0 && rc == 0) {
1102 error_loc = "unlock group lock";
1110 * Internal helper for migrate_copy_data(). Check lease and report error if
1113 * \param[in] fd File descriptor on which to check the lease.
1115 * \retval 0 Migration can keep on going.
1116 * \retval -errno Error occurred, abort migration.
1118 static int check_lease(int fd)
1122 rc = llapi_lease_check(fd);
1124 return 0; /* llapi_check_lease returns > 0 on success. */
1129 static int migrate_nonblock(int fd, int fdv)
1136 rc = fstat(fd, &st);
1138 error_loc = "cannot stat source file";
1142 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1144 error_loc = "cannot get data version";
1148 rc = migrate_copy_data(fd, fdv, check_lease);
1150 error_loc = "data copy failed";
1154 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1156 error_loc = "cannot get data version";
1162 error_loc = "source file changed";
1166 /* Make sure we keep original atime/mtime values */
1167 rc = migrate_set_timestamps(fdv, &st);
1169 error_loc = "set target file timestamp failed";
1176 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1178 struct pool_to_id_cbdata data = { .pool = pool };
1179 struct llapi_layout *layout = NULL;
1182 layout = llapi_layout_get_by_path(fname, 0);
1185 "error %s: file '%s' couldn't get layout: rc=%d\n",
1186 progname, fname, errno);
1190 rc = llapi_layout_sanity(layout, fname, false, true);
1192 llapi_layout_sanity_perror(errno);
1195 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1204 llapi_layout_free(layout);
1208 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1209 __u32 flags, __u32 neg_flags)
1212 __u32 flags_array[2];
1219 "error %s: neither component id nor pool is specified\n",
1223 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1229 ids[count] = comp_id;
1230 flags_array[count] = flags;
1235 if (neg_flags & LCME_FL_STALE) {
1237 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1242 ids[count] = comp_id;
1243 flags_array[count] = neg_flags | LCME_FL_NEG;
1247 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1249 if (errno == EUCLEAN) {
1252 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1253 progname, comp_id, fname);
1256 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1257 progname, comp_id, fname, flags, neg_flags);
1264 static int lfs_component_del(char *fname, __u32 comp_id,
1265 __u32 flags, __u32 neg_flags)
1269 if (flags && neg_flags)
1272 if (!flags && neg_flags)
1273 flags = neg_flags | LCME_FL_NEG;
1275 if ((flags && comp_id) || (!flags && !comp_id))
1279 if (flags & ~LCME_KNOWN_FLAGS) {
1281 "%s setstripe: unknown flags %#x\n",
1285 } else if (comp_id > LCME_ID_MAX) {
1286 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1291 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1294 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1295 progname, comp_id, fname, strerror(errno));
1299 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1306 rc = llapi_layout_file_comp_add(fname, layout);
1308 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1309 fname, strerror(errno));
1313 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1314 struct llapi_layout *layout)
1322 fd = lstat(fname, &st);
1323 if (fd == 0 && S_ISDIR(st.st_mode))
1324 open_flags = O_DIRECTORY | O_RDONLY;
1326 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1328 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1329 S_ISDIR(st.st_mode) ?
1330 "set default composite layout for" :
1331 "create composite file",
1332 fname, strerror(errno));
1336 static int lfs_migrate(char *name, __u64 migration_flags,
1337 struct llapi_stripe_param *param,
1338 struct llapi_layout *layout)
1340 struct llapi_layout *existing;
1341 uint64_t dom_new, dom_cur;
1346 rc = migrate_open_files(name, migration_flags, param, layout,
1351 rc = llapi_layout_dom_size(layout, &dom_new);
1353 error_loc = "cannot get new layout DoM size";
1356 /* special case for migration to DOM layout*/
1357 existing = llapi_layout_get_by_fd(fd, 0);
1359 error_loc = "cannot get existing layout";
1363 rc = llapi_layout_dom_size(existing, &dom_cur);
1365 error_loc = "cannot get current layout DoM size";
1370 * if file has DoM layout already then migration is possible to
1371 * the new layout with the same DoM component via swap layout,
1372 * if new layout used bigger DOM size, then mirroring is used
1374 if (dom_new > dom_cur) {
1375 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1378 error_loc = "cannot migrate to DOM layout";
1382 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1384 * Blocking mode (forced if servers do not support file lease).
1385 * It is also the default mode, since we cannot distinguish
1386 * between a broken lease and a server that does not support
1387 * atomic swap/close (LU-6785)
1389 rc = migrate_block(fd, fdv);
1393 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1395 error_loc = "cannot get lease";
1399 rc = migrate_nonblock(fd, fdv);
1401 llapi_lease_release(fd);
1406 * Atomically put lease, swap layouts and close.
1407 * for a migration we need to check data version on file did
1410 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1412 error_loc = "cannot swap layout";
1424 fprintf(stderr, "error: %s: %s: %s: %s\n",
1425 progname, name, error_loc, strerror(-rc));
1426 else if (migration_flags & MIGRATION_VERBOSE)
1427 printf("%s\n", name);
1432 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1435 char *dup_string = NULL;
1441 if (!string || !string[0])
1444 dup_string = strdup(string);
1446 llapi_printf(LLAPI_MSG_ERROR,
1447 "%s: insufficient memory\n",
1452 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1456 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1457 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1458 const char *comp_name = comp_flags_table[i].cfn_name;
1460 if (strcmp(name, comp_name) == 0) {
1461 *flags |= comp_flag;
1463 } else if (strncmp(name, "^", 1) == 0 &&
1464 strcmp(name + 1, comp_name) == 0) {
1465 *neg_flags |= comp_flag;
1470 llapi_printf(LLAPI_MSG_ERROR,
1471 "%s: component flag '%s' not supported\n",
1478 if (!*flags && !*neg_flags)
1481 /* don't allow to set and exclude the same flag */
1482 if (*flags & *neg_flags)
1490 static int mdthash_input(char *string, __u32 *inflags,
1491 __u32 *exflags, __u32 *type)
1498 {"migrating", LMV_HASH_FLAG_MIGRATION},
1499 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1500 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1509 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1513 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1514 if (strcmp(name, mhflist[i].name) == 0 ||
1515 name[0] == mhflist[i].name[0]) {
1516 *inflags |= mhflist[i].flag;
1518 } else if (name[0] == '^' &&
1519 (strcmp(name + 1, mhflist[i].name) == 0 ||
1520 name[1] == mhflist[i].name[0])) {
1521 *exflags |= mhflist[i].flag;
1526 i = check_hashtype(name);
1531 llapi_printf(LLAPI_MSG_ERROR,
1532 "%s: invalid mdt_hash value '%s'\n",
1538 /* don't allow to include and exclude the same flag */
1539 if (*inflags & *exflags) {
1540 llapi_printf(LLAPI_MSG_ERROR,
1541 "%s: include and exclude same flag '%s'\n",
1549 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1557 if (strncmp(string, "^", 1) == 0) {
1558 *neg_state = llapi_layout_string_flags(string + 1);
1559 if (*neg_state != 0)
1562 *state = llapi_layout_string_flags(string);
1567 llapi_printf(LLAPI_MSG_ERROR,
1568 "%s: mirrored file state '%s' not supported\n",
1574 * struct mirror_args - Command-line arguments for mirror(s).
1575 * @m_count: Number of mirrors to be created with this layout.
1576 * @m_flags: Mirror level flags, only 'prefer' is supported.
1577 * @m_layout: Mirror layout.
1578 * @m_file: A victim file. Its layout will be split and used as a mirror.
1579 * @m_next: Point to the next node of the list.
1581 * Command-line arguments for mirror(s) will be parsed and stored in
1582 * a linked list that consists of this structure.
1584 struct mirror_args {
1587 struct llapi_layout *m_layout;
1589 struct mirror_args *m_next;
1594 * enum mirror_flags - Flags for extending a mirrored file.
1595 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1596 * in case the victim file(s) contains the same data as the
1597 * original mirrored file.
1598 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1599 * @MF_COMP_ID: specified component id instead of mirror id
1601 * Flags for extending a mirrored file.
1611 * mirror_create_sanity_check() - Check mirror list.
1612 * @list: A linked list that stores the mirror arguments.
1614 * This function does a sanity check on @list for creating
1617 * Return: 0 on success or a negative error code on failure.
1619 static int mirror_create_sanity_check(const char *fname,
1620 struct mirror_args *list,
1624 bool has_m_file = false;
1625 bool has_m_layout = false;
1630 if (fname && check_fname) {
1631 struct llapi_layout *layout;
1633 layout = llapi_layout_get_by_path(fname, 0);
1636 "error: %s: file '%s' couldn't get layout\n",
1641 rc = llapi_layout_sanity(layout, fname, false, true);
1643 llapi_layout_free(layout);
1646 llapi_layout_sanity_perror(rc);
1654 llapi_layout_free(list->m_layout);
1657 llapi_layout_get_by_path(list->m_file, 0);
1658 if (!list->m_layout) {
1660 "error: %s: file '%s' has no layout\n",
1661 progname, list->m_file);
1665 has_m_layout = true;
1666 if (!list->m_layout) {
1667 fprintf(stderr, "error: %s: no mirror layout\n",
1673 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1675 llapi_layout_sanity_perror(rc);
1679 list = list->m_next;
1682 if (has_m_file && has_m_layout) {
1684 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1692 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1694 __u32 mirror_flags = *(__u32 *)cbdata;
1698 rc = llapi_layout_comp_flags_get(layout, &flags);
1703 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1708 return LLAPI_LAYOUT_ITER_CONT;
1712 * mirror_create() - Create a mirrored file.
1713 * @fname: The file to be created.
1714 * @mirror_list: A linked list that stores the mirror arguments.
1716 * This function creates a mirrored file @fname with the mirror(s)
1717 * from @mirror_list.
1719 * Return: 0 on success or a negative error code on failure.
1721 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1723 struct llapi_layout *layout = NULL;
1724 struct mirror_args *cur_mirror = NULL;
1725 uint16_t mirror_count = 0;
1729 rc = mirror_create_sanity_check(fname, mirror_list, false);
1733 cur_mirror = mirror_list;
1734 while (cur_mirror) {
1735 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1737 &cur_mirror->m_flags);
1740 fprintf(stderr, "%s: failed to set mirror flags\n",
1745 for (i = 0; i < cur_mirror->m_count; i++) {
1746 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1750 "error: %s: merge layout failed: %s\n",
1751 progname, strerror(errno));
1755 mirror_count += cur_mirror->m_count;
1756 cur_mirror = cur_mirror->m_next;
1760 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1764 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1767 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1768 progname, strerror(errno));
1772 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1780 llapi_layout_free(layout);
1785 * Compare files and check lease on @fd.
1787 * \retval bytes number of bytes are the same
1789 static ssize_t mirror_file_compare(int fd, int fdv)
1791 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1793 ssize_t bytes_done = 0;
1794 ssize_t bytes_read = 0;
1796 buf = malloc(buflen * 2);
1801 if (!llapi_lease_check(fd)) {
1802 bytes_done = -EBUSY;
1806 bytes_read = read(fd, buf, buflen);
1807 if (bytes_read <= 0)
1810 if (bytes_read != read(fdv, buf + buflen, buflen))
1814 * XXX: should compute the checksum on each buffer and then
1815 * compare checksum to avoid cache collision
1817 if (memcmp(buf, buf + buflen, bytes_read))
1820 bytes_done += bytes_read;
1828 static int mirror_extend_file(const char *fname, const char *victim_file,
1829 enum mirror_flags mirror_flags)
1834 struct stat stbuf_v;
1835 struct ll_ioc_lease *data = NULL;
1838 fd = open(fname, O_RDWR);
1840 error_loc = "open source file";
1845 fdv = open(victim_file, O_RDWR);
1847 error_loc = "open target file";
1852 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1853 error_loc = "stat source or target file";
1858 if (stbuf.st_dev != stbuf_v.st_dev) {
1859 error_loc = "stat source and target file";
1864 /* mirrors should be of the same size */
1865 if (stbuf.st_size != stbuf_v.st_size) {
1866 error_loc = "file sizes don't match";
1871 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1873 error_loc = "cannot get lease";
1877 if (!(mirror_flags & MF_NO_VERIFY)) {
1879 /* mirrors should have the same contents */
1880 ret = mirror_file_compare(fd, fdv);
1881 if (ret != stbuf.st_size) {
1882 error_loc = "file busy or contents don't match";
1883 rc = ret < 0 ? ret : -EINVAL;
1888 /* Get rid of caching pages from clients */
1889 rc = llapi_file_flush(fd);
1891 error_loc = "cannot get data version";
1895 rc = llapi_file_flush(fdv);
1897 error_loc = "cannot get data version";
1901 rc = migrate_set_timestamps(fd, &stbuf);
1903 error_loc = "cannot set source file timestamp";
1907 /* Atomically put lease, merge layouts and close. */
1908 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1910 error_loc = "memory allocation";
1913 data->lil_mode = LL_LEASE_UNLCK;
1914 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1915 data->lil_count = 1;
1916 data->lil_ids[0] = fdv;
1917 rc = llapi_lease_set(fd, data);
1919 error_loc = "cannot merge layout";
1921 } else if (rc == 0) {
1923 error_loc = "lost lease lock";
1936 (void) unlink(victim_file);
1938 fprintf(stderr, "error: %s: %s: %s: %s\n",
1939 progname, fname, error_loc, strerror(-rc));
1943 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1944 bool inherit, uint32_t flags)
1946 struct llapi_layout *f_layout = NULL;
1947 struct ll_ioc_lease *data = NULL;
1954 f_layout = llapi_layout_get_by_path(name, 0);
1957 fprintf(stderr, "%s: cannot get layout\n", progname);
1960 rc = llapi_layout_get_last_init_comp(f_layout);
1962 fprintf(stderr, "%s: cannot get the last init comp\n",
1966 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1969 "%s: cannot inherit from the last init comp\n",
1974 llapi_layout_comp_flags_set(m_layout, flags);
1975 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1980 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1982 error_loc = "cannot get lease";
1986 rc = fstat(fd, &st);
1988 error_loc = "cannot stat source file";
1992 rc = migrate_nonblock(fd, fdv);
1994 llapi_lease_release(fd);
1998 rc = migrate_set_timestamps(fd, &st);
2000 error_loc = "cannot set source file timestamp";
2004 /* Atomically put lease, merge layouts and close. */
2005 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2007 error_loc = "memory allocation";
2010 data->lil_mode = LL_LEASE_UNLCK;
2011 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2012 data->lil_count = 1;
2013 data->lil_ids[0] = fdv;
2014 rc = llapi_lease_set(fd, data);
2016 error_loc = "cannot merge layout";
2018 } else if (rc == 0) {
2020 error_loc = "lost lease lock";
2033 fprintf(stderr, "error: %s: %s: %s: %s\n",
2034 progname, name, error_loc, strerror(-rc));
2038 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2039 enum mirror_flags mirror_flags)
2043 while (mirror_list) {
2044 if (mirror_list->m_file) {
2045 rc = mirror_extend_file(fname, mirror_list->m_file,
2048 __u32 mirror_count = mirror_list->m_count;
2050 while (mirror_count > 0) {
2051 rc = mirror_extend_layout(fname,
2052 mirror_list->m_layout,
2053 mirror_list->m_inherit,
2054 mirror_list->m_flags);
2064 mirror_list = mirror_list->m_next;
2070 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2075 rc = llapi_layout_mirror_id_get(layout, &id);
2079 if ((__u16)id == *(__u16 *)cbdata)
2080 return LLAPI_LAYOUT_ITER_STOP;
2082 return LLAPI_LAYOUT_ITER_CONT;
2085 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2090 rc = llapi_layout_comp_id_get(layout, &id);
2094 if (id == *(__u32 *)cbdata)
2095 return LLAPI_LAYOUT_ITER_STOP;
2097 return LLAPI_LAYOUT_ITER_CONT;
2100 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2102 char buf[LOV_MAXPOOLNAME + 1];
2103 struct pool_to_id_cbdata *d = (void *)cbdata;
2107 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2110 if (strcmp(d->pool, buf))
2111 return LLAPI_LAYOUT_ITER_CONT;
2113 rc = llapi_layout_mirror_id_get(layout, &id);
2118 return LLAPI_LAYOUT_ITER_STOP;
2121 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2123 char buf[LOV_MAXPOOLNAME + 1];
2124 struct pool_to_id_cbdata *d = (void *)cbdata;
2128 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2131 if (strcmp(d->pool, buf))
2132 return LLAPI_LAYOUT_ITER_CONT;
2134 rc = llapi_layout_comp_id_get(layout, &id);
2139 return LLAPI_LAYOUT_ITER_STOP;
2142 struct collect_ids_data {
2148 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2150 struct collect_ids_data *cid = cbdata;
2154 rc = llapi_layout_mirror_id_get(layout, &id);
2158 if ((__u16)id != cid->cid_exclude) {
2161 for (i = 0; i < cid->cid_count; i++) {
2162 /* already collected the mirror id */
2163 if (id == cid->cid_ids[i])
2164 return LLAPI_LAYOUT_ITER_CONT;
2166 cid->cid_ids[cid->cid_count] = id;
2170 return LLAPI_LAYOUT_ITER_CONT;
2174 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2175 * @mirror_id: Mirror id to be checked.
2176 * @layout: Mirror component list.
2178 * This function checks if a mirror with specified @mirror_id is the last
2179 * non-stale mirror of a layout @layout.
2181 * Return: true or false.
2184 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2186 __u16 mirror_ids[128] = { 0 };
2187 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2189 .cid_exclude = mirror_id, };
2192 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2194 for (i = 0; i < cid.cid_count; i++) {
2195 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2198 comp_size = llapi_mirror_find_stale(layout, comp_array,
2199 ARRAY_SIZE(comp_array),
2208 static int mirror_split(const char *fname, __u32 id, const char *pool,
2209 enum mirror_flags mflags, const char *victim_file)
2211 struct llapi_layout *layout;
2212 char parent[PATH_MAX];
2213 char victim[PATH_MAX];
2214 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2216 struct ll_ioc_lease *data;
2217 uint16_t mirror_count;
2221 bool purge = true; /* delete mirror by setting fdv=fd */
2225 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2227 "error %s: the source file '%s' and -f file are the same\n",
2232 /* check fname contains mirror with mirror_id/comp_id */
2233 layout = llapi_layout_get_by_path(fname, 0);
2236 "error %s: file '%s' couldn't get layout\n",
2241 rc = llapi_layout_sanity(layout, fname, false, true);
2243 llapi_layout_sanity_perror(rc);
2247 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2250 "error %s: file '%s' couldn't get mirror count\n",
2254 if (mirror_count < 2) {
2256 "error %s: file '%s' has %d component, cannot split\n",
2257 progname, fname, mirror_count);
2261 if (mflags & MF_COMP_POOL) {
2262 struct pool_to_id_cbdata data = { .pool = pool };
2264 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2266 mirror_id = data.id;
2267 } else if (mflags & MF_COMP_ID) {
2268 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2269 mirror_id = mirror_id_of(id);
2271 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2275 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2278 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2279 if (mflags & MF_COMP_POOL) {
2281 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2282 progname, fname, pool);
2284 } else if (mflags & MF_COMP_ID) {
2286 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2287 progname, fname, id);
2291 "error %s: file '%s' does not contain mirror with id %u\n",
2292 progname, fname, id);
2297 fd = open(fname, O_RDWR);
2300 "error %s: open file '%s' failed: %s\n",
2301 progname, fname, strerror(errno));
2305 /* get victim file directory pathname */
2306 if (strlen(fname) > sizeof(parent) - 1) {
2307 fprintf(stderr, "error %s: file name of '%s' too long\n",
2312 strncpy(parent, fname, sizeof(parent));
2313 ptr = strrchr(parent, '/');
2315 if (!getcwd(parent, sizeof(parent))) {
2316 fprintf(stderr, "error %s: getcwd failed: %s\n",
2317 progname, strerror(errno));
2327 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2329 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2334 rc = llapi_file_is_encrypted(fd);
2336 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2337 progname, fname, rc);
2344 /* use a temp file to store the splitted layout */
2345 if (mflags & MF_DESTROY) {
2346 char file_path[PATH_MAX];
2347 unsigned int rnumber;
2350 if (last_non_stale_mirror(mirror_id, layout)) {
2353 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2359 /* don't use volatile file for mirror destroy */
2363 * try the old way to delete mirror using
2368 rc = snprintf(file_path,
2370 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2374 rc >= sizeof(file_path)) {
2375 fdv = -ENAMETOOLONG;
2379 open_flags = O_RDWR |
2380 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2381 O_CREAT | O_EXCL | O_NOFOLLOW;
2382 fdv = open(file_path, open_flags,
2386 } while (fdv < 0 && rc == -EEXIST);
2392 "error %s: not permitted on encrypted file '%s': %d\n",
2393 progname, fname, rc);
2397 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2399 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2402 /* user specified victim file */
2406 "error %s: not permitted on encrypted file '%s': %d\n",
2407 progname, fname, rc);
2410 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2415 "error %s: create victim file failed: %s\n",
2416 progname, strerror(errno));
2420 /* get lease lock of fname */
2421 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2424 "error %s: cannot get lease of file '%s': %d\n",
2425 progname, fname, rc);
2429 /* Atomatically put lease, split layouts and close. */
2430 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2436 data->lil_mode = LL_LEASE_UNLCK;
2437 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2438 data->lil_count = 2;
2439 data->lil_ids[0] = fdv;
2440 data->lil_ids[1] = mirror_id;
2441 rc = llapi_lease_set(fd, data);
2443 if (rc == -EINVAL && purge) {
2444 /* could be old MDS which prohibit fd==fdv */
2449 if (rc == 0) /* lost lease lock */
2452 "error %s: cannot split '%s': %s\n",
2453 progname, fname, strerror(-rc));
2465 llapi_layout_free(layout);
2470 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2471 __u16 *mirror_ids, int ids_nr);
2473 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2474 __u64 migration_flags,
2475 struct llapi_stripe_param *param,
2476 struct llapi_layout *layout)
2478 struct ll_ioc_lease *data = NULL;
2481 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2483 error_loc = "cannot get lease";
2487 /* Atomically put lease, merge layouts, resync and close. */
2488 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2490 error_loc = "memory allocation";
2493 data->lil_mode = LL_LEASE_UNLCK;
2494 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2495 data->lil_count = 1;
2496 data->lil_ids[0] = fdv;
2497 rc = llapi_lease_set(fd, data);
2499 error_loc = "cannot merge layout";
2501 } else if (rc == 0) {
2503 error_loc = "lost lease lock";
2509 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2511 error_loc = "cannot resync file";
2515 /* delete first mirror now */
2516 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2518 error_loc = "cannot delete old layout";
2526 fprintf(stderr, "error: %s: %s: %s: %s\n",
2527 progname, name, error_loc, strerror(-rc));
2528 else if (migration_flags & MIGRATION_VERBOSE)
2529 printf("%s\n", name);
2536 * Parse a string containing an target index list into an array of integers.
2538 * The input string contains a comma delimited list of individual
2539 * indices and ranges, for example "1,2-4,7". Add the indices into the
2540 * \a tgts array and remove duplicates.
2542 * \param[out] tgts array to store indices in
2543 * \param[in] size size of \a tgts array
2544 * \param[in] offset starting index in \a tgts
2545 * \param[in] arg string containing OST index list
2546 * \param[in/out] overstriping index list may contain duplicates
2548 * \retval positive number of indices in \a tgts
2549 * \retval -EINVAL unable to parse \a arg
2551 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2552 unsigned long long *pattern)
2556 int slots = size - offset;
2558 bool overstriped = false;
2564 end_of_loop = false;
2565 while (!end_of_loop) {
2566 int start_index = 0;
2569 char *endptr = NULL;
2573 ptr = strchrnul(arg, ',');
2575 end_of_loop = *ptr == '\0';
2579 start_index = strtol(arg, &endptr, 0);
2580 if (endptr == arg) /* no data at all */
2582 if (errno != 0 || start_index < -1 ||
2583 (*endptr != '-' && *endptr != '\0'))
2586 end_index = start_index;
2587 if (*endptr == '-') {
2589 end_index = strtol(endptr + 1, &endptr, 0);
2590 if (errno != 0 || *endptr != '\0' || end_index < -1)
2592 if (end_index < start_index)
2596 for (i = start_index; i <= end_index && slots > 0; i++) {
2599 /* remove duplicate */
2600 for (j = 0; j < offset; j++) {
2601 if (tgts[j] == i && pattern &&
2602 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2604 else if (tgts[j] == i)
2610 if (j == offset) { /* check complete */
2616 if (slots == 0 && i < end_index)
2624 if (!end_of_loop && ptr)
2627 if (!overstriped && pattern)
2628 *pattern = LLAPI_LAYOUT_DEFAULT;
2630 return rc < 0 ? rc : nr;
2633 struct lfs_setstripe_args {
2634 unsigned long long lsa_comp_end;
2635 unsigned long long lsa_stripe_size;
2636 unsigned long long lsa_extension_size;
2637 long long lsa_stripe_count;
2638 long long lsa_stripe_off;
2639 __u32 lsa_comp_flags;
2640 __u32 lsa_comp_neg_flags;
2641 unsigned long long lsa_pattern;
2642 unsigned int lsa_mirror_count;
2644 bool lsa_first_comp;
2645 bool lsa_extension_comp;
2647 char *lsa_pool_name;
2650 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2652 unsigned int mirror_count = lsa->lsa_mirror_count;
2653 bool first_comp = lsa->lsa_first_comp;
2655 memset(lsa, 0, sizeof(*lsa));
2657 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2658 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2659 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2660 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2661 lsa->lsa_pool_name = NULL;
2663 lsa->lsa_mirror_count = mirror_count;
2664 lsa->lsa_first_comp = first_comp;
2668 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2669 * @lsa: Stripe options to be initialized and inherited.
2671 * This function initializes stripe options in @lsa and inherit
2672 * stripe_size, stripe_count and OST pool_name options.
2676 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2678 unsigned long long stripe_size;
2679 long long stripe_count;
2680 char *pool_name = NULL;
2682 stripe_size = lsa->lsa_stripe_size;
2683 stripe_count = lsa->lsa_stripe_count;
2684 pool_name = lsa->lsa_pool_name;
2686 setstripe_args_init(lsa);
2688 lsa->lsa_stripe_size = stripe_size;
2689 lsa->lsa_stripe_count = stripe_count;
2690 lsa->lsa_pool_name = pool_name;
2693 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2695 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2696 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2697 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2698 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2699 lsa->lsa_comp_end != 0);
2702 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2704 if (lsa->lsa_nr_tgts) {
2705 if (lsa->lsa_nr_tgts < 0 ||
2706 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2707 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2713 if (lsa->lsa_stripe_count > 0 &&
2714 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2715 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2716 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2717 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2718 lsa->lsa_stripe_count,
2730 * comp_args_to_layout() - Create or extend a composite layout.
2731 * @composite: Pointer to the composite layout.
2732 * @lsa: Stripe options for the new component.
2734 * This function creates or extends a composite layout by adding a new
2735 * component with stripe options from @lsa.
2737 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2739 * Return: 0 on success or an error code on failure.
2741 static int comp_args_to_layout(struct llapi_layout **composite,
2742 struct lfs_setstripe_args *lsa,
2745 struct llapi_layout *layout = *composite;
2746 uint64_t prev_end = 0;
2752 layout = llapi_layout_alloc();
2754 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2759 *composite = layout;
2760 lsa->lsa_first_comp = true;
2765 * Get current component extent, current component
2766 * must be the tail component.
2768 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2770 fprintf(stderr, "Get comp extent failed. %s\n",
2775 if (lsa->lsa_first_comp) {
2777 rc = llapi_layout_add_first_comp(layout);
2779 rc = llapi_layout_comp_add(layout);
2782 fprintf(stderr, "Add component failed. %s\n",
2788 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2790 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2791 lsa->lsa_comp_flags, strerror(errno));
2796 uint64_t comp_end = lsa->lsa_comp_end;
2799 * The extendable component is 0-length, so it can be removed
2800 * if there is insufficient space to extend it.
2802 if (lsa->lsa_extension_comp)
2803 comp_end = prev_end;
2805 rc = llapi_layout_comp_extent_set(layout, prev_end,
2808 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2809 prev_end, comp_end, strerror(errno));
2813 /* reset lsa_first_comp */
2814 lsa->lsa_first_comp = false;
2816 /* Data-on-MDT component setting */
2817 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2819 if (lsa->lsa_stripe_count == 0)
2820 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2821 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2822 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2823 if (lsa->lsa_stripe_off == -1 ||
2824 lsa->lsa_stripe_off == 0)
2825 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2827 * In case of Data-on-MDT patterns the only extra option
2828 * applicable is stripe size option.
2830 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2832 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2833 lsa->lsa_stripe_count);
2837 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2838 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2840 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2841 lsa->lsa_stripe_size);
2845 if (lsa->lsa_nr_tgts != 0) {
2847 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2852 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2854 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2855 lsa->lsa_stripe_off);
2859 if (lsa->lsa_pool_name != 0) {
2861 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2862 lsa->lsa_pool_name);
2867 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2869 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2874 /* Data-on-MDT component has always single stripe up to end */
2875 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2876 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2877 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2879 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2886 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2887 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2889 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2890 rc = llapi_layout_extension_size_set(layout, size);
2892 rc = llapi_layout_stripe_size_set(layout, size);
2895 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2896 size, strerror(errno));
2900 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2902 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2903 lsa->lsa_stripe_count, strerror(errno));
2907 if (lsa->lsa_pool_name) {
2908 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2910 fprintf(stderr, "Set pool name: %s failed. %s\n",
2911 lsa->lsa_pool_name, strerror(errno));
2915 rc = llapi_layout_pool_name_set(layout, "");
2917 fprintf(stderr, "Clear pool name failed: %s\n",
2923 rc = lsa_args_stripe_count_check(lsa);
2927 if (lsa->lsa_nr_tgts > 0) {
2930 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2931 rc = llapi_layout_ost_index_set(layout, i,
2936 /* Make sure stripe offset is in OST list. */
2937 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2941 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2942 lsa->lsa_stripe_off);
2946 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2947 lsa->lsa_stripe_off != -1) {
2948 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2951 fprintf(stderr, "Set ost index %d failed. %s\n",
2952 i, strerror(errno));
2956 /* Create the second, virtual component of extension space */
2957 if (lsa->lsa_extension_comp) {
2958 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2959 lsa->lsa_extension_comp = false;
2966 static int build_component(struct llapi_layout **layout,
2967 struct lfs_setstripe_args *lsa, bool set_extent)
2971 rc = comp_args_to_layout(layout, lsa, set_extent);
2975 if (lsa->lsa_mirror_count > 0) {
2976 rc = llapi_layout_mirror_count_set(*layout,
2977 lsa->lsa_mirror_count);
2981 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2984 lsa->lsa_mirror_count = 0;
2990 static int build_prev_component(struct llapi_layout **layout,
2991 struct lfs_setstripe_args *prev,
2992 struct lfs_setstripe_args *lsa,
2995 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2998 if (prev->lsa_stripe_size) {
3000 prev->lsa_comp_end = lsa->lsa_comp_end;
3001 prev->lsa_extension_size = lsa->lsa_extension_size;
3002 prev->lsa_extension_comp = true;
3005 rc = build_component(layout, prev, true);
3011 * Copy lsa to previous lsa;
3012 * if this is an extension component, make the previous invalid;
3015 prev->lsa_stripe_size = 0;
3022 static int build_layout_from_yaml_node(struct cYAML *node,
3023 struct llapi_layout **layout,
3024 struct lfs_setstripe_args *lsa,
3025 struct lfs_setstripe_args *prevp)
3027 struct lfs_setstripe_args prev = { 0 };
3028 __u32 *osts = lsa->lsa_tgts;
3036 string = node->cy_string;
3038 if (node->cy_type == CYAML_TYPE_OBJECT) {
3039 /* go deep to sub blocks */
3040 if (string && !strncmp(string, "component", 9) &&
3041 strncmp(string, "component0", 10) &&
3042 strncmp(string, "components", 10)) {
3043 rc = build_prev_component(layout, prevp, lsa,
3048 /* initialize lsa. */
3049 setstripe_args_init(lsa);
3050 lsa->lsa_first_comp = false;
3051 lsa->lsa_tgts = osts;
3054 rc = build_layout_from_yaml_node(node->cy_child, layout,
3059 if (!node->cy_string)
3062 /* skip leading lmm_ if present, to simplify parsing */
3063 if (strncmp(string, "lmm_", 4) == 0)
3066 if (node->cy_type == CYAML_TYPE_STRING) {
3067 if (!strcmp(string, "lcme_extent.e_end")) {
3068 if (!strcmp(node->cy_valuestring, "EOF") ||
3069 !strcmp(node->cy_valuestring, "eof"))
3070 lsa->lsa_comp_end = LUSTRE_EOF;
3071 } else if (!strcmp(string, "pool")) {
3072 lsa->lsa_pool_name = node->cy_valuestring;
3073 } else if (!strcmp(string, "pattern")) {
3074 if (!strcmp(node->cy_valuestring, "mdt"))
3075 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3076 if (!strcmp(node->cy_valuestring,
3077 "raid0,overstriped"))
3079 LLAPI_LAYOUT_OVERSTRIPING;
3080 } else if (!strcmp(string, "lcme_flags")) {
3081 rc = comp_str2flags(node->cy_valuestring,
3082 &lsa->lsa_comp_flags,
3083 &lsa->lsa_comp_neg_flags);
3087 * Only template flags have meaning in
3088 * the layout for a new file
3090 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3092 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3093 if (!strcmp(string, "lcm_mirror_count")) {
3094 lsa->lsa_mirror_count = node->cy_valueint;
3095 } else if (!strcmp(string, "lcme_extent.e_start")) {
3096 if (node->cy_valueint == 0)
3097 lsa->lsa_first_comp = true;
3098 } else if (!strcmp(string, "lcme_extent.e_end")) {
3099 if (node->cy_valueint == -1)
3100 lsa->lsa_comp_end = LUSTRE_EOF;
3102 lsa->lsa_comp_end = node->cy_valueint;
3103 } else if (!strcmp(string, "stripe_count")) {
3104 lsa->lsa_stripe_count = node->cy_valueint;
3105 } else if (!strcmp(string, "stripe_size")) {
3106 lsa->lsa_stripe_size = node->cy_valueint;
3107 } else if (!strcmp(string, "extension_size")) {
3108 lsa->lsa_extension_size = node->cy_valueint;
3109 lsa->lsa_extension_comp = true;
3110 } else if (!strcmp(string, "stripe_offset")) {
3111 lsa->lsa_stripe_off = node->cy_valueint;
3112 } else if (!strcmp(string, "l_ost_idx")) {
3113 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3118 node = node->cy_next;
3121 if (prevp == &prev) {
3122 rc = build_prev_component(layout, prevp, lsa, true);