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 (long long)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 (unsigned long long)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 (long long)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",
2463 (unsigned long long)lsa->lsa_pattern,
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",
2473 (unsigned long long)lsa->lsa_pattern,
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 (long long)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 (long long)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",
3767 (long long)lsa.lsa_stripe_count,
3773 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3774 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3775 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3776 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3778 lmu->lum_magic = LMV_USER_MAGIC;
3781 migrate_mdt_param.fp_lmv_md = lmu;
3782 migrate_mdt_param.fp_migrate = 1;
3783 } else if (layout == NULL) {
3784 /* initialize stripe parameters */
3785 param = calloc(1, offsetof(typeof(*param),
3786 lsp_osts[lsa.lsa_nr_tgts]));
3787 if (param == NULL) {
3789 "%s %s: cannot allocate memory for parameters: %s\n",
3790 progname, argv[0], strerror(ENOMEM));
3795 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3796 param->lsp_stripe_size = lsa.lsa_stripe_size;
3797 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3798 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3799 param->lsp_stripe_count = -1;
3801 param->lsp_stripe_count = lsa.lsa_stripe_count;
3803 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3804 param->lsp_stripe_offset = -1;
3806 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3807 param->lsp_stripe_pattern =
3808 llapi_pattern_to_lov(lsa.lsa_pattern);
3809 if (param->lsp_stripe_pattern == EINVAL) {
3810 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3815 param->lsp_pool = lsa.lsa_pool_name;
3816 param->lsp_is_specific = false;
3817 if (lsa.lsa_nr_tgts > 0) {
3818 if (lsa.lsa_stripe_count > 0 &&
3819 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3820 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3821 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3823 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3825 (long long)lsa.lsa_stripe_count,
3831 param->lsp_is_specific = true;
3832 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3833 memcpy(param->lsp_osts, tgts,
3834 sizeof(*tgts) * lsa.lsa_nr_tgts);
3839 /* generate a layout from a YAML template */
3840 result = lfs_comp_create_from_yaml(template, &layout,
3843 fprintf(stderr, "error: %s: can't create composite "
3844 "layout from template file %s\n",
3848 } else if (from_copy) {
3849 layout = llapi_layout_get_by_path(template, 0);
3850 if (layout == NULL) {
3852 "%s: can't create composite layout from file %s.\n",
3853 progname, template);
3858 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3859 if (migrate_mdt_mode) {
3860 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3861 } else if (migrate_mode) {
3862 result = lfs_migrate(fname, migration_flags, param,
3864 } else if (comp_set != 0) {
3865 result = lfs_component_set(fname, comp_id,
3867 lsa.lsa_comp_neg_flags);
3868 } else if (comp_del != 0) {
3869 result = lfs_component_del(fname, comp_id,
3871 lsa.lsa_comp_neg_flags);
3872 } else if (comp_add != 0) {
3873 result = lfs_component_add(fname, layout);
3874 } else if (opc == SO_MIRROR_CREATE) {
3875 result = mirror_create(fname, mirror_list);
3876 } else if (opc == SO_MIRROR_EXTEND) {
3877 result = mirror_extend(fname, mirror_list,
3879 } else if (opc == SO_MIRROR_SPLIT) {
3880 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3882 "%s %s: no mirror id or component id or pool name"
3883 " is specified\n", progname, argv[0]);
3886 if (lsa.lsa_pool_name)
3887 mirror_flags |= MF_COMP_POOL;
3888 else if (mirror_id != 0)
3889 comp_id = mirror_id;
3891 mirror_flags |= MF_COMP_ID;
3892 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3894 has_m_file ? mirror_list->m_file :
3896 } else if (layout != NULL) {
3897 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3903 } else if (foreign_mode) {
3904 result = llapi_file_create_foreign(fname, mode, type,
3911 result = llapi_file_open_param(fname,
3920 /* Save the first error encountered. */
3927 if (mode_opt != NULL)
3928 umask(previous_umask);
3931 free(migrate_mdt_param.fp_lmv_md);
3932 llapi_layout_free(layout);
3933 lfs_mirror_list_free(mirror_list);
3938 llapi_layout_free(layout);
3939 lfs_mirror_list_free(mirror_list);
3943 static int lfs_poollist(int argc, char **argv)
3948 return llapi_poollist(argv[1]);
3951 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3956 char *endptr = "AD";
3961 else if (str[0] == '-')
3967 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3968 long long val = strtoll(timebuf, &endptr, 0);
3973 unit *= 52; /* 52 weeks + 1 day below */
3974 case 'w': /* fallthrough */
3976 case '\0': /* days are default unit if none used */
3977 case 'd': /* fallthrough */
3978 unit = (unit + (*endptr == 'y')) * 24;
3979 case 'h': /* fallthrough */
3981 case 'm': /* fallthrough */
3983 case 's': /* fallthrough */
3985 /* don't need to multiply by 1 for seconds */
3988 "%s find: bad time string '%s': %s\n",
3989 progname, timebuf, strerror(EINVAL));
3993 if (param->fp_time_margin == 0 ||
3994 (*endptr && unit < param->fp_time_margin))
3995 param->fp_time_margin = unit;
4002 fprintf(stderr, "%s find: bad time '%s': too large\n",
4012 static int name2uid(unsigned int *id, const char *name)
4014 struct passwd *passwd;
4016 passwd = getpwnam(name);
4019 *id = passwd->pw_uid;
4024 static int name2gid(unsigned int *id, const char *name)
4026 struct group *group;
4028 group = getgrnam(name);
4031 *id = group->gr_gid;
4036 static inline int name2projid(unsigned int *id, const char *name)
4041 static int uid2name(char **name, unsigned int id)
4043 struct passwd *passwd;
4045 passwd = getpwuid(id);
4048 *name = passwd->pw_name;
4053 static inline int gid2name(char **name, unsigned int id)
4055 struct group *group;
4057 group = getgrgid(id);
4060 *name = group->gr_name;
4065 static int name2layout(__u32 *layout, char *name)
4067 char *ptr, *layout_name;
4070 for (ptr = name; ; ptr = NULL) {
4071 layout_name = strtok(ptr, ",");
4072 if (layout_name == NULL)
4074 if (strcmp(layout_name, "released") == 0)
4075 *layout |= LOV_PATTERN_F_RELEASED;
4076 else if (strcmp(layout_name, "raid0") == 0)
4077 *layout |= LOV_PATTERN_RAID0;
4078 else if (strcmp(layout_name, "mdt") == 0)
4079 *layout |= LOV_PATTERN_MDT;
4080 else if (strcmp(layout_name, "overstriping") == 0)
4081 *layout |= LOV_PATTERN_OVERSTRIPING;
4088 static int lfs_find(int argc, char **argv)
4093 struct find_param param = {
4096 .fp_time_margin = 24 * 60 * 60,
4098 struct option long_opts[] = {
4099 { .val = 'A', .name = "atime", .has_arg = required_argument },
4100 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4101 { .val = LFS_COMP_COUNT_OPT,
4102 .name = "comp-count", .has_arg = required_argument },
4103 { .val = LFS_COMP_COUNT_OPT,
4104 .name = "component-count",
4105 .has_arg = required_argument },
4106 { .val = LFS_COMP_FLAGS_OPT,
4107 .name = "comp-flags", .has_arg = required_argument },
4108 { .val = LFS_COMP_FLAGS_OPT,
4109 .name = "component-flags",
4110 .has_arg = required_argument },
4111 { .val = LFS_COMP_START_OPT,
4112 .name = "comp-start", .has_arg = required_argument },
4113 { .val = LFS_COMP_START_OPT,
4114 .name = "component-start",
4115 .has_arg = required_argument },
4116 { .val = LFS_MIRROR_STATE_OPT,
4117 .name = "mirror-state", .has_arg = required_argument },
4118 { .val = LFS_LAYOUT_FOREIGN_OPT,
4119 .name = "foreign", .has_arg = optional_argument},
4120 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4121 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4122 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4123 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4124 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4125 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4126 { .val = 'E', .name = "component-end",
4127 .has_arg = required_argument },
4128 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4129 { .val = LFS_LAYOUT_FOREIGN_OPT,
4130 .name = "foreign", .has_arg = optional_argument},
4131 { .val = 'g', .name = "gid", .has_arg = required_argument },
4132 { .val = 'G', .name = "group", .has_arg = required_argument },
4133 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4134 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4135 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4136 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4137 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4138 { .val = 'L', .name = "layout", .has_arg = required_argument },
4139 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4140 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4141 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4142 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4143 { .val = 'n', .name = "name", .has_arg = required_argument },
4144 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4145 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4146 { .val = 'O', .name = "obd", .has_arg = required_argument },
4147 { .val = 'O', .name = "ost", .has_arg = required_argument },
4148 /* no short option for pool yet, can be 'p' after 2.18 */
4149 { .val = LFS_POOL_OPT,
4150 .name = "pool", .has_arg = required_argument },
4151 { .val = '0', .name = "print0", .has_arg = no_argument },
4152 { .val = 'P', .name = "print", .has_arg = no_argument },
4153 { .val = LFS_PROJID_OPT,
4154 .name = "projid", .has_arg = required_argument },
4155 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4156 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4157 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4158 { .val = 's', .name = "size", .has_arg = required_argument },
4159 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4160 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4161 { .val = 't', .name = "type", .has_arg = required_argument },
4162 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4163 { .val = 'u', .name = "uid", .has_arg = required_argument },
4164 { .val = 'U', .name = "user", .has_arg = required_argument },
4165 { .val = 'z', .name = "extension-size",
4166 .has_arg = required_argument },
4167 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4168 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4169 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4182 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4183 while ((c = getopt_long_only(argc, argv,
4184 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4185 long_opts, NULL)) >= 0) {
4190 /* '!' is part of option */
4191 /* when getopt_long_only() finds a string which is not
4192 * an option nor a known option argument it returns 1
4193 * in that case if we already have found pathstart and pathend
4194 * (i.e. we have the list of pathnames),
4195 * the only supported value is "!"
4197 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4198 if (!isoption && pathend != -1) {
4199 fprintf(stderr, "err: %s: filename|dirname must either "
4200 "precede options or follow options\n",
4205 if (!isoption && pathstart == -1)
4206 pathstart = optind - 1;
4207 if (isoption && pathstart != -1 && pathend == -1)
4208 pathend = optind - 2;
4214 /* unknown; opt is "!" or path component,
4215 * checking done above.
4217 if (strcmp(optarg, "!") == 0)
4221 xtime = ¶m.fp_atime;
4222 xsign = ¶m.fp_asign;
4223 param.fp_exclude_atime = !!neg_opt;
4224 /* no break, this falls through to 'C' for ctime */
4227 xtime = ¶m.fp_ctime;
4228 xsign = ¶m.fp_csign;
4229 param.fp_exclude_ctime = !!neg_opt;
4231 /* no break, this falls through to 'M' for mtime */
4234 xtime = ¶m.fp_mtime;
4235 xsign = ¶m.fp_msign;
4236 param.fp_exclude_mtime = !!neg_opt;
4238 rc = set_time(¶m, &t, xtime, optarg);
4239 if (rc == LONG_MAX) {
4247 if (optarg[0] == '+') {
4248 param.fp_blocks_sign = -1;
4250 } else if (optarg[0] == '-') {
4251 param.fp_blocks_sign = 1;
4255 param.fp_blocks_units = 1024;
4256 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4257 ¶m.fp_blocks_units, 0);
4259 fprintf(stderr, "error: bad blocks '%s'\n",
4263 param.fp_check_blocks = 1;
4264 param.fp_exclude_blocks = !!neg_opt;
4266 case LFS_COMP_COUNT_OPT:
4267 if (optarg[0] == '+') {
4268 param.fp_comp_count_sign = -1;
4270 } else if (optarg[0] == '-') {
4271 param.fp_comp_count_sign = 1;
4275 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4276 if (*endptr != '\0') {
4277 fprintf(stderr, "error: bad component count "
4281 param.fp_check_comp_count = 1;
4282 param.fp_exclude_comp_count = !!neg_opt;
4284 case LFS_COMP_FLAGS_OPT:
4285 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4286 ¶m.fp_comp_neg_flags);
4288 fprintf(stderr, "error: bad component flags "
4292 param.fp_check_comp_flags = 1;
4294 __u32 flags = param.fp_comp_neg_flags;
4295 param.fp_comp_neg_flags = param.fp_comp_flags;
4296 param.fp_comp_flags = flags;
4299 case LFS_COMP_START_OPT:
4300 if (optarg[0] == '+') {
4301 param.fp_comp_start_sign = -1;
4303 } else if (optarg[0] == '-') {
4304 param.fp_comp_start_sign = 1;
4308 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4309 ¶m.fp_comp_start_units, 0);
4311 fprintf(stderr, "error: bad component start "
4315 param.fp_check_comp_start = 1;
4316 param.fp_exclude_comp_start = !!neg_opt;
4318 case LFS_MIRROR_STATE_OPT:
4319 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4320 ¶m.fp_mirror_neg_state);
4323 "error: bad mirrored file state '%s'\n",
4327 param.fp_check_mirror_state = 1;
4329 __u16 state = param.fp_mirror_neg_state;
4330 param.fp_mirror_neg_state =
4331 param.fp_mirror_state;
4332 param.fp_mirror_state = state;
4336 if (optarg[0] == '+') {
4337 param.fp_stripe_count_sign = -1;
4339 } else if (optarg[0] == '-') {
4340 param.fp_stripe_count_sign = 1;
4344 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4345 if (*endptr != '\0') {
4346 fprintf(stderr,"error: bad stripe_count '%s'\n",
4351 param.fp_check_stripe_count = 1;
4352 param.fp_exclude_stripe_count = !!neg_opt;
4355 param.fp_max_depth = strtol(optarg, 0, 0);
4358 if (optarg[0] == '+') {
4359 param.fp_comp_end_sign = -1;
4361 } else if (optarg[0] == '-') {
4362 param.fp_comp_end_sign = 1;
4366 if (arg_is_eof(optarg)) {
4367 param.fp_comp_end = LUSTRE_EOF;
4368 param.fp_comp_end_units = 1;
4371 rc = llapi_parse_size(optarg,
4373 ¶m.fp_comp_end_units, 0);
4376 fprintf(stderr, "error: bad component end "
4380 param.fp_check_comp_end = 1;
4381 param.fp_exclude_comp_end = !!neg_opt;
4383 case LFS_LAYOUT_FOREIGN_OPT: {
4384 /* all types by default */
4385 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4387 if (optarg != NULL) {
4388 /* check pure numeric */
4389 type = strtoul(optarg, &endptr, 0);
4392 type = check_foreign_type_name(optarg);
4393 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4395 "%s %s: unknown foreign type '%s'\n",
4402 param.fp_foreign_type = type;
4403 param.fp_check_foreign = 1;
4404 param.fp_exclude_foreign = !!neg_opt;
4409 rc = name2gid(¶m.fp_gid, optarg);
4411 param.fp_gid = strtoul(optarg, &endptr, 10);
4412 if (*endptr != '\0') {
4413 fprintf(stderr, "Group/GID: %s cannot "
4414 "be found.\n", optarg);
4419 param.fp_exclude_gid = !!neg_opt;
4420 param.fp_check_gid = 1;
4423 param.fp_hash_type = check_hashtype(optarg);
4424 if (param.fp_hash_type == 0) {
4425 fprintf(stderr, "error: bad hash_type '%s'\n",
4430 param.fp_check_hash_type = 1;
4431 param.fp_exclude_hash_type = !!neg_opt;
4437 ret = name2layout(¶m.fp_layout, optarg);
4440 param.fp_exclude_layout = !!neg_opt;
4441 param.fp_check_layout = 1;
4445 rc = name2uid(¶m.fp_uid, optarg);
4447 param.fp_uid = strtoul(optarg, &endptr, 10);
4448 if (*endptr != '\0') {
4449 fprintf(stderr, "User/UID: %s cannot "
4450 "be found.\n", optarg);
4455 param.fp_exclude_uid = !!neg_opt;
4456 param.fp_check_uid = 1;
4459 param.fp_pattern = (char *)optarg;
4460 param.fp_exclude_pattern = !!neg_opt;
4463 if (optarg[0] == '+') {
4464 param.fp_mirror_count_sign = -1;
4466 } else if (optarg[0] == '-') {
4467 param.fp_mirror_count_sign = 1;
4471 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4472 if (*endptr != '\0') {
4474 "error: bad mirror count '%s'\n",
4478 param.fp_check_mirror_count = 1;
4479 param.fp_exclude_mirror_count = !!neg_opt;
4484 char *buf, *token, *next, *p;
4488 buf = strdup(optarg);
4494 param.fp_exclude_obd = !!neg_opt;
4497 while (token && *token) {
4498 token = strchr(token, ',');
4505 param.fp_exclude_mdt = !!neg_opt;
4506 param.fp_num_alloc_mdts += len;
4507 tmp = realloc(param.fp_mdt_uuid,
4508 param.fp_num_alloc_mdts *
4509 sizeof(*param.fp_mdt_uuid));
4515 param.fp_mdt_uuid = tmp;
4517 param.fp_exclude_obd = !!neg_opt;
4518 param.fp_num_alloc_obds += len;
4519 tmp = realloc(param.fp_obd_uuid,
4520 param.fp_num_alloc_obds *
4521 sizeof(*param.fp_obd_uuid));
4527 param.fp_obd_uuid = tmp;
4529 for (token = buf; token && *token; token = next) {
4530 struct obd_uuid *puuid;
4533 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4536 ¶m.fp_obd_uuid[param.fp_num_obds++];
4538 p = strchr(token, ',');
4545 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4550 strncpy(puuid->uuid, token,
4551 sizeof(puuid->uuid));
4558 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4562 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4564 "Pool name %s is too long (max %d)\n",
4565 optarg, LOV_MAXPOOLNAME);
4570 * We do check for empty pool because empty pool
4571 * is used to find V1 LOV attributes
4573 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4574 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4575 param.fp_exclude_pool = !!neg_opt;
4576 param.fp_check_pool = 1;
4578 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4579 case 'p': /* want this for --pool, to match getstripe/find */
4581 "warning: -p deprecated, use --print0 or -0\n");
4584 param.fp_zero_end = 1;
4586 case 'P': /* we always print, this option is a no-op */
4588 case LFS_PROJID_OPT:
4589 rc = name2projid(¶m.fp_projid, optarg);
4591 param.fp_projid = strtoul(optarg, &endptr, 10);
4592 if (*endptr != '\0') {
4594 "Invalid project ID: %s",
4600 param.fp_exclude_projid = !!neg_opt;
4601 param.fp_check_projid = 1;
4604 if (optarg[0] == '+') {
4605 param.fp_size_sign = -1;
4607 } else if (optarg[0] == '-') {
4608 param.fp_size_sign = 1;
4612 ret = llapi_parse_size(optarg, ¶m.fp_size,
4613 ¶m.fp_size_units, 0);
4615 fprintf(stderr, "error: bad file size '%s'\n",
4619 param.fp_check_size = 1;
4620 param.fp_exclude_size = !!neg_opt;
4623 if (optarg[0] == '+') {
4624 param.fp_stripe_size_sign = -1;
4626 } else if (optarg[0] == '-') {
4627 param.fp_stripe_size_sign = 1;
4631 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4632 ¶m.fp_stripe_size_units, 0);
4634 fprintf(stderr, "error: bad stripe_size '%s'\n",
4638 param.fp_check_stripe_size = 1;
4639 param.fp_exclude_stripe_size = !!neg_opt;
4642 param.fp_exclude_type = !!neg_opt;
4643 switch (optarg[0]) {
4645 param.fp_type = S_IFBLK;
4648 param.fp_type = S_IFCHR;
4651 param.fp_type = S_IFDIR;
4654 param.fp_type = S_IFREG;
4657 param.fp_type = S_IFLNK;
4660 param.fp_type = S_IFIFO;
4663 param.fp_type = S_IFSOCK;
4666 fprintf(stderr, "error: %s: bad type '%s'\n",
4673 if (optarg[0] == '+') {
4674 param.fp_mdt_count_sign = -1;
4676 } else if (optarg[0] == '-') {
4677 param.fp_mdt_count_sign = 1;
4681 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4682 if (*endptr != '\0') {
4683 fprintf(stderr, "error: bad mdt_count '%s'\n",
4688 param.fp_check_mdt_count = 1;
4689 param.fp_exclude_mdt_count = !!neg_opt;
4692 if (optarg[0] == '+') {
4693 param.fp_ext_size_sign = -1;
4695 } else if (optarg[0] == '-') {
4696 param.fp_ext_size_sign = 1;
4700 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4701 ¶m.fp_ext_size_units, 0);
4703 fprintf(stderr, "error: bad ext-size '%s'\n",
4707 param.fp_ext_size /= SEL_UNIT_SIZE;
4708 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4709 param.fp_check_ext_size = 1;
4710 param.fp_exclude_ext_size = !!neg_opt;
4718 if (pathstart == -1) {
4719 fprintf(stderr, "error: %s: no filename|pathname\n",
4723 } else if (pathend == -1) {
4729 rc = llapi_find(argv[pathstart], ¶m);
4732 pathbad = pathstart;
4734 } while (++pathstart < pathend);
4737 fprintf(stderr, "%s: failed for '%s': %s\n",
4738 progname, argv[pathbad], strerror(-rc));
4741 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4742 free(param.fp_obd_uuid);
4744 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4745 free(param.fp_mdt_uuid);
4750 static int lfs_getstripe_internal(int argc, char **argv,
4751 struct find_param *param)
4753 struct option long_opts[] = {
4754 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4755 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4756 { .val = LFS_COMP_COUNT_OPT,
4757 .name = "comp-count", .has_arg = no_argument },
4758 { .val = LFS_COMP_COUNT_OPT,
4759 .name = "component-count", .has_arg = no_argument },
4760 { .val = LFS_COMP_FLAGS_OPT,
4761 .name = "comp-flags", .has_arg = optional_argument },
4762 { .val = LFS_COMP_FLAGS_OPT,
4763 .name = "component-flags", .has_arg = optional_argument },
4764 { .val = LFS_COMP_START_OPT,
4765 .name = "comp-start", .has_arg = optional_argument },
4766 { .val = LFS_COMP_START_OPT,
4767 .name = "component-start", .has_arg = optional_argument },
4768 { .val = LFS_MIRROR_INDEX_OPT,
4769 .name = "mirror-index", .has_arg = required_argument },
4770 { .val = LFS_MIRROR_ID_OPT,
4771 .name = "mirror-id", .has_arg = required_argument },
4772 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4773 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4774 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4775 { .val = 'd', .name = "directory", .has_arg = no_argument },
4776 { .val = 'D', .name = "default", .has_arg = no_argument },
4777 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4778 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4779 { .val = 'F', .name = "fid", .has_arg = no_argument },
4780 { .val = 'g', .name = "generation", .has_arg = no_argument },
4781 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4782 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4783 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4784 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4785 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4786 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4787 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
4788 { .val = 'L', .name = "layout", .has_arg = no_argument },
4789 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4790 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4791 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4792 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4793 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4794 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4795 { .val = 'O', .name = "obd", .has_arg = required_argument },
4796 { .val = 'O', .name = "ost", .has_arg = required_argument },
4797 { .val = 'p', .name = "pool", .has_arg = no_argument },
4798 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4799 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4800 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4801 { .val = 'R', .name = "raw", .has_arg = no_argument },
4802 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4803 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4804 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4805 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4806 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4807 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4808 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4809 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4810 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
4811 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
4815 int pathstart = -1, pathend = -1;
4819 while ((c = getopt_long(argc, argv,
4820 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
4821 long_opts, NULL)) != -1) {
4825 /* '!' is part of option */
4826 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4827 if (!isoption && pathend != -1) {
4829 "error: %s: filename|dirname must either precede options or follow options\n",
4833 if (!isoption && pathstart == -1)
4834 pathstart = optind - 1;
4835 if (isoption && pathstart != -1 && pathend == -1)
4836 pathend = optind - 2;
4840 /* unknown: opt is "!" */
4841 if (strcmp(optarg, "!") == 0)
4845 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4846 param->fp_verbose |= VERBOSE_COUNT;
4847 param->fp_max_depth = 0;
4850 case LFS_COMP_COUNT_OPT:
4851 param->fp_verbose |= VERBOSE_COMP_COUNT;
4852 param->fp_max_depth = 0;
4854 case LFS_COMP_FLAGS_OPT:
4855 if (optarg != NULL) {
4856 rc = comp_str2flags(optarg,
4857 ¶m->fp_comp_flags,
4858 ¶m->fp_comp_neg_flags);
4860 fprintf(stderr, "error: %s bad "
4861 "component flags '%s'.\n",
4865 param->fp_check_comp_flags = 1;
4867 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4868 param->fp_max_depth = 0;
4871 case LFS_COMP_START_OPT:
4872 if (optarg != NULL) {
4874 if (tmp[0] == '+') {
4875 param->fp_comp_start_sign = -1;
4877 } else if (tmp[0] == '-') {
4878 param->fp_comp_start_sign = 1;
4881 rc = llapi_parse_size(tmp,
4882 ¶m->fp_comp_start,
4883 ¶m->fp_comp_start_units, 0);
4885 fprintf(stderr, "error: %s bad "
4886 "component start '%s'.\n",
4890 param->fp_check_comp_start = 1;
4893 param->fp_verbose |= VERBOSE_COMP_START;
4894 param->fp_max_depth = 0;
4897 case LFS_MIRROR_INDEX_OPT:
4898 if (optarg[0] == '+') {
4899 param->fp_mirror_index_sign = -1;
4901 } else if (optarg[0] == '-') {
4902 param->fp_mirror_index_sign = 1;
4906 param->fp_mirror_index = strtoul(optarg, &end, 0);
4907 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4908 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4910 "%s %s: invalid mirror index '%s'\n",
4911 progname, argv[0], optarg);
4914 if (param->fp_mirror_id != 0) {
4916 "%s %s: can't specify both mirror index and mirror ID\n",
4920 param->fp_check_mirror_index = 1;
4921 param->fp_exclude_mirror_index = !!neg_opt;
4923 case LFS_MIRROR_ID_OPT:
4924 if (optarg[0] == '+') {
4925 param->fp_mirror_id_sign = -1;
4927 } else if (optarg[0] == '-') {
4928 param->fp_mirror_id_sign = 1;
4932 param->fp_mirror_id = strtoul(optarg, &end, 0);
4933 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4934 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4936 "%s %s: invalid mirror ID '%s'\n",
4937 progname, argv[0], optarg);
4940 if (param->fp_mirror_index != 0) {
4942 "%s %s: can't specify both mirror index and mirror ID\n",
4946 param->fp_check_mirror_id = 1;
4947 param->fp_exclude_mirror_id = !!neg_opt;
4950 param->fp_max_depth = 0;
4953 param->fp_get_default_lmv = 1;
4956 if (optarg != NULL) {
4958 if (tmp[0] == '+') {
4959 param->fp_comp_end_sign = -1;
4961 } else if (tmp[0] == '-') {
4962 param->fp_comp_end_sign = 1;
4966 if (arg_is_eof(tmp)) {
4967 param->fp_comp_end = LUSTRE_EOF;
4968 param->fp_comp_end_units = 1;
4971 rc = llapi_parse_size(tmp,
4972 ¶m->fp_comp_end,
4973 ¶m->fp_comp_end_units, 0);
4976 fprintf(stderr, "error: %s bad "
4977 "component end '%s'.\n",
4981 param->fp_check_comp_end = 1;
4983 param->fp_verbose |= VERBOSE_COMP_END;
4984 param->fp_max_depth = 0;
4988 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4989 param->fp_verbose |= VERBOSE_DFID;
4990 param->fp_max_depth = 0;
4994 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4995 param->fp_verbose |= VERBOSE_GENERATION;
4996 param->fp_max_depth = 0;
5000 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5001 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5002 param->fp_max_depth = 0;
5006 if (optarg != NULL) {
5007 param->fp_comp_id = strtoul(optarg, &end, 0);
5008 if (*end != '\0' || param->fp_comp_id == 0 ||
5009 param->fp_comp_id > LCME_ID_MAX) {
5010 fprintf(stderr, "error: %s bad "
5011 "component id '%s'\n",
5015 param->fp_check_comp_id = 1;
5018 param->fp_max_depth = 0;
5019 param->fp_verbose |= VERBOSE_COMP_ID;
5023 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5024 param->fp_verbose |= VERBOSE_PATTERN;
5025 param->fp_max_depth = 0;
5028 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5030 fprintf(stderr, "warning: '-M' deprecated"
5031 ", use '--mdt-index' or '-m' instead\n");
5034 if (!(param->fp_verbose & VERBOSE_DETAIL))
5035 param->fp_max_depth = 0;
5036 param->fp_verbose |= VERBOSE_MDTINDEX;
5039 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5040 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5041 param->fp_max_depth = 0;
5045 if (param->fp_obd_uuid) {
5047 "error: %s: only one obduuid allowed",
5051 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5054 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5055 param->fp_verbose |= VERBOSE_POOL;
5056 param->fp_max_depth = 0;
5063 param->fp_recursive = 1;
5069 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5070 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5071 param->fp_max_depth = 0;
5075 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5081 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5082 param->fp_verbose |= VERBOSE_EXT_SIZE;
5083 param->fp_max_depth = 0;
5091 if (pathstart == -1) {
5092 fprintf(stderr, "error: %s: no filename|pathname\n",
5095 } else if (pathend == -1) {
5103 if (param->fp_recursive)
5104 param->fp_max_depth = -1;
5105 else if (param->fp_verbose & VERBOSE_DETAIL)
5106 param->fp_max_depth = 1;
5108 if (!param->fp_verbose)
5109 param->fp_verbose = VERBOSE_DEFAULT;
5110 if (param->fp_quiet)
5111 param->fp_verbose = VERBOSE_OBJID;
5114 rc = llapi_getstripe(argv[pathstart], param);
5115 } while (++pathstart < pathend && !rc);
5118 fprintf(stderr, "error: %s failed for %s.\n",
5119 argv[0], argv[optind - 1]);
5123 static int lfs_tgts(int argc, char **argv)
5125 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5126 struct find_param param;
5127 int index = 0, rc=0;
5132 if (argc == 2 && !realpath(argv[1], path)) {
5134 fprintf(stderr, "error: invalid path '%s': %s\n",
5135 argv[1], strerror(-rc));
5139 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5140 /* Check if we have a mount point */
5141 if (mntdir[0] == '\0')
5144 memset(¶m, 0, sizeof(param));
5145 if (!strcmp(argv[0], "mdts"))
5146 param.fp_get_lmv = 1;
5148 rc = llapi_ostlist(mntdir, ¶m);
5150 fprintf(stderr, "error: %s: failed on %s\n",
5153 if (path[0] != '\0')
5155 memset(mntdir, 0, PATH_MAX);
5161 static int lfs_getstripe(int argc, char **argv)
5163 struct find_param param = { 0 };
5165 param.fp_max_depth = 1;
5166 return lfs_getstripe_internal(argc, argv, ¶m);
5170 static int lfs_getdirstripe(int argc, char **argv)
5172 struct find_param param = { 0 };
5173 struct option long_opts[] = {
5174 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5175 { .val = 'D', .name = "default", .has_arg = no_argument },
5176 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5177 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5178 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5179 { .val = 'O', .name = "obd", .has_arg = required_argument },
5180 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5181 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5182 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5183 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5187 param.fp_get_lmv = 1;
5189 while ((c = getopt_long(argc, argv,
5190 "cDHimO:rtTvy", long_opts, NULL)) != -1)
5195 param.fp_verbose |= VERBOSE_COUNT;
5198 param.fp_get_default_lmv = 1;
5202 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5204 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5206 fprintf(stderr, "warning: '-t' deprecated, "
5207 "use '--mdt-hash' or '-H' instead\n");
5210 param.fp_verbose |= VERBOSE_HASH_TYPE;
5213 if (param.fp_obd_uuid) {
5215 "error: %s: only one obduuid allowed",
5219 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5222 param.fp_recursive = 1;
5225 param.fp_verbose |= VERBOSE_DETAIL;
5231 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5232 progname, argv[0], argv[optind - 1]);
5240 if (param.fp_recursive)
5241 param.fp_max_depth = -1;
5243 if (!param.fp_verbose)
5244 param.fp_verbose = VERBOSE_DEFAULT;
5247 rc = llapi_getstripe(argv[optind], ¶m);
5248 } while (++optind < argc && !rc);
5251 fprintf(stderr, "error: %s failed for %s.\n",
5252 argv[0], argv[optind - 1]);
5257 MNTDF_INODES = 0x0001,
5258 MNTDF_COOKED = 0x0002,
5259 MNTDF_LAZY = 0x0004,
5260 MNTDF_VERBOSE = 0x0008,
5261 MNTDF_SHOW = 0x0010,
5264 #define COOK(value) \
5267 while (value > 1024) { \
5275 #define CDF "%11llu"
5276 #define HDF "%8.1f%c"
5280 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5282 double avail, used, ratio = 0;
5285 avail = st->os_ffree;
5286 used = st->os_files - st->os_ffree;
5288 avail = st->os_bavail;
5289 used = st->os_blocks - st->os_bfree;
5291 if (avail + used > 0)
5292 ratio = used / (used + avail) * 100;
5294 /* Round up to match df(1) usage percentage */
5295 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5298 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5299 * so only show flags reflecting those states by default. Informational states
5300 * are only shown with "-v" and use lower-case names to distinguish them.
5301 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5303 static struct obd_statfs_state_names {
5304 enum obd_statfs_state osn_state;
5305 const char osn_name;
5308 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
5309 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
5310 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
5311 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
5312 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
5313 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
5314 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
5315 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
5316 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
5319 static int showdf(char *mntdir, struct obd_statfs *stat,
5320 char *uuid, enum mntdf_flags flags,
5321 char *type, int index, int rc)
5323 long long avail, used, total;
5325 char *suffix = "KMGTPEZY";
5326 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5327 char tbuf[3 * sizeof(__u64)];
5328 char ubuf[3 * sizeof(__u64)];
5329 char abuf[3 * sizeof(__u64)];
5330 char rbuf[3 * sizeof(__u64)];
5337 if (flags & MNTDF_INODES) {
5338 avail = stat->os_ffree;
5339 used = stat->os_files - stat->os_ffree;
5340 total = stat->os_files;
5342 int shift = flags & MNTDF_COOKED ? 0 : 10;
5344 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5345 used = ((stat->os_blocks - stat->os_bfree) *
5346 stat->os_bsize) >> shift;
5347 total = (stat->os_blocks * stat->os_bsize) >> shift;
5350 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5352 if (flags & MNTDF_COOKED) {
5356 cook_val = (double)total;
5359 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5362 snprintf(tbuf, sizeof(tbuf), CDF, total);
5364 cook_val = (double)used;
5367 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5370 snprintf(ubuf, sizeof(ubuf), CDF, used);
5372 cook_val = (double)avail;
5375 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5378 snprintf(abuf, sizeof(abuf), CDF, avail);
5380 snprintf(tbuf, sizeof(tbuf), CDF, total);
5381 snprintf(ubuf, sizeof(tbuf), CDF, used);
5382 snprintf(abuf, sizeof(tbuf), CDF, avail);
5385 sprintf(rbuf, RDF, ratio);
5386 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5387 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5389 printf("[%s:%d]", type, index);
5391 if (stat->os_state) {
5395 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5396 if (oss_names[i].osn_state & stat->os_state &&
5397 (oss_names[i].osn_err ||
5398 flags & MNTDF_VERBOSE))
5399 printf("%c", oss_names[i].osn_name);
5406 printf(UUF": inactive device\n", uuid);
5409 printf(UUF": %s\n", uuid, strerror(-rc));
5416 struct ll_stat_type {
5421 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5423 struct ll_statfs_data {
5425 struct obd_statfs sd_st;
5428 struct ll_statfs_buf {
5430 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5433 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5434 int ops, struct ll_statfs_buf *lsb)
5436 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5437 struct obd_uuid uuid_buf;
5438 char *poolname = NULL;
5439 struct ll_stat_type types[] = {
5440 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5441 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5442 { .st_name = NULL } };
5443 struct ll_stat_type *tp;
5444 __u64 ost_files = 0;
5445 __u64 ost_ffree = 0;
5453 poolname = strchr(pool, '.');
5454 if (poolname != NULL) {
5455 if (strncmp(fsname, pool, strlen(fsname))) {
5456 fprintf(stderr, "filesystem name incorrect\n");
5464 fd = open(mntdir, O_RDONLY);
5467 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5472 if (flags & MNTDF_SHOW) {
5473 if (flags & MNTDF_INODES)
5474 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5475 "UUID", "Inodes", "IUsed", "IFree",
5476 "IUse%", "Mounted on");
5478 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5480 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5481 "Used", "Available", "Use%", "Mounted on");
5484 for (tp = types; tp->st_name != NULL; tp++) {
5485 bool have_ost = false;
5487 if (!(tp->st_op & ops))
5490 for (index = 0; ; index++) {
5491 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5492 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5493 type = flags & MNTDF_LAZY ?
5494 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5495 rc2 = llapi_obd_fstatfs(fd, type, index,
5496 &stat_buf, &uuid_buf);
5501 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5502 if (!(flags & MNTDF_VERBOSE))
5504 } else if (rc2 < 0 && rc == 0) {
5508 /* If we have OSTs then don't report MDT block counts.
5509 * For MDT-only filesystems the expectation is that all
5510 * layouts have a DoM component. For filesystems with
5511 * OSTs, files are not necessarily going to store data
5512 * on MDTs, and MDT space is limited to a fraction of
5513 * OST space, so don't include it in the summary.
5515 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5522 if (poolname && tp->st_op == LL_STATFS_LOV &&
5523 llapi_search_ost(fsname, poolname,
5524 obd_uuid2str(&uuid_buf)) != 1)
5527 /* the llapi_obd_statfs() call may have returned with
5528 * an error, but if it filled in uuid_buf we will at
5529 * lease use that to print out a message for that OBD.
5530 * If we didn't get anything in the uuid_buf, then fill
5531 * it in so that we can print an error message. */
5532 if (uuid_buf.uuid[0] == '\0')
5533 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5534 "%s%04x", tp->st_name, index);
5536 lsb->sb_buf[lsb->sb_count].sd_index = index;
5537 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5540 if (flags & MNTDF_SHOW)
5541 showdf(mntdir, &stat_buf,
5542 obd_uuid2str(&uuid_buf), flags,
5543 tp->st_name, index, rc2);
5548 if (tp->st_op == LL_STATFS_LMV) {
5549 sum.os_ffree += stat_buf.os_ffree;
5550 sum.os_files += stat_buf.os_files;
5551 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5552 ost_files += stat_buf.os_files;
5553 ost_ffree += stat_buf.os_ffree;
5555 sum.os_blocks += stat_buf.os_blocks *
5557 sum.os_bfree += stat_buf.os_bfree *
5559 sum.os_bavail += stat_buf.os_bavail *
5566 /* If we have _some_ OSTs, but don't have as many free objects on the
5567 * OST as inodes on the MDTs, reduce the reported number of inodes
5568 * to compensate, so that the "inodes in use" number is correct.
5569 * This should be kept in sync with ll_statfs_internal().
5571 if (ost_files && ost_ffree < sum.os_ffree) {
5572 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5573 sum.os_ffree = ost_ffree;
5575 if (flags & MNTDF_SHOW) {
5577 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5584 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5586 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5588 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5590 int r1 = obd_statfs_ratio(st1, false);
5591 int r2 = obd_statfs_ratio(st2, false);
5592 int64_t result = r1 - r2;
5594 /* if both space usage are above 90, compare free inodes */
5595 if (r1 > 90 && r2 > 90)
5596 result = st2->os_ffree - st1->os_ffree;
5600 else if (result == 0)
5607 static int lfs_setdirstripe(int argc, char **argv)
5610 struct lfs_setstripe_args lsa = { 0 };
5611 struct llapi_stripe_param *param = NULL;
5612 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5615 char *mode_opt = NULL;
5616 bool default_stripe = false;
5617 bool delete = false;
5618 bool auto_distributed = false;
5619 bool foreign_mode = false;
5620 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5621 mode_t previous_mode = 0;
5622 struct ll_statfs_buf *lsb = NULL;
5623 char mntdir[PATH_MAX] = "";
5625 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5626 struct option long_opts[] = {
5627 { .val = 'c', .name = "count", .has_arg = required_argument },
5628 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5629 { .val = 'd', .name = "delete", .has_arg = no_argument },
5630 { .val = 'D', .name = "default", .has_arg = no_argument },
5631 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5632 { .val = LFS_LAYOUT_FLAGS_OPT,
5633 .name = "flags", .has_arg = required_argument },
5634 { .val = LFS_LAYOUT_FOREIGN_OPT,
5635 .name = "foreign", .has_arg = optional_argument},
5636 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5637 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5638 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5639 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5641 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5642 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5643 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5645 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5646 { .val = 'i', .name = "index", .has_arg = required_argument },
5648 { .val = 'o', .name = "mode", .has_arg = required_argument },
5649 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5650 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5652 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5653 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5654 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5658 setstripe_args_init(&lsa);
5660 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5661 long_opts, NULL)) >= 0) {
5668 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5671 "%s %s: invalid stripe count '%s'\n",
5672 progname, argv[0], optarg);
5678 default_stripe = true;
5681 default_stripe = true;
5683 case LFS_LAYOUT_FOREIGN_OPT:
5684 if (optarg != NULL) {
5685 /* check pure numeric */
5686 type = strtoul(optarg, &end, 0);
5689 type = check_foreign_type_name(optarg);
5690 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5692 "%s %s: unknown foreign type '%s'\n",
5699 foreign_mode = true;
5701 case LFS_LAYOUT_FLAGS_OPT:
5702 flags = strtoul(optarg, &end, 16);
5705 "%s %s: bad flags '%s'\n",
5706 progname, argv[0], optarg);
5710 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5712 fprintf(stderr, "warning: '--hash-type' and '-t' "
5713 "deprecated, use '--mdt-hash' or '-H' instead\n");
5716 lsa.lsa_pattern = check_hashtype(optarg);
5717 if (lsa.lsa_pattern == 0) {
5719 "%s %s: bad stripe hash type '%s'\n",
5720 progname, argv[0], optarg);
5725 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5728 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5729 if (strcmp(argv[optind - 1], "--index") == 0)
5731 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5734 lsa.lsa_nr_tgts = parse_targets(mdts,
5735 sizeof(mdts) / sizeof(__u32),
5736 lsa.lsa_nr_tgts, optarg, NULL);
5737 if (lsa.lsa_nr_tgts < 0) {
5739 "%s %s: invalid MDT target(s) '%s'\n",
5740 progname, argv[0], optarg);
5744 lsa.lsa_tgts = mdts;
5745 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5746 lsa.lsa_stripe_off = mdts[0];
5748 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5750 fprintf(stderr, "warning: '-m' is deprecated, "
5751 "use '--mode' or '-o' instead\n");
5760 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5761 progname, argv[0], argv[optind - 1]);
5766 if (optind == argc) {
5767 fprintf(stderr, "%s %s: DIR must be specified\n",
5772 if (xattr && !foreign_mode) {
5773 /* only print a warning as this is armless and will be
5777 "%s %s: xattr has been specified for non-foreign layout\n",
5779 } else if (foreign_mode && !xattr) {
5781 "%s %s: xattr must be provided in foreign mode\n",
5786 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5787 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5789 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5794 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5795 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5797 "%s %s: stripe offset and count must be specified\n",
5803 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5804 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5806 "%s %s: cannot specify -d with -c or -i options\n",
5811 if (mode_opt != NULL) {
5812 mode = strtoul(mode_opt, &end, 8);
5815 "%s %s: bad MODE '%s'\n",
5816 progname, argv[0], mode_opt);
5819 previous_mode = umask(0);
5822 /* foreign LMV/dir case */
5824 if (argc > optind + 1) {
5826 "%s %s: cannot specify multiple foreign dirs\n",
5831 dname = argv[optind];
5832 result = llapi_dir_create_foreign(dname, mode, type, flags,
5836 "%s mkdir: can't create foreign dir '%s': %s\n",
5837 progname, dname, strerror(-result));
5842 * initialize stripe parameters, in case param is converted to specific,
5843 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5845 param = calloc(1, offsetof(typeof(*param),
5846 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5847 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5848 if (param == NULL) {
5850 "%s %s: cannot allocate memory for parameters: %s\n",
5851 progname, argv[0], strerror(ENOMEM));
5855 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5856 param->lsp_stripe_count = lsa.lsa_stripe_count;
5857 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5858 param->lsp_stripe_offset = -1;
5860 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5861 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5862 param->lsp_stripe_pattern = lsa.lsa_pattern;
5864 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
5865 param->lsp_pool = lsa.lsa_pool_name;
5866 param->lsp_is_specific = false;
5867 if (lsa.lsa_nr_tgts > 1) {
5868 if (lsa.lsa_stripe_count > 0 &&
5869 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5870 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5872 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
5873 argv[0], (long long)lsa.lsa_stripe_count,
5879 param->lsp_is_specific = true;
5880 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5881 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5884 if (!default_stripe && (lsa.lsa_pattern & LMV_HASH_FLAG_SPACE)) {
5885 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5891 if (param->lsp_stripe_offset != -1 &&
5892 lsa.lsa_pattern & LMV_HASH_FLAG_SPACE) {
5893 fprintf(stderr, "%s %s: can only specify -H space with -i -1\n",
5899 dname = argv[optind];
5901 if (default_stripe) {
5902 result = llapi_dir_set_default_lmv(dname, param);
5905 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5906 progname, dname, strerror(-result));
5911 * if current \a dname isn't under the same \a mntdir as the
5912 * last one, and the last one was auto-distributed, restore
5915 if (mntdir[0] != '\0' &&
5916 strncmp(dname, mntdir, strlen(mntdir)) &&
5918 param->lsp_is_specific = false;
5919 param->lsp_stripe_offset = -1;
5920 auto_distributed = false;
5924 * TODO: when MDT can allocate object with QoS (LU-9435), below
5925 * code should be removed, instead we should let LMV to allocate
5926 * the starting MDT object, and then let LOD allocate other MDT
5929 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5930 char path[PATH_MAX] = "";
5933 lsb = malloc(sizeof(*lsb));
5941 /* use mntdir for dirname() temporarily */
5942 strncpy(mntdir, dname, sizeof(mntdir) - 1);
5943 if (!realpath(dirname(mntdir), path)) {
5946 "error: invalid path '%s': %s\n",
5947 argv[optind], strerror(errno));
5952 result = llapi_search_mounts(path, 0, mntdir, NULL);
5953 if (result < 0 || mntdir[0] == '\0') {
5955 "No suitable Lustre mount found\n");
5959 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5964 if (param->lsp_stripe_count > lsb->sb_count) {
5966 "error: stripe count %d is too big\n",
5967 param->lsp_stripe_count);
5972 qsort(lsb->sb_buf, lsb->sb_count,
5973 sizeof(struct ll_statfs_data),
5974 ll_statfs_data_comp);
5976 auto_distributed = true;
5979 if (auto_distributed) {
5981 int nr = MAX(param->lsp_stripe_count,
5984 /* don't use server whose usage is above 90% */
5985 while (nr != param->lsp_stripe_count &&
5986 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5988 nr = MAX(param->lsp_stripe_count, nr / 2);
5990 /* get \a r between [0, nr) */
5993 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5994 if (param->lsp_stripe_count > 1) {
5997 param->lsp_is_specific = true;
5998 for (; i < param->lsp_stripe_count; i++)
5999 param->lsp_tgts[(i + r) % nr] =
6000 lsb->sb_buf[i].sd_index;
6004 result = llapi_dir_create(dname, mode, param);
6007 "%s setdirstripe: cannot create dir '%s': %s\n",
6008 progname, dname, strerror(-result));
6009 } while (!result && (dname = argv[++optind]));
6011 if (mode_opt != NULL)
6012 umask(previous_mode);
6020 static int lfs_rmentry(int argc, char **argv)
6027 fprintf(stderr, "error: %s: missing dirname\n",
6033 dname = argv[index];
6034 while (dname != NULL) {
6035 result = llapi_direntry_remove(dname);
6037 fprintf(stderr, "error: %s: remove dir entry '%s' "
6038 "failed\n", argv[0], dname);
6041 dname = argv[++index];
6046 static int lfs_mv(int argc, char **argv)
6048 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6049 struct find_param param = {
6056 struct option long_opts[] = {
6057 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6058 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6059 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6062 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6064 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6066 fprintf(stderr, "warning: '-M' deprecated"
6067 ", use '--mdt-index' or '-m' instead\n");
6070 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6072 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6078 param.fp_verbose = VERBOSE_DETAIL;
6081 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6082 progname, argv[optind - 1]);
6087 if (lmu.lum_stripe_offset == -1) {
6088 fprintf(stderr, "%s mv: MDT index must be specified\n",
6093 if (optind >= argc) {
6094 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6099 /* initialize migrate mdt parameters */
6100 param.fp_lmv_md = &lmu;
6101 param.fp_migrate = 1;
6102 rc = llapi_migrate_mdt(argv[optind], ¶m);
6104 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6105 progname, argv[optind], param.fp_mdt_index,
6110 static int lfs_osts(int argc, char **argv)
6112 return lfs_tgts(argc, argv);
6115 static int lfs_mdts(int argc, char **argv)
6117 return lfs_tgts(argc, argv);
6120 static int lfs_df(int argc, char **argv)
6122 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6123 enum mntdf_flags flags = MNTDF_SHOW;
6124 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6125 int c, rc = 0, index = 0;
6126 char fsname[PATH_MAX] = "", *pool_name = NULL;
6127 struct option long_opts[] = {
6128 { .val = 'h', .name = "human-readable",
6129 .has_arg = no_argument },
6130 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6131 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6132 { .val = 'p', .name = "pool", .has_arg = required_argument },
6133 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6136 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6139 flags |= MNTDF_COOKED;
6142 flags |= MNTDF_INODES;
6145 flags |= MNTDF_LAZY;
6151 flags |= MNTDF_VERBOSE;
6157 if (optind < argc && !realpath(argv[optind], path)) {
6159 fprintf(stderr, "error: invalid path '%s': %s\n",
6160 argv[optind], strerror(-rc));
6164 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6165 /* Check if we have a mount point */
6166 if (mntdir[0] == '\0')
6169 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6170 if (rc || path[0] != '\0')
6172 fsname[0] = '\0'; /* avoid matching in next loop */
6173 mntdir[0] = '\0'; /* avoid matching in next loop */
6179 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
6180 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6184 /* llapi_search_mounts() fills "fsname", but that is not called if
6185 * explicit paths are specified on the command-line
6187 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
6188 int rc = llapi_getname(mntdir, fsname, fsnamelen);
6191 fprintf(stderr, "cannot get instance for '%s': %s\n",
6192 mntdir, strerror(-rc));
6196 if (!opt_instance) {
6197 /* print only the fsname name */
6198 buf = strchr(fsname, '-');
6202 } else if (!opt_fsname) {
6203 /* print only the instance name */
6204 buf = strchr(fsname, '-');
6210 } else if (opt_fsname) {
6211 /* print only the fsname */
6215 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
6220 static int lfs_getname(int argc, char **argv)
6222 struct option long_opts[] = {
6223 { .val = 'h', .name = "help", .has_arg = no_argument },
6224 { .val = 'i', .name = "instance", .has_arg = no_argument },
6225 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6227 bool opt_instance = false, opt_fsname = false;
6228 char fsname[PATH_MAX] = "";
6231 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6234 opt_instance = true;
6245 /* If neither option is given, print both instance and fsname */
6246 if (!opt_instance && !opt_fsname)
6247 opt_instance = opt_fsname = true;
6249 if (optind == argc) { /* no paths specified, get all paths. */
6250 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6253 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6254 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6255 opt_instance, opt_fsname, true);
6258 path[0] = fsname[0] = mntdir[0] = '\0';
6260 } else { /* paths specified, only attempt to search these. */
6263 /* if only one path is given, print only requested info */
6264 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6266 for (; optind < argc; optind++) {
6267 rc2 = print_instance(argv[optind], fsname,
6268 sizeof(fsname), opt_instance,
6269 opt_fsname, opt_mntdir);
6279 static int lfs_check(int argc, char **argv)
6281 char mntdir[PATH_MAX] = {'\0'};
6290 fprintf(stderr, "%s check: server type must be specified\n",
6295 obd_types[0] = obd_type1;
6296 obd_types[1] = obd_type2;
6297 obd_types[2] = obd_type3;
6299 if (strcmp(argv[1], "osts") == 0) {
6300 strcpy(obd_types[0], "osc");
6301 } else if (strcmp(argv[1], "mdts") == 0 ||
6302 strcmp(argv[1], "mds") == 0) {
6303 strcpy(obd_types[0], "mdc");
6304 } else if (strcmp(argv[1], "mgts") == 0) {
6305 strcpy(obd_types[0], "mgc");
6306 } else if (strcmp(argv[1], "all") == 0 ||
6307 strcmp(argv[1], "servers") == 0) {
6309 strcpy(obd_types[0], "osc");
6310 strcpy(obd_types[1], "mdc");
6311 strcpy(obd_types[2], "mgc");
6313 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6318 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6319 if (rc < 0 || mntdir[0] == '\0') {
6321 "%s check: cannot find mounted Lustre filesystem: %s\n",
6322 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6326 rc = llapi_target_check(num_types, obd_types, mntdir);
6328 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6329 progname, argv[1], strerror(-rc));
6335 #ifdef HAVE_SYS_QUOTA_H
6336 #define ARG2INT(nr, str, msg) \
6339 nr = strtol(str, &endp, 0); \
6340 if (*endp != '\0') { \
6341 fprintf(stderr, "%s: bad %s '%s'\n", \
6342 progname, msg, str); \
6347 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6349 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6350 * returns the value or ULONG_MAX on integer overflow or incorrect format
6352 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6353 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6354 * 3. empty integer value is interpreted as 0
6356 static unsigned long str2sec(const char* timestr)
6358 const char spec[] = "smhdw";
6359 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6360 unsigned long val = 0;
6363 if (strpbrk(timestr, spec) == NULL) {
6364 /* no specifiers inside the time string,
6365 should treat it as an integer value */
6366 val = strtoul(timestr, &tail, 10);
6367 return *tail ? ULONG_MAX : val;
6370 /* format string is XXwXXdXXhXXmXXs */
6376 v = strtoul(timestr, &tail, 10);
6377 if (v == ULONG_MAX || *tail == '\0')
6378 /* value too large (ULONG_MAX or more)
6379 or missing specifier */
6382 ptr = strchr(spec, *tail);
6384 /* unknown specifier */
6389 /* check if product will overflow the type */
6390 if (!(v < ULONG_MAX / mult[ind]))
6393 ADD_OVERFLOW(val, mult[ind] * v);
6394 if (val == ULONG_MAX)
6406 #define ARG2ULL(nr, str, def_units) \
6408 unsigned long long limit, units = def_units; \
6411 rc = llapi_parse_size(str, &limit, &units, 1); \
6413 fprintf(stderr, "%s: invalid limit '%s'\n", \
6420 static inline int has_times_option(int argc, char **argv)
6424 for (i = 1; i < argc; i++)
6425 if (!strcmp(argv[i], "-t"))
6431 int lfs_setquota_times(int argc, char **argv)
6434 struct if_quotactl qctl;
6435 char *mnt, *obd_type = (char *)qctl.obd_type;
6436 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6437 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6438 struct option long_opts[] = {
6439 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6440 { .val = 'g', .name = "group", .has_arg = no_argument },
6441 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6442 { .val = 'p', .name = "projid", .has_arg = no_argument },
6443 { .val = 't', .name = "times", .has_arg = no_argument },
6444 { .val = 'u', .name = "user", .has_arg = no_argument },
6448 memset(&qctl, 0, sizeof(qctl));
6449 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6450 qctl.qc_type = ALLQUOTA;
6452 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6453 long_opts, NULL)) != -1) {
6464 if (qctl.qc_type != ALLQUOTA) {
6465 fprintf(stderr, "error: -u/g/p can't be used "
6466 "more than once\n");
6469 qctl.qc_type = qtype;
6472 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6473 fprintf(stderr, "error: bad block-grace: %s\n",
6477 dqb->dqb_valid |= QIF_BTIME;
6480 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6481 fprintf(stderr, "error: bad inode-grace: %s\n",
6485 dqb->dqb_valid |= QIF_ITIME;
6487 case 't': /* Yes, of course! */
6489 default: /* getopt prints error message for us when opterr != 0 */
6494 if (qctl.qc_type == ALLQUOTA) {
6495 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6499 if (optind != argc - 1) {
6500 fprintf(stderr, "error: unexpected parameters encountered\n");
6504 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6505 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6506 fprintf(stderr, "error: grace time is too large\n");
6511 rc = llapi_quotactl(mnt, &qctl);
6514 fprintf(stderr, "%s %s ", obd_type,
6515 obd_uuid2str(&qctl.obd_uuid));
6516 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6523 #define BSLIMIT (1 << 0)
6524 #define BHLIMIT (1 << 1)
6525 #define ISLIMIT (1 << 2)
6526 #define IHLIMIT (1 << 3)
6528 int lfs_setquota(int argc, char **argv)
6531 struct if_quotactl qctl;
6532 char *mnt, *obd_type = (char *)qctl.obd_type;
6533 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6534 struct option long_opts[] = {
6535 { .val = 'b', .name = "block-softlimit",
6536 .has_arg = required_argument },
6537 { .val = 'B', .name = "block-hardlimit",
6538 .has_arg = required_argument },
6539 { .val = 'd', .name = "default", .has_arg = no_argument },
6540 { .val = 'g', .name = "group", .has_arg = required_argument },
6541 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6542 { .val = 'i', .name = "inode-softlimit",
6543 .has_arg = required_argument },
6544 { .val = 'I', .name = "inode-hardlimit",
6545 .has_arg = required_argument },
6546 { .val = 'p', .name = "projid", .has_arg = required_argument },
6547 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6548 { .val = 'u', .name = "user", .has_arg = required_argument },
6549 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6551 unsigned limit_mask = 0;
6553 bool use_default = false;
6556 if (has_times_option(argc, argv))
6557 return lfs_setquota_times(argc, argv);
6559 memset(&qctl, 0, sizeof(qctl));
6560 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6561 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6562 * so it can be used as a marker that qc_type
6563 * isn't reinitialized from command line */
6565 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6566 long_opts, NULL)) != -1) {
6569 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6572 goto quota_type_def;
6575 rc = name2uid(&qctl.qc_id, optarg);
6578 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6581 goto quota_type_def;
6584 rc = name2gid(&qctl.qc_id, optarg);
6587 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6590 goto quota_type_def;
6593 rc = name2projid(&qctl.qc_id, optarg);
6596 qctl.qc_id = strtoul(optarg, &endptr, 10);
6597 if (*endptr != '\0') {
6598 fprintf(stderr, "%s setquota: invalid"
6599 " id '%s'\n", progname, optarg);
6604 if (qctl.qc_id == 0) {
6605 fprintf(stderr, "%s setquota: can't set quota"
6606 " for root usr/group/project.\n",
6612 if (qctl.qc_type != ALLQUOTA) {
6614 "%s setquota: only one of -u, -U, -g,"
6615 " -G, -p or -P may be specified\n",
6619 qctl.qc_type = qtype;
6622 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6626 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6627 dqb->dqb_bsoftlimit >>= 10;
6628 limit_mask |= BSLIMIT;
6629 if (dqb->dqb_bsoftlimit &&
6630 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6632 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6633 "See '%s help setquota' or Lustre manual for details\n",
6635 (unsigned long long)dqb->dqb_bsoftlimit,
6639 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6640 dqb->dqb_bhardlimit >>= 10;
6641 limit_mask |= BHLIMIT;
6642 if (dqb->dqb_bhardlimit &&
6643 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6645 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6646 "See '%s help setquota' or Lustre manual for details\n",
6648 (unsigned long long)dqb->dqb_bhardlimit,
6652 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6653 limit_mask |= ISLIMIT;
6654 if (dqb->dqb_isoftlimit &&
6655 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6657 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6658 "See '%s help setquota' or Lustre manual for details\n",
6660 (unsigned long long)dqb->dqb_isoftlimit,
6664 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6665 limit_mask |= IHLIMIT;
6666 if (dqb->dqb_ihardlimit &&
6667 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6669 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6670 "See '%s help setquota' or Lustre manual for details\n",
6672 (unsigned long long)dqb->dqb_ihardlimit,
6677 "%s setquota: unrecognized option '%s'\n",
6678 progname, argv[optind - 1]);
6683 if (qctl.qc_type == ALLQUOTA) {
6685 "%s setquota: either -u or -g must be specified\n",
6690 if (!use_default && limit_mask == 0) {
6692 "%s setquota: at least one limit must be specified\n",
6697 if (use_default && limit_mask != 0) {
6699 "%s setquota: limits should not be specified when"
6700 " using default quota\n",
6705 if (use_default && qctl.qc_id == 0) {
6707 "%s setquota: can not set default quota for root"
6708 " user/group/project\n",
6713 if (optind != argc - 1) {
6715 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6716 progname, argv[optind]);
6723 dqb->dqb_bhardlimit = 0;
6724 dqb->dqb_bsoftlimit = 0;
6725 dqb->dqb_ihardlimit = 0;
6726 dqb->dqb_isoftlimit = 0;
6729 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6730 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6731 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6732 /* sigh, we can't just set blimits/ilimits */
6733 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6734 .qc_type = qctl.qc_type,
6735 .qc_id = qctl.qc_id};
6737 rc = llapi_quotactl(mnt, &tmp_qctl);
6741 if (!(limit_mask & BHLIMIT))
6742 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6743 if (!(limit_mask & BSLIMIT))
6744 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6745 if (!(limit_mask & IHLIMIT))
6746 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6747 if (!(limit_mask & ISLIMIT))
6748 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6750 /* Keep grace times if we have got no softlimit arguments */
6751 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6752 dqb->dqb_valid |= QIF_BTIME;
6753 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6756 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6757 dqb->dqb_valid |= QIF_ITIME;
6758 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6762 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6763 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6765 rc = llapi_quotactl(mnt, &qctl);
6769 "%s setquota: cannot quotactl '%s' '%s': %s",
6771 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6778 /* Converts seconds value into format string
6779 * result is returned in buf
6781 * 1. result is in descenting order: 1w2d3h4m5s
6782 * 2. zero fields are not filled (except for p. 3): 5d1s
6783 * 3. zero seconds value is presented as "0s"
6785 static char * __sec2str(time_t seconds, char *buf)
6787 const char spec[] = "smhdw";
6788 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6793 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6794 c = seconds / mult[i];
6796 if (c > 0 || (i == 0 && buf == tail))
6797 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6805 static void sec2str(time_t seconds, char *buf, int rc)
6812 tail = __sec2str(seconds, tail);
6814 if (rc && tail - buf < 39) {
6820 static void diff2str(time_t seconds, char *buf, time_t now)
6826 if (seconds <= now) {
6827 strcpy(buf, "none");
6830 __sec2str(seconds - now, buf);
6833 static void print_quota_title(char *name, struct if_quotactl *qctl,
6834 bool human_readable, bool show_default)
6837 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6838 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6839 "Filesystem", "bquota", "blimit", "bgrace",
6840 "iquota", "ilimit", "igrace");
6842 printf("Disk quotas for %s %s (%cid %u):\n",
6843 qtype_name(qctl->qc_type), name,
6844 *qtype_name(qctl->qc_type), qctl->qc_id);
6845 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6846 "Filesystem", human_readable ? "used" : "kbytes",
6847 "quota", "limit", "grace",
6848 "files", "quota", "limit", "grace");
6852 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6855 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6858 snprintf(buf, buflen, "%5.4gP",
6859 (double)num / ((__u64)1 << 40));
6861 snprintf(buf, buflen, "%5.4gT",
6862 (double)num / (1 << 30));
6864 snprintf(buf, buflen, "%5.4gG",
6865 (double)num / (1 << 20));
6867 snprintf(buf, buflen, "%5.4gM",
6868 (double)num / (1 << 10));
6870 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6874 #define STRBUF_LEN 32
6875 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6876 int rc, bool h, bool show_default)
6882 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6883 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6884 int bover = 0, iover = 0;
6885 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6886 char numbuf[3][STRBUF_LEN];
6888 char strbuf[STRBUF_LEN];
6890 if (dqb->dqb_bhardlimit &&
6891 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6893 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6894 if (dqb->dqb_btime > now) {
6901 if (dqb->dqb_ihardlimit &&
6902 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6904 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6905 if (dqb->dqb_itime > now) {
6913 if (strlen(mnt) > 15)
6914 printf("%s\n%15s", mnt, "");
6916 printf("%15s", mnt);
6919 diff2str(dqb->dqb_btime, timebuf, now);
6920 else if (show_default)
6921 snprintf(timebuf, sizeof(timebuf), "%llu",
6922 (unsigned long long)dqb->dqb_btime);
6924 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6925 strbuf, sizeof(strbuf), h);
6926 if (rc == -EREMOTEIO)
6927 sprintf(numbuf[0], "%s*", strbuf);
6929 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6930 "%s" : "[%s]", strbuf);
6932 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6933 if (type == QC_GENERAL)
6934 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6935 "%s" : "[%s]", strbuf);
6937 sprintf(numbuf[1], "%s", "-");
6939 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6940 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6941 "%s" : "[%s]", strbuf);
6944 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6946 printf(" %7s%c %6s %7s %7s",
6947 numbuf[0], bover ? '*' : ' ', numbuf[1],
6948 numbuf[2], bover > 1 ? timebuf : "-");
6952 diff2str(dqb->dqb_itime, timebuf, now);
6953 else if (show_default)
6954 snprintf(timebuf, sizeof(timebuf), "%llu",
6955 (unsigned long long)dqb->dqb_itime);
6957 snprintf(numbuf[0], sizeof(numbuf),
6958 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6959 (uintmax_t)dqb->dqb_curinodes);
6961 if (type == QC_GENERAL)
6962 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6964 (uintmax_t)dqb->dqb_isoftlimit);
6966 sprintf(numbuf[1], "%s", "-");
6968 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6969 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6972 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6973 else if (type != QC_OSTIDX)
6974 printf(" %7s%c %6s %7s %7s",
6975 numbuf[0], iover ? '*' : ' ', numbuf[1],
6976 numbuf[2], iover > 1 ? timebuf : "-");
6978 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6981 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6982 qctl->qc_cmd == Q_GETOINFO) {
6986 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6987 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6988 printf("Block grace time: %s; Inode grace time: %s\n",
6989 bgtimebuf, igtimebuf);
6993 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6994 bool h, __u64 *total)
6996 int rc = 0, rc1 = 0, count = 0;
6997 __u32 valid = qctl->qc_valid;
6999 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7001 fprintf(stderr, "can not get %s count: %s\n",
7002 is_mdt ? "mdt": "ost", strerror(-rc));
7006 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
7007 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7008 rc = llapi_quotactl(mnt, qctl);
7010 /* It is remote client case. */
7011 if (rc == -EOPNOTSUPP) {
7018 fprintf(stderr, "quotactl %s%d failed.\n",
7019 is_mdt ? "mdt": "ost", qctl->qc_idx);
7023 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7024 qctl->qc_valid, 0, h, false);
7025 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7026 qctl->qc_dqblk.dqb_bhardlimit;
7029 qctl->qc_valid = valid;
7033 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7034 int verbose, int quiet, bool human_readable,
7037 int rc1 = 0, rc2 = 0, rc3 = 0;
7038 char *obd_type = (char *)qctl->obd_type;
7039 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7040 __u64 total_ialloc = 0, total_balloc = 0;
7041 bool use_default_for_blk = false;
7042 bool use_default_for_file = false;
7045 rc1 = llapi_quotactl(mnt, qctl);
7049 fprintf(stderr, "%s quotas are not enabled.\n",
7050 qtype_name(qctl->qc_type));
7053 fprintf(stderr, "Permission denied.\n");
7056 /* We already got error message. */
7059 fprintf(stderr, "Unexpected quotactl error: %s\n",
7064 if (!show_default && qctl->qc_id == 0) {
7065 qctl->qc_dqblk.dqb_bhardlimit = 0;
7066 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7067 qctl->qc_dqblk.dqb_ihardlimit = 0;
7068 qctl->qc_dqblk.dqb_isoftlimit = 0;
7069 qctl->qc_dqblk.dqb_btime = 0;
7070 qctl->qc_dqblk.dqb_itime = 0;
7071 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7074 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7075 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7076 use_default_for_blk = true;
7077 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7080 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7081 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7082 use_default_for_file = true;
7083 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7086 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7087 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7088 print_quota_title(name, qctl, human_readable, show_default);
7090 if (rc1 && *obd_type)
7091 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7093 if (qctl->qc_valid != QC_GENERAL)
7096 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
7097 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7098 (QIF_LIMITS|QIF_USAGE));
7100 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7102 if (!show_default && verbose &&
7103 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
7104 char strbuf[STRBUF_LEN];
7106 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7108 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7110 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7112 printf("Total allocated inode limit: %ju, total "
7113 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
7117 if (use_default_for_blk)
7118 printf("%cid %u is using default block quota setting\n",
7119 *qtype_name(qctl->qc_type), qctl->qc_id);
7121 if (use_default_for_file)
7122 printf("%cid %u is using default file quota setting\n",
7123 *qtype_name(qctl->qc_type), qctl->qc_id);
7125 if (rc1 || rc2 || rc3 || inacc)
7126 printf("Some errors happened when getting quota info. "
7127 "Some devices may be not working or deactivated. "
7128 "The data in \"[]\" is inaccurate.\n");
7134 static int lfs_project(int argc, char **argv)
7136 int ret = 0, err = 0, c, i;
7137 struct project_handle_control phc = { 0 };
7138 enum lfs_project_ops_t op;
7141 phc.assign_projid = false;
7142 /* default action */
7143 op = LFS_PROJECT_LIST;
7145 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7148 if (op != LFS_PROJECT_LIST) {
7150 "%s: cannot specify '-c' '-C' '-s' together\n",
7155 op = LFS_PROJECT_CHECK;
7158 if (op != LFS_PROJECT_LIST) {
7160 "%s: cannot specify '-c' '-C' '-s' together\n",
7165 op = LFS_PROJECT_CLEAR;
7168 if (op != LFS_PROJECT_LIST) {
7170 "%s: cannot specify '-c' '-C' '-s' together\n",
7175 phc.set_inherit = true;
7176 op = LFS_PROJECT_SET;
7182 phc.keep_projid = true;
7185 phc.recursive = true;
7188 phc.projid = strtoul(optarg, NULL, 0);
7189 phc.assign_projid = true;
7193 phc.newline = false;
7196 fprintf(stderr, "%s: invalid option '%c'\n",
7202 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7203 op = LFS_PROJECT_SET;
7204 phc.set_projid = true;
7205 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7206 phc.set_projid = true;
7210 case LFS_PROJECT_CHECK:
7211 if (phc.keep_projid) {
7213 "%s: '-k' is useless together with '-c'\n",
7218 case LFS_PROJECT_CLEAR:
7221 "%s: '-0' is useless together with '-C'\n",
7225 if (phc.assign_projid) {
7227 "%s: '-p' is useless together with '-C'\n",
7232 case LFS_PROJECT_SET:
7235 "%s: '-0' is useless together with '-s'\n",
7239 if (phc.keep_projid) {
7241 "%s: '-k' is useless together with '-s'\n",
7249 "%s: '-0' is useless for list operations\n",
7259 fprintf(stderr, "%s: missing file or directory target(s)\n",
7264 for (i = 0; i < argc; i++) {
7266 case LFS_PROJECT_CHECK:
7267 err = lfs_project_check(argv[i], &phc);
7269 case LFS_PROJECT_LIST:
7270 err = lfs_project_list(argv[i], &phc);
7272 case LFS_PROJECT_CLEAR:
7273 err = lfs_project_clear(argv[i], &phc);
7275 case LFS_PROJECT_SET:
7276 err = lfs_project_set(argv[i], &phc);
7288 static int lfs_quota(int argc, char **argv)
7291 char *mnt, *name = NULL;
7292 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
7293 .qc_type = ALLQUOTA };
7294 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
7295 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7297 __u32 valid = QC_GENERAL, idx = 0;
7298 bool human_readable = false;
7299 bool show_default = false;
7302 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
7305 show_default = true;
7310 show_default = true;
7315 show_default = true;
7319 if (qctl.qc_type != ALLQUOTA) {
7321 "%s quota: only one of -u, -g, or -p may be specified\n",
7325 qctl.qc_type = qtype;
7328 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7331 valid = qctl.qc_valid = QC_UUID;
7332 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7335 valid = qctl.qc_valid = QC_MDTIDX;
7336 idx = qctl.qc_idx = atoi(optarg);
7337 if (idx == 0 && *optarg != '0') {
7339 "%s quota: invalid MDT index '%s'\n",
7345 valid = qctl.qc_valid = QC_OSTIDX;
7346 idx = qctl.qc_idx = atoi(optarg);
7347 if (idx == 0 && *optarg != '0') {
7349 "%s quota: invalid OST index '%s'\n",
7361 human_readable = true;
7364 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7365 progname, argv[optind - 1]);
7370 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7371 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7372 optind == argc - 1 && !show_default) {
7374 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7377 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7378 qctl.qc_type = qtype;
7379 qctl.qc_valid = valid;
7380 if (qtype == USRQUOTA) {
7381 qctl.qc_id = geteuid();
7382 rc = uid2name(&name, qctl.qc_id);
7384 qctl.qc_id = getegid();
7385 rc = gid2name(&name, qctl.qc_id);
7386 memset(&qctl.qc_dqblk, 0,
7387 sizeof(qctl.qc_dqblk));
7392 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7393 human_readable, show_default);
7398 /* lfs quota -u username /path/to/lustre/mount */
7399 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7400 /* options should be followed by u/g-name and mntpoint */
7401 if ((!show_default && optind + 2 != argc) ||
7402 (show_default && optind + 1 != argc) ||
7403 qctl.qc_type == ALLQUOTA) {
7405 "%s quota: name and mount point must be specified\n",
7410 if (!show_default) {
7411 name = argv[optind++];
7412 switch (qctl.qc_type) {
7414 rc = name2uid(&qctl.qc_id, name);
7417 rc = name2gid(&qctl.qc_id, name);
7420 rc = name2projid(&qctl.qc_id, name);
7427 qctl.qc_valid = QC_GENERAL;
7428 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7433 qctl.qc_id = strtoul(name, &endptr, 10);
7434 if (*endptr != '\0') {
7435 fprintf(stderr, "%s quota: invalid id '%s'\n",
7440 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7441 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7447 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7448 human_readable, show_default);
7451 #endif /* HAVE_SYS_QUOTA_H! */
7453 static int flushctx_ioctl(char *mp)
7457 fd = open(mp, O_RDONLY);
7459 fprintf(stderr, "flushctx: error open %s: %s\n",
7460 mp, strerror(errno));
7464 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7466 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7467 mp, strerror(errno));
7473 static int lfs_flushctx(int argc, char **argv)
7475 int kdestroy = 0, c;
7476 char mntdir[PATH_MAX] = {'\0'};
7480 while ((c = getopt(argc, argv, "k")) != -1) {
7486 fprintf(stderr, "error: %s: option '-%c' "
7487 "unrecognized\n", argv[0], c);
7493 if ((rc = system("kdestroy > /dev/null")) != 0) {
7494 rc = WEXITSTATUS(rc);
7495 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7499 if (optind >= argc) {
7500 /* flush for all mounted lustre fs. */
7501 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7502 /* Check if we have a mount point */
7503 if (mntdir[0] == '\0')
7506 if (flushctx_ioctl(mntdir))
7509 mntdir[0] = '\0'; /* avoid matching in next loop */
7512 /* flush fs as specified */
7513 while (optind < argc) {
7514 if (flushctx_ioctl(argv[optind++]))
7521 static int lfs_changelog(int argc, char **argv)
7523 void *changelog_priv;
7524 struct changelog_rec *rec;
7525 long long startrec = 0, endrec = 0;
7527 struct option long_opts[] = {
7528 { .val = 'f', .name = "follow", .has_arg = no_argument },
7530 char short_opts[] = "f";
7533 while ((rc = getopt_long(argc, argv, short_opts,
7534 long_opts, NULL)) != -1) {
7541 "%s changelog: unrecognized option '%s'\n",
7542 progname, argv[optind - 1]);
7546 if (optind >= argc) {
7547 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7552 mdd = argv[optind++];
7554 startrec = strtoll(argv[optind++], NULL, 10);
7556 endrec = strtoll(argv[optind++], NULL, 10);
7558 rc = llapi_changelog_start(&changelog_priv,
7559 CHANGELOG_FLAG_BLOCK |
7560 CHANGELOG_FLAG_JOBID |
7561 CHANGELOG_FLAG_EXTRA_FLAGS |
7562 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7565 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7566 progname, strerror(errno = -rc));
7570 rc = llapi_changelog_set_xflags(changelog_priv,
7571 CHANGELOG_EXTRA_FLAG_UIDGID |
7572 CHANGELOG_EXTRA_FLAG_NID |
7573 CHANGELOG_EXTRA_FLAG_OMODE |
7574 CHANGELOG_EXTRA_FLAG_XATTR);
7577 "%s changelog: cannot set xflags for changelog: %s\n",
7578 progname, strerror(errno = -rc));
7582 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7586 if (endrec && rec->cr_index > endrec) {
7587 llapi_changelog_free(&rec);
7590 if (rec->cr_index < startrec) {
7591 llapi_changelog_free(&rec);
7595 secs = rec->cr_time >> 30;
7596 gmtime_r(&secs, &ts);
7597 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7598 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7599 changelog_type2str(rec->cr_type),
7600 ts.tm_hour, ts.tm_min, ts.tm_sec,
7601 (int)(rec->cr_time & ((1 << 30) - 1)),
7602 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7603 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7605 if (rec->cr_flags & CLF_JOBID) {
7606 struct changelog_ext_jobid *jid =
7607 changelog_rec_jobid(rec);
7609 if (jid->cr_jobid[0] != '\0')
7610 printf(" j=%s", jid->cr_jobid);
7613 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7614 struct changelog_ext_extra_flags *ef =
7615 changelog_rec_extra_flags(rec);
7617 printf(" ef=0x%llx",
7618 (unsigned long long)ef->cr_extra_flags);
7620 if (ef->cr_extra_flags & CLFE_UIDGID) {
7621 struct changelog_ext_uidgid *uidgid =
7622 changelog_rec_uidgid(rec);
7624 printf(" u=%llu:%llu",
7625 (unsigned long long)uidgid->cr_uid,
7626 (unsigned long long)uidgid->cr_gid);
7628 if (ef->cr_extra_flags & CLFE_NID) {
7629 struct changelog_ext_nid *nid =
7630 changelog_rec_nid(rec);
7633 libcfs_nid2str(nid->cr_nid));
7636 if (ef->cr_extra_flags & CLFE_OPEN) {
7637 struct changelog_ext_openmode *omd =
7638 changelog_rec_openmode(rec);
7639 char mode[] = "---";
7641 /* exec mode must be exclusive */
7642 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7645 if (omd->cr_openflags & MDS_FMODE_READ)
7647 if (omd->cr_openflags &
7654 if (strcmp(mode, "---") != 0)
7655 printf(" m=%s", mode);
7659 if (ef->cr_extra_flags & CLFE_XATTR) {
7660 struct changelog_ext_xattr *xattr =
7661 changelog_rec_xattr(rec);
7663 if (xattr->cr_xattr[0] != '\0')
7664 printf(" x=%s", xattr->cr_xattr);
7668 if (rec->cr_namelen)
7669 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7670 rec->cr_namelen, changelog_rec_name(rec));
7672 if (rec->cr_flags & CLF_RENAME) {
7673 struct changelog_ext_rename *rnm =
7674 changelog_rec_rename(rec);
7676 if (!fid_is_zero(&rnm->cr_sfid))
7677 printf(" s="DFID" sp="DFID" %.*s",
7678 PFID(&rnm->cr_sfid),
7679 PFID(&rnm->cr_spfid),
7680 (int)changelog_rec_snamelen(rec),
7681 changelog_rec_sname(rec));
7685 llapi_changelog_free(&rec);
7688 llapi_changelog_fini(&changelog_priv);
7691 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7692 progname, strerror(errno = -rc));
7694 return (rc == 1 ? 0 : rc);
7697 static int lfs_changelog_clear(int argc, char **argv)
7705 endrec = strtoll(argv[3], NULL, 10);
7707 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7710 fprintf(stderr, "%s: record out of range: %llu\n",
7712 else if (rc == -ENOENT)
7713 fprintf(stderr, "%s: no changelog user: %s\n",
7716 fprintf(stderr, "%s error: %s\n", argv[0],
7725 static int lfs_fid2path(int argc, char **argv)
7727 struct option long_opts[] = {
7728 { .val = 'c', .name = "cur", .has_arg = no_argument },
7729 { .val = 'l', .name = "link", .has_arg = required_argument },
7730 { .val = 'r', .name = "rec", .has_arg = required_argument },
7732 char short_opts[] = "cl:r:";
7733 char *device, *fid, *path;
7734 long long recno = -1;
7739 char *endptr = NULL;
7741 while ((rc = getopt_long(argc, argv, short_opts,
7742 long_opts, NULL)) != -1) {
7748 linkno = strtol(optarg, &endptr, 10);
7749 if (*endptr != '\0') {
7751 "%s fid2path: invalid linkno '%s'\n",
7757 recno = strtoll(optarg, &endptr, 10);
7758 if (*endptr != '\0') {
7760 "%s fid2path: invalid recno '%s'\n",
7767 "%s fid2path: unrecognized option '%s'\n",
7768 progname, argv[optind - 1]);
7775 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7780 device = argv[optind++];
7781 path = calloc(1, PATH_MAX);
7785 "%s fid2path: cannot allocate memory for path: %s\n",
7786 progname, strerror(-rc));
7791 while (optind < argc) {
7792 fid = argv[optind++];
7794 lnktmp = (linkno >= 0) ? linkno : 0;
7796 int oldtmp = lnktmp;
7797 long long rectmp = recno;
7799 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7803 "%s fid2path: cannot find '%s': %s\n",
7804 progname, fid, strerror(errno = -rc2));
7811 fprintf(stdout, "%lld ", rectmp);
7812 if (device[0] == '/') {
7813 fprintf(stdout, "%s", device);
7814 if (device[strlen(device) - 1] != '/')
7815 fprintf(stdout, "/");
7816 } else if (path[0] == '\0') {
7817 fprintf(stdout, "/");
7819 fprintf(stdout, "%s\n", path);
7822 /* specified linkno */
7824 if (oldtmp == lnktmp)
7834 static int lfs_path2fid(int argc, char **argv)
7836 struct option long_opts[] = {
7837 { .val = 'p', .name = "parents", .has_arg = no_argument },
7840 const char short_opts[] = "p";
7841 const char *sep = "";
7844 bool show_parents = false;
7846 while ((rc = getopt_long(argc, argv, short_opts,
7847 long_opts, NULL)) != -1) {
7850 show_parents = true;
7854 "%s path2fid: unrecognized option '%s'\n",
7855 progname, argv[optind - 1]);
7860 if (optind > argc - 1) {
7861 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7865 else if (optind < argc - 1)
7869 for (path = argv + optind; *path != NULL; path++) {
7871 if (!show_parents) {
7872 err = llapi_path2fid(*path, &fid);
7874 printf("%s%s"DFID"\n",
7875 *sep != '\0' ? *path : "", sep,
7878 char name[NAME_MAX + 1];
7879 unsigned int linkno = 0;
7881 while ((err = llapi_path2parent(*path, linkno, &fid,
7882 name, sizeof(name))) == 0) {
7883 if (*sep != '\0' && linkno == 0)
7884 printf("%s%s", *path, sep);
7886 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7891 /* err == -ENODATA is end-of-loop */
7892 if (linkno > 0 && err == -ENODATA) {
7900 "%s path2fid: cannot get %sfid for '%s': %s\n",
7901 progname, show_parents ? "parent " : "", *path,
7913 #define MAX_ERRNO 4095
7914 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
7916 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
7920 rc = llapi_rmfid(device, fa);
7922 fprintf(stderr, "rmfid(): rc = %d\n", rc);
7926 for (k = 0; k < fa->fa_nr; k++) {
7927 rc = (__s32)fa->fa_fids[k].f_ver;
7928 if (!IS_ERR_VALUE(rc))
7934 fa->fa_fids[k].f_ver = 0;
7935 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
7936 PFID(&fa->fa_fids[k]), rc);
7942 static int lfs_rmfid(int argc, char **argv)
7944 char *fidstr, *device;
7945 int rc = 0, rc2, nr;
7946 struct fid_array *fa;
7948 if (optind > argc - 1) {
7949 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
7953 device = argv[optind++];
7956 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
7962 while (optind < argc) {
7965 fidstr = argv[optind++];
7966 while (*fidstr == '[')
7968 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
7970 fprintf(stderr, "unrecognized FID: %s\n",
7975 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
7976 /* start another batch */
7977 rc2 = lfs_rmfid_and_show_errors(device, fa);
7984 rc2 = lfs_rmfid_and_show_errors(device, fa);
7992 static int lfs_data_version(int argc, char **argv)
7999 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8002 fprintf(stderr, "%s data_version: FILE must be specified\n",
8007 while ((c = getopt(argc, argv, "nrw")) != -1) {
8010 data_version_flags = 0;
8013 data_version_flags |= LL_DV_RD_FLUSH;
8016 data_version_flags |= LL_DV_WR_FLUSH;
8020 "%s data_version: unrecognized option '%s'\n",
8021 progname, argv[optind - 1]);
8025 if (optind == argc) {
8026 fprintf(stderr, "%s data_version: FILE must be specified\n",
8031 path = argv[optind];
8032 fd = open(path, O_RDONLY);
8035 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8036 progname, path, strerror(-rc));
8040 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8043 "%s data_version: cannot get version for '%s': %s\n",
8044 progname, path, strerror(-rc));
8046 printf("%ju" "\n", (uintmax_t)data_version);
8052 static int lfs_hsm_state(int argc, char **argv)
8057 struct hsm_user_state hus;
8065 rc = llapi_hsm_state_get(path, &hus);
8067 fprintf(stderr, "can't get hsm state for %s: %s\n",
8068 path, strerror(errno = -rc));
8072 /* Display path name and status flags */
8073 printf("%s: (0x%08x)", path, hus.hus_states);
8075 if (hus.hus_states & HS_RELEASED)
8076 printf(" released");
8077 if (hus.hus_states & HS_EXISTS)
8079 if (hus.hus_states & HS_DIRTY)
8081 if (hus.hus_states & HS_ARCHIVED)
8082 printf(" archived");
8083 /* Display user-settable flags */
8084 if (hus.hus_states & HS_NORELEASE)
8085 printf(" never_release");
8086 if (hus.hus_states & HS_NOARCHIVE)
8087 printf(" never_archive");
8088 if (hus.hus_states & HS_LOST)
8089 printf(" lost_from_hsm");
8091 if (hus.hus_archive_id != 0)
8092 printf(", archive_id:%d", hus.hus_archive_id);
8095 } while (++i < argc);
8100 #define LFS_HSM_SET 0
8101 #define LFS_HSM_CLEAR 1
8104 * Generic function to set or clear HSM flags.
8105 * Used by hsm_set and hsm_clear.
8107 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8109 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8111 struct option long_opts[] = {
8112 { .val = 'A', .name = "archived", .has_arg = no_argument },
8113 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8114 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8115 { .val = 'e', .name = "exists", .has_arg = no_argument },
8116 { .val = 'l', .name = "lost", .has_arg = no_argument },
8117 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8118 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8120 char short_opts[] = "lraAdei:";
8124 __u32 archive_id = 0;
8130 while ((c = getopt_long(argc, argv, short_opts,
8131 long_opts, NULL)) != -1) {
8137 mask |= HS_NOARCHIVE;
8140 mask |= HS_ARCHIVED;
8143 mask |= HS_NORELEASE;
8152 archive_id = strtol(optarg, &end, 10);
8154 fprintf(stderr, "invalid archive_id: '%s'\n",
8162 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8163 argv[0], argv[optind - 1]);
8168 /* User should have specified a flag */
8172 while (optind < argc) {
8174 path = argv[optind];
8176 /* If mode == 0, this means we apply the mask. */
8177 if (mode == LFS_HSM_SET)
8178 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8180 rc = llapi_hsm_state_set(path, 0, mask, 0);
8183 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8184 path, strerror(errno = -rc));
8193 static int lfs_hsm_action(int argc, char **argv)
8198 struct hsm_current_action hca;
8199 struct hsm_extent he;
8200 enum hsm_user_action hua;
8201 enum hsm_progress_states hps;
8209 rc = llapi_hsm_current_action(path, &hca);
8211 fprintf(stderr, "can't get hsm action for %s: %s\n",
8212 path, strerror(errno = -rc));
8215 he = hca.hca_location;
8216 hua = hca.hca_action;
8217 hps = hca.hca_state;
8219 printf("%s: %s", path, hsm_user_action2name(hua));
8221 /* Skip file without action */
8222 if (hca.hca_action == HUA_NONE) {
8227 printf(" %s ", hsm_progress_state2name(hps));
8229 if ((hps == HPS_RUNNING) &&
8230 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8231 printf("(%llu bytes moved)\n",
8232 (unsigned long long)he.length);
8233 else if ((he.offset + he.length) == LUSTRE_EOF)
8234 printf("(from %llu to EOF)\n",
8235 (unsigned long long)he.offset);
8237 printf("(from %llu to %llu)\n",
8238 (unsigned long long)he.offset,
8239 (unsigned long long)(he.offset + he.length));
8241 } while (++i < argc);
8246 static int lfs_hsm_set(int argc, char **argv)
8248 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8251 static int lfs_hsm_clear(int argc, char **argv)
8253 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8257 * Check file state and return its fid, to be used by lfs_hsm_request().
8259 * \param[in] file Path to file to check
8260 * \param[in,out] fid Pointer to allocated lu_fid struct.
8261 * \param[in,out] last_dev Pointer to last device id used.
8263 * \return 0 on success.
8265 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8271 rc = lstat(file, &st);
8273 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8276 /* Checking for regular file as archiving as posix copytool
8277 * rejects archiving files other than regular files
8279 if (!S_ISREG(st.st_mode)) {
8280 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8283 /* A request should be ... */
8284 if (*last_dev != st.st_dev && *last_dev != 0) {
8285 fprintf(stderr, "All files should be "
8286 "on the same filesystem: %s\n", file);
8289 *last_dev = st.st_dev;
8291 rc = llapi_path2fid(file, fid);
8293 fprintf(stderr, "Cannot read FID of %s: %s\n",
8294 file, strerror(-rc));
8300 /* Fill an HSM HUR item with a given file name.
8302 * If mntpath is set, then the filename is actually a FID, and no
8303 * lookup on the filesystem will be performed.
8305 * \param[in] hur the user request to fill
8306 * \param[in] idx index of the item inside the HUR to fill
8307 * \param[in] mntpath mountpoint of Lustre
8308 * \param[in] fname filename (if mtnpath is NULL)
8309 * or FID (if mntpath is set)
8310 * \param[in] last_dev pointer to last device id used
8312 * \retval 0 on success
8313 * \retval CMD_HELP or a negative errno on error
8315 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8316 const char *mntpath, const char *fname,
8319 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8322 hui->hui_extent.length = -1;
8324 if (mntpath != NULL) {
8327 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
8331 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8336 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8340 hur->hur_request.hr_itemcount++;
8345 static int lfs_hsm_request(int argc, char **argv, int action)
8347 struct option long_opts[] = {
8348 { .val = 'a', .name = "archive", .has_arg = required_argument },
8349 { .val = 'D', .name = "data", .has_arg = required_argument },
8350 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8351 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8354 char short_opts[] = "l:D:a:m:";
8355 struct hsm_user_request *hur, *oldhur;
8360 char *filelist = NULL;
8361 char fullpath[PATH_MAX];
8362 char *opaque = NULL;
8366 int nbfile_alloc = 0;
8367 char *some_file = NULL;
8368 char *mntpath = NULL;
8374 while ((c = getopt_long(argc, argv, short_opts,
8375 long_opts, NULL)) != -1) {
8384 if (action != HUA_ARCHIVE &&
8385 action != HUA_REMOVE) {
8387 "error: -a is supported only "
8388 "when archiving or removing\n");
8391 archive_id = atoi(optarg);
8394 if (some_file == NULL) {
8396 some_file = strdup(optarg);
8402 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8403 argv[0], argv[optind - 1]);
8408 /* All remaining args are files, so we have at least nbfile */
8409 nbfile = argc - optind;
8411 if ((nbfile == 0) && (filelist == NULL))
8415 opaque_len = strlen(opaque);
8417 /* Alloc the request structure with enough place to store all files
8418 * from command line. */
8419 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8421 fprintf(stderr, "Cannot create the request: %s\n",
8425 nbfile_alloc = nbfile;
8427 hur->hur_request.hr_action = action;
8428 hur->hur_request.hr_archive_id = archive_id;
8429 hur->hur_request.hr_flags = 0;
8431 /* All remaining args are files, add them */
8432 if (nbfile != 0 && some_file == NULL)
8433 some_file = strdup(argv[optind]);
8435 for (i = 0; i < nbfile; i++) {
8436 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8442 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8444 /* If a filelist was specified, read the filelist from it. */
8445 if (filelist != NULL) {
8446 fp = fopen(filelist, "r");
8448 fprintf(stderr, "Cannot read the file list %s: %s\n",
8449 filelist, strerror(errno));
8454 while ((rc = getline(&line, &len, fp)) != -1) {
8455 /* If allocated buffer was too small, get something
8457 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8460 nbfile_alloc = nbfile_alloc * 2 + 1;
8462 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8465 fprintf(stderr, "hsm: cannot allocate "
8466 "the request: %s\n",
8473 size = hur_len(oldhur);
8475 fprintf(stderr, "hsm: cannot allocate "
8476 "%u files + %u bytes data\n",
8477 oldhur->hur_request.hr_itemcount,
8478 oldhur->hur_request.hr_data_len);
8485 memcpy(hur, oldhur, size);
8490 if (line[strlen(line) - 1] == '\n')
8491 line[strlen(line) - 1] = '\0';
8493 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8494 mntpath, line, &last_dev);
8500 if (some_file == NULL) {
8510 /* If a --data was used, add it to the request */
8511 hur->hur_request.hr_data_len = opaque_len;
8513 memcpy(hur_data(hur), opaque, opaque_len);
8515 /* Send the HSM request */
8516 if (realpath(some_file, fullpath) == NULL) {
8517 fprintf(stderr, "Could not find path '%s': %s\n",
8518 some_file, strerror(errno));
8520 rc = llapi_hsm_request(fullpath, hur);
8522 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8523 some_file, strerror(-rc));
8533 static int lfs_hsm_archive(int argc, char **argv)
8535 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8538 static int lfs_hsm_restore(int argc, char **argv)
8540 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8543 static int lfs_hsm_release(int argc, char **argv)
8545 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8548 static int lfs_hsm_remove(int argc, char **argv)
8550 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8553 static int lfs_hsm_cancel(int argc, char **argv)
8555 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8558 static int lfs_swap_layouts(int argc, char **argv)
8563 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8564 SWAP_LAYOUTS_KEEP_MTIME |
8565 SWAP_LAYOUTS_KEEP_ATIME);
8568 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8570 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8572 int lfs_get_mode(const char *string)
8574 enum lock_mode_user mode;
8576 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8577 if (lock_mode_names[mode] == NULL)
8579 if (strcmp(string, lock_mode_names[mode]) == 0)
8586 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8588 enum lu_ladvise_type advice;
8591 advice < ARRAY_SIZE(ladvise_names); advice++) {
8592 if (ladvise_names[advice] == NULL)
8594 if (strcmp(string, ladvise_names[advice]) == 0)
8598 return LU_LADVISE_INVALID;
8601 static int lfs_ladvise(int argc, char **argv)
8603 struct option long_opts[] = {
8604 { .val = 'a', .name = "advice", .has_arg = required_argument },
8605 { .val = 'b', .name = "background", .has_arg = no_argument },
8606 { .val = 'e', .name = "end", .has_arg = required_argument },
8607 { .val = 'l', .name = "length", .has_arg = required_argument },
8608 { .val = 'm', .name = "mode", .has_arg = required_argument },
8609 { .val = 's', .name = "start", .has_arg = required_argument },
8610 { .val = 'u', .name = "unset", .has_arg = no_argument },
8612 char short_opts[] = "a:be:l:m:s:u";
8617 struct llapi_lu_ladvise advice;
8618 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8619 unsigned long long start = 0;
8620 unsigned long long end = LUSTRE_EOF;
8621 unsigned long long length = 0;
8622 unsigned long long size_units;
8623 unsigned long long flags = 0;
8627 while ((c = getopt_long(argc, argv, short_opts,
8628 long_opts, NULL)) != -1) {
8631 advice_type = lfs_get_ladvice(optarg);
8632 if (advice_type == LU_LADVISE_INVALID) {
8633 fprintf(stderr, "%s: invalid advice type "
8634 "'%s'\n", argv[0], optarg);
8635 fprintf(stderr, "Valid types:");
8637 for (advice_type = 0;
8638 advice_type < ARRAY_SIZE(ladvise_names);
8640 if (ladvise_names[advice_type] == NULL)
8642 fprintf(stderr, " %s",
8643 ladvise_names[advice_type]);
8645 fprintf(stderr, "\n");
8658 rc = llapi_parse_size(optarg, &end,
8661 fprintf(stderr, "%s: bad end offset '%s'\n",
8668 rc = llapi_parse_size(optarg, &start,
8671 fprintf(stderr, "%s: bad start offset "
8672 "'%s'\n", argv[0], optarg);
8678 rc = llapi_parse_size(optarg, &length,
8681 fprintf(stderr, "%s: bad length '%s'\n",
8687 mode = lfs_get_mode(optarg);
8689 fprintf(stderr, "%s: bad mode '%s', valid "
8690 "modes are READ or WRITE\n",
8698 fprintf(stderr, "%s: option '%s' unrecognized\n",
8699 argv[0], argv[optind - 1]);
8704 if (advice_type == LU_LADVISE_INVALID) {
8705 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8706 fprintf(stderr, "Valid types:");
8707 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8709 if (ladvise_names[advice_type] == NULL)
8711 fprintf(stderr, " %s", ladvise_names[advice_type]);
8713 fprintf(stderr, "\n");
8717 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8718 fprintf(stderr, "%s: Lock no expand advice is a per file "
8719 "descriptor advice, so when called from lfs, "
8720 "it does nothing.\n", argv[0]);
8724 if (argc <= optind) {
8725 fprintf(stderr, "%s: please give one or more file names\n",
8730 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8731 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8736 if (end == LUSTRE_EOF && length != 0)
8737 end = start + length;
8740 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8741 argv[0], start, end);
8745 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8746 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8751 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8752 fprintf(stderr, "%s: mode is required with lockahead\n",
8757 while (optind < argc) {
8760 path = argv[optind++];
8762 fd = open(path, O_RDONLY);
8764 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8765 argv[0], path, strerror(errno));
8770 advice.lla_start = start;
8771 advice.lla_end = end;
8772 advice.lla_advice = advice_type;
8773 advice.lla_value1 = 0;
8774 advice.lla_value2 = 0;
8775 advice.lla_value3 = 0;
8776 advice.lla_value4 = 0;
8777 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8778 advice.lla_lockahead_mode = mode;
8779 advice.lla_peradvice_flags = flags;
8782 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8785 fprintf(stderr, "%s: cannot give advice '%s' to file "
8786 "'%s': %s\n", argv[0],
8787 ladvise_names[advice_type],
8788 path, strerror(errno));
8794 if (rc == 0 && rc2 < 0)
8800 static const char *const heat_names[] = LU_HEAT_NAMES;
8802 static int lfs_heat_get(int argc, char **argv)
8804 struct lu_heat *heat;
8813 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8815 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8820 while (optind < argc) {
8821 path = argv[optind++];
8823 fd = open(path, O_RDONLY);
8825 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8826 argv[0], path, strerror(errno));
8831 heat->lh_count = OBD_HEAT_COUNT;
8832 rc2 = llapi_heat_get(fd, heat);
8835 fprintf(stderr, "%s: cannot get heat of file '%s'"
8836 ": %s\n", argv[0], path, strerror(errno));
8840 printf("flags: %x\n", heat->lh_flags);
8841 for (i = 0; i < heat->lh_count; i++)
8842 printf("%s: %llu\n", heat_names[i],
8843 (unsigned long long)heat->lh_heat[i]);
8845 if (rc == 0 && rc2 < 0)
8853 static int lfs_heat_set(int argc, char **argv)
8855 struct option long_opts[] = {
8856 { .val = 'c', .name = "clear", .has_arg = no_argument },
8857 { .val = 'o', .name = "off", .has_arg = no_argument },
8858 { .val = 'O', .name = "on", .has_arg = no_argument },
8860 enum lu_heat_flag flags = 0;
8870 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8873 flags |= LU_HEAT_FLAG_CLEAR;
8876 flags |= LU_HEAT_FLAG_CLEAR;
8877 flags |= LU_HEAT_FLAG_OFF;
8880 flags &= ~LU_HEAT_FLAG_OFF;
8885 fprintf(stderr, "%s: option '%s' unrecognized\n",
8886 argv[0], argv[optind - 1]);
8891 if (argc <= optind) {
8892 fprintf(stderr, "%s: please give one or more file names\n",
8897 while (optind < argc) {
8898 path = argv[optind++];
8900 fd = open(path, O_RDONLY);
8902 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8903 argv[0], path, strerror(errno));
8908 rc2 = llapi_heat_set(fd, flags);
8911 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8912 ": %s\n", argv[0], path, strerror(errno));
8916 if (rc == 0 && rc2 < 0)
8922 /** The input string contains a comma delimited list of component ids and
8923 * ranges, for example "1,2-4,7".
8925 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8927 bool end_of_loop = false;
8935 while (!end_of_loop) {
8939 char *endptr = NULL;
8942 ptr = strchrnul(arg, ',');
8943 end_of_loop = *ptr == '\0';
8946 start_index = strtol(arg, &endptr, 0);
8947 if (endptr == arg) /* no data at all */
8949 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8951 if (start_index < 0)
8954 end_index = start_index;
8955 if (*endptr == '-') {
8956 end_index = strtol(endptr + 1, &endptr, 0);
8957 if (*endptr != '\0')
8959 if (end_index < start_index)
8963 for (i = start_index; i <= end_index && size > 0; i++) {
8966 /* remove duplicate */
8967 for (j = 0; j < nr; j++) {
8971 if (j == nr) { /* no duplicate */
8977 if (size == 0 && i < end_index)
8984 if (!end_of_loop && ptr != NULL)
8987 return rc < 0 ? rc : nr;
8991 * struct verify_mirror_id - Mirror id to be verified.
8992 * @mirror_id: A specified mirror id.
8993 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8995 struct verify_mirror_id {
9001 * compare_mirror_ids() - Compare mirror ids.
9002 * @layout: Mirror component list.
9003 * @cbdata: Callback data in verify_mirror_id structure.
9005 * This is a callback function called by llapi_layout_comp_iterate()
9006 * to compare the specified mirror id with the one in the current
9007 * component of @layout. If they are the same, then the specified
9008 * mirror id is valid.
9010 * Return: a negative error code on failure or
9011 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9012 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9015 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9017 struct verify_mirror_id *mirror_id_cbdata =
9018 (struct verify_mirror_id *)cbdata;
9022 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9026 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9027 progname, strerror(errno));
9031 if (mirror_id_cbdata->mirror_id == mirror_id) {
9032 mirror_id_cbdata->is_valid_id = true;
9033 return LLAPI_LAYOUT_ITER_STOP;
9036 return LLAPI_LAYOUT_ITER_CONT;
9040 * verify_mirror_ids() - Verify specified mirror ids.
9041 * @fname: Mirrored file name.
9042 * @mirror_ids: Specified mirror ids to be verified.
9043 * @ids_nr: Number of specified mirror ids.
9045 * This function verifies that specified @mirror_ids are valid
9046 * in the mirrored file @fname.
9048 * Return: 0 on success or a negative error code on failure.
9051 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9053 struct llapi_layout *layout = NULL;
9054 struct verify_mirror_id mirror_id_cbdata = { 0 };
9065 if (stat(fname, &stbuf) < 0) {
9066 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9067 progname, fname, strerror(errno));
9072 if (!S_ISREG(stbuf.st_mode)) {
9073 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9079 fd = open(fname, O_DIRECT | O_RDONLY);
9081 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9082 progname, fname, strerror(errno));
9087 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9089 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9090 progname, fname, strerror(errno));
9094 layout = llapi_layout_get_by_fd(fd, 0);
9095 if (layout == NULL) {
9096 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9097 progname, fname, strerror(errno));
9099 llapi_lease_release(fd);
9103 rc = llapi_layout_flags_get(layout, &flr_state);
9105 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9106 progname, fname, strerror(errno));
9111 flr_state &= LCM_FL_FLR_MASK;
9112 switch (flr_state) {
9115 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9116 progname, fname, llapi_layout_flags_string(flr_state));
9123 for (i = 0; i < ids_nr; i++) {
9124 mirror_id_cbdata.mirror_id = mirror_ids[i];
9125 mirror_id_cbdata.is_valid_id = false;
9127 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9132 "%s: '%s' failed to verify mirror id: %u.\n",
9133 progname, fname, mirror_ids[i]);
9137 if (!mirror_id_cbdata.is_valid_id) {
9140 "%s: '%s' invalid specified mirror id: %u.\n",
9141 progname, fname, mirror_ids[i]);
9147 llapi_layout_free(layout);
9148 llapi_lease_release(fd);
9156 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9157 __u16 *mirror_ids, int ids_nr)
9159 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9160 struct llapi_layout *layout;
9171 if (stat(fname, &stbuf) < 0) {
9172 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9173 progname, fname, strerror(errno));
9177 if (!S_ISREG(stbuf.st_mode)) {
9178 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9184 fd = open(fname, O_DIRECT | O_RDWR);
9186 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9187 progname, fname, strerror(errno));
9192 layout = llapi_layout_get_by_fd(fd, 0);
9193 if (layout == NULL) {
9194 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9195 progname, fname, strerror(errno));
9200 rc = llapi_layout_flags_get(layout, &flr_state);
9202 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9203 progname, fname, strerror(errno));
9208 flr_state &= LCM_FL_FLR_MASK;
9209 if (flr_state == LCM_FL_NONE) {
9211 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9216 /* get stale component info */
9217 comp_size = llapi_mirror_find_stale(layout, comp_array,
9218 ARRAY_SIZE(comp_array),
9219 mirror_ids, ids_nr);
9220 if (comp_size <= 0) {
9225 ioc->lil_mode = LL_LEASE_WRLCK;
9226 ioc->lil_flags = LL_LEASE_RESYNC;
9227 rc = llapi_lease_set(fd, ioc);
9229 if (rc == -EALREADY)
9233 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9234 progname, fname, strerror(-rc));
9238 /* get the read range [start, end) */
9239 start = comp_array[0].lrc_start;
9240 end = comp_array[0].lrc_end;
9241 for (idx = 1; idx < comp_size; idx++) {
9242 if (comp_array[idx].lrc_start < start)
9243 start = comp_array[idx].lrc_start;
9244 if (end < comp_array[idx].lrc_end)
9245 end = comp_array[idx].lrc_end;
9248 rc = llapi_lease_check(fd);
9249 if (rc != LL_LEASE_WRLCK) {
9250 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9255 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9258 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9259 progname, fname, strerror(-rc));
9261 /* need to do the lease unlock even resync fails */
9262 ioc->lil_mode = LL_LEASE_UNLCK;
9263 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9265 for (idx = 0; idx < comp_size; idx++) {
9266 if (comp_array[idx].lrc_synced) {
9267 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9272 rc2 = llapi_lease_set(fd, ioc);
9274 * llapi_lease_set returns lease mode when it request to unlock
9278 /* rc2 == 0 means lost lease lock */
9279 if (rc2 == 0 && rc == 0)
9281 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9283 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9287 llapi_layout_free(layout);
9294 static inline int lfs_mirror_resync(int argc, char **argv)
9296 struct ll_ioc_lease *ioc = NULL;
9297 __u16 mirror_ids[128] = { 0 };
9302 struct option long_opts[] = {
9303 { .val = 'o', .name = "only", .has_arg = required_argument },
9306 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9309 rc = parse_mirror_ids(mirror_ids,
9310 sizeof(mirror_ids) / sizeof(__u16),
9314 "%s: bad mirror ids '%s'.\n",
9321 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9322 argv[0], argv[optind - 1]);
9328 if (argc == optind) {
9329 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9334 if (ids_nr > 0 && argc > optind + 1) {
9336 "%s: option '--only' cannot be used upon multiple files.\n",
9344 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9349 /* set the lease on the file */
9350 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9352 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9353 argv[0], strerror(errno));
9358 for (; optind < argc; optind++) {
9359 rc = lfs_mirror_resync_file(argv[optind], ioc,
9360 mirror_ids, ids_nr);
9361 /* ignore previous file's error, continue with next file */
9364 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9372 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9374 struct llapi_layout *layout;
9377 layout = llapi_layout_get_by_fd(fd, 0);
9378 if (layout == NULL) {
9379 fprintf(stderr, "could not get layout.\n");
9383 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9385 fprintf(stderr, "failed to iterate layout\n");
9386 llapi_layout_free(layout);
9389 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9390 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9391 llapi_layout_free(layout);
9395 llapi_layout_free(layout);
9401 * Check whether two files are the same file
9402 * \retval 0 same file
9403 * \retval 1 not the same file
9404 * \retval <0 error code
9406 static inline int check_same_file(const char *f1, const char *f2)
9411 if (stat(f1, &stbuf1) < 0) {
9412 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9413 progname, f1, strerror(errno));
9417 if (stat(f2, &stbuf2) < 0) {
9418 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9419 progname, f2, strerror(errno));
9423 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9424 stbuf1.st_ino == stbuf2.st_ino)
9430 static inline int lfs_mirror_read(int argc, char **argv)
9433 __u16 mirror_id = 0;
9434 const char *outfile = NULL;
9440 const size_t buflen = 4 << 20;
9442 struct option long_opts[] = {
9443 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9444 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9447 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9452 mirror_id = strtoul(optarg, &end, 0);
9453 if (*end != '\0' || mirror_id == 0) {
9455 "%s %s: invalid mirror ID '%s'\n",
9456 progname, argv[0], optarg);
9464 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9465 progname, argv[optind - 1]);
9470 if (argc == optind) {
9471 fprintf(stderr, "%s %s: no mirrored file provided\n",
9474 } else if (argc > optind + 1) {
9475 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9479 if (mirror_id == 0) {
9480 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9485 /* open mirror file */
9486 fname = argv[optind];
9489 rc = check_same_file(fname, outfile);
9492 "%s %s: output file cannot be the mirrored file\n",
9500 fd = open(fname, O_DIRECT | O_RDONLY);
9502 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9503 progname, argv[0], fname, strerror(errno));
9507 /* verify mirror id */
9508 rc = verify_mirror_id_by_fd(fd, mirror_id);
9511 "%s %s: cannot find mirror with ID %u in '%s'\n",
9512 progname, argv[0], mirror_id, fname);
9516 /* open output file */
9518 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9520 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9521 progname, argv[0], outfile, strerror(errno));
9526 outfd = STDOUT_FILENO;
9529 /* allocate buffer */
9530 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9532 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9533 progname, argv[0], rc);
9540 ssize_t written = 0;
9542 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9543 if (bytes_read < 0) {
9546 "%s %s: fail to read data from mirror %u: %s\n",
9547 progname, argv[0], mirror_id, strerror(-rc));
9552 if (bytes_read == 0)
9555 while (written < bytes_read) {
9558 written2 = write(outfd, buf + written,
9559 bytes_read - written);
9562 "%s %s: fail to write %s: %s\n",
9563 progname, argv[0], outfile ? : "STDOUT",
9568 written += written2;
9571 if (written != bytes_read) {
9573 "%s %s: written %ld bytes does not match with %ld read.\n",
9574 progname, argv[0], written, bytes_read);
9596 static inline int lfs_mirror_write(int argc, char **argv)
9599 __u16 mirror_id = 0;
9600 const char *inputfile = NULL;
9606 const size_t buflen = 4 << 20;
9608 size_t page_size = sysconf(_SC_PAGESIZE);
9609 struct ll_ioc_lease_id ioc;
9611 struct option long_opts[] = {
9612 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9613 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9616 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9621 mirror_id = strtoul(optarg, &end, 0);
9622 if (*end != '\0' || mirror_id == 0) {
9624 "%s %s: invalid mirror ID '%s'\n",
9625 progname, argv[0], optarg);
9633 fprintf(stderr, "%s: option '%s' unrecognized\n",
9634 progname, argv[optind - 1]);
9639 if (argc == optind) {
9640 fprintf(stderr, "%s %s: no mirrored file provided\n",
9643 } else if (argc > optind + 1) {
9644 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9648 if (mirror_id == 0) {
9649 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9654 /* open mirror file */
9655 fname = argv[optind];
9658 rc = check_same_file(fname, inputfile);
9661 "%s %s: input file cannot be the mirrored file\n",
9669 fd = open(fname, O_DIRECT | O_WRONLY);
9671 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9672 progname, argv[0], fname, strerror(errno));
9676 /* verify mirror id */
9677 rc = verify_mirror_id_by_fd(fd, mirror_id);
9680 "%s %s: cannot find mirror with ID %u in '%s'\n",
9681 progname, argv[0], mirror_id, fname);
9685 /* open input file */
9687 inputfd = open(inputfile, O_RDONLY, 0644);
9689 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9690 progname, argv[0], inputfile, strerror(errno));
9695 inputfd = STDIN_FILENO;
9698 /* allocate buffer */
9699 rc = posix_memalign(&buf, page_size, buflen);
9701 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9702 progname, argv[0], rc);
9706 /* prepare target mirror components instantiation */
9707 ioc.lil_mode = LL_LEASE_WRLCK;
9708 ioc.lil_flags = LL_LEASE_RESYNC;
9709 ioc.lil_mirror_id = mirror_id;
9710 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9713 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9714 progname, argv[0], fname, strerror(errno));
9724 rc = llapi_lease_check(fd);
9725 if (rc != LL_LEASE_WRLCK) {
9726 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9727 progname, argv[0], fname);
9731 bytes_read = read(inputfd, buf, buflen);
9732 if (bytes_read < 0) {
9735 "%s %s: fail to read data from '%s': %s\n",
9736 progname, argv[0], inputfile ? : "STDIN",
9743 if (bytes_read == 0)
9746 /* round up to page align to make direct IO happy. */
9747 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9749 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9754 "%s %s: fail to write to mirror %u: %s\n",
9755 progname, argv[0], mirror_id,
9763 if (pos & (page_size - 1)) {
9764 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9769 ioc.lil_mode = LL_LEASE_UNLCK;
9770 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9772 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9777 "%s %s: release lease lock of '%s' failed: %s\n",
9778 progname, argv[0], fname, strerror(errno));
9795 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9797 struct llapi_layout *layout;
9798 struct collect_ids_data cid = { .cid_ids = ids,
9800 .cid_exclude = exclude_id, };
9803 layout = llapi_layout_get_by_fd(fd, 0);
9804 if (layout == NULL) {
9805 fprintf(stderr, "could not get layout\n");
9809 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9811 fprintf(stderr, "failed to iterate layout\n");
9812 llapi_layout_free(layout);
9816 llapi_layout_free(layout);
9818 return cid.cid_count;
9821 static inline int lfs_mirror_copy(int argc, char **argv)
9824 __u16 read_mirror_id = 0;
9825 __u16 ids[128] = { 0 };
9827 struct llapi_layout *layout = NULL;
9828 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9835 struct ll_ioc_lease *ioc = NULL;
9836 struct ll_ioc_lease_id *resync_ioc;
9838 struct option long_opts[] = {
9839 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9840 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9843 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9848 read_mirror_id = strtoul(optarg, &end, 0);
9849 if (*end != '\0' || read_mirror_id == 0) {
9851 "%s %s: invalid read mirror ID '%s'\n",
9852 progname, argv[0], optarg);
9857 if (!strcmp(optarg, "-1")) {
9858 /* specify all other mirrors */
9862 count = parse_mirror_ids((__u16 *)ids,
9870 fprintf(stderr, "%s: option '%s' unrecognized\n",
9871 progname, argv[optind - 1]);
9876 if (argc == optind) {
9877 fprintf(stderr, "%s %s: no mirrored file provided\n",
9880 } else if (argc > optind + 1) {
9881 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9885 if (read_mirror_id == 0) {
9887 "%s %s: no valid read mirror ID %d is provided\n",
9888 progname, argv[0], read_mirror_id);
9894 "%s %s: no write mirror ID is provided\n",
9899 for (i = 0; i < count; i++) {
9900 if (read_mirror_id == ids[i]) {
9902 "%s %s: read and write mirror ID cannot be the same\n",
9908 /* open mirror file */
9909 fname = argv[optind];
9911 fd = open(fname, O_DIRECT | O_RDWR);
9913 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9914 progname, argv[0], fname, strerror(errno));
9918 /* write to all other mirrors */
9919 if (ids[0] == (__u16)-1) {
9920 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9924 "%s %s: failed to get other mirror ids in '%s': %d\n",
9925 progname, argv[0], fname, rc);
9930 /* verify mirror id */
9931 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9934 "%s %s: cannot find mirror with ID %u in '%s'\n",
9935 progname, argv[0], read_mirror_id, fname);
9939 for (i = 0; i < count; i++) {
9940 rc = verify_mirror_id_by_fd(fd, ids[i]);
9943 "%s %s: cannot find mirror with ID %u in '%s'\n",
9944 progname, argv[0], ids[i], fname);
9949 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9952 "%s %s: cannot alloc comp id array for ioc: %s\n",
9953 progname, argv[0], strerror(errno));
9958 /* get stale component info */
9959 layout = llapi_layout_get_by_fd(fd, 0);
9960 if (layout == NULL) {
9961 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9962 progname, argv[0], fname, strerror(errno));
9966 comp_size = llapi_mirror_find_stale(layout, comp_array,
9967 ARRAY_SIZE(comp_array),
9969 llapi_layout_free(layout);
9970 if (comp_size < 0) {
9975 /* prepare target mirror components instantiation */
9976 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9977 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9978 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9980 resync_ioc->lil_mirror_id = ids[0];
9982 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9983 rc = llapi_lease_set(fd, ioc);
9986 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9987 progname, argv[0], fname, strerror(errno));
9991 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9994 fprintf(stderr, "%s %s: copy error: %d\n",
9995 progname, argv[0], rc);
9999 fprintf(stdout, "mirror copied successfully: ");
10000 for (i = 0; i < copied; i++)
10001 fprintf(stdout, "%d ", ids[i]);
10002 fprintf(stdout, "\n");
10004 ioc->lil_mode = LL_LEASE_UNLCK;
10005 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10006 ioc->lil_count = 0;
10007 for (i = 0; i < comp_size; i++) {
10010 for (j = 0; j < copied; j++) {
10011 if (comp_array[i].lrc_mirror_id != ids[j])
10014 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10018 rc = llapi_lease_set(fd, ioc);
10023 "%s %s: release lease lock of '%s' failed: %s\n",
10024 progname, argv[0], fname, strerror(errno));
10038 * struct verify_chunk - Mirror chunk to be verified.
10039 * @chunk: [start, end) of the chunk.
10040 * @mirror_count: Number of mirror ids in @mirror_id array.
10041 * @mirror_id: Array of valid mirror ids that cover the chunk.
10043 struct verify_chunk {
10044 struct lu_extent chunk;
10045 unsigned int mirror_count;
10046 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10050 * print_chunks() - Print chunk information.
10051 * @fname: Mirrored file name.
10052 * @chunks: Array of chunks.
10053 * @chunk_count: Number of chunks in @chunks array.
10055 * This function prints [start, end) of each chunk in @chunks
10056 * for mirrored file @fname, and also prints the valid mirror ids
10057 * that cover the chunk.
10062 void print_chunks(const char *fname, struct verify_chunk *chunks,
10068 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10069 for (i = 0; i < chunk_count; i++) {
10070 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10072 if (chunks[i].mirror_count == 0)
10073 fprintf(stdout, "\t[");
10075 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10076 for (j = 1; j < chunks[i].mirror_count; j++)
10077 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10079 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10081 fprintf(stdout, "\n");
10085 * print_checksums() - Print CRC-32 checksum values.
10086 * @chunk: A chunk and its corresponding valid mirror ids.
10087 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10089 * This function prints CRC-32 checksum values on @chunk for
10090 * each valid mirror that covers it.
10095 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10100 "CRC-32 checksum value for chunk "DEXT":\n",
10101 PEXT(&chunk->chunk));
10102 for (i = 0; i < chunk->mirror_count; i++)
10103 fprintf(stdout, "Mirror %u:\t%#lx\n",
10104 chunk->mirror_id[i], crc[i]);
10105 fprintf(stdout, "\n");
10109 * filter_mirror_id() - Filter specified mirror ids.
10110 * @chunks: Array of chunks.
10111 * @chunk_count: Number of chunks in @chunks array.
10112 * @mirror_ids: Specified mirror ids to be verified.
10113 * @ids_nr: Number of specified mirror ids.
10115 * This function scans valid mirror ids that cover each chunk in @chunks
10116 * and filters specified mirror ids.
10121 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10122 __u16 *mirror_ids, int ids_nr)
10127 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10128 unsigned int valid_count = 0;
10130 for (i = 0; i < chunk_count; i++) {
10131 if (chunks[i].mirror_count == 0)
10135 for (j = 0; j < ids_nr; j++) {
10136 for (k = 0; k < chunks[i].mirror_count; k++) {
10137 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10138 valid_id[valid_count] = mirror_ids[j];
10145 memcpy(chunks[i].mirror_id, valid_id,
10146 sizeof(__u16) * valid_count);
10147 chunks[i].mirror_count = valid_count;
10152 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10153 * @layout: Mirror component list.
10154 * @chunks: Array of chunks.
10155 * @chunks_size: Array size of @chunks.
10157 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10158 * to find out chunk segments and store them in @chunks array.
10160 * The @mirror_id array in each element of @chunks will store the valid
10161 * mirror ids that cover the chunk. If a mirror component covering the
10162 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10163 * will not be stored into the @mirror_id array, and the chunk for that
10164 * mirror will not be verified.
10166 * The @mirror_count in each element of @chunks will store the number of
10167 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10168 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10169 * indicates the chunk is valid in only one mirror. In both cases, the
10170 * chunk will not be verified.
10172 * Here is an example:
10174 * 0 1M 2M 3M 4M EOF
10175 * +------+-------------+--------------------+
10176 * | | | S | mirror1
10177 * +------+------+------+------+-------------+
10178 * | | S | S | | mirror2
10179 * +-------------+------+------+-------------+
10181 * prepared @chunks array will contain 5 elements:
10182 * (([0, 1M), [1, 2], 2),
10183 * ([1M, 2M), [1, 2], 2),
10184 * ([2M, 3M), [1], 1),
10185 * ([3M, 4M], [], 0),
10186 * ([4M, EOF), [2], 1))
10188 * Return: the actual array size of @chunks on success
10189 * or a negative error code on failure.
10192 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10193 struct verify_chunk *chunks,
10194 size_t chunks_size)
10198 uint32_t mirror_id;
10204 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10207 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10210 "%s: move to the first layout component: %s.\n",
10211 progname, strerror(errno));
10217 chunks[idx].chunk.e_end = LUSTRE_EOF;
10219 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10222 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10223 progname, strerror(errno));
10227 if (start > chunks[idx].chunk.e_start ||
10228 end <= chunks[idx].chunk.e_start)
10231 if (end < chunks[idx].chunk.e_end)
10232 chunks[idx].chunk.e_end = end;
10234 rc = llapi_layout_comp_flags_get(layout, &flags);
10237 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10238 progname, strerror(errno));
10242 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10245 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10248 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10249 progname, strerror(errno));
10253 chunks[idx].mirror_id[i] = mirror_id;
10255 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10257 "%s: mirror_id array is too small.\n",
10264 rc = llapi_layout_comp_use(layout,
10265 LLAPI_LAYOUT_COMP_USE_NEXT);
10268 "%s: move to the next layout component: %s.\n",
10269 progname, strerror(errno));
10272 } /* loop through all components */
10274 chunks[idx].mirror_count = i;
10276 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10280 if (idx >= chunks_size) {
10281 fprintf(stderr, "%s: chunks array is too small.\n",
10287 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10291 return rc < 0 ? rc : idx + 1;
10295 * lfs_mirror_verify_chunk() - Verify a chunk.
10296 * @fd: File descriptor of the mirrored file.
10297 * @file_size: Size of the mirrored file.
10298 * @chunk: A chunk and its corresponding valid mirror ids.
10299 * @verbose: Verbose mode.
10301 * This function verifies a @chunk contains exactly the same data
10302 * ammong the mirrors that cover it.
10304 * If @verbose is specified, then the function will print where the
10305 * differences are if the data do not match. Otherwise, it will
10306 * just return an error in that case.
10308 * Return: 0 on success or a negative error code on failure.
10311 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10312 struct verify_chunk *chunk, int verbose)
10314 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10316 size_t page_size = sysconf(_SC_PAGESIZE);
10317 ssize_t bytes_read;
10318 ssize_t bytes_done;
10322 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10326 if (file_size == 0)
10329 rc = posix_memalign(&buf, page_size, buflen);
10330 if (rc) /* error code is returned directly */
10334 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10335 PEXT(&chunk->chunk));
10336 for (i = 0; i < chunk->mirror_count; i++)
10337 fprintf(stdout, " %u", chunk->mirror_id[i]);
10338 fprintf(stdout, "\n");
10342 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10343 pos = chunk->chunk.e_start;
10344 while (bytes_done < count) {
10345 /* compute initial CRC-32 checksum */
10346 crc = crc32(0L, Z_NULL, 0);
10347 memset(crc_array, 0, sizeof(crc_array));
10350 for (i = 0; i < chunk->mirror_count; i++) {
10351 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10353 if (bytes_read < 0) {
10356 "%s: failed to read data from mirror %u: %s.\n",
10357 progname, chunk->mirror_id[i],
10362 /* compute new CRC-32 checksum */
10363 crc_array[i] = crc32(crc, buf, bytes_read);
10367 print_checksums(chunk, crc_array);
10369 /* compare CRC-32 checksum values */
10370 for (i = 1; i < chunk->mirror_count; i++) {
10371 if (crc_array[i] != crc_array[0]) {
10375 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10376 progname, PEXT(&chunk->chunk),
10377 chunk->mirror_id[0],
10378 chunk->mirror_id[i]);
10383 bytes_done += bytes_read;
10386 if (verbose > 1 && rc == 0) {
10387 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10388 PEXT(&chunk->chunk));
10389 for (i = 0; i < chunk->mirror_count; i++)
10390 fprintf(stdout, " %u", chunk->mirror_id[i]);
10391 fprintf(stdout, " PASS\n\n");
10400 * lfs_mirror_verify_file() - Verify a mirrored file.
10401 * @fname: Mirrored file name.
10402 * @mirror_ids: Specified mirror ids to be verified.
10403 * @ids_nr: Number of specified mirror ids.
10404 * @verbose: Verbose mode.
10406 * This function verifies that each SYNC mirror of a mirrored file
10407 * specified by @fname contains exactly the same data.
10409 * If @mirror_ids is specified, then the function will verify the
10410 * mirrors specified by @mirror_ids contain exactly the same data.
10412 * If @verbose is specified, then the function will print where the
10413 * differences are if the data do not match. Otherwise, it will
10414 * just return an error in that case.
10416 * Return: 0 on success or a negative error code on failure.
10419 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10422 struct verify_chunk chunks_array[1024] = { };
10423 struct llapi_layout *layout = NULL;
10425 uint32_t flr_state;
10427 int chunk_count = 0;
10433 if (stat(fname, &stbuf) < 0) {
10434 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10435 progname, fname, strerror(errno));
10440 if (!S_ISREG(stbuf.st_mode)) {
10441 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10447 if (stbuf.st_size == 0) {
10449 fprintf(stdout, "%s: '%s' file size is 0.\n",
10455 fd = open(fname, O_DIRECT | O_RDONLY);
10457 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10458 progname, fname, strerror(errno));
10463 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10465 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10466 progname, fname, strerror(errno));
10470 layout = llapi_layout_get_by_fd(fd, 0);
10471 if (layout == NULL) {
10472 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10473 progname, fname, strerror(errno));
10475 llapi_lease_release(fd);
10479 rc = llapi_layout_flags_get(layout, &flr_state);
10481 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10482 progname, fname, strerror(errno));
10487 flr_state &= LCM_FL_FLR_MASK;
10488 switch (flr_state) {
10491 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10492 progname, fname, llapi_layout_flags_string(flr_state));
10498 /* find out mirror chunks to be verified */
10499 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10500 ARRAY_SIZE(chunks_array));
10501 if (chunk_count < 0) {
10507 /* filter specified mirror ids */
10508 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10511 print_chunks(fname, chunks_array, chunk_count);
10513 for (idx = 0; idx < chunk_count; idx++) {
10514 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10517 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10519 PEXT(&chunks_array[idx].chunk),
10520 (unsigned long long)stbuf.st_size);
10524 if (chunks_array[idx].mirror_count == 0) {
10526 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10528 PEXT(&chunks_array[idx].chunk));
10530 fprintf(stderr, "skipped\n");
10534 fprintf(stderr, "failed\n");
10538 if (chunks_array[idx].mirror_count == 1) {
10541 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10543 PEXT(&chunks_array[idx].chunk),
10544 chunks_array[idx].mirror_id[0]);
10548 rc = llapi_lease_check(fd);
10549 if (rc != LL_LEASE_RDLCK) {
10550 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10555 /* verify one chunk */
10556 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10557 &chunks_array[idx], verbose);
10571 llapi_layout_free(layout);
10572 llapi_lease_release(fd);
10580 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10581 * @argc: The count of lfs mirror verify command line arguments.
10582 * @argv: Array of strings for lfs mirror verify command line arguments.
10584 * This function parses lfs mirror verify command and verifies the
10585 * specified mirrored file(s).
10587 * Return: 0 on success or a negative error code on failure.
10589 static inline int lfs_mirror_verify(int argc, char **argv)
10591 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10597 char cmd[PATH_MAX];
10599 struct option long_opts[] = {
10600 { .val = 'o', .name = "only", .has_arg = required_argument },
10601 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10602 { .name = NULL } };
10604 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10606 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10609 rc = parse_mirror_ids(mirror_ids,
10610 ARRAY_SIZE(mirror_ids),
10614 "%s: bad mirror ids '%s'.\n",
10621 "%s: at least 2 mirror ids needed with '--only' option.\n",
10631 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10632 progname, argv[optind - 1]);
10638 if (argc == optind) {
10639 fprintf(stderr, "%s: no file name given.\n", progname);
10644 if (ids_nr > 0 && argc > optind + 1) {
10646 "%s: '--only' cannot be used upon multiple files.\n",
10654 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10660 for (; optind < argc; optind++) {
10661 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10671 * lfs_mirror() - Parse and execute lfs mirror commands.
10672 * @argc: The count of lfs mirror command line arguments.
10673 * @argv: Array of strings for lfs mirror command line arguments.
10675 * This function parses lfs mirror commands and performs the
10676 * corresponding functions specified in mirror_cmdlist[].
10678 * Return: 0 on success or an error code on failure.
10680 static int lfs_mirror(int argc, char **argv)
10682 char cmd[PATH_MAX];
10685 setlinebuf(stdout);
10687 Parser_init("lfs-mirror > ", mirror_cmdlist);
10689 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10691 program_invocation_short_name = cmd;
10693 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10695 rc = Parser_commands();
10697 return rc < 0 ? -rc : rc;
10700 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10702 #if __BYTE_ORDER == __BIG_ENDIAN
10703 __swab16s(&attrs->lsa_valid);
10704 __swab64s(&attrs->lsa_size);
10705 __swab64s(&attrs->lsa_blocks);
10709 enum lfs_som_type {
10710 LFS_SOM_SIZE = 0x1,
10711 LFS_SOM_BLOCKS = 0x2,
10712 LFS_SOM_FLAGS = 0x4,
10713 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10717 static int lfs_getsom(int argc, char **argv)
10720 struct lustre_som_attrs *attrs;
10721 char buf[sizeof(*attrs) + 64];
10722 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10725 while ((c = getopt(argc, argv, "sbf")) != -1) {
10728 type = LFS_SOM_SIZE;
10731 type = LFS_SOM_BLOCKS;
10734 type = LFS_SOM_FLAGS;
10737 fprintf(stderr, "%s: invalid option '%c'\n",
10747 fprintf(stderr, "%s: %s\n",
10748 progname, argc == 0 ? "miss file target" :
10749 "input more than 2 files");
10754 attrs = (void *)buf;
10755 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10758 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10759 argv[0], strerror(errno), errno);
10763 lustre_som_swab(attrs);
10766 case LFS_SOM_ATTR_ALL:
10767 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10768 path, (unsigned long long)attrs->lsa_size,
10769 (unsigned long long)attrs->lsa_blocks,
10773 printf("%llu\n", (unsigned long long)attrs->lsa_size);
10775 case LFS_SOM_BLOCKS:
10776 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
10778 case LFS_SOM_FLAGS:
10779 printf("%x\n", attrs->lsa_valid);
10782 fprintf(stderr, "%s: unknown option\n", progname);
10790 * lfs_mirror_list_commands() - List lfs mirror commands.
10791 * @argc: The count of command line arguments.
10792 * @argv: Array of strings for command line arguments.
10794 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10796 * Return: 0 on success.
10798 static int lfs_mirror_list_commands(int argc, char **argv)
10800 char buffer[81] = "";
10802 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10808 static int lfs_pcc_attach(int argc, char **argv)
10810 struct option long_opts[] = {
10811 { .val = 'i', .name = "id", .has_arg = required_argument },
10812 { .name = NULL } };
10815 __u32 archive_id = 0;
10818 char fullpath[PATH_MAX];
10819 enum lu_pcc_type type = LU_PCC_READWRITE;
10822 while ((c = getopt_long(argc, argv, "i:",
10823 long_opts, NULL)) != -1) {
10826 archive_id = strtoul(optarg, &end, 0);
10827 if (*end != '\0' || archive_id == 0) {
10828 fprintf(stderr, "error: %s: bad archive ID "
10829 "'%s'\n", argv[0], optarg);
10836 fprintf(stderr, "%s: option '%s' unrecognized\n",
10837 argv[0], argv[optind - 1]);
10842 if (argc <= optind) {
10843 fprintf(stderr, "%s: must specify one or more file names\n",
10848 while (optind < argc) {
10851 path = argv[optind++];
10852 if (realpath(path, fullpath) == NULL) {
10853 fprintf(stderr, "%s: could not find path '%s': %s\n",
10854 argv[0], path, strerror(errno));
10860 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10862 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10863 "with archive ID '%u': %s\n", argv[0],
10864 path, archive_id, strerror(-rc2));
10872 static int lfs_pcc_attach_fid(int argc, char **argv)
10874 struct option long_opts[] = {
10875 { .val = 'i', .name = "id", .has_arg = required_argument },
10876 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10877 { .name = NULL } };
10878 char short_opts[] = "i:m:";
10881 __u32 archive_id = 0;
10883 const char *mntpath = NULL;
10884 const char *fidstr;
10885 enum lu_pcc_type type = LU_PCC_READWRITE;
10888 while ((c = getopt_long(argc, argv, short_opts,
10889 long_opts, NULL)) != -1) {
10892 archive_id = strtoul(optarg, &end, 0);
10893 if (*end != '\0') {
10894 fprintf(stderr, "error: %s: bad archive ID "
10895 "'%s'\n", argv[0], optarg);
10905 fprintf(stderr, "%s: option '%s' unrecognized\n",
10906 argv[0], argv[optind - 1]);
10911 if (archive_id == 0) {
10912 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10916 if (mntpath == NULL) {
10917 fprintf(stderr, "%s: must specify Lustre mount point\n",
10922 if (argc <= optind) {
10923 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10927 while (optind < argc) {
10930 fidstr = argv[optind++];
10932 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10935 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10936 "with archive ID '%u': %s\n", argv[0],
10937 fidstr, mntpath, archive_id, strerror(rc2));
10939 if (rc == 0 && rc2 < 0)
10945 static int lfs_pcc_detach(int argc, char **argv)
10947 struct option long_opts[] = {
10948 { .val = 'k', .name = "keep", .has_arg = no_argument },
10949 { .name = NULL } };
10950 char short_opts[] = "k";
10954 char fullpath[PATH_MAX];
10955 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10958 while ((c = getopt_long(argc, argv, short_opts,
10959 long_opts, NULL)) != -1) {
10962 detach_opt = PCC_DETACH_OPT_NONE;
10967 fprintf(stderr, "%s: option '%s' unrecognized\n",
10968 argv[0], argv[optind - 1]);
10973 while (optind < argc) {
10976 path = argv[optind++];
10977 if (realpath(path, fullpath) == NULL) {
10978 fprintf(stderr, "%s: could not find path '%s': %s\n",
10979 argv[0], path, strerror(errno));
10985 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
10988 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
10989 "%s\n", argv[0], path, strerror(errno));
10997 static int lfs_pcc_detach_fid(int argc, char **argv)
10999 struct option long_opts[] = {
11000 { .val = 'k', .name = "keep", .has_arg = no_argument },
11001 { .name = NULL } };
11002 char short_opts[] = "k";
11006 const char *mntpath;
11007 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11010 while ((c = getopt_long(argc, argv, short_opts,
11011 long_opts, NULL)) != -1) {
11014 detach_opt = PCC_DETACH_OPT_NONE;
11019 fprintf(stderr, "%s: option '%s' unrecognized\n",
11020 argv[0], argv[optind - 1]);
11025 mntpath = argv[optind++];
11027 while (optind < argc) {
11030 fid = argv[optind++];
11032 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11034 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
11035 "from PCC: %s\n", argv[0], fid, mntpath,
11044 static int lfs_pcc_state(int argc, char **argv)
11048 char fullpath[PATH_MAX];
11049 struct lu_pcc_state state;
11054 fprintf(stderr, "%s: must specify one or more file names\n",
11059 while (optind < argc) {
11062 path = argv[optind++];
11063 if (realpath(path, fullpath) == NULL) {
11064 fprintf(stderr, "%s: could not find path '%s': %s\n",
11065 argv[0], path, strerror(errno));
11071 rc2 = llapi_pcc_state_get(fullpath, &state);
11075 fprintf(stderr, "%s: cannot get PCC state of '%s': "
11076 "%s\n", argv[0], path, strerror(-rc2));
11080 printf("file: %s", path);
11081 printf(", type: %s", pcc_type2string(state.pccs_type));
11082 if (state.pccs_type == LU_PCC_NONE &&
11083 state.pccs_open_count == 0) {
11088 printf(", PCC file: %s", state.pccs_path);
11089 printf(", user number: %u", state.pccs_open_count);
11090 printf(", flags: %x", state.pccs_flags);
11097 * lfs_pcc_list_commands() - List lfs pcc commands.
11098 * @argc: The count of command line arguments.
11099 * @argv: Array of strings for command line arguments.
11101 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11103 * Return: 0 on success.
11105 static int lfs_pcc_list_commands(int argc, char **argv)
11107 char buffer[81] = "";
11109 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11116 * lfs_pcc() - Parse and execute lfs pcc commands.
11117 * @argc: The count of lfs pcc command line arguments.
11118 * @argv: Array of strings for lfs pcc command line arguments.
11120 * This function parses lfs pcc commands and performs the
11121 * corresponding functions specified in pcc_cmdlist[].
11123 * Return: 0 on success or an error code on failure.
11125 static int lfs_pcc(int argc, char **argv)
11127 char cmd[PATH_MAX];
11130 setlinebuf(stdout);
11132 Parser_init("lfs-pcc > ", pcc_cmdlist);
11134 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11136 program_invocation_short_name = cmd;
11138 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11140 rc = Parser_commands();
11142 return rc < 0 ? -rc : rc;
11145 static int lfs_list_commands(int argc, char **argv)
11147 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11149 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11154 int main(int argc, char **argv)
11158 /* Ensure that liblustreapi constructor has run */
11159 if (!llapi_liblustreapi_initialized())
11160 fprintf(stderr, "liblustreapi was not properly initialized\n");
11162 setlinebuf(stdout);
11165 Parser_init("lfs > ", cmdlist);
11167 progname = program_invocation_short_name; /* Used in error messages */
11169 llapi_set_command_name(argv[1]);
11170 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11171 llapi_clear_command_name();
11173 rc = Parser_commands();
11176 return rc < 0 ? -rc : rc;
11179 #ifdef _LUSTRE_IDL_H_
11180 /* Everything we need here should be included by lustreapi.h. */
11181 # error "lfs should not depend on lustre_idl.h"
11182 #endif /* _LUSTRE_IDL_H_ */