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 <block-grace>]\n"
516 " [--inode-grace <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 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
541 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
543 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
544 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
545 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
546 {"project", lfs_project, 0,
547 "Change or list project attribute for specified file or directory.\n"
548 "usage: project [-d|-r] <file|directory...>\n"
549 " list project ID and flags on file(s) or directories\n"
550 " project [-p id] [-s] [-r] <file|directory...>\n"
551 " set project ID and/or inherit flag for specified file(s) or directories\n"
552 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
553 " check project ID and flags on file(s) or directories, print outliers\n"
554 " project -C [-r] [-k] <file|directory...>\n"
555 " clear the project inherit flag and ID on the file or directory\n"
558 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
559 "usage: flushctx [-k] [mountpoint...]"},
560 {"changelog", lfs_changelog, 0,
561 "Show the metadata changes on an MDT."
562 "\nusage: changelog <mdtname> [startrec [endrec]]"},
563 {"changelog_clear", lfs_changelog_clear, 0,
564 "Indicate that old changelog records up to <endrec> are no longer of "
565 "interest to consumer <id>, allowing the system to free up space.\n"
566 "An <endrec> of 0 means all records.\n"
567 "usage: changelog_clear <mdtname> <id> <endrec>"},
568 {"fid2path", lfs_fid2path, 0,
569 "Resolve the full path(s) for given FID(s). For a specific hardlink "
570 "specify link number <linkno>.\n"
571 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
572 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
573 "usage: path2fid [--parents] <path> ..."},
574 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
575 "usage: rmfid <fsname|rootpath> <fid> ..."},
576 {"data_version", lfs_data_version, 0, "Display file data version for "
577 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
578 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
579 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
580 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
581 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
582 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
583 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
585 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
586 "[--archived] [--lost] <file> ..."},
587 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
588 "given files.\n" "usage: hsm_action <file> ..."},
589 {"hsm_archive", lfs_hsm_archive, 0,
590 "Archive file to external storage.\n"
591 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
593 {"hsm_restore", lfs_hsm_restore, 0,
594 "Restore file from external storage.\n"
595 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
596 {"hsm_release", lfs_hsm_release, 0,
597 "Release files from Lustre.\n"
598 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
599 {"hsm_remove", lfs_hsm_remove, 0,
600 "Remove file copy from external storage.\n"
601 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
603 " (FILE [FILE ...] | "
604 "--mntpath MOUNTPATH FID [FID ...])\n"
606 "Note: To remove an archived copy of a file already deleted from a "
608 "--mntpath option and a list of FIDs must be specified"
610 {"hsm_cancel", lfs_hsm_cancel, 0,
611 "Cancel requests related to specified files.\n"
612 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
613 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
614 "usage: swap_layouts <path1> <path2>"},
615 {"migrate", lfs_setstripe_migrate, 0,
616 "migrate a directory between MDTs.\n"
617 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
618 " [--mdt-hash|-H] <hash_type>\n"
619 " [--mdt-index|-m] <start_mdt_index>\n"
622 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
623 " it's the MDT index of first stripe\n"
624 "\tmdt_count: number of MDTs to stripe a directory over\n"
625 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
626 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
627 " all_char sum of characters % MDT_COUNT\n"
629 "migrate file objects from one OST "
630 "layout\nto another (may be not safe with concurent writes).\n"
632 "[--stripe-count|-c] <stripe_count>\n"
633 "[--overstripe-count|-C] <stripe_count>\n"
634 " [--stripe-index|-i] <start_ost_index>\n"
635 " [--stripe-size|-S] <stripe_size>\n"
636 " [--pool|-p] <pool_name>\n"
637 " [--ost|-o] <ost_indices>\n"
639 " [--non-block|-n]\n"
640 " [--non-direct|-D]\n"
641 " <file|directory>\n"
642 "\tstripe_count: number of OSTs to stripe a file over\n"
643 "\t Using -C instead of -c allows overstriping, which\n"
644 "\t will place more than one stripe per OST if\n"
645 "\t stripe_count is greater than the number of OSTs\n"
646 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
647 "\tstripe_size: number of bytes to store before moving to the next OST\n"
648 "\tpool_name: name of the predefined pool of OSTs\n"
649 "\tost_indices: OSTs to stripe over, in order\n"
650 "\tblock: Block file access during data migration (default)\n"
651 "\tnon-block: Abort migrations if concurrent access is detected\n"
652 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
654 "To move directories between MDTs. This command is deprecated, "
655 "use \"migrate\" instead.\n"
656 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
658 {"ladvise", lfs_ladvise, 0,
659 "Provide servers with advice about access patterns for a file.\n"
660 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
661 " [--background|-b] [--unset|-u]\n\n"
662 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
663 " {[--mode|-m [READ,WRITE]}\n"
665 {"mirror", lfs_mirror, mirror_cmdlist,
666 "lfs commands used to manage files with mirrored components:\n"
667 "lfs mirror create - create a mirrored file or directory\n"
668 "lfs mirror extend - add mirror(s) to an existing file\n"
669 "lfs mirror split - split a mirror from an existing mirrored file\n"
670 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
671 "lfs mirror read - read a mirror content of a mirrored file\n"
672 "lfs mirror write - write to a mirror of a mirrored file\n"
673 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
674 "lfs mirror verify - verify mirrored file(s)\n"},
675 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
676 "usage: getsom [-s] [-b] [-f] <path>\n"
677 "\t-s: Only show the size value of the SOM data for a given file\n"
678 "\t-b: Only show the blocks value of the SOM data for a given file\n"
679 "\t-f: Only show the flags value of the SOM data for a given file\n"},
680 {"heat_get", lfs_heat_get, 0,
681 "To get heat of files.\n"
682 "usage: heat_get <file> ...\n"},
683 {"heat_set", lfs_heat_set, 0,
684 "To set heat flags of files.\n"
685 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
686 "\t--clear|-c: Clear file heat for given files\n"
687 "\t--off|-o: Turn off file heat for given files\n"
688 "\t--on|-O: Turn on file heat for given files\n"},
689 {"pcc", lfs_pcc, pcc_cmdlist,
690 "lfs commands used to interact with PCC features:\n"
691 "lfs pcc attach - attach given files to Persistent Client Cache\n"
692 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
693 "lfs pcc state - display the PCC state for given files\n"
694 "lfs pcc detach - detach given files from Persistent Client Cache\n"
695 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
696 {"help", Parser_help, 0, "help"},
697 {"exit", Parser_quit, 0, "quit"},
698 {"quit", Parser_quit, 0, "quit"},
699 {"--version", Parser_version, 0,
700 "output build version of the utility and exit"},
701 {"--list-commands", lfs_list_commands, 0,
702 "list commands supported by the utility and exit"},
707 static int check_hashtype(const char *hashtype)
711 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
712 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
718 static uint32_t check_foreign_type_name(const char *foreign_type_name)
722 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
723 if (lu_foreign_types[i].lft_name == NULL)
725 if (strcmp(foreign_type_name,
726 lu_foreign_types[i].lft_name) == 0)
727 return lu_foreign_types[i].lft_type;
730 return LU_FOREIGN_TYPE_UNKNOWN;
733 static const char *error_loc = "syserror";
736 MIGRATION_NONBLOCK = 0x0001,
737 MIGRATION_MIRROR = 0x0002,
738 MIGRATION_NONDIRECT = 0x0004,
739 MIGRATION_VERBOSE = 0x0008,
742 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
743 struct llapi_layout *layout);
746 migrate_open_files(const char *name, __u64 migration_flags,
747 const struct llapi_stripe_param *param,
748 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
755 char parent[PATH_MAX];
756 char volatile_file[PATH_MAX];
762 if (param == NULL && layout == NULL) {
763 error_loc = "layout information";
767 /* search for file directory pathname */
768 if (strlen(name) > sizeof(parent) - 1) {
769 error_loc = "source file name";
773 strncpy(parent, name, sizeof(parent));
774 ptr = strrchr(parent, '/');
776 if (getcwd(parent, sizeof(parent)) == NULL) {
777 error_loc = "getcwd";
781 if (ptr == parent) /* leading '/' */
786 /* open file, direct io */
787 /* even if the file is only read, WR mode is nedeed to allow
788 * layout swap on fd */
790 if (!(migration_flags & MIGRATION_NONDIRECT))
792 fd = open(name, rflags);
795 error_loc = "cannot open source file";
799 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
801 error_loc = "cannot get MDT index";
806 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
807 mode_t open_mode = S_IRUSR | S_IWUSR;
809 random_value = random();
810 rc = snprintf(volatile_file, sizeof(volatile_file),
811 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
812 mdt_index, random_value);
813 if (rc >= sizeof(volatile_file)) {
818 /* create, open a volatile file, use caching (ie no directio) */
820 fdv = llapi_file_open_param(volatile_file, open_flags,
823 fdv = lfs_component_create(volatile_file, open_flags,
825 } while (fdv < 0 && (rc = fdv) == -EEXIST);
828 error_loc = "cannot create volatile file";
832 /* In case the MDT does not support creation of volatile files
833 * we should try to unlink it. */
834 (void)unlink(volatile_file);
836 /* Not-owner (root?) special case.
837 * Need to set owner/group of volatile file like original.
838 * This will allow to pass related check during layout_swap.
843 error_loc = "cannot stat source file";
847 rc = fstat(fdv, &stv);
850 error_loc = "cannot stat volatile";
854 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
855 rc = fchown(fdv, st.st_uid, st.st_gid);
858 error_loc = "cannot change ownwership of volatile";
877 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
879 struct llapi_layout *layout;
880 size_t buf_size = 4 * 1024 * 1024;
889 layout = llapi_layout_get_by_fd(fd_src, 0);
890 if (layout != NULL) {
891 uint64_t stripe_size;
893 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
895 buf_size = stripe_size;
897 llapi_layout_free(layout);
900 /* Use a page-aligned buffer for direct I/O */
901 rc = posix_memalign(&buf, getpagesize(), buf_size);
906 /* read new data only if we have written all
907 * previously read data */
910 rc = check_file(fd_src);
915 rsize = read(fd_src, buf, buf_size);
927 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
946 static int migrate_copy_timestamps(int fd, int fdv)
950 if (fstat(fd, &st) == 0) {
951 struct timeval tv[2] = {
952 {.tv_sec = st.st_atime},
953 {.tv_sec = st.st_mtime}
956 return futimes(fdv, tv);
962 static int migrate_block(int fd, int fdv)
969 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
971 error_loc = "cannot get dataversion";
979 /* The grouplock blocks all concurrent accesses to the file.
980 * It has to be taken after llapi_get_data_version as it would
982 rc = llapi_group_lock(fd, gid);
984 error_loc = "cannot get group lock";
988 rc = migrate_copy_data(fd, fdv, NULL);
990 error_loc = "data copy failed";
994 /* Make sure we keep original atime/mtime values */
995 rc = migrate_copy_timestamps(fd, fdv);
997 error_loc = "timestamp copy failed";
1002 * for a migration we need to check data version on file did
1005 * Pass in gid=0 since we already own grouplock. */
1006 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1007 SWAP_LAYOUTS_CHECK_DV1);
1008 if (rc == -EAGAIN) {
1009 error_loc = "file changed";
1011 } else if (rc < 0) {
1012 error_loc = "cannot swap layout";
1017 rc2 = llapi_group_unlock(fd, gid);
1018 if (rc2 < 0 && rc == 0) {
1019 error_loc = "unlock group lock";
1027 * Internal helper for migrate_copy_data(). Check lease and report error if
1030 * \param[in] fd File descriptor on which to check the lease.
1032 * \retval 0 Migration can keep on going.
1033 * \retval -errno Error occurred, abort migration.
1035 static int check_lease(int fd)
1039 rc = llapi_lease_check(fd);
1041 return 0; /* llapi_check_lease returns > 0 on success. */
1046 static int migrate_nonblock(int fd, int fdv)
1052 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1054 error_loc = "cannot get data version";
1058 rc = migrate_copy_data(fd, fdv, check_lease);
1060 error_loc = "data copy failed";
1064 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1066 error_loc = "cannot get data version";
1072 error_loc = "source file changed";
1076 /* Make sure we keep original atime/mtime values */
1077 rc = migrate_copy_timestamps(fd, fdv);
1079 error_loc = "timestamp copy failed";
1086 static int lfs_component_set(char *fname, int comp_id,
1087 __u32 flags, __u32 neg_flags)
1090 __u32 flags_array[2];
1095 ids[count] = comp_id;
1096 flags_array[count] = flags;
1101 if (neg_flags & LCME_FL_STALE) {
1102 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1103 "component. Please use lfs-mirror-resync(1) "
1104 "instead\n", progname);
1108 ids[count] = comp_id;
1109 flags_array[count] = neg_flags | LCME_FL_NEG;
1113 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1115 if (errno == EUCLEAN) {
1118 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1119 progname, comp_id, fname);
1122 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1123 progname, comp_id, fname, flags, neg_flags);
1130 static int lfs_component_del(char *fname, __u32 comp_id,
1131 __u32 flags, __u32 neg_flags)
1135 if (flags && neg_flags)
1138 if (!flags && neg_flags)
1139 flags = neg_flags | LCME_FL_NEG;
1141 if ((flags && comp_id) || (!flags && !comp_id))
1145 if (flags & ~LCME_KNOWN_FLAGS) {
1147 "%s setstripe: unknown flags %#x\n",
1151 } else if (comp_id > LCME_ID_MAX) {
1152 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1157 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1160 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1161 progname, comp_id, fname, strerror(errno));
1165 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1172 rc = llapi_layout_file_comp_add(fname, layout);
1174 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1175 fname, strerror(errno));
1179 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1180 struct llapi_layout *layout)
1188 fd = lstat(fname, &st);
1189 if (fd == 0 && S_ISDIR(st.st_mode))
1190 open_flags = O_DIRECTORY | O_RDONLY;
1192 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1194 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1195 S_ISDIR(st.st_mode) ?
1196 "set default composite layout for" :
1197 "create composite file",
1198 fname, strerror(errno));
1202 static int lfs_migrate(char *name, __u64 migration_flags,
1203 struct llapi_stripe_param *param,
1204 struct llapi_layout *layout)
1206 struct llapi_layout *existing;
1207 uint64_t dom_new, dom_cur;
1212 rc = migrate_open_files(name, migration_flags, param, layout,
1217 rc = llapi_layout_dom_size(layout, &dom_new);
1219 error_loc = "cannot get new layout DoM size";
1222 /* special case for migration to DOM layout*/
1223 existing = llapi_layout_get_by_fd(fd, 0);
1225 error_loc = "cannot get existing layout";
1229 rc = llapi_layout_dom_size(existing, &dom_cur);
1231 error_loc = "cannot get current layout DoM size";
1235 /* if file has DoM layout already then migration is possible to
1236 * the new layout with the same DoM component via swap layout,
1237 * if new layout used bigger DOM size, then mirroring is used
1239 if (dom_new > dom_cur) {
1240 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1243 error_loc = "cannot migrate to DOM layout";
1247 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1248 /* Blocking mode (forced if servers do not support file lease).
1249 * It is also the default mode, since we cannot distinguish
1250 * between a broken lease and a server that does not support
1251 * atomic swap/close (LU-6785) */
1252 rc = migrate_block(fd, fdv);
1256 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1258 error_loc = "cannot get lease";
1262 rc = migrate_nonblock(fd, fdv);
1264 llapi_lease_release(fd);
1268 /* Atomically put lease, swap layouts and close.
1269 * for a migration we need to check data version on file did
1271 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1273 error_loc = "cannot swap layout";
1285 fprintf(stderr, "error: %s: %s: %s: %s\n",
1286 progname, name, error_loc, strerror(-rc));
1287 else if (migration_flags & MIGRATION_VERBOSE)
1288 printf("%s\n", name);
1293 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1302 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1306 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1307 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1308 const char *comp_name = comp_flags_table[i].cfn_name;
1310 if (strcmp(name, comp_name) == 0) {
1311 *flags |= comp_flag;
1313 } else if (strncmp(name, "^", 1) == 0 &&
1314 strcmp(name + 1, comp_name) == 0) {
1315 *neg_flags |= comp_flag;
1320 llapi_printf(LLAPI_MSG_ERROR,
1321 "%s: component flag '%s' not supported\n",
1327 if (!*flags && !*neg_flags)
1330 /* don't allow to set and exclude the same flag */
1331 if (*flags & *neg_flags)
1337 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1345 if (strncmp(string, "^", 1) == 0) {
1346 *neg_state = llapi_layout_string_flags(string + 1);
1347 if (*neg_state != 0)
1350 *state = llapi_layout_string_flags(string);
1355 llapi_printf(LLAPI_MSG_ERROR,
1356 "%s: mirrored file state '%s' not supported\n",
1362 * struct mirror_args - Command-line arguments for mirror(s).
1363 * @m_count: Number of mirrors to be created with this layout.
1364 * @m_flags: Mirror level flags, only 'prefer' is supported.
1365 * @m_layout: Mirror layout.
1366 * @m_file: A victim file. Its layout will be split and used as a mirror.
1367 * @m_next: Point to the next node of the list.
1369 * Command-line arguments for mirror(s) will be parsed and stored in
1370 * a linked list that consists of this structure.
1372 struct mirror_args {
1375 struct llapi_layout *m_layout;
1377 struct mirror_args *m_next;
1381 * enum mirror_flags - Flags for extending a mirrored file.
1382 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1383 * in case the victim file(s) contains the same data as the
1384 * original mirrored file.
1385 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1386 * @MF_COMP_ID: specified component id instead of mirror id
1388 * Flags for extending a mirrored file.
1398 * mirror_create_sanity_check() - Check mirror list.
1399 * @list: A linked list that stores the mirror arguments.
1401 * This function does a sanity check on @list for creating
1404 * Return: 0 on success or a negative error code on failure.
1406 static int mirror_create_sanity_check(const char *fname,
1407 struct mirror_args *list)
1410 bool has_m_file = false;
1411 bool has_m_layout = false;
1417 struct llapi_layout *layout;
1419 layout = llapi_layout_get_by_path(fname, 0);
1422 "error: %s: file '%s' couldn't get layout\n",
1427 rc = llapi_layout_sanity(layout, false, true);
1429 llapi_layout_free(layout);
1432 llapi_layout_sanity_perror(rc);
1437 while (list != NULL) {
1438 if (list->m_file != NULL) {
1440 llapi_layout_free(list->m_layout);
1443 llapi_layout_get_by_path(list->m_file, 0);
1444 if (list->m_layout == NULL) {
1446 "error: %s: file '%s' has no layout\n",
1447 progname, list->m_file);
1451 has_m_layout = true;
1452 if (list->m_layout == NULL) {
1453 fprintf(stderr, "error: %s: no mirror layout\n",
1459 rc = llapi_layout_sanity(list->m_layout, false, true);
1461 llapi_layout_sanity_perror(rc);
1465 list = list->m_next;
1468 if (has_m_file && has_m_layout) {
1470 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1478 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1480 __u32 mirror_flags = *(__u32 *)cbdata;
1484 rc = llapi_layout_comp_flags_get(layout, &flags);
1489 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1494 return LLAPI_LAYOUT_ITER_CONT;
1498 * mirror_create() - Create a mirrored file.
1499 * @fname: The file to be created.
1500 * @mirror_list: A linked list that stores the mirror arguments.
1502 * This function creates a mirrored file @fname with the mirror(s)
1503 * from @mirror_list.
1505 * Return: 0 on success or a negative error code on failure.
1507 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1509 struct llapi_layout *layout = NULL;
1510 struct mirror_args *cur_mirror = NULL;
1511 uint16_t mirror_count = 0;
1515 rc = mirror_create_sanity_check(NULL, mirror_list);
1519 cur_mirror = mirror_list;
1520 while (cur_mirror != NULL) {
1521 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1523 &cur_mirror->m_flags);
1526 fprintf(stderr, "%s: failed to set mirror flags\n",
1531 for (i = 0; i < cur_mirror->m_count; i++) {
1532 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1535 fprintf(stderr, "error: %s: "
1536 "merge layout failed: %s\n",
1537 progname, strerror(errno));
1541 mirror_count += cur_mirror->m_count;
1542 cur_mirror = cur_mirror->m_next;
1545 if (layout == NULL) {
1546 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1550 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1553 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1554 progname, strerror(errno));
1558 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1566 llapi_layout_free(layout);
1571 * Compare files and check lease on @fd.
1573 * \retval bytes number of bytes are the same
1575 static ssize_t mirror_file_compare(int fd, int fdv)
1577 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1579 ssize_t bytes_done = 0;
1580 ssize_t bytes_read = 0;
1582 buf = malloc(buflen * 2);
1587 if (!llapi_lease_check(fd)) {
1588 bytes_done = -EBUSY;
1592 bytes_read = read(fd, buf, buflen);
1593 if (bytes_read <= 0)
1596 if (bytes_read != read(fdv, buf + buflen, buflen))
1599 /* XXX: should compute the checksum on each buffer and then
1600 * compare checksum to avoid cache collision */
1601 if (memcmp(buf, buf + buflen, bytes_read))
1604 bytes_done += bytes_read;
1612 static int mirror_extend_file(const char *fname, const char *victim_file,
1613 enum mirror_flags mirror_flags)
1618 struct stat stbuf_v;
1619 struct ll_ioc_lease *data = NULL;
1622 fd = open(fname, O_RDWR);
1624 error_loc = "open source file";
1629 fdv = open(victim_file, O_RDWR);
1631 error_loc = "open target file";
1636 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1637 error_loc = "stat source or target file";
1642 if (stbuf.st_dev != stbuf_v.st_dev) {
1643 error_loc = "stat source and target file";
1648 /* mirrors should be of the same size */
1649 if (stbuf.st_size != stbuf_v.st_size) {
1650 error_loc = "file sizes don't match";
1655 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1657 error_loc = "cannot get lease";
1661 if (!(mirror_flags & MF_NO_VERIFY)) {
1663 /* mirrors should have the same contents */
1664 ret = mirror_file_compare(fd, fdv);
1665 if (ret != stbuf.st_size) {
1666 error_loc = "file busy or contents don't match";
1667 rc = ret < 0 ? ret : -EINVAL;
1672 /* Get rid of caching pages from clients */
1673 rc = llapi_file_flush(fd);
1675 error_loc = "cannot get data version";
1679 rc = llapi_file_flush(fdv);
1681 error_loc = "cannot get data version";
1686 /* Make sure we keep original atime/mtime values */
1687 rc = migrate_copy_timestamps(fd, fdv);
1689 error_loc = "cannot copy timestamp";
1693 /* Atomically put lease, merge layouts and close. */
1694 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1696 error_loc = "memory allocation";
1699 data->lil_mode = LL_LEASE_UNLCK;
1700 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1701 data->lil_count = 1;
1702 data->lil_ids[0] = fdv;
1703 rc = llapi_lease_set(fd, data);
1705 error_loc = "cannot merge layout";
1707 } else if (rc == 0) {
1709 error_loc = "lost lease lock";
1722 (void) unlink(victim_file);
1724 fprintf(stderr, "error: %s: %s: %s: %s\n",
1725 progname, fname, error_loc, strerror(-rc));
1729 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1731 struct ll_ioc_lease *data = NULL;
1736 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1740 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1742 error_loc = "cannot get lease";
1746 rc = migrate_nonblock(fd, fdv);
1748 llapi_lease_release(fd);
1752 /* Atomically put lease, merge layouts and close. */
1753 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1755 error_loc = "memory allocation";
1758 data->lil_mode = LL_LEASE_UNLCK;
1759 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1760 data->lil_count = 1;
1761 data->lil_ids[0] = fdv;
1762 rc = llapi_lease_set(fd, data);
1764 error_loc = "cannot merge layout";
1766 } else if (rc == 0) {
1768 error_loc = "lost lease lock";
1781 fprintf(stderr, "error: %s: %s: %s: %s\n",
1782 progname, name, error_loc, strerror(-rc));
1786 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1787 enum mirror_flags mirror_flags)
1791 rc = mirror_create_sanity_check(fname, mirror_list);
1795 while (mirror_list) {
1796 if (mirror_list->m_file != NULL) {
1797 rc = mirror_extend_file(fname, mirror_list->m_file,
1800 __u32 mirror_count = mirror_list->m_count;
1802 while (mirror_count > 0) {
1803 rc = mirror_extend_layout(fname,
1804 mirror_list->m_layout);
1814 mirror_list = mirror_list->m_next;
1820 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1825 rc = llapi_layout_mirror_id_get(layout, &id);
1829 if ((__u16)id == *(__u16 *)cbdata)
1830 return LLAPI_LAYOUT_ITER_STOP;
1832 return LLAPI_LAYOUT_ITER_CONT;
1835 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1840 rc = llapi_layout_comp_id_get(layout, &id);
1844 if (id == *(__u32 *)cbdata)
1845 return LLAPI_LAYOUT_ITER_STOP;
1847 return LLAPI_LAYOUT_ITER_CONT;
1850 struct pool_to_id_cbdata {
1854 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1856 char buf[LOV_MAXPOOLNAME + 1];
1857 struct pool_to_id_cbdata *d = (void *)cbdata;
1861 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1864 if (strcmp(d->pool, buf))
1865 return LLAPI_LAYOUT_ITER_CONT;
1867 rc = llapi_layout_mirror_id_get(layout, &id);
1872 return LLAPI_LAYOUT_ITER_STOP;
1875 struct collect_ids_data {
1881 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1883 struct collect_ids_data *cid = cbdata;
1887 rc = llapi_layout_mirror_id_get(layout, &id);
1891 if ((__u16)id != cid->cid_exclude) {
1894 for (i = 0; i < cid->cid_count; i++) {
1895 /* already collected the mirror id */
1896 if (id == cid->cid_ids[i])
1897 return LLAPI_LAYOUT_ITER_CONT;
1899 cid->cid_ids[cid->cid_count] = id;
1903 return LLAPI_LAYOUT_ITER_CONT;
1907 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1908 * @mirror_id: Mirror id to be checked.
1909 * @layout: Mirror component list.
1911 * This function checks if a mirror with specified @mirror_id is the last
1912 * non-stale mirror of a layout @layout.
1914 * Return: true or false.
1917 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1919 __u16 mirror_ids[128] = { 0 };
1920 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1922 .cid_exclude = mirror_id, };
1925 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1927 for (i = 0; i < cid.cid_count; i++) {
1928 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1931 comp_size = llapi_mirror_find_stale(layout, comp_array,
1932 ARRAY_SIZE(comp_array),
1941 static int mirror_split(const char *fname, __u32 id, const char *pool,
1942 enum mirror_flags mflags, const char *victim_file)
1944 struct llapi_layout *layout;
1945 char parent[PATH_MAX];
1946 char victim[PATH_MAX];
1947 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1949 struct ll_ioc_lease *data;
1950 uint16_t mirror_count;
1955 /* check fname contains mirror with mirror_id/comp_id */
1956 layout = llapi_layout_get_by_path(fname, 0);
1959 "error %s: file '%s' couldn't get layout\n",
1964 rc = llapi_layout_sanity(layout, false, true);
1966 llapi_layout_sanity_perror(rc);
1970 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1973 "error %s: file '%s' couldn't get mirror count\n",
1977 if (mirror_count < 2) {
1979 "error %s: file '%s' has %d component, cannot split\n",
1980 progname, fname, mirror_count);
1984 if (mflags & MF_COMP_POOL) {
1985 struct pool_to_id_cbdata data = { .pool = pool };
1987 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
1990 } else if (mflags & MF_COMP_ID) {
1991 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1992 id = mirror_id_of(id);
1994 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1997 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2000 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2002 "error %s: file '%s' does not contain mirror with id %u\n",
2003 progname, fname, id);
2007 fd = open(fname, O_RDWR);
2010 "error %s: open file '%s' failed: %s\n",
2011 progname, fname, strerror(errno));
2015 /* get victim file directory pathname */
2016 if (strlen(fname) > sizeof(parent) - 1) {
2017 fprintf(stderr, "error %s: file name of '%s' too long\n",
2022 strncpy(parent, fname, sizeof(parent));
2023 ptr = strrchr(parent, '/');
2025 if (getcwd(parent, sizeof(parent)) == NULL) {
2026 fprintf(stderr, "error %s: getcwd failed: %s\n",
2027 progname, strerror(errno));
2037 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2039 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2044 if (victim_file == NULL) {
2045 /* use a temp file to store the splitted layout */
2046 if (mflags & MF_DESTROY) {
2047 if (last_non_stale_mirror(id, layout)) {
2050 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2055 fdv = llapi_create_volatile_idx(parent, mdt_index,
2056 O_LOV_DELAY_CREATE);
2058 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2060 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2063 /* user specified victim file */
2064 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2069 "error %s: create victim file failed: %s\n",
2070 progname, strerror(errno));
2074 /* get lease lock of fname */
2075 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2078 "error %s: cannot get lease of file '%s': %d\n",
2079 progname, fname, rc);
2083 /* Atomatically put lease, split layouts and close. */
2084 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2090 data->lil_mode = LL_LEASE_UNLCK;
2091 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2092 data->lil_count = 2;
2093 data->lil_ids[0] = fdv;
2094 data->lil_ids[1] = id;
2095 rc = llapi_lease_set(fd, data);
2097 if (rc == 0) /* lost lease lock */
2100 "error %s: cannot split '%s': %s\n",
2101 progname, fname, strerror(-rc));
2112 llapi_layout_free(layout);
2117 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2118 __u16 *mirror_ids, int ids_nr);
2120 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2121 __u64 migration_flags,
2122 struct llapi_stripe_param *param,
2123 struct llapi_layout *layout)
2125 struct ll_ioc_lease *data = NULL;
2128 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2130 error_loc = "cannot get lease";
2134 /* Atomically put lease, merge layouts, resync and close. */
2135 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2137 error_loc = "memory allocation";
2140 data->lil_mode = LL_LEASE_UNLCK;
2141 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2142 data->lil_count = 1;
2143 data->lil_ids[0] = fdv;
2144 rc = llapi_lease_set(fd, data);
2146 error_loc = "cannot merge layout";
2148 } else if (rc == 0) {
2150 error_loc = "lost lease lock";
2156 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2158 error_loc = "cannot resync file";
2162 /* delete first mirror now */
2163 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2165 error_loc = "cannot delete old layout";
2173 fprintf(stderr, "error: %s: %s: %s: %s\n",
2174 progname, name, error_loc, strerror(-rc));
2175 else if (migration_flags & MIGRATION_VERBOSE)
2176 printf("%s\n", name);
2183 * Parse a string containing an target index list into an array of integers.
2185 * The input string contains a comma delimited list of individual
2186 * indices and ranges, for example "1,2-4,7". Add the indices into the
2187 * \a tgts array and remove duplicates.
2189 * \param[out] tgts array to store indices in
2190 * \param[in] size size of \a tgts array
2191 * \param[in] offset starting index in \a tgts
2192 * \param[in] arg string containing OST index list
2193 * \param[in/out] overstriping index list may contain duplicates
2195 * \retval positive number of indices in \a tgts
2196 * \retval -EINVAL unable to parse \a arg
2198 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2199 unsigned long long *pattern)
2203 int slots = size - offset;
2205 bool overstriped = false;
2211 end_of_loop = false;
2212 while (!end_of_loop) {
2213 int start_index = 0;
2216 char *endptr = NULL;
2220 ptr = strchrnul(arg, ',');
2222 end_of_loop = *ptr == '\0';
2225 start_index = strtol(arg, &endptr, 0);
2226 if (endptr == arg) /* no data at all */
2228 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2231 end_index = start_index;
2232 if (*endptr == '-') {
2233 end_index = strtol(endptr + 1, &endptr, 0);
2234 if (*endptr != '\0')
2236 if (end_index < start_index)
2240 for (i = start_index; i <= end_index && slots > 0; i++) {
2243 /* remove duplicate */
2244 for (j = 0; j < offset; j++) {
2245 if (tgts[j] == i && pattern &&
2246 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2248 else if (tgts[j] == i)
2254 if (j == offset) { /* check complete */
2260 if (slots == 0 && i < end_index)
2268 if (!end_of_loop && ptr != NULL)
2271 if (!overstriped && pattern)
2272 *pattern = LLAPI_LAYOUT_DEFAULT;
2274 return rc < 0 ? rc : nr;
2277 struct lfs_setstripe_args {
2278 unsigned long long lsa_comp_end;
2279 unsigned long long lsa_stripe_size;
2280 unsigned long long lsa_extension_size;
2281 long long lsa_stripe_count;
2282 long long lsa_stripe_off;
2283 __u32 lsa_comp_flags;
2284 __u32 lsa_comp_neg_flags;
2285 unsigned long long lsa_pattern;
2286 unsigned int lsa_mirror_count;
2288 bool lsa_first_comp;
2289 bool lsa_extension_comp;
2291 char *lsa_pool_name;
2294 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2296 unsigned int mirror_count = lsa->lsa_mirror_count;
2297 bool first_comp = lsa->lsa_first_comp;
2299 memset(lsa, 0, sizeof(*lsa));
2301 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2302 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2303 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2304 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2305 lsa->lsa_pool_name = NULL;
2307 lsa->lsa_mirror_count = mirror_count;
2308 lsa->lsa_first_comp = first_comp;
2312 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2313 * @lsa: Stripe options to be initialized and inherited.
2315 * This function initializes stripe options in @lsa and inherit
2316 * stripe_size, stripe_count and OST pool_name options.
2320 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2322 unsigned long long stripe_size;
2323 long long stripe_count;
2324 char *pool_name = NULL;
2326 stripe_size = lsa->lsa_stripe_size;
2327 stripe_count = lsa->lsa_stripe_count;
2328 pool_name = lsa->lsa_pool_name;
2330 setstripe_args_init(lsa);
2332 lsa->lsa_stripe_size = stripe_size;
2333 lsa->lsa_stripe_count = stripe_count;
2334 lsa->lsa_pool_name = pool_name;
2337 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2339 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2340 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2341 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2342 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2343 lsa->lsa_comp_end != 0);
2347 * comp_args_to_layout() - Create or extend a composite layout.
2348 * @composite: Pointer to the composite layout.
2349 * @lsa: Stripe options for the new component.
2351 * This function creates or extends a composite layout by adding a new
2352 * component with stripe options from @lsa.
2354 * Return: 0 on success or an error code on failure.
2356 static int comp_args_to_layout(struct llapi_layout **composite,
2357 struct lfs_setstripe_args *lsa,
2360 struct llapi_layout *layout = *composite;
2361 uint64_t prev_end = 0;
2366 if (layout == NULL) {
2367 layout = llapi_layout_alloc();
2368 if (layout == NULL) {
2369 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2374 *composite = layout;
2375 lsa->lsa_first_comp = true;
2379 /* Get current component extent, current component
2380 * must be the tail component. */
2381 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2383 fprintf(stderr, "Get comp extent failed. %s\n",
2388 if (lsa->lsa_first_comp)
2391 if (lsa->lsa_first_comp)
2392 rc = llapi_layout_add_first_comp(layout);
2394 rc = llapi_layout_comp_add(layout);
2396 fprintf(stderr, "Add component failed. %s\n",
2402 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2404 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2405 lsa->lsa_comp_flags, strerror(errno));
2410 uint64_t comp_end = lsa->lsa_comp_end;
2412 /* The extendable component is 0-length, so it can be removed
2413 * if there is insufficient space to extend it. */
2414 if (lsa->lsa_extension_comp)
2415 comp_end = prev_end;
2417 rc = llapi_layout_comp_extent_set(layout, prev_end,
2420 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2421 prev_end, comp_end, strerror(errno));
2425 /* reset lsa_first_comp */
2426 lsa->lsa_first_comp = false;
2428 /* Data-on-MDT component setting */
2429 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2430 /* In case of Data-on-MDT patterns the only extra option
2431 * applicable is stripe size option. */
2432 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2433 fprintf(stderr, "Option 'stripe-count' can't be "
2434 "specified with Data-on-MDT component: %lld\n",
2435 lsa->lsa_stripe_count);
2439 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2440 fprintf(stderr, "Option 'stripe-size' can't be "
2441 "specified with Data-on-MDT component: %llu\n",
2442 lsa->lsa_stripe_size);
2446 if (lsa->lsa_nr_tgts != 0) {
2447 fprintf(stderr, "Option 'ost-list' can't be specified "
2448 "with Data-on-MDT component: '%i'\n",
2453 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2454 fprintf(stderr, "Option 'stripe-offset' can't be "
2455 "specified with Data-on-MDT component: %lld\n",
2456 lsa->lsa_stripe_off);
2460 if (lsa->lsa_pool_name != 0) {
2461 fprintf(stderr, "Option 'pool' can't be specified "
2462 "with Data-on-MDT component: '%s'\n",
2463 lsa->lsa_pool_name);
2468 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2470 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2475 /* Data-on-MDT component has always single stripe up to end */
2476 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2477 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2478 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2480 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2487 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2488 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2490 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2491 rc = llapi_layout_extension_size_set(layout, size);
2493 rc = llapi_layout_stripe_size_set(layout, size);
2496 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2497 size, strerror(errno));
2501 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2503 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2504 lsa->lsa_stripe_count, strerror(errno));
2508 if (lsa->lsa_pool_name != NULL) {
2509 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2511 fprintf(stderr, "Set pool name: %s failed. %s\n",
2512 lsa->lsa_pool_name, strerror(errno));
2516 rc = llapi_layout_pool_name_set(layout, "");
2518 fprintf(stderr, "Clear pool name failed: %s\n",
2524 if (lsa->lsa_nr_tgts > 0) {
2525 if (lsa->lsa_stripe_count > 0 &&
2526 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2527 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2528 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2529 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2530 lsa->lsa_stripe_count,
2535 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2536 rc = llapi_layout_ost_index_set(layout, i,
2541 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2542 lsa->lsa_stripe_off != -1) {
2543 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2546 fprintf(stderr, "Set ost index %d failed. %s\n",
2547 i, strerror(errno));
2551 /* Create the second, virtual component of extension space */
2552 if (lsa->lsa_extension_comp) {
2553 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2554 lsa->lsa_extension_comp = false;
2561 static int build_component(struct llapi_layout **layout,
2562 struct lfs_setstripe_args *lsa, bool set_extent)
2566 rc = comp_args_to_layout(layout, lsa, set_extent);
2570 if (lsa->lsa_mirror_count > 0) {
2571 rc = llapi_layout_mirror_count_set(*layout,
2572 lsa->lsa_mirror_count);
2576 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2579 lsa->lsa_mirror_count = 0;
2585 static int build_layout_from_yaml_node(struct cYAML *node,
2586 struct llapi_layout **layout,
2587 struct lfs_setstripe_args *lsa,
2594 if (node->cy_type == CYAML_TYPE_OBJECT) {
2595 /* go deep to sub blocks */
2596 rc = build_layout_from_yaml_node(node->cy_child, layout,
2601 if (node->cy_string == NULL)
2604 string = node->cy_string;
2605 /* skip leading lmm_ if present, to simplify parsing */
2606 if (strncmp(string, "lmm_", 4) == 0)
2609 if (node->cy_type == CYAML_TYPE_STRING) {
2610 if (!strcmp(string, "lcme_extent.e_end")) {
2611 if (!strcmp(node->cy_valuestring, "EOF") ||
2612 !strcmp(node->cy_valuestring, "eof"))
2613 lsa->lsa_comp_end = LUSTRE_EOF;
2614 } else if (!strcmp(string, "pool")) {
2615 lsa->lsa_pool_name = node->cy_valuestring;
2616 } else if (!strcmp(string, "pattern")) {
2617 if (!strcmp(node->cy_valuestring, "mdt"))
2618 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2619 if (!strcmp(node->cy_valuestring,
2620 "raid0,overstriped"))
2622 LLAPI_LAYOUT_OVERSTRIPING;
2623 } else if (!strcmp(string, "lcme_flags")) {
2624 rc = comp_str2flags(node->cy_valuestring,
2625 &lsa->lsa_comp_flags,
2626 &lsa->lsa_comp_neg_flags);
2629 /* Only template flags have meaning in
2630 * the layout for a new file
2632 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2634 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2635 if (!strcmp(string, "lcm_mirror_count")) {
2636 lsa->lsa_mirror_count = node->cy_valueint;
2637 } else if (!strcmp(string, "lcme_extent.e_start")) {
2638 if (node->cy_valueint != 0 || *layout != NULL) {
2639 rc = build_component(layout, lsa, true);
2644 if (node->cy_valueint == 0)
2645 lsa->lsa_first_comp = true;
2647 /* initialize lsa */
2648 setstripe_args_init(lsa);
2649 lsa->lsa_tgts = osts;
2650 } else if (!strcmp(string, "lcme_extent.e_end")) {
2651 if (node->cy_valueint == -1)
2652 lsa->lsa_comp_end = LUSTRE_EOF;
2654 lsa->lsa_comp_end = node->cy_valueint;
2655 } else if (!strcmp(string, "stripe_count")) {
2656 lsa->lsa_stripe_count = node->cy_valueint;
2657 } else if (!strcmp(string, "stripe_size")) {
2658 lsa->lsa_stripe_size = node->cy_valueint;
2659 } else if (!strcmp(string, "stripe_offset")) {
2660 lsa->lsa_stripe_off = node->cy_valueint;
2661 } else if (!strcmp(string, "l_ost_idx")) {
2662 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2667 node = node->cy_next;
2673 static int lfs_comp_create_from_yaml(char *template,
2674 struct llapi_layout **layout,
2675 struct lfs_setstripe_args *lsa,
2678 struct cYAML *tree = NULL, *err_rc = NULL;
2681 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2683 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2684 progname, template);
2685 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2686 "can't parse", &err_rc);
2687 cYAML_print_tree2file(stderr, err_rc);
2688 cYAML_free_tree(err_rc);
2693 /* initialize lsa for plain file */
2694 setstripe_args_init(lsa);
2695 lsa->lsa_tgts = osts;
2697 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2699 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2700 progname, template);
2703 rc = build_component(layout, lsa, *layout != NULL);
2705 /* clean clean lsa */
2706 setstripe_args_init(lsa);
2710 cYAML_free_tree(tree);
2715 * Get the extension size from the next (SEL) component and extend the
2716 * current component on it. The start of the next component is to be
2719 * \param[in] layout the current layout
2720 * \param[in] start the start of the current component
2721 * \param[in,out] end the end of the current component
2722 * \param[in] offset the offset to adjust the end position to instead of
2725 * \retval 0 - extended successfully
2726 * \retval < 0 - error
2728 static int layout_extend_comp(struct llapi_layout *layout,
2729 uint64_t start, uint64_t *end,
2732 uint64_t size, next_start, next_end;
2735 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2737 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2738 "%s\n", progname, strerror(errno));
2742 /* Even if the @size will not be used below, this will fail if
2743 * this is not a SEL component - a good confirmation we are
2744 * working on right components. */
2745 rc = llapi_layout_extension_size_get(layout, &size);
2747 fprintf(stderr, "%s setstripe: cannot get component ext size: "
2748 "%s\n", progname, strerror(errno));
2752 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2754 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2755 progname, strerror(errno));
2759 next_start += offset ?: size;
2760 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2762 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2763 progname, strerror(errno));
2767 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2769 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2770 "%s\n", progname, strerror(errno));
2774 *end += offset ?: size;
2775 rc = llapi_layout_comp_extent_set(layout, start, *end);
2777 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2778 progname, strerror(errno));
2786 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2787 * end of the last component in the existing file, and adjust the
2788 * first extent start of the components to be added accordingly.
2790 * In the create mode, we need to check if the first component is an extendable
2791 * SEL component and extend its length to the extension size (first component
2792 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2794 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2797 struct llapi_layout *head;
2798 uint64_t start = 0, prev_end = 0;
2807 head = llapi_layout_get_by_path(fname, 0);
2810 "%s setstripe: cannot read layout from '%s': "
2811 "%s\n", progname, fname, strerror(errno));
2813 } else if (errno == ENODATA) {
2814 /* file without LOVEA, this component-add will be turned
2815 * into a component-create. */
2816 llapi_layout_free(head);
2819 /* the new layout will be added to an empty one, it
2820 * still needs to be adjusted below */
2823 } else if (!llapi_layout_is_composite(head)) {
2824 fprintf(stderr, "%s setstripe: '%s' not a composite "
2825 "file\n", progname, fname);
2826 llapi_layout_free(head);
2830 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2832 fprintf(stderr, "%s setstripe: cannot get prev "
2833 "extent: %s\n", progname, strerror(errno));
2834 llapi_layout_free(head);
2838 llapi_layout_free(head);
2842 /* Make sure we use the first component of the layout to be added. */
2843 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2846 "%s setstripe: cannot move component cursor: %s\n",
2847 progname, strerror(errno));
2851 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2853 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2854 progname, strerror(errno));
2858 if (start == 0 && end == 0) {
2859 rc = layout_extend_comp(layout, start, &end,
2860 comp_add ? prev_end : 0);
2865 if (start > prev_end || end < prev_end) {
2866 fprintf(stderr, "%s setstripe: first extent [%lu, %lu) not "
2867 "adjacent with extent end %lu\n",
2868 progname, start, end, prev_end);
2872 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2874 fprintf(stderr, "%s setstripe: cannot set component extent "
2876 progname, prev_end, end, strerror(errno));
2883 static int mirror_adjust_first_extents(struct mirror_args *list)
2890 while (list != NULL) {
2891 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
2894 list = list->m_next;
2900 static inline bool arg_is_eof(char *arg)
2902 return !strncmp(arg, "-1", strlen("-1")) ||
2903 !strncmp(arg, "EOF", strlen("EOF")) ||
2904 !strncmp(arg, "eof", strlen("eof"));
2908 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2910 * Return: Valid mirror_args pointer on success and
2911 * NULL if memory allocation fails.
2913 static struct mirror_args *lfs_mirror_alloc(void)
2915 struct mirror_args *mirror = NULL;
2918 mirror = calloc(1, sizeof(*mirror));
2929 * lfs_mirror_free() - Free memory allocated for a mirror argument
2931 * @mirror: Previously allocated mirror argument structure by
2932 * lfs_mirror_alloc().
2934 * Free memory allocated for @mirror.
2938 static void lfs_mirror_free(struct mirror_args *mirror)
2940 if (mirror->m_layout != NULL)
2941 llapi_layout_free(mirror->m_layout);
2946 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2947 * @mirror_list: Previously allocated mirror list.
2949 * Free memory allocated for @mirror_list.
2953 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2955 struct mirror_args *next_mirror = NULL;
2957 while (mirror_list != NULL) {
2958 next_mirror = mirror_list->m_next;
2959 lfs_mirror_free(mirror_list);
2960 mirror_list = next_mirror;
2972 LFS_COMP_NO_VERIFY_OPT,
2974 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2976 LFS_MIRROR_STATE_OPT,
2978 LFS_MIRROR_INDEX_OPT,
2979 LFS_LAYOUT_FOREIGN_OPT,
2985 static int lfs_setstripe_internal(int argc, char **argv,
2986 enum setstripe_origin opc)
2988 struct lfs_setstripe_args lsa = { 0 };
2989 struct llapi_stripe_param *param = NULL;
2990 struct find_param migrate_mdt_param = {
3000 unsigned long long size_units = 1;
3001 bool migrate_mode = false;
3002 bool migrate_mdt_mode = false;
3003 bool setstripe_mode = false;
3004 bool migration_block = false;
3005 __u64 migration_flags = 0;
3006 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3007 int comp_del = 0, comp_set = 0;
3010 struct llapi_layout *layout = NULL;
3011 struct llapi_layout **lpp = &layout;
3012 bool mirror_mode = false;
3013 bool has_m_file = false;
3014 __u32 mirror_count = 0;
3015 enum mirror_flags mirror_flags = 0;
3016 struct mirror_args *mirror_list = NULL;
3017 struct mirror_args *new_mirror = NULL;
3018 struct mirror_args *last_mirror = NULL;
3019 __u16 mirror_id = 0;
3021 bool from_yaml = false;
3022 bool from_copy = false;
3023 char *template = NULL;
3024 bool foreign_mode = false;
3026 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3027 char *mode_opt = NULL;
3028 mode_t previous_umask = 0;
3029 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3031 struct option long_opts[] = {
3032 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3033 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3034 /* --block is only valid in migrate mode */
3035 { .val = 'b', .name = "block", .has_arg = no_argument },
3036 { .val = LFS_COMP_ADD_OPT,
3037 .name = "comp-add", .has_arg = no_argument },
3038 { .val = LFS_COMP_ADD_OPT,
3039 .name = "component-add", .has_arg = no_argument },
3040 { .val = LFS_COMP_DEL_OPT,
3041 .name = "comp-del", .has_arg = no_argument },
3042 { .val = LFS_COMP_DEL_OPT,
3043 .name = "component-del", .has_arg = no_argument },
3044 { .val = LFS_COMP_FLAGS_OPT,
3045 .name = "comp-flags", .has_arg = required_argument },
3046 { .val = LFS_COMP_FLAGS_OPT,
3047 .name = "component-flags",
3048 .has_arg = required_argument },
3049 { .val = LFS_COMP_SET_OPT,
3050 .name = "comp-set", .has_arg = no_argument },
3051 { .val = LFS_COMP_SET_OPT,
3052 .name = "component-set",
3053 .has_arg = no_argument},
3054 { .val = LFS_COMP_NO_VERIFY_OPT,
3055 .name = "no-verify", .has_arg = no_argument},
3056 { .val = LFS_LAYOUT_FLAGS_OPT,
3057 .name = "flags", .has_arg = required_argument},
3058 { .val = LFS_LAYOUT_FOREIGN_OPT,
3059 .name = "foreign", .has_arg = optional_argument},
3060 { .val = LFS_MIRROR_ID_OPT,
3061 .name = "mirror-id", .has_arg = required_argument},
3062 { .val = LFS_MODE_OPT,
3063 .name = "mode", .has_arg = required_argument},
3064 { .val = LFS_LAYOUT_COPY,
3065 .name = "copy", .has_arg = required_argument},
3066 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3067 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3068 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3069 { .val = 'C', .name = "overstripe-count",
3070 .has_arg = required_argument},
3071 { .val = 'd', .name = "delete", .has_arg = no_argument},
3072 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3073 /* --non-direct is only valid in migrate mode */
3074 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3075 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3076 { .val = 'E', .name = "component-end",
3077 .has_arg = required_argument},
3078 { .val = 'f', .name = "file", .has_arg = required_argument },
3079 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3080 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3081 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3082 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3083 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3084 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3085 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3086 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3087 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3088 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3089 { .val = 'L', .name = "layout", .has_arg = required_argument },
3090 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3091 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3092 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3093 /* --non-block is only valid in migrate mode */
3094 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3095 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3096 { .val = 'o', .name = "ost", .has_arg = required_argument },
3097 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3098 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3099 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3101 { .val = 'p', .name = "pool", .has_arg = required_argument },
3102 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3103 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3104 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3105 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3106 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3107 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3108 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3109 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3110 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3111 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3112 /* --verbose is only valid in migrate mode */
3113 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3114 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3115 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3116 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3117 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3120 setstripe_args_init(&lsa);
3122 migrate_mode = (opc == SO_MIGRATE);
3123 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3124 setstripe_mode = (opc == SO_SETSTRIPE);
3125 if (opc == SO_MIRROR_DELETE) {
3127 mirror_flags = MF_DESTROY;
3130 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3132 while ((c = getopt_long(argc, argv,
3133 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3134 long_opts, NULL)) >= 0) {
3140 case LFS_COMP_ADD_OPT:
3143 case LFS_COMP_DEL_OPT:
3146 case LFS_COMP_FLAGS_OPT:
3147 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3148 &lsa.lsa_comp_neg_flags);
3151 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3152 fprintf(stderr, "%s: inverted flags are not supported\n",
3157 case LFS_COMP_SET_OPT:
3160 case LFS_COMP_NO_VERIFY_OPT:
3161 mirror_flags |= MF_NO_VERIFY;
3163 case LFS_MIRROR_ID_OPT:
3164 mirror_id = strtoul(optarg, &end, 0);
3165 if (*end != '\0' || mirror_id == 0) {
3167 "%s %s: invalid mirror ID '%s'\n",
3168 progname, argv[0], optarg);
3172 case LFS_LAYOUT_FLAGS_OPT: {
3175 /* check for numeric flags (foreign and mirror cases) */
3176 if (setstripe_mode && !mirror_mode && !last_mirror) {
3177 flags = strtoul(optarg, &end, 16);
3180 "%s %s: bad flags '%s'\n",
3181 progname, argv[0], optarg);
3187 if (!mirror_mode || !last_mirror) {
3188 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
3193 result = comp_str2flags(optarg, &last_mirror->m_flags,
3199 fprintf(stderr, "%s: inverted flags are not supported\n",
3204 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3206 "%s: unsupported mirror flags: %s\n",
3213 case LFS_LAYOUT_FOREIGN_OPT:
3214 if (optarg != NULL) {
3215 /* check pure numeric */
3216 type = strtoul(optarg, &end, 0);
3219 type = check_foreign_type_name(optarg);
3220 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3222 "%s %s: unrecognized foreign type '%s'\n",
3229 foreign_mode = true;
3233 if (mode_opt != NULL) {
3234 mode = strtoul(mode_opt, &end, 8);
3237 "%s %s: bad mode '%s'\n",
3238 progname, argv[0], mode_opt);
3241 previous_umask = umask(0);
3244 case LFS_LAYOUT_COPY:
3249 if (!migrate_mode) {
3251 "%s %s: -b|--block valid only for migrate command\n",
3255 migration_block = true;
3258 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3260 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3264 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3267 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3270 "%s %s: invalid stripe count '%s'\n",
3271 progname, argv[0], optarg);
3275 if (lsa.lsa_stripe_count == -1)
3276 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3279 /* delete the default striping pattern */
3281 if (opc == SO_MIRROR_SPLIT) {
3284 "%s %s: -d cannot used with -f\n",
3288 mirror_flags |= MF_DESTROY;
3292 if (!migrate_mode) {
3294 "%s %s: -D|--non-direct is valid "
3295 "only for migrate command\n",
3299 migration_flags |= MIGRATION_NONDIRECT;
3302 if (lsa.lsa_comp_end != 0) {
3303 result = comp_args_to_layout(lpp, &lsa, true);
3305 fprintf(stderr, "%s: invalid layout\n",
3310 setstripe_args_init_inherit(&lsa);
3313 if (arg_is_eof(optarg)) {
3314 lsa.lsa_comp_end = LUSTRE_EOF;
3316 result = llapi_parse_size(optarg,
3321 "%s %s: invalid component end '%s'\n",
3322 progname, argv[0], optarg);
3328 if (!migrate_mode) {
3329 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3333 lsa.lsa_pattern = check_hashtype(optarg);
3334 if (lsa.lsa_pattern == 0) {
3336 "%s %s: bad stripe hash type '%s'\n",
3337 progname, argv[0], optarg);
3342 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3345 "%s %s: invalid stripe offset '%s'\n",
3346 progname, argv[0], optarg);
3349 if (lsa.lsa_stripe_off == -1)
3350 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3353 comp_id = strtoul(optarg, &end, 0);
3354 if (*end != '\0' || comp_id == 0 ||
3355 comp_id > LCME_ID_MAX) {
3357 "%s %s: invalid component ID '%s'\n",
3358 progname, argv[0], optarg);
3363 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3365 "error: %s: invalid option: %s\n",
3366 progname, argv[optopt + 1]);
3369 if (opc == SO_MIRROR_EXTEND) {
3370 if (last_mirror == NULL) {
3372 "error: %s: '-N' must exist in front of '%s'\n",
3373 progname, argv[optopt + 1]);
3376 last_mirror->m_file = optarg;
3377 last_mirror->m_count = 1;
3380 if (mirror_list == NULL)
3381 mirror_list = lfs_mirror_alloc();
3382 mirror_list->m_file = optarg;
3387 if (strcmp(argv[optind - 1], "mdt") == 0) {
3388 /* Can be only the first component */
3389 if (layout != NULL) {
3391 fprintf(stderr, "error: 'mdt' layout "
3392 "can be only the first one\n");
3395 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3397 fprintf(stderr, "error: 'mdt' layout "
3398 "size is too big\n");
3401 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3402 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3404 fprintf(stderr, "error: layout '%s' is "
3405 "unknown, supported layouts are: "
3406 "'mdt', 'raid0'\n", argv[optind]);
3411 if (!migrate_mode) {
3413 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3417 migrate_mdt_mode = true;
3418 lsa.lsa_nr_tgts = parse_targets(tgts,
3419 sizeof(tgts) / sizeof(__u32),
3420 lsa.lsa_nr_tgts, optarg, NULL);
3421 if (lsa.lsa_nr_tgts < 0) {
3423 "%s %s: invalid MDT target(s) '%s'\n",
3424 progname, argv[0], optarg);
3428 lsa.lsa_tgts = tgts;
3429 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3430 lsa.lsa_stripe_off = tgts[0];
3433 if (!migrate_mode) {
3435 "%s %s: -n|--non-block valid only for migrate command\n",
3439 migration_flags |= MIGRATION_NONBLOCK;
3442 if (opc == SO_SETSTRIPE) {
3443 opc = SO_MIRROR_CREATE;
3447 if (optarg != NULL) {
3448 mirror_count = strtoul(optarg, &end, 0);
3449 if (*end != '\0' || mirror_count == 0) {
3451 "error: %s: bad mirror count: %s\n",
3458 new_mirror = lfs_mirror_alloc();
3459 new_mirror->m_count = mirror_count;
3461 if (mirror_list == NULL)
3462 mirror_list = new_mirror;
3464 if (last_mirror != NULL) {
3465 /* wrap up last mirror */
3466 if (lsa.lsa_comp_end == 0)
3467 lsa.lsa_comp_end = LUSTRE_EOF;
3469 result = comp_args_to_layout(lpp, &lsa, true);
3471 lfs_mirror_free(new_mirror);
3475 setstripe_args_init_inherit(&lsa);
3477 last_mirror->m_next = new_mirror;
3480 last_mirror = new_mirror;
3481 lpp = &last_mirror->m_layout;
3484 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3485 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3486 fprintf(stderr, "warning: '--ost-list' is "
3487 "deprecated, use '--ost' instead\n");
3489 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3491 "%s %s: -o|--ost incompatible with DoM layout\n",
3495 /* -o allows overstriping, and must note it because
3496 * parse_targets is shared with MDT striping, which
3497 * does not allow duplicates
3499 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3500 lsa.lsa_nr_tgts = parse_targets(tgts,
3501 sizeof(tgts) / sizeof(__u32),
3502 lsa.lsa_nr_tgts, optarg,
3504 if (lsa.lsa_nr_tgts < 0) {
3506 "%s %s: invalid OST target(s) '%s'\n",
3507 progname, argv[0], optarg);
3511 lsa.lsa_tgts = tgts;
3512 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3513 lsa.lsa_stripe_off = tgts[0];
3518 lsa.lsa_pool_name = optarg;
3520 if (strlen(lsa.lsa_pool_name) == 0 ||
3521 strncmp(lsa.lsa_pool_name, "none",
3522 LOV_MAXPOOLNAME) == 0)
3523 lsa.lsa_pool_name = NULL;
3526 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3530 "%s %s: invalid stripe size '%s'\n",
3531 progname, argv[0], optarg);
3536 if (!migrate_mode) {
3538 "%s %s: -v|--verbose valid only for migrate command\n",
3542 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3543 migration_flags = MIGRATION_VERBOSE;
3553 result = llapi_parse_size(optarg,
3554 &lsa.lsa_extension_size,
3558 "%s %s: invalid extension size '%s'\n",
3559 progname, argv[0], optarg);
3563 lsa.lsa_extension_comp = true;
3566 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3567 progname, argv[0], argv[optind - 1]);
3572 fname = argv[optind];
3574 if (optind == argc) {
3575 fprintf(stderr, "%s %s: FILE must be specified\n",
3580 if (xattr && !foreign_mode) {
3581 /* only print a warning as this is harmless and will be ignored
3584 "%s %s: xattr has been specified for non-foreign layout\n",
3586 } else if (foreign_mode && !xattr) {
3588 "%s %s: xattr must be provided in foreign mode\n",
3593 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3594 comp_set || comp_id || delete || from_copy ||
3595 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3598 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3603 if (mirror_mode && mirror_count == 0) {
3605 "error: %s: --mirror-count|-N option is required\n",
3612 if (lsa.lsa_comp_end == 0)
3613 lsa.lsa_comp_end = LUSTRE_EOF;
3616 if (lsa.lsa_comp_end != 0) {
3617 result = comp_args_to_layout(lpp, &lsa, true);
3619 fprintf(stderr, "error: %s: invalid layout\n",
3626 if (mirror_flags & MF_NO_VERIFY) {
3627 if (opc != SO_MIRROR_EXTEND) {
3629 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3633 } else if (!has_m_file) {
3635 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3642 if (comp_set && !comp_id) {
3643 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3648 if ((delete + comp_set + comp_del + comp_add) > 1) {
3650 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3655 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3656 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3658 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3663 if ((comp_set || comp_del) &&
3664 (setstripe_args_specified(&lsa) || layout != NULL)) {
3666 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3671 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3673 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3678 if (comp_add || comp_del) {
3681 result = lstat(fname, &st);
3682 if (result == 0 && S_ISDIR(st.st_mode)) {
3684 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3690 fprintf(stderr, "error: %s: can't use --component-add "
3691 "or --component-del for mirror operation\n",
3698 if (layout == NULL) {
3700 "%s %s: option -E must be specified with --component-add\n",
3706 if (layout != NULL || mirror_list != NULL) {
3708 result = mirror_adjust_first_extents(mirror_list);
3710 result = layout_adjust_first_extent(fname, layout,
3712 if (result == -ENODATA)
3714 else if (result != 0) {
3715 fprintf(stderr, "error: %s: invalid layout\n",
3721 if (from_yaml && from_copy) {
3723 "%s: can't specify --yaml and --copy together\n",
3728 if ((from_yaml || from_copy) &&
3729 (setstripe_args_specified(&lsa) || layout != NULL)) {
3730 fprintf(stderr, "error: %s: can't specify --yaml with "
3731 "-c, -S, -i, -o, -p or -E options.\n",
3736 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3738 "%s %s: options --non-block and --block are mutually exclusive\n",
3743 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3744 opc != SO_MIRROR_DELETE && comp_id != 0) {
3746 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3751 if (migrate_mdt_mode) {
3752 struct lmv_user_md *lmu;
3754 /* initialize migrate mdt parameters */
3755 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3756 LMV_USER_MAGIC_SPECIFIC));
3759 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3760 progname, argv[0], strerror(ENOMEM));
3764 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3765 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3766 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3768 "%s %s: migrate should specify MDT index\n",
3773 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3774 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3775 lmu->lum_hash_type = lsa.lsa_pattern;
3777 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3778 if (lsa.lsa_pool_name) {
3779 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3780 sizeof(lmu->lum_pool_name) - 1);
3781 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3783 if (lsa.lsa_nr_tgts > 1) {
3786 if (lsa.lsa_stripe_count > 0 &&
3787 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3788 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3790 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3791 progname, lsa.lsa_stripe_count,
3797 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3798 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3799 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3800 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3802 lmu->lum_magic = LMV_USER_MAGIC;
3805 migrate_mdt_param.fp_lmv_md = lmu;
3806 migrate_mdt_param.fp_migrate = 1;
3807 } else if (layout == NULL) {
3808 /* initialize stripe parameters */
3809 param = calloc(1, offsetof(typeof(*param),
3810 lsp_osts[lsa.lsa_nr_tgts]));
3811 if (param == NULL) {
3813 "%s %s: cannot allocate memory for parameters: %s\n",
3814 progname, argv[0], strerror(ENOMEM));
3819 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3820 param->lsp_stripe_size = lsa.lsa_stripe_size;
3821 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3822 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3823 param->lsp_stripe_count = -1;
3825 param->lsp_stripe_count = lsa.lsa_stripe_count;
3827 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3828 param->lsp_stripe_offset = -1;
3830 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3831 param->lsp_stripe_pattern =
3832 llapi_pattern_to_lov(lsa.lsa_pattern);
3833 if (param->lsp_stripe_pattern == EINVAL) {
3834 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3839 param->lsp_pool = lsa.lsa_pool_name;
3840 param->lsp_is_specific = false;
3841 if (lsa.lsa_nr_tgts > 0) {
3842 if (lsa.lsa_stripe_count > 0 &&
3843 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3844 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3845 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3847 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3848 argv[0], lsa.lsa_stripe_count,
3854 param->lsp_is_specific = true;
3855 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3856 memcpy(param->lsp_osts, tgts,
3857 sizeof(*tgts) * lsa.lsa_nr_tgts);
3862 /* generate a layout from a YAML template */
3863 result = lfs_comp_create_from_yaml(template, &layout,
3866 fprintf(stderr, "error: %s: can't create composite "
3867 "layout from template file %s\n",
3871 } else if (from_copy) {
3872 layout = llapi_layout_get_by_path(template, 0);
3873 if (layout == NULL) {
3875 "%s: can't create composite layout from file %s.\n",
3876 progname, template);
3881 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3882 if (migrate_mdt_mode) {
3883 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3884 } else if (migrate_mode) {
3885 result = lfs_migrate(fname, migration_flags, param,
3887 } else if (comp_set != 0) {
3888 result = lfs_component_set(fname, comp_id,
3890 lsa.lsa_comp_neg_flags);
3891 } else if (comp_del != 0) {
3892 result = lfs_component_del(fname, comp_id,
3894 lsa.lsa_comp_neg_flags);
3895 } else if (comp_add != 0) {
3896 result = lfs_component_add(fname, layout);
3897 } else if (opc == SO_MIRROR_CREATE) {
3898 result = mirror_create(fname, mirror_list);
3899 } else if (opc == SO_MIRROR_EXTEND) {
3900 result = mirror_extend(fname, mirror_list,
3902 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
3903 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3905 "%s: no mirror specified to delete from '%s'\n",
3909 if (lsa.lsa_pool_name)
3910 mirror_flags |= MF_COMP_POOL;
3911 else if (mirror_id != 0)
3912 comp_id = mirror_id;
3914 mirror_flags |= MF_COMP_ID;
3915 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3917 has_m_file ? mirror_list->m_file :
3919 } else if (layout != NULL) {
3920 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3926 } else if (foreign_mode) {
3927 result = llapi_file_create_foreign(fname, mode, type,
3934 result = llapi_file_open_param(fname,
3943 /* Save the first error encountered. */
3950 if (mode_opt != NULL)
3951 umask(previous_umask);
3954 free(migrate_mdt_param.fp_lmv_md);
3955 llapi_layout_free(layout);
3956 lfs_mirror_list_free(mirror_list);
3961 llapi_layout_free(layout);
3962 lfs_mirror_list_free(mirror_list);
3966 static int lfs_poollist(int argc, char **argv)
3971 return llapi_poollist(argv[1]);
3974 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3979 char *endptr = "AD";
3984 else if (str[0] == '-')
3990 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3991 long long val = strtoll(timebuf, &endptr, 0);
3996 unit *= 52; /* 52 weeks + 1 day below */
3997 case 'w': /* fallthrough */
3999 case '\0': /* days are default unit if none used */
4000 case 'd': /* fallthrough */
4001 unit = (unit + (*endptr == 'y')) * 24;
4002 case 'h': /* fallthrough */
4004 case 'm': /* fallthrough */
4006 case 's': /* fallthrough */
4008 /* don't need to multiply by 1 for seconds */
4011 "%s find: bad time string '%s': %s\n",
4012 progname, timebuf, strerror(EINVAL));
4016 if (param->fp_time_margin == 0 ||
4017 (*endptr && unit < param->fp_time_margin))
4018 param->fp_time_margin = unit;
4025 fprintf(stderr, "%s find: bad time '%s': too large\n",
4035 static int name2uid(unsigned int *id, const char *name)
4037 struct passwd *passwd;
4039 passwd = getpwnam(name);
4042 *id = passwd->pw_uid;
4047 static int name2gid(unsigned int *id, const char *name)
4049 struct group *group;
4051 group = getgrnam(name);
4054 *id = group->gr_gid;
4059 static inline int name2projid(unsigned int *id, const char *name)
4064 static int uid2name(char **name, unsigned int id)
4066 struct passwd *passwd;
4068 passwd = getpwuid(id);
4071 *name = passwd->pw_name;
4076 static inline int gid2name(char **name, unsigned int id)
4078 struct group *group;
4080 group = getgrgid(id);
4083 *name = group->gr_name;
4088 static int name2layout(__u32 *layout, char *name)
4090 char *ptr, *layout_name;
4093 for (ptr = name; ; ptr = NULL) {
4094 layout_name = strtok(ptr, ",");
4095 if (layout_name == NULL)
4097 if (strcmp(layout_name, "released") == 0)
4098 *layout |= LOV_PATTERN_F_RELEASED;
4099 else if (strcmp(layout_name, "raid0") == 0)
4100 *layout |= LOV_PATTERN_RAID0;
4101 else if (strcmp(layout_name, "mdt") == 0)
4102 *layout |= LOV_PATTERN_MDT;
4103 else if (strcmp(layout_name, "overstriping") == 0)
4104 *layout |= LOV_PATTERN_OVERSTRIPING;
4111 static int lfs_find(int argc, char **argv)
4116 struct find_param param = {
4119 .fp_time_margin = 24 * 60 * 60,
4121 struct option long_opts[] = {
4122 { .val = 'A', .name = "atime", .has_arg = required_argument },
4123 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4124 { .val = LFS_COMP_COUNT_OPT,
4125 .name = "comp-count", .has_arg = required_argument },
4126 { .val = LFS_COMP_COUNT_OPT,
4127 .name = "component-count",
4128 .has_arg = required_argument },
4129 { .val = LFS_COMP_FLAGS_OPT,
4130 .name = "comp-flags", .has_arg = required_argument },
4131 { .val = LFS_COMP_FLAGS_OPT,
4132 .name = "component-flags",
4133 .has_arg = required_argument },
4134 { .val = LFS_COMP_START_OPT,
4135 .name = "comp-start", .has_arg = required_argument },
4136 { .val = LFS_COMP_START_OPT,
4137 .name = "component-start",
4138 .has_arg = required_argument },
4139 { .val = LFS_MIRROR_STATE_OPT,
4140 .name = "mirror-state", .has_arg = required_argument },
4141 { .val = LFS_LAYOUT_FOREIGN_OPT,
4142 .name = "foreign", .has_arg = optional_argument},
4143 { .val = LFS_NEWERXY_OPT,
4144 .name = "newer", .has_arg = required_argument},
4145 { .val = LFS_NEWERXY_OPT,
4146 .name = "neweraa", .has_arg = required_argument},
4147 { .val = LFS_NEWERXY_OPT,
4148 .name = "neweram", .has_arg = required_argument},
4149 { .val = LFS_NEWERXY_OPT,
4150 .name = "newerac", .has_arg = required_argument},
4151 { .val = LFS_NEWERXY_OPT,
4152 .name = "newerma", .has_arg = required_argument},
4153 { .val = LFS_NEWERXY_OPT,
4154 .name = "newermm", .has_arg = required_argument},
4155 { .val = LFS_NEWERXY_OPT,
4156 .name = "newermc", .has_arg = required_argument},
4157 { .val = LFS_NEWERXY_OPT,
4158 .name = "newerca", .has_arg = required_argument},
4159 { .val = LFS_NEWERXY_OPT,
4160 .name = "newercm", .has_arg = required_argument},
4161 { .val = LFS_NEWERXY_OPT,
4162 .name = "newercc", .has_arg = required_argument},
4163 { .val = LFS_NEWERXY_OPT,
4164 .name = "newerat", .has_arg = required_argument},
4165 { .val = LFS_NEWERXY_OPT,
4166 .name = "newermt", .has_arg = required_argument},
4167 { .val = LFS_NEWERXY_OPT,
4168 .name = "newerct", .has_arg = required_argument},
4169 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4170 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4171 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4172 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4173 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4174 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4175 { .val = 'E', .name = "component-end",
4176 .has_arg = required_argument },
4177 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4178 { .val = LFS_LAYOUT_FOREIGN_OPT,
4179 .name = "foreign", .has_arg = optional_argument},
4180 { .val = 'g', .name = "gid", .has_arg = required_argument },
4181 { .val = 'G', .name = "group", .has_arg = required_argument },
4182 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4183 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4184 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4185 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4186 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4187 { .val = 'L', .name = "layout", .has_arg = required_argument },
4188 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4189 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4190 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4191 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4192 { .val = 'n', .name = "name", .has_arg = required_argument },
4193 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4194 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4195 { .val = 'O', .name = "obd", .has_arg = required_argument },
4196 { .val = 'O', .name = "ost", .has_arg = required_argument },
4197 /* no short option for pool yet, can be 'p' after 2.18 */
4198 { .val = LFS_POOL_OPT,
4199 .name = "pool", .has_arg = required_argument },
4200 { .val = '0', .name = "print0", .has_arg = no_argument },
4201 { .val = 'P', .name = "print", .has_arg = no_argument },
4202 { .val = LFS_PROJID_OPT,
4203 .name = "projid", .has_arg = required_argument },
4204 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4205 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4206 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4207 { .val = 's', .name = "size", .has_arg = required_argument },
4208 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4209 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4210 { .val = 't', .name = "type", .has_arg = required_argument },
4211 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4212 { .val = 'u', .name = "uid", .has_arg = required_argument },
4213 { .val = 'U', .name = "user", .has_arg = required_argument },
4214 { .val = 'z', .name = "extension-size",
4215 .has_arg = required_argument },
4216 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4217 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4218 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4232 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4233 while ((c = getopt_long_only(argc, argv,
4234 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4235 long_opts, &optidx)) >= 0) {
4240 /* '!' is part of option */
4241 /* when getopt_long_only() finds a string which is not
4242 * an option nor a known option argument it returns 1
4243 * in that case if we already have found pathstart and pathend
4244 * (i.e. we have the list of pathnames),
4245 * the only supported value is "!"
4247 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4248 if (!isoption && pathend != -1) {
4249 fprintf(stderr, "err: %s: filename|dirname must either "
4250 "precede options or follow options\n",
4255 if (!isoption && pathstart == -1)
4256 pathstart = optind - 1;
4257 if (isoption && pathstart != -1 && pathend == -1)
4258 pathend = optind - 2;
4264 /* unknown; opt is "!" or path component,
4265 * checking done above.
4267 if (strcmp(optarg, "!") == 0)
4271 xtime = ¶m.fp_atime;
4272 xsign = ¶m.fp_asign;
4273 param.fp_exclude_atime = !!neg_opt;
4274 /* no break, this falls through to 'C' for ctime */
4277 xtime = ¶m.fp_ctime;
4278 xsign = ¶m.fp_csign;
4279 param.fp_exclude_ctime = !!neg_opt;
4281 /* no break, this falls through to 'M' for mtime */
4284 xtime = ¶m.fp_mtime;
4285 xsign = ¶m.fp_msign;
4286 param.fp_exclude_mtime = !!neg_opt;
4288 rc = set_time(¶m, &t, xtime, optarg);
4289 if (rc == LONG_MAX) {
4297 if (optarg[0] == '+') {
4298 param.fp_blocks_sign = -1;
4300 } else if (optarg[0] == '-') {
4301 param.fp_blocks_sign = 1;
4305 param.fp_blocks_units = 1024;
4306 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4307 ¶m.fp_blocks_units, 0);
4309 fprintf(stderr, "error: bad blocks '%s'\n",
4313 param.fp_check_blocks = 1;
4314 param.fp_exclude_blocks = !!neg_opt;
4316 case LFS_COMP_COUNT_OPT:
4317 if (optarg[0] == '+') {
4318 param.fp_comp_count_sign = -1;
4320 } else if (optarg[0] == '-') {
4321 param.fp_comp_count_sign = 1;
4325 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4326 if (*endptr != '\0') {
4327 fprintf(stderr, "error: bad component count "
4331 param.fp_check_comp_count = 1;
4332 param.fp_exclude_comp_count = !!neg_opt;
4334 case LFS_COMP_FLAGS_OPT:
4335 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4336 ¶m.fp_comp_neg_flags);
4338 fprintf(stderr, "error: bad component flags "
4342 param.fp_check_comp_flags = 1;
4344 __u32 flags = param.fp_comp_neg_flags;
4345 param.fp_comp_neg_flags = param.fp_comp_flags;
4346 param.fp_comp_flags = flags;
4349 case LFS_COMP_START_OPT:
4350 if (optarg[0] == '+') {
4351 param.fp_comp_start_sign = -1;
4353 } else if (optarg[0] == '-') {
4354 param.fp_comp_start_sign = 1;
4358 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4359 ¶m.fp_comp_start_units, 0);
4361 fprintf(stderr, "error: bad component start "
4365 param.fp_check_comp_start = 1;
4366 param.fp_exclude_comp_start = !!neg_opt;
4368 case LFS_MIRROR_STATE_OPT:
4369 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4370 ¶m.fp_mirror_neg_state);
4373 "error: bad mirrored file state '%s'\n",
4377 param.fp_check_mirror_state = 1;
4379 __u16 state = param.fp_mirror_neg_state;
4380 param.fp_mirror_neg_state =
4381 param.fp_mirror_state;
4382 param.fp_mirror_state = state;
4386 if (optarg[0] == '+') {
4387 param.fp_stripe_count_sign = -1;
4389 } else if (optarg[0] == '-') {
4390 param.fp_stripe_count_sign = 1;
4394 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4395 if (*endptr != '\0') {
4396 fprintf(stderr,"error: bad stripe_count '%s'\n",
4401 param.fp_check_stripe_count = 1;
4402 param.fp_exclude_stripe_count = !!neg_opt;
4405 param.fp_max_depth = strtol(optarg, 0, 0);
4408 if (optarg[0] == '+') {
4409 param.fp_comp_end_sign = -1;
4411 } else if (optarg[0] == '-') {
4412 param.fp_comp_end_sign = 1;
4416 if (arg_is_eof(optarg)) {
4417 param.fp_comp_end = LUSTRE_EOF;
4418 param.fp_comp_end_units = 1;
4421 rc = llapi_parse_size(optarg,
4423 ¶m.fp_comp_end_units, 0);
4426 fprintf(stderr, "error: bad component end "
4430 param.fp_check_comp_end = 1;
4431 param.fp_exclude_comp_end = !!neg_opt;
4433 case LFS_LAYOUT_FOREIGN_OPT: {
4434 /* all types by default */
4435 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4437 if (optarg != NULL) {
4438 /* check pure numeric */
4439 type = strtoul(optarg, &endptr, 0);
4442 type = check_foreign_type_name(optarg);
4443 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4445 "%s %s: unknown foreign type '%s'\n",
4452 param.fp_foreign_type = type;
4453 param.fp_check_foreign = 1;
4454 param.fp_exclude_foreign = !!neg_opt;
4457 case LFS_NEWERXY_OPT: {
4463 time_t ref = time(NULL);
4465 /* no need to check bad options, they won't get here */
4466 if (strlen(long_opts[optidx].name) == 7) {
4467 x = long_opts[optidx].name[5];
4468 y = long_opts[optidx].name[6];
4472 static const char *const fmts[] = {
4473 "%Y-%m-%d %H:%M:%S",
4476 "%H:%M:%S", /* sometime today */
4485 for (i = 0; fmts[i] != NULL; i++) {
4488 /* Init for times relative to today */
4489 if (strncmp(fmts[i], "%H", 2) == 0)
4490 localtime_r(&ref, &tm);
4492 memset(&tm, 0, sizeof(tm));
4493 ptr = strptime(optarg, fmts[i], &tm);
4495 while (ptr && isspace(*ptr))
4497 if (ptr == optarg + strlen(optarg)) {
4505 "%s: invalid time '%s'\n",
4508 "supported formats are:\n ");
4509 for (i = 0; fmts[i] != NULL; i++)
4510 fprintf(stderr, "'%s', ",
4512 fprintf(stderr, "\n");
4519 struct stat statbuf;
4521 if (stat(optarg, &statbuf) < 0) {
4523 "%s: cannot stat file '%s': %s\n",
4532 ref = statbuf.st_atime;
4535 ref = statbuf.st_mtime;
4538 ref = statbuf.st_ctime;
4542 "%s: invalid Y argument: '%c'\n",
4551 xidx = NEWERXY_ATIME;
4554 xidx = NEWERXY_MTIME;
4557 xidx = NEWERXY_CTIME;
4561 "%s: invalid X argument: '%c'\n",
4568 newery = ¶m.fp_newery[xidx][negidx];
4574 *newery = *newery > ref ? ref : *newery;
4576 *newery = *newery > ref ? *newery : ref;
4578 param.fp_newerxy = 1;
4583 rc = name2gid(¶m.fp_gid, optarg);
4585 param.fp_gid = strtoul(optarg, &endptr, 10);
4586 if (*endptr != '\0') {
4587 fprintf(stderr, "Group/GID: %s cannot "
4588 "be found.\n", optarg);
4593 param.fp_exclude_gid = !!neg_opt;
4594 param.fp_check_gid = 1;
4597 param.fp_hash_type = check_hashtype(optarg);
4598 if (param.fp_hash_type == 0) {
4599 fprintf(stderr, "error: bad hash_type '%s'\n",
4604 param.fp_check_hash_type = 1;
4605 param.fp_exclude_hash_type = !!neg_opt;
4611 ret = name2layout(¶m.fp_layout, optarg);
4614 param.fp_exclude_layout = !!neg_opt;
4615 param.fp_check_layout = 1;
4619 rc = name2uid(¶m.fp_uid, optarg);
4621 param.fp_uid = strtoul(optarg, &endptr, 10);
4622 if (*endptr != '\0') {
4623 fprintf(stderr, "User/UID: %s cannot "
4624 "be found.\n", optarg);
4629 param.fp_exclude_uid = !!neg_opt;
4630 param.fp_check_uid = 1;
4633 param.fp_pattern = (char *)optarg;
4634 param.fp_exclude_pattern = !!neg_opt;
4637 if (optarg[0] == '+') {
4638 param.fp_mirror_count_sign = -1;
4640 } else if (optarg[0] == '-') {
4641 param.fp_mirror_count_sign = 1;
4645 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4646 if (*endptr != '\0') {
4648 "error: bad mirror count '%s'\n",
4652 param.fp_check_mirror_count = 1;
4653 param.fp_exclude_mirror_count = !!neg_opt;
4658 char *buf, *token, *next, *p;
4662 buf = strdup(optarg);
4668 param.fp_exclude_obd = !!neg_opt;
4671 while (token && *token) {
4672 token = strchr(token, ',');
4679 param.fp_exclude_mdt = !!neg_opt;
4680 param.fp_num_alloc_mdts += len;
4681 tmp = realloc(param.fp_mdt_uuid,
4682 param.fp_num_alloc_mdts *
4683 sizeof(*param.fp_mdt_uuid));
4689 param.fp_mdt_uuid = tmp;
4691 param.fp_exclude_obd = !!neg_opt;
4692 param.fp_num_alloc_obds += len;
4693 tmp = realloc(param.fp_obd_uuid,
4694 param.fp_num_alloc_obds *
4695 sizeof(*param.fp_obd_uuid));
4701 param.fp_obd_uuid = tmp;
4703 for (token = buf; token && *token; token = next) {
4704 struct obd_uuid *puuid;
4707 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4710 ¶m.fp_obd_uuid[param.fp_num_obds++];
4712 p = strchr(token, ',');
4719 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4724 strncpy(puuid->uuid, token,
4725 sizeof(puuid->uuid));
4732 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4736 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4738 "Pool name %s is too long (max %d)\n",
4739 optarg, LOV_MAXPOOLNAME);
4744 * We do check for empty pool because empty pool
4745 * is used to find V1 LOV attributes
4747 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4748 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4749 param.fp_exclude_pool = !!neg_opt;
4750 param.fp_check_pool = 1;
4752 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4753 case 'p': /* want this for --pool, to match getstripe/find */
4755 "warning: -p deprecated, use --print0 or -0\n");
4758 param.fp_zero_end = 1;
4760 case 'P': /* we always print, this option is a no-op */
4762 case LFS_PROJID_OPT:
4763 rc = name2projid(¶m.fp_projid, optarg);
4765 param.fp_projid = strtoul(optarg, &endptr, 10);
4766 if (*endptr != '\0') {
4768 "Invalid project ID: %s",
4774 param.fp_exclude_projid = !!neg_opt;
4775 param.fp_check_projid = 1;
4778 if (optarg[0] == '+') {
4779 param.fp_size_sign = -1;
4781 } else if (optarg[0] == '-') {
4782 param.fp_size_sign = 1;
4786 ret = llapi_parse_size(optarg, ¶m.fp_size,
4787 ¶m.fp_size_units, 0);
4789 fprintf(stderr, "error: bad file size '%s'\n",
4793 param.fp_check_size = 1;
4794 param.fp_exclude_size = !!neg_opt;
4797 if (optarg[0] == '+') {
4798 param.fp_stripe_size_sign = -1;
4800 } else if (optarg[0] == '-') {
4801 param.fp_stripe_size_sign = 1;
4805 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4806 ¶m.fp_stripe_size_units, 0);
4808 fprintf(stderr, "error: bad stripe_size '%s'\n",
4812 param.fp_check_stripe_size = 1;
4813 param.fp_exclude_stripe_size = !!neg_opt;
4816 param.fp_exclude_type = !!neg_opt;
4817 switch (optarg[0]) {
4819 param.fp_type = S_IFBLK;
4822 param.fp_type = S_IFCHR;
4825 param.fp_type = S_IFDIR;
4828 param.fp_type = S_IFREG;
4831 param.fp_type = S_IFLNK;
4834 param.fp_type = S_IFIFO;
4837 param.fp_type = S_IFSOCK;
4840 fprintf(stderr, "error: %s: bad type '%s'\n",
4847 if (optarg[0] == '+') {
4848 param.fp_mdt_count_sign = -1;
4850 } else if (optarg[0] == '-') {
4851 param.fp_mdt_count_sign = 1;
4855 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4856 if (*endptr != '\0') {
4857 fprintf(stderr, "error: bad mdt_count '%s'\n",
4862 param.fp_check_mdt_count = 1;
4863 param.fp_exclude_mdt_count = !!neg_opt;
4866 if (optarg[0] == '+') {
4867 param.fp_ext_size_sign = -1;
4869 } else if (optarg[0] == '-') {
4870 param.fp_ext_size_sign = 1;
4874 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4875 ¶m.fp_ext_size_units, 0);
4877 fprintf(stderr, "error: bad ext-size '%s'\n",
4881 param.fp_ext_size /= SEL_UNIT_SIZE;
4882 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4883 param.fp_check_ext_size = 1;
4884 param.fp_exclude_ext_size = !!neg_opt;
4892 if (pathstart == -1) {
4893 fprintf(stderr, "error: %s: no filename|pathname\n",
4897 } else if (pathend == -1) {
4903 rc = llapi_find(argv[pathstart], ¶m);
4906 pathbad = pathstart;
4908 } while (++pathstart < pathend);
4911 fprintf(stderr, "%s: failed for '%s': %s\n",
4912 progname, argv[pathbad], strerror(-rc));
4915 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4916 free(param.fp_obd_uuid);
4918 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4919 free(param.fp_mdt_uuid);
4924 static int lfs_getstripe_internal(int argc, char **argv,
4925 struct find_param *param)
4927 struct option long_opts[] = {
4928 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4929 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4930 { .val = LFS_COMP_COUNT_OPT,
4931 .name = "comp-count", .has_arg = no_argument },
4932 { .val = LFS_COMP_COUNT_OPT,
4933 .name = "component-count", .has_arg = no_argument },
4934 { .val = LFS_COMP_FLAGS_OPT,
4935 .name = "comp-flags", .has_arg = optional_argument },
4936 { .val = LFS_COMP_FLAGS_OPT,
4937 .name = "component-flags", .has_arg = optional_argument },
4938 { .val = LFS_COMP_START_OPT,
4939 .name = "comp-start", .has_arg = optional_argument },
4940 { .val = LFS_COMP_START_OPT,
4941 .name = "component-start", .has_arg = optional_argument },
4942 { .val = LFS_MIRROR_INDEX_OPT,
4943 .name = "mirror-index", .has_arg = required_argument },
4944 { .val = LFS_MIRROR_ID_OPT,
4945 .name = "mirror-id", .has_arg = required_argument },
4946 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4947 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4948 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4949 { .val = 'd', .name = "directory", .has_arg = no_argument },
4950 { .val = 'D', .name = "default", .has_arg = no_argument },
4951 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4952 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4953 { .val = 'F', .name = "fid", .has_arg = no_argument },
4954 { .val = 'g', .name = "generation", .has_arg = no_argument },
4955 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4956 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4957 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4958 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4959 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4960 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4961 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
4962 { .val = 'L', .name = "layout", .has_arg = no_argument },
4963 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4964 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4965 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4966 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4967 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4968 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4969 { .val = 'O', .name = "obd", .has_arg = required_argument },
4970 { .val = 'O', .name = "ost", .has_arg = required_argument },
4971 { .val = 'p', .name = "pool", .has_arg = no_argument },
4972 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4973 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4974 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4975 { .val = 'R', .name = "raw", .has_arg = no_argument },
4976 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4977 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4978 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4979 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4980 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4981 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4982 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4983 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4984 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
4985 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
4989 int pathstart = -1, pathend = -1;
4993 while ((c = getopt_long(argc, argv,
4994 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
4995 long_opts, NULL)) != -1) {
4999 /* '!' is part of option */
5000 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5001 if (!isoption && pathend != -1) {
5003 "error: %s: filename|dirname must either precede options or follow options\n",
5007 if (!isoption && pathstart == -1)
5008 pathstart = optind - 1;
5009 if (isoption && pathstart != -1 && pathend == -1)
5010 pathend = optind - 2;
5014 /* unknown: opt is "!" */
5015 if (strcmp(optarg, "!") == 0)
5019 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5020 param->fp_verbose |= VERBOSE_COUNT;
5021 param->fp_max_depth = 0;
5024 case LFS_COMP_COUNT_OPT:
5025 param->fp_verbose |= VERBOSE_COMP_COUNT;
5026 param->fp_max_depth = 0;
5028 case LFS_COMP_FLAGS_OPT:
5029 if (optarg != NULL) {
5030 rc = comp_str2flags(optarg,
5031 ¶m->fp_comp_flags,
5032 ¶m->fp_comp_neg_flags);
5034 fprintf(stderr, "error: %s bad "
5035 "component flags '%s'.\n",
5039 param->fp_check_comp_flags = 1;
5041 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5042 param->fp_max_depth = 0;
5045 case LFS_COMP_START_OPT:
5046 if (optarg != NULL) {
5048 if (tmp[0] == '+') {
5049 param->fp_comp_start_sign = -1;
5051 } else if (tmp[0] == '-') {
5052 param->fp_comp_start_sign = 1;
5055 rc = llapi_parse_size(tmp,
5056 ¶m->fp_comp_start,
5057 ¶m->fp_comp_start_units, 0);
5059 fprintf(stderr, "error: %s bad "
5060 "component start '%s'.\n",
5064 param->fp_check_comp_start = 1;
5067 param->fp_verbose |= VERBOSE_COMP_START;
5068 param->fp_max_depth = 0;
5071 case LFS_MIRROR_INDEX_OPT:
5072 if (optarg[0] == '+') {
5073 param->fp_mirror_index_sign = -1;
5075 } else if (optarg[0] == '-') {
5076 param->fp_mirror_index_sign = 1;
5080 param->fp_mirror_index = strtoul(optarg, &end, 0);
5081 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5082 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5084 "%s %s: invalid mirror index '%s'\n",
5085 progname, argv[0], optarg);
5088 if (param->fp_mirror_id != 0) {
5090 "%s %s: can't specify both mirror index and mirror ID\n",
5094 param->fp_check_mirror_index = 1;
5095 param->fp_exclude_mirror_index = !!neg_opt;
5097 case LFS_MIRROR_ID_OPT:
5098 if (optarg[0] == '+') {
5099 param->fp_mirror_id_sign = -1;
5101 } else if (optarg[0] == '-') {
5102 param->fp_mirror_id_sign = 1;
5106 param->fp_mirror_id = strtoul(optarg, &end, 0);
5107 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5108 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5110 "%s %s: invalid mirror ID '%s'\n",
5111 progname, argv[0], optarg);
5114 if (param->fp_mirror_index != 0) {
5116 "%s %s: can't specify both mirror index and mirror ID\n",
5120 param->fp_check_mirror_id = 1;
5121 param->fp_exclude_mirror_id = !!neg_opt;
5124 param->fp_max_depth = 0;
5127 param->fp_get_default_lmv = 1;
5130 if (optarg != NULL) {
5132 if (tmp[0] == '+') {
5133 param->fp_comp_end_sign = -1;
5135 } else if (tmp[0] == '-') {
5136 param->fp_comp_end_sign = 1;
5140 if (arg_is_eof(tmp)) {
5141 param->fp_comp_end = LUSTRE_EOF;
5142 param->fp_comp_end_units = 1;
5145 rc = llapi_parse_size(tmp,
5146 ¶m->fp_comp_end,
5147 ¶m->fp_comp_end_units, 0);
5150 fprintf(stderr, "error: %s bad "
5151 "component end '%s'.\n",
5155 param->fp_check_comp_end = 1;
5157 param->fp_verbose |= VERBOSE_COMP_END;
5158 param->fp_max_depth = 0;
5162 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5163 param->fp_verbose |= VERBOSE_DFID;
5164 param->fp_max_depth = 0;
5168 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5169 param->fp_verbose |= VERBOSE_GENERATION;
5170 param->fp_max_depth = 0;
5174 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5175 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5176 param->fp_max_depth = 0;
5180 if (optarg != NULL) {
5181 param->fp_comp_id = strtoul(optarg, &end, 0);
5182 if (*end != '\0' || param->fp_comp_id == 0 ||
5183 param->fp_comp_id > LCME_ID_MAX) {
5184 fprintf(stderr, "error: %s bad "
5185 "component id '%s'\n",
5189 param->fp_check_comp_id = 1;
5192 param->fp_max_depth = 0;
5193 param->fp_verbose |= VERBOSE_COMP_ID;
5197 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5198 param->fp_verbose |= VERBOSE_PATTERN;
5199 param->fp_max_depth = 0;
5202 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5204 fprintf(stderr, "warning: '-M' deprecated"
5205 ", use '--mdt-index' or '-m' instead\n");
5208 if (!(param->fp_verbose & VERBOSE_DETAIL))
5209 param->fp_max_depth = 0;
5210 param->fp_verbose |= VERBOSE_MDTINDEX;
5213 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5214 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5215 param->fp_max_depth = 0;
5219 if (param->fp_obd_uuid) {
5221 "error: %s: only one obduuid allowed",
5225 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5228 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5229 param->fp_verbose |= VERBOSE_POOL;
5230 param->fp_max_depth = 0;
5237 param->fp_recursive = 1;
5243 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5244 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5245 param->fp_max_depth = 0;
5249 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5255 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5256 param->fp_verbose |= VERBOSE_EXT_SIZE;
5257 param->fp_max_depth = 0;
5265 if (pathstart == -1) {
5266 fprintf(stderr, "error: %s: no filename|pathname\n",
5269 } else if (pathend == -1) {
5277 if (param->fp_recursive)
5278 param->fp_max_depth = -1;
5279 else if (param->fp_verbose & VERBOSE_DETAIL)
5280 param->fp_max_depth = 1;
5282 if (!param->fp_verbose)
5283 param->fp_verbose = VERBOSE_DEFAULT;
5284 if (param->fp_quiet)
5285 param->fp_verbose = VERBOSE_OBJID;
5288 rc = llapi_getstripe(argv[pathstart], param);
5289 } while (++pathstart < pathend && !rc);
5292 fprintf(stderr, "error: %s failed for %s.\n",
5293 argv[0], argv[optind - 1]);
5297 static int lfs_tgts(int argc, char **argv)
5299 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5300 struct find_param param;
5301 int index = 0, rc=0;
5306 if (argc == 2 && !realpath(argv[1], path)) {
5308 fprintf(stderr, "error: invalid path '%s': %s\n",
5309 argv[1], strerror(-rc));
5313 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5314 /* Check if we have a mount point */
5315 if (mntdir[0] == '\0')
5318 memset(¶m, 0, sizeof(param));
5319 if (!strcmp(argv[0], "mdts"))
5320 param.fp_get_lmv = 1;
5322 rc = llapi_ostlist(mntdir, ¶m);
5324 fprintf(stderr, "error: %s: failed on %s\n",
5327 if (path[0] != '\0')
5329 memset(mntdir, 0, PATH_MAX);
5335 static int lfs_getstripe(int argc, char **argv)
5337 struct find_param param = { 0 };
5339 param.fp_max_depth = 1;
5340 return lfs_getstripe_internal(argc, argv, ¶m);
5344 static int lfs_getdirstripe(int argc, char **argv)
5346 struct find_param param = { 0 };
5347 struct option long_opts[] = {
5348 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5349 { .val = 'D', .name = "default", .has_arg = no_argument },
5350 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5351 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5352 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5353 { .val = 'O', .name = "obd", .has_arg = required_argument },
5354 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5355 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5356 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5357 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5361 param.fp_get_lmv = 1;
5363 while ((c = getopt_long(argc, argv,
5364 "cDHimO:rtTvy", long_opts, NULL)) != -1)
5369 param.fp_verbose |= VERBOSE_COUNT;
5372 param.fp_get_default_lmv = 1;
5376 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5378 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5380 fprintf(stderr, "warning: '-t' deprecated, "
5381 "use '--mdt-hash' or '-H' instead\n");
5384 param.fp_verbose |= VERBOSE_HASH_TYPE;
5387 if (param.fp_obd_uuid) {
5389 "error: %s: only one obduuid allowed",
5393 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5396 param.fp_recursive = 1;
5399 param.fp_verbose |= VERBOSE_DETAIL;
5405 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5406 progname, argv[0], argv[optind - 1]);
5414 if (param.fp_recursive)
5415 param.fp_max_depth = -1;
5417 if (!param.fp_verbose)
5418 param.fp_verbose = VERBOSE_DEFAULT;
5421 rc = llapi_getstripe(argv[optind], ¶m);
5422 } while (++optind < argc && !rc);
5425 fprintf(stderr, "error: %s failed for %s.\n",
5426 argv[0], argv[optind - 1]);
5431 MNTDF_INODES = 0x0001,
5432 MNTDF_COOKED = 0x0002,
5433 MNTDF_LAZY = 0x0004,
5434 MNTDF_VERBOSE = 0x0008,
5435 MNTDF_SHOW = 0x0010,
5438 #define COOK(value) \
5441 while (value > 1024) { \
5449 #define CDF "%11llu"
5450 #define HDF "%8.1f%c"
5454 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5456 double avail, used, ratio = 0;
5459 avail = st->os_ffree;
5460 used = st->os_files - st->os_ffree;
5462 avail = st->os_bavail;
5463 used = st->os_blocks - st->os_bfree;
5465 if (avail + used > 0)
5466 ratio = used / (used + avail) * 100;
5468 /* Round up to match df(1) usage percentage */
5469 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5472 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5473 * so only show flags reflecting those states by default. Informational states
5474 * are only shown with "-v" and use lower-case names to distinguish them.
5475 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5477 static struct obd_statfs_state_names {
5478 enum obd_statfs_state osn_state;
5479 const char osn_name;
5482 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
5483 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
5484 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
5485 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
5486 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
5487 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
5488 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
5489 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
5490 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
5493 static int showdf(char *mntdir, struct obd_statfs *stat,
5494 char *uuid, enum mntdf_flags flags,
5495 char *type, int index, int rc)
5497 long long avail, used, total;
5499 char *suffix = "KMGTPEZY";
5500 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5501 char tbuf[3 * sizeof(__u64)];
5502 char ubuf[3 * sizeof(__u64)];
5503 char abuf[3 * sizeof(__u64)];
5504 char rbuf[3 * sizeof(__u64)];
5511 if (flags & MNTDF_INODES) {
5512 avail = stat->os_ffree;
5513 used = stat->os_files - stat->os_ffree;
5514 total = stat->os_files;
5516 int shift = flags & MNTDF_COOKED ? 0 : 10;
5518 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5519 used = ((stat->os_blocks - stat->os_bfree) *
5520 stat->os_bsize) >> shift;
5521 total = (stat->os_blocks * stat->os_bsize) >> shift;
5524 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5526 if (flags & MNTDF_COOKED) {
5530 cook_val = (double)total;
5533 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5536 snprintf(tbuf, sizeof(tbuf), CDF, total);
5538 cook_val = (double)used;
5541 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5544 snprintf(ubuf, sizeof(ubuf), CDF, used);
5546 cook_val = (double)avail;
5549 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5552 snprintf(abuf, sizeof(abuf), CDF, avail);
5554 snprintf(tbuf, sizeof(tbuf), CDF, total);
5555 snprintf(ubuf, sizeof(tbuf), CDF, used);
5556 snprintf(abuf, sizeof(tbuf), CDF, avail);
5559 sprintf(rbuf, RDF, ratio);
5560 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5561 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5563 printf("[%s:%d]", type, index);
5565 if (stat->os_state) {
5569 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5570 if (oss_names[i].osn_state & stat->os_state &&
5571 (oss_names[i].osn_err ||
5572 flags & MNTDF_VERBOSE))
5573 printf("%c", oss_names[i].osn_name);
5580 printf(UUF": inactive device\n", uuid);
5583 printf(UUF": %s\n", uuid, strerror(-rc));
5590 struct ll_stat_type {
5595 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5597 struct ll_statfs_data {
5599 struct obd_statfs sd_st;
5602 struct ll_statfs_buf {
5604 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5607 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5608 int ops, struct ll_statfs_buf *lsb)
5610 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5611 struct obd_uuid uuid_buf;
5612 char *poolname = NULL;
5613 struct ll_stat_type types[] = {
5614 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5615 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5616 { .st_name = NULL } };
5617 struct ll_stat_type *tp;
5618 __u64 ost_files = 0;
5619 __u64 ost_ffree = 0;
5627 poolname = strchr(pool, '.');
5628 if (poolname != NULL) {
5629 if (strncmp(fsname, pool, strlen(fsname))) {
5630 fprintf(stderr, "filesystem name incorrect\n");
5638 fd = open(mntdir, O_RDONLY);
5641 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5646 if (flags & MNTDF_SHOW) {
5647 if (flags & MNTDF_INODES)
5648 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5649 "UUID", "Inodes", "IUsed", "IFree",
5650 "IUse%", "Mounted on");
5652 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5654 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5655 "Used", "Available", "Use%", "Mounted on");
5658 for (tp = types; tp->st_name != NULL; tp++) {
5659 bool have_ost = false;
5661 if (!(tp->st_op & ops))
5664 for (index = 0; ; index++) {
5665 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5666 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5667 type = flags & MNTDF_LAZY ?
5668 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5669 rc2 = llapi_obd_fstatfs(fd, type, index,
5670 &stat_buf, &uuid_buf);
5675 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5676 if (!(flags & MNTDF_VERBOSE))
5678 } else if (rc2 < 0 && rc == 0) {
5682 /* If we have OSTs then don't report MDT block counts.
5683 * For MDT-only filesystems the expectation is that all
5684 * layouts have a DoM component. For filesystems with
5685 * OSTs, files are not necessarily going to store data
5686 * on MDTs, and MDT space is limited to a fraction of
5687 * OST space, so don't include it in the summary.
5689 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5696 if (poolname && tp->st_op == LL_STATFS_LOV &&
5697 llapi_search_ost(fsname, poolname,
5698 obd_uuid2str(&uuid_buf)) != 1)
5701 /* the llapi_obd_statfs() call may have returned with
5702 * an error, but if it filled in uuid_buf we will at
5703 * lease use that to print out a message for that OBD.
5704 * If we didn't get anything in the uuid_buf, then fill
5705 * it in so that we can print an error message. */
5706 if (uuid_buf.uuid[0] == '\0')
5707 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5708 "%s%04x", tp->st_name, index);
5710 lsb->sb_buf[lsb->sb_count].sd_index = index;
5711 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5714 if (flags & MNTDF_SHOW)
5715 showdf(mntdir, &stat_buf,
5716 obd_uuid2str(&uuid_buf), flags,
5717 tp->st_name, index, rc2);
5722 if (tp->st_op == LL_STATFS_LMV) {
5723 sum.os_ffree += stat_buf.os_ffree;
5724 sum.os_files += stat_buf.os_files;
5725 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5726 ost_files += stat_buf.os_files;
5727 ost_ffree += stat_buf.os_ffree;
5729 sum.os_blocks += stat_buf.os_blocks *
5731 sum.os_bfree += stat_buf.os_bfree *
5733 sum.os_bavail += stat_buf.os_bavail *
5740 /* If we have _some_ OSTs, but don't have as many free objects on the
5741 * OST as inodes on the MDTs, reduce the reported number of inodes
5742 * to compensate, so that the "inodes in use" number is correct.
5743 * This should be kept in sync with ll_statfs_internal().
5745 if (ost_files && ost_ffree < sum.os_ffree) {
5746 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5747 sum.os_ffree = ost_ffree;
5749 if (flags & MNTDF_SHOW) {
5751 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5759 static int lfs_setdirstripe(int argc, char **argv)
5762 struct lfs_setstripe_args lsa = { 0 };
5763 struct llapi_stripe_param *param = NULL;
5764 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5767 char *mode_opt = NULL;
5768 bool default_stripe = false;
5769 bool delete = false;
5770 bool foreign_mode = false;
5771 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5772 mode_t previous_mode = 0;
5774 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5775 struct option long_opts[] = {
5776 { .val = 'c', .name = "count", .has_arg = required_argument },
5777 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5778 { .val = 'd', .name = "delete", .has_arg = no_argument },
5779 { .val = 'D', .name = "default", .has_arg = no_argument },
5780 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5781 { .val = LFS_LAYOUT_FLAGS_OPT,
5782 .name = "flags", .has_arg = required_argument },
5783 { .val = LFS_LAYOUT_FOREIGN_OPT,
5784 .name = "foreign", .has_arg = optional_argument},
5785 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5786 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5787 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5788 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5790 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5791 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5792 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5794 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5795 { .val = 'i', .name = "index", .has_arg = required_argument },
5797 { .val = 'o', .name = "mode", .has_arg = required_argument },
5798 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5799 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5801 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5802 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5803 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5807 setstripe_args_init(&lsa);
5809 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5810 long_opts, NULL)) >= 0) {
5817 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5820 "%s %s: invalid stripe count '%s'\n",
5821 progname, argv[0], optarg);
5827 default_stripe = true;
5830 default_stripe = true;
5832 case LFS_LAYOUT_FOREIGN_OPT:
5833 if (optarg != NULL) {
5834 /* check pure numeric */
5835 type = strtoul(optarg, &end, 0);
5838 type = check_foreign_type_name(optarg);
5839 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5841 "%s %s: unknown foreign type '%s'\n",
5848 foreign_mode = true;
5850 case LFS_LAYOUT_FLAGS_OPT:
5851 flags = strtoul(optarg, &end, 16);
5854 "%s %s: bad flags '%s'\n",
5855 progname, argv[0], optarg);
5859 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5861 fprintf(stderr, "warning: '--hash-type' and '-t' "
5862 "deprecated, use '--mdt-hash' or '-H' instead\n");
5865 lsa.lsa_pattern = check_hashtype(optarg);
5866 if (lsa.lsa_pattern == 0) {
5868 "%s %s: bad stripe hash type '%s'\n",
5869 progname, argv[0], optarg);
5874 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5877 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5878 if (strcmp(argv[optind - 1], "--index") == 0)
5880 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5883 lsa.lsa_nr_tgts = parse_targets(mdts,
5884 sizeof(mdts) / sizeof(__u32),
5885 lsa.lsa_nr_tgts, optarg, NULL);
5886 if (lsa.lsa_nr_tgts < 0) {
5888 "%s %s: invalid MDT target(s) '%s'\n",
5889 progname, argv[0], optarg);
5893 lsa.lsa_tgts = mdts;
5894 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5895 lsa.lsa_stripe_off = mdts[0];
5897 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5899 fprintf(stderr, "warning: '-m' is deprecated, "
5900 "use '--mode' or '-o' instead\n");
5909 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5910 progname, argv[0], argv[optind - 1]);
5915 if (optind == argc) {
5916 fprintf(stderr, "%s %s: DIR must be specified\n",
5921 if (xattr && !foreign_mode) {
5922 /* only print a warning as this is armless and will be
5926 "%s %s: xattr has been specified for non-foreign layout\n",
5928 } else if (foreign_mode && !xattr) {
5930 "%s %s: xattr must be provided in foreign mode\n",
5935 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5936 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5938 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5943 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5944 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5946 "%s %s: stripe offset and count must be specified\n",
5952 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5953 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5955 "%s %s: cannot specify -d with -c or -i options\n",
5960 if (mode_opt != NULL) {
5961 mode = strtoul(mode_opt, &end, 8);
5964 "%s %s: bad MODE '%s'\n",
5965 progname, argv[0], mode_opt);
5968 previous_mode = umask(0);
5971 /* foreign LMV/dir case */
5973 if (argc > optind + 1) {
5975 "%s %s: cannot specify multiple foreign dirs\n",
5980 dname = argv[optind];
5981 result = llapi_dir_create_foreign(dname, mode, type, flags,
5985 "%s mkdir: can't create foreign dir '%s': %s\n",
5986 progname, dname, strerror(-result));
5991 * initialize stripe parameters, in case param is converted to specific,
5992 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5994 param = calloc(1, offsetof(typeof(*param),
5995 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5996 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5997 if (param == NULL) {
5999 "%s %s: cannot allocate memory for parameters: %s\n",
6000 progname, argv[0], strerror(ENOMEM));
6004 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6005 param->lsp_stripe_count = lsa.lsa_stripe_count;
6006 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6007 param->lsp_stripe_offset = -1;
6009 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6010 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6011 param->lsp_stripe_pattern = lsa.lsa_pattern;
6013 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
6014 param->lsp_pool = lsa.lsa_pool_name;
6015 param->lsp_is_specific = false;
6016 if (lsa.lsa_nr_tgts > 1) {
6017 if (lsa.lsa_stripe_count > 0 &&
6018 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6019 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6021 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6022 argv[0], lsa.lsa_stripe_count,
6028 param->lsp_is_specific = true;
6029 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6030 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6033 dname = argv[optind];
6035 if (default_stripe) {
6036 result = llapi_dir_set_default_lmv(dname, param);
6039 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6040 progname, dname, strerror(-result));
6044 result = llapi_dir_create(dname, mode, param);
6047 "%s setdirstripe: cannot create dir '%s': %s\n",
6048 progname, dname, strerror(-result));
6049 } while (!result && (dname = argv[++optind]));
6051 if (mode_opt != NULL)
6052 umask(previous_mode);
6059 static int lfs_rmentry(int argc, char **argv)
6066 fprintf(stderr, "error: %s: missing dirname\n",
6072 dname = argv[index];
6073 while (dname != NULL) {
6074 result = llapi_direntry_remove(dname);
6076 fprintf(stderr, "error: %s: remove dir entry '%s' "
6077 "failed\n", argv[0], dname);
6080 dname = argv[++index];
6085 static int lfs_mv(int argc, char **argv)
6087 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6088 struct find_param param = {
6095 struct option long_opts[] = {
6096 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6097 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6098 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6101 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6103 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6105 fprintf(stderr, "warning: '-M' deprecated"
6106 ", use '--mdt-index' or '-m' instead\n");
6109 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6111 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6117 param.fp_verbose = VERBOSE_DETAIL;
6120 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6121 progname, argv[optind - 1]);
6126 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6127 fprintf(stderr, "%s mv: MDT index must be specified\n",
6132 if (optind >= argc) {
6133 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6138 /* initialize migrate mdt parameters */
6139 param.fp_lmv_md = &lmu;
6140 param.fp_migrate = 1;
6141 rc = llapi_migrate_mdt(argv[optind], ¶m);
6143 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6144 progname, argv[optind], param.fp_mdt_index,
6149 static int lfs_osts(int argc, char **argv)
6151 return lfs_tgts(argc, argv);
6154 static int lfs_mdts(int argc, char **argv)
6156 return lfs_tgts(argc, argv);
6159 static int lfs_df(int argc, char **argv)
6161 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6162 enum mntdf_flags flags = MNTDF_SHOW;
6163 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6164 int c, rc = 0, index = 0;
6165 char fsname[PATH_MAX] = "", *pool_name = NULL;
6166 struct option long_opts[] = {
6167 { .val = 'h', .name = "human-readable",
6168 .has_arg = no_argument },
6169 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6170 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6171 { .val = 'p', .name = "pool", .has_arg = required_argument },
6172 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6175 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6178 flags |= MNTDF_COOKED;
6181 flags |= MNTDF_INODES;
6184 flags |= MNTDF_LAZY;
6190 flags |= MNTDF_VERBOSE;
6196 if (optind < argc && !realpath(argv[optind], path)) {
6198 fprintf(stderr, "error: invalid path '%s': %s\n",
6199 argv[optind], strerror(-rc));
6203 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6204 /* Check if we have a mount point */
6205 if (mntdir[0] == '\0')
6208 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6209 if (rc || path[0] != '\0')
6211 fsname[0] = '\0'; /* avoid matching in next loop */
6212 mntdir[0] = '\0'; /* avoid matching in next loop */
6218 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6219 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6223 if (opt_fsname == opt_instance) { /* both true or both false */
6224 rc = llapi_getname(mntdir, buf, buflen);
6225 } else if (opt_fsname) {
6226 /* llapi_search_mounts() fills @buf with fsname, but that is not
6227 * called if explicit paths are specified on the command-line
6230 rc = llapi_get_fsname(mntdir, buf, buflen);
6231 } else /* if (opt_instance) */ {
6232 rc = llapi_get_instance(mntdir, buf, buflen);
6236 fprintf(stderr, "cannot get instance for '%s': %s\n",
6237 mntdir, strerror(-rc));
6242 printf("%s %s\n", buf, mntdir);
6244 printf("%s\n", buf);
6249 static int lfs_getname(int argc, char **argv)
6251 struct option long_opts[] = {
6252 { .val = 'h', .name = "help", .has_arg = no_argument },
6253 { .val = 'i', .name = "instance", .has_arg = no_argument },
6254 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6256 bool opt_instance = false, opt_fsname = false;
6257 char fsname[PATH_MAX] = "";
6260 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6263 opt_instance = true;
6274 if (optind == argc) { /* no paths specified, get all paths. */
6275 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6278 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6279 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6280 opt_instance, opt_fsname, true);
6283 path[0] = fsname[0] = mntdir[0] = '\0';
6285 } else { /* paths specified, only attempt to search these. */
6288 /* if only one path is given, print only requested info */
6289 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6291 for (; optind < argc; optind++) {
6292 rc2 = print_instance(argv[optind], fsname,
6293 sizeof(fsname), opt_instance,
6294 opt_fsname, opt_mntdir);
6304 static int lfs_check(int argc, char **argv)
6306 char mntdir[PATH_MAX] = {'\0'};
6315 fprintf(stderr, "%s check: server type must be specified\n",
6320 obd_types[0] = obd_type1;
6321 obd_types[1] = obd_type2;
6322 obd_types[2] = obd_type3;
6324 if (strcmp(argv[1], "osts") == 0) {
6325 strcpy(obd_types[0], "osc");
6326 } else if (strcmp(argv[1], "mdts") == 0 ||
6327 strcmp(argv[1], "mds") == 0) {
6328 strcpy(obd_types[0], "mdc");
6329 } else if (strcmp(argv[1], "mgts") == 0) {
6330 strcpy(obd_types[0], "mgc");
6331 } else if (strcmp(argv[1], "all") == 0 ||
6332 strcmp(argv[1], "servers") == 0) {
6334 strcpy(obd_types[0], "osc");
6335 strcpy(obd_types[1], "mdc");
6336 strcpy(obd_types[2], "mgc");
6338 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6343 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6344 if (rc < 0 || mntdir[0] == '\0') {
6346 "%s check: cannot find mounted Lustre filesystem: %s\n",
6347 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6351 rc = llapi_target_check(num_types, obd_types, mntdir);
6353 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6354 progname, argv[1], strerror(-rc));
6360 #ifdef HAVE_SYS_QUOTA_H
6361 #define ARG2INT(nr, str, msg) \
6364 nr = strtol(str, &endp, 0); \
6365 if (*endp != '\0') { \
6366 fprintf(stderr, "%s: bad %s '%s'\n", \
6367 progname, msg, str); \
6372 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6374 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6375 * returns the value or ULONG_MAX on integer overflow or incorrect format
6377 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6378 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6379 * 3. empty integer value is interpreted as 0
6381 static unsigned long str2sec(const char* timestr)
6383 const char spec[] = "smhdw";
6384 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6385 unsigned long val = 0;
6388 if (strpbrk(timestr, spec) == NULL) {
6389 /* no specifiers inside the time string,
6390 should treat it as an integer value */
6391 val = strtoul(timestr, &tail, 10);
6392 return *tail ? ULONG_MAX : val;
6395 /* format string is XXwXXdXXhXXmXXs */
6401 v = strtoul(timestr, &tail, 10);
6402 if (v == ULONG_MAX || *tail == '\0')
6403 /* value too large (ULONG_MAX or more)
6404 or missing specifier */
6407 ptr = strchr(spec, *tail);
6409 /* unknown specifier */
6414 /* check if product will overflow the type */
6415 if (!(v < ULONG_MAX / mult[ind]))
6418 ADD_OVERFLOW(val, mult[ind] * v);
6419 if (val == ULONG_MAX)
6431 #define ARG2ULL(nr, str, def_units) \
6433 unsigned long long limit, units = def_units; \
6436 rc = llapi_parse_size(str, &limit, &units, 1); \
6438 fprintf(stderr, "%s: invalid limit '%s'\n", \
6445 static inline int has_times_option(int argc, char **argv)
6449 for (i = 1; i < argc; i++)
6450 if (!strcmp(argv[i], "-t"))
6456 int lfs_setquota_times(int argc, char **argv)
6459 struct if_quotactl qctl;
6460 char *mnt, *obd_type = (char *)qctl.obd_type;
6461 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6462 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6463 struct option long_opts[] = {
6464 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6465 { .val = 'g', .name = "group", .has_arg = no_argument },
6466 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6467 { .val = 'p', .name = "projid", .has_arg = no_argument },
6468 { .val = 't', .name = "times", .has_arg = no_argument },
6469 { .val = 'u', .name = "user", .has_arg = no_argument },
6473 memset(&qctl, 0, sizeof(qctl));
6474 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6475 qctl.qc_type = ALLQUOTA;
6477 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6478 long_opts, NULL)) != -1) {
6489 if (qctl.qc_type != ALLQUOTA) {
6490 fprintf(stderr, "error: -u/g/p can't be used "
6491 "more than once\n");
6494 qctl.qc_type = qtype;
6497 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6498 fprintf(stderr, "error: bad block-grace: %s\n",
6502 dqb->dqb_valid |= QIF_BTIME;
6505 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6506 fprintf(stderr, "error: bad inode-grace: %s\n",
6510 dqb->dqb_valid |= QIF_ITIME;
6512 case 't': /* Yes, of course! */
6514 default: /* getopt prints error message for us when opterr != 0 */
6519 if (qctl.qc_type == ALLQUOTA) {
6520 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6524 if (optind != argc - 1) {
6525 fprintf(stderr, "error: unexpected parameters encountered\n");
6529 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6530 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6531 fprintf(stderr, "error: grace time is too large\n");
6536 rc = llapi_quotactl(mnt, &qctl);
6539 fprintf(stderr, "%s %s ", obd_type,
6540 obd_uuid2str(&qctl.obd_uuid));
6541 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6548 #define BSLIMIT (1 << 0)
6549 #define BHLIMIT (1 << 1)
6550 #define ISLIMIT (1 << 2)
6551 #define IHLIMIT (1 << 3)
6553 int lfs_setquota(int argc, char **argv)
6556 struct if_quotactl qctl;
6557 char *mnt, *obd_type = (char *)qctl.obd_type;
6558 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6559 struct option long_opts[] = {
6560 { .val = 'b', .name = "block-softlimit",
6561 .has_arg = required_argument },
6562 { .val = 'B', .name = "block-hardlimit",
6563 .has_arg = required_argument },
6564 { .val = 'd', .name = "default", .has_arg = no_argument },
6565 { .val = 'g', .name = "group", .has_arg = required_argument },
6566 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6567 { .val = 'i', .name = "inode-softlimit",
6568 .has_arg = required_argument },
6569 { .val = 'I', .name = "inode-hardlimit",
6570 .has_arg = required_argument },
6571 { .val = 'p', .name = "projid", .has_arg = required_argument },
6572 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6573 { .val = 'u', .name = "user", .has_arg = required_argument },
6574 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6576 unsigned limit_mask = 0;
6578 bool use_default = false;
6581 if (has_times_option(argc, argv))
6582 return lfs_setquota_times(argc, argv);
6584 memset(&qctl, 0, sizeof(qctl));
6585 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6586 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6587 * so it can be used as a marker that qc_type
6588 * isn't reinitialized from command line */
6590 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6591 long_opts, NULL)) != -1) {
6594 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6597 goto quota_type_def;
6600 rc = name2uid(&qctl.qc_id, optarg);
6603 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6606 goto quota_type_def;
6609 rc = name2gid(&qctl.qc_id, optarg);
6612 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6615 goto quota_type_def;
6618 rc = name2projid(&qctl.qc_id, optarg);
6621 qctl.qc_id = strtoul(optarg, &endptr, 10);
6622 if (*endptr != '\0') {
6623 fprintf(stderr, "%s setquota: invalid"
6624 " id '%s'\n", progname, optarg);
6629 if (qctl.qc_id == 0) {
6630 fprintf(stderr, "%s setquota: can't set quota"
6631 " for root usr/group/project.\n",
6637 if (qctl.qc_type != ALLQUOTA) {
6639 "%s setquota: only one of -u, -U, -g,"
6640 " -G, -p or -P may be specified\n",
6644 qctl.qc_type = qtype;
6647 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6651 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6652 dqb->dqb_bsoftlimit >>= 10;
6653 limit_mask |= BSLIMIT;
6654 if (dqb->dqb_bsoftlimit &&
6655 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6657 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6658 "See '%s help setquota' or Lustre manual for details\n",
6660 (unsigned long long)dqb->dqb_bsoftlimit,
6664 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6665 dqb->dqb_bhardlimit >>= 10;
6666 limit_mask |= BHLIMIT;
6667 if (dqb->dqb_bhardlimit &&
6668 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6670 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6671 "See '%s help setquota' or Lustre manual for details\n",
6673 (unsigned long long)dqb->dqb_bhardlimit,
6677 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6678 limit_mask |= ISLIMIT;
6679 if (dqb->dqb_isoftlimit &&
6680 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6682 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6683 "See '%s help setquota' or Lustre manual for details\n",
6685 (unsigned long long)dqb->dqb_isoftlimit,
6689 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6690 limit_mask |= IHLIMIT;
6691 if (dqb->dqb_ihardlimit &&
6692 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6694 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6695 "See '%s help setquota' or Lustre manual for details\n",
6697 (unsigned long long)dqb->dqb_ihardlimit,
6702 "%s setquota: unrecognized option '%s'\n",
6703 progname, argv[optind - 1]);
6708 if (qctl.qc_type == ALLQUOTA) {
6710 "%s setquota: either -u or -g must be specified\n",
6715 if (!use_default && limit_mask == 0) {
6717 "%s setquota: at least one limit must be specified\n",
6722 if (use_default && limit_mask != 0) {
6724 "%s setquota: limits should not be specified when"
6725 " using default quota\n",
6730 if (use_default && qctl.qc_id == 0) {
6732 "%s setquota: can not set default quota for root"
6733 " user/group/project\n",
6738 if (optind != argc - 1) {
6740 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6741 progname, argv[optind]);
6748 dqb->dqb_bhardlimit = 0;
6749 dqb->dqb_bsoftlimit = 0;
6750 dqb->dqb_ihardlimit = 0;
6751 dqb->dqb_isoftlimit = 0;
6754 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6755 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6756 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6757 /* sigh, we can't just set blimits/ilimits */
6758 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6759 .qc_type = qctl.qc_type,
6760 .qc_id = qctl.qc_id};
6762 rc = llapi_quotactl(mnt, &tmp_qctl);
6766 if (!(limit_mask & BHLIMIT))
6767 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6768 if (!(limit_mask & BSLIMIT))
6769 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6770 if (!(limit_mask & IHLIMIT))
6771 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6772 if (!(limit_mask & ISLIMIT))
6773 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6775 /* Keep grace times if we have got no softlimit arguments */
6776 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6777 dqb->dqb_valid |= QIF_BTIME;
6778 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6781 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6782 dqb->dqb_valid |= QIF_ITIME;
6783 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6787 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6788 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6790 rc = llapi_quotactl(mnt, &qctl);
6794 "%s setquota: cannot quotactl '%s' '%s': %s",
6796 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6803 /* Converts seconds value into format string
6804 * result is returned in buf
6806 * 1. result is in descenting order: 1w2d3h4m5s
6807 * 2. zero fields are not filled (except for p. 3): 5d1s
6808 * 3. zero seconds value is presented as "0s"
6810 static char * __sec2str(time_t seconds, char *buf)
6812 const char spec[] = "smhdw";
6813 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6818 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
6819 c = seconds / mult[i];
6821 if (c > 0 || (i == 0 && buf == tail))
6822 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
6831 static void sec2str(time_t seconds, char *buf, int rc)
6838 tail = __sec2str(seconds, tail);
6840 if (rc && tail - buf < 39) {
6846 static void diff2str(time_t seconds, char *buf, time_t now)
6852 if (seconds <= now) {
6853 strcpy(buf, "none");
6856 __sec2str(seconds - now, buf);
6859 static void print_quota_title(char *name, struct if_quotactl *qctl,
6860 bool human_readable, bool show_default)
6863 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6864 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6865 "Filesystem", "bquota", "blimit", "bgrace",
6866 "iquota", "ilimit", "igrace");
6868 printf("Disk quotas for %s %s (%cid %u):\n",
6869 qtype_name(qctl->qc_type), name,
6870 *qtype_name(qctl->qc_type), qctl->qc_id);
6871 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6872 "Filesystem", human_readable ? "used" : "kbytes",
6873 "quota", "limit", "grace",
6874 "files", "quota", "limit", "grace");
6878 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6881 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6884 snprintf(buf, buflen, "%5.4gP",
6885 (double)num / ((__u64)1 << 40));
6887 snprintf(buf, buflen, "%5.4gT",
6888 (double)num / (1 << 30));
6890 snprintf(buf, buflen, "%5.4gG",
6891 (double)num / (1 << 20));
6893 snprintf(buf, buflen, "%5.4gM",
6894 (double)num / (1 << 10));
6896 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6900 #define STRBUF_LEN 32
6901 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6902 int rc, bool h, bool show_default)
6908 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6909 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6910 int bover = 0, iover = 0;
6911 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6912 char numbuf[3][STRBUF_LEN];
6914 char strbuf[STRBUF_LEN];
6916 if (dqb->dqb_bhardlimit &&
6917 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6919 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6920 if (dqb->dqb_btime > now) {
6927 if (dqb->dqb_ihardlimit &&
6928 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6930 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6931 if (dqb->dqb_itime > now) {
6939 if (strlen(mnt) > 15)
6940 printf("%s\n%15s", mnt, "");
6942 printf("%15s", mnt);
6945 diff2str(dqb->dqb_btime, timebuf, now);
6946 else if (show_default)
6947 snprintf(timebuf, sizeof(timebuf), "%llu",
6948 (unsigned long long)dqb->dqb_btime);
6950 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6951 strbuf, sizeof(strbuf), h);
6952 if (rc == -EREMOTEIO)
6953 sprintf(numbuf[0], "%s*", strbuf);
6955 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6956 "%s" : "[%s]", strbuf);
6958 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6959 if (type == QC_GENERAL)
6960 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6961 "%s" : "[%s]", strbuf);
6963 sprintf(numbuf[1], "%s", "-");
6965 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6966 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6967 "%s" : "[%s]", strbuf);
6970 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6972 printf(" %7s%c %6s %7s %7s",
6973 numbuf[0], bover ? '*' : ' ', numbuf[1],
6974 numbuf[2], bover > 1 ? timebuf : "-");
6978 diff2str(dqb->dqb_itime, timebuf, now);
6979 else if (show_default)
6980 snprintf(timebuf, sizeof(timebuf), "%llu",
6981 (unsigned long long)dqb->dqb_itime);
6983 snprintf(numbuf[0], sizeof(numbuf),
6984 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6985 (uintmax_t)dqb->dqb_curinodes);
6987 if (type == QC_GENERAL)
6988 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6990 (uintmax_t)dqb->dqb_isoftlimit);
6992 sprintf(numbuf[1], "%s", "-");
6994 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6995 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6998 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6999 else if (type != QC_OSTIDX)
7000 printf(" %7s%c %6s %7s %7s",
7001 numbuf[0], iover ? '*' : ' ', numbuf[1],
7002 numbuf[2], iover > 1 ? timebuf : "-");
7004 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7007 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
7008 qctl->qc_cmd == Q_GETOINFO) {
7012 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7013 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7014 printf("Block grace time: %s; Inode grace time: %s\n",
7015 bgtimebuf, igtimebuf);
7019 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7020 bool h, __u64 *total)
7022 int rc = 0, rc1 = 0, count = 0;
7023 __u32 valid = qctl->qc_valid;
7025 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7027 fprintf(stderr, "can not get %s count: %s\n",
7028 is_mdt ? "mdt": "ost", strerror(-rc));
7032 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
7033 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7034 rc = llapi_quotactl(mnt, qctl);
7036 /* It is remote client case. */
7037 if (rc == -EOPNOTSUPP) {
7044 fprintf(stderr, "quotactl %s%d failed.\n",
7045 is_mdt ? "mdt": "ost", qctl->qc_idx);
7049 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7050 qctl->qc_valid, 0, h, false);
7051 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7052 qctl->qc_dqblk.dqb_bhardlimit;
7055 qctl->qc_valid = valid;
7059 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7060 int verbose, int quiet, bool human_readable,
7063 int rc1 = 0, rc2 = 0, rc3 = 0;
7064 char *obd_type = (char *)qctl->obd_type;
7065 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7066 __u64 total_ialloc = 0, total_balloc = 0;
7067 bool use_default_for_blk = false;
7068 bool use_default_for_file = false;
7071 rc1 = llapi_quotactl(mnt, qctl);
7075 fprintf(stderr, "%s quotas are not enabled.\n",
7076 qtype_name(qctl->qc_type));
7079 fprintf(stderr, "Permission denied.\n");
7082 /* We already got error message. */
7085 fprintf(stderr, "Unexpected quotactl error: %s\n",
7090 if (!show_default && qctl->qc_id == 0) {
7091 qctl->qc_dqblk.dqb_bhardlimit = 0;
7092 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7093 qctl->qc_dqblk.dqb_ihardlimit = 0;
7094 qctl->qc_dqblk.dqb_isoftlimit = 0;
7095 qctl->qc_dqblk.dqb_btime = 0;
7096 qctl->qc_dqblk.dqb_itime = 0;
7097 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7100 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7101 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7102 use_default_for_blk = true;
7103 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7106 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7107 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7108 use_default_for_file = true;
7109 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7112 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7113 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7114 print_quota_title(name, qctl, human_readable, show_default);
7116 if (rc1 && *obd_type)
7117 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7119 if (qctl->qc_valid != QC_GENERAL)
7122 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
7123 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7124 (QIF_LIMITS|QIF_USAGE));
7126 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7128 if (!show_default && verbose &&
7129 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
7130 char strbuf[STRBUF_LEN];
7132 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7134 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7136 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7138 printf("Total allocated inode limit: %ju, total "
7139 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
7143 if (use_default_for_blk)
7144 printf("%cid %u is using default block quota setting\n",
7145 *qtype_name(qctl->qc_type), qctl->qc_id);
7147 if (use_default_for_file)
7148 printf("%cid %u is using default file quota setting\n",
7149 *qtype_name(qctl->qc_type), qctl->qc_id);
7151 if (rc1 || rc2 || rc3 || inacc)
7152 printf("Some errors happened when getting quota info. "
7153 "Some devices may be not working or deactivated. "
7154 "The data in \"[]\" is inaccurate.\n");
7168 static int lfs_project(int argc, char **argv)
7170 int ret = 0, err = 0, c, i;
7171 struct project_handle_control phc = { 0 };
7172 enum lfs_project_ops_t op;
7175 phc.assign_projid = false;
7176 /* default action */
7177 op = LFS_PROJECT_LIST;
7179 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7182 if (op != LFS_PROJECT_LIST) {
7184 "%s: cannot specify '-c' '-C' '-s' together\n",
7189 op = LFS_PROJECT_CHECK;
7192 if (op != LFS_PROJECT_LIST) {
7194 "%s: cannot specify '-c' '-C' '-s' together\n",
7199 op = LFS_PROJECT_CLEAR;
7202 if (op != LFS_PROJECT_LIST) {
7204 "%s: cannot specify '-c' '-C' '-s' together\n",
7209 phc.set_inherit = true;
7210 op = LFS_PROJECT_SET;
7216 phc.keep_projid = true;
7219 phc.recursive = true;
7222 phc.projid = strtoul(optarg, NULL, 0);
7223 phc.assign_projid = true;
7227 phc.newline = false;
7230 fprintf(stderr, "%s: invalid option '%c'\n",
7236 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7237 op = LFS_PROJECT_SET;
7238 phc.set_projid = true;
7239 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7240 phc.set_projid = true;
7244 case LFS_PROJECT_CHECK:
7245 if (phc.keep_projid) {
7247 "%s: '-k' is useless together with '-c'\n",
7252 case LFS_PROJECT_CLEAR:
7255 "%s: '-0' is useless together with '-C'\n",
7259 if (phc.assign_projid) {
7261 "%s: '-p' is useless together with '-C'\n",
7266 case LFS_PROJECT_SET:
7269 "%s: '-0' is useless together with '-s'\n",
7273 if (phc.keep_projid) {
7275 "%s: '-k' is useless together with '-s'\n",
7283 "%s: '-0' is useless for list operations\n",
7293 fprintf(stderr, "%s: missing file or directory target(s)\n",
7298 for (i = 0; i < argc; i++) {
7300 case LFS_PROJECT_CHECK:
7301 err = lfs_project_check(argv[i], &phc);
7303 case LFS_PROJECT_LIST:
7304 err = lfs_project_list(argv[i], &phc);
7306 case LFS_PROJECT_CLEAR:
7307 err = lfs_project_clear(argv[i], &phc);
7309 case LFS_PROJECT_SET:
7310 err = lfs_project_set(argv[i], &phc);
7322 static int lfs_quota(int argc, char **argv)
7325 char *mnt, *name = NULL;
7326 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
7327 .qc_type = ALLQUOTA };
7328 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
7329 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7331 __u32 valid = QC_GENERAL, idx = 0;
7332 bool human_readable = false;
7333 bool show_default = false;
7336 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
7339 show_default = true;
7344 show_default = true;
7349 show_default = true;
7353 if (qctl.qc_type != ALLQUOTA) {
7355 "%s quota: only one of -u, -g, or -p may be specified\n",
7359 qctl.qc_type = qtype;
7362 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7365 valid = qctl.qc_valid = QC_UUID;
7366 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7369 valid = qctl.qc_valid = QC_MDTIDX;
7370 idx = qctl.qc_idx = atoi(optarg);
7371 if (idx == 0 && *optarg != '0') {
7373 "%s quota: invalid MDT index '%s'\n",
7379 valid = qctl.qc_valid = QC_OSTIDX;
7380 idx = qctl.qc_idx = atoi(optarg);
7381 if (idx == 0 && *optarg != '0') {
7383 "%s quota: invalid OST index '%s'\n",
7395 human_readable = true;
7398 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7399 progname, argv[optind - 1]);
7404 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7405 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7406 optind == argc - 1 && !show_default) {
7408 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7411 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7412 qctl.qc_type = qtype;
7413 qctl.qc_valid = valid;
7414 if (qtype == USRQUOTA) {
7415 qctl.qc_id = geteuid();
7416 rc = uid2name(&name, qctl.qc_id);
7418 qctl.qc_id = getegid();
7419 rc = gid2name(&name, qctl.qc_id);
7420 memset(&qctl.qc_dqblk, 0,
7421 sizeof(qctl.qc_dqblk));
7426 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7427 human_readable, show_default);
7432 /* lfs quota -u username /path/to/lustre/mount */
7433 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7434 /* options should be followed by u/g-name and mntpoint */
7435 if ((!show_default && optind + 2 != argc) ||
7436 (show_default && optind + 1 != argc) ||
7437 qctl.qc_type == ALLQUOTA) {
7439 "%s quota: name and mount point must be specified\n",
7444 if (!show_default) {
7445 name = argv[optind++];
7446 switch (qctl.qc_type) {
7448 rc = name2uid(&qctl.qc_id, name);
7451 rc = name2gid(&qctl.qc_id, name);
7454 rc = name2projid(&qctl.qc_id, name);
7461 qctl.qc_valid = QC_GENERAL;
7462 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7467 qctl.qc_id = strtoul(name, &endptr, 10);
7468 if (*endptr != '\0') {
7469 fprintf(stderr, "%s quota: invalid id '%s'\n",
7474 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7475 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7481 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7482 human_readable, show_default);
7485 #endif /* HAVE_SYS_QUOTA_H! */
7487 static int flushctx_ioctl(char *mp)
7491 fd = open(mp, O_RDONLY);
7493 fprintf(stderr, "flushctx: error open %s: %s\n",
7494 mp, strerror(errno));
7498 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7500 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7501 mp, strerror(errno));
7507 static int lfs_flushctx(int argc, char **argv)
7509 int kdestroy = 0, c;
7510 char mntdir[PATH_MAX] = {'\0'};
7514 while ((c = getopt(argc, argv, "k")) != -1) {
7520 fprintf(stderr, "error: %s: option '-%c' "
7521 "unrecognized\n", argv[0], c);
7527 if ((rc = system("kdestroy > /dev/null")) != 0) {
7528 rc = WEXITSTATUS(rc);
7529 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7533 if (optind >= argc) {
7534 /* flush for all mounted lustre fs. */
7535 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7536 /* Check if we have a mount point */
7537 if (mntdir[0] == '\0')
7540 if (flushctx_ioctl(mntdir))
7543 mntdir[0] = '\0'; /* avoid matching in next loop */
7546 /* flush fs as specified */
7547 while (optind < argc) {
7548 if (flushctx_ioctl(argv[optind++]))
7555 static int lfs_changelog(int argc, char **argv)
7557 void *changelog_priv;
7558 struct changelog_rec *rec;
7559 long long startrec = 0, endrec = 0;
7561 struct option long_opts[] = {
7562 { .val = 'f', .name = "follow", .has_arg = no_argument },
7564 char short_opts[] = "f";
7567 while ((rc = getopt_long(argc, argv, short_opts,
7568 long_opts, NULL)) != -1) {
7575 "%s changelog: unrecognized option '%s'\n",
7576 progname, argv[optind - 1]);
7580 if (optind >= argc) {
7581 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7586 mdd = argv[optind++];
7588 startrec = strtoll(argv[optind++], NULL, 10);
7590 endrec = strtoll(argv[optind++], NULL, 10);
7592 rc = llapi_changelog_start(&changelog_priv,
7593 CHANGELOG_FLAG_BLOCK |
7594 CHANGELOG_FLAG_JOBID |
7595 CHANGELOG_FLAG_EXTRA_FLAGS |
7596 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7599 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7600 progname, strerror(errno = -rc));
7604 rc = llapi_changelog_set_xflags(changelog_priv,
7605 CHANGELOG_EXTRA_FLAG_UIDGID |
7606 CHANGELOG_EXTRA_FLAG_NID |
7607 CHANGELOG_EXTRA_FLAG_OMODE |
7608 CHANGELOG_EXTRA_FLAG_XATTR);
7611 "%s changelog: cannot set xflags for changelog: %s\n",
7612 progname, strerror(errno = -rc));
7616 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7620 if (endrec && rec->cr_index > endrec) {
7621 llapi_changelog_free(&rec);
7624 if (rec->cr_index < startrec) {
7625 llapi_changelog_free(&rec);
7629 secs = rec->cr_time >> 30;
7630 gmtime_r(&secs, &ts);
7631 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7632 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7633 changelog_type2str(rec->cr_type),
7634 ts.tm_hour, ts.tm_min, ts.tm_sec,
7635 (int)(rec->cr_time & ((1 << 30) - 1)),
7636 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7637 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7639 if (rec->cr_flags & CLF_JOBID) {
7640 struct changelog_ext_jobid *jid =
7641 changelog_rec_jobid(rec);
7643 if (jid->cr_jobid[0] != '\0')
7644 printf(" j=%s", jid->cr_jobid);
7647 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7648 struct changelog_ext_extra_flags *ef =
7649 changelog_rec_extra_flags(rec);
7651 printf(" ef=0x%llx",
7652 (unsigned long long)ef->cr_extra_flags);
7654 if (ef->cr_extra_flags & CLFE_UIDGID) {
7655 struct changelog_ext_uidgid *uidgid =
7656 changelog_rec_uidgid(rec);
7658 printf(" u=%llu:%llu",
7659 (unsigned long long)uidgid->cr_uid,
7660 (unsigned long long)uidgid->cr_gid);
7662 if (ef->cr_extra_flags & CLFE_NID) {
7663 struct changelog_ext_nid *nid =
7664 changelog_rec_nid(rec);
7667 libcfs_nid2str(nid->cr_nid));
7670 if (ef->cr_extra_flags & CLFE_OPEN) {
7671 struct changelog_ext_openmode *omd =
7672 changelog_rec_openmode(rec);
7673 char mode[] = "---";
7675 /* exec mode must be exclusive */
7676 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7679 if (omd->cr_openflags & MDS_FMODE_READ)
7681 if (omd->cr_openflags &
7688 if (strcmp(mode, "---") != 0)
7689 printf(" m=%s", mode);
7693 if (ef->cr_extra_flags & CLFE_XATTR) {
7694 struct changelog_ext_xattr *xattr =
7695 changelog_rec_xattr(rec);
7697 if (xattr->cr_xattr[0] != '\0')
7698 printf(" x=%s", xattr->cr_xattr);
7702 if (rec->cr_namelen)
7703 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7704 rec->cr_namelen, changelog_rec_name(rec));
7706 if (rec->cr_flags & CLF_RENAME) {
7707 struct changelog_ext_rename *rnm =
7708 changelog_rec_rename(rec);
7710 if (!fid_is_zero(&rnm->cr_sfid))
7711 printf(" s="DFID" sp="DFID" %.*s",
7712 PFID(&rnm->cr_sfid),
7713 PFID(&rnm->cr_spfid),
7714 (int)changelog_rec_snamelen(rec),
7715 changelog_rec_sname(rec));
7719 llapi_changelog_free(&rec);
7722 llapi_changelog_fini(&changelog_priv);
7725 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7726 progname, strerror(errno = -rc));
7728 return (rc == 1 ? 0 : rc);
7731 static int lfs_changelog_clear(int argc, char **argv)
7739 endrec = strtoll(argv[3], NULL, 10);
7741 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7744 fprintf(stderr, "%s: record out of range: %llu\n",
7746 else if (rc == -ENOENT)
7747 fprintf(stderr, "%s: no changelog user: %s\n",
7750 fprintf(stderr, "%s error: %s\n", argv[0],
7759 static int lfs_fid2path(int argc, char **argv)
7761 struct option long_opts[] = {
7762 { .val = 'c', .name = "cur", .has_arg = no_argument },
7763 { .val = 'l', .name = "link", .has_arg = required_argument },
7764 { .val = 'r', .name = "rec", .has_arg = required_argument },
7766 char short_opts[] = "cl:r:";
7767 char *device, *fid, *path;
7768 long long recno = -1;
7773 char *endptr = NULL;
7775 while ((rc = getopt_long(argc, argv, short_opts,
7776 long_opts, NULL)) != -1) {
7782 linkno = strtol(optarg, &endptr, 10);
7783 if (*endptr != '\0') {
7785 "%s fid2path: invalid linkno '%s'\n",
7791 recno = strtoll(optarg, &endptr, 10);
7792 if (*endptr != '\0') {
7794 "%s fid2path: invalid recno '%s'\n",
7801 "%s fid2path: unrecognized option '%s'\n",
7802 progname, argv[optind - 1]);
7809 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7814 device = argv[optind++];
7815 path = calloc(1, PATH_MAX);
7819 "%s fid2path: cannot allocate memory for path: %s\n",
7820 progname, strerror(-rc));
7825 while (optind < argc) {
7826 fid = argv[optind++];
7828 lnktmp = (linkno >= 0) ? linkno : 0;
7830 int oldtmp = lnktmp;
7831 long long rectmp = recno;
7833 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7837 "%s fid2path: cannot find '%s': %s\n",
7838 progname, fid, strerror(errno = -rc2));
7845 fprintf(stdout, "%lld ", rectmp);
7846 if (device[0] == '/') {
7847 fprintf(stdout, "%s", device);
7848 if (device[strlen(device) - 1] != '/')
7849 fprintf(stdout, "/");
7850 } else if (path[0] == '\0') {
7851 fprintf(stdout, "/");
7853 fprintf(stdout, "%s\n", path);
7856 /* specified linkno */
7858 if (oldtmp == lnktmp)
7868 static int lfs_path2fid(int argc, char **argv)
7870 struct option long_opts[] = {
7871 { .val = 'p', .name = "parents", .has_arg = no_argument },
7874 const char short_opts[] = "p";
7875 const char *sep = "";
7878 bool show_parents = false;
7880 while ((rc = getopt_long(argc, argv, short_opts,
7881 long_opts, NULL)) != -1) {
7884 show_parents = true;
7888 "%s path2fid: unrecognized option '%s'\n",
7889 progname, argv[optind - 1]);
7894 if (optind > argc - 1) {
7895 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7899 else if (optind < argc - 1)
7903 for (path = argv + optind; *path != NULL; path++) {
7905 if (!show_parents) {
7906 err = llapi_path2fid(*path, &fid);
7908 printf("%s%s"DFID"\n",
7909 *sep != '\0' ? *path : "", sep,
7912 char name[NAME_MAX + 1];
7913 unsigned int linkno = 0;
7915 while ((err = llapi_path2parent(*path, linkno, &fid,
7916 name, sizeof(name))) == 0) {
7917 if (*sep != '\0' && linkno == 0)
7918 printf("%s%s", *path, sep);
7920 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7925 /* err == -ENODATA is end-of-loop */
7926 if (linkno > 0 && err == -ENODATA) {
7934 "%s path2fid: cannot get %sfid for '%s': %s\n",
7935 progname, show_parents ? "parent " : "", *path,
7947 #define MAX_ERRNO 4095
7948 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
7950 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
7954 rc = llapi_rmfid(device, fa);
7956 fprintf(stderr, "rmfid(): rc = %d\n", rc);
7960 for (k = 0; k < fa->fa_nr; k++) {
7961 rc = (__s32)fa->fa_fids[k].f_ver;
7962 if (!IS_ERR_VALUE(rc))
7968 fa->fa_fids[k].f_ver = 0;
7969 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
7970 PFID(&fa->fa_fids[k]), rc);
7976 static int lfs_rmfid(int argc, char **argv)
7978 char *fidstr, *device;
7979 int rc = 0, rc2, nr;
7980 struct fid_array *fa;
7982 if (optind > argc - 1) {
7983 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
7987 device = argv[optind++];
7990 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
7996 while (optind < argc) {
7999 fidstr = argv[optind++];
8000 while (*fidstr == '[')
8002 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8004 fprintf(stderr, "unrecognized FID: %s\n",
8009 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8010 /* start another batch */
8011 rc2 = lfs_rmfid_and_show_errors(device, fa);
8018 rc2 = lfs_rmfid_and_show_errors(device, fa);
8026 static int lfs_data_version(int argc, char **argv)
8033 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8036 fprintf(stderr, "%s data_version: FILE must be specified\n",
8041 while ((c = getopt(argc, argv, "nrw")) != -1) {
8044 data_version_flags = 0;
8047 data_version_flags |= LL_DV_RD_FLUSH;
8050 data_version_flags |= LL_DV_WR_FLUSH;
8054 "%s data_version: unrecognized option '%s'\n",
8055 progname, argv[optind - 1]);
8059 if (optind == argc) {
8060 fprintf(stderr, "%s data_version: FILE must be specified\n",
8065 path = argv[optind];
8066 fd = open(path, O_RDONLY);
8069 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8070 progname, path, strerror(-rc));
8074 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8077 "%s data_version: cannot get version for '%s': %s\n",
8078 progname, path, strerror(-rc));
8080 printf("%ju" "\n", (uintmax_t)data_version);
8086 static int lfs_hsm_state(int argc, char **argv)
8091 struct hsm_user_state hus;
8099 rc = llapi_hsm_state_get(path, &hus);
8101 fprintf(stderr, "can't get hsm state for %s: %s\n",
8102 path, strerror(errno = -rc));
8106 /* Display path name and status flags */
8107 printf("%s: (0x%08x)", path, hus.hus_states);
8109 if (hus.hus_states & HS_RELEASED)
8110 printf(" released");
8111 if (hus.hus_states & HS_EXISTS)
8113 if (hus.hus_states & HS_DIRTY)
8115 if (hus.hus_states & HS_ARCHIVED)
8116 printf(" archived");
8117 /* Display user-settable flags */
8118 if (hus.hus_states & HS_NORELEASE)
8119 printf(" never_release");
8120 if (hus.hus_states & HS_NOARCHIVE)
8121 printf(" never_archive");
8122 if (hus.hus_states & HS_LOST)
8123 printf(" lost_from_hsm");
8125 if (hus.hus_archive_id != 0)
8126 printf(", archive_id:%d", hus.hus_archive_id);
8129 } while (++i < argc);
8134 #define LFS_HSM_SET 0
8135 #define LFS_HSM_CLEAR 1
8138 * Generic function to set or clear HSM flags.
8139 * Used by hsm_set and hsm_clear.
8141 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8143 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8145 struct option long_opts[] = {
8146 { .val = 'A', .name = "archived", .has_arg = no_argument },
8147 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8148 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8149 { .val = 'e', .name = "exists", .has_arg = no_argument },
8150 { .val = 'l', .name = "lost", .has_arg = no_argument },
8151 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8152 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8154 char short_opts[] = "lraAdei:";
8158 __u32 archive_id = 0;
8164 while ((c = getopt_long(argc, argv, short_opts,
8165 long_opts, NULL)) != -1) {
8171 mask |= HS_NOARCHIVE;
8174 mask |= HS_ARCHIVED;
8177 mask |= HS_NORELEASE;
8186 archive_id = strtol(optarg, &end, 10);
8188 fprintf(stderr, "invalid archive_id: '%s'\n",
8196 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8197 argv[0], argv[optind - 1]);
8202 /* User should have specified a flag */
8206 while (optind < argc) {
8208 path = argv[optind];
8210 /* If mode == 0, this means we apply the mask. */
8211 if (mode == LFS_HSM_SET)
8212 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8214 rc = llapi_hsm_state_set(path, 0, mask, 0);
8217 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8218 path, strerror(errno = -rc));
8227 static int lfs_hsm_action(int argc, char **argv)
8232 struct hsm_current_action hca;
8233 struct hsm_extent he;
8234 enum hsm_user_action hua;
8235 enum hsm_progress_states hps;
8243 rc = llapi_hsm_current_action(path, &hca);
8245 fprintf(stderr, "can't get hsm action for %s: %s\n",
8246 path, strerror(errno = -rc));
8249 he = hca.hca_location;
8250 hua = hca.hca_action;
8251 hps = hca.hca_state;
8253 printf("%s: %s", path, hsm_user_action2name(hua));
8255 /* Skip file without action */
8256 if (hca.hca_action == HUA_NONE) {
8261 printf(" %s ", hsm_progress_state2name(hps));
8263 if ((hps == HPS_RUNNING) &&
8264 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8265 printf("(%llu bytes moved)\n",
8266 (unsigned long long)he.length);
8267 else if ((he.offset + he.length) == LUSTRE_EOF)
8268 printf("(from %llu to EOF)\n",
8269 (unsigned long long)he.offset);
8271 printf("(from %llu to %llu)\n",
8272 (unsigned long long)he.offset,
8273 (unsigned long long)(he.offset + he.length));
8275 } while (++i < argc);
8280 static int lfs_hsm_set(int argc, char **argv)
8282 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8285 static int lfs_hsm_clear(int argc, char **argv)
8287 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8291 * Check file state and return its fid, to be used by lfs_hsm_request().
8293 * \param[in] file Path to file to check
8294 * \param[in,out] fid Pointer to allocated lu_fid struct.
8295 * \param[in,out] last_dev Pointer to last device id used.
8297 * \return 0 on success.
8299 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8305 rc = lstat(file, &st);
8307 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8310 /* Checking for regular file as archiving as posix copytool
8311 * rejects archiving files other than regular files
8313 if (!S_ISREG(st.st_mode)) {
8314 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8317 /* A request should be ... */
8318 if (*last_dev != st.st_dev && *last_dev != 0) {
8319 fprintf(stderr, "All files should be "
8320 "on the same filesystem: %s\n", file);
8323 *last_dev = st.st_dev;
8325 rc = llapi_path2fid(file, fid);
8327 fprintf(stderr, "Cannot read FID of %s: %s\n",
8328 file, strerror(-rc));
8334 /* Fill an HSM HUR item with a given file name.
8336 * If mntpath is set, then the filename is actually a FID, and no
8337 * lookup on the filesystem will be performed.
8339 * \param[in] hur the user request to fill
8340 * \param[in] idx index of the item inside the HUR to fill
8341 * \param[in] mntpath mountpoint of Lustre
8342 * \param[in] fname filename (if mtnpath is NULL)
8343 * or FID (if mntpath is set)
8344 * \param[in] last_dev pointer to last device id used
8346 * \retval 0 on success
8347 * \retval CMD_HELP or a negative errno on error
8349 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8350 const char *mntpath, const char *fname,
8353 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8356 hui->hui_extent.length = -1;
8358 if (mntpath != NULL) {
8359 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8361 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8364 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8368 hur->hur_request.hr_itemcount++;
8373 static int lfs_hsm_request(int argc, char **argv, int action)
8375 struct option long_opts[] = {
8376 { .val = 'a', .name = "archive", .has_arg = required_argument },
8377 { .val = 'D', .name = "data", .has_arg = required_argument },
8378 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8379 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8382 char short_opts[] = "l:D:a:m:";
8383 struct hsm_user_request *hur, *oldhur;
8388 char *filelist = NULL;
8389 char fullpath[PATH_MAX];
8390 char *opaque = NULL;
8394 int nbfile_alloc = 0;
8395 char *some_file = NULL;
8396 char *mntpath = NULL;
8402 while ((c = getopt_long(argc, argv, short_opts,
8403 long_opts, NULL)) != -1) {
8412 if (action != HUA_ARCHIVE &&
8413 action != HUA_REMOVE) {
8415 "error: -a is supported only "
8416 "when archiving or removing\n");
8419 archive_id = atoi(optarg);
8422 if (some_file == NULL) {
8424 some_file = strdup(optarg);
8430 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8431 argv[0], argv[optind - 1]);
8436 /* All remaining args are files, so we have at least nbfile */
8437 nbfile = argc - optind;
8439 if ((nbfile == 0) && (filelist == NULL))
8443 opaque_len = strlen(opaque);
8445 /* Alloc the request structure with enough place to store all files
8446 * from command line. */
8447 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8449 fprintf(stderr, "Cannot create the request: %s\n",
8453 nbfile_alloc = nbfile;
8455 hur->hur_request.hr_action = action;
8456 hur->hur_request.hr_archive_id = archive_id;
8457 hur->hur_request.hr_flags = 0;
8459 /* All remaining args are files, add them */
8460 if (nbfile != 0 && some_file == NULL)
8461 some_file = strdup(argv[optind]);
8463 for (i = 0; i < nbfile; i++) {
8464 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8470 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8472 /* If a filelist was specified, read the filelist from it. */
8473 if (filelist != NULL) {
8474 fp = fopen(filelist, "r");
8476 fprintf(stderr, "Cannot read the file list %s: %s\n",
8477 filelist, strerror(errno));
8482 while ((rc = getline(&line, &len, fp)) != -1) {
8483 /* If allocated buffer was too small, get something
8485 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8488 nbfile_alloc = nbfile_alloc * 2 + 1;
8490 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8493 fprintf(stderr, "hsm: cannot allocate "
8494 "the request: %s\n",
8501 size = hur_len(oldhur);
8503 fprintf(stderr, "hsm: cannot allocate "
8504 "%u files + %u bytes data\n",
8505 oldhur->hur_request.hr_itemcount,
8506 oldhur->hur_request.hr_data_len);
8513 memcpy(hur, oldhur, size);
8518 if (line[strlen(line) - 1] == '\n')
8519 line[strlen(line) - 1] = '\0';
8521 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8522 mntpath, line, &last_dev);
8528 if (some_file == NULL) {
8538 /* If a --data was used, add it to the request */
8539 hur->hur_request.hr_data_len = opaque_len;
8541 memcpy(hur_data(hur), opaque, opaque_len);
8543 /* Send the HSM request */
8544 if (realpath(some_file, fullpath) == NULL) {
8545 fprintf(stderr, "Could not find path '%s': %s\n",
8546 some_file, strerror(errno));
8548 rc = llapi_hsm_request(fullpath, hur);
8550 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8551 some_file, strerror(-rc));
8561 static int lfs_hsm_archive(int argc, char **argv)
8563 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8566 static int lfs_hsm_restore(int argc, char **argv)
8568 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8571 static int lfs_hsm_release(int argc, char **argv)
8573 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8576 static int lfs_hsm_remove(int argc, char **argv)
8578 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8581 static int lfs_hsm_cancel(int argc, char **argv)
8583 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8586 static int lfs_swap_layouts(int argc, char **argv)
8591 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8592 SWAP_LAYOUTS_KEEP_MTIME |
8593 SWAP_LAYOUTS_KEEP_ATIME);
8596 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8598 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8600 int lfs_get_mode(const char *string)
8602 enum lock_mode_user mode;
8604 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8605 if (lock_mode_names[mode] == NULL)
8607 if (strcmp(string, lock_mode_names[mode]) == 0)
8614 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8616 enum lu_ladvise_type advice;
8619 advice < ARRAY_SIZE(ladvise_names); advice++) {
8620 if (ladvise_names[advice] == NULL)
8622 if (strcmp(string, ladvise_names[advice]) == 0)
8626 return LU_LADVISE_INVALID;
8629 static int lfs_ladvise(int argc, char **argv)
8631 struct option long_opts[] = {
8632 { .val = 'a', .name = "advice", .has_arg = required_argument },
8633 { .val = 'b', .name = "background", .has_arg = no_argument },
8634 { .val = 'e', .name = "end", .has_arg = required_argument },
8635 { .val = 'l', .name = "length", .has_arg = required_argument },
8636 { .val = 'm', .name = "mode", .has_arg = required_argument },
8637 { .val = 's', .name = "start", .has_arg = required_argument },
8638 { .val = 'u', .name = "unset", .has_arg = no_argument },
8640 char short_opts[] = "a:be:l:m:s:u";
8645 struct llapi_lu_ladvise advice;
8646 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8647 unsigned long long start = 0;
8648 unsigned long long end = LUSTRE_EOF;
8649 unsigned long long length = 0;
8650 unsigned long long size_units;
8651 unsigned long long flags = 0;
8655 while ((c = getopt_long(argc, argv, short_opts,
8656 long_opts, NULL)) != -1) {
8659 advice_type = lfs_get_ladvice(optarg);
8660 if (advice_type == LU_LADVISE_INVALID) {
8661 fprintf(stderr, "%s: invalid advice type "
8662 "'%s'\n", argv[0], optarg);
8663 fprintf(stderr, "Valid types:");
8665 for (advice_type = 0;
8666 advice_type < ARRAY_SIZE(ladvise_names);
8668 if (ladvise_names[advice_type] == NULL)
8670 fprintf(stderr, " %s",
8671 ladvise_names[advice_type]);
8673 fprintf(stderr, "\n");
8686 rc = llapi_parse_size(optarg, &end,
8689 fprintf(stderr, "%s: bad end offset '%s'\n",
8696 rc = llapi_parse_size(optarg, &start,
8699 fprintf(stderr, "%s: bad start offset "
8700 "'%s'\n", argv[0], optarg);
8706 rc = llapi_parse_size(optarg, &length,
8709 fprintf(stderr, "%s: bad length '%s'\n",
8715 mode = lfs_get_mode(optarg);
8717 fprintf(stderr, "%s: bad mode '%s', valid "
8718 "modes are READ or WRITE\n",
8726 fprintf(stderr, "%s: option '%s' unrecognized\n",
8727 argv[0], argv[optind - 1]);
8732 if (advice_type == LU_LADVISE_INVALID) {
8733 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8734 fprintf(stderr, "Valid types:");
8735 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8737 if (ladvise_names[advice_type] == NULL)
8739 fprintf(stderr, " %s", ladvise_names[advice_type]);
8741 fprintf(stderr, "\n");
8745 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8746 fprintf(stderr, "%s: Lock no expand advice is a per file "
8747 "descriptor advice, so when called from lfs, "
8748 "it does nothing.\n", argv[0]);
8752 if (argc <= optind) {
8753 fprintf(stderr, "%s: please give one or more file names\n",
8758 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8759 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8764 if (end == LUSTRE_EOF && length != 0)
8765 end = start + length;
8768 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8769 argv[0], start, end);
8773 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8774 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8779 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8780 fprintf(stderr, "%s: mode is required with lockahead\n",
8785 while (optind < argc) {
8788 path = argv[optind++];
8790 fd = open(path, O_RDONLY);
8792 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8793 argv[0], path, strerror(errno));
8798 advice.lla_start = start;
8799 advice.lla_end = end;
8800 advice.lla_advice = advice_type;
8801 advice.lla_value1 = 0;
8802 advice.lla_value2 = 0;
8803 advice.lla_value3 = 0;
8804 advice.lla_value4 = 0;
8805 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8806 advice.lla_lockahead_mode = mode;
8807 advice.lla_peradvice_flags = flags;
8810 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8813 fprintf(stderr, "%s: cannot give advice '%s' to file "
8814 "'%s': %s\n", argv[0],
8815 ladvise_names[advice_type],
8816 path, strerror(errno));
8822 if (rc == 0 && rc2 < 0)
8828 static const char *const heat_names[] = LU_HEAT_NAMES;
8830 static int lfs_heat_get(int argc, char **argv)
8832 struct lu_heat *heat;
8841 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8843 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8848 while (optind < argc) {
8849 path = argv[optind++];
8851 fd = open(path, O_RDONLY);
8853 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8854 argv[0], path, strerror(errno));
8859 heat->lh_count = OBD_HEAT_COUNT;
8860 rc2 = llapi_heat_get(fd, heat);
8863 fprintf(stderr, "%s: cannot get heat of file '%s'"
8864 ": %s\n", argv[0], path, strerror(errno));
8868 printf("flags: %x\n", heat->lh_flags);
8869 for (i = 0; i < heat->lh_count; i++)
8870 printf("%s: %llu\n", heat_names[i],
8871 (unsigned long long)heat->lh_heat[i]);
8873 if (rc == 0 && rc2 < 0)
8881 static int lfs_heat_set(int argc, char **argv)
8883 struct option long_opts[] = {
8884 { .val = 'c', .name = "clear", .has_arg = no_argument },
8885 { .val = 'o', .name = "off", .has_arg = no_argument },
8886 { .val = 'O', .name = "on", .has_arg = no_argument },
8888 enum lu_heat_flag flags = 0;
8898 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8901 flags |= LU_HEAT_FLAG_CLEAR;
8904 flags |= LU_HEAT_FLAG_CLEAR;
8905 flags |= LU_HEAT_FLAG_OFF;
8908 flags &= ~LU_HEAT_FLAG_OFF;
8913 fprintf(stderr, "%s: option '%s' unrecognized\n",
8914 argv[0], argv[optind - 1]);
8919 if (argc <= optind) {
8920 fprintf(stderr, "%s: please give one or more file names\n",
8925 while (optind < argc) {
8926 path = argv[optind++];
8928 fd = open(path, O_RDONLY);
8930 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8931 argv[0], path, strerror(errno));
8936 rc2 = llapi_heat_set(fd, flags);
8939 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8940 ": %s\n", argv[0], path, strerror(errno));
8944 if (rc == 0 && rc2 < 0)
8950 /** The input string contains a comma delimited list of component ids and
8951 * ranges, for example "1,2-4,7".
8953 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8955 bool end_of_loop = false;
8963 while (!end_of_loop) {
8967 char *endptr = NULL;
8970 ptr = strchrnul(arg, ',');
8971 end_of_loop = *ptr == '\0';
8974 start_index = strtol(arg, &endptr, 0);
8975 if (endptr == arg) /* no data at all */
8977 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8979 if (start_index < 0)
8982 end_index = start_index;
8983 if (*endptr == '-') {
8984 end_index = strtol(endptr + 1, &endptr, 0);
8985 if (*endptr != '\0')
8987 if (end_index < start_index)
8991 for (i = start_index; i <= end_index && size > 0; i++) {
8994 /* remove duplicate */
8995 for (j = 0; j < nr; j++) {
8999 if (j == nr) { /* no duplicate */
9005 if (size == 0 && i < end_index)
9012 if (!end_of_loop && ptr != NULL)
9015 return rc < 0 ? rc : nr;
9019 * struct verify_mirror_id - Mirror id to be verified.
9020 * @mirror_id: A specified mirror id.
9021 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9023 struct verify_mirror_id {
9029 * compare_mirror_ids() - Compare mirror ids.
9030 * @layout: Mirror component list.
9031 * @cbdata: Callback data in verify_mirror_id structure.
9033 * This is a callback function called by llapi_layout_comp_iterate()
9034 * to compare the specified mirror id with the one in the current
9035 * component of @layout. If they are the same, then the specified
9036 * mirror id is valid.
9038 * Return: a negative error code on failure or
9039 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9040 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9043 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9045 struct verify_mirror_id *mirror_id_cbdata =
9046 (struct verify_mirror_id *)cbdata;
9050 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9054 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9055 progname, strerror(errno));
9059 if (mirror_id_cbdata->mirror_id == mirror_id) {
9060 mirror_id_cbdata->is_valid_id = true;
9061 return LLAPI_LAYOUT_ITER_STOP;
9064 return LLAPI_LAYOUT_ITER_CONT;
9068 * verify_mirror_ids() - Verify specified mirror ids.
9069 * @fname: Mirrored file name.
9070 * @mirror_ids: Specified mirror ids to be verified.
9071 * @ids_nr: Number of specified mirror ids.
9073 * This function verifies that specified @mirror_ids are valid
9074 * in the mirrored file @fname.
9076 * Return: 0 on success or a negative error code on failure.
9079 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9081 struct llapi_layout *layout = NULL;
9082 struct verify_mirror_id mirror_id_cbdata = { 0 };
9093 if (stat(fname, &stbuf) < 0) {
9094 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9095 progname, fname, strerror(errno));
9100 if (!S_ISREG(stbuf.st_mode)) {
9101 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9107 fd = open(fname, O_DIRECT | O_RDONLY);
9109 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9110 progname, fname, strerror(errno));
9115 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9117 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9118 progname, fname, strerror(errno));
9122 layout = llapi_layout_get_by_fd(fd, 0);
9123 if (layout == NULL) {
9124 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9125 progname, fname, strerror(errno));
9127 llapi_lease_release(fd);
9131 rc = llapi_layout_flags_get(layout, &flr_state);
9133 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9134 progname, fname, strerror(errno));
9139 flr_state &= LCM_FL_FLR_MASK;
9140 switch (flr_state) {
9143 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9144 progname, fname, llapi_layout_flags_string(flr_state));
9151 for (i = 0; i < ids_nr; i++) {
9152 mirror_id_cbdata.mirror_id = mirror_ids[i];
9153 mirror_id_cbdata.is_valid_id = false;
9155 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9160 "%s: '%s' failed to verify mirror id: %u.\n",
9161 progname, fname, mirror_ids[i]);
9165 if (!mirror_id_cbdata.is_valid_id) {
9168 "%s: '%s' invalid specified mirror id: %u.\n",
9169 progname, fname, mirror_ids[i]);
9175 llapi_layout_free(layout);
9176 llapi_lease_release(fd);
9184 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9185 __u16 *mirror_ids, int ids_nr)
9187 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9188 struct llapi_layout *layout;
9199 if (stat(fname, &stbuf) < 0) {
9200 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9201 progname, fname, strerror(errno));
9205 if (!S_ISREG(stbuf.st_mode)) {
9206 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9212 fd = open(fname, O_DIRECT | O_RDWR);
9214 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9215 progname, fname, strerror(errno));
9220 layout = llapi_layout_get_by_fd(fd, 0);
9221 if (layout == NULL) {
9222 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9223 progname, fname, strerror(errno));
9228 rc = llapi_layout_flags_get(layout, &flr_state);
9230 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9231 progname, fname, strerror(errno));
9236 flr_state &= LCM_FL_FLR_MASK;
9237 if (flr_state == LCM_FL_NONE) {
9239 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9244 /* get stale component info */
9245 comp_size = llapi_mirror_find_stale(layout, comp_array,
9246 ARRAY_SIZE(comp_array),
9247 mirror_ids, ids_nr);
9248 if (comp_size <= 0) {
9253 ioc->lil_mode = LL_LEASE_WRLCK;
9254 ioc->lil_flags = LL_LEASE_RESYNC;
9255 rc = llapi_lease_set(fd, ioc);
9257 if (rc == -EALREADY)
9261 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9262 progname, fname, strerror(-rc));
9266 /* get the read range [start, end) */
9267 start = comp_array[0].lrc_start;
9268 end = comp_array[0].lrc_end;
9269 for (idx = 1; idx < comp_size; idx++) {
9270 if (comp_array[idx].lrc_start < start)
9271 start = comp_array[idx].lrc_start;
9272 if (end < comp_array[idx].lrc_end)
9273 end = comp_array[idx].lrc_end;
9276 rc = llapi_lease_check(fd);
9277 if (rc != LL_LEASE_WRLCK) {
9278 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9283 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9286 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9287 progname, fname, strerror(-rc));
9289 /* need to do the lease unlock even resync fails */
9290 ioc->lil_mode = LL_LEASE_UNLCK;
9291 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9293 for (idx = 0; idx < comp_size; idx++) {
9294 if (comp_array[idx].lrc_synced) {
9295 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9300 rc2 = llapi_lease_set(fd, ioc);
9302 * llapi_lease_set returns lease mode when it request to unlock
9306 /* rc2 == 0 means lost lease lock */
9307 if (rc2 == 0 && rc == 0)
9309 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9311 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9315 llapi_layout_free(layout);
9322 static inline int lfs_mirror_resync(int argc, char **argv)
9324 struct ll_ioc_lease *ioc = NULL;
9325 __u16 mirror_ids[128] = { 0 };
9330 struct option long_opts[] = {
9331 { .val = 'o', .name = "only", .has_arg = required_argument },
9334 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9337 rc = parse_mirror_ids(mirror_ids,
9338 sizeof(mirror_ids) / sizeof(__u16),
9342 "%s: bad mirror ids '%s'.\n",
9349 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9350 argv[0], argv[optind - 1]);
9356 if (argc == optind) {
9357 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9362 if (ids_nr > 0 && argc > optind + 1) {
9364 "%s: option '--only' cannot be used upon multiple files.\n",
9372 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9377 /* set the lease on the file */
9378 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9380 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9381 argv[0], strerror(errno));
9386 for (; optind < argc; optind++) {
9387 rc = lfs_mirror_resync_file(argv[optind], ioc,
9388 mirror_ids, ids_nr);
9389 /* ignore previous file's error, continue with next file */
9392 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9400 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9402 struct llapi_layout *layout;
9405 layout = llapi_layout_get_by_fd(fd, 0);
9406 if (layout == NULL) {
9407 fprintf(stderr, "could not get layout.\n");
9411 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9413 fprintf(stderr, "failed to iterate layout\n");
9414 llapi_layout_free(layout);
9417 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9418 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9419 llapi_layout_free(layout);
9423 llapi_layout_free(layout);
9429 * Check whether two files are the same file
9430 * \retval 0 same file
9431 * \retval 1 not the same file
9432 * \retval <0 error code
9434 static inline int check_same_file(const char *f1, const char *f2)
9439 if (stat(f1, &stbuf1) < 0) {
9440 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9441 progname, f1, strerror(errno));
9445 if (stat(f2, &stbuf2) < 0) {
9446 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9447 progname, f2, strerror(errno));
9451 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9452 stbuf1.st_ino == stbuf2.st_ino)
9458 static inline int lfs_mirror_read(int argc, char **argv)
9461 __u16 mirror_id = 0;
9462 const char *outfile = NULL;
9468 const size_t buflen = 4 << 20;
9470 struct option long_opts[] = {
9471 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9472 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9475 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9480 mirror_id = strtoul(optarg, &end, 0);
9481 if (*end != '\0' || mirror_id == 0) {
9483 "%s %s: invalid mirror ID '%s'\n",
9484 progname, argv[0], optarg);
9492 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9493 progname, argv[optind - 1]);
9498 if (argc == optind) {
9499 fprintf(stderr, "%s %s: no mirrored file provided\n",
9502 } else if (argc > optind + 1) {
9503 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9507 if (mirror_id == 0) {
9508 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9513 /* open mirror file */
9514 fname = argv[optind];
9517 rc = check_same_file(fname, outfile);
9520 "%s %s: output file cannot be the mirrored file\n",
9528 fd = open(fname, O_DIRECT | O_RDONLY);
9530 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9531 progname, argv[0], fname, strerror(errno));
9535 /* verify mirror id */
9536 rc = verify_mirror_id_by_fd(fd, mirror_id);
9539 "%s %s: cannot find mirror with ID %u in '%s'\n",
9540 progname, argv[0], mirror_id, fname);
9544 /* open output file */
9546 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9548 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9549 progname, argv[0], outfile, strerror(errno));
9554 outfd = STDOUT_FILENO;
9557 /* allocate buffer */
9558 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9560 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9561 progname, argv[0], rc);
9568 ssize_t written = 0;
9570 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9571 if (bytes_read < 0) {
9574 "%s %s: fail to read data from mirror %u: %s\n",
9575 progname, argv[0], mirror_id, strerror(-rc));
9580 if (bytes_read == 0)
9583 while (written < bytes_read) {
9586 written2 = write(outfd, buf + written,
9587 bytes_read - written);
9590 "%s %s: fail to write %s: %s\n",
9591 progname, argv[0], outfile ? : "STDOUT",
9596 written += written2;
9599 if (written != bytes_read) {
9601 "%s %s: written %ld bytes does not match with %ld read.\n",
9602 progname, argv[0], written, bytes_read);
9624 static inline int lfs_mirror_write(int argc, char **argv)
9627 __u16 mirror_id = 0;
9628 const char *inputfile = NULL;
9634 const size_t buflen = 4 << 20;
9636 size_t page_size = sysconf(_SC_PAGESIZE);
9637 struct ll_ioc_lease_id ioc;
9639 struct option long_opts[] = {
9640 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9641 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9644 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9649 mirror_id = strtoul(optarg, &end, 0);
9650 if (*end != '\0' || mirror_id == 0) {
9652 "%s %s: invalid mirror ID '%s'\n",
9653 progname, argv[0], optarg);
9661 fprintf(stderr, "%s: option '%s' unrecognized\n",
9662 progname, argv[optind - 1]);
9667 if (argc == optind) {
9668 fprintf(stderr, "%s %s: no mirrored file provided\n",
9671 } else if (argc > optind + 1) {
9672 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9676 if (mirror_id == 0) {
9677 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9682 /* open mirror file */
9683 fname = argv[optind];
9686 rc = check_same_file(fname, inputfile);
9689 "%s %s: input file cannot be the mirrored file\n",
9697 fd = open(fname, O_DIRECT | O_WRONLY);
9699 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9700 progname, argv[0], fname, strerror(errno));
9704 /* verify mirror id */
9705 rc = verify_mirror_id_by_fd(fd, mirror_id);
9708 "%s %s: cannot find mirror with ID %u in '%s'\n",
9709 progname, argv[0], mirror_id, fname);
9713 /* open input file */
9715 inputfd = open(inputfile, O_RDONLY, 0644);
9717 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9718 progname, argv[0], inputfile, strerror(errno));
9723 inputfd = STDIN_FILENO;
9726 /* allocate buffer */
9727 rc = posix_memalign(&buf, page_size, buflen);
9729 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9730 progname, argv[0], rc);
9734 /* prepare target mirror components instantiation */
9735 ioc.lil_mode = LL_LEASE_WRLCK;
9736 ioc.lil_flags = LL_LEASE_RESYNC;
9737 ioc.lil_mirror_id = mirror_id;
9738 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9741 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9742 progname, argv[0], fname, strerror(errno));
9752 rc = llapi_lease_check(fd);
9753 if (rc != LL_LEASE_WRLCK) {
9754 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9755 progname, argv[0], fname);
9759 bytes_read = read(inputfd, buf, buflen);
9760 if (bytes_read < 0) {
9763 "%s %s: fail to read data from '%s': %s\n",
9764 progname, argv[0], inputfile ? : "STDIN",
9771 if (bytes_read == 0)
9774 /* round up to page align to make direct IO happy. */
9775 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9777 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9782 "%s %s: fail to write to mirror %u: %s\n",
9783 progname, argv[0], mirror_id,
9791 if (pos & (page_size - 1)) {
9792 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9797 ioc.lil_mode = LL_LEASE_UNLCK;
9798 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9800 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9805 "%s %s: release lease lock of '%s' failed: %s\n",
9806 progname, argv[0], fname, strerror(errno));
9823 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9825 struct llapi_layout *layout;
9826 struct collect_ids_data cid = { .cid_ids = ids,
9828 .cid_exclude = exclude_id, };
9831 layout = llapi_layout_get_by_fd(fd, 0);
9832 if (layout == NULL) {
9833 fprintf(stderr, "could not get layout\n");
9837 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9839 fprintf(stderr, "failed to iterate layout\n");
9840 llapi_layout_free(layout);
9844 llapi_layout_free(layout);
9846 return cid.cid_count;
9849 static inline int lfs_mirror_copy(int argc, char **argv)
9852 __u16 read_mirror_id = 0;
9853 __u16 ids[128] = { 0 };
9855 struct llapi_layout *layout = NULL;
9856 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9863 struct ll_ioc_lease *ioc = NULL;
9864 struct ll_ioc_lease_id *resync_ioc;
9866 struct option long_opts[] = {
9867 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9868 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9871 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9876 read_mirror_id = strtoul(optarg, &end, 0);
9877 if (*end != '\0' || read_mirror_id == 0) {
9879 "%s %s: invalid read mirror ID '%s'\n",
9880 progname, argv[0], optarg);
9885 if (!strcmp(optarg, "-1")) {
9886 /* specify all other mirrors */
9890 count = parse_mirror_ids((__u16 *)ids,
9898 fprintf(stderr, "%s: option '%s' unrecognized\n",
9899 progname, argv[optind - 1]);
9904 if (argc == optind) {
9905 fprintf(stderr, "%s %s: no mirrored file provided\n",
9908 } else if (argc > optind + 1) {
9909 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9913 if (read_mirror_id == 0) {
9915 "%s %s: no valid read mirror ID %d is provided\n",
9916 progname, argv[0], read_mirror_id);
9922 "%s %s: no write mirror ID is provided\n",
9927 for (i = 0; i < count; i++) {
9928 if (read_mirror_id == ids[i]) {
9930 "%s %s: read and write mirror ID cannot be the same\n",
9936 /* open mirror file */
9937 fname = argv[optind];
9939 fd = open(fname, O_DIRECT | O_RDWR);
9941 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9942 progname, argv[0], fname, strerror(errno));
9946 /* write to all other mirrors */
9947 if (ids[0] == (__u16)-1) {
9948 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9952 "%s %s: failed to get other mirror ids in '%s': %d\n",
9953 progname, argv[0], fname, rc);
9958 /* verify mirror id */
9959 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9962 "%s %s: cannot find mirror with ID %u in '%s'\n",
9963 progname, argv[0], read_mirror_id, fname);
9967 for (i = 0; i < count; i++) {
9968 rc = verify_mirror_id_by_fd(fd, ids[i]);
9971 "%s %s: cannot find mirror with ID %u in '%s'\n",
9972 progname, argv[0], ids[i], fname);
9977 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9980 "%s %s: cannot alloc comp id array for ioc: %s\n",
9981 progname, argv[0], strerror(errno));
9986 /* get stale component info */
9987 layout = llapi_layout_get_by_fd(fd, 0);
9988 if (layout == NULL) {
9989 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9990 progname, argv[0], fname, strerror(errno));
9994 comp_size = llapi_mirror_find_stale(layout, comp_array,
9995 ARRAY_SIZE(comp_array),
9997 llapi_layout_free(layout);
9998 if (comp_size < 0) {
10003 /* prepare target mirror components instantiation */
10004 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10005 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10006 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10008 resync_ioc->lil_mirror_id = ids[0];
10010 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10011 rc = llapi_lease_set(fd, ioc);
10014 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10015 progname, argv[0], fname, strerror(errno));
10019 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10022 fprintf(stderr, "%s %s: copy error: %d\n",
10023 progname, argv[0], rc);
10027 fprintf(stdout, "mirror copied successfully: ");
10028 for (i = 0; i < copied; i++)
10029 fprintf(stdout, "%d ", ids[i]);
10030 fprintf(stdout, "\n");
10032 ioc->lil_mode = LL_LEASE_UNLCK;
10033 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10034 ioc->lil_count = 0;
10035 for (i = 0; i < comp_size; i++) {
10038 for (j = 0; j < copied; j++) {
10039 if (comp_array[i].lrc_mirror_id != ids[j])
10042 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10046 rc = llapi_lease_set(fd, ioc);
10051 "%s %s: release lease lock of '%s' failed: %s\n",
10052 progname, argv[0], fname, strerror(errno));
10066 * struct verify_chunk - Mirror chunk to be verified.
10067 * @chunk: [start, end) of the chunk.
10068 * @mirror_count: Number of mirror ids in @mirror_id array.
10069 * @mirror_id: Array of valid mirror ids that cover the chunk.
10071 struct verify_chunk {
10072 struct lu_extent chunk;
10073 unsigned int mirror_count;
10074 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10078 * print_chunks() - Print chunk information.
10079 * @fname: Mirrored file name.
10080 * @chunks: Array of chunks.
10081 * @chunk_count: Number of chunks in @chunks array.
10083 * This function prints [start, end) of each chunk in @chunks
10084 * for mirrored file @fname, and also prints the valid mirror ids
10085 * that cover the chunk.
10090 void print_chunks(const char *fname, struct verify_chunk *chunks,
10096 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10097 for (i = 0; i < chunk_count; i++) {
10098 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10100 if (chunks[i].mirror_count == 0)
10101 fprintf(stdout, "\t[");
10103 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10104 for (j = 1; j < chunks[i].mirror_count; j++)
10105 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10107 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10109 fprintf(stdout, "\n");
10113 * print_checksums() - Print CRC-32 checksum values.
10114 * @chunk: A chunk and its corresponding valid mirror ids.
10115 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10117 * This function prints CRC-32 checksum values on @chunk for
10118 * each valid mirror that covers it.
10123 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10128 "CRC-32 checksum value for chunk "DEXT":\n",
10129 PEXT(&chunk->chunk));
10130 for (i = 0; i < chunk->mirror_count; i++)
10131 fprintf(stdout, "Mirror %u:\t%#lx\n",
10132 chunk->mirror_id[i], crc[i]);
10133 fprintf(stdout, "\n");
10137 * filter_mirror_id() - Filter specified mirror ids.
10138 * @chunks: Array of chunks.
10139 * @chunk_count: Number of chunks in @chunks array.
10140 * @mirror_ids: Specified mirror ids to be verified.
10141 * @ids_nr: Number of specified mirror ids.
10143 * This function scans valid mirror ids that cover each chunk in @chunks
10144 * and filters specified mirror ids.
10149 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10150 __u16 *mirror_ids, int ids_nr)
10155 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10156 unsigned int valid_count = 0;
10158 for (i = 0; i < chunk_count; i++) {
10159 if (chunks[i].mirror_count == 0)
10163 for (j = 0; j < ids_nr; j++) {
10164 for (k = 0; k < chunks[i].mirror_count; k++) {
10165 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10166 valid_id[valid_count] = mirror_ids[j];
10173 memcpy(chunks[i].mirror_id, valid_id,
10174 sizeof(__u16) * valid_count);
10175 chunks[i].mirror_count = valid_count;
10180 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10181 * @layout: Mirror component list.
10182 * @chunks: Array of chunks.
10183 * @chunks_size: Array size of @chunks.
10185 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10186 * to find out chunk segments and store them in @chunks array.
10188 * The @mirror_id array in each element of @chunks will store the valid
10189 * mirror ids that cover the chunk. If a mirror component covering the
10190 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10191 * will not be stored into the @mirror_id array, and the chunk for that
10192 * mirror will not be verified.
10194 * The @mirror_count in each element of @chunks will store the number of
10195 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10196 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10197 * indicates the chunk is valid in only one mirror. In both cases, the
10198 * chunk will not be verified.
10200 * Here is an example:
10202 * 0 1M 2M 3M 4M EOF
10203 * +------+-------------+--------------------+
10204 * | | | S | mirror1
10205 * +------+------+------+------+-------------+
10206 * | | S | S | | mirror2
10207 * +-------------+------+------+-------------+
10209 * prepared @chunks array will contain 5 elements:
10210 * (([0, 1M), [1, 2], 2),
10211 * ([1M, 2M), [1, 2], 2),
10212 * ([2M, 3M), [1], 1),
10213 * ([3M, 4M], [], 0),
10214 * ([4M, EOF), [2], 1))
10216 * Return: the actual array size of @chunks on success
10217 * or a negative error code on failure.
10220 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10221 struct verify_chunk *chunks,
10222 size_t chunks_size)
10226 uint32_t mirror_id;
10232 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10235 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10238 "%s: move to the first layout component: %s.\n",
10239 progname, strerror(errno));
10245 chunks[idx].chunk.e_end = LUSTRE_EOF;
10247 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10250 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10251 progname, strerror(errno));
10255 if (start > chunks[idx].chunk.e_start ||
10256 end <= chunks[idx].chunk.e_start)
10259 if (end < chunks[idx].chunk.e_end)
10260 chunks[idx].chunk.e_end = end;
10262 rc = llapi_layout_comp_flags_get(layout, &flags);
10265 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10266 progname, strerror(errno));
10270 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10273 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10276 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10277 progname, strerror(errno));
10281 chunks[idx].mirror_id[i] = mirror_id;
10283 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10285 "%s: mirror_id array is too small.\n",
10292 rc = llapi_layout_comp_use(layout,
10293 LLAPI_LAYOUT_COMP_USE_NEXT);
10296 "%s: move to the next layout component: %s.\n",
10297 progname, strerror(errno));
10300 } /* loop through all components */
10302 chunks[idx].mirror_count = i;
10304 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10308 if (idx >= chunks_size) {
10309 fprintf(stderr, "%s: chunks array is too small.\n",
10315 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10319 return rc < 0 ? rc : idx + 1;
10323 * lfs_mirror_verify_chunk() - Verify a chunk.
10324 * @fd: File descriptor of the mirrored file.
10325 * @file_size: Size of the mirrored file.
10326 * @chunk: A chunk and its corresponding valid mirror ids.
10327 * @verbose: Verbose mode.
10329 * This function verifies a @chunk contains exactly the same data
10330 * ammong the mirrors that cover it.
10332 * If @verbose is specified, then the function will print where the
10333 * differences are if the data do not match. Otherwise, it will
10334 * just return an error in that case.
10336 * Return: 0 on success or a negative error code on failure.
10339 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10340 struct verify_chunk *chunk, int verbose)
10342 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10344 size_t page_size = sysconf(_SC_PAGESIZE);
10345 ssize_t bytes_read;
10346 ssize_t bytes_done;
10350 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10354 if (file_size == 0)
10357 rc = posix_memalign(&buf, page_size, buflen);
10358 if (rc) /* error code is returned directly */
10362 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10363 PEXT(&chunk->chunk));
10364 for (i = 0; i < chunk->mirror_count; i++)
10365 fprintf(stdout, " %u", chunk->mirror_id[i]);
10366 fprintf(stdout, "\n");
10370 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10371 pos = chunk->chunk.e_start;
10372 while (bytes_done < count) {
10373 /* compute initial CRC-32 checksum */
10374 crc = crc32(0L, Z_NULL, 0);
10375 memset(crc_array, 0, sizeof(crc_array));
10378 for (i = 0; i < chunk->mirror_count; i++) {
10379 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10381 if (bytes_read < 0) {
10384 "%s: failed to read data from mirror %u: %s.\n",
10385 progname, chunk->mirror_id[i],
10390 /* compute new CRC-32 checksum */
10391 crc_array[i] = crc32(crc, buf, bytes_read);
10395 print_checksums(chunk, crc_array);
10397 /* compare CRC-32 checksum values */
10398 for (i = 1; i < chunk->mirror_count; i++) {
10399 if (crc_array[i] != crc_array[0]) {
10403 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10404 progname, PEXT(&chunk->chunk),
10405 chunk->mirror_id[0],
10406 chunk->mirror_id[i]);
10411 bytes_done += bytes_read;
10414 if (verbose > 1 && rc == 0) {
10415 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10416 PEXT(&chunk->chunk));
10417 for (i = 0; i < chunk->mirror_count; i++)
10418 fprintf(stdout, " %u", chunk->mirror_id[i]);
10419 fprintf(stdout, " PASS\n\n");
10428 * lfs_mirror_verify_file() - Verify a mirrored file.
10429 * @fname: Mirrored file name.
10430 * @mirror_ids: Specified mirror ids to be verified.
10431 * @ids_nr: Number of specified mirror ids.
10432 * @verbose: Verbose mode.
10434 * This function verifies that each SYNC mirror of a mirrored file
10435 * specified by @fname contains exactly the same data.
10437 * If @mirror_ids is specified, then the function will verify the
10438 * mirrors specified by @mirror_ids contain exactly the same data.
10440 * If @verbose is specified, then the function will print where the
10441 * differences are if the data do not match. Otherwise, it will
10442 * just return an error in that case.
10444 * Return: 0 on success or a negative error code on failure.
10447 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10450 struct verify_chunk chunks_array[1024] = { };
10451 struct llapi_layout *layout = NULL;
10453 uint32_t flr_state;
10455 int chunk_count = 0;
10461 if (stat(fname, &stbuf) < 0) {
10462 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10463 progname, fname, strerror(errno));
10468 if (!S_ISREG(stbuf.st_mode)) {
10469 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10475 if (stbuf.st_size == 0) {
10477 fprintf(stdout, "%s: '%s' file size is 0.\n",
10483 fd = open(fname, O_DIRECT | O_RDONLY);
10485 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10486 progname, fname, strerror(errno));
10491 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10493 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10494 progname, fname, strerror(errno));
10498 layout = llapi_layout_get_by_fd(fd, 0);
10499 if (layout == NULL) {
10500 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10501 progname, fname, strerror(errno));
10503 llapi_lease_release(fd);
10507 rc = llapi_layout_flags_get(layout, &flr_state);
10509 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10510 progname, fname, strerror(errno));
10515 flr_state &= LCM_FL_FLR_MASK;
10516 switch (flr_state) {
10519 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10520 progname, fname, llapi_layout_flags_string(flr_state));
10526 /* find out mirror chunks to be verified */
10527 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10528 ARRAY_SIZE(chunks_array));
10529 if (chunk_count < 0) {
10535 /* filter specified mirror ids */
10536 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10539 print_chunks(fname, chunks_array, chunk_count);
10541 for (idx = 0; idx < chunk_count; idx++) {
10542 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10545 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10547 PEXT(&chunks_array[idx].chunk),
10548 (unsigned long long)stbuf.st_size);
10552 if (chunks_array[idx].mirror_count == 0) {
10554 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10556 PEXT(&chunks_array[idx].chunk));
10558 fprintf(stderr, "skipped\n");
10562 fprintf(stderr, "failed\n");
10566 if (chunks_array[idx].mirror_count == 1) {
10569 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10571 PEXT(&chunks_array[idx].chunk),
10572 chunks_array[idx].mirror_id[0]);
10576 rc = llapi_lease_check(fd);
10577 if (rc != LL_LEASE_RDLCK) {
10578 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10583 /* verify one chunk */
10584 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10585 &chunks_array[idx], verbose);
10599 llapi_layout_free(layout);
10600 llapi_lease_release(fd);
10608 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10609 * @argc: The count of lfs mirror verify command line arguments.
10610 * @argv: Array of strings for lfs mirror verify command line arguments.
10612 * This function parses lfs mirror verify command and verifies the
10613 * specified mirrored file(s).
10615 * Return: 0 on success or a negative error code on failure.
10617 static inline int lfs_mirror_verify(int argc, char **argv)
10619 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10625 char cmd[PATH_MAX];
10627 struct option long_opts[] = {
10628 { .val = 'o', .name = "only", .has_arg = required_argument },
10629 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10630 { .name = NULL } };
10632 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10634 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10637 rc = parse_mirror_ids(mirror_ids,
10638 ARRAY_SIZE(mirror_ids),
10642 "%s: bad mirror ids '%s'.\n",
10649 "%s: at least 2 mirror ids needed with '--only' option.\n",
10659 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10660 progname, argv[optind - 1]);
10666 if (argc == optind) {
10667 fprintf(stderr, "%s: no file name given.\n", progname);
10672 if (ids_nr > 0 && argc > optind + 1) {
10674 "%s: '--only' cannot be used upon multiple files.\n",
10682 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10688 for (; optind < argc; optind++) {
10689 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10699 * lfs_mirror() - Parse and execute lfs mirror commands.
10700 * @argc: The count of lfs mirror command line arguments.
10701 * @argv: Array of strings for lfs mirror command line arguments.
10703 * This function parses lfs mirror commands and performs the
10704 * corresponding functions specified in mirror_cmdlist[].
10706 * Return: 0 on success or an error code on failure.
10708 static int lfs_mirror(int argc, char **argv)
10710 char cmd[PATH_MAX];
10713 setlinebuf(stdout);
10715 Parser_init("lfs-mirror > ", mirror_cmdlist);
10717 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10719 program_invocation_short_name = cmd;
10721 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10723 rc = Parser_commands();
10725 return rc < 0 ? -rc : rc;
10728 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10730 #if __BYTE_ORDER == __BIG_ENDIAN
10731 __swab16s(&attrs->lsa_valid);
10732 __swab64s(&attrs->lsa_size);
10733 __swab64s(&attrs->lsa_blocks);
10737 enum lfs_som_type {
10738 LFS_SOM_SIZE = 0x1,
10739 LFS_SOM_BLOCKS = 0x2,
10740 LFS_SOM_FLAGS = 0x4,
10741 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10745 static int lfs_getsom(int argc, char **argv)
10748 struct lustre_som_attrs *attrs;
10749 char buf[sizeof(*attrs) + 64];
10750 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10753 while ((c = getopt(argc, argv, "sbf")) != -1) {
10756 type = LFS_SOM_SIZE;
10759 type = LFS_SOM_BLOCKS;
10762 type = LFS_SOM_FLAGS;
10765 fprintf(stderr, "%s: invalid option '%c'\n",
10775 fprintf(stderr, "%s: %s\n",
10776 progname, argc == 0 ? "miss file target" :
10777 "input more than 2 files");
10782 attrs = (void *)buf;
10783 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10786 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10787 argv[0], strerror(errno), errno);
10791 lustre_som_swab(attrs);
10794 case LFS_SOM_ATTR_ALL:
10795 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10796 path, (unsigned long long)attrs->lsa_size,
10797 (unsigned long long)attrs->lsa_blocks,
10801 printf("%llu\n", (unsigned long long)attrs->lsa_size);
10803 case LFS_SOM_BLOCKS:
10804 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
10806 case LFS_SOM_FLAGS:
10807 printf("%x\n", attrs->lsa_valid);
10810 fprintf(stderr, "%s: unknown option\n", progname);
10818 * lfs_mirror_list_commands() - List lfs mirror commands.
10819 * @argc: The count of command line arguments.
10820 * @argv: Array of strings for command line arguments.
10822 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10824 * Return: 0 on success.
10826 static int lfs_mirror_list_commands(int argc, char **argv)
10828 char buffer[81] = "";
10830 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10836 static int lfs_pcc_attach(int argc, char **argv)
10838 struct option long_opts[] = {
10839 { .val = 'i', .name = "id", .has_arg = required_argument },
10840 { .name = NULL } };
10843 __u32 archive_id = 0;
10846 char fullpath[PATH_MAX];
10847 enum lu_pcc_type type = LU_PCC_READWRITE;
10850 while ((c = getopt_long(argc, argv, "i:",
10851 long_opts, NULL)) != -1) {
10854 archive_id = strtoul(optarg, &end, 0);
10855 if (*end != '\0' || archive_id == 0) {
10856 fprintf(stderr, "error: %s: bad archive ID "
10857 "'%s'\n", argv[0], optarg);
10864 fprintf(stderr, "%s: option '%s' unrecognized\n",
10865 argv[0], argv[optind - 1]);
10870 if (archive_id == 0) {
10871 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
10875 if (argc <= optind) {
10876 fprintf(stderr, "%s: must specify one or more file names\n",
10881 while (optind < argc) {
10884 path = argv[optind++];
10885 if (realpath(path, fullpath) == NULL) {
10886 fprintf(stderr, "%s: could not find path '%s': %s\n",
10887 argv[0], path, strerror(errno));
10893 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10895 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10896 "with archive ID '%u': %s\n", argv[0],
10897 path, archive_id, strerror(-rc2));
10905 static int lfs_pcc_attach_fid(int argc, char **argv)
10907 struct option long_opts[] = {
10908 { .val = 'i', .name = "id", .has_arg = required_argument },
10909 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10910 { .name = NULL } };
10911 char short_opts[] = "i:m:";
10914 __u32 archive_id = 0;
10916 const char *mntpath = NULL;
10917 const char *fidstr;
10918 enum lu_pcc_type type = LU_PCC_READWRITE;
10921 while ((c = getopt_long(argc, argv, short_opts,
10922 long_opts, NULL)) != -1) {
10925 archive_id = strtoul(optarg, &end, 0);
10926 if (*end != '\0') {
10927 fprintf(stderr, "error: %s: bad archive ID "
10928 "'%s'\n", argv[0], optarg);
10938 fprintf(stderr, "%s: option '%s' unrecognized\n",
10939 argv[0], argv[optind - 1]);
10944 if (archive_id == 0) {
10945 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10949 if (mntpath == NULL) {
10950 fprintf(stderr, "%s: must specify Lustre mount point\n",
10955 if (argc <= optind) {
10956 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10960 while (optind < argc) {
10963 fidstr = argv[optind++];
10965 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10968 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10969 "with archive ID '%u': %s\n", argv[0],
10970 fidstr, mntpath, archive_id, strerror(rc2));
10972 if (rc == 0 && rc2 < 0)
10978 static int lfs_pcc_detach(int argc, char **argv)
10980 struct option long_opts[] = {
10981 { .val = 'k', .name = "keep", .has_arg = no_argument },
10982 { .name = NULL } };
10983 char short_opts[] = "k";
10987 char fullpath[PATH_MAX];
10988 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
10991 while ((c = getopt_long(argc, argv, short_opts,
10992 long_opts, NULL)) != -1) {
10995 detach_opt = PCC_DETACH_OPT_NONE;
11000 fprintf(stderr, "%s: option '%s' unrecognized\n",
11001 argv[0], argv[optind - 1]);
11006 while (optind < argc) {
11009 path = argv[optind++];
11010 if (realpath(path, fullpath) == NULL) {
11011 fprintf(stderr, "%s: could not find path '%s': %s\n",
11012 argv[0], path, strerror(errno));
11018 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11021 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
11022 "%s\n", argv[0], path, strerror(errno));
11030 static int lfs_pcc_detach_fid(int argc, char **argv)
11032 struct option long_opts[] = {
11033 { .val = 'k', .name = "keep", .has_arg = no_argument },
11034 { .name = NULL } };
11035 char short_opts[] = "k";
11039 const char *mntpath;
11040 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11043 while ((c = getopt_long(argc, argv, short_opts,
11044 long_opts, NULL)) != -1) {
11047 detach_opt = PCC_DETACH_OPT_NONE;
11052 fprintf(stderr, "%s: option '%s' unrecognized\n",
11053 argv[0], argv[optind - 1]);
11058 mntpath = argv[optind++];
11060 while (optind < argc) {
11063 fid = argv[optind++];
11065 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11067 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
11068 "from PCC: %s\n", argv[0], fid, mntpath,
11077 static int lfs_pcc_state(int argc, char **argv)
11081 char fullpath[PATH_MAX];
11082 struct lu_pcc_state state;
11087 fprintf(stderr, "%s: must specify one or more file names\n",
11092 while (optind < argc) {
11095 path = argv[optind++];
11096 if (realpath(path, fullpath) == NULL) {
11097 fprintf(stderr, "%s: could not find path '%s': %s\n",
11098 argv[0], path, strerror(errno));
11104 rc2 = llapi_pcc_state_get(fullpath, &state);
11108 fprintf(stderr, "%s: cannot get PCC state of '%s': "
11109 "%s\n", argv[0], path, strerror(-rc2));
11113 printf("file: %s", path);
11114 printf(", type: %s", pcc_type2string(state.pccs_type));
11115 if (state.pccs_type == LU_PCC_NONE &&
11116 state.pccs_open_count == 0) {
11121 printf(", PCC file: %s", state.pccs_path);
11122 printf(", user number: %u", state.pccs_open_count);
11123 printf(", flags: %x", state.pccs_flags);
11130 * lfs_pcc_list_commands() - List lfs pcc commands.
11131 * @argc: The count of command line arguments.
11132 * @argv: Array of strings for command line arguments.
11134 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11136 * Return: 0 on success.
11138 static int lfs_pcc_list_commands(int argc, char **argv)
11140 char buffer[81] = "";
11142 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11149 * lfs_pcc() - Parse and execute lfs pcc commands.
11150 * @argc: The count of lfs pcc command line arguments.
11151 * @argv: Array of strings for lfs pcc command line arguments.
11153 * This function parses lfs pcc commands and performs the
11154 * corresponding functions specified in pcc_cmdlist[].
11156 * Return: 0 on success or an error code on failure.
11158 static int lfs_pcc(int argc, char **argv)
11160 char cmd[PATH_MAX];
11163 setlinebuf(stdout);
11165 Parser_init("lfs-pcc > ", pcc_cmdlist);
11167 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11169 program_invocation_short_name = cmd;
11171 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11173 rc = Parser_commands();
11175 return rc < 0 ? -rc : rc;
11178 static int lfs_list_commands(int argc, char **argv)
11180 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11182 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11187 int main(int argc, char **argv)
11191 /* Ensure that liblustreapi constructor has run */
11192 if (!llapi_liblustreapi_initialized())
11193 fprintf(stderr, "liblustreapi was not properly initialized\n");
11195 setlinebuf(stdout);
11198 Parser_init("lfs > ", cmdlist);
11200 progname = program_invocation_short_name; /* Used in error messages */
11202 llapi_set_command_name(argv[1]);
11203 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11204 llapi_clear_command_name();
11206 rc = Parser_commands();
11209 return rc < 0 ? -rc : rc;
11212 #ifdef _LUSTRE_IDL_H_
11213 /* Everything we need here should be included by lustreapi.h. */
11214 # error "lfs should not depend on lustre_idl.h"
11215 #endif /* _LUSTRE_IDL_H_ */