4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
61 #include <sys/xattr.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
81 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
82 #endif /* !ARRAY_SIZE */
85 static int lfs_find(int argc, char **argv);
86 static int lfs_getstripe(int argc, char **argv);
87 static int lfs_getdirstripe(int argc, char **argv);
88 static int lfs_setdirstripe(int argc, char **argv);
89 static int lfs_rmentry(int argc, char **argv);
90 static int lfs_osts(int argc, char **argv);
91 static int lfs_mdts(int argc, char **argv);
92 static int lfs_df(int argc, char **argv);
93 static int lfs_getname(int argc, char **argv);
94 static int lfs_check(int argc, char **argv);
95 #ifdef HAVE_SYS_QUOTA_H
96 static int lfs_setquota(int argc, char **argv);
97 static int lfs_quota(int argc, char **argv);
98 static int lfs_project(int argc, char **argv);
100 static int lfs_flushctx(int argc, char **argv);
101 static int lfs_poollist(int argc, char **argv);
102 static int lfs_changelog(int argc, char **argv);
103 static int lfs_changelog_clear(int argc, char **argv);
104 static int lfs_fid2path(int argc, char **argv);
105 static int lfs_path2fid(int argc, char **argv);
106 static int lfs_rmfid(int argc, char **argv);
107 static int lfs_data_version(int argc, char **argv);
108 static int lfs_hsm_state(int argc, char **argv);
109 static int lfs_hsm_set(int argc, char **argv);
110 static int lfs_hsm_clear(int argc, char **argv);
111 static int lfs_hsm_action(int argc, char **argv);
112 static int lfs_hsm_archive(int argc, char **argv);
113 static int lfs_hsm_restore(int argc, char **argv);
114 static int lfs_hsm_release(int argc, char **argv);
115 static int lfs_hsm_remove(int argc, char **argv);
116 static int lfs_hsm_cancel(int argc, char **argv);
117 static int lfs_swap_layouts(int argc, char **argv);
118 static int lfs_mv(int argc, char **argv);
119 static int lfs_ladvise(int argc, char **argv);
120 static int lfs_getsom(int argc, char **argv);
121 static int lfs_heat_get(int argc, char **argv);
122 static int lfs_heat_set(int argc, char **argv);
123 static int lfs_mirror(int argc, char **argv);
124 static int lfs_mirror_list_commands(int argc, char **argv);
125 static int lfs_list_commands(int argc, char **argv);
126 static inline int lfs_mirror_resync(int argc, char **argv);
127 static inline int lfs_mirror_verify(int argc, char **argv);
128 static inline int lfs_mirror_read(int argc, char **argv);
129 static inline int lfs_mirror_write(int argc, char **argv);
130 static inline int lfs_mirror_copy(int argc, char **argv);
131 static int lfs_pcc_attach(int argc, char **argv);
132 static int lfs_pcc_attach_fid(int argc, char **argv);
133 static int lfs_pcc_detach(int argc, char **argv);
134 static int lfs_pcc_detach_fid(int argc, char **argv);
135 static int lfs_pcc_state(int argc, char **argv);
136 static int lfs_pcc(int argc, char **argv);
137 static int lfs_pcc_list_commands(int argc, char **argv);
138 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
139 __u64 migration_flags,
140 struct llapi_stripe_param *param,
141 struct llapi_layout *layout);
143 enum setstripe_origin {
152 static int lfs_setstripe_internal(int argc, char **argv,
153 enum setstripe_origin opc);
155 static inline int lfs_setstripe(int argc, char **argv)
157 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
159 static inline int lfs_setstripe_migrate(int argc, char **argv)
161 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
163 static inline int lfs_mirror_create(int argc, char **argv)
165 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
167 static inline int lfs_mirror_extend(int argc, char **argv)
169 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
171 static inline int lfs_mirror_split(int argc, char **argv)
173 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
175 static inline int lfs_mirror_delete(int argc, char **argv)
177 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
180 /* Setstripe and migrate share mostly the same parameters */
181 #define SSM_CMD_COMMON(cmd) \
182 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
183 " [--stripe-count|-c <stripe_count>]\n" \
184 " [--overstripe-count|-C <stripe_count>]\n" \
185 " [--stripe-index|-i <start_ost_idx>]\n" \
186 " [--stripe-size|-S <stripe_size>]\n" \
187 " [--layout|-L <pattern>]\n" \
188 " [--pool|-p <pool_name>]\n" \
189 " [--ost|-o <ost_indices>]\n" \
190 " [--yaml|-y <yaml_template_file>]\n" \
191 " [--copy=<lustre_src>]\n"
193 #define SSM_HELP_COMMON \
194 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
195 "\t Using -C instead of -c allows overstriping, which\n" \
196 "\t will place more than one stripe per OST if\n" \
197 "\t stripe_count is greater than the number of OSTs\n" \
198 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
199 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
200 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
201 "\t respectively)\n" \
202 "\tpool_name: Name of OST pool to use (default none)\n" \
203 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
204 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
205 "\t Indices be specified in a format of:\n" \
206 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
208 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
209 "\t If --pool is set with --ost, then the OSTs\n" \
210 "\t must be the members of the pool.\n" \
211 "\tcomp_end: Extent end of component, start after previous end.\n"\
212 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
213 "\t respectively, -1 for EOF). Must be a multiple of\n"\
214 "\t stripe_size.\n" \
215 "\tyaml_template_file:\n" \
216 "\t YAML layout template file, can't be used with -c,\n" \
217 "\t -i, -S, -p, -o, or -E arguments.\n" \
218 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
219 "\t another lustre file or directory, can't used with\n" \
220 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
222 #define MIRROR_CREATE_HELP \
223 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
224 "\t setstripe layout options\n" \
225 "\t It defaults to 1 if not specified; if specified,\n" \
226 "\t it must follow the option without a space.\n" \
227 "\t The option can also be repeated multiple times to\n" \
228 "\t separate mirrors that have different layouts.\n" \
229 "\tsetstripe options: Mirror layout\n" \
230 "\t It can be a plain layout or a composite layout.\n" \
231 "\t If not specified, the stripe options inherited\n" \
232 "\t from the previous component will be used.\n" \
233 "\tflags: set flags to the component of the current mirror.\n" \
234 "\t Only \"prefer\" flag is supported so far.\n"
236 #define MIRROR_EXTEND_HELP \
238 "\tvictim_file: The layout of victim_file will be split and used\n" \
239 "\t as a mirror added to the mirrored file.\n" \
240 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
241 "\t from victim file(s) in case the victim file(s)\n" \
242 "\t contains the same data as the original mirrored\n" \
245 #define MIRROR_EXTEND_USAGE \
246 " <--mirror-count|-N[mirror_count]>\n" \
247 " [setstripe options|-f <victim_file>]\n" \
250 #define SETSTRIPE_USAGE \
251 SSM_CMD_COMMON("setstripe") \
252 MIRROR_EXTEND_USAGE \
253 " <directory|filename>\n" \
257 #define MIGRATE_USAGE \
258 SSM_CMD_COMMON("migrate ") \
259 " [--block|-b] [--non-block|-n]\n" \
260 " [--non-direct|-D] [--verbose|-v]\n" \
264 "\tblock: Block file access during data migration (default)\n" \
265 "\tnon-block: Abort migrations if concurrent access is detected\n" \
266 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
267 "\tverbose: Print each filename as it is migrated\n" \
269 #define SETDIRSTRIPE_USAGE \
270 " [--mdt-count|-c stripe_count>\n" \
271 " [--mdt-hash|-H mdt_hash]\n" \
272 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
273 " [--default|-D] [--mode|-o mode] <dir>\n" \
274 "\tstripe_count: stripe count of the striped directory\n" \
275 "\tmdt_index: MDT index of first stripe\n" \
276 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
277 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
278 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
279 " space create subdirectories with balanced space usage\n" \
280 "\tdefault_stripe: set default dirstripe of the directory\n" \
281 "\tmode: the file access permission of the directory (octal)\n" \
282 "To create dir with a foreign (free format) layout :\n" \
283 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
284 "[--mode|-m mode] [--flags <hex>] <dir>\n" \
285 "\tmode: the mode of the directory\n" \
286 "\tforeign_type: none or daos\n"
289 * command_t mirror_cmdlist - lfs mirror commands.
291 command_t mirror_cmdlist[] = {
292 { .pc_name = "create", .pc_func = lfs_mirror_create,
293 .pc_help = "Create a mirrored file.\n"
294 "usage: lfs mirror create "
295 "<--mirror-count|-N[mirror_count]> "
296 "[setstripe options] ... <filename|directory> ...\n"
297 MIRROR_CREATE_HELP },
298 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
299 .pc_help = "delete a mirror from a file.\n"
300 "usage: lfs mirror delete {--comp-id|-I <comp_id>|-p <pool>} <mirrored_file> ...\n"
302 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
303 .pc_help = "Extend a mirrored file.\n"
304 "usage: lfs mirror extend "
305 "<--mirror-count|-N[mirror_count]> [--no-verify] "
306 "[setstripe options|-f <victim_file>] ... <filename> ...\n"
307 MIRROR_EXTEND_HELP },
308 { .pc_name = "split", .pc_func = lfs_mirror_split,
309 .pc_help = "Split a mirrored file.\n"
310 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
311 "\t <--component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
312 "\t [-f <new_file>] <mirrored_file> ...\n"
313 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
314 "\t can be fetched by lfs getstripe command.\n"
315 "\tcomp_id: Unique component ID within a mirror.\n"
316 "\tpool: Components on specified pool.\n"
317 "\tnew_file: This option indicates the layout of the split\n"
318 "\t mirror will be stored into. If not specified,\n"
319 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
320 "\t will be used.\n" },
321 { .pc_name = "read", .pc_func = lfs_mirror_read,
322 .pc_help = "Read the content of a specified mirror of a file.\n"
323 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
324 "[--outfile|-o <output_file>] <mirrored_file>\n" },
325 { .pc_name = "write", .pc_func = lfs_mirror_write,
326 .pc_help = "Write to a specified mirror of a file.\n"
327 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
328 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
329 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
330 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
331 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
332 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
333 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
334 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
335 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
336 "<mirrored file> [<mirrored file2>...]\n"},
337 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
338 .pc_help = "Verify mirrored file(s).\n"
339 "usage: lfs mirror verify "
340 "[--only <mirror_id,mirror_id2[,...]>] "
341 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
342 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
343 .pc_help = "list commands supported by lfs mirror"},
344 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
345 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
346 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
351 * command_t pcc_cmdlist - lfs pcc commands.
353 command_t pcc_cmdlist[] = {
354 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
355 .pc_help = "Attach given files to the Persistent Client Cache.\n"
356 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
357 "\t-i: archive id for RW-PCC\n" },
358 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
359 .pc_help = "Attach given files into PCC by FID(s).\n"
360 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
362 "\t-i: archive id for RW-PCC\n"
363 "\t-m: Lustre mount point\n" },
364 { .pc_name = "state", .pc_func = lfs_pcc_state,
365 .pc_help = "Display the PCC state for given files.\n"
366 "usage: lfs pcc state <file> ...\n" },
367 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
368 .pc_help = "Detach given files from the Persistent Client Cache.\n"
369 "usage: lfs pcc detach <file> ...\n" },
370 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
371 .pc_help = "Detach given files from PCC by FID(s).\n"
372 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
373 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
374 .pc_help = "list commands supported by lfs pcc"},
375 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
376 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
377 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
381 /* all available commands */
382 command_t cmdlist[] = {
383 {"setstripe", lfs_setstripe, 0,
384 "To create a file with specified striping/composite layout, or\n"
385 "create/replace the default layout on an existing directory:\n"
386 SSM_CMD_COMMON("setstripe")
388 " <directory|filename>\n"
390 "To add component(s) to an existing composite file:\n"
391 SSM_CMD_COMMON("setstripe --component-add")
393 "To totally delete the default striping from an existing directory:\n"
394 "usage: setstripe -d <directory>\n"
396 "To create a mirrored file or set s default mirror layout on a directory:\n"
397 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
399 "To delete the last component(s) from an existing composite file\n"
400 "(note that this will also delete any data in those components):\n"
401 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
402 " [--component-flags|-F <comp_flags>]\n"
404 "\tcomp_id: Unique component ID to delete\n"
405 "\tcomp_flags: 'init' indicating all instantiated components\n"
406 "\t '^init' indicating all uninstantiated components\n"
407 "\t-I and -F cannot be specified at the same time\n"
408 "To create a file with a foreign (free format) layout:\n"
409 "usage: setstripe --foreign[=<foreign_type>]\n"
410 " --xattr|-x <layout_string> [--flags <hex>]\n"
411 " [--mode <mode>] <filename>\n"},
412 {"getstripe", lfs_getstripe, 0,
413 "To list the layout pattern for a given file or files in a\n"
414 "directory or recursively for all files in a directory tree.\n"
415 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
416 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
417 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
418 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
419 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
420 " [--component-id[=comp_id]|-I[comp_id]]\n"
421 " [--component-flags[=comp_flags]]\n"
422 " [--component-count]\n"
423 " [--extension-size|--ext-size|-z]\n"
424 " [--component-start[=[+-]comp_start]]\n"
425 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
426 " [[!] --mirror-index=[+-]<index> |\n"
427 " [!] --mirror-id=[+-]<id>]\n"
428 " <directory|filename> ..."},
429 {"setdirstripe", lfs_setdirstripe, 0,
430 "To create a striped directory on a specified MDT. This can only\n"
431 "be done on MDT0 with the right of administrator.\n"
432 "usage: setdirstripe [OPTION] <directory>\n"
434 {"getdirstripe", lfs_getdirstripe, 0,
435 "To list the layout pattern info for a given directory\n"
436 "or recursively for all directories in a directory tree.\n"
437 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
438 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
439 " [--recursive|-r] [--yaml|-y]\n"
440 " [--verbose|-v] [--default|-D] <dir> ..."},
441 {"mkdir", lfs_setdirstripe, 0,
442 "To create a striped directory on a specified MDT. This can only\n"
443 "be done on MDT0 with the right of administrator.\n"
444 "usage: mkdir [OPTION] <directory>\n"
446 {"rm_entry", lfs_rmentry, 0,
447 "To remove the name entry of the remote directory. Note: This\n"
448 "command will only delete the name entry, i.e. the remote directory\n"
449 "will become inaccessable after this command. This can only be done\n"
450 "by the administrator\n"
451 "usage: rm_entry <dir>\n"},
452 {"pool_list", lfs_poollist, 0,
453 "List pools or pool OSTs\n"
454 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
455 {"find", lfs_find, 0,
456 "find files matching given attributes recursively in directory tree.\n"
457 "usage: find <directory|filename> ...\n"
458 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
459 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
460 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
461 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
462 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
463 " [[!] --stripe-count|-c [+-]<stripes>]\n"
464 " [[!] --stripe-index|-i <index,...>]\n"
465 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
466 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
467 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
468 " [[!] --projid <projid>]\n"
469 " [[!] --foreign[=<foreign_type>]]\n"
470 " [[!] --layout|-L released,raid0,mdt]\n"
471 " [[!] --foreign[=<foreign_type>]]\n"
472 " [[!] --component-count [+-]<comp_cnt>]\n"
473 " [[!] --component-start [+-]N[kMGTPE]]\n"
474 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
475 " [[!] --component-flags <comp_flags>]\n"
476 " [[!] --mirror-count|-N [+-]<n>]\n"
477 " [[!] --mirror-state <[^]state>]\n"
478 " [[!] --mdt-count|-T [+-]<stripes>]\n"
479 " [[!] --mdt-hash|-H <hashtype>\n"
480 " [[!] --mdt-index|-m <uuid|index,...>]\n"
481 "\t !: used before an option indicates 'NOT' requested attribute\n"
482 "\t -: used before a value indicates less than requested value\n"
483 "\t +: used before a value indicates more than requested value\n"
484 "\thashtype: hash type of the striped directory.\n"
485 "\t fnv_1a_64 FNV-1a hash algorithm\n"
486 "\t all_char sum of characters % MDT_COUNT\n"},
487 {"check", lfs_check, 0,
488 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
489 "or all the servers (MGTs, MDTs and OSTs).\n"
490 "usage: check <mgts|osts|mdts|all>"},
491 {"osts", lfs_osts, 0, "list OSTs connected to client "
492 "[for specified path only]\n" "usage: osts [path]"},
493 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
494 "[for specified path only]\n" "usage: mdts [path]"},
496 "report filesystem disk space usage or inodes usage "
497 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
498 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
499 {"getname", lfs_getname, 0,
500 "list instances and specified mount points [for specified path only]\n"
501 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
502 #ifdef HAVE_SYS_QUOTA_H
503 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
504 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
505 " -b <block-softlimit> -B <block-hardlimit>\n"
506 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
507 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
508 " [--block-softlimit <block-softlimit>]\n"
509 " [--block-hardlimit <block-hardlimit>]\n"
510 " [--inode-softlimit <inode-softlimit>]\n"
511 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
512 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
513 " [--block-grace <block-grace>]\n"
514 " [--inode-grace <inode-grace>] <filesystem>\n"
515 " setquota <-U|-G|-P>\n"
516 " -b <block-softlimit> -B <block-hardlimit>\n"
517 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
518 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
519 " [--block-softlimit <block-softlimit>]\n"
520 " [--block-hardlimit <block-hardlimit>]\n"
521 " [--inode-softlimit <inode-softlimit>]\n"
522 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
523 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
525 " -b can be used instead of --block-softlimit/--block-grace\n"
526 " -B can be used instead of --block-hardlimit\n"
527 " -i can be used instead of --inode-softlimit/--inode-grace\n"
528 " -I can be used instead of --inode-hardlimit\n"
529 " -d can be used instead of --default\n\n"
530 "Note: The total quota space will be split into many qunits and\n"
531 " balanced over all server targets, the minimal qunit size is\n"
532 " 1M bytes for block space and 1K inodes for inode space.\n\n"
533 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
534 " Quota space rebalancing process will stop when this mininum\n"
535 " value is reached. As a result, quota exceeded can be returned\n"
536 " while many targets still have 1MB or 1K inodes of spare\n"
538 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
539 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
541 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
542 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
543 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
544 {"project", lfs_project, 0,
545 "Change or list project attribute for specified file or directory.\n"
546 "usage: project [-d|-r] <file|directory...>\n"
547 " list project ID and flags on file(s) or directories\n"
548 " project [-p id] [-s] [-r] <file|directory...>\n"
549 " set project ID and/or inherit flag for specified file(s) or directories\n"
550 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
551 " check project ID and flags on file(s) or directories, print outliers\n"
552 " project -C [-r] [-k] <file|directory...>\n"
553 " clear the project inherit flag and ID on the file or directory\n"
556 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
557 "usage: flushctx [-k] [mountpoint...]"},
558 {"changelog", lfs_changelog, 0,
559 "Show the metadata changes on an MDT."
560 "\nusage: changelog <mdtname> [startrec [endrec]]"},
561 {"changelog_clear", lfs_changelog_clear, 0,
562 "Indicate that old changelog records up to <endrec> are no longer of "
563 "interest to consumer <id>, allowing the system to free up space.\n"
564 "An <endrec> of 0 means all records.\n"
565 "usage: changelog_clear <mdtname> <id> <endrec>"},
566 {"fid2path", lfs_fid2path, 0,
567 "Resolve the full path(s) for given FID(s). For a specific hardlink "
568 "specify link number <linkno>.\n"
569 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
570 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
571 "usage: path2fid [--parents] <path> ..."},
572 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
573 "usage: rmfid <fsname|rootpath> <fid> ..."},
574 {"data_version", lfs_data_version, 0, "Display file data version for "
575 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
576 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
577 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
578 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
579 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
580 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
581 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
583 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
584 "[--archived] [--lost] <file> ..."},
585 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
586 "given files.\n" "usage: hsm_action <file> ..."},
587 {"hsm_archive", lfs_hsm_archive, 0,
588 "Archive file to external storage.\n"
589 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
591 {"hsm_restore", lfs_hsm_restore, 0,
592 "Restore file from external storage.\n"
593 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
594 {"hsm_release", lfs_hsm_release, 0,
595 "Release files from Lustre.\n"
596 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
597 {"hsm_remove", lfs_hsm_remove, 0,
598 "Remove file copy from external storage.\n"
599 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
601 " (FILE [FILE ...] | "
602 "--mntpath MOUNTPATH FID [FID ...])\n"
604 "Note: To remove an archived copy of a file already deleted from a "
606 "--mntpath option and a list of FIDs must be specified"
608 {"hsm_cancel", lfs_hsm_cancel, 0,
609 "Cancel requests related to specified files.\n"
610 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
611 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
612 "usage: swap_layouts <path1> <path2>"},
613 {"migrate", lfs_setstripe_migrate, 0,
614 "migrate a directory between MDTs.\n"
615 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
616 " [--mdt-hash|-H] <hash_type>\n"
617 " [--mdt-index|-m] <start_mdt_index>\n"
620 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
621 " it's the MDT index of first stripe\n"
622 "\tmdt_count: number of MDTs to stripe a directory over\n"
623 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
624 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
625 " all_char sum of characters % MDT_COUNT\n"
627 "migrate file objects from one OST "
628 "layout\nto another (may be not safe with concurent writes).\n"
630 "[--stripe-count|-c] <stripe_count>\n"
631 "[--overstripe-count|-C] <stripe_count>\n"
632 " [--stripe-index|-i] <start_ost_index>\n"
633 " [--stripe-size|-S] <stripe_size>\n"
634 " [--pool|-p] <pool_name>\n"
635 " [--ost|-o] <ost_indices>\n"
637 " [--non-block|-n]\n"
638 " [--non-direct|-D]\n"
639 " <file|directory>\n"
640 "\tstripe_count: number of OSTs to stripe a file over\n"
641 "\t Using -C instead of -c allows overstriping, which\n"
642 "\t will place more than one stripe per OST if\n"
643 "\t stripe_count is greater than the number of OSTs\n"
644 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
645 "\tstripe_size: number of bytes to store before moving to the next OST\n"
646 "\tpool_name: name of the predefined pool of OSTs\n"
647 "\tost_indices: OSTs to stripe over, in order\n"
648 "\tblock: Block file access during data migration (default)\n"
649 "\tnon-block: Abort migrations if concurrent access is detected\n"
650 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
652 "To move directories between MDTs. This command is deprecated, "
653 "use \"migrate\" instead.\n"
654 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
656 {"ladvise", lfs_ladvise, 0,
657 "Provide servers with advice about access patterns for a file.\n"
658 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
659 " [--background|-b] [--unset|-u]\n\n"
660 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
661 " {[--mode|-m [READ,WRITE]}\n"
663 {"mirror", lfs_mirror, mirror_cmdlist,
664 "lfs commands used to manage files with mirrored components:\n"
665 "lfs mirror create - create a mirrored file or directory\n"
666 "lfs mirror extend - add mirror(s) to an existing file\n"
667 "lfs mirror split - split a mirror from an existing mirrored file\n"
668 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
669 "lfs mirror read - read a mirror content of a mirrored file\n"
670 "lfs mirror write - write to a mirror of a mirrored file\n"
671 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
672 "lfs mirror verify - verify mirrored file(s)\n"},
673 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
674 "usage: getsom [-s] [-b] [-f] <path>\n"
675 "\t-s: Only show the size value of the SOM data for a given file\n"
676 "\t-b: Only show the blocks value of the SOM data for a given file\n"
677 "\t-f: Only show the flags value of the SOM data for a given file\n"},
678 {"heat_get", lfs_heat_get, 0,
679 "To get heat of files.\n"
680 "usage: heat_get <file> ...\n"},
681 {"heat_set", lfs_heat_set, 0,
682 "To set heat flags of files.\n"
683 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
684 "\t--clear|-c: Clear file heat for given files\n"
685 "\t--off|-o: Turn off file heat for given files\n"
686 "\t--on|-O: Turn on file heat for given files\n"},
687 {"pcc", lfs_pcc, pcc_cmdlist,
688 "lfs commands used to interact with PCC features:\n"
689 "lfs pcc attach - attach given files to Persistent Client Cache\n"
690 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
691 "lfs pcc state - display the PCC state for given files\n"
692 "lfs pcc detach - detach given files from Persistent Client Cache\n"
693 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
694 {"help", Parser_help, 0, "help"},
695 {"exit", Parser_quit, 0, "quit"},
696 {"quit", Parser_quit, 0, "quit"},
697 {"--version", Parser_version, 0,
698 "output build version of the utility and exit"},
699 {"--list-commands", lfs_list_commands, 0,
700 "list commands supported by the utility and exit"},
705 static int check_hashtype(const char *hashtype)
709 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
710 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
716 static uint32_t check_foreign_type_name(const char *foreign_type_name)
720 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
721 if (lu_foreign_types[i].lft_name == NULL)
723 if (strcmp(foreign_type_name,
724 lu_foreign_types[i].lft_name) == 0)
725 return lu_foreign_types[i].lft_type;
728 return LU_FOREIGN_TYPE_UNKNOWN;
731 static const char *error_loc = "syserror";
734 MIGRATION_NONBLOCK = 0x0001,
735 MIGRATION_MIRROR = 0x0002,
736 MIGRATION_NONDIRECT = 0x0004,
737 MIGRATION_VERBOSE = 0x0008,
740 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
741 struct llapi_layout *layout);
744 migrate_open_files(const char *name, __u64 migration_flags,
745 const struct llapi_stripe_param *param,
746 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
753 char parent[PATH_MAX];
754 char volatile_file[PATH_MAX];
760 if (param == NULL && layout == NULL) {
761 error_loc = "layout information";
765 /* search for file directory pathname */
766 if (strlen(name) > sizeof(parent) - 1) {
767 error_loc = "source file name";
771 strncpy(parent, name, sizeof(parent));
772 ptr = strrchr(parent, '/');
774 if (getcwd(parent, sizeof(parent)) == NULL) {
775 error_loc = "getcwd";
779 if (ptr == parent) /* leading '/' */
784 /* open file, direct io */
785 /* even if the file is only read, WR mode is nedeed to allow
786 * layout swap on fd */
788 if (!(migration_flags & MIGRATION_NONDIRECT))
790 fd = open(name, rflags);
793 error_loc = "cannot open source file";
797 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
799 error_loc = "cannot get MDT index";
804 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
805 mode_t open_mode = S_IRUSR | S_IWUSR;
807 random_value = random();
808 rc = snprintf(volatile_file, sizeof(volatile_file),
809 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
810 mdt_index, random_value);
811 if (rc >= sizeof(volatile_file)) {
816 /* create, open a volatile file, use caching (ie no directio) */
818 fdv = llapi_file_open_param(volatile_file, open_flags,
821 fdv = lfs_component_create(volatile_file, open_flags,
823 } while (fdv < 0 && (rc = fdv) == -EEXIST);
826 error_loc = "cannot create volatile file";
830 /* In case the MDT does not support creation of volatile files
831 * we should try to unlink it. */
832 (void)unlink(volatile_file);
834 /* Not-owner (root?) special case.
835 * Need to set owner/group of volatile file like original.
836 * This will allow to pass related check during layout_swap.
841 error_loc = "cannot stat source file";
845 rc = fstat(fdv, &stv);
848 error_loc = "cannot stat volatile";
852 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
853 rc = fchown(fdv, st.st_uid, st.st_gid);
856 error_loc = "cannot change ownwership of volatile";
875 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
877 struct llapi_layout *layout;
878 size_t buf_size = 4 * 1024 * 1024;
887 layout = llapi_layout_get_by_fd(fd_src, 0);
888 if (layout != NULL) {
889 uint64_t stripe_size;
891 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
893 buf_size = stripe_size;
895 llapi_layout_free(layout);
898 /* Use a page-aligned buffer for direct I/O */
899 rc = posix_memalign(&buf, getpagesize(), buf_size);
904 /* read new data only if we have written all
905 * previously read data */
908 rc = check_file(fd_src);
913 rsize = read(fd_src, buf, buf_size);
925 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
944 static int migrate_copy_timestamps(int fd, int fdv)
948 if (fstat(fd, &st) == 0) {
949 struct timeval tv[2] = {
950 {.tv_sec = st.st_atime},
951 {.tv_sec = st.st_mtime}
954 return futimes(fdv, tv);
960 static int migrate_block(int fd, int fdv)
967 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
969 error_loc = "cannot get dataversion";
977 /* The grouplock blocks all concurrent accesses to the file.
978 * It has to be taken after llapi_get_data_version as it would
980 rc = llapi_group_lock(fd, gid);
982 error_loc = "cannot get group lock";
986 rc = migrate_copy_data(fd, fdv, NULL);
988 error_loc = "data copy failed";
992 /* Make sure we keep original atime/mtime values */
993 rc = migrate_copy_timestamps(fd, fdv);
995 error_loc = "timestamp copy failed";
1000 * for a migration we need to check data version on file did
1003 * Pass in gid=0 since we already own grouplock. */
1004 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1005 SWAP_LAYOUTS_CHECK_DV1);
1006 if (rc == -EAGAIN) {
1007 error_loc = "file changed";
1009 } else if (rc < 0) {
1010 error_loc = "cannot swap layout";
1015 rc2 = llapi_group_unlock(fd, gid);
1016 if (rc2 < 0 && rc == 0) {
1017 error_loc = "unlock group lock";
1025 * Internal helper for migrate_copy_data(). Check lease and report error if
1028 * \param[in] fd File descriptor on which to check the lease.
1030 * \retval 0 Migration can keep on going.
1031 * \retval -errno Error occurred, abort migration.
1033 static int check_lease(int fd)
1037 rc = llapi_lease_check(fd);
1039 return 0; /* llapi_check_lease returns > 0 on success. */
1044 static int migrate_nonblock(int fd, int fdv)
1050 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1052 error_loc = "cannot get data version";
1056 rc = migrate_copy_data(fd, fdv, check_lease);
1058 error_loc = "data copy failed";
1062 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1064 error_loc = "cannot get data version";
1070 error_loc = "source file changed";
1074 /* Make sure we keep original atime/mtime values */
1075 rc = migrate_copy_timestamps(fd, fdv);
1077 error_loc = "timestamp copy failed";
1084 static int lfs_component_set(char *fname, int comp_id,
1085 __u32 flags, __u32 neg_flags)
1088 __u32 flags_array[2];
1093 ids[count] = comp_id;
1094 flags_array[count] = flags;
1099 if (neg_flags & LCME_FL_STALE) {
1100 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1101 "component. Please use lfs-mirror-resync(1) "
1102 "instead\n", progname);
1106 ids[count] = comp_id;
1107 flags_array[count] = neg_flags | LCME_FL_NEG;
1111 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1113 if (errno == EUCLEAN) {
1116 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1117 progname, comp_id, fname);
1120 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1121 progname, comp_id, fname, flags, neg_flags);
1128 static int lfs_component_del(char *fname, __u32 comp_id,
1129 __u32 flags, __u32 neg_flags)
1133 if (flags && neg_flags)
1136 if (!flags && neg_flags)
1137 flags = neg_flags | LCME_FL_NEG;
1139 if ((flags && comp_id) || (!flags && !comp_id))
1143 if (flags & ~LCME_KNOWN_FLAGS) {
1145 "%s setstripe: unknown flags %#x\n",
1149 } else if (comp_id > LCME_ID_MAX) {
1150 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1155 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1158 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1159 progname, comp_id, fname, strerror(errno));
1163 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1170 rc = llapi_layout_file_comp_add(fname, layout);
1172 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1173 fname, strerror(errno));
1177 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1178 struct llapi_layout *layout)
1186 fd = lstat(fname, &st);
1187 if (fd == 0 && S_ISDIR(st.st_mode))
1188 open_flags = O_DIRECTORY | O_RDONLY;
1190 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1192 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1193 S_ISDIR(st.st_mode) ?
1194 "set default composite layout for" :
1195 "create composite file",
1196 fname, strerror(errno));
1200 static int lfs_migrate(char *name, __u64 migration_flags,
1201 struct llapi_stripe_param *param,
1202 struct llapi_layout *layout)
1204 struct llapi_layout *existing;
1205 uint64_t dom_new, dom_cur;
1210 rc = migrate_open_files(name, migration_flags, param, layout,
1215 rc = llapi_layout_dom_size(layout, &dom_new);
1217 error_loc = "cannot get new layout DoM size";
1220 /* special case for migration to DOM layout*/
1221 existing = llapi_layout_get_by_fd(fd, 0);
1223 error_loc = "cannot get existing layout";
1227 rc = llapi_layout_dom_size(existing, &dom_cur);
1229 error_loc = "cannot get current layout DoM size";
1233 /* if file has DoM layout already then migration is possible to
1234 * the new layout with the same DoM component via swap layout,
1235 * if new layout used bigger DOM size, then mirroring is used
1237 if (dom_new > dom_cur) {
1238 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1241 error_loc = "cannot migrate to DOM layout";
1245 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1246 /* Blocking mode (forced if servers do not support file lease).
1247 * It is also the default mode, since we cannot distinguish
1248 * between a broken lease and a server that does not support
1249 * atomic swap/close (LU-6785) */
1250 rc = migrate_block(fd, fdv);
1254 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1256 error_loc = "cannot get lease";
1260 rc = migrate_nonblock(fd, fdv);
1262 llapi_lease_release(fd);
1266 /* Atomically put lease, swap layouts and close.
1267 * for a migration we need to check data version on file did
1269 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1271 error_loc = "cannot swap layout";
1283 fprintf(stderr, "error: %s: %s: %s: %s\n",
1284 progname, name, error_loc, strerror(-rc));
1285 else if (migration_flags & MIGRATION_VERBOSE)
1286 printf("%s\n", name);
1291 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1300 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1304 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1305 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1306 const char *comp_name = comp_flags_table[i].cfn_name;
1308 if (strcmp(name, comp_name) == 0) {
1309 *flags |= comp_flag;
1311 } else if (strncmp(name, "^", 1) == 0 &&
1312 strcmp(name + 1, comp_name) == 0) {
1313 *neg_flags |= comp_flag;
1318 llapi_printf(LLAPI_MSG_ERROR,
1319 "%s: component flag '%s' not supported\n",
1325 if (!*flags && !*neg_flags)
1328 /* don't allow to set and exclude the same flag */
1329 if (*flags & *neg_flags)
1335 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1343 if (strncmp(string, "^", 1) == 0) {
1344 *neg_state = llapi_layout_string_flags(string + 1);
1345 if (*neg_state != 0)
1348 *state = llapi_layout_string_flags(string);
1353 llapi_printf(LLAPI_MSG_ERROR,
1354 "%s: mirrored file state '%s' not supported\n",
1360 * struct mirror_args - Command-line arguments for mirror(s).
1361 * @m_count: Number of mirrors to be created with this layout.
1362 * @m_flags: Mirror level flags, only 'prefer' is supported.
1363 * @m_layout: Mirror layout.
1364 * @m_file: A victim file. Its layout will be split and used as a mirror.
1365 * @m_next: Point to the next node of the list.
1367 * Command-line arguments for mirror(s) will be parsed and stored in
1368 * a linked list that consists of this structure.
1370 struct mirror_args {
1373 struct llapi_layout *m_layout;
1375 struct mirror_args *m_next;
1379 * enum mirror_flags - Flags for extending a mirrored file.
1380 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1381 * in case the victim file(s) contains the same data as the
1382 * original mirrored file.
1383 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1384 * @MF_COMP_ID: specified component id instead of mirror id
1386 * Flags for extending a mirrored file.
1396 * mirror_create_sanity_check() - Check mirror list.
1397 * @list: A linked list that stores the mirror arguments.
1399 * This function does a sanity check on @list for creating
1402 * Return: 0 on success or a negative error code on failure.
1404 static int mirror_create_sanity_check(const char *fname,
1405 struct mirror_args *list)
1408 bool has_m_file = false;
1409 bool has_m_layout = false;
1415 struct llapi_layout *layout;
1417 layout = llapi_layout_get_by_path(fname, 0);
1420 "error: %s: file '%s' couldn't get layout\n",
1425 rc = llapi_layout_sanity(layout, false, true);
1427 llapi_layout_free(layout);
1430 llapi_layout_sanity_perror(rc);
1435 while (list != NULL) {
1436 if (list->m_file != NULL) {
1438 llapi_layout_free(list->m_layout);
1441 llapi_layout_get_by_path(list->m_file, 0);
1442 if (list->m_layout == NULL) {
1444 "error: %s: file '%s' has no layout\n",
1445 progname, list->m_file);
1449 has_m_layout = true;
1450 if (list->m_layout == NULL) {
1451 fprintf(stderr, "error: %s: no mirror layout\n",
1457 rc = llapi_layout_sanity(list->m_layout, false, true);
1459 llapi_layout_sanity_perror(rc);
1463 list = list->m_next;
1466 if (has_m_file && has_m_layout) {
1468 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1476 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1478 __u32 mirror_flags = *(__u32 *)cbdata;
1482 rc = llapi_layout_comp_flags_get(layout, &flags);
1487 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1492 return LLAPI_LAYOUT_ITER_CONT;
1496 * mirror_create() - Create a mirrored file.
1497 * @fname: The file to be created.
1498 * @mirror_list: A linked list that stores the mirror arguments.
1500 * This function creates a mirrored file @fname with the mirror(s)
1501 * from @mirror_list.
1503 * Return: 0 on success or a negative error code on failure.
1505 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1507 struct llapi_layout *layout = NULL;
1508 struct mirror_args *cur_mirror = NULL;
1509 uint16_t mirror_count = 0;
1513 rc = mirror_create_sanity_check(NULL, mirror_list);
1517 cur_mirror = mirror_list;
1518 while (cur_mirror != NULL) {
1519 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1521 &cur_mirror->m_flags);
1524 fprintf(stderr, "%s: failed to set mirror flags\n",
1529 for (i = 0; i < cur_mirror->m_count; i++) {
1530 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1533 fprintf(stderr, "error: %s: "
1534 "merge layout failed: %s\n",
1535 progname, strerror(errno));
1539 mirror_count += cur_mirror->m_count;
1540 cur_mirror = cur_mirror->m_next;
1543 if (layout == NULL) {
1544 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1548 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1551 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1552 progname, strerror(errno));
1556 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1564 llapi_layout_free(layout);
1569 * Compare files and check lease on @fd.
1571 * \retval bytes number of bytes are the same
1573 static ssize_t mirror_file_compare(int fd, int fdv)
1575 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1577 ssize_t bytes_done = 0;
1578 ssize_t bytes_read = 0;
1580 buf = malloc(buflen * 2);
1585 if (!llapi_lease_check(fd)) {
1586 bytes_done = -EBUSY;
1590 bytes_read = read(fd, buf, buflen);
1591 if (bytes_read <= 0)
1594 if (bytes_read != read(fdv, buf + buflen, buflen))
1597 /* XXX: should compute the checksum on each buffer and then
1598 * compare checksum to avoid cache collision */
1599 if (memcmp(buf, buf + buflen, bytes_read))
1602 bytes_done += bytes_read;
1610 static int mirror_extend_file(const char *fname, const char *victim_file,
1611 enum mirror_flags mirror_flags)
1616 struct stat stbuf_v;
1617 struct ll_ioc_lease *data = NULL;
1620 fd = open(fname, O_RDWR);
1622 error_loc = "open source file";
1627 fdv = open(victim_file, O_RDWR);
1629 error_loc = "open target file";
1634 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1635 error_loc = "stat source or target file";
1640 if (stbuf.st_dev != stbuf_v.st_dev) {
1641 error_loc = "stat source and target file";
1646 /* mirrors should be of the same size */
1647 if (stbuf.st_size != stbuf_v.st_size) {
1648 error_loc = "file sizes don't match";
1653 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1655 error_loc = "cannot get lease";
1659 if (!(mirror_flags & MF_NO_VERIFY)) {
1661 /* mirrors should have the same contents */
1662 ret = mirror_file_compare(fd, fdv);
1663 if (ret != stbuf.st_size) {
1664 error_loc = "file busy or contents don't match";
1665 rc = ret < 0 ? ret : -EINVAL;
1670 /* Get rid of caching pages from clients */
1671 rc = llapi_file_flush(fd);
1673 error_loc = "cannot get data version";
1677 rc = llapi_file_flush(fdv);
1679 error_loc = "cannot get data version";
1684 /* Make sure we keep original atime/mtime values */
1685 rc = migrate_copy_timestamps(fd, fdv);
1687 error_loc = "cannot copy timestamp";
1691 /* Atomically put lease, merge layouts and close. */
1692 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1694 error_loc = "memory allocation";
1697 data->lil_mode = LL_LEASE_UNLCK;
1698 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1699 data->lil_count = 1;
1700 data->lil_ids[0] = fdv;
1701 rc = llapi_lease_set(fd, data);
1703 error_loc = "cannot merge layout";
1705 } else if (rc == 0) {
1707 error_loc = "lost lease lock";
1720 (void) unlink(victim_file);
1722 fprintf(stderr, "error: %s: %s: %s: %s\n",
1723 progname, fname, error_loc, strerror(-rc));
1727 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1729 struct ll_ioc_lease *data = NULL;
1734 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1738 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1740 error_loc = "cannot get lease";
1744 rc = migrate_nonblock(fd, fdv);
1746 llapi_lease_release(fd);
1750 /* Atomically put lease, merge layouts and close. */
1751 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1753 error_loc = "memory allocation";
1756 data->lil_mode = LL_LEASE_UNLCK;
1757 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1758 data->lil_count = 1;
1759 data->lil_ids[0] = fdv;
1760 rc = llapi_lease_set(fd, data);
1762 error_loc = "cannot merge layout";
1764 } else if (rc == 0) {
1766 error_loc = "lost lease lock";
1779 fprintf(stderr, "error: %s: %s: %s: %s\n",
1780 progname, name, error_loc, strerror(-rc));
1784 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1785 enum mirror_flags mirror_flags)
1789 rc = mirror_create_sanity_check(fname, mirror_list);
1793 while (mirror_list) {
1794 if (mirror_list->m_file != NULL) {
1795 rc = mirror_extend_file(fname, mirror_list->m_file,
1798 __u32 mirror_count = mirror_list->m_count;
1800 while (mirror_count > 0) {
1801 rc = mirror_extend_layout(fname,
1802 mirror_list->m_layout);
1812 mirror_list = mirror_list->m_next;
1818 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1823 rc = llapi_layout_mirror_id_get(layout, &id);
1827 if ((__u16)id == *(__u16 *)cbdata)
1828 return LLAPI_LAYOUT_ITER_STOP;
1830 return LLAPI_LAYOUT_ITER_CONT;
1833 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1838 rc = llapi_layout_comp_id_get(layout, &id);
1842 if (id == *(__u32 *)cbdata)
1843 return LLAPI_LAYOUT_ITER_STOP;
1845 return LLAPI_LAYOUT_ITER_CONT;
1848 struct pool_to_id_cbdata {
1852 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1854 char buf[LOV_MAXPOOLNAME + 1];
1855 struct pool_to_id_cbdata *d = (void *)cbdata;
1859 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1862 if (strcmp(d->pool, buf))
1863 return LLAPI_LAYOUT_ITER_CONT;
1865 rc = llapi_layout_mirror_id_get(layout, &id);
1870 return LLAPI_LAYOUT_ITER_STOP;
1873 struct collect_ids_data {
1879 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1881 struct collect_ids_data *cid = cbdata;
1885 rc = llapi_layout_mirror_id_get(layout, &id);
1889 if ((__u16)id != cid->cid_exclude) {
1892 for (i = 0; i < cid->cid_count; i++) {
1893 /* already collected the mirror id */
1894 if (id == cid->cid_ids[i])
1895 return LLAPI_LAYOUT_ITER_CONT;
1897 cid->cid_ids[cid->cid_count] = id;
1901 return LLAPI_LAYOUT_ITER_CONT;
1905 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1906 * @mirror_id: Mirror id to be checked.
1907 * @layout: Mirror component list.
1909 * This function checks if a mirror with specified @mirror_id is the last
1910 * non-stale mirror of a layout @layout.
1912 * Return: true or false.
1915 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1917 __u16 mirror_ids[128] = { 0 };
1918 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1920 .cid_exclude = mirror_id, };
1923 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1925 for (i = 0; i < cid.cid_count; i++) {
1926 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1929 comp_size = llapi_mirror_find_stale(layout, comp_array,
1930 ARRAY_SIZE(comp_array),
1939 static int mirror_split(const char *fname, __u32 id, const char *pool,
1940 enum mirror_flags mflags, const char *victim_file)
1942 struct llapi_layout *layout;
1943 char parent[PATH_MAX];
1944 char victim[PATH_MAX];
1945 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1947 struct ll_ioc_lease *data;
1948 uint16_t mirror_count;
1953 /* check fname contains mirror with mirror_id/comp_id */
1954 layout = llapi_layout_get_by_path(fname, 0);
1957 "error %s: file '%s' couldn't get layout\n",
1962 rc = llapi_layout_sanity(layout, false, true);
1964 llapi_layout_sanity_perror(rc);
1968 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1971 "error %s: file '%s' couldn't get mirror count\n",
1975 if (mirror_count < 2) {
1977 "error %s: file '%s' has %d component, cannot split\n",
1978 progname, fname, mirror_count);
1982 if (mflags & MF_COMP_POOL) {
1983 struct pool_to_id_cbdata data = { .pool = pool };
1985 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
1988 } else if (mflags & MF_COMP_ID) {
1989 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1990 id = mirror_id_of(id);
1992 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1995 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1998 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2000 "error %s: file '%s' does not contain mirror with id %u\n",
2001 progname, fname, id);
2005 fd = open(fname, O_RDWR);
2008 "error %s: open file '%s' failed: %s\n",
2009 progname, fname, strerror(errno));
2013 /* get victim file directory pathname */
2014 if (strlen(fname) > sizeof(parent) - 1) {
2015 fprintf(stderr, "error %s: file name of '%s' too long\n",
2020 strncpy(parent, fname, sizeof(parent));
2021 ptr = strrchr(parent, '/');
2023 if (getcwd(parent, sizeof(parent)) == NULL) {
2024 fprintf(stderr, "error %s: getcwd failed: %s\n",
2025 progname, strerror(errno));
2035 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2037 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2042 if (victim_file == NULL) {
2043 /* use a temp file to store the splitted layout */
2044 if (mflags & MF_DESTROY) {
2045 if (last_non_stale_mirror(id, layout)) {
2048 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2053 fdv = llapi_create_volatile_idx(parent, mdt_index,
2054 O_LOV_DELAY_CREATE);
2056 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2058 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2061 /* user specified victim file */
2062 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2067 "error %s: create victim file failed: %s\n",
2068 progname, strerror(errno));
2072 /* get lease lock of fname */
2073 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2076 "error %s: cannot get lease of file '%s': %d\n",
2077 progname, fname, rc);
2081 /* Atomatically put lease, split layouts and close. */
2082 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2088 data->lil_mode = LL_LEASE_UNLCK;
2089 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2090 data->lil_count = 2;
2091 data->lil_ids[0] = fdv;
2092 data->lil_ids[1] = id;
2093 rc = llapi_lease_set(fd, data);
2095 if (rc == 0) /* lost lease lock */
2098 "error %s: cannot split '%s': %s\n",
2099 progname, fname, strerror(-rc));
2110 llapi_layout_free(layout);
2115 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2116 __u16 *mirror_ids, int ids_nr);
2118 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2119 __u64 migration_flags,
2120 struct llapi_stripe_param *param,
2121 struct llapi_layout *layout)
2123 struct ll_ioc_lease *data = NULL;
2126 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2128 error_loc = "cannot get lease";
2132 /* Atomically put lease, merge layouts, resync and close. */
2133 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2135 error_loc = "memory allocation";
2138 data->lil_mode = LL_LEASE_UNLCK;
2139 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2140 data->lil_count = 1;
2141 data->lil_ids[0] = fdv;
2142 rc = llapi_lease_set(fd, data);
2144 error_loc = "cannot merge layout";
2146 } else if (rc == 0) {
2148 error_loc = "lost lease lock";
2154 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2156 error_loc = "cannot resync file";
2160 /* delete first mirror now */
2161 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2163 error_loc = "cannot delete old layout";
2171 fprintf(stderr, "error: %s: %s: %s: %s\n",
2172 progname, name, error_loc, strerror(-rc));
2173 else if (migration_flags & MIGRATION_VERBOSE)
2174 printf("%s\n", name);
2181 * Parse a string containing an target index list into an array of integers.
2183 * The input string contains a comma delimited list of individual
2184 * indices and ranges, for example "1,2-4,7". Add the indices into the
2185 * \a tgts array and remove duplicates.
2187 * \param[out] tgts array to store indices in
2188 * \param[in] size size of \a tgts array
2189 * \param[in] offset starting index in \a tgts
2190 * \param[in] arg string containing OST index list
2191 * \param[in/out] overstriping index list may contain duplicates
2193 * \retval positive number of indices in \a tgts
2194 * \retval -EINVAL unable to parse \a arg
2196 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2197 unsigned long long *pattern)
2201 int slots = size - offset;
2203 bool overstriped = false;
2209 end_of_loop = false;
2210 while (!end_of_loop) {
2211 int start_index = 0;
2214 char *endptr = NULL;
2218 ptr = strchrnul(arg, ',');
2220 end_of_loop = *ptr == '\0';
2223 start_index = strtol(arg, &endptr, 0);
2224 if (endptr == arg) /* no data at all */
2226 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2229 end_index = start_index;
2230 if (*endptr == '-') {
2231 end_index = strtol(endptr + 1, &endptr, 0);
2232 if (*endptr != '\0')
2234 if (end_index < start_index)
2238 for (i = start_index; i <= end_index && slots > 0; i++) {
2241 /* remove duplicate */
2242 for (j = 0; j < offset; j++) {
2243 if (tgts[j] == i && pattern &&
2244 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2246 else if (tgts[j] == i)
2252 if (j == offset) { /* check complete */
2258 if (slots == 0 && i < end_index)
2266 if (!end_of_loop && ptr != NULL)
2269 if (!overstriped && pattern)
2270 *pattern = LLAPI_LAYOUT_DEFAULT;
2272 return rc < 0 ? rc : nr;
2275 struct lfs_setstripe_args {
2276 unsigned long long lsa_comp_end;
2277 unsigned long long lsa_stripe_size;
2278 unsigned long long lsa_extension_size;
2279 long long lsa_stripe_count;
2280 long long lsa_stripe_off;
2281 __u32 lsa_comp_flags;
2282 __u32 lsa_comp_neg_flags;
2283 unsigned long long lsa_pattern;
2284 unsigned int lsa_mirror_count;
2286 bool lsa_first_comp;
2287 bool lsa_extension_comp;
2289 char *lsa_pool_name;
2292 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2294 unsigned int mirror_count = lsa->lsa_mirror_count;
2295 bool first_comp = lsa->lsa_first_comp;
2297 memset(lsa, 0, sizeof(*lsa));
2299 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2300 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2301 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2302 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2303 lsa->lsa_pool_name = NULL;
2305 lsa->lsa_mirror_count = mirror_count;
2306 lsa->lsa_first_comp = first_comp;
2310 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2311 * @lsa: Stripe options to be initialized and inherited.
2313 * This function initializes stripe options in @lsa and inherit
2314 * stripe_size, stripe_count and OST pool_name options.
2318 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2320 unsigned long long stripe_size;
2321 long long stripe_count;
2322 char *pool_name = NULL;
2324 stripe_size = lsa->lsa_stripe_size;
2325 stripe_count = lsa->lsa_stripe_count;
2326 pool_name = lsa->lsa_pool_name;
2328 setstripe_args_init(lsa);
2330 lsa->lsa_stripe_size = stripe_size;
2331 lsa->lsa_stripe_count = stripe_count;
2332 lsa->lsa_pool_name = pool_name;
2335 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2337 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2338 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2339 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2340 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2341 lsa->lsa_comp_end != 0);
2345 * comp_args_to_layout() - Create or extend a composite layout.
2346 * @composite: Pointer to the composite layout.
2347 * @lsa: Stripe options for the new component.
2349 * This function creates or extends a composite layout by adding a new
2350 * component with stripe options from @lsa.
2352 * Return: 0 on success or an error code on failure.
2354 static int comp_args_to_layout(struct llapi_layout **composite,
2355 struct lfs_setstripe_args *lsa,
2358 struct llapi_layout *layout = *composite;
2359 uint64_t prev_end = 0;
2364 if (layout == NULL) {
2365 layout = llapi_layout_alloc();
2366 if (layout == NULL) {
2367 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2372 *composite = layout;
2373 lsa->lsa_first_comp = true;
2377 /* Get current component extent, current component
2378 * must be the tail component. */
2379 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2381 fprintf(stderr, "Get comp extent failed. %s\n",
2386 if (lsa->lsa_first_comp)
2389 if (lsa->lsa_first_comp)
2390 rc = llapi_layout_add_first_comp(layout);
2392 rc = llapi_layout_comp_add(layout);
2394 fprintf(stderr, "Add component failed. %s\n",
2400 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2402 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2403 lsa->lsa_comp_flags, strerror(errno));
2408 uint64_t comp_end = lsa->lsa_comp_end;
2410 /* The extendable component is 0-length, so it can be removed
2411 * if there is insufficient space to extend it. */
2412 if (lsa->lsa_extension_comp)
2413 comp_end = prev_end;
2415 rc = llapi_layout_comp_extent_set(layout, prev_end,
2418 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2419 prev_end, comp_end, strerror(errno));
2423 /* reset lsa_first_comp */
2424 lsa->lsa_first_comp = false;
2426 /* Data-on-MDT component setting */
2427 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2428 /* In case of Data-on-MDT patterns the only extra option
2429 * applicable is stripe size option. */
2430 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2431 fprintf(stderr, "Option 'stripe-count' can't be "
2432 "specified with Data-on-MDT component: %lld\n",
2433 lsa->lsa_stripe_count);
2437 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2438 fprintf(stderr, "Option 'stripe-size' can't be "
2439 "specified with Data-on-MDT component: %llu\n",
2440 lsa->lsa_stripe_size);
2444 if (lsa->lsa_nr_tgts != 0) {
2445 fprintf(stderr, "Option 'ost-list' can't be specified "
2446 "with Data-on-MDT component: '%i'\n",
2451 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2452 fprintf(stderr, "Option 'stripe-offset' can't be "
2453 "specified with Data-on-MDT component: %lld\n",
2454 lsa->lsa_stripe_off);
2458 if (lsa->lsa_pool_name != 0) {
2459 fprintf(stderr, "Option 'pool' can't be specified "
2460 "with Data-on-MDT component: '%s'\n",
2461 lsa->lsa_pool_name);
2466 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2468 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2473 /* Data-on-MDT component has always single stripe up to end */
2474 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2475 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2476 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2478 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2485 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2486 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2488 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2489 rc = llapi_layout_extension_size_set(layout, size);
2491 rc = llapi_layout_stripe_size_set(layout, size);
2494 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2495 size, strerror(errno));
2499 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2501 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2502 lsa->lsa_stripe_count, strerror(errno));
2506 if (lsa->lsa_pool_name != NULL) {
2507 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2509 fprintf(stderr, "Set pool name: %s failed. %s\n",
2510 lsa->lsa_pool_name, strerror(errno));
2514 rc = llapi_layout_pool_name_set(layout, "");
2516 fprintf(stderr, "Clear pool name failed: %s\n",
2522 if (lsa->lsa_nr_tgts > 0) {
2523 if (lsa->lsa_stripe_count > 0 &&
2524 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2525 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2526 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2527 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2528 lsa->lsa_stripe_count,
2533 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2534 rc = llapi_layout_ost_index_set(layout, i,
2539 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2540 lsa->lsa_stripe_off != -1) {
2541 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2544 fprintf(stderr, "Set ost index %d failed. %s\n",
2545 i, strerror(errno));
2549 /* Create the second, virtual component of extension space */
2550 if (lsa->lsa_extension_comp) {
2551 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2552 lsa->lsa_extension_comp = false;
2559 static int build_component(struct llapi_layout **layout,
2560 struct lfs_setstripe_args *lsa, bool set_extent)
2564 rc = comp_args_to_layout(layout, lsa, set_extent);
2568 if (lsa->lsa_mirror_count > 0) {
2569 rc = llapi_layout_mirror_count_set(*layout,
2570 lsa->lsa_mirror_count);
2574 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2577 lsa->lsa_mirror_count = 0;
2583 static int build_layout_from_yaml_node(struct cYAML *node,
2584 struct llapi_layout **layout,
2585 struct lfs_setstripe_args *lsa,
2592 if (node->cy_type == CYAML_TYPE_OBJECT) {
2593 /* go deep to sub blocks */
2594 rc = build_layout_from_yaml_node(node->cy_child, layout,
2599 if (node->cy_string == NULL)
2602 string = node->cy_string;
2603 /* skip leading lmm_ if present, to simplify parsing */
2604 if (strncmp(string, "lmm_", 4) == 0)
2607 if (node->cy_type == CYAML_TYPE_STRING) {
2608 if (!strcmp(string, "lcme_extent.e_end")) {
2609 if (!strcmp(node->cy_valuestring, "EOF") ||
2610 !strcmp(node->cy_valuestring, "eof"))
2611 lsa->lsa_comp_end = LUSTRE_EOF;
2612 } else if (!strcmp(string, "pool")) {
2613 lsa->lsa_pool_name = node->cy_valuestring;
2614 } else if (!strcmp(string, "pattern")) {
2615 if (!strcmp(node->cy_valuestring, "mdt"))
2616 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2617 if (!strcmp(node->cy_valuestring,
2618 "raid0,overstriped"))
2620 LLAPI_LAYOUT_OVERSTRIPING;
2621 } else if (!strcmp(string, "lcme_flags")) {
2622 rc = comp_str2flags(node->cy_valuestring,
2623 &lsa->lsa_comp_flags,
2624 &lsa->lsa_comp_neg_flags);
2627 /* Only template flags have meaning in
2628 * the layout for a new file
2630 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2632 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2633 if (!strcmp(string, "lcm_mirror_count")) {
2634 lsa->lsa_mirror_count = node->cy_valueint;
2635 } else if (!strcmp(string, "lcme_extent.e_start")) {
2636 if (node->cy_valueint != 0 || *layout != NULL) {
2637 rc = build_component(layout, lsa, true);
2642 if (node->cy_valueint == 0)
2643 lsa->lsa_first_comp = true;
2645 /* initialize lsa */
2646 setstripe_args_init(lsa);
2647 lsa->lsa_tgts = osts;
2648 } else if (!strcmp(string, "lcme_extent.e_end")) {
2649 if (node->cy_valueint == -1)
2650 lsa->lsa_comp_end = LUSTRE_EOF;
2652 lsa->lsa_comp_end = node->cy_valueint;
2653 } else if (!strcmp(string, "stripe_count")) {
2654 lsa->lsa_stripe_count = node->cy_valueint;
2655 } else if (!strcmp(string, "stripe_size")) {
2656 lsa->lsa_stripe_size = node->cy_valueint;
2657 } else if (!strcmp(string, "stripe_offset")) {
2658 lsa->lsa_stripe_off = node->cy_valueint;
2659 } else if (!strcmp(string, "l_ost_idx")) {
2660 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2665 node = node->cy_next;
2671 static int lfs_comp_create_from_yaml(char *template,
2672 struct llapi_layout **layout,
2673 struct lfs_setstripe_args *lsa,
2676 struct cYAML *tree = NULL, *err_rc = NULL;
2679 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2681 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2682 progname, template);
2683 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2684 "can't parse", &err_rc);
2685 cYAML_print_tree2file(stderr, err_rc);
2686 cYAML_free_tree(err_rc);
2691 /* initialize lsa for plain file */
2692 setstripe_args_init(lsa);
2693 lsa->lsa_tgts = osts;
2695 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2697 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2698 progname, template);
2701 rc = build_component(layout, lsa, *layout != NULL);
2703 /* clean clean lsa */
2704 setstripe_args_init(lsa);
2708 cYAML_free_tree(tree);
2713 * Get the extension size from the next (SEL) component and extend the
2714 * current component on it. The start of the next component is to be
2717 * \param[in] layout the current layout
2718 * \param[in] start the start of the current component
2719 * \param[in,out] end the end of the current component
2720 * \param[in] offset the offset to adjust the end position to instead of
2723 * \retval 0 - extended successfully
2724 * \retval < 0 - error
2726 static int layout_extend_comp(struct llapi_layout *layout,
2727 uint64_t start, uint64_t *end,
2730 uint64_t size, next_start, next_end;
2733 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2735 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2736 "%s\n", progname, strerror(errno));
2740 /* Even if the @size will not be used below, this will fail if
2741 * this is not a SEL component - a good confirmation we are
2742 * working on right components. */
2743 rc = llapi_layout_extension_size_get(layout, &size);
2745 fprintf(stderr, "%s setstripe: cannot get component ext size: "
2746 "%s\n", progname, strerror(errno));
2750 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2752 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2753 progname, strerror(errno));
2757 next_start += offset ?: size;
2758 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2760 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2761 progname, strerror(errno));
2765 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2767 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2768 "%s\n", progname, strerror(errno));
2772 *end += offset ?: size;
2773 rc = llapi_layout_comp_extent_set(layout, start, *end);
2775 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2776 progname, strerror(errno));
2784 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2785 * end of the last component in the existing file, and adjust the
2786 * first extent start of the components to be added accordingly.
2788 * In the create mode, we need to check if the first component is an extendable
2789 * SEL component and extend its length to the extension size (first component
2790 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2792 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2795 struct llapi_layout *head;
2796 uint64_t start = 0, prev_end = 0;
2805 head = llapi_layout_get_by_path(fname, 0);
2808 "%s setstripe: cannot read layout from '%s': "
2809 "%s\n", progname, fname, strerror(errno));
2811 } else if (errno == ENODATA) {
2812 /* file without LOVEA, this component-add will be turned
2813 * into a component-create. */
2814 llapi_layout_free(head);
2817 /* the new layout will be added to an empty one, it
2818 * still needs to be adjusted below */
2821 } else if (!llapi_layout_is_composite(head)) {
2822 fprintf(stderr, "%s setstripe: '%s' not a composite "
2823 "file\n", progname, fname);
2824 llapi_layout_free(head);
2828 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2830 fprintf(stderr, "%s setstripe: cannot get prev "
2831 "extent: %s\n", progname, strerror(errno));
2832 llapi_layout_free(head);
2836 llapi_layout_free(head);
2840 /* Make sure we use the first component of the layout to be added. */
2841 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2844 "%s setstripe: cannot move component cursor: %s\n",
2845 progname, strerror(errno));
2849 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2851 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2852 progname, strerror(errno));
2856 if (start == 0 && end == 0) {
2857 rc = layout_extend_comp(layout, start, &end,
2858 comp_add ? prev_end : 0);
2863 if (start > prev_end || end < prev_end) {
2864 fprintf(stderr, "%s setstripe: first extent [%lu, %lu) not "
2865 "adjacent with extent end %lu\n",
2866 progname, start, end, prev_end);
2870 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2872 fprintf(stderr, "%s setstripe: cannot set component extent "
2874 progname, prev_end, end, strerror(errno));
2881 static int mirror_adjust_first_extents(struct mirror_args *list)
2888 while (list != NULL) {
2889 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
2892 list = list->m_next;
2898 static inline bool arg_is_eof(char *arg)
2900 return !strncmp(arg, "-1", strlen("-1")) ||
2901 !strncmp(arg, "EOF", strlen("EOF")) ||
2902 !strncmp(arg, "eof", strlen("eof"));
2906 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2908 * Return: Valid mirror_args pointer on success and
2909 * NULL if memory allocation fails.
2911 static struct mirror_args *lfs_mirror_alloc(void)
2913 struct mirror_args *mirror = NULL;
2916 mirror = calloc(1, sizeof(*mirror));
2927 * lfs_mirror_free() - Free memory allocated for a mirror argument
2929 * @mirror: Previously allocated mirror argument structure by
2930 * lfs_mirror_alloc().
2932 * Free memory allocated for @mirror.
2936 static void lfs_mirror_free(struct mirror_args *mirror)
2938 if (mirror->m_layout != NULL)
2939 llapi_layout_free(mirror->m_layout);
2944 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2945 * @mirror_list: Previously allocated mirror list.
2947 * Free memory allocated for @mirror_list.
2951 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2953 struct mirror_args *next_mirror = NULL;
2955 while (mirror_list != NULL) {
2956 next_mirror = mirror_list->m_next;
2957 lfs_mirror_free(mirror_list);
2958 mirror_list = next_mirror;
2970 LFS_COMP_NO_VERIFY_OPT,
2972 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2974 LFS_MIRROR_STATE_OPT,
2976 LFS_MIRROR_INDEX_OPT,
2977 LFS_LAYOUT_FOREIGN_OPT,
2982 static int lfs_setstripe_internal(int argc, char **argv,
2983 enum setstripe_origin opc)
2985 struct lfs_setstripe_args lsa = { 0 };
2986 struct llapi_stripe_param *param = NULL;
2987 struct find_param migrate_mdt_param = {
2997 unsigned long long size_units = 1;
2998 bool migrate_mode = false;
2999 bool migrate_mdt_mode = false;
3000 bool setstripe_mode = false;
3001 bool migration_block = false;
3002 __u64 migration_flags = 0;
3003 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3004 int comp_del = 0, comp_set = 0;
3007 struct llapi_layout *layout = NULL;
3008 struct llapi_layout **lpp = &layout;
3009 bool mirror_mode = false;
3010 bool has_m_file = false;
3011 __u32 mirror_count = 0;
3012 enum mirror_flags mirror_flags = 0;
3013 struct mirror_args *mirror_list = NULL;
3014 struct mirror_args *new_mirror = NULL;
3015 struct mirror_args *last_mirror = NULL;
3016 __u16 mirror_id = 0;
3018 bool from_yaml = false;
3019 bool from_copy = false;
3020 char *template = NULL;
3021 bool foreign_mode = false;
3023 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3024 char *mode_opt = NULL;
3025 mode_t previous_umask = 0;
3026 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3028 struct option long_opts[] = {
3029 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3030 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3031 /* --block is only valid in migrate mode */
3032 { .val = 'b', .name = "block", .has_arg = no_argument },
3033 { .val = LFS_COMP_ADD_OPT,
3034 .name = "comp-add", .has_arg = no_argument },
3035 { .val = LFS_COMP_ADD_OPT,
3036 .name = "component-add", .has_arg = no_argument },
3037 { .val = LFS_COMP_DEL_OPT,
3038 .name = "comp-del", .has_arg = no_argument },
3039 { .val = LFS_COMP_DEL_OPT,
3040 .name = "component-del", .has_arg = no_argument },
3041 { .val = LFS_COMP_FLAGS_OPT,
3042 .name = "comp-flags", .has_arg = required_argument },
3043 { .val = LFS_COMP_FLAGS_OPT,
3044 .name = "component-flags",
3045 .has_arg = required_argument },
3046 { .val = LFS_COMP_SET_OPT,
3047 .name = "comp-set", .has_arg = no_argument },
3048 { .val = LFS_COMP_SET_OPT,
3049 .name = "component-set",
3050 .has_arg = no_argument},
3051 { .val = LFS_COMP_NO_VERIFY_OPT,
3052 .name = "no-verify", .has_arg = no_argument},
3053 { .val = LFS_LAYOUT_FLAGS_OPT,
3054 .name = "flags", .has_arg = required_argument},
3055 { .val = LFS_LAYOUT_FOREIGN_OPT,
3056 .name = "foreign", .has_arg = optional_argument},
3057 { .val = LFS_MIRROR_ID_OPT,
3058 .name = "mirror-id", .has_arg = required_argument},
3059 { .val = LFS_MODE_OPT,
3060 .name = "mode", .has_arg = required_argument},
3061 { .val = LFS_LAYOUT_COPY,
3062 .name = "copy", .has_arg = required_argument},
3063 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3064 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3065 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3066 { .val = 'C', .name = "overstripe-count",
3067 .has_arg = required_argument},
3068 { .val = 'd', .name = "delete", .has_arg = no_argument},
3069 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3070 /* --non-direct is only valid in migrate mode */
3071 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3072 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3073 { .val = 'E', .name = "component-end",
3074 .has_arg = required_argument},
3075 { .val = 'f', .name = "file", .has_arg = required_argument },
3076 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3077 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3078 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3079 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3080 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3081 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3082 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3083 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3084 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3085 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3086 { .val = 'L', .name = "layout", .has_arg = required_argument },
3087 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3088 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3089 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3090 /* --non-block is only valid in migrate mode */
3091 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3092 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3093 { .val = 'o', .name = "ost", .has_arg = required_argument },
3094 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3095 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3096 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3098 { .val = 'p', .name = "pool", .has_arg = required_argument },
3099 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3100 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3101 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3102 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3103 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3104 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3105 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3106 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3107 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3108 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3109 /* --verbose is only valid in migrate mode */
3110 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3111 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3112 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3113 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3114 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3117 setstripe_args_init(&lsa);
3119 migrate_mode = (opc == SO_MIGRATE);
3120 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3121 setstripe_mode = (opc == SO_SETSTRIPE);
3122 if (opc == SO_MIRROR_DELETE) {
3124 mirror_flags = MF_DESTROY;
3127 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3129 while ((c = getopt_long(argc, argv,
3130 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3131 long_opts, NULL)) >= 0) {
3137 case LFS_COMP_ADD_OPT:
3140 case LFS_COMP_DEL_OPT:
3143 case LFS_COMP_FLAGS_OPT:
3144 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3145 &lsa.lsa_comp_neg_flags);
3148 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3149 fprintf(stderr, "%s: inverted flags are not supported\n",
3154 case LFS_COMP_SET_OPT:
3157 case LFS_COMP_NO_VERIFY_OPT:
3158 mirror_flags |= MF_NO_VERIFY;
3160 case LFS_MIRROR_ID_OPT:
3161 mirror_id = strtoul(optarg, &end, 0);
3162 if (*end != '\0' || mirror_id == 0) {
3164 "%s %s: invalid mirror ID '%s'\n",
3165 progname, argv[0], optarg);
3169 case LFS_LAYOUT_FLAGS_OPT: {
3172 /* check for numeric flags (foreign and mirror cases) */
3173 if (setstripe_mode && !mirror_mode && !last_mirror) {
3174 flags = strtoul(optarg, &end, 16);
3177 "%s %s: bad flags '%s'\n",
3178 progname, argv[0], optarg);
3184 if (!mirror_mode || !last_mirror) {
3185 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
3190 result = comp_str2flags(optarg, &last_mirror->m_flags,
3196 fprintf(stderr, "%s: inverted flags are not supported\n",
3201 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3203 "%s: unsupported mirror flags: %s\n",
3210 case LFS_LAYOUT_FOREIGN_OPT:
3211 if (optarg != NULL) {
3212 /* check pure numeric */
3213 type = strtoul(optarg, &end, 0);
3216 type = check_foreign_type_name(optarg);
3217 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3219 "%s %s: unrecognized foreign type '%s'\n",
3226 foreign_mode = true;
3230 if (mode_opt != NULL) {
3231 mode = strtoul(mode_opt, &end, 8);
3234 "%s %s: bad mode '%s'\n",
3235 progname, argv[0], mode_opt);
3238 previous_umask = umask(0);
3241 case LFS_LAYOUT_COPY:
3246 if (!migrate_mode) {
3248 "%s %s: -b|--block valid only for migrate command\n",
3252 migration_block = true;
3255 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3258 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3261 "%s %s: invalid stripe count '%s'\n",
3262 progname, argv[0], optarg);
3266 if (lsa.lsa_stripe_count == -1)
3267 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3270 /* delete the default striping pattern */
3272 if (opc == SO_MIRROR_SPLIT) {
3275 "%s %s: -d cannot used with -f\n",
3279 mirror_flags |= MF_DESTROY;
3283 if (!migrate_mode) {
3285 "%s %s: -D|--non-direct is valid "
3286 "only for migrate command\n",
3290 migration_flags |= MIGRATION_NONDIRECT;
3293 if (lsa.lsa_comp_end != 0) {
3294 result = comp_args_to_layout(lpp, &lsa, true);
3296 fprintf(stderr, "%s: invalid layout\n",
3301 setstripe_args_init_inherit(&lsa);
3304 if (arg_is_eof(optarg)) {
3305 lsa.lsa_comp_end = LUSTRE_EOF;
3307 result = llapi_parse_size(optarg,
3312 "%s %s: invalid component end '%s'\n",
3313 progname, argv[0], optarg);
3319 if (!migrate_mode) {
3320 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3324 lsa.lsa_pattern = check_hashtype(optarg);
3325 if (lsa.lsa_pattern == 0) {
3327 "%s %s: bad stripe hash type '%s'\n",
3328 progname, argv[0], optarg);
3333 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3336 "%s %s: invalid stripe offset '%s'\n",
3337 progname, argv[0], optarg);
3340 if (lsa.lsa_stripe_off == -1)
3341 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3344 comp_id = strtoul(optarg, &end, 0);
3345 if (*end != '\0' || comp_id == 0 ||
3346 comp_id > LCME_ID_MAX) {
3348 "%s %s: invalid component ID '%s'\n",
3349 progname, argv[0], optarg);
3354 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3356 "error: %s: invalid option: %s\n",
3357 progname, argv[optopt + 1]);
3360 if (opc == SO_MIRROR_EXTEND) {
3361 if (last_mirror == NULL) {
3363 "error: %s: '-N' must exist in front of '%s'\n",
3364 progname, argv[optopt + 1]);
3367 last_mirror->m_file = optarg;
3368 last_mirror->m_count = 1;
3371 if (mirror_list == NULL)
3372 mirror_list = lfs_mirror_alloc();
3373 mirror_list->m_file = optarg;
3378 if (strcmp(argv[optind - 1], "mdt") == 0) {
3379 /* Can be only the first component */
3380 if (layout != NULL) {
3382 fprintf(stderr, "error: 'mdt' layout "
3383 "can be only the first one\n");
3386 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3388 fprintf(stderr, "error: 'mdt' layout "
3389 "size is too big\n");
3392 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3393 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3395 fprintf(stderr, "error: layout '%s' is "
3396 "unknown, supported layouts are: "
3397 "'mdt', 'raid0'\n", argv[optind]);
3402 if (!migrate_mode) {
3404 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3408 migrate_mdt_mode = true;
3409 lsa.lsa_nr_tgts = parse_targets(tgts,
3410 sizeof(tgts) / sizeof(__u32),
3411 lsa.lsa_nr_tgts, optarg, NULL);
3412 if (lsa.lsa_nr_tgts < 0) {
3414 "%s %s: invalid MDT target(s) '%s'\n",
3415 progname, argv[0], optarg);
3419 lsa.lsa_tgts = tgts;
3420 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3421 lsa.lsa_stripe_off = tgts[0];
3424 if (!migrate_mode) {
3426 "%s %s: -n|--non-block valid only for migrate command\n",
3430 migration_flags |= MIGRATION_NONBLOCK;
3433 if (opc == SO_SETSTRIPE) {
3434 opc = SO_MIRROR_CREATE;
3438 if (optarg != NULL) {
3439 mirror_count = strtoul(optarg, &end, 0);
3440 if (*end != '\0' || mirror_count == 0) {
3442 "error: %s: bad mirror count: %s\n",
3449 new_mirror = lfs_mirror_alloc();
3450 new_mirror->m_count = mirror_count;
3452 if (mirror_list == NULL)
3453 mirror_list = new_mirror;
3455 if (last_mirror != NULL) {
3456 /* wrap up last mirror */
3457 if (lsa.lsa_comp_end == 0)
3458 lsa.lsa_comp_end = LUSTRE_EOF;
3460 result = comp_args_to_layout(lpp, &lsa, true);
3462 lfs_mirror_free(new_mirror);
3466 setstripe_args_init_inherit(&lsa);
3468 last_mirror->m_next = new_mirror;
3471 last_mirror = new_mirror;
3472 lpp = &last_mirror->m_layout;
3475 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3476 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3477 fprintf(stderr, "warning: '--ost-list' is "
3478 "deprecated, use '--ost' instead\n");
3480 /* -o allows overstriping, and must note it because
3481 * parse_targets is shared with MDT striping, which
3482 * does not allow duplicates
3484 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3485 lsa.lsa_nr_tgts = parse_targets(tgts,
3486 sizeof(tgts) / sizeof(__u32),
3487 lsa.lsa_nr_tgts, optarg,
3489 if (lsa.lsa_nr_tgts < 0) {
3491 "%s %s: invalid OST target(s) '%s'\n",
3492 progname, argv[0], optarg);
3496 lsa.lsa_tgts = tgts;
3497 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3498 lsa.lsa_stripe_off = tgts[0];
3503 lsa.lsa_pool_name = optarg;
3505 if (strlen(lsa.lsa_pool_name) == 0 ||
3506 strncmp(lsa.lsa_pool_name, "none",
3507 LOV_MAXPOOLNAME) == 0)
3508 lsa.lsa_pool_name = NULL;
3511 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3515 "%s %s: invalid stripe size '%s'\n",
3516 progname, argv[0], optarg);
3521 if (!migrate_mode) {
3523 "%s %s: -v|--verbose valid only for migrate command\n",
3527 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3528 migration_flags = MIGRATION_VERBOSE;
3538 result = llapi_parse_size(optarg,
3539 &lsa.lsa_extension_size,
3543 "%s %s: invalid extension size '%s'\n",
3544 progname, argv[0], optarg);
3548 lsa.lsa_extension_comp = true;
3551 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3552 progname, argv[0], argv[optind - 1]);
3557 fname = argv[optind];
3559 if (optind == argc) {
3560 fprintf(stderr, "%s %s: FILE must be specified\n",
3565 if (xattr && !foreign_mode) {
3566 /* only print a warning as this is harmless and will be ignored
3569 "%s %s: xattr has been specified for non-foreign layout\n",
3571 } else if (foreign_mode && !xattr) {
3573 "%s %s: xattr must be provided in foreign mode\n",
3578 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3579 comp_set || comp_id || delete || from_copy ||
3580 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3583 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3588 if (mirror_mode && mirror_count == 0) {
3590 "error: %s: --mirror-count|-N option is required\n",
3597 if (lsa.lsa_comp_end == 0)
3598 lsa.lsa_comp_end = LUSTRE_EOF;
3601 if (lsa.lsa_comp_end != 0) {
3602 result = comp_args_to_layout(lpp, &lsa, true);
3604 fprintf(stderr, "error: %s: invalid layout\n",
3611 if (mirror_flags & MF_NO_VERIFY) {
3612 if (opc != SO_MIRROR_EXTEND) {
3614 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3618 } else if (!has_m_file) {
3620 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3627 if (comp_set && !comp_id) {
3628 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3633 if ((delete + comp_set + comp_del + comp_add) > 1) {
3635 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3640 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3641 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3643 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3648 if ((comp_set || comp_del) &&
3649 (setstripe_args_specified(&lsa) || layout != NULL)) {
3651 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3656 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3658 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3663 if (comp_add || comp_del) {
3666 result = lstat(fname, &st);
3667 if (result == 0 && S_ISDIR(st.st_mode)) {
3669 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3675 fprintf(stderr, "error: %s: can't use --component-add "
3676 "or --component-del for mirror operation\n",
3683 if (layout == NULL) {
3685 "%s %s: option -E must be specified with --component-add\n",
3691 if (layout != NULL || mirror_list != NULL) {
3693 result = mirror_adjust_first_extents(mirror_list);
3695 result = layout_adjust_first_extent(fname, layout,
3697 if (result == -ENODATA)
3699 else if (result != 0) {
3700 fprintf(stderr, "error: %s: invalid layout\n",
3706 if (from_yaml && from_copy) {
3708 "%s: can't specify --yaml and --copy together\n",
3713 if ((from_yaml || from_copy) &&
3714 (setstripe_args_specified(&lsa) || layout != NULL)) {
3715 fprintf(stderr, "error: %s: can't specify --yaml with "
3716 "-c, -S, -i, -o, -p or -E options.\n",
3721 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3723 "%s %s: options --non-block and --block are mutually exclusive\n",
3728 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3729 opc != SO_MIRROR_DELETE && comp_id != 0) {
3731 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3736 if (migrate_mdt_mode) {
3737 struct lmv_user_md *lmu;
3739 /* initialize migrate mdt parameters */
3740 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3741 LMV_USER_MAGIC_SPECIFIC));
3744 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3745 progname, argv[0], strerror(ENOMEM));
3749 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3750 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3751 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3753 "%s %s: migrate should specify MDT index\n",
3758 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3759 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3760 lmu->lum_hash_type = lsa.lsa_pattern;
3762 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3763 if (lsa.lsa_pool_name) {
3764 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3765 sizeof(lmu->lum_pool_name) - 1);
3766 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3768 if (lsa.lsa_nr_tgts > 1) {
3771 if (lsa.lsa_stripe_count > 0 &&
3772 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3773 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3775 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3776 progname, lsa.lsa_stripe_count,
3782 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3783 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3784 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3785 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3787 lmu->lum_magic = LMV_USER_MAGIC;
3790 migrate_mdt_param.fp_lmv_md = lmu;
3791 migrate_mdt_param.fp_migrate = 1;
3792 } else if (layout == NULL) {
3793 /* initialize stripe parameters */
3794 param = calloc(1, offsetof(typeof(*param),
3795 lsp_osts[lsa.lsa_nr_tgts]));
3796 if (param == NULL) {
3798 "%s %s: cannot allocate memory for parameters: %s\n",
3799 progname, argv[0], strerror(ENOMEM));
3804 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3805 param->lsp_stripe_size = lsa.lsa_stripe_size;
3806 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3807 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3808 param->lsp_stripe_count = -1;
3810 param->lsp_stripe_count = lsa.lsa_stripe_count;
3812 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3813 param->lsp_stripe_offset = -1;
3815 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3816 param->lsp_stripe_pattern =
3817 llapi_pattern_to_lov(lsa.lsa_pattern);
3818 if (param->lsp_stripe_pattern == EINVAL) {
3819 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3824 param->lsp_pool = lsa.lsa_pool_name;
3825 param->lsp_is_specific = false;
3826 if (lsa.lsa_nr_tgts > 0) {
3827 if (lsa.lsa_stripe_count > 0 &&
3828 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3829 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3830 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3832 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3833 argv[0], lsa.lsa_stripe_count,
3839 param->lsp_is_specific = true;
3840 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3841 memcpy(param->lsp_osts, tgts,
3842 sizeof(*tgts) * lsa.lsa_nr_tgts);
3847 /* generate a layout from a YAML template */
3848 result = lfs_comp_create_from_yaml(template, &layout,
3851 fprintf(stderr, "error: %s: can't create composite "
3852 "layout from template file %s\n",
3856 } else if (from_copy) {
3857 layout = llapi_layout_get_by_path(template, 0);
3858 if (layout == NULL) {
3860 "%s: can't create composite layout from file %s.\n",
3861 progname, template);
3866 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3867 if (migrate_mdt_mode) {
3868 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3869 } else if (migrate_mode) {
3870 result = lfs_migrate(fname, migration_flags, param,
3872 } else if (comp_set != 0) {
3873 result = lfs_component_set(fname, comp_id,
3875 lsa.lsa_comp_neg_flags);
3876 } else if (comp_del != 0) {
3877 result = lfs_component_del(fname, comp_id,
3879 lsa.lsa_comp_neg_flags);
3880 } else if (comp_add != 0) {
3881 result = lfs_component_add(fname, layout);
3882 } else if (opc == SO_MIRROR_CREATE) {
3883 result = mirror_create(fname, mirror_list);
3884 } else if (opc == SO_MIRROR_EXTEND) {
3885 result = mirror_extend(fname, mirror_list,
3887 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
3888 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3890 "%s: no mirror specified to delete from '%s'\n",
3894 if (lsa.lsa_pool_name)
3895 mirror_flags |= MF_COMP_POOL;
3896 else if (mirror_id != 0)
3897 comp_id = mirror_id;
3899 mirror_flags |= MF_COMP_ID;
3900 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3902 has_m_file ? mirror_list->m_file :
3904 } else if (layout != NULL) {
3905 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3911 } else if (foreign_mode) {
3912 result = llapi_file_create_foreign(fname, mode, type,
3919 result = llapi_file_open_param(fname,
3928 /* Save the first error encountered. */
3935 if (mode_opt != NULL)
3936 umask(previous_umask);
3939 free(migrate_mdt_param.fp_lmv_md);
3940 llapi_layout_free(layout);
3941 lfs_mirror_list_free(mirror_list);
3946 llapi_layout_free(layout);
3947 lfs_mirror_list_free(mirror_list);
3951 static int lfs_poollist(int argc, char **argv)
3956 return llapi_poollist(argv[1]);
3959 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3964 char *endptr = "AD";
3969 else if (str[0] == '-')
3975 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3976 long long val = strtoll(timebuf, &endptr, 0);
3981 unit *= 52; /* 52 weeks + 1 day below */
3982 case 'w': /* fallthrough */
3984 case '\0': /* days are default unit if none used */
3985 case 'd': /* fallthrough */
3986 unit = (unit + (*endptr == 'y')) * 24;
3987 case 'h': /* fallthrough */
3989 case 'm': /* fallthrough */
3991 case 's': /* fallthrough */
3993 /* don't need to multiply by 1 for seconds */
3996 "%s find: bad time string '%s': %s\n",
3997 progname, timebuf, strerror(EINVAL));
4001 if (param->fp_time_margin == 0 ||
4002 (*endptr && unit < param->fp_time_margin))
4003 param->fp_time_margin = unit;
4010 fprintf(stderr, "%s find: bad time '%s': too large\n",
4020 static int name2uid(unsigned int *id, const char *name)
4022 struct passwd *passwd;
4024 passwd = getpwnam(name);
4027 *id = passwd->pw_uid;
4032 static int name2gid(unsigned int *id, const char *name)
4034 struct group *group;
4036 group = getgrnam(name);
4039 *id = group->gr_gid;
4044 static inline int name2projid(unsigned int *id, const char *name)
4049 static int uid2name(char **name, unsigned int id)
4051 struct passwd *passwd;
4053 passwd = getpwuid(id);
4056 *name = passwd->pw_name;
4061 static inline int gid2name(char **name, unsigned int id)
4063 struct group *group;
4065 group = getgrgid(id);
4068 *name = group->gr_name;
4073 static int name2layout(__u32 *layout, char *name)
4075 char *ptr, *layout_name;
4078 for (ptr = name; ; ptr = NULL) {
4079 layout_name = strtok(ptr, ",");
4080 if (layout_name == NULL)
4082 if (strcmp(layout_name, "released") == 0)
4083 *layout |= LOV_PATTERN_F_RELEASED;
4084 else if (strcmp(layout_name, "raid0") == 0)
4085 *layout |= LOV_PATTERN_RAID0;
4086 else if (strcmp(layout_name, "mdt") == 0)
4087 *layout |= LOV_PATTERN_MDT;
4088 else if (strcmp(layout_name, "overstriping") == 0)
4089 *layout |= LOV_PATTERN_OVERSTRIPING;
4096 static int lfs_find(int argc, char **argv)
4101 struct find_param param = {
4104 .fp_time_margin = 24 * 60 * 60,
4106 struct option long_opts[] = {
4107 { .val = 'A', .name = "atime", .has_arg = required_argument },
4108 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4109 { .val = LFS_COMP_COUNT_OPT,
4110 .name = "comp-count", .has_arg = required_argument },
4111 { .val = LFS_COMP_COUNT_OPT,
4112 .name = "component-count",
4113 .has_arg = required_argument },
4114 { .val = LFS_COMP_FLAGS_OPT,
4115 .name = "comp-flags", .has_arg = required_argument },
4116 { .val = LFS_COMP_FLAGS_OPT,
4117 .name = "component-flags",
4118 .has_arg = required_argument },
4119 { .val = LFS_COMP_START_OPT,
4120 .name = "comp-start", .has_arg = required_argument },
4121 { .val = LFS_COMP_START_OPT,
4122 .name = "component-start",
4123 .has_arg = required_argument },
4124 { .val = LFS_MIRROR_STATE_OPT,
4125 .name = "mirror-state", .has_arg = required_argument },
4126 { .val = LFS_LAYOUT_FOREIGN_OPT,
4127 .name = "foreign", .has_arg = optional_argument},
4128 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4129 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4130 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4131 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4132 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4133 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4134 { .val = 'E', .name = "component-end",
4135 .has_arg = required_argument },
4136 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4137 { .val = LFS_LAYOUT_FOREIGN_OPT,
4138 .name = "foreign", .has_arg = optional_argument},
4139 { .val = 'g', .name = "gid", .has_arg = required_argument },
4140 { .val = 'G', .name = "group", .has_arg = required_argument },
4141 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4142 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4143 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4144 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4145 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4146 { .val = 'L', .name = "layout", .has_arg = required_argument },
4147 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4148 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4149 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4150 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4151 { .val = 'n', .name = "name", .has_arg = required_argument },
4152 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4153 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4154 { .val = 'O', .name = "obd", .has_arg = required_argument },
4155 { .val = 'O', .name = "ost", .has_arg = required_argument },
4156 /* no short option for pool yet, can be 'p' after 2.18 */
4157 { .val = LFS_POOL_OPT,
4158 .name = "pool", .has_arg = required_argument },
4159 { .val = '0', .name = "print0", .has_arg = no_argument },
4160 { .val = 'P', .name = "print", .has_arg = no_argument },
4161 { .val = LFS_PROJID_OPT,
4162 .name = "projid", .has_arg = required_argument },
4163 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4164 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4165 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4166 { .val = 's', .name = "size", .has_arg = required_argument },
4167 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4168 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4169 { .val = 't', .name = "type", .has_arg = required_argument },
4170 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4171 { .val = 'u', .name = "uid", .has_arg = required_argument },
4172 { .val = 'U', .name = "user", .has_arg = required_argument },
4173 { .val = 'z', .name = "extension-size",
4174 .has_arg = required_argument },
4175 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4176 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4177 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4190 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4191 while ((c = getopt_long_only(argc, argv,
4192 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4193 long_opts, NULL)) >= 0) {
4198 /* '!' is part of option */
4199 /* when getopt_long_only() finds a string which is not
4200 * an option nor a known option argument it returns 1
4201 * in that case if we already have found pathstart and pathend
4202 * (i.e. we have the list of pathnames),
4203 * the only supported value is "!"
4205 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4206 if (!isoption && pathend != -1) {
4207 fprintf(stderr, "err: %s: filename|dirname must either "
4208 "precede options or follow options\n",
4213 if (!isoption && pathstart == -1)
4214 pathstart = optind - 1;
4215 if (isoption && pathstart != -1 && pathend == -1)
4216 pathend = optind - 2;
4222 /* unknown; opt is "!" or path component,
4223 * checking done above.
4225 if (strcmp(optarg, "!") == 0)
4229 xtime = ¶m.fp_atime;
4230 xsign = ¶m.fp_asign;
4231 param.fp_exclude_atime = !!neg_opt;
4232 /* no break, this falls through to 'C' for ctime */
4235 xtime = ¶m.fp_ctime;
4236 xsign = ¶m.fp_csign;
4237 param.fp_exclude_ctime = !!neg_opt;
4239 /* no break, this falls through to 'M' for mtime */
4242 xtime = ¶m.fp_mtime;
4243 xsign = ¶m.fp_msign;
4244 param.fp_exclude_mtime = !!neg_opt;
4246 rc = set_time(¶m, &t, xtime, optarg);
4247 if (rc == LONG_MAX) {
4255 if (optarg[0] == '+') {
4256 param.fp_blocks_sign = -1;
4258 } else if (optarg[0] == '-') {
4259 param.fp_blocks_sign = 1;
4263 param.fp_blocks_units = 1024;
4264 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4265 ¶m.fp_blocks_units, 0);
4267 fprintf(stderr, "error: bad blocks '%s'\n",
4271 param.fp_check_blocks = 1;
4272 param.fp_exclude_blocks = !!neg_opt;
4274 case LFS_COMP_COUNT_OPT:
4275 if (optarg[0] == '+') {
4276 param.fp_comp_count_sign = -1;
4278 } else if (optarg[0] == '-') {
4279 param.fp_comp_count_sign = 1;
4283 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4284 if (*endptr != '\0') {
4285 fprintf(stderr, "error: bad component count "
4289 param.fp_check_comp_count = 1;
4290 param.fp_exclude_comp_count = !!neg_opt;
4292 case LFS_COMP_FLAGS_OPT:
4293 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4294 ¶m.fp_comp_neg_flags);
4296 fprintf(stderr, "error: bad component flags "
4300 param.fp_check_comp_flags = 1;
4302 __u32 flags = param.fp_comp_neg_flags;
4303 param.fp_comp_neg_flags = param.fp_comp_flags;
4304 param.fp_comp_flags = flags;
4307 case LFS_COMP_START_OPT:
4308 if (optarg[0] == '+') {
4309 param.fp_comp_start_sign = -1;
4311 } else if (optarg[0] == '-') {
4312 param.fp_comp_start_sign = 1;
4316 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4317 ¶m.fp_comp_start_units, 0);
4319 fprintf(stderr, "error: bad component start "
4323 param.fp_check_comp_start = 1;
4324 param.fp_exclude_comp_start = !!neg_opt;
4326 case LFS_MIRROR_STATE_OPT:
4327 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4328 ¶m.fp_mirror_neg_state);
4331 "error: bad mirrored file state '%s'\n",
4335 param.fp_check_mirror_state = 1;
4337 __u16 state = param.fp_mirror_neg_state;
4338 param.fp_mirror_neg_state =
4339 param.fp_mirror_state;
4340 param.fp_mirror_state = state;
4344 if (optarg[0] == '+') {
4345 param.fp_stripe_count_sign = -1;
4347 } else if (optarg[0] == '-') {
4348 param.fp_stripe_count_sign = 1;
4352 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4353 if (*endptr != '\0') {
4354 fprintf(stderr,"error: bad stripe_count '%s'\n",
4359 param.fp_check_stripe_count = 1;
4360 param.fp_exclude_stripe_count = !!neg_opt;
4363 param.fp_max_depth = strtol(optarg, 0, 0);
4366 if (optarg[0] == '+') {
4367 param.fp_comp_end_sign = -1;
4369 } else if (optarg[0] == '-') {
4370 param.fp_comp_end_sign = 1;
4374 if (arg_is_eof(optarg)) {
4375 param.fp_comp_end = LUSTRE_EOF;
4376 param.fp_comp_end_units = 1;
4379 rc = llapi_parse_size(optarg,
4381 ¶m.fp_comp_end_units, 0);
4384 fprintf(stderr, "error: bad component end "
4388 param.fp_check_comp_end = 1;
4389 param.fp_exclude_comp_end = !!neg_opt;
4391 case LFS_LAYOUT_FOREIGN_OPT: {
4392 /* all types by default */
4393 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4395 if (optarg != NULL) {
4396 /* check pure numeric */
4397 type = strtoul(optarg, &endptr, 0);
4400 type = check_foreign_type_name(optarg);
4401 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4403 "%s %s: unknown foreign type '%s'\n",
4410 param.fp_foreign_type = type;
4411 param.fp_check_foreign = 1;
4412 param.fp_exclude_foreign = !!neg_opt;
4417 rc = name2gid(¶m.fp_gid, optarg);
4419 param.fp_gid = strtoul(optarg, &endptr, 10);
4420 if (*endptr != '\0') {
4421 fprintf(stderr, "Group/GID: %s cannot "
4422 "be found.\n", optarg);
4427 param.fp_exclude_gid = !!neg_opt;
4428 param.fp_check_gid = 1;
4431 param.fp_hash_type = check_hashtype(optarg);
4432 if (param.fp_hash_type == 0) {
4433 fprintf(stderr, "error: bad hash_type '%s'\n",
4438 param.fp_check_hash_type = 1;
4439 param.fp_exclude_hash_type = !!neg_opt;
4445 ret = name2layout(¶m.fp_layout, optarg);
4448 param.fp_exclude_layout = !!neg_opt;
4449 param.fp_check_layout = 1;
4453 rc = name2uid(¶m.fp_uid, optarg);
4455 param.fp_uid = strtoul(optarg, &endptr, 10);
4456 if (*endptr != '\0') {
4457 fprintf(stderr, "User/UID: %s cannot "
4458 "be found.\n", optarg);
4463 param.fp_exclude_uid = !!neg_opt;
4464 param.fp_check_uid = 1;
4467 param.fp_pattern = (char *)optarg;
4468 param.fp_exclude_pattern = !!neg_opt;
4471 if (optarg[0] == '+') {
4472 param.fp_mirror_count_sign = -1;
4474 } else if (optarg[0] == '-') {
4475 param.fp_mirror_count_sign = 1;
4479 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4480 if (*endptr != '\0') {
4482 "error: bad mirror count '%s'\n",
4486 param.fp_check_mirror_count = 1;
4487 param.fp_exclude_mirror_count = !!neg_opt;
4492 char *buf, *token, *next, *p;
4496 buf = strdup(optarg);
4502 param.fp_exclude_obd = !!neg_opt;
4505 while (token && *token) {
4506 token = strchr(token, ',');
4513 param.fp_exclude_mdt = !!neg_opt;
4514 param.fp_num_alloc_mdts += len;
4515 tmp = realloc(param.fp_mdt_uuid,
4516 param.fp_num_alloc_mdts *
4517 sizeof(*param.fp_mdt_uuid));
4523 param.fp_mdt_uuid = tmp;
4525 param.fp_exclude_obd = !!neg_opt;
4526 param.fp_num_alloc_obds += len;
4527 tmp = realloc(param.fp_obd_uuid,
4528 param.fp_num_alloc_obds *
4529 sizeof(*param.fp_obd_uuid));
4535 param.fp_obd_uuid = tmp;
4537 for (token = buf; token && *token; token = next) {
4538 struct obd_uuid *puuid;
4541 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4544 ¶m.fp_obd_uuid[param.fp_num_obds++];
4546 p = strchr(token, ',');
4553 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4558 strncpy(puuid->uuid, token,
4559 sizeof(puuid->uuid));
4566 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4570 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4572 "Pool name %s is too long (max %d)\n",
4573 optarg, LOV_MAXPOOLNAME);
4578 * We do check for empty pool because empty pool
4579 * is used to find V1 LOV attributes
4581 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4582 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4583 param.fp_exclude_pool = !!neg_opt;
4584 param.fp_check_pool = 1;
4586 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4587 case 'p': /* want this for --pool, to match getstripe/find */
4589 "warning: -p deprecated, use --print0 or -0\n");
4592 param.fp_zero_end = 1;
4594 case 'P': /* we always print, this option is a no-op */
4596 case LFS_PROJID_OPT:
4597 rc = name2projid(¶m.fp_projid, optarg);
4599 param.fp_projid = strtoul(optarg, &endptr, 10);
4600 if (*endptr != '\0') {
4602 "Invalid project ID: %s",
4608 param.fp_exclude_projid = !!neg_opt;
4609 param.fp_check_projid = 1;
4612 if (optarg[0] == '+') {
4613 param.fp_size_sign = -1;
4615 } else if (optarg[0] == '-') {
4616 param.fp_size_sign = 1;
4620 ret = llapi_parse_size(optarg, ¶m.fp_size,
4621 ¶m.fp_size_units, 0);
4623 fprintf(stderr, "error: bad file size '%s'\n",
4627 param.fp_check_size = 1;
4628 param.fp_exclude_size = !!neg_opt;
4631 if (optarg[0] == '+') {
4632 param.fp_stripe_size_sign = -1;
4634 } else if (optarg[0] == '-') {
4635 param.fp_stripe_size_sign = 1;
4639 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4640 ¶m.fp_stripe_size_units, 0);
4642 fprintf(stderr, "error: bad stripe_size '%s'\n",
4646 param.fp_check_stripe_size = 1;
4647 param.fp_exclude_stripe_size = !!neg_opt;
4650 param.fp_exclude_type = !!neg_opt;
4651 switch (optarg[0]) {
4653 param.fp_type = S_IFBLK;
4656 param.fp_type = S_IFCHR;
4659 param.fp_type = S_IFDIR;
4662 param.fp_type = S_IFREG;
4665 param.fp_type = S_IFLNK;
4668 param.fp_type = S_IFIFO;
4671 param.fp_type = S_IFSOCK;
4674 fprintf(stderr, "error: %s: bad type '%s'\n",
4681 if (optarg[0] == '+') {
4682 param.fp_mdt_count_sign = -1;
4684 } else if (optarg[0] == '-') {
4685 param.fp_mdt_count_sign = 1;
4689 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4690 if (*endptr != '\0') {
4691 fprintf(stderr, "error: bad mdt_count '%s'\n",
4696 param.fp_check_mdt_count = 1;
4697 param.fp_exclude_mdt_count = !!neg_opt;
4700 if (optarg[0] == '+') {
4701 param.fp_ext_size_sign = -1;
4703 } else if (optarg[0] == '-') {
4704 param.fp_ext_size_sign = 1;
4708 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4709 ¶m.fp_ext_size_units, 0);
4711 fprintf(stderr, "error: bad ext-size '%s'\n",
4715 param.fp_ext_size /= SEL_UNIT_SIZE;
4716 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4717 param.fp_check_ext_size = 1;
4718 param.fp_exclude_ext_size = !!neg_opt;
4726 if (pathstart == -1) {
4727 fprintf(stderr, "error: %s: no filename|pathname\n",
4731 } else if (pathend == -1) {
4737 rc = llapi_find(argv[pathstart], ¶m);
4740 pathbad = pathstart;
4742 } while (++pathstart < pathend);
4745 fprintf(stderr, "%s: failed for '%s': %s\n",
4746 progname, argv[pathbad], strerror(-rc));
4749 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4750 free(param.fp_obd_uuid);
4752 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4753 free(param.fp_mdt_uuid);
4758 static int lfs_getstripe_internal(int argc, char **argv,
4759 struct find_param *param)
4761 struct option long_opts[] = {
4762 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4763 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4764 { .val = LFS_COMP_COUNT_OPT,
4765 .name = "comp-count", .has_arg = no_argument },
4766 { .val = LFS_COMP_COUNT_OPT,
4767 .name = "component-count", .has_arg = no_argument },
4768 { .val = LFS_COMP_FLAGS_OPT,
4769 .name = "comp-flags", .has_arg = optional_argument },
4770 { .val = LFS_COMP_FLAGS_OPT,
4771 .name = "component-flags", .has_arg = optional_argument },
4772 { .val = LFS_COMP_START_OPT,
4773 .name = "comp-start", .has_arg = optional_argument },
4774 { .val = LFS_COMP_START_OPT,
4775 .name = "component-start", .has_arg = optional_argument },
4776 { .val = LFS_MIRROR_INDEX_OPT,
4777 .name = "mirror-index", .has_arg = required_argument },
4778 { .val = LFS_MIRROR_ID_OPT,
4779 .name = "mirror-id", .has_arg = required_argument },
4780 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4781 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4782 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4783 { .val = 'd', .name = "directory", .has_arg = no_argument },
4784 { .val = 'D', .name = "default", .has_arg = no_argument },
4785 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4786 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4787 { .val = 'F', .name = "fid", .has_arg = no_argument },
4788 { .val = 'g', .name = "generation", .has_arg = no_argument },
4789 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4790 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4791 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4792 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4793 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4794 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4795 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
4796 { .val = 'L', .name = "layout", .has_arg = no_argument },
4797 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4798 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4799 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4800 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4801 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4802 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4803 { .val = 'O', .name = "obd", .has_arg = required_argument },
4804 { .val = 'O', .name = "ost", .has_arg = required_argument },
4805 { .val = 'p', .name = "pool", .has_arg = no_argument },
4806 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4807 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4808 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4809 { .val = 'R', .name = "raw", .has_arg = no_argument },
4810 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4811 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4812 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4813 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4814 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4815 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4816 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4817 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4818 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
4819 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
4823 int pathstart = -1, pathend = -1;
4827 while ((c = getopt_long(argc, argv,
4828 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
4829 long_opts, NULL)) != -1) {
4833 /* '!' is part of option */
4834 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4835 if (!isoption && pathend != -1) {
4837 "error: %s: filename|dirname must either precede options or follow options\n",
4841 if (!isoption && pathstart == -1)
4842 pathstart = optind - 1;
4843 if (isoption && pathstart != -1 && pathend == -1)
4844 pathend = optind - 2;
4848 /* unknown: opt is "!" */
4849 if (strcmp(optarg, "!") == 0)
4853 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4854 param->fp_verbose |= VERBOSE_COUNT;
4855 param->fp_max_depth = 0;
4858 case LFS_COMP_COUNT_OPT:
4859 param->fp_verbose |= VERBOSE_COMP_COUNT;
4860 param->fp_max_depth = 0;
4862 case LFS_COMP_FLAGS_OPT:
4863 if (optarg != NULL) {
4864 rc = comp_str2flags(optarg,
4865 ¶m->fp_comp_flags,
4866 ¶m->fp_comp_neg_flags);
4868 fprintf(stderr, "error: %s bad "
4869 "component flags '%s'.\n",
4873 param->fp_check_comp_flags = 1;
4875 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4876 param->fp_max_depth = 0;
4879 case LFS_COMP_START_OPT:
4880 if (optarg != NULL) {
4882 if (tmp[0] == '+') {
4883 param->fp_comp_start_sign = -1;
4885 } else if (tmp[0] == '-') {
4886 param->fp_comp_start_sign = 1;
4889 rc = llapi_parse_size(tmp,
4890 ¶m->fp_comp_start,
4891 ¶m->fp_comp_start_units, 0);
4893 fprintf(stderr, "error: %s bad "
4894 "component start '%s'.\n",
4898 param->fp_check_comp_start = 1;
4901 param->fp_verbose |= VERBOSE_COMP_START;
4902 param->fp_max_depth = 0;
4905 case LFS_MIRROR_INDEX_OPT:
4906 if (optarg[0] == '+') {
4907 param->fp_mirror_index_sign = -1;
4909 } else if (optarg[0] == '-') {
4910 param->fp_mirror_index_sign = 1;
4914 param->fp_mirror_index = strtoul(optarg, &end, 0);
4915 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4916 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4918 "%s %s: invalid mirror index '%s'\n",
4919 progname, argv[0], optarg);
4922 if (param->fp_mirror_id != 0) {
4924 "%s %s: can't specify both mirror index and mirror ID\n",
4928 param->fp_check_mirror_index = 1;
4929 param->fp_exclude_mirror_index = !!neg_opt;
4931 case LFS_MIRROR_ID_OPT:
4932 if (optarg[0] == '+') {
4933 param->fp_mirror_id_sign = -1;
4935 } else if (optarg[0] == '-') {
4936 param->fp_mirror_id_sign = 1;
4940 param->fp_mirror_id = strtoul(optarg, &end, 0);
4941 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4942 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4944 "%s %s: invalid mirror ID '%s'\n",
4945 progname, argv[0], optarg);
4948 if (param->fp_mirror_index != 0) {
4950 "%s %s: can't specify both mirror index and mirror ID\n",
4954 param->fp_check_mirror_id = 1;
4955 param->fp_exclude_mirror_id = !!neg_opt;
4958 param->fp_max_depth = 0;
4961 param->fp_get_default_lmv = 1;
4964 if (optarg != NULL) {
4966 if (tmp[0] == '+') {
4967 param->fp_comp_end_sign = -1;
4969 } else if (tmp[0] == '-') {
4970 param->fp_comp_end_sign = 1;
4974 if (arg_is_eof(tmp)) {
4975 param->fp_comp_end = LUSTRE_EOF;
4976 param->fp_comp_end_units = 1;
4979 rc = llapi_parse_size(tmp,
4980 ¶m->fp_comp_end,
4981 ¶m->fp_comp_end_units, 0);
4984 fprintf(stderr, "error: %s bad "
4985 "component end '%s'.\n",
4989 param->fp_check_comp_end = 1;
4991 param->fp_verbose |= VERBOSE_COMP_END;
4992 param->fp_max_depth = 0;
4996 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4997 param->fp_verbose |= VERBOSE_DFID;
4998 param->fp_max_depth = 0;
5002 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5003 param->fp_verbose |= VERBOSE_GENERATION;
5004 param->fp_max_depth = 0;
5008 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5009 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5010 param->fp_max_depth = 0;
5014 if (optarg != NULL) {
5015 param->fp_comp_id = strtoul(optarg, &end, 0);
5016 if (*end != '\0' || param->fp_comp_id == 0 ||
5017 param->fp_comp_id > LCME_ID_MAX) {
5018 fprintf(stderr, "error: %s bad "
5019 "component id '%s'\n",
5023 param->fp_check_comp_id = 1;
5026 param->fp_max_depth = 0;
5027 param->fp_verbose |= VERBOSE_COMP_ID;
5031 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5032 param->fp_verbose |= VERBOSE_PATTERN;
5033 param->fp_max_depth = 0;
5036 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5038 fprintf(stderr, "warning: '-M' deprecated"
5039 ", use '--mdt-index' or '-m' instead\n");
5042 if (!(param->fp_verbose & VERBOSE_DETAIL))
5043 param->fp_max_depth = 0;
5044 param->fp_verbose |= VERBOSE_MDTINDEX;
5047 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5048 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5049 param->fp_max_depth = 0;
5053 if (param->fp_obd_uuid) {
5055 "error: %s: only one obduuid allowed",
5059 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5062 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5063 param->fp_verbose |= VERBOSE_POOL;
5064 param->fp_max_depth = 0;
5071 param->fp_recursive = 1;
5077 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5078 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5079 param->fp_max_depth = 0;
5083 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5089 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5090 param->fp_verbose |= VERBOSE_EXT_SIZE;
5091 param->fp_max_depth = 0;
5099 if (pathstart == -1) {
5100 fprintf(stderr, "error: %s: no filename|pathname\n",
5103 } else if (pathend == -1) {
5111 if (param->fp_recursive)
5112 param->fp_max_depth = -1;
5113 else if (param->fp_verbose & VERBOSE_DETAIL)
5114 param->fp_max_depth = 1;
5116 if (!param->fp_verbose)
5117 param->fp_verbose = VERBOSE_DEFAULT;
5118 if (param->fp_quiet)
5119 param->fp_verbose = VERBOSE_OBJID;
5122 rc = llapi_getstripe(argv[pathstart], param);
5123 } while (++pathstart < pathend && !rc);
5126 fprintf(stderr, "error: %s failed for %s.\n",
5127 argv[0], argv[optind - 1]);
5131 static int lfs_tgts(int argc, char **argv)
5133 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5134 struct find_param param;
5135 int index = 0, rc=0;
5140 if (argc == 2 && !realpath(argv[1], path)) {
5142 fprintf(stderr, "error: invalid path '%s': %s\n",
5143 argv[1], strerror(-rc));
5147 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5148 /* Check if we have a mount point */
5149 if (mntdir[0] == '\0')
5152 memset(¶m, 0, sizeof(param));
5153 if (!strcmp(argv[0], "mdts"))
5154 param.fp_get_lmv = 1;
5156 rc = llapi_ostlist(mntdir, ¶m);
5158 fprintf(stderr, "error: %s: failed on %s\n",
5161 if (path[0] != '\0')
5163 memset(mntdir, 0, PATH_MAX);
5169 static int lfs_getstripe(int argc, char **argv)
5171 struct find_param param = { 0 };
5173 param.fp_max_depth = 1;
5174 return lfs_getstripe_internal(argc, argv, ¶m);
5178 static int lfs_getdirstripe(int argc, char **argv)
5180 struct find_param param = { 0 };
5181 struct option long_opts[] = {
5182 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5183 { .val = 'D', .name = "default", .has_arg = no_argument },
5184 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5185 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5186 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5187 { .val = 'O', .name = "obd", .has_arg = required_argument },
5188 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5189 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5190 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5191 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5195 param.fp_get_lmv = 1;
5197 while ((c = getopt_long(argc, argv,
5198 "cDHimO:rtTvy", long_opts, NULL)) != -1)
5203 param.fp_verbose |= VERBOSE_COUNT;
5206 param.fp_get_default_lmv = 1;
5210 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5212 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5214 fprintf(stderr, "warning: '-t' deprecated, "
5215 "use '--mdt-hash' or '-H' instead\n");
5218 param.fp_verbose |= VERBOSE_HASH_TYPE;
5221 if (param.fp_obd_uuid) {
5223 "error: %s: only one obduuid allowed",
5227 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5230 param.fp_recursive = 1;
5233 param.fp_verbose |= VERBOSE_DETAIL;
5239 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5240 progname, argv[0], argv[optind - 1]);
5248 if (param.fp_recursive)
5249 param.fp_max_depth = -1;
5251 if (!param.fp_verbose)
5252 param.fp_verbose = VERBOSE_DEFAULT;
5255 rc = llapi_getstripe(argv[optind], ¶m);
5256 } while (++optind < argc && !rc);
5259 fprintf(stderr, "error: %s failed for %s.\n",
5260 argv[0], argv[optind - 1]);
5265 MNTDF_INODES = 0x0001,
5266 MNTDF_COOKED = 0x0002,
5267 MNTDF_LAZY = 0x0004,
5268 MNTDF_VERBOSE = 0x0008,
5269 MNTDF_SHOW = 0x0010,
5272 #define COOK(value) \
5275 while (value > 1024) { \
5283 #define CDF "%11llu"
5284 #define HDF "%8.1f%c"
5288 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5290 double avail, used, ratio = 0;
5293 avail = st->os_ffree;
5294 used = st->os_files - st->os_ffree;
5296 avail = st->os_bavail;
5297 used = st->os_blocks - st->os_bfree;
5299 if (avail + used > 0)
5300 ratio = used / (used + avail) * 100;
5302 /* Round up to match df(1) usage percentage */
5303 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5306 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5307 * so only show flags reflecting those states by default. Informational states
5308 * are only shown with "-v" and use lower-case names to distinguish them.
5309 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5311 static struct obd_statfs_state_names {
5312 enum obd_statfs_state osn_state;
5313 const char osn_name;
5316 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
5317 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
5318 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
5319 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
5320 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
5321 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
5322 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
5323 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
5324 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
5327 static int showdf(char *mntdir, struct obd_statfs *stat,
5328 char *uuid, enum mntdf_flags flags,
5329 char *type, int index, int rc)
5331 long long avail, used, total;
5333 char *suffix = "KMGTPEZY";
5334 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5335 char tbuf[3 * sizeof(__u64)];
5336 char ubuf[3 * sizeof(__u64)];
5337 char abuf[3 * sizeof(__u64)];
5338 char rbuf[3 * sizeof(__u64)];
5345 if (flags & MNTDF_INODES) {
5346 avail = stat->os_ffree;
5347 used = stat->os_files - stat->os_ffree;
5348 total = stat->os_files;
5350 int shift = flags & MNTDF_COOKED ? 0 : 10;
5352 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5353 used = ((stat->os_blocks - stat->os_bfree) *
5354 stat->os_bsize) >> shift;
5355 total = (stat->os_blocks * stat->os_bsize) >> shift;
5358 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5360 if (flags & MNTDF_COOKED) {
5364 cook_val = (double)total;
5367 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5370 snprintf(tbuf, sizeof(tbuf), CDF, total);
5372 cook_val = (double)used;
5375 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5378 snprintf(ubuf, sizeof(ubuf), CDF, used);
5380 cook_val = (double)avail;
5383 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5386 snprintf(abuf, sizeof(abuf), CDF, avail);
5388 snprintf(tbuf, sizeof(tbuf), CDF, total);
5389 snprintf(ubuf, sizeof(tbuf), CDF, used);
5390 snprintf(abuf, sizeof(tbuf), CDF, avail);
5393 sprintf(rbuf, RDF, ratio);
5394 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5395 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5397 printf("[%s:%d]", type, index);
5399 if (stat->os_state) {
5403 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5404 if (oss_names[i].osn_state & stat->os_state &&
5405 (oss_names[i].osn_err ||
5406 flags & MNTDF_VERBOSE))
5407 printf("%c", oss_names[i].osn_name);
5414 printf(UUF": inactive device\n", uuid);
5417 printf(UUF": %s\n", uuid, strerror(-rc));
5424 struct ll_stat_type {
5429 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5431 struct ll_statfs_data {
5433 struct obd_statfs sd_st;
5436 struct ll_statfs_buf {
5438 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5441 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5442 int ops, struct ll_statfs_buf *lsb)
5444 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5445 struct obd_uuid uuid_buf;
5446 char *poolname = NULL;
5447 struct ll_stat_type types[] = {
5448 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5449 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5450 { .st_name = NULL } };
5451 struct ll_stat_type *tp;
5452 __u64 ost_files = 0;
5453 __u64 ost_ffree = 0;
5461 poolname = strchr(pool, '.');
5462 if (poolname != NULL) {
5463 if (strncmp(fsname, pool, strlen(fsname))) {
5464 fprintf(stderr, "filesystem name incorrect\n");
5472 fd = open(mntdir, O_RDONLY);
5475 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5480 if (flags & MNTDF_SHOW) {
5481 if (flags & MNTDF_INODES)
5482 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5483 "UUID", "Inodes", "IUsed", "IFree",
5484 "IUse%", "Mounted on");
5486 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5488 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5489 "Used", "Available", "Use%", "Mounted on");
5492 for (tp = types; tp->st_name != NULL; tp++) {
5493 bool have_ost = false;
5495 if (!(tp->st_op & ops))
5498 for (index = 0; ; index++) {
5499 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5500 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5501 type = flags & MNTDF_LAZY ?
5502 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5503 rc2 = llapi_obd_fstatfs(fd, type, index,
5504 &stat_buf, &uuid_buf);
5509 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5510 if (!(flags & MNTDF_VERBOSE))
5512 } else if (rc2 < 0 && rc == 0) {
5516 /* If we have OSTs then don't report MDT block counts.
5517 * For MDT-only filesystems the expectation is that all
5518 * layouts have a DoM component. For filesystems with
5519 * OSTs, files are not necessarily going to store data
5520 * on MDTs, and MDT space is limited to a fraction of
5521 * OST space, so don't include it in the summary.
5523 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5530 if (poolname && tp->st_op == LL_STATFS_LOV &&
5531 llapi_search_ost(fsname, poolname,
5532 obd_uuid2str(&uuid_buf)) != 1)
5535 /* the llapi_obd_statfs() call may have returned with
5536 * an error, but if it filled in uuid_buf we will at
5537 * lease use that to print out a message for that OBD.
5538 * If we didn't get anything in the uuid_buf, then fill
5539 * it in so that we can print an error message. */
5540 if (uuid_buf.uuid[0] == '\0')
5541 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5542 "%s%04x", tp->st_name, index);
5544 lsb->sb_buf[lsb->sb_count].sd_index = index;
5545 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5548 if (flags & MNTDF_SHOW)
5549 showdf(mntdir, &stat_buf,
5550 obd_uuid2str(&uuid_buf), flags,
5551 tp->st_name, index, rc2);
5556 if (tp->st_op == LL_STATFS_LMV) {
5557 sum.os_ffree += stat_buf.os_ffree;
5558 sum.os_files += stat_buf.os_files;
5559 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5560 ost_files += stat_buf.os_files;
5561 ost_ffree += stat_buf.os_ffree;
5563 sum.os_blocks += stat_buf.os_blocks *
5565 sum.os_bfree += stat_buf.os_bfree *
5567 sum.os_bavail += stat_buf.os_bavail *
5574 /* If we have _some_ OSTs, but don't have as many free objects on the
5575 * OST as inodes on the MDTs, reduce the reported number of inodes
5576 * to compensate, so that the "inodes in use" number is correct.
5577 * This should be kept in sync with ll_statfs_internal().
5579 if (ost_files && ost_ffree < sum.os_ffree) {
5580 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5581 sum.os_ffree = ost_ffree;
5583 if (flags & MNTDF_SHOW) {
5585 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5593 static int lfs_setdirstripe(int argc, char **argv)
5596 struct lfs_setstripe_args lsa = { 0 };
5597 struct llapi_stripe_param *param = NULL;
5598 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5601 char *mode_opt = NULL;
5602 bool default_stripe = false;
5603 bool delete = false;
5604 bool foreign_mode = false;
5605 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5606 mode_t previous_mode = 0;
5608 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5609 struct option long_opts[] = {
5610 { .val = 'c', .name = "count", .has_arg = required_argument },
5611 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5612 { .val = 'd', .name = "delete", .has_arg = no_argument },
5613 { .val = 'D', .name = "default", .has_arg = no_argument },
5614 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5615 { .val = LFS_LAYOUT_FLAGS_OPT,
5616 .name = "flags", .has_arg = required_argument },
5617 { .val = LFS_LAYOUT_FOREIGN_OPT,
5618 .name = "foreign", .has_arg = optional_argument},
5619 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5620 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5621 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5622 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5624 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5625 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5626 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5628 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5629 { .val = 'i', .name = "index", .has_arg = required_argument },
5631 { .val = 'o', .name = "mode", .has_arg = required_argument },
5632 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5633 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5635 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5636 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5637 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5641 setstripe_args_init(&lsa);
5643 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5644 long_opts, NULL)) >= 0) {
5651 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5654 "%s %s: invalid stripe count '%s'\n",
5655 progname, argv[0], optarg);
5661 default_stripe = true;
5664 default_stripe = true;
5666 case LFS_LAYOUT_FOREIGN_OPT:
5667 if (optarg != NULL) {
5668 /* check pure numeric */
5669 type = strtoul(optarg, &end, 0);
5672 type = check_foreign_type_name(optarg);
5673 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5675 "%s %s: unknown foreign type '%s'\n",
5682 foreign_mode = true;
5684 case LFS_LAYOUT_FLAGS_OPT:
5685 flags = strtoul(optarg, &end, 16);
5688 "%s %s: bad flags '%s'\n",
5689 progname, argv[0], optarg);
5693 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5695 fprintf(stderr, "warning: '--hash-type' and '-t' "
5696 "deprecated, use '--mdt-hash' or '-H' instead\n");
5699 lsa.lsa_pattern = check_hashtype(optarg);
5700 if (lsa.lsa_pattern == 0) {
5702 "%s %s: bad stripe hash type '%s'\n",
5703 progname, argv[0], optarg);
5708 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5711 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5712 if (strcmp(argv[optind - 1], "--index") == 0)
5714 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5717 lsa.lsa_nr_tgts = parse_targets(mdts,
5718 sizeof(mdts) / sizeof(__u32),
5719 lsa.lsa_nr_tgts, optarg, NULL);
5720 if (lsa.lsa_nr_tgts < 0) {
5722 "%s %s: invalid MDT target(s) '%s'\n",
5723 progname, argv[0], optarg);
5727 lsa.lsa_tgts = mdts;
5728 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5729 lsa.lsa_stripe_off = mdts[0];
5731 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5733 fprintf(stderr, "warning: '-m' is deprecated, "
5734 "use '--mode' or '-o' instead\n");
5743 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5744 progname, argv[0], argv[optind - 1]);
5749 if (optind == argc) {
5750 fprintf(stderr, "%s %s: DIR must be specified\n",
5755 if (xattr && !foreign_mode) {
5756 /* only print a warning as this is armless and will be
5760 "%s %s: xattr has been specified for non-foreign layout\n",
5762 } else if (foreign_mode && !xattr) {
5764 "%s %s: xattr must be provided in foreign mode\n",
5769 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5770 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5772 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5777 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5778 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5780 "%s %s: stripe offset and count must be specified\n",
5786 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5787 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5789 "%s %s: cannot specify -d with -c or -i options\n",
5794 if (mode_opt != NULL) {
5795 mode = strtoul(mode_opt, &end, 8);
5798 "%s %s: bad MODE '%s'\n",
5799 progname, argv[0], mode_opt);
5802 previous_mode = umask(0);
5805 /* foreign LMV/dir case */
5807 if (argc > optind + 1) {
5809 "%s %s: cannot specify multiple foreign dirs\n",
5814 dname = argv[optind];
5815 result = llapi_dir_create_foreign(dname, mode, type, flags,
5819 "%s mkdir: can't create foreign dir '%s': %s\n",
5820 progname, dname, strerror(-result));
5825 * initialize stripe parameters, in case param is converted to specific,
5826 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5828 param = calloc(1, offsetof(typeof(*param),
5829 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5830 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5831 if (param == NULL) {
5833 "%s %s: cannot allocate memory for parameters: %s\n",
5834 progname, argv[0], strerror(ENOMEM));
5838 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5839 param->lsp_stripe_count = lsa.lsa_stripe_count;
5840 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5841 param->lsp_stripe_offset = -1;
5843 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5844 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5845 param->lsp_stripe_pattern = lsa.lsa_pattern;
5847 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
5848 param->lsp_pool = lsa.lsa_pool_name;
5849 param->lsp_is_specific = false;
5850 if (lsa.lsa_nr_tgts > 1) {
5851 if (lsa.lsa_stripe_count > 0 &&
5852 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5853 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5855 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
5856 argv[0], lsa.lsa_stripe_count,
5862 param->lsp_is_specific = true;
5863 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5864 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5867 dname = argv[optind];
5869 if (default_stripe) {
5870 result = llapi_dir_set_default_lmv(dname, param);
5873 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5874 progname, dname, strerror(-result));
5878 result = llapi_dir_create(dname, mode, param);
5881 "%s setdirstripe: cannot create dir '%s': %s\n",
5882 progname, dname, strerror(-result));
5883 } while (!result && (dname = argv[++optind]));
5885 if (mode_opt != NULL)
5886 umask(previous_mode);
5893 static int lfs_rmentry(int argc, char **argv)
5900 fprintf(stderr, "error: %s: missing dirname\n",
5906 dname = argv[index];
5907 while (dname != NULL) {
5908 result = llapi_direntry_remove(dname);
5910 fprintf(stderr, "error: %s: remove dir entry '%s' "
5911 "failed\n", argv[0], dname);
5914 dname = argv[++index];
5919 static int lfs_mv(int argc, char **argv)
5921 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5922 struct find_param param = {
5929 struct option long_opts[] = {
5930 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5931 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5932 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5935 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5937 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5939 fprintf(stderr, "warning: '-M' deprecated"
5940 ", use '--mdt-index' or '-m' instead\n");
5943 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5945 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5951 param.fp_verbose = VERBOSE_DETAIL;
5954 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5955 progname, argv[optind - 1]);
5960 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
5961 fprintf(stderr, "%s mv: MDT index must be specified\n",
5966 if (optind >= argc) {
5967 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5972 /* initialize migrate mdt parameters */
5973 param.fp_lmv_md = &lmu;
5974 param.fp_migrate = 1;
5975 rc = llapi_migrate_mdt(argv[optind], ¶m);
5977 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5978 progname, argv[optind], param.fp_mdt_index,
5983 static int lfs_osts(int argc, char **argv)
5985 return lfs_tgts(argc, argv);
5988 static int lfs_mdts(int argc, char **argv)
5990 return lfs_tgts(argc, argv);
5993 static int lfs_df(int argc, char **argv)
5995 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5996 enum mntdf_flags flags = MNTDF_SHOW;
5997 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5998 int c, rc = 0, index = 0;
5999 char fsname[PATH_MAX] = "", *pool_name = NULL;
6000 struct option long_opts[] = {
6001 { .val = 'h', .name = "human-readable",
6002 .has_arg = no_argument },
6003 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6004 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6005 { .val = 'p', .name = "pool", .has_arg = required_argument },
6006 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6009 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6012 flags |= MNTDF_COOKED;
6015 flags |= MNTDF_INODES;
6018 flags |= MNTDF_LAZY;
6024 flags |= MNTDF_VERBOSE;
6030 if (optind < argc && !realpath(argv[optind], path)) {
6032 fprintf(stderr, "error: invalid path '%s': %s\n",
6033 argv[optind], strerror(-rc));
6037 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6038 /* Check if we have a mount point */
6039 if (mntdir[0] == '\0')
6042 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6043 if (rc || path[0] != '\0')
6045 fsname[0] = '\0'; /* avoid matching in next loop */
6046 mntdir[0] = '\0'; /* avoid matching in next loop */
6052 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
6053 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6057 /* llapi_search_mounts() fills "fsname", but that is not called if
6058 * explicit paths are specified on the command-line
6060 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
6061 int rc = llapi_getname(mntdir, fsname, fsnamelen);
6064 fprintf(stderr, "cannot get instance for '%s': %s\n",
6065 mntdir, strerror(-rc));
6069 if (!opt_instance) {
6070 /* print only the fsname name */
6071 buf = strchr(fsname, '-');
6075 } else if (!opt_fsname) {
6076 /* print only the instance name */
6077 buf = strchr(fsname, '-');
6083 } else if (opt_fsname) {
6084 /* print only the fsname */
6088 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
6093 static int lfs_getname(int argc, char **argv)
6095 struct option long_opts[] = {
6096 { .val = 'h', .name = "help", .has_arg = no_argument },
6097 { .val = 'i', .name = "instance", .has_arg = no_argument },
6098 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6100 bool opt_instance = false, opt_fsname = false;
6101 char fsname[PATH_MAX] = "";
6104 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6107 opt_instance = true;
6118 /* If neither option is given, print both instance and fsname */
6119 if (!opt_instance && !opt_fsname)
6120 opt_instance = opt_fsname = true;
6122 if (optind == argc) { /* no paths specified, get all paths. */
6123 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6126 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6127 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6128 opt_instance, opt_fsname, true);
6131 path[0] = fsname[0] = mntdir[0] = '\0';
6133 } else { /* paths specified, only attempt to search these. */
6136 /* if only one path is given, print only requested info */
6137 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6139 for (; optind < argc; optind++) {
6140 rc2 = print_instance(argv[optind], fsname,
6141 sizeof(fsname), opt_instance,
6142 opt_fsname, opt_mntdir);
6152 static int lfs_check(int argc, char **argv)
6154 char mntdir[PATH_MAX] = {'\0'};
6163 fprintf(stderr, "%s check: server type must be specified\n",
6168 obd_types[0] = obd_type1;
6169 obd_types[1] = obd_type2;
6170 obd_types[2] = obd_type3;
6172 if (strcmp(argv[1], "osts") == 0) {
6173 strcpy(obd_types[0], "osc");
6174 } else if (strcmp(argv[1], "mdts") == 0 ||
6175 strcmp(argv[1], "mds") == 0) {
6176 strcpy(obd_types[0], "mdc");
6177 } else if (strcmp(argv[1], "mgts") == 0) {
6178 strcpy(obd_types[0], "mgc");
6179 } else if (strcmp(argv[1], "all") == 0 ||
6180 strcmp(argv[1], "servers") == 0) {
6182 strcpy(obd_types[0], "osc");
6183 strcpy(obd_types[1], "mdc");
6184 strcpy(obd_types[2], "mgc");
6186 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6191 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6192 if (rc < 0 || mntdir[0] == '\0') {
6194 "%s check: cannot find mounted Lustre filesystem: %s\n",
6195 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6199 rc = llapi_target_check(num_types, obd_types, mntdir);
6201 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6202 progname, argv[1], strerror(-rc));
6208 #ifdef HAVE_SYS_QUOTA_H
6209 #define ARG2INT(nr, str, msg) \
6212 nr = strtol(str, &endp, 0); \
6213 if (*endp != '\0') { \
6214 fprintf(stderr, "%s: bad %s '%s'\n", \
6215 progname, msg, str); \
6220 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6222 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6223 * returns the value or ULONG_MAX on integer overflow or incorrect format
6225 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6226 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6227 * 3. empty integer value is interpreted as 0
6229 static unsigned long str2sec(const char* timestr)
6231 const char spec[] = "smhdw";
6232 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6233 unsigned long val = 0;
6236 if (strpbrk(timestr, spec) == NULL) {
6237 /* no specifiers inside the time string,
6238 should treat it as an integer value */
6239 val = strtoul(timestr, &tail, 10);
6240 return *tail ? ULONG_MAX : val;
6243 /* format string is XXwXXdXXhXXmXXs */
6249 v = strtoul(timestr, &tail, 10);
6250 if (v == ULONG_MAX || *tail == '\0')
6251 /* value too large (ULONG_MAX or more)
6252 or missing specifier */
6255 ptr = strchr(spec, *tail);
6257 /* unknown specifier */
6262 /* check if product will overflow the type */
6263 if (!(v < ULONG_MAX / mult[ind]))
6266 ADD_OVERFLOW(val, mult[ind] * v);
6267 if (val == ULONG_MAX)
6279 #define ARG2ULL(nr, str, def_units) \
6281 unsigned long long limit, units = def_units; \
6284 rc = llapi_parse_size(str, &limit, &units, 1); \
6286 fprintf(stderr, "%s: invalid limit '%s'\n", \
6293 static inline int has_times_option(int argc, char **argv)
6297 for (i = 1; i < argc; i++)
6298 if (!strcmp(argv[i], "-t"))
6304 int lfs_setquota_times(int argc, char **argv)
6307 struct if_quotactl qctl;
6308 char *mnt, *obd_type = (char *)qctl.obd_type;
6309 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6310 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6311 struct option long_opts[] = {
6312 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6313 { .val = 'g', .name = "group", .has_arg = no_argument },
6314 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6315 { .val = 'p', .name = "projid", .has_arg = no_argument },
6316 { .val = 't', .name = "times", .has_arg = no_argument },
6317 { .val = 'u', .name = "user", .has_arg = no_argument },
6321 memset(&qctl, 0, sizeof(qctl));
6322 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6323 qctl.qc_type = ALLQUOTA;
6325 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6326 long_opts, NULL)) != -1) {
6337 if (qctl.qc_type != ALLQUOTA) {
6338 fprintf(stderr, "error: -u/g/p can't be used "
6339 "more than once\n");
6342 qctl.qc_type = qtype;
6345 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6346 fprintf(stderr, "error: bad block-grace: %s\n",
6350 dqb->dqb_valid |= QIF_BTIME;
6353 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6354 fprintf(stderr, "error: bad inode-grace: %s\n",
6358 dqb->dqb_valid |= QIF_ITIME;
6360 case 't': /* Yes, of course! */
6362 default: /* getopt prints error message for us when opterr != 0 */
6367 if (qctl.qc_type == ALLQUOTA) {
6368 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6372 if (optind != argc - 1) {
6373 fprintf(stderr, "error: unexpected parameters encountered\n");
6377 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6378 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6379 fprintf(stderr, "error: grace time is too large\n");
6384 rc = llapi_quotactl(mnt, &qctl);
6387 fprintf(stderr, "%s %s ", obd_type,
6388 obd_uuid2str(&qctl.obd_uuid));
6389 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6396 #define BSLIMIT (1 << 0)
6397 #define BHLIMIT (1 << 1)
6398 #define ISLIMIT (1 << 2)
6399 #define IHLIMIT (1 << 3)
6401 int lfs_setquota(int argc, char **argv)
6404 struct if_quotactl qctl;
6405 char *mnt, *obd_type = (char *)qctl.obd_type;
6406 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6407 struct option long_opts[] = {
6408 { .val = 'b', .name = "block-softlimit",
6409 .has_arg = required_argument },
6410 { .val = 'B', .name = "block-hardlimit",
6411 .has_arg = required_argument },
6412 { .val = 'd', .name = "default", .has_arg = no_argument },
6413 { .val = 'g', .name = "group", .has_arg = required_argument },
6414 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6415 { .val = 'i', .name = "inode-softlimit",
6416 .has_arg = required_argument },
6417 { .val = 'I', .name = "inode-hardlimit",
6418 .has_arg = required_argument },
6419 { .val = 'p', .name = "projid", .has_arg = required_argument },
6420 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6421 { .val = 'u', .name = "user", .has_arg = required_argument },
6422 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6424 unsigned limit_mask = 0;
6426 bool use_default = false;
6429 if (has_times_option(argc, argv))
6430 return lfs_setquota_times(argc, argv);
6432 memset(&qctl, 0, sizeof(qctl));
6433 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6434 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6435 * so it can be used as a marker that qc_type
6436 * isn't reinitialized from command line */
6438 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6439 long_opts, NULL)) != -1) {
6442 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6445 goto quota_type_def;
6448 rc = name2uid(&qctl.qc_id, optarg);
6451 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6454 goto quota_type_def;
6457 rc = name2gid(&qctl.qc_id, optarg);
6460 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6463 goto quota_type_def;
6466 rc = name2projid(&qctl.qc_id, optarg);
6469 qctl.qc_id = strtoul(optarg, &endptr, 10);
6470 if (*endptr != '\0') {
6471 fprintf(stderr, "%s setquota: invalid"
6472 " id '%s'\n", progname, optarg);
6477 if (qctl.qc_id == 0) {
6478 fprintf(stderr, "%s setquota: can't set quota"
6479 " for root usr/group/project.\n",
6485 if (qctl.qc_type != ALLQUOTA) {
6487 "%s setquota: only one of -u, -U, -g,"
6488 " -G, -p or -P may be specified\n",
6492 qctl.qc_type = qtype;
6495 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6499 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6500 dqb->dqb_bsoftlimit >>= 10;
6501 limit_mask |= BSLIMIT;
6502 if (dqb->dqb_bsoftlimit &&
6503 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6505 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6506 "See '%s help setquota' or Lustre manual for details\n",
6508 (unsigned long long)dqb->dqb_bsoftlimit,
6512 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6513 dqb->dqb_bhardlimit >>= 10;
6514 limit_mask |= BHLIMIT;
6515 if (dqb->dqb_bhardlimit &&
6516 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6518 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6519 "See '%s help setquota' or Lustre manual for details\n",
6521 (unsigned long long)dqb->dqb_bhardlimit,
6525 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6526 limit_mask |= ISLIMIT;
6527 if (dqb->dqb_isoftlimit &&
6528 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6530 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6531 "See '%s help setquota' or Lustre manual for details\n",
6533 (unsigned long long)dqb->dqb_isoftlimit,
6537 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6538 limit_mask |= IHLIMIT;
6539 if (dqb->dqb_ihardlimit &&
6540 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6542 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6543 "See '%s help setquota' or Lustre manual for details\n",
6545 (unsigned long long)dqb->dqb_ihardlimit,
6550 "%s setquota: unrecognized option '%s'\n",
6551 progname, argv[optind - 1]);
6556 if (qctl.qc_type == ALLQUOTA) {
6558 "%s setquota: either -u or -g must be specified\n",
6563 if (!use_default && limit_mask == 0) {
6565 "%s setquota: at least one limit must be specified\n",
6570 if (use_default && limit_mask != 0) {
6572 "%s setquota: limits should not be specified when"
6573 " using default quota\n",
6578 if (use_default && qctl.qc_id == 0) {
6580 "%s setquota: can not set default quota for root"
6581 " user/group/project\n",
6586 if (optind != argc - 1) {
6588 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6589 progname, argv[optind]);
6596 dqb->dqb_bhardlimit = 0;
6597 dqb->dqb_bsoftlimit = 0;
6598 dqb->dqb_ihardlimit = 0;
6599 dqb->dqb_isoftlimit = 0;
6602 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6603 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6604 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6605 /* sigh, we can't just set blimits/ilimits */
6606 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6607 .qc_type = qctl.qc_type,
6608 .qc_id = qctl.qc_id};
6610 rc = llapi_quotactl(mnt, &tmp_qctl);
6614 if (!(limit_mask & BHLIMIT))
6615 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6616 if (!(limit_mask & BSLIMIT))
6617 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6618 if (!(limit_mask & IHLIMIT))
6619 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6620 if (!(limit_mask & ISLIMIT))
6621 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6623 /* Keep grace times if we have got no softlimit arguments */
6624 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6625 dqb->dqb_valid |= QIF_BTIME;
6626 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6629 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6630 dqb->dqb_valid |= QIF_ITIME;
6631 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6635 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6636 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6638 rc = llapi_quotactl(mnt, &qctl);
6642 "%s setquota: cannot quotactl '%s' '%s': %s",
6644 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6651 /* Converts seconds value into format string
6652 * result is returned in buf
6654 * 1. result is in descenting order: 1w2d3h4m5s
6655 * 2. zero fields are not filled (except for p. 3): 5d1s
6656 * 3. zero seconds value is presented as "0s"
6658 static char * __sec2str(time_t seconds, char *buf)
6660 const char spec[] = "smhdw";
6661 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6666 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6667 c = seconds / mult[i];
6669 if (c > 0 || (i == 0 && buf == tail))
6670 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6678 static void sec2str(time_t seconds, char *buf, int rc)
6685 tail = __sec2str(seconds, tail);
6687 if (rc && tail - buf < 39) {
6693 static void diff2str(time_t seconds, char *buf, time_t now)
6699 if (seconds <= now) {
6700 strcpy(buf, "none");
6703 __sec2str(seconds - now, buf);
6706 static void print_quota_title(char *name, struct if_quotactl *qctl,
6707 bool human_readable, bool show_default)
6710 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6711 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6712 "Filesystem", "bquota", "blimit", "bgrace",
6713 "iquota", "ilimit", "igrace");
6715 printf("Disk quotas for %s %s (%cid %u):\n",
6716 qtype_name(qctl->qc_type), name,
6717 *qtype_name(qctl->qc_type), qctl->qc_id);
6718 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6719 "Filesystem", human_readable ? "used" : "kbytes",
6720 "quota", "limit", "grace",
6721 "files", "quota", "limit", "grace");
6725 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6728 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6731 snprintf(buf, buflen, "%5.4gP",
6732 (double)num / ((__u64)1 << 40));
6734 snprintf(buf, buflen, "%5.4gT",
6735 (double)num / (1 << 30));
6737 snprintf(buf, buflen, "%5.4gG",
6738 (double)num / (1 << 20));
6740 snprintf(buf, buflen, "%5.4gM",
6741 (double)num / (1 << 10));
6743 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6747 #define STRBUF_LEN 32
6748 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6749 int rc, bool h, bool show_default)
6755 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6756 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6757 int bover = 0, iover = 0;
6758 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6759 char numbuf[3][STRBUF_LEN];
6761 char strbuf[STRBUF_LEN];
6763 if (dqb->dqb_bhardlimit &&
6764 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6766 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6767 if (dqb->dqb_btime > now) {
6774 if (dqb->dqb_ihardlimit &&
6775 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6777 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6778 if (dqb->dqb_itime > now) {
6786 if (strlen(mnt) > 15)
6787 printf("%s\n%15s", mnt, "");
6789 printf("%15s", mnt);
6792 diff2str(dqb->dqb_btime, timebuf, now);
6793 else if (show_default)
6794 snprintf(timebuf, sizeof(timebuf), "%llu",
6795 (unsigned long long)dqb->dqb_btime);
6797 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6798 strbuf, sizeof(strbuf), h);
6799 if (rc == -EREMOTEIO)
6800 sprintf(numbuf[0], "%s*", strbuf);
6802 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6803 "%s" : "[%s]", strbuf);
6805 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6806 if (type == QC_GENERAL)
6807 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6808 "%s" : "[%s]", strbuf);
6810 sprintf(numbuf[1], "%s", "-");
6812 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6813 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6814 "%s" : "[%s]", strbuf);
6817 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6819 printf(" %7s%c %6s %7s %7s",
6820 numbuf[0], bover ? '*' : ' ', numbuf[1],
6821 numbuf[2], bover > 1 ? timebuf : "-");
6825 diff2str(dqb->dqb_itime, timebuf, now);
6826 else if (show_default)
6827 snprintf(timebuf, sizeof(timebuf), "%llu",
6828 (unsigned long long)dqb->dqb_itime);
6830 snprintf(numbuf[0], sizeof(numbuf),
6831 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6832 (uintmax_t)dqb->dqb_curinodes);
6834 if (type == QC_GENERAL)
6835 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6837 (uintmax_t)dqb->dqb_isoftlimit);
6839 sprintf(numbuf[1], "%s", "-");
6841 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6842 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6845 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6846 else if (type != QC_OSTIDX)
6847 printf(" %7s%c %6s %7s %7s",
6848 numbuf[0], iover ? '*' : ' ', numbuf[1],
6849 numbuf[2], iover > 1 ? timebuf : "-");
6851 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6854 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6855 qctl->qc_cmd == Q_GETOINFO) {
6859 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6860 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6861 printf("Block grace time: %s; Inode grace time: %s\n",
6862 bgtimebuf, igtimebuf);
6866 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6867 bool h, __u64 *total)
6869 int rc = 0, rc1 = 0, count = 0;
6870 __u32 valid = qctl->qc_valid;
6872 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6874 fprintf(stderr, "can not get %s count: %s\n",
6875 is_mdt ? "mdt": "ost", strerror(-rc));
6879 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6880 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6881 rc = llapi_quotactl(mnt, qctl);
6883 /* It is remote client case. */
6884 if (rc == -EOPNOTSUPP) {
6891 fprintf(stderr, "quotactl %s%d failed.\n",
6892 is_mdt ? "mdt": "ost", qctl->qc_idx);
6896 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6897 qctl->qc_valid, 0, h, false);
6898 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6899 qctl->qc_dqblk.dqb_bhardlimit;
6902 qctl->qc_valid = valid;
6906 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6907 int verbose, int quiet, bool human_readable,
6910 int rc1 = 0, rc2 = 0, rc3 = 0;
6911 char *obd_type = (char *)qctl->obd_type;
6912 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6913 __u64 total_ialloc = 0, total_balloc = 0;
6914 bool use_default_for_blk = false;
6915 bool use_default_for_file = false;
6918 rc1 = llapi_quotactl(mnt, qctl);
6922 fprintf(stderr, "%s quotas are not enabled.\n",
6923 qtype_name(qctl->qc_type));
6926 fprintf(stderr, "Permission denied.\n");
6929 /* We already got error message. */
6932 fprintf(stderr, "Unexpected quotactl error: %s\n",
6937 if (!show_default && qctl->qc_id == 0) {
6938 qctl->qc_dqblk.dqb_bhardlimit = 0;
6939 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6940 qctl->qc_dqblk.dqb_ihardlimit = 0;
6941 qctl->qc_dqblk.dqb_isoftlimit = 0;
6942 qctl->qc_dqblk.dqb_btime = 0;
6943 qctl->qc_dqblk.dqb_itime = 0;
6944 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6947 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6948 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6949 use_default_for_blk = true;
6950 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6953 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6954 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6955 use_default_for_file = true;
6956 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6959 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6960 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6961 print_quota_title(name, qctl, human_readable, show_default);
6963 if (rc1 && *obd_type)
6964 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6966 if (qctl->qc_valid != QC_GENERAL)
6969 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6970 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6971 (QIF_LIMITS|QIF_USAGE));
6973 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6975 if (!show_default && verbose &&
6976 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6977 char strbuf[STRBUF_LEN];
6979 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6981 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6983 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6985 printf("Total allocated inode limit: %ju, total "
6986 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6990 if (use_default_for_blk)
6991 printf("%cid %u is using default block quota setting\n",
6992 *qtype_name(qctl->qc_type), qctl->qc_id);
6994 if (use_default_for_file)
6995 printf("%cid %u is using default file quota setting\n",
6996 *qtype_name(qctl->qc_type), qctl->qc_id);
6998 if (rc1 || rc2 || rc3 || inacc)
6999 printf("Some errors happened when getting quota info. "
7000 "Some devices may be not working or deactivated. "
7001 "The data in \"[]\" is inaccurate.\n");
7007 static int lfs_project(int argc, char **argv)
7009 int ret = 0, err = 0, c, i;
7010 struct project_handle_control phc = { 0 };
7011 enum lfs_project_ops_t op;
7014 phc.assign_projid = false;
7015 /* default action */
7016 op = LFS_PROJECT_LIST;
7018 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7021 if (op != LFS_PROJECT_LIST) {
7023 "%s: cannot specify '-c' '-C' '-s' together\n",
7028 op = LFS_PROJECT_CHECK;
7031 if (op != LFS_PROJECT_LIST) {
7033 "%s: cannot specify '-c' '-C' '-s' together\n",
7038 op = LFS_PROJECT_CLEAR;
7041 if (op != LFS_PROJECT_LIST) {
7043 "%s: cannot specify '-c' '-C' '-s' together\n",
7048 phc.set_inherit = true;
7049 op = LFS_PROJECT_SET;
7055 phc.keep_projid = true;
7058 phc.recursive = true;
7061 phc.projid = strtoul(optarg, NULL, 0);
7062 phc.assign_projid = true;
7066 phc.newline = false;
7069 fprintf(stderr, "%s: invalid option '%c'\n",
7075 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7076 op = LFS_PROJECT_SET;
7077 phc.set_projid = true;
7078 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7079 phc.set_projid = true;
7083 case LFS_PROJECT_CHECK:
7084 if (phc.keep_projid) {
7086 "%s: '-k' is useless together with '-c'\n",
7091 case LFS_PROJECT_CLEAR:
7094 "%s: '-0' is useless together with '-C'\n",
7098 if (phc.assign_projid) {
7100 "%s: '-p' is useless together with '-C'\n",
7105 case LFS_PROJECT_SET:
7108 "%s: '-0' is useless together with '-s'\n",
7112 if (phc.keep_projid) {
7114 "%s: '-k' is useless together with '-s'\n",
7122 "%s: '-0' is useless for list operations\n",
7132 fprintf(stderr, "%s: missing file or directory target(s)\n",
7137 for (i = 0; i < argc; i++) {
7139 case LFS_PROJECT_CHECK:
7140 err = lfs_project_check(argv[i], &phc);
7142 case LFS_PROJECT_LIST:
7143 err = lfs_project_list(argv[i], &phc);
7145 case LFS_PROJECT_CLEAR:
7146 err = lfs_project_clear(argv[i], &phc);
7148 case LFS_PROJECT_SET:
7149 err = lfs_project_set(argv[i], &phc);
7161 static int lfs_quota(int argc, char **argv)
7164 char *mnt, *name = NULL;
7165 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
7166 .qc_type = ALLQUOTA };
7167 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
7168 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7170 __u32 valid = QC_GENERAL, idx = 0;
7171 bool human_readable = false;
7172 bool show_default = false;
7175 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
7178 show_default = true;
7183 show_default = true;
7188 show_default = true;
7192 if (qctl.qc_type != ALLQUOTA) {
7194 "%s quota: only one of -u, -g, or -p may be specified\n",
7198 qctl.qc_type = qtype;
7201 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7204 valid = qctl.qc_valid = QC_UUID;
7205 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7208 valid = qctl.qc_valid = QC_MDTIDX;
7209 idx = qctl.qc_idx = atoi(optarg);
7210 if (idx == 0 && *optarg != '0') {
7212 "%s quota: invalid MDT index '%s'\n",
7218 valid = qctl.qc_valid = QC_OSTIDX;
7219 idx = qctl.qc_idx = atoi(optarg);
7220 if (idx == 0 && *optarg != '0') {
7222 "%s quota: invalid OST index '%s'\n",
7234 human_readable = true;
7237 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7238 progname, argv[optind - 1]);
7243 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7244 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7245 optind == argc - 1 && !show_default) {
7247 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7250 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7251 qctl.qc_type = qtype;
7252 qctl.qc_valid = valid;
7253 if (qtype == USRQUOTA) {
7254 qctl.qc_id = geteuid();
7255 rc = uid2name(&name, qctl.qc_id);
7257 qctl.qc_id = getegid();
7258 rc = gid2name(&name, qctl.qc_id);
7259 memset(&qctl.qc_dqblk, 0,
7260 sizeof(qctl.qc_dqblk));
7265 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7266 human_readable, show_default);
7271 /* lfs quota -u username /path/to/lustre/mount */
7272 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7273 /* options should be followed by u/g-name and mntpoint */
7274 if ((!show_default && optind + 2 != argc) ||
7275 (show_default && optind + 1 != argc) ||
7276 qctl.qc_type == ALLQUOTA) {
7278 "%s quota: name and mount point must be specified\n",
7283 if (!show_default) {
7284 name = argv[optind++];
7285 switch (qctl.qc_type) {
7287 rc = name2uid(&qctl.qc_id, name);
7290 rc = name2gid(&qctl.qc_id, name);
7293 rc = name2projid(&qctl.qc_id, name);
7300 qctl.qc_valid = QC_GENERAL;
7301 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7306 qctl.qc_id = strtoul(name, &endptr, 10);
7307 if (*endptr != '\0') {
7308 fprintf(stderr, "%s quota: invalid id '%s'\n",
7313 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7314 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7320 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7321 human_readable, show_default);
7324 #endif /* HAVE_SYS_QUOTA_H! */
7326 static int flushctx_ioctl(char *mp)
7330 fd = open(mp, O_RDONLY);
7332 fprintf(stderr, "flushctx: error open %s: %s\n",
7333 mp, strerror(errno));
7337 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7339 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7340 mp, strerror(errno));
7346 static int lfs_flushctx(int argc, char **argv)
7348 int kdestroy = 0, c;
7349 char mntdir[PATH_MAX] = {'\0'};
7353 while ((c = getopt(argc, argv, "k")) != -1) {
7359 fprintf(stderr, "error: %s: option '-%c' "
7360 "unrecognized\n", argv[0], c);
7366 if ((rc = system("kdestroy > /dev/null")) != 0) {
7367 rc = WEXITSTATUS(rc);
7368 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7372 if (optind >= argc) {
7373 /* flush for all mounted lustre fs. */
7374 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7375 /* Check if we have a mount point */
7376 if (mntdir[0] == '\0')
7379 if (flushctx_ioctl(mntdir))
7382 mntdir[0] = '\0'; /* avoid matching in next loop */
7385 /* flush fs as specified */
7386 while (optind < argc) {
7387 if (flushctx_ioctl(argv[optind++]))
7394 static int lfs_changelog(int argc, char **argv)
7396 void *changelog_priv;
7397 struct changelog_rec *rec;
7398 long long startrec = 0, endrec = 0;
7400 struct option long_opts[] = {
7401 { .val = 'f', .name = "follow", .has_arg = no_argument },
7403 char short_opts[] = "f";
7406 while ((rc = getopt_long(argc, argv, short_opts,
7407 long_opts, NULL)) != -1) {
7414 "%s changelog: unrecognized option '%s'\n",
7415 progname, argv[optind - 1]);
7419 if (optind >= argc) {
7420 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7425 mdd = argv[optind++];
7427 startrec = strtoll(argv[optind++], NULL, 10);
7429 endrec = strtoll(argv[optind++], NULL, 10);
7431 rc = llapi_changelog_start(&changelog_priv,
7432 CHANGELOG_FLAG_BLOCK |
7433 CHANGELOG_FLAG_JOBID |
7434 CHANGELOG_FLAG_EXTRA_FLAGS |
7435 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7438 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7439 progname, strerror(errno = -rc));
7443 rc = llapi_changelog_set_xflags(changelog_priv,
7444 CHANGELOG_EXTRA_FLAG_UIDGID |
7445 CHANGELOG_EXTRA_FLAG_NID |
7446 CHANGELOG_EXTRA_FLAG_OMODE |
7447 CHANGELOG_EXTRA_FLAG_XATTR);
7450 "%s changelog: cannot set xflags for changelog: %s\n",
7451 progname, strerror(errno = -rc));
7455 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7459 if (endrec && rec->cr_index > endrec) {
7460 llapi_changelog_free(&rec);
7463 if (rec->cr_index < startrec) {
7464 llapi_changelog_free(&rec);
7468 secs = rec->cr_time >> 30;
7469 gmtime_r(&secs, &ts);
7470 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7471 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7472 changelog_type2str(rec->cr_type),
7473 ts.tm_hour, ts.tm_min, ts.tm_sec,
7474 (int)(rec->cr_time & ((1 << 30) - 1)),
7475 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7476 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7478 if (rec->cr_flags & CLF_JOBID) {
7479 struct changelog_ext_jobid *jid =
7480 changelog_rec_jobid(rec);
7482 if (jid->cr_jobid[0] != '\0')
7483 printf(" j=%s", jid->cr_jobid);
7486 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7487 struct changelog_ext_extra_flags *ef =
7488 changelog_rec_extra_flags(rec);
7490 printf(" ef=0x%llx",
7491 (unsigned long long)ef->cr_extra_flags);
7493 if (ef->cr_extra_flags & CLFE_UIDGID) {
7494 struct changelog_ext_uidgid *uidgid =
7495 changelog_rec_uidgid(rec);
7497 printf(" u=%llu:%llu",
7498 (unsigned long long)uidgid->cr_uid,
7499 (unsigned long long)uidgid->cr_gid);
7501 if (ef->cr_extra_flags & CLFE_NID) {
7502 struct changelog_ext_nid *nid =
7503 changelog_rec_nid(rec);
7506 libcfs_nid2str(nid->cr_nid));
7509 if (ef->cr_extra_flags & CLFE_OPEN) {
7510 struct changelog_ext_openmode *omd =
7511 changelog_rec_openmode(rec);
7512 char mode[] = "---";
7514 /* exec mode must be exclusive */
7515 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7518 if (omd->cr_openflags & MDS_FMODE_READ)
7520 if (omd->cr_openflags &
7527 if (strcmp(mode, "---") != 0)
7528 printf(" m=%s", mode);
7532 if (ef->cr_extra_flags & CLFE_XATTR) {
7533 struct changelog_ext_xattr *xattr =
7534 changelog_rec_xattr(rec);
7536 if (xattr->cr_xattr[0] != '\0')
7537 printf(" x=%s", xattr->cr_xattr);
7541 if (rec->cr_namelen)
7542 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7543 rec->cr_namelen, changelog_rec_name(rec));
7545 if (rec->cr_flags & CLF_RENAME) {
7546 struct changelog_ext_rename *rnm =
7547 changelog_rec_rename(rec);
7549 if (!fid_is_zero(&rnm->cr_sfid))
7550 printf(" s="DFID" sp="DFID" %.*s",
7551 PFID(&rnm->cr_sfid),
7552 PFID(&rnm->cr_spfid),
7553 (int)changelog_rec_snamelen(rec),
7554 changelog_rec_sname(rec));
7558 llapi_changelog_free(&rec);
7561 llapi_changelog_fini(&changelog_priv);
7564 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7565 progname, strerror(errno = -rc));
7567 return (rc == 1 ? 0 : rc);
7570 static int lfs_changelog_clear(int argc, char **argv)
7578 endrec = strtoll(argv[3], NULL, 10);
7580 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7583 fprintf(stderr, "%s: record out of range: %llu\n",
7585 else if (rc == -ENOENT)
7586 fprintf(stderr, "%s: no changelog user: %s\n",
7589 fprintf(stderr, "%s error: %s\n", argv[0],
7598 static int lfs_fid2path(int argc, char **argv)
7600 struct option long_opts[] = {
7601 { .val = 'c', .name = "cur", .has_arg = no_argument },
7602 { .val = 'l', .name = "link", .has_arg = required_argument },
7603 { .val = 'r', .name = "rec", .has_arg = required_argument },
7605 char short_opts[] = "cl:r:";
7606 char *device, *fid, *path;
7607 long long recno = -1;
7612 char *endptr = NULL;
7614 while ((rc = getopt_long(argc, argv, short_opts,
7615 long_opts, NULL)) != -1) {
7621 linkno = strtol(optarg, &endptr, 10);
7622 if (*endptr != '\0') {
7624 "%s fid2path: invalid linkno '%s'\n",
7630 recno = strtoll(optarg, &endptr, 10);
7631 if (*endptr != '\0') {
7633 "%s fid2path: invalid recno '%s'\n",
7640 "%s fid2path: unrecognized option '%s'\n",
7641 progname, argv[optind - 1]);
7648 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7653 device = argv[optind++];
7654 path = calloc(1, PATH_MAX);
7658 "%s fid2path: cannot allocate memory for path: %s\n",
7659 progname, strerror(-rc));
7664 while (optind < argc) {
7665 fid = argv[optind++];
7667 lnktmp = (linkno >= 0) ? linkno : 0;
7669 int oldtmp = lnktmp;
7670 long long rectmp = recno;
7672 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7676 "%s fid2path: cannot find '%s': %s\n",
7677 progname, fid, strerror(errno = -rc2));
7684 fprintf(stdout, "%lld ", rectmp);
7685 if (device[0] == '/') {
7686 fprintf(stdout, "%s", device);
7687 if (device[strlen(device) - 1] != '/')
7688 fprintf(stdout, "/");
7689 } else if (path[0] == '\0') {
7690 fprintf(stdout, "/");
7692 fprintf(stdout, "%s\n", path);
7695 /* specified linkno */
7697 if (oldtmp == lnktmp)
7707 static int lfs_path2fid(int argc, char **argv)
7709 struct option long_opts[] = {
7710 { .val = 'p', .name = "parents", .has_arg = no_argument },
7713 const char short_opts[] = "p";
7714 const char *sep = "";
7717 bool show_parents = false;
7719 while ((rc = getopt_long(argc, argv, short_opts,
7720 long_opts, NULL)) != -1) {
7723 show_parents = true;
7727 "%s path2fid: unrecognized option '%s'\n",
7728 progname, argv[optind - 1]);
7733 if (optind > argc - 1) {
7734 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7738 else if (optind < argc - 1)
7742 for (path = argv + optind; *path != NULL; path++) {
7744 if (!show_parents) {
7745 err = llapi_path2fid(*path, &fid);
7747 printf("%s%s"DFID"\n",
7748 *sep != '\0' ? *path : "", sep,
7751 char name[NAME_MAX + 1];
7752 unsigned int linkno = 0;
7754 while ((err = llapi_path2parent(*path, linkno, &fid,
7755 name, sizeof(name))) == 0) {
7756 if (*sep != '\0' && linkno == 0)
7757 printf("%s%s", *path, sep);
7759 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7764 /* err == -ENODATA is end-of-loop */
7765 if (linkno > 0 && err == -ENODATA) {
7773 "%s path2fid: cannot get %sfid for '%s': %s\n",
7774 progname, show_parents ? "parent " : "", *path,
7786 #define MAX_ERRNO 4095
7787 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
7789 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
7793 rc = llapi_rmfid(device, fa);
7795 fprintf(stderr, "rmfid(): rc = %d\n", rc);
7799 for (k = 0; k < fa->fa_nr; k++) {
7800 rc = (__s32)fa->fa_fids[k].f_ver;
7801 if (!IS_ERR_VALUE(rc))
7807 fa->fa_fids[k].f_ver = 0;
7808 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
7809 PFID(&fa->fa_fids[k]), rc);
7815 static int lfs_rmfid(int argc, char **argv)
7817 char *fidstr, *device;
7818 int rc = 0, rc2, nr;
7819 struct fid_array *fa;
7821 if (optind > argc - 1) {
7822 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
7826 device = argv[optind++];
7829 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
7835 while (optind < argc) {
7838 fidstr = argv[optind++];
7839 while (*fidstr == '[')
7841 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
7843 fprintf(stderr, "unrecognized FID: %s\n",
7848 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
7849 /* start another batch */
7850 rc2 = lfs_rmfid_and_show_errors(device, fa);
7857 rc2 = lfs_rmfid_and_show_errors(device, fa);
7865 static int lfs_data_version(int argc, char **argv)
7872 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7875 fprintf(stderr, "%s data_version: FILE must be specified\n",
7880 while ((c = getopt(argc, argv, "nrw")) != -1) {
7883 data_version_flags = 0;
7886 data_version_flags |= LL_DV_RD_FLUSH;
7889 data_version_flags |= LL_DV_WR_FLUSH;
7893 "%s data_version: unrecognized option '%s'\n",
7894 progname, argv[optind - 1]);
7898 if (optind == argc) {
7899 fprintf(stderr, "%s data_version: FILE must be specified\n",
7904 path = argv[optind];
7905 fd = open(path, O_RDONLY);
7908 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7909 progname, path, strerror(-rc));
7913 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7916 "%s data_version: cannot get version for '%s': %s\n",
7917 progname, path, strerror(-rc));
7919 printf("%ju" "\n", (uintmax_t)data_version);
7925 static int lfs_hsm_state(int argc, char **argv)
7930 struct hsm_user_state hus;
7938 rc = llapi_hsm_state_get(path, &hus);
7940 fprintf(stderr, "can't get hsm state for %s: %s\n",
7941 path, strerror(errno = -rc));
7945 /* Display path name and status flags */
7946 printf("%s: (0x%08x)", path, hus.hus_states);
7948 if (hus.hus_states & HS_RELEASED)
7949 printf(" released");
7950 if (hus.hus_states & HS_EXISTS)
7952 if (hus.hus_states & HS_DIRTY)
7954 if (hus.hus_states & HS_ARCHIVED)
7955 printf(" archived");
7956 /* Display user-settable flags */
7957 if (hus.hus_states & HS_NORELEASE)
7958 printf(" never_release");
7959 if (hus.hus_states & HS_NOARCHIVE)
7960 printf(" never_archive");
7961 if (hus.hus_states & HS_LOST)
7962 printf(" lost_from_hsm");
7964 if (hus.hus_archive_id != 0)
7965 printf(", archive_id:%d", hus.hus_archive_id);
7968 } while (++i < argc);
7973 #define LFS_HSM_SET 0
7974 #define LFS_HSM_CLEAR 1
7977 * Generic function to set or clear HSM flags.
7978 * Used by hsm_set and hsm_clear.
7980 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7982 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7984 struct option long_opts[] = {
7985 { .val = 'A', .name = "archived", .has_arg = no_argument },
7986 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7987 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7988 { .val = 'e', .name = "exists", .has_arg = no_argument },
7989 { .val = 'l', .name = "lost", .has_arg = no_argument },
7990 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7991 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7993 char short_opts[] = "lraAdei:";
7997 __u32 archive_id = 0;
8003 while ((c = getopt_long(argc, argv, short_opts,
8004 long_opts, NULL)) != -1) {
8010 mask |= HS_NOARCHIVE;
8013 mask |= HS_ARCHIVED;
8016 mask |= HS_NORELEASE;
8025 archive_id = strtol(optarg, &end, 10);
8027 fprintf(stderr, "invalid archive_id: '%s'\n",
8035 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8036 argv[0], argv[optind - 1]);
8041 /* User should have specified a flag */
8045 while (optind < argc) {
8047 path = argv[optind];
8049 /* If mode == 0, this means we apply the mask. */
8050 if (mode == LFS_HSM_SET)
8051 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8053 rc = llapi_hsm_state_set(path, 0, mask, 0);
8056 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8057 path, strerror(errno = -rc));
8066 static int lfs_hsm_action(int argc, char **argv)
8071 struct hsm_current_action hca;
8072 struct hsm_extent he;
8073 enum hsm_user_action hua;
8074 enum hsm_progress_states hps;
8082 rc = llapi_hsm_current_action(path, &hca);
8084 fprintf(stderr, "can't get hsm action for %s: %s\n",
8085 path, strerror(errno = -rc));
8088 he = hca.hca_location;
8089 hua = hca.hca_action;
8090 hps = hca.hca_state;
8092 printf("%s: %s", path, hsm_user_action2name(hua));
8094 /* Skip file without action */
8095 if (hca.hca_action == HUA_NONE) {
8100 printf(" %s ", hsm_progress_state2name(hps));
8102 if ((hps == HPS_RUNNING) &&
8103 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8104 printf("(%llu bytes moved)\n",
8105 (unsigned long long)he.length);
8106 else if ((he.offset + he.length) == LUSTRE_EOF)
8107 printf("(from %llu to EOF)\n",
8108 (unsigned long long)he.offset);
8110 printf("(from %llu to %llu)\n",
8111 (unsigned long long)he.offset,
8112 (unsigned long long)(he.offset + he.length));
8114 } while (++i < argc);
8119 static int lfs_hsm_set(int argc, char **argv)
8121 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8124 static int lfs_hsm_clear(int argc, char **argv)
8126 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8130 * Check file state and return its fid, to be used by lfs_hsm_request().
8132 * \param[in] file Path to file to check
8133 * \param[in,out] fid Pointer to allocated lu_fid struct.
8134 * \param[in,out] last_dev Pointer to last device id used.
8136 * \return 0 on success.
8138 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8144 rc = lstat(file, &st);
8146 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8149 /* Checking for regular file as archiving as posix copytool
8150 * rejects archiving files other than regular files
8152 if (!S_ISREG(st.st_mode)) {
8153 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8156 /* A request should be ... */
8157 if (*last_dev != st.st_dev && *last_dev != 0) {
8158 fprintf(stderr, "All files should be "
8159 "on the same filesystem: %s\n", file);
8162 *last_dev = st.st_dev;
8164 rc = llapi_path2fid(file, fid);
8166 fprintf(stderr, "Cannot read FID of %s: %s\n",
8167 file, strerror(-rc));
8173 /* Fill an HSM HUR item with a given file name.
8175 * If mntpath is set, then the filename is actually a FID, and no
8176 * lookup on the filesystem will be performed.
8178 * \param[in] hur the user request to fill
8179 * \param[in] idx index of the item inside the HUR to fill
8180 * \param[in] mntpath mountpoint of Lustre
8181 * \param[in] fname filename (if mtnpath is NULL)
8182 * or FID (if mntpath is set)
8183 * \param[in] last_dev pointer to last device id used
8185 * \retval 0 on success
8186 * \retval CMD_HELP or a negative errno on error
8188 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8189 const char *mntpath, const char *fname,
8192 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8195 hui->hui_extent.length = -1;
8197 if (mntpath != NULL) {
8198 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8200 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8203 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8207 hur->hur_request.hr_itemcount++;
8212 static int lfs_hsm_request(int argc, char **argv, int action)
8214 struct option long_opts[] = {
8215 { .val = 'a', .name = "archive", .has_arg = required_argument },
8216 { .val = 'D', .name = "data", .has_arg = required_argument },
8217 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8218 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8221 char short_opts[] = "l:D:a:m:";
8222 struct hsm_user_request *hur, *oldhur;
8227 char *filelist = NULL;
8228 char fullpath[PATH_MAX];
8229 char *opaque = NULL;
8233 int nbfile_alloc = 0;
8234 char *some_file = NULL;
8235 char *mntpath = NULL;
8241 while ((c = getopt_long(argc, argv, short_opts,
8242 long_opts, NULL)) != -1) {
8251 if (action != HUA_ARCHIVE &&
8252 action != HUA_REMOVE) {
8254 "error: -a is supported only "
8255 "when archiving or removing\n");
8258 archive_id = atoi(optarg);
8261 if (some_file == NULL) {
8263 some_file = strdup(optarg);
8269 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8270 argv[0], argv[optind - 1]);
8275 /* All remaining args are files, so we have at least nbfile */
8276 nbfile = argc - optind;
8278 if ((nbfile == 0) && (filelist == NULL))
8282 opaque_len = strlen(opaque);
8284 /* Alloc the request structure with enough place to store all files
8285 * from command line. */
8286 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8288 fprintf(stderr, "Cannot create the request: %s\n",
8292 nbfile_alloc = nbfile;
8294 hur->hur_request.hr_action = action;
8295 hur->hur_request.hr_archive_id = archive_id;
8296 hur->hur_request.hr_flags = 0;
8298 /* All remaining args are files, add them */
8299 if (nbfile != 0 && some_file == NULL)
8300 some_file = strdup(argv[optind]);
8302 for (i = 0; i < nbfile; i++) {
8303 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8309 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8311 /* If a filelist was specified, read the filelist from it. */
8312 if (filelist != NULL) {
8313 fp = fopen(filelist, "r");
8315 fprintf(stderr, "Cannot read the file list %s: %s\n",
8316 filelist, strerror(errno));
8321 while ((rc = getline(&line, &len, fp)) != -1) {
8322 /* If allocated buffer was too small, get something
8324 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8327 nbfile_alloc = nbfile_alloc * 2 + 1;
8329 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8332 fprintf(stderr, "hsm: cannot allocate "
8333 "the request: %s\n",
8340 size = hur_len(oldhur);
8342 fprintf(stderr, "hsm: cannot allocate "
8343 "%u files + %u bytes data\n",
8344 oldhur->hur_request.hr_itemcount,
8345 oldhur->hur_request.hr_data_len);
8352 memcpy(hur, oldhur, size);
8357 if (line[strlen(line) - 1] == '\n')
8358 line[strlen(line) - 1] = '\0';
8360 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8361 mntpath, line, &last_dev);
8367 if (some_file == NULL) {
8377 /* If a --data was used, add it to the request */
8378 hur->hur_request.hr_data_len = opaque_len;
8380 memcpy(hur_data(hur), opaque, opaque_len);
8382 /* Send the HSM request */
8383 if (realpath(some_file, fullpath) == NULL) {
8384 fprintf(stderr, "Could not find path '%s': %s\n",
8385 some_file, strerror(errno));
8387 rc = llapi_hsm_request(fullpath, hur);
8389 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8390 some_file, strerror(-rc));
8400 static int lfs_hsm_archive(int argc, char **argv)
8402 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8405 static int lfs_hsm_restore(int argc, char **argv)
8407 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8410 static int lfs_hsm_release(int argc, char **argv)
8412 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8415 static int lfs_hsm_remove(int argc, char **argv)
8417 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8420 static int lfs_hsm_cancel(int argc, char **argv)
8422 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8425 static int lfs_swap_layouts(int argc, char **argv)
8430 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8431 SWAP_LAYOUTS_KEEP_MTIME |
8432 SWAP_LAYOUTS_KEEP_ATIME);
8435 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8437 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8439 int lfs_get_mode(const char *string)
8441 enum lock_mode_user mode;
8443 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8444 if (lock_mode_names[mode] == NULL)
8446 if (strcmp(string, lock_mode_names[mode]) == 0)
8453 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8455 enum lu_ladvise_type advice;
8458 advice < ARRAY_SIZE(ladvise_names); advice++) {
8459 if (ladvise_names[advice] == NULL)
8461 if (strcmp(string, ladvise_names[advice]) == 0)
8465 return LU_LADVISE_INVALID;
8468 static int lfs_ladvise(int argc, char **argv)
8470 struct option long_opts[] = {
8471 { .val = 'a', .name = "advice", .has_arg = required_argument },
8472 { .val = 'b', .name = "background", .has_arg = no_argument },
8473 { .val = 'e', .name = "end", .has_arg = required_argument },
8474 { .val = 'l', .name = "length", .has_arg = required_argument },
8475 { .val = 'm', .name = "mode", .has_arg = required_argument },
8476 { .val = 's', .name = "start", .has_arg = required_argument },
8477 { .val = 'u', .name = "unset", .has_arg = no_argument },
8479 char short_opts[] = "a:be:l:m:s:u";
8484 struct llapi_lu_ladvise advice;
8485 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8486 unsigned long long start = 0;
8487 unsigned long long end = LUSTRE_EOF;
8488 unsigned long long length = 0;
8489 unsigned long long size_units;
8490 unsigned long long flags = 0;
8494 while ((c = getopt_long(argc, argv, short_opts,
8495 long_opts, NULL)) != -1) {
8498 advice_type = lfs_get_ladvice(optarg);
8499 if (advice_type == LU_LADVISE_INVALID) {
8500 fprintf(stderr, "%s: invalid advice type "
8501 "'%s'\n", argv[0], optarg);
8502 fprintf(stderr, "Valid types:");
8504 for (advice_type = 0;
8505 advice_type < ARRAY_SIZE(ladvise_names);
8507 if (ladvise_names[advice_type] == NULL)
8509 fprintf(stderr, " %s",
8510 ladvise_names[advice_type]);
8512 fprintf(stderr, "\n");
8525 rc = llapi_parse_size(optarg, &end,
8528 fprintf(stderr, "%s: bad end offset '%s'\n",
8535 rc = llapi_parse_size(optarg, &start,
8538 fprintf(stderr, "%s: bad start offset "
8539 "'%s'\n", argv[0], optarg);
8545 rc = llapi_parse_size(optarg, &length,
8548 fprintf(stderr, "%s: bad length '%s'\n",
8554 mode = lfs_get_mode(optarg);
8556 fprintf(stderr, "%s: bad mode '%s', valid "
8557 "modes are READ or WRITE\n",
8565 fprintf(stderr, "%s: option '%s' unrecognized\n",
8566 argv[0], argv[optind - 1]);
8571 if (advice_type == LU_LADVISE_INVALID) {
8572 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8573 fprintf(stderr, "Valid types:");
8574 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8576 if (ladvise_names[advice_type] == NULL)
8578 fprintf(stderr, " %s", ladvise_names[advice_type]);
8580 fprintf(stderr, "\n");
8584 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8585 fprintf(stderr, "%s: Lock no expand advice is a per file "
8586 "descriptor advice, so when called from lfs, "
8587 "it does nothing.\n", argv[0]);
8591 if (argc <= optind) {
8592 fprintf(stderr, "%s: please give one or more file names\n",
8597 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8598 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8603 if (end == LUSTRE_EOF && length != 0)
8604 end = start + length;
8607 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8608 argv[0], start, end);
8612 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8613 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8618 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8619 fprintf(stderr, "%s: mode is required with lockahead\n",
8624 while (optind < argc) {
8627 path = argv[optind++];
8629 fd = open(path, O_RDONLY);
8631 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8632 argv[0], path, strerror(errno));
8637 advice.lla_start = start;
8638 advice.lla_end = end;
8639 advice.lla_advice = advice_type;
8640 advice.lla_value1 = 0;
8641 advice.lla_value2 = 0;
8642 advice.lla_value3 = 0;
8643 advice.lla_value4 = 0;
8644 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8645 advice.lla_lockahead_mode = mode;
8646 advice.lla_peradvice_flags = flags;
8649 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8652 fprintf(stderr, "%s: cannot give advice '%s' to file "
8653 "'%s': %s\n", argv[0],
8654 ladvise_names[advice_type],
8655 path, strerror(errno));
8661 if (rc == 0 && rc2 < 0)
8667 static const char *const heat_names[] = LU_HEAT_NAMES;
8669 static int lfs_heat_get(int argc, char **argv)
8671 struct lu_heat *heat;
8680 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8682 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8687 while (optind < argc) {
8688 path = argv[optind++];
8690 fd = open(path, O_RDONLY);
8692 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8693 argv[0], path, strerror(errno));
8698 heat->lh_count = OBD_HEAT_COUNT;
8699 rc2 = llapi_heat_get(fd, heat);
8702 fprintf(stderr, "%s: cannot get heat of file '%s'"
8703 ": %s\n", argv[0], path, strerror(errno));
8707 printf("flags: %x\n", heat->lh_flags);
8708 for (i = 0; i < heat->lh_count; i++)
8709 printf("%s: %llu\n", heat_names[i],
8710 (unsigned long long)heat->lh_heat[i]);
8712 if (rc == 0 && rc2 < 0)
8720 static int lfs_heat_set(int argc, char **argv)
8722 struct option long_opts[] = {
8723 { .val = 'c', .name = "clear", .has_arg = no_argument },
8724 { .val = 'o', .name = "off", .has_arg = no_argument },
8725 { .val = 'O', .name = "on", .has_arg = no_argument },
8727 enum lu_heat_flag flags = 0;
8737 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8740 flags |= LU_HEAT_FLAG_CLEAR;
8743 flags |= LU_HEAT_FLAG_CLEAR;
8744 flags |= LU_HEAT_FLAG_OFF;
8747 flags &= ~LU_HEAT_FLAG_OFF;
8752 fprintf(stderr, "%s: option '%s' unrecognized\n",
8753 argv[0], argv[optind - 1]);
8758 if (argc <= optind) {
8759 fprintf(stderr, "%s: please give one or more file names\n",
8764 while (optind < argc) {
8765 path = argv[optind++];
8767 fd = open(path, O_RDONLY);
8769 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8770 argv[0], path, strerror(errno));
8775 rc2 = llapi_heat_set(fd, flags);
8778 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8779 ": %s\n", argv[0], path, strerror(errno));
8783 if (rc == 0 && rc2 < 0)
8789 /** The input string contains a comma delimited list of component ids and
8790 * ranges, for example "1,2-4,7".
8792 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8794 bool end_of_loop = false;
8802 while (!end_of_loop) {
8806 char *endptr = NULL;
8809 ptr = strchrnul(arg, ',');
8810 end_of_loop = *ptr == '\0';
8813 start_index = strtol(arg, &endptr, 0);
8814 if (endptr == arg) /* no data at all */
8816 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8818 if (start_index < 0)
8821 end_index = start_index;
8822 if (*endptr == '-') {
8823 end_index = strtol(endptr + 1, &endptr, 0);
8824 if (*endptr != '\0')
8826 if (end_index < start_index)
8830 for (i = start_index; i <= end_index && size > 0; i++) {
8833 /* remove duplicate */
8834 for (j = 0; j < nr; j++) {
8838 if (j == nr) { /* no duplicate */
8844 if (size == 0 && i < end_index)
8851 if (!end_of_loop && ptr != NULL)
8854 return rc < 0 ? rc : nr;
8858 * struct verify_mirror_id - Mirror id to be verified.
8859 * @mirror_id: A specified mirror id.
8860 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8862 struct verify_mirror_id {
8868 * compare_mirror_ids() - Compare mirror ids.
8869 * @layout: Mirror component list.
8870 * @cbdata: Callback data in verify_mirror_id structure.
8872 * This is a callback function called by llapi_layout_comp_iterate()
8873 * to compare the specified mirror id with the one in the current
8874 * component of @layout. If they are the same, then the specified
8875 * mirror id is valid.
8877 * Return: a negative error code on failure or
8878 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8879 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8882 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8884 struct verify_mirror_id *mirror_id_cbdata =
8885 (struct verify_mirror_id *)cbdata;
8889 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8893 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8894 progname, strerror(errno));
8898 if (mirror_id_cbdata->mirror_id == mirror_id) {
8899 mirror_id_cbdata->is_valid_id = true;
8900 return LLAPI_LAYOUT_ITER_STOP;
8903 return LLAPI_LAYOUT_ITER_CONT;
8907 * verify_mirror_ids() - Verify specified mirror ids.
8908 * @fname: Mirrored file name.
8909 * @mirror_ids: Specified mirror ids to be verified.
8910 * @ids_nr: Number of specified mirror ids.
8912 * This function verifies that specified @mirror_ids are valid
8913 * in the mirrored file @fname.
8915 * Return: 0 on success or a negative error code on failure.
8918 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8920 struct llapi_layout *layout = NULL;
8921 struct verify_mirror_id mirror_id_cbdata = { 0 };
8932 if (stat(fname, &stbuf) < 0) {
8933 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8934 progname, fname, strerror(errno));
8939 if (!S_ISREG(stbuf.st_mode)) {
8940 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8946 fd = open(fname, O_DIRECT | O_RDONLY);
8948 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8949 progname, fname, strerror(errno));
8954 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8956 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8957 progname, fname, strerror(errno));
8961 layout = llapi_layout_get_by_fd(fd, 0);
8962 if (layout == NULL) {
8963 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8964 progname, fname, strerror(errno));
8966 llapi_lease_release(fd);
8970 rc = llapi_layout_flags_get(layout, &flr_state);
8972 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8973 progname, fname, strerror(errno));
8978 flr_state &= LCM_FL_FLR_MASK;
8979 switch (flr_state) {
8982 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8983 progname, fname, llapi_layout_flags_string(flr_state));
8990 for (i = 0; i < ids_nr; i++) {
8991 mirror_id_cbdata.mirror_id = mirror_ids[i];
8992 mirror_id_cbdata.is_valid_id = false;
8994 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8999 "%s: '%s' failed to verify mirror id: %u.\n",
9000 progname, fname, mirror_ids[i]);
9004 if (!mirror_id_cbdata.is_valid_id) {
9007 "%s: '%s' invalid specified mirror id: %u.\n",
9008 progname, fname, mirror_ids[i]);
9014 llapi_layout_free(layout);
9015 llapi_lease_release(fd);
9023 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9024 __u16 *mirror_ids, int ids_nr)
9026 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9027 struct llapi_layout *layout;
9038 if (stat(fname, &stbuf) < 0) {
9039 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9040 progname, fname, strerror(errno));
9044 if (!S_ISREG(stbuf.st_mode)) {
9045 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9051 fd = open(fname, O_DIRECT | O_RDWR);
9053 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9054 progname, fname, strerror(errno));
9059 layout = llapi_layout_get_by_fd(fd, 0);
9060 if (layout == NULL) {
9061 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9062 progname, fname, strerror(errno));
9067 rc = llapi_layout_flags_get(layout, &flr_state);
9069 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9070 progname, fname, strerror(errno));
9075 flr_state &= LCM_FL_FLR_MASK;
9076 if (flr_state == LCM_FL_NONE) {
9078 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9083 /* get stale component info */
9084 comp_size = llapi_mirror_find_stale(layout, comp_array,
9085 ARRAY_SIZE(comp_array),
9086 mirror_ids, ids_nr);
9087 if (comp_size <= 0) {
9092 ioc->lil_mode = LL_LEASE_WRLCK;
9093 ioc->lil_flags = LL_LEASE_RESYNC;
9094 rc = llapi_lease_set(fd, ioc);
9096 if (rc == -EALREADY)
9100 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9101 progname, fname, strerror(-rc));
9105 /* get the read range [start, end) */
9106 start = comp_array[0].lrc_start;
9107 end = comp_array[0].lrc_end;
9108 for (idx = 1; idx < comp_size; idx++) {
9109 if (comp_array[idx].lrc_start < start)
9110 start = comp_array[idx].lrc_start;
9111 if (end < comp_array[idx].lrc_end)
9112 end = comp_array[idx].lrc_end;
9115 rc = llapi_lease_check(fd);
9116 if (rc != LL_LEASE_WRLCK) {
9117 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9122 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9125 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9126 progname, fname, strerror(-rc));
9128 /* need to do the lease unlock even resync fails */
9129 ioc->lil_mode = LL_LEASE_UNLCK;
9130 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9132 for (idx = 0; idx < comp_size; idx++) {
9133 if (comp_array[idx].lrc_synced) {
9134 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9139 rc2 = llapi_lease_set(fd, ioc);
9141 * llapi_lease_set returns lease mode when it request to unlock
9145 /* rc2 == 0 means lost lease lock */
9146 if (rc2 == 0 && rc == 0)
9148 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9150 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9154 llapi_layout_free(layout);
9161 static inline int lfs_mirror_resync(int argc, char **argv)
9163 struct ll_ioc_lease *ioc = NULL;
9164 __u16 mirror_ids[128] = { 0 };
9169 struct option long_opts[] = {
9170 { .val = 'o', .name = "only", .has_arg = required_argument },
9173 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9176 rc = parse_mirror_ids(mirror_ids,
9177 sizeof(mirror_ids) / sizeof(__u16),
9181 "%s: bad mirror ids '%s'.\n",
9188 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9189 argv[0], argv[optind - 1]);
9195 if (argc == optind) {
9196 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9201 if (ids_nr > 0 && argc > optind + 1) {
9203 "%s: option '--only' cannot be used upon multiple files.\n",
9211 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9216 /* set the lease on the file */
9217 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9219 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9220 argv[0], strerror(errno));
9225 for (; optind < argc; optind++) {
9226 rc = lfs_mirror_resync_file(argv[optind], ioc,
9227 mirror_ids, ids_nr);
9228 /* ignore previous file's error, continue with next file */
9231 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9239 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9241 struct llapi_layout *layout;
9244 layout = llapi_layout_get_by_fd(fd, 0);
9245 if (layout == NULL) {
9246 fprintf(stderr, "could not get layout.\n");
9250 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9252 fprintf(stderr, "failed to iterate layout\n");
9253 llapi_layout_free(layout);
9256 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9257 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9258 llapi_layout_free(layout);
9262 llapi_layout_free(layout);
9268 * Check whether two files are the same file
9269 * \retval 0 same file
9270 * \retval 1 not the same file
9271 * \retval <0 error code
9273 static inline int check_same_file(const char *f1, const char *f2)
9278 if (stat(f1, &stbuf1) < 0) {
9279 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9280 progname, f1, strerror(errno));
9284 if (stat(f2, &stbuf2) < 0) {
9285 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9286 progname, f2, strerror(errno));
9290 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9291 stbuf1.st_ino == stbuf2.st_ino)
9297 static inline int lfs_mirror_read(int argc, char **argv)
9300 __u16 mirror_id = 0;
9301 const char *outfile = NULL;
9307 const size_t buflen = 4 << 20;
9309 struct option long_opts[] = {
9310 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9311 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9314 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9319 mirror_id = strtoul(optarg, &end, 0);
9320 if (*end != '\0' || mirror_id == 0) {
9322 "%s %s: invalid mirror ID '%s'\n",
9323 progname, argv[0], optarg);
9331 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9332 progname, argv[optind - 1]);
9337 if (argc == optind) {
9338 fprintf(stderr, "%s %s: no mirrored file provided\n",
9341 } else if (argc > optind + 1) {
9342 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9346 if (mirror_id == 0) {
9347 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9352 /* open mirror file */
9353 fname = argv[optind];
9356 rc = check_same_file(fname, outfile);
9359 "%s %s: output file cannot be the mirrored file\n",
9367 fd = open(fname, O_DIRECT | O_RDONLY);
9369 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9370 progname, argv[0], fname, strerror(errno));
9374 /* verify mirror id */
9375 rc = verify_mirror_id_by_fd(fd, mirror_id);
9378 "%s %s: cannot find mirror with ID %u in '%s'\n",
9379 progname, argv[0], mirror_id, fname);
9383 /* open output file */
9385 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9387 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9388 progname, argv[0], outfile, strerror(errno));
9393 outfd = STDOUT_FILENO;
9396 /* allocate buffer */
9397 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9399 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9400 progname, argv[0], rc);
9407 ssize_t written = 0;
9409 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9410 if (bytes_read < 0) {
9413 "%s %s: fail to read data from mirror %u: %s\n",
9414 progname, argv[0], mirror_id, strerror(-rc));
9419 if (bytes_read == 0)
9422 while (written < bytes_read) {
9425 written2 = write(outfd, buf + written,
9426 bytes_read - written);
9429 "%s %s: fail to write %s: %s\n",
9430 progname, argv[0], outfile ? : "STDOUT",
9435 written += written2;
9438 if (written != bytes_read) {
9440 "%s %s: written %ld bytes does not match with %ld read.\n",
9441 progname, argv[0], written, bytes_read);
9463 static inline int lfs_mirror_write(int argc, char **argv)
9466 __u16 mirror_id = 0;
9467 const char *inputfile = NULL;
9473 const size_t buflen = 4 << 20;
9475 size_t page_size = sysconf(_SC_PAGESIZE);
9476 struct ll_ioc_lease_id ioc;
9478 struct option long_opts[] = {
9479 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9480 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9483 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9488 mirror_id = strtoul(optarg, &end, 0);
9489 if (*end != '\0' || mirror_id == 0) {
9491 "%s %s: invalid mirror ID '%s'\n",
9492 progname, argv[0], optarg);
9500 fprintf(stderr, "%s: option '%s' unrecognized\n",
9501 progname, argv[optind - 1]);
9506 if (argc == optind) {
9507 fprintf(stderr, "%s %s: no mirrored file provided\n",
9510 } else if (argc > optind + 1) {
9511 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9515 if (mirror_id == 0) {
9516 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9521 /* open mirror file */
9522 fname = argv[optind];
9525 rc = check_same_file(fname, inputfile);
9528 "%s %s: input file cannot be the mirrored file\n",
9536 fd = open(fname, O_DIRECT | O_WRONLY);
9538 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9539 progname, argv[0], fname, strerror(errno));
9543 /* verify mirror id */
9544 rc = verify_mirror_id_by_fd(fd, mirror_id);
9547 "%s %s: cannot find mirror with ID %u in '%s'\n",
9548 progname, argv[0], mirror_id, fname);
9552 /* open input file */
9554 inputfd = open(inputfile, O_RDONLY, 0644);
9556 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9557 progname, argv[0], inputfile, strerror(errno));
9562 inputfd = STDIN_FILENO;
9565 /* allocate buffer */
9566 rc = posix_memalign(&buf, page_size, buflen);
9568 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9569 progname, argv[0], rc);
9573 /* prepare target mirror components instantiation */
9574 ioc.lil_mode = LL_LEASE_WRLCK;
9575 ioc.lil_flags = LL_LEASE_RESYNC;
9576 ioc.lil_mirror_id = mirror_id;
9577 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9580 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9581 progname, argv[0], fname, strerror(errno));
9591 rc = llapi_lease_check(fd);
9592 if (rc != LL_LEASE_WRLCK) {
9593 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9594 progname, argv[0], fname);
9598 bytes_read = read(inputfd, buf, buflen);
9599 if (bytes_read < 0) {
9602 "%s %s: fail to read data from '%s': %s\n",
9603 progname, argv[0], inputfile ? : "STDIN",
9610 if (bytes_read == 0)
9613 /* round up to page align to make direct IO happy. */
9614 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9616 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9621 "%s %s: fail to write to mirror %u: %s\n",
9622 progname, argv[0], mirror_id,
9630 if (pos & (page_size - 1)) {
9631 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9636 ioc.lil_mode = LL_LEASE_UNLCK;
9637 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9639 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9644 "%s %s: release lease lock of '%s' failed: %s\n",
9645 progname, argv[0], fname, strerror(errno));
9662 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9664 struct llapi_layout *layout;
9665 struct collect_ids_data cid = { .cid_ids = ids,
9667 .cid_exclude = exclude_id, };
9670 layout = llapi_layout_get_by_fd(fd, 0);
9671 if (layout == NULL) {
9672 fprintf(stderr, "could not get layout\n");
9676 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9678 fprintf(stderr, "failed to iterate layout\n");
9679 llapi_layout_free(layout);
9683 llapi_layout_free(layout);
9685 return cid.cid_count;
9688 static inline int lfs_mirror_copy(int argc, char **argv)
9691 __u16 read_mirror_id = 0;
9692 __u16 ids[128] = { 0 };
9694 struct llapi_layout *layout = NULL;
9695 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9702 struct ll_ioc_lease *ioc = NULL;
9703 struct ll_ioc_lease_id *resync_ioc;
9705 struct option long_opts[] = {
9706 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9707 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9710 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9715 read_mirror_id = strtoul(optarg, &end, 0);
9716 if (*end != '\0' || read_mirror_id == 0) {
9718 "%s %s: invalid read mirror ID '%s'\n",
9719 progname, argv[0], optarg);
9724 if (!strcmp(optarg, "-1")) {
9725 /* specify all other mirrors */
9729 count = parse_mirror_ids((__u16 *)ids,
9737 fprintf(stderr, "%s: option '%s' unrecognized\n",
9738 progname, argv[optind - 1]);
9743 if (argc == optind) {
9744 fprintf(stderr, "%s %s: no mirrored file provided\n",
9747 } else if (argc > optind + 1) {
9748 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9752 if (read_mirror_id == 0) {
9754 "%s %s: no valid read mirror ID %d is provided\n",
9755 progname, argv[0], read_mirror_id);
9761 "%s %s: no write mirror ID is provided\n",
9766 for (i = 0; i < count; i++) {
9767 if (read_mirror_id == ids[i]) {
9769 "%s %s: read and write mirror ID cannot be the same\n",
9775 /* open mirror file */
9776 fname = argv[optind];
9778 fd = open(fname, O_DIRECT | O_RDWR);
9780 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9781 progname, argv[0], fname, strerror(errno));
9785 /* write to all other mirrors */
9786 if (ids[0] == (__u16)-1) {
9787 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9791 "%s %s: failed to get other mirror ids in '%s': %d\n",
9792 progname, argv[0], fname, rc);
9797 /* verify mirror id */
9798 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9801 "%s %s: cannot find mirror with ID %u in '%s'\n",
9802 progname, argv[0], read_mirror_id, fname);
9806 for (i = 0; i < count; i++) {
9807 rc = verify_mirror_id_by_fd(fd, ids[i]);
9810 "%s %s: cannot find mirror with ID %u in '%s'\n",
9811 progname, argv[0], ids[i], fname);
9816 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9819 "%s %s: cannot alloc comp id array for ioc: %s\n",
9820 progname, argv[0], strerror(errno));
9825 /* get stale component info */
9826 layout = llapi_layout_get_by_fd(fd, 0);
9827 if (layout == NULL) {
9828 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9829 progname, argv[0], fname, strerror(errno));
9833 comp_size = llapi_mirror_find_stale(layout, comp_array,
9834 ARRAY_SIZE(comp_array),
9836 llapi_layout_free(layout);
9837 if (comp_size < 0) {
9842 /* prepare target mirror components instantiation */
9843 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9844 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9845 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9847 resync_ioc->lil_mirror_id = ids[0];
9849 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9850 rc = llapi_lease_set(fd, ioc);
9853 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9854 progname, argv[0], fname, strerror(errno));
9858 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9861 fprintf(stderr, "%s %s: copy error: %d\n",
9862 progname, argv[0], rc);
9866 fprintf(stdout, "mirror copied successfully: ");
9867 for (i = 0; i < copied; i++)
9868 fprintf(stdout, "%d ", ids[i]);
9869 fprintf(stdout, "\n");
9871 ioc->lil_mode = LL_LEASE_UNLCK;
9872 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9874 for (i = 0; i < comp_size; i++) {
9877 for (j = 0; j < copied; j++) {
9878 if (comp_array[i].lrc_mirror_id != ids[j])
9881 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9885 rc = llapi_lease_set(fd, ioc);
9890 "%s %s: release lease lock of '%s' failed: %s\n",
9891 progname, argv[0], fname, strerror(errno));
9905 * struct verify_chunk - Mirror chunk to be verified.
9906 * @chunk: [start, end) of the chunk.
9907 * @mirror_count: Number of mirror ids in @mirror_id array.
9908 * @mirror_id: Array of valid mirror ids that cover the chunk.
9910 struct verify_chunk {
9911 struct lu_extent chunk;
9912 unsigned int mirror_count;
9913 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9917 * print_chunks() - Print chunk information.
9918 * @fname: Mirrored file name.
9919 * @chunks: Array of chunks.
9920 * @chunk_count: Number of chunks in @chunks array.
9922 * This function prints [start, end) of each chunk in @chunks
9923 * for mirrored file @fname, and also prints the valid mirror ids
9924 * that cover the chunk.
9929 void print_chunks(const char *fname, struct verify_chunk *chunks,
9935 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9936 for (i = 0; i < chunk_count; i++) {
9937 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9939 if (chunks[i].mirror_count == 0)
9940 fprintf(stdout, "\t[");
9942 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9943 for (j = 1; j < chunks[i].mirror_count; j++)
9944 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9946 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9948 fprintf(stdout, "\n");
9952 * print_checksums() - Print CRC-32 checksum values.
9953 * @chunk: A chunk and its corresponding valid mirror ids.
9954 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9956 * This function prints CRC-32 checksum values on @chunk for
9957 * each valid mirror that covers it.
9962 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9967 "CRC-32 checksum value for chunk "DEXT":\n",
9968 PEXT(&chunk->chunk));
9969 for (i = 0; i < chunk->mirror_count; i++)
9970 fprintf(stdout, "Mirror %u:\t%#lx\n",
9971 chunk->mirror_id[i], crc[i]);
9972 fprintf(stdout, "\n");
9976 * filter_mirror_id() - Filter specified mirror ids.
9977 * @chunks: Array of chunks.
9978 * @chunk_count: Number of chunks in @chunks array.
9979 * @mirror_ids: Specified mirror ids to be verified.
9980 * @ids_nr: Number of specified mirror ids.
9982 * This function scans valid mirror ids that cover each chunk in @chunks
9983 * and filters specified mirror ids.
9988 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9989 __u16 *mirror_ids, int ids_nr)
9994 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9995 unsigned int valid_count = 0;
9997 for (i = 0; i < chunk_count; i++) {
9998 if (chunks[i].mirror_count == 0)
10002 for (j = 0; j < ids_nr; j++) {
10003 for (k = 0; k < chunks[i].mirror_count; k++) {
10004 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10005 valid_id[valid_count] = mirror_ids[j];
10012 memcpy(chunks[i].mirror_id, valid_id,
10013 sizeof(__u16) * valid_count);
10014 chunks[i].mirror_count = valid_count;
10019 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10020 * @layout: Mirror component list.
10021 * @chunks: Array of chunks.
10022 * @chunks_size: Array size of @chunks.
10024 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10025 * to find out chunk segments and store them in @chunks array.
10027 * The @mirror_id array in each element of @chunks will store the valid
10028 * mirror ids that cover the chunk. If a mirror component covering the
10029 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10030 * will not be stored into the @mirror_id array, and the chunk for that
10031 * mirror will not be verified.
10033 * The @mirror_count in each element of @chunks will store the number of
10034 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10035 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10036 * indicates the chunk is valid in only one mirror. In both cases, the
10037 * chunk will not be verified.
10039 * Here is an example:
10041 * 0 1M 2M 3M 4M EOF
10042 * +------+-------------+--------------------+
10043 * | | | S | mirror1
10044 * +------+------+------+------+-------------+
10045 * | | S | S | | mirror2
10046 * +-------------+------+------+-------------+
10048 * prepared @chunks array will contain 5 elements:
10049 * (([0, 1M), [1, 2], 2),
10050 * ([1M, 2M), [1, 2], 2),
10051 * ([2M, 3M), [1], 1),
10052 * ([3M, 4M], [], 0),
10053 * ([4M, EOF), [2], 1))
10055 * Return: the actual array size of @chunks on success
10056 * or a negative error code on failure.
10059 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10060 struct verify_chunk *chunks,
10061 size_t chunks_size)
10065 uint32_t mirror_id;
10071 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10074 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10077 "%s: move to the first layout component: %s.\n",
10078 progname, strerror(errno));
10084 chunks[idx].chunk.e_end = LUSTRE_EOF;
10086 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10089 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10090 progname, strerror(errno));
10094 if (start > chunks[idx].chunk.e_start ||
10095 end <= chunks[idx].chunk.e_start)
10098 if (end < chunks[idx].chunk.e_end)
10099 chunks[idx].chunk.e_end = end;
10101 rc = llapi_layout_comp_flags_get(layout, &flags);
10104 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10105 progname, strerror(errno));
10109 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10112 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10115 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10116 progname, strerror(errno));
10120 chunks[idx].mirror_id[i] = mirror_id;
10122 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10124 "%s: mirror_id array is too small.\n",
10131 rc = llapi_layout_comp_use(layout,
10132 LLAPI_LAYOUT_COMP_USE_NEXT);
10135 "%s: move to the next layout component: %s.\n",
10136 progname, strerror(errno));
10139 } /* loop through all components */
10141 chunks[idx].mirror_count = i;
10143 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10147 if (idx >= chunks_size) {
10148 fprintf(stderr, "%s: chunks array is too small.\n",
10154 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10158 return rc < 0 ? rc : idx + 1;
10162 * lfs_mirror_verify_chunk() - Verify a chunk.
10163 * @fd: File descriptor of the mirrored file.
10164 * @file_size: Size of the mirrored file.
10165 * @chunk: A chunk and its corresponding valid mirror ids.
10166 * @verbose: Verbose mode.
10168 * This function verifies a @chunk contains exactly the same data
10169 * ammong the mirrors that cover it.
10171 * If @verbose is specified, then the function will print where the
10172 * differences are if the data do not match. Otherwise, it will
10173 * just return an error in that case.
10175 * Return: 0 on success or a negative error code on failure.
10178 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10179 struct verify_chunk *chunk, int verbose)
10181 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10183 size_t page_size = sysconf(_SC_PAGESIZE);
10184 ssize_t bytes_read;
10185 ssize_t bytes_done;
10189 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10193 if (file_size == 0)
10196 rc = posix_memalign(&buf, page_size, buflen);
10197 if (rc) /* error code is returned directly */
10201 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10202 PEXT(&chunk->chunk));
10203 for (i = 0; i < chunk->mirror_count; i++)
10204 fprintf(stdout, " %u", chunk->mirror_id[i]);
10205 fprintf(stdout, "\n");
10209 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10210 pos = chunk->chunk.e_start;
10211 while (bytes_done < count) {
10212 /* compute initial CRC-32 checksum */
10213 crc = crc32(0L, Z_NULL, 0);
10214 memset(crc_array, 0, sizeof(crc_array));
10217 for (i = 0; i < chunk->mirror_count; i++) {
10218 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10220 if (bytes_read < 0) {
10223 "%s: failed to read data from mirror %u: %s.\n",
10224 progname, chunk->mirror_id[i],
10229 /* compute new CRC-32 checksum */
10230 crc_array[i] = crc32(crc, buf, bytes_read);
10234 print_checksums(chunk, crc_array);
10236 /* compare CRC-32 checksum values */
10237 for (i = 1; i < chunk->mirror_count; i++) {
10238 if (crc_array[i] != crc_array[0]) {
10242 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10243 progname, PEXT(&chunk->chunk),
10244 chunk->mirror_id[0],
10245 chunk->mirror_id[i]);
10250 bytes_done += bytes_read;
10253 if (verbose > 1 && rc == 0) {
10254 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10255 PEXT(&chunk->chunk));
10256 for (i = 0; i < chunk->mirror_count; i++)
10257 fprintf(stdout, " %u", chunk->mirror_id[i]);
10258 fprintf(stdout, " PASS\n\n");
10267 * lfs_mirror_verify_file() - Verify a mirrored file.
10268 * @fname: Mirrored file name.
10269 * @mirror_ids: Specified mirror ids to be verified.
10270 * @ids_nr: Number of specified mirror ids.
10271 * @verbose: Verbose mode.
10273 * This function verifies that each SYNC mirror of a mirrored file
10274 * specified by @fname contains exactly the same data.
10276 * If @mirror_ids is specified, then the function will verify the
10277 * mirrors specified by @mirror_ids contain exactly the same data.
10279 * If @verbose is specified, then the function will print where the
10280 * differences are if the data do not match. Otherwise, it will
10281 * just return an error in that case.
10283 * Return: 0 on success or a negative error code on failure.
10286 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10289 struct verify_chunk chunks_array[1024] = { };
10290 struct llapi_layout *layout = NULL;
10292 uint32_t flr_state;
10294 int chunk_count = 0;
10300 if (stat(fname, &stbuf) < 0) {
10301 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10302 progname, fname, strerror(errno));
10307 if (!S_ISREG(stbuf.st_mode)) {
10308 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10314 if (stbuf.st_size == 0) {
10316 fprintf(stdout, "%s: '%s' file size is 0.\n",
10322 fd = open(fname, O_DIRECT | O_RDONLY);
10324 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10325 progname, fname, strerror(errno));
10330 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10332 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10333 progname, fname, strerror(errno));
10337 layout = llapi_layout_get_by_fd(fd, 0);
10338 if (layout == NULL) {
10339 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10340 progname, fname, strerror(errno));
10342 llapi_lease_release(fd);
10346 rc = llapi_layout_flags_get(layout, &flr_state);
10348 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10349 progname, fname, strerror(errno));
10354 flr_state &= LCM_FL_FLR_MASK;
10355 switch (flr_state) {
10358 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10359 progname, fname, llapi_layout_flags_string(flr_state));
10365 /* find out mirror chunks to be verified */
10366 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10367 ARRAY_SIZE(chunks_array));
10368 if (chunk_count < 0) {
10374 /* filter specified mirror ids */
10375 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10378 print_chunks(fname, chunks_array, chunk_count);
10380 for (idx = 0; idx < chunk_count; idx++) {
10381 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10384 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10386 PEXT(&chunks_array[idx].chunk),
10387 (unsigned long long)stbuf.st_size);
10391 if (chunks_array[idx].mirror_count == 0) {
10393 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10395 PEXT(&chunks_array[idx].chunk));
10397 fprintf(stderr, "skipped\n");
10401 fprintf(stderr, "failed\n");
10405 if (chunks_array[idx].mirror_count == 1) {
10408 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10410 PEXT(&chunks_array[idx].chunk),
10411 chunks_array[idx].mirror_id[0]);
10415 rc = llapi_lease_check(fd);
10416 if (rc != LL_LEASE_RDLCK) {
10417 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10422 /* verify one chunk */
10423 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10424 &chunks_array[idx], verbose);
10438 llapi_layout_free(layout);
10439 llapi_lease_release(fd);
10447 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10448 * @argc: The count of lfs mirror verify command line arguments.
10449 * @argv: Array of strings for lfs mirror verify command line arguments.
10451 * This function parses lfs mirror verify command and verifies the
10452 * specified mirrored file(s).
10454 * Return: 0 on success or a negative error code on failure.
10456 static inline int lfs_mirror_verify(int argc, char **argv)
10458 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10464 char cmd[PATH_MAX];
10466 struct option long_opts[] = {
10467 { .val = 'o', .name = "only", .has_arg = required_argument },
10468 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10469 { .name = NULL } };
10471 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10473 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10476 rc = parse_mirror_ids(mirror_ids,
10477 ARRAY_SIZE(mirror_ids),
10481 "%s: bad mirror ids '%s'.\n",
10488 "%s: at least 2 mirror ids needed with '--only' option.\n",
10498 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10499 progname, argv[optind - 1]);
10505 if (argc == optind) {
10506 fprintf(stderr, "%s: no file name given.\n", progname);
10511 if (ids_nr > 0 && argc > optind + 1) {
10513 "%s: '--only' cannot be used upon multiple files.\n",
10521 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10527 for (; optind < argc; optind++) {
10528 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10538 * lfs_mirror() - Parse and execute lfs mirror commands.
10539 * @argc: The count of lfs mirror command line arguments.
10540 * @argv: Array of strings for lfs mirror command line arguments.
10542 * This function parses lfs mirror commands and performs the
10543 * corresponding functions specified in mirror_cmdlist[].
10545 * Return: 0 on success or an error code on failure.
10547 static int lfs_mirror(int argc, char **argv)
10549 char cmd[PATH_MAX];
10552 setlinebuf(stdout);
10554 Parser_init("lfs-mirror > ", mirror_cmdlist);
10556 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10558 program_invocation_short_name = cmd;
10560 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10562 rc = Parser_commands();
10564 return rc < 0 ? -rc : rc;
10567 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10569 #if __BYTE_ORDER == __BIG_ENDIAN
10570 __swab16s(&attrs->lsa_valid);
10571 __swab64s(&attrs->lsa_size);
10572 __swab64s(&attrs->lsa_blocks);
10576 enum lfs_som_type {
10577 LFS_SOM_SIZE = 0x1,
10578 LFS_SOM_BLOCKS = 0x2,
10579 LFS_SOM_FLAGS = 0x4,
10580 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10584 static int lfs_getsom(int argc, char **argv)
10587 struct lustre_som_attrs *attrs;
10588 char buf[sizeof(*attrs) + 64];
10589 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10592 while ((c = getopt(argc, argv, "sbf")) != -1) {
10595 type = LFS_SOM_SIZE;
10598 type = LFS_SOM_BLOCKS;
10601 type = LFS_SOM_FLAGS;
10604 fprintf(stderr, "%s: invalid option '%c'\n",
10614 fprintf(stderr, "%s: %s\n",
10615 progname, argc == 0 ? "miss file target" :
10616 "input more than 2 files");
10621 attrs = (void *)buf;
10622 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10625 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10626 argv[0], strerror(errno), errno);
10630 lustre_som_swab(attrs);
10633 case LFS_SOM_ATTR_ALL:
10634 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10635 path, (unsigned long long)attrs->lsa_size,
10636 (unsigned long long)attrs->lsa_blocks,
10640 printf("%llu\n", (unsigned long long)attrs->lsa_size);
10642 case LFS_SOM_BLOCKS:
10643 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
10645 case LFS_SOM_FLAGS:
10646 printf("%x\n", attrs->lsa_valid);
10649 fprintf(stderr, "%s: unknown option\n", progname);
10657 * lfs_mirror_list_commands() - List lfs mirror commands.
10658 * @argc: The count of command line arguments.
10659 * @argv: Array of strings for command line arguments.
10661 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10663 * Return: 0 on success.
10665 static int lfs_mirror_list_commands(int argc, char **argv)
10667 char buffer[81] = "";
10669 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10675 static int lfs_pcc_attach(int argc, char **argv)
10677 struct option long_opts[] = {
10678 { .val = 'i', .name = "id", .has_arg = required_argument },
10679 { .name = NULL } };
10682 __u32 archive_id = 0;
10685 char fullpath[PATH_MAX];
10686 enum lu_pcc_type type = LU_PCC_READWRITE;
10689 while ((c = getopt_long(argc, argv, "i:",
10690 long_opts, NULL)) != -1) {
10693 archive_id = strtoul(optarg, &end, 0);
10694 if (*end != '\0' || archive_id == 0) {
10695 fprintf(stderr, "error: %s: bad archive ID "
10696 "'%s'\n", argv[0], optarg);
10703 fprintf(stderr, "%s: option '%s' unrecognized\n",
10704 argv[0], argv[optind - 1]);
10709 if (archive_id == 0) {
10710 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
10714 if (argc <= optind) {
10715 fprintf(stderr, "%s: must specify one or more file names\n",
10720 while (optind < argc) {
10723 path = argv[optind++];
10724 if (realpath(path, fullpath) == NULL) {
10725 fprintf(stderr, "%s: could not find path '%s': %s\n",
10726 argv[0], path, strerror(errno));
10732 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10734 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10735 "with archive ID '%u': %s\n", argv[0],
10736 path, archive_id, strerror(-rc2));
10744 static int lfs_pcc_attach_fid(int argc, char **argv)
10746 struct option long_opts[] = {
10747 { .val = 'i', .name = "id", .has_arg = required_argument },
10748 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10749 { .name = NULL } };
10750 char short_opts[] = "i:m:";
10753 __u32 archive_id = 0;
10755 const char *mntpath = NULL;
10756 const char *fidstr;
10757 enum lu_pcc_type type = LU_PCC_READWRITE;
10760 while ((c = getopt_long(argc, argv, short_opts,
10761 long_opts, NULL)) != -1) {
10764 archive_id = strtoul(optarg, &end, 0);
10765 if (*end != '\0') {
10766 fprintf(stderr, "error: %s: bad archive ID "
10767 "'%s'\n", argv[0], optarg);
10777 fprintf(stderr, "%s: option '%s' unrecognized\n",
10778 argv[0], argv[optind - 1]);
10783 if (archive_id == 0) {
10784 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10788 if (mntpath == NULL) {
10789 fprintf(stderr, "%s: must specify Lustre mount point\n",
10794 if (argc <= optind) {
10795 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10799 while (optind < argc) {
10802 fidstr = argv[optind++];
10804 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10807 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10808 "with archive ID '%u': %s\n", argv[0],
10809 fidstr, mntpath, archive_id, strerror(rc2));
10811 if (rc == 0 && rc2 < 0)
10817 static int lfs_pcc_detach(int argc, char **argv)
10819 struct option long_opts[] = {
10820 { .val = 'k', .name = "keep", .has_arg = no_argument },
10821 { .name = NULL } };
10822 char short_opts[] = "k";
10826 char fullpath[PATH_MAX];
10827 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10830 while ((c = getopt_long(argc, argv, short_opts,
10831 long_opts, NULL)) != -1) {
10834 detach_opt = PCC_DETACH_OPT_NONE;
10839 fprintf(stderr, "%s: option '%s' unrecognized\n",
10840 argv[0], argv[optind - 1]);
10845 while (optind < argc) {
10848 path = argv[optind++];
10849 if (realpath(path, fullpath) == NULL) {
10850 fprintf(stderr, "%s: could not find path '%s': %s\n",
10851 argv[0], path, strerror(errno));
10857 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
10860 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
10861 "%s\n", argv[0], path, strerror(errno));
10869 static int lfs_pcc_detach_fid(int argc, char **argv)
10871 struct option long_opts[] = {
10872 { .val = 'k', .name = "keep", .has_arg = no_argument },
10873 { .name = NULL } };
10874 char short_opts[] = "k";
10878 const char *mntpath;
10879 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10882 while ((c = getopt_long(argc, argv, short_opts,
10883 long_opts, NULL)) != -1) {
10886 detach_opt = PCC_DETACH_OPT_NONE;
10891 fprintf(stderr, "%s: option '%s' unrecognized\n",
10892 argv[0], argv[optind - 1]);
10897 mntpath = argv[optind++];
10899 while (optind < argc) {
10902 fid = argv[optind++];
10904 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
10906 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
10907 "from PCC: %s\n", argv[0], fid, mntpath,
10916 static int lfs_pcc_state(int argc, char **argv)
10920 char fullpath[PATH_MAX];
10921 struct lu_pcc_state state;
10926 fprintf(stderr, "%s: must specify one or more file names\n",
10931 while (optind < argc) {
10934 path = argv[optind++];
10935 if (realpath(path, fullpath) == NULL) {
10936 fprintf(stderr, "%s: could not find path '%s': %s\n",
10937 argv[0], path, strerror(errno));
10943 rc2 = llapi_pcc_state_get(fullpath, &state);
10947 fprintf(stderr, "%s: cannot get PCC state of '%s': "
10948 "%s\n", argv[0], path, strerror(-rc2));
10952 printf("file: %s", path);
10953 printf(", type: %s", pcc_type2string(state.pccs_type));
10954 if (state.pccs_type == LU_PCC_NONE &&
10955 state.pccs_open_count == 0) {
10960 printf(", PCC file: %s", state.pccs_path);
10961 printf(", user number: %u", state.pccs_open_count);
10962 printf(", flags: %x", state.pccs_flags);
10969 * lfs_pcc_list_commands() - List lfs pcc commands.
10970 * @argc: The count of command line arguments.
10971 * @argv: Array of strings for command line arguments.
10973 * This function lists lfs pcc commands defined in pcc_cmdlist[].
10975 * Return: 0 on success.
10977 static int lfs_pcc_list_commands(int argc, char **argv)
10979 char buffer[81] = "";
10981 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
10988 * lfs_pcc() - Parse and execute lfs pcc commands.
10989 * @argc: The count of lfs pcc command line arguments.
10990 * @argv: Array of strings for lfs pcc command line arguments.
10992 * This function parses lfs pcc commands and performs the
10993 * corresponding functions specified in pcc_cmdlist[].
10995 * Return: 0 on success or an error code on failure.
10997 static int lfs_pcc(int argc, char **argv)
10999 char cmd[PATH_MAX];
11002 setlinebuf(stdout);
11004 Parser_init("lfs-pcc > ", pcc_cmdlist);
11006 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11008 program_invocation_short_name = cmd;
11010 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11012 rc = Parser_commands();
11014 return rc < 0 ? -rc : rc;
11017 static int lfs_list_commands(int argc, char **argv)
11019 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11021 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11026 int main(int argc, char **argv)
11030 /* Ensure that liblustreapi constructor has run */
11031 if (!llapi_liblustreapi_initialized())
11032 fprintf(stderr, "liblustreapi was not properly initialized\n");
11034 setlinebuf(stdout);
11037 Parser_init("lfs > ", cmdlist);
11039 progname = program_invocation_short_name; /* Used in error messages */
11041 llapi_set_command_name(argv[1]);
11042 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11043 llapi_clear_command_name();
11045 rc = Parser_commands();
11048 return rc < 0 ? -rc : rc;
11051 #ifdef _LUSTRE_IDL_H_
11052 /* Everything we need here should be included by lustreapi.h. */
11053 # error "lfs should not depend on lustre_idl.h"
11054 #endif /* _LUSTRE_IDL_H_ */