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 {
151 static int lfs_setstripe_internal(int argc, char **argv,
152 enum setstripe_origin opc);
154 static inline int lfs_setstripe(int argc, char **argv)
156 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
158 static inline int lfs_setstripe_migrate(int argc, char **argv)
160 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
162 static inline int lfs_mirror_create(int argc, char **argv)
164 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
166 static inline int lfs_mirror_extend(int argc, char **argv)
168 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
170 static inline int lfs_mirror_split(int argc, char **argv)
172 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
175 /* Setstripe and migrate share mostly the same parameters */
176 #define SSM_CMD_COMMON(cmd) \
177 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
178 " [--stripe-count|-c <stripe_count>]\n" \
179 " [--overstripe-count|-C <stripe_count>]\n" \
180 " [--stripe-index|-i <start_ost_idx>]\n" \
181 " [--stripe-size|-S <stripe_size>]\n" \
182 " [--layout|-L <pattern>]\n" \
183 " [--pool|-p <pool_name>]\n" \
184 " [--ost|-o <ost_indices>]\n" \
185 " [--yaml|-y <yaml_template_file>]\n" \
186 " [--copy=<lustre_src>]\n"
188 #define SSM_HELP_COMMON \
189 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
190 "\t Using -C instead of -c allows overstriping, which\n" \
191 "\t will place more than one stripe per OST if\n" \
192 "\t stripe_count is greater than the number of OSTs\n" \
193 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
194 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
195 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
196 "\t respectively)\n" \
197 "\tpool_name: Name of OST pool to use (default none)\n" \
198 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
199 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
200 "\t Indices be specified in a format of:\n" \
201 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
203 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
204 "\t If --pool is set with --ost, then the OSTs\n" \
205 "\t must be the members of the pool.\n" \
206 "\tcomp_end: Extent end of component, start after previous end.\n"\
207 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
208 "\t respectively, -1 for EOF). Must be a multiple of\n"\
209 "\t stripe_size.\n" \
210 "\tyaml_template_file:\n" \
211 "\t YAML layout template file, can't be used with -c,\n" \
212 "\t -i, -S, -p, -o, or -E arguments.\n" \
213 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
214 "\t another lustre file or directory, can't used with\n" \
215 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
217 #define MIRROR_CREATE_HELP \
218 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
219 "\t setstripe layout options\n" \
220 "\t It defaults to 1 if not specified; if specified,\n" \
221 "\t it must follow the option without a space.\n" \
222 "\t The option can also be repeated multiple times to\n" \
223 "\t separate mirrors that have different layouts.\n" \
224 "\tsetstripe options: Mirror layout\n" \
225 "\t It can be a plain layout or a composite layout.\n" \
226 "\t If not specified, the stripe options inherited\n" \
227 "\t from the previous component will be used.\n" \
228 "\tflags: set flags to the component of the current mirror.\n" \
229 "\t Only \"prefer\" flag is supported so far.\n"
231 #define MIRROR_EXTEND_HELP \
233 "\tvictim_file: The layout of victim_file will be split and used\n" \
234 "\t as a mirror added to the mirrored file.\n" \
235 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
236 "\t from victim file(s) in case the victim file(s)\n" \
237 "\t contains the same data as the original mirrored\n" \
240 #define MIRROR_EXTEND_USAGE \
241 " <--mirror-count|-N[mirror_count]>\n" \
242 " [setstripe options|-f <victim_file>]\n" \
245 #define SETSTRIPE_USAGE \
246 SSM_CMD_COMMON("setstripe") \
247 MIRROR_EXTEND_USAGE \
248 " <directory|filename>\n" \
252 #define MIGRATE_USAGE \
253 SSM_CMD_COMMON("migrate ") \
254 " [--block|-b] [--non-block|-n]\n" \
255 " [--non-direct|-D] [--verbose|-v]\n" \
259 "\tblock: Block file access during data migration (default)\n" \
260 "\tnon-block: Abort migrations if concurrent access is detected\n" \
261 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
262 "\tverbose: Print each filename as it is migrated\n" \
264 #define SETDIRSTRIPE_USAGE \
265 " [--mdt-count|-c stripe_count>\n" \
266 " [--mdt-hash|-H mdt_hash]\n" \
267 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
268 " [--default|-D] [--mode|-o mode] <dir>\n" \
269 "\tstripe_count: stripe count of the striped directory\n" \
270 "\tmdt_index: MDT index of first stripe\n" \
271 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
272 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
273 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
274 " space create subdirectories with balanced space usage\n" \
275 "\tdefault_stripe: set default dirstripe of the directory\n" \
276 "\tmode: the file access permission of the directory (octal)\n" \
277 "To create dir with a foreign (free format) layout :\n" \
278 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
279 "[--mode|-m mode] [--flags <hex>] <dir>\n" \
280 "\tmode: the mode of the directory\n" \
281 "\tforeign_type: none or daos\n"
284 * command_t mirror_cmdlist - lfs mirror commands.
286 command_t mirror_cmdlist[] = {
287 { .pc_name = "create", .pc_func = lfs_mirror_create,
288 .pc_help = "Create a mirrored file.\n"
289 "usage: lfs mirror create "
290 "<--mirror-count|-N[mirror_count]> "
291 "[setstripe options] ... <filename|directory>\n"
292 MIRROR_CREATE_HELP },
293 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
294 .pc_help = "Extend a mirrored file.\n"
295 "usage: lfs mirror extend "
296 "<--mirror-count|-N[mirror_count]> [--no-verify] "
297 "[setstripe options|-f <victim_file>] ... <filename>\n"
298 MIRROR_EXTEND_HELP },
299 { .pc_name = "split", .pc_func = lfs_mirror_split,
300 .pc_help = "Split a mirrored file.\n"
301 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
302 "\t <--component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
303 "\t [-f <new_file>] <mirrored file>\n"
304 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
305 "\t can be fetched by lfs getstripe command.\n"
306 "\tcomp_id: Unique component ID within a mirror.\n"
307 "\tpool: Components on specified pool.\n"
308 "\tnew_file: This option indicates the layout of the split\n"
309 "\t mirror will be stored into. If not specified,\n"
310 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
311 "\t will be used.\n" },
312 { .pc_name = "read", .pc_func = lfs_mirror_read,
313 .pc_help = "Read the content of a specified mirror of a file.\n"
314 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
315 "[--outfile|-o <output_file>] <mirrored_file>\n" },
316 { .pc_name = "write", .pc_func = lfs_mirror_write,
317 .pc_help = "Write to a specified mirror of a file.\n"
318 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
319 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
320 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
321 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
322 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
323 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
324 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
325 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
326 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
327 "<mirrored file> [<mirrored file2>...]\n"},
328 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
329 .pc_help = "Verify mirrored file(s).\n"
330 "usage: lfs mirror verify "
331 "[--only <mirror_id,mirror_id2[,...]>] "
332 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
333 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
334 .pc_help = "list commands supported by lfs mirror"},
335 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
336 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
337 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
342 * command_t pcc_cmdlist - lfs pcc commands.
344 command_t pcc_cmdlist[] = {
345 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
346 .pc_help = "Attach given files to the Persistent Client Cache.\n"
347 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
348 "\t-i: archive id for RW-PCC\n" },
349 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
350 .pc_help = "Attach given files into PCC by FID(s).\n"
351 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
353 "\t-i: archive id for RW-PCC\n"
354 "\t-m: Lustre mount point\n" },
355 { .pc_name = "state", .pc_func = lfs_pcc_state,
356 .pc_help = "Display the PCC state for given files.\n"
357 "usage: lfs pcc state <file> ...\n" },
358 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
359 .pc_help = "Detach given files from the Persistent Client Cache.\n"
360 "usage: lfs pcc detach <file> ...\n" },
361 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
362 .pc_help = "Detach given files from PCC by FID(s).\n"
363 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
364 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
365 .pc_help = "list commands supported by lfs pcc"},
366 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
367 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
368 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
372 /* all available commands */
373 command_t cmdlist[] = {
374 {"setstripe", lfs_setstripe, 0,
375 "To create a file with specified striping/composite layout, or\n"
376 "create/replace the default layout on an existing directory:\n"
377 SSM_CMD_COMMON("setstripe")
379 " <directory|filename>\n"
381 "To add component(s) to an existing composite file:\n"
382 SSM_CMD_COMMON("setstripe --component-add")
384 "To totally delete the default striping from an existing directory:\n"
385 "usage: setstripe -d <directory>\n"
387 "To create a mirrored file or set s default mirror layout on a directory:\n"
388 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
390 "To delete the last component(s) from an existing composite file\n"
391 "(note that this will also delete any data in those components):\n"
392 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
393 " [--component-flags|-F <comp_flags>]\n"
395 "\tcomp_id: Unique component ID to delete\n"
396 "\tcomp_flags: 'init' indicating all instantiated components\n"
397 "\t '^init' indicating all uninstantiated components\n"
398 "\t-I and -F cannot be specified at the same time\n"
399 "To create a file with a foreign (free format) layout:\n"
400 "usage: setstripe --foreign[=<foreign_type>]\n"
401 " --xattr|-x <layout_string> [--flags <hex>]\n"
402 " [--mode <mode>] <filename>\n"},
403 {"getstripe", lfs_getstripe, 0,
404 "To list the layout pattern for a given file or files in a\n"
405 "directory or recursively for all files in a directory tree.\n"
406 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
407 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
408 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
409 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
410 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
411 " [--component-id[=comp_id]|-I[comp_id]]\n"
412 " [--component-flags[=comp_flags]]\n"
413 " [--component-count]\n"
414 " [--extension-size|--ext-size|-z]\n"
415 " [--component-start[=[+-]comp_start]]\n"
416 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
417 " [[!] --mirror-index=[+-]<index> |\n"
418 " [!] --mirror-id=[+-]<id>]\n"
419 " <directory|filename> ..."},
420 {"setdirstripe", lfs_setdirstripe, 0,
421 "To create a striped directory on a specified MDT. This can only\n"
422 "be done on MDT0 with the right of administrator.\n"
423 "usage: setdirstripe [OPTION] <directory>\n"
425 {"getdirstripe", lfs_getdirstripe, 0,
426 "To list the layout pattern info for a given directory\n"
427 "or recursively for all directories in a directory tree.\n"
428 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
429 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
430 " [--recursive|-r] [--yaml|-y]\n"
431 " [--verbose|-v] [--default|-D] <dir> ..."},
432 {"mkdir", lfs_setdirstripe, 0,
433 "To create a striped directory on a specified MDT. This can only\n"
434 "be done on MDT0 with the right of administrator.\n"
435 "usage: mkdir [OPTION] <directory>\n"
437 {"rm_entry", lfs_rmentry, 0,
438 "To remove the name entry of the remote directory. Note: This\n"
439 "command will only delete the name entry, i.e. the remote directory\n"
440 "will become inaccessable after this command. This can only be done\n"
441 "by the administrator\n"
442 "usage: rm_entry <dir>\n"},
443 {"pool_list", lfs_poollist, 0,
444 "List pools or pool OSTs\n"
445 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
446 {"find", lfs_find, 0,
447 "find files matching given attributes recursively in directory tree.\n"
448 "usage: find <directory|filename> ...\n"
449 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
450 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
451 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
452 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
453 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
454 " [[!] --stripe-count|-c [+-]<stripes>]\n"
455 " [[!] --stripe-index|-i <index,...>]\n"
456 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
457 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
458 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
459 " [[!] --projid <projid>]\n"
460 " [[!] --foreign[=<foreign_type>]]\n"
461 " [[!] --layout|-L released,raid0,mdt]\n"
462 " [[!] --foreign[=<foreign_type>]]\n"
463 " [[!] --component-count [+-]<comp_cnt>]\n"
464 " [[!] --component-start [+-]N[kMGTPE]]\n"
465 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
466 " [[!] --component-flags <comp_flags>]\n"
467 " [[!] --mirror-count|-N [+-]<n>]\n"
468 " [[!] --mirror-state <[^]state>]\n"
469 " [[!] --mdt-count|-T [+-]<stripes>]\n"
470 " [[!] --mdt-hash|-H <hashtype>\n"
471 " [[!] --mdt-index|-m <uuid|index,...>]\n"
472 "\t !: used before an option indicates 'NOT' requested attribute\n"
473 "\t -: used before a value indicates less than requested value\n"
474 "\t +: used before a value indicates more than requested value\n"
475 "\thashtype: hash type of the striped directory.\n"
476 "\t fnv_1a_64 FNV-1a hash algorithm\n"
477 "\t all_char sum of characters % MDT_COUNT\n"},
478 {"check", lfs_check, 0,
479 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
480 "or all the servers (MGTs, MDTs and OSTs).\n"
481 "usage: check <mgts|osts|mdts|all>"},
482 {"osts", lfs_osts, 0, "list OSTs connected to client "
483 "[for specified path only]\n" "usage: osts [path]"},
484 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
485 "[for specified path only]\n" "usage: mdts [path]"},
487 "report filesystem disk space usage or inodes usage "
488 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
489 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
490 {"getname", lfs_getname, 0,
491 "list instances and specified mount points [for specified path only]\n"
492 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
493 #ifdef HAVE_SYS_QUOTA_H
494 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
495 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
496 " -b <block-softlimit> -B <block-hardlimit>\n"
497 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
498 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
499 " [--block-softlimit <block-softlimit>]\n"
500 " [--block-hardlimit <block-hardlimit>]\n"
501 " [--inode-softlimit <inode-softlimit>]\n"
502 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
503 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
504 " [--block-grace <block-grace>]\n"
505 " [--inode-grace <inode-grace>] <filesystem>\n"
506 " setquota <-U|-G|-P>\n"
507 " -b <block-softlimit> -B <block-hardlimit>\n"
508 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
509 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
510 " [--block-softlimit <block-softlimit>]\n"
511 " [--block-hardlimit <block-hardlimit>]\n"
512 " [--inode-softlimit <inode-softlimit>]\n"
513 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
514 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
516 " -b can be used instead of --block-softlimit/--block-grace\n"
517 " -B can be used instead of --block-hardlimit\n"
518 " -i can be used instead of --inode-softlimit/--inode-grace\n"
519 " -I can be used instead of --inode-hardlimit\n"
520 " -d can be used instead of --default\n\n"
521 "Note: The total quota space will be split into many qunits and\n"
522 " balanced over all server targets, the minimal qunit size is\n"
523 " 1M bytes for block space and 1K inodes for inode space.\n\n"
524 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
525 " Quota space rebalancing process will stop when this mininum\n"
526 " value is reached. As a result, quota exceeded can be returned\n"
527 " while many targets still have 1MB or 1K inodes of spare\n"
529 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
530 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
532 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
533 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
534 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
535 {"project", lfs_project, 0,
536 "Change or list project attribute for specified file or directory.\n"
537 "usage: project [-d|-r] <file|directory...>\n"
538 " list project ID and flags on file(s) or directories\n"
539 " project [-p id] [-s] [-r] <file|directory...>\n"
540 " set project ID and/or inherit flag for specified file(s) or directories\n"
541 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
542 " check project ID and flags on file(s) or directories, print outliers\n"
543 " project -C [-r] [-k] <file|directory...>\n"
544 " clear the project inherit flag and ID on the file or directory\n"
547 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
548 "usage: flushctx [-k] [mountpoint...]"},
549 {"changelog", lfs_changelog, 0,
550 "Show the metadata changes on an MDT."
551 "\nusage: changelog <mdtname> [startrec [endrec]]"},
552 {"changelog_clear", lfs_changelog_clear, 0,
553 "Indicate that old changelog records up to <endrec> are no longer of "
554 "interest to consumer <id>, allowing the system to free up space.\n"
555 "An <endrec> of 0 means all records.\n"
556 "usage: changelog_clear <mdtname> <id> <endrec>"},
557 {"fid2path", lfs_fid2path, 0,
558 "Resolve the full path(s) for given FID(s). For a specific hardlink "
559 "specify link number <linkno>.\n"
560 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
561 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
562 "usage: path2fid [--parents] <path> ..."},
563 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
564 "usage: rmfid <fsname|rootpath> <fid> ..."},
565 {"data_version", lfs_data_version, 0, "Display file data version for "
566 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
567 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
568 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
569 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
570 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
571 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
572 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
574 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
575 "[--archived] [--lost] <file> ..."},
576 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
577 "given files.\n" "usage: hsm_action <file> ..."},
578 {"hsm_archive", lfs_hsm_archive, 0,
579 "Archive file to external storage.\n"
580 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
582 {"hsm_restore", lfs_hsm_restore, 0,
583 "Restore file from external storage.\n"
584 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
585 {"hsm_release", lfs_hsm_release, 0,
586 "Release files from Lustre.\n"
587 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
588 {"hsm_remove", lfs_hsm_remove, 0,
589 "Remove file copy from external storage.\n"
590 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
592 " (FILE [FILE ...] | "
593 "--mntpath MOUNTPATH FID [FID ...])\n"
595 "Note: To remove an archived copy of a file already deleted from a "
597 "--mntpath option and a list of FIDs must be specified"
599 {"hsm_cancel", lfs_hsm_cancel, 0,
600 "Cancel requests related to specified files.\n"
601 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
602 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
603 "usage: swap_layouts <path1> <path2>"},
604 {"migrate", lfs_setstripe_migrate, 0,
605 "migrate a directory between MDTs.\n"
606 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
607 " [--mdt-hash|-H] <hash_type>\n"
608 " [--mdt-index|-m] <start_mdt_index>\n"
611 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
612 " it's the MDT index of first stripe\n"
613 "\tmdt_count: number of MDTs to stripe a directory over\n"
614 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
615 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
616 " all_char sum of characters % MDT_COUNT\n"
618 "migrate file objects from one OST "
619 "layout\nto another (may be not safe with concurent writes).\n"
621 "[--stripe-count|-c] <stripe_count>\n"
622 "[--overstripe-count|-C] <stripe_count>\n"
623 " [--stripe-index|-i] <start_ost_index>\n"
624 " [--stripe-size|-S] <stripe_size>\n"
625 " [--pool|-p] <pool_name>\n"
626 " [--ost|-o] <ost_indices>\n"
628 " [--non-block|-n]\n"
629 " [--non-direct|-D]\n"
630 " <file|directory>\n"
631 "\tstripe_count: number of OSTs to stripe a file over\n"
632 "\t Using -C instead of -c allows overstriping, which\n"
633 "\t will place more than one stripe per OST if\n"
634 "\t stripe_count is greater than the number of OSTs\n"
635 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
636 "\tstripe_size: number of bytes to store before moving to the next OST\n"
637 "\tpool_name: name of the predefined pool of OSTs\n"
638 "\tost_indices: OSTs to stripe over, in order\n"
639 "\tblock: Block file access during data migration (default)\n"
640 "\tnon-block: Abort migrations if concurrent access is detected\n"
641 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
643 "To move directories between MDTs. This command is deprecated, "
644 "use \"migrate\" instead.\n"
645 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
647 {"ladvise", lfs_ladvise, 0,
648 "Provide servers with advice about access patterns for a file.\n"
649 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
650 " [--background|-b] [--unset|-u]\n\n"
651 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
652 " {[--mode|-m [READ,WRITE]}\n"
654 {"mirror", lfs_mirror, mirror_cmdlist,
655 "lfs commands used to manage files with mirrored components:\n"
656 "lfs mirror create - create a mirrored file or directory\n"
657 "lfs mirror extend - add mirror(s) to an existing file\n"
658 "lfs mirror split - split a mirror from an existing mirrored file\n"
659 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
660 "lfs mirror read - read a mirror content of a mirrored file\n"
661 "lfs mirror write - write to a mirror of a mirrored file\n"
662 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
663 "lfs mirror verify - verify mirrored file(s)\n"},
664 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
665 "usage: getsom [-s] [-b] [-f] <path>\n"
666 "\t-s: Only show the size value of the SOM data for a given file\n"
667 "\t-b: Only show the blocks value of the SOM data for a given file\n"
668 "\t-f: Only show the flags value of the SOM data for a given file\n"},
669 {"heat_get", lfs_heat_get, 0,
670 "To get heat of files.\n"
671 "usage: heat_get <file> ...\n"},
672 {"heat_set", lfs_heat_set, 0,
673 "To set heat flags of files.\n"
674 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
675 "\t--clear|-c: Clear file heat for given files\n"
676 "\t--off|-o: Turn off file heat for given files\n"
677 "\t--on|-O: Turn on file heat for given files\n"},
678 {"pcc", lfs_pcc, pcc_cmdlist,
679 "lfs commands used to interact with PCC features:\n"
680 "lfs pcc attach - attach given files to Persistent Client Cache\n"
681 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
682 "lfs pcc state - display the PCC state for given files\n"
683 "lfs pcc detach - detach given files from Persistent Client Cache\n"
684 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
685 {"help", Parser_help, 0, "help"},
686 {"exit", Parser_quit, 0, "quit"},
687 {"quit", Parser_quit, 0, "quit"},
688 {"--version", Parser_version, 0,
689 "output build version of the utility and exit"},
690 {"--list-commands", lfs_list_commands, 0,
691 "list commands supported by the utility and exit"},
696 static int check_hashtype(const char *hashtype)
700 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
701 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
704 if (!strcmp(hashtype, LMV_HASH_NAME_SPACE))
705 return LMV_HASH_TYPE_DEFAULT | LMV_HASH_FLAG_SPACE;
710 static uint32_t check_foreign_type_name(const char *foreign_type_name)
714 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
715 if (lu_foreign_types[i].lft_name == NULL)
717 if (strcmp(foreign_type_name,
718 lu_foreign_types[i].lft_name) == 0)
719 return lu_foreign_types[i].lft_type;
722 return LU_FOREIGN_TYPE_UNKNOWN;
725 static const char *error_loc = "syserror";
728 MIGRATION_NONBLOCK = 0x0001,
729 MIGRATION_MIRROR = 0x0002,
730 MIGRATION_NONDIRECT = 0x0004,
731 MIGRATION_VERBOSE = 0x0008,
734 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
735 struct llapi_layout *layout);
738 migrate_open_files(const char *name, __u64 migration_flags,
739 const struct llapi_stripe_param *param,
740 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
747 char parent[PATH_MAX];
748 char volatile_file[PATH_MAX];
754 if (param == NULL && layout == NULL) {
755 error_loc = "layout information";
759 /* search for file directory pathname */
760 if (strlen(name) > sizeof(parent) - 1) {
761 error_loc = "source file name";
765 strncpy(parent, name, sizeof(parent));
766 ptr = strrchr(parent, '/');
768 if (getcwd(parent, sizeof(parent)) == NULL) {
769 error_loc = "getcwd";
773 if (ptr == parent) /* leading '/' */
778 /* open file, direct io */
779 /* even if the file is only read, WR mode is nedeed to allow
780 * layout swap on fd */
782 if (!(migration_flags & MIGRATION_NONDIRECT))
784 fd = open(name, rflags);
787 error_loc = "cannot open source file";
791 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
793 error_loc = "cannot get MDT index";
798 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
799 mode_t open_mode = S_IRUSR | S_IWUSR;
801 random_value = random();
802 rc = snprintf(volatile_file, sizeof(volatile_file),
803 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
804 mdt_index, random_value);
805 if (rc >= sizeof(volatile_file)) {
810 /* create, open a volatile file, use caching (ie no directio) */
812 fdv = llapi_file_open_param(volatile_file, open_flags,
815 fdv = lfs_component_create(volatile_file, open_flags,
817 } while (fdv < 0 && (rc = fdv) == -EEXIST);
820 error_loc = "cannot create volatile file";
824 /* In case the MDT does not support creation of volatile files
825 * we should try to unlink it. */
826 (void)unlink(volatile_file);
828 /* Not-owner (root?) special case.
829 * Need to set owner/group of volatile file like original.
830 * This will allow to pass related check during layout_swap.
835 error_loc = "cannot stat source file";
839 rc = fstat(fdv, &stv);
842 error_loc = "cannot stat volatile";
846 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
847 rc = fchown(fdv, st.st_uid, st.st_gid);
850 error_loc = "cannot change ownwership of volatile";
869 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
871 struct llapi_layout *layout;
872 size_t buf_size = 4 * 1024 * 1024;
881 layout = llapi_layout_get_by_fd(fd_src, 0);
882 if (layout != NULL) {
883 uint64_t stripe_size;
885 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
887 buf_size = stripe_size;
889 llapi_layout_free(layout);
892 /* Use a page-aligned buffer for direct I/O */
893 rc = posix_memalign(&buf, getpagesize(), buf_size);
898 /* read new data only if we have written all
899 * previously read data */
902 rc = check_file(fd_src);
907 rsize = read(fd_src, buf, buf_size);
919 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
938 static int migrate_copy_timestamps(int fd, int fdv)
942 if (fstat(fd, &st) == 0) {
943 struct timeval tv[2] = {
944 {.tv_sec = st.st_atime},
945 {.tv_sec = st.st_mtime}
948 return futimes(fdv, tv);
954 static int migrate_block(int fd, int fdv)
961 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
963 error_loc = "cannot get dataversion";
971 /* The grouplock blocks all concurrent accesses to the file.
972 * It has to be taken after llapi_get_data_version as it would
974 rc = llapi_group_lock(fd, gid);
976 error_loc = "cannot get group lock";
980 rc = migrate_copy_data(fd, fdv, NULL);
982 error_loc = "data copy failed";
986 /* Make sure we keep original atime/mtime values */
987 rc = migrate_copy_timestamps(fd, fdv);
989 error_loc = "timestamp copy failed";
994 * for a migration we need to check data version on file did
997 * Pass in gid=0 since we already own grouplock. */
998 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
999 SWAP_LAYOUTS_CHECK_DV1);
1000 if (rc == -EAGAIN) {
1001 error_loc = "file changed";
1003 } else if (rc < 0) {
1004 error_loc = "cannot swap layout";
1009 rc2 = llapi_group_unlock(fd, gid);
1010 if (rc2 < 0 && rc == 0) {
1011 error_loc = "unlock group lock";
1019 * Internal helper for migrate_copy_data(). Check lease and report error if
1022 * \param[in] fd File descriptor on which to check the lease.
1024 * \retval 0 Migration can keep on going.
1025 * \retval -errno Error occurred, abort migration.
1027 static int check_lease(int fd)
1031 rc = llapi_lease_check(fd);
1033 return 0; /* llapi_check_lease returns > 0 on success. */
1038 static int migrate_nonblock(int fd, int fdv)
1044 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1046 error_loc = "cannot get data version";
1050 rc = migrate_copy_data(fd, fdv, check_lease);
1052 error_loc = "data copy failed";
1056 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1058 error_loc = "cannot get data version";
1064 error_loc = "source file changed";
1068 /* Make sure we keep original atime/mtime values */
1069 rc = migrate_copy_timestamps(fd, fdv);
1071 error_loc = "timestamp copy failed";
1078 static int lfs_component_set(char *fname, int comp_id,
1079 __u32 flags, __u32 neg_flags)
1082 __u32 flags_array[2];
1087 ids[count] = comp_id;
1088 flags_array[count] = flags;
1093 if (neg_flags & LCME_FL_STALE) {
1094 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1095 "component. Please use lfs-mirror-resync(1) "
1096 "instead\n", progname);
1100 ids[count] = comp_id;
1101 flags_array[count] = neg_flags | LCME_FL_NEG;
1105 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1107 if (errno == EUCLEAN) {
1110 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1111 progname, comp_id, fname);
1114 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1115 progname, comp_id, fname, flags, neg_flags);
1122 static int lfs_component_del(char *fname, __u32 comp_id,
1123 __u32 flags, __u32 neg_flags)
1127 if (flags && neg_flags)
1130 if (!flags && neg_flags)
1131 flags = neg_flags | LCME_FL_NEG;
1133 if ((flags && comp_id) || (!flags && !comp_id))
1137 if (flags & ~LCME_KNOWN_FLAGS) {
1139 "%s setstripe: unknown flags %#x\n",
1143 } else if (comp_id > LCME_ID_MAX) {
1144 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1149 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1152 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1153 progname, comp_id, fname, strerror(errno));
1157 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1164 rc = llapi_layout_file_comp_add(fname, layout);
1166 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1167 fname, strerror(errno));
1171 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1172 struct llapi_layout *layout)
1180 fd = lstat(fname, &st);
1181 if (fd == 0 && S_ISDIR(st.st_mode))
1182 open_flags = O_DIRECTORY | O_RDONLY;
1184 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1186 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1187 S_ISDIR(st.st_mode) ?
1188 "set default composite layout for" :
1189 "create composite file",
1190 fname, strerror(errno));
1194 static int lfs_migrate(char *name, __u64 migration_flags,
1195 struct llapi_stripe_param *param,
1196 struct llapi_layout *layout)
1198 struct llapi_layout *existing;
1199 uint64_t dom_new, dom_cur;
1204 rc = migrate_open_files(name, migration_flags, param, layout,
1209 rc = llapi_layout_dom_size(layout, &dom_new);
1211 error_loc = "cannot get new layout DoM size";
1214 /* special case for migration to DOM layout*/
1215 existing = llapi_layout_get_by_fd(fd, 0);
1217 error_loc = "cannot get existing layout";
1221 rc = llapi_layout_dom_size(existing, &dom_cur);
1223 error_loc = "cannot get current layout DoM size";
1227 /* if file has DoM layout already then migration is possible to
1228 * the new layout with the same DoM component via swap layout,
1229 * if new layout used bigger DOM size, then mirroring is used
1231 if (dom_new > dom_cur) {
1232 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1235 error_loc = "cannot migrate to DOM layout";
1239 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1240 /* Blocking mode (forced if servers do not support file lease).
1241 * It is also the default mode, since we cannot distinguish
1242 * between a broken lease and a server that does not support
1243 * atomic swap/close (LU-6785) */
1244 rc = migrate_block(fd, fdv);
1248 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1250 error_loc = "cannot get lease";
1254 rc = migrate_nonblock(fd, fdv);
1256 llapi_lease_release(fd);
1260 /* Atomically put lease, swap layouts and close.
1261 * for a migration we need to check data version on file did
1263 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1265 error_loc = "cannot swap layout";
1277 fprintf(stderr, "error: %s: %s: %s: %s\n",
1278 progname, name, error_loc, strerror(-rc));
1279 else if (migration_flags & MIGRATION_VERBOSE)
1280 printf("%s\n", name);
1285 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1294 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1298 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1299 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1300 const char *comp_name = comp_flags_table[i].cfn_name;
1302 if (strcmp(name, comp_name) == 0) {
1303 *flags |= comp_flag;
1305 } else if (strncmp(name, "^", 1) == 0 &&
1306 strcmp(name + 1, comp_name) == 0) {
1307 *neg_flags |= comp_flag;
1312 llapi_printf(LLAPI_MSG_ERROR,
1313 "%s: component flag '%s' not supported\n",
1319 if (!*flags && !*neg_flags)
1322 /* don't allow to set and exclude the same flag */
1323 if (*flags & *neg_flags)
1329 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1337 if (strncmp(string, "^", 1) == 0) {
1338 *neg_state = llapi_layout_string_flags(string + 1);
1339 if (*neg_state != 0)
1342 *state = llapi_layout_string_flags(string);
1347 llapi_printf(LLAPI_MSG_ERROR,
1348 "%s: mirrored file state '%s' not supported\n",
1354 * struct mirror_args - Command-line arguments for mirror(s).
1355 * @m_count: Number of mirrors to be created with this layout.
1356 * @m_flags: Mirror level flags, only 'prefer' is supported.
1357 * @m_layout: Mirror layout.
1358 * @m_file: A victim file. Its layout will be split and used as a mirror.
1359 * @m_next: Point to the next node of the list.
1361 * Command-line arguments for mirror(s) will be parsed and stored in
1362 * a linked list that consists of this structure.
1364 struct mirror_args {
1367 struct llapi_layout *m_layout;
1369 struct mirror_args *m_next;
1373 * enum mirror_flags - Flags for extending a mirrored file.
1374 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1375 * in case the victim file(s) contains the same data as the
1376 * original mirrored file.
1377 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1378 * @MF_COMP_ID: specified component id instead of mirror id
1380 * Flags for extending a mirrored file.
1390 * mirror_create_sanity_check() - Check mirror list.
1391 * @list: A linked list that stores the mirror arguments.
1393 * This function does a sanity check on @list for creating
1396 * Return: 0 on success or a negative error code on failure.
1398 static int mirror_create_sanity_check(const char *fname,
1399 struct mirror_args *list)
1402 bool has_m_file = false;
1403 bool has_m_layout = false;
1409 struct llapi_layout *layout;
1411 layout = llapi_layout_get_by_path(fname, 0);
1414 "error: %s: file '%s' couldn't get layout\n",
1419 rc = llapi_layout_sanity(layout, false, true);
1421 llapi_layout_free(layout);
1424 llapi_layout_sanity_perror(rc);
1429 while (list != NULL) {
1430 if (list->m_file != NULL) {
1432 llapi_layout_free(list->m_layout);
1435 llapi_layout_get_by_path(list->m_file, 0);
1436 if (list->m_layout == NULL) {
1438 "error: %s: file '%s' has no layout\n",
1439 progname, list->m_file);
1443 has_m_layout = true;
1444 if (list->m_layout == NULL) {
1445 fprintf(stderr, "error: %s: no mirror layout\n",
1451 rc = llapi_layout_sanity(list->m_layout, false, true);
1453 llapi_layout_sanity_perror(rc);
1457 list = list->m_next;
1460 if (has_m_file && has_m_layout) {
1462 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1470 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1472 __u32 mirror_flags = *(__u32 *)cbdata;
1476 rc = llapi_layout_comp_flags_get(layout, &flags);
1481 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1486 return LLAPI_LAYOUT_ITER_CONT;
1490 * mirror_create() - Create a mirrored file.
1491 * @fname: The file to be created.
1492 * @mirror_list: A linked list that stores the mirror arguments.
1494 * This function creates a mirrored file @fname with the mirror(s)
1495 * from @mirror_list.
1497 * Return: 0 on success or a negative error code on failure.
1499 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1501 struct llapi_layout *layout = NULL;
1502 struct mirror_args *cur_mirror = NULL;
1503 uint16_t mirror_count = 0;
1507 rc = mirror_create_sanity_check(NULL, mirror_list);
1511 cur_mirror = mirror_list;
1512 while (cur_mirror != NULL) {
1513 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1515 &cur_mirror->m_flags);
1518 fprintf(stderr, "%s: failed to set mirror flags\n",
1523 for (i = 0; i < cur_mirror->m_count; i++) {
1524 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1527 fprintf(stderr, "error: %s: "
1528 "merge layout failed: %s\n",
1529 progname, strerror(errno));
1533 mirror_count += cur_mirror->m_count;
1534 cur_mirror = cur_mirror->m_next;
1537 if (layout == NULL) {
1538 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1542 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1545 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1546 progname, strerror(errno));
1550 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1558 llapi_layout_free(layout);
1563 * Compare files and check lease on @fd.
1565 * \retval bytes number of bytes are the same
1567 static ssize_t mirror_file_compare(int fd, int fdv)
1569 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1571 ssize_t bytes_done = 0;
1572 ssize_t bytes_read = 0;
1574 buf = malloc(buflen * 2);
1579 if (!llapi_lease_check(fd)) {
1580 bytes_done = -EBUSY;
1584 bytes_read = read(fd, buf, buflen);
1585 if (bytes_read <= 0)
1588 if (bytes_read != read(fdv, buf + buflen, buflen))
1591 /* XXX: should compute the checksum on each buffer and then
1592 * compare checksum to avoid cache collision */
1593 if (memcmp(buf, buf + buflen, bytes_read))
1596 bytes_done += bytes_read;
1604 static int mirror_extend_file(const char *fname, const char *victim_file,
1605 enum mirror_flags mirror_flags)
1610 struct stat stbuf_v;
1611 struct ll_ioc_lease *data = NULL;
1614 fd = open(fname, O_RDWR);
1616 error_loc = "open source file";
1621 fdv = open(victim_file, O_RDWR);
1623 error_loc = "open target file";
1628 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1629 error_loc = "stat source or target file";
1634 if (stbuf.st_dev != stbuf_v.st_dev) {
1635 error_loc = "stat source and target file";
1640 /* mirrors should be of the same size */
1641 if (stbuf.st_size != stbuf_v.st_size) {
1642 error_loc = "file sizes don't match";
1647 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1649 error_loc = "cannot get lease";
1653 if (!(mirror_flags & MF_NO_VERIFY)) {
1655 /* mirrors should have the same contents */
1656 ret = mirror_file_compare(fd, fdv);
1657 if (ret != stbuf.st_size) {
1658 error_loc = "file busy or contents don't match";
1659 rc = ret < 0 ? ret : -EINVAL;
1664 /* Get rid of caching pages from clients */
1665 rc = llapi_file_flush(fd);
1667 error_loc = "cannot get data version";
1671 rc = llapi_file_flush(fdv);
1673 error_loc = "cannot get data version";
1678 /* Make sure we keep original atime/mtime values */
1679 rc = migrate_copy_timestamps(fd, fdv);
1681 error_loc = "cannot copy timestamp";
1685 /* Atomically put lease, merge layouts and close. */
1686 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1688 error_loc = "memory allocation";
1691 data->lil_mode = LL_LEASE_UNLCK;
1692 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1693 data->lil_count = 1;
1694 data->lil_ids[0] = fdv;
1695 rc = llapi_lease_set(fd, data);
1697 error_loc = "cannot merge layout";
1699 } else if (rc == 0) {
1701 error_loc = "lost lease lock";
1714 (void) unlink(victim_file);
1716 fprintf(stderr, "error: %s: %s: %s: %s\n",
1717 progname, fname, error_loc, strerror(-rc));
1721 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1723 struct ll_ioc_lease *data = NULL;
1728 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1732 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1734 error_loc = "cannot get lease";
1738 rc = migrate_nonblock(fd, fdv);
1740 llapi_lease_release(fd);
1744 /* Atomically put lease, merge layouts and close. */
1745 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1747 error_loc = "memory allocation";
1750 data->lil_mode = LL_LEASE_UNLCK;
1751 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1752 data->lil_count = 1;
1753 data->lil_ids[0] = fdv;
1754 rc = llapi_lease_set(fd, data);
1756 error_loc = "cannot merge layout";
1758 } else if (rc == 0) {
1760 error_loc = "lost lease lock";
1773 fprintf(stderr, "error: %s: %s: %s: %s\n",
1774 progname, name, error_loc, strerror(-rc));
1778 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1779 enum mirror_flags mirror_flags)
1783 rc = mirror_create_sanity_check(fname, mirror_list);
1787 while (mirror_list) {
1788 if (mirror_list->m_file != NULL) {
1789 rc = mirror_extend_file(fname, mirror_list->m_file,
1792 __u32 mirror_count = mirror_list->m_count;
1794 while (mirror_count > 0) {
1795 rc = mirror_extend_layout(fname,
1796 mirror_list->m_layout);
1806 mirror_list = mirror_list->m_next;
1812 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1817 rc = llapi_layout_mirror_id_get(layout, &id);
1821 if ((__u16)id == *(__u16 *)cbdata)
1822 return LLAPI_LAYOUT_ITER_STOP;
1824 return LLAPI_LAYOUT_ITER_CONT;
1827 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1832 rc = llapi_layout_comp_id_get(layout, &id);
1836 if (id == *(__u32 *)cbdata)
1837 return LLAPI_LAYOUT_ITER_STOP;
1839 return LLAPI_LAYOUT_ITER_CONT;
1842 struct pool_to_id_cbdata {
1846 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1848 char buf[LOV_MAXPOOLNAME + 1];
1849 struct pool_to_id_cbdata *d = (void *)cbdata;
1853 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1856 if (strcmp(d->pool, buf))
1857 return LLAPI_LAYOUT_ITER_CONT;
1859 rc = llapi_layout_mirror_id_get(layout, &id);
1864 return LLAPI_LAYOUT_ITER_STOP;
1867 struct collect_ids_data {
1873 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1875 struct collect_ids_data *cid = cbdata;
1879 rc = llapi_layout_mirror_id_get(layout, &id);
1883 if ((__u16)id != cid->cid_exclude) {
1886 for (i = 0; i < cid->cid_count; i++) {
1887 /* already collected the mirror id */
1888 if (id == cid->cid_ids[i])
1889 return LLAPI_LAYOUT_ITER_CONT;
1891 cid->cid_ids[cid->cid_count] = id;
1895 return LLAPI_LAYOUT_ITER_CONT;
1899 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1900 * @mirror_id: Mirror id to be checked.
1901 * @layout: Mirror component list.
1903 * This function checks if a mirror with specified @mirror_id is the last
1904 * non-stale mirror of a layout @layout.
1906 * Return: true or false.
1909 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1911 __u16 mirror_ids[128] = { 0 };
1912 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1914 .cid_exclude = mirror_id, };
1917 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1919 for (i = 0; i < cid.cid_count; i++) {
1920 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1923 comp_size = llapi_mirror_find_stale(layout, comp_array,
1924 ARRAY_SIZE(comp_array),
1933 static int mirror_split(const char *fname, __u32 id, const char *pool,
1934 enum mirror_flags mflags, const char *victim_file)
1936 struct llapi_layout *layout;
1937 char parent[PATH_MAX];
1938 char victim[PATH_MAX];
1939 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1941 struct ll_ioc_lease *data;
1942 uint16_t mirror_count;
1947 /* check fname contains mirror with mirror_id/comp_id */
1948 layout = llapi_layout_get_by_path(fname, 0);
1951 "error %s: file '%s' couldn't get layout\n",
1956 rc = llapi_layout_sanity(layout, false, true);
1958 llapi_layout_sanity_perror(rc);
1962 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1965 "error %s: file '%s' couldn't get mirror count\n",
1969 if (mirror_count < 2) {
1971 "error %s: file '%s' has %d component, cannot split\n",
1972 progname, fname, mirror_count);
1976 if (mflags & MF_COMP_POOL) {
1977 struct pool_to_id_cbdata data = { .pool = pool };
1979 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
1982 } else if (mflags & MF_COMP_ID) {
1983 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1984 id = mirror_id_of(id);
1986 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1989 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1992 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1994 "error %s: file '%s' does not contain mirror with id %u\n",
1995 progname, fname, id);
1999 fd = open(fname, O_RDWR);
2002 "error %s: open file '%s' failed: %s\n",
2003 progname, fname, strerror(errno));
2007 /* get victim file directory pathname */
2008 if (strlen(fname) > sizeof(parent) - 1) {
2009 fprintf(stderr, "error %s: file name of '%s' too long\n",
2014 strncpy(parent, fname, sizeof(parent));
2015 ptr = strrchr(parent, '/');
2017 if (getcwd(parent, sizeof(parent)) == NULL) {
2018 fprintf(stderr, "error %s: getcwd failed: %s\n",
2019 progname, strerror(errno));
2029 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2031 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2036 if (victim_file == NULL) {
2037 /* use a temp file to store the splitted layout */
2038 if (mflags & MF_DESTROY) {
2039 if (last_non_stale_mirror(id, layout)) {
2042 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2047 fdv = llapi_create_volatile_idx(parent, mdt_index,
2048 O_LOV_DELAY_CREATE);
2050 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2052 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2055 /* user specified victim file */
2056 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2061 "error %s: create victim file failed: %s\n",
2062 progname, strerror(errno));
2066 /* get lease lock of fname */
2067 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2070 "error %s: cannot get lease of file '%s': %d\n",
2071 progname, fname, rc);
2075 /* Atomatically put lease, split layouts and close. */
2076 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2082 data->lil_mode = LL_LEASE_UNLCK;
2083 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2084 data->lil_count = 2;
2085 data->lil_ids[0] = fdv;
2086 data->lil_ids[1] = id;
2087 rc = llapi_lease_set(fd, data);
2089 if (rc == 0) /* lost lease lock */
2092 "error %s: cannot split '%s': %s\n",
2093 progname, fname, strerror(-rc));
2104 llapi_layout_free(layout);
2109 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2110 __u16 *mirror_ids, int ids_nr);
2112 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2113 __u64 migration_flags,
2114 struct llapi_stripe_param *param,
2115 struct llapi_layout *layout)
2117 struct ll_ioc_lease *data = NULL;
2120 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2122 error_loc = "cannot get lease";
2126 /* Atomically put lease, merge layouts, resync and close. */
2127 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2129 error_loc = "memory allocation";
2132 data->lil_mode = LL_LEASE_UNLCK;
2133 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2134 data->lil_count = 1;
2135 data->lil_ids[0] = fdv;
2136 rc = llapi_lease_set(fd, data);
2138 error_loc = "cannot merge layout";
2140 } else if (rc == 0) {
2142 error_loc = "lost lease lock";
2148 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2150 error_loc = "cannot resync file";
2154 /* delete first mirror now */
2155 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2157 error_loc = "cannot delete old layout";
2165 fprintf(stderr, "error: %s: %s: %s: %s\n",
2166 progname, name, error_loc, strerror(-rc));
2167 else if (migration_flags & MIGRATION_VERBOSE)
2168 printf("%s\n", name);
2175 * Parse a string containing an target index list into an array of integers.
2177 * The input string contains a comma delimited list of individual
2178 * indices and ranges, for example "1,2-4,7". Add the indices into the
2179 * \a tgts array and remove duplicates.
2181 * \param[out] tgts array to store indices in
2182 * \param[in] size size of \a tgts array
2183 * \param[in] offset starting index in \a tgts
2184 * \param[in] arg string containing OST index list
2185 * \param[in/out] overstriping index list may contain duplicates
2187 * \retval positive number of indices in \a tgts
2188 * \retval -EINVAL unable to parse \a arg
2190 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2191 unsigned long long *pattern)
2195 int slots = size - offset;
2197 bool overstriped = false;
2203 end_of_loop = false;
2204 while (!end_of_loop) {
2205 int start_index = 0;
2208 char *endptr = NULL;
2212 ptr = strchrnul(arg, ',');
2214 end_of_loop = *ptr == '\0';
2217 start_index = strtol(arg, &endptr, 0);
2218 if (endptr == arg) /* no data at all */
2220 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2223 end_index = start_index;
2224 if (*endptr == '-') {
2225 end_index = strtol(endptr + 1, &endptr, 0);
2226 if (*endptr != '\0')
2228 if (end_index < start_index)
2232 for (i = start_index; i <= end_index && slots > 0; i++) {
2235 /* remove duplicate */
2236 for (j = 0; j < offset; j++) {
2237 if (tgts[j] == i && pattern &&
2238 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2240 else if (tgts[j] == i)
2246 if (j == offset) { /* check complete */
2252 if (slots == 0 && i < end_index)
2260 if (!end_of_loop && ptr != NULL)
2263 if (!overstriped && pattern)
2264 *pattern = LLAPI_LAYOUT_DEFAULT;
2266 return rc < 0 ? rc : nr;
2269 struct lfs_setstripe_args {
2270 unsigned long long lsa_comp_end;
2271 unsigned long long lsa_stripe_size;
2272 unsigned long long lsa_extension_size;
2273 long long lsa_stripe_count;
2274 long long lsa_stripe_off;
2275 __u32 lsa_comp_flags;
2276 __u32 lsa_comp_neg_flags;
2277 unsigned long long lsa_pattern;
2278 unsigned int lsa_mirror_count;
2280 bool lsa_first_comp;
2281 bool lsa_extension_comp;
2283 char *lsa_pool_name;
2286 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2288 unsigned int mirror_count = lsa->lsa_mirror_count;
2289 bool first_comp = lsa->lsa_first_comp;
2291 memset(lsa, 0, sizeof(*lsa));
2293 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2294 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2295 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2296 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2297 lsa->lsa_pool_name = NULL;
2299 lsa->lsa_mirror_count = mirror_count;
2300 lsa->lsa_first_comp = first_comp;
2304 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2305 * @lsa: Stripe options to be initialized and inherited.
2307 * This function initializes stripe options in @lsa and inherit
2308 * stripe_size, stripe_count and OST pool_name options.
2312 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2314 unsigned long long stripe_size;
2315 long long stripe_count;
2316 char *pool_name = NULL;
2318 stripe_size = lsa->lsa_stripe_size;
2319 stripe_count = lsa->lsa_stripe_count;
2320 pool_name = lsa->lsa_pool_name;
2322 setstripe_args_init(lsa);
2324 lsa->lsa_stripe_size = stripe_size;
2325 lsa->lsa_stripe_count = stripe_count;
2326 lsa->lsa_pool_name = pool_name;
2329 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2331 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2332 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2333 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2334 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2335 lsa->lsa_comp_end != 0);
2339 * comp_args_to_layout() - Create or extend a composite layout.
2340 * @composite: Pointer to the composite layout.
2341 * @lsa: Stripe options for the new component.
2343 * This function creates or extends a composite layout by adding a new
2344 * component with stripe options from @lsa.
2346 * Return: 0 on success or an error code on failure.
2348 static int comp_args_to_layout(struct llapi_layout **composite,
2349 struct lfs_setstripe_args *lsa,
2352 struct llapi_layout *layout = *composite;
2353 uint64_t prev_end = 0;
2358 if (layout == NULL) {
2359 layout = llapi_layout_alloc();
2360 if (layout == NULL) {
2361 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2366 *composite = layout;
2367 lsa->lsa_first_comp = true;
2371 /* Get current component extent, current component
2372 * must be the tail component. */
2373 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2375 fprintf(stderr, "Get comp extent failed. %s\n",
2380 if (lsa->lsa_first_comp)
2383 if (lsa->lsa_first_comp)
2384 rc = llapi_layout_add_first_comp(layout);
2386 rc = llapi_layout_comp_add(layout);
2388 fprintf(stderr, "Add component failed. %s\n",
2394 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2396 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2397 lsa->lsa_comp_flags, strerror(errno));
2402 uint64_t comp_end = lsa->lsa_comp_end;
2404 /* The extendable component is 0-length, so it can be removed
2405 * if there is insufficient space to extend it. */
2406 if (lsa->lsa_extension_comp)
2407 comp_end = prev_end;
2409 rc = llapi_layout_comp_extent_set(layout, prev_end,
2412 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2413 prev_end, comp_end, strerror(errno));
2417 /* reset lsa_first_comp */
2418 lsa->lsa_first_comp = false;
2420 /* Data-on-MDT component setting */
2421 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2422 /* In case of Data-on-MDT patterns the only extra option
2423 * applicable is stripe size option. */
2424 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2425 fprintf(stderr, "Option 'stripe-count' can't be "
2426 "specified with Data-on-MDT component: %lld\n",
2427 lsa->lsa_stripe_count);
2431 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2432 fprintf(stderr, "Option 'stripe-size' can't be "
2433 "specified with Data-on-MDT component: %llu\n",
2434 lsa->lsa_stripe_size);
2438 if (lsa->lsa_nr_tgts != 0) {
2439 fprintf(stderr, "Option 'ost-list' can't be specified "
2440 "with Data-on-MDT component: '%i'\n",
2445 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2446 fprintf(stderr, "Option 'stripe-offset' can't be "
2447 "specified with Data-on-MDT component: %lld\n",
2448 lsa->lsa_stripe_off);
2452 if (lsa->lsa_pool_name != 0) {
2453 fprintf(stderr, "Option 'pool' can't be specified "
2454 "with Data-on-MDT component: '%s'\n",
2455 lsa->lsa_pool_name);
2460 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2462 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2467 /* Data-on-MDT component has always single stripe up to end */
2468 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2469 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2470 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2472 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2479 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2480 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2482 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2483 rc = llapi_layout_extension_size_set(layout, size);
2485 rc = llapi_layout_stripe_size_set(layout, size);
2488 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2489 size, strerror(errno));
2493 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2495 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2496 lsa->lsa_stripe_count, strerror(errno));
2500 if (lsa->lsa_pool_name != NULL) {
2501 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2503 fprintf(stderr, "Set pool name: %s failed. %s\n",
2504 lsa->lsa_pool_name, strerror(errno));
2508 rc = llapi_layout_pool_name_set(layout, "");
2510 fprintf(stderr, "Clear pool name failed: %s\n",
2516 if (lsa->lsa_nr_tgts > 0) {
2517 if (lsa->lsa_stripe_count > 0 &&
2518 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2519 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2520 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2521 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2522 lsa->lsa_stripe_count,
2527 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2528 rc = llapi_layout_ost_index_set(layout, i,
2533 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2534 lsa->lsa_stripe_off != -1) {
2535 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2538 fprintf(stderr, "Set ost index %d failed. %s\n",
2539 i, strerror(errno));
2543 /* Create the second, virtual component of extension space */
2544 if (lsa->lsa_extension_comp) {
2545 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2546 lsa->lsa_extension_comp = false;
2553 static int build_component(struct llapi_layout **layout,
2554 struct lfs_setstripe_args *lsa, bool set_extent)
2558 rc = comp_args_to_layout(layout, lsa, set_extent);
2562 if (lsa->lsa_mirror_count > 0) {
2563 rc = llapi_layout_mirror_count_set(*layout,
2564 lsa->lsa_mirror_count);
2568 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2571 lsa->lsa_mirror_count = 0;
2577 static int build_layout_from_yaml_node(struct cYAML *node,
2578 struct llapi_layout **layout,
2579 struct lfs_setstripe_args *lsa,
2586 if (node->cy_type == CYAML_TYPE_OBJECT) {
2587 /* go deep to sub blocks */
2588 rc = build_layout_from_yaml_node(node->cy_child, layout,
2593 if (node->cy_string == NULL)
2596 string = node->cy_string;
2597 /* skip leading lmm_ if present, to simplify parsing */
2598 if (strncmp(string, "lmm_", 4) == 0)
2601 if (node->cy_type == CYAML_TYPE_STRING) {
2602 if (!strcmp(string, "lcme_extent.e_end")) {
2603 if (!strcmp(node->cy_valuestring, "EOF") ||
2604 !strcmp(node->cy_valuestring, "eof"))
2605 lsa->lsa_comp_end = LUSTRE_EOF;
2606 } else if (!strcmp(string, "pool")) {
2607 lsa->lsa_pool_name = node->cy_valuestring;
2608 } else if (!strcmp(string, "pattern")) {
2609 if (!strcmp(node->cy_valuestring, "mdt"))
2610 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2611 if (!strcmp(node->cy_valuestring,
2612 "raid0,overstriped"))
2614 LLAPI_LAYOUT_OVERSTRIPING;
2615 } else if (!strcmp(string, "lcme_flags")) {
2616 rc = comp_str2flags(node->cy_valuestring,
2617 &lsa->lsa_comp_flags,
2618 &lsa->lsa_comp_neg_flags);
2621 /* Only template flags have meaning in
2622 * the layout for a new file
2624 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2626 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2627 if (!strcmp(string, "lcm_mirror_count")) {
2628 lsa->lsa_mirror_count = node->cy_valueint;
2629 } else if (!strcmp(string, "lcme_extent.e_start")) {
2630 if (node->cy_valueint != 0 || *layout != NULL) {
2631 rc = build_component(layout, lsa, true);
2636 if (node->cy_valueint == 0)
2637 lsa->lsa_first_comp = true;
2639 /* initialize lsa */
2640 setstripe_args_init(lsa);
2641 lsa->lsa_tgts = osts;
2642 } else if (!strcmp(string, "lcme_extent.e_end")) {
2643 if (node->cy_valueint == -1)
2644 lsa->lsa_comp_end = LUSTRE_EOF;
2646 lsa->lsa_comp_end = node->cy_valueint;
2647 } else if (!strcmp(string, "stripe_count")) {
2648 lsa->lsa_stripe_count = node->cy_valueint;
2649 } else if (!strcmp(string, "stripe_size")) {
2650 lsa->lsa_stripe_size = node->cy_valueint;
2651 } else if (!strcmp(string, "stripe_offset")) {
2652 lsa->lsa_stripe_off = node->cy_valueint;
2653 } else if (!strcmp(string, "l_ost_idx")) {
2654 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2659 node = node->cy_next;
2665 static int lfs_comp_create_from_yaml(char *template,
2666 struct llapi_layout **layout,
2667 struct lfs_setstripe_args *lsa,
2670 struct cYAML *tree = NULL, *err_rc = NULL;
2673 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2675 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2676 progname, template);
2677 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2678 "can't parse", &err_rc);
2679 cYAML_print_tree2file(stderr, err_rc);
2680 cYAML_free_tree(err_rc);
2685 /* initialize lsa for plain file */
2686 setstripe_args_init(lsa);
2687 lsa->lsa_tgts = osts;
2689 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2691 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2692 progname, template);
2695 rc = build_component(layout, lsa, *layout != NULL);
2697 /* clean clean lsa */
2698 setstripe_args_init(lsa);
2702 cYAML_free_tree(tree);
2707 * Get the extension size from the next (SEL) component and extend the
2708 * current component on it. The start of the next component is to be
2711 * \param[in] layout the current layout
2712 * \param[in] start the start of the current component
2713 * \param[in,out] end the end of the current component
2714 * \param[in] offset the offset to adjust the end position to instead of
2717 * \retval 0 - extended successfully
2718 * \retval < 0 - error
2720 static int layout_extend_comp(struct llapi_layout *layout,
2721 uint64_t start, uint64_t *end,
2724 uint64_t size, next_start, next_end;
2727 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2729 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2730 "%s\n", progname, strerror(errno));
2734 /* Even if the @size will not be used below, this will fail if
2735 * this is not a SEL component - a good confirmation we are
2736 * working on right components. */
2737 rc = llapi_layout_extension_size_get(layout, &size);
2739 fprintf(stderr, "%s setstripe: cannot get component ext size: "
2740 "%s\n", progname, strerror(errno));
2744 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2746 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2747 progname, strerror(errno));
2751 next_start += offset ?: size;
2752 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2754 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2755 progname, strerror(errno));
2759 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2761 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2762 "%s\n", progname, strerror(errno));
2766 *end += offset ?: size;
2767 rc = llapi_layout_comp_extent_set(layout, start, *end);
2769 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2770 progname, strerror(errno));
2778 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2779 * end of the last component in the existing file, and adjust the
2780 * first extent start of the components to be added accordingly.
2782 * In the create mode, we need to check if the first component is an extendable
2783 * SEL component and extend its length to the extension size (first component
2784 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2786 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2789 struct llapi_layout *head;
2790 uint64_t start = 0, prev_end = 0;
2799 head = llapi_layout_get_by_path(fname, 0);
2802 "%s setstripe: cannot read layout from '%s': "
2803 "%s\n", progname, fname, strerror(errno));
2805 } else if (errno == ENODATA) {
2806 /* file without LOVEA, this component-add will be turned
2807 * into a component-create. */
2808 llapi_layout_free(head);
2811 /* the new layout will be added to an empty one, it
2812 * still needs to be adjusted below */
2815 } else if (!llapi_layout_is_composite(head)) {
2816 fprintf(stderr, "%s setstripe: '%s' not a composite "
2817 "file\n", progname, fname);
2818 llapi_layout_free(head);
2822 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2824 fprintf(stderr, "%s setstripe: cannot get prev "
2825 "extent: %s\n", progname, strerror(errno));
2826 llapi_layout_free(head);
2830 llapi_layout_free(head);
2834 /* Make sure we use the first component of the layout to be added. */
2835 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2838 "%s setstripe: cannot move component cursor: %s\n",
2839 progname, strerror(errno));
2843 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2845 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2846 progname, strerror(errno));
2850 if (start == 0 && end == 0) {
2851 rc = layout_extend_comp(layout, start, &end,
2852 comp_add ? prev_end : 0);
2857 if (start > prev_end || end < prev_end) {
2858 fprintf(stderr, "%s setstripe: first extent [%lu, %lu) not "
2859 "adjacent with extent end %lu\n",
2860 progname, start, end, prev_end);
2864 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2866 fprintf(stderr, "%s setstripe: cannot set component extent "
2868 progname, prev_end, end, strerror(errno));
2875 static int mirror_adjust_first_extents(struct mirror_args *list)
2882 while (list != NULL) {
2883 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
2886 list = list->m_next;
2892 static inline bool arg_is_eof(char *arg)
2894 return !strncmp(arg, "-1", strlen("-1")) ||
2895 !strncmp(arg, "EOF", strlen("EOF")) ||
2896 !strncmp(arg, "eof", strlen("eof"));
2900 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2902 * Return: Valid mirror_args pointer on success and
2903 * NULL if memory allocation fails.
2905 static struct mirror_args *lfs_mirror_alloc(void)
2907 struct mirror_args *mirror = NULL;
2910 mirror = calloc(1, sizeof(*mirror));
2921 * lfs_mirror_free() - Free memory allocated for a mirror argument
2923 * @mirror: Previously allocated mirror argument structure by
2924 * lfs_mirror_alloc().
2926 * Free memory allocated for @mirror.
2930 static void lfs_mirror_free(struct mirror_args *mirror)
2932 if (mirror->m_layout != NULL)
2933 llapi_layout_free(mirror->m_layout);
2938 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2939 * @mirror_list: Previously allocated mirror list.
2941 * Free memory allocated for @mirror_list.
2945 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2947 struct mirror_args *next_mirror = NULL;
2949 while (mirror_list != NULL) {
2950 next_mirror = mirror_list->m_next;
2951 lfs_mirror_free(mirror_list);
2952 mirror_list = next_mirror;
2964 LFS_COMP_NO_VERIFY_OPT,
2966 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2968 LFS_MIRROR_STATE_OPT,
2970 LFS_MIRROR_INDEX_OPT,
2971 LFS_LAYOUT_FOREIGN_OPT,
2976 static int lfs_setstripe_internal(int argc, char **argv,
2977 enum setstripe_origin opc)
2979 struct lfs_setstripe_args lsa = { 0 };
2980 struct llapi_stripe_param *param = NULL;
2981 struct find_param migrate_mdt_param = {
2991 unsigned long long size_units = 1;
2992 bool migrate_mode = false;
2993 bool migrate_mdt_mode = false;
2994 bool setstripe_mode = false;
2995 bool migration_block = false;
2996 __u64 migration_flags = 0;
2997 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2998 int comp_del = 0, comp_set = 0;
3001 struct llapi_layout *layout = NULL;
3002 struct llapi_layout **lpp = &layout;
3003 bool mirror_mode = false;
3004 bool has_m_file = false;
3005 __u32 mirror_count = 0;
3006 enum mirror_flags mirror_flags = 0;
3007 struct mirror_args *mirror_list = NULL;
3008 struct mirror_args *new_mirror = NULL;
3009 struct mirror_args *last_mirror = NULL;
3010 __u16 mirror_id = 0;
3012 bool from_yaml = false;
3013 bool from_copy = false;
3014 char *template = NULL;
3015 bool foreign_mode = false;
3017 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3018 char *mode_opt = NULL;
3019 mode_t previous_umask = 0;
3020 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3022 struct option long_opts[] = {
3023 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3024 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3025 /* --block is only valid in migrate mode */
3026 { .val = 'b', .name = "block", .has_arg = no_argument },
3027 { .val = LFS_COMP_ADD_OPT,
3028 .name = "comp-add", .has_arg = no_argument },
3029 { .val = LFS_COMP_ADD_OPT,
3030 .name = "component-add", .has_arg = no_argument },
3031 { .val = LFS_COMP_DEL_OPT,
3032 .name = "comp-del", .has_arg = no_argument },
3033 { .val = LFS_COMP_DEL_OPT,
3034 .name = "component-del", .has_arg = no_argument },
3035 { .val = LFS_COMP_FLAGS_OPT,
3036 .name = "comp-flags", .has_arg = required_argument },
3037 { .val = LFS_COMP_FLAGS_OPT,
3038 .name = "component-flags",
3039 .has_arg = required_argument },
3040 { .val = LFS_COMP_SET_OPT,
3041 .name = "comp-set", .has_arg = no_argument },
3042 { .val = LFS_COMP_SET_OPT,
3043 .name = "component-set",
3044 .has_arg = no_argument},
3045 { .val = LFS_COMP_NO_VERIFY_OPT,
3046 .name = "no-verify", .has_arg = no_argument},
3047 { .val = LFS_LAYOUT_FLAGS_OPT,
3048 .name = "flags", .has_arg = required_argument},
3049 { .val = LFS_LAYOUT_FOREIGN_OPT,
3050 .name = "foreign", .has_arg = optional_argument},
3051 { .val = LFS_MIRROR_ID_OPT,
3052 .name = "mirror-id", .has_arg = required_argument},
3053 { .val = LFS_MODE_OPT,
3054 .name = "mode", .has_arg = required_argument},
3055 { .val = LFS_LAYOUT_COPY,
3056 .name = "copy", .has_arg = required_argument},
3057 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3058 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3059 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3060 { .val = 'C', .name = "overstripe-count",
3061 .has_arg = required_argument},
3062 { .val = 'd', .name = "delete", .has_arg = no_argument},
3063 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3064 /* --non-direct is only valid in migrate mode */
3065 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3066 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3067 { .val = 'E', .name = "component-end",
3068 .has_arg = required_argument},
3069 { .val = 'f', .name = "file", .has_arg = required_argument },
3070 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3071 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3072 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3073 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3074 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3075 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3076 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3077 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3078 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3079 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3080 { .val = 'L', .name = "layout", .has_arg = required_argument },
3081 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3082 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3083 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3084 /* --non-block is only valid in migrate mode */
3085 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3086 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3087 { .val = 'o', .name = "ost", .has_arg = required_argument },
3088 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3089 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3090 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3092 { .val = 'p', .name = "pool", .has_arg = required_argument },
3093 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3094 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3095 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3096 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3097 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3098 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3099 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3100 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3101 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3102 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3103 /* --verbose is only valid in migrate mode */
3104 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3105 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3106 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3107 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3108 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3111 setstripe_args_init(&lsa);
3113 migrate_mode = (opc == SO_MIGRATE);
3114 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3115 setstripe_mode = (opc == SO_SETSTRIPE);
3117 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3119 while ((c = getopt_long(argc, argv,
3120 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3121 long_opts, NULL)) >= 0) {
3127 case LFS_COMP_ADD_OPT:
3130 case LFS_COMP_DEL_OPT:
3133 case LFS_COMP_FLAGS_OPT:
3134 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3135 &lsa.lsa_comp_neg_flags);
3138 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3139 fprintf(stderr, "%s: inverted flags are not supported\n",
3144 case LFS_COMP_SET_OPT:
3147 case LFS_COMP_NO_VERIFY_OPT:
3148 mirror_flags |= MF_NO_VERIFY;
3150 case LFS_MIRROR_ID_OPT:
3151 mirror_id = strtoul(optarg, &end, 0);
3152 if (*end != '\0' || mirror_id == 0) {
3154 "%s %s: invalid mirror ID '%s'\n",
3155 progname, argv[0], optarg);
3159 case LFS_LAYOUT_FLAGS_OPT: {
3162 /* check for numeric flags (foreign and mirror cases) */
3163 if (setstripe_mode && !mirror_mode && !last_mirror) {
3164 flags = strtoul(optarg, &end, 16);
3167 "%s %s: bad flags '%s'\n",
3168 progname, argv[0], optarg);
3174 if (!mirror_mode || !last_mirror) {
3175 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
3180 result = comp_str2flags(optarg, &last_mirror->m_flags,
3186 fprintf(stderr, "%s: inverted flags are not supported\n",
3191 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3193 "%s: unsupported mirror flags: %s\n",
3200 case LFS_LAYOUT_FOREIGN_OPT:
3201 if (optarg != NULL) {
3202 /* check pure numeric */
3203 type = strtoul(optarg, &end, 0);
3206 type = check_foreign_type_name(optarg);
3207 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3209 "%s %s: unrecognized foreign type '%s'\n",
3216 foreign_mode = true;
3220 if (mode_opt != NULL) {
3221 mode = strtoul(mode_opt, &end, 8);
3224 "%s %s: bad mode '%s'\n",
3225 progname, argv[0], mode_opt);
3228 previous_umask = umask(0);
3231 case LFS_LAYOUT_COPY:
3236 if (!migrate_mode) {
3238 "%s %s: -b|--block valid only for migrate command\n",
3242 migration_block = true;
3245 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3248 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3251 "%s %s: invalid stripe count '%s'\n",
3252 progname, argv[0], optarg);
3256 if (lsa.lsa_stripe_count == -1)
3257 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3260 /* delete the default striping pattern */
3262 if (opc == SO_MIRROR_SPLIT) {
3265 "%s %s: -d cannot used with -f\n",
3269 mirror_flags |= MF_DESTROY;
3273 if (!migrate_mode) {
3275 "%s %s: -D|--non-direct is valid "
3276 "only for migrate command\n",
3280 migration_flags |= MIGRATION_NONDIRECT;
3283 if (lsa.lsa_comp_end != 0) {
3284 result = comp_args_to_layout(lpp, &lsa, true);
3286 fprintf(stderr, "%s: invalid layout\n",
3291 setstripe_args_init_inherit(&lsa);
3294 if (arg_is_eof(optarg)) {
3295 lsa.lsa_comp_end = LUSTRE_EOF;
3297 result = llapi_parse_size(optarg,
3302 "%s %s: invalid component end '%s'\n",
3303 progname, argv[0], optarg);
3309 if (!migrate_mode) {
3310 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3314 lsa.lsa_pattern = check_hashtype(optarg);
3315 if (lsa.lsa_pattern == 0) {
3317 "%s %s: bad stripe hash type '%s'\n",
3318 progname, argv[0], optarg);
3323 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3326 "%s %s: invalid stripe offset '%s'\n",
3327 progname, argv[0], optarg);
3330 if (lsa.lsa_stripe_off == -1)
3331 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3334 comp_id = strtoul(optarg, &end, 0);
3335 if (*end != '\0' || comp_id == 0 ||
3336 comp_id > LCME_ID_MAX) {
3338 "%s %s: invalid component ID '%s'\n",
3339 progname, argv[0], optarg);
3344 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3346 "error: %s: invalid option: %s\n",
3347 progname, argv[optopt + 1]);
3350 if (opc == SO_MIRROR_EXTEND) {
3351 if (last_mirror == NULL) {
3353 "error: %s: '-N' must exist in front of '%s'\n",
3354 progname, argv[optopt + 1]);
3357 last_mirror->m_file = optarg;
3358 last_mirror->m_count = 1;
3361 if (mirror_list == NULL)
3362 mirror_list = lfs_mirror_alloc();
3363 mirror_list->m_file = optarg;
3368 if (strcmp(argv[optind - 1], "mdt") == 0) {
3369 /* Can be only the first component */
3370 if (layout != NULL) {
3372 fprintf(stderr, "error: 'mdt' layout "
3373 "can be only the first one\n");
3376 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3378 fprintf(stderr, "error: 'mdt' layout "
3379 "size is too big\n");
3382 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3383 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3385 fprintf(stderr, "error: layout '%s' is "
3386 "unknown, supported layouts are: "
3387 "'mdt', 'raid0'\n", argv[optind]);
3392 if (!migrate_mode) {
3394 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3398 migrate_mdt_mode = true;
3399 lsa.lsa_nr_tgts = parse_targets(tgts,
3400 sizeof(tgts) / sizeof(__u32),
3401 lsa.lsa_nr_tgts, optarg, NULL);
3402 if (lsa.lsa_nr_tgts < 0) {
3404 "%s %s: invalid MDT target(s) '%s'\n",
3405 progname, argv[0], optarg);
3409 lsa.lsa_tgts = tgts;
3410 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3411 lsa.lsa_stripe_off = tgts[0];
3414 if (!migrate_mode) {
3416 "%s %s: -n|--non-block valid only for migrate command\n",
3420 migration_flags |= MIGRATION_NONBLOCK;
3423 if (opc == SO_SETSTRIPE) {
3424 opc = SO_MIRROR_CREATE;
3428 if (optarg != NULL) {
3429 mirror_count = strtoul(optarg, &end, 0);
3430 if (*end != '\0' || mirror_count == 0) {
3432 "error: %s: bad mirror count: %s\n",
3439 new_mirror = lfs_mirror_alloc();
3440 new_mirror->m_count = mirror_count;
3442 if (mirror_list == NULL)
3443 mirror_list = new_mirror;
3445 if (last_mirror != NULL) {
3446 /* wrap up last mirror */
3447 if (lsa.lsa_comp_end == 0)
3448 lsa.lsa_comp_end = LUSTRE_EOF;
3450 result = comp_args_to_layout(lpp, &lsa, true);
3452 lfs_mirror_free(new_mirror);
3456 setstripe_args_init_inherit(&lsa);
3458 last_mirror->m_next = new_mirror;
3461 last_mirror = new_mirror;
3462 lpp = &last_mirror->m_layout;
3465 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3466 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3467 fprintf(stderr, "warning: '--ost-list' is "
3468 "deprecated, use '--ost' instead\n");
3470 /* -o allows overstriping, and must note it because
3471 * parse_targets is shared with MDT striping, which
3472 * does not allow duplicates
3474 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3475 lsa.lsa_nr_tgts = parse_targets(tgts,
3476 sizeof(tgts) / sizeof(__u32),
3477 lsa.lsa_nr_tgts, optarg,
3479 if (lsa.lsa_nr_tgts < 0) {
3481 "%s %s: invalid OST target(s) '%s'\n",
3482 progname, argv[0], optarg);
3486 lsa.lsa_tgts = tgts;
3487 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3488 lsa.lsa_stripe_off = tgts[0];
3493 lsa.lsa_pool_name = optarg;
3495 if (strlen(lsa.lsa_pool_name) == 0 ||
3496 strncmp(lsa.lsa_pool_name, "none",
3497 LOV_MAXPOOLNAME) == 0)
3498 lsa.lsa_pool_name = NULL;
3501 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3505 "%s %s: invalid stripe size '%s'\n",
3506 progname, argv[0], optarg);
3511 if (!migrate_mode) {
3513 "%s %s: -v|--verbose valid only for migrate command\n",
3517 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3518 migration_flags = MIGRATION_VERBOSE;
3528 result = llapi_parse_size(optarg,
3529 &lsa.lsa_extension_size,
3533 "%s %s: invalid extension size '%s'\n",
3534 progname, argv[0], optarg);
3538 lsa.lsa_extension_comp = true;
3541 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3542 progname, argv[0], argv[optind - 1]);
3547 fname = argv[optind];
3549 if (optind == argc) {
3550 fprintf(stderr, "%s %s: FILE must be specified\n",
3555 if (xattr && !foreign_mode) {
3556 /* only print a warning as this is harmless and will be ignored
3559 "%s %s: xattr has been specified for non-foreign layout\n",
3561 } else if (foreign_mode && !xattr) {
3563 "%s %s: xattr must be provided in foreign mode\n",
3568 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3569 comp_set || comp_id || delete || from_copy ||
3570 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3573 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3578 if (mirror_mode && mirror_count == 0) {
3580 "error: %s: --mirror-count|-N option is required\n",
3587 if (lsa.lsa_comp_end == 0)
3588 lsa.lsa_comp_end = LUSTRE_EOF;
3591 if (lsa.lsa_comp_end != 0) {
3592 result = comp_args_to_layout(lpp, &lsa, true);
3594 fprintf(stderr, "error: %s: invalid layout\n",
3601 if (mirror_flags & MF_NO_VERIFY) {
3602 if (opc != SO_MIRROR_EXTEND) {
3604 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3608 } else if (!has_m_file) {
3610 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3617 if (comp_set && !comp_id) {
3618 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3623 if ((delete + comp_set + comp_del + comp_add) > 1) {
3625 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3630 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3631 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3633 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3638 if ((comp_set || comp_del) &&
3639 (setstripe_args_specified(&lsa) || layout != NULL)) {
3641 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3646 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3648 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3653 if (comp_add || comp_del) {
3656 result = lstat(fname, &st);
3657 if (result == 0 && S_ISDIR(st.st_mode)) {
3659 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3665 fprintf(stderr, "error: %s: can't use --component-add "
3666 "or --component-del for mirror operation\n",
3673 if (layout == NULL) {
3675 "%s %s: option -E must be specified with --component-add\n",
3681 if (layout != NULL || mirror_list != NULL) {
3683 result = mirror_adjust_first_extents(mirror_list);
3685 result = layout_adjust_first_extent(fname, layout,
3687 if (result == -ENODATA)
3689 else if (result != 0) {
3690 fprintf(stderr, "error: %s: invalid layout\n",
3696 if (from_yaml && from_copy) {
3698 "%s: can't specify --yaml and --copy together\n",
3703 if ((from_yaml || from_copy) &&
3704 (setstripe_args_specified(&lsa) || layout != NULL)) {
3705 fprintf(stderr, "error: %s: can't specify --yaml with "
3706 "-c, -S, -i, -o, -p or -E options.\n",
3711 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3713 "%s %s: options --non-block and --block are mutually exclusive\n",
3718 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3721 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3726 if (migrate_mdt_mode) {
3727 struct lmv_user_md *lmu;
3729 /* initialize migrate mdt parameters */
3730 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3731 LMV_USER_MAGIC_SPECIFIC));
3734 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3735 progname, argv[0], strerror(ENOMEM));
3739 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3740 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3741 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3743 "%s %s: migrate should specify MDT index\n",
3748 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3749 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3750 lmu->lum_hash_type = lsa.lsa_pattern;
3752 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3753 if (lsa.lsa_pool_name) {
3754 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3755 sizeof(lmu->lum_pool_name) - 1);
3756 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3758 if (lsa.lsa_nr_tgts > 1) {
3761 if (lsa.lsa_stripe_count > 0 &&
3762 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3763 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3765 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3766 progname, lsa.lsa_stripe_count,
3772 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3773 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3774 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3775 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3777 lmu->lum_magic = LMV_USER_MAGIC;
3780 migrate_mdt_param.fp_lmv_md = lmu;
3781 migrate_mdt_param.fp_migrate = 1;
3782 } else if (layout == NULL) {
3783 /* initialize stripe parameters */
3784 param = calloc(1, offsetof(typeof(*param),
3785 lsp_osts[lsa.lsa_nr_tgts]));
3786 if (param == NULL) {
3788 "%s %s: cannot allocate memory for parameters: %s\n",
3789 progname, argv[0], strerror(ENOMEM));
3794 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3795 param->lsp_stripe_size = lsa.lsa_stripe_size;
3796 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3797 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3798 param->lsp_stripe_count = -1;
3800 param->lsp_stripe_count = lsa.lsa_stripe_count;
3802 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3803 param->lsp_stripe_offset = -1;
3805 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3806 param->lsp_stripe_pattern =
3807 llapi_pattern_to_lov(lsa.lsa_pattern);
3808 if (param->lsp_stripe_pattern == EINVAL) {
3809 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3814 param->lsp_pool = lsa.lsa_pool_name;
3815 param->lsp_is_specific = false;
3816 if (lsa.lsa_nr_tgts > 0) {
3817 if (lsa.lsa_stripe_count > 0 &&
3818 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3819 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3820 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3822 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3823 argv[0], lsa.lsa_stripe_count,
3829 param->lsp_is_specific = true;
3830 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3831 memcpy(param->lsp_osts, tgts,
3832 sizeof(*tgts) * lsa.lsa_nr_tgts);
3837 /* generate a layout from a YAML template */
3838 result = lfs_comp_create_from_yaml(template, &layout,
3841 fprintf(stderr, "error: %s: can't create composite "
3842 "layout from template file %s\n",
3846 } else if (from_copy) {
3847 layout = llapi_layout_get_by_path(template, 0);
3848 if (layout == NULL) {
3850 "%s: can't create composite layout from file %s.\n",
3851 progname, template);
3856 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3857 if (migrate_mdt_mode) {
3858 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3859 } else if (migrate_mode) {
3860 result = lfs_migrate(fname, migration_flags, param,
3862 } else if (comp_set != 0) {
3863 result = lfs_component_set(fname, comp_id,
3865 lsa.lsa_comp_neg_flags);
3866 } else if (comp_del != 0) {
3867 result = lfs_component_del(fname, comp_id,
3869 lsa.lsa_comp_neg_flags);
3870 } else if (comp_add != 0) {
3871 result = lfs_component_add(fname, layout);
3872 } else if (opc == SO_MIRROR_CREATE) {
3873 result = mirror_create(fname, mirror_list);
3874 } else if (opc == SO_MIRROR_EXTEND) {
3875 result = mirror_extend(fname, mirror_list,
3877 } else if (opc == SO_MIRROR_SPLIT) {
3878 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3880 "%s %s: no mirror id or component id or pool name"
3881 " is specified\n", progname, argv[0]);
3884 if (lsa.lsa_pool_name)
3885 mirror_flags |= MF_COMP_POOL;
3886 else if (mirror_id != 0)
3887 comp_id = mirror_id;
3889 mirror_flags |= MF_COMP_ID;
3890 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3892 has_m_file ? mirror_list->m_file :
3894 } else if (layout != NULL) {
3895 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3901 } else if (foreign_mode) {
3902 result = llapi_file_create_foreign(fname, mode, type,
3909 result = llapi_file_open_param(fname,
3918 /* Save the first error encountered. */
3925 if (mode_opt != NULL)
3926 umask(previous_umask);
3929 free(migrate_mdt_param.fp_lmv_md);
3930 llapi_layout_free(layout);
3931 lfs_mirror_list_free(mirror_list);
3936 llapi_layout_free(layout);
3937 lfs_mirror_list_free(mirror_list);
3941 static int lfs_poollist(int argc, char **argv)
3946 return llapi_poollist(argv[1]);
3949 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3954 char *endptr = "AD";
3959 else if (str[0] == '-')
3965 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3966 long long val = strtoll(timebuf, &endptr, 0);
3971 unit *= 52; /* 52 weeks + 1 day below */
3972 case 'w': /* fallthrough */
3974 case '\0': /* days are default unit if none used */
3975 case 'd': /* fallthrough */
3976 unit = (unit + (*endptr == 'y')) * 24;
3977 case 'h': /* fallthrough */
3979 case 'm': /* fallthrough */
3981 case 's': /* fallthrough */
3983 /* don't need to multiply by 1 for seconds */
3986 "%s find: bad time string '%s': %s\n",
3987 progname, timebuf, strerror(EINVAL));
3991 if (param->fp_time_margin == 0 ||
3992 (*endptr && unit < param->fp_time_margin))
3993 param->fp_time_margin = unit;
4000 fprintf(stderr, "%s find: bad time '%s': too large\n",
4010 static int name2uid(unsigned int *id, const char *name)
4012 struct passwd *passwd;
4014 passwd = getpwnam(name);
4017 *id = passwd->pw_uid;
4022 static int name2gid(unsigned int *id, const char *name)
4024 struct group *group;
4026 group = getgrnam(name);
4029 *id = group->gr_gid;
4034 static inline int name2projid(unsigned int *id, const char *name)
4039 static int uid2name(char **name, unsigned int id)
4041 struct passwd *passwd;
4043 passwd = getpwuid(id);
4046 *name = passwd->pw_name;
4051 static inline int gid2name(char **name, unsigned int id)
4053 struct group *group;
4055 group = getgrgid(id);
4058 *name = group->gr_name;
4063 static int name2layout(__u32 *layout, char *name)
4065 char *ptr, *layout_name;
4068 for (ptr = name; ; ptr = NULL) {
4069 layout_name = strtok(ptr, ",");
4070 if (layout_name == NULL)
4072 if (strcmp(layout_name, "released") == 0)
4073 *layout |= LOV_PATTERN_F_RELEASED;
4074 else if (strcmp(layout_name, "raid0") == 0)
4075 *layout |= LOV_PATTERN_RAID0;
4076 else if (strcmp(layout_name, "mdt") == 0)
4077 *layout |= LOV_PATTERN_MDT;
4078 else if (strcmp(layout_name, "overstriping") == 0)
4079 *layout |= LOV_PATTERN_OVERSTRIPING;
4086 static int lfs_find(int argc, char **argv)
4091 struct find_param param = {
4094 .fp_time_margin = 24 * 60 * 60,
4096 struct option long_opts[] = {
4097 { .val = 'A', .name = "atime", .has_arg = required_argument },
4098 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4099 { .val = LFS_COMP_COUNT_OPT,
4100 .name = "comp-count", .has_arg = required_argument },
4101 { .val = LFS_COMP_COUNT_OPT,
4102 .name = "component-count",
4103 .has_arg = required_argument },
4104 { .val = LFS_COMP_FLAGS_OPT,
4105 .name = "comp-flags", .has_arg = required_argument },
4106 { .val = LFS_COMP_FLAGS_OPT,
4107 .name = "component-flags",
4108 .has_arg = required_argument },
4109 { .val = LFS_COMP_START_OPT,
4110 .name = "comp-start", .has_arg = required_argument },
4111 { .val = LFS_COMP_START_OPT,
4112 .name = "component-start",
4113 .has_arg = required_argument },
4114 { .val = LFS_MIRROR_STATE_OPT,
4115 .name = "mirror-state", .has_arg = required_argument },
4116 { .val = LFS_LAYOUT_FOREIGN_OPT,
4117 .name = "foreign", .has_arg = optional_argument},
4118 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4119 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4120 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4121 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4122 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4123 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4124 { .val = 'E', .name = "component-end",
4125 .has_arg = required_argument },
4126 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4127 { .val = LFS_LAYOUT_FOREIGN_OPT,
4128 .name = "foreign", .has_arg = optional_argument},
4129 { .val = 'g', .name = "gid", .has_arg = required_argument },
4130 { .val = 'G', .name = "group", .has_arg = required_argument },
4131 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4132 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4133 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4134 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4135 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4136 { .val = 'L', .name = "layout", .has_arg = required_argument },
4137 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4138 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4139 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4140 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4141 { .val = 'n', .name = "name", .has_arg = required_argument },
4142 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4143 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4144 { .val = 'O', .name = "obd", .has_arg = required_argument },
4145 { .val = 'O', .name = "ost", .has_arg = required_argument },
4146 /* no short option for pool yet, can be 'p' after 2.18 */
4147 { .val = LFS_POOL_OPT,
4148 .name = "pool", .has_arg = required_argument },
4149 { .val = '0', .name = "print0", .has_arg = no_argument },
4150 { .val = 'P', .name = "print", .has_arg = no_argument },
4151 { .val = LFS_PROJID_OPT,
4152 .name = "projid", .has_arg = required_argument },
4153 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4154 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4155 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4156 { .val = 's', .name = "size", .has_arg = required_argument },
4157 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4158 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4159 { .val = 't', .name = "type", .has_arg = required_argument },
4160 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4161 { .val = 'u', .name = "uid", .has_arg = required_argument },
4162 { .val = 'U', .name = "user", .has_arg = required_argument },
4163 { .val = 'z', .name = "extension-size",
4164 .has_arg = required_argument },
4165 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4166 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4167 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4180 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4181 while ((c = getopt_long_only(argc, argv,
4182 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4183 long_opts, NULL)) >= 0) {
4188 /* '!' is part of option */
4189 /* when getopt_long_only() finds a string which is not
4190 * an option nor a known option argument it returns 1
4191 * in that case if we already have found pathstart and pathend
4192 * (i.e. we have the list of pathnames),
4193 * the only supported value is "!"
4195 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4196 if (!isoption && pathend != -1) {
4197 fprintf(stderr, "err: %s: filename|dirname must either "
4198 "precede options or follow options\n",
4203 if (!isoption && pathstart == -1)
4204 pathstart = optind - 1;
4205 if (isoption && pathstart != -1 && pathend == -1)
4206 pathend = optind - 2;
4212 /* unknown; opt is "!" or path component,
4213 * checking done above.
4215 if (strcmp(optarg, "!") == 0)
4219 xtime = ¶m.fp_atime;
4220 xsign = ¶m.fp_asign;
4221 param.fp_exclude_atime = !!neg_opt;
4222 /* no break, this falls through to 'C' for ctime */
4225 xtime = ¶m.fp_ctime;
4226 xsign = ¶m.fp_csign;
4227 param.fp_exclude_ctime = !!neg_opt;
4229 /* no break, this falls through to 'M' for mtime */
4232 xtime = ¶m.fp_mtime;
4233 xsign = ¶m.fp_msign;
4234 param.fp_exclude_mtime = !!neg_opt;
4236 rc = set_time(¶m, &t, xtime, optarg);
4237 if (rc == LONG_MAX) {
4245 if (optarg[0] == '+') {
4246 param.fp_blocks_sign = -1;
4248 } else if (optarg[0] == '-') {
4249 param.fp_blocks_sign = 1;
4253 param.fp_blocks_units = 1024;
4254 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4255 ¶m.fp_blocks_units, 0);
4257 fprintf(stderr, "error: bad blocks '%s'\n",
4261 param.fp_check_blocks = 1;
4262 param.fp_exclude_blocks = !!neg_opt;
4264 case LFS_COMP_COUNT_OPT:
4265 if (optarg[0] == '+') {
4266 param.fp_comp_count_sign = -1;
4268 } else if (optarg[0] == '-') {
4269 param.fp_comp_count_sign = 1;
4273 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4274 if (*endptr != '\0') {
4275 fprintf(stderr, "error: bad component count "
4279 param.fp_check_comp_count = 1;
4280 param.fp_exclude_comp_count = !!neg_opt;
4282 case LFS_COMP_FLAGS_OPT:
4283 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4284 ¶m.fp_comp_neg_flags);
4286 fprintf(stderr, "error: bad component flags "
4290 param.fp_check_comp_flags = 1;
4292 __u32 flags = param.fp_comp_neg_flags;
4293 param.fp_comp_neg_flags = param.fp_comp_flags;
4294 param.fp_comp_flags = flags;
4297 case LFS_COMP_START_OPT:
4298 if (optarg[0] == '+') {
4299 param.fp_comp_start_sign = -1;
4301 } else if (optarg[0] == '-') {
4302 param.fp_comp_start_sign = 1;
4306 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4307 ¶m.fp_comp_start_units, 0);
4309 fprintf(stderr, "error: bad component start "
4313 param.fp_check_comp_start = 1;
4314 param.fp_exclude_comp_start = !!neg_opt;
4316 case LFS_MIRROR_STATE_OPT:
4317 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4318 ¶m.fp_mirror_neg_state);
4321 "error: bad mirrored file state '%s'\n",
4325 param.fp_check_mirror_state = 1;
4327 __u16 state = param.fp_mirror_neg_state;
4328 param.fp_mirror_neg_state =
4329 param.fp_mirror_state;
4330 param.fp_mirror_state = state;
4334 if (optarg[0] == '+') {
4335 param.fp_stripe_count_sign = -1;
4337 } else if (optarg[0] == '-') {
4338 param.fp_stripe_count_sign = 1;
4342 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4343 if (*endptr != '\0') {
4344 fprintf(stderr,"error: bad stripe_count '%s'\n",
4349 param.fp_check_stripe_count = 1;
4350 param.fp_exclude_stripe_count = !!neg_opt;
4353 param.fp_max_depth = strtol(optarg, 0, 0);
4356 if (optarg[0] == '+') {
4357 param.fp_comp_end_sign = -1;
4359 } else if (optarg[0] == '-') {
4360 param.fp_comp_end_sign = 1;
4364 if (arg_is_eof(optarg)) {
4365 param.fp_comp_end = LUSTRE_EOF;
4366 param.fp_comp_end_units = 1;
4369 rc = llapi_parse_size(optarg,
4371 ¶m.fp_comp_end_units, 0);
4374 fprintf(stderr, "error: bad component end "
4378 param.fp_check_comp_end = 1;
4379 param.fp_exclude_comp_end = !!neg_opt;
4381 case LFS_LAYOUT_FOREIGN_OPT: {
4382 /* all types by default */
4383 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4385 if (optarg != NULL) {
4386 /* check pure numeric */
4387 type = strtoul(optarg, &endptr, 0);
4390 type = check_foreign_type_name(optarg);
4391 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4393 "%s %s: unknown foreign type '%s'\n",
4400 param.fp_foreign_type = type;
4401 param.fp_check_foreign = 1;
4402 param.fp_exclude_foreign = !!neg_opt;
4407 rc = name2gid(¶m.fp_gid, optarg);
4409 param.fp_gid = strtoul(optarg, &endptr, 10);
4410 if (*endptr != '\0') {
4411 fprintf(stderr, "Group/GID: %s cannot "
4412 "be found.\n", optarg);
4417 param.fp_exclude_gid = !!neg_opt;
4418 param.fp_check_gid = 1;
4421 param.fp_hash_type = check_hashtype(optarg);
4422 if (param.fp_hash_type == 0) {
4423 fprintf(stderr, "error: bad hash_type '%s'\n",
4428 param.fp_check_hash_type = 1;
4429 param.fp_exclude_hash_type = !!neg_opt;
4435 ret = name2layout(¶m.fp_layout, optarg);
4438 param.fp_exclude_layout = !!neg_opt;
4439 param.fp_check_layout = 1;
4443 rc = name2uid(¶m.fp_uid, optarg);
4445 param.fp_uid = strtoul(optarg, &endptr, 10);
4446 if (*endptr != '\0') {
4447 fprintf(stderr, "User/UID: %s cannot "
4448 "be found.\n", optarg);
4453 param.fp_exclude_uid = !!neg_opt;
4454 param.fp_check_uid = 1;
4457 param.fp_pattern = (char *)optarg;
4458 param.fp_exclude_pattern = !!neg_opt;
4461 if (optarg[0] == '+') {
4462 param.fp_mirror_count_sign = -1;
4464 } else if (optarg[0] == '-') {
4465 param.fp_mirror_count_sign = 1;
4469 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4470 if (*endptr != '\0') {
4472 "error: bad mirror count '%s'\n",
4476 param.fp_check_mirror_count = 1;
4477 param.fp_exclude_mirror_count = !!neg_opt;
4482 char *buf, *token, *next, *p;
4486 buf = strdup(optarg);
4492 param.fp_exclude_obd = !!neg_opt;
4495 while (token && *token) {
4496 token = strchr(token, ',');
4503 param.fp_exclude_mdt = !!neg_opt;
4504 param.fp_num_alloc_mdts += len;
4505 tmp = realloc(param.fp_mdt_uuid,
4506 param.fp_num_alloc_mdts *
4507 sizeof(*param.fp_mdt_uuid));
4513 param.fp_mdt_uuid = tmp;
4515 param.fp_exclude_obd = !!neg_opt;
4516 param.fp_num_alloc_obds += len;
4517 tmp = realloc(param.fp_obd_uuid,
4518 param.fp_num_alloc_obds *
4519 sizeof(*param.fp_obd_uuid));
4525 param.fp_obd_uuid = tmp;
4527 for (token = buf; token && *token; token = next) {
4528 struct obd_uuid *puuid;
4531 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4534 ¶m.fp_obd_uuid[param.fp_num_obds++];
4536 p = strchr(token, ',');
4543 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4548 strncpy(puuid->uuid, token,
4549 sizeof(puuid->uuid));
4556 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4560 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4562 "Pool name %s is too long (max %d)\n",
4563 optarg, LOV_MAXPOOLNAME);
4568 * We do check for empty pool because empty pool
4569 * is used to find V1 LOV attributes
4571 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4572 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4573 param.fp_exclude_pool = !!neg_opt;
4574 param.fp_check_pool = 1;
4576 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4577 case 'p': /* want this for --pool, to match getstripe/find */
4579 "warning: -p deprecated, use --print0 or -0\n");
4582 param.fp_zero_end = 1;
4584 case 'P': /* we always print, this option is a no-op */
4586 case LFS_PROJID_OPT:
4587 rc = name2projid(¶m.fp_projid, optarg);
4589 param.fp_projid = strtoul(optarg, &endptr, 10);
4590 if (*endptr != '\0') {
4592 "Invalid project ID: %s",
4598 param.fp_exclude_projid = !!neg_opt;
4599 param.fp_check_projid = 1;
4602 if (optarg[0] == '+') {
4603 param.fp_size_sign = -1;
4605 } else if (optarg[0] == '-') {
4606 param.fp_size_sign = 1;
4610 ret = llapi_parse_size(optarg, ¶m.fp_size,
4611 ¶m.fp_size_units, 0);
4613 fprintf(stderr, "error: bad file size '%s'\n",
4617 param.fp_check_size = 1;
4618 param.fp_exclude_size = !!neg_opt;
4621 if (optarg[0] == '+') {
4622 param.fp_stripe_size_sign = -1;
4624 } else if (optarg[0] == '-') {
4625 param.fp_stripe_size_sign = 1;
4629 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4630 ¶m.fp_stripe_size_units, 0);
4632 fprintf(stderr, "error: bad stripe_size '%s'\n",
4636 param.fp_check_stripe_size = 1;
4637 param.fp_exclude_stripe_size = !!neg_opt;
4640 param.fp_exclude_type = !!neg_opt;
4641 switch (optarg[0]) {
4643 param.fp_type = S_IFBLK;
4646 param.fp_type = S_IFCHR;
4649 param.fp_type = S_IFDIR;
4652 param.fp_type = S_IFREG;
4655 param.fp_type = S_IFLNK;
4658 param.fp_type = S_IFIFO;
4661 param.fp_type = S_IFSOCK;
4664 fprintf(stderr, "error: %s: bad type '%s'\n",
4671 if (optarg[0] == '+') {
4672 param.fp_mdt_count_sign = -1;
4674 } else if (optarg[0] == '-') {
4675 param.fp_mdt_count_sign = 1;
4679 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4680 if (*endptr != '\0') {
4681 fprintf(stderr, "error: bad mdt_count '%s'\n",
4686 param.fp_check_mdt_count = 1;
4687 param.fp_exclude_mdt_count = !!neg_opt;
4690 if (optarg[0] == '+') {
4691 param.fp_ext_size_sign = -1;
4693 } else if (optarg[0] == '-') {
4694 param.fp_ext_size_sign = 1;
4698 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4699 ¶m.fp_ext_size_units, 0);
4701 fprintf(stderr, "error: bad ext-size '%s'\n",
4705 param.fp_ext_size /= SEL_UNIT_SIZE;
4706 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4707 param.fp_check_ext_size = 1;
4708 param.fp_exclude_ext_size = !!neg_opt;
4716 if (pathstart == -1) {
4717 fprintf(stderr, "error: %s: no filename|pathname\n",
4721 } else if (pathend == -1) {
4727 rc = llapi_find(argv[pathstart], ¶m);
4730 pathbad = pathstart;
4732 } while (++pathstart < pathend);
4735 fprintf(stderr, "%s: failed for '%s': %s\n",
4736 progname, argv[pathbad], strerror(-rc));
4739 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4740 free(param.fp_obd_uuid);
4742 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4743 free(param.fp_mdt_uuid);
4748 static int lfs_getstripe_internal(int argc, char **argv,
4749 struct find_param *param)
4751 struct option long_opts[] = {
4752 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4753 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4754 { .val = LFS_COMP_COUNT_OPT,
4755 .name = "comp-count", .has_arg = no_argument },
4756 { .val = LFS_COMP_COUNT_OPT,
4757 .name = "component-count", .has_arg = no_argument },
4758 { .val = LFS_COMP_FLAGS_OPT,
4759 .name = "comp-flags", .has_arg = optional_argument },
4760 { .val = LFS_COMP_FLAGS_OPT,
4761 .name = "component-flags", .has_arg = optional_argument },
4762 { .val = LFS_COMP_START_OPT,
4763 .name = "comp-start", .has_arg = optional_argument },
4764 { .val = LFS_COMP_START_OPT,
4765 .name = "component-start", .has_arg = optional_argument },
4766 { .val = LFS_MIRROR_INDEX_OPT,
4767 .name = "mirror-index", .has_arg = required_argument },
4768 { .val = LFS_MIRROR_ID_OPT,
4769 .name = "mirror-id", .has_arg = required_argument },
4770 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4771 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4772 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4773 { .val = 'd', .name = "directory", .has_arg = no_argument },
4774 { .val = 'D', .name = "default", .has_arg = no_argument },
4775 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4776 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4777 { .val = 'F', .name = "fid", .has_arg = no_argument },
4778 { .val = 'g', .name = "generation", .has_arg = no_argument },
4779 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4780 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4781 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4782 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4783 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4784 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4785 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
4786 { .val = 'L', .name = "layout", .has_arg = no_argument },
4787 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4788 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4789 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4790 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4791 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4792 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4793 { .val = 'O', .name = "obd", .has_arg = required_argument },
4794 { .val = 'O', .name = "ost", .has_arg = required_argument },
4795 { .val = 'p', .name = "pool", .has_arg = no_argument },
4796 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4797 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4798 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4799 { .val = 'R', .name = "raw", .has_arg = no_argument },
4800 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4801 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4802 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4803 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4804 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4805 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4806 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4807 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4808 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
4809 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
4813 int pathstart = -1, pathend = -1;
4817 while ((c = getopt_long(argc, argv,
4818 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
4819 long_opts, NULL)) != -1) {
4823 /* '!' is part of option */
4824 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4825 if (!isoption && pathend != -1) {
4827 "error: %s: filename|dirname must either precede options or follow options\n",
4831 if (!isoption && pathstart == -1)
4832 pathstart = optind - 1;
4833 if (isoption && pathstart != -1 && pathend == -1)
4834 pathend = optind - 2;
4838 /* unknown: opt is "!" */
4839 if (strcmp(optarg, "!") == 0)
4843 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4844 param->fp_verbose |= VERBOSE_COUNT;
4845 param->fp_max_depth = 0;
4848 case LFS_COMP_COUNT_OPT:
4849 param->fp_verbose |= VERBOSE_COMP_COUNT;
4850 param->fp_max_depth = 0;
4852 case LFS_COMP_FLAGS_OPT:
4853 if (optarg != NULL) {
4854 rc = comp_str2flags(optarg,
4855 ¶m->fp_comp_flags,
4856 ¶m->fp_comp_neg_flags);
4858 fprintf(stderr, "error: %s bad "
4859 "component flags '%s'.\n",
4863 param->fp_check_comp_flags = 1;
4865 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4866 param->fp_max_depth = 0;
4869 case LFS_COMP_START_OPT:
4870 if (optarg != NULL) {
4872 if (tmp[0] == '+') {
4873 param->fp_comp_start_sign = -1;
4875 } else if (tmp[0] == '-') {
4876 param->fp_comp_start_sign = 1;
4879 rc = llapi_parse_size(tmp,
4880 ¶m->fp_comp_start,
4881 ¶m->fp_comp_start_units, 0);
4883 fprintf(stderr, "error: %s bad "
4884 "component start '%s'.\n",
4888 param->fp_check_comp_start = 1;
4891 param->fp_verbose |= VERBOSE_COMP_START;
4892 param->fp_max_depth = 0;
4895 case LFS_MIRROR_INDEX_OPT:
4896 if (optarg[0] == '+') {
4897 param->fp_mirror_index_sign = -1;
4899 } else if (optarg[0] == '-') {
4900 param->fp_mirror_index_sign = 1;
4904 param->fp_mirror_index = strtoul(optarg, &end, 0);
4905 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4906 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4908 "%s %s: invalid mirror index '%s'\n",
4909 progname, argv[0], optarg);
4912 if (param->fp_mirror_id != 0) {
4914 "%s %s: can't specify both mirror index and mirror ID\n",
4918 param->fp_check_mirror_index = 1;
4919 param->fp_exclude_mirror_index = !!neg_opt;
4921 case LFS_MIRROR_ID_OPT:
4922 if (optarg[0] == '+') {
4923 param->fp_mirror_id_sign = -1;
4925 } else if (optarg[0] == '-') {
4926 param->fp_mirror_id_sign = 1;
4930 param->fp_mirror_id = strtoul(optarg, &end, 0);
4931 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4932 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4934 "%s %s: invalid mirror ID '%s'\n",
4935 progname, argv[0], optarg);
4938 if (param->fp_mirror_index != 0) {
4940 "%s %s: can't specify both mirror index and mirror ID\n",
4944 param->fp_check_mirror_id = 1;
4945 param->fp_exclude_mirror_id = !!neg_opt;
4948 param->fp_max_depth = 0;
4951 param->fp_get_default_lmv = 1;
4954 if (optarg != NULL) {
4956 if (tmp[0] == '+') {
4957 param->fp_comp_end_sign = -1;
4959 } else if (tmp[0] == '-') {
4960 param->fp_comp_end_sign = 1;
4964 if (arg_is_eof(tmp)) {
4965 param->fp_comp_end = LUSTRE_EOF;
4966 param->fp_comp_end_units = 1;
4969 rc = llapi_parse_size(tmp,
4970 ¶m->fp_comp_end,
4971 ¶m->fp_comp_end_units, 0);
4974 fprintf(stderr, "error: %s bad "
4975 "component end '%s'.\n",
4979 param->fp_check_comp_end = 1;
4981 param->fp_verbose |= VERBOSE_COMP_END;
4982 param->fp_max_depth = 0;
4986 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4987 param->fp_verbose |= VERBOSE_DFID;
4988 param->fp_max_depth = 0;
4992 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4993 param->fp_verbose |= VERBOSE_GENERATION;
4994 param->fp_max_depth = 0;
4998 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4999 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5000 param->fp_max_depth = 0;
5004 if (optarg != NULL) {
5005 param->fp_comp_id = strtoul(optarg, &end, 0);
5006 if (*end != '\0' || param->fp_comp_id == 0 ||
5007 param->fp_comp_id > LCME_ID_MAX) {
5008 fprintf(stderr, "error: %s bad "
5009 "component id '%s'\n",
5013 param->fp_check_comp_id = 1;
5016 param->fp_max_depth = 0;
5017 param->fp_verbose |= VERBOSE_COMP_ID;
5021 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5022 param->fp_verbose |= VERBOSE_PATTERN;
5023 param->fp_max_depth = 0;
5026 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5028 fprintf(stderr, "warning: '-M' deprecated"
5029 ", use '--mdt-index' or '-m' instead\n");
5032 if (!(param->fp_verbose & VERBOSE_DETAIL))
5033 param->fp_max_depth = 0;
5034 param->fp_verbose |= VERBOSE_MDTINDEX;
5037 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5038 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5039 param->fp_max_depth = 0;
5043 if (param->fp_obd_uuid) {
5045 "error: %s: only one obduuid allowed",
5049 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5052 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5053 param->fp_verbose |= VERBOSE_POOL;
5054 param->fp_max_depth = 0;
5061 param->fp_recursive = 1;
5067 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5068 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5069 param->fp_max_depth = 0;
5073 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5079 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5080 param->fp_verbose |= VERBOSE_EXT_SIZE;
5081 param->fp_max_depth = 0;
5089 if (pathstart == -1) {
5090 fprintf(stderr, "error: %s: no filename|pathname\n",
5093 } else if (pathend == -1) {
5101 if (param->fp_recursive)
5102 param->fp_max_depth = -1;
5103 else if (param->fp_verbose & VERBOSE_DETAIL)
5104 param->fp_max_depth = 1;
5106 if (!param->fp_verbose)
5107 param->fp_verbose = VERBOSE_DEFAULT;
5108 if (param->fp_quiet)
5109 param->fp_verbose = VERBOSE_OBJID;
5112 rc = llapi_getstripe(argv[pathstart], param);
5113 } while (++pathstart < pathend && !rc);
5116 fprintf(stderr, "error: %s failed for %s.\n",
5117 argv[0], argv[optind - 1]);
5121 static int lfs_tgts(int argc, char **argv)
5123 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5124 struct find_param param;
5125 int index = 0, rc=0;
5130 if (argc == 2 && !realpath(argv[1], path)) {
5132 fprintf(stderr, "error: invalid path '%s': %s\n",
5133 argv[1], strerror(-rc));
5137 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5138 /* Check if we have a mount point */
5139 if (mntdir[0] == '\0')
5142 memset(¶m, 0, sizeof(param));
5143 if (!strcmp(argv[0], "mdts"))
5144 param.fp_get_lmv = 1;
5146 rc = llapi_ostlist(mntdir, ¶m);
5148 fprintf(stderr, "error: %s: failed on %s\n",
5151 if (path[0] != '\0')
5153 memset(mntdir, 0, PATH_MAX);
5159 static int lfs_getstripe(int argc, char **argv)
5161 struct find_param param = { 0 };
5163 param.fp_max_depth = 1;
5164 return lfs_getstripe_internal(argc, argv, ¶m);
5168 static int lfs_getdirstripe(int argc, char **argv)
5170 struct find_param param = { 0 };
5171 struct option long_opts[] = {
5172 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5173 { .val = 'D', .name = "default", .has_arg = no_argument },
5174 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5175 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5176 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5177 { .val = 'O', .name = "obd", .has_arg = required_argument },
5178 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5179 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5180 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5181 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5185 param.fp_get_lmv = 1;
5187 while ((c = getopt_long(argc, argv,
5188 "cDHimO:rtTvy", long_opts, NULL)) != -1)
5193 param.fp_verbose |= VERBOSE_COUNT;
5196 param.fp_get_default_lmv = 1;
5200 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5202 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5204 fprintf(stderr, "warning: '-t' deprecated, "
5205 "use '--mdt-hash' or '-H' instead\n");
5208 param.fp_verbose |= VERBOSE_HASH_TYPE;
5211 if (param.fp_obd_uuid) {
5213 "error: %s: only one obduuid allowed",
5217 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5220 param.fp_recursive = 1;
5223 param.fp_verbose |= VERBOSE_DETAIL;
5229 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5230 progname, argv[0], argv[optind - 1]);
5238 if (param.fp_recursive)
5239 param.fp_max_depth = -1;
5241 if (!param.fp_verbose)
5242 param.fp_verbose = VERBOSE_DEFAULT;
5245 rc = llapi_getstripe(argv[optind], ¶m);
5246 } while (++optind < argc && !rc);
5249 fprintf(stderr, "error: %s failed for %s.\n",
5250 argv[0], argv[optind - 1]);
5255 MNTDF_INODES = 0x0001,
5256 MNTDF_COOKED = 0x0002,
5257 MNTDF_LAZY = 0x0004,
5258 MNTDF_VERBOSE = 0x0008,
5259 MNTDF_SHOW = 0x0010,
5262 #define COOK(value) \
5265 while (value > 1024) { \
5273 #define CDF "%11llu"
5274 #define HDF "%8.1f%c"
5278 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5280 double avail, used, ratio = 0;
5283 avail = st->os_ffree;
5284 used = st->os_files - st->os_ffree;
5286 avail = st->os_bavail;
5287 used = st->os_blocks - st->os_bfree;
5289 if (avail + used > 0)
5290 ratio = used / (used + avail) * 100;
5292 /* Round up to match df(1) usage percentage */
5293 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5296 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5297 * so only show flags reflecting those states by default. Informational states
5298 * are only shown with "-v" and use lower-case names to distinguish them.
5299 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5301 static struct obd_statfs_state_names {
5302 enum obd_statfs_state osn_state;
5303 const char osn_name;
5306 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
5307 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
5308 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
5309 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
5310 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
5311 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
5312 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
5313 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
5314 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
5317 static int showdf(char *mntdir, struct obd_statfs *stat,
5318 char *uuid, enum mntdf_flags flags,
5319 char *type, int index, int rc)
5321 long long avail, used, total;
5323 char *suffix = "KMGTPEZY";
5324 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5325 char tbuf[3 * sizeof(__u64)];
5326 char ubuf[3 * sizeof(__u64)];
5327 char abuf[3 * sizeof(__u64)];
5328 char rbuf[3 * sizeof(__u64)];
5335 if (flags & MNTDF_INODES) {
5336 avail = stat->os_ffree;
5337 used = stat->os_files - stat->os_ffree;
5338 total = stat->os_files;
5340 int shift = flags & MNTDF_COOKED ? 0 : 10;
5342 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5343 used = ((stat->os_blocks - stat->os_bfree) *
5344 stat->os_bsize) >> shift;
5345 total = (stat->os_blocks * stat->os_bsize) >> shift;
5348 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5350 if (flags & MNTDF_COOKED) {
5354 cook_val = (double)total;
5357 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5360 snprintf(tbuf, sizeof(tbuf), CDF, total);
5362 cook_val = (double)used;
5365 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5368 snprintf(ubuf, sizeof(ubuf), CDF, used);
5370 cook_val = (double)avail;
5373 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5376 snprintf(abuf, sizeof(abuf), CDF, avail);
5378 snprintf(tbuf, sizeof(tbuf), CDF, total);
5379 snprintf(ubuf, sizeof(tbuf), CDF, used);
5380 snprintf(abuf, sizeof(tbuf), CDF, avail);
5383 sprintf(rbuf, RDF, ratio);
5384 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5385 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5387 printf("[%s:%d]", type, index);
5389 if (stat->os_state) {
5393 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5394 if (oss_names[i].osn_state & stat->os_state &&
5395 (oss_names[i].osn_err ||
5396 flags & MNTDF_VERBOSE))
5397 printf("%c", oss_names[i].osn_name);
5404 printf(UUF": inactive device\n", uuid);
5407 printf(UUF": %s\n", uuid, strerror(-rc));
5414 struct ll_stat_type {
5419 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5421 struct ll_statfs_data {
5423 struct obd_statfs sd_st;
5426 struct ll_statfs_buf {
5428 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5431 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5432 int ops, struct ll_statfs_buf *lsb)
5434 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5435 struct obd_uuid uuid_buf;
5436 char *poolname = NULL;
5437 struct ll_stat_type types[] = {
5438 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5439 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5440 { .st_name = NULL } };
5441 struct ll_stat_type *tp;
5442 __u64 ost_files = 0;
5443 __u64 ost_ffree = 0;
5451 poolname = strchr(pool, '.');
5452 if (poolname != NULL) {
5453 if (strncmp(fsname, pool, strlen(fsname))) {
5454 fprintf(stderr, "filesystem name incorrect\n");
5462 fd = open(mntdir, O_RDONLY);
5465 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5470 if (flags & MNTDF_SHOW) {
5471 if (flags & MNTDF_INODES)
5472 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5473 "UUID", "Inodes", "IUsed", "IFree",
5474 "IUse%", "Mounted on");
5476 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5478 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5479 "Used", "Available", "Use%", "Mounted on");
5482 for (tp = types; tp->st_name != NULL; tp++) {
5483 bool have_ost = false;
5485 if (!(tp->st_op & ops))
5488 for (index = 0; ; index++) {
5489 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5490 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5491 type = flags & MNTDF_LAZY ?
5492 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5493 rc2 = llapi_obd_fstatfs(fd, type, index,
5494 &stat_buf, &uuid_buf);
5499 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5500 if (!(flags & MNTDF_VERBOSE))
5502 } else if (rc2 < 0 && rc == 0) {
5506 /* If we have OSTs then don't report MDT block counts.
5507 * For MDT-only filesystems the expectation is that all
5508 * layouts have a DoM component. For filesystems with
5509 * OSTs, files are not necessarily going to store data
5510 * on MDTs, and MDT space is limited to a fraction of
5511 * OST space, so don't include it in the summary.
5513 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5520 if (poolname && tp->st_op == LL_STATFS_LOV &&
5521 llapi_search_ost(fsname, poolname,
5522 obd_uuid2str(&uuid_buf)) != 1)
5525 /* the llapi_obd_statfs() call may have returned with
5526 * an error, but if it filled in uuid_buf we will at
5527 * lease use that to print out a message for that OBD.
5528 * If we didn't get anything in the uuid_buf, then fill
5529 * it in so that we can print an error message. */
5530 if (uuid_buf.uuid[0] == '\0')
5531 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5532 "%s%04x", tp->st_name, index);
5534 lsb->sb_buf[lsb->sb_count].sd_index = index;
5535 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5538 if (flags & MNTDF_SHOW)
5539 showdf(mntdir, &stat_buf,
5540 obd_uuid2str(&uuid_buf), flags,
5541 tp->st_name, index, rc2);
5546 if (tp->st_op == LL_STATFS_LMV) {
5547 sum.os_ffree += stat_buf.os_ffree;
5548 sum.os_files += stat_buf.os_files;
5549 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5550 ost_files += stat_buf.os_files;
5551 ost_ffree += stat_buf.os_ffree;
5553 sum.os_blocks += stat_buf.os_blocks *
5555 sum.os_bfree += stat_buf.os_bfree *
5557 sum.os_bavail += stat_buf.os_bavail *
5564 /* If we have _some_ OSTs, but don't have as many free objects on the
5565 * OST as inodes on the MDTs, reduce the reported number of inodes
5566 * to compensate, so that the "inodes in use" number is correct.
5567 * This should be kept in sync with ll_statfs_internal().
5569 if (ost_files && ost_ffree < sum.os_ffree) {
5570 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5571 sum.os_ffree = ost_ffree;
5573 if (flags & MNTDF_SHOW) {
5575 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5582 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5584 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5586 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5588 int r1 = obd_statfs_ratio(st1, false);
5589 int r2 = obd_statfs_ratio(st2, false);
5590 int64_t result = r1 - r2;
5592 /* if both space usage are above 90, compare free inodes */
5593 if (r1 > 90 && r2 > 90)
5594 result = st2->os_ffree - st1->os_ffree;
5598 else if (result == 0)
5605 static int lfs_setdirstripe(int argc, char **argv)
5608 struct lfs_setstripe_args lsa = { 0 };
5609 struct llapi_stripe_param *param = NULL;
5610 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5613 char *mode_opt = NULL;
5614 bool default_stripe = false;
5615 bool delete = false;
5616 bool auto_distributed = false;
5617 bool foreign_mode = false;
5618 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5619 mode_t previous_mode = 0;
5620 struct ll_statfs_buf *lsb = NULL;
5621 char mntdir[PATH_MAX] = "";
5623 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5624 struct option long_opts[] = {
5625 { .val = 'c', .name = "count", .has_arg = required_argument },
5626 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5627 { .val = 'd', .name = "delete", .has_arg = no_argument },
5628 { .val = 'D', .name = "default", .has_arg = no_argument },
5629 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5630 { .val = LFS_LAYOUT_FLAGS_OPT,
5631 .name = "flags", .has_arg = required_argument },
5632 { .val = LFS_LAYOUT_FOREIGN_OPT,
5633 .name = "foreign", .has_arg = optional_argument},
5634 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5635 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5636 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5637 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5639 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5640 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5641 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5643 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5644 { .val = 'i', .name = "index", .has_arg = required_argument },
5646 { .val = 'o', .name = "mode", .has_arg = required_argument },
5647 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5648 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5650 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5651 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5652 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5656 setstripe_args_init(&lsa);
5658 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5659 long_opts, NULL)) >= 0) {
5666 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5669 "%s %s: invalid stripe count '%s'\n",
5670 progname, argv[0], optarg);
5676 default_stripe = true;
5679 default_stripe = true;
5681 case LFS_LAYOUT_FOREIGN_OPT:
5682 if (optarg != NULL) {
5683 /* check pure numeric */
5684 type = strtoul(optarg, &end, 0);
5687 type = check_foreign_type_name(optarg);
5688 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5690 "%s %s: unknown foreign type '%s'\n",
5697 foreign_mode = true;
5699 case LFS_LAYOUT_FLAGS_OPT:
5700 flags = strtoul(optarg, &end, 16);
5703 "%s %s: bad flags '%s'\n",
5704 progname, argv[0], optarg);
5708 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5710 fprintf(stderr, "warning: '--hash-type' and '-t' "
5711 "deprecated, use '--mdt-hash' or '-H' instead\n");
5714 lsa.lsa_pattern = check_hashtype(optarg);
5715 if (lsa.lsa_pattern == 0) {
5717 "%s %s: bad stripe hash type '%s'\n",
5718 progname, argv[0], optarg);
5723 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5726 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5727 if (strcmp(argv[optind - 1], "--index") == 0)
5729 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5732 lsa.lsa_nr_tgts = parse_targets(mdts,
5733 sizeof(mdts) / sizeof(__u32),
5734 lsa.lsa_nr_tgts, optarg, NULL);
5735 if (lsa.lsa_nr_tgts < 0) {
5737 "%s %s: invalid MDT target(s) '%s'\n",
5738 progname, argv[0], optarg);
5742 lsa.lsa_tgts = mdts;
5743 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5744 lsa.lsa_stripe_off = mdts[0];
5746 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5748 fprintf(stderr, "warning: '-m' is deprecated, "
5749 "use '--mode' or '-o' instead\n");
5758 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5759 progname, argv[0], argv[optind - 1]);
5764 if (optind == argc) {
5765 fprintf(stderr, "%s %s: DIR must be specified\n",
5770 if (xattr && !foreign_mode) {
5771 /* only print a warning as this is armless and will be
5775 "%s %s: xattr has been specified for non-foreign layout\n",
5777 } else if (foreign_mode && !xattr) {
5779 "%s %s: xattr must be provided in foreign mode\n",
5784 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5785 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5787 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5792 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5793 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5795 "%s %s: stripe offset and count must be specified\n",
5801 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5802 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5804 "%s %s: cannot specify -d with -c or -i options\n",
5809 if (mode_opt != NULL) {
5810 mode = strtoul(mode_opt, &end, 8);
5813 "%s %s: bad MODE '%s'\n",
5814 progname, argv[0], mode_opt);
5817 previous_mode = umask(0);
5820 /* foreign LMV/dir case */
5822 if (argc > optind + 1) {
5824 "%s %s: cannot specify multiple foreign dirs\n",
5829 dname = argv[optind];
5830 result = llapi_dir_create_foreign(dname, mode, type, flags,
5834 "%s mkdir: can't create foreign dir '%s': %s\n",
5835 progname, dname, strerror(-result));
5840 * initialize stripe parameters, in case param is converted to specific,
5841 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5843 param = calloc(1, offsetof(typeof(*param),
5844 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5845 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5846 if (param == NULL) {
5848 "%s %s: cannot allocate memory for parameters: %s\n",
5849 progname, argv[0], strerror(ENOMEM));
5853 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5854 param->lsp_stripe_count = lsa.lsa_stripe_count;
5855 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5856 param->lsp_stripe_offset = -1;
5858 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5859 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5860 param->lsp_stripe_pattern = lsa.lsa_pattern;
5862 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
5863 param->lsp_pool = lsa.lsa_pool_name;
5864 param->lsp_is_specific = false;
5865 if (lsa.lsa_nr_tgts > 1) {
5866 if (lsa.lsa_stripe_count > 0 &&
5867 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5868 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5870 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
5871 argv[0], lsa.lsa_stripe_count,
5877 param->lsp_is_specific = true;
5878 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5879 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5882 if (!default_stripe && (lsa.lsa_pattern & LMV_HASH_FLAG_SPACE)) {
5883 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5889 if (param->lsp_stripe_offset != -1 &&
5890 lsa.lsa_pattern & LMV_HASH_FLAG_SPACE) {
5891 fprintf(stderr, "%s %s: can only specify -H space with -i -1\n",
5897 dname = argv[optind];
5899 if (default_stripe) {
5900 result = llapi_dir_set_default_lmv(dname, param);
5903 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5904 progname, dname, strerror(-result));
5909 * if current \a dname isn't under the same \a mntdir as the
5910 * last one, and the last one was auto-distributed, restore
5913 if (mntdir[0] != '\0' &&
5914 strncmp(dname, mntdir, strlen(mntdir)) &&
5916 param->lsp_is_specific = false;
5917 param->lsp_stripe_offset = -1;
5918 auto_distributed = false;
5922 * TODO: when MDT can allocate object with QoS (LU-9435), below
5923 * code should be removed, instead we should let LMV to allocate
5924 * the starting MDT object, and then let LOD allocate other MDT
5927 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5928 char path[PATH_MAX] = "";
5931 lsb = malloc(sizeof(*lsb));
5939 /* use mntdir for dirname() temporarily */
5940 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5941 if (!realpath(dirname(mntdir), path)) {
5944 "error: invalid path '%s': %s\n",
5945 argv[optind], strerror(errno));
5950 result = llapi_search_mounts(path, 0, mntdir, NULL);
5951 if (result < 0 || mntdir[0] == '\0') {
5953 "No suitable Lustre mount found\n");
5957 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5962 if (param->lsp_stripe_count > lsb->sb_count) {
5964 "error: stripe count %d is too big\n",
5965 param->lsp_stripe_count);
5970 qsort(lsb->sb_buf, lsb->sb_count,
5971 sizeof(struct ll_statfs_data),
5972 ll_statfs_data_comp);
5974 auto_distributed = true;
5977 if (auto_distributed) {
5979 int nr = MAX(param->lsp_stripe_count,
5982 /* don't use server whose usage is above 90% */
5983 while (nr != param->lsp_stripe_count &&
5984 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5986 nr = MAX(param->lsp_stripe_count, nr / 2);
5988 /* get \a r between [0, nr) */
5991 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5992 if (param->lsp_stripe_count > 1) {
5995 param->lsp_is_specific = true;
5996 for (; i < param->lsp_stripe_count; i++)
5997 param->lsp_tgts[(i + r) % nr] =
5998 lsb->sb_buf[i].sd_index;
6002 result = llapi_dir_create(dname, mode, param);
6005 "%s setdirstripe: cannot create dir '%s': %s\n",
6006 progname, dname, strerror(-result));
6007 } while (!result && (dname = argv[++optind]));
6009 if (mode_opt != NULL)
6010 umask(previous_mode);
6018 static int lfs_rmentry(int argc, char **argv)
6025 fprintf(stderr, "error: %s: missing dirname\n",
6031 dname = argv[index];
6032 while (dname != NULL) {
6033 result = llapi_direntry_remove(dname);
6035 fprintf(stderr, "error: %s: remove dir entry '%s' "
6036 "failed\n", argv[0], dname);
6039 dname = argv[++index];
6044 static int lfs_mv(int argc, char **argv)
6046 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6047 struct find_param param = {
6054 struct option long_opts[] = {
6055 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6056 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6057 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6060 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6062 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6064 fprintf(stderr, "warning: '-M' deprecated"
6065 ", use '--mdt-index' or '-m' instead\n");
6068 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6070 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6076 param.fp_verbose = VERBOSE_DETAIL;
6079 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6080 progname, argv[optind - 1]);
6085 if (lmu.lum_stripe_offset == -1) {
6086 fprintf(stderr, "%s mv: MDT index must be specified\n",
6091 if (optind >= argc) {
6092 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6097 /* initialize migrate mdt parameters */
6098 param.fp_lmv_md = &lmu;
6099 param.fp_migrate = 1;
6100 rc = llapi_migrate_mdt(argv[optind], ¶m);
6102 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6103 progname, argv[optind], param.fp_mdt_index,
6108 static int lfs_osts(int argc, char **argv)
6110 return lfs_tgts(argc, argv);
6113 static int lfs_mdts(int argc, char **argv)
6115 return lfs_tgts(argc, argv);
6118 static int lfs_df(int argc, char **argv)
6120 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6121 enum mntdf_flags flags = MNTDF_SHOW;
6122 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6123 int c, rc = 0, index = 0;
6124 char fsname[PATH_MAX] = "", *pool_name = NULL;
6125 struct option long_opts[] = {
6126 { .val = 'h', .name = "human-readable",
6127 .has_arg = no_argument },
6128 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6129 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6130 { .val = 'p', .name = "pool", .has_arg = required_argument },
6131 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6134 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6137 flags |= MNTDF_COOKED;
6140 flags |= MNTDF_INODES;
6143 flags |= MNTDF_LAZY;
6149 flags |= MNTDF_VERBOSE;
6155 if (optind < argc && !realpath(argv[optind], path)) {
6157 fprintf(stderr, "error: invalid path '%s': %s\n",
6158 argv[optind], strerror(-rc));
6162 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6163 /* Check if we have a mount point */
6164 if (mntdir[0] == '\0')
6167 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6168 if (rc || path[0] != '\0')
6170 fsname[0] = '\0'; /* avoid matching in next loop */
6171 mntdir[0] = '\0'; /* avoid matching in next loop */
6177 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
6178 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6182 /* llapi_search_mounts() fills "fsname", but that is not called if
6183 * explicit paths are specified on the command-line
6185 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
6186 int rc = llapi_getname(mntdir, fsname, fsnamelen);
6189 fprintf(stderr, "cannot get instance for '%s': %s\n",
6190 mntdir, strerror(-rc));
6194 if (!opt_instance) {
6195 /* print only the fsname name */
6196 buf = strchr(fsname, '-');
6200 } else if (!opt_fsname) {
6201 /* print only the instance name */
6202 buf = strchr(fsname, '-');
6208 } else if (opt_fsname) {
6209 /* print only the fsname */
6213 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
6218 static int lfs_getname(int argc, char **argv)
6220 struct option long_opts[] = {
6221 { .val = 'h', .name = "help", .has_arg = no_argument },
6222 { .val = 'i', .name = "instance", .has_arg = no_argument },
6223 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6225 bool opt_instance = false, opt_fsname = false;
6226 char fsname[PATH_MAX] = "";
6229 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6232 opt_instance = true;
6243 /* If neither option is given, print both instance and fsname */
6244 if (!opt_instance && !opt_fsname)
6245 opt_instance = opt_fsname = true;
6247 if (optind == argc) { /* no paths specified, get all paths. */
6248 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6251 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6252 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6253 opt_instance, opt_fsname, true);
6256 path[0] = fsname[0] = mntdir[0] = '\0';
6258 } else { /* paths specified, only attempt to search these. */
6261 /* if only one path is given, print only requested info */
6262 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6264 for (; optind < argc; optind++) {
6265 rc2 = print_instance(argv[optind], fsname,
6266 sizeof(fsname), opt_instance,
6267 opt_fsname, opt_mntdir);
6277 static int lfs_check(int argc, char **argv)
6279 char mntdir[PATH_MAX] = {'\0'};
6288 fprintf(stderr, "%s check: server type must be specified\n",
6293 obd_types[0] = obd_type1;
6294 obd_types[1] = obd_type2;
6295 obd_types[2] = obd_type3;
6297 if (strcmp(argv[1], "osts") == 0) {
6298 strcpy(obd_types[0], "osc");
6299 } else if (strcmp(argv[1], "mdts") == 0 ||
6300 strcmp(argv[1], "mds") == 0) {
6301 strcpy(obd_types[0], "mdc");
6302 } else if (strcmp(argv[1], "mgts") == 0) {
6303 strcpy(obd_types[0], "mgc");
6304 } else if (strcmp(argv[1], "all") == 0 ||
6305 strcmp(argv[1], "servers") == 0) {
6307 strcpy(obd_types[0], "osc");
6308 strcpy(obd_types[1], "mdc");
6309 strcpy(obd_types[2], "mgc");
6311 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6316 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6317 if (rc < 0 || mntdir[0] == '\0') {
6319 "%s check: cannot find mounted Lustre filesystem: %s\n",
6320 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6324 rc = llapi_target_check(num_types, obd_types, mntdir);
6326 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6327 progname, argv[1], strerror(-rc));
6333 #ifdef HAVE_SYS_QUOTA_H
6334 #define ARG2INT(nr, str, msg) \
6337 nr = strtol(str, &endp, 0); \
6338 if (*endp != '\0') { \
6339 fprintf(stderr, "%s: bad %s '%s'\n", \
6340 progname, msg, str); \
6345 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6347 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6348 * returns the value or ULONG_MAX on integer overflow or incorrect format
6350 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6351 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6352 * 3. empty integer value is interpreted as 0
6354 static unsigned long str2sec(const char* timestr)
6356 const char spec[] = "smhdw";
6357 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6358 unsigned long val = 0;
6361 if (strpbrk(timestr, spec) == NULL) {
6362 /* no specifiers inside the time string,
6363 should treat it as an integer value */
6364 val = strtoul(timestr, &tail, 10);
6365 return *tail ? ULONG_MAX : val;
6368 /* format string is XXwXXdXXhXXmXXs */
6374 v = strtoul(timestr, &tail, 10);
6375 if (v == ULONG_MAX || *tail == '\0')
6376 /* value too large (ULONG_MAX or more)
6377 or missing specifier */
6380 ptr = strchr(spec, *tail);
6382 /* unknown specifier */
6387 /* check if product will overflow the type */
6388 if (!(v < ULONG_MAX / mult[ind]))
6391 ADD_OVERFLOW(val, mult[ind] * v);
6392 if (val == ULONG_MAX)
6404 #define ARG2ULL(nr, str, def_units) \
6406 unsigned long long limit, units = def_units; \
6409 rc = llapi_parse_size(str, &limit, &units, 1); \
6411 fprintf(stderr, "%s: invalid limit '%s'\n", \
6418 static inline int has_times_option(int argc, char **argv)
6422 for (i = 1; i < argc; i++)
6423 if (!strcmp(argv[i], "-t"))
6429 int lfs_setquota_times(int argc, char **argv)
6432 struct if_quotactl qctl;
6433 char *mnt, *obd_type = (char *)qctl.obd_type;
6434 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6435 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6436 struct option long_opts[] = {
6437 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6438 { .val = 'g', .name = "group", .has_arg = no_argument },
6439 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6440 { .val = 'p', .name = "projid", .has_arg = no_argument },
6441 { .val = 't', .name = "times", .has_arg = no_argument },
6442 { .val = 'u', .name = "user", .has_arg = no_argument },
6446 memset(&qctl, 0, sizeof(qctl));
6447 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6448 qctl.qc_type = ALLQUOTA;
6450 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6451 long_opts, NULL)) != -1) {
6462 if (qctl.qc_type != ALLQUOTA) {
6463 fprintf(stderr, "error: -u/g/p can't be used "
6464 "more than once\n");
6467 qctl.qc_type = qtype;
6470 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6471 fprintf(stderr, "error: bad block-grace: %s\n",
6475 dqb->dqb_valid |= QIF_BTIME;
6478 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6479 fprintf(stderr, "error: bad inode-grace: %s\n",
6483 dqb->dqb_valid |= QIF_ITIME;
6485 case 't': /* Yes, of course! */
6487 default: /* getopt prints error message for us when opterr != 0 */
6492 if (qctl.qc_type == ALLQUOTA) {
6493 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6497 if (optind != argc - 1) {
6498 fprintf(stderr, "error: unexpected parameters encountered\n");
6502 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6503 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6504 fprintf(stderr, "error: grace time is too large\n");
6509 rc = llapi_quotactl(mnt, &qctl);
6512 fprintf(stderr, "%s %s ", obd_type,
6513 obd_uuid2str(&qctl.obd_uuid));
6514 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6521 #define BSLIMIT (1 << 0)
6522 #define BHLIMIT (1 << 1)
6523 #define ISLIMIT (1 << 2)
6524 #define IHLIMIT (1 << 3)
6526 int lfs_setquota(int argc, char **argv)
6529 struct if_quotactl qctl;
6530 char *mnt, *obd_type = (char *)qctl.obd_type;
6531 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6532 struct option long_opts[] = {
6533 { .val = 'b', .name = "block-softlimit",
6534 .has_arg = required_argument },
6535 { .val = 'B', .name = "block-hardlimit",
6536 .has_arg = required_argument },
6537 { .val = 'd', .name = "default", .has_arg = no_argument },
6538 { .val = 'g', .name = "group", .has_arg = required_argument },
6539 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6540 { .val = 'i', .name = "inode-softlimit",
6541 .has_arg = required_argument },
6542 { .val = 'I', .name = "inode-hardlimit",
6543 .has_arg = required_argument },
6544 { .val = 'p', .name = "projid", .has_arg = required_argument },
6545 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6546 { .val = 'u', .name = "user", .has_arg = required_argument },
6547 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6549 unsigned limit_mask = 0;
6551 bool use_default = false;
6554 if (has_times_option(argc, argv))
6555 return lfs_setquota_times(argc, argv);
6557 memset(&qctl, 0, sizeof(qctl));
6558 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6559 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6560 * so it can be used as a marker that qc_type
6561 * isn't reinitialized from command line */
6563 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6564 long_opts, NULL)) != -1) {
6567 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6570 goto quota_type_def;
6573 rc = name2uid(&qctl.qc_id, optarg);
6576 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6579 goto quota_type_def;
6582 rc = name2gid(&qctl.qc_id, optarg);
6585 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6588 goto quota_type_def;
6591 rc = name2projid(&qctl.qc_id, optarg);
6594 qctl.qc_id = strtoul(optarg, &endptr, 10);
6595 if (*endptr != '\0') {
6596 fprintf(stderr, "%s setquota: invalid"
6597 " id '%s'\n", progname, optarg);
6602 if (qctl.qc_id == 0) {
6603 fprintf(stderr, "%s setquota: can't set quota"
6604 " for root usr/group/project.\n",
6610 if (qctl.qc_type != ALLQUOTA) {
6612 "%s setquota: only one of -u, -U, -g,"
6613 " -G, -p or -P may be specified\n",
6617 qctl.qc_type = qtype;
6620 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6624 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6625 dqb->dqb_bsoftlimit >>= 10;
6626 limit_mask |= BSLIMIT;
6627 if (dqb->dqb_bsoftlimit &&
6628 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6630 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6631 "See '%s help setquota' or Lustre manual for details\n",
6633 (unsigned long long)dqb->dqb_bsoftlimit,
6637 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6638 dqb->dqb_bhardlimit >>= 10;
6639 limit_mask |= BHLIMIT;
6640 if (dqb->dqb_bhardlimit &&
6641 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6643 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6644 "See '%s help setquota' or Lustre manual for details\n",
6646 (unsigned long long)dqb->dqb_bhardlimit,
6650 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6651 limit_mask |= ISLIMIT;
6652 if (dqb->dqb_isoftlimit &&
6653 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6655 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6656 "See '%s help setquota' or Lustre manual for details\n",
6658 (unsigned long long)dqb->dqb_isoftlimit,
6662 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6663 limit_mask |= IHLIMIT;
6664 if (dqb->dqb_ihardlimit &&
6665 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6667 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6668 "See '%s help setquota' or Lustre manual for details\n",
6670 (unsigned long long)dqb->dqb_ihardlimit,
6675 "%s setquota: unrecognized option '%s'\n",
6676 progname, argv[optind - 1]);
6681 if (qctl.qc_type == ALLQUOTA) {
6683 "%s setquota: either -u or -g must be specified\n",
6688 if (!use_default && limit_mask == 0) {
6690 "%s setquota: at least one limit must be specified\n",
6695 if (use_default && limit_mask != 0) {
6697 "%s setquota: limits should not be specified when"
6698 " using default quota\n",
6703 if (use_default && qctl.qc_id == 0) {
6705 "%s setquota: can not set default quota for root"
6706 " user/group/project\n",
6711 if (optind != argc - 1) {
6713 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6714 progname, argv[optind]);
6721 dqb->dqb_bhardlimit = 0;
6722 dqb->dqb_bsoftlimit = 0;
6723 dqb->dqb_ihardlimit = 0;
6724 dqb->dqb_isoftlimit = 0;
6727 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6728 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6729 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6730 /* sigh, we can't just set blimits/ilimits */
6731 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6732 .qc_type = qctl.qc_type,
6733 .qc_id = qctl.qc_id};
6735 rc = llapi_quotactl(mnt, &tmp_qctl);
6739 if (!(limit_mask & BHLIMIT))
6740 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6741 if (!(limit_mask & BSLIMIT))
6742 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6743 if (!(limit_mask & IHLIMIT))
6744 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6745 if (!(limit_mask & ISLIMIT))
6746 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6748 /* Keep grace times if we have got no softlimit arguments */
6749 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6750 dqb->dqb_valid |= QIF_BTIME;
6751 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6754 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6755 dqb->dqb_valid |= QIF_ITIME;
6756 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6760 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6761 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6763 rc = llapi_quotactl(mnt, &qctl);
6767 "%s setquota: cannot quotactl '%s' '%s': %s",
6769 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6776 /* Converts seconds value into format string
6777 * result is returned in buf
6779 * 1. result is in descenting order: 1w2d3h4m5s
6780 * 2. zero fields are not filled (except for p. 3): 5d1s
6781 * 3. zero seconds value is presented as "0s"
6783 static char * __sec2str(time_t seconds, char *buf)
6785 const char spec[] = "smhdw";
6786 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6791 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6792 c = seconds / mult[i];
6794 if (c > 0 || (i == 0 && buf == tail))
6795 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6803 static void sec2str(time_t seconds, char *buf, int rc)
6810 tail = __sec2str(seconds, tail);
6812 if (rc && tail - buf < 39) {
6818 static void diff2str(time_t seconds, char *buf, time_t now)
6824 if (seconds <= now) {
6825 strcpy(buf, "none");
6828 __sec2str(seconds - now, buf);
6831 static void print_quota_title(char *name, struct if_quotactl *qctl,
6832 bool human_readable, bool show_default)
6835 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6836 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6837 "Filesystem", "bquota", "blimit", "bgrace",
6838 "iquota", "ilimit", "igrace");
6840 printf("Disk quotas for %s %s (%cid %u):\n",
6841 qtype_name(qctl->qc_type), name,
6842 *qtype_name(qctl->qc_type), qctl->qc_id);
6843 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6844 "Filesystem", human_readable ? "used" : "kbytes",
6845 "quota", "limit", "grace",
6846 "files", "quota", "limit", "grace");
6850 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6853 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6856 snprintf(buf, buflen, "%5.4gP",
6857 (double)num / ((__u64)1 << 40));
6859 snprintf(buf, buflen, "%5.4gT",
6860 (double)num / (1 << 30));
6862 snprintf(buf, buflen, "%5.4gG",
6863 (double)num / (1 << 20));
6865 snprintf(buf, buflen, "%5.4gM",
6866 (double)num / (1 << 10));
6868 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6872 #define STRBUF_LEN 32
6873 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6874 int rc, bool h, bool show_default)
6880 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6881 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6882 int bover = 0, iover = 0;
6883 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6884 char numbuf[3][STRBUF_LEN];
6886 char strbuf[STRBUF_LEN];
6888 if (dqb->dqb_bhardlimit &&
6889 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6891 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6892 if (dqb->dqb_btime > now) {
6899 if (dqb->dqb_ihardlimit &&
6900 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6902 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6903 if (dqb->dqb_itime > now) {
6911 if (strlen(mnt) > 15)
6912 printf("%s\n%15s", mnt, "");
6914 printf("%15s", mnt);
6917 diff2str(dqb->dqb_btime, timebuf, now);
6918 else if (show_default)
6919 snprintf(timebuf, sizeof(timebuf), "%llu",
6920 (unsigned long long)dqb->dqb_btime);
6922 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6923 strbuf, sizeof(strbuf), h);
6924 if (rc == -EREMOTEIO)
6925 sprintf(numbuf[0], "%s*", strbuf);
6927 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6928 "%s" : "[%s]", strbuf);
6930 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6931 if (type == QC_GENERAL)
6932 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6933 "%s" : "[%s]", strbuf);
6935 sprintf(numbuf[1], "%s", "-");
6937 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6938 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6939 "%s" : "[%s]", strbuf);
6942 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6944 printf(" %7s%c %6s %7s %7s",
6945 numbuf[0], bover ? '*' : ' ', numbuf[1],
6946 numbuf[2], bover > 1 ? timebuf : "-");
6950 diff2str(dqb->dqb_itime, timebuf, now);
6951 else if (show_default)
6952 snprintf(timebuf, sizeof(timebuf), "%llu",
6953 (unsigned long long)dqb->dqb_itime);
6955 snprintf(numbuf[0], sizeof(numbuf),
6956 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6957 (uintmax_t)dqb->dqb_curinodes);
6959 if (type == QC_GENERAL)
6960 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6962 (uintmax_t)dqb->dqb_isoftlimit);
6964 sprintf(numbuf[1], "%s", "-");
6966 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6967 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6970 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6971 else if (type != QC_OSTIDX)
6972 printf(" %7s%c %6s %7s %7s",
6973 numbuf[0], iover ? '*' : ' ', numbuf[1],
6974 numbuf[2], iover > 1 ? timebuf : "-");
6976 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6979 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6980 qctl->qc_cmd == Q_GETOINFO) {
6984 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6985 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6986 printf("Block grace time: %s; Inode grace time: %s\n",
6987 bgtimebuf, igtimebuf);
6991 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6992 bool h, __u64 *total)
6994 int rc = 0, rc1 = 0, count = 0;
6995 __u32 valid = qctl->qc_valid;
6997 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6999 fprintf(stderr, "can not get %s count: %s\n",
7000 is_mdt ? "mdt": "ost", strerror(-rc));
7004 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
7005 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7006 rc = llapi_quotactl(mnt, qctl);
7008 /* It is remote client case. */
7009 if (rc == -EOPNOTSUPP) {
7016 fprintf(stderr, "quotactl %s%d failed.\n",
7017 is_mdt ? "mdt": "ost", qctl->qc_idx);
7021 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7022 qctl->qc_valid, 0, h, false);
7023 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7024 qctl->qc_dqblk.dqb_bhardlimit;
7027 qctl->qc_valid = valid;
7031 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7032 int verbose, int quiet, bool human_readable,
7035 int rc1 = 0, rc2 = 0, rc3 = 0;
7036 char *obd_type = (char *)qctl->obd_type;
7037 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7038 __u64 total_ialloc = 0, total_balloc = 0;
7039 bool use_default_for_blk = false;
7040 bool use_default_for_file = false;
7043 rc1 = llapi_quotactl(mnt, qctl);
7047 fprintf(stderr, "%s quotas are not enabled.\n",
7048 qtype_name(qctl->qc_type));
7051 fprintf(stderr, "Permission denied.\n");
7054 /* We already got error message. */
7057 fprintf(stderr, "Unexpected quotactl error: %s\n",
7062 if (!show_default && qctl->qc_id == 0) {
7063 qctl->qc_dqblk.dqb_bhardlimit = 0;
7064 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7065 qctl->qc_dqblk.dqb_ihardlimit = 0;
7066 qctl->qc_dqblk.dqb_isoftlimit = 0;
7067 qctl->qc_dqblk.dqb_btime = 0;
7068 qctl->qc_dqblk.dqb_itime = 0;
7069 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7072 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7073 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7074 use_default_for_blk = true;
7075 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7078 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7079 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7080 use_default_for_file = true;
7081 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7084 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7085 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7086 print_quota_title(name, qctl, human_readable, show_default);
7088 if (rc1 && *obd_type)
7089 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7091 if (qctl->qc_valid != QC_GENERAL)
7094 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
7095 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7096 (QIF_LIMITS|QIF_USAGE));
7098 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7100 if (!show_default && verbose &&
7101 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
7102 char strbuf[STRBUF_LEN];
7104 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7106 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7108 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7110 printf("Total allocated inode limit: %ju, total "
7111 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
7115 if (use_default_for_blk)
7116 printf("%cid %u is using default block quota setting\n",
7117 *qtype_name(qctl->qc_type), qctl->qc_id);
7119 if (use_default_for_file)
7120 printf("%cid %u is using default file quota setting\n",
7121 *qtype_name(qctl->qc_type), qctl->qc_id);
7123 if (rc1 || rc2 || rc3 || inacc)
7124 printf("Some errors happened when getting quota info. "
7125 "Some devices may be not working or deactivated. "
7126 "The data in \"[]\" is inaccurate.\n");
7132 static int lfs_project(int argc, char **argv)
7134 int ret = 0, err = 0, c, i;
7135 struct project_handle_control phc = { 0 };
7136 enum lfs_project_ops_t op;
7139 phc.assign_projid = false;
7140 /* default action */
7141 op = LFS_PROJECT_LIST;
7143 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7146 if (op != LFS_PROJECT_LIST) {
7148 "%s: cannot specify '-c' '-C' '-s' together\n",
7153 op = LFS_PROJECT_CHECK;
7156 if (op != LFS_PROJECT_LIST) {
7158 "%s: cannot specify '-c' '-C' '-s' together\n",
7163 op = LFS_PROJECT_CLEAR;
7166 if (op != LFS_PROJECT_LIST) {
7168 "%s: cannot specify '-c' '-C' '-s' together\n",
7173 phc.set_inherit = true;
7174 op = LFS_PROJECT_SET;
7180 phc.keep_projid = true;
7183 phc.recursive = true;
7186 phc.projid = strtoul(optarg, NULL, 0);
7187 phc.assign_projid = true;
7191 phc.newline = false;
7194 fprintf(stderr, "%s: invalid option '%c'\n",
7200 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7201 op = LFS_PROJECT_SET;
7202 phc.set_projid = true;
7203 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7204 phc.set_projid = true;
7208 case LFS_PROJECT_CHECK:
7209 if (phc.keep_projid) {
7211 "%s: '-k' is useless together with '-c'\n",
7216 case LFS_PROJECT_CLEAR:
7219 "%s: '-0' is useless together with '-C'\n",
7223 if (phc.assign_projid) {
7225 "%s: '-p' is useless together with '-C'\n",
7230 case LFS_PROJECT_SET:
7233 "%s: '-0' is useless together with '-s'\n",
7237 if (phc.keep_projid) {
7239 "%s: '-k' is useless together with '-s'\n",
7247 "%s: '-0' is useless for list operations\n",
7257 fprintf(stderr, "%s: missing file or directory target(s)\n",
7262 for (i = 0; i < argc; i++) {
7264 case LFS_PROJECT_CHECK:
7265 err = lfs_project_check(argv[i], &phc);
7267 case LFS_PROJECT_LIST:
7268 err = lfs_project_list(argv[i], &phc);
7270 case LFS_PROJECT_CLEAR:
7271 err = lfs_project_clear(argv[i], &phc);
7273 case LFS_PROJECT_SET:
7274 err = lfs_project_set(argv[i], &phc);
7286 static int lfs_quota(int argc, char **argv)
7289 char *mnt, *name = NULL;
7290 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
7291 .qc_type = ALLQUOTA };
7292 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
7293 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7295 __u32 valid = QC_GENERAL, idx = 0;
7296 bool human_readable = false;
7297 bool show_default = false;
7300 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
7303 show_default = true;
7308 show_default = true;
7313 show_default = true;
7317 if (qctl.qc_type != ALLQUOTA) {
7319 "%s quota: only one of -u, -g, or -p may be specified\n",
7323 qctl.qc_type = qtype;
7326 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7329 valid = qctl.qc_valid = QC_UUID;
7330 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7333 valid = qctl.qc_valid = QC_MDTIDX;
7334 idx = qctl.qc_idx = atoi(optarg);
7335 if (idx == 0 && *optarg != '0') {
7337 "%s quota: invalid MDT index '%s'\n",
7343 valid = qctl.qc_valid = QC_OSTIDX;
7344 idx = qctl.qc_idx = atoi(optarg);
7345 if (idx == 0 && *optarg != '0') {
7347 "%s quota: invalid OST index '%s'\n",
7359 human_readable = true;
7362 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7363 progname, argv[optind - 1]);
7368 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7369 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7370 optind == argc - 1 && !show_default) {
7372 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7375 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7376 qctl.qc_type = qtype;
7377 qctl.qc_valid = valid;
7378 if (qtype == USRQUOTA) {
7379 qctl.qc_id = geteuid();
7380 rc = uid2name(&name, qctl.qc_id);
7382 qctl.qc_id = getegid();
7383 rc = gid2name(&name, qctl.qc_id);
7384 memset(&qctl.qc_dqblk, 0,
7385 sizeof(qctl.qc_dqblk));
7390 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7391 human_readable, show_default);
7396 /* lfs quota -u username /path/to/lustre/mount */
7397 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7398 /* options should be followed by u/g-name and mntpoint */
7399 if ((!show_default && optind + 2 != argc) ||
7400 (show_default && optind + 1 != argc) ||
7401 qctl.qc_type == ALLQUOTA) {
7403 "%s quota: name and mount point must be specified\n",
7408 if (!show_default) {
7409 name = argv[optind++];
7410 switch (qctl.qc_type) {
7412 rc = name2uid(&qctl.qc_id, name);
7415 rc = name2gid(&qctl.qc_id, name);
7418 rc = name2projid(&qctl.qc_id, name);
7425 qctl.qc_valid = QC_GENERAL;
7426 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7431 qctl.qc_id = strtoul(name, &endptr, 10);
7432 if (*endptr != '\0') {
7433 fprintf(stderr, "%s quota: invalid id '%s'\n",
7438 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7439 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7445 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7446 human_readable, show_default);
7449 #endif /* HAVE_SYS_QUOTA_H! */
7451 static int flushctx_ioctl(char *mp)
7455 fd = open(mp, O_RDONLY);
7457 fprintf(stderr, "flushctx: error open %s: %s\n",
7458 mp, strerror(errno));
7462 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7464 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7465 mp, strerror(errno));
7471 static int lfs_flushctx(int argc, char **argv)
7473 int kdestroy = 0, c;
7474 char mntdir[PATH_MAX] = {'\0'};
7478 while ((c = getopt(argc, argv, "k")) != -1) {
7484 fprintf(stderr, "error: %s: option '-%c' "
7485 "unrecognized\n", argv[0], c);
7491 if ((rc = system("kdestroy > /dev/null")) != 0) {
7492 rc = WEXITSTATUS(rc);
7493 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7497 if (optind >= argc) {
7498 /* flush for all mounted lustre fs. */
7499 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7500 /* Check if we have a mount point */
7501 if (mntdir[0] == '\0')
7504 if (flushctx_ioctl(mntdir))
7507 mntdir[0] = '\0'; /* avoid matching in next loop */
7510 /* flush fs as specified */
7511 while (optind < argc) {
7512 if (flushctx_ioctl(argv[optind++]))
7519 static int lfs_changelog(int argc, char **argv)
7521 void *changelog_priv;
7522 struct changelog_rec *rec;
7523 long long startrec = 0, endrec = 0;
7525 struct option long_opts[] = {
7526 { .val = 'f', .name = "follow", .has_arg = no_argument },
7528 char short_opts[] = "f";
7531 while ((rc = getopt_long(argc, argv, short_opts,
7532 long_opts, NULL)) != -1) {
7539 "%s changelog: unrecognized option '%s'\n",
7540 progname, argv[optind - 1]);
7544 if (optind >= argc) {
7545 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7550 mdd = argv[optind++];
7552 startrec = strtoll(argv[optind++], NULL, 10);
7554 endrec = strtoll(argv[optind++], NULL, 10);
7556 rc = llapi_changelog_start(&changelog_priv,
7557 CHANGELOG_FLAG_BLOCK |
7558 CHANGELOG_FLAG_JOBID |
7559 CHANGELOG_FLAG_EXTRA_FLAGS |
7560 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7563 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7564 progname, strerror(errno = -rc));
7568 rc = llapi_changelog_set_xflags(changelog_priv,
7569 CHANGELOG_EXTRA_FLAG_UIDGID |
7570 CHANGELOG_EXTRA_FLAG_NID |
7571 CHANGELOG_EXTRA_FLAG_OMODE |
7572 CHANGELOG_EXTRA_FLAG_XATTR);
7575 "%s changelog: cannot set xflags for changelog: %s\n",
7576 progname, strerror(errno = -rc));
7580 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7584 if (endrec && rec->cr_index > endrec) {
7585 llapi_changelog_free(&rec);
7588 if (rec->cr_index < startrec) {
7589 llapi_changelog_free(&rec);
7593 secs = rec->cr_time >> 30;
7594 gmtime_r(&secs, &ts);
7595 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7596 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7597 changelog_type2str(rec->cr_type),
7598 ts.tm_hour, ts.tm_min, ts.tm_sec,
7599 (int)(rec->cr_time & ((1 << 30) - 1)),
7600 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7601 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7603 if (rec->cr_flags & CLF_JOBID) {
7604 struct changelog_ext_jobid *jid =
7605 changelog_rec_jobid(rec);
7607 if (jid->cr_jobid[0] != '\0')
7608 printf(" j=%s", jid->cr_jobid);
7611 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7612 struct changelog_ext_extra_flags *ef =
7613 changelog_rec_extra_flags(rec);
7615 printf(" ef=0x%llx",
7616 (unsigned long long)ef->cr_extra_flags);
7618 if (ef->cr_extra_flags & CLFE_UIDGID) {
7619 struct changelog_ext_uidgid *uidgid =
7620 changelog_rec_uidgid(rec);
7622 printf(" u=%llu:%llu",
7623 (unsigned long long)uidgid->cr_uid,
7624 (unsigned long long)uidgid->cr_gid);
7626 if (ef->cr_extra_flags & CLFE_NID) {
7627 struct changelog_ext_nid *nid =
7628 changelog_rec_nid(rec);
7631 libcfs_nid2str(nid->cr_nid));
7634 if (ef->cr_extra_flags & CLFE_OPEN) {
7635 struct changelog_ext_openmode *omd =
7636 changelog_rec_openmode(rec);
7637 char mode[] = "---";
7639 /* exec mode must be exclusive */
7640 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7643 if (omd->cr_openflags & MDS_FMODE_READ)
7645 if (omd->cr_openflags &
7652 if (strcmp(mode, "---") != 0)
7653 printf(" m=%s", mode);
7657 if (ef->cr_extra_flags & CLFE_XATTR) {
7658 struct changelog_ext_xattr *xattr =
7659 changelog_rec_xattr(rec);
7661 if (xattr->cr_xattr[0] != '\0')
7662 printf(" x=%s", xattr->cr_xattr);
7666 if (rec->cr_namelen)
7667 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7668 rec->cr_namelen, changelog_rec_name(rec));
7670 if (rec->cr_flags & CLF_RENAME) {
7671 struct changelog_ext_rename *rnm =
7672 changelog_rec_rename(rec);
7674 if (!fid_is_zero(&rnm->cr_sfid))
7675 printf(" s="DFID" sp="DFID" %.*s",
7676 PFID(&rnm->cr_sfid),
7677 PFID(&rnm->cr_spfid),
7678 (int)changelog_rec_snamelen(rec),
7679 changelog_rec_sname(rec));
7683 llapi_changelog_free(&rec);
7686 llapi_changelog_fini(&changelog_priv);
7689 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7690 progname, strerror(errno = -rc));
7692 return (rc == 1 ? 0 : rc);
7695 static int lfs_changelog_clear(int argc, char **argv)
7703 endrec = strtoll(argv[3], NULL, 10);
7705 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7708 fprintf(stderr, "%s: record out of range: %llu\n",
7710 else if (rc == -ENOENT)
7711 fprintf(stderr, "%s: no changelog user: %s\n",
7714 fprintf(stderr, "%s error: %s\n", argv[0],
7723 static int lfs_fid2path(int argc, char **argv)
7725 struct option long_opts[] = {
7726 { .val = 'c', .name = "cur", .has_arg = no_argument },
7727 { .val = 'l', .name = "link", .has_arg = required_argument },
7728 { .val = 'r', .name = "rec", .has_arg = required_argument },
7730 char short_opts[] = "cl:r:";
7731 char *device, *fid, *path;
7732 long long recno = -1;
7737 char *endptr = NULL;
7739 while ((rc = getopt_long(argc, argv, short_opts,
7740 long_opts, NULL)) != -1) {
7746 linkno = strtol(optarg, &endptr, 10);
7747 if (*endptr != '\0') {
7749 "%s fid2path: invalid linkno '%s'\n",
7755 recno = strtoll(optarg, &endptr, 10);
7756 if (*endptr != '\0') {
7758 "%s fid2path: invalid recno '%s'\n",
7765 "%s fid2path: unrecognized option '%s'\n",
7766 progname, argv[optind - 1]);
7773 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7778 device = argv[optind++];
7779 path = calloc(1, PATH_MAX);
7783 "%s fid2path: cannot allocate memory for path: %s\n",
7784 progname, strerror(-rc));
7789 while (optind < argc) {
7790 fid = argv[optind++];
7792 lnktmp = (linkno >= 0) ? linkno : 0;
7794 int oldtmp = lnktmp;
7795 long long rectmp = recno;
7797 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7801 "%s fid2path: cannot find '%s': %s\n",
7802 progname, fid, strerror(errno = -rc2));
7809 fprintf(stdout, "%lld ", rectmp);
7810 if (device[0] == '/') {
7811 fprintf(stdout, "%s", device);
7812 if (device[strlen(device) - 1] != '/')
7813 fprintf(stdout, "/");
7814 } else if (path[0] == '\0') {
7815 fprintf(stdout, "/");
7817 fprintf(stdout, "%s\n", path);
7820 /* specified linkno */
7822 if (oldtmp == lnktmp)
7832 static int lfs_path2fid(int argc, char **argv)
7834 struct option long_opts[] = {
7835 { .val = 'p', .name = "parents", .has_arg = no_argument },
7838 const char short_opts[] = "p";
7839 const char *sep = "";
7842 bool show_parents = false;
7844 while ((rc = getopt_long(argc, argv, short_opts,
7845 long_opts, NULL)) != -1) {
7848 show_parents = true;
7852 "%s path2fid: unrecognized option '%s'\n",
7853 progname, argv[optind - 1]);
7858 if (optind > argc - 1) {
7859 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7863 else if (optind < argc - 1)
7867 for (path = argv + optind; *path != NULL; path++) {
7869 if (!show_parents) {
7870 err = llapi_path2fid(*path, &fid);
7872 printf("%s%s"DFID"\n",
7873 *sep != '\0' ? *path : "", sep,
7876 char name[NAME_MAX + 1];
7877 unsigned int linkno = 0;
7879 while ((err = llapi_path2parent(*path, linkno, &fid,
7880 name, sizeof(name))) == 0) {
7881 if (*sep != '\0' && linkno == 0)
7882 printf("%s%s", *path, sep);
7884 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7889 /* err == -ENODATA is end-of-loop */
7890 if (linkno > 0 && err == -ENODATA) {
7898 "%s path2fid: cannot get %sfid for '%s': %s\n",
7899 progname, show_parents ? "parent " : "", *path,
7911 #define MAX_ERRNO 4095
7912 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
7914 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
7918 rc = llapi_rmfid(device, fa);
7920 fprintf(stderr, "rmfid(): rc = %d\n", rc);
7924 for (k = 0; k < fa->fa_nr; k++) {
7925 rc = (__s32)fa->fa_fids[k].f_ver;
7926 if (!IS_ERR_VALUE(rc))
7932 fa->fa_fids[k].f_ver = 0;
7933 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
7934 PFID(&fa->fa_fids[k]), rc);
7940 static int lfs_rmfid(int argc, char **argv)
7942 char *fidstr, *device;
7943 int rc = 0, rc2, nr;
7944 struct fid_array *fa;
7946 if (optind > argc - 1) {
7947 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
7951 device = argv[optind++];
7954 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
7960 while (optind < argc) {
7963 fidstr = argv[optind++];
7964 while (*fidstr == '[')
7966 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
7968 fprintf(stderr, "unrecognized FID: %s\n",
7973 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
7974 /* start another batch */
7975 rc2 = lfs_rmfid_and_show_errors(device, fa);
7982 rc2 = lfs_rmfid_and_show_errors(device, fa);
7990 static int lfs_data_version(int argc, char **argv)
7997 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8000 fprintf(stderr, "%s data_version: FILE must be specified\n",
8005 while ((c = getopt(argc, argv, "nrw")) != -1) {
8008 data_version_flags = 0;
8011 data_version_flags |= LL_DV_RD_FLUSH;
8014 data_version_flags |= LL_DV_WR_FLUSH;
8018 "%s data_version: unrecognized option '%s'\n",
8019 progname, argv[optind - 1]);
8023 if (optind == argc) {
8024 fprintf(stderr, "%s data_version: FILE must be specified\n",
8029 path = argv[optind];
8030 fd = open(path, O_RDONLY);
8033 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8034 progname, path, strerror(-rc));
8038 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8041 "%s data_version: cannot get version for '%s': %s\n",
8042 progname, path, strerror(-rc));
8044 printf("%ju" "\n", (uintmax_t)data_version);
8050 static int lfs_hsm_state(int argc, char **argv)
8055 struct hsm_user_state hus;
8063 rc = llapi_hsm_state_get(path, &hus);
8065 fprintf(stderr, "can't get hsm state for %s: %s\n",
8066 path, strerror(errno = -rc));
8070 /* Display path name and status flags */
8071 printf("%s: (0x%08x)", path, hus.hus_states);
8073 if (hus.hus_states & HS_RELEASED)
8074 printf(" released");
8075 if (hus.hus_states & HS_EXISTS)
8077 if (hus.hus_states & HS_DIRTY)
8079 if (hus.hus_states & HS_ARCHIVED)
8080 printf(" archived");
8081 /* Display user-settable flags */
8082 if (hus.hus_states & HS_NORELEASE)
8083 printf(" never_release");
8084 if (hus.hus_states & HS_NOARCHIVE)
8085 printf(" never_archive");
8086 if (hus.hus_states & HS_LOST)
8087 printf(" lost_from_hsm");
8089 if (hus.hus_archive_id != 0)
8090 printf(", archive_id:%d", hus.hus_archive_id);
8093 } while (++i < argc);
8098 #define LFS_HSM_SET 0
8099 #define LFS_HSM_CLEAR 1
8102 * Generic function to set or clear HSM flags.
8103 * Used by hsm_set and hsm_clear.
8105 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8107 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8109 struct option long_opts[] = {
8110 { .val = 'A', .name = "archived", .has_arg = no_argument },
8111 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8112 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8113 { .val = 'e', .name = "exists", .has_arg = no_argument },
8114 { .val = 'l', .name = "lost", .has_arg = no_argument },
8115 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8116 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8118 char short_opts[] = "lraAdei:";
8122 __u32 archive_id = 0;
8128 while ((c = getopt_long(argc, argv, short_opts,
8129 long_opts, NULL)) != -1) {
8135 mask |= HS_NOARCHIVE;
8138 mask |= HS_ARCHIVED;
8141 mask |= HS_NORELEASE;
8150 archive_id = strtol(optarg, &end, 10);
8152 fprintf(stderr, "invalid archive_id: '%s'\n",
8160 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8161 argv[0], argv[optind - 1]);
8166 /* User should have specified a flag */
8170 while (optind < argc) {
8172 path = argv[optind];
8174 /* If mode == 0, this means we apply the mask. */
8175 if (mode == LFS_HSM_SET)
8176 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8178 rc = llapi_hsm_state_set(path, 0, mask, 0);
8181 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8182 path, strerror(errno = -rc));
8191 static int lfs_hsm_action(int argc, char **argv)
8196 struct hsm_current_action hca;
8197 struct hsm_extent he;
8198 enum hsm_user_action hua;
8199 enum hsm_progress_states hps;
8207 rc = llapi_hsm_current_action(path, &hca);
8209 fprintf(stderr, "can't get hsm action for %s: %s\n",
8210 path, strerror(errno = -rc));
8213 he = hca.hca_location;
8214 hua = hca.hca_action;
8215 hps = hca.hca_state;
8217 printf("%s: %s", path, hsm_user_action2name(hua));
8219 /* Skip file without action */
8220 if (hca.hca_action == HUA_NONE) {
8225 printf(" %s ", hsm_progress_state2name(hps));
8227 if ((hps == HPS_RUNNING) &&
8228 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8229 printf("(%llu bytes moved)\n",
8230 (unsigned long long)he.length);
8231 else if ((he.offset + he.length) == LUSTRE_EOF)
8232 printf("(from %llu to EOF)\n",
8233 (unsigned long long)he.offset);
8235 printf("(from %llu to %llu)\n",
8236 (unsigned long long)he.offset,
8237 (unsigned long long)(he.offset + he.length));
8239 } while (++i < argc);
8244 static int lfs_hsm_set(int argc, char **argv)
8246 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8249 static int lfs_hsm_clear(int argc, char **argv)
8251 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8255 * Check file state and return its fid, to be used by lfs_hsm_request().
8257 * \param[in] file Path to file to check
8258 * \param[in,out] fid Pointer to allocated lu_fid struct.
8259 * \param[in,out] last_dev Pointer to last device id used.
8261 * \return 0 on success.
8263 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8269 rc = lstat(file, &st);
8271 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8274 /* Checking for regular file as archiving as posix copytool
8275 * rejects archiving files other than regular files
8277 if (!S_ISREG(st.st_mode)) {
8278 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8281 /* A request should be ... */
8282 if (*last_dev != st.st_dev && *last_dev != 0) {
8283 fprintf(stderr, "All files should be "
8284 "on the same filesystem: %s\n", file);
8287 *last_dev = st.st_dev;
8289 rc = llapi_path2fid(file, fid);
8291 fprintf(stderr, "Cannot read FID of %s: %s\n",
8292 file, strerror(-rc));
8298 /* Fill an HSM HUR item with a given file name.
8300 * If mntpath is set, then the filename is actually a FID, and no
8301 * lookup on the filesystem will be performed.
8303 * \param[in] hur the user request to fill
8304 * \param[in] idx index of the item inside the HUR to fill
8305 * \param[in] mntpath mountpoint of Lustre
8306 * \param[in] fname filename (if mtnpath is NULL)
8307 * or FID (if mntpath is set)
8308 * \param[in] last_dev pointer to last device id used
8310 * \retval 0 on success
8311 * \retval CMD_HELP or a negative errno on error
8313 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8314 const char *mntpath, const char *fname,
8317 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8320 hui->hui_extent.length = -1;
8322 if (mntpath != NULL) {
8325 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
8329 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8334 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8338 hur->hur_request.hr_itemcount++;
8343 static int lfs_hsm_request(int argc, char **argv, int action)
8345 struct option long_opts[] = {
8346 { .val = 'a', .name = "archive", .has_arg = required_argument },
8347 { .val = 'D', .name = "data", .has_arg = required_argument },
8348 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8349 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8352 char short_opts[] = "l:D:a:m:";
8353 struct hsm_user_request *hur, *oldhur;
8358 char *filelist = NULL;
8359 char fullpath[PATH_MAX];
8360 char *opaque = NULL;
8364 int nbfile_alloc = 0;
8365 char *some_file = NULL;
8366 char *mntpath = NULL;
8372 while ((c = getopt_long(argc, argv, short_opts,
8373 long_opts, NULL)) != -1) {
8382 if (action != HUA_ARCHIVE &&
8383 action != HUA_REMOVE) {
8385 "error: -a is supported only "
8386 "when archiving or removing\n");
8389 archive_id = atoi(optarg);
8392 if (some_file == NULL) {
8394 some_file = strdup(optarg);
8400 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8401 argv[0], argv[optind - 1]);
8406 /* All remaining args are files, so we have at least nbfile */
8407 nbfile = argc - optind;
8409 if ((nbfile == 0) && (filelist == NULL))
8413 opaque_len = strlen(opaque);
8415 /* Alloc the request structure with enough place to store all files
8416 * from command line. */
8417 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8419 fprintf(stderr, "Cannot create the request: %s\n",
8423 nbfile_alloc = nbfile;
8425 hur->hur_request.hr_action = action;
8426 hur->hur_request.hr_archive_id = archive_id;
8427 hur->hur_request.hr_flags = 0;
8429 /* All remaining args are files, add them */
8430 if (nbfile != 0 && some_file == NULL)
8431 some_file = strdup(argv[optind]);
8433 for (i = 0; i < nbfile; i++) {
8434 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8440 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8442 /* If a filelist was specified, read the filelist from it. */
8443 if (filelist != NULL) {
8444 fp = fopen(filelist, "r");
8446 fprintf(stderr, "Cannot read the file list %s: %s\n",
8447 filelist, strerror(errno));
8452 while ((rc = getline(&line, &len, fp)) != -1) {
8453 /* If allocated buffer was too small, get something
8455 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8458 nbfile_alloc = nbfile_alloc * 2 + 1;
8460 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8463 fprintf(stderr, "hsm: cannot allocate "
8464 "the request: %s\n",
8471 size = hur_len(oldhur);
8473 fprintf(stderr, "hsm: cannot allocate "
8474 "%u files + %u bytes data\n",
8475 oldhur->hur_request.hr_itemcount,
8476 oldhur->hur_request.hr_data_len);
8483 memcpy(hur, oldhur, size);
8488 if (line[strlen(line) - 1] == '\n')
8489 line[strlen(line) - 1] = '\0';
8491 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8492 mntpath, line, &last_dev);
8498 if (some_file == NULL) {
8508 /* If a --data was used, add it to the request */
8509 hur->hur_request.hr_data_len = opaque_len;
8511 memcpy(hur_data(hur), opaque, opaque_len);
8513 /* Send the HSM request */
8514 if (realpath(some_file, fullpath) == NULL) {
8515 fprintf(stderr, "Could not find path '%s': %s\n",
8516 some_file, strerror(errno));
8518 rc = llapi_hsm_request(fullpath, hur);
8520 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8521 some_file, strerror(-rc));
8531 static int lfs_hsm_archive(int argc, char **argv)
8533 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8536 static int lfs_hsm_restore(int argc, char **argv)
8538 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8541 static int lfs_hsm_release(int argc, char **argv)
8543 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8546 static int lfs_hsm_remove(int argc, char **argv)
8548 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8551 static int lfs_hsm_cancel(int argc, char **argv)
8553 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8556 static int lfs_swap_layouts(int argc, char **argv)
8561 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8562 SWAP_LAYOUTS_KEEP_MTIME |
8563 SWAP_LAYOUTS_KEEP_ATIME);
8566 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8568 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8570 int lfs_get_mode(const char *string)
8572 enum lock_mode_user mode;
8574 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8575 if (lock_mode_names[mode] == NULL)
8577 if (strcmp(string, lock_mode_names[mode]) == 0)
8584 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8586 enum lu_ladvise_type advice;
8589 advice < ARRAY_SIZE(ladvise_names); advice++) {
8590 if (ladvise_names[advice] == NULL)
8592 if (strcmp(string, ladvise_names[advice]) == 0)
8596 return LU_LADVISE_INVALID;
8599 static int lfs_ladvise(int argc, char **argv)
8601 struct option long_opts[] = {
8602 { .val = 'a', .name = "advice", .has_arg = required_argument },
8603 { .val = 'b', .name = "background", .has_arg = no_argument },
8604 { .val = 'e', .name = "end", .has_arg = required_argument },
8605 { .val = 'l', .name = "length", .has_arg = required_argument },
8606 { .val = 'm', .name = "mode", .has_arg = required_argument },
8607 { .val = 's', .name = "start", .has_arg = required_argument },
8608 { .val = 'u', .name = "unset", .has_arg = no_argument },
8610 char short_opts[] = "a:be:l:m:s:u";
8615 struct llapi_lu_ladvise advice;
8616 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8617 unsigned long long start = 0;
8618 unsigned long long end = LUSTRE_EOF;
8619 unsigned long long length = 0;
8620 unsigned long long size_units;
8621 unsigned long long flags = 0;
8625 while ((c = getopt_long(argc, argv, short_opts,
8626 long_opts, NULL)) != -1) {
8629 advice_type = lfs_get_ladvice(optarg);
8630 if (advice_type == LU_LADVISE_INVALID) {
8631 fprintf(stderr, "%s: invalid advice type "
8632 "'%s'\n", argv[0], optarg);
8633 fprintf(stderr, "Valid types:");
8635 for (advice_type = 0;
8636 advice_type < ARRAY_SIZE(ladvise_names);
8638 if (ladvise_names[advice_type] == NULL)
8640 fprintf(stderr, " %s",
8641 ladvise_names[advice_type]);
8643 fprintf(stderr, "\n");
8656 rc = llapi_parse_size(optarg, &end,
8659 fprintf(stderr, "%s: bad end offset '%s'\n",
8666 rc = llapi_parse_size(optarg, &start,
8669 fprintf(stderr, "%s: bad start offset "
8670 "'%s'\n", argv[0], optarg);
8676 rc = llapi_parse_size(optarg, &length,
8679 fprintf(stderr, "%s: bad length '%s'\n",
8685 mode = lfs_get_mode(optarg);
8687 fprintf(stderr, "%s: bad mode '%s', valid "
8688 "modes are READ or WRITE\n",
8696 fprintf(stderr, "%s: option '%s' unrecognized\n",
8697 argv[0], argv[optind - 1]);
8702 if (advice_type == LU_LADVISE_INVALID) {
8703 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8704 fprintf(stderr, "Valid types:");
8705 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8707 if (ladvise_names[advice_type] == NULL)
8709 fprintf(stderr, " %s", ladvise_names[advice_type]);
8711 fprintf(stderr, "\n");
8715 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8716 fprintf(stderr, "%s: Lock no expand advice is a per file "
8717 "descriptor advice, so when called from lfs, "
8718 "it does nothing.\n", argv[0]);
8722 if (argc <= optind) {
8723 fprintf(stderr, "%s: please give one or more file names\n",
8728 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8729 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8734 if (end == LUSTRE_EOF && length != 0)
8735 end = start + length;
8738 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8739 argv[0], start, end);
8743 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8744 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8749 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8750 fprintf(stderr, "%s: mode is required with lockahead\n",
8755 while (optind < argc) {
8758 path = argv[optind++];
8760 fd = open(path, O_RDONLY);
8762 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8763 argv[0], path, strerror(errno));
8768 advice.lla_start = start;
8769 advice.lla_end = end;
8770 advice.lla_advice = advice_type;
8771 advice.lla_value1 = 0;
8772 advice.lla_value2 = 0;
8773 advice.lla_value3 = 0;
8774 advice.lla_value4 = 0;
8775 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8776 advice.lla_lockahead_mode = mode;
8777 advice.lla_peradvice_flags = flags;
8780 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8783 fprintf(stderr, "%s: cannot give advice '%s' to file "
8784 "'%s': %s\n", argv[0],
8785 ladvise_names[advice_type],
8786 path, strerror(errno));
8792 if (rc == 0 && rc2 < 0)
8798 static const char *const heat_names[] = LU_HEAT_NAMES;
8800 static int lfs_heat_get(int argc, char **argv)
8802 struct lu_heat *heat;
8811 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8813 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8818 while (optind < argc) {
8819 path = argv[optind++];
8821 fd = open(path, O_RDONLY);
8823 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8824 argv[0], path, strerror(errno));
8829 heat->lh_count = OBD_HEAT_COUNT;
8830 rc2 = llapi_heat_get(fd, heat);
8833 fprintf(stderr, "%s: cannot get heat of file '%s'"
8834 ": %s\n", argv[0], path, strerror(errno));
8838 printf("flags: %x\n", heat->lh_flags);
8839 for (i = 0; i < heat->lh_count; i++)
8840 printf("%s: %llu\n", heat_names[i],
8841 (unsigned long long)heat->lh_heat[i]);
8843 if (rc == 0 && rc2 < 0)
8851 static int lfs_heat_set(int argc, char **argv)
8853 struct option long_opts[] = {
8854 { .val = 'c', .name = "clear", .has_arg = no_argument },
8855 { .val = 'o', .name = "off", .has_arg = no_argument },
8856 { .val = 'O', .name = "on", .has_arg = no_argument },
8858 enum lu_heat_flag flags = 0;
8868 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8871 flags |= LU_HEAT_FLAG_CLEAR;
8874 flags |= LU_HEAT_FLAG_CLEAR;
8875 flags |= LU_HEAT_FLAG_OFF;
8878 flags &= ~LU_HEAT_FLAG_OFF;
8883 fprintf(stderr, "%s: option '%s' unrecognized\n",
8884 argv[0], argv[optind - 1]);
8889 if (argc <= optind) {
8890 fprintf(stderr, "%s: please give one or more file names\n",
8895 while (optind < argc) {
8896 path = argv[optind++];
8898 fd = open(path, O_RDONLY);
8900 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8901 argv[0], path, strerror(errno));
8906 rc2 = llapi_heat_set(fd, flags);
8909 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8910 ": %s\n", argv[0], path, strerror(errno));
8914 if (rc == 0 && rc2 < 0)
8920 /** The input string contains a comma delimited list of component ids and
8921 * ranges, for example "1,2-4,7".
8923 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8925 bool end_of_loop = false;
8933 while (!end_of_loop) {
8937 char *endptr = NULL;
8940 ptr = strchrnul(arg, ',');
8941 end_of_loop = *ptr == '\0';
8944 start_index = strtol(arg, &endptr, 0);
8945 if (endptr == arg) /* no data at all */
8947 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8949 if (start_index < 0)
8952 end_index = start_index;
8953 if (*endptr == '-') {
8954 end_index = strtol(endptr + 1, &endptr, 0);
8955 if (*endptr != '\0')
8957 if (end_index < start_index)
8961 for (i = start_index; i <= end_index && size > 0; i++) {
8964 /* remove duplicate */
8965 for (j = 0; j < nr; j++) {
8969 if (j == nr) { /* no duplicate */
8975 if (size == 0 && i < end_index)
8982 if (!end_of_loop && ptr != NULL)
8985 return rc < 0 ? rc : nr;
8989 * struct verify_mirror_id - Mirror id to be verified.
8990 * @mirror_id: A specified mirror id.
8991 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8993 struct verify_mirror_id {
8999 * compare_mirror_ids() - Compare mirror ids.
9000 * @layout: Mirror component list.
9001 * @cbdata: Callback data in verify_mirror_id structure.
9003 * This is a callback function called by llapi_layout_comp_iterate()
9004 * to compare the specified mirror id with the one in the current
9005 * component of @layout. If they are the same, then the specified
9006 * mirror id is valid.
9008 * Return: a negative error code on failure or
9009 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9010 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9013 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9015 struct verify_mirror_id *mirror_id_cbdata =
9016 (struct verify_mirror_id *)cbdata;
9020 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9024 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9025 progname, strerror(errno));
9029 if (mirror_id_cbdata->mirror_id == mirror_id) {
9030 mirror_id_cbdata->is_valid_id = true;
9031 return LLAPI_LAYOUT_ITER_STOP;
9034 return LLAPI_LAYOUT_ITER_CONT;
9038 * verify_mirror_ids() - Verify specified mirror ids.
9039 * @fname: Mirrored file name.
9040 * @mirror_ids: Specified mirror ids to be verified.
9041 * @ids_nr: Number of specified mirror ids.
9043 * This function verifies that specified @mirror_ids are valid
9044 * in the mirrored file @fname.
9046 * Return: 0 on success or a negative error code on failure.
9049 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9051 struct llapi_layout *layout = NULL;
9052 struct verify_mirror_id mirror_id_cbdata = { 0 };
9063 if (stat(fname, &stbuf) < 0) {
9064 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9065 progname, fname, strerror(errno));
9070 if (!S_ISREG(stbuf.st_mode)) {
9071 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9077 fd = open(fname, O_DIRECT | O_RDONLY);
9079 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9080 progname, fname, strerror(errno));
9085 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9087 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9088 progname, fname, strerror(errno));
9092 layout = llapi_layout_get_by_fd(fd, 0);
9093 if (layout == NULL) {
9094 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9095 progname, fname, strerror(errno));
9097 llapi_lease_release(fd);
9101 rc = llapi_layout_flags_get(layout, &flr_state);
9103 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9104 progname, fname, strerror(errno));
9109 flr_state &= LCM_FL_FLR_MASK;
9110 switch (flr_state) {
9113 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9114 progname, fname, llapi_layout_flags_string(flr_state));
9121 for (i = 0; i < ids_nr; i++) {
9122 mirror_id_cbdata.mirror_id = mirror_ids[i];
9123 mirror_id_cbdata.is_valid_id = false;
9125 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9130 "%s: '%s' failed to verify mirror id: %u.\n",
9131 progname, fname, mirror_ids[i]);
9135 if (!mirror_id_cbdata.is_valid_id) {
9138 "%s: '%s' invalid specified mirror id: %u.\n",
9139 progname, fname, mirror_ids[i]);
9145 llapi_layout_free(layout);
9146 llapi_lease_release(fd);
9154 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9155 __u16 *mirror_ids, int ids_nr)
9157 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9158 struct llapi_layout *layout;
9169 if (stat(fname, &stbuf) < 0) {
9170 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9171 progname, fname, strerror(errno));
9175 if (!S_ISREG(stbuf.st_mode)) {
9176 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9182 fd = open(fname, O_DIRECT | O_RDWR);
9184 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9185 progname, fname, strerror(errno));
9190 layout = llapi_layout_get_by_fd(fd, 0);
9191 if (layout == NULL) {
9192 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9193 progname, fname, strerror(errno));
9198 rc = llapi_layout_flags_get(layout, &flr_state);
9200 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9201 progname, fname, strerror(errno));
9206 flr_state &= LCM_FL_FLR_MASK;
9207 if (flr_state == LCM_FL_NONE) {
9209 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9214 /* get stale component info */
9215 comp_size = llapi_mirror_find_stale(layout, comp_array,
9216 ARRAY_SIZE(comp_array),
9217 mirror_ids, ids_nr);
9218 if (comp_size <= 0) {
9223 ioc->lil_mode = LL_LEASE_WRLCK;
9224 ioc->lil_flags = LL_LEASE_RESYNC;
9225 rc = llapi_lease_set(fd, ioc);
9227 if (rc == -EALREADY)
9231 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9232 progname, fname, strerror(-rc));
9236 /* get the read range [start, end) */
9237 start = comp_array[0].lrc_start;
9238 end = comp_array[0].lrc_end;
9239 for (idx = 1; idx < comp_size; idx++) {
9240 if (comp_array[idx].lrc_start < start)
9241 start = comp_array[idx].lrc_start;
9242 if (end < comp_array[idx].lrc_end)
9243 end = comp_array[idx].lrc_end;
9246 rc = llapi_lease_check(fd);
9247 if (rc != LL_LEASE_WRLCK) {
9248 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9253 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9256 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9257 progname, fname, strerror(-rc));
9259 /* need to do the lease unlock even resync fails */
9260 ioc->lil_mode = LL_LEASE_UNLCK;
9261 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9263 for (idx = 0; idx < comp_size; idx++) {
9264 if (comp_array[idx].lrc_synced) {
9265 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9270 rc2 = llapi_lease_set(fd, ioc);
9272 * llapi_lease_set returns lease mode when it request to unlock
9276 /* rc2 == 0 means lost lease lock */
9277 if (rc2 == 0 && rc == 0)
9279 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9281 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9285 llapi_layout_free(layout);
9292 static inline int lfs_mirror_resync(int argc, char **argv)
9294 struct ll_ioc_lease *ioc = NULL;
9295 __u16 mirror_ids[128] = { 0 };
9300 struct option long_opts[] = {
9301 { .val = 'o', .name = "only", .has_arg = required_argument },
9304 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9307 rc = parse_mirror_ids(mirror_ids,
9308 sizeof(mirror_ids) / sizeof(__u16),
9312 "%s: bad mirror ids '%s'.\n",
9319 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9320 argv[0], argv[optind - 1]);
9326 if (argc == optind) {
9327 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9332 if (ids_nr > 0 && argc > optind + 1) {
9334 "%s: option '--only' cannot be used upon multiple files.\n",
9342 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9347 /* set the lease on the file */
9348 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9350 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9351 argv[0], strerror(errno));
9356 for (; optind < argc; optind++) {
9357 rc = lfs_mirror_resync_file(argv[optind], ioc,
9358 mirror_ids, ids_nr);
9359 /* ignore previous file's error, continue with next file */
9362 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9370 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9372 struct llapi_layout *layout;
9375 layout = llapi_layout_get_by_fd(fd, 0);
9376 if (layout == NULL) {
9377 fprintf(stderr, "could not get layout.\n");
9381 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9383 fprintf(stderr, "failed to iterate layout\n");
9384 llapi_layout_free(layout);
9387 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9388 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9389 llapi_layout_free(layout);
9393 llapi_layout_free(layout);
9399 * Check whether two files are the same file
9400 * \retval 0 same file
9401 * \retval 1 not the same file
9402 * \retval <0 error code
9404 static inline int check_same_file(const char *f1, const char *f2)
9409 if (stat(f1, &stbuf1) < 0) {
9410 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9411 progname, f1, strerror(errno));
9415 if (stat(f2, &stbuf2) < 0) {
9416 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9417 progname, f2, strerror(errno));
9421 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9422 stbuf1.st_ino == stbuf2.st_ino)
9428 static inline int lfs_mirror_read(int argc, char **argv)
9431 __u16 mirror_id = 0;
9432 const char *outfile = NULL;
9438 const size_t buflen = 4 << 20;
9440 struct option long_opts[] = {
9441 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9442 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9445 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9450 mirror_id = strtoul(optarg, &end, 0);
9451 if (*end != '\0' || mirror_id == 0) {
9453 "%s %s: invalid mirror ID '%s'\n",
9454 progname, argv[0], optarg);
9462 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9463 progname, argv[optind - 1]);
9468 if (argc == optind) {
9469 fprintf(stderr, "%s %s: no mirrored file provided\n",
9472 } else if (argc > optind + 1) {
9473 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9477 if (mirror_id == 0) {
9478 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9483 /* open mirror file */
9484 fname = argv[optind];
9487 rc = check_same_file(fname, outfile);
9490 "%s %s: output file cannot be the mirrored file\n",
9498 fd = open(fname, O_DIRECT | O_RDONLY);
9500 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9501 progname, argv[0], fname, strerror(errno));
9505 /* verify mirror id */
9506 rc = verify_mirror_id_by_fd(fd, mirror_id);
9509 "%s %s: cannot find mirror with ID %u in '%s'\n",
9510 progname, argv[0], mirror_id, fname);
9514 /* open output file */
9516 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9518 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9519 progname, argv[0], outfile, strerror(errno));
9524 outfd = STDOUT_FILENO;
9527 /* allocate buffer */
9528 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9530 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9531 progname, argv[0], rc);
9538 ssize_t written = 0;
9540 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9541 if (bytes_read < 0) {
9544 "%s %s: fail to read data from mirror %u: %s\n",
9545 progname, argv[0], mirror_id, strerror(-rc));
9550 if (bytes_read == 0)
9553 while (written < bytes_read) {
9556 written2 = write(outfd, buf + written,
9557 bytes_read - written);
9560 "%s %s: fail to write %s: %s\n",
9561 progname, argv[0], outfile ? : "STDOUT",
9566 written += written2;
9569 if (written != bytes_read) {
9571 "%s %s: written %ld bytes does not match with %ld read.\n",
9572 progname, argv[0], written, bytes_read);
9594 static inline int lfs_mirror_write(int argc, char **argv)
9597 __u16 mirror_id = 0;
9598 const char *inputfile = NULL;
9604 const size_t buflen = 4 << 20;
9606 size_t page_size = sysconf(_SC_PAGESIZE);
9607 struct ll_ioc_lease_id ioc;
9609 struct option long_opts[] = {
9610 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9611 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9614 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9619 mirror_id = strtoul(optarg, &end, 0);
9620 if (*end != '\0' || mirror_id == 0) {
9622 "%s %s: invalid mirror ID '%s'\n",
9623 progname, argv[0], optarg);
9631 fprintf(stderr, "%s: option '%s' unrecognized\n",
9632 progname, argv[optind - 1]);
9637 if (argc == optind) {
9638 fprintf(stderr, "%s %s: no mirrored file provided\n",
9641 } else if (argc > optind + 1) {
9642 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9646 if (mirror_id == 0) {
9647 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9652 /* open mirror file */
9653 fname = argv[optind];
9656 rc = check_same_file(fname, inputfile);
9659 "%s %s: input file cannot be the mirrored file\n",
9667 fd = open(fname, O_DIRECT | O_WRONLY);
9669 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9670 progname, argv[0], fname, strerror(errno));
9674 /* verify mirror id */
9675 rc = verify_mirror_id_by_fd(fd, mirror_id);
9678 "%s %s: cannot find mirror with ID %u in '%s'\n",
9679 progname, argv[0], mirror_id, fname);
9683 /* open input file */
9685 inputfd = open(inputfile, O_RDONLY, 0644);
9687 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9688 progname, argv[0], inputfile, strerror(errno));
9693 inputfd = STDIN_FILENO;
9696 /* allocate buffer */
9697 rc = posix_memalign(&buf, page_size, buflen);
9699 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9700 progname, argv[0], rc);
9704 /* prepare target mirror components instantiation */
9705 ioc.lil_mode = LL_LEASE_WRLCK;
9706 ioc.lil_flags = LL_LEASE_RESYNC;
9707 ioc.lil_mirror_id = mirror_id;
9708 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9711 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9712 progname, argv[0], fname, strerror(errno));
9722 rc = llapi_lease_check(fd);
9723 if (rc != LL_LEASE_WRLCK) {
9724 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9725 progname, argv[0], fname);
9729 bytes_read = read(inputfd, buf, buflen);
9730 if (bytes_read < 0) {
9733 "%s %s: fail to read data from '%s': %s\n",
9734 progname, argv[0], inputfile ? : "STDIN",
9741 if (bytes_read == 0)
9744 /* round up to page align to make direct IO happy. */
9745 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9747 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9752 "%s %s: fail to write to mirror %u: %s\n",
9753 progname, argv[0], mirror_id,
9761 if (pos & (page_size - 1)) {
9762 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9767 ioc.lil_mode = LL_LEASE_UNLCK;
9768 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9770 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9775 "%s %s: release lease lock of '%s' failed: %s\n",
9776 progname, argv[0], fname, strerror(errno));
9793 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9795 struct llapi_layout *layout;
9796 struct collect_ids_data cid = { .cid_ids = ids,
9798 .cid_exclude = exclude_id, };
9801 layout = llapi_layout_get_by_fd(fd, 0);
9802 if (layout == NULL) {
9803 fprintf(stderr, "could not get layout\n");
9807 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9809 fprintf(stderr, "failed to iterate layout\n");
9810 llapi_layout_free(layout);
9814 llapi_layout_free(layout);
9816 return cid.cid_count;
9819 static inline int lfs_mirror_copy(int argc, char **argv)
9822 __u16 read_mirror_id = 0;
9823 __u16 ids[128] = { 0 };
9825 struct llapi_layout *layout = NULL;
9826 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9833 struct ll_ioc_lease *ioc = NULL;
9834 struct ll_ioc_lease_id *resync_ioc;
9836 struct option long_opts[] = {
9837 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9838 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9841 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9846 read_mirror_id = strtoul(optarg, &end, 0);
9847 if (*end != '\0' || read_mirror_id == 0) {
9849 "%s %s: invalid read mirror ID '%s'\n",
9850 progname, argv[0], optarg);
9855 if (!strcmp(optarg, "-1")) {
9856 /* specify all other mirrors */
9860 count = parse_mirror_ids((__u16 *)ids,
9868 fprintf(stderr, "%s: option '%s' unrecognized\n",
9869 progname, argv[optind - 1]);
9874 if (argc == optind) {
9875 fprintf(stderr, "%s %s: no mirrored file provided\n",
9878 } else if (argc > optind + 1) {
9879 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9883 if (read_mirror_id == 0) {
9885 "%s %s: no valid read mirror ID %d is provided\n",
9886 progname, argv[0], read_mirror_id);
9892 "%s %s: no write mirror ID is provided\n",
9897 for (i = 0; i < count; i++) {
9898 if (read_mirror_id == ids[i]) {
9900 "%s %s: read and write mirror ID cannot be the same\n",
9906 /* open mirror file */
9907 fname = argv[optind];
9909 fd = open(fname, O_DIRECT | O_RDWR);
9911 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9912 progname, argv[0], fname, strerror(errno));
9916 /* write to all other mirrors */
9917 if (ids[0] == (__u16)-1) {
9918 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9922 "%s %s: failed to get other mirror ids in '%s': %d\n",
9923 progname, argv[0], fname, rc);
9928 /* verify mirror id */
9929 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9932 "%s %s: cannot find mirror with ID %u in '%s'\n",
9933 progname, argv[0], read_mirror_id, fname);
9937 for (i = 0; i < count; i++) {
9938 rc = verify_mirror_id_by_fd(fd, ids[i]);
9941 "%s %s: cannot find mirror with ID %u in '%s'\n",
9942 progname, argv[0], ids[i], fname);
9947 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9950 "%s %s: cannot alloc comp id array for ioc: %s\n",
9951 progname, argv[0], strerror(errno));
9956 /* get stale component info */
9957 layout = llapi_layout_get_by_fd(fd, 0);
9958 if (layout == NULL) {
9959 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9960 progname, argv[0], fname, strerror(errno));
9964 comp_size = llapi_mirror_find_stale(layout, comp_array,
9965 ARRAY_SIZE(comp_array),
9967 llapi_layout_free(layout);
9968 if (comp_size < 0) {
9973 /* prepare target mirror components instantiation */
9974 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9975 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9976 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9978 resync_ioc->lil_mirror_id = ids[0];
9980 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9981 rc = llapi_lease_set(fd, ioc);
9984 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9985 progname, argv[0], fname, strerror(errno));
9989 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9992 fprintf(stderr, "%s %s: copy error: %d\n",
9993 progname, argv[0], rc);
9997 fprintf(stdout, "mirror copied successfully: ");
9998 for (i = 0; i < copied; i++)
9999 fprintf(stdout, "%d ", ids[i]);
10000 fprintf(stdout, "\n");
10002 ioc->lil_mode = LL_LEASE_UNLCK;
10003 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10004 ioc->lil_count = 0;
10005 for (i = 0; i < comp_size; i++) {
10008 for (j = 0; j < copied; j++) {
10009 if (comp_array[i].lrc_mirror_id != ids[j])
10012 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10016 rc = llapi_lease_set(fd, ioc);
10021 "%s %s: release lease lock of '%s' failed: %s\n",
10022 progname, argv[0], fname, strerror(errno));
10036 * struct verify_chunk - Mirror chunk to be verified.
10037 * @chunk: [start, end) of the chunk.
10038 * @mirror_count: Number of mirror ids in @mirror_id array.
10039 * @mirror_id: Array of valid mirror ids that cover the chunk.
10041 struct verify_chunk {
10042 struct lu_extent chunk;
10043 unsigned int mirror_count;
10044 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10048 * print_chunks() - Print chunk information.
10049 * @fname: Mirrored file name.
10050 * @chunks: Array of chunks.
10051 * @chunk_count: Number of chunks in @chunks array.
10053 * This function prints [start, end) of each chunk in @chunks
10054 * for mirrored file @fname, and also prints the valid mirror ids
10055 * that cover the chunk.
10060 void print_chunks(const char *fname, struct verify_chunk *chunks,
10066 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10067 for (i = 0; i < chunk_count; i++) {
10068 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10070 if (chunks[i].mirror_count == 0)
10071 fprintf(stdout, "\t[");
10073 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10074 for (j = 1; j < chunks[i].mirror_count; j++)
10075 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10077 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10079 fprintf(stdout, "\n");
10083 * print_checksums() - Print CRC-32 checksum values.
10084 * @chunk: A chunk and its corresponding valid mirror ids.
10085 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10087 * This function prints CRC-32 checksum values on @chunk for
10088 * each valid mirror that covers it.
10093 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10098 "CRC-32 checksum value for chunk "DEXT":\n",
10099 PEXT(&chunk->chunk));
10100 for (i = 0; i < chunk->mirror_count; i++)
10101 fprintf(stdout, "Mirror %u:\t%#lx\n",
10102 chunk->mirror_id[i], crc[i]);
10103 fprintf(stdout, "\n");
10107 * filter_mirror_id() - Filter specified mirror ids.
10108 * @chunks: Array of chunks.
10109 * @chunk_count: Number of chunks in @chunks array.
10110 * @mirror_ids: Specified mirror ids to be verified.
10111 * @ids_nr: Number of specified mirror ids.
10113 * This function scans valid mirror ids that cover each chunk in @chunks
10114 * and filters specified mirror ids.
10119 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10120 __u16 *mirror_ids, int ids_nr)
10125 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10126 unsigned int valid_count = 0;
10128 for (i = 0; i < chunk_count; i++) {
10129 if (chunks[i].mirror_count == 0)
10133 for (j = 0; j < ids_nr; j++) {
10134 for (k = 0; k < chunks[i].mirror_count; k++) {
10135 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10136 valid_id[valid_count] = mirror_ids[j];
10143 memcpy(chunks[i].mirror_id, valid_id,
10144 sizeof(__u16) * valid_count);
10145 chunks[i].mirror_count = valid_count;
10150 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10151 * @layout: Mirror component list.
10152 * @chunks: Array of chunks.
10153 * @chunks_size: Array size of @chunks.
10155 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10156 * to find out chunk segments and store them in @chunks array.
10158 * The @mirror_id array in each element of @chunks will store the valid
10159 * mirror ids that cover the chunk. If a mirror component covering the
10160 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10161 * will not be stored into the @mirror_id array, and the chunk for that
10162 * mirror will not be verified.
10164 * The @mirror_count in each element of @chunks will store the number of
10165 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10166 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10167 * indicates the chunk is valid in only one mirror. In both cases, the
10168 * chunk will not be verified.
10170 * Here is an example:
10172 * 0 1M 2M 3M 4M EOF
10173 * +------+-------------+--------------------+
10174 * | | | S | mirror1
10175 * +------+------+------+------+-------------+
10176 * | | S | S | | mirror2
10177 * +-------------+------+------+-------------+
10179 * prepared @chunks array will contain 5 elements:
10180 * (([0, 1M), [1, 2], 2),
10181 * ([1M, 2M), [1, 2], 2),
10182 * ([2M, 3M), [1], 1),
10183 * ([3M, 4M], [], 0),
10184 * ([4M, EOF), [2], 1))
10186 * Return: the actual array size of @chunks on success
10187 * or a negative error code on failure.
10190 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10191 struct verify_chunk *chunks,
10192 size_t chunks_size)
10196 uint32_t mirror_id;
10202 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10205 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10208 "%s: move to the first layout component: %s.\n",
10209 progname, strerror(errno));
10215 chunks[idx].chunk.e_end = LUSTRE_EOF;
10217 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10220 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10221 progname, strerror(errno));
10225 if (start > chunks[idx].chunk.e_start ||
10226 end <= chunks[idx].chunk.e_start)
10229 if (end < chunks[idx].chunk.e_end)
10230 chunks[idx].chunk.e_end = end;
10232 rc = llapi_layout_comp_flags_get(layout, &flags);
10235 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10236 progname, strerror(errno));
10240 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10243 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10246 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10247 progname, strerror(errno));
10251 chunks[idx].mirror_id[i] = mirror_id;
10253 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10255 "%s: mirror_id array is too small.\n",
10262 rc = llapi_layout_comp_use(layout,
10263 LLAPI_LAYOUT_COMP_USE_NEXT);
10266 "%s: move to the next layout component: %s.\n",
10267 progname, strerror(errno));
10270 } /* loop through all components */
10272 chunks[idx].mirror_count = i;
10274 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10278 if (idx >= chunks_size) {
10279 fprintf(stderr, "%s: chunks array is too small.\n",
10285 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10289 return rc < 0 ? rc : idx + 1;
10293 * lfs_mirror_verify_chunk() - Verify a chunk.
10294 * @fd: File descriptor of the mirrored file.
10295 * @file_size: Size of the mirrored file.
10296 * @chunk: A chunk and its corresponding valid mirror ids.
10297 * @verbose: Verbose mode.
10299 * This function verifies a @chunk contains exactly the same data
10300 * ammong the mirrors that cover it.
10302 * If @verbose is specified, then the function will print where the
10303 * differences are if the data do not match. Otherwise, it will
10304 * just return an error in that case.
10306 * Return: 0 on success or a negative error code on failure.
10309 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10310 struct verify_chunk *chunk, int verbose)
10312 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10314 size_t page_size = sysconf(_SC_PAGESIZE);
10315 ssize_t bytes_read;
10316 ssize_t bytes_done;
10320 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10324 if (file_size == 0)
10327 rc = posix_memalign(&buf, page_size, buflen);
10328 if (rc) /* error code is returned directly */
10332 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10333 PEXT(&chunk->chunk));
10334 for (i = 0; i < chunk->mirror_count; i++)
10335 fprintf(stdout, " %u", chunk->mirror_id[i]);
10336 fprintf(stdout, "\n");
10340 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10341 pos = chunk->chunk.e_start;
10342 while (bytes_done < count) {
10343 /* compute initial CRC-32 checksum */
10344 crc = crc32(0L, Z_NULL, 0);
10345 memset(crc_array, 0, sizeof(crc_array));
10348 for (i = 0; i < chunk->mirror_count; i++) {
10349 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10351 if (bytes_read < 0) {
10354 "%s: failed to read data from mirror %u: %s.\n",
10355 progname, chunk->mirror_id[i],
10360 /* compute new CRC-32 checksum */
10361 crc_array[i] = crc32(crc, buf, bytes_read);
10365 print_checksums(chunk, crc_array);
10367 /* compare CRC-32 checksum values */
10368 for (i = 1; i < chunk->mirror_count; i++) {
10369 if (crc_array[i] != crc_array[0]) {
10373 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10374 progname, PEXT(&chunk->chunk),
10375 chunk->mirror_id[0],
10376 chunk->mirror_id[i]);
10381 bytes_done += bytes_read;
10384 if (verbose > 1 && rc == 0) {
10385 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10386 PEXT(&chunk->chunk));
10387 for (i = 0; i < chunk->mirror_count; i++)
10388 fprintf(stdout, " %u", chunk->mirror_id[i]);
10389 fprintf(stdout, " PASS\n\n");
10398 * lfs_mirror_verify_file() - Verify a mirrored file.
10399 * @fname: Mirrored file name.
10400 * @mirror_ids: Specified mirror ids to be verified.
10401 * @ids_nr: Number of specified mirror ids.
10402 * @verbose: Verbose mode.
10404 * This function verifies that each SYNC mirror of a mirrored file
10405 * specified by @fname contains exactly the same data.
10407 * If @mirror_ids is specified, then the function will verify the
10408 * mirrors specified by @mirror_ids contain exactly the same data.
10410 * If @verbose is specified, then the function will print where the
10411 * differences are if the data do not match. Otherwise, it will
10412 * just return an error in that case.
10414 * Return: 0 on success or a negative error code on failure.
10417 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10420 struct verify_chunk chunks_array[1024] = { };
10421 struct llapi_layout *layout = NULL;
10423 uint32_t flr_state;
10425 int chunk_count = 0;
10431 if (stat(fname, &stbuf) < 0) {
10432 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10433 progname, fname, strerror(errno));
10438 if (!S_ISREG(stbuf.st_mode)) {
10439 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10445 if (stbuf.st_size == 0) {
10447 fprintf(stdout, "%s: '%s' file size is 0.\n",
10453 fd = open(fname, O_DIRECT | O_RDONLY);
10455 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10456 progname, fname, strerror(errno));
10461 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10463 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10464 progname, fname, strerror(errno));
10468 layout = llapi_layout_get_by_fd(fd, 0);
10469 if (layout == NULL) {
10470 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10471 progname, fname, strerror(errno));
10473 llapi_lease_release(fd);
10477 rc = llapi_layout_flags_get(layout, &flr_state);
10479 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10480 progname, fname, strerror(errno));
10485 flr_state &= LCM_FL_FLR_MASK;
10486 switch (flr_state) {
10489 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10490 progname, fname, llapi_layout_flags_string(flr_state));
10496 /* find out mirror chunks to be verified */
10497 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10498 ARRAY_SIZE(chunks_array));
10499 if (chunk_count < 0) {
10505 /* filter specified mirror ids */
10506 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10509 print_chunks(fname, chunks_array, chunk_count);
10511 for (idx = 0; idx < chunk_count; idx++) {
10512 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10515 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10517 PEXT(&chunks_array[idx].chunk),
10518 (unsigned long long)stbuf.st_size);
10522 if (chunks_array[idx].mirror_count == 0) {
10524 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10526 PEXT(&chunks_array[idx].chunk));
10528 fprintf(stderr, "skipped\n");
10532 fprintf(stderr, "failed\n");
10536 if (chunks_array[idx].mirror_count == 1) {
10539 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10541 PEXT(&chunks_array[idx].chunk),
10542 chunks_array[idx].mirror_id[0]);
10546 rc = llapi_lease_check(fd);
10547 if (rc != LL_LEASE_RDLCK) {
10548 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10553 /* verify one chunk */
10554 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10555 &chunks_array[idx], verbose);
10569 llapi_layout_free(layout);
10570 llapi_lease_release(fd);
10578 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10579 * @argc: The count of lfs mirror verify command line arguments.
10580 * @argv: Array of strings for lfs mirror verify command line arguments.
10582 * This function parses lfs mirror verify command and verifies the
10583 * specified mirrored file(s).
10585 * Return: 0 on success or a negative error code on failure.
10587 static inline int lfs_mirror_verify(int argc, char **argv)
10589 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10595 char cmd[PATH_MAX];
10597 struct option long_opts[] = {
10598 { .val = 'o', .name = "only", .has_arg = required_argument },
10599 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10600 { .name = NULL } };
10602 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10604 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10607 rc = parse_mirror_ids(mirror_ids,
10608 ARRAY_SIZE(mirror_ids),
10612 "%s: bad mirror ids '%s'.\n",
10619 "%s: at least 2 mirror ids needed with '--only' option.\n",
10629 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10630 progname, argv[optind - 1]);
10636 if (argc == optind) {
10637 fprintf(stderr, "%s: no file name given.\n", progname);
10642 if (ids_nr > 0 && argc > optind + 1) {
10644 "%s: '--only' cannot be used upon multiple files.\n",
10652 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10658 for (; optind < argc; optind++) {
10659 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10669 * lfs_mirror() - Parse and execute lfs mirror commands.
10670 * @argc: The count of lfs mirror command line arguments.
10671 * @argv: Array of strings for lfs mirror command line arguments.
10673 * This function parses lfs mirror commands and performs the
10674 * corresponding functions specified in mirror_cmdlist[].
10676 * Return: 0 on success or an error code on failure.
10678 static int lfs_mirror(int argc, char **argv)
10680 char cmd[PATH_MAX];
10683 setlinebuf(stdout);
10685 Parser_init("lfs-mirror > ", mirror_cmdlist);
10687 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10689 program_invocation_short_name = cmd;
10691 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10693 rc = Parser_commands();
10695 return rc < 0 ? -rc : rc;
10698 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10700 #if __BYTE_ORDER == __BIG_ENDIAN
10701 __swab16s(&attrs->lsa_valid);
10702 __swab64s(&attrs->lsa_size);
10703 __swab64s(&attrs->lsa_blocks);
10707 enum lfs_som_type {
10708 LFS_SOM_SIZE = 0x1,
10709 LFS_SOM_BLOCKS = 0x2,
10710 LFS_SOM_FLAGS = 0x4,
10711 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10715 static int lfs_getsom(int argc, char **argv)
10718 struct lustre_som_attrs *attrs;
10719 char buf[sizeof(*attrs) + 64];
10720 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10723 while ((c = getopt(argc, argv, "sbf")) != -1) {
10726 type = LFS_SOM_SIZE;
10729 type = LFS_SOM_BLOCKS;
10732 type = LFS_SOM_FLAGS;
10735 fprintf(stderr, "%s: invalid option '%c'\n",
10745 fprintf(stderr, "%s: %s\n",
10746 progname, argc == 0 ? "miss file target" :
10747 "input more than 2 files");
10752 attrs = (void *)buf;
10753 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10756 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10757 argv[0], strerror(errno), errno);
10761 lustre_som_swab(attrs);
10764 case LFS_SOM_ATTR_ALL:
10765 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10766 path, (unsigned long long)attrs->lsa_size,
10767 (unsigned long long)attrs->lsa_blocks,
10771 printf("%llu\n", (unsigned long long)attrs->lsa_size);
10773 case LFS_SOM_BLOCKS:
10774 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
10776 case LFS_SOM_FLAGS:
10777 printf("%x\n", attrs->lsa_valid);
10780 fprintf(stderr, "%s: unknown option\n", progname);
10788 * lfs_mirror_list_commands() - List lfs mirror commands.
10789 * @argc: The count of command line arguments.
10790 * @argv: Array of strings for command line arguments.
10792 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10794 * Return: 0 on success.
10796 static int lfs_mirror_list_commands(int argc, char **argv)
10798 char buffer[81] = "";
10800 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10806 static int lfs_pcc_attach(int argc, char **argv)
10808 struct option long_opts[] = {
10809 { .val = 'i', .name = "id", .has_arg = required_argument },
10810 { .name = NULL } };
10813 __u32 archive_id = 0;
10816 char fullpath[PATH_MAX];
10817 enum lu_pcc_type type = LU_PCC_READWRITE;
10820 while ((c = getopt_long(argc, argv, "i:",
10821 long_opts, NULL)) != -1) {
10824 archive_id = strtoul(optarg, &end, 0);
10825 if (*end != '\0' || archive_id == 0) {
10826 fprintf(stderr, "error: %s: bad archive ID "
10827 "'%s'\n", argv[0], optarg);
10834 fprintf(stderr, "%s: option '%s' unrecognized\n",
10835 argv[0], argv[optind - 1]);
10840 if (argc <= optind) {
10841 fprintf(stderr, "%s: must specify one or more file names\n",
10846 while (optind < argc) {
10849 path = argv[optind++];
10850 if (realpath(path, fullpath) == NULL) {
10851 fprintf(stderr, "%s: could not find path '%s': %s\n",
10852 argv[0], path, strerror(errno));
10858 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10860 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10861 "with archive ID '%u': %s\n", argv[0],
10862 path, archive_id, strerror(-rc2));
10870 static int lfs_pcc_attach_fid(int argc, char **argv)
10872 struct option long_opts[] = {
10873 { .val = 'i', .name = "id", .has_arg = required_argument },
10874 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10875 { .name = NULL } };
10876 char short_opts[] = "i:m:";
10879 __u32 archive_id = 0;
10881 const char *mntpath = NULL;
10882 const char *fidstr;
10883 enum lu_pcc_type type = LU_PCC_READWRITE;
10886 while ((c = getopt_long(argc, argv, short_opts,
10887 long_opts, NULL)) != -1) {
10890 archive_id = strtoul(optarg, &end, 0);
10891 if (*end != '\0') {
10892 fprintf(stderr, "error: %s: bad archive ID "
10893 "'%s'\n", argv[0], optarg);
10903 fprintf(stderr, "%s: option '%s' unrecognized\n",
10904 argv[0], argv[optind - 1]);
10909 if (archive_id == 0) {
10910 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10914 if (mntpath == NULL) {
10915 fprintf(stderr, "%s: must specify Lustre mount point\n",
10920 if (argc <= optind) {
10921 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10925 while (optind < argc) {
10928 fidstr = argv[optind++];
10930 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10933 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10934 "with archive ID '%u': %s\n", argv[0],
10935 fidstr, mntpath, archive_id, strerror(rc2));
10937 if (rc == 0 && rc2 < 0)
10943 static int lfs_pcc_detach(int argc, char **argv)
10945 struct option long_opts[] = {
10946 { .val = 'k', .name = "keep", .has_arg = no_argument },
10947 { .name = NULL } };
10948 char short_opts[] = "k";
10952 char fullpath[PATH_MAX];
10953 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10956 while ((c = getopt_long(argc, argv, short_opts,
10957 long_opts, NULL)) != -1) {
10960 detach_opt = PCC_DETACH_OPT_NONE;
10965 fprintf(stderr, "%s: option '%s' unrecognized\n",
10966 argv[0], argv[optind - 1]);
10971 while (optind < argc) {
10974 path = argv[optind++];
10975 if (realpath(path, fullpath) == NULL) {
10976 fprintf(stderr, "%s: could not find path '%s': %s\n",
10977 argv[0], path, strerror(errno));
10983 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
10986 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
10987 "%s\n", argv[0], path, strerror(errno));
10995 static int lfs_pcc_detach_fid(int argc, char **argv)
10997 struct option long_opts[] = {
10998 { .val = 'k', .name = "keep", .has_arg = no_argument },
10999 { .name = NULL } };
11000 char short_opts[] = "k";
11004 const char *mntpath;
11005 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11008 while ((c = getopt_long(argc, argv, short_opts,
11009 long_opts, NULL)) != -1) {
11012 detach_opt = PCC_DETACH_OPT_NONE;
11017 fprintf(stderr, "%s: option '%s' unrecognized\n",
11018 argv[0], argv[optind - 1]);
11023 mntpath = argv[optind++];
11025 while (optind < argc) {
11028 fid = argv[optind++];
11030 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11032 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
11033 "from PCC: %s\n", argv[0], fid, mntpath,
11042 static int lfs_pcc_state(int argc, char **argv)
11046 char fullpath[PATH_MAX];
11047 struct lu_pcc_state state;
11052 fprintf(stderr, "%s: must specify one or more file names\n",
11057 while (optind < argc) {
11060 path = argv[optind++];
11061 if (realpath(path, fullpath) == NULL) {
11062 fprintf(stderr, "%s: could not find path '%s': %s\n",
11063 argv[0], path, strerror(errno));
11069 rc2 = llapi_pcc_state_get(fullpath, &state);
11073 fprintf(stderr, "%s: cannot get PCC state of '%s': "
11074 "%s\n", argv[0], path, strerror(-rc2));
11078 printf("file: %s", path);
11079 printf(", type: %s", pcc_type2string(state.pccs_type));
11080 if (state.pccs_type == LU_PCC_NONE &&
11081 state.pccs_open_count == 0) {
11086 printf(", PCC file: %s", state.pccs_path);
11087 printf(", user number: %u", state.pccs_open_count);
11088 printf(", flags: %x", state.pccs_flags);
11095 * lfs_pcc_list_commands() - List lfs pcc commands.
11096 * @argc: The count of command line arguments.
11097 * @argv: Array of strings for command line arguments.
11099 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11101 * Return: 0 on success.
11103 static int lfs_pcc_list_commands(int argc, char **argv)
11105 char buffer[81] = "";
11107 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11114 * lfs_pcc() - Parse and execute lfs pcc commands.
11115 * @argc: The count of lfs pcc command line arguments.
11116 * @argv: Array of strings for lfs pcc command line arguments.
11118 * This function parses lfs pcc commands and performs the
11119 * corresponding functions specified in pcc_cmdlist[].
11121 * Return: 0 on success or an error code on failure.
11123 static int lfs_pcc(int argc, char **argv)
11125 char cmd[PATH_MAX];
11128 setlinebuf(stdout);
11130 Parser_init("lfs-pcc > ", pcc_cmdlist);
11132 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11134 program_invocation_short_name = cmd;
11136 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11138 rc = Parser_commands();
11140 return rc < 0 ? -rc : rc;
11143 static int lfs_list_commands(int argc, char **argv)
11145 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11147 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11152 int main(int argc, char **argv)
11156 /* Ensure that liblustreapi constructor has run */
11157 if (!llapi_liblustreapi_initialized())
11158 fprintf(stderr, "liblustreapi was not properly initialized\n");
11160 setlinebuf(stdout);
11163 Parser_init("lfs > ", cmdlist);
11165 progname = program_invocation_short_name; /* Used in error messages */
11167 llapi_set_command_name(argv[1]);
11168 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11169 llapi_clear_command_name();
11171 rc = Parser_commands();
11174 return rc < 0 ? -rc : rc;
11177 #ifdef _LUSTRE_IDL_H_
11178 /* Everything we need here should be included by lustreapi.h. */
11179 # error "lfs should not depend on lustre_idl.h"
11180 #endif /* _LUSTRE_IDL_H_ */