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 <libcfs/util/string.h>
75 #include <lustre/lustreapi.h>
76 #include <linux/lustre/lustre_ver.h>
77 #include <linux/lustre/lustre_param.h>
78 #include <linux/lnet/nidstr.h>
82 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
83 #endif /* !ARRAY_SIZE */
86 static int lfs_find(int argc, char **argv);
87 static int lfs_getstripe(int argc, char **argv);
88 static int lfs_getdirstripe(int argc, char **argv);
89 static int lfs_setdirstripe(int argc, char **argv);
90 static int lfs_rmentry(int argc, char **argv);
91 static int lfs_osts(int argc, char **argv);
92 static int lfs_mdts(int argc, char **argv);
93 static int lfs_df(int argc, char **argv);
94 static int lfs_getname(int argc, char **argv);
95 static int lfs_check(int argc, char **argv);
96 #ifdef HAVE_SYS_QUOTA_H
97 static int lfs_setquota(int argc, char **argv);
98 static int lfs_quota(int argc, char **argv);
99 static int lfs_project(int argc, char **argv);
101 static int lfs_flushctx(int argc, char **argv);
102 static int lfs_poollist(int argc, char **argv);
103 static int lfs_changelog(int argc, char **argv);
104 static int lfs_changelog_clear(int argc, char **argv);
105 static int lfs_fid2path(int argc, char **argv);
106 static int lfs_path2fid(int argc, char **argv);
107 static int lfs_rmfid(int argc, char **argv);
108 static int lfs_data_version(int argc, char **argv);
109 static int lfs_hsm_state(int argc, char **argv);
110 static int lfs_hsm_set(int argc, char **argv);
111 static int lfs_hsm_clear(int argc, char **argv);
112 static int lfs_hsm_action(int argc, char **argv);
113 static int lfs_hsm_archive(int argc, char **argv);
114 static int lfs_hsm_restore(int argc, char **argv);
115 static int lfs_hsm_release(int argc, char **argv);
116 static int lfs_hsm_remove(int argc, char **argv);
117 static int lfs_hsm_cancel(int argc, char **argv);
118 static int lfs_swap_layouts(int argc, char **argv);
119 static int lfs_mv(int argc, char **argv);
120 static int lfs_ladvise(int argc, char **argv);
121 static int lfs_getsom(int argc, char **argv);
122 static int lfs_heat_get(int argc, char **argv);
123 static int lfs_heat_set(int argc, char **argv);
124 static int lfs_mirror(int argc, char **argv);
125 static int lfs_mirror_list_commands(int argc, char **argv);
126 static int lfs_list_commands(int argc, char **argv);
127 static inline int lfs_mirror_resync(int argc, char **argv);
128 static inline int lfs_mirror_verify(int argc, char **argv);
129 static inline int lfs_mirror_read(int argc, char **argv);
130 static inline int lfs_mirror_write(int argc, char **argv);
131 static inline int lfs_mirror_copy(int argc, char **argv);
132 static int lfs_pcc_attach(int argc, char **argv);
133 static int lfs_pcc_attach_fid(int argc, char **argv);
134 static int lfs_pcc_detach(int argc, char **argv);
135 static int lfs_pcc_detach_fid(int argc, char **argv);
136 static int lfs_pcc_state(int argc, char **argv);
137 static int lfs_pcc(int argc, char **argv);
138 static int lfs_pcc_list_commands(int argc, char **argv);
139 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
140 __u64 migration_flags,
141 struct llapi_stripe_param *param,
142 struct llapi_layout *layout);
144 enum setstripe_origin {
153 static int lfs_setstripe_internal(int argc, char **argv,
154 enum setstripe_origin opc);
156 static inline int lfs_setstripe(int argc, char **argv)
158 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
160 static inline int lfs_setstripe_migrate(int argc, char **argv)
162 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
164 static inline int lfs_mirror_create(int argc, char **argv)
166 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
168 static inline int lfs_mirror_extend(int argc, char **argv)
170 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
172 static inline int lfs_mirror_split(int argc, char **argv)
174 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
176 static inline int lfs_mirror_delete(int argc, char **argv)
178 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
181 /* Setstripe and migrate share mostly the same parameters */
182 #define SSM_CMD_COMMON(cmd) \
183 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
184 " [--stripe-count|-c <stripe_count>]\n" \
185 " [--overstripe-count|-C <stripe_count>]\n" \
186 " [--stripe-index|-i <start_ost_idx>]\n" \
187 " [--stripe-size|-S <stripe_size>]\n" \
188 " [--layout|-L <pattern>]\n" \
189 " [--pool|-p <pool_name>]\n" \
190 " [--ost|-o <ost_indices>]\n" \
191 " [--yaml|-y <yaml_template_file>]\n" \
192 " [--copy=<lustre_src>]\n"
194 #define SSM_HELP_COMMON \
195 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
196 "\t Using -C instead of -c allows overstriping, which\n" \
197 "\t will place more than one stripe per OST if\n" \
198 "\t stripe_count is greater than the number of OSTs\n" \
199 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
200 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
201 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
202 "\t respectively)\n" \
203 "\tpool_name: Name of OST pool to use (default none)\n" \
204 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
205 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
206 "\t Indices be specified in a format of:\n" \
207 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
209 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
210 "\t If --pool is set with --ost, then the OSTs\n" \
211 "\t must be the members of the pool.\n" \
212 "\tcomp_end: Extent end of component, start after previous end.\n"\
213 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
214 "\t respectively, -1 for EOF). Must be a multiple of\n"\
215 "\t stripe_size.\n" \
216 "\tyaml_template_file:\n" \
217 "\t YAML layout template file, can't be used with -c,\n" \
218 "\t -i, -S, -p, -o, or -E arguments.\n" \
219 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
220 "\t another lustre file or directory, can't used with\n" \
221 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
223 #define MIRROR_CREATE_HELP \
224 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
225 "\t setstripe layout options\n" \
226 "\t It defaults to 1 if not specified; if specified,\n" \
227 "\t it must follow the option without a space.\n" \
228 "\t The option can also be repeated multiple times to\n" \
229 "\t separate mirrors that have different layouts.\n" \
230 "\tsetstripe options: Mirror layout\n" \
231 "\t It can be a plain layout or a composite layout.\n" \
232 "\t If not specified, the stripe options inherited\n" \
233 "\t from the previous component will be used.\n" \
234 "\tflags: set flags to the component of the current mirror.\n" \
235 "\t Only \"prefer\" flag is supported so far.\n"
237 #define MIRROR_EXTEND_HELP \
239 "\tvictim_file: The layout of victim_file will be split and used\n" \
240 "\t as a mirror added to the mirrored file.\n" \
241 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
242 "\t from victim file(s) in case the victim file(s)\n" \
243 "\t contains the same data as the original mirrored\n" \
246 #define MIRROR_EXTEND_USAGE \
247 " <--mirror-count|-N[mirror_count]>\n" \
248 " [setstripe options|-f <victim_file>]\n" \
251 #define SETSTRIPE_USAGE \
252 SSM_CMD_COMMON("setstripe") \
253 MIRROR_EXTEND_USAGE \
254 " <directory|filename>\n" \
258 #define MIGRATE_USAGE \
259 SSM_CMD_COMMON("migrate ") \
260 " [--block|-b] [--non-block|-n]\n" \
261 " [--non-direct|-D] [--verbose|-v]\n" \
265 "\tblock: Block file access during data migration (default)\n" \
266 "\tnon-block: Abort migrations if concurrent access is detected\n" \
267 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
268 "\tverbose: Print each filename as it is migrated\n" \
270 #define SETDIRSTRIPE_USAGE \
271 " [--mdt-count|-c stripe_count>\n" \
272 " [--mdt-hash|-H mdt_hash]\n" \
273 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
274 " [--default|-D] [--mode|-o mode] <dir>\n" \
275 "\tstripe_count: stripe count of the striped directory\n" \
276 "\tmdt_index: MDT index of first stripe\n" \
277 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
278 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
279 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
280 " space create subdirectories with balanced space usage\n" \
281 "\tdefault_stripe: set default dirstripe of the directory\n" \
282 "\tmode: the file access permission of the directory (octal)\n" \
283 "To create dir with a foreign (free format) layout :\n" \
284 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
285 "[--mode|-m mode] [--flags <hex>] <dir>\n" \
286 "\tmode: the mode of the directory\n" \
287 "\tforeign_type: none or daos\n"
290 * command_t mirror_cmdlist - lfs mirror commands.
292 command_t mirror_cmdlist[] = {
293 { .pc_name = "create", .pc_func = lfs_mirror_create,
294 .pc_help = "Create a mirrored file.\n"
295 "usage: lfs mirror create "
296 "<--mirror-count|-N[mirror_count]> "
297 "[setstripe options] ... <filename|directory> ...\n"
298 MIRROR_CREATE_HELP },
299 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
300 .pc_help = "delete a mirror from a file.\n"
301 "usage: lfs mirror delete {--comp-id|-I <comp_id>|-p <pool>} <mirrored_file> ...\n"
303 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
304 .pc_help = "Extend a mirrored file.\n"
305 "usage: lfs mirror extend "
306 "<--mirror-count|-N[mirror_count]> [--no-verify] "
307 "[setstripe options|-f <victim_file>] ... <filename> ...\n"
308 MIRROR_EXTEND_HELP },
309 { .pc_name = "split", .pc_func = lfs_mirror_split,
310 .pc_help = "Split a mirrored file.\n"
311 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
312 "\t <--component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
313 "\t [-f <new_file>] <mirrored_file> ...\n"
314 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
315 "\t can be fetched by lfs getstripe command.\n"
316 "\tcomp_id: Unique component ID within a mirror.\n"
317 "\tpool: Components on specified pool.\n"
318 "\tnew_file: This option indicates the layout of the split\n"
319 "\t mirror will be stored into. If not specified,\n"
320 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
321 "\t will be used.\n" },
322 { .pc_name = "read", .pc_func = lfs_mirror_read,
323 .pc_help = "Read the content of a specified mirror of a file.\n"
324 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
325 "[--outfile|-o <output_file>] <mirrored_file>\n" },
326 { .pc_name = "write", .pc_func = lfs_mirror_write,
327 .pc_help = "Write to a specified mirror of a file.\n"
328 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
329 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
330 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
331 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
332 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
333 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
334 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
335 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
336 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
337 "<mirrored file> [<mirrored file2>...]\n"},
338 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
339 .pc_help = "Verify mirrored file(s).\n"
340 "usage: lfs mirror verify "
341 "[--only <mirror_id,mirror_id2[,...]>] "
342 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
343 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
344 .pc_help = "list commands supported by lfs mirror"},
345 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
346 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
347 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
352 * command_t pcc_cmdlist - lfs pcc commands.
354 command_t pcc_cmdlist[] = {
355 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
356 .pc_help = "Attach given files to the Persistent Client Cache.\n"
357 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
358 "\t-i: archive id for RW-PCC\n" },
359 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
360 .pc_help = "Attach given files into PCC by FID(s).\n"
361 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
363 "\t-i: archive id for RW-PCC\n"
364 "\t-m: Lustre mount point\n" },
365 { .pc_name = "state", .pc_func = lfs_pcc_state,
366 .pc_help = "Display the PCC state for given files.\n"
367 "usage: lfs pcc state <file> ...\n" },
368 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
369 .pc_help = "Detach given files from the Persistent Client Cache.\n"
370 "usage: lfs pcc detach <file> ...\n" },
371 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
372 .pc_help = "Detach given files from PCC by FID(s).\n"
373 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
374 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
375 .pc_help = "list commands supported by lfs pcc"},
376 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
377 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
378 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
382 /* all available commands */
383 command_t cmdlist[] = {
384 {"setstripe", lfs_setstripe, 0,
385 "To create a file with specified striping/composite layout, or\n"
386 "create/replace the default layout on an existing directory:\n"
387 SSM_CMD_COMMON("setstripe")
389 " <directory|filename>\n"
391 "To add component(s) to an existing composite file:\n"
392 SSM_CMD_COMMON("setstripe --component-add")
394 "To totally delete the default striping from an existing directory:\n"
395 "usage: setstripe -d <directory>\n"
397 "To create a mirrored file or set s default mirror layout on a directory:\n"
398 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
400 "To delete the last component(s) from an existing composite file\n"
401 "(note that this will also delete any data in those components):\n"
402 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
403 " [--component-flags|-F <comp_flags>]\n"
405 "\tcomp_id: Unique component ID to delete\n"
406 "\tcomp_flags: 'init' indicating all instantiated components\n"
407 "\t '^init' indicating all uninstantiated components\n"
408 "\t-I and -F cannot be specified at the same time\n"
409 "To create a file with a foreign (free format) layout:\n"
410 "usage: setstripe --foreign[=<foreign_type>]\n"
411 " --xattr|-x <layout_string> [--flags <hex>]\n"
412 " [--mode <mode>] <filename>\n"},
413 {"getstripe", lfs_getstripe, 0,
414 "To list the layout pattern for a given file or files in a\n"
415 "directory or recursively for all files in a directory tree.\n"
416 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
417 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
418 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
419 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
420 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
421 " [--component-id[=comp_id]|-I[comp_id]]\n"
422 " [--component-flags[=comp_flags]]\n"
423 " [--component-count]\n"
424 " [--extension-size|--ext-size|-z]\n"
425 " [--component-start[=[+-]comp_start]]\n"
426 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
427 " [[!] --mirror-index=[+-]<index> |\n"
428 " [!] --mirror-id=[+-]<id>]\n"
429 " <directory|filename> ..."},
430 {"setdirstripe", lfs_setdirstripe, 0,
431 "To create a striped directory on a specified MDT. This can only\n"
432 "be done on MDT0 with the right of administrator.\n"
433 "usage: setdirstripe [OPTION] <directory>\n"
435 {"getdirstripe", lfs_getdirstripe, 0,
436 "To list the layout pattern info for a given directory\n"
437 "or recursively for all directories in a directory tree.\n"
438 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
439 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
440 " [--recursive|-r] [--yaml|-y]\n"
441 " [--verbose|-v] [--default|-D] <dir> ..."},
442 {"mkdir", lfs_setdirstripe, 0,
443 "To create a striped directory on a specified MDT. This can only\n"
444 "be done on MDT0 with the right of administrator.\n"
445 "usage: mkdir [OPTION] <directory>\n"
447 {"rm_entry", lfs_rmentry, 0,
448 "To remove the name entry of the remote directory. Note: This\n"
449 "command will only delete the name entry, i.e. the remote directory\n"
450 "will become inaccessable after this command. This can only be done\n"
451 "by the administrator\n"
452 "usage: rm_entry <dir>\n"},
453 {"pool_list", lfs_poollist, 0,
454 "List pools or pool OSTs\n"
455 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
456 {"find", lfs_find, 0,
457 "find files matching given attributes recursively in directory tree.\n"
458 "usage: find <directory|filename> ...\n"
459 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
460 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
461 " [[!] --newer[XY] <reference>]\n"
462 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
463 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
464 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
465 " [[!] --stripe-count|-c [+-]<stripes>]\n"
466 " [[!] --stripe-index|-i <index,...>]\n"
467 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
468 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
469 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
470 " [[!] --projid <projid>]\n"
471 " [[!] --foreign[=<foreign_type>]]\n"
472 " [[!] --layout|-L released,raid0,mdt]\n"
473 " [[!] --foreign[=<foreign_type>]]\n"
474 " [[!] --component-count [+-]<comp_cnt>]\n"
475 " [[!] --component-start [+-]N[kMGTPE]]\n"
476 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
477 " [[!] --component-flags <comp_flags>]\n"
478 " [[!] --mirror-count|-N [+-]<n>]\n"
479 " [[!] --mirror-state <[^]state>]\n"
480 " [[!] --mdt-count|-T [+-]<stripes>]\n"
481 " [[!] --mdt-hash|-H <hashtype>\n"
482 " [[!] --mdt-index|-m <uuid|index,...>]\n"
483 "\t !: used before an option indicates 'NOT' requested attribute\n"
484 "\t -: used before a value indicates less than requested value\n"
485 "\t +: used before a value indicates more than requested value\n"
486 "\thashtype: hash type of the striped directory.\n"
487 "\t fnv_1a_64 FNV-1a hash algorithm\n"
488 "\t all_char sum of characters % MDT_COUNT\n"},
489 {"check", lfs_check, 0,
490 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
491 "or all the servers (MGTs, MDTs and OSTs).\n"
492 "usage: check <mgts|osts|mdts|all>"},
493 {"osts", lfs_osts, 0, "list OSTs connected to client "
494 "[for specified path only]\n" "usage: osts [path]"},
495 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
496 "[for specified path only]\n" "usage: mdts [path]"},
498 "report filesystem disk space usage or inodes usage "
499 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
500 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
501 {"getname", lfs_getname, 0,
502 "list instances and specified mount points [for specified path only]\n"
503 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
504 #ifdef HAVE_SYS_QUOTA_H
505 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
506 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
507 " -b <block-softlimit> -B <block-hardlimit>\n"
508 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
509 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\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 [-t] <-u|--user|-g|--group|-p|--projid>\n"
515 " [--block-grace 'notify'|<block-grace>]\n"
516 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
517 " setquota <-U|-G|-P>\n"
518 " -b <block-softlimit> -B <block-hardlimit>\n"
519 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
520 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
521 " [--block-softlimit <block-softlimit>]\n"
522 " [--block-hardlimit <block-hardlimit>]\n"
523 " [--inode-softlimit <inode-softlimit>]\n"
524 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
525 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
527 " -b can be used instead of --block-softlimit/--block-grace\n"
528 " -B can be used instead of --block-hardlimit\n"
529 " -i can be used instead of --inode-softlimit/--inode-grace\n"
530 " -I can be used instead of --inode-hardlimit\n"
531 " -d can be used instead of --default\n\n"
532 "Note: The total quota space will be split into many qunits and\n"
533 " balanced over all server targets, the minimal qunit size is\n"
534 " 1M bytes for block space and 1K inodes for inode space.\n\n"
535 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
536 " Quota space rebalancing process will stop when this mininum\n"
537 " value is reached. As a result, quota exceeded can be returned\n"
538 " while many targets still have 1MB or 1K inodes of spare\n"
540 " When setting the grace time, 'notify' can be used as grace to\n"
541 " be notified after the quota is over soft limit but prevents\n"
542 " the soft limit from becoming the hard limit."},
543 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
544 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
546 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
547 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
548 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
549 {"project", lfs_project, 0,
550 "Change or list project attribute for specified file or directory.\n"
551 "usage: project [-d|-r] <file|directory...>\n"
552 " list project ID and flags on file(s) or directories\n"
553 " project [-p id] [-s] [-r] <file|directory...>\n"
554 " set project ID and/or inherit flag for specified file(s) or directories\n"
555 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
556 " check project ID and flags on file(s) or directories, print outliers\n"
557 " project -C [-r] [-k] <file|directory...>\n"
558 " clear the project inherit flag and ID on the file or directory\n"
561 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
562 "usage: flushctx [-k] [mountpoint...]"},
563 {"changelog", lfs_changelog, 0,
564 "Show the metadata changes on an MDT."
565 "\nusage: changelog <mdtname> [startrec [endrec]]"},
566 {"changelog_clear", lfs_changelog_clear, 0,
567 "Indicate that old changelog records up to <endrec> are no longer of "
568 "interest to consumer <id>, allowing the system to free up space.\n"
569 "An <endrec> of 0 means all records.\n"
570 "usage: changelog_clear <mdtname> <id> <endrec>"},
571 {"fid2path", lfs_fid2path, 0,
572 "Resolve the full path(s) for given FID(s). For a specific hardlink "
573 "specify link number <linkno>.\n"
574 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
575 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
576 "usage: path2fid [--parents] <path> ..."},
577 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
578 "usage: rmfid <fsname|rootpath> <fid> ..."},
579 {"data_version", lfs_data_version, 0, "Display file data version for "
580 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
581 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
582 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
583 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
584 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
585 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
586 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
588 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
589 "[--archived] [--lost] <file> ..."},
590 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
591 "given files.\n" "usage: hsm_action <file> ..."},
592 {"hsm_archive", lfs_hsm_archive, 0,
593 "Archive file to external storage.\n"
594 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
596 {"hsm_restore", lfs_hsm_restore, 0,
597 "Restore file from external storage.\n"
598 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
599 {"hsm_release", lfs_hsm_release, 0,
600 "Release files from Lustre.\n"
601 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
602 {"hsm_remove", lfs_hsm_remove, 0,
603 "Remove file copy from external storage.\n"
604 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
606 " (FILE [FILE ...] | "
607 "--mntpath MOUNTPATH FID [FID ...])\n"
609 "Note: To remove an archived copy of a file already deleted from a "
611 "--mntpath option and a list of FIDs must be specified"
613 {"hsm_cancel", lfs_hsm_cancel, 0,
614 "Cancel requests related to specified files.\n"
615 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
616 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
617 "usage: swap_layouts <path1> <path2>"},
618 {"migrate", lfs_setstripe_migrate, 0,
619 "migrate a directory between MDTs.\n"
620 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
621 " [--mdt-hash|-H] <hash_type>\n"
622 " [--mdt-index|-m] <start_mdt_index>\n"
625 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
626 " it's the MDT index of first stripe\n"
627 "\tmdt_count: number of MDTs to stripe a directory over\n"
628 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
629 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
630 " all_char sum of characters % MDT_COUNT\n"
632 "migrate file objects from one OST "
633 "layout\nto another (may be not safe with concurent writes).\n"
635 "[--stripe-count|-c] <stripe_count>\n"
636 "[--overstripe-count|-C] <stripe_count>\n"
637 " [--stripe-index|-i] <start_ost_index>\n"
638 " [--stripe-size|-S] <stripe_size>\n"
639 " [--pool|-p] <pool_name>\n"
640 " [--ost|-o] <ost_indices>\n"
642 " [--non-block|-n]\n"
643 " [--non-direct|-D]\n"
644 " <file|directory>\n"
645 "\tstripe_count: number of OSTs to stripe a file over\n"
646 "\t Using -C instead of -c allows overstriping, which\n"
647 "\t will place more than one stripe per OST if\n"
648 "\t stripe_count is greater than the number of OSTs\n"
649 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
650 "\tstripe_size: number of bytes to store before moving to the next OST\n"
651 "\tpool_name: name of the predefined pool of OSTs\n"
652 "\tost_indices: OSTs to stripe over, in order\n"
653 "\tblock: Block file access during data migration (default)\n"
654 "\tnon-block: Abort migrations if concurrent access is detected\n"
655 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
657 "To move directories between MDTs. This command is deprecated, "
658 "use \"migrate\" instead.\n"
659 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
661 {"ladvise", lfs_ladvise, 0,
662 "Provide servers with advice about access patterns for a file.\n"
663 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
664 " [--background|-b] [--unset|-u]\n\n"
665 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
666 " {[--mode|-m [READ,WRITE]}\n"
668 {"mirror", lfs_mirror, mirror_cmdlist,
669 "lfs commands used to manage files with mirrored components:\n"
670 "lfs mirror create - create a mirrored file or directory\n"
671 "lfs mirror extend - add mirror(s) to an existing file\n"
672 "lfs mirror split - split a mirror from an existing mirrored file\n"
673 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
674 "lfs mirror read - read a mirror content of a mirrored file\n"
675 "lfs mirror write - write to a mirror of a mirrored file\n"
676 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
677 "lfs mirror verify - verify mirrored file(s)\n"},
678 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
679 "usage: getsom [-s] [-b] [-f] <path>\n"
680 "\t-s: Only show the size value of the SOM data for a given file\n"
681 "\t-b: Only show the blocks value of the SOM data for a given file\n"
682 "\t-f: Only show the flags value of the SOM data for a given file\n"},
683 {"heat_get", lfs_heat_get, 0,
684 "To get heat of files.\n"
685 "usage: heat_get <file> ...\n"},
686 {"heat_set", lfs_heat_set, 0,
687 "To set heat flags of files.\n"
688 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
689 "\t--clear|-c: Clear file heat for given files\n"
690 "\t--off|-o: Turn off file heat for given files\n"
691 "\t--on|-O: Turn on file heat for given files\n"},
692 {"pcc", lfs_pcc, pcc_cmdlist,
693 "lfs commands used to interact with PCC features:\n"
694 "lfs pcc attach - attach given files to Persistent Client Cache\n"
695 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
696 "lfs pcc state - display the PCC state for given files\n"
697 "lfs pcc detach - detach given files from Persistent Client Cache\n"
698 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
699 {"help", Parser_help, 0, "help"},
700 {"exit", Parser_quit, 0, "quit"},
701 {"quit", Parser_quit, 0, "quit"},
702 {"--version", Parser_version, 0,
703 "output build version of the utility and exit"},
704 {"--list-commands", lfs_list_commands, 0,
705 "list commands supported by the utility and exit"},
710 static int check_hashtype(const char *hashtype)
714 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
715 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
721 static uint32_t check_foreign_type_name(const char *foreign_type_name)
725 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
726 if (lu_foreign_types[i].lft_name == NULL)
728 if (strcmp(foreign_type_name,
729 lu_foreign_types[i].lft_name) == 0)
730 return lu_foreign_types[i].lft_type;
733 return LU_FOREIGN_TYPE_UNKNOWN;
736 static const char *error_loc = "syserror";
739 MIGRATION_NONBLOCK = 0x0001,
740 MIGRATION_MIRROR = 0x0002,
741 MIGRATION_NONDIRECT = 0x0004,
742 MIGRATION_VERBOSE = 0x0008,
745 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
746 struct llapi_layout *layout);
749 migrate_open_files(const char *name, __u64 migration_flags,
750 const struct llapi_stripe_param *param,
751 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
758 char parent[PATH_MAX];
759 char volatile_file[PATH_MAX];
765 if (param == NULL && layout == NULL) {
766 error_loc = "layout information";
770 /* search for file directory pathname */
771 if (strlen(name) > sizeof(parent) - 1) {
772 error_loc = "source file name";
776 strncpy(parent, name, sizeof(parent));
777 ptr = strrchr(parent, '/');
779 if (getcwd(parent, sizeof(parent)) == NULL) {
780 error_loc = "getcwd";
784 if (ptr == parent) /* leading '/' */
789 /* open file, direct io */
790 /* even if the file is only read, WR mode is nedeed to allow
791 * layout swap on fd */
793 if (!(migration_flags & MIGRATION_NONDIRECT))
795 fd = open(name, rflags);
798 error_loc = "cannot open source file";
802 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
804 error_loc = "cannot get MDT index";
809 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
810 mode_t open_mode = S_IRUSR | S_IWUSR;
812 random_value = random();
813 rc = snprintf(volatile_file, sizeof(volatile_file),
814 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
815 mdt_index, random_value);
816 if (rc >= sizeof(volatile_file)) {
821 /* create, open a volatile file, use caching (ie no directio) */
823 fdv = llapi_file_open_param(volatile_file, open_flags,
826 fdv = lfs_component_create(volatile_file, open_flags,
828 } while (fdv < 0 && (rc = fdv) == -EEXIST);
831 error_loc = "cannot create volatile file";
835 /* In case the MDT does not support creation of volatile files
836 * we should try to unlink it. */
837 (void)unlink(volatile_file);
839 /* Not-owner (root?) special case.
840 * Need to set owner/group of volatile file like original.
841 * This will allow to pass related check during layout_swap.
846 error_loc = "cannot stat source file";
850 rc = fstat(fdv, &stv);
853 error_loc = "cannot stat volatile";
857 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
858 rc = fchown(fdv, st.st_uid, st.st_gid);
861 error_loc = "cannot change ownwership of volatile";
880 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
882 struct llapi_layout *layout;
883 size_t buf_size = 4 * 1024 * 1024;
892 layout = llapi_layout_get_by_fd(fd_src, 0);
893 if (layout != NULL) {
894 uint64_t stripe_size;
896 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
898 buf_size = stripe_size;
900 llapi_layout_free(layout);
903 /* Use a page-aligned buffer for direct I/O */
904 rc = posix_memalign(&buf, getpagesize(), buf_size);
909 /* read new data only if we have written all
910 * previously read data */
913 rc = check_file(fd_src);
918 rsize = read(fd_src, buf, buf_size);
930 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
949 static int migrate_copy_timestamps(int fd, int fdv)
953 if (fstat(fd, &st) == 0) {
954 struct timeval tv[2] = {
955 {.tv_sec = st.st_atime},
956 {.tv_sec = st.st_mtime}
959 return futimes(fdv, tv);
965 static int migrate_block(int fd, int fdv)
972 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
974 error_loc = "cannot get dataversion";
982 /* The grouplock blocks all concurrent accesses to the file.
983 * It has to be taken after llapi_get_data_version as it would
985 rc = llapi_group_lock(fd, gid);
987 error_loc = "cannot get group lock";
991 rc = migrate_copy_data(fd, fdv, NULL);
993 error_loc = "data copy failed";
997 /* Make sure we keep original atime/mtime values */
998 rc = migrate_copy_timestamps(fd, fdv);
1000 error_loc = "timestamp copy failed";
1005 * for a migration we need to check data version on file did
1008 * Pass in gid=0 since we already own grouplock. */
1009 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1010 SWAP_LAYOUTS_CHECK_DV1);
1011 if (rc == -EAGAIN) {
1012 error_loc = "file changed";
1014 } else if (rc < 0) {
1015 error_loc = "cannot swap layout";
1020 rc2 = llapi_group_unlock(fd, gid);
1021 if (rc2 < 0 && rc == 0) {
1022 error_loc = "unlock group lock";
1030 * Internal helper for migrate_copy_data(). Check lease and report error if
1033 * \param[in] fd File descriptor on which to check the lease.
1035 * \retval 0 Migration can keep on going.
1036 * \retval -errno Error occurred, abort migration.
1038 static int check_lease(int fd)
1042 rc = llapi_lease_check(fd);
1044 return 0; /* llapi_check_lease returns > 0 on success. */
1049 static int migrate_nonblock(int fd, int fdv)
1055 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1057 error_loc = "cannot get data version";
1061 rc = migrate_copy_data(fd, fdv, check_lease);
1063 error_loc = "data copy failed";
1067 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1069 error_loc = "cannot get data version";
1075 error_loc = "source file changed";
1079 /* Make sure we keep original atime/mtime values */
1080 rc = migrate_copy_timestamps(fd, fdv);
1082 error_loc = "timestamp copy failed";
1089 static int lfs_component_set(char *fname, int comp_id,
1090 __u32 flags, __u32 neg_flags)
1093 __u32 flags_array[2];
1098 ids[count] = comp_id;
1099 flags_array[count] = flags;
1104 if (neg_flags & LCME_FL_STALE) {
1105 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1106 "component. Please use lfs-mirror-resync(1) "
1107 "instead\n", progname);
1111 ids[count] = comp_id;
1112 flags_array[count] = neg_flags | LCME_FL_NEG;
1116 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1118 if (errno == EUCLEAN) {
1121 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1122 progname, comp_id, fname);
1125 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1126 progname, comp_id, fname, flags, neg_flags);
1133 static int lfs_component_del(char *fname, __u32 comp_id,
1134 __u32 flags, __u32 neg_flags)
1138 if (flags && neg_flags)
1141 if (!flags && neg_flags)
1142 flags = neg_flags | LCME_FL_NEG;
1144 if ((flags && comp_id) || (!flags && !comp_id))
1148 if (flags & ~LCME_KNOWN_FLAGS) {
1150 "%s setstripe: unknown flags %#x\n",
1154 } else if (comp_id > LCME_ID_MAX) {
1155 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1160 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1163 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1164 progname, comp_id, fname, strerror(errno));
1168 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1175 rc = llapi_layout_file_comp_add(fname, layout);
1177 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1178 fname, strerror(errno));
1182 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1183 struct llapi_layout *layout)
1191 fd = lstat(fname, &st);
1192 if (fd == 0 && S_ISDIR(st.st_mode))
1193 open_flags = O_DIRECTORY | O_RDONLY;
1195 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1197 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1198 S_ISDIR(st.st_mode) ?
1199 "set default composite layout for" :
1200 "create composite file",
1201 fname, strerror(errno));
1205 static int lfs_migrate(char *name, __u64 migration_flags,
1206 struct llapi_stripe_param *param,
1207 struct llapi_layout *layout)
1209 struct llapi_layout *existing;
1210 uint64_t dom_new, dom_cur;
1215 rc = migrate_open_files(name, migration_flags, param, layout,
1220 rc = llapi_layout_dom_size(layout, &dom_new);
1222 error_loc = "cannot get new layout DoM size";
1225 /* special case for migration to DOM layout*/
1226 existing = llapi_layout_get_by_fd(fd, 0);
1228 error_loc = "cannot get existing layout";
1232 rc = llapi_layout_dom_size(existing, &dom_cur);
1234 error_loc = "cannot get current layout DoM size";
1238 /* if file has DoM layout already then migration is possible to
1239 * the new layout with the same DoM component via swap layout,
1240 * if new layout used bigger DOM size, then mirroring is used
1242 if (dom_new > dom_cur) {
1243 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1246 error_loc = "cannot migrate to DOM layout";
1250 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1251 /* Blocking mode (forced if servers do not support file lease).
1252 * It is also the default mode, since we cannot distinguish
1253 * between a broken lease and a server that does not support
1254 * atomic swap/close (LU-6785) */
1255 rc = migrate_block(fd, fdv);
1259 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1261 error_loc = "cannot get lease";
1265 rc = migrate_nonblock(fd, fdv);
1267 llapi_lease_release(fd);
1271 /* Atomically put lease, swap layouts and close.
1272 * for a migration we need to check data version on file did
1274 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1276 error_loc = "cannot swap layout";
1288 fprintf(stderr, "error: %s: %s: %s: %s\n",
1289 progname, name, error_loc, strerror(-rc));
1290 else if (migration_flags & MIGRATION_VERBOSE)
1291 printf("%s\n", name);
1296 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1305 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1309 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1310 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1311 const char *comp_name = comp_flags_table[i].cfn_name;
1313 if (strcmp(name, comp_name) == 0) {
1314 *flags |= comp_flag;
1316 } else if (strncmp(name, "^", 1) == 0 &&
1317 strcmp(name + 1, comp_name) == 0) {
1318 *neg_flags |= comp_flag;
1323 llapi_printf(LLAPI_MSG_ERROR,
1324 "%s: component flag '%s' not supported\n",
1330 if (!*flags && !*neg_flags)
1333 /* don't allow to set and exclude the same flag */
1334 if (*flags & *neg_flags)
1340 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1348 if (strncmp(string, "^", 1) == 0) {
1349 *neg_state = llapi_layout_string_flags(string + 1);
1350 if (*neg_state != 0)
1353 *state = llapi_layout_string_flags(string);
1358 llapi_printf(LLAPI_MSG_ERROR,
1359 "%s: mirrored file state '%s' not supported\n",
1365 * struct mirror_args - Command-line arguments for mirror(s).
1366 * @m_count: Number of mirrors to be created with this layout.
1367 * @m_flags: Mirror level flags, only 'prefer' is supported.
1368 * @m_layout: Mirror layout.
1369 * @m_file: A victim file. Its layout will be split and used as a mirror.
1370 * @m_next: Point to the next node of the list.
1372 * Command-line arguments for mirror(s) will be parsed and stored in
1373 * a linked list that consists of this structure.
1375 struct mirror_args {
1378 struct llapi_layout *m_layout;
1380 struct mirror_args *m_next;
1384 * enum mirror_flags - Flags for extending a mirrored file.
1385 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1386 * in case the victim file(s) contains the same data as the
1387 * original mirrored file.
1388 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1389 * @MF_COMP_ID: specified component id instead of mirror id
1391 * Flags for extending a mirrored file.
1401 * mirror_create_sanity_check() - Check mirror list.
1402 * @list: A linked list that stores the mirror arguments.
1404 * This function does a sanity check on @list for creating
1407 * Return: 0 on success or a negative error code on failure.
1409 static int mirror_create_sanity_check(const char *fname,
1410 struct mirror_args *list)
1413 bool has_m_file = false;
1414 bool has_m_layout = false;
1420 struct llapi_layout *layout;
1422 layout = llapi_layout_get_by_path(fname, 0);
1425 "error: %s: file '%s' couldn't get layout\n",
1430 rc = llapi_layout_sanity(layout, false, true);
1432 llapi_layout_free(layout);
1435 llapi_layout_sanity_perror(rc);
1440 while (list != NULL) {
1441 if (list->m_file != NULL) {
1443 llapi_layout_free(list->m_layout);
1446 llapi_layout_get_by_path(list->m_file, 0);
1447 if (list->m_layout == NULL) {
1449 "error: %s: file '%s' has no layout\n",
1450 progname, list->m_file);
1454 has_m_layout = true;
1455 if (list->m_layout == NULL) {
1456 fprintf(stderr, "error: %s: no mirror layout\n",
1462 rc = llapi_layout_sanity(list->m_layout, false, true);
1464 llapi_layout_sanity_perror(rc);
1468 list = list->m_next;
1471 if (has_m_file && has_m_layout) {
1473 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1481 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1483 __u32 mirror_flags = *(__u32 *)cbdata;
1487 rc = llapi_layout_comp_flags_get(layout, &flags);
1492 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1497 return LLAPI_LAYOUT_ITER_CONT;
1501 * mirror_create() - Create a mirrored file.
1502 * @fname: The file to be created.
1503 * @mirror_list: A linked list that stores the mirror arguments.
1505 * This function creates a mirrored file @fname with the mirror(s)
1506 * from @mirror_list.
1508 * Return: 0 on success or a negative error code on failure.
1510 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1512 struct llapi_layout *layout = NULL;
1513 struct mirror_args *cur_mirror = NULL;
1514 uint16_t mirror_count = 0;
1518 rc = mirror_create_sanity_check(NULL, mirror_list);
1522 cur_mirror = mirror_list;
1523 while (cur_mirror != NULL) {
1524 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1526 &cur_mirror->m_flags);
1529 fprintf(stderr, "%s: failed to set mirror flags\n",
1534 for (i = 0; i < cur_mirror->m_count; i++) {
1535 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1538 fprintf(stderr, "error: %s: "
1539 "merge layout failed: %s\n",
1540 progname, strerror(errno));
1544 mirror_count += cur_mirror->m_count;
1545 cur_mirror = cur_mirror->m_next;
1548 if (layout == NULL) {
1549 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1553 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1556 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1557 progname, strerror(errno));
1561 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1569 llapi_layout_free(layout);
1574 * Compare files and check lease on @fd.
1576 * \retval bytes number of bytes are the same
1578 static ssize_t mirror_file_compare(int fd, int fdv)
1580 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1582 ssize_t bytes_done = 0;
1583 ssize_t bytes_read = 0;
1585 buf = malloc(buflen * 2);
1590 if (!llapi_lease_check(fd)) {
1591 bytes_done = -EBUSY;
1595 bytes_read = read(fd, buf, buflen);
1596 if (bytes_read <= 0)
1599 if (bytes_read != read(fdv, buf + buflen, buflen))
1602 /* XXX: should compute the checksum on each buffer and then
1603 * compare checksum to avoid cache collision */
1604 if (memcmp(buf, buf + buflen, bytes_read))
1607 bytes_done += bytes_read;
1615 static int mirror_extend_file(const char *fname, const char *victim_file,
1616 enum mirror_flags mirror_flags)
1621 struct stat stbuf_v;
1622 struct ll_ioc_lease *data = NULL;
1625 fd = open(fname, O_RDWR);
1627 error_loc = "open source file";
1632 fdv = open(victim_file, O_RDWR);
1634 error_loc = "open target file";
1639 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1640 error_loc = "stat source or target file";
1645 if (stbuf.st_dev != stbuf_v.st_dev) {
1646 error_loc = "stat source and target file";
1651 /* mirrors should be of the same size */
1652 if (stbuf.st_size != stbuf_v.st_size) {
1653 error_loc = "file sizes don't match";
1658 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1660 error_loc = "cannot get lease";
1664 if (!(mirror_flags & MF_NO_VERIFY)) {
1666 /* mirrors should have the same contents */
1667 ret = mirror_file_compare(fd, fdv);
1668 if (ret != stbuf.st_size) {
1669 error_loc = "file busy or contents don't match";
1670 rc = ret < 0 ? ret : -EINVAL;
1675 /* Get rid of caching pages from clients */
1676 rc = llapi_file_flush(fd);
1678 error_loc = "cannot get data version";
1682 rc = llapi_file_flush(fdv);
1684 error_loc = "cannot get data version";
1689 /* Make sure we keep original atime/mtime values */
1690 rc = migrate_copy_timestamps(fd, fdv);
1692 error_loc = "cannot copy timestamp";
1696 /* Atomically put lease, merge layouts and close. */
1697 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1699 error_loc = "memory allocation";
1702 data->lil_mode = LL_LEASE_UNLCK;
1703 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1704 data->lil_count = 1;
1705 data->lil_ids[0] = fdv;
1706 rc = llapi_lease_set(fd, data);
1708 error_loc = "cannot merge layout";
1710 } else if (rc == 0) {
1712 error_loc = "lost lease lock";
1725 (void) unlink(victim_file);
1727 fprintf(stderr, "error: %s: %s: %s: %s\n",
1728 progname, fname, error_loc, strerror(-rc));
1732 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1734 struct ll_ioc_lease *data = NULL;
1739 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1743 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1745 error_loc = "cannot get lease";
1749 rc = migrate_nonblock(fd, fdv);
1751 llapi_lease_release(fd);
1755 /* Atomically put lease, merge layouts and close. */
1756 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1758 error_loc = "memory allocation";
1761 data->lil_mode = LL_LEASE_UNLCK;
1762 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1763 data->lil_count = 1;
1764 data->lil_ids[0] = fdv;
1765 rc = llapi_lease_set(fd, data);
1767 error_loc = "cannot merge layout";
1769 } else if (rc == 0) {
1771 error_loc = "lost lease lock";
1784 fprintf(stderr, "error: %s: %s: %s: %s\n",
1785 progname, name, error_loc, strerror(-rc));
1789 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1790 enum mirror_flags mirror_flags)
1794 rc = mirror_create_sanity_check(fname, mirror_list);
1798 while (mirror_list) {
1799 if (mirror_list->m_file != NULL) {
1800 rc = mirror_extend_file(fname, mirror_list->m_file,
1803 __u32 mirror_count = mirror_list->m_count;
1805 while (mirror_count > 0) {
1806 rc = mirror_extend_layout(fname,
1807 mirror_list->m_layout);
1817 mirror_list = mirror_list->m_next;
1823 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1828 rc = llapi_layout_mirror_id_get(layout, &id);
1832 if ((__u16)id == *(__u16 *)cbdata)
1833 return LLAPI_LAYOUT_ITER_STOP;
1835 return LLAPI_LAYOUT_ITER_CONT;
1838 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1843 rc = llapi_layout_comp_id_get(layout, &id);
1847 if (id == *(__u32 *)cbdata)
1848 return LLAPI_LAYOUT_ITER_STOP;
1850 return LLAPI_LAYOUT_ITER_CONT;
1853 struct pool_to_id_cbdata {
1857 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1859 char buf[LOV_MAXPOOLNAME + 1];
1860 struct pool_to_id_cbdata *d = (void *)cbdata;
1864 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1867 if (strcmp(d->pool, buf))
1868 return LLAPI_LAYOUT_ITER_CONT;
1870 rc = llapi_layout_mirror_id_get(layout, &id);
1875 return LLAPI_LAYOUT_ITER_STOP;
1878 struct collect_ids_data {
1884 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1886 struct collect_ids_data *cid = cbdata;
1890 rc = llapi_layout_mirror_id_get(layout, &id);
1894 if ((__u16)id != cid->cid_exclude) {
1897 for (i = 0; i < cid->cid_count; i++) {
1898 /* already collected the mirror id */
1899 if (id == cid->cid_ids[i])
1900 return LLAPI_LAYOUT_ITER_CONT;
1902 cid->cid_ids[cid->cid_count] = id;
1906 return LLAPI_LAYOUT_ITER_CONT;
1910 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1911 * @mirror_id: Mirror id to be checked.
1912 * @layout: Mirror component list.
1914 * This function checks if a mirror with specified @mirror_id is the last
1915 * non-stale mirror of a layout @layout.
1917 * Return: true or false.
1920 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1922 __u16 mirror_ids[128] = { 0 };
1923 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1925 .cid_exclude = mirror_id, };
1928 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1930 for (i = 0; i < cid.cid_count; i++) {
1931 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1934 comp_size = llapi_mirror_find_stale(layout, comp_array,
1935 ARRAY_SIZE(comp_array),
1944 static int mirror_split(const char *fname, __u32 id, const char *pool,
1945 enum mirror_flags mflags, const char *victim_file)
1947 struct llapi_layout *layout;
1948 char parent[PATH_MAX];
1949 char victim[PATH_MAX];
1950 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1952 struct ll_ioc_lease *data;
1953 uint16_t mirror_count;
1958 /* check fname contains mirror with mirror_id/comp_id */
1959 layout = llapi_layout_get_by_path(fname, 0);
1962 "error %s: file '%s' couldn't get layout\n",
1967 rc = llapi_layout_sanity(layout, false, true);
1969 llapi_layout_sanity_perror(rc);
1973 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1976 "error %s: file '%s' couldn't get mirror count\n",
1980 if (mirror_count < 2) {
1982 "error %s: file '%s' has %d component, cannot split\n",
1983 progname, fname, mirror_count);
1987 if (mflags & MF_COMP_POOL) {
1988 struct pool_to_id_cbdata data = { .pool = pool };
1990 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
1993 } else if (mflags & MF_COMP_ID) {
1994 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1995 id = mirror_id_of(id);
1997 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2000 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2003 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2005 "error %s: file '%s' does not contain mirror with id %u\n",
2006 progname, fname, id);
2010 fd = open(fname, O_RDWR);
2013 "error %s: open file '%s' failed: %s\n",
2014 progname, fname, strerror(errno));
2018 /* get victim file directory pathname */
2019 if (strlen(fname) > sizeof(parent) - 1) {
2020 fprintf(stderr, "error %s: file name of '%s' too long\n",
2025 strncpy(parent, fname, sizeof(parent));
2026 ptr = strrchr(parent, '/');
2028 if (getcwd(parent, sizeof(parent)) == NULL) {
2029 fprintf(stderr, "error %s: getcwd failed: %s\n",
2030 progname, strerror(errno));
2040 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2042 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2047 if (victim_file == NULL) {
2048 /* use a temp file to store the splitted layout */
2049 if (mflags & MF_DESTROY) {
2050 if (last_non_stale_mirror(id, layout)) {
2053 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2058 fdv = llapi_create_volatile_idx(parent, mdt_index,
2059 O_LOV_DELAY_CREATE);
2061 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2063 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2066 /* user specified victim file */
2067 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2072 "error %s: create victim file failed: %s\n",
2073 progname, strerror(errno));
2077 /* get lease lock of fname */
2078 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2081 "error %s: cannot get lease of file '%s': %d\n",
2082 progname, fname, rc);
2086 /* Atomatically put lease, split layouts and close. */
2087 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2093 data->lil_mode = LL_LEASE_UNLCK;
2094 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2095 data->lil_count = 2;
2096 data->lil_ids[0] = fdv;
2097 data->lil_ids[1] = id;
2098 rc = llapi_lease_set(fd, data);
2100 if (rc == 0) /* lost lease lock */
2103 "error %s: cannot split '%s': %s\n",
2104 progname, fname, strerror(-rc));
2115 llapi_layout_free(layout);
2120 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2121 __u16 *mirror_ids, int ids_nr);
2123 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2124 __u64 migration_flags,
2125 struct llapi_stripe_param *param,
2126 struct llapi_layout *layout)
2128 struct ll_ioc_lease *data = NULL;
2131 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2133 error_loc = "cannot get lease";
2137 /* Atomically put lease, merge layouts, resync and close. */
2138 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2140 error_loc = "memory allocation";
2143 data->lil_mode = LL_LEASE_UNLCK;
2144 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2145 data->lil_count = 1;
2146 data->lil_ids[0] = fdv;
2147 rc = llapi_lease_set(fd, data);
2149 error_loc = "cannot merge layout";
2151 } else if (rc == 0) {
2153 error_loc = "lost lease lock";
2159 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2161 error_loc = "cannot resync file";
2165 /* delete first mirror now */
2166 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2168 error_loc = "cannot delete old layout";
2176 fprintf(stderr, "error: %s: %s: %s: %s\n",
2177 progname, name, error_loc, strerror(-rc));
2178 else if (migration_flags & MIGRATION_VERBOSE)
2179 printf("%s\n", name);
2186 * Parse a string containing an target index list into an array of integers.
2188 * The input string contains a comma delimited list of individual
2189 * indices and ranges, for example "1,2-4,7". Add the indices into the
2190 * \a tgts array and remove duplicates.
2192 * \param[out] tgts array to store indices in
2193 * \param[in] size size of \a tgts array
2194 * \param[in] offset starting index in \a tgts
2195 * \param[in] arg string containing OST index list
2196 * \param[in/out] overstriping index list may contain duplicates
2198 * \retval positive number of indices in \a tgts
2199 * \retval -EINVAL unable to parse \a arg
2201 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2202 unsigned long long *pattern)
2206 int slots = size - offset;
2208 bool overstriped = false;
2214 end_of_loop = false;
2215 while (!end_of_loop) {
2216 int start_index = 0;
2219 char *endptr = NULL;
2223 ptr = strchrnul(arg, ',');
2225 end_of_loop = *ptr == '\0';
2228 start_index = strtol(arg, &endptr, 0);
2229 if (endptr == arg) /* no data at all */
2231 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2234 end_index = start_index;
2235 if (*endptr == '-') {
2236 end_index = strtol(endptr + 1, &endptr, 0);
2237 if (*endptr != '\0')
2239 if (end_index < start_index)
2243 for (i = start_index; i <= end_index && slots > 0; i++) {
2246 /* remove duplicate */
2247 for (j = 0; j < offset; j++) {
2248 if (tgts[j] == i && pattern &&
2249 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2251 else if (tgts[j] == i)
2257 if (j == offset) { /* check complete */
2263 if (slots == 0 && i < end_index)
2271 if (!end_of_loop && ptr != NULL)
2274 if (!overstriped && pattern)
2275 *pattern = LLAPI_LAYOUT_DEFAULT;
2277 return rc < 0 ? rc : nr;
2280 struct lfs_setstripe_args {
2281 unsigned long long lsa_comp_end;
2282 unsigned long long lsa_stripe_size;
2283 unsigned long long lsa_extension_size;
2284 long long lsa_stripe_count;
2285 long long lsa_stripe_off;
2286 __u32 lsa_comp_flags;
2287 __u32 lsa_comp_neg_flags;
2288 unsigned long long lsa_pattern;
2289 unsigned int lsa_mirror_count;
2291 bool lsa_first_comp;
2292 bool lsa_extension_comp;
2294 char *lsa_pool_name;
2297 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2299 unsigned int mirror_count = lsa->lsa_mirror_count;
2300 bool first_comp = lsa->lsa_first_comp;
2302 memset(lsa, 0, sizeof(*lsa));
2304 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2305 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2306 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2307 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2308 lsa->lsa_pool_name = NULL;
2310 lsa->lsa_mirror_count = mirror_count;
2311 lsa->lsa_first_comp = first_comp;
2315 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2316 * @lsa: Stripe options to be initialized and inherited.
2318 * This function initializes stripe options in @lsa and inherit
2319 * stripe_size, stripe_count and OST pool_name options.
2323 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2325 unsigned long long stripe_size;
2326 long long stripe_count;
2327 char *pool_name = NULL;
2329 stripe_size = lsa->lsa_stripe_size;
2330 stripe_count = lsa->lsa_stripe_count;
2331 pool_name = lsa->lsa_pool_name;
2333 setstripe_args_init(lsa);
2335 lsa->lsa_stripe_size = stripe_size;
2336 lsa->lsa_stripe_count = stripe_count;
2337 lsa->lsa_pool_name = pool_name;
2340 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2342 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2343 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2344 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2345 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2346 lsa->lsa_comp_end != 0);
2350 * comp_args_to_layout() - Create or extend a composite layout.
2351 * @composite: Pointer to the composite layout.
2352 * @lsa: Stripe options for the new component.
2354 * This function creates or extends a composite layout by adding a new
2355 * component with stripe options from @lsa.
2357 * Return: 0 on success or an error code on failure.
2359 static int comp_args_to_layout(struct llapi_layout **composite,
2360 struct lfs_setstripe_args *lsa,
2363 struct llapi_layout *layout = *composite;
2364 uint64_t prev_end = 0;
2369 if (layout == NULL) {
2370 layout = llapi_layout_alloc();
2371 if (layout == NULL) {
2372 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2377 *composite = layout;
2378 lsa->lsa_first_comp = true;
2382 /* Get current component extent, current component
2383 * must be the tail component. */
2384 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2386 fprintf(stderr, "Get comp extent failed. %s\n",
2391 if (lsa->lsa_first_comp)
2394 if (lsa->lsa_first_comp)
2395 rc = llapi_layout_add_first_comp(layout);
2397 rc = llapi_layout_comp_add(layout);
2399 fprintf(stderr, "Add component failed. %s\n",
2405 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2407 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2408 lsa->lsa_comp_flags, strerror(errno));
2413 uint64_t comp_end = lsa->lsa_comp_end;
2415 /* The extendable component is 0-length, so it can be removed
2416 * if there is insufficient space to extend it. */
2417 if (lsa->lsa_extension_comp)
2418 comp_end = prev_end;
2420 rc = llapi_layout_comp_extent_set(layout, prev_end,
2423 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2424 prev_end, comp_end, strerror(errno));
2428 /* reset lsa_first_comp */
2429 lsa->lsa_first_comp = false;
2431 /* Data-on-MDT component setting */
2432 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2433 /* In case of Data-on-MDT patterns the only extra option
2434 * applicable is stripe size option. */
2435 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2436 fprintf(stderr, "Option 'stripe-count' can't be "
2437 "specified with Data-on-MDT component: %lld\n",
2438 lsa->lsa_stripe_count);
2442 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2443 fprintf(stderr, "Option 'stripe-size' can't be "
2444 "specified with Data-on-MDT component: %llu\n",
2445 lsa->lsa_stripe_size);
2449 if (lsa->lsa_nr_tgts != 0) {
2450 fprintf(stderr, "Option 'ost-list' can't be specified "
2451 "with Data-on-MDT component: '%i'\n",
2456 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2457 fprintf(stderr, "Option 'stripe-offset' can't be "
2458 "specified with Data-on-MDT component: %lld\n",
2459 lsa->lsa_stripe_off);
2463 if (lsa->lsa_pool_name != 0) {
2464 fprintf(stderr, "Option 'pool' can't be specified "
2465 "with Data-on-MDT component: '%s'\n",
2466 lsa->lsa_pool_name);
2471 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2473 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2478 /* Data-on-MDT component has always single stripe up to end */
2479 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2480 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2481 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2483 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2490 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2491 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2493 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2494 rc = llapi_layout_extension_size_set(layout, size);
2496 rc = llapi_layout_stripe_size_set(layout, size);
2499 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2500 size, strerror(errno));
2504 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2506 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2507 lsa->lsa_stripe_count, strerror(errno));
2511 if (lsa->lsa_pool_name != NULL) {
2512 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2514 fprintf(stderr, "Set pool name: %s failed. %s\n",
2515 lsa->lsa_pool_name, strerror(errno));
2519 rc = llapi_layout_pool_name_set(layout, "");
2521 fprintf(stderr, "Clear pool name failed: %s\n",
2527 if (lsa->lsa_nr_tgts > 0) {
2528 if (lsa->lsa_stripe_count > 0 &&
2529 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2530 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2531 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2532 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2533 lsa->lsa_stripe_count,
2538 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2539 rc = llapi_layout_ost_index_set(layout, i,
2544 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2545 lsa->lsa_stripe_off != -1) {
2546 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2549 fprintf(stderr, "Set ost index %d failed. %s\n",
2550 i, strerror(errno));
2554 /* Create the second, virtual component of extension space */
2555 if (lsa->lsa_extension_comp) {
2556 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2557 lsa->lsa_extension_comp = false;
2564 static int build_component(struct llapi_layout **layout,
2565 struct lfs_setstripe_args *lsa, bool set_extent)
2569 rc = comp_args_to_layout(layout, lsa, set_extent);
2573 if (lsa->lsa_mirror_count > 0) {
2574 rc = llapi_layout_mirror_count_set(*layout,
2575 lsa->lsa_mirror_count);
2579 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2582 lsa->lsa_mirror_count = 0;
2588 static int build_layout_from_yaml_node(struct cYAML *node,
2589 struct llapi_layout **layout,
2590 struct lfs_setstripe_args *lsa,
2597 if (node->cy_type == CYAML_TYPE_OBJECT) {
2598 /* go deep to sub blocks */
2599 rc = build_layout_from_yaml_node(node->cy_child, layout,
2604 if (node->cy_string == NULL)
2607 string = node->cy_string;
2608 /* skip leading lmm_ if present, to simplify parsing */
2609 if (strncmp(string, "lmm_", 4) == 0)
2612 if (node->cy_type == CYAML_TYPE_STRING) {
2613 if (!strcmp(string, "lcme_extent.e_end")) {
2614 if (!strcmp(node->cy_valuestring, "EOF") ||
2615 !strcmp(node->cy_valuestring, "eof"))
2616 lsa->lsa_comp_end = LUSTRE_EOF;
2617 } else if (!strcmp(string, "pool")) {
2618 lsa->lsa_pool_name = node->cy_valuestring;
2619 } else if (!strcmp(string, "pattern")) {
2620 if (!strcmp(node->cy_valuestring, "mdt"))
2621 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2622 if (!strcmp(node->cy_valuestring,
2623 "raid0,overstriped"))
2625 LLAPI_LAYOUT_OVERSTRIPING;
2626 } else if (!strcmp(string, "lcme_flags")) {
2627 rc = comp_str2flags(node->cy_valuestring,
2628 &lsa->lsa_comp_flags,
2629 &lsa->lsa_comp_neg_flags);
2632 /* Only template flags have meaning in
2633 * the layout for a new file
2635 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2637 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2638 if (!strcmp(string, "lcm_mirror_count")) {
2639 lsa->lsa_mirror_count = node->cy_valueint;
2640 } else if (!strcmp(string, "lcme_extent.e_start")) {
2641 if (node->cy_valueint != 0 || *layout != NULL) {
2642 rc = build_component(layout, lsa, true);
2647 if (node->cy_valueint == 0)
2648 lsa->lsa_first_comp = true;
2650 /* initialize lsa */
2651 setstripe_args_init(lsa);
2652 lsa->lsa_tgts = osts;
2653 } else if (!strcmp(string, "lcme_extent.e_end")) {
2654 if (node->cy_valueint == -1)
2655 lsa->lsa_comp_end = LUSTRE_EOF;
2657 lsa->lsa_comp_end = node->cy_valueint;
2658 } else if (!strcmp(string, "stripe_count")) {
2659 lsa->lsa_stripe_count = node->cy_valueint;
2660 } else if (!strcmp(string, "stripe_size")) {
2661 lsa->lsa_stripe_size = node->cy_valueint;
2662 } else if (!strcmp(string, "stripe_offset")) {
2663 lsa->lsa_stripe_off = node->cy_valueint;
2664 } else if (!strcmp(string, "l_ost_idx")) {
2665 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2670 node = node->cy_next;
2676 static int lfs_comp_create_from_yaml(char *template,
2677 struct llapi_layout **layout,
2678 struct lfs_setstripe_args *lsa,
2681 struct cYAML *tree = NULL, *err_rc = NULL;
2684 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2686 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2687 progname, template);
2688 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2689 "can't parse", &err_rc);
2690 cYAML_print_tree2file(stderr, err_rc);
2691 cYAML_free_tree(err_rc);
2696 /* initialize lsa for plain file */
2697 setstripe_args_init(lsa);
2698 lsa->lsa_tgts = osts;
2700 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2702 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2703 progname, template);
2706 rc = build_component(layout, lsa, *layout != NULL);
2708 /* clean clean lsa */
2709 setstripe_args_init(lsa);
2713 cYAML_free_tree(tree);
2718 * Get the extension size from the next (SEL) component and extend the
2719 * current component on it. The start of the next component is to be
2722 * \param[in] layout the current layout
2723 * \param[in] start the start of the current component
2724 * \param[in,out] end the end of the current component
2725 * \param[in] offset the offset to adjust the end position to instead of
2728 * \retval 0 - extended successfully
2729 * \retval < 0 - error
2731 static int layout_extend_comp(struct llapi_layout *layout,
2732 uint64_t start, uint64_t *end,
2735 uint64_t size, next_start, next_end;
2738 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2740 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2741 "%s\n", progname, strerror(errno));
2745 /* Even if the @size will not be used below, this will fail if
2746 * this is not a SEL component - a good confirmation we are
2747 * working on right components. */
2748 rc = llapi_layout_extension_size_get(layout, &size);
2750 fprintf(stderr, "%s setstripe: cannot get component ext size: "
2751 "%s\n", progname, strerror(errno));
2755 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2757 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2758 progname, strerror(errno));
2762 next_start += offset ?: size;
2763 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2765 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2766 progname, strerror(errno));
2770 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2772 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2773 "%s\n", progname, strerror(errno));
2777 *end += offset ?: size;
2778 rc = llapi_layout_comp_extent_set(layout, start, *end);
2780 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2781 progname, strerror(errno));
2789 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2790 * end of the last component in the existing file, and adjust the
2791 * first extent start of the components to be added accordingly.
2793 * In the create mode, we need to check if the first component is an extendable
2794 * SEL component and extend its length to the extension size (first component
2795 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2797 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2800 struct llapi_layout *head;
2801 uint64_t start = 0, prev_end = 0;
2810 head = llapi_layout_get_by_path(fname, 0);
2813 "%s setstripe: cannot read layout from '%s': "
2814 "%s\n", progname, fname, strerror(errno));
2816 } else if (errno == ENODATA) {
2817 /* file without LOVEA, this component-add will be turned
2818 * into a component-create. */
2819 llapi_layout_free(head);
2822 /* the new layout will be added to an empty one, it
2823 * still needs to be adjusted below */
2826 } else if (!llapi_layout_is_composite(head)) {
2827 fprintf(stderr, "%s setstripe: '%s' not a composite "
2828 "file\n", progname, fname);
2829 llapi_layout_free(head);
2833 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2835 fprintf(stderr, "%s setstripe: cannot get prev "
2836 "extent: %s\n", progname, strerror(errno));
2837 llapi_layout_free(head);
2841 llapi_layout_free(head);
2845 /* Make sure we use the first component of the layout to be added. */
2846 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2849 "%s setstripe: cannot move component cursor: %s\n",
2850 progname, strerror(errno));
2854 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2856 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2857 progname, strerror(errno));
2861 if (start == 0 && end == 0) {
2862 rc = layout_extend_comp(layout, start, &end,
2863 comp_add ? prev_end : 0);
2868 if (start > prev_end || end < prev_end) {
2869 fprintf(stderr, "%s setstripe: first extent [%lu, %lu) not "
2870 "adjacent with extent end %lu\n",
2871 progname, start, end, prev_end);
2875 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2877 fprintf(stderr, "%s setstripe: cannot set component extent "
2879 progname, prev_end, end, strerror(errno));
2886 static int mirror_adjust_first_extents(struct mirror_args *list)
2893 while (list != NULL) {
2894 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
2897 list = list->m_next;
2903 static inline bool arg_is_eof(char *arg)
2905 return !strncmp(arg, "-1", strlen("-1")) ||
2906 !strncmp(arg, "EOF", strlen("EOF")) ||
2907 !strncmp(arg, "eof", strlen("eof"));
2911 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2913 * Return: Valid mirror_args pointer on success and
2914 * NULL if memory allocation fails.
2916 static struct mirror_args *lfs_mirror_alloc(void)
2918 struct mirror_args *mirror = NULL;
2921 mirror = calloc(1, sizeof(*mirror));
2932 * lfs_mirror_free() - Free memory allocated for a mirror argument
2934 * @mirror: Previously allocated mirror argument structure by
2935 * lfs_mirror_alloc().
2937 * Free memory allocated for @mirror.
2941 static void lfs_mirror_free(struct mirror_args *mirror)
2943 if (mirror->m_layout != NULL)
2944 llapi_layout_free(mirror->m_layout);
2949 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2950 * @mirror_list: Previously allocated mirror list.
2952 * Free memory allocated for @mirror_list.
2956 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2958 struct mirror_args *next_mirror = NULL;
2960 while (mirror_list != NULL) {
2961 next_mirror = mirror_list->m_next;
2962 lfs_mirror_free(mirror_list);
2963 mirror_list = next_mirror;
2975 LFS_COMP_NO_VERIFY_OPT,
2977 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2979 LFS_MIRROR_STATE_OPT,
2981 LFS_MIRROR_INDEX_OPT,
2982 LFS_LAYOUT_FOREIGN_OPT,
2988 static int lfs_setstripe_internal(int argc, char **argv,
2989 enum setstripe_origin opc)
2991 struct lfs_setstripe_args lsa = { 0 };
2992 struct llapi_stripe_param *param = NULL;
2993 struct find_param migrate_mdt_param = {
3003 unsigned long long size_units = 1;
3004 bool migrate_mode = false;
3005 bool migrate_mdt_mode = false;
3006 bool setstripe_mode = false;
3007 bool migration_block = false;
3008 __u64 migration_flags = 0;
3009 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3010 int comp_del = 0, comp_set = 0;
3013 struct llapi_layout *layout = NULL;
3014 struct llapi_layout **lpp = &layout;
3015 bool mirror_mode = false;
3016 bool has_m_file = false;
3017 __u32 mirror_count = 0;
3018 enum mirror_flags mirror_flags = 0;
3019 struct mirror_args *mirror_list = NULL;
3020 struct mirror_args *new_mirror = NULL;
3021 struct mirror_args *last_mirror = NULL;
3022 __u16 mirror_id = 0;
3024 bool from_yaml = false;
3025 bool from_copy = false;
3026 char *template = NULL;
3027 bool foreign_mode = false;
3029 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3030 char *mode_opt = NULL;
3031 mode_t previous_umask = 0;
3032 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3034 struct option long_opts[] = {
3035 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3036 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3037 /* --block is only valid in migrate mode */
3038 { .val = 'b', .name = "block", .has_arg = no_argument },
3039 { .val = LFS_COMP_ADD_OPT,
3040 .name = "comp-add", .has_arg = no_argument },
3041 { .val = LFS_COMP_ADD_OPT,
3042 .name = "component-add", .has_arg = no_argument },
3043 { .val = LFS_COMP_DEL_OPT,
3044 .name = "comp-del", .has_arg = no_argument },
3045 { .val = LFS_COMP_DEL_OPT,
3046 .name = "component-del", .has_arg = no_argument },
3047 { .val = LFS_COMP_FLAGS_OPT,
3048 .name = "comp-flags", .has_arg = required_argument },
3049 { .val = LFS_COMP_FLAGS_OPT,
3050 .name = "component-flags",
3051 .has_arg = required_argument },
3052 { .val = LFS_COMP_SET_OPT,
3053 .name = "comp-set", .has_arg = no_argument },
3054 { .val = LFS_COMP_SET_OPT,
3055 .name = "component-set",
3056 .has_arg = no_argument},
3057 { .val = LFS_COMP_NO_VERIFY_OPT,
3058 .name = "no-verify", .has_arg = no_argument},
3059 { .val = LFS_LAYOUT_FLAGS_OPT,
3060 .name = "flags", .has_arg = required_argument},
3061 { .val = LFS_LAYOUT_FOREIGN_OPT,
3062 .name = "foreign", .has_arg = optional_argument},
3063 { .val = LFS_MIRROR_ID_OPT,
3064 .name = "mirror-id", .has_arg = required_argument},
3065 { .val = LFS_MODE_OPT,
3066 .name = "mode", .has_arg = required_argument},
3067 { .val = LFS_LAYOUT_COPY,
3068 .name = "copy", .has_arg = required_argument},
3069 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3070 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3071 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3072 { .val = 'C', .name = "overstripe-count",
3073 .has_arg = required_argument},
3074 { .val = 'd', .name = "delete", .has_arg = no_argument},
3075 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3076 /* --non-direct is only valid in migrate mode */
3077 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3078 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3079 { .val = 'E', .name = "component-end",
3080 .has_arg = required_argument},
3081 { .val = 'f', .name = "file", .has_arg = required_argument },
3082 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3083 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3084 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3085 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3086 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3087 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3088 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3089 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3090 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3091 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3092 { .val = 'L', .name = "layout", .has_arg = required_argument },
3093 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3094 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3095 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3096 /* --non-block is only valid in migrate mode */
3097 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3098 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3099 { .val = 'o', .name = "ost", .has_arg = required_argument },
3100 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3101 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3102 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3104 { .val = 'p', .name = "pool", .has_arg = required_argument },
3105 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3106 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3107 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3108 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3109 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3110 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3111 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3112 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3113 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3114 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3115 /* --verbose is only valid in migrate mode */
3116 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3117 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3118 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3119 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3120 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3123 setstripe_args_init(&lsa);
3125 migrate_mode = (opc == SO_MIGRATE);
3126 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3127 setstripe_mode = (opc == SO_SETSTRIPE);
3128 if (opc == SO_MIRROR_DELETE) {
3130 mirror_flags = MF_DESTROY;
3133 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3135 while ((c = getopt_long(argc, argv,
3136 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3137 long_opts, NULL)) >= 0) {
3143 case LFS_COMP_ADD_OPT:
3146 case LFS_COMP_DEL_OPT:
3149 case LFS_COMP_FLAGS_OPT:
3150 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3151 &lsa.lsa_comp_neg_flags);
3154 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3155 fprintf(stderr, "%s: inverted flags are not supported\n",
3160 case LFS_COMP_SET_OPT:
3163 case LFS_COMP_NO_VERIFY_OPT:
3164 mirror_flags |= MF_NO_VERIFY;
3166 case LFS_MIRROR_ID_OPT:
3167 mirror_id = strtoul(optarg, &end, 0);
3168 if (*end != '\0' || mirror_id == 0) {
3170 "%s %s: invalid mirror ID '%s'\n",
3171 progname, argv[0], optarg);
3175 case LFS_LAYOUT_FLAGS_OPT: {
3178 /* check for numeric flags (foreign and mirror cases) */
3179 if (setstripe_mode && !mirror_mode && !last_mirror) {
3180 flags = strtoul(optarg, &end, 16);
3183 "%s %s: bad flags '%s'\n",
3184 progname, argv[0], optarg);
3190 if (!mirror_mode || !last_mirror) {
3191 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
3196 result = comp_str2flags(optarg, &last_mirror->m_flags,
3202 fprintf(stderr, "%s: inverted flags are not supported\n",
3207 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3209 "%s: unsupported mirror flags: %s\n",
3216 case LFS_LAYOUT_FOREIGN_OPT:
3217 if (optarg != NULL) {
3218 /* check pure numeric */
3219 type = strtoul(optarg, &end, 0);
3222 type = check_foreign_type_name(optarg);
3223 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3225 "%s %s: unrecognized foreign type '%s'\n",
3232 foreign_mode = true;
3236 if (mode_opt != NULL) {
3237 mode = strtoul(mode_opt, &end, 8);
3240 "%s %s: bad mode '%s'\n",
3241 progname, argv[0], mode_opt);
3244 previous_umask = umask(0);
3247 case LFS_LAYOUT_COPY:
3252 if (!migrate_mode) {
3254 "%s %s: -b|--block valid only for migrate command\n",
3258 migration_block = true;
3261 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3263 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3267 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3270 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3273 "%s %s: invalid stripe count '%s'\n",
3274 progname, argv[0], optarg);
3278 if (lsa.lsa_stripe_count == -1)
3279 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3282 /* delete the default striping pattern */
3284 if (opc == SO_MIRROR_SPLIT) {
3287 "%s %s: -d cannot used with -f\n",
3291 mirror_flags |= MF_DESTROY;
3295 if (!migrate_mode) {
3297 "%s %s: -D|--non-direct is valid "
3298 "only for migrate command\n",
3302 migration_flags |= MIGRATION_NONDIRECT;
3305 if (lsa.lsa_comp_end != 0) {
3306 result = comp_args_to_layout(lpp, &lsa, true);
3308 fprintf(stderr, "%s: invalid layout\n",
3313 setstripe_args_init_inherit(&lsa);
3316 if (arg_is_eof(optarg)) {
3317 lsa.lsa_comp_end = LUSTRE_EOF;
3319 result = llapi_parse_size(optarg,
3324 "%s %s: invalid component end '%s'\n",
3325 progname, argv[0], optarg);
3331 if (!migrate_mode) {
3332 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3336 lsa.lsa_pattern = check_hashtype(optarg);
3337 if (lsa.lsa_pattern == 0) {
3339 "%s %s: bad stripe hash type '%s'\n",
3340 progname, argv[0], optarg);
3345 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3348 "%s %s: invalid stripe offset '%s'\n",
3349 progname, argv[0], optarg);
3352 if (lsa.lsa_stripe_off == -1)
3353 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3356 comp_id = strtoul(optarg, &end, 0);
3357 if (*end != '\0' || comp_id == 0 ||
3358 comp_id > LCME_ID_MAX) {
3360 "%s %s: invalid component ID '%s'\n",
3361 progname, argv[0], optarg);
3366 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3368 "error: %s: invalid option: %s\n",
3369 progname, argv[optopt + 1]);
3372 if (opc == SO_MIRROR_EXTEND) {
3373 if (last_mirror == NULL) {
3375 "error: %s: '-N' must exist in front of '%s'\n",
3376 progname, argv[optopt + 1]);
3379 last_mirror->m_file = optarg;
3380 last_mirror->m_count = 1;
3383 if (mirror_list == NULL)
3384 mirror_list = lfs_mirror_alloc();
3385 mirror_list->m_file = optarg;
3390 if (strcmp(argv[optind - 1], "mdt") == 0) {
3391 /* Can be only the first component */
3392 if (layout != NULL) {
3394 fprintf(stderr, "error: 'mdt' layout "
3395 "can be only the first one\n");
3398 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3400 fprintf(stderr, "error: 'mdt' layout "
3401 "size is too big\n");
3404 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3405 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3407 fprintf(stderr, "error: layout '%s' is "
3408 "unknown, supported layouts are: "
3409 "'mdt', 'raid0'\n", argv[optind]);
3414 if (!migrate_mode) {
3416 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3420 migrate_mdt_mode = true;
3421 lsa.lsa_nr_tgts = parse_targets(tgts,
3422 sizeof(tgts) / sizeof(__u32),
3423 lsa.lsa_nr_tgts, optarg, NULL);
3424 if (lsa.lsa_nr_tgts < 0) {
3426 "%s %s: invalid MDT target(s) '%s'\n",
3427 progname, argv[0], optarg);
3431 lsa.lsa_tgts = tgts;
3432 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3433 lsa.lsa_stripe_off = tgts[0];
3436 if (!migrate_mode) {
3438 "%s %s: -n|--non-block valid only for migrate command\n",
3442 migration_flags |= MIGRATION_NONBLOCK;
3445 if (opc == SO_SETSTRIPE) {
3446 opc = SO_MIRROR_CREATE;
3450 if (optarg != NULL) {
3451 mirror_count = strtoul(optarg, &end, 0);
3452 if (*end != '\0' || mirror_count == 0) {
3454 "error: %s: bad mirror count: %s\n",
3461 new_mirror = lfs_mirror_alloc();
3462 new_mirror->m_count = mirror_count;
3464 if (mirror_list == NULL)
3465 mirror_list = new_mirror;
3467 if (last_mirror != NULL) {
3468 /* wrap up last mirror */
3469 if (lsa.lsa_comp_end == 0)
3470 lsa.lsa_comp_end = LUSTRE_EOF;
3472 result = comp_args_to_layout(lpp, &lsa, true);
3474 lfs_mirror_free(new_mirror);
3478 setstripe_args_init_inherit(&lsa);
3480 last_mirror->m_next = new_mirror;
3483 last_mirror = new_mirror;
3484 lpp = &last_mirror->m_layout;
3487 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3488 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3489 fprintf(stderr, "warning: '--ost-list' is "
3490 "deprecated, use '--ost' instead\n");
3492 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3494 "%s %s: -o|--ost incompatible with DoM layout\n",
3498 /* -o allows overstriping, and must note it because
3499 * parse_targets is shared with MDT striping, which
3500 * does not allow duplicates
3502 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3503 lsa.lsa_nr_tgts = parse_targets(tgts,
3504 sizeof(tgts) / sizeof(__u32),
3505 lsa.lsa_nr_tgts, optarg,
3507 if (lsa.lsa_nr_tgts < 0) {
3509 "%s %s: invalid OST target(s) '%s'\n",
3510 progname, argv[0], optarg);
3514 lsa.lsa_tgts = tgts;
3515 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3516 lsa.lsa_stripe_off = tgts[0];
3521 lsa.lsa_pool_name = optarg;
3523 if (strlen(lsa.lsa_pool_name) == 0 ||
3524 strncmp(lsa.lsa_pool_name, "none",
3525 LOV_MAXPOOLNAME) == 0)
3526 lsa.lsa_pool_name = NULL;
3529 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3533 "%s %s: invalid stripe size '%s'\n",
3534 progname, argv[0], optarg);
3539 if (!migrate_mode) {
3541 "%s %s: -v|--verbose valid only for migrate command\n",
3545 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3546 migration_flags = MIGRATION_VERBOSE;
3556 result = llapi_parse_size(optarg,
3557 &lsa.lsa_extension_size,
3561 "%s %s: invalid extension size '%s'\n",
3562 progname, argv[0], optarg);
3566 lsa.lsa_extension_comp = true;
3569 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3570 progname, argv[0], argv[optind - 1]);
3575 fname = argv[optind];
3577 if (optind == argc) {
3578 fprintf(stderr, "%s %s: FILE must be specified\n",
3583 if (xattr && !foreign_mode) {
3584 /* only print a warning as this is harmless and will be ignored
3587 "%s %s: xattr has been specified for non-foreign layout\n",
3589 } else if (foreign_mode && !xattr) {
3591 "%s %s: xattr must be provided in foreign mode\n",
3596 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3597 comp_set || comp_id || delete || from_copy ||
3598 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3601 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3606 if (mirror_mode && mirror_count == 0) {
3608 "error: %s: --mirror-count|-N option is required\n",
3615 if (lsa.lsa_comp_end == 0)
3616 lsa.lsa_comp_end = LUSTRE_EOF;
3619 if (lsa.lsa_comp_end != 0) {
3620 result = comp_args_to_layout(lpp, &lsa, true);
3622 fprintf(stderr, "error: %s: invalid layout\n",
3629 if (mirror_flags & MF_NO_VERIFY) {
3630 if (opc != SO_MIRROR_EXTEND) {
3632 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3636 } else if (!has_m_file) {
3638 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3645 if (comp_set && !comp_id) {
3646 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3651 if ((delete + comp_set + comp_del + comp_add) > 1) {
3653 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3658 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3659 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3661 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3666 if ((comp_set || comp_del) &&
3667 (setstripe_args_specified(&lsa) || layout != NULL)) {
3669 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3674 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3676 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3681 if (comp_add || comp_del) {
3684 result = lstat(fname, &st);
3685 if (result == 0 && S_ISDIR(st.st_mode)) {
3687 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3693 fprintf(stderr, "error: %s: can't use --component-add "
3694 "or --component-del for mirror operation\n",
3701 if (layout == NULL) {
3703 "%s %s: option -E must be specified with --component-add\n",
3709 if (layout != NULL || mirror_list != NULL) {
3711 result = mirror_adjust_first_extents(mirror_list);
3713 result = layout_adjust_first_extent(fname, layout,
3715 if (result == -ENODATA)
3717 else if (result != 0) {
3718 fprintf(stderr, "error: %s: invalid layout\n",
3724 if (from_yaml && from_copy) {
3726 "%s: can't specify --yaml and --copy together\n",
3731 if ((from_yaml || from_copy) &&
3732 (setstripe_args_specified(&lsa) || layout != NULL)) {
3733 fprintf(stderr, "error: %s: can't specify --yaml with "
3734 "-c, -S, -i, -o, -p or -E options.\n",
3739 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3741 "%s %s: options --non-block and --block are mutually exclusive\n",
3746 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3747 opc != SO_MIRROR_DELETE && comp_id != 0) {
3749 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3754 if (migrate_mdt_mode) {
3755 struct lmv_user_md *lmu;
3757 /* initialize migrate mdt parameters */
3758 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3759 LMV_USER_MAGIC_SPECIFIC));
3762 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3763 progname, argv[0], strerror(ENOMEM));
3767 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3768 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3769 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3771 "%s %s: migrate should specify MDT index\n",
3776 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3777 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3778 lmu->lum_hash_type = lsa.lsa_pattern;
3780 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3781 if (lsa.lsa_pool_name) {
3782 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3783 sizeof(lmu->lum_pool_name) - 1);
3784 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3786 if (lsa.lsa_nr_tgts > 1) {
3789 if (lsa.lsa_stripe_count > 0 &&
3790 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3791 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3793 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3794 progname, lsa.lsa_stripe_count,
3800 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3801 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3802 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3803 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3805 lmu->lum_magic = LMV_USER_MAGIC;
3808 migrate_mdt_param.fp_lmv_md = lmu;
3809 migrate_mdt_param.fp_migrate = 1;
3810 } else if (layout == NULL) {
3811 /* initialize stripe parameters */
3812 param = calloc(1, offsetof(typeof(*param),
3813 lsp_osts[lsa.lsa_nr_tgts]));
3814 if (param == NULL) {
3816 "%s %s: cannot allocate memory for parameters: %s\n",
3817 progname, argv[0], strerror(ENOMEM));
3822 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3823 param->lsp_stripe_size = lsa.lsa_stripe_size;
3824 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3825 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3826 param->lsp_stripe_count = -1;
3828 param->lsp_stripe_count = lsa.lsa_stripe_count;
3830 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3831 param->lsp_stripe_offset = -1;
3833 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3834 param->lsp_stripe_pattern =
3835 llapi_pattern_to_lov(lsa.lsa_pattern);
3836 if (param->lsp_stripe_pattern == EINVAL) {
3837 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3842 param->lsp_pool = lsa.lsa_pool_name;
3843 param->lsp_is_specific = false;
3844 if (lsa.lsa_nr_tgts > 0) {
3845 if (lsa.lsa_stripe_count > 0 &&
3846 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3847 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3848 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3850 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3851 argv[0], lsa.lsa_stripe_count,
3857 param->lsp_is_specific = true;
3858 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3859 memcpy(param->lsp_osts, tgts,
3860 sizeof(*tgts) * lsa.lsa_nr_tgts);
3865 /* generate a layout from a YAML template */
3866 result = lfs_comp_create_from_yaml(template, &layout,
3869 fprintf(stderr, "error: %s: can't create composite "
3870 "layout from template file %s\n",
3874 } else if (from_copy) {
3875 layout = llapi_layout_get_by_path(template, 0);
3876 if (layout == NULL) {
3878 "%s: can't create composite layout from file %s.\n",
3879 progname, template);
3884 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3885 if (migrate_mdt_mode) {
3886 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3887 } else if (migrate_mode) {
3888 result = lfs_migrate(fname, migration_flags, param,
3890 } else if (comp_set != 0) {
3891 result = lfs_component_set(fname, comp_id,
3893 lsa.lsa_comp_neg_flags);
3894 } else if (comp_del != 0) {
3895 result = lfs_component_del(fname, comp_id,
3897 lsa.lsa_comp_neg_flags);
3898 } else if (comp_add != 0) {
3899 result = lfs_component_add(fname, layout);
3900 } else if (opc == SO_MIRROR_CREATE) {
3901 result = mirror_create(fname, mirror_list);
3902 } else if (opc == SO_MIRROR_EXTEND) {
3903 result = mirror_extend(fname, mirror_list,
3905 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
3906 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3908 "%s: no mirror specified to delete from '%s'\n",
3912 if (lsa.lsa_pool_name)
3913 mirror_flags |= MF_COMP_POOL;
3914 else if (mirror_id != 0)
3915 comp_id = mirror_id;
3917 mirror_flags |= MF_COMP_ID;
3918 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3920 has_m_file ? mirror_list->m_file :
3922 } else if (layout != NULL) {
3923 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3929 } else if (foreign_mode) {
3930 result = llapi_file_create_foreign(fname, mode, type,
3937 result = llapi_file_open_param(fname,
3946 /* Save the first error encountered. */
3953 if (mode_opt != NULL)
3954 umask(previous_umask);
3957 free(migrate_mdt_param.fp_lmv_md);
3958 llapi_layout_free(layout);
3959 lfs_mirror_list_free(mirror_list);
3964 llapi_layout_free(layout);
3965 lfs_mirror_list_free(mirror_list);
3969 static int lfs_poollist(int argc, char **argv)
3974 return llapi_poollist(argv[1]);
3977 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3982 char *endptr = "AD";
3987 else if (str[0] == '-')
3993 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3994 long long val = strtoll(timebuf, &endptr, 0);
3999 unit *= 52; /* 52 weeks + 1 day below */
4000 case 'w': /* fallthrough */
4002 case '\0': /* days are default unit if none used */
4003 case 'd': /* fallthrough */
4004 unit = (unit + (*endptr == 'y')) * 24;
4005 case 'h': /* fallthrough */
4007 case 'm': /* fallthrough */
4009 case 's': /* fallthrough */
4011 /* don't need to multiply by 1 for seconds */
4014 "%s find: bad time string '%s': %s\n",
4015 progname, timebuf, strerror(EINVAL));
4019 if (param->fp_time_margin == 0 ||
4020 (*endptr && unit < param->fp_time_margin))
4021 param->fp_time_margin = unit;
4028 fprintf(stderr, "%s find: bad time '%s': too large\n",
4038 static int name2uid(unsigned int *id, const char *name)
4040 struct passwd *passwd;
4042 passwd = getpwnam(name);
4045 *id = passwd->pw_uid;
4050 static int name2gid(unsigned int *id, const char *name)
4052 struct group *group;
4054 group = getgrnam(name);
4057 *id = group->gr_gid;
4062 static inline int name2projid(unsigned int *id, const char *name)
4067 static int uid2name(char **name, unsigned int id)
4069 struct passwd *passwd;
4071 passwd = getpwuid(id);
4074 *name = passwd->pw_name;
4079 static inline int gid2name(char **name, unsigned int id)
4081 struct group *group;
4083 group = getgrgid(id);
4086 *name = group->gr_name;
4091 static int name2layout(__u32 *layout, char *name)
4093 char *ptr, *layout_name;
4096 for (ptr = name; ; ptr = NULL) {
4097 layout_name = strtok(ptr, ",");
4098 if (layout_name == NULL)
4100 if (strcmp(layout_name, "released") == 0)
4101 *layout |= LOV_PATTERN_F_RELEASED;
4102 else if (strcmp(layout_name, "raid0") == 0)
4103 *layout |= LOV_PATTERN_RAID0;
4104 else if (strcmp(layout_name, "mdt") == 0)
4105 *layout |= LOV_PATTERN_MDT;
4106 else if (strcmp(layout_name, "overstriping") == 0)
4107 *layout |= LOV_PATTERN_OVERSTRIPING;
4114 static int lfs_find(int argc, char **argv)
4119 struct find_param param = {
4122 .fp_time_margin = 24 * 60 * 60,
4124 struct option long_opts[] = {
4125 { .val = 'A', .name = "atime", .has_arg = required_argument },
4126 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4127 { .val = LFS_COMP_COUNT_OPT,
4128 .name = "comp-count", .has_arg = required_argument },
4129 { .val = LFS_COMP_COUNT_OPT,
4130 .name = "component-count",
4131 .has_arg = required_argument },
4132 { .val = LFS_COMP_FLAGS_OPT,
4133 .name = "comp-flags", .has_arg = required_argument },
4134 { .val = LFS_COMP_FLAGS_OPT,
4135 .name = "component-flags",
4136 .has_arg = required_argument },
4137 { .val = LFS_COMP_START_OPT,
4138 .name = "comp-start", .has_arg = required_argument },
4139 { .val = LFS_COMP_START_OPT,
4140 .name = "component-start",
4141 .has_arg = required_argument },
4142 { .val = LFS_MIRROR_STATE_OPT,
4143 .name = "mirror-state", .has_arg = required_argument },
4144 { .val = LFS_LAYOUT_FOREIGN_OPT,
4145 .name = "foreign", .has_arg = optional_argument},
4146 { .val = LFS_NEWERXY_OPT,
4147 .name = "newer", .has_arg = required_argument},
4148 { .val = LFS_NEWERXY_OPT,
4149 .name = "neweraa", .has_arg = required_argument},
4150 { .val = LFS_NEWERXY_OPT,
4151 .name = "neweram", .has_arg = required_argument},
4152 { .val = LFS_NEWERXY_OPT,
4153 .name = "newerac", .has_arg = required_argument},
4154 { .val = LFS_NEWERXY_OPT,
4155 .name = "newerma", .has_arg = required_argument},
4156 { .val = LFS_NEWERXY_OPT,
4157 .name = "newermm", .has_arg = required_argument},
4158 { .val = LFS_NEWERXY_OPT,
4159 .name = "newermc", .has_arg = required_argument},
4160 { .val = LFS_NEWERXY_OPT,
4161 .name = "newerca", .has_arg = required_argument},
4162 { .val = LFS_NEWERXY_OPT,
4163 .name = "newercm", .has_arg = required_argument},
4164 { .val = LFS_NEWERXY_OPT,
4165 .name = "newercc", .has_arg = required_argument},
4166 { .val = LFS_NEWERXY_OPT,
4167 .name = "newerat", .has_arg = required_argument},
4168 { .val = LFS_NEWERXY_OPT,
4169 .name = "newermt", .has_arg = required_argument},
4170 { .val = LFS_NEWERXY_OPT,
4171 .name = "newerct", .has_arg = required_argument},
4172 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4173 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4174 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4175 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4176 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4177 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4178 { .val = 'E', .name = "component-end",
4179 .has_arg = required_argument },
4180 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4181 { .val = LFS_LAYOUT_FOREIGN_OPT,
4182 .name = "foreign", .has_arg = optional_argument},
4183 { .val = 'g', .name = "gid", .has_arg = required_argument },
4184 { .val = 'G', .name = "group", .has_arg = required_argument },
4185 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4186 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4187 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4188 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4189 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4190 { .val = 'L', .name = "layout", .has_arg = required_argument },
4191 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4192 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4193 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4194 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4195 { .val = 'n', .name = "name", .has_arg = required_argument },
4196 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4197 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4198 { .val = 'O', .name = "obd", .has_arg = required_argument },
4199 { .val = 'O', .name = "ost", .has_arg = required_argument },
4200 /* no short option for pool yet, can be 'p' after 2.18 */
4201 { .val = LFS_POOL_OPT,
4202 .name = "pool", .has_arg = required_argument },
4203 { .val = '0', .name = "print0", .has_arg = no_argument },
4204 { .val = 'P', .name = "print", .has_arg = no_argument },
4205 { .val = LFS_PROJID_OPT,
4206 .name = "projid", .has_arg = required_argument },
4207 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4208 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4209 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4210 { .val = 's', .name = "size", .has_arg = required_argument },
4211 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4212 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4213 { .val = 't', .name = "type", .has_arg = required_argument },
4214 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4215 { .val = 'u', .name = "uid", .has_arg = required_argument },
4216 { .val = 'U', .name = "user", .has_arg = required_argument },
4217 { .val = 'z', .name = "extension-size",
4218 .has_arg = required_argument },
4219 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4220 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4221 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4235 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4236 while ((c = getopt_long_only(argc, argv,
4237 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4238 long_opts, &optidx)) >= 0) {
4243 /* '!' is part of option */
4244 /* when getopt_long_only() finds a string which is not
4245 * an option nor a known option argument it returns 1
4246 * in that case if we already have found pathstart and pathend
4247 * (i.e. we have the list of pathnames),
4248 * the only supported value is "!"
4250 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4251 if (!isoption && pathend != -1) {
4252 fprintf(stderr, "err: %s: filename|dirname must either "
4253 "precede options or follow options\n",
4258 if (!isoption && pathstart == -1)
4259 pathstart = optind - 1;
4260 if (isoption && pathstart != -1 && pathend == -1)
4261 pathend = optind - 2;
4267 /* unknown; opt is "!" or path component,
4268 * checking done above.
4270 if (strcmp(optarg, "!") == 0)
4274 xtime = ¶m.fp_atime;
4275 xsign = ¶m.fp_asign;
4276 param.fp_exclude_atime = !!neg_opt;
4277 /* no break, this falls through to 'C' for ctime */
4280 xtime = ¶m.fp_ctime;
4281 xsign = ¶m.fp_csign;
4282 param.fp_exclude_ctime = !!neg_opt;
4284 /* no break, this falls through to 'M' for mtime */
4287 xtime = ¶m.fp_mtime;
4288 xsign = ¶m.fp_msign;
4289 param.fp_exclude_mtime = !!neg_opt;
4291 rc = set_time(¶m, &t, xtime, optarg);
4292 if (rc == LONG_MAX) {
4300 if (optarg[0] == '+') {
4301 param.fp_blocks_sign = -1;
4303 } else if (optarg[0] == '-') {
4304 param.fp_blocks_sign = 1;
4308 param.fp_blocks_units = 1024;
4309 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4310 ¶m.fp_blocks_units, 0);
4312 fprintf(stderr, "error: bad blocks '%s'\n",
4316 param.fp_check_blocks = 1;
4317 param.fp_exclude_blocks = !!neg_opt;
4319 case LFS_COMP_COUNT_OPT:
4320 if (optarg[0] == '+') {
4321 param.fp_comp_count_sign = -1;
4323 } else if (optarg[0] == '-') {
4324 param.fp_comp_count_sign = 1;
4328 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4329 if (*endptr != '\0') {
4330 fprintf(stderr, "error: bad component count "
4334 param.fp_check_comp_count = 1;
4335 param.fp_exclude_comp_count = !!neg_opt;
4337 case LFS_COMP_FLAGS_OPT:
4338 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4339 ¶m.fp_comp_neg_flags);
4341 fprintf(stderr, "error: bad component flags "
4345 param.fp_check_comp_flags = 1;
4347 __u32 flags = param.fp_comp_neg_flags;
4348 param.fp_comp_neg_flags = param.fp_comp_flags;
4349 param.fp_comp_flags = flags;
4352 case LFS_COMP_START_OPT:
4353 if (optarg[0] == '+') {
4354 param.fp_comp_start_sign = -1;
4356 } else if (optarg[0] == '-') {
4357 param.fp_comp_start_sign = 1;
4361 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4362 ¶m.fp_comp_start_units, 0);
4364 fprintf(stderr, "error: bad component start "
4368 param.fp_check_comp_start = 1;
4369 param.fp_exclude_comp_start = !!neg_opt;
4371 case LFS_MIRROR_STATE_OPT:
4372 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4373 ¶m.fp_mirror_neg_state);
4376 "error: bad mirrored file state '%s'\n",
4380 param.fp_check_mirror_state = 1;
4382 __u16 state = param.fp_mirror_neg_state;
4383 param.fp_mirror_neg_state =
4384 param.fp_mirror_state;
4385 param.fp_mirror_state = state;
4389 if (optarg[0] == '+') {
4390 param.fp_stripe_count_sign = -1;
4392 } else if (optarg[0] == '-') {
4393 param.fp_stripe_count_sign = 1;
4397 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4398 if (*endptr != '\0') {
4399 fprintf(stderr,"error: bad stripe_count '%s'\n",
4404 param.fp_check_stripe_count = 1;
4405 param.fp_exclude_stripe_count = !!neg_opt;
4408 param.fp_max_depth = strtol(optarg, 0, 0);
4411 if (optarg[0] == '+') {
4412 param.fp_comp_end_sign = -1;
4414 } else if (optarg[0] == '-') {
4415 param.fp_comp_end_sign = 1;
4419 if (arg_is_eof(optarg)) {
4420 param.fp_comp_end = LUSTRE_EOF;
4421 param.fp_comp_end_units = 1;
4424 rc = llapi_parse_size(optarg,
4426 ¶m.fp_comp_end_units, 0);
4429 fprintf(stderr, "error: bad component end "
4433 param.fp_check_comp_end = 1;
4434 param.fp_exclude_comp_end = !!neg_opt;
4436 case LFS_LAYOUT_FOREIGN_OPT: {
4437 /* all types by default */
4438 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4440 if (optarg != NULL) {
4441 /* check pure numeric */
4442 type = strtoul(optarg, &endptr, 0);
4445 type = check_foreign_type_name(optarg);
4446 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4448 "%s %s: unknown foreign type '%s'\n",
4455 param.fp_foreign_type = type;
4456 param.fp_check_foreign = 1;
4457 param.fp_exclude_foreign = !!neg_opt;
4460 case LFS_NEWERXY_OPT: {
4466 time_t ref = time(NULL);
4468 /* no need to check bad options, they won't get here */
4469 if (strlen(long_opts[optidx].name) == 7) {
4470 x = long_opts[optidx].name[5];
4471 y = long_opts[optidx].name[6];
4475 static const char *const fmts[] = {
4476 "%Y-%m-%d %H:%M:%S",
4479 "%H:%M:%S", /* sometime today */
4488 for (i = 0; fmts[i] != NULL; i++) {
4491 /* Init for times relative to today */
4492 if (strncmp(fmts[i], "%H", 2) == 0)
4493 localtime_r(&ref, &tm);
4495 memset(&tm, 0, sizeof(tm));
4496 ptr = strptime(optarg, fmts[i], &tm);
4498 while (ptr && isspace(*ptr))
4500 if (ptr == optarg + strlen(optarg)) {
4508 "%s: invalid time '%s'\n",
4511 "supported formats are:\n ");
4512 for (i = 0; fmts[i] != NULL; i++)
4513 fprintf(stderr, "'%s', ",
4515 fprintf(stderr, "\n");
4522 struct stat statbuf;
4524 if (stat(optarg, &statbuf) < 0) {
4526 "%s: cannot stat file '%s': %s\n",
4535 ref = statbuf.st_atime;
4538 ref = statbuf.st_mtime;
4541 ref = statbuf.st_ctime;
4545 "%s: invalid Y argument: '%c'\n",
4554 xidx = NEWERXY_ATIME;
4557 xidx = NEWERXY_MTIME;
4560 xidx = NEWERXY_CTIME;
4564 "%s: invalid X argument: '%c'\n",
4571 newery = ¶m.fp_newery[xidx][negidx];
4577 *newery = *newery > ref ? ref : *newery;
4579 *newery = *newery > ref ? *newery : ref;
4581 param.fp_newerxy = 1;
4586 rc = name2gid(¶m.fp_gid, optarg);
4588 param.fp_gid = strtoul(optarg, &endptr, 10);
4589 if (*endptr != '\0') {
4590 fprintf(stderr, "Group/GID: %s cannot "
4591 "be found.\n", optarg);
4596 param.fp_exclude_gid = !!neg_opt;
4597 param.fp_check_gid = 1;
4600 param.fp_hash_type = check_hashtype(optarg);
4601 if (param.fp_hash_type == 0) {
4602 fprintf(stderr, "error: bad hash_type '%s'\n",
4607 param.fp_check_hash_type = 1;
4608 param.fp_exclude_hash_type = !!neg_opt;
4614 ret = name2layout(¶m.fp_layout, optarg);
4617 param.fp_exclude_layout = !!neg_opt;
4618 param.fp_check_layout = 1;
4622 rc = name2uid(¶m.fp_uid, optarg);
4624 param.fp_uid = strtoul(optarg, &endptr, 10);
4625 if (*endptr != '\0') {
4626 fprintf(stderr, "User/UID: %s cannot "
4627 "be found.\n", optarg);
4632 param.fp_exclude_uid = !!neg_opt;
4633 param.fp_check_uid = 1;
4636 param.fp_pattern = (char *)optarg;
4637 param.fp_exclude_pattern = !!neg_opt;
4640 if (optarg[0] == '+') {
4641 param.fp_mirror_count_sign = -1;
4643 } else if (optarg[0] == '-') {
4644 param.fp_mirror_count_sign = 1;
4648 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4649 if (*endptr != '\0') {
4651 "error: bad mirror count '%s'\n",
4655 param.fp_check_mirror_count = 1;
4656 param.fp_exclude_mirror_count = !!neg_opt;
4661 char *buf, *token, *next, *p;
4665 buf = strdup(optarg);
4671 param.fp_exclude_obd = !!neg_opt;
4674 while (token && *token) {
4675 token = strchr(token, ',');
4682 param.fp_exclude_mdt = !!neg_opt;
4683 param.fp_num_alloc_mdts += len;
4684 tmp = realloc(param.fp_mdt_uuid,
4685 param.fp_num_alloc_mdts *
4686 sizeof(*param.fp_mdt_uuid));
4692 param.fp_mdt_uuid = tmp;
4694 param.fp_exclude_obd = !!neg_opt;
4695 param.fp_num_alloc_obds += len;
4696 tmp = realloc(param.fp_obd_uuid,
4697 param.fp_num_alloc_obds *
4698 sizeof(*param.fp_obd_uuid));
4704 param.fp_obd_uuid = tmp;
4706 for (token = buf; token && *token; token = next) {
4707 struct obd_uuid *puuid;
4710 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4713 ¶m.fp_obd_uuid[param.fp_num_obds++];
4715 p = strchr(token, ',');
4722 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4727 strncpy(puuid->uuid, token,
4728 sizeof(puuid->uuid));
4735 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4739 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4741 "Pool name %s is too long (max %d)\n",
4742 optarg, LOV_MAXPOOLNAME);
4747 * We do check for empty pool because empty pool
4748 * is used to find V1 LOV attributes
4750 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4751 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4752 param.fp_exclude_pool = !!neg_opt;
4753 param.fp_check_pool = 1;
4755 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4756 case 'p': /* want this for --pool, to match getstripe/find */
4758 "warning: -p deprecated, use --print0 or -0\n");
4761 param.fp_zero_end = 1;
4763 case 'P': /* we always print, this option is a no-op */
4765 case LFS_PROJID_OPT:
4766 rc = name2projid(¶m.fp_projid, optarg);
4768 param.fp_projid = strtoul(optarg, &endptr, 10);
4769 if (*endptr != '\0') {
4771 "Invalid project ID: %s",
4777 param.fp_exclude_projid = !!neg_opt;
4778 param.fp_check_projid = 1;
4781 if (optarg[0] == '+') {
4782 param.fp_size_sign = -1;
4784 } else if (optarg[0] == '-') {
4785 param.fp_size_sign = 1;
4789 ret = llapi_parse_size(optarg, ¶m.fp_size,
4790 ¶m.fp_size_units, 0);
4792 fprintf(stderr, "error: bad file size '%s'\n",
4796 param.fp_check_size = 1;
4797 param.fp_exclude_size = !!neg_opt;
4800 if (optarg[0] == '+') {
4801 param.fp_stripe_size_sign = -1;
4803 } else if (optarg[0] == '-') {
4804 param.fp_stripe_size_sign = 1;
4808 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4809 ¶m.fp_stripe_size_units, 0);
4811 fprintf(stderr, "error: bad stripe_size '%s'\n",
4815 param.fp_check_stripe_size = 1;
4816 param.fp_exclude_stripe_size = !!neg_opt;
4819 param.fp_exclude_type = !!neg_opt;
4820 switch (optarg[0]) {
4822 param.fp_type = S_IFBLK;
4825 param.fp_type = S_IFCHR;
4828 param.fp_type = S_IFDIR;
4831 param.fp_type = S_IFREG;
4834 param.fp_type = S_IFLNK;
4837 param.fp_type = S_IFIFO;
4840 param.fp_type = S_IFSOCK;
4843 fprintf(stderr, "error: %s: bad type '%s'\n",
4850 if (optarg[0] == '+') {
4851 param.fp_mdt_count_sign = -1;
4853 } else if (optarg[0] == '-') {
4854 param.fp_mdt_count_sign = 1;
4858 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4859 if (*endptr != '\0') {
4860 fprintf(stderr, "error: bad mdt_count '%s'\n",
4865 param.fp_check_mdt_count = 1;
4866 param.fp_exclude_mdt_count = !!neg_opt;
4869 if (optarg[0] == '+') {
4870 param.fp_ext_size_sign = -1;
4872 } else if (optarg[0] == '-') {
4873 param.fp_ext_size_sign = 1;
4877 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4878 ¶m.fp_ext_size_units, 0);
4880 fprintf(stderr, "error: bad ext-size '%s'\n",
4884 param.fp_ext_size /= SEL_UNIT_SIZE;
4885 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4886 param.fp_check_ext_size = 1;
4887 param.fp_exclude_ext_size = !!neg_opt;
4895 if (pathstart == -1) {
4896 fprintf(stderr, "error: %s: no filename|pathname\n",
4900 } else if (pathend == -1) {
4906 rc = llapi_find(argv[pathstart], ¶m);
4909 pathbad = pathstart;
4911 } while (++pathstart < pathend);
4914 fprintf(stderr, "%s: failed for '%s': %s\n",
4915 progname, argv[pathbad], strerror(-rc));
4918 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4919 free(param.fp_obd_uuid);
4921 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4922 free(param.fp_mdt_uuid);
4927 static int lfs_getstripe_internal(int argc, char **argv,
4928 struct find_param *param)
4930 struct option long_opts[] = {
4931 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4932 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4933 { .val = LFS_COMP_COUNT_OPT,
4934 .name = "comp-count", .has_arg = no_argument },
4935 { .val = LFS_COMP_COUNT_OPT,
4936 .name = "component-count", .has_arg = no_argument },
4937 { .val = LFS_COMP_FLAGS_OPT,
4938 .name = "comp-flags", .has_arg = optional_argument },
4939 { .val = LFS_COMP_FLAGS_OPT,
4940 .name = "component-flags", .has_arg = optional_argument },
4941 { .val = LFS_COMP_START_OPT,
4942 .name = "comp-start", .has_arg = optional_argument },
4943 { .val = LFS_COMP_START_OPT,
4944 .name = "component-start", .has_arg = optional_argument },
4945 { .val = LFS_MIRROR_INDEX_OPT,
4946 .name = "mirror-index", .has_arg = required_argument },
4947 { .val = LFS_MIRROR_ID_OPT,
4948 .name = "mirror-id", .has_arg = required_argument },
4949 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4950 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4951 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4952 { .val = 'd', .name = "directory", .has_arg = no_argument },
4953 { .val = 'D', .name = "default", .has_arg = no_argument },
4954 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4955 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4956 { .val = 'F', .name = "fid", .has_arg = no_argument },
4957 { .val = 'g', .name = "generation", .has_arg = no_argument },
4958 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4959 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4960 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4961 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4962 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4963 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4964 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
4965 { .val = 'L', .name = "layout", .has_arg = no_argument },
4966 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4967 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4968 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4969 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4970 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4971 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4972 { .val = 'O', .name = "obd", .has_arg = required_argument },
4973 { .val = 'O', .name = "ost", .has_arg = required_argument },
4974 { .val = 'p', .name = "pool", .has_arg = no_argument },
4975 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4976 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4977 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4978 { .val = 'R', .name = "raw", .has_arg = no_argument },
4979 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4980 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4981 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4982 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4983 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4984 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4985 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4986 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4987 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
4988 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
4992 int pathstart = -1, pathend = -1;
4996 while ((c = getopt_long(argc, argv,
4997 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
4998 long_opts, NULL)) != -1) {
5002 /* '!' is part of option */
5003 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5004 if (!isoption && pathend != -1) {
5006 "error: %s: filename|dirname must either precede options or follow options\n",
5010 if (!isoption && pathstart == -1)
5011 pathstart = optind - 1;
5012 if (isoption && pathstart != -1 && pathend == -1)
5013 pathend = optind - 2;
5017 /* unknown: opt is "!" */
5018 if (strcmp(optarg, "!") == 0)
5022 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5023 param->fp_verbose |= VERBOSE_COUNT;
5024 param->fp_max_depth = 0;
5027 case LFS_COMP_COUNT_OPT:
5028 param->fp_verbose |= VERBOSE_COMP_COUNT;
5029 param->fp_max_depth = 0;
5031 case LFS_COMP_FLAGS_OPT:
5032 if (optarg != NULL) {
5033 rc = comp_str2flags(optarg,
5034 ¶m->fp_comp_flags,
5035 ¶m->fp_comp_neg_flags);
5037 fprintf(stderr, "error: %s bad "
5038 "component flags '%s'.\n",
5042 param->fp_check_comp_flags = 1;
5044 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5045 param->fp_max_depth = 0;
5048 case LFS_COMP_START_OPT:
5049 if (optarg != NULL) {
5051 if (tmp[0] == '+') {
5052 param->fp_comp_start_sign = -1;
5054 } else if (tmp[0] == '-') {
5055 param->fp_comp_start_sign = 1;
5058 rc = llapi_parse_size(tmp,
5059 ¶m->fp_comp_start,
5060 ¶m->fp_comp_start_units, 0);
5062 fprintf(stderr, "error: %s bad "
5063 "component start '%s'.\n",
5067 param->fp_check_comp_start = 1;
5070 param->fp_verbose |= VERBOSE_COMP_START;
5071 param->fp_max_depth = 0;
5074 case LFS_MIRROR_INDEX_OPT:
5075 if (optarg[0] == '+') {
5076 param->fp_mirror_index_sign = -1;
5078 } else if (optarg[0] == '-') {
5079 param->fp_mirror_index_sign = 1;
5083 param->fp_mirror_index = strtoul(optarg, &end, 0);
5084 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5085 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5087 "%s %s: invalid mirror index '%s'\n",
5088 progname, argv[0], optarg);
5091 if (param->fp_mirror_id != 0) {
5093 "%s %s: can't specify both mirror index and mirror ID\n",
5097 param->fp_check_mirror_index = 1;
5098 param->fp_exclude_mirror_index = !!neg_opt;
5100 case LFS_MIRROR_ID_OPT:
5101 if (optarg[0] == '+') {
5102 param->fp_mirror_id_sign = -1;
5104 } else if (optarg[0] == '-') {
5105 param->fp_mirror_id_sign = 1;
5109 param->fp_mirror_id = strtoul(optarg, &end, 0);
5110 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5111 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5113 "%s %s: invalid mirror ID '%s'\n",
5114 progname, argv[0], optarg);
5117 if (param->fp_mirror_index != 0) {
5119 "%s %s: can't specify both mirror index and mirror ID\n",
5123 param->fp_check_mirror_id = 1;
5124 param->fp_exclude_mirror_id = !!neg_opt;
5127 param->fp_max_depth = 0;
5130 param->fp_get_default_lmv = 1;
5133 if (optarg != NULL) {
5135 if (tmp[0] == '+') {
5136 param->fp_comp_end_sign = -1;
5138 } else if (tmp[0] == '-') {
5139 param->fp_comp_end_sign = 1;
5143 if (arg_is_eof(tmp)) {
5144 param->fp_comp_end = LUSTRE_EOF;
5145 param->fp_comp_end_units = 1;
5148 rc = llapi_parse_size(tmp,
5149 ¶m->fp_comp_end,
5150 ¶m->fp_comp_end_units, 0);
5153 fprintf(stderr, "error: %s bad "
5154 "component end '%s'.\n",
5158 param->fp_check_comp_end = 1;
5160 param->fp_verbose |= VERBOSE_COMP_END;
5161 param->fp_max_depth = 0;
5165 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5166 param->fp_verbose |= VERBOSE_DFID;
5167 param->fp_max_depth = 0;
5171 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5172 param->fp_verbose |= VERBOSE_GENERATION;
5173 param->fp_max_depth = 0;
5177 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5178 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5179 param->fp_max_depth = 0;
5183 if (optarg != NULL) {
5184 param->fp_comp_id = strtoul(optarg, &end, 0);
5185 if (*end != '\0' || param->fp_comp_id == 0 ||
5186 param->fp_comp_id > LCME_ID_MAX) {
5187 fprintf(stderr, "error: %s bad "
5188 "component id '%s'\n",
5192 param->fp_check_comp_id = 1;
5195 param->fp_max_depth = 0;
5196 param->fp_verbose |= VERBOSE_COMP_ID;
5200 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5201 param->fp_verbose |= VERBOSE_PATTERN;
5202 param->fp_max_depth = 0;
5205 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5207 fprintf(stderr, "warning: '-M' deprecated"
5208 ", use '--mdt-index' or '-m' instead\n");
5211 if (!(param->fp_verbose & VERBOSE_DETAIL))
5212 param->fp_max_depth = 0;
5213 param->fp_verbose |= VERBOSE_MDTINDEX;
5216 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5217 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5218 param->fp_max_depth = 0;
5222 if (param->fp_obd_uuid) {
5224 "error: %s: only one obduuid allowed",
5228 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5231 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5232 param->fp_verbose |= VERBOSE_POOL;
5233 param->fp_max_depth = 0;
5240 param->fp_recursive = 1;
5246 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5247 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5248 param->fp_max_depth = 0;
5252 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5258 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5259 param->fp_verbose |= VERBOSE_EXT_SIZE;
5260 param->fp_max_depth = 0;
5268 if (pathstart == -1) {
5269 fprintf(stderr, "error: %s: no filename|pathname\n",
5272 } else if (pathend == -1) {
5280 if (param->fp_recursive)
5281 param->fp_max_depth = -1;
5282 else if (param->fp_verbose & VERBOSE_DETAIL)
5283 param->fp_max_depth = 1;
5285 if (!param->fp_verbose)
5286 param->fp_verbose = VERBOSE_DEFAULT;
5287 if (param->fp_quiet)
5288 param->fp_verbose = VERBOSE_OBJID;
5291 rc = llapi_getstripe(argv[pathstart], param);
5292 } while (++pathstart < pathend && !rc);
5295 fprintf(stderr, "error: %s failed for %s.\n",
5296 argv[0], argv[optind - 1]);
5300 static int lfs_tgts(int argc, char **argv)
5302 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5303 struct find_param param;
5304 int index = 0, rc=0;
5309 if (argc == 2 && !realpath(argv[1], path)) {
5311 fprintf(stderr, "error: invalid path '%s': %s\n",
5312 argv[1], strerror(-rc));
5316 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5317 /* Check if we have a mount point */
5318 if (mntdir[0] == '\0')
5321 memset(¶m, 0, sizeof(param));
5322 if (!strcmp(argv[0], "mdts"))
5323 param.fp_get_lmv = 1;
5325 rc = llapi_ostlist(mntdir, ¶m);
5327 fprintf(stderr, "error: %s: failed on %s\n",
5330 if (path[0] != '\0')
5332 memset(mntdir, 0, PATH_MAX);
5338 static int lfs_getstripe(int argc, char **argv)
5340 struct find_param param = { 0 };
5342 param.fp_max_depth = 1;
5343 return lfs_getstripe_internal(argc, argv, ¶m);
5347 static int lfs_getdirstripe(int argc, char **argv)
5349 struct find_param param = { 0 };
5350 struct option long_opts[] = {
5351 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5352 { .val = 'D', .name = "default", .has_arg = no_argument },
5353 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5354 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5355 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5356 { .val = 'O', .name = "obd", .has_arg = required_argument },
5357 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5358 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5359 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5360 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5364 param.fp_get_lmv = 1;
5366 while ((c = getopt_long(argc, argv,
5367 "cDHimO:rtTvy", long_opts, NULL)) != -1)
5372 param.fp_verbose |= VERBOSE_COUNT;
5375 param.fp_get_default_lmv = 1;
5379 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5381 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5383 fprintf(stderr, "warning: '-t' deprecated, "
5384 "use '--mdt-hash' or '-H' instead\n");
5387 param.fp_verbose |= VERBOSE_HASH_TYPE;
5390 if (param.fp_obd_uuid) {
5392 "error: %s: only one obduuid allowed",
5396 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5399 param.fp_recursive = 1;
5402 param.fp_verbose |= VERBOSE_DETAIL;
5408 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5409 progname, argv[0], argv[optind - 1]);
5417 if (param.fp_recursive)
5418 param.fp_max_depth = -1;
5420 if (!param.fp_verbose)
5421 param.fp_verbose = VERBOSE_DEFAULT;
5424 rc = llapi_getstripe(argv[optind], ¶m);
5425 } while (++optind < argc && !rc);
5428 fprintf(stderr, "error: %s failed for %s.\n",
5429 argv[0], argv[optind - 1]);
5434 MNTDF_INODES = 0x0001,
5435 MNTDF_COOKED = 0x0002,
5436 MNTDF_LAZY = 0x0004,
5437 MNTDF_VERBOSE = 0x0008,
5438 MNTDF_SHOW = 0x0010,
5441 #define COOK(value) \
5444 while (value > 1024) { \
5452 #define CDF "%11llu"
5453 #define HDF "%8.1f%c"
5457 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5459 double avail, used, ratio = 0;
5462 avail = st->os_ffree;
5463 used = st->os_files - st->os_ffree;
5465 avail = st->os_bavail;
5466 used = st->os_blocks - st->os_bfree;
5468 if (avail + used > 0)
5469 ratio = used / (used + avail) * 100;
5471 /* Round up to match df(1) usage percentage */
5472 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5475 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5476 * so only show flags reflecting those states by default. Informational states
5477 * are only shown with "-v" and use lower-case names to distinguish them.
5478 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5480 static struct obd_statfs_state_names {
5481 enum obd_statfs_state osn_state;
5482 const char osn_name;
5485 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
5486 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
5487 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
5488 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
5489 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
5490 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
5491 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
5492 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
5493 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
5496 static int showdf(char *mntdir, struct obd_statfs *stat,
5497 char *uuid, enum mntdf_flags flags,
5498 char *type, int index, int rc)
5500 long long avail, used, total;
5502 char *suffix = "KMGTPEZY";
5503 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5504 char tbuf[3 * sizeof(__u64)];
5505 char ubuf[3 * sizeof(__u64)];
5506 char abuf[3 * sizeof(__u64)];
5507 char rbuf[3 * sizeof(__u64)];
5514 if (flags & MNTDF_INODES) {
5515 avail = stat->os_ffree;
5516 used = stat->os_files - stat->os_ffree;
5517 total = stat->os_files;
5519 int shift = flags & MNTDF_COOKED ? 0 : 10;
5521 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5522 used = ((stat->os_blocks - stat->os_bfree) *
5523 stat->os_bsize) >> shift;
5524 total = (stat->os_blocks * stat->os_bsize) >> shift;
5527 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5529 if (flags & MNTDF_COOKED) {
5533 cook_val = (double)total;
5536 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5539 snprintf(tbuf, sizeof(tbuf), CDF, total);
5541 cook_val = (double)used;
5544 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5547 snprintf(ubuf, sizeof(ubuf), CDF, used);
5549 cook_val = (double)avail;
5552 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5555 snprintf(abuf, sizeof(abuf), CDF, avail);
5557 snprintf(tbuf, sizeof(tbuf), CDF, total);
5558 snprintf(ubuf, sizeof(tbuf), CDF, used);
5559 snprintf(abuf, sizeof(tbuf), CDF, avail);
5562 sprintf(rbuf, RDF, ratio);
5563 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5564 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5566 printf("[%s:%d]", type, index);
5568 if (stat->os_state) {
5572 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5573 if (oss_names[i].osn_state & stat->os_state &&
5574 (oss_names[i].osn_err ||
5575 flags & MNTDF_VERBOSE))
5576 printf("%c", oss_names[i].osn_name);
5583 printf(UUF": inactive device\n", uuid);
5586 printf(UUF": %s\n", uuid, strerror(-rc));
5593 struct ll_stat_type {
5598 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5600 struct ll_statfs_data {
5602 struct obd_statfs sd_st;
5605 struct ll_statfs_buf {
5607 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5610 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5611 int ops, struct ll_statfs_buf *lsb)
5613 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5614 struct obd_uuid uuid_buf;
5615 char *poolname = NULL;
5616 struct ll_stat_type types[] = {
5617 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5618 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5619 { .st_name = NULL } };
5620 struct ll_stat_type *tp;
5621 __u64 ost_files = 0;
5622 __u64 ost_ffree = 0;
5630 poolname = strchr(pool, '.');
5631 if (poolname != NULL) {
5632 if (strncmp(fsname, pool, strlen(fsname))) {
5633 fprintf(stderr, "filesystem name incorrect\n");
5641 fd = open(mntdir, O_RDONLY);
5644 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5649 if (flags & MNTDF_SHOW) {
5650 if (flags & MNTDF_INODES)
5651 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5652 "UUID", "Inodes", "IUsed", "IFree",
5653 "IUse%", "Mounted on");
5655 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5657 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5658 "Used", "Available", "Use%", "Mounted on");
5661 for (tp = types; tp->st_name != NULL; tp++) {
5662 bool have_ost = false;
5664 if (!(tp->st_op & ops))
5667 for (index = 0; ; index++) {
5668 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5669 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5670 type = flags & MNTDF_LAZY ?
5671 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5672 rc2 = llapi_obd_fstatfs(fd, type, index,
5673 &stat_buf, &uuid_buf);
5678 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5679 if (!(flags & MNTDF_VERBOSE))
5681 } else if (rc2 < 0 && rc == 0) {
5685 /* If we have OSTs then don't report MDT block counts.
5686 * For MDT-only filesystems the expectation is that all
5687 * layouts have a DoM component. For filesystems with
5688 * OSTs, files are not necessarily going to store data
5689 * on MDTs, and MDT space is limited to a fraction of
5690 * OST space, so don't include it in the summary.
5692 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5699 if (poolname && tp->st_op == LL_STATFS_LOV &&
5700 llapi_search_ost(fsname, poolname,
5701 obd_uuid2str(&uuid_buf)) != 1)
5704 /* the llapi_obd_statfs() call may have returned with
5705 * an error, but if it filled in uuid_buf we will at
5706 * lease use that to print out a message for that OBD.
5707 * If we didn't get anything in the uuid_buf, then fill
5708 * it in so that we can print an error message. */
5709 if (uuid_buf.uuid[0] == '\0')
5710 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5711 "%s%04x", tp->st_name, index);
5713 lsb->sb_buf[lsb->sb_count].sd_index = index;
5714 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5717 if (flags & MNTDF_SHOW)
5718 showdf(mntdir, &stat_buf,
5719 obd_uuid2str(&uuid_buf), flags,
5720 tp->st_name, index, rc2);
5725 if (tp->st_op == LL_STATFS_LMV) {
5726 sum.os_ffree += stat_buf.os_ffree;
5727 sum.os_files += stat_buf.os_files;
5728 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5729 ost_files += stat_buf.os_files;
5730 ost_ffree += stat_buf.os_ffree;
5732 sum.os_blocks += stat_buf.os_blocks *
5734 sum.os_bfree += stat_buf.os_bfree *
5736 sum.os_bavail += stat_buf.os_bavail *
5743 /* If we have _some_ OSTs, but don't have as many free objects on the
5744 * OST as inodes on the MDTs, reduce the reported number of inodes
5745 * to compensate, so that the "inodes in use" number is correct.
5746 * This should be kept in sync with ll_statfs_internal().
5748 if (ost_files && ost_ffree < sum.os_ffree) {
5749 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5750 sum.os_ffree = ost_ffree;
5752 if (flags & MNTDF_SHOW) {
5754 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5762 static int lfs_setdirstripe(int argc, char **argv)
5765 struct lfs_setstripe_args lsa = { 0 };
5766 struct llapi_stripe_param *param = NULL;
5767 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5770 char *mode_opt = NULL;
5771 bool default_stripe = false;
5772 bool delete = false;
5773 bool foreign_mode = false;
5774 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5775 mode_t previous_mode = 0;
5777 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5778 struct option long_opts[] = {
5779 { .val = 'c', .name = "count", .has_arg = required_argument },
5780 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5781 { .val = 'd', .name = "delete", .has_arg = no_argument },
5782 { .val = 'D', .name = "default", .has_arg = no_argument },
5783 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5784 { .val = LFS_LAYOUT_FLAGS_OPT,
5785 .name = "flags", .has_arg = required_argument },
5786 { .val = LFS_LAYOUT_FOREIGN_OPT,
5787 .name = "foreign", .has_arg = optional_argument},
5788 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5789 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5790 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5791 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5793 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5794 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5795 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5797 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5798 { .val = 'i', .name = "index", .has_arg = required_argument },
5800 { .val = 'o', .name = "mode", .has_arg = required_argument },
5801 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5802 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5804 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5805 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5806 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5810 setstripe_args_init(&lsa);
5812 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5813 long_opts, NULL)) >= 0) {
5820 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5823 "%s %s: invalid stripe count '%s'\n",
5824 progname, argv[0], optarg);
5830 default_stripe = true;
5833 default_stripe = true;
5835 case LFS_LAYOUT_FOREIGN_OPT:
5836 if (optarg != NULL) {
5837 /* check pure numeric */
5838 type = strtoul(optarg, &end, 0);
5841 type = check_foreign_type_name(optarg);
5842 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5844 "%s %s: unknown foreign type '%s'\n",
5851 foreign_mode = true;
5853 case LFS_LAYOUT_FLAGS_OPT:
5854 flags = strtoul(optarg, &end, 16);
5857 "%s %s: bad flags '%s'\n",
5858 progname, argv[0], optarg);
5862 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5864 fprintf(stderr, "warning: '--hash-type' and '-t' "
5865 "deprecated, use '--mdt-hash' or '-H' instead\n");
5868 lsa.lsa_pattern = check_hashtype(optarg);
5869 if (lsa.lsa_pattern == 0) {
5871 "%s %s: bad stripe hash type '%s'\n",
5872 progname, argv[0], optarg);
5877 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5880 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5881 if (strcmp(argv[optind - 1], "--index") == 0)
5883 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5886 lsa.lsa_nr_tgts = parse_targets(mdts,
5887 sizeof(mdts) / sizeof(__u32),
5888 lsa.lsa_nr_tgts, optarg, NULL);
5889 if (lsa.lsa_nr_tgts < 0) {
5891 "%s %s: invalid MDT target(s) '%s'\n",
5892 progname, argv[0], optarg);
5896 lsa.lsa_tgts = mdts;
5897 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5898 lsa.lsa_stripe_off = mdts[0];
5900 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5902 fprintf(stderr, "warning: '-m' is deprecated, "
5903 "use '--mode' or '-o' instead\n");
5912 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5913 progname, argv[0], argv[optind - 1]);
5918 if (optind == argc) {
5919 fprintf(stderr, "%s %s: DIR must be specified\n",
5924 if (xattr && !foreign_mode) {
5925 /* only print a warning as this is armless and will be
5929 "%s %s: xattr has been specified for non-foreign layout\n",
5931 } else if (foreign_mode && !xattr) {
5933 "%s %s: xattr must be provided in foreign mode\n",
5938 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5939 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5941 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5946 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5947 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5949 "%s %s: stripe offset and count must be specified\n",
5955 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5956 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5958 "%s %s: cannot specify -d with -c or -i options\n",
5963 if (mode_opt != NULL) {
5964 mode = strtoul(mode_opt, &end, 8);
5967 "%s %s: bad MODE '%s'\n",
5968 progname, argv[0], mode_opt);
5971 previous_mode = umask(0);
5974 /* foreign LMV/dir case */
5976 if (argc > optind + 1) {
5978 "%s %s: cannot specify multiple foreign dirs\n",
5983 dname = argv[optind];
5984 result = llapi_dir_create_foreign(dname, mode, type, flags,
5988 "%s mkdir: can't create foreign dir '%s': %s\n",
5989 progname, dname, strerror(-result));
5994 * initialize stripe parameters, in case param is converted to specific,
5995 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5997 param = calloc(1, offsetof(typeof(*param),
5998 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5999 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6000 if (param == NULL) {
6002 "%s %s: cannot allocate memory for parameters: %s\n",
6003 progname, argv[0], strerror(ENOMEM));
6007 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6008 param->lsp_stripe_count = lsa.lsa_stripe_count;
6009 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6010 param->lsp_stripe_offset = -1;
6012 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6013 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6014 param->lsp_stripe_pattern = lsa.lsa_pattern;
6016 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
6017 param->lsp_pool = lsa.lsa_pool_name;
6018 param->lsp_is_specific = false;
6019 if (lsa.lsa_nr_tgts > 1) {
6020 if (lsa.lsa_stripe_count > 0 &&
6021 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6022 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6024 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6025 argv[0], lsa.lsa_stripe_count,
6031 param->lsp_is_specific = true;
6032 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6033 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6036 dname = argv[optind];
6038 if (default_stripe) {
6039 result = llapi_dir_set_default_lmv(dname, param);
6042 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6043 progname, dname, strerror(-result));
6047 result = llapi_dir_create(dname, mode, param);
6050 "%s setdirstripe: cannot create dir '%s': %s\n",
6051 progname, dname, strerror(-result));
6052 } while (!result && (dname = argv[++optind]));
6054 if (mode_opt != NULL)
6055 umask(previous_mode);
6062 static int lfs_rmentry(int argc, char **argv)
6069 fprintf(stderr, "error: %s: missing dirname\n",
6075 dname = argv[index];
6076 while (dname != NULL) {
6077 result = llapi_direntry_remove(dname);
6079 fprintf(stderr, "error: %s: remove dir entry '%s' "
6080 "failed\n", argv[0], dname);
6083 dname = argv[++index];
6088 static int lfs_mv(int argc, char **argv)
6090 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6091 struct find_param param = {
6098 struct option long_opts[] = {
6099 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6100 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6101 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6104 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6106 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6108 fprintf(stderr, "warning: '-M' deprecated"
6109 ", use '--mdt-index' or '-m' instead\n");
6112 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6114 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6120 param.fp_verbose = VERBOSE_DETAIL;
6123 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6124 progname, argv[optind - 1]);
6129 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6130 fprintf(stderr, "%s mv: MDT index must be specified\n",
6135 if (optind >= argc) {
6136 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6141 /* initialize migrate mdt parameters */
6142 param.fp_lmv_md = &lmu;
6143 param.fp_migrate = 1;
6144 rc = llapi_migrate_mdt(argv[optind], ¶m);
6146 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6147 progname, argv[optind], param.fp_mdt_index,
6152 static int lfs_osts(int argc, char **argv)
6154 return lfs_tgts(argc, argv);
6157 static int lfs_mdts(int argc, char **argv)
6159 return lfs_tgts(argc, argv);
6162 static int lfs_df(int argc, char **argv)
6164 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6165 enum mntdf_flags flags = MNTDF_SHOW;
6166 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6167 int c, rc = 0, index = 0;
6168 char fsname[PATH_MAX] = "", *pool_name = NULL;
6169 struct option long_opts[] = {
6170 { .val = 'h', .name = "human-readable",
6171 .has_arg = no_argument },
6172 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6173 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6174 { .val = 'p', .name = "pool", .has_arg = required_argument },
6175 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6178 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6181 flags |= MNTDF_COOKED;
6184 flags |= MNTDF_INODES;
6187 flags |= MNTDF_LAZY;
6193 flags |= MNTDF_VERBOSE;
6199 if (optind < argc && !realpath(argv[optind], path)) {
6201 fprintf(stderr, "error: invalid path '%s': %s\n",
6202 argv[optind], strerror(-rc));
6206 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6207 /* Check if we have a mount point */
6208 if (mntdir[0] == '\0')
6211 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6212 if (rc || path[0] != '\0')
6214 fsname[0] = '\0'; /* avoid matching in next loop */
6215 mntdir[0] = '\0'; /* avoid matching in next loop */
6221 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6222 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6226 if (opt_fsname == opt_instance) { /* both true or both false */
6227 rc = llapi_getname(mntdir, buf, buflen);
6228 } else if (opt_fsname) {
6229 /* llapi_search_mounts() fills @buf with fsname, but that is not
6230 * called if explicit paths are specified on the command-line
6233 rc = llapi_get_fsname(mntdir, buf, buflen);
6234 } else /* if (opt_instance) */ {
6235 rc = llapi_get_instance(mntdir, buf, buflen);
6239 fprintf(stderr, "cannot get instance for '%s': %s\n",
6240 mntdir, strerror(-rc));
6245 printf("%s %s\n", buf, mntdir);
6247 printf("%s\n", buf);
6252 static int lfs_getname(int argc, char **argv)
6254 struct option long_opts[] = {
6255 { .val = 'h', .name = "help", .has_arg = no_argument },
6256 { .val = 'i', .name = "instance", .has_arg = no_argument },
6257 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6259 bool opt_instance = false, opt_fsname = false;
6260 char fsname[PATH_MAX] = "";
6263 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6266 opt_instance = true;
6277 if (optind == argc) { /* no paths specified, get all paths. */
6278 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6281 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6282 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6283 opt_instance, opt_fsname, true);
6286 path[0] = fsname[0] = mntdir[0] = '\0';
6288 } else { /* paths specified, only attempt to search these. */
6291 /* if only one path is given, print only requested info */
6292 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6294 for (; optind < argc; optind++) {
6295 rc2 = print_instance(argv[optind], fsname,
6296 sizeof(fsname), opt_instance,
6297 opt_fsname, opt_mntdir);
6307 static int lfs_check(int argc, char **argv)
6309 char mntdir[PATH_MAX] = {'\0'};
6318 fprintf(stderr, "%s check: server type must be specified\n",
6323 obd_types[0] = obd_type1;
6324 obd_types[1] = obd_type2;
6325 obd_types[2] = obd_type3;
6327 if (strcmp(argv[1], "osts") == 0) {
6328 strcpy(obd_types[0], "osc");
6329 } else if (strcmp(argv[1], "mdts") == 0 ||
6330 strcmp(argv[1], "mds") == 0) {
6331 strcpy(obd_types[0], "mdc");
6332 } else if (strcmp(argv[1], "mgts") == 0) {
6333 strcpy(obd_types[0], "mgc");
6334 } else if (strcmp(argv[1], "all") == 0 ||
6335 strcmp(argv[1], "servers") == 0) {
6337 strcpy(obd_types[0], "osc");
6338 strcpy(obd_types[1], "mdc");
6339 strcpy(obd_types[2], "mgc");
6341 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6346 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6347 if (rc < 0 || mntdir[0] == '\0') {
6349 "%s check: cannot find mounted Lustre filesystem: %s\n",
6350 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6354 rc = llapi_target_check(num_types, obd_types, mntdir);
6356 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6357 progname, argv[1], strerror(-rc));
6363 #ifdef HAVE_SYS_QUOTA_H
6364 #define ARG2INT(nr, str, msg) \
6367 nr = strtol(str, &endp, 0); \
6368 if (*endp != '\0') { \
6369 fprintf(stderr, "%s: bad %s '%s'\n", \
6370 progname, msg, str); \
6375 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6377 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6378 * returns the value or ULONG_MAX on integer overflow or incorrect format
6380 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6381 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6382 * 3. empty integer value is interpreted as 0
6384 static unsigned long str2sec(const char* timestr)
6386 const char spec[] = "smhdw";
6387 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6388 unsigned long val = 0;
6391 if (strpbrk(timestr, spec) == NULL) {
6392 /* no specifiers inside the time string,
6393 should treat it as an integer value */
6394 val = strtoul(timestr, &tail, 10);
6395 return *tail ? ULONG_MAX : val;
6398 /* format string is XXwXXdXXhXXmXXs */
6404 v = strtoul(timestr, &tail, 10);
6405 if (v == ULONG_MAX || *tail == '\0')
6406 /* value too large (ULONG_MAX or more)
6407 or missing specifier */
6410 ptr = strchr(spec, *tail);
6412 /* unknown specifier */
6417 /* check if product will overflow the type */
6418 if (!(v < ULONG_MAX / mult[ind]))
6421 ADD_OVERFLOW(val, mult[ind] * v);
6422 if (val == ULONG_MAX)
6434 #define ARG2ULL(nr, str, def_units) \
6436 unsigned long long limit, units = def_units; \
6439 rc = llapi_parse_size(str, &limit, &units, 1); \
6441 fprintf(stderr, "%s: invalid limit '%s'\n", \
6448 static inline int has_times_option(int argc, char **argv)
6452 for (i = 1; i < argc; i++)
6453 if (!strcmp(argv[i], "-t"))
6459 int lfs_setquota_times(int argc, char **argv)
6462 struct if_quotactl qctl;
6463 char *mnt, *obd_type = (char *)qctl.obd_type;
6464 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6465 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6466 struct option long_opts[] = {
6467 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6468 { .val = 'g', .name = "group", .has_arg = no_argument },
6469 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6470 { .val = 'p', .name = "projid", .has_arg = no_argument },
6471 { .val = 't', .name = "times", .has_arg = no_argument },
6472 { .val = 'u', .name = "user", .has_arg = no_argument },
6476 memset(&qctl, 0, sizeof(qctl));
6477 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6478 qctl.qc_type = ALLQUOTA;
6480 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6481 long_opts, NULL)) != -1) {
6492 if (qctl.qc_type != ALLQUOTA) {
6493 fprintf(stderr, "error: -u/g/p can't be used "
6494 "more than once\n");
6497 qctl.qc_type = qtype;
6500 if (strncmp(optarg, NOTIFY_GRACE,
6501 strlen(NOTIFY_GRACE)) == 0) {
6502 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6504 dqi->dqi_bgrace = str2sec(optarg);
6505 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6506 fprintf(stderr, "error: bad "
6507 "block-grace: %s\n", optarg);
6511 dqb->dqb_valid |= QIF_BTIME;
6514 if (strncmp(optarg, NOTIFY_GRACE,
6515 strlen(NOTIFY_GRACE)) == 0) {
6516 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6518 dqi->dqi_igrace = str2sec(optarg);
6519 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6520 fprintf(stderr, "error: bad "
6521 "inode-grace: %s\n", optarg);
6525 dqb->dqb_valid |= QIF_ITIME;
6527 case 't': /* Yes, of course! */
6529 /* getopt prints error message for us when opterr != 0 */
6535 if (qctl.qc_type == ALLQUOTA) {
6536 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6540 if (optind != argc - 1) {
6541 fprintf(stderr, "error: unexpected parameters encountered\n");
6546 rc = llapi_quotactl(mnt, &qctl);
6549 fprintf(stderr, "%s %s ", obd_type,
6550 obd_uuid2str(&qctl.obd_uuid));
6551 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6558 #define BSLIMIT (1 << 0)
6559 #define BHLIMIT (1 << 1)
6560 #define ISLIMIT (1 << 2)
6561 #define IHLIMIT (1 << 3)
6563 int lfs_setquota(int argc, char **argv)
6566 struct if_quotactl qctl;
6567 char *mnt, *obd_type = (char *)qctl.obd_type;
6568 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6569 struct option long_opts[] = {
6570 { .val = 'b', .name = "block-softlimit",
6571 .has_arg = required_argument },
6572 { .val = 'B', .name = "block-hardlimit",
6573 .has_arg = required_argument },
6574 { .val = 'd', .name = "default", .has_arg = no_argument },
6575 { .val = 'g', .name = "group", .has_arg = required_argument },
6576 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6577 { .val = 'i', .name = "inode-softlimit",
6578 .has_arg = required_argument },
6579 { .val = 'I', .name = "inode-hardlimit",
6580 .has_arg = required_argument },
6581 { .val = 'p', .name = "projid", .has_arg = required_argument },
6582 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6583 { .val = 'u', .name = "user", .has_arg = required_argument },
6584 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6586 unsigned limit_mask = 0;
6588 bool use_default = false;
6591 if (has_times_option(argc, argv))
6592 return lfs_setquota_times(argc, argv);
6594 memset(&qctl, 0, sizeof(qctl));
6595 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6596 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6597 * so it can be used as a marker that qc_type
6598 * isn't reinitialized from command line */
6600 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6601 long_opts, NULL)) != -1) {
6604 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6607 goto quota_type_def;
6610 rc = name2uid(&qctl.qc_id, optarg);
6613 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6616 goto quota_type_def;
6619 rc = name2gid(&qctl.qc_id, optarg);
6622 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6625 goto quota_type_def;
6628 rc = name2projid(&qctl.qc_id, optarg);
6631 qctl.qc_id = strtoul(optarg, &endptr, 10);
6632 if (*endptr != '\0') {
6633 fprintf(stderr, "%s setquota: invalid"
6634 " id '%s'\n", progname, optarg);
6639 if (qctl.qc_id == 0) {
6640 fprintf(stderr, "%s setquota: can't set quota"
6641 " for root usr/group/project.\n",
6647 if (qctl.qc_type != ALLQUOTA) {
6649 "%s setquota: only one of -u, -U, -g,"
6650 " -G, -p or -P may be specified\n",
6654 qctl.qc_type = qtype;
6657 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6661 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6662 dqb->dqb_bsoftlimit >>= 10;
6663 limit_mask |= BSLIMIT;
6664 if (dqb->dqb_bsoftlimit &&
6665 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6667 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6668 "See '%s help setquota' or Lustre manual for details\n",
6670 (unsigned long long)dqb->dqb_bsoftlimit,
6674 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6675 dqb->dqb_bhardlimit >>= 10;
6676 limit_mask |= BHLIMIT;
6677 if (dqb->dqb_bhardlimit &&
6678 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6680 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6681 "See '%s help setquota' or Lustre manual for details\n",
6683 (unsigned long long)dqb->dqb_bhardlimit,
6687 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6688 limit_mask |= ISLIMIT;
6689 if (dqb->dqb_isoftlimit &&
6690 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6692 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6693 "See '%s help setquota' or Lustre manual for details\n",
6695 (unsigned long long)dqb->dqb_isoftlimit,
6699 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6700 limit_mask |= IHLIMIT;
6701 if (dqb->dqb_ihardlimit &&
6702 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6704 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6705 "See '%s help setquota' or Lustre manual for details\n",
6707 (unsigned long long)dqb->dqb_ihardlimit,
6712 "%s setquota: unrecognized option '%s'\n",
6713 progname, argv[optind - 1]);
6718 if (qctl.qc_type == ALLQUOTA) {
6720 "%s setquota: either -u or -g must be specified\n",
6725 if (!use_default && limit_mask == 0) {
6727 "%s setquota: at least one limit must be specified\n",
6732 if (use_default && limit_mask != 0) {
6734 "%s setquota: limits should not be specified when"
6735 " using default quota\n",
6740 if (use_default && qctl.qc_id == 0) {
6742 "%s setquota: can not set default quota for root"
6743 " user/group/project\n",
6748 if (optind != argc - 1) {
6750 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6751 progname, argv[optind]);
6758 dqb->dqb_bhardlimit = 0;
6759 dqb->dqb_bsoftlimit = 0;
6760 dqb->dqb_ihardlimit = 0;
6761 dqb->dqb_isoftlimit = 0;
6764 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6765 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6766 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6767 /* sigh, we can't just set blimits/ilimits */
6768 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6769 .qc_type = qctl.qc_type,
6770 .qc_id = qctl.qc_id};
6772 rc = llapi_quotactl(mnt, &tmp_qctl);
6776 if (!(limit_mask & BHLIMIT))
6777 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6778 if (!(limit_mask & BSLIMIT))
6779 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6780 if (!(limit_mask & IHLIMIT))
6781 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6782 if (!(limit_mask & ISLIMIT))
6783 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6785 /* Keep grace times if we have got no softlimit arguments */
6786 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6787 dqb->dqb_valid |= QIF_BTIME;
6788 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6791 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6792 dqb->dqb_valid |= QIF_ITIME;
6793 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6797 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6798 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6800 rc = llapi_quotactl(mnt, &qctl);
6804 "%s setquota: cannot quotactl '%s' '%s': %s",
6806 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6813 /* Converts seconds value into format string
6814 * result is returned in buf
6816 * 1. result is in descenting order: 1w2d3h4m5s
6817 * 2. zero fields are not filled (except for p. 3): 5d1s
6818 * 3. zero seconds value is presented as "0s"
6820 static char * __sec2str(time_t seconds, char *buf)
6822 const char spec[] = "smhdw";
6823 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6828 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
6829 c = seconds / mult[i];
6831 if (c > 0 || (i == 0 && buf == tail))
6832 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
6841 static void sec2str(time_t seconds, char *buf, int rc)
6848 tail = __sec2str(seconds, tail);
6850 if (rc && tail - buf < 39) {
6856 static void diff2str(time_t seconds, char *buf, time_t now)
6862 if (seconds <= now) {
6863 strcpy(buf, "none");
6866 __sec2str(seconds - now, buf);
6869 static void print_quota_title(char *name, struct if_quotactl *qctl,
6870 bool human_readable, bool show_default)
6873 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6874 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6875 "Filesystem", "bquota", "blimit", "bgrace",
6876 "iquota", "ilimit", "igrace");
6878 printf("Disk quotas for %s %s (%cid %u):\n",
6879 qtype_name(qctl->qc_type), name,
6880 *qtype_name(qctl->qc_type), qctl->qc_id);
6881 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6882 "Filesystem", human_readable ? "used" : "kbytes",
6883 "quota", "limit", "grace",
6884 "files", "quota", "limit", "grace");
6888 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6891 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6894 snprintf(buf, buflen, "%5.4gP",
6895 (double)num / ((__u64)1 << 40));
6897 snprintf(buf, buflen, "%5.4gT",
6898 (double)num / (1 << 30));
6900 snprintf(buf, buflen, "%5.4gG",
6901 (double)num / (1 << 20));
6903 snprintf(buf, buflen, "%5.4gM",
6904 (double)num / (1 << 10));
6906 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6910 #define STRBUF_LEN 32
6911 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6912 int rc, bool h, bool show_default)
6918 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6919 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6920 int bover = 0, iover = 0;
6921 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6922 char numbuf[3][STRBUF_LEN];
6924 char strbuf[STRBUF_LEN];
6926 if (dqb->dqb_bhardlimit &&
6927 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6929 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6930 if (dqb->dqb_btime > now) {
6937 if (dqb->dqb_ihardlimit &&
6938 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6940 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6941 if (dqb->dqb_itime > now) {
6949 if (strlen(mnt) > 15)
6950 printf("%s\n%15s", mnt, "");
6952 printf("%15s", mnt);
6955 diff2str(dqb->dqb_btime, timebuf, now);
6956 else if (show_default)
6957 snprintf(timebuf, sizeof(timebuf), "%llu",
6958 (unsigned long long)dqb->dqb_btime);
6960 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6961 strbuf, sizeof(strbuf), h);
6962 if (rc == -EREMOTEIO)
6963 sprintf(numbuf[0], "%s*", strbuf);
6965 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6966 "%s" : "[%s]", strbuf);
6968 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6969 if (type == QC_GENERAL)
6970 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6971 "%s" : "[%s]", strbuf);
6973 sprintf(numbuf[1], "%s", "-");
6975 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6976 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6977 "%s" : "[%s]", strbuf);
6980 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6982 printf(" %7s%c %6s %7s %7s",
6983 numbuf[0], bover ? '*' : ' ', numbuf[1],
6984 numbuf[2], bover > 1 ? timebuf : "-");
6988 diff2str(dqb->dqb_itime, timebuf, now);
6989 else if (show_default)
6990 snprintf(timebuf, sizeof(timebuf), "%llu",
6991 (unsigned long long)dqb->dqb_itime);
6993 snprintf(numbuf[0], sizeof(numbuf),
6994 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6995 (uintmax_t)dqb->dqb_curinodes);
6997 if (type == QC_GENERAL)
6998 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7000 (uintmax_t)dqb->dqb_isoftlimit);
7002 sprintf(numbuf[1], "%s", "-");
7004 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7005 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7008 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7009 else if (type != QC_OSTIDX)
7010 printf(" %7s%c %6s %7s %7s",
7011 numbuf[0], iover ? '*' : ' ', numbuf[1],
7012 numbuf[2], iover > 1 ? timebuf : "-");
7014 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7016 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
7017 qctl->qc_cmd == Q_GETOINFO) {
7021 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7022 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7024 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7025 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7026 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7028 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7030 printf("Block grace time: %s; Inode grace time: %s\n",
7031 bgtimebuf, igtimebuf);
7035 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7036 bool h, __u64 *total)
7038 int rc = 0, rc1 = 0, count = 0;
7039 __u32 valid = qctl->qc_valid;
7041 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7043 fprintf(stderr, "can not get %s count: %s\n",
7044 is_mdt ? "mdt": "ost", strerror(-rc));
7048 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
7049 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7050 rc = llapi_quotactl(mnt, qctl);
7052 /* It is remote client case. */
7053 if (rc == -EOPNOTSUPP) {
7060 fprintf(stderr, "quotactl %s%d failed.\n",
7061 is_mdt ? "mdt": "ost", qctl->qc_idx);
7065 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7066 qctl->qc_valid, 0, h, false);
7067 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7068 qctl->qc_dqblk.dqb_bhardlimit;
7071 qctl->qc_valid = valid;
7075 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7076 int verbose, int quiet, bool human_readable,
7079 int rc1 = 0, rc2 = 0, rc3 = 0;
7080 char *obd_type = (char *)qctl->obd_type;
7081 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7082 __u64 total_ialloc = 0, total_balloc = 0;
7083 bool use_default_for_blk = false;
7084 bool use_default_for_file = false;
7087 rc1 = llapi_quotactl(mnt, qctl);
7091 fprintf(stderr, "%s quotas are not enabled.\n",
7092 qtype_name(qctl->qc_type));
7095 fprintf(stderr, "Permission denied.\n");
7098 /* We already got error message. */
7101 fprintf(stderr, "Unexpected quotactl error: %s\n",
7106 if (!show_default && qctl->qc_id == 0) {
7107 qctl->qc_dqblk.dqb_bhardlimit = 0;
7108 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7109 qctl->qc_dqblk.dqb_ihardlimit = 0;
7110 qctl->qc_dqblk.dqb_isoftlimit = 0;
7111 qctl->qc_dqblk.dqb_btime = 0;
7112 qctl->qc_dqblk.dqb_itime = 0;
7113 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7116 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7117 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7118 use_default_for_blk = true;
7119 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7122 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7123 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7124 use_default_for_file = true;
7125 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7128 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7129 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7130 print_quota_title(name, qctl, human_readable, show_default);
7132 if (rc1 && *obd_type)
7133 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7135 if (qctl->qc_valid != QC_GENERAL)
7138 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
7139 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7140 (QIF_LIMITS|QIF_USAGE));
7142 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7144 if (!show_default && verbose &&
7145 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
7146 char strbuf[STRBUF_LEN];
7148 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7150 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7152 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7154 printf("Total allocated inode limit: %ju, total "
7155 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
7159 if (use_default_for_blk)
7160 printf("%cid %u is using default block quota setting\n",
7161 *qtype_name(qctl->qc_type), qctl->qc_id);
7163 if (use_default_for_file)
7164 printf("%cid %u is using default file quota setting\n",
7165 *qtype_name(qctl->qc_type), qctl->qc_id);
7167 if (rc1 || rc2 || rc3 || inacc)
7168 printf("Some errors happened when getting quota info. "
7169 "Some devices may be not working or deactivated. "
7170 "The data in \"[]\" is inaccurate.\n");
7184 static int lfs_project(int argc, char **argv)
7186 int ret = 0, err = 0, c, i;
7187 struct project_handle_control phc = { 0 };
7188 enum lfs_project_ops_t op;
7191 phc.assign_projid = false;
7192 /* default action */
7193 op = LFS_PROJECT_LIST;
7195 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7198 if (op != LFS_PROJECT_LIST) {
7200 "%s: cannot specify '-c' '-C' '-s' together\n",
7205 op = LFS_PROJECT_CHECK;
7208 if (op != LFS_PROJECT_LIST) {
7210 "%s: cannot specify '-c' '-C' '-s' together\n",
7215 op = LFS_PROJECT_CLEAR;
7218 if (op != LFS_PROJECT_LIST) {
7220 "%s: cannot specify '-c' '-C' '-s' together\n",
7225 phc.set_inherit = true;
7226 op = LFS_PROJECT_SET;
7232 phc.keep_projid = true;
7235 phc.recursive = true;
7238 phc.projid = strtoul(optarg, NULL, 0);
7239 phc.assign_projid = true;
7243 phc.newline = false;
7246 fprintf(stderr, "%s: invalid option '%c'\n",
7252 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7253 op = LFS_PROJECT_SET;
7254 phc.set_projid = true;
7255 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7256 phc.set_projid = true;
7260 case LFS_PROJECT_CHECK:
7261 if (phc.keep_projid) {
7263 "%s: '-k' is useless together with '-c'\n",
7268 case LFS_PROJECT_CLEAR:
7271 "%s: '-0' is useless together with '-C'\n",
7275 if (phc.assign_projid) {
7277 "%s: '-p' is useless together with '-C'\n",
7282 case LFS_PROJECT_SET:
7285 "%s: '-0' is useless together with '-s'\n",
7289 if (phc.keep_projid) {
7291 "%s: '-k' is useless together with '-s'\n",
7299 "%s: '-0' is useless for list operations\n",
7309 fprintf(stderr, "%s: missing file or directory target(s)\n",
7314 for (i = 0; i < argc; i++) {
7316 case LFS_PROJECT_CHECK:
7317 err = lfs_project_check(argv[i], &phc);
7319 case LFS_PROJECT_LIST:
7320 err = lfs_project_list(argv[i], &phc);
7322 case LFS_PROJECT_CLEAR:
7323 err = lfs_project_clear(argv[i], &phc);
7325 case LFS_PROJECT_SET:
7326 err = lfs_project_set(argv[i], &phc);
7338 static int lfs_quota(int argc, char **argv)
7341 char *mnt, *name = NULL;
7342 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
7343 .qc_type = ALLQUOTA };
7344 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
7345 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7347 __u32 valid = QC_GENERAL, idx = 0;
7348 bool human_readable = false;
7349 bool show_default = false;
7352 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
7355 show_default = true;
7360 show_default = true;
7365 show_default = true;
7369 if (qctl.qc_type != ALLQUOTA) {
7371 "%s quota: only one of -u, -g, or -p may be specified\n",
7375 qctl.qc_type = qtype;
7378 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7381 valid = qctl.qc_valid = QC_UUID;
7382 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7385 valid = qctl.qc_valid = QC_MDTIDX;
7386 idx = qctl.qc_idx = atoi(optarg);
7387 if (idx == 0 && *optarg != '0') {
7389 "%s quota: invalid MDT index '%s'\n",
7395 valid = qctl.qc_valid = QC_OSTIDX;
7396 idx = qctl.qc_idx = atoi(optarg);
7397 if (idx == 0 && *optarg != '0') {
7399 "%s quota: invalid OST index '%s'\n",
7411 human_readable = true;
7414 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7415 progname, argv[optind - 1]);
7420 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7421 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7422 optind == argc - 1 && !show_default) {
7424 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7427 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7428 qctl.qc_type = qtype;
7429 qctl.qc_valid = valid;
7430 if (qtype == USRQUOTA) {
7431 qctl.qc_id = geteuid();
7432 rc = uid2name(&name, qctl.qc_id);
7434 qctl.qc_id = getegid();
7435 rc = gid2name(&name, qctl.qc_id);
7436 memset(&qctl.qc_dqblk, 0,
7437 sizeof(qctl.qc_dqblk));
7442 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7443 human_readable, show_default);
7448 /* lfs quota -u username /path/to/lustre/mount */
7449 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7450 /* options should be followed by u/g-name and mntpoint */
7451 if ((!show_default && optind + 2 != argc) ||
7452 (show_default && optind + 1 != argc) ||
7453 qctl.qc_type == ALLQUOTA) {
7455 "%s quota: name and mount point must be specified\n",
7460 if (!show_default) {
7461 name = argv[optind++];
7462 switch (qctl.qc_type) {
7464 rc = name2uid(&qctl.qc_id, name);
7467 rc = name2gid(&qctl.qc_id, name);
7470 rc = name2projid(&qctl.qc_id, name);
7477 qctl.qc_valid = QC_GENERAL;
7478 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7483 qctl.qc_id = strtoul(name, &endptr, 10);
7484 if (*endptr != '\0') {
7485 fprintf(stderr, "%s quota: invalid id '%s'\n",
7490 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7491 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7497 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7498 human_readable, show_default);
7501 #endif /* HAVE_SYS_QUOTA_H! */
7503 static int flushctx_ioctl(char *mp)
7507 fd = open(mp, O_RDONLY);
7509 fprintf(stderr, "flushctx: error open %s: %s\n",
7510 mp, strerror(errno));
7514 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7516 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7517 mp, strerror(errno));
7523 static int lfs_flushctx(int argc, char **argv)
7525 int kdestroy = 0, c;
7526 char mntdir[PATH_MAX] = {'\0'};
7530 while ((c = getopt(argc, argv, "k")) != -1) {
7536 fprintf(stderr, "error: %s: option '-%c' "
7537 "unrecognized\n", argv[0], c);
7543 if ((rc = system("kdestroy > /dev/null")) != 0) {
7544 rc = WEXITSTATUS(rc);
7545 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7549 if (optind >= argc) {
7550 /* flush for all mounted lustre fs. */
7551 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7552 /* Check if we have a mount point */
7553 if (mntdir[0] == '\0')
7556 if (flushctx_ioctl(mntdir))
7559 mntdir[0] = '\0'; /* avoid matching in next loop */
7562 /* flush fs as specified */
7563 while (optind < argc) {
7564 if (flushctx_ioctl(argv[optind++]))
7571 static int lfs_changelog(int argc, char **argv)
7573 void *changelog_priv;
7574 struct changelog_rec *rec;
7575 long long startrec = 0, endrec = 0;
7577 struct option long_opts[] = {
7578 { .val = 'f', .name = "follow", .has_arg = no_argument },
7580 char short_opts[] = "f";
7583 while ((rc = getopt_long(argc, argv, short_opts,
7584 long_opts, NULL)) != -1) {
7591 "%s changelog: unrecognized option '%s'\n",
7592 progname, argv[optind - 1]);
7596 if (optind >= argc) {
7597 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7602 mdd = argv[optind++];
7604 startrec = strtoll(argv[optind++], NULL, 10);
7606 endrec = strtoll(argv[optind++], NULL, 10);
7608 rc = llapi_changelog_start(&changelog_priv,
7609 CHANGELOG_FLAG_BLOCK |
7610 CHANGELOG_FLAG_JOBID |
7611 CHANGELOG_FLAG_EXTRA_FLAGS |
7612 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7615 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7616 progname, strerror(errno = -rc));
7620 rc = llapi_changelog_set_xflags(changelog_priv,
7621 CHANGELOG_EXTRA_FLAG_UIDGID |
7622 CHANGELOG_EXTRA_FLAG_NID |
7623 CHANGELOG_EXTRA_FLAG_OMODE |
7624 CHANGELOG_EXTRA_FLAG_XATTR);
7627 "%s changelog: cannot set xflags for changelog: %s\n",
7628 progname, strerror(errno = -rc));
7632 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7636 if (endrec && rec->cr_index > endrec) {
7637 llapi_changelog_free(&rec);
7640 if (rec->cr_index < startrec) {
7641 llapi_changelog_free(&rec);
7645 secs = rec->cr_time >> 30;
7646 gmtime_r(&secs, &ts);
7647 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7648 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7649 changelog_type2str(rec->cr_type),
7650 ts.tm_hour, ts.tm_min, ts.tm_sec,
7651 (int)(rec->cr_time & ((1 << 30) - 1)),
7652 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7653 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7655 if (rec->cr_flags & CLF_JOBID) {
7656 struct changelog_ext_jobid *jid =
7657 changelog_rec_jobid(rec);
7659 if (jid->cr_jobid[0] != '\0')
7660 printf(" j=%s", jid->cr_jobid);
7663 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7664 struct changelog_ext_extra_flags *ef =
7665 changelog_rec_extra_flags(rec);
7667 printf(" ef=0x%llx",
7668 (unsigned long long)ef->cr_extra_flags);
7670 if (ef->cr_extra_flags & CLFE_UIDGID) {
7671 struct changelog_ext_uidgid *uidgid =
7672 changelog_rec_uidgid(rec);
7674 printf(" u=%llu:%llu",
7675 (unsigned long long)uidgid->cr_uid,
7676 (unsigned long long)uidgid->cr_gid);
7678 if (ef->cr_extra_flags & CLFE_NID) {
7679 struct changelog_ext_nid *nid =
7680 changelog_rec_nid(rec);
7683 libcfs_nid2str(nid->cr_nid));
7686 if (ef->cr_extra_flags & CLFE_OPEN) {
7687 struct changelog_ext_openmode *omd =
7688 changelog_rec_openmode(rec);
7689 char mode[] = "---";
7691 /* exec mode must be exclusive */
7692 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7695 if (omd->cr_openflags & MDS_FMODE_READ)
7697 if (omd->cr_openflags &
7704 if (strcmp(mode, "---") != 0)
7705 printf(" m=%s", mode);
7709 if (ef->cr_extra_flags & CLFE_XATTR) {
7710 struct changelog_ext_xattr *xattr =
7711 changelog_rec_xattr(rec);
7713 if (xattr->cr_xattr[0] != '\0')
7714 printf(" x=%s", xattr->cr_xattr);
7718 if (rec->cr_namelen)
7719 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7720 rec->cr_namelen, changelog_rec_name(rec));
7722 if (rec->cr_flags & CLF_RENAME) {
7723 struct changelog_ext_rename *rnm =
7724 changelog_rec_rename(rec);
7726 if (!fid_is_zero(&rnm->cr_sfid))
7727 printf(" s="DFID" sp="DFID" %.*s",
7728 PFID(&rnm->cr_sfid),
7729 PFID(&rnm->cr_spfid),
7730 (int)changelog_rec_snamelen(rec),
7731 changelog_rec_sname(rec));
7735 llapi_changelog_free(&rec);
7738 llapi_changelog_fini(&changelog_priv);
7741 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7742 progname, strerror(errno = -rc));
7744 return (rc == 1 ? 0 : rc);
7747 static int lfs_changelog_clear(int argc, char **argv)
7755 endrec = strtoll(argv[3], NULL, 10);
7757 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7760 fprintf(stderr, "%s: record out of range: %llu\n",
7762 else if (rc == -ENOENT)
7763 fprintf(stderr, "%s: no changelog user: %s\n",
7766 fprintf(stderr, "%s error: %s\n", argv[0],
7775 static int lfs_fid2path(int argc, char **argv)
7777 struct option long_opts[] = {
7778 { .val = 'c', .name = "cur", .has_arg = no_argument },
7779 { .val = 'l', .name = "link", .has_arg = required_argument },
7780 { .val = 'r', .name = "rec", .has_arg = required_argument },
7782 char short_opts[] = "cl:r:";
7783 char *device, *fid, *path;
7784 long long recno = -1;
7789 char *endptr = NULL;
7791 while ((rc = getopt_long(argc, argv, short_opts,
7792 long_opts, NULL)) != -1) {
7798 linkno = strtol(optarg, &endptr, 10);
7799 if (*endptr != '\0') {
7801 "%s fid2path: invalid linkno '%s'\n",
7807 recno = strtoll(optarg, &endptr, 10);
7808 if (*endptr != '\0') {
7810 "%s fid2path: invalid recno '%s'\n",
7817 "%s fid2path: unrecognized option '%s'\n",
7818 progname, argv[optind - 1]);
7825 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7830 device = argv[optind++];
7831 path = calloc(1, PATH_MAX);
7835 "%s fid2path: cannot allocate memory for path: %s\n",
7836 progname, strerror(-rc));
7841 while (optind < argc) {
7842 fid = argv[optind++];
7844 lnktmp = (linkno >= 0) ? linkno : 0;
7846 int oldtmp = lnktmp;
7847 long long rectmp = recno;
7849 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7853 "%s fid2path: cannot find '%s': %s\n",
7854 progname, fid, strerror(errno = -rc2));
7861 fprintf(stdout, "%lld ", rectmp);
7862 if (device[0] == '/') {
7863 fprintf(stdout, "%s", device);
7864 if (device[strlen(device) - 1] != '/')
7865 fprintf(stdout, "/");
7866 } else if (path[0] == '\0') {
7867 fprintf(stdout, "/");
7869 fprintf(stdout, "%s\n", path);
7872 /* specified linkno */
7874 if (oldtmp == lnktmp)
7884 static int lfs_path2fid(int argc, char **argv)
7886 struct option long_opts[] = {
7887 { .val = 'p', .name = "parents", .has_arg = no_argument },
7890 const char short_opts[] = "p";
7891 const char *sep = "";
7894 bool show_parents = false;
7896 while ((rc = getopt_long(argc, argv, short_opts,
7897 long_opts, NULL)) != -1) {
7900 show_parents = true;
7904 "%s path2fid: unrecognized option '%s'\n",
7905 progname, argv[optind - 1]);
7910 if (optind > argc - 1) {
7911 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7915 else if (optind < argc - 1)
7919 for (path = argv + optind; *path != NULL; path++) {
7921 if (!show_parents) {
7922 err = llapi_path2fid(*path, &fid);
7924 printf("%s%s"DFID"\n",
7925 *sep != '\0' ? *path : "", sep,
7928 char name[NAME_MAX + 1];
7929 unsigned int linkno = 0;
7931 while ((err = llapi_path2parent(*path, linkno, &fid,
7932 name, sizeof(name))) == 0) {
7933 if (*sep != '\0' && linkno == 0)
7934 printf("%s%s", *path, sep);
7936 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7941 /* err == -ENODATA is end-of-loop */
7942 if (linkno > 0 && err == -ENODATA) {
7950 "%s path2fid: cannot get %sfid for '%s': %s\n",
7951 progname, show_parents ? "parent " : "", *path,
7963 #define MAX_ERRNO 4095
7964 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
7966 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
7970 rc = llapi_rmfid(device, fa);
7972 fprintf(stderr, "rmfid(): rc = %d\n", rc);
7976 for (k = 0; k < fa->fa_nr; k++) {
7977 rc = (__s32)fa->fa_fids[k].f_ver;
7978 if (!IS_ERR_VALUE(rc))
7984 fa->fa_fids[k].f_ver = 0;
7985 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
7986 PFID(&fa->fa_fids[k]), rc);
7992 static int lfs_rmfid(int argc, char **argv)
7994 char *fidstr, *device;
7995 int rc = 0, rc2, nr;
7996 struct fid_array *fa;
7998 if (optind > argc - 1) {
7999 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8003 device = argv[optind++];
8006 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8012 while (optind < argc) {
8015 fidstr = argv[optind++];
8016 while (*fidstr == '[')
8018 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8020 fprintf(stderr, "unrecognized FID: %s\n",
8025 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8026 /* start another batch */
8027 rc2 = lfs_rmfid_and_show_errors(device, fa);
8034 rc2 = lfs_rmfid_and_show_errors(device, fa);
8042 static int lfs_data_version(int argc, char **argv)
8049 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8052 fprintf(stderr, "%s data_version: FILE must be specified\n",
8057 while ((c = getopt(argc, argv, "nrw")) != -1) {
8060 data_version_flags = 0;
8063 data_version_flags |= LL_DV_RD_FLUSH;
8066 data_version_flags |= LL_DV_WR_FLUSH;
8070 "%s data_version: unrecognized option '%s'\n",
8071 progname, argv[optind - 1]);
8075 if (optind == argc) {
8076 fprintf(stderr, "%s data_version: FILE must be specified\n",
8081 path = argv[optind];
8082 fd = open(path, O_RDONLY);
8085 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8086 progname, path, strerror(-rc));
8090 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8093 "%s data_version: cannot get version for '%s': %s\n",
8094 progname, path, strerror(-rc));
8096 printf("%ju" "\n", (uintmax_t)data_version);
8102 static int lfs_hsm_state(int argc, char **argv)
8107 struct hsm_user_state hus;
8115 rc = llapi_hsm_state_get(path, &hus);
8117 fprintf(stderr, "can't get hsm state for %s: %s\n",
8118 path, strerror(errno = -rc));
8122 /* Display path name and status flags */
8123 printf("%s: (0x%08x)", path, hus.hus_states);
8125 if (hus.hus_states & HS_RELEASED)
8126 printf(" released");
8127 if (hus.hus_states & HS_EXISTS)
8129 if (hus.hus_states & HS_DIRTY)
8131 if (hus.hus_states & HS_ARCHIVED)
8132 printf(" archived");
8133 /* Display user-settable flags */
8134 if (hus.hus_states & HS_NORELEASE)
8135 printf(" never_release");
8136 if (hus.hus_states & HS_NOARCHIVE)
8137 printf(" never_archive");
8138 if (hus.hus_states & HS_LOST)
8139 printf(" lost_from_hsm");
8141 if (hus.hus_archive_id != 0)
8142 printf(", archive_id:%d", hus.hus_archive_id);
8145 } while (++i < argc);
8150 #define LFS_HSM_SET 0
8151 #define LFS_HSM_CLEAR 1
8154 * Generic function to set or clear HSM flags.
8155 * Used by hsm_set and hsm_clear.
8157 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8159 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8161 struct option long_opts[] = {
8162 { .val = 'A', .name = "archived", .has_arg = no_argument },
8163 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8164 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8165 { .val = 'e', .name = "exists", .has_arg = no_argument },
8166 { .val = 'l', .name = "lost", .has_arg = no_argument },
8167 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8168 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8170 char short_opts[] = "lraAdei:";
8174 __u32 archive_id = 0;
8180 while ((c = getopt_long(argc, argv, short_opts,
8181 long_opts, NULL)) != -1) {
8187 mask |= HS_NOARCHIVE;
8190 mask |= HS_ARCHIVED;
8193 mask |= HS_NORELEASE;
8202 archive_id = strtol(optarg, &end, 10);
8204 fprintf(stderr, "invalid archive_id: '%s'\n",
8212 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8213 argv[0], argv[optind - 1]);
8218 /* User should have specified a flag */
8222 while (optind < argc) {
8224 path = argv[optind];
8226 /* If mode == 0, this means we apply the mask. */
8227 if (mode == LFS_HSM_SET)
8228 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8230 rc = llapi_hsm_state_set(path, 0, mask, 0);
8233 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8234 path, strerror(errno = -rc));
8243 static int lfs_hsm_action(int argc, char **argv)
8248 struct hsm_current_action hca;
8249 struct hsm_extent he;
8250 enum hsm_user_action hua;
8251 enum hsm_progress_states hps;
8259 rc = llapi_hsm_current_action(path, &hca);
8261 fprintf(stderr, "can't get hsm action for %s: %s\n",
8262 path, strerror(errno = -rc));
8265 he = hca.hca_location;
8266 hua = hca.hca_action;
8267 hps = hca.hca_state;
8269 printf("%s: %s", path, hsm_user_action2name(hua));
8271 /* Skip file without action */
8272 if (hca.hca_action == HUA_NONE) {
8277 printf(" %s ", hsm_progress_state2name(hps));
8279 if ((hps == HPS_RUNNING) &&
8280 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8281 printf("(%llu bytes moved)\n",
8282 (unsigned long long)he.length);
8283 else if ((he.offset + he.length) == LUSTRE_EOF)
8284 printf("(from %llu to EOF)\n",
8285 (unsigned long long)he.offset);
8287 printf("(from %llu to %llu)\n",
8288 (unsigned long long)he.offset,
8289 (unsigned long long)(he.offset + he.length));
8291 } while (++i < argc);
8296 static int lfs_hsm_set(int argc, char **argv)
8298 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8301 static int lfs_hsm_clear(int argc, char **argv)
8303 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8307 * Check file state and return its fid, to be used by lfs_hsm_request().
8309 * \param[in] file Path to file to check
8310 * \param[in,out] fid Pointer to allocated lu_fid struct.
8311 * \param[in,out] last_dev Pointer to last device id used.
8313 * \return 0 on success.
8315 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8321 rc = lstat(file, &st);
8323 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8326 /* Checking for regular file as archiving as posix copytool
8327 * rejects archiving files other than regular files
8329 if (!S_ISREG(st.st_mode)) {
8330 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8333 /* A request should be ... */
8334 if (*last_dev != st.st_dev && *last_dev != 0) {
8335 fprintf(stderr, "All files should be "
8336 "on the same filesystem: %s\n", file);
8339 *last_dev = st.st_dev;
8341 rc = llapi_path2fid(file, fid);
8343 fprintf(stderr, "Cannot read FID of %s: %s\n",
8344 file, strerror(-rc));
8350 /* Fill an HSM HUR item with a given file name.
8352 * If mntpath is set, then the filename is actually a FID, and no
8353 * lookup on the filesystem will be performed.
8355 * \param[in] hur the user request to fill
8356 * \param[in] idx index of the item inside the HUR to fill
8357 * \param[in] mntpath mountpoint of Lustre
8358 * \param[in] fname filename (if mtnpath is NULL)
8359 * or FID (if mntpath is set)
8360 * \param[in] last_dev pointer to last device id used
8362 * \retval 0 on success
8363 * \retval CMD_HELP or a negative errno on error
8365 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8366 const char *mntpath, const char *fname,
8369 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8372 hui->hui_extent.length = -1;
8374 if (mntpath != NULL) {
8375 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8377 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8380 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8384 hur->hur_request.hr_itemcount++;
8389 static int lfs_hsm_request(int argc, char **argv, int action)
8391 struct option long_opts[] = {
8392 { .val = 'a', .name = "archive", .has_arg = required_argument },
8393 { .val = 'D', .name = "data", .has_arg = required_argument },
8394 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8395 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8398 char short_opts[] = "l:D:a:m:";
8399 struct hsm_user_request *hur, *oldhur;
8404 char *filelist = NULL;
8405 char fullpath[PATH_MAX];
8406 char *opaque = NULL;
8410 int nbfile_alloc = 0;
8411 char *some_file = NULL;
8412 char *mntpath = NULL;
8418 while ((c = getopt_long(argc, argv, short_opts,
8419 long_opts, NULL)) != -1) {
8428 if (action != HUA_ARCHIVE &&
8429 action != HUA_REMOVE) {
8431 "error: -a is supported only "
8432 "when archiving or removing\n");
8435 archive_id = atoi(optarg);
8438 if (some_file == NULL) {
8440 some_file = strdup(optarg);
8446 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8447 argv[0], argv[optind - 1]);
8452 /* All remaining args are files, so we have at least nbfile */
8453 nbfile = argc - optind;
8455 if ((nbfile == 0) && (filelist == NULL))
8459 opaque_len = strlen(opaque);
8461 /* Alloc the request structure with enough place to store all files
8462 * from command line. */
8463 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8465 fprintf(stderr, "Cannot create the request: %s\n",
8469 nbfile_alloc = nbfile;
8471 hur->hur_request.hr_action = action;
8472 hur->hur_request.hr_archive_id = archive_id;
8473 hur->hur_request.hr_flags = 0;
8475 /* All remaining args are files, add them */
8476 if (nbfile != 0 && some_file == NULL)
8477 some_file = strdup(argv[optind]);
8479 for (i = 0; i < nbfile; i++) {
8480 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8486 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8488 /* If a filelist was specified, read the filelist from it. */
8489 if (filelist != NULL) {
8490 fp = fopen(filelist, "r");
8492 fprintf(stderr, "Cannot read the file list %s: %s\n",
8493 filelist, strerror(errno));
8498 while ((rc = getline(&line, &len, fp)) != -1) {
8499 /* If allocated buffer was too small, get something
8501 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8504 nbfile_alloc = nbfile_alloc * 2 + 1;
8506 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8509 fprintf(stderr, "hsm: cannot allocate "
8510 "the request: %s\n",
8517 size = hur_len(oldhur);
8519 fprintf(stderr, "hsm: cannot allocate "
8520 "%u files + %u bytes data\n",
8521 oldhur->hur_request.hr_itemcount,
8522 oldhur->hur_request.hr_data_len);
8529 memcpy(hur, oldhur, size);
8534 if (line[strlen(line) - 1] == '\n')
8535 line[strlen(line) - 1] = '\0';
8537 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8538 mntpath, line, &last_dev);
8544 if (some_file == NULL) {
8554 /* If a --data was used, add it to the request */
8555 hur->hur_request.hr_data_len = opaque_len;
8557 memcpy(hur_data(hur), opaque, opaque_len);
8559 /* Send the HSM request */
8560 if (realpath(some_file, fullpath) == NULL) {
8561 fprintf(stderr, "Could not find path '%s': %s\n",
8562 some_file, strerror(errno));
8564 rc = llapi_hsm_request(fullpath, hur);
8566 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8567 some_file, strerror(-rc));
8577 static int lfs_hsm_archive(int argc, char **argv)
8579 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8582 static int lfs_hsm_restore(int argc, char **argv)
8584 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8587 static int lfs_hsm_release(int argc, char **argv)
8589 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8592 static int lfs_hsm_remove(int argc, char **argv)
8594 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8597 static int lfs_hsm_cancel(int argc, char **argv)
8599 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8602 static int lfs_swap_layouts(int argc, char **argv)
8607 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8608 SWAP_LAYOUTS_KEEP_MTIME |
8609 SWAP_LAYOUTS_KEEP_ATIME);
8612 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8614 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8616 int lfs_get_mode(const char *string)
8618 enum lock_mode_user mode;
8620 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8621 if (lock_mode_names[mode] == NULL)
8623 if (strcmp(string, lock_mode_names[mode]) == 0)
8630 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8632 enum lu_ladvise_type advice;
8635 advice < ARRAY_SIZE(ladvise_names); advice++) {
8636 if (ladvise_names[advice] == NULL)
8638 if (strcmp(string, ladvise_names[advice]) == 0)
8642 return LU_LADVISE_INVALID;
8645 static int lfs_ladvise(int argc, char **argv)
8647 struct option long_opts[] = {
8648 { .val = 'a', .name = "advice", .has_arg = required_argument },
8649 { .val = 'b', .name = "background", .has_arg = no_argument },
8650 { .val = 'e', .name = "end", .has_arg = required_argument },
8651 { .val = 'l', .name = "length", .has_arg = required_argument },
8652 { .val = 'm', .name = "mode", .has_arg = required_argument },
8653 { .val = 's', .name = "start", .has_arg = required_argument },
8654 { .val = 'u', .name = "unset", .has_arg = no_argument },
8656 char short_opts[] = "a:be:l:m:s:u";
8661 struct llapi_lu_ladvise advice;
8662 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8663 unsigned long long start = 0;
8664 unsigned long long end = LUSTRE_EOF;
8665 unsigned long long length = 0;
8666 unsigned long long size_units;
8667 unsigned long long flags = 0;
8671 while ((c = getopt_long(argc, argv, short_opts,
8672 long_opts, NULL)) != -1) {
8675 advice_type = lfs_get_ladvice(optarg);
8676 if (advice_type == LU_LADVISE_INVALID) {
8677 fprintf(stderr, "%s: invalid advice type "
8678 "'%s'\n", argv[0], optarg);
8679 fprintf(stderr, "Valid types:");
8681 for (advice_type = 0;
8682 advice_type < ARRAY_SIZE(ladvise_names);
8684 if (ladvise_names[advice_type] == NULL)
8686 fprintf(stderr, " %s",
8687 ladvise_names[advice_type]);
8689 fprintf(stderr, "\n");
8702 rc = llapi_parse_size(optarg, &end,
8705 fprintf(stderr, "%s: bad end offset '%s'\n",
8712 rc = llapi_parse_size(optarg, &start,
8715 fprintf(stderr, "%s: bad start offset "
8716 "'%s'\n", argv[0], optarg);
8722 rc = llapi_parse_size(optarg, &length,
8725 fprintf(stderr, "%s: bad length '%s'\n",
8731 mode = lfs_get_mode(optarg);
8733 fprintf(stderr, "%s: bad mode '%s', valid "
8734 "modes are READ or WRITE\n",
8742 fprintf(stderr, "%s: option '%s' unrecognized\n",
8743 argv[0], argv[optind - 1]);
8748 if (advice_type == LU_LADVISE_INVALID) {
8749 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8750 fprintf(stderr, "Valid types:");
8751 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8753 if (ladvise_names[advice_type] == NULL)
8755 fprintf(stderr, " %s", ladvise_names[advice_type]);
8757 fprintf(stderr, "\n");
8761 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8762 fprintf(stderr, "%s: Lock no expand advice is a per file "
8763 "descriptor advice, so when called from lfs, "
8764 "it does nothing.\n", argv[0]);
8768 if (argc <= optind) {
8769 fprintf(stderr, "%s: please give one or more file names\n",
8774 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8775 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8780 if (end == LUSTRE_EOF && length != 0)
8781 end = start + length;
8784 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8785 argv[0], start, end);
8789 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8790 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8795 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8796 fprintf(stderr, "%s: mode is required with lockahead\n",
8801 while (optind < argc) {
8804 path = argv[optind++];
8806 fd = open(path, O_RDONLY);
8808 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8809 argv[0], path, strerror(errno));
8814 advice.lla_start = start;
8815 advice.lla_end = end;
8816 advice.lla_advice = advice_type;
8817 advice.lla_value1 = 0;
8818 advice.lla_value2 = 0;
8819 advice.lla_value3 = 0;
8820 advice.lla_value4 = 0;
8821 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8822 advice.lla_lockahead_mode = mode;
8823 advice.lla_peradvice_flags = flags;
8826 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8829 fprintf(stderr, "%s: cannot give advice '%s' to file "
8830 "'%s': %s\n", argv[0],
8831 ladvise_names[advice_type],
8832 path, strerror(errno));
8838 if (rc == 0 && rc2 < 0)
8844 static const char *const heat_names[] = LU_HEAT_NAMES;
8846 static int lfs_heat_get(int argc, char **argv)
8848 struct lu_heat *heat;
8857 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8859 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8864 while (optind < argc) {
8865 path = argv[optind++];
8867 fd = open(path, O_RDONLY);
8869 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8870 argv[0], path, strerror(errno));
8875 heat->lh_count = OBD_HEAT_COUNT;
8876 rc2 = llapi_heat_get(fd, heat);
8879 fprintf(stderr, "%s: cannot get heat of file '%s'"
8880 ": %s\n", argv[0], path, strerror(errno));
8884 printf("flags: %x\n", heat->lh_flags);
8885 for (i = 0; i < heat->lh_count; i++)
8886 printf("%s: %llu\n", heat_names[i],
8887 (unsigned long long)heat->lh_heat[i]);
8889 if (rc == 0 && rc2 < 0)
8897 static int lfs_heat_set(int argc, char **argv)
8899 struct option long_opts[] = {
8900 { .val = 'c', .name = "clear", .has_arg = no_argument },
8901 { .val = 'o', .name = "off", .has_arg = no_argument },
8902 { .val = 'O', .name = "on", .has_arg = no_argument },
8904 enum lu_heat_flag flags = 0;
8914 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8917 flags |= LU_HEAT_FLAG_CLEAR;
8920 flags |= LU_HEAT_FLAG_CLEAR;
8921 flags |= LU_HEAT_FLAG_OFF;
8924 flags &= ~LU_HEAT_FLAG_OFF;
8929 fprintf(stderr, "%s: option '%s' unrecognized\n",
8930 argv[0], argv[optind - 1]);
8935 if (argc <= optind) {
8936 fprintf(stderr, "%s: please give one or more file names\n",
8941 while (optind < argc) {
8942 path = argv[optind++];
8944 fd = open(path, O_RDONLY);
8946 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8947 argv[0], path, strerror(errno));
8952 rc2 = llapi_heat_set(fd, flags);
8955 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8956 ": %s\n", argv[0], path, strerror(errno));
8960 if (rc == 0 && rc2 < 0)
8966 /** The input string contains a comma delimited list of component ids and
8967 * ranges, for example "1,2-4,7".
8969 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8971 bool end_of_loop = false;
8979 while (!end_of_loop) {
8983 char *endptr = NULL;
8986 ptr = strchrnul(arg, ',');
8987 end_of_loop = *ptr == '\0';
8990 start_index = strtol(arg, &endptr, 0);
8991 if (endptr == arg) /* no data at all */
8993 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8995 if (start_index < 0)
8998 end_index = start_index;
8999 if (*endptr == '-') {
9000 end_index = strtol(endptr + 1, &endptr, 0);
9001 if (*endptr != '\0')
9003 if (end_index < start_index)
9007 for (i = start_index; i <= end_index && size > 0; i++) {
9010 /* remove duplicate */
9011 for (j = 0; j < nr; j++) {
9015 if (j == nr) { /* no duplicate */
9021 if (size == 0 && i < end_index)
9028 if (!end_of_loop && ptr != NULL)
9031 return rc < 0 ? rc : nr;
9035 * struct verify_mirror_id - Mirror id to be verified.
9036 * @mirror_id: A specified mirror id.
9037 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9039 struct verify_mirror_id {
9045 * compare_mirror_ids() - Compare mirror ids.
9046 * @layout: Mirror component list.
9047 * @cbdata: Callback data in verify_mirror_id structure.
9049 * This is a callback function called by llapi_layout_comp_iterate()
9050 * to compare the specified mirror id with the one in the current
9051 * component of @layout. If they are the same, then the specified
9052 * mirror id is valid.
9054 * Return: a negative error code on failure or
9055 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9056 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9059 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9061 struct verify_mirror_id *mirror_id_cbdata =
9062 (struct verify_mirror_id *)cbdata;
9066 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9070 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9071 progname, strerror(errno));
9075 if (mirror_id_cbdata->mirror_id == mirror_id) {
9076 mirror_id_cbdata->is_valid_id = true;
9077 return LLAPI_LAYOUT_ITER_STOP;
9080 return LLAPI_LAYOUT_ITER_CONT;
9084 * verify_mirror_ids() - Verify specified mirror ids.
9085 * @fname: Mirrored file name.
9086 * @mirror_ids: Specified mirror ids to be verified.
9087 * @ids_nr: Number of specified mirror ids.
9089 * This function verifies that specified @mirror_ids are valid
9090 * in the mirrored file @fname.
9092 * Return: 0 on success or a negative error code on failure.
9095 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9097 struct llapi_layout *layout = NULL;
9098 struct verify_mirror_id mirror_id_cbdata = { 0 };
9109 if (stat(fname, &stbuf) < 0) {
9110 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9111 progname, fname, strerror(errno));
9116 if (!S_ISREG(stbuf.st_mode)) {
9117 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9123 fd = open(fname, O_DIRECT | O_RDONLY);
9125 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9126 progname, fname, strerror(errno));
9131 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9133 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9134 progname, fname, strerror(errno));
9138 layout = llapi_layout_get_by_fd(fd, 0);
9139 if (layout == NULL) {
9140 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9141 progname, fname, strerror(errno));
9143 llapi_lease_release(fd);
9147 rc = llapi_layout_flags_get(layout, &flr_state);
9149 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9150 progname, fname, strerror(errno));
9155 flr_state &= LCM_FL_FLR_MASK;
9156 switch (flr_state) {
9159 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9160 progname, fname, llapi_layout_flags_string(flr_state));
9167 for (i = 0; i < ids_nr; i++) {
9168 mirror_id_cbdata.mirror_id = mirror_ids[i];
9169 mirror_id_cbdata.is_valid_id = false;
9171 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9176 "%s: '%s' failed to verify mirror id: %u.\n",
9177 progname, fname, mirror_ids[i]);
9181 if (!mirror_id_cbdata.is_valid_id) {
9184 "%s: '%s' invalid specified mirror id: %u.\n",
9185 progname, fname, mirror_ids[i]);
9191 llapi_layout_free(layout);
9192 llapi_lease_release(fd);
9200 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9201 __u16 *mirror_ids, int ids_nr)
9203 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9204 struct llapi_layout *layout;
9215 if (stat(fname, &stbuf) < 0) {
9216 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9217 progname, fname, strerror(errno));
9221 if (!S_ISREG(stbuf.st_mode)) {
9222 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9228 fd = open(fname, O_DIRECT | O_RDWR);
9230 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9231 progname, fname, strerror(errno));
9236 layout = llapi_layout_get_by_fd(fd, 0);
9237 if (layout == NULL) {
9238 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9239 progname, fname, strerror(errno));
9244 rc = llapi_layout_flags_get(layout, &flr_state);
9246 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9247 progname, fname, strerror(errno));
9252 flr_state &= LCM_FL_FLR_MASK;
9253 if (flr_state == LCM_FL_NONE) {
9255 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9260 /* get stale component info */
9261 comp_size = llapi_mirror_find_stale(layout, comp_array,
9262 ARRAY_SIZE(comp_array),
9263 mirror_ids, ids_nr);
9264 if (comp_size <= 0) {
9269 ioc->lil_mode = LL_LEASE_WRLCK;
9270 ioc->lil_flags = LL_LEASE_RESYNC;
9271 rc = llapi_lease_set(fd, ioc);
9273 if (rc == -EALREADY)
9277 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9278 progname, fname, strerror(-rc));
9282 /* get the read range [start, end) */
9283 start = comp_array[0].lrc_start;
9284 end = comp_array[0].lrc_end;
9285 for (idx = 1; idx < comp_size; idx++) {
9286 if (comp_array[idx].lrc_start < start)
9287 start = comp_array[idx].lrc_start;
9288 if (end < comp_array[idx].lrc_end)
9289 end = comp_array[idx].lrc_end;
9292 rc = llapi_lease_check(fd);
9293 if (rc != LL_LEASE_WRLCK) {
9294 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9299 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9302 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9303 progname, fname, strerror(-rc));
9305 /* need to do the lease unlock even resync fails */
9306 ioc->lil_mode = LL_LEASE_UNLCK;
9307 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9309 for (idx = 0; idx < comp_size; idx++) {
9310 if (comp_array[idx].lrc_synced) {
9311 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9316 rc2 = llapi_lease_set(fd, ioc);
9318 * llapi_lease_set returns lease mode when it request to unlock
9322 /* rc2 == 0 means lost lease lock */
9323 if (rc2 == 0 && rc == 0)
9325 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9327 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9331 llapi_layout_free(layout);
9338 static inline int lfs_mirror_resync(int argc, char **argv)
9340 struct ll_ioc_lease *ioc = NULL;
9341 __u16 mirror_ids[128] = { 0 };
9346 struct option long_opts[] = {
9347 { .val = 'o', .name = "only", .has_arg = required_argument },
9350 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9353 rc = parse_mirror_ids(mirror_ids,
9354 sizeof(mirror_ids) / sizeof(__u16),
9358 "%s: bad mirror ids '%s'.\n",
9365 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9366 argv[0], argv[optind - 1]);
9372 if (argc == optind) {
9373 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9378 if (ids_nr > 0 && argc > optind + 1) {
9380 "%s: option '--only' cannot be used upon multiple files.\n",
9388 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9393 /* set the lease on the file */
9394 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9396 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9397 argv[0], strerror(errno));
9402 for (; optind < argc; optind++) {
9403 rc = lfs_mirror_resync_file(argv[optind], ioc,
9404 mirror_ids, ids_nr);
9405 /* ignore previous file's error, continue with next file */
9408 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9416 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9418 struct llapi_layout *layout;
9421 layout = llapi_layout_get_by_fd(fd, 0);
9422 if (layout == NULL) {
9423 fprintf(stderr, "could not get layout.\n");
9427 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9429 fprintf(stderr, "failed to iterate layout\n");
9430 llapi_layout_free(layout);
9433 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9434 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9435 llapi_layout_free(layout);
9439 llapi_layout_free(layout);
9445 * Check whether two files are the same file
9446 * \retval 0 same file
9447 * \retval 1 not the same file
9448 * \retval <0 error code
9450 static inline int check_same_file(const char *f1, const char *f2)
9455 if (stat(f1, &stbuf1) < 0) {
9456 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9457 progname, f1, strerror(errno));
9461 if (stat(f2, &stbuf2) < 0) {
9462 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9463 progname, f2, strerror(errno));
9467 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9468 stbuf1.st_ino == stbuf2.st_ino)
9474 static inline int lfs_mirror_read(int argc, char **argv)
9477 __u16 mirror_id = 0;
9478 const char *outfile = NULL;
9484 const size_t buflen = 4 << 20;
9486 struct option long_opts[] = {
9487 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9488 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9491 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9496 mirror_id = strtoul(optarg, &end, 0);
9497 if (*end != '\0' || mirror_id == 0) {
9499 "%s %s: invalid mirror ID '%s'\n",
9500 progname, argv[0], optarg);
9508 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9509 progname, argv[optind - 1]);
9514 if (argc == optind) {
9515 fprintf(stderr, "%s %s: no mirrored file provided\n",
9518 } else if (argc > optind + 1) {
9519 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9523 if (mirror_id == 0) {
9524 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9529 /* open mirror file */
9530 fname = argv[optind];
9533 rc = check_same_file(fname, outfile);
9536 "%s %s: output file cannot be the mirrored file\n",
9544 fd = open(fname, O_DIRECT | O_RDONLY);
9546 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9547 progname, argv[0], fname, strerror(errno));
9551 /* verify mirror id */
9552 rc = verify_mirror_id_by_fd(fd, mirror_id);
9555 "%s %s: cannot find mirror with ID %u in '%s'\n",
9556 progname, argv[0], mirror_id, fname);
9560 /* open output file */
9562 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9564 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9565 progname, argv[0], outfile, strerror(errno));
9570 outfd = STDOUT_FILENO;
9573 /* allocate buffer */
9574 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9576 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9577 progname, argv[0], rc);
9584 ssize_t written = 0;
9586 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9587 if (bytes_read < 0) {
9590 "%s %s: fail to read data from mirror %u: %s\n",
9591 progname, argv[0], mirror_id, strerror(-rc));
9596 if (bytes_read == 0)
9599 while (written < bytes_read) {
9602 written2 = write(outfd, buf + written,
9603 bytes_read - written);
9606 "%s %s: fail to write %s: %s\n",
9607 progname, argv[0], outfile ? : "STDOUT",
9612 written += written2;
9615 if (written != bytes_read) {
9617 "%s %s: written %ld bytes does not match with %ld read.\n",
9618 progname, argv[0], written, bytes_read);
9640 static inline int lfs_mirror_write(int argc, char **argv)
9643 __u16 mirror_id = 0;
9644 const char *inputfile = NULL;
9650 const size_t buflen = 4 << 20;
9652 size_t page_size = sysconf(_SC_PAGESIZE);
9653 struct ll_ioc_lease_id ioc;
9655 struct option long_opts[] = {
9656 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9657 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9660 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9665 mirror_id = strtoul(optarg, &end, 0);
9666 if (*end != '\0' || mirror_id == 0) {
9668 "%s %s: invalid mirror ID '%s'\n",
9669 progname, argv[0], optarg);
9677 fprintf(stderr, "%s: option '%s' unrecognized\n",
9678 progname, argv[optind - 1]);
9683 if (argc == optind) {
9684 fprintf(stderr, "%s %s: no mirrored file provided\n",
9687 } else if (argc > optind + 1) {
9688 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9692 if (mirror_id == 0) {
9693 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9698 /* open mirror file */
9699 fname = argv[optind];
9702 rc = check_same_file(fname, inputfile);
9705 "%s %s: input file cannot be the mirrored file\n",
9713 fd = open(fname, O_DIRECT | O_WRONLY);
9715 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9716 progname, argv[0], fname, strerror(errno));
9720 /* verify mirror id */
9721 rc = verify_mirror_id_by_fd(fd, mirror_id);
9724 "%s %s: cannot find mirror with ID %u in '%s'\n",
9725 progname, argv[0], mirror_id, fname);
9729 /* open input file */
9731 inputfd = open(inputfile, O_RDONLY, 0644);
9733 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9734 progname, argv[0], inputfile, strerror(errno));
9739 inputfd = STDIN_FILENO;
9742 /* allocate buffer */
9743 rc = posix_memalign(&buf, page_size, buflen);
9745 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9746 progname, argv[0], rc);
9750 /* prepare target mirror components instantiation */
9751 ioc.lil_mode = LL_LEASE_WRLCK;
9752 ioc.lil_flags = LL_LEASE_RESYNC;
9753 ioc.lil_mirror_id = mirror_id;
9754 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9757 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9758 progname, argv[0], fname, strerror(errno));
9768 rc = llapi_lease_check(fd);
9769 if (rc != LL_LEASE_WRLCK) {
9770 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9771 progname, argv[0], fname);
9775 bytes_read = read(inputfd, buf, buflen);
9776 if (bytes_read < 0) {
9779 "%s %s: fail to read data from '%s': %s\n",
9780 progname, argv[0], inputfile ? : "STDIN",
9787 if (bytes_read == 0)
9790 /* round up to page align to make direct IO happy. */
9791 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9793 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9798 "%s %s: fail to write to mirror %u: %s\n",
9799 progname, argv[0], mirror_id,
9807 if (pos & (page_size - 1)) {
9808 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9813 ioc.lil_mode = LL_LEASE_UNLCK;
9814 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9816 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9821 "%s %s: release lease lock of '%s' failed: %s\n",
9822 progname, argv[0], fname, strerror(errno));
9839 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9841 struct llapi_layout *layout;
9842 struct collect_ids_data cid = { .cid_ids = ids,
9844 .cid_exclude = exclude_id, };
9847 layout = llapi_layout_get_by_fd(fd, 0);
9848 if (layout == NULL) {
9849 fprintf(stderr, "could not get layout\n");
9853 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9855 fprintf(stderr, "failed to iterate layout\n");
9856 llapi_layout_free(layout);
9860 llapi_layout_free(layout);
9862 return cid.cid_count;
9865 static inline int lfs_mirror_copy(int argc, char **argv)
9868 __u16 read_mirror_id = 0;
9869 __u16 ids[128] = { 0 };
9871 struct llapi_layout *layout = NULL;
9872 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9879 struct ll_ioc_lease *ioc = NULL;
9880 struct ll_ioc_lease_id *resync_ioc;
9882 struct option long_opts[] = {
9883 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9884 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9887 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9892 read_mirror_id = strtoul(optarg, &end, 0);
9893 if (*end != '\0' || read_mirror_id == 0) {
9895 "%s %s: invalid read mirror ID '%s'\n",
9896 progname, argv[0], optarg);
9901 if (!strcmp(optarg, "-1")) {
9902 /* specify all other mirrors */
9906 count = parse_mirror_ids((__u16 *)ids,
9914 fprintf(stderr, "%s: option '%s' unrecognized\n",
9915 progname, argv[optind - 1]);
9920 if (argc == optind) {
9921 fprintf(stderr, "%s %s: no mirrored file provided\n",
9924 } else if (argc > optind + 1) {
9925 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9929 if (read_mirror_id == 0) {
9931 "%s %s: no valid read mirror ID %d is provided\n",
9932 progname, argv[0], read_mirror_id);
9938 "%s %s: no write mirror ID is provided\n",
9943 for (i = 0; i < count; i++) {
9944 if (read_mirror_id == ids[i]) {
9946 "%s %s: read and write mirror ID cannot be the same\n",
9952 /* open mirror file */
9953 fname = argv[optind];
9955 fd = open(fname, O_DIRECT | O_RDWR);
9957 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9958 progname, argv[0], fname, strerror(errno));
9962 /* write to all other mirrors */
9963 if (ids[0] == (__u16)-1) {
9964 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9968 "%s %s: failed to get other mirror ids in '%s': %d\n",
9969 progname, argv[0], fname, rc);
9974 /* verify mirror id */
9975 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9978 "%s %s: cannot find mirror with ID %u in '%s'\n",
9979 progname, argv[0], read_mirror_id, fname);
9983 for (i = 0; i < count; i++) {
9984 rc = verify_mirror_id_by_fd(fd, ids[i]);
9987 "%s %s: cannot find mirror with ID %u in '%s'\n",
9988 progname, argv[0], ids[i], fname);
9993 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9996 "%s %s: cannot alloc comp id array for ioc: %s\n",
9997 progname, argv[0], strerror(errno));
10002 /* get stale component info */
10003 layout = llapi_layout_get_by_fd(fd, 0);
10004 if (layout == NULL) {
10005 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10006 progname, argv[0], fname, strerror(errno));
10010 comp_size = llapi_mirror_find_stale(layout, comp_array,
10011 ARRAY_SIZE(comp_array),
10013 llapi_layout_free(layout);
10014 if (comp_size < 0) {
10019 /* prepare target mirror components instantiation */
10020 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10021 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10022 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10024 resync_ioc->lil_mirror_id = ids[0];
10026 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10027 rc = llapi_lease_set(fd, ioc);
10030 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10031 progname, argv[0], fname, strerror(errno));
10035 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10038 fprintf(stderr, "%s %s: copy error: %d\n",
10039 progname, argv[0], rc);
10043 fprintf(stdout, "mirror copied successfully: ");
10044 for (i = 0; i < copied; i++)
10045 fprintf(stdout, "%d ", ids[i]);
10046 fprintf(stdout, "\n");
10048 ioc->lil_mode = LL_LEASE_UNLCK;
10049 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10050 ioc->lil_count = 0;
10051 for (i = 0; i < comp_size; i++) {
10054 for (j = 0; j < copied; j++) {
10055 if (comp_array[i].lrc_mirror_id != ids[j])
10058 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10062 rc = llapi_lease_set(fd, ioc);
10067 "%s %s: release lease lock of '%s' failed: %s\n",
10068 progname, argv[0], fname, strerror(errno));
10082 * struct verify_chunk - Mirror chunk to be verified.
10083 * @chunk: [start, end) of the chunk.
10084 * @mirror_count: Number of mirror ids in @mirror_id array.
10085 * @mirror_id: Array of valid mirror ids that cover the chunk.
10087 struct verify_chunk {
10088 struct lu_extent chunk;
10089 unsigned int mirror_count;
10090 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10094 * print_chunks() - Print chunk information.
10095 * @fname: Mirrored file name.
10096 * @chunks: Array of chunks.
10097 * @chunk_count: Number of chunks in @chunks array.
10099 * This function prints [start, end) of each chunk in @chunks
10100 * for mirrored file @fname, and also prints the valid mirror ids
10101 * that cover the chunk.
10106 void print_chunks(const char *fname, struct verify_chunk *chunks,
10112 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10113 for (i = 0; i < chunk_count; i++) {
10114 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10116 if (chunks[i].mirror_count == 0)
10117 fprintf(stdout, "\t[");
10119 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10120 for (j = 1; j < chunks[i].mirror_count; j++)
10121 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10123 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10125 fprintf(stdout, "\n");
10129 * print_checksums() - Print CRC-32 checksum values.
10130 * @chunk: A chunk and its corresponding valid mirror ids.
10131 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10133 * This function prints CRC-32 checksum values on @chunk for
10134 * each valid mirror that covers it.
10139 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10144 "CRC-32 checksum value for chunk "DEXT":\n",
10145 PEXT(&chunk->chunk));
10146 for (i = 0; i < chunk->mirror_count; i++)
10147 fprintf(stdout, "Mirror %u:\t%#lx\n",
10148 chunk->mirror_id[i], crc[i]);
10149 fprintf(stdout, "\n");
10153 * filter_mirror_id() - Filter specified mirror ids.
10154 * @chunks: Array of chunks.
10155 * @chunk_count: Number of chunks in @chunks array.
10156 * @mirror_ids: Specified mirror ids to be verified.
10157 * @ids_nr: Number of specified mirror ids.
10159 * This function scans valid mirror ids that cover each chunk in @chunks
10160 * and filters specified mirror ids.
10165 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10166 __u16 *mirror_ids, int ids_nr)
10171 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10172 unsigned int valid_count = 0;
10174 for (i = 0; i < chunk_count; i++) {
10175 if (chunks[i].mirror_count == 0)
10179 for (j = 0; j < ids_nr; j++) {
10180 for (k = 0; k < chunks[i].mirror_count; k++) {
10181 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10182 valid_id[valid_count] = mirror_ids[j];
10189 memcpy(chunks[i].mirror_id, valid_id,
10190 sizeof(__u16) * valid_count);
10191 chunks[i].mirror_count = valid_count;
10196 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10197 * @layout: Mirror component list.
10198 * @chunks: Array of chunks.
10199 * @chunks_size: Array size of @chunks.
10201 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10202 * to find out chunk segments and store them in @chunks array.
10204 * The @mirror_id array in each element of @chunks will store the valid
10205 * mirror ids that cover the chunk. If a mirror component covering the
10206 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10207 * will not be stored into the @mirror_id array, and the chunk for that
10208 * mirror will not be verified.
10210 * The @mirror_count in each element of @chunks will store the number of
10211 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10212 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10213 * indicates the chunk is valid in only one mirror. In both cases, the
10214 * chunk will not be verified.
10216 * Here is an example:
10218 * 0 1M 2M 3M 4M EOF
10219 * +------+-------------+--------------------+
10220 * | | | S | mirror1
10221 * +------+------+------+------+-------------+
10222 * | | S | S | | mirror2
10223 * +-------------+------+------+-------------+
10225 * prepared @chunks array will contain 5 elements:
10226 * (([0, 1M), [1, 2], 2),
10227 * ([1M, 2M), [1, 2], 2),
10228 * ([2M, 3M), [1], 1),
10229 * ([3M, 4M], [], 0),
10230 * ([4M, EOF), [2], 1))
10232 * Return: the actual array size of @chunks on success
10233 * or a negative error code on failure.
10236 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10237 struct verify_chunk *chunks,
10238 size_t chunks_size)
10242 uint32_t mirror_id;
10248 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10251 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10254 "%s: move to the first layout component: %s.\n",
10255 progname, strerror(errno));
10261 chunks[idx].chunk.e_end = LUSTRE_EOF;
10263 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10266 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10267 progname, strerror(errno));
10271 if (start > chunks[idx].chunk.e_start ||
10272 end <= chunks[idx].chunk.e_start)
10275 if (end < chunks[idx].chunk.e_end)
10276 chunks[idx].chunk.e_end = end;
10278 rc = llapi_layout_comp_flags_get(layout, &flags);
10281 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10282 progname, strerror(errno));
10286 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10289 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10292 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10293 progname, strerror(errno));
10297 chunks[idx].mirror_id[i] = mirror_id;
10299 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10301 "%s: mirror_id array is too small.\n",
10308 rc = llapi_layout_comp_use(layout,
10309 LLAPI_LAYOUT_COMP_USE_NEXT);
10312 "%s: move to the next layout component: %s.\n",
10313 progname, strerror(errno));
10316 } /* loop through all components */
10318 chunks[idx].mirror_count = i;
10320 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10324 if (idx >= chunks_size) {
10325 fprintf(stderr, "%s: chunks array is too small.\n",
10331 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10335 return rc < 0 ? rc : idx + 1;
10339 * lfs_mirror_verify_chunk() - Verify a chunk.
10340 * @fd: File descriptor of the mirrored file.
10341 * @file_size: Size of the mirrored file.
10342 * @chunk: A chunk and its corresponding valid mirror ids.
10343 * @verbose: Verbose mode.
10345 * This function verifies a @chunk contains exactly the same data
10346 * ammong the mirrors that cover it.
10348 * If @verbose is specified, then the function will print where the
10349 * differences are if the data do not match. Otherwise, it will
10350 * just return an error in that case.
10352 * Return: 0 on success or a negative error code on failure.
10355 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10356 struct verify_chunk *chunk, int verbose)
10358 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10360 size_t page_size = sysconf(_SC_PAGESIZE);
10361 ssize_t bytes_read;
10362 ssize_t bytes_done;
10366 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10370 if (file_size == 0)
10373 rc = posix_memalign(&buf, page_size, buflen);
10374 if (rc) /* error code is returned directly */
10378 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10379 PEXT(&chunk->chunk));
10380 for (i = 0; i < chunk->mirror_count; i++)
10381 fprintf(stdout, " %u", chunk->mirror_id[i]);
10382 fprintf(stdout, "\n");
10386 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10387 pos = chunk->chunk.e_start;
10388 while (bytes_done < count) {
10389 /* compute initial CRC-32 checksum */
10390 crc = crc32(0L, Z_NULL, 0);
10391 memset(crc_array, 0, sizeof(crc_array));
10394 for (i = 0; i < chunk->mirror_count; i++) {
10395 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10397 if (bytes_read < 0) {
10400 "%s: failed to read data from mirror %u: %s.\n",
10401 progname, chunk->mirror_id[i],
10406 /* compute new CRC-32 checksum */
10407 crc_array[i] = crc32(crc, buf, bytes_read);
10411 print_checksums(chunk, crc_array);
10413 /* compare CRC-32 checksum values */
10414 for (i = 1; i < chunk->mirror_count; i++) {
10415 if (crc_array[i] != crc_array[0]) {
10419 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10420 progname, PEXT(&chunk->chunk),
10421 chunk->mirror_id[0],
10422 chunk->mirror_id[i]);
10427 bytes_done += bytes_read;
10430 if (verbose > 1 && rc == 0) {
10431 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10432 PEXT(&chunk->chunk));
10433 for (i = 0; i < chunk->mirror_count; i++)
10434 fprintf(stdout, " %u", chunk->mirror_id[i]);
10435 fprintf(stdout, " PASS\n\n");
10444 * lfs_mirror_verify_file() - Verify a mirrored file.
10445 * @fname: Mirrored file name.
10446 * @mirror_ids: Specified mirror ids to be verified.
10447 * @ids_nr: Number of specified mirror ids.
10448 * @verbose: Verbose mode.
10450 * This function verifies that each SYNC mirror of a mirrored file
10451 * specified by @fname contains exactly the same data.
10453 * If @mirror_ids is specified, then the function will verify the
10454 * mirrors specified by @mirror_ids contain exactly the same data.
10456 * If @verbose is specified, then the function will print where the
10457 * differences are if the data do not match. Otherwise, it will
10458 * just return an error in that case.
10460 * Return: 0 on success or a negative error code on failure.
10463 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10466 struct verify_chunk chunks_array[1024] = { };
10467 struct llapi_layout *layout = NULL;
10469 uint32_t flr_state;
10471 int chunk_count = 0;
10477 if (stat(fname, &stbuf) < 0) {
10478 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10479 progname, fname, strerror(errno));
10484 if (!S_ISREG(stbuf.st_mode)) {
10485 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10491 if (stbuf.st_size == 0) {
10493 fprintf(stdout, "%s: '%s' file size is 0.\n",
10499 fd = open(fname, O_DIRECT | O_RDONLY);
10501 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10502 progname, fname, strerror(errno));
10507 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10509 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10510 progname, fname, strerror(errno));
10514 layout = llapi_layout_get_by_fd(fd, 0);
10515 if (layout == NULL) {
10516 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10517 progname, fname, strerror(errno));
10519 llapi_lease_release(fd);
10523 rc = llapi_layout_flags_get(layout, &flr_state);
10525 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10526 progname, fname, strerror(errno));
10531 flr_state &= LCM_FL_FLR_MASK;
10532 switch (flr_state) {
10535 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10536 progname, fname, llapi_layout_flags_string(flr_state));
10542 /* find out mirror chunks to be verified */
10543 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10544 ARRAY_SIZE(chunks_array));
10545 if (chunk_count < 0) {
10551 /* filter specified mirror ids */
10552 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10555 print_chunks(fname, chunks_array, chunk_count);
10557 for (idx = 0; idx < chunk_count; idx++) {
10558 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10561 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10563 PEXT(&chunks_array[idx].chunk),
10564 (unsigned long long)stbuf.st_size);
10568 if (chunks_array[idx].mirror_count == 0) {
10570 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10572 PEXT(&chunks_array[idx].chunk));
10574 fprintf(stderr, "skipped\n");
10578 fprintf(stderr, "failed\n");
10582 if (chunks_array[idx].mirror_count == 1) {
10585 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10587 PEXT(&chunks_array[idx].chunk),
10588 chunks_array[idx].mirror_id[0]);
10592 rc = llapi_lease_check(fd);
10593 if (rc != LL_LEASE_RDLCK) {
10594 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10599 /* verify one chunk */
10600 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10601 &chunks_array[idx], verbose);
10615 llapi_layout_free(layout);
10616 llapi_lease_release(fd);
10624 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10625 * @argc: The count of lfs mirror verify command line arguments.
10626 * @argv: Array of strings for lfs mirror verify command line arguments.
10628 * This function parses lfs mirror verify command and verifies the
10629 * specified mirrored file(s).
10631 * Return: 0 on success or a negative error code on failure.
10633 static inline int lfs_mirror_verify(int argc, char **argv)
10635 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10641 char cmd[PATH_MAX];
10643 struct option long_opts[] = {
10644 { .val = 'o', .name = "only", .has_arg = required_argument },
10645 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10646 { .name = NULL } };
10648 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10650 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10653 rc = parse_mirror_ids(mirror_ids,
10654 ARRAY_SIZE(mirror_ids),
10658 "%s: bad mirror ids '%s'.\n",
10665 "%s: at least 2 mirror ids needed with '--only' option.\n",
10675 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10676 progname, argv[optind - 1]);
10682 if (argc == optind) {
10683 fprintf(stderr, "%s: no file name given.\n", progname);
10688 if (ids_nr > 0 && argc > optind + 1) {
10690 "%s: '--only' cannot be used upon multiple files.\n",
10698 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10704 for (; optind < argc; optind++) {
10705 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10715 * lfs_mirror() - Parse and execute lfs mirror commands.
10716 * @argc: The count of lfs mirror command line arguments.
10717 * @argv: Array of strings for lfs mirror command line arguments.
10719 * This function parses lfs mirror commands and performs the
10720 * corresponding functions specified in mirror_cmdlist[].
10722 * Return: 0 on success or an error code on failure.
10724 static int lfs_mirror(int argc, char **argv)
10726 char cmd[PATH_MAX];
10729 setlinebuf(stdout);
10731 Parser_init("lfs-mirror > ", mirror_cmdlist);
10733 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10735 program_invocation_short_name = cmd;
10737 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10739 rc = Parser_commands();
10741 return rc < 0 ? -rc : rc;
10744 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10746 #if __BYTE_ORDER == __BIG_ENDIAN
10747 __swab16s(&attrs->lsa_valid);
10748 __swab64s(&attrs->lsa_size);
10749 __swab64s(&attrs->lsa_blocks);
10753 enum lfs_som_type {
10754 LFS_SOM_SIZE = 0x1,
10755 LFS_SOM_BLOCKS = 0x2,
10756 LFS_SOM_FLAGS = 0x4,
10757 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10761 static int lfs_getsom(int argc, char **argv)
10764 struct lustre_som_attrs *attrs;
10765 char buf[sizeof(*attrs) + 64];
10766 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10769 while ((c = getopt(argc, argv, "sbf")) != -1) {
10772 type = LFS_SOM_SIZE;
10775 type = LFS_SOM_BLOCKS;
10778 type = LFS_SOM_FLAGS;
10781 fprintf(stderr, "%s: invalid option '%c'\n",
10791 fprintf(stderr, "%s: %s\n",
10792 progname, argc == 0 ? "miss file target" :
10793 "input more than 2 files");
10798 attrs = (void *)buf;
10799 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10802 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10803 argv[0], strerror(errno), errno);
10807 lustre_som_swab(attrs);
10810 case LFS_SOM_ATTR_ALL:
10811 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10812 path, (unsigned long long)attrs->lsa_size,
10813 (unsigned long long)attrs->lsa_blocks,
10817 printf("%llu\n", (unsigned long long)attrs->lsa_size);
10819 case LFS_SOM_BLOCKS:
10820 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
10822 case LFS_SOM_FLAGS:
10823 printf("%x\n", attrs->lsa_valid);
10826 fprintf(stderr, "%s: unknown option\n", progname);
10834 * lfs_mirror_list_commands() - List lfs mirror commands.
10835 * @argc: The count of command line arguments.
10836 * @argv: Array of strings for command line arguments.
10838 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10840 * Return: 0 on success.
10842 static int lfs_mirror_list_commands(int argc, char **argv)
10844 char buffer[81] = "";
10846 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10852 static int lfs_pcc_attach(int argc, char **argv)
10854 struct option long_opts[] = {
10855 { .val = 'i', .name = "id", .has_arg = required_argument },
10856 { .name = NULL } };
10859 __u32 archive_id = 0;
10862 char fullpath[PATH_MAX];
10863 enum lu_pcc_type type = LU_PCC_READWRITE;
10866 while ((c = getopt_long(argc, argv, "i:",
10867 long_opts, NULL)) != -1) {
10870 archive_id = strtoul(optarg, &end, 0);
10871 if (*end != '\0' || archive_id == 0) {
10872 fprintf(stderr, "error: %s: bad archive ID "
10873 "'%s'\n", argv[0], optarg);
10880 fprintf(stderr, "%s: option '%s' unrecognized\n",
10881 argv[0], argv[optind - 1]);
10886 if (archive_id == 0) {
10887 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
10891 if (argc <= optind) {
10892 fprintf(stderr, "%s: must specify one or more file names\n",
10897 while (optind < argc) {
10900 path = argv[optind++];
10901 if (realpath(path, fullpath) == NULL) {
10902 fprintf(stderr, "%s: could not find path '%s': %s\n",
10903 argv[0], path, strerror(errno));
10909 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10911 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10912 "with archive ID '%u': %s\n", argv[0],
10913 path, archive_id, strerror(-rc2));
10921 static int lfs_pcc_attach_fid(int argc, char **argv)
10923 struct option long_opts[] = {
10924 { .val = 'i', .name = "id", .has_arg = required_argument },
10925 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10926 { .name = NULL } };
10927 char short_opts[] = "i:m:";
10930 __u32 archive_id = 0;
10932 const char *mntpath = NULL;
10933 const char *fidstr;
10934 enum lu_pcc_type type = LU_PCC_READWRITE;
10937 while ((c = getopt_long(argc, argv, short_opts,
10938 long_opts, NULL)) != -1) {
10941 archive_id = strtoul(optarg, &end, 0);
10942 if (*end != '\0') {
10943 fprintf(stderr, "error: %s: bad archive ID "
10944 "'%s'\n", argv[0], optarg);
10954 fprintf(stderr, "%s: option '%s' unrecognized\n",
10955 argv[0], argv[optind - 1]);
10960 if (archive_id == 0) {
10961 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10965 if (mntpath == NULL) {
10966 fprintf(stderr, "%s: must specify Lustre mount point\n",
10971 if (argc <= optind) {
10972 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10976 while (optind < argc) {
10979 fidstr = argv[optind++];
10981 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10984 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10985 "with archive ID '%u': %s\n", argv[0],
10986 fidstr, mntpath, archive_id, strerror(rc2));
10988 if (rc == 0 && rc2 < 0)
10994 static int lfs_pcc_detach(int argc, char **argv)
10996 struct option long_opts[] = {
10997 { .val = 'k', .name = "keep", .has_arg = no_argument },
10998 { .name = NULL } };
10999 char short_opts[] = "k";
11003 char fullpath[PATH_MAX];
11004 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11007 while ((c = getopt_long(argc, argv, short_opts,
11008 long_opts, NULL)) != -1) {
11011 detach_opt = PCC_DETACH_OPT_NONE;
11016 fprintf(stderr, "%s: option '%s' unrecognized\n",
11017 argv[0], argv[optind - 1]);
11022 while (optind < argc) {
11025 path = argv[optind++];
11026 if (realpath(path, fullpath) == NULL) {
11027 fprintf(stderr, "%s: could not find path '%s': %s\n",
11028 argv[0], path, strerror(errno));
11034 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11037 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
11038 "%s\n", argv[0], path, strerror(errno));
11046 static int lfs_pcc_detach_fid(int argc, char **argv)
11048 struct option long_opts[] = {
11049 { .val = 'k', .name = "keep", .has_arg = no_argument },
11050 { .name = NULL } };
11051 char short_opts[] = "k";
11055 const char *mntpath;
11056 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11059 while ((c = getopt_long(argc, argv, short_opts,
11060 long_opts, NULL)) != -1) {
11063 detach_opt = PCC_DETACH_OPT_NONE;
11068 fprintf(stderr, "%s: option '%s' unrecognized\n",
11069 argv[0], argv[optind - 1]);
11074 mntpath = argv[optind++];
11076 while (optind < argc) {
11079 fid = argv[optind++];
11081 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11083 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
11084 "from PCC: %s\n", argv[0], fid, mntpath,
11093 static int lfs_pcc_state(int argc, char **argv)
11097 char fullpath[PATH_MAX];
11098 struct lu_pcc_state state;
11103 fprintf(stderr, "%s: must specify one or more file names\n",
11108 while (optind < argc) {
11111 path = argv[optind++];
11112 if (realpath(path, fullpath) == NULL) {
11113 fprintf(stderr, "%s: could not find path '%s': %s\n",
11114 argv[0], path, strerror(errno));
11120 rc2 = llapi_pcc_state_get(fullpath, &state);
11124 fprintf(stderr, "%s: cannot get PCC state of '%s': "
11125 "%s\n", argv[0], path, strerror(-rc2));
11129 printf("file: %s", path);
11130 printf(", type: %s", pcc_type2string(state.pccs_type));
11131 if (state.pccs_type == LU_PCC_NONE &&
11132 state.pccs_open_count == 0) {
11137 printf(", PCC file: %s", state.pccs_path);
11138 printf(", user number: %u", state.pccs_open_count);
11139 printf(", flags: %x", state.pccs_flags);
11146 * lfs_pcc_list_commands() - List lfs pcc commands.
11147 * @argc: The count of command line arguments.
11148 * @argv: Array of strings for command line arguments.
11150 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11152 * Return: 0 on success.
11154 static int lfs_pcc_list_commands(int argc, char **argv)
11156 char buffer[81] = "";
11158 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11165 * lfs_pcc() - Parse and execute lfs pcc commands.
11166 * @argc: The count of lfs pcc command line arguments.
11167 * @argv: Array of strings for lfs pcc command line arguments.
11169 * This function parses lfs pcc commands and performs the
11170 * corresponding functions specified in pcc_cmdlist[].
11172 * Return: 0 on success or an error code on failure.
11174 static int lfs_pcc(int argc, char **argv)
11176 char cmd[PATH_MAX];
11179 setlinebuf(stdout);
11181 Parser_init("lfs-pcc > ", pcc_cmdlist);
11183 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11185 program_invocation_short_name = cmd;
11187 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11189 rc = Parser_commands();
11191 return rc < 0 ? -rc : rc;
11194 static int lfs_list_commands(int argc, char **argv)
11196 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11198 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11203 int main(int argc, char **argv)
11207 /* Ensure that liblustreapi constructor has run */
11208 if (!llapi_liblustreapi_initialized())
11209 fprintf(stderr, "liblustreapi was not properly initialized\n");
11211 setlinebuf(stdout);
11214 Parser_init("lfs > ", cmdlist);
11216 progname = program_invocation_short_name; /* Used in error messages */
11218 llapi_set_command_name(argv[1]);
11219 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11220 llapi_clear_command_name();
11222 rc = Parser_commands();
11225 return rc < 0 ? -rc : rc;
11228 #ifdef _LUSTRE_IDL_H_
11229 /* Everything we need here should be included by lustreapi.h. */
11230 # error "lfs should not depend on lustre_idl.h"
11231 #endif /* _LUSTRE_IDL_H_ */