4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
61 #include <sys/xattr.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
81 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
82 #endif /* !ARRAY_SIZE */
85 static int lfs_find(int argc, char **argv);
86 static int lfs_getstripe(int argc, char **argv);
87 static int lfs_getdirstripe(int argc, char **argv);
88 static int lfs_setdirstripe(int argc, char **argv);
89 static int lfs_rmentry(int argc, char **argv);
90 static int lfs_osts(int argc, char **argv);
91 static int lfs_mdts(int argc, char **argv);
92 static int lfs_df(int argc, char **argv);
93 static int lfs_getname(int argc, char **argv);
94 static int lfs_check(int argc, char **argv);
95 #ifdef HAVE_SYS_QUOTA_H
96 static int lfs_setquota(int argc, char **argv);
97 static int lfs_quota(int argc, char **argv);
98 static int lfs_project(int argc, char **argv);
100 static int lfs_flushctx(int argc, char **argv);
101 static int lfs_poollist(int argc, char **argv);
102 static int lfs_changelog(int argc, char **argv);
103 static int lfs_changelog_clear(int argc, char **argv);
104 static int lfs_fid2path(int argc, char **argv);
105 static int lfs_path2fid(int argc, char **argv);
106 static int lfs_data_version(int argc, char **argv);
107 static int lfs_hsm_state(int argc, char **argv);
108 static int lfs_hsm_set(int argc, char **argv);
109 static int lfs_hsm_clear(int argc, char **argv);
110 static int lfs_hsm_action(int argc, char **argv);
111 static int lfs_hsm_archive(int argc, char **argv);
112 static int lfs_hsm_restore(int argc, char **argv);
113 static int lfs_hsm_release(int argc, char **argv);
114 static int lfs_hsm_remove(int argc, char **argv);
115 static int lfs_hsm_cancel(int argc, char **argv);
116 static int lfs_swap_layouts(int argc, char **argv);
117 static int lfs_mv(int argc, char **argv);
118 static int lfs_ladvise(int argc, char **argv);
119 static int lfs_getsom(int argc, char **argv);
120 static int lfs_heat_get(int argc, char **argv);
121 static int lfs_heat_set(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_read(int argc, char **argv);
128 static inline int lfs_mirror_write(int argc, char **argv);
129 static inline int lfs_mirror_copy(int argc, char **argv);
131 enum setstripe_origin {
139 static int lfs_setstripe_internal(int argc, char **argv,
140 enum setstripe_origin opc);
142 static inline int lfs_setstripe(int argc, char **argv)
144 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
146 static inline int lfs_setstripe_migrate(int argc, char **argv)
148 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
150 static inline int lfs_mirror_create(int argc, char **argv)
152 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
154 static inline int lfs_mirror_extend(int argc, char **argv)
156 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
158 static inline int lfs_mirror_split(int argc, char **argv)
160 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
163 /* Setstripe and migrate share mostly the same parameters */
164 #define SSM_CMD_COMMON(cmd) \
165 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
166 " [--stripe-count|-c <stripe_count>]\n" \
167 " [--overstripe-count|-C <stripe_count>]\n" \
168 " [--stripe-index|-i <start_ost_idx>]\n" \
169 " [--stripe-size|-S <stripe_size>]\n" \
170 " [--layout|-L <pattern>]\n" \
171 " [--pool|-p <pool_name>]\n" \
172 " [--ost|-o <ost_indices>]\n" \
173 " [--yaml|-y <yaml_template_file>]\n" \
174 " [--copy=<lustre_src>]\n"
176 #define SSM_HELP_COMMON \
177 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
178 "\t Using -C instead of -c allows overstriping, which\n" \
179 "\t will place more than one stripe per OST if\n" \
180 "\t stripe_count is greater than the number of OSTs\n" \
181 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
182 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
183 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
184 "\t respectively)\n" \
185 "\tpool_name: Name of OST pool to use (default none)\n" \
186 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
187 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
188 "\t Indices be specified in a format of:\n" \
189 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
191 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
192 "\t If --pool is set with --ost, then the OSTs\n" \
193 "\t must be the members of the pool.\n" \
194 "\tcomp_end: Extent end of component, start after previous end.\n"\
195 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
196 "\t respectively, -1 for EOF). Must be a multiple of\n"\
197 "\t stripe_size.\n" \
198 "\tyaml_template_file:\n" \
199 "\t YAML layout template file, can't be used with -c,\n" \
200 "\t -i, -S, -p, -o, or -E arguments.\n" \
201 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
202 "\t another lustre file or directory, can't used with\n" \
203 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
205 #define MIRROR_CREATE_HELP \
206 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
207 "\t setstripe layout options\n" \
208 "\t It defaults to 1 if not specified; if specified,\n" \
209 "\t it must follow the option without a space.\n" \
210 "\t The option can also be repeated multiple times to\n" \
211 "\t separate mirrors that have different layouts.\n" \
212 "\tsetstripe options: Mirror layout\n" \
213 "\t It can be a plain layout or a composite layout.\n" \
214 "\t If not specified, the stripe options inherited\n" \
215 "\t from the previous component will be used.\n" \
216 "\tflags: set flags to the component of the current mirror.\n" \
217 "\t Only \"prefer\" flag is supported so far.\n"
219 #define MIRROR_EXTEND_HELP \
221 "\tvictim_file: The layout of victim_file will be split and used\n" \
222 "\t as a mirror added to the mirrored file.\n" \
223 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
224 "\t from victim file(s) in case the victim file(s)\n" \
225 "\t contains the same data as the original mirrored\n" \
228 #define MIRROR_EXTEND_USAGE \
229 " <--mirror-count|-N[mirror_count]>\n" \
230 " [setstripe options|-f <victim_file>]\n" \
233 #define SETSTRIPE_USAGE \
234 SSM_CMD_COMMON("setstripe") \
235 MIRROR_EXTEND_USAGE \
236 " <directory|filename>\n" \
240 #define MIGRATE_USAGE \
241 SSM_CMD_COMMON("migrate ") \
242 " [--block|-b] [--non-block|-n]\n" \
243 " [--non-direct|-D] [--verbose|-v]\n" \
247 "\tblock: Block file access during data migration (default)\n" \
248 "\tnon-block: Abort migrations if concurrent access is detected\n" \
249 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
250 "\tverbose: Print each filename as it is migrated\n" \
252 #define SETDIRSTRIPE_USAGE \
253 " [--mdt-count|-c stripe_count>\n" \
254 " [--mdt-hash|-H mdt_hash]\n" \
255 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
256 " [--default|-D] [--mode|-o mode] <dir>\n" \
257 "\tstripe_count: stripe count of the striped directory\n" \
258 "\tmdt_index: MDT index of first stripe\n" \
259 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
260 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
261 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
262 " space create subdirectories with balanced space usage\n" \
263 "\tdefault_stripe: set default dirstripe of the directory\n" \
264 "\tmode: the file access permission of the directory (octal)\n" \
265 "To create dir with a foreign (free format) layout :\n" \
266 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
267 "[--mode|-m mode] [--flags <hex>] <dir>\n" \
268 "\tmode: the mode of the directory\n" \
269 "\tforeign_type: none or daos\n"
272 * command_t mirror_cmdlist - lfs mirror commands.
274 command_t mirror_cmdlist[] = {
275 { .pc_name = "create", .pc_func = lfs_mirror_create,
276 .pc_help = "Create a mirrored file.\n"
277 "usage: lfs mirror create "
278 "<--mirror-count|-N[mirror_count]> "
279 "[setstripe options] ... <filename|directory>\n"
280 MIRROR_CREATE_HELP },
281 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
282 .pc_help = "Extend a mirrored file.\n"
283 "usage: lfs mirror extend "
284 "<--mirror-count|-N[mirror_count]> [--no-verify] "
285 "[setstripe options|-f <victim_file>] ... <filename>\n"
286 MIRROR_EXTEND_HELP },
287 { .pc_name = "split", .pc_func = lfs_mirror_split,
288 .pc_help = "Split a mirrored file.\n"
289 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
290 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
291 "\t [-f <new_file>] <mirrored file>\n"
292 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
293 "\t can be fetched by lfs getstripe command.\n"
294 "\tcomp_id: Unique component ID within a mirror.\n"
295 "\tnew_file: This option indicates the layout of the split\n"
296 "\t mirror will be stored into. If not specified,\n"
297 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
298 "\t will be used.\n" },
299 { .pc_name = "read", .pc_func = lfs_mirror_read,
300 .pc_help = "Read the content of a specified mirror of a file.\n"
301 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
302 "[--outfile|-o <output_file>] <mirrored_file>\n" },
303 { .pc_name = "write", .pc_func = lfs_mirror_write,
304 .pc_help = "Write to a specified mirror of a file.\n"
305 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
306 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
307 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
308 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
309 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
310 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
311 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
312 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
313 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
314 "<mirrored file> [<mirrored file2>...]\n"},
315 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
316 .pc_help = "Verify mirrored file(s).\n"
317 "usage: lfs mirror verify "
318 "[--only <mirror_id,mirror_id2[,...]>] "
319 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
320 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
321 .pc_help = "list commands supported by lfs mirror"},
322 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
323 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
324 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
328 /* all available commands */
329 command_t cmdlist[] = {
330 {"setstripe", lfs_setstripe, 0,
331 "To create a file with specified striping/composite layout, or\n"
332 "create/replace the default layout on an existing directory:\n"
333 SSM_CMD_COMMON("setstripe")
335 " <directory|filename>\n"
337 "To add component(s) to an existing composite file:\n"
338 SSM_CMD_COMMON("setstripe --component-add")
340 "To totally delete the default striping from an existing directory:\n"
341 "usage: setstripe -d <directory>\n"
343 "To create a mirrored file or set s default mirror layout on a directory:\n"
344 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
346 "To delete the last component(s) from an existing composite file\n"
347 "(note that this will also delete any data in those components):\n"
348 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
349 " [--component-flags|-F <comp_flags>]\n"
351 "\tcomp_id: Unique component ID to delete\n"
352 "\tcomp_flags: 'init' indicating all instantiated components\n"
353 "\t '^init' indicating all uninstantiated components\n"
354 "\t-I and -F cannot be specified at the same time\n"
355 "To create a file with a foreign (free format) layout:\n"
356 "usage: setstripe --foreign[=<foreign_type>]\n"
357 " --xattr|-x <layout_string> [--flags <hex>]\n"
358 " [--mode <mode>] <filename>\n"},
359 {"getstripe", lfs_getstripe, 0,
360 "To list the layout pattern for a given file or files in a\n"
361 "directory or recursively for all files in a directory tree.\n"
362 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
363 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
364 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
365 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
366 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
367 " [--component-id[=comp_id]|-I[comp_id]]\n"
368 " [--component-flags[=comp_flags]]\n"
369 " [--component-count]\n"
370 " [--component-start[=[+-]comp_start]]\n"
371 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
372 " [[!] --mirror-index=[+-]<index> |\n"
373 " [!] --mirror-id=[+-]<id>]\n"
374 " <directory|filename> ..."},
375 {"setdirstripe", lfs_setdirstripe, 0,
376 "To create a striped directory on a specified MDT. This can only\n"
377 "be done on MDT0 with the right of administrator.\n"
378 "usage: setdirstripe [OPTION] <directory>\n"
380 {"getdirstripe", lfs_getdirstripe, 0,
381 "To list the layout pattern info for a given directory\n"
382 "or recursively for all directories in a directory tree.\n"
383 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
384 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
385 " [--recursive|-r] [--yaml|-y]\n"
386 " [--verbose|-v] [--default|-D] <dir> ..."},
387 {"mkdir", lfs_setdirstripe, 0,
388 "To create a striped directory on a specified MDT. This can only\n"
389 "be done on MDT0 with the right of administrator.\n"
390 "usage: mkdir [OPTION] <directory>\n"
392 {"rm_entry", lfs_rmentry, 0,
393 "To remove the name entry of the remote directory. Note: This\n"
394 "command will only delete the name entry, i.e. the remote directory\n"
395 "will become inaccessable after this command. This can only be done\n"
396 "by the administrator\n"
397 "usage: rm_entry <dir>\n"},
398 {"pool_list", lfs_poollist, 0,
399 "List pools or pool OSTs\n"
400 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
401 {"find", lfs_find, 0,
402 "find files matching given attributes recursively in directory tree.\n"
403 "usage: find <directory|filename> ...\n"
404 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
405 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
406 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
407 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
408 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
409 " [[!] --stripe-count|-c [+-]<stripes>]\n"
410 " [[!] --stripe-index|-i <index,...>]\n"
411 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
412 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
413 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
414 " [[!] --projid <projid>]\n"
415 " [[!] --foreign[=<foreign_type>]]\n"
416 " [[!] --layout|-L released,raid0,mdt]\n"
417 " [[!] --foreign[=<foreign_type>]]\n"
418 " [[!] --component-count [+-]<comp_cnt>]\n"
419 " [[!] --component-start [+-]N[kMGTPE]]\n"
420 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
421 " [[!] --component-flags <comp_flags>]\n"
422 " [[!] --mirror-count|-N [+-]<n>]\n"
423 " [[!] --mirror-state <[^]state>]\n"
424 " [[!] --mdt-count|-T [+-]<stripes>]\n"
425 " [[!] --mdt-hash|-H <hashtype>\n"
426 " [[!] --mdt-index|-m <uuid|index,...>]\n"
427 "\t !: used before an option indicates 'NOT' requested attribute\n"
428 "\t -: used before a value indicates less than requested value\n"
429 "\t +: used before a value indicates more than requested value\n"
430 "\thashtype: hash type of the striped directory.\n"
431 "\t fnv_1a_64 FNV-1a hash algorithm\n"
432 "\t all_char sum of characters % MDT_COUNT\n"},
433 {"check", lfs_check, 0,
434 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
435 "or all the servers (MGTs, MDTs and OSTs).\n"
436 "usage: check <mgts|osts|mdts|all>"},
437 {"osts", lfs_osts, 0, "list OSTs connected to client "
438 "[for specified path only]\n" "usage: osts [path]"},
439 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
440 "[for specified path only]\n" "usage: mdts [path]"},
442 "report filesystem disk space usage or inodes usage "
443 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
444 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
445 {"getname", lfs_getname, 0,
446 "list instances and specified mount points [for specified path only]\n"
447 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
448 #ifdef HAVE_SYS_QUOTA_H
449 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
450 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
451 " -b <block-softlimit> -B <block-hardlimit>\n"
452 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
453 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
454 " [--block-softlimit <block-softlimit>]\n"
455 " [--block-hardlimit <block-hardlimit>]\n"
456 " [--inode-softlimit <inode-softlimit>]\n"
457 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
458 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
459 " [--block-grace <block-grace>]\n"
460 " [--inode-grace <inode-grace>] <filesystem>\n"
461 " setquota <-U|-G|-P>\n"
462 " -b <block-softlimit> -B <block-hardlimit>\n"
463 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
464 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
465 " [--block-softlimit <block-softlimit>]\n"
466 " [--block-hardlimit <block-hardlimit>]\n"
467 " [--inode-softlimit <inode-softlimit>]\n"
468 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
469 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
471 " -b can be used instead of --block-softlimit/--block-grace\n"
472 " -B can be used instead of --block-hardlimit\n"
473 " -i can be used instead of --inode-softlimit/--inode-grace\n"
474 " -I can be used instead of --inode-hardlimit\n"
475 " -d can be used instead of --default\n\n"
476 "Note: The total quota space will be split into many qunits and\n"
477 " balanced over all server targets, the minimal qunit size is\n"
478 " 1M bytes for block space and 1K inodes for inode space.\n\n"
479 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
480 " Quota space rebalancing process will stop when this mininum\n"
481 " value is reached. As a result, quota exceeded can be returned\n"
482 " while many targets still have 1MB or 1K inodes of spare\n"
484 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
485 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
487 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
488 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
489 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
490 {"project", lfs_project, 0,
491 "Change or list project attribute for specified file or directory.\n"
492 "usage: project [-d|-r] <file|directory...>\n"
493 " list project ID and flags on file(s) or directories\n"
494 " project [-p id] [-s] [-r] <file|directory...>\n"
495 " set project ID and/or inherit flag for specified file(s) or directories\n"
496 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
497 " check project ID and flags on file(s) or directories, print outliers\n"
498 " project -C [-r] [-k] <file|directory...>\n"
499 " clear the project inherit flag and ID on the file or directory\n"
502 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
503 "usage: flushctx [-k] [mountpoint...]"},
504 {"changelog", lfs_changelog, 0,
505 "Show the metadata changes on an MDT."
506 "\nusage: changelog <mdtname> [startrec [endrec]]"},
507 {"changelog_clear", lfs_changelog_clear, 0,
508 "Indicate that old changelog records up to <endrec> are no longer of "
509 "interest to consumer <id>, allowing the system to free up space.\n"
510 "An <endrec> of 0 means all records.\n"
511 "usage: changelog_clear <mdtname> <id> <endrec>"},
512 {"fid2path", lfs_fid2path, 0,
513 "Resolve the full path(s) for given FID(s). For a specific hardlink "
514 "specify link number <linkno>.\n"
515 /* "For a historical link name, specify changelog record <recno>.\n" */
516 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
517 /* [ --rec <recno> ] */ },
518 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
519 "usage: path2fid [--parents] <path> ..."},
520 {"data_version", lfs_data_version, 0, "Display file data version for "
521 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
522 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
523 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
524 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
525 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
526 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
527 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
529 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
530 "[--archived] [--lost] <file> ..."},
531 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
532 "given files.\n" "usage: hsm_action <file> ..."},
533 {"hsm_archive", lfs_hsm_archive, 0,
534 "Archive file to external storage.\n"
535 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
537 {"hsm_restore", lfs_hsm_restore, 0,
538 "Restore file from external storage.\n"
539 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
540 {"hsm_release", lfs_hsm_release, 0,
541 "Release files from Lustre.\n"
542 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
543 {"hsm_remove", lfs_hsm_remove, 0,
544 "Remove file copy from external storage.\n"
545 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
547 " (FILE [FILE ...] | "
548 "--mntpath MOUNTPATH FID [FID ...])\n"
550 "Note: To remove an archived copy of a file already deleted from a "
552 "--mntpath option and a list of FIDs must be specified"
554 {"hsm_cancel", lfs_hsm_cancel, 0,
555 "Cancel requests related to specified files.\n"
556 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
557 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
558 "usage: swap_layouts <path1> <path2>"},
559 {"migrate", lfs_setstripe_migrate, 0,
560 "migrate a directory between MDTs.\n"
561 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
562 " [--mdt-hash|-H] <hash_type>\n"
563 " [--mdt-index|-m] <start_mdt_index>\n"
566 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
567 " it's the MDT index of first stripe\n"
568 "\tmdt_count: number of MDTs to stripe a directory over\n"
569 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
570 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
571 " all_char sum of characters % MDT_COUNT\n"
573 "migrate file objects from one OST "
574 "layout\nto another (may be not safe with concurent writes).\n"
576 "[--stripe-count|-c] <stripe_count>\n"
577 "[--overstripe-count|-C] <stripe_count>\n"
578 " [--stripe-index|-i] <start_ost_index>\n"
579 " [--stripe-size|-S] <stripe_size>\n"
580 " [--pool|-p] <pool_name>\n"
581 " [--ost|-o] <ost_indices>\n"
583 " [--non-block|-n]\n"
584 " [--non-direct|-D]\n"
585 " <file|directory>\n"
586 "\tstripe_count: number of OSTs to stripe a file over\n"
587 "\t Using -C instead of -c allows overstriping, which\n"
588 "\t will place more than one stripe per OST if\n"
589 "\t stripe_count is greater than the number of OSTs\n"
590 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
591 "\tstripe_size: number of bytes to store before moving to the next OST\n"
592 "\tpool_name: name of the predefined pool of OSTs\n"
593 "\tost_indices: OSTs to stripe over, in order\n"
594 "\tblock: Block file access during data migration (default)\n"
595 "\tnon-block: Abort migrations if concurrent access is detected\n"
596 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
598 "To move directories between MDTs. This command is deprecated, "
599 "use \"migrate\" instead.\n"
600 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
602 {"ladvise", lfs_ladvise, 0,
603 "Provide servers with advice about access patterns for a file.\n"
604 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
605 " [--background|-b] [--unset|-u]\n\n"
606 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
607 " {[--mode|-m [READ,WRITE]}\n"
609 {"mirror", lfs_mirror, mirror_cmdlist,
610 "lfs commands used to manage files with mirrored components:\n"
611 "lfs mirror create - create a mirrored file or directory\n"
612 "lfs mirror extend - add mirror(s) to an existing file\n"
613 "lfs mirror split - split a mirror from an existing mirrored file\n"
614 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
615 "lfs mirror read - read a mirror content of a mirrored file\n"
616 "lfs mirror write - write to a mirror of a mirrored file\n"
617 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
618 "lfs mirror verify - verify mirrored file(s)\n"},
619 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
620 "usage: getsom [-s] [-b] [-f] <path>\n"
621 "\t-s: Only show the size value of the SOM data for a given file\n"
622 "\t-b: Only show the blocks value of the SOM data for a given file\n"
623 "\t-f: Only show the flags value of the SOM data for a given file\n"},
624 {"heat_get", lfs_heat_get, 0,
625 "To get heat of files.\n"
626 "usage: heat_get <file> ...\n"},
627 {"heat_set", lfs_heat_set, 0,
628 "To set heat flags of files.\n"
629 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
630 "\t--clear|-c: Clear file heat for given files\n"
631 "\t--off|-o: Turn off file heat for given files\n"
632 "\t--on|-O: Turn on file heat for given files\n"},
633 {"help", Parser_help, 0, "help"},
634 {"exit", Parser_quit, 0, "quit"},
635 {"quit", Parser_quit, 0, "quit"},
636 {"--version", Parser_version, 0,
637 "output build version of the utility and exit"},
638 {"--list-commands", lfs_list_commands, 0,
639 "list commands supported by the utility and exit"},
644 static int check_hashtype(const char *hashtype)
648 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
649 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
655 static uint32_t check_foreign_type_name(const char *foreign_type_name)
659 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
660 if (lu_foreign_types[i].lft_name == NULL)
662 if (strcmp(foreign_type_name,
663 lu_foreign_types[i].lft_name) == 0)
664 return lu_foreign_types[i].lft_type;
667 return LU_FOREIGN_TYPE_UNKNOWN;
670 static const char *error_loc = "syserror";
673 MIGRATION_NONBLOCK = 0x0001,
674 MIGRATION_MIRROR = 0x0002,
675 MIGRATION_NONDIRECT = 0x0004,
676 MIGRATION_VERBOSE = 0x0008,
679 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
680 struct llapi_layout *layout);
683 migrate_open_files(const char *name, __u64 migration_flags,
684 const struct llapi_stripe_param *param,
685 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
692 char parent[PATH_MAX];
693 char volatile_file[PATH_MAX];
699 if (param == NULL && layout == NULL) {
700 error_loc = "layout information";
704 /* search for file directory pathname */
705 if (strlen(name) > sizeof(parent) - 1) {
706 error_loc = "source file name";
710 strncpy(parent, name, sizeof(parent));
711 ptr = strrchr(parent, '/');
713 if (getcwd(parent, sizeof(parent)) == NULL) {
714 error_loc = "getcwd";
718 if (ptr == parent) /* leading '/' */
723 /* open file, direct io */
724 /* even if the file is only read, WR mode is nedeed to allow
725 * layout swap on fd */
727 if (!(migration_flags & MIGRATION_NONDIRECT))
729 fd = open(name, rflags);
732 error_loc = "cannot open source file";
736 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
738 error_loc = "cannot get MDT index";
743 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
744 mode_t open_mode = S_IRUSR | S_IWUSR;
746 random_value = random();
747 rc = snprintf(volatile_file, sizeof(volatile_file),
748 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
749 mdt_index, random_value);
750 if (rc >= sizeof(volatile_file)) {
755 /* create, open a volatile file, use caching (ie no directio) */
757 fdv = llapi_file_open_param(volatile_file, open_flags,
760 fdv = lfs_component_create(volatile_file, open_flags,
762 } while (fdv < 0 && (rc = fdv) == -EEXIST);
765 error_loc = "cannot create volatile file";
769 /* In case the MDT does not support creation of volatile files
770 * we should try to unlink it. */
771 (void)unlink(volatile_file);
773 /* Not-owner (root?) special case.
774 * Need to set owner/group of volatile file like original.
775 * This will allow to pass related check during layout_swap.
780 error_loc = "cannot stat source file";
784 rc = fstat(fdv, &stv);
787 error_loc = "cannot stat volatile";
791 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
792 rc = fchown(fdv, st.st_uid, st.st_gid);
795 error_loc = "cannot change ownwership of volatile";
814 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
816 struct llapi_layout *layout;
817 size_t buf_size = 4 * 1024 * 1024;
826 layout = llapi_layout_get_by_fd(fd_src, 0);
827 if (layout != NULL) {
828 uint64_t stripe_size;
830 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
832 buf_size = stripe_size;
834 llapi_layout_free(layout);
837 /* Use a page-aligned buffer for direct I/O */
838 rc = posix_memalign(&buf, getpagesize(), buf_size);
843 /* read new data only if we have written all
844 * previously read data */
847 rc = check_file(fd_src);
852 rsize = read(fd_src, buf, buf_size);
864 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
883 static int migrate_copy_timestamps(int fd, int fdv)
887 if (fstat(fd, &st) == 0) {
888 struct timeval tv[2] = {
889 {.tv_sec = st.st_atime},
890 {.tv_sec = st.st_mtime}
893 return futimes(fdv, tv);
899 static int migrate_block(int fd, int fdv)
906 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
908 error_loc = "cannot get dataversion";
916 /* The grouplock blocks all concurrent accesses to the file.
917 * It has to be taken after llapi_get_data_version as it would
919 rc = llapi_group_lock(fd, gid);
921 error_loc = "cannot get group lock";
925 rc = migrate_copy_data(fd, fdv, NULL);
927 error_loc = "data copy failed";
931 /* Make sure we keep original atime/mtime values */
932 rc = migrate_copy_timestamps(fd, fdv);
934 error_loc = "timestamp copy failed";
939 * for a migration we need to check data version on file did
942 * Pass in gid=0 since we already own grouplock. */
943 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
944 SWAP_LAYOUTS_CHECK_DV1);
946 error_loc = "file changed";
949 error_loc = "cannot swap layout";
954 rc2 = llapi_group_unlock(fd, gid);
955 if (rc2 < 0 && rc == 0) {
956 error_loc = "unlock group lock";
964 * Internal helper for migrate_copy_data(). Check lease and report error if
967 * \param[in] fd File descriptor on which to check the lease.
969 * \retval 0 Migration can keep on going.
970 * \retval -errno Error occurred, abort migration.
972 static int check_lease(int fd)
976 rc = llapi_lease_check(fd);
978 return 0; /* llapi_check_lease returns > 0 on success. */
983 static int migrate_nonblock(int fd, int fdv)
989 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
991 error_loc = "cannot get data version";
995 rc = migrate_copy_data(fd, fdv, check_lease);
997 error_loc = "data copy failed";
1001 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1003 error_loc = "cannot get data version";
1009 error_loc = "source file changed";
1013 /* Make sure we keep original atime/mtime values */
1014 rc = migrate_copy_timestamps(fd, fdv);
1016 error_loc = "timestamp copy failed";
1023 static int lfs_component_set(char *fname, int comp_id,
1024 __u32 flags, __u32 neg_flags)
1027 __u32 flags_array[2];
1032 ids[count] = comp_id;
1033 flags_array[count] = flags;
1038 ids[count] = comp_id;
1039 flags_array[count] = neg_flags | LCME_FL_NEG;
1043 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1046 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1047 progname, comp_id, fname, flags, neg_flags);
1052 static int lfs_component_del(char *fname, __u32 comp_id,
1053 __u32 flags, __u32 neg_flags)
1057 if (flags && neg_flags)
1060 if (!flags && neg_flags)
1061 flags = neg_flags | LCME_FL_NEG;
1063 if ((flags && comp_id) || (!flags && !comp_id))
1066 /* LCME_FL_INIT is the only supported flag in PFL */
1068 if (flags & ~LCME_KNOWN_FLAGS) {
1070 "%s setstripe: unknown flags %#x\n",
1074 } else if (comp_id > LCME_ID_MAX) {
1075 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1080 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1083 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1084 progname, comp_id, fname, strerror(errno));
1088 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1095 rc = llapi_layout_file_comp_add(fname, layout);
1097 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1098 fname, strerror(errno));
1102 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1103 struct llapi_layout *layout)
1111 fd = lstat(fname, &st);
1112 if (fd == 0 && S_ISDIR(st.st_mode))
1113 open_flags = O_DIRECTORY | O_RDONLY;
1115 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1117 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1118 S_ISDIR(st.st_mode) ?
1119 "set default composite layout for" :
1120 "create composite file",
1121 fname, strerror(errno));
1125 static int lfs_migrate(char *name, __u64 migration_flags,
1126 struct llapi_stripe_param *param,
1127 struct llapi_layout *layout)
1133 rc = migrate_open_files(name, migration_flags, param, layout,
1138 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1139 /* Blocking mode (forced if servers do not support file lease).
1140 * It is also the default mode, since we cannot distinguish
1141 * between a broken lease and a server that does not support
1142 * atomic swap/close (LU-6785) */
1143 rc = migrate_block(fd, fdv);
1147 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1149 error_loc = "cannot get lease";
1153 rc = migrate_nonblock(fd, fdv);
1155 llapi_lease_release(fd);
1159 /* Atomically put lease, swap layouts and close.
1160 * for a migration we need to check data version on file did
1162 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1164 error_loc = "cannot swap layout";
1176 fprintf(stderr, "error: %s: %s: %s: %s\n",
1177 progname, name, error_loc, strerror(-rc));
1178 else if (migration_flags & MIGRATION_VERBOSE)
1179 printf("%s\n", name);
1184 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1193 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1197 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1198 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1199 const char *comp_name = comp_flags_table[i].cfn_name;
1201 if (strcmp(name, comp_name) == 0) {
1202 *flags |= comp_flag;
1204 } else if (strncmp(name, "^", 1) == 0 &&
1205 strcmp(name + 1, comp_name) == 0) {
1206 *neg_flags |= comp_flag;
1211 llapi_printf(LLAPI_MSG_ERROR,
1212 "%s: component flag '%s' not supported\n",
1218 if (!*flags && !*neg_flags)
1221 /* don't allow to set and exclude the same flag */
1222 if (*flags & *neg_flags)
1228 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1236 if (strncmp(string, "^", 1) == 0) {
1237 *neg_state = llapi_layout_string_flags(string + 1);
1238 if (*neg_state != 0)
1241 *state = llapi_layout_string_flags(string);
1246 llapi_printf(LLAPI_MSG_ERROR,
1247 "%s: mirrored file state '%s' not supported\n",
1253 * struct mirror_args - Command-line arguments for mirror(s).
1254 * @m_count: Number of mirrors to be created with this layout.
1255 * @m_flags: Mirror level flags, only 'prefer' is supported.
1256 * @m_layout: Mirror layout.
1257 * @m_file: A victim file. Its layout will be split and used as a mirror.
1258 * @m_next: Point to the next node of the list.
1260 * Command-line arguments for mirror(s) will be parsed and stored in
1261 * a linked list that consists of this structure.
1263 struct mirror_args {
1266 struct llapi_layout *m_layout;
1268 struct mirror_args *m_next;
1271 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1276 rc = llapi_layout_comp_flags_get(layout, &flags);
1280 if (flags & LCME_FL_NEG) {
1281 fprintf(stderr, "error: %s: negative flags are not supported\n",
1286 if (flags & LCME_FL_STALE) {
1287 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1288 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1292 return LLAPI_LAYOUT_ITER_CONT;
1295 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1297 uint64_t start, end;
1301 /* LU-10112: do not support dom+flr in phase 1 */
1302 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1306 rc = llapi_layout_pattern_get(layout, &pattern);
1310 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1311 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1316 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1320 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1324 if (end != LUSTRE_EOF) {
1325 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1330 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1335 * enum mirror_flags - Flags for extending a mirrored file.
1336 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1337 * in case the victim file(s) contains the same data as the
1338 * original mirrored file.
1339 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1340 * @MF_COMP_ID: specified component id instead of mirror id
1342 * Flags for extending a mirrored file.
1351 * mirror_create_sanity_check() - Check mirror list.
1352 * @list: A linked list that stores the mirror arguments.
1354 * This function does a sanity check on @list for creating
1357 * Return: 0 on success or a negative error code on failure.
1359 static int mirror_create_sanity_check(const char *fname,
1360 struct mirror_args *list)
1363 bool has_m_file = false;
1364 bool has_m_layout = false;
1370 struct llapi_layout *layout;
1372 layout = llapi_layout_get_by_path(fname, 0);
1375 "error: %s: file '%s' couldn't get layout\n",
1380 rc = mirror_sanity_check_one(layout);
1381 llapi_layout_free(layout);
1387 while (list != NULL) {
1388 if (list->m_file != NULL) {
1390 llapi_layout_free(list->m_layout);
1393 llapi_layout_get_by_path(list->m_file, 0);
1394 if (list->m_layout == NULL) {
1396 "error: %s: file '%s' has no layout\n",
1397 progname, list->m_file);
1401 has_m_layout = true;
1402 if (list->m_layout == NULL) {
1403 fprintf(stderr, "error: %s: no mirror layout\n",
1409 rc = mirror_sanity_check_one(list->m_layout);
1413 list = list->m_next;
1416 if (has_m_file && has_m_layout) {
1418 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1426 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1428 __u32 mirror_flags = *(__u32 *)cbdata;
1432 rc = llapi_layout_comp_flags_get(layout, &flags);
1437 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1442 return LLAPI_LAYOUT_ITER_CONT;
1446 * mirror_create() - Create a mirrored file.
1447 * @fname: The file to be created.
1448 * @mirror_list: A linked list that stores the mirror arguments.
1450 * This function creates a mirrored file @fname with the mirror(s)
1451 * from @mirror_list.
1453 * Return: 0 on success or a negative error code on failure.
1455 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1457 struct llapi_layout *layout = NULL;
1458 struct mirror_args *cur_mirror = NULL;
1459 uint16_t mirror_count = 0;
1463 rc = mirror_create_sanity_check(NULL, mirror_list);
1467 cur_mirror = mirror_list;
1468 while (cur_mirror != NULL) {
1469 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1471 &cur_mirror->m_flags);
1474 fprintf(stderr, "%s: failed to set mirror flags\n",
1479 for (i = 0; i < cur_mirror->m_count; i++) {
1480 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1483 fprintf(stderr, "error: %s: "
1484 "merge layout failed: %s\n",
1485 progname, strerror(errno));
1489 mirror_count += cur_mirror->m_count;
1490 cur_mirror = cur_mirror->m_next;
1493 if (layout == NULL) {
1494 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1498 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1501 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1502 progname, strerror(errno));
1506 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1514 llapi_layout_free(layout);
1519 * Compare files and check lease on @fd.
1521 * \retval bytes number of bytes are the same
1523 static ssize_t mirror_file_compare(int fd, int fdv)
1525 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1527 ssize_t bytes_done = 0;
1528 ssize_t bytes_read = 0;
1530 buf = malloc(buflen * 2);
1535 if (!llapi_lease_check(fd)) {
1536 bytes_done = -EBUSY;
1540 bytes_read = read(fd, buf, buflen);
1541 if (bytes_read <= 0)
1544 if (bytes_read != read(fdv, buf + buflen, buflen))
1547 /* XXX: should compute the checksum on each buffer and then
1548 * compare checksum to avoid cache collision */
1549 if (memcmp(buf, buf + buflen, bytes_read))
1552 bytes_done += bytes_read;
1560 static int mirror_extend_file(const char *fname, const char *victim_file,
1561 enum mirror_flags mirror_flags)
1566 struct stat stbuf_v;
1567 struct ll_ioc_lease *data = NULL;
1570 fd = open(fname, O_RDWR);
1572 error_loc = "open source file";
1577 fdv = open(victim_file, O_RDWR);
1579 error_loc = "open target file";
1584 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1585 error_loc = "stat source or target file";
1590 if (stbuf.st_dev != stbuf_v.st_dev) {
1591 error_loc = "stat source and target file";
1596 /* mirrors should be of the same size */
1597 if (stbuf.st_size != stbuf_v.st_size) {
1598 error_loc = "file sizes don't match";
1603 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1605 error_loc = "cannot get lease";
1609 if (!(mirror_flags & MF_NO_VERIFY)) {
1611 /* mirrors should have the same contents */
1612 ret = mirror_file_compare(fd, fdv);
1613 if (ret != stbuf.st_size) {
1614 error_loc = "file busy or contents don't match";
1615 rc = ret < 0 ? ret : -EINVAL;
1620 /* Get rid of caching pages from clients */
1621 rc = llapi_file_flush(fd);
1623 error_loc = "cannot get data version";
1627 rc = llapi_file_flush(fdv);
1629 error_loc = "cannot get data version";
1634 /* Make sure we keep original atime/mtime values */
1635 rc = migrate_copy_timestamps(fd, fdv);
1637 error_loc = "cannot copy timestamp";
1641 /* Atomically put lease, merge layouts and close. */
1642 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1644 error_loc = "memory allocation";
1647 data->lil_mode = LL_LEASE_UNLCK;
1648 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1649 data->lil_count = 1;
1650 data->lil_ids[0] = fdv;
1651 rc = llapi_lease_set(fd, data);
1653 error_loc = "cannot merge layout";
1655 } else if (rc == 0) {
1657 error_loc = "lost lease lock";
1670 (void) unlink(victim_file);
1672 fprintf(stderr, "error: %s: %s: %s: %s\n",
1673 progname, fname, error_loc, strerror(-rc));
1677 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1679 struct ll_ioc_lease *data = NULL;
1684 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1688 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1690 error_loc = "cannot get lease";
1694 rc = migrate_nonblock(fd, fdv);
1696 llapi_lease_release(fd);
1700 /* Atomically put lease, merge layouts and close. */
1701 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1703 error_loc = "memory allocation";
1706 data->lil_mode = LL_LEASE_UNLCK;
1707 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1708 data->lil_count = 1;
1709 data->lil_ids[0] = fdv;
1710 rc = llapi_lease_set(fd, data);
1712 error_loc = "cannot merge layout";
1714 } else if (rc == 0) {
1716 error_loc = "lost lease lock";
1729 fprintf(stderr, "error: %s: %s: %s: %s\n",
1730 progname, name, error_loc, strerror(-rc));
1734 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1735 enum mirror_flags mirror_flags)
1739 rc = mirror_create_sanity_check(fname, mirror_list);
1743 while (mirror_list) {
1744 if (mirror_list->m_file != NULL) {
1745 rc = mirror_extend_file(fname, mirror_list->m_file,
1748 __u32 mirror_count = mirror_list->m_count;
1750 while (mirror_count > 0) {
1751 rc = mirror_extend_layout(fname,
1752 mirror_list->m_layout);
1762 mirror_list = mirror_list->m_next;
1768 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1773 rc = llapi_layout_mirror_id_get(layout, &id);
1777 if ((__u16)id == *(__u16 *)cbdata)
1778 return LLAPI_LAYOUT_ITER_STOP;
1780 return LLAPI_LAYOUT_ITER_CONT;
1783 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1788 rc = llapi_layout_comp_id_get(layout, &id);
1792 if (id == *(__u32 *)cbdata)
1793 return LLAPI_LAYOUT_ITER_STOP;
1795 return LLAPI_LAYOUT_ITER_CONT;
1797 static int mirror_split(const char *fname, __u32 id,
1798 enum mirror_flags mflags, const char *victim_file)
1800 struct llapi_layout *layout;
1801 char parent[PATH_MAX];
1802 char victim[PATH_MAX];
1803 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1805 struct ll_ioc_lease *data;
1806 uint16_t mirror_count;
1811 /* check fname contains mirror with mirror_id/comp_id */
1812 layout = llapi_layout_get_by_path(fname, 0);
1815 "error %s: file '%s' couldn't get layout\n",
1820 rc = mirror_sanity_check_one(layout);
1824 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1827 "error %s: file '%s' couldn't get mirror count\n",
1831 if (mirror_count < 2) {
1833 "error %s: file '%s' has %d component, cannot split\n",
1834 progname, fname, mirror_count);
1838 if (mflags & MF_COMP_ID) {
1839 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1840 id = mirror_id_of(id);
1842 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1845 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1848 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1850 "error %s: file '%s' does not contain mirror with id %u\n",
1851 progname, fname, id);
1855 fd = open(fname, O_RDWR);
1858 "error %s: open file '%s' failed: %s\n",
1859 progname, fname, strerror(errno));
1863 /* get victim file directory pathname */
1864 if (strlen(fname) > sizeof(parent) - 1) {
1865 fprintf(stderr, "error %s: file name of '%s' too long\n",
1870 strncpy(parent, fname, sizeof(parent));
1871 ptr = strrchr(parent, '/');
1873 if (getcwd(parent, sizeof(parent)) == NULL) {
1874 fprintf(stderr, "error %s: getcwd failed: %s\n",
1875 progname, strerror(errno));
1885 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1887 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1892 if (victim_file == NULL) {
1893 /* use a temp file to store the splitted layout */
1894 if (mflags & MF_DESTROY) {
1895 fdv = llapi_create_volatile_idx(parent, mdt_index,
1896 O_LOV_DELAY_CREATE);
1898 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1900 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1903 /* user specified victim file */
1904 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1909 "error %s: create victim file failed: %s\n",
1910 progname, strerror(errno));
1914 /* get lease lock of fname */
1915 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1918 "error %s: cannot get lease of file '%s': %d\n",
1919 progname, fname, rc);
1923 /* Atomatically put lease, split layouts and close. */
1924 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1930 data->lil_mode = LL_LEASE_UNLCK;
1931 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1932 data->lil_count = 2;
1933 data->lil_ids[0] = fdv;
1934 data->lil_ids[1] = id;
1935 rc = llapi_lease_set(fd, data);
1937 if (rc == 0) /* lost lease lock */
1940 "error %s: cannot split '%s': %s\n",
1941 progname, fname, strerror(-rc));
1952 llapi_layout_free(layout);
1957 * Parse a string containing an target index list into an array of integers.
1959 * The input string contains a comma delimited list of individual
1960 * indices and ranges, for example "1,2-4,7". Add the indices into the
1961 * \a tgts array and remove duplicates.
1963 * \param[out] tgts array to store indices in
1964 * \param[in] size size of \a tgts array
1965 * \param[in] offset starting index in \a tgts
1966 * \param[in] arg string containing OST index list
1967 * \param[in/out] overstriping index list may contain duplicates
1969 * \retval positive number of indices in \a tgts
1970 * \retval -EINVAL unable to parse \a arg
1972 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
1973 unsigned long long *pattern)
1977 int slots = size - offset;
1979 bool overstriped = false;
1985 end_of_loop = false;
1986 while (!end_of_loop) {
1987 int start_index = 0;
1990 char *endptr = NULL;
1994 ptr = strchrnul(arg, ',');
1996 end_of_loop = *ptr == '\0';
1999 start_index = strtol(arg, &endptr, 0);
2000 if (endptr == arg) /* no data at all */
2002 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2005 end_index = start_index;
2006 if (*endptr == '-') {
2007 end_index = strtol(endptr + 1, &endptr, 0);
2008 if (*endptr != '\0')
2010 if (end_index < start_index)
2014 for (i = start_index; i <= end_index && slots > 0; i++) {
2017 /* remove duplicate */
2018 for (j = 0; j < offset; j++) {
2019 if (tgts[j] == i && pattern &&
2020 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2022 else if (tgts[j] == i)
2028 if (j == offset) { /* check complete */
2034 if (slots == 0 && i < end_index)
2042 if (!end_of_loop && ptr != NULL)
2045 if (!overstriped && pattern)
2046 *pattern = LLAPI_LAYOUT_DEFAULT;
2048 return rc < 0 ? rc : nr;
2051 struct lfs_setstripe_args {
2052 unsigned long long lsa_comp_end;
2053 unsigned long long lsa_stripe_size;
2054 long long lsa_stripe_count;
2055 long long lsa_stripe_off;
2056 __u32 lsa_comp_flags;
2057 __u32 lsa_comp_neg_flags;
2058 unsigned long long lsa_pattern;
2059 unsigned int lsa_mirror_count;
2061 bool lsa_first_comp;
2063 char *lsa_pool_name;
2066 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2068 unsigned int mirror_count = lsa->lsa_mirror_count;
2069 bool first_comp = lsa->lsa_first_comp;
2071 memset(lsa, 0, sizeof(*lsa));
2073 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2074 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2075 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2076 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2077 lsa->lsa_pool_name = NULL;
2079 lsa->lsa_mirror_count = mirror_count;
2080 lsa->lsa_first_comp = first_comp;
2084 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2085 * @lsa: Stripe options to be initialized and inherited.
2087 * This function initializes stripe options in @lsa and inherit
2088 * stripe_size, stripe_count and OST pool_name options.
2092 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2094 unsigned long long stripe_size;
2095 long long stripe_count;
2096 char *pool_name = NULL;
2098 stripe_size = lsa->lsa_stripe_size;
2099 stripe_count = lsa->lsa_stripe_count;
2100 pool_name = lsa->lsa_pool_name;
2102 setstripe_args_init(lsa);
2104 lsa->lsa_stripe_size = stripe_size;
2105 lsa->lsa_stripe_count = stripe_count;
2106 lsa->lsa_pool_name = pool_name;
2109 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2111 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2112 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2113 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2114 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2115 lsa->lsa_pool_name != NULL ||
2116 lsa->lsa_comp_end != 0);
2120 * comp_args_to_layout() - Create or extend a composite layout.
2121 * @composite: Pointer to the composite layout.
2122 * @lsa: Stripe options for the new component.
2124 * This function creates or extends a composite layout by adding a new
2125 * component with stripe options from @lsa.
2127 * Return: 0 on success or an error code on failure.
2129 static int comp_args_to_layout(struct llapi_layout **composite,
2130 struct lfs_setstripe_args *lsa,
2133 struct llapi_layout *layout = *composite;
2134 uint64_t prev_end = 0;
2137 if (layout == NULL) {
2138 layout = llapi_layout_alloc();
2139 if (layout == NULL) {
2140 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2144 *composite = layout;
2148 /* Get current component extent, current component
2149 * must be the tail component. */
2150 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2152 fprintf(stderr, "Get comp extent failed. %s\n",
2157 if (lsa->lsa_first_comp)
2160 if (lsa->lsa_first_comp)
2161 rc = llapi_layout_add_first_comp(layout);
2163 rc = llapi_layout_comp_add(layout);
2165 fprintf(stderr, "Add component failed. %s\n",
2170 /* reset lsa_first_comp */
2171 lsa->lsa_first_comp = false;
2174 rc = llapi_layout_comp_extent_set(layout, prev_end,
2177 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2178 prev_end, lsa->lsa_comp_end, strerror(errno));
2183 /* Data-on-MDT component setting */
2184 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2185 /* In case of Data-on-MDT patterns the only extra option
2186 * applicable is stripe size option. */
2187 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2188 fprintf(stderr, "Option 'stripe-count' can't be "
2189 "specified with Data-on-MDT component: %lld\n",
2190 lsa->lsa_stripe_count);
2193 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2194 fprintf(stderr, "Option 'stripe-size' can't be "
2195 "specified with Data-on-MDT component: %llu\n",
2196 lsa->lsa_stripe_size);
2199 if (lsa->lsa_nr_tgts != 0) {
2200 fprintf(stderr, "Option 'ost-list' can't be specified "
2201 "with Data-on-MDT component: '%i'\n",
2205 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2206 fprintf(stderr, "Option 'stripe-offset' can't be "
2207 "specified with Data-on-MDT component: %lld\n",
2208 lsa->lsa_stripe_off);
2211 if (lsa->lsa_pool_name != 0) {
2212 fprintf(stderr, "Option 'pool' can't be specified "
2213 "with Data-on-MDT component: '%s'\n",
2214 lsa->lsa_pool_name);
2218 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2220 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2221 lsa->lsa_pattern, strerror(errno));
2224 /* Data-on-MDT component has always single stripe up to end */
2225 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2226 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2227 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2229 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2230 lsa->lsa_pattern, strerror(errno));
2235 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2237 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2238 lsa->lsa_stripe_size, strerror(errno));
2242 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2244 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2245 lsa->lsa_stripe_count, strerror(errno));
2249 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2251 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2252 lsa->lsa_comp_flags, strerror(errno));
2256 if (lsa->lsa_pool_name != NULL) {
2257 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2259 fprintf(stderr, "Set pool name: %s failed. %s\n",
2260 lsa->lsa_pool_name, strerror(errno));
2264 rc = llapi_layout_pool_name_set(layout, "");
2266 fprintf(stderr, "Clear pool name failed: %s\n",
2272 if (lsa->lsa_nr_tgts > 0) {
2273 if (lsa->lsa_stripe_count > 0 &&
2274 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2275 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2276 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2277 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2278 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2281 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2282 rc = llapi_layout_ost_index_set(layout, i,
2287 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2288 lsa->lsa_stripe_off != -1) {
2289 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2292 fprintf(stderr, "Set ost index %d failed. %s\n",
2293 i, strerror(errno));
2300 static int build_component(struct llapi_layout **layout,
2301 struct lfs_setstripe_args *lsa, bool set_extent)
2305 rc = comp_args_to_layout(layout, lsa, set_extent);
2309 if (lsa->lsa_mirror_count > 0) {
2310 rc = llapi_layout_mirror_count_set(*layout,
2311 lsa->lsa_mirror_count);
2315 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2318 lsa->lsa_mirror_count = 0;
2324 static int build_layout_from_yaml_node(struct cYAML *node,
2325 struct llapi_layout **layout,
2326 struct lfs_setstripe_args *lsa,
2333 if (node->cy_type == CYAML_TYPE_OBJECT) {
2334 /* go deep to sub blocks */
2335 rc = build_layout_from_yaml_node(node->cy_child, layout,
2340 if (node->cy_string == NULL)
2343 string = node->cy_string;
2344 /* skip leading lmm_ if present, to simplify parsing */
2345 if (strncmp(string, "lmm_", 4) == 0)
2348 if (node->cy_type == CYAML_TYPE_STRING) {
2349 if (!strcmp(string, "lcme_extent.e_end")) {
2350 if (!strcmp(node->cy_valuestring, "EOF") ||
2351 !strcmp(node->cy_valuestring, "eof"))
2352 lsa->lsa_comp_end = LUSTRE_EOF;
2353 } else if (!strcmp(string, "pool")) {
2354 lsa->lsa_pool_name = node->cy_valuestring;
2355 } else if (!strcmp(string, "pattern")) {
2356 if (!strcmp(node->cy_valuestring, "mdt"))
2357 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2358 if (!strcmp(node->cy_valuestring,
2359 "raid0,overstriped"))
2361 LLAPI_LAYOUT_OVERSTRIPING;
2362 } else if (!strcmp(string, "lcme_flags")) {
2363 rc = comp_str2flags(node->cy_valuestring,
2364 &lsa->lsa_comp_flags,
2365 &lsa->lsa_comp_neg_flags);
2368 /* Only template flags have meaning in
2369 * the layout for a new file
2371 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2373 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2374 if (!strcmp(string, "lcm_mirror_count")) {
2375 lsa->lsa_mirror_count = node->cy_valueint;
2376 } else if (!strcmp(string, "lcme_extent.e_start")) {
2377 if (node->cy_valueint != 0 || *layout != NULL) {
2378 rc = build_component(layout, lsa, true);
2383 if (node->cy_valueint == 0)
2384 lsa->lsa_first_comp = true;
2386 /* initialize lsa */
2387 setstripe_args_init(lsa);
2388 lsa->lsa_tgts = osts;
2389 } else if (!strcmp(string, "lcme_extent.e_end")) {
2390 if (node->cy_valueint == -1)
2391 lsa->lsa_comp_end = LUSTRE_EOF;
2393 lsa->lsa_comp_end = node->cy_valueint;
2394 } else if (!strcmp(string, "stripe_count")) {
2395 lsa->lsa_stripe_count = node->cy_valueint;
2396 } else if (!strcmp(string, "stripe_size")) {
2397 lsa->lsa_stripe_size = node->cy_valueint;
2398 } else if (!strcmp(string, "stripe_offset")) {
2399 lsa->lsa_stripe_off = node->cy_valueint;
2400 } else if (!strcmp(string, "l_ost_idx")) {
2401 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2406 node = node->cy_next;
2412 static int lfs_comp_create_from_yaml(char *template,
2413 struct llapi_layout **layout,
2414 struct lfs_setstripe_args *lsa,
2417 struct cYAML *tree = NULL, *err_rc = NULL;
2420 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2422 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2423 progname, template);
2424 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2425 "can't parse", &err_rc);
2426 cYAML_print_tree2file(stderr, err_rc);
2427 cYAML_free_tree(err_rc);
2432 /* initialize lsa for plain file */
2433 setstripe_args_init(lsa);
2434 lsa->lsa_tgts = osts;
2436 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2438 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2439 progname, template);
2442 rc = build_component(layout, lsa, *layout != NULL);
2444 /* clean clean lsa */
2445 setstripe_args_init(lsa);
2449 cYAML_free_tree(tree);
2453 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2454 * end of the last component in the existing file, and adjust the
2455 * first extent start of the components to be added accordingly. */
2456 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2458 struct llapi_layout *head;
2459 uint64_t start, end, stripe_size, prev_end = 0;
2462 if (layout == NULL) {
2464 "%s setstripe: layout must be specified\n",
2470 head = llapi_layout_get_by_path(fname, 0);
2473 "%s setstripe: cannot read layout from '%s': %s\n",
2474 progname, fname, strerror(errno));
2476 } else if (errno == ENODATA) {
2477 /* file without LOVEA, this component-add will be turned
2478 * into a component-create. */
2479 llapi_layout_free(head);
2481 } else if (!llapi_layout_is_composite(head)) {
2482 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2484 llapi_layout_free(head);
2488 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2490 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2491 progname, strerror(errno));
2492 llapi_layout_free(head);
2496 llapi_layout_free(head);
2498 /* Make sure we use the first component of the layout to be added. */
2499 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2502 "%s setstripe: cannot move component cursor: %s\n",
2503 progname, strerror(errno));
2507 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2509 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2510 progname, strerror(errno));
2514 if (start > prev_end || end <= prev_end) {
2516 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2517 progname, start, end, prev_end);
2521 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2523 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2524 progname, strerror(errno));
2528 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2529 (prev_end & (stripe_size - 1))) {
2531 "%s setstripe: stripe size %lu not aligned with %lu\n",
2532 progname, stripe_size, prev_end);
2536 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2539 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2540 progname, prev_end, end, strerror(errno));
2547 static inline bool arg_is_eof(char *arg)
2549 return !strncmp(arg, "-1", strlen("-1")) ||
2550 !strncmp(arg, "EOF", strlen("EOF")) ||
2551 !strncmp(arg, "eof", strlen("eof"));
2555 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2557 * Return: Valid mirror_args pointer on success and
2558 * NULL if memory allocation fails.
2560 static struct mirror_args *lfs_mirror_alloc(void)
2562 struct mirror_args *mirror = NULL;
2565 mirror = calloc(1, sizeof(*mirror));
2576 * lfs_mirror_free() - Free memory allocated for a mirror argument
2578 * @mirror: Previously allocated mirror argument structure by
2579 * lfs_mirror_alloc().
2581 * Free memory allocated for @mirror.
2585 static void lfs_mirror_free(struct mirror_args *mirror)
2587 if (mirror->m_layout != NULL)
2588 llapi_layout_free(mirror->m_layout);
2593 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2594 * @mirror_list: Previously allocated mirror list.
2596 * Free memory allocated for @mirror_list.
2600 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2602 struct mirror_args *next_mirror = NULL;
2604 while (mirror_list != NULL) {
2605 next_mirror = mirror_list->m_next;
2606 lfs_mirror_free(mirror_list);
2607 mirror_list = next_mirror;
2619 LFS_COMP_NO_VERIFY_OPT,
2621 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2623 LFS_MIRROR_STATE_OPT,
2625 LFS_MIRROR_INDEX_OPT,
2626 LFS_LAYOUT_FOREIGN_OPT,
2631 static int lfs_setstripe_internal(int argc, char **argv,
2632 enum setstripe_origin opc)
2634 struct lfs_setstripe_args lsa = { 0 };
2635 struct llapi_stripe_param *param = NULL;
2636 struct find_param migrate_mdt_param = {
2646 unsigned long long size_units = 1;
2647 bool migrate_mode = false;
2648 bool migrate_mdt_mode = false;
2649 bool setstripe_mode = false;
2650 bool migration_block = false;
2651 __u64 migration_flags = 0;
2652 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2653 int comp_del = 0, comp_set = 0;
2656 struct llapi_layout *layout = NULL;
2657 struct llapi_layout **lpp = &layout;
2658 bool mirror_mode = false;
2659 bool has_m_file = false;
2660 __u32 mirror_count = 0;
2661 enum mirror_flags mirror_flags = 0;
2662 struct mirror_args *mirror_list = NULL;
2663 struct mirror_args *new_mirror = NULL;
2664 struct mirror_args *last_mirror = NULL;
2665 __u16 mirror_id = 0;
2667 bool from_yaml = false;
2668 bool from_copy = false;
2669 char *template = NULL;
2670 bool foreign_mode = false;
2672 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
2673 char *mode_opt = NULL;
2674 mode_t previous_umask = 0;
2675 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2677 struct option long_opts[] = {
2678 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2679 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2680 /* --block is only valid in migrate mode */
2681 { .val = 'b', .name = "block", .has_arg = no_argument },
2682 { .val = LFS_COMP_ADD_OPT,
2683 .name = "comp-add", .has_arg = no_argument },
2684 { .val = LFS_COMP_ADD_OPT,
2685 .name = "component-add", .has_arg = no_argument },
2686 { .val = LFS_COMP_DEL_OPT,
2687 .name = "comp-del", .has_arg = no_argument },
2688 { .val = LFS_COMP_DEL_OPT,
2689 .name = "component-del", .has_arg = no_argument },
2690 { .val = LFS_COMP_FLAGS_OPT,
2691 .name = "comp-flags", .has_arg = required_argument },
2692 { .val = LFS_COMP_FLAGS_OPT,
2693 .name = "component-flags",
2694 .has_arg = required_argument },
2695 { .val = LFS_COMP_SET_OPT,
2696 .name = "comp-set", .has_arg = no_argument },
2697 { .val = LFS_COMP_SET_OPT,
2698 .name = "component-set",
2699 .has_arg = no_argument},
2700 { .val = LFS_COMP_NO_VERIFY_OPT,
2701 .name = "no-verify", .has_arg = no_argument},
2702 { .val = LFS_LAYOUT_FLAGS_OPT,
2703 .name = "flags", .has_arg = required_argument},
2704 { .val = LFS_LAYOUT_FOREIGN_OPT,
2705 .name = "foreign", .has_arg = optional_argument},
2706 { .val = LFS_MIRROR_ID_OPT,
2707 .name = "mirror-id", .has_arg = required_argument},
2708 { .val = LFS_MODE_OPT,
2709 .name = "mode", .has_arg = required_argument},
2710 { .val = LFS_LAYOUT_COPY,
2711 .name = "copy", .has_arg = required_argument},
2712 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2713 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2714 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2715 { .val = 'C', .name = "overstripe-count",
2716 .has_arg = required_argument},
2717 { .val = 'd', .name = "delete", .has_arg = no_argument},
2718 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2719 /* --non-direct is only valid in migrate mode */
2720 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2721 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2722 { .val = 'E', .name = "component-end",
2723 .has_arg = required_argument},
2724 { .val = 'f', .name = "file", .has_arg = required_argument },
2725 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2726 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2727 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2728 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2729 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2730 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2731 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2732 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2733 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2734 { .val = 'L', .name = "layout", .has_arg = required_argument },
2735 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2736 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2737 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2738 /* --non-block is only valid in migrate mode */
2739 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2740 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2741 { .val = 'o', .name = "ost", .has_arg = required_argument },
2742 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2743 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2744 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2746 { .val = 'p', .name = "pool", .has_arg = required_argument },
2747 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2748 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2749 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2750 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2751 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2752 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2753 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2754 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2755 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2756 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2757 /* --verbose is only valid in migrate mode */
2758 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2759 { .val = 'x', .name = "xattr", .has_arg = required_argument },
2760 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2763 setstripe_args_init(&lsa);
2765 migrate_mode = (opc == SO_MIGRATE);
2766 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2767 setstripe_mode = (opc == SO_SETSTRIPE);
2769 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2771 while ((c = getopt_long(argc, argv,
2772 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:",
2773 long_opts, NULL)) >= 0) {
2778 case LFS_COMP_ADD_OPT:
2781 case LFS_COMP_DEL_OPT:
2784 case LFS_COMP_FLAGS_OPT:
2785 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2786 &lsa.lsa_comp_neg_flags);
2789 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2790 fprintf(stderr, "%s: inverted flags are not supported\n",
2794 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2796 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2803 case LFS_COMP_SET_OPT:
2806 case LFS_COMP_NO_VERIFY_OPT:
2807 mirror_flags |= MF_NO_VERIFY;
2809 case LFS_MIRROR_ID_OPT:
2810 mirror_id = strtoul(optarg, &end, 0);
2811 if (*end != '\0' || mirror_id == 0) {
2813 "%s %s: invalid mirror ID '%s'\n",
2814 progname, argv[0], optarg);
2818 case LFS_LAYOUT_FLAGS_OPT: {
2821 /* check for numeric flags (foreign and mirror cases) */
2822 if (setstripe_mode && !mirror_mode && !last_mirror) {
2823 flags = strtoul(optarg, &end, 16);
2826 "%s %s: bad flags '%s'\n",
2827 progname, argv[0], optarg);
2833 if (!mirror_mode || !last_mirror) {
2834 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2839 result = comp_str2flags(optarg, &last_mirror->m_flags,
2845 fprintf(stderr, "%s: inverted flags are not supported\n",
2850 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2852 "%s: unsupported mirror flags: %s\n",
2859 case LFS_LAYOUT_FOREIGN_OPT:
2860 if (optarg != NULL) {
2861 /* check pure numeric */
2862 type = strtoul(optarg, &end, 0);
2865 type = check_foreign_type_name(optarg);
2866 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
2868 "%s %s: unrecognized foreign type '%s'\n",
2875 foreign_mode = true;
2879 if (mode_opt != NULL) {
2880 mode = strtoul(mode_opt, &end, 8);
2883 "%s %s: bad mode '%s'\n",
2884 progname, argv[0], mode_opt);
2887 previous_umask = umask(0);
2890 case LFS_LAYOUT_COPY:
2895 if (!migrate_mode) {
2897 "%s %s: -b|--block valid only for migrate command\n",
2901 migration_block = true;
2904 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
2907 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2910 "%s %s: invalid stripe count '%s'\n",
2911 progname, argv[0], optarg);
2915 if (lsa.lsa_stripe_count == -1)
2916 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2919 /* delete the default striping pattern */
2921 if (opc == SO_MIRROR_SPLIT) {
2924 "%s %s: -d cannot used with -f\n",
2928 mirror_flags |= MF_DESTROY;
2932 if (!migrate_mode) {
2934 "%s %s: -D|--non-direct is valid "
2935 "only for migrate command\n",
2939 migration_flags |= MIGRATION_NONDIRECT;
2942 if (lsa.lsa_comp_end != 0) {
2943 result = comp_args_to_layout(lpp, &lsa, true);
2946 "%s %s: invalid layout\n",
2951 setstripe_args_init_inherit(&lsa);
2954 if (arg_is_eof(optarg)) {
2955 lsa.lsa_comp_end = LUSTRE_EOF;
2957 result = llapi_parse_size(optarg,
2962 "%s %s: invalid component end '%s'\n",
2963 progname, argv[0], optarg);
2969 if (!migrate_mode) {
2970 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2974 lsa.lsa_pattern = check_hashtype(optarg);
2975 if (lsa.lsa_pattern == 0) {
2977 "%s %s: bad stripe hash type '%s'\n",
2978 progname, argv[0], optarg);
2983 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2986 "%s %s: invalid stripe offset '%s'\n",
2987 progname, argv[0], optarg);
2990 if (lsa.lsa_stripe_off == -1)
2991 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2994 comp_id = strtoul(optarg, &end, 0);
2995 if (*end != '\0' || comp_id == 0 ||
2996 comp_id > LCME_ID_MAX) {
2998 "%s %s: invalid component ID '%s'\n",
2999 progname, argv[0], optarg);
3004 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3006 "error: %s: invalid option: %s\n",
3007 progname, argv[optopt + 1]);
3010 if (opc == SO_MIRROR_EXTEND) {
3011 if (last_mirror == NULL) {
3013 "error: %s: '-N' must exist in front of '%s'\n",
3014 progname, argv[optopt + 1]);
3017 last_mirror->m_file = optarg;
3018 last_mirror->m_count = 1;
3021 if (mirror_list == NULL)
3022 mirror_list = lfs_mirror_alloc();
3023 mirror_list->m_file = optarg;
3028 if (strcmp(argv[optind - 1], "mdt") == 0) {
3029 /* Can be only the first component */
3030 if (layout != NULL) {
3032 fprintf(stderr, "error: 'mdt' layout "
3033 "can be only the first one\n");
3036 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3038 fprintf(stderr, "error: 'mdt' layout "
3039 "size is too big\n");
3042 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3043 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3045 fprintf(stderr, "error: layout '%s' is "
3046 "unknown, supported layouts are: "
3047 "'mdt', 'raid0'\n", argv[optind]);
3052 if (!migrate_mode) {
3054 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3058 migrate_mdt_mode = true;
3059 lsa.lsa_nr_tgts = parse_targets(tgts,
3060 sizeof(tgts) / sizeof(__u32),
3061 lsa.lsa_nr_tgts, optarg, NULL);
3062 if (lsa.lsa_nr_tgts < 0) {
3064 "%s %s: invalid MDT target(s) '%s'\n",
3065 progname, argv[0], optarg);
3069 lsa.lsa_tgts = tgts;
3070 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3071 lsa.lsa_stripe_off = tgts[0];
3074 if (!migrate_mode) {
3076 "%s %s: -n|--non-block valid only for migrate command\n",
3080 migration_flags |= MIGRATION_NONBLOCK;
3083 if (opc == SO_SETSTRIPE) {
3084 opc = SO_MIRROR_CREATE;
3088 if (optarg != NULL) {
3089 mirror_count = strtoul(optarg, &end, 0);
3090 if (*end != '\0' || mirror_count == 0) {
3092 "error: %s: bad mirror count: %s\n",
3099 new_mirror = lfs_mirror_alloc();
3100 new_mirror->m_count = mirror_count;
3102 if (mirror_list == NULL)
3103 mirror_list = new_mirror;
3105 if (last_mirror != NULL) {
3106 /* wrap up last mirror */
3107 if (lsa.lsa_comp_end == 0)
3108 lsa.lsa_comp_end = LUSTRE_EOF;
3110 result = comp_args_to_layout(lpp, &lsa, true);
3112 lfs_mirror_free(new_mirror);
3116 setstripe_args_init_inherit(&lsa);
3118 last_mirror->m_next = new_mirror;
3121 last_mirror = new_mirror;
3122 lpp = &last_mirror->m_layout;
3125 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3126 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3127 fprintf(stderr, "warning: '--ost-list' is "
3128 "deprecated, use '--ost' instead\n");
3130 /* -o allows overstriping, and must note it because
3131 * parse_targets is shared with MDT striping, which
3132 * does not allow duplicates
3134 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3135 lsa.lsa_nr_tgts = parse_targets(tgts,
3136 sizeof(tgts) / sizeof(__u32),
3137 lsa.lsa_nr_tgts, optarg,
3139 if (lsa.lsa_nr_tgts < 0) {
3141 "%s %s: invalid OST target(s) '%s'\n",
3142 progname, argv[0], optarg);
3146 lsa.lsa_tgts = tgts;
3147 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3148 lsa.lsa_stripe_off = tgts[0];
3153 lsa.lsa_pool_name = optarg;
3155 if (strlen(lsa.lsa_pool_name) == 0 ||
3156 strncmp(lsa.lsa_pool_name, "none",
3157 LOV_MAXPOOLNAME) == 0)
3158 lsa.lsa_pool_name = NULL;
3161 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3165 "%s %s: invalid stripe size '%s'\n",
3166 progname, argv[0], optarg);
3171 if (!migrate_mode) {
3173 "%s %s: -v|--verbose valid only for migrate command\n",
3177 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3178 migration_flags = MIGRATION_VERBOSE;
3188 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3189 progname, argv[0], argv[optind - 1]);
3194 fname = argv[optind];
3196 if (optind == argc) {
3197 fprintf(stderr, "%s %s: FILE must be specified\n",
3202 if (xattr && !foreign_mode) {
3203 /* only print a warning as this is harmless and will be ignored
3206 "%s %s: xattr has been specified for non-foreign layout\n",
3208 } else if (foreign_mode && !xattr) {
3210 "%s %s: xattr must be provided in foreign mode\n",
3215 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3216 comp_set || comp_id || delete || from_copy ||
3217 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3220 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3225 if (mirror_mode && mirror_count == 0) {
3227 "error: %s: --mirror-count|-N option is required\n",
3234 if (lsa.lsa_comp_end == 0)
3235 lsa.lsa_comp_end = LUSTRE_EOF;
3238 if (lsa.lsa_comp_end != 0) {
3239 result = comp_args_to_layout(lpp, &lsa, true);
3244 if (mirror_flags & MF_NO_VERIFY) {
3245 if (opc != SO_MIRROR_EXTEND) {
3247 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3251 } else if (!has_m_file) {
3253 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3260 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3261 * altered by user space tool, so we don't need to support the
3262 * --component-set for this moment. */
3263 if (comp_set && !comp_id) {
3264 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3269 if ((delete + comp_set + comp_del + comp_add) > 1) {
3271 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3276 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3277 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3279 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3284 if ((comp_set || comp_del) &&
3285 (setstripe_args_specified(&lsa) || layout != NULL)) {
3287 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3292 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3294 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3299 if (comp_add || comp_del) {
3302 result = lstat(fname, &st);
3303 if (result == 0 && S_ISDIR(st.st_mode)) {
3305 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3311 fprintf(stderr, "error: %s: can't use --component-add "
3312 "or --component-del for mirror operation\n",
3319 if (layout == NULL) {
3321 "%s %s: option -E must be specified with --component-add\n",
3326 result = adjust_first_extent(fname, layout);
3327 if (result == -ENODATA)
3329 else if (result != 0)
3333 if (from_yaml && from_copy) {
3335 "%s: can't specify --yaml and --copy together\n",
3340 if ((from_yaml || from_copy) &&
3341 (setstripe_args_specified(&lsa) || layout != NULL)) {
3342 fprintf(stderr, "error: %s: can't specify --yaml with "
3343 "-c, -S, -i, -o, -p or -E options.\n",
3348 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3350 "%s %s: options --non-block and --block are mutually exclusive\n",
3355 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3358 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3363 if (migrate_mdt_mode) {
3364 struct lmv_user_md *lmu;
3366 /* initialize migrate mdt parameters */
3367 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3368 LMV_USER_MAGIC_SPECIFIC));
3371 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3372 progname, argv[0], strerror(ENOMEM));
3376 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3377 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3378 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3380 "%s %s: migrate should specify MDT index\n",
3385 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3386 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3387 lmu->lum_hash_type = lsa.lsa_pattern;
3389 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3390 if (lsa.lsa_pool_name)
3391 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3392 sizeof(lmu->lum_pool_name) - 1);
3393 if (lsa.lsa_nr_tgts > 1) {
3396 if (lsa.lsa_stripe_count > 0 &&
3397 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3398 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3400 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3401 progname, lsa.lsa_stripe_count,
3407 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3408 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3409 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3410 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3412 lmu->lum_magic = LMV_USER_MAGIC;
3415 migrate_mdt_param.fp_lmv_md = lmu;
3416 migrate_mdt_param.fp_migrate = 1;
3417 } else if (layout == NULL) {
3418 /* initialize stripe parameters */
3419 param = calloc(1, offsetof(typeof(*param),
3420 lsp_osts[lsa.lsa_nr_tgts]));
3421 if (param == NULL) {
3423 "%s %s: cannot allocate memory for parameters: %s\n",
3424 progname, argv[0], strerror(ENOMEM));
3429 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3430 param->lsp_stripe_size = lsa.lsa_stripe_size;
3431 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3432 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3433 param->lsp_stripe_count = -1;
3435 param->lsp_stripe_count = lsa.lsa_stripe_count;
3437 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3438 param->lsp_stripe_offset = -1;
3440 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3441 param->lsp_stripe_pattern =
3442 llapi_pattern_to_lov(lsa.lsa_pattern);
3443 if (param->lsp_stripe_pattern == EINVAL) {
3444 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3449 param->lsp_pool = lsa.lsa_pool_name;
3450 param->lsp_is_specific = false;
3451 if (lsa.lsa_nr_tgts > 0) {
3452 if (lsa.lsa_stripe_count > 0 &&
3453 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3454 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3455 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3456 fprintf(stderr, "error: %s: stripe count %lld "
3457 "doesn't match the number of OSTs: %d\n"
3458 , argv[0], lsa.lsa_stripe_count,
3464 param->lsp_is_specific = true;
3465 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3466 memcpy(param->lsp_osts, tgts,
3467 sizeof(*tgts) * lsa.lsa_nr_tgts);
3472 /* generate a layout from a YAML template */
3473 result = lfs_comp_create_from_yaml(template, &layout,
3476 fprintf(stderr, "error: %s: can't create composite "
3477 "layout from template file %s\n",
3481 } else if (from_copy) {
3482 layout = llapi_layout_get_by_path(template, 0);
3483 if (layout == NULL) {
3485 "%s: can't create composite layout from file %s.\n",
3486 progname, template);
3491 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3492 if (migrate_mdt_mode) {
3493 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3494 } else if (migrate_mode) {
3495 result = lfs_migrate(fname, migration_flags, param,
3497 } else if (comp_set != 0) {
3498 result = lfs_component_set(fname, comp_id,
3500 lsa.lsa_comp_neg_flags);
3501 } else if (comp_del != 0) {
3502 result = lfs_component_del(fname, comp_id,
3504 lsa.lsa_comp_neg_flags);
3505 } else if (comp_add != 0) {
3506 result = lfs_component_add(fname, layout);
3507 } else if (opc == SO_MIRROR_CREATE) {
3508 result = mirror_create(fname, mirror_list);
3509 } else if (opc == SO_MIRROR_EXTEND) {
3510 result = mirror_extend(fname, mirror_list,
3512 } else if (opc == SO_MIRROR_SPLIT) {
3513 if (mirror_id == 0 && comp_id == 0) {
3515 "%s %s: no mirror id or component id is specified\n",
3520 comp_id = mirror_id;
3522 mirror_flags |= MF_COMP_ID;
3523 result = mirror_split(fname, comp_id, mirror_flags,
3524 has_m_file ? mirror_list->m_file :
3526 } else if (layout != NULL) {
3527 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3533 } else if (foreign_mode) {
3534 result = llapi_file_create_foreign(fname, mode, type,
3541 result = llapi_file_open_param(fname,
3550 /* Save the first error encountered. */
3557 if (mode_opt != NULL)
3558 umask(previous_umask);
3561 free(migrate_mdt_param.fp_lmv_md);
3562 llapi_layout_free(layout);
3563 lfs_mirror_list_free(mirror_list);
3568 llapi_layout_free(layout);
3569 lfs_mirror_list_free(mirror_list);
3573 static int lfs_poollist(int argc, char **argv)
3578 return llapi_poollist(argv[1]);
3581 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3586 char *endptr = "AD";
3591 else if (str[0] == '-')
3597 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3598 long long val = strtoll(timebuf, &endptr, 0);
3603 unit *= 52; /* 52 weeks + 1 day below */
3604 case 'w': /* fallthrough */
3606 case '\0': /* days are default unit if none used */
3607 case 'd': /* fallthrough */
3608 unit = (unit + (*endptr == 'y')) * 24;
3609 case 'h': /* fallthrough */
3611 case 'm': /* fallthrough */
3613 case 's': /* fallthrough */
3615 /* don't need to multiply by 1 for seconds */
3618 "%s find: bad time string '%s': %s\n",
3619 progname, timebuf, strerror(EINVAL));
3623 if (param->fp_time_margin == 0 ||
3624 (*endptr && unit < param->fp_time_margin))
3625 param->fp_time_margin = unit;
3632 fprintf(stderr, "%s find: bad time '%s': too large\n",
3642 static int name2uid(unsigned int *id, const char *name)
3644 struct passwd *passwd;
3646 passwd = getpwnam(name);
3649 *id = passwd->pw_uid;
3654 static int name2gid(unsigned int *id, const char *name)
3656 struct group *group;
3658 group = getgrnam(name);
3661 *id = group->gr_gid;
3666 static inline int name2projid(unsigned int *id, const char *name)
3671 static int uid2name(char **name, unsigned int id)
3673 struct passwd *passwd;
3675 passwd = getpwuid(id);
3678 *name = passwd->pw_name;
3683 static inline int gid2name(char **name, unsigned int id)
3685 struct group *group;
3687 group = getgrgid(id);
3690 *name = group->gr_name;
3695 static int name2layout(__u32 *layout, char *name)
3697 char *ptr, *layout_name;
3700 for (ptr = name; ; ptr = NULL) {
3701 layout_name = strtok(ptr, ",");
3702 if (layout_name == NULL)
3704 if (strcmp(layout_name, "released") == 0)
3705 *layout |= LOV_PATTERN_F_RELEASED;
3706 else if (strcmp(layout_name, "raid0") == 0)
3707 *layout |= LOV_PATTERN_RAID0;
3708 else if (strcmp(layout_name, "mdt") == 0)
3709 *layout |= LOV_PATTERN_MDT;
3710 else if (strcmp(layout_name, "overstriping") == 0)
3711 *layout |= LOV_PATTERN_OVERSTRIPING;
3718 static int lfs_find(int argc, char **argv)
3723 struct find_param param = {
3726 .fp_time_margin = 24 * 60 * 60,
3728 struct option long_opts[] = {
3729 { .val = 'A', .name = "atime", .has_arg = required_argument },
3730 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3731 { .val = LFS_COMP_COUNT_OPT,
3732 .name = "comp-count", .has_arg = required_argument },
3733 { .val = LFS_COMP_COUNT_OPT,
3734 .name = "component-count",
3735 .has_arg = required_argument },
3736 { .val = LFS_COMP_FLAGS_OPT,
3737 .name = "comp-flags", .has_arg = required_argument },
3738 { .val = LFS_COMP_FLAGS_OPT,
3739 .name = "component-flags",
3740 .has_arg = required_argument },
3741 { .val = LFS_COMP_START_OPT,
3742 .name = "comp-start", .has_arg = required_argument },
3743 { .val = LFS_COMP_START_OPT,
3744 .name = "component-start",
3745 .has_arg = required_argument },
3746 { .val = LFS_MIRROR_STATE_OPT,
3747 .name = "mirror-state", .has_arg = required_argument },
3748 { .val = LFS_LAYOUT_FOREIGN_OPT,
3749 .name = "foreign", .has_arg = optional_argument},
3750 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3751 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3752 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3753 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3754 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3755 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3756 { .val = 'E', .name = "component-end",
3757 .has_arg = required_argument },
3758 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3759 { .val = LFS_LAYOUT_FOREIGN_OPT,
3760 .name = "foreign", .has_arg = optional_argument},
3761 { .val = 'g', .name = "gid", .has_arg = required_argument },
3762 { .val = 'G', .name = "group", .has_arg = required_argument },
3763 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3764 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3765 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3766 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3767 { .val = 'L', .name = "layout", .has_arg = required_argument },
3768 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3769 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3770 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3771 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3772 { .val = 'n', .name = "name", .has_arg = required_argument },
3773 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3774 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3775 { .val = 'O', .name = "obd", .has_arg = required_argument },
3776 { .val = 'O', .name = "ost", .has_arg = required_argument },
3777 /* no short option for pool yet, can be 'p' after 2.18 */
3778 { .val = LFS_POOL_OPT,
3779 .name = "pool", .has_arg = required_argument },
3780 { .val = '0', .name = "print0", .has_arg = no_argument },
3781 { .val = 'P', .name = "print", .has_arg = no_argument },
3782 { .val = LFS_PROJID_OPT,
3783 .name = "projid", .has_arg = required_argument },
3784 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3785 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3786 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3787 { .val = 's', .name = "size", .has_arg = required_argument },
3788 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3789 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3790 { .val = 't', .name = "type", .has_arg = required_argument },
3791 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3792 { .val = 'u', .name = "uid", .has_arg = required_argument },
3793 { .val = 'U', .name = "user", .has_arg = required_argument },
3794 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3795 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3807 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3808 while ((c = getopt_long_only(argc, argv,
3809 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3810 long_opts, NULL)) >= 0) {
3815 /* '!' is part of option */
3816 /* when getopt_long_only() finds a string which is not
3817 * an option nor a known option argument it returns 1
3818 * in that case if we already have found pathstart and pathend
3819 * (i.e. we have the list of pathnames),
3820 * the only supported value is "!"
3822 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3823 if (!isoption && pathend != -1) {
3824 fprintf(stderr, "err: %s: filename|dirname must either "
3825 "precede options or follow options\n",
3830 if (!isoption && pathstart == -1)
3831 pathstart = optind - 1;
3832 if (isoption && pathstart != -1 && pathend == -1)
3833 pathend = optind - 2;
3839 /* unknown; opt is "!" or path component,
3840 * checking done above.
3842 if (strcmp(optarg, "!") == 0)
3846 xtime = ¶m.fp_atime;
3847 xsign = ¶m.fp_asign;
3848 param.fp_exclude_atime = !!neg_opt;
3849 /* no break, this falls through to 'C' for ctime */
3852 xtime = ¶m.fp_ctime;
3853 xsign = ¶m.fp_csign;
3854 param.fp_exclude_ctime = !!neg_opt;
3856 /* no break, this falls through to 'M' for mtime */
3859 xtime = ¶m.fp_mtime;
3860 xsign = ¶m.fp_msign;
3861 param.fp_exclude_mtime = !!neg_opt;
3863 rc = set_time(¶m, &t, xtime, optarg);
3864 if (rc == LONG_MAX) {
3872 if (optarg[0] == '+') {
3873 param.fp_blocks_sign = -1;
3875 } else if (optarg[0] == '-') {
3876 param.fp_blocks_sign = 1;
3880 param.fp_blocks_units = 1024;
3881 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3882 ¶m.fp_blocks_units, 0);
3884 fprintf(stderr, "error: bad blocks '%s'\n",
3888 param.fp_check_blocks = 1;
3889 param.fp_exclude_blocks = !!neg_opt;
3891 case LFS_COMP_COUNT_OPT:
3892 if (optarg[0] == '+') {
3893 param.fp_comp_count_sign = -1;
3895 } else if (optarg[0] == '-') {
3896 param.fp_comp_count_sign = 1;
3900 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3901 if (*endptr != '\0') {
3902 fprintf(stderr, "error: bad component count "
3906 param.fp_check_comp_count = 1;
3907 param.fp_exclude_comp_count = !!neg_opt;
3909 case LFS_COMP_FLAGS_OPT:
3910 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3911 ¶m.fp_comp_neg_flags);
3913 fprintf(stderr, "error: bad component flags "
3917 param.fp_check_comp_flags = 1;
3919 __u32 flags = param.fp_comp_neg_flags;
3920 param.fp_comp_neg_flags = param.fp_comp_flags;
3921 param.fp_comp_flags = flags;
3924 case LFS_COMP_START_OPT:
3925 if (optarg[0] == '+') {
3926 param.fp_comp_start_sign = -1;
3928 } else if (optarg[0] == '-') {
3929 param.fp_comp_start_sign = 1;
3933 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3934 ¶m.fp_comp_start_units, 0);
3936 fprintf(stderr, "error: bad component start "
3940 param.fp_check_comp_start = 1;
3941 param.fp_exclude_comp_start = !!neg_opt;
3943 case LFS_MIRROR_STATE_OPT:
3944 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3945 ¶m.fp_mirror_neg_state);
3948 "error: bad mirrored file state '%s'\n",
3952 param.fp_check_mirror_state = 1;
3954 __u16 state = param.fp_mirror_neg_state;
3955 param.fp_mirror_neg_state =
3956 param.fp_mirror_state;
3957 param.fp_mirror_state = state;
3961 if (optarg[0] == '+') {
3962 param.fp_stripe_count_sign = -1;
3964 } else if (optarg[0] == '-') {
3965 param.fp_stripe_count_sign = 1;
3969 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3970 if (*endptr != '\0') {
3971 fprintf(stderr,"error: bad stripe_count '%s'\n",
3976 param.fp_check_stripe_count = 1;
3977 param.fp_exclude_stripe_count = !!neg_opt;
3980 param.fp_max_depth = strtol(optarg, 0, 0);
3983 if (optarg[0] == '+') {
3984 param.fp_comp_end_sign = -1;
3986 } else if (optarg[0] == '-') {
3987 param.fp_comp_end_sign = 1;
3991 if (arg_is_eof(optarg)) {
3992 param.fp_comp_end = LUSTRE_EOF;
3993 param.fp_comp_end_units = 1;
3996 rc = llapi_parse_size(optarg,
3998 ¶m.fp_comp_end_units, 0);
4001 fprintf(stderr, "error: bad component end "
4005 param.fp_check_comp_end = 1;
4006 param.fp_exclude_comp_end = !!neg_opt;
4008 case LFS_LAYOUT_FOREIGN_OPT: {
4009 /* all types by default */
4010 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4012 if (optarg != NULL) {
4013 /* check pure numeric */
4014 type = strtoul(optarg, &endptr, 0);
4017 type = check_foreign_type_name(optarg);
4018 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4020 "%s %s: unknown foreign type '%s'\n",
4027 param.fp_foreign_type = type;
4028 param.fp_check_foreign = 1;
4029 param.fp_exclude_foreign = !!neg_opt;
4034 rc = name2gid(¶m.fp_gid, optarg);
4036 param.fp_gid = strtoul(optarg, &endptr, 10);
4037 if (*endptr != '\0') {
4038 fprintf(stderr, "Group/GID: %s cannot "
4039 "be found.\n", optarg);
4044 param.fp_exclude_gid = !!neg_opt;
4045 param.fp_check_gid = 1;
4048 param.fp_hash_type = check_hashtype(optarg);
4049 if (param.fp_hash_type == 0) {
4050 fprintf(stderr, "error: bad hash_type '%s'\n",
4055 param.fp_check_hash_type = 1;
4056 param.fp_exclude_hash_type = !!neg_opt;
4059 ret = name2layout(¶m.fp_layout, optarg);
4062 param.fp_exclude_layout = !!neg_opt;
4063 param.fp_check_layout = 1;
4067 rc = name2uid(¶m.fp_uid, optarg);
4069 param.fp_uid = strtoul(optarg, &endptr, 10);
4070 if (*endptr != '\0') {
4071 fprintf(stderr, "User/UID: %s cannot "
4072 "be found.\n", optarg);
4077 param.fp_exclude_uid = !!neg_opt;
4078 param.fp_check_uid = 1;
4081 param.fp_pattern = (char *)optarg;
4082 param.fp_exclude_pattern = !!neg_opt;
4085 if (optarg[0] == '+') {
4086 param.fp_mirror_count_sign = -1;
4088 } else if (optarg[0] == '-') {
4089 param.fp_mirror_count_sign = 1;
4093 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4094 if (*endptr != '\0') {
4096 "error: bad mirror count '%s'\n",
4100 param.fp_check_mirror_count = 1;
4101 param.fp_exclude_mirror_count = !!neg_opt;
4106 char *buf, *token, *next, *p;
4110 buf = strdup(optarg);
4116 param.fp_exclude_obd = !!neg_opt;
4119 while (token && *token) {
4120 token = strchr(token, ',');
4127 param.fp_exclude_mdt = !!neg_opt;
4128 param.fp_num_alloc_mdts += len;
4129 tmp = realloc(param.fp_mdt_uuid,
4130 param.fp_num_alloc_mdts *
4131 sizeof(*param.fp_mdt_uuid));
4137 param.fp_mdt_uuid = tmp;
4139 param.fp_exclude_obd = !!neg_opt;
4140 param.fp_num_alloc_obds += len;
4141 tmp = realloc(param.fp_obd_uuid,
4142 param.fp_num_alloc_obds *
4143 sizeof(*param.fp_obd_uuid));
4149 param.fp_obd_uuid = tmp;
4151 for (token = buf; token && *token; token = next) {
4152 struct obd_uuid *puuid;
4155 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4158 ¶m.fp_obd_uuid[param.fp_num_obds++];
4160 p = strchr(token, ',');
4167 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4172 strncpy(puuid->uuid, token,
4173 sizeof(puuid->uuid));
4180 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4184 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4186 "Pool name %s is too long (max %d)\n",
4187 optarg, LOV_MAXPOOLNAME);
4192 * We do check for empty pool because empty pool
4193 * is used to find V1 LOV attributes
4195 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4196 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4197 param.fp_exclude_pool = !!neg_opt;
4198 param.fp_check_pool = 1;
4200 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4201 case 'p': /* want this for --pool, to match getstripe/find */
4203 "warning: -p deprecated, use --print0 or -0\n");
4206 param.fp_zero_end = 1;
4208 case 'P': /* we always print, this option is a no-op */
4210 case LFS_PROJID_OPT:
4211 rc = name2projid(¶m.fp_projid, optarg);
4213 param.fp_projid = strtoul(optarg, &endptr, 10);
4214 if (*endptr != '\0') {
4216 "Invalid project ID: %s",
4222 param.fp_exclude_projid = !!neg_opt;
4223 param.fp_check_projid = 1;
4226 if (optarg[0] == '+') {
4227 param.fp_size_sign = -1;
4229 } else if (optarg[0] == '-') {
4230 param.fp_size_sign = 1;
4234 ret = llapi_parse_size(optarg, ¶m.fp_size,
4235 ¶m.fp_size_units, 0);
4237 fprintf(stderr, "error: bad file size '%s'\n",
4241 param.fp_check_size = 1;
4242 param.fp_exclude_size = !!neg_opt;
4245 if (optarg[0] == '+') {
4246 param.fp_stripe_size_sign = -1;
4248 } else if (optarg[0] == '-') {
4249 param.fp_stripe_size_sign = 1;
4253 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4254 ¶m.fp_stripe_size_units, 0);
4256 fprintf(stderr, "error: bad stripe_size '%s'\n",
4260 param.fp_check_stripe_size = 1;
4261 param.fp_exclude_stripe_size = !!neg_opt;
4264 param.fp_exclude_type = !!neg_opt;
4265 switch (optarg[0]) {
4267 param.fp_type = S_IFBLK;
4270 param.fp_type = S_IFCHR;
4273 param.fp_type = S_IFDIR;
4276 param.fp_type = S_IFREG;
4279 param.fp_type = S_IFLNK;
4282 param.fp_type = S_IFIFO;
4285 param.fp_type = S_IFSOCK;
4288 fprintf(stderr, "error: %s: bad type '%s'\n",
4295 if (optarg[0] == '+') {
4296 param.fp_mdt_count_sign = -1;
4298 } else if (optarg[0] == '-') {
4299 param.fp_mdt_count_sign = 1;
4303 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4304 if (*endptr != '\0') {
4305 fprintf(stderr, "error: bad mdt_count '%s'\n",
4310 param.fp_check_mdt_count = 1;
4311 param.fp_exclude_mdt_count = !!neg_opt;
4319 if (pathstart == -1) {
4320 fprintf(stderr, "error: %s: no filename|pathname\n",
4324 } else if (pathend == -1) {
4330 rc = llapi_find(argv[pathstart], ¶m);
4331 if (rc != 0 && ret == 0)
4333 } while (++pathstart < pathend);
4336 fprintf(stderr, "error: %s failed for %s.\n",
4337 argv[0], argv[optind - 1]);
4339 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4340 free(param.fp_obd_uuid);
4342 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4343 free(param.fp_mdt_uuid);
4348 static int lfs_getstripe_internal(int argc, char **argv,
4349 struct find_param *param)
4351 struct option long_opts[] = {
4352 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4353 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4354 { .val = LFS_COMP_COUNT_OPT,
4355 .name = "comp-count", .has_arg = no_argument },
4356 { .val = LFS_COMP_COUNT_OPT,
4357 .name = "component-count", .has_arg = no_argument },
4358 { .val = LFS_COMP_FLAGS_OPT,
4359 .name = "comp-flags", .has_arg = optional_argument },
4360 { .val = LFS_COMP_FLAGS_OPT,
4361 .name = "component-flags", .has_arg = optional_argument },
4362 { .val = LFS_COMP_START_OPT,
4363 .name = "comp-start", .has_arg = optional_argument },
4364 { .val = LFS_COMP_START_OPT,
4365 .name = "component-start", .has_arg = optional_argument },
4366 { .val = LFS_MIRROR_INDEX_OPT,
4367 .name = "mirror-index", .has_arg = required_argument },
4368 { .val = LFS_MIRROR_ID_OPT,
4369 .name = "mirror-id", .has_arg = required_argument },
4370 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4371 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4372 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4373 { .val = 'd', .name = "directory", .has_arg = no_argument },
4374 { .val = 'D', .name = "default", .has_arg = no_argument },
4375 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4376 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4377 { .val = 'F', .name = "fid", .has_arg = no_argument },
4378 { .val = 'g', .name = "generation", .has_arg = no_argument },
4379 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4380 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4381 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4382 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4383 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4384 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4385 { .val = 'L', .name = "layout", .has_arg = no_argument },
4386 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4387 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4388 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4389 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4390 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4391 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4392 { .val = 'O', .name = "obd", .has_arg = required_argument },
4393 { .val = 'O', .name = "ost", .has_arg = required_argument },
4394 { .val = 'p', .name = "pool", .has_arg = no_argument },
4395 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4396 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4397 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4398 { .val = 'R', .name = "raw", .has_arg = no_argument },
4399 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4400 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4401 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4402 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4403 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4404 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4405 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4406 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4410 int pathstart = -1, pathend = -1;
4414 while ((c = getopt_long(argc, argv,
4415 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4416 long_opts, NULL)) != -1) {
4420 /* '!' is part of option */
4421 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4422 if (!isoption && pathend != -1) {
4424 "error: %s: filename|dirname must either precede options or follow options\n",
4428 if (!isoption && pathstart == -1)
4429 pathstart = optind - 1;
4430 if (isoption && pathstart != -1 && pathend == -1)
4431 pathend = optind - 2;
4435 /* unknown: opt is "!" */
4436 if (strcmp(optarg, "!") == 0)
4440 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4441 param->fp_verbose |= VERBOSE_COUNT;
4442 param->fp_max_depth = 0;
4445 case LFS_COMP_COUNT_OPT:
4446 param->fp_verbose |= VERBOSE_COMP_COUNT;
4447 param->fp_max_depth = 0;
4449 case LFS_COMP_FLAGS_OPT:
4450 if (optarg != NULL) {
4451 rc = comp_str2flags(optarg,
4452 ¶m->fp_comp_flags,
4453 ¶m->fp_comp_neg_flags);
4455 fprintf(stderr, "error: %s bad "
4456 "component flags '%s'.\n",
4460 param->fp_check_comp_flags = 1;
4462 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4463 param->fp_max_depth = 0;
4466 case LFS_COMP_START_OPT:
4467 if (optarg != NULL) {
4469 if (tmp[0] == '+') {
4470 param->fp_comp_start_sign = -1;
4472 } else if (tmp[0] == '-') {
4473 param->fp_comp_start_sign = 1;
4476 rc = llapi_parse_size(tmp,
4477 ¶m->fp_comp_start,
4478 ¶m->fp_comp_start_units, 0);
4480 fprintf(stderr, "error: %s bad "
4481 "component start '%s'.\n",
4485 param->fp_check_comp_start = 1;
4488 param->fp_verbose |= VERBOSE_COMP_START;
4489 param->fp_max_depth = 0;
4492 case LFS_MIRROR_INDEX_OPT:
4493 if (optarg[0] == '+') {
4494 param->fp_mirror_index_sign = -1;
4496 } else if (optarg[0] == '-') {
4497 param->fp_mirror_index_sign = 1;
4501 param->fp_mirror_index = strtoul(optarg, &end, 0);
4502 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4503 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4505 "%s %s: invalid mirror index '%s'\n",
4506 progname, argv[0], optarg);
4509 if (param->fp_mirror_id != 0) {
4511 "%s %s: can't specify both mirror index and mirror ID\n",
4515 param->fp_check_mirror_index = 1;
4516 param->fp_exclude_mirror_index = !!neg_opt;
4518 case LFS_MIRROR_ID_OPT:
4519 if (optarg[0] == '+') {
4520 param->fp_mirror_id_sign = -1;
4522 } else if (optarg[0] == '-') {
4523 param->fp_mirror_id_sign = 1;
4527 param->fp_mirror_id = strtoul(optarg, &end, 0);
4528 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4529 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4531 "%s %s: invalid mirror ID '%s'\n",
4532 progname, argv[0], optarg);
4535 if (param->fp_mirror_index != 0) {
4537 "%s %s: can't specify both mirror index and mirror ID\n",
4541 param->fp_check_mirror_id = 1;
4542 param->fp_exclude_mirror_id = !!neg_opt;
4545 param->fp_max_depth = 0;
4548 param->fp_get_default_lmv = 1;
4551 if (optarg != NULL) {
4553 if (tmp[0] == '+') {
4554 param->fp_comp_end_sign = -1;
4556 } else if (tmp[0] == '-') {
4557 param->fp_comp_end_sign = 1;
4561 if (arg_is_eof(tmp)) {
4562 param->fp_comp_end = LUSTRE_EOF;
4563 param->fp_comp_end_units = 1;
4566 rc = llapi_parse_size(tmp,
4567 ¶m->fp_comp_end,
4568 ¶m->fp_comp_end_units, 0);
4571 fprintf(stderr, "error: %s bad "
4572 "component end '%s'.\n",
4576 param->fp_check_comp_end = 1;
4578 param->fp_verbose |= VERBOSE_COMP_END;
4579 param->fp_max_depth = 0;
4583 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4584 param->fp_verbose |= VERBOSE_DFID;
4585 param->fp_max_depth = 0;
4589 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4590 param->fp_verbose |= VERBOSE_GENERATION;
4591 param->fp_max_depth = 0;
4595 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4596 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4597 param->fp_max_depth = 0;
4601 if (optarg != NULL) {
4602 param->fp_comp_id = strtoul(optarg, &end, 0);
4603 if (*end != '\0' || param->fp_comp_id == 0 ||
4604 param->fp_comp_id > LCME_ID_MAX) {
4605 fprintf(stderr, "error: %s bad "
4606 "component id '%s'\n",
4610 param->fp_check_comp_id = 1;
4613 param->fp_max_depth = 0;
4614 param->fp_verbose |= VERBOSE_COMP_ID;
4618 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4619 param->fp_verbose |= VERBOSE_PATTERN;
4620 param->fp_max_depth = 0;
4623 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4625 fprintf(stderr, "warning: '-M' deprecated"
4626 ", use '--mdt-index' or '-m' instead\n");
4629 if (!(param->fp_verbose & VERBOSE_DETAIL))
4630 param->fp_max_depth = 0;
4631 param->fp_verbose |= VERBOSE_MDTINDEX;
4634 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4635 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4636 param->fp_max_depth = 0;
4640 if (param->fp_obd_uuid) {
4642 "error: %s: only one obduuid allowed",
4646 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4649 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4650 param->fp_verbose |= VERBOSE_POOL;
4651 param->fp_max_depth = 0;
4658 param->fp_recursive = 1;
4664 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4665 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4666 param->fp_max_depth = 0;
4670 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4680 if (pathstart == -1) {
4681 fprintf(stderr, "error: %s: no filename|pathname\n",
4684 } else if (pathend == -1) {
4692 if (param->fp_recursive)
4693 param->fp_max_depth = -1;
4694 else if (param->fp_verbose & VERBOSE_DETAIL)
4695 param->fp_max_depth = 1;
4697 if (!param->fp_verbose)
4698 param->fp_verbose = VERBOSE_DEFAULT;
4699 if (param->fp_quiet)
4700 param->fp_verbose = VERBOSE_OBJID;
4703 rc = llapi_getstripe(argv[pathstart], param);
4704 } while (++pathstart < pathend && !rc);
4707 fprintf(stderr, "error: %s failed for %s.\n",
4708 argv[0], argv[optind - 1]);
4712 static int lfs_tgts(int argc, char **argv)
4714 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4715 struct find_param param;
4716 int index = 0, rc=0;
4721 if (argc == 2 && !realpath(argv[1], path)) {
4723 fprintf(stderr, "error: invalid path '%s': %s\n",
4724 argv[1], strerror(-rc));
4728 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4729 /* Check if we have a mount point */
4730 if (mntdir[0] == '\0')
4733 memset(¶m, 0, sizeof(param));
4734 if (!strcmp(argv[0], "mdts"))
4735 param.fp_get_lmv = 1;
4737 rc = llapi_ostlist(mntdir, ¶m);
4739 fprintf(stderr, "error: %s: failed on %s\n",
4742 if (path[0] != '\0')
4744 memset(mntdir, 0, PATH_MAX);
4750 static int lfs_getstripe(int argc, char **argv)
4752 struct find_param param = { 0 };
4754 param.fp_max_depth = 1;
4755 return lfs_getstripe_internal(argc, argv, ¶m);
4759 static int lfs_getdirstripe(int argc, char **argv)
4761 struct find_param param = { 0 };
4762 struct option long_opts[] = {
4763 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4764 { .val = 'D', .name = "default", .has_arg = no_argument },
4765 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4766 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4767 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4768 { .val = 'O', .name = "obd", .has_arg = required_argument },
4769 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4770 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4771 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4772 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4776 param.fp_get_lmv = 1;
4778 while ((c = getopt_long(argc, argv,
4779 "cDHimO:rtTvy", long_opts, NULL)) != -1)
4784 param.fp_verbose |= VERBOSE_COUNT;
4787 param.fp_get_default_lmv = 1;
4791 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4793 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4795 fprintf(stderr, "warning: '-t' deprecated, "
4796 "use '--mdt-hash' or '-H' instead\n");
4799 param.fp_verbose |= VERBOSE_HASH_TYPE;
4802 if (param.fp_obd_uuid) {
4804 "error: %s: only one obduuid allowed",
4808 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4811 param.fp_recursive = 1;
4814 param.fp_verbose |= VERBOSE_DETAIL;
4820 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4821 progname, argv[0], argv[optind - 1]);
4829 if (param.fp_recursive)
4830 param.fp_max_depth = -1;
4832 if (!param.fp_verbose)
4833 param.fp_verbose = VERBOSE_DEFAULT;
4836 rc = llapi_getstripe(argv[optind], ¶m);
4837 } while (++optind < argc && !rc);
4840 fprintf(stderr, "error: %s failed for %s.\n",
4841 argv[0], argv[optind - 1]);
4846 MNTDF_INODES = 0x0001,
4847 MNTDF_COOKED = 0x0002,
4848 MNTDF_LAZY = 0x0004,
4849 MNTDF_VERBOSE = 0x0008,
4850 MNTDF_SHOW = 0x0010,
4853 #define COOK(value) \
4856 while (value > 1024) { \
4864 #define CDF "%11llu"
4865 #define HDF "%8.1f%c"
4869 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4871 double avail, used, ratio = 0;
4874 avail = st->os_ffree;
4875 used = st->os_files - st->os_ffree;
4877 avail = st->os_bavail;
4878 used = st->os_blocks - st->os_bfree;
4880 if (avail + used > 0)
4881 ratio = used / (used + avail) * 100;
4883 /* Round up to match df(1) usage percentage */
4884 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4887 /* This is only used to reflect various problem states for lfs df, so we only
4888 * translate the flags reflecting those states.
4890 static char obd_statfs_state_names[] = {
4891 [OS_STATE_DEGRADED] = 'D',
4892 [OS_STATE_READONLY] = 'R',
4893 [OS_STATE_NOPRECREATE] = 'N',
4894 [OS_STATE_ENOSPC] = 'S',
4895 [OS_STATE_ENOINO] = 'I',
4898 static char obd_statfs_state2char(int s)
4900 /* Not an error state, do not print here */
4901 if (s == OS_STATE_NONROT)
4904 if (s > ARRAY_SIZE(obd_statfs_state_names)/sizeof(char) ||
4905 obd_statfs_state_names[s] == 0)
4908 return obd_statfs_state_names[s];
4911 static int showdf(char *mntdir, struct obd_statfs *stat,
4912 char *uuid, enum mntdf_flags flags,
4913 char *type, int index, int rc)
4915 long long avail, used, total;
4917 char *suffix = "KMGTPEZY";
4918 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4919 char tbuf[3 * sizeof(__u64)];
4920 char ubuf[3 * sizeof(__u64)];
4921 char abuf[3 * sizeof(__u64)];
4922 char rbuf[3 * sizeof(__u64)];
4929 if (flags & MNTDF_INODES) {
4930 avail = stat->os_ffree;
4931 used = stat->os_files - stat->os_ffree;
4932 total = stat->os_files;
4934 int shift = flags & MNTDF_COOKED ? 0 : 10;
4936 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4937 used = ((stat->os_blocks - stat->os_bfree) *
4938 stat->os_bsize) >> shift;
4939 total = (stat->os_blocks * stat->os_bsize) >> shift;
4942 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4944 if (flags & MNTDF_COOKED) {
4948 cook_val = (double)total;
4951 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4954 snprintf(tbuf, sizeof(tbuf), CDF, total);
4956 cook_val = (double)used;
4959 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4962 snprintf(ubuf, sizeof(ubuf), CDF, used);
4964 cook_val = (double)avail;
4967 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4970 snprintf(abuf, sizeof(abuf), CDF, avail);
4972 snprintf(tbuf, sizeof(tbuf), CDF, total);
4973 snprintf(ubuf, sizeof(tbuf), CDF, used);
4974 snprintf(abuf, sizeof(tbuf), CDF, avail);
4977 sprintf(rbuf, RDF, ratio);
4978 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4979 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4981 printf("[%s:%d]", type, index);
4983 if (stat->os_state) {
4988 for (i = 0, state = stat->os_state; state != 0; i++) {
4989 uint32_t mask = 1 << i;
4990 if (!(state & mask))
4992 printf("%c", obd_statfs_state2char(mask));
5000 printf(UUF": inactive device\n", uuid);
5003 printf(UUF": %s\n", uuid, strerror(-rc));
5010 struct ll_stat_type {
5015 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5017 struct ll_statfs_data {
5019 struct obd_statfs sd_st;
5022 struct ll_statfs_buf {
5024 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5027 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5028 int ops, struct ll_statfs_buf *lsb)
5030 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5031 struct obd_uuid uuid_buf;
5032 char *poolname = NULL;
5033 struct ll_stat_type types[] = {
5034 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5035 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5036 { .st_name = NULL } };
5037 struct ll_stat_type *tp;
5038 __u64 ost_files = 0;
5039 __u64 ost_ffree = 0;
5047 poolname = strchr(pool, '.');
5048 if (poolname != NULL) {
5049 if (strncmp(fsname, pool, strlen(fsname))) {
5050 fprintf(stderr, "filesystem name incorrect\n");
5058 fd = open(mntdir, O_RDONLY);
5061 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5066 if (flags & MNTDF_SHOW) {
5067 if (flags & MNTDF_INODES)
5068 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5069 "UUID", "Inodes", "IUsed", "IFree",
5070 "IUse%", "Mounted on");
5072 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5074 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5075 "Used", "Available", "Use%", "Mounted on");
5078 for (tp = types; tp->st_name != NULL; tp++) {
5079 bool have_ost = false;
5081 if (!(tp->st_op & ops))
5084 for (index = 0; ; index++) {
5085 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5086 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5087 type = flags & MNTDF_LAZY ?
5088 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5089 rc2 = llapi_obd_fstatfs(fd, type, index,
5090 &stat_buf, &uuid_buf);
5095 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5096 if (!(flags & MNTDF_VERBOSE))
5098 } else if (rc2 < 0 && rc == 0) {
5102 /* If we have OSTs then don't report MDT block counts.
5103 * For MDT-only filesystems the expectation is that all
5104 * layouts have a DoM component. For filesystems with
5105 * OSTs, files are not necessarily going to store data
5106 * on MDTs, and MDT space is limited to a fraction of
5107 * OST space, so don't include it in the summary.
5109 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5116 if (poolname && tp->st_op == LL_STATFS_LOV &&
5117 llapi_search_ost(fsname, poolname,
5118 obd_uuid2str(&uuid_buf)) != 1)
5121 /* the llapi_obd_statfs() call may have returned with
5122 * an error, but if it filled in uuid_buf we will at
5123 * lease use that to print out a message for that OBD.
5124 * If we didn't get anything in the uuid_buf, then fill
5125 * it in so that we can print an error message. */
5126 if (uuid_buf.uuid[0] == '\0')
5127 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5128 "%s%04x", tp->st_name, index);
5130 lsb->sb_buf[lsb->sb_count].sd_index = index;
5131 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5134 if (flags & MNTDF_SHOW)
5135 showdf(mntdir, &stat_buf,
5136 obd_uuid2str(&uuid_buf), flags,
5137 tp->st_name, index, rc2);
5142 if (tp->st_op == LL_STATFS_LMV) {
5143 sum.os_ffree += stat_buf.os_ffree;
5144 sum.os_files += stat_buf.os_files;
5145 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5146 ost_files += stat_buf.os_files;
5147 ost_ffree += stat_buf.os_ffree;
5149 sum.os_blocks += stat_buf.os_blocks *
5151 sum.os_bfree += stat_buf.os_bfree *
5153 sum.os_bavail += stat_buf.os_bavail *
5160 /* If we have _some_ OSTs, but don't have as many free objects on the
5161 * OST as inodes on the MDTs, reduce the reported number of inodes
5162 * to compensate, so that the "inodes in use" number is correct.
5163 * This should be kept in sync with ll_statfs_internal().
5165 if (ost_files && ost_ffree < sum.os_ffree) {
5166 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5167 sum.os_ffree = ost_ffree;
5169 if (flags & MNTDF_SHOW) {
5171 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5178 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5180 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5182 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5184 int r1 = obd_statfs_ratio(st1, false);
5185 int r2 = obd_statfs_ratio(st2, false);
5186 int64_t result = r1 - r2;
5188 /* if both space usage are above 90, compare free inodes */
5189 if (r1 > 90 && r2 > 90)
5190 result = st2->os_ffree - st1->os_ffree;
5194 else if (result == 0)
5201 static int lfs_setdirstripe(int argc, char **argv)
5204 struct lfs_setstripe_args lsa = { 0 };
5205 struct llapi_stripe_param *param = NULL;
5206 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5209 char *mode_opt = NULL;
5210 bool default_stripe = false;
5211 bool delete = false;
5212 bool auto_distributed = false;
5213 bool foreign_mode = false;
5214 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5215 mode_t previous_mode = 0;
5216 struct ll_statfs_buf *lsb = NULL;
5217 char mntdir[PATH_MAX] = "";
5219 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5220 struct option long_opts[] = {
5221 { .val = 'c', .name = "count", .has_arg = required_argument },
5222 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5223 { .val = 'd', .name = "delete", .has_arg = no_argument },
5224 { .val = 'D', .name = "default", .has_arg = no_argument },
5225 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5226 { .val = LFS_LAYOUT_FLAGS_OPT,
5227 .name = "flags", .has_arg = required_argument },
5228 { .val = LFS_LAYOUT_FOREIGN_OPT,
5229 .name = "foreign", .has_arg = optional_argument},
5230 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5231 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5232 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5233 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5235 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5236 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5238 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5239 { .val = 'i', .name = "index", .has_arg = required_argument },
5241 { .val = 'o', .name = "mode", .has_arg = required_argument },
5242 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5243 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5245 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5246 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5247 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5251 setstripe_args_init(&lsa);
5253 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5254 long_opts, NULL)) >= 0) {
5261 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5264 "%s %s: invalid stripe count '%s'\n",
5265 progname, argv[0], optarg);
5271 default_stripe = true;
5274 default_stripe = true;
5276 case LFS_LAYOUT_FOREIGN_OPT:
5277 if (optarg != NULL) {
5278 /* check pure numeric */
5279 type = strtoul(optarg, &end, 0);
5282 type = check_foreign_type_name(optarg);
5283 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5285 "%s %s: unknown foreign type '%s'\n",
5292 foreign_mode = true;
5294 case LFS_LAYOUT_FLAGS_OPT:
5295 flags = strtoul(optarg, &end, 16);
5298 "%s %s: bad flags '%s'\n",
5299 progname, argv[0], optarg);
5303 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5305 fprintf(stderr, "warning: '--hash-type' and '-t' "
5306 "deprecated, use '--mdt-hash' or '-H' instead\n");
5309 lsa.lsa_pattern = check_hashtype(optarg);
5310 if (lsa.lsa_pattern == 0) {
5312 "%s %s: bad stripe hash type '%s'\n",
5313 progname, argv[0], optarg);
5318 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5321 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5322 if (strcmp(argv[optind - 1], "--index") == 0)
5324 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5327 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
5328 lsa.lsa_nr_tgts = parse_targets(mdts,
5329 sizeof(mdts) / sizeof(__u32),
5330 lsa.lsa_nr_tgts, optarg, NULL);
5331 if (lsa.lsa_nr_tgts < 0) {
5333 "%s %s: invalid MDT target(s) '%s'\n",
5334 progname, argv[0], optarg);
5337 lsa.lsa_pattern = 0;
5339 lsa.lsa_tgts = mdts;
5340 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5341 lsa.lsa_stripe_off = mdts[0];
5343 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5345 fprintf(stderr, "warning: '-m' is deprecated, "
5346 "use '--mode' or '-o' instead\n");
5355 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5356 progname, argv[0], argv[optind - 1]);
5361 if (optind == argc) {
5362 fprintf(stderr, "%s %s: DIR must be specified\n",
5367 if (xattr && !foreign_mode) {
5368 /* only print a warning as this is armless and will be
5372 "%s %s: xattr has been specified for non-foreign layout\n",
5374 } else if (foreign_mode && !xattr) {
5376 "%s %s: xattr must be provided in foreign mode\n",
5381 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5382 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5384 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5389 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5390 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5392 "%s %s: stripe offset and count must be specified\n",
5398 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5399 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5401 "%s %s: cannot specify -d with -c or -i options\n",
5406 if (mode_opt != NULL) {
5407 mode = strtoul(mode_opt, &end, 8);
5410 "%s %s: bad MODE '%s'\n",
5411 progname, argv[0], mode_opt);
5414 previous_mode = umask(0);
5417 /* foreign LMV/dir case */
5419 if (argc > optind + 1) {
5421 "%s %s: cannot specify multiple foreign dirs\n",
5426 dname = argv[optind];
5427 result = llapi_dir_create_foreign(dname, mode, type, flags,
5431 "%s mkdir: can't create foreign dir '%s': %s\n",
5432 progname, dname, strerror(-result));
5437 * initialize stripe parameters, in case param is converted to specific,
5438 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5440 param = calloc(1, offsetof(typeof(*param),
5441 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5442 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5443 if (param == NULL) {
5445 "%s %s: cannot allocate memory for parameters: %s\n",
5446 progname, argv[0], strerror(ENOMEM));
5450 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5451 param->lsp_stripe_count = lsa.lsa_stripe_count;
5452 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5453 param->lsp_stripe_offset = -1;
5455 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5456 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5457 param->lsp_stripe_pattern = lsa.lsa_pattern;
5459 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5460 param->lsp_pool = lsa.lsa_pool_name;
5461 param->lsp_is_specific = false;
5462 if (lsa.lsa_nr_tgts > 1) {
5463 if (lsa.lsa_stripe_count > 0 &&
5464 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5465 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5466 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5467 "match the number of MDTs: %d\n",
5468 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5473 param->lsp_is_specific = true;
5474 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5475 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5478 if (!default_stripe && lsa.lsa_pattern == LMV_HASH_TYPE_SPACE) {
5479 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5485 dname = argv[optind];
5487 if (default_stripe) {
5488 result = llapi_dir_set_default_lmv(dname, param);
5491 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5492 progname, dname, strerror(-result));
5497 * if current \a dname isn't under the same \a mntdir as the
5498 * last one, and the last one was auto-distributed, restore
5501 if (mntdir[0] != '\0' &&
5502 strncmp(dname, mntdir, strlen(mntdir)) &&
5504 param->lsp_is_specific = false;
5505 param->lsp_stripe_offset = -1;
5506 auto_distributed = false;
5510 * TODO: when MDT can allocate object with QoS (LU-9435), below
5511 * code should be removed, instead we should let LMV to allocate
5512 * the starting MDT object, and then let LOD allocate other MDT
5515 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5516 char path[PATH_MAX] = "";
5519 lsb = malloc(sizeof(*lsb));
5527 /* use mntdir for dirname() temporarily */
5528 strncpy(mntdir, dname, sizeof(mntdir));
5529 if (!realpath(dirname(mntdir), path)) {
5532 "error: invalid path '%s': %s\n",
5533 argv[optind], strerror(errno));
5538 result = llapi_search_mounts(path, 0, mntdir, NULL);
5539 if (result < 0 || mntdir[0] == '\0') {
5541 "No suitable Lustre mount found\n");
5545 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5550 if (param->lsp_stripe_count > lsb->sb_count) {
5552 "error: stripe count %d is too big\n",
5553 param->lsp_stripe_count);
5558 qsort(lsb->sb_buf, lsb->sb_count,
5559 sizeof(struct ll_statfs_data),
5560 ll_statfs_data_comp);
5562 auto_distributed = true;
5565 if (auto_distributed) {
5567 int nr = MAX(param->lsp_stripe_count,
5570 /* don't use server whose usage is above 90% */
5571 while (nr != param->lsp_stripe_count &&
5572 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5574 nr = MAX(param->lsp_stripe_count, nr / 2);
5576 /* get \a r between [0, nr) */
5579 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5580 if (param->lsp_stripe_count > 1) {
5583 param->lsp_is_specific = true;
5584 for (; i < param->lsp_stripe_count; i++)
5585 param->lsp_tgts[(i + r) % nr] =
5586 lsb->sb_buf[i].sd_index;
5590 result = llapi_dir_create(dname, mode, param);
5593 "%s setdirstripe: cannot create dir '%s': %s\n",
5594 progname, dname, strerror(-result));
5595 } while (!result && (dname = argv[++optind]));
5597 if (mode_opt != NULL)
5598 umask(previous_mode);
5606 static int lfs_rmentry(int argc, char **argv)
5613 fprintf(stderr, "error: %s: missing dirname\n",
5619 dname = argv[index];
5620 while (dname != NULL) {
5621 result = llapi_direntry_remove(dname);
5623 fprintf(stderr, "error: %s: remove dir entry '%s' "
5624 "failed\n", argv[0], dname);
5627 dname = argv[++index];
5632 static int lfs_mv(int argc, char **argv)
5634 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5635 struct find_param param = {
5642 struct option long_opts[] = {
5643 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5644 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5645 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5648 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5650 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5652 fprintf(stderr, "warning: '-M' deprecated"
5653 ", use '--mdt-index' or '-m' instead\n");
5656 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5658 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5664 param.fp_verbose = VERBOSE_DETAIL;
5667 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5668 progname, argv[optind - 1]);
5673 if (lmu.lum_stripe_offset == -1) {
5674 fprintf(stderr, "%s mv: MDT index must be specified\n",
5679 if (optind >= argc) {
5680 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5685 /* initialize migrate mdt parameters */
5686 param.fp_lmv_md = &lmu;
5687 param.fp_migrate = 1;
5688 rc = llapi_migrate_mdt(argv[optind], ¶m);
5690 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5691 progname, argv[optind], param.fp_mdt_index,
5696 static int lfs_osts(int argc, char **argv)
5698 return lfs_tgts(argc, argv);
5701 static int lfs_mdts(int argc, char **argv)
5703 return lfs_tgts(argc, argv);
5706 static int lfs_df(int argc, char **argv)
5708 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5709 enum mntdf_flags flags = MNTDF_SHOW;
5710 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5711 int c, rc = 0, index = 0;
5712 char fsname[PATH_MAX] = "", *pool_name = NULL;
5713 struct option long_opts[] = {
5714 { .val = 'h', .name = "human-readable",
5715 .has_arg = no_argument },
5716 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5717 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5718 { .val = 'p', .name = "pool", .has_arg = required_argument },
5719 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5722 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5725 flags |= MNTDF_COOKED;
5728 flags |= MNTDF_INODES;
5731 flags |= MNTDF_LAZY;
5737 flags |= MNTDF_VERBOSE;
5743 if (optind < argc && !realpath(argv[optind], path)) {
5745 fprintf(stderr, "error: invalid path '%s': %s\n",
5746 argv[optind], strerror(-rc));
5750 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5751 /* Check if we have a mount point */
5752 if (mntdir[0] == '\0')
5755 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5756 if (rc || path[0] != '\0')
5758 fsname[0] = '\0'; /* avoid matching in next loop */
5759 mntdir[0] = '\0'; /* avoid matching in next loop */
5765 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5766 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5770 /* llapi_search_mounts() fills "fsname", but that is not called if
5771 * explicit paths are specified on the command-line
5773 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5774 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5777 fprintf(stderr, "cannot get instance for '%s': %s\n",
5778 mntdir, strerror(-rc));
5782 if (!opt_instance) {
5783 /* print only the fsname name */
5784 buf = strchr(fsname, '-');
5788 } else if (!opt_fsname) {
5789 /* print only the instance name */
5790 buf = strchr(fsname, '-');
5796 } else if (opt_fsname) {
5797 /* print only the fsname */
5801 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5806 static int lfs_getname(int argc, char **argv)
5808 struct option long_opts[] = {
5809 { .val = 'h', .name = "help", .has_arg = no_argument },
5810 { .val = 'i', .name = "instance", .has_arg = no_argument },
5811 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5813 bool opt_instance = false, opt_fsname = false;
5814 char fsname[PATH_MAX] = "";
5817 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5820 opt_instance = true;
5831 /* If neither option is given, print both instance and fsname */
5832 if (!opt_instance && !opt_fsname)
5833 opt_instance = opt_fsname = true;
5835 if (optind == argc) { /* no paths specified, get all paths. */
5836 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5839 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5840 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5841 opt_instance, opt_fsname, true);
5844 path[0] = fsname[0] = mntdir[0] = '\0';
5846 } else { /* paths specified, only attempt to search these. */
5849 /* if only one path is given, print only requested info */
5850 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5852 for (; optind < argc; optind++) {
5853 rc2 = print_instance(argv[optind], fsname,
5854 sizeof(fsname), opt_instance,
5855 opt_fsname, opt_mntdir);
5865 static int lfs_check(int argc, char **argv)
5867 char mntdir[PATH_MAX] = {'\0'};
5876 fprintf(stderr, "%s check: server type must be specified\n",
5881 obd_types[0] = obd_type1;
5882 obd_types[1] = obd_type2;
5883 obd_types[2] = obd_type3;
5885 if (strcmp(argv[1], "osts") == 0) {
5886 strcpy(obd_types[0], "osc");
5887 } else if (strcmp(argv[1], "mdts") == 0 ||
5888 strcmp(argv[1], "mds") == 0) {
5889 strcpy(obd_types[0], "mdc");
5890 } else if (strcmp(argv[1], "mgts") == 0) {
5891 strcpy(obd_types[0], "mgc");
5892 } else if (strcmp(argv[1], "all") == 0 ||
5893 strcmp(argv[1], "servers") == 0) {
5895 strcpy(obd_types[0], "osc");
5896 strcpy(obd_types[1], "mdc");
5897 strcpy(obd_types[2], "mgc");
5899 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5904 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5905 if (rc < 0 || mntdir[0] == '\0') {
5907 "%s check: cannot find mounted Lustre filesystem: %s\n",
5908 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5912 rc = llapi_target_check(num_types, obd_types, mntdir);
5914 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5915 progname, argv[1], strerror(-rc));
5921 #ifdef HAVE_SYS_QUOTA_H
5922 #define ARG2INT(nr, str, msg) \
5925 nr = strtol(str, &endp, 0); \
5926 if (*endp != '\0') { \
5927 fprintf(stderr, "%s: bad %s '%s'\n", \
5928 progname, msg, str); \
5933 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5935 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5936 * returns the value or ULONG_MAX on integer overflow or incorrect format
5938 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5939 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5940 * 3. empty integer value is interpreted as 0
5942 static unsigned long str2sec(const char* timestr)
5944 const char spec[] = "smhdw";
5945 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5946 unsigned long val = 0;
5949 if (strpbrk(timestr, spec) == NULL) {
5950 /* no specifiers inside the time string,
5951 should treat it as an integer value */
5952 val = strtoul(timestr, &tail, 10);
5953 return *tail ? ULONG_MAX : val;
5956 /* format string is XXwXXdXXhXXmXXs */
5962 v = strtoul(timestr, &tail, 10);
5963 if (v == ULONG_MAX || *tail == '\0')
5964 /* value too large (ULONG_MAX or more)
5965 or missing specifier */
5968 ptr = strchr(spec, *tail);
5970 /* unknown specifier */
5975 /* check if product will overflow the type */
5976 if (!(v < ULONG_MAX / mult[ind]))
5979 ADD_OVERFLOW(val, mult[ind] * v);
5980 if (val == ULONG_MAX)
5992 #define ARG2ULL(nr, str, def_units) \
5994 unsigned long long limit, units = def_units; \
5997 rc = llapi_parse_size(str, &limit, &units, 1); \
5999 fprintf(stderr, "%s: invalid limit '%s'\n", \
6006 static inline int has_times_option(int argc, char **argv)
6010 for (i = 1; i < argc; i++)
6011 if (!strcmp(argv[i], "-t"))
6017 int lfs_setquota_times(int argc, char **argv)
6020 struct if_quotactl qctl;
6021 char *mnt, *obd_type = (char *)qctl.obd_type;
6022 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6023 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6024 struct option long_opts[] = {
6025 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6026 { .val = 'g', .name = "group", .has_arg = no_argument },
6027 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6028 { .val = 'p', .name = "projid", .has_arg = no_argument },
6029 { .val = 't', .name = "times", .has_arg = no_argument },
6030 { .val = 'u', .name = "user", .has_arg = no_argument },
6034 memset(&qctl, 0, sizeof(qctl));
6035 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6036 qctl.qc_type = ALLQUOTA;
6038 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6039 long_opts, NULL)) != -1) {
6050 if (qctl.qc_type != ALLQUOTA) {
6051 fprintf(stderr, "error: -u/g/p can't be used "
6052 "more than once\n");
6055 qctl.qc_type = qtype;
6058 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6059 fprintf(stderr, "error: bad block-grace: %s\n",
6063 dqb->dqb_valid |= QIF_BTIME;
6066 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6067 fprintf(stderr, "error: bad inode-grace: %s\n",
6071 dqb->dqb_valid |= QIF_ITIME;
6073 case 't': /* Yes, of course! */
6075 default: /* getopt prints error message for us when opterr != 0 */
6080 if (qctl.qc_type == ALLQUOTA) {
6081 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6085 if (optind != argc - 1) {
6086 fprintf(stderr, "error: unexpected parameters encountered\n");
6090 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6091 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6092 fprintf(stderr, "error: grace time is too large\n");
6097 rc = llapi_quotactl(mnt, &qctl);
6100 fprintf(stderr, "%s %s ", obd_type,
6101 obd_uuid2str(&qctl.obd_uuid));
6102 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6109 #define BSLIMIT (1 << 0)
6110 #define BHLIMIT (1 << 1)
6111 #define ISLIMIT (1 << 2)
6112 #define IHLIMIT (1 << 3)
6114 int lfs_setquota(int argc, char **argv)
6117 struct if_quotactl qctl;
6118 char *mnt, *obd_type = (char *)qctl.obd_type;
6119 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6120 struct option long_opts[] = {
6121 { .val = 'b', .name = "block-softlimit",
6122 .has_arg = required_argument },
6123 { .val = 'B', .name = "block-hardlimit",
6124 .has_arg = required_argument },
6125 { .val = 'd', .name = "default", .has_arg = no_argument },
6126 { .val = 'g', .name = "group", .has_arg = required_argument },
6127 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6128 { .val = 'i', .name = "inode-softlimit",
6129 .has_arg = required_argument },
6130 { .val = 'I', .name = "inode-hardlimit",
6131 .has_arg = required_argument },
6132 { .val = 'p', .name = "projid", .has_arg = required_argument },
6133 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6134 { .val = 'u', .name = "user", .has_arg = required_argument },
6135 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6137 unsigned limit_mask = 0;
6139 bool use_default = false;
6142 if (has_times_option(argc, argv))
6143 return lfs_setquota_times(argc, argv);
6145 memset(&qctl, 0, sizeof(qctl));
6146 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6147 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6148 * so it can be used as a marker that qc_type
6149 * isn't reinitialized from command line */
6151 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6152 long_opts, NULL)) != -1) {
6155 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6158 goto quota_type_def;
6161 rc = name2uid(&qctl.qc_id, optarg);
6164 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6167 goto quota_type_def;
6170 rc = name2gid(&qctl.qc_id, optarg);
6173 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6176 goto quota_type_def;
6179 rc = name2projid(&qctl.qc_id, optarg);
6182 qctl.qc_id = strtoul(optarg, &endptr, 10);
6183 if (*endptr != '\0') {
6184 fprintf(stderr, "%s setquota: invalid"
6185 " id '%s'\n", progname, optarg);
6190 if (qctl.qc_id == 0) {
6191 fprintf(stderr, "%s setquota: can't set quota"
6192 " for root usr/group/project.\n",
6198 if (qctl.qc_type != ALLQUOTA) {
6200 "%s setquota: only one of -u, -U, -g,"
6201 " -G, -p or -P may be specified\n",
6205 qctl.qc_type = qtype;
6208 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6212 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6213 dqb->dqb_bsoftlimit >>= 10;
6214 limit_mask |= BSLIMIT;
6215 if (dqb->dqb_bsoftlimit &&
6216 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6218 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6219 "See '%s help setquota' or Lustre manual for details\n",
6220 progname, dqb->dqb_bsoftlimit,
6224 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6225 dqb->dqb_bhardlimit >>= 10;
6226 limit_mask |= BHLIMIT;
6227 if (dqb->dqb_bhardlimit &&
6228 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6230 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6231 "See '%s help setquota' or Lustre manual for details\n",
6232 progname, dqb->dqb_bhardlimit,
6236 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6237 limit_mask |= ISLIMIT;
6238 if (dqb->dqb_isoftlimit &&
6239 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6241 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6242 "See '%s help setquota' or Lustre manual for details\n",
6243 progname, dqb->dqb_isoftlimit,
6247 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6248 limit_mask |= IHLIMIT;
6249 if (dqb->dqb_ihardlimit &&
6250 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6252 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6253 "See '%s help setquota' or Lustre manual for details\n",
6254 progname, dqb->dqb_ihardlimit,
6259 "%s setquota: unrecognized option '%s'\n",
6260 progname, argv[optind - 1]);
6265 if (qctl.qc_type == ALLQUOTA) {
6267 "%s setquota: either -u or -g must be specified\n",
6272 if (!use_default && limit_mask == 0) {
6274 "%s setquota: at least one limit must be specified\n",
6279 if (use_default && limit_mask != 0) {
6281 "%s setquota: limits should not be specified when"
6282 " using default quota\n",
6287 if (use_default && qctl.qc_id == 0) {
6289 "%s setquota: can not set default quota for root"
6290 " user/group/project\n",
6295 if (optind != argc - 1) {
6297 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6298 progname, argv[optind]);
6305 dqb->dqb_bhardlimit = 0;
6306 dqb->dqb_bsoftlimit = 0;
6307 dqb->dqb_ihardlimit = 0;
6308 dqb->dqb_isoftlimit = 0;
6311 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6312 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6313 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6314 /* sigh, we can't just set blimits/ilimits */
6315 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6316 .qc_type = qctl.qc_type,
6317 .qc_id = qctl.qc_id};
6319 rc = llapi_quotactl(mnt, &tmp_qctl);
6323 if (!(limit_mask & BHLIMIT))
6324 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6325 if (!(limit_mask & BSLIMIT))
6326 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6327 if (!(limit_mask & IHLIMIT))
6328 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6329 if (!(limit_mask & ISLIMIT))
6330 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6332 /* Keep grace times if we have got no softlimit arguments */
6333 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6334 dqb->dqb_valid |= QIF_BTIME;
6335 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6338 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6339 dqb->dqb_valid |= QIF_ITIME;
6340 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6344 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6345 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6347 rc = llapi_quotactl(mnt, &qctl);
6351 "%s setquota: cannot quotactl '%s' '%s': %s",
6353 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6360 /* Converts seconds value into format string
6361 * result is returned in buf
6363 * 1. result is in descenting order: 1w2d3h4m5s
6364 * 2. zero fields are not filled (except for p. 3): 5d1s
6365 * 3. zero seconds value is presented as "0s"
6367 static char * __sec2str(time_t seconds, char *buf)
6369 const char spec[] = "smhdw";
6370 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6375 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6376 c = seconds / mult[i];
6378 if (c > 0 || (i == 0 && buf == tail))
6379 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6387 static void sec2str(time_t seconds, char *buf, int rc)
6394 tail = __sec2str(seconds, tail);
6396 if (rc && tail - buf < 39) {
6402 static void diff2str(time_t seconds, char *buf, time_t now)
6408 if (seconds <= now) {
6409 strcpy(buf, "none");
6412 __sec2str(seconds - now, buf);
6415 static void print_quota_title(char *name, struct if_quotactl *qctl,
6416 bool human_readable, bool show_default)
6419 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6420 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6421 "Filesystem", "bquota", "blimit", "bgrace",
6422 "iquota", "ilimit", "igrace");
6424 printf("Disk quotas for %s %s (%cid %u):\n",
6425 qtype_name(qctl->qc_type), name,
6426 *qtype_name(qctl->qc_type), qctl->qc_id);
6427 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6428 "Filesystem", human_readable ? "used" : "kbytes",
6429 "quota", "limit", "grace",
6430 "files", "quota", "limit", "grace");
6434 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6437 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6440 snprintf(buf, buflen, "%5.4gP",
6441 (double)num / ((__u64)1 << 40));
6443 snprintf(buf, buflen, "%5.4gT",
6444 (double)num / (1 << 30));
6446 snprintf(buf, buflen, "%5.4gG",
6447 (double)num / (1 << 20));
6449 snprintf(buf, buflen, "%5.4gM",
6450 (double)num / (1 << 10));
6452 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6456 #define STRBUF_LEN 32
6457 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6458 int rc, bool h, bool show_default)
6464 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6465 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6466 int bover = 0, iover = 0;
6467 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6468 char numbuf[3][STRBUF_LEN];
6470 char strbuf[STRBUF_LEN];
6472 if (dqb->dqb_bhardlimit &&
6473 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6475 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6476 if (dqb->dqb_btime > now) {
6483 if (dqb->dqb_ihardlimit &&
6484 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6486 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6487 if (dqb->dqb_itime > now) {
6495 if (strlen(mnt) > 15)
6496 printf("%s\n%15s", mnt, "");
6498 printf("%15s", mnt);
6501 diff2str(dqb->dqb_btime, timebuf, now);
6502 else if (show_default)
6503 snprintf(timebuf, sizeof(timebuf), "%llu",
6506 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6507 strbuf, sizeof(strbuf), h);
6508 if (rc == -EREMOTEIO)
6509 sprintf(numbuf[0], "%s*", strbuf);
6511 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6512 "%s" : "[%s]", strbuf);
6514 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6515 if (type == QC_GENERAL)
6516 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6517 "%s" : "[%s]", strbuf);
6519 sprintf(numbuf[1], "%s", "-");
6521 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6522 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6523 "%s" : "[%s]", strbuf);
6526 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6528 printf(" %7s%c %6s %7s %7s",
6529 numbuf[0], bover ? '*' : ' ', numbuf[1],
6530 numbuf[2], bover > 1 ? timebuf : "-");
6534 diff2str(dqb->dqb_itime, timebuf, now);
6535 else if (show_default)
6536 snprintf(timebuf, sizeof(timebuf), "%llu",
6539 snprintf(numbuf[0], sizeof(numbuf),
6540 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6541 (uintmax_t)dqb->dqb_curinodes);
6543 if (type == QC_GENERAL)
6544 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6546 (uintmax_t)dqb->dqb_isoftlimit);
6548 sprintf(numbuf[1], "%s", "-");
6550 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6551 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6554 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6555 else if (type != QC_OSTIDX)
6556 printf(" %7s%c %6s %7s %7s",
6557 numbuf[0], iover ? '*' : ' ', numbuf[1],
6558 numbuf[2], iover > 1 ? timebuf : "-");
6560 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6563 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6564 qctl->qc_cmd == Q_GETOINFO) {
6568 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6569 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6570 printf("Block grace time: %s; Inode grace time: %s\n",
6571 bgtimebuf, igtimebuf);
6575 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6576 bool h, __u64 *total)
6578 int rc = 0, rc1 = 0, count = 0;
6579 __u32 valid = qctl->qc_valid;
6581 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6583 fprintf(stderr, "can not get %s count: %s\n",
6584 is_mdt ? "mdt": "ost", strerror(-rc));
6588 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6589 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6590 rc = llapi_quotactl(mnt, qctl);
6592 /* It is remote client case. */
6593 if (rc == -EOPNOTSUPP) {
6600 fprintf(stderr, "quotactl %s%d failed.\n",
6601 is_mdt ? "mdt": "ost", qctl->qc_idx);
6605 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6606 qctl->qc_valid, 0, h, false);
6607 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6608 qctl->qc_dqblk.dqb_bhardlimit;
6611 qctl->qc_valid = valid;
6615 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6616 int verbose, int quiet, bool human_readable,
6619 int rc1 = 0, rc2 = 0, rc3 = 0;
6620 char *obd_type = (char *)qctl->obd_type;
6621 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6622 __u64 total_ialloc = 0, total_balloc = 0;
6623 bool use_default_for_blk = false;
6624 bool use_default_for_file = false;
6627 rc1 = llapi_quotactl(mnt, qctl);
6631 fprintf(stderr, "%s quotas are not enabled.\n",
6632 qtype_name(qctl->qc_type));
6635 fprintf(stderr, "Permission denied.\n");
6638 /* We already got error message. */
6641 fprintf(stderr, "Unexpected quotactl error: %s\n",
6646 if (!show_default && qctl->qc_id == 0) {
6647 qctl->qc_dqblk.dqb_bhardlimit = 0;
6648 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6649 qctl->qc_dqblk.dqb_ihardlimit = 0;
6650 qctl->qc_dqblk.dqb_isoftlimit = 0;
6651 qctl->qc_dqblk.dqb_btime = 0;
6652 qctl->qc_dqblk.dqb_itime = 0;
6653 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6656 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6657 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6658 use_default_for_blk = true;
6659 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6662 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6663 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6664 use_default_for_file = true;
6665 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6668 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6669 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6670 print_quota_title(name, qctl, human_readable, show_default);
6672 if (rc1 && *obd_type)
6673 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6675 if (qctl->qc_valid != QC_GENERAL)
6678 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6679 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6680 (QIF_LIMITS|QIF_USAGE));
6682 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6684 if (!show_default && verbose &&
6685 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6686 char strbuf[STRBUF_LEN];
6688 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6690 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6692 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6694 printf("Total allocated inode limit: %ju, total "
6695 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6699 if (use_default_for_blk)
6700 printf("%cid %u is using default block quota setting\n",
6701 *qtype_name(qctl->qc_type), qctl->qc_id);
6703 if (use_default_for_file)
6704 printf("%cid %u is using default file quota setting\n",
6705 *qtype_name(qctl->qc_type), qctl->qc_id);
6707 if (rc1 || rc2 || rc3 || inacc)
6708 printf("Some errors happened when getting quota info. "
6709 "Some devices may be not working or deactivated. "
6710 "The data in \"[]\" is inaccurate.\n");
6716 static int lfs_project(int argc, char **argv)
6718 int ret = 0, err = 0, c, i;
6719 struct project_handle_control phc = { 0 };
6720 enum lfs_project_ops_t op;
6723 phc.assign_projid = false;
6724 /* default action */
6725 op = LFS_PROJECT_LIST;
6727 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6730 if (op != LFS_PROJECT_LIST) {
6732 "%s: cannot specify '-c' '-C' '-s' together\n",
6737 op = LFS_PROJECT_CHECK;
6740 if (op != LFS_PROJECT_LIST) {
6742 "%s: cannot specify '-c' '-C' '-s' together\n",
6747 op = LFS_PROJECT_CLEAR;
6750 if (op != LFS_PROJECT_LIST) {
6752 "%s: cannot specify '-c' '-C' '-s' together\n",
6757 phc.set_inherit = true;
6758 op = LFS_PROJECT_SET;
6764 phc.keep_projid = true;
6767 phc.recursive = true;
6770 phc.projid = strtoul(optarg, NULL, 0);
6771 phc.assign_projid = true;
6775 phc.newline = false;
6778 fprintf(stderr, "%s: invalid option '%c'\n",
6784 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6785 op = LFS_PROJECT_SET;
6786 phc.set_projid = true;
6787 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6788 phc.set_projid = true;
6792 case LFS_PROJECT_CHECK:
6793 if (phc.keep_projid) {
6795 "%s: '-k' is useless together with '-c'\n",
6800 case LFS_PROJECT_CLEAR:
6803 "%s: '-0' is useless together with '-C'\n",
6807 if (phc.assign_projid) {
6809 "%s: '-p' is useless together with '-C'\n",
6814 case LFS_PROJECT_SET:
6817 "%s: '-0' is useless together with '-s'\n",
6821 if (phc.keep_projid) {
6823 "%s: '-k' is useless together with '-s'\n",
6831 "%s: '-0' is useless for list operations\n",
6841 fprintf(stderr, "%s: missing file or directory target(s)\n",
6846 for (i = 0; i < argc; i++) {
6848 case LFS_PROJECT_CHECK:
6849 err = lfs_project_check(argv[i], &phc);
6851 case LFS_PROJECT_LIST:
6852 err = lfs_project_list(argv[i], &phc);
6854 case LFS_PROJECT_CLEAR:
6855 err = lfs_project_clear(argv[i], &phc);
6857 case LFS_PROJECT_SET:
6858 err = lfs_project_set(argv[i], &phc);
6870 static int lfs_quota(int argc, char **argv)
6873 char *mnt, *name = NULL;
6874 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6875 .qc_type = ALLQUOTA };
6876 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6877 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6879 __u32 valid = QC_GENERAL, idx = 0;
6880 bool human_readable = false;
6881 bool show_default = false;
6884 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6887 show_default = true;
6892 show_default = true;
6897 show_default = true;
6901 if (qctl.qc_type != ALLQUOTA) {
6903 "%s quota: only one of -u, -g, or -p may be specified\n",
6907 qctl.qc_type = qtype;
6910 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6913 valid = qctl.qc_valid = QC_UUID;
6914 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6917 valid = qctl.qc_valid = QC_MDTIDX;
6918 idx = qctl.qc_idx = atoi(optarg);
6919 if (idx == 0 && *optarg != '0') {
6921 "%s quota: invalid MDT index '%s'\n",
6927 valid = qctl.qc_valid = QC_OSTIDX;
6928 idx = qctl.qc_idx = atoi(optarg);
6929 if (idx == 0 && *optarg != '0') {
6931 "%s quota: invalid OST index '%s'\n",
6943 human_readable = true;
6946 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6947 progname, argv[optind - 1]);
6952 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6953 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6954 optind == argc - 1 && !show_default) {
6956 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6957 qctl.qc_valid = valid;
6960 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6961 qctl.qc_type = qtype;
6962 if (qtype == USRQUOTA) {
6963 qctl.qc_id = geteuid();
6964 rc = uid2name(&name, qctl.qc_id);
6966 qctl.qc_id = getegid();
6967 rc = gid2name(&name, qctl.qc_id);
6972 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6973 human_readable, show_default);
6978 /* lfs quota -u username /path/to/lustre/mount */
6979 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6980 /* options should be followed by u/g-name and mntpoint */
6981 if ((!show_default && optind + 2 != argc) ||
6982 (show_default && optind + 1 != argc) ||
6983 qctl.qc_type == ALLQUOTA) {
6985 "%s quota: name and mount point must be specified\n",
6990 if (!show_default) {
6991 name = argv[optind++];
6992 switch (qctl.qc_type) {
6994 rc = name2uid(&qctl.qc_id, name);
6997 rc = name2gid(&qctl.qc_id, name);
7000 rc = name2projid(&qctl.qc_id, name);
7007 qctl.qc_valid = QC_GENERAL;
7008 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7013 qctl.qc_id = strtoul(name, &endptr, 10);
7014 if (*endptr != '\0') {
7015 fprintf(stderr, "%s quota: invalid id '%s'\n",
7020 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7021 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7027 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7028 human_readable, show_default);
7031 #endif /* HAVE_SYS_QUOTA_H! */
7033 static int flushctx_ioctl(char *mp)
7037 fd = open(mp, O_RDONLY);
7039 fprintf(stderr, "flushctx: error open %s: %s\n",
7040 mp, strerror(errno));
7044 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7046 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7047 mp, strerror(errno));
7053 static int lfs_flushctx(int argc, char **argv)
7055 int kdestroy = 0, c;
7056 char mntdir[PATH_MAX] = {'\0'};
7060 while ((c = getopt(argc, argv, "k")) != -1) {
7066 fprintf(stderr, "error: %s: option '-%c' "
7067 "unrecognized\n", argv[0], c);
7073 if ((rc = system("kdestroy > /dev/null")) != 0) {
7074 rc = WEXITSTATUS(rc);
7075 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7079 if (optind >= argc) {
7080 /* flush for all mounted lustre fs. */
7081 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7082 /* Check if we have a mount point */
7083 if (mntdir[0] == '\0')
7086 if (flushctx_ioctl(mntdir))
7089 mntdir[0] = '\0'; /* avoid matching in next loop */
7092 /* flush fs as specified */
7093 while (optind < argc) {
7094 if (flushctx_ioctl(argv[optind++]))
7101 static int lfs_changelog(int argc, char **argv)
7103 void *changelog_priv;
7104 struct changelog_rec *rec;
7105 long long startrec = 0, endrec = 0;
7107 struct option long_opts[] = {
7108 { .val = 'f', .name = "follow", .has_arg = no_argument },
7110 char short_opts[] = "f";
7113 while ((rc = getopt_long(argc, argv, short_opts,
7114 long_opts, NULL)) != -1) {
7121 "%s changelog: unrecognized option '%s'\n",
7122 progname, argv[optind - 1]);
7126 if (optind >= argc) {
7127 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7132 mdd = argv[optind++];
7134 startrec = strtoll(argv[optind++], NULL, 10);
7136 endrec = strtoll(argv[optind++], NULL, 10);
7138 rc = llapi_changelog_start(&changelog_priv,
7139 CHANGELOG_FLAG_BLOCK |
7140 CHANGELOG_FLAG_JOBID |
7141 CHANGELOG_FLAG_EXTRA_FLAGS |
7142 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7145 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7146 progname, strerror(errno = -rc));
7150 rc = llapi_changelog_set_xflags(changelog_priv,
7151 CHANGELOG_EXTRA_FLAG_UIDGID |
7152 CHANGELOG_EXTRA_FLAG_NID |
7153 CHANGELOG_EXTRA_FLAG_OMODE |
7154 CHANGELOG_EXTRA_FLAG_XATTR);
7157 "%s changelog: cannot set xflags for changelog: %s\n",
7158 progname, strerror(errno = -rc));
7162 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7166 if (endrec && rec->cr_index > endrec) {
7167 llapi_changelog_free(&rec);
7170 if (rec->cr_index < startrec) {
7171 llapi_changelog_free(&rec);
7175 secs = rec->cr_time >> 30;
7176 gmtime_r(&secs, &ts);
7177 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7178 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7179 changelog_type2str(rec->cr_type),
7180 ts.tm_hour, ts.tm_min, ts.tm_sec,
7181 (int)(rec->cr_time & ((1 << 30) - 1)),
7182 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7183 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7185 if (rec->cr_flags & CLF_JOBID) {
7186 struct changelog_ext_jobid *jid =
7187 changelog_rec_jobid(rec);
7189 if (jid->cr_jobid[0] != '\0')
7190 printf(" j=%s", jid->cr_jobid);
7193 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7194 struct changelog_ext_extra_flags *ef =
7195 changelog_rec_extra_flags(rec);
7197 printf(" ef=0x%llx", ef->cr_extra_flags);
7199 if (ef->cr_extra_flags & CLFE_UIDGID) {
7200 struct changelog_ext_uidgid *uidgid =
7201 changelog_rec_uidgid(rec);
7203 printf(" u=%llu:%llu",
7204 uidgid->cr_uid, uidgid->cr_gid);
7206 if (ef->cr_extra_flags & CLFE_NID) {
7207 struct changelog_ext_nid *nid =
7208 changelog_rec_nid(rec);
7211 libcfs_nid2str(nid->cr_nid));
7214 if (ef->cr_extra_flags & CLFE_OPEN) {
7215 struct changelog_ext_openmode *omd =
7216 changelog_rec_openmode(rec);
7217 char mode[] = "---";
7219 /* exec mode must be exclusive */
7220 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7223 if (omd->cr_openflags & MDS_FMODE_READ)
7225 if (omd->cr_openflags &
7232 if (strcmp(mode, "---") != 0)
7233 printf(" m=%s", mode);
7237 if (ef->cr_extra_flags & CLFE_XATTR) {
7238 struct changelog_ext_xattr *xattr =
7239 changelog_rec_xattr(rec);
7241 if (xattr->cr_xattr[0] != '\0')
7242 printf(" x=%s", xattr->cr_xattr);
7246 if (rec->cr_namelen)
7247 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7248 rec->cr_namelen, changelog_rec_name(rec));
7250 if (rec->cr_flags & CLF_RENAME) {
7251 struct changelog_ext_rename *rnm =
7252 changelog_rec_rename(rec);
7254 if (!fid_is_zero(&rnm->cr_sfid))
7255 printf(" s="DFID" sp="DFID" %.*s",
7256 PFID(&rnm->cr_sfid),
7257 PFID(&rnm->cr_spfid),
7258 (int)changelog_rec_snamelen(rec),
7259 changelog_rec_sname(rec));
7263 llapi_changelog_free(&rec);
7266 llapi_changelog_fini(&changelog_priv);
7269 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7270 progname, strerror(errno = -rc));
7272 return (rc == 1 ? 0 : rc);
7275 static int lfs_changelog_clear(int argc, char **argv)
7283 endrec = strtoll(argv[3], NULL, 10);
7285 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7288 fprintf(stderr, "%s: record out of range: %llu\n",
7290 else if (rc == -ENOENT)
7291 fprintf(stderr, "%s: no changelog user: %s\n",
7294 fprintf(stderr, "%s error: %s\n", argv[0],
7303 static int lfs_fid2path(int argc, char **argv)
7305 struct option long_opts[] = {
7306 { .val = 'c', .name = "cur", .has_arg = no_argument },
7307 { .val = 'l', .name = "link", .has_arg = required_argument },
7308 { .val = 'r', .name = "rec", .has_arg = required_argument },
7310 char short_opts[] = "cl:r:";
7311 char *device, *fid, *path;
7312 long long recno = -1;
7317 char *endptr = NULL;
7319 while ((rc = getopt_long(argc, argv, short_opts,
7320 long_opts, NULL)) != -1) {
7326 linkno = strtol(optarg, &endptr, 10);
7327 if (*endptr != '\0') {
7329 "%s fid2path: invalid linkno '%s'\n",
7335 recno = strtoll(optarg, &endptr, 10);
7336 if (*endptr != '\0') {
7338 "%s fid2path: invalid recno '%s'\n",
7345 "%s fid2path: unrecognized option '%s'\n",
7346 progname, argv[optind - 1]);
7353 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7358 device = argv[optind++];
7359 path = calloc(1, PATH_MAX);
7363 "%s fid2path: cannot allocate memory for path: %s\n",
7364 progname, strerror(-rc));
7369 while (optind < argc) {
7370 fid = argv[optind++];
7372 lnktmp = (linkno >= 0) ? linkno : 0;
7374 int oldtmp = lnktmp;
7375 long long rectmp = recno;
7377 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7381 "%s fid2path: cannot find '%s': %s\n",
7382 progname, fid, strerror(errno = -rc2));
7389 fprintf(stdout, "%lld ", rectmp);
7390 if (device[0] == '/') {
7391 fprintf(stdout, "%s", device);
7392 if (device[strlen(device) - 1] != '/')
7393 fprintf(stdout, "/");
7394 } else if (path[0] == '\0') {
7395 fprintf(stdout, "/");
7397 fprintf(stdout, "%s\n", path);
7400 /* specified linkno */
7402 if (oldtmp == lnktmp)
7412 static int lfs_path2fid(int argc, char **argv)
7414 struct option long_opts[] = {
7415 { .val = 'p', .name = "parents", .has_arg = no_argument },
7418 const char short_opts[] = "p";
7419 const char *sep = "";
7422 bool show_parents = false;
7424 while ((rc = getopt_long(argc, argv, short_opts,
7425 long_opts, NULL)) != -1) {
7428 show_parents = true;
7432 "%s path2fid: unrecognized option '%s'\n",
7433 progname, argv[optind - 1]);
7438 if (optind > argc - 1) {
7439 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7443 else if (optind < argc - 1)
7447 for (path = argv + optind; *path != NULL; path++) {
7449 if (!show_parents) {
7450 err = llapi_path2fid(*path, &fid);
7452 printf("%s%s"DFID"\n",
7453 *sep != '\0' ? *path : "", sep,
7456 char name[NAME_MAX + 1];
7457 unsigned int linkno = 0;
7459 while ((err = llapi_path2parent(*path, linkno, &fid,
7460 name, sizeof(name))) == 0) {
7461 if (*sep != '\0' && linkno == 0)
7462 printf("%s%s", *path, sep);
7464 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7469 /* err == -ENODATA is end-of-loop */
7470 if (linkno > 0 && err == -ENODATA) {
7478 "%s path2fid: cannot get %sfid for '%s': %s\n",
7479 progname, show_parents ? "parent " : "", *path,
7491 static int lfs_data_version(int argc, char **argv)
7498 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7501 fprintf(stderr, "%s data_version: FILE must be specified\n",
7506 while ((c = getopt(argc, argv, "nrw")) != -1) {
7509 data_version_flags = 0;
7512 data_version_flags |= LL_DV_RD_FLUSH;
7515 data_version_flags |= LL_DV_WR_FLUSH;
7519 "%s data_version: unrecognized option '%s'\n",
7520 progname, argv[optind - 1]);
7524 if (optind == argc) {
7525 fprintf(stderr, "%s data_version: FILE must be specified\n",
7530 path = argv[optind];
7531 fd = open(path, O_RDONLY);
7534 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7535 progname, path, strerror(-rc));
7539 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7542 "%s data_version: cannot get version for '%s': %s\n",
7543 progname, path, strerror(-rc));
7545 printf("%ju" "\n", (uintmax_t)data_version);
7551 static int lfs_hsm_state(int argc, char **argv)
7556 struct hsm_user_state hus;
7564 rc = llapi_hsm_state_get(path, &hus);
7566 fprintf(stderr, "can't get hsm state for %s: %s\n",
7567 path, strerror(errno = -rc));
7571 /* Display path name and status flags */
7572 printf("%s: (0x%08x)", path, hus.hus_states);
7574 if (hus.hus_states & HS_RELEASED)
7575 printf(" released");
7576 if (hus.hus_states & HS_EXISTS)
7578 if (hus.hus_states & HS_DIRTY)
7580 if (hus.hus_states & HS_ARCHIVED)
7581 printf(" archived");
7582 /* Display user-settable flags */
7583 if (hus.hus_states & HS_NORELEASE)
7584 printf(" never_release");
7585 if (hus.hus_states & HS_NOARCHIVE)
7586 printf(" never_archive");
7587 if (hus.hus_states & HS_LOST)
7588 printf(" lost_from_hsm");
7590 if (hus.hus_archive_id != 0)
7591 printf(", archive_id:%d", hus.hus_archive_id);
7594 } while (++i < argc);
7599 #define LFS_HSM_SET 0
7600 #define LFS_HSM_CLEAR 1
7603 * Generic function to set or clear HSM flags.
7604 * Used by hsm_set and hsm_clear.
7606 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7608 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7610 struct option long_opts[] = {
7611 { .val = 'A', .name = "archived", .has_arg = no_argument },
7612 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7613 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7614 { .val = 'e', .name = "exists", .has_arg = no_argument },
7615 { .val = 'l', .name = "lost", .has_arg = no_argument },
7616 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7617 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7619 char short_opts[] = "lraAdei:";
7623 __u32 archive_id = 0;
7629 while ((c = getopt_long(argc, argv, short_opts,
7630 long_opts, NULL)) != -1) {
7636 mask |= HS_NOARCHIVE;
7639 mask |= HS_ARCHIVED;
7642 mask |= HS_NORELEASE;
7651 archive_id = strtol(optarg, &end, 10);
7653 fprintf(stderr, "invalid archive_id: '%s'\n",
7661 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7662 argv[0], argv[optind - 1]);
7667 /* User should have specified a flag */
7671 while (optind < argc) {
7673 path = argv[optind];
7675 /* If mode == 0, this means we apply the mask. */
7676 if (mode == LFS_HSM_SET)
7677 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7679 rc = llapi_hsm_state_set(path, 0, mask, 0);
7682 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7683 path, strerror(errno = -rc));
7692 static int lfs_hsm_action(int argc, char **argv)
7697 struct hsm_current_action hca;
7698 struct hsm_extent he;
7699 enum hsm_user_action hua;
7700 enum hsm_progress_states hps;
7708 rc = llapi_hsm_current_action(path, &hca);
7710 fprintf(stderr, "can't get hsm action for %s: %s\n",
7711 path, strerror(errno = -rc));
7714 he = hca.hca_location;
7715 hua = hca.hca_action;
7716 hps = hca.hca_state;
7718 printf("%s: %s", path, hsm_user_action2name(hua));
7720 /* Skip file without action */
7721 if (hca.hca_action == HUA_NONE) {
7726 printf(" %s ", hsm_progress_state2name(hps));
7728 if ((hps == HPS_RUNNING) &&
7729 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7730 printf("(%llu bytes moved)\n",
7731 (unsigned long long)he.length);
7732 else if ((he.offset + he.length) == LUSTRE_EOF)
7733 printf("(from %llu to EOF)\n",
7734 (unsigned long long)he.offset);
7736 printf("(from %llu to %llu)\n",
7737 (unsigned long long)he.offset,
7738 (unsigned long long)(he.offset + he.length));
7740 } while (++i < argc);
7745 static int lfs_hsm_set(int argc, char **argv)
7747 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7750 static int lfs_hsm_clear(int argc, char **argv)
7752 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7756 * Check file state and return its fid, to be used by lfs_hsm_request().
7758 * \param[in] file Path to file to check
7759 * \param[in,out] fid Pointer to allocated lu_fid struct.
7760 * \param[in,out] last_dev Pointer to last device id used.
7762 * \return 0 on success.
7764 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7770 rc = lstat(file, &st);
7772 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7775 /* Checking for regular file as archiving as posix copytool
7776 * rejects archiving files other than regular files
7778 if (!S_ISREG(st.st_mode)) {
7779 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7782 /* A request should be ... */
7783 if (*last_dev != st.st_dev && *last_dev != 0) {
7784 fprintf(stderr, "All files should be "
7785 "on the same filesystem: %s\n", file);
7788 *last_dev = st.st_dev;
7790 rc = llapi_path2fid(file, fid);
7792 fprintf(stderr, "Cannot read FID of %s: %s\n",
7793 file, strerror(-rc));
7799 /* Fill an HSM HUR item with a given file name.
7801 * If mntpath is set, then the filename is actually a FID, and no
7802 * lookup on the filesystem will be performed.
7804 * \param[in] hur the user request to fill
7805 * \param[in] idx index of the item inside the HUR to fill
7806 * \param[in] mntpath mountpoint of Lustre
7807 * \param[in] fname filename (if mtnpath is NULL)
7808 * or FID (if mntpath is set)
7809 * \param[in] last_dev pointer to last device id used
7811 * \retval 0 on success
7812 * \retval CMD_HELP or a negative errno on error
7814 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7815 const char *mntpath, const char *fname,
7818 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7821 hui->hui_extent.length = -1;
7823 if (mntpath != NULL) {
7826 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7830 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7835 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7839 hur->hur_request.hr_itemcount++;
7844 static int lfs_hsm_request(int argc, char **argv, int action)
7846 struct option long_opts[] = {
7847 { .val = 'a', .name = "archive", .has_arg = required_argument },
7848 { .val = 'D', .name = "data", .has_arg = required_argument },
7849 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7850 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7853 char short_opts[] = "l:D:a:m:";
7854 struct hsm_user_request *hur, *oldhur;
7859 char *filelist = NULL;
7860 char fullpath[PATH_MAX];
7861 char *opaque = NULL;
7865 int nbfile_alloc = 0;
7866 char *some_file = NULL;
7867 char *mntpath = NULL;
7873 while ((c = getopt_long(argc, argv, short_opts,
7874 long_opts, NULL)) != -1) {
7883 if (action != HUA_ARCHIVE &&
7884 action != HUA_REMOVE) {
7886 "error: -a is supported only "
7887 "when archiving or removing\n");
7890 archive_id = atoi(optarg);
7893 if (some_file == NULL) {
7895 some_file = strdup(optarg);
7901 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7902 argv[0], argv[optind - 1]);
7907 /* All remaining args are files, so we have at least nbfile */
7908 nbfile = argc - optind;
7910 if ((nbfile == 0) && (filelist == NULL))
7914 opaque_len = strlen(opaque);
7916 /* Alloc the request structure with enough place to store all files
7917 * from command line. */
7918 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7920 fprintf(stderr, "Cannot create the request: %s\n",
7924 nbfile_alloc = nbfile;
7926 hur->hur_request.hr_action = action;
7927 hur->hur_request.hr_archive_id = archive_id;
7928 hur->hur_request.hr_flags = 0;
7930 /* All remaining args are files, add them */
7931 if (nbfile != 0 && some_file == NULL)
7932 some_file = strdup(argv[optind]);
7934 for (i = 0; i < nbfile; i++) {
7935 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7941 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7943 /* If a filelist was specified, read the filelist from it. */
7944 if (filelist != NULL) {
7945 fp = fopen(filelist, "r");
7947 fprintf(stderr, "Cannot read the file list %s: %s\n",
7948 filelist, strerror(errno));
7953 while ((rc = getline(&line, &len, fp)) != -1) {
7954 /* If allocated buffer was too small, get something
7956 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7959 nbfile_alloc = nbfile_alloc * 2 + 1;
7961 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7964 fprintf(stderr, "hsm: cannot allocate "
7965 "the request: %s\n",
7972 size = hur_len(oldhur);
7974 fprintf(stderr, "hsm: cannot allocate "
7975 "%u files + %u bytes data\n",
7976 oldhur->hur_request.hr_itemcount,
7977 oldhur->hur_request.hr_data_len);
7984 memcpy(hur, oldhur, size);
7989 if (line[strlen(line) - 1] == '\n')
7990 line[strlen(line) - 1] = '\0';
7992 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7993 mntpath, line, &last_dev);
7999 if (some_file == NULL) {
8009 /* If a --data was used, add it to the request */
8010 hur->hur_request.hr_data_len = opaque_len;
8012 memcpy(hur_data(hur), opaque, opaque_len);
8014 /* Send the HSM request */
8015 if (realpath(some_file, fullpath) == NULL) {
8016 fprintf(stderr, "Could not find path '%s': %s\n",
8017 some_file, strerror(errno));
8019 rc = llapi_hsm_request(fullpath, hur);
8021 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8022 some_file, strerror(-rc));
8032 static int lfs_hsm_archive(int argc, char **argv)
8034 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8037 static int lfs_hsm_restore(int argc, char **argv)
8039 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8042 static int lfs_hsm_release(int argc, char **argv)
8044 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8047 static int lfs_hsm_remove(int argc, char **argv)
8049 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8052 static int lfs_hsm_cancel(int argc, char **argv)
8054 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8057 static int lfs_swap_layouts(int argc, char **argv)
8062 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8063 SWAP_LAYOUTS_KEEP_MTIME |
8064 SWAP_LAYOUTS_KEEP_ATIME);
8067 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8069 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8071 int lfs_get_mode(const char *string)
8073 enum lock_mode_user mode;
8075 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8076 if (lock_mode_names[mode] == NULL)
8078 if (strcmp(string, lock_mode_names[mode]) == 0)
8085 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8087 enum lu_ladvise_type advice;
8090 advice < ARRAY_SIZE(ladvise_names); advice++) {
8091 if (ladvise_names[advice] == NULL)
8093 if (strcmp(string, ladvise_names[advice]) == 0)
8097 return LU_LADVISE_INVALID;
8100 static int lfs_ladvise(int argc, char **argv)
8102 struct option long_opts[] = {
8103 { .val = 'a', .name = "advice", .has_arg = required_argument },
8104 { .val = 'b', .name = "background", .has_arg = no_argument },
8105 { .val = 'e', .name = "end", .has_arg = required_argument },
8106 { .val = 'l', .name = "length", .has_arg = required_argument },
8107 { .val = 'm', .name = "mode", .has_arg = required_argument },
8108 { .val = 's', .name = "start", .has_arg = required_argument },
8109 { .val = 'u', .name = "unset", .has_arg = no_argument },
8111 char short_opts[] = "a:be:l:m:s:u";
8116 struct llapi_lu_ladvise advice;
8117 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8118 unsigned long long start = 0;
8119 unsigned long long end = LUSTRE_EOF;
8120 unsigned long long length = 0;
8121 unsigned long long size_units;
8122 unsigned long long flags = 0;
8126 while ((c = getopt_long(argc, argv, short_opts,
8127 long_opts, NULL)) != -1) {
8130 advice_type = lfs_get_ladvice(optarg);
8131 if (advice_type == LU_LADVISE_INVALID) {
8132 fprintf(stderr, "%s: invalid advice type "
8133 "'%s'\n", argv[0], optarg);
8134 fprintf(stderr, "Valid types:");
8136 for (advice_type = 0;
8137 advice_type < ARRAY_SIZE(ladvise_names);
8139 if (ladvise_names[advice_type] == NULL)
8141 fprintf(stderr, " %s",
8142 ladvise_names[advice_type]);
8144 fprintf(stderr, "\n");
8157 rc = llapi_parse_size(optarg, &end,
8160 fprintf(stderr, "%s: bad end offset '%s'\n",
8167 rc = llapi_parse_size(optarg, &start,
8170 fprintf(stderr, "%s: bad start offset "
8171 "'%s'\n", argv[0], optarg);
8177 rc = llapi_parse_size(optarg, &length,
8180 fprintf(stderr, "%s: bad length '%s'\n",
8186 mode = lfs_get_mode(optarg);
8188 fprintf(stderr, "%s: bad mode '%s', valid "
8189 "modes are READ or WRITE\n",
8197 fprintf(stderr, "%s: option '%s' unrecognized\n",
8198 argv[0], argv[optind - 1]);
8203 if (advice_type == LU_LADVISE_INVALID) {
8204 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8205 fprintf(stderr, "Valid types:");
8206 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8208 if (ladvise_names[advice_type] == NULL)
8210 fprintf(stderr, " %s", ladvise_names[advice_type]);
8212 fprintf(stderr, "\n");
8216 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8217 fprintf(stderr, "%s: Lock no expand advice is a per file "
8218 "descriptor advice, so when called from lfs, "
8219 "it does nothing.\n", argv[0]);
8223 if (argc <= optind) {
8224 fprintf(stderr, "%s: please give one or more file names\n",
8229 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8230 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8235 if (end == LUSTRE_EOF && length != 0)
8236 end = start + length;
8239 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8240 argv[0], start, end);
8244 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8245 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8250 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8251 fprintf(stderr, "%s: mode is required with lockahead\n",
8256 while (optind < argc) {
8259 path = argv[optind++];
8261 fd = open(path, O_RDONLY);
8263 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8264 argv[0], path, strerror(errno));
8269 advice.lla_start = start;
8270 advice.lla_end = end;
8271 advice.lla_advice = advice_type;
8272 advice.lla_value1 = 0;
8273 advice.lla_value2 = 0;
8274 advice.lla_value3 = 0;
8275 advice.lla_value4 = 0;
8276 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8277 advice.lla_lockahead_mode = mode;
8278 advice.lla_peradvice_flags = flags;
8281 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8284 fprintf(stderr, "%s: cannot give advice '%s' to file "
8285 "'%s': %s\n", argv[0],
8286 ladvise_names[advice_type],
8287 path, strerror(errno));
8293 if (rc == 0 && rc2 < 0)
8299 static const char *const heat_names[] = LU_HEAT_NAMES;
8301 static int lfs_heat_get(int argc, char **argv)
8303 struct lu_heat *heat;
8312 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8314 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8319 while (optind < argc) {
8320 path = argv[optind++];
8322 fd = open(path, O_RDONLY);
8324 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8325 argv[0], path, strerror(errno));
8330 heat->lh_count = OBD_HEAT_COUNT;
8331 rc2 = llapi_heat_get(fd, heat);
8334 fprintf(stderr, "%s: cannot get heat of file '%s'"
8335 ": %s\n", argv[0], path, strerror(errno));
8339 printf("flags: %x\n", heat->lh_flags);
8340 for (i = 0; i < heat->lh_count; i++)
8341 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8343 if (rc == 0 && rc2 < 0)
8351 static int lfs_heat_set(int argc, char **argv)
8353 struct option long_opts[] = {
8354 { .val = 'c', .name = "clear", .has_arg = no_argument },
8355 { .val = 'o', .name = "off", .has_arg = no_argument },
8356 { .val = 'O', .name = "on", .has_arg = no_argument },
8358 enum lu_heat_flag flags = 0;
8368 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8371 flags |= LU_HEAT_FLAG_CLEAR;
8374 flags |= LU_HEAT_FLAG_CLEAR;
8375 flags |= LU_HEAT_FLAG_OFF;
8378 flags &= ~LU_HEAT_FLAG_OFF;
8383 fprintf(stderr, "%s: option '%s' unrecognized\n",
8384 argv[0], argv[optind - 1]);
8389 if (argc <= optind) {
8390 fprintf(stderr, "%s: please give one or more file names\n",
8395 while (optind < argc) {
8396 path = argv[optind++];
8398 fd = open(path, O_RDONLY);
8400 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8401 argv[0], path, strerror(errno));
8406 rc2 = llapi_heat_set(fd, flags);
8409 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8410 ": %s\n", argv[0], path, strerror(errno));
8414 if (rc == 0 && rc2 < 0)
8420 /** The input string contains a comma delimited list of component ids and
8421 * ranges, for example "1,2-4,7".
8423 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8425 bool end_of_loop = false;
8433 while (!end_of_loop) {
8437 char *endptr = NULL;
8440 ptr = strchrnul(arg, ',');
8441 end_of_loop = *ptr == '\0';
8444 start_index = strtol(arg, &endptr, 0);
8445 if (endptr == arg) /* no data at all */
8447 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8449 if (start_index < 0)
8452 end_index = start_index;
8453 if (*endptr == '-') {
8454 end_index = strtol(endptr + 1, &endptr, 0);
8455 if (*endptr != '\0')
8457 if (end_index < start_index)
8461 for (i = start_index; i <= end_index && size > 0; i++) {
8464 /* remove duplicate */
8465 for (j = 0; j < nr; j++) {
8469 if (j == nr) { /* no duplicate */
8475 if (size == 0 && i < end_index)
8482 if (!end_of_loop && ptr != NULL)
8485 return rc < 0 ? rc : nr;
8489 * struct verify_mirror_id - Mirror id to be verified.
8490 * @mirror_id: A specified mirror id.
8491 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8493 struct verify_mirror_id {
8499 * compare_mirror_ids() - Compare mirror ids.
8500 * @layout: Mirror component list.
8501 * @cbdata: Callback data in verify_mirror_id structure.
8503 * This is a callback function called by llapi_layout_comp_iterate()
8504 * to compare the specified mirror id with the one in the current
8505 * component of @layout. If they are the same, then the specified
8506 * mirror id is valid.
8508 * Return: a negative error code on failure or
8509 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8510 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8513 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8515 struct verify_mirror_id *mirror_id_cbdata =
8516 (struct verify_mirror_id *)cbdata;
8520 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8524 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8525 progname, strerror(errno));
8529 if (mirror_id_cbdata->mirror_id == mirror_id) {
8530 mirror_id_cbdata->is_valid_id = true;
8531 return LLAPI_LAYOUT_ITER_STOP;
8534 return LLAPI_LAYOUT_ITER_CONT;
8538 * verify_mirror_ids() - Verify specified mirror ids.
8539 * @fname: Mirrored file name.
8540 * @mirror_ids: Specified mirror ids to be verified.
8541 * @ids_nr: Number of specified mirror ids.
8543 * This function verifies that specified @mirror_ids are valid
8544 * in the mirrored file @fname.
8546 * Return: 0 on success or a negative error code on failure.
8549 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8551 struct llapi_layout *layout = NULL;
8552 struct verify_mirror_id mirror_id_cbdata = { 0 };
8563 if (stat(fname, &stbuf) < 0) {
8564 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8565 progname, fname, strerror(errno));
8570 if (!S_ISREG(stbuf.st_mode)) {
8571 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8577 fd = open(fname, O_DIRECT | O_RDONLY);
8579 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8580 progname, fname, strerror(errno));
8585 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8587 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8588 progname, fname, strerror(errno));
8592 layout = llapi_layout_get_by_fd(fd, 0);
8593 if (layout == NULL) {
8594 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8595 progname, fname, strerror(errno));
8597 llapi_lease_release(fd);
8601 rc = llapi_layout_flags_get(layout, &flr_state);
8603 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8604 progname, fname, strerror(errno));
8609 flr_state &= LCM_FL_FLR_MASK;
8610 switch (flr_state) {
8613 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8614 progname, fname, llapi_layout_flags_string(flr_state));
8621 for (i = 0; i < ids_nr; i++) {
8622 mirror_id_cbdata.mirror_id = mirror_ids[i];
8623 mirror_id_cbdata.is_valid_id = false;
8625 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8630 "%s: '%s' failed to verify mirror id: %u.\n",
8631 progname, fname, mirror_ids[i]);
8635 if (!mirror_id_cbdata.is_valid_id) {
8638 "%s: '%s' invalid specified mirror id: %u.\n",
8639 progname, fname, mirror_ids[i]);
8645 llapi_layout_free(layout);
8646 llapi_lease_release(fd);
8654 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8655 __u16 *mirror_ids, int ids_nr)
8657 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8658 struct llapi_layout *layout;
8668 if (stat(fname, &stbuf) < 0) {
8669 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8670 progname, fname, strerror(errno));
8674 if (!S_ISREG(stbuf.st_mode)) {
8675 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8681 fd = open(fname, O_DIRECT | O_RDWR);
8683 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8684 progname, fname, strerror(errno));
8689 layout = llapi_layout_get_by_fd(fd, 0);
8690 if (layout == NULL) {
8691 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8692 progname, fname, strerror(errno));
8697 rc = llapi_layout_flags_get(layout, &flr_state);
8699 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8700 progname, fname, strerror(errno));
8705 flr_state &= LCM_FL_FLR_MASK;
8706 if (flr_state == LCM_FL_NONE) {
8708 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8713 /* get stale component info */
8714 comp_size = llapi_mirror_find_stale(layout, comp_array,
8715 ARRAY_SIZE(comp_array),
8716 mirror_ids, ids_nr);
8717 if (comp_size <= 0) {
8722 ioc->lil_mode = LL_LEASE_WRLCK;
8723 ioc->lil_flags = LL_LEASE_RESYNC;
8724 rc = llapi_lease_set(fd, ioc);
8726 if (rc == -EALREADY)
8730 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8731 progname, fname, strerror(errno));
8735 /* get the read range [start, end) */
8736 start = comp_array[0].lrc_start;
8737 end = comp_array[0].lrc_end;
8738 for (idx = 1; idx < comp_size; idx++) {
8739 if (comp_array[idx].lrc_start < start)
8740 start = comp_array[idx].lrc_start;
8741 if (end < comp_array[idx].lrc_end)
8742 end = comp_array[idx].lrc_end;
8745 rc = llapi_lease_check(fd);
8746 if (rc != LL_LEASE_WRLCK) {
8747 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8752 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8755 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8756 progname, fname, rc);
8758 /* prepare ioc for lease put */
8759 ioc->lil_mode = LL_LEASE_UNLCK;
8760 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8762 for (idx = 0; idx < comp_size; idx++) {
8763 if (comp_array[idx].lrc_synced) {
8764 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8769 rc = llapi_lease_set(fd, ioc);
8771 if (rc == 0) /* lost lease lock */
8773 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8774 progname, fname, strerror(errno));
8778 * llapi_lease_set returns lease mode when it request to unlock
8784 llapi_layout_free(layout);
8791 static inline int lfs_mirror_resync(int argc, char **argv)
8793 struct ll_ioc_lease *ioc = NULL;
8794 __u16 mirror_ids[128] = { 0 };
8799 struct option long_opts[] = {
8800 { .val = 'o', .name = "only", .has_arg = required_argument },
8803 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8806 rc = parse_mirror_ids(mirror_ids,
8807 sizeof(mirror_ids) / sizeof(__u16),
8811 "%s: bad mirror ids '%s'.\n",
8818 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8819 argv[0], argv[optind - 1]);
8825 if (argc == optind) {
8826 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8831 if (ids_nr > 0 && argc > optind + 1) {
8833 "%s: option '--only' cannot be used upon multiple files.\n",
8841 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8846 /* set the lease on the file */
8847 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8849 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8850 argv[0], strerror(errno));
8855 for (; optind < argc; optind++) {
8856 rc = lfs_mirror_resync_file(argv[optind], ioc,
8857 mirror_ids, ids_nr);
8858 /* ignore previous file's error, continue with next file */
8861 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8869 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8871 struct llapi_layout *layout;
8874 layout = llapi_layout_get_by_fd(fd, 0);
8875 if (layout == NULL) {
8876 fprintf(stderr, "could not get layout.\n");
8880 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8882 fprintf(stderr, "failed to iterate layout\n");
8883 llapi_layout_free(layout);
8886 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8887 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8888 llapi_layout_free(layout);
8892 llapi_layout_free(layout);
8898 * Check whether two files are the same file
8899 * \retval 0 same file
8900 * \retval 1 not the same file
8901 * \retval <0 error code
8903 static inline int check_same_file(const char *f1, const char *f2)
8908 if (stat(f1, &stbuf1) < 0) {
8909 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8910 progname, f1, strerror(errno));
8914 if (stat(f2, &stbuf2) < 0) {
8915 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8916 progname, f2, strerror(errno));
8920 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8921 stbuf1.st_ino == stbuf2.st_ino)
8927 static inline int lfs_mirror_read(int argc, char **argv)
8930 __u16 mirror_id = 0;
8931 const char *outfile = NULL;
8937 const size_t buflen = 4 << 20;
8939 struct option long_opts[] = {
8940 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8941 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8944 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8949 mirror_id = strtoul(optarg, &end, 0);
8950 if (*end != '\0' || mirror_id == 0) {
8952 "%s %s: invalid mirror ID '%s'\n",
8953 progname, argv[0], optarg);
8961 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8962 progname, argv[optind - 1]);
8967 if (argc == optind) {
8968 fprintf(stderr, "%s %s: no mirrored file provided\n",
8971 } else if (argc > optind + 1) {
8972 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8976 if (mirror_id == 0) {
8977 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8982 /* open mirror file */
8983 fname = argv[optind];
8986 rc = check_same_file(fname, outfile);
8989 "%s %s: output file cannot be the mirrored file\n",
8997 fd = open(fname, O_DIRECT | O_RDONLY);
8999 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9000 progname, argv[0], fname, strerror(errno));
9004 /* verify mirror id */
9005 rc = verify_mirror_id_by_fd(fd, mirror_id);
9008 "%s %s: cannot find mirror with ID %u in '%s'\n",
9009 progname, argv[0], mirror_id, fname);
9013 /* open output file */
9015 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9017 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9018 progname, argv[0], outfile, strerror(errno));
9023 outfd = STDOUT_FILENO;
9026 /* allocate buffer */
9027 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9029 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9030 progname, argv[0], rc);
9037 ssize_t written = 0;
9039 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9040 if (bytes_read < 0) {
9043 "%s %s: fail to read data from mirror %u: %s\n",
9044 progname, argv[0], mirror_id, strerror(-rc));
9049 if (bytes_read == 0)
9052 while (written < bytes_read) {
9055 written2 = write(outfd, buf + written,
9056 bytes_read - written);
9059 "%s %s: fail to write %s: %s\n",
9060 progname, argv[0], outfile ? : "STDOUT",
9065 written += written2;
9068 if (written != bytes_read) {
9070 "%s %s: written %ld bytes does not match with %ld read.\n",
9071 progname, argv[0], written, bytes_read);
9093 static inline int lfs_mirror_write(int argc, char **argv)
9096 __u16 mirror_id = 0;
9097 const char *inputfile = NULL;
9103 const size_t buflen = 4 << 20;
9105 size_t page_size = sysconf(_SC_PAGESIZE);
9106 struct ll_ioc_lease_id ioc;
9108 struct option long_opts[] = {
9109 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9110 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9113 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9118 mirror_id = strtoul(optarg, &end, 0);
9119 if (*end != '\0' || mirror_id == 0) {
9121 "%s %s: invalid mirror ID '%s'\n",
9122 progname, argv[0], optarg);
9130 fprintf(stderr, "%s: option '%s' unrecognized\n",
9131 progname, argv[optind - 1]);
9136 if (argc == optind) {
9137 fprintf(stderr, "%s %s: no mirrored file provided\n",
9140 } else if (argc > optind + 1) {
9141 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9145 if (mirror_id == 0) {
9146 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9151 /* open mirror file */
9152 fname = argv[optind];
9155 rc = check_same_file(fname, inputfile);
9158 "%s %s: input file cannot be the mirrored file\n",
9166 fd = open(fname, O_DIRECT | O_WRONLY);
9168 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9169 progname, argv[0], fname, strerror(errno));
9173 /* verify mirror id */
9174 rc = verify_mirror_id_by_fd(fd, mirror_id);
9177 "%s %s: cannot find mirror with ID %u in '%s'\n",
9178 progname, argv[0], mirror_id, fname);
9182 /* open input file */
9184 inputfd = open(inputfile, O_RDONLY, 0644);
9186 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9187 progname, argv[0], inputfile, strerror(errno));
9192 inputfd = STDIN_FILENO;
9195 /* allocate buffer */
9196 rc = posix_memalign(&buf, page_size, buflen);
9198 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9199 progname, argv[0], rc);
9203 /* prepare target mirror components instantiation */
9204 ioc.lil_mode = LL_LEASE_WRLCK;
9205 ioc.lil_flags = LL_LEASE_RESYNC;
9206 ioc.lil_mirror_id = mirror_id;
9207 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9210 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9211 progname, argv[0], fname, strerror(errno));
9221 rc = llapi_lease_check(fd);
9222 if (rc != LL_LEASE_WRLCK) {
9223 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9224 progname, argv[0], fname);
9228 bytes_read = read(inputfd, buf, buflen);
9229 if (bytes_read < 0) {
9232 "%s %s: fail to read data from '%s': %s\n",
9233 progname, argv[0], inputfile ? : "STDIN",
9240 if (bytes_read == 0)
9243 /* round up to page align to make direct IO happy. */
9244 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9246 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9251 "%s %s: fail to write to mirror %u: %s\n",
9252 progname, argv[0], mirror_id,
9260 if (pos & (page_size - 1)) {
9261 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9266 ioc.lil_mode = LL_LEASE_UNLCK;
9267 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9269 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9274 "%s %s: release lease lock of '%s' failed: %s\n",
9275 progname, argv[0], fname, strerror(errno));
9292 struct collect_ids_data {
9298 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9300 struct collect_ids_data *cid = cbdata;
9304 rc = llapi_layout_mirror_id_get(layout, &id);
9308 if ((__u16)id != cid->cid_exclude) {
9311 for (i = 0; i < cid->cid_count; i++) {
9312 /* already collected the mirror id */
9313 if (id == cid->cid_ids[i])
9314 return LLAPI_LAYOUT_ITER_CONT;
9316 cid->cid_ids[cid->cid_count] = id;
9320 return LLAPI_LAYOUT_ITER_CONT;
9323 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9325 struct llapi_layout *layout;
9326 struct collect_ids_data cid = { .cid_ids = ids,
9328 .cid_exclude = exclude_id, };
9331 layout = llapi_layout_get_by_fd(fd, 0);
9332 if (layout == NULL) {
9333 fprintf(stderr, "could not get layout\n");
9337 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9339 fprintf(stderr, "failed to iterate layout\n");
9340 llapi_layout_free(layout);
9344 llapi_layout_free(layout);
9346 return cid.cid_count;
9349 static inline int lfs_mirror_copy(int argc, char **argv)
9352 __u16 read_mirror_id = 0;
9353 __u16 ids[128] = { 0 };
9355 struct llapi_layout *layout = NULL;
9356 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9363 struct ll_ioc_lease *ioc = NULL;
9364 struct ll_ioc_lease_id *resync_ioc;
9366 struct option long_opts[] = {
9367 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9368 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9371 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9376 read_mirror_id = strtoul(optarg, &end, 0);
9377 if (*end != '\0' || read_mirror_id == 0) {
9379 "%s %s: invalid read mirror ID '%s'\n",
9380 progname, argv[0], optarg);
9385 if (!strcmp(optarg, "-1")) {
9386 /* specify all other mirrors */
9390 count = parse_mirror_ids((__u16 *)ids,
9398 fprintf(stderr, "%s: option '%s' unrecognized\n",
9399 progname, argv[optind - 1]);
9404 if (argc == optind) {
9405 fprintf(stderr, "%s %s: no mirrored file provided\n",
9408 } else if (argc > optind + 1) {
9409 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9413 if (read_mirror_id == 0) {
9415 "%s %s: no valid read mirror ID %d is provided\n",
9416 progname, argv[0], read_mirror_id);
9422 "%s %s: no write mirror ID is provided\n",
9427 for (i = 0; i < count; i++) {
9428 if (read_mirror_id == ids[i]) {
9430 "%s %s: read and write mirror ID cannot be the same\n",
9436 /* open mirror file */
9437 fname = argv[optind];
9439 fd = open(fname, O_DIRECT | O_RDWR);
9441 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9442 progname, argv[0], fname, strerror(errno));
9446 /* write to all other mirrors */
9447 if (ids[0] == (__u16)-1) {
9448 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9452 "%s %s: failed to get other mirror ids in '%s': %d\n",
9453 progname, argv[0], fname, rc);
9458 /* verify mirror id */
9459 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9462 "%s %s: cannot find mirror with ID %u in '%s'\n",
9463 progname, argv[0], read_mirror_id, fname);
9467 for (i = 0; i < count; i++) {
9468 rc = verify_mirror_id_by_fd(fd, ids[i]);
9471 "%s %s: cannot find mirror with ID %u in '%s'\n",
9472 progname, argv[0], ids[i], fname);
9477 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9480 "%s %s: cannot alloc comp id array for ioc: %s\n",
9481 progname, argv[0], strerror(errno));
9486 /* get stale component info */
9487 layout = llapi_layout_get_by_fd(fd, 0);
9488 if (layout == NULL) {
9489 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9490 progname, argv[0], fname, strerror(errno));
9494 comp_size = llapi_mirror_find_stale(layout, comp_array,
9495 ARRAY_SIZE(comp_array),
9497 llapi_layout_free(layout);
9498 if (comp_size < 0) {
9503 /* prepare target mirror components instantiation */
9504 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9505 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9506 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9508 resync_ioc->lil_mirror_id = ids[0];
9510 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9511 rc = llapi_lease_set(fd, ioc);
9514 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9515 progname, argv[0], fname, strerror(errno));
9519 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9522 fprintf(stderr, "%s %s: copy error: %d\n",
9523 progname, argv[0], rc);
9527 fprintf(stdout, "mirror copied successfully: ");
9528 for (i = 0; i < copied; i++)
9529 fprintf(stdout, "%d ", ids[i]);
9530 fprintf(stdout, "\n");
9532 ioc->lil_mode = LL_LEASE_UNLCK;
9533 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9535 for (i = 0; i < comp_size; i++) {
9538 for (j = 0; j < copied; j++) {
9539 if (comp_array[i].lrc_mirror_id != ids[j])
9542 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9546 rc = llapi_lease_set(fd, ioc);
9551 "%s %s: release lease lock of '%s' failed: %s\n",
9552 progname, argv[0], fname, strerror(errno));
9566 * struct verify_chunk - Mirror chunk to be verified.
9567 * @chunk: [start, end) of the chunk.
9568 * @mirror_count: Number of mirror ids in @mirror_id array.
9569 * @mirror_id: Array of valid mirror ids that cover the chunk.
9571 struct verify_chunk {
9572 struct lu_extent chunk;
9573 unsigned int mirror_count;
9574 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9578 * print_chunks() - Print chunk information.
9579 * @fname: Mirrored file name.
9580 * @chunks: Array of chunks.
9581 * @chunk_count: Number of chunks in @chunks array.
9583 * This function prints [start, end) of each chunk in @chunks
9584 * for mirrored file @fname, and also prints the valid mirror ids
9585 * that cover the chunk.
9590 void print_chunks(const char *fname, struct verify_chunk *chunks,
9596 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9597 for (i = 0; i < chunk_count; i++) {
9598 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9600 if (chunks[i].mirror_count == 0)
9601 fprintf(stdout, "\t[");
9603 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9604 for (j = 1; j < chunks[i].mirror_count; j++)
9605 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9607 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9609 fprintf(stdout, "\n");
9613 * print_checksums() - Print CRC-32 checksum values.
9614 * @chunk: A chunk and its corresponding valid mirror ids.
9615 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9617 * This function prints CRC-32 checksum values on @chunk for
9618 * each valid mirror that covers it.
9623 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9628 "CRC-32 checksum value for chunk "DEXT":\n",
9629 PEXT(&chunk->chunk));
9630 for (i = 0; i < chunk->mirror_count; i++)
9631 fprintf(stdout, "Mirror %u:\t%#lx\n",
9632 chunk->mirror_id[i], crc[i]);
9633 fprintf(stdout, "\n");
9637 * filter_mirror_id() - Filter specified mirror ids.
9638 * @chunks: Array of chunks.
9639 * @chunk_count: Number of chunks in @chunks array.
9640 * @mirror_ids: Specified mirror ids to be verified.
9641 * @ids_nr: Number of specified mirror ids.
9643 * This function scans valid mirror ids that cover each chunk in @chunks
9644 * and filters specified mirror ids.
9649 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9650 __u16 *mirror_ids, int ids_nr)
9655 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9656 unsigned int valid_count = 0;
9658 for (i = 0; i < chunk_count; i++) {
9659 if (chunks[i].mirror_count == 0)
9663 for (j = 0; j < ids_nr; j++) {
9664 for (k = 0; k < chunks[i].mirror_count; k++) {
9665 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9666 valid_id[valid_count] = mirror_ids[j];
9673 memcpy(chunks[i].mirror_id, valid_id,
9674 sizeof(__u16) * valid_count);
9675 chunks[i].mirror_count = valid_count;
9680 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9681 * @layout: Mirror component list.
9682 * @chunks: Array of chunks.
9683 * @chunks_size: Array size of @chunks.
9685 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9686 * to find out chunk segments and store them in @chunks array.
9688 * The @mirror_id array in each element of @chunks will store the valid
9689 * mirror ids that cover the chunk. If a mirror component covering the
9690 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9691 * will not be stored into the @mirror_id array, and the chunk for that
9692 * mirror will not be verified.
9694 * The @mirror_count in each element of @chunks will store the number of
9695 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9696 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9697 * indicates the chunk is valid in only one mirror. In both cases, the
9698 * chunk will not be verified.
9700 * Here is an example:
9703 * +------+-------------+--------------------+
9705 * +------+------+------+------+-------------+
9706 * | | S | S | | mirror2
9707 * +-------------+------+------+-------------+
9709 * prepared @chunks array will contain 5 elements:
9710 * (([0, 1M), [1, 2], 2),
9711 * ([1M, 2M), [1, 2], 2),
9712 * ([2M, 3M), [1], 1),
9713 * ([3M, 4M], [], 0),
9714 * ([4M, EOF), [2], 1))
9716 * Return: the actual array size of @chunks on success
9717 * or a negative error code on failure.
9720 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9721 struct verify_chunk *chunks,
9732 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9735 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9738 "%s: move to the first layout component: %s.\n",
9739 progname, strerror(errno));
9745 chunks[idx].chunk.e_end = LUSTRE_EOF;
9747 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9750 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9751 progname, strerror(errno));
9755 if (start > chunks[idx].chunk.e_start ||
9756 end <= chunks[idx].chunk.e_start)
9759 if (end < chunks[idx].chunk.e_end)
9760 chunks[idx].chunk.e_end = end;
9762 rc = llapi_layout_comp_flags_get(layout, &flags);
9765 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9766 progname, strerror(errno));
9770 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9773 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9776 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9777 progname, strerror(errno));
9781 chunks[idx].mirror_id[i] = mirror_id;
9783 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9785 "%s: mirror_id array is too small.\n",
9792 rc = llapi_layout_comp_use(layout,
9793 LLAPI_LAYOUT_COMP_USE_NEXT);
9796 "%s: move to the next layout component: %s.\n",
9797 progname, strerror(errno));
9800 } /* loop through all components */
9802 chunks[idx].mirror_count = i;
9804 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9808 if (idx >= chunks_size) {
9809 fprintf(stderr, "%s: chunks array is too small.\n",
9815 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9819 return rc < 0 ? rc : idx + 1;
9823 * lfs_mirror_verify_chunk() - Verify a chunk.
9824 * @fd: File descriptor of the mirrored file.
9825 * @file_size: Size of the mirrored file.
9826 * @chunk: A chunk and its corresponding valid mirror ids.
9827 * @verbose: Verbose mode.
9829 * This function verifies a @chunk contains exactly the same data
9830 * ammong the mirrors that cover it.
9832 * If @verbose is specified, then the function will print where the
9833 * differences are if the data do not match. Otherwise, it will
9834 * just return an error in that case.
9836 * Return: 0 on success or a negative error code on failure.
9839 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9840 struct verify_chunk *chunk, int verbose)
9842 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9844 size_t page_size = sysconf(_SC_PAGESIZE);
9850 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9857 rc = posix_memalign(&buf, page_size, buflen);
9858 if (rc) /* error code is returned directly */
9862 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9863 PEXT(&chunk->chunk));
9864 for (i = 0; i < chunk->mirror_count; i++)
9865 fprintf(stdout, " %u", chunk->mirror_id[i]);
9866 fprintf(stdout, "\n");
9870 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9871 pos = chunk->chunk.e_start;
9872 while (bytes_done < count) {
9873 /* compute initial CRC-32 checksum */
9874 crc = crc32(0L, Z_NULL, 0);
9875 memset(crc_array, 0, sizeof(crc_array));
9878 for (i = 0; i < chunk->mirror_count; i++) {
9879 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9881 if (bytes_read < 0) {
9884 "%s: failed to read data from mirror %u: %s.\n",
9885 progname, chunk->mirror_id[i],
9890 /* compute new CRC-32 checksum */
9891 crc_array[i] = crc32(crc, buf, bytes_read);
9895 print_checksums(chunk, crc_array);
9897 /* compare CRC-32 checksum values */
9898 for (i = 1; i < chunk->mirror_count; i++) {
9899 if (crc_array[i] != crc_array[0]) {
9903 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9904 progname, PEXT(&chunk->chunk),
9905 chunk->mirror_id[0],
9906 chunk->mirror_id[i]);
9911 bytes_done += bytes_read;
9914 if (verbose > 1 && rc == 0) {
9915 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9916 PEXT(&chunk->chunk));
9917 for (i = 0; i < chunk->mirror_count; i++)
9918 fprintf(stdout, " %u", chunk->mirror_id[i]);
9919 fprintf(stdout, " PASS\n\n");
9928 * lfs_mirror_verify_file() - Verify a mirrored file.
9929 * @fname: Mirrored file name.
9930 * @mirror_ids: Specified mirror ids to be verified.
9931 * @ids_nr: Number of specified mirror ids.
9932 * @verbose: Verbose mode.
9934 * This function verifies that each SYNC mirror of a mirrored file
9935 * specified by @fname contains exactly the same data.
9937 * If @mirror_ids is specified, then the function will verify the
9938 * mirrors specified by @mirror_ids contain exactly the same data.
9940 * If @verbose is specified, then the function will print where the
9941 * differences are if the data do not match. Otherwise, it will
9942 * just return an error in that case.
9944 * Return: 0 on success or a negative error code on failure.
9947 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9950 struct verify_chunk chunks_array[1024] = { };
9951 struct llapi_layout *layout = NULL;
9955 int chunk_count = 0;
9961 if (stat(fname, &stbuf) < 0) {
9962 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9963 progname, fname, strerror(errno));
9968 if (!S_ISREG(stbuf.st_mode)) {
9969 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9975 if (stbuf.st_size == 0) {
9977 fprintf(stdout, "%s: '%s' file size is 0.\n",
9983 fd = open(fname, O_DIRECT | O_RDONLY);
9985 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9986 progname, fname, strerror(errno));
9991 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9993 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9994 progname, fname, strerror(errno));
9998 layout = llapi_layout_get_by_fd(fd, 0);
9999 if (layout == NULL) {
10000 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10001 progname, fname, strerror(errno));
10003 llapi_lease_release(fd);
10007 rc = llapi_layout_flags_get(layout, &flr_state);
10009 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10010 progname, fname, strerror(errno));
10015 flr_state &= LCM_FL_FLR_MASK;
10016 switch (flr_state) {
10019 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10020 progname, fname, llapi_layout_flags_string(flr_state));
10026 /* find out mirror chunks to be verified */
10027 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10028 ARRAY_SIZE(chunks_array));
10029 if (chunk_count < 0) {
10035 /* filter specified mirror ids */
10036 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10039 print_chunks(fname, chunks_array, chunk_count);
10041 for (idx = 0; idx < chunk_count; idx++) {
10042 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10045 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10047 PEXT(&chunks_array[idx].chunk),
10048 (unsigned long long)stbuf.st_size);
10052 if (chunks_array[idx].mirror_count == 0) {
10054 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10056 PEXT(&chunks_array[idx].chunk));
10058 fprintf(stderr, "skipped\n");
10062 fprintf(stderr, "failed\n");
10066 if (chunks_array[idx].mirror_count == 1) {
10069 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10071 PEXT(&chunks_array[idx].chunk),
10072 chunks_array[idx].mirror_id[0]);
10076 rc = llapi_lease_check(fd);
10077 if (rc != LL_LEASE_RDLCK) {
10078 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10083 /* verify one chunk */
10084 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10085 &chunks_array[idx], verbose);
10099 llapi_layout_free(layout);
10100 llapi_lease_release(fd);
10108 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10109 * @argc: The count of lfs mirror verify command line arguments.
10110 * @argv: Array of strings for lfs mirror verify command line arguments.
10112 * This function parses lfs mirror verify command and verifies the
10113 * specified mirrored file(s).
10115 * Return: 0 on success or a negative error code on failure.
10117 static inline int lfs_mirror_verify(int argc, char **argv)
10119 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10125 char cmd[PATH_MAX];
10127 struct option long_opts[] = {
10128 { .val = 'o', .name = "only", .has_arg = required_argument },
10129 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10130 { .name = NULL } };
10132 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10134 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10137 rc = parse_mirror_ids(mirror_ids,
10138 ARRAY_SIZE(mirror_ids),
10142 "%s: bad mirror ids '%s'.\n",
10149 "%s: at least 2 mirror ids needed with '--only' option.\n",
10159 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10160 progname, argv[optind - 1]);
10166 if (argc == optind) {
10167 fprintf(stderr, "%s: no file name given.\n", progname);
10172 if (ids_nr > 0 && argc > optind + 1) {
10174 "%s: '--only' cannot be used upon multiple files.\n",
10182 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10188 for (; optind < argc; optind++) {
10189 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10199 * lfs_mirror() - Parse and execute lfs mirror commands.
10200 * @argc: The count of lfs mirror command line arguments.
10201 * @argv: Array of strings for lfs mirror command line arguments.
10203 * This function parses lfs mirror commands and performs the
10204 * corresponding functions specified in mirror_cmdlist[].
10206 * Return: 0 on success or an error code on failure.
10208 static int lfs_mirror(int argc, char **argv)
10210 char cmd[PATH_MAX];
10213 setlinebuf(stdout);
10215 Parser_init("lfs-mirror > ", mirror_cmdlist);
10217 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10219 program_invocation_short_name = cmd;
10221 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10223 rc = Parser_commands();
10225 return rc < 0 ? -rc : rc;
10228 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10230 #if __BYTE_ORDER == __BIG_ENDIAN
10231 __swab16s(&attrs->lsa_valid);
10232 __swab64s(&attrs->lsa_size);
10233 __swab64s(&attrs->lsa_blocks);
10237 enum lfs_som_type {
10238 LFS_SOM_SIZE = 0x1,
10239 LFS_SOM_BLOCKS = 0x2,
10240 LFS_SOM_FLAGS = 0x4,
10241 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10245 static int lfs_getsom(int argc, char **argv)
10248 struct lustre_som_attrs *attrs;
10249 char buf[sizeof(*attrs) + 64];
10250 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10253 while ((c = getopt(argc, argv, "sbf")) != -1) {
10256 type = LFS_SOM_SIZE;
10259 type = LFS_SOM_BLOCKS;
10262 type = LFS_SOM_FLAGS;
10265 fprintf(stderr, "%s: invalid option '%c'\n",
10275 fprintf(stderr, "%s: %s\n",
10276 progname, argc == 0 ? "miss file target" :
10277 "input more than 2 files");
10282 attrs = (void *)buf;
10283 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10286 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10287 argv[0], strerror(errno), errno);
10291 lustre_som_swab(attrs);
10294 case LFS_SOM_ATTR_ALL:
10295 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10296 path, attrs->lsa_size, attrs->lsa_blocks,
10300 printf("%llu\n", attrs->lsa_size);
10302 case LFS_SOM_BLOCKS:
10303 printf("%llu\n", attrs->lsa_blocks);
10305 case LFS_SOM_FLAGS:
10306 printf("%x\n", attrs->lsa_valid);
10309 fprintf(stderr, "%s: unknown option\n", progname);
10317 * lfs_mirror_list_commands() - List lfs mirror commands.
10318 * @argc: The count of command line arguments.
10319 * @argv: Array of strings for command line arguments.
10321 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10323 * Return: 0 on success.
10325 static int lfs_mirror_list_commands(int argc, char **argv)
10327 char buffer[81] = "";
10329 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10335 static int lfs_list_commands(int argc, char **argv)
10337 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10339 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10344 int main(int argc, char **argv)
10348 /* Ensure that liblustreapi constructor has run */
10349 if (!llapi_liblustreapi_initialized())
10350 fprintf(stderr, "liblustreapi was not properly initialized\n");
10352 setlinebuf(stdout);
10355 Parser_init("lfs > ", cmdlist);
10357 progname = program_invocation_short_name; /* Used in error messages */
10359 llapi_set_command_name(argv[1]);
10360 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10361 llapi_clear_command_name();
10363 rc = Parser_commands();
10366 return rc < 0 ? -rc : rc;
10369 #ifdef _LUSTRE_IDL_H_
10370 /* Everything we need here should be included by lustreapi.h. */
10371 # error "lfs should not depend on lustre_idl.h"
10372 #endif /* _LUSTRE_IDL_H_ */