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 static int showdf(char *mntdir, struct obd_statfs *stat,
4888 char *uuid, enum mntdf_flags flags,
4889 char *type, int index, int rc)
4891 long long avail, used, total;
4893 char *suffix = "KMGTPEZY";
4894 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4895 char tbuf[3 * sizeof(__u64)];
4896 char ubuf[3 * sizeof(__u64)];
4897 char abuf[3 * sizeof(__u64)];
4898 char rbuf[3 * sizeof(__u64)];
4905 if (flags & MNTDF_INODES) {
4906 avail = stat->os_ffree;
4907 used = stat->os_files - stat->os_ffree;
4908 total = stat->os_files;
4910 int shift = flags & MNTDF_COOKED ? 0 : 10;
4912 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4913 used = ((stat->os_blocks - stat->os_bfree) *
4914 stat->os_bsize) >> shift;
4915 total = (stat->os_blocks * stat->os_bsize) >> shift;
4918 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4920 if (flags & MNTDF_COOKED) {
4924 cook_val = (double)total;
4927 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4930 snprintf(tbuf, sizeof(tbuf), CDF, total);
4932 cook_val = (double)used;
4935 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4938 snprintf(ubuf, sizeof(ubuf), CDF, used);
4940 cook_val = (double)avail;
4943 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4946 snprintf(abuf, sizeof(abuf), CDF, avail);
4948 snprintf(tbuf, sizeof(tbuf), CDF, total);
4949 snprintf(ubuf, sizeof(tbuf), CDF, used);
4950 snprintf(abuf, sizeof(tbuf), CDF, avail);
4953 sprintf(rbuf, RDF, ratio);
4954 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4955 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4957 printf("[%s:%d]", type, index);
4959 if (stat->os_state) {
4961 * Each character represents the matching
4964 const char state_names[] = "DRSI";
4969 for (i = 0, state = stat->os_state;
4970 state && i < sizeof(state_names); i++) {
4971 if (!(state & (1 << i)))
4973 printf("%c", state_names[i]);
4981 printf(UUF": inactive device\n", uuid);
4984 printf(UUF": %s\n", uuid, strerror(-rc));
4991 struct ll_stat_type {
4996 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4998 struct ll_statfs_data {
5000 struct obd_statfs sd_st;
5003 struct ll_statfs_buf {
5005 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5008 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5009 int ops, struct ll_statfs_buf *lsb)
5011 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5012 struct obd_uuid uuid_buf;
5013 char *poolname = NULL;
5014 struct ll_stat_type types[] = {
5015 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5016 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5017 { .st_name = NULL } };
5018 struct ll_stat_type *tp;
5019 __u64 ost_files = 0;
5020 __u64 ost_ffree = 0;
5028 poolname = strchr(pool, '.');
5029 if (poolname != NULL) {
5030 if (strncmp(fsname, pool, strlen(fsname))) {
5031 fprintf(stderr, "filesystem name incorrect\n");
5039 fd = open(mntdir, O_RDONLY);
5042 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5047 if (flags & MNTDF_SHOW) {
5048 if (flags & MNTDF_INODES)
5049 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5050 "UUID", "Inodes", "IUsed", "IFree",
5051 "IUse%", "Mounted on");
5053 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5055 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5056 "Used", "Available", "Use%", "Mounted on");
5059 for (tp = types; tp->st_name != NULL; tp++) {
5060 bool have_ost = false;
5062 if (!(tp->st_op & ops))
5065 for (index = 0; ; index++) {
5066 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5067 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5068 type = flags & MNTDF_LAZY ?
5069 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5070 rc2 = llapi_obd_fstatfs(fd, type, index,
5071 &stat_buf, &uuid_buf);
5076 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5077 if (!(flags & MNTDF_VERBOSE))
5079 } else if (rc2 < 0 && rc == 0) {
5083 /* If we have OSTs then don't report MDT block counts.
5084 * For MDT-only filesystems the expectation is that all
5085 * layouts have a DoM component. For filesystems with
5086 * OSTs, files are not necessarily going to store data
5087 * on MDTs, and MDT space is limited to a fraction of
5088 * OST space, so don't include it in the summary.
5090 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5097 if (poolname && tp->st_op == LL_STATFS_LOV &&
5098 llapi_search_ost(fsname, poolname,
5099 obd_uuid2str(&uuid_buf)) != 1)
5102 /* the llapi_obd_statfs() call may have returned with
5103 * an error, but if it filled in uuid_buf we will at
5104 * lease use that to print out a message for that OBD.
5105 * If we didn't get anything in the uuid_buf, then fill
5106 * it in so that we can print an error message. */
5107 if (uuid_buf.uuid[0] == '\0')
5108 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5109 "%s%04x", tp->st_name, index);
5111 lsb->sb_buf[lsb->sb_count].sd_index = index;
5112 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5115 if (flags & MNTDF_SHOW)
5116 showdf(mntdir, &stat_buf,
5117 obd_uuid2str(&uuid_buf), flags,
5118 tp->st_name, index, rc2);
5123 if (tp->st_op == LL_STATFS_LMV) {
5124 sum.os_ffree += stat_buf.os_ffree;
5125 sum.os_files += stat_buf.os_files;
5126 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5127 ost_files += stat_buf.os_files;
5128 ost_ffree += stat_buf.os_ffree;
5130 sum.os_blocks += stat_buf.os_blocks *
5132 sum.os_bfree += stat_buf.os_bfree *
5134 sum.os_bavail += stat_buf.os_bavail *
5141 /* If we have _some_ OSTs, but don't have as many free objects on the
5142 * OST as inodes on the MDTs, reduce the reported number of inodes
5143 * to compensate, so that the "inodes in use" number is correct.
5144 * This should be kept in sync with ll_statfs_internal().
5146 if (ost_files && ost_ffree < sum.os_ffree) {
5147 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5148 sum.os_ffree = ost_ffree;
5150 if (flags & MNTDF_SHOW) {
5152 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5159 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
5161 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
5163 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
5165 int r1 = obd_statfs_ratio(st1, false);
5166 int r2 = obd_statfs_ratio(st2, false);
5167 int64_t result = r1 - r2;
5169 /* if both space usage are above 90, compare free inodes */
5170 if (r1 > 90 && r2 > 90)
5171 result = st2->os_ffree - st1->os_ffree;
5175 else if (result == 0)
5182 static int lfs_setdirstripe(int argc, char **argv)
5185 struct lfs_setstripe_args lsa = { 0 };
5186 struct llapi_stripe_param *param = NULL;
5187 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5190 char *mode_opt = NULL;
5191 bool default_stripe = false;
5192 bool delete = false;
5193 bool auto_distributed = false;
5194 bool foreign_mode = false;
5195 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5196 mode_t previous_mode = 0;
5197 struct ll_statfs_buf *lsb = NULL;
5198 char mntdir[PATH_MAX] = "";
5200 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5201 struct option long_opts[] = {
5202 { .val = 'c', .name = "count", .has_arg = required_argument },
5203 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5204 { .val = 'd', .name = "delete", .has_arg = no_argument },
5205 { .val = 'D', .name = "default", .has_arg = no_argument },
5206 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5207 { .val = LFS_LAYOUT_FLAGS_OPT,
5208 .name = "flags", .has_arg = required_argument },
5209 { .val = LFS_LAYOUT_FOREIGN_OPT,
5210 .name = "foreign", .has_arg = optional_argument},
5211 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5212 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5213 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5214 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5216 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5217 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5219 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5220 { .val = 'i', .name = "index", .has_arg = required_argument },
5222 { .val = 'o', .name = "mode", .has_arg = required_argument },
5223 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5224 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5226 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5227 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5228 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5232 setstripe_args_init(&lsa);
5234 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5235 long_opts, NULL)) >= 0) {
5242 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5245 "%s %s: invalid stripe count '%s'\n",
5246 progname, argv[0], optarg);
5252 default_stripe = true;
5255 default_stripe = true;
5257 case LFS_LAYOUT_FOREIGN_OPT:
5258 if (optarg != NULL) {
5259 /* check pure numeric */
5260 type = strtoul(optarg, &end, 0);
5263 type = check_foreign_type_name(optarg);
5264 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5266 "%s %s: unknown foreign type '%s'\n",
5273 foreign_mode = true;
5275 case LFS_LAYOUT_FLAGS_OPT:
5276 flags = strtoul(optarg, &end, 16);
5279 "%s %s: bad flags '%s'\n",
5280 progname, argv[0], optarg);
5284 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5286 fprintf(stderr, "warning: '--hash-type' and '-t' "
5287 "deprecated, use '--mdt-hash' or '-H' instead\n");
5290 lsa.lsa_pattern = check_hashtype(optarg);
5291 if (lsa.lsa_pattern == 0) {
5293 "%s %s: bad stripe hash type '%s'\n",
5294 progname, argv[0], optarg);
5299 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5302 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5303 if (strcmp(argv[optind - 1], "--index") == 0)
5305 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5308 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
5309 lsa.lsa_nr_tgts = parse_targets(mdts,
5310 sizeof(mdts) / sizeof(__u32),
5311 lsa.lsa_nr_tgts, optarg, NULL);
5312 if (lsa.lsa_nr_tgts < 0) {
5314 "%s %s: invalid MDT target(s) '%s'\n",
5315 progname, argv[0], optarg);
5318 lsa.lsa_pattern = 0;
5320 lsa.lsa_tgts = mdts;
5321 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5322 lsa.lsa_stripe_off = mdts[0];
5324 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5326 fprintf(stderr, "warning: '-m' is deprecated, "
5327 "use '--mode' or '-o' instead\n");
5336 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5337 progname, argv[0], argv[optind - 1]);
5342 if (optind == argc) {
5343 fprintf(stderr, "%s %s: DIR must be specified\n",
5348 if (xattr && !foreign_mode) {
5349 /* only print a warning as this is armless and will be
5353 "%s %s: xattr has been specified for non-foreign layout\n",
5355 } else if (foreign_mode && !xattr) {
5357 "%s %s: xattr must be provided in foreign mode\n",
5362 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5363 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5365 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5370 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5371 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5373 "%s %s: stripe offset and count must be specified\n",
5379 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5380 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5382 "%s %s: cannot specify -d with -c or -i options\n",
5387 if (mode_opt != NULL) {
5388 mode = strtoul(mode_opt, &end, 8);
5391 "%s %s: bad MODE '%s'\n",
5392 progname, argv[0], mode_opt);
5395 previous_mode = umask(0);
5398 /* foreign LMV/dir case */
5400 if (argc > optind + 1) {
5402 "%s %s: cannot specify multiple foreign dirs\n",
5407 dname = argv[optind];
5408 result = llapi_dir_create_foreign(dname, mode, type, flags,
5412 "%s mkdir: can't create foreign dir '%s': %s\n",
5413 progname, dname, strerror(-result));
5418 * initialize stripe parameters, in case param is converted to specific,
5419 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5421 param = calloc(1, offsetof(typeof(*param),
5422 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5423 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5424 if (param == NULL) {
5426 "%s %s: cannot allocate memory for parameters: %s\n",
5427 progname, argv[0], strerror(ENOMEM));
5431 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5432 param->lsp_stripe_count = lsa.lsa_stripe_count;
5433 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5434 param->lsp_stripe_offset = -1;
5436 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5437 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5438 param->lsp_stripe_pattern = lsa.lsa_pattern;
5440 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5441 param->lsp_pool = lsa.lsa_pool_name;
5442 param->lsp_is_specific = false;
5443 if (lsa.lsa_nr_tgts > 1) {
5444 if (lsa.lsa_stripe_count > 0 &&
5445 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5446 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5447 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5448 "match the number of MDTs: %d\n",
5449 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5454 param->lsp_is_specific = true;
5455 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5456 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5459 if (!default_stripe && lsa.lsa_pattern == LMV_HASH_TYPE_SPACE) {
5460 fprintf(stderr, "%s %s: can only specify -H space with -D\n",
5466 dname = argv[optind];
5468 if (default_stripe) {
5469 result = llapi_dir_set_default_lmv(dname, param);
5472 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
5473 progname, dname, strerror(-result));
5478 * if current \a dname isn't under the same \a mntdir as the
5479 * last one, and the last one was auto-distributed, restore
5482 if (mntdir[0] != '\0' &&
5483 strncmp(dname, mntdir, strlen(mntdir)) &&
5485 param->lsp_is_specific = false;
5486 param->lsp_stripe_offset = -1;
5487 auto_distributed = false;
5491 * TODO: when MDT can allocate object with QoS (LU-9435), below
5492 * code should be removed, instead we should let LMV to allocate
5493 * the starting MDT object, and then let LOD allocate other MDT
5496 if (!param->lsp_is_specific && param->lsp_stripe_offset == -1) {
5497 char path[PATH_MAX] = "";
5500 lsb = malloc(sizeof(*lsb));
5508 /* use mntdir for dirname() temporarily */
5509 strncpy(mntdir, dname, sizeof(mntdir));
5510 if (!realpath(dirname(mntdir), path)) {
5513 "error: invalid path '%s': %s\n",
5514 argv[optind], strerror(errno));
5519 result = llapi_search_mounts(path, 0, mntdir, NULL);
5520 if (result < 0 || mntdir[0] == '\0') {
5522 "No suitable Lustre mount found\n");
5526 result = mntdf(mntdir, NULL, NULL, 0, LL_STATFS_LMV,
5531 if (param->lsp_stripe_count > lsb->sb_count) {
5533 "error: stripe count %d is too big\n",
5534 param->lsp_stripe_count);
5539 qsort(lsb->sb_buf, lsb->sb_count,
5540 sizeof(struct ll_statfs_data),
5541 ll_statfs_data_comp);
5543 auto_distributed = true;
5546 if (auto_distributed) {
5548 int nr = MAX(param->lsp_stripe_count,
5551 /* don't use server whose usage is above 90% */
5552 while (nr != param->lsp_stripe_count &&
5553 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st, false) >
5555 nr = MAX(param->lsp_stripe_count, nr / 2);
5557 /* get \a r between [0, nr) */
5560 param->lsp_stripe_offset = lsb->sb_buf[r].sd_index;
5561 if (param->lsp_stripe_count > 1) {
5564 param->lsp_is_specific = true;
5565 for (; i < param->lsp_stripe_count; i++)
5566 param->lsp_tgts[(i + r) % nr] =
5567 lsb->sb_buf[i].sd_index;
5571 result = llapi_dir_create(dname, mode, param);
5574 "%s setdirstripe: cannot create dir '%s': %s\n",
5575 progname, dname, strerror(-result));
5576 } while (!result && (dname = argv[++optind]));
5578 if (mode_opt != NULL)
5579 umask(previous_mode);
5587 static int lfs_rmentry(int argc, char **argv)
5594 fprintf(stderr, "error: %s: missing dirname\n",
5600 dname = argv[index];
5601 while (dname != NULL) {
5602 result = llapi_direntry_remove(dname);
5604 fprintf(stderr, "error: %s: remove dir entry '%s' "
5605 "failed\n", argv[0], dname);
5608 dname = argv[++index];
5613 static int lfs_mv(int argc, char **argv)
5615 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5616 struct find_param param = {
5623 struct option long_opts[] = {
5624 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5625 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5626 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5629 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5631 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5633 fprintf(stderr, "warning: '-M' deprecated"
5634 ", use '--mdt-index' or '-m' instead\n");
5637 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5639 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5645 param.fp_verbose = VERBOSE_DETAIL;
5648 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5649 progname, argv[optind - 1]);
5654 if (lmu.lum_stripe_offset == -1) {
5655 fprintf(stderr, "%s mv: MDT index must be specified\n",
5660 if (optind >= argc) {
5661 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5666 /* initialize migrate mdt parameters */
5667 param.fp_lmv_md = &lmu;
5668 param.fp_migrate = 1;
5669 rc = llapi_migrate_mdt(argv[optind], ¶m);
5671 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5672 progname, argv[optind], param.fp_mdt_index,
5677 static int lfs_osts(int argc, char **argv)
5679 return lfs_tgts(argc, argv);
5682 static int lfs_mdts(int argc, char **argv)
5684 return lfs_tgts(argc, argv);
5687 static int lfs_df(int argc, char **argv)
5689 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5690 enum mntdf_flags flags = MNTDF_SHOW;
5691 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5692 int c, rc = 0, index = 0;
5693 char fsname[PATH_MAX] = "", *pool_name = NULL;
5694 struct option long_opts[] = {
5695 { .val = 'h', .name = "human-readable",
5696 .has_arg = no_argument },
5697 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5698 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5699 { .val = 'p', .name = "pool", .has_arg = required_argument },
5700 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5703 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5706 flags |= MNTDF_COOKED;
5709 flags |= MNTDF_INODES;
5712 flags |= MNTDF_LAZY;
5718 flags |= MNTDF_VERBOSE;
5724 if (optind < argc && !realpath(argv[optind], path)) {
5726 fprintf(stderr, "error: invalid path '%s': %s\n",
5727 argv[optind], strerror(-rc));
5731 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5732 /* Check if we have a mount point */
5733 if (mntdir[0] == '\0')
5736 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5737 if (rc || path[0] != '\0')
5739 fsname[0] = '\0'; /* avoid matching in next loop */
5740 mntdir[0] = '\0'; /* avoid matching in next loop */
5746 static int print_instance(const char *mntdir, char *fsname, size_t fsnamelen,
5747 bool opt_instance, bool opt_fsname, bool opt_mntdir)
5751 /* llapi_search_mounts() fills "fsname", but that is not called if
5752 * explicit paths are specified on the command-line
5754 if (opt_instance || (opt_fsname && fsname[0] == '\0')) {
5755 int rc = llapi_getname(mntdir, fsname, fsnamelen);
5758 fprintf(stderr, "cannot get instance for '%s': %s\n",
5759 mntdir, strerror(-rc));
5763 if (!opt_instance) {
5764 /* print only the fsname name */
5765 buf = strchr(fsname, '-');
5769 } else if (!opt_fsname) {
5770 /* print only the instance name */
5771 buf = strchr(fsname, '-');
5777 } else if (opt_fsname) {
5778 /* print only the fsname */
5782 printf("%s %s\n", buf, opt_mntdir ? mntdir : "");
5787 static int lfs_getname(int argc, char **argv)
5789 struct option long_opts[] = {
5790 { .val = 'h', .name = "help", .has_arg = no_argument },
5791 { .val = 'i', .name = "instance", .has_arg = no_argument },
5792 { .val = 'n', .name = "fsname", .has_arg = no_argument },
5794 bool opt_instance = false, opt_fsname = false;
5795 char fsname[PATH_MAX] = "";
5798 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
5801 opt_instance = true;
5812 /* If neither option is given, print both instance and fsname */
5813 if (!opt_instance && !opt_fsname)
5814 opt_instance = opt_fsname = true;
5816 if (optind == argc) { /* no paths specified, get all paths. */
5817 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
5820 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5821 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
5822 opt_instance, opt_fsname, true);
5825 path[0] = fsname[0] = mntdir[0] = '\0';
5827 } else { /* paths specified, only attempt to search these. */
5830 /* if only one path is given, print only requested info */
5831 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
5833 for (; optind < argc; optind++) {
5834 rc2 = print_instance(argv[optind], fsname,
5835 sizeof(fsname), opt_instance,
5836 opt_fsname, opt_mntdir);
5846 static int lfs_check(int argc, char **argv)
5848 char mntdir[PATH_MAX] = {'\0'};
5857 fprintf(stderr, "%s check: server type must be specified\n",
5862 obd_types[0] = obd_type1;
5863 obd_types[1] = obd_type2;
5864 obd_types[2] = obd_type3;
5866 if (strcmp(argv[1], "osts") == 0) {
5867 strcpy(obd_types[0], "osc");
5868 } else if (strcmp(argv[1], "mdts") == 0 ||
5869 strcmp(argv[1], "mds") == 0) {
5870 strcpy(obd_types[0], "mdc");
5871 } else if (strcmp(argv[1], "mgts") == 0) {
5872 strcpy(obd_types[0], "mgc");
5873 } else if (strcmp(argv[1], "all") == 0 ||
5874 strcmp(argv[1], "servers") == 0) {
5876 strcpy(obd_types[0], "osc");
5877 strcpy(obd_types[1], "mdc");
5878 strcpy(obd_types[2], "mgc");
5880 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5885 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5886 if (rc < 0 || mntdir[0] == '\0') {
5888 "%s check: cannot find mounted Lustre filesystem: %s\n",
5889 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5893 rc = llapi_target_check(num_types, obd_types, mntdir);
5895 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5896 progname, argv[1], strerror(-rc));
5902 #ifdef HAVE_SYS_QUOTA_H
5903 #define ARG2INT(nr, str, msg) \
5906 nr = strtol(str, &endp, 0); \
5907 if (*endp != '\0') { \
5908 fprintf(stderr, "%s: bad %s '%s'\n", \
5909 progname, msg, str); \
5914 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5916 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5917 * returns the value or ULONG_MAX on integer overflow or incorrect format
5919 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5920 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5921 * 3. empty integer value is interpreted as 0
5923 static unsigned long str2sec(const char* timestr)
5925 const char spec[] = "smhdw";
5926 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5927 unsigned long val = 0;
5930 if (strpbrk(timestr, spec) == NULL) {
5931 /* no specifiers inside the time string,
5932 should treat it as an integer value */
5933 val = strtoul(timestr, &tail, 10);
5934 return *tail ? ULONG_MAX : val;
5937 /* format string is XXwXXdXXhXXmXXs */
5943 v = strtoul(timestr, &tail, 10);
5944 if (v == ULONG_MAX || *tail == '\0')
5945 /* value too large (ULONG_MAX or more)
5946 or missing specifier */
5949 ptr = strchr(spec, *tail);
5951 /* unknown specifier */
5956 /* check if product will overflow the type */
5957 if (!(v < ULONG_MAX / mult[ind]))
5960 ADD_OVERFLOW(val, mult[ind] * v);
5961 if (val == ULONG_MAX)
5973 #define ARG2ULL(nr, str, def_units) \
5975 unsigned long long limit, units = def_units; \
5978 rc = llapi_parse_size(str, &limit, &units, 1); \
5980 fprintf(stderr, "%s: invalid limit '%s'\n", \
5987 static inline int has_times_option(int argc, char **argv)
5991 for (i = 1; i < argc; i++)
5992 if (!strcmp(argv[i], "-t"))
5998 int lfs_setquota_times(int argc, char **argv)
6001 struct if_quotactl qctl;
6002 char *mnt, *obd_type = (char *)qctl.obd_type;
6003 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6004 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6005 struct option long_opts[] = {
6006 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6007 { .val = 'g', .name = "group", .has_arg = no_argument },
6008 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6009 { .val = 'p', .name = "projid", .has_arg = no_argument },
6010 { .val = 't', .name = "times", .has_arg = no_argument },
6011 { .val = 'u', .name = "user", .has_arg = no_argument },
6015 memset(&qctl, 0, sizeof(qctl));
6016 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6017 qctl.qc_type = ALLQUOTA;
6019 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6020 long_opts, NULL)) != -1) {
6031 if (qctl.qc_type != ALLQUOTA) {
6032 fprintf(stderr, "error: -u/g/p can't be used "
6033 "more than once\n");
6036 qctl.qc_type = qtype;
6039 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
6040 fprintf(stderr, "error: bad block-grace: %s\n",
6044 dqb->dqb_valid |= QIF_BTIME;
6047 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
6048 fprintf(stderr, "error: bad inode-grace: %s\n",
6052 dqb->dqb_valid |= QIF_ITIME;
6054 case 't': /* Yes, of course! */
6056 default: /* getopt prints error message for us when opterr != 0 */
6061 if (qctl.qc_type == ALLQUOTA) {
6062 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6066 if (optind != argc - 1) {
6067 fprintf(stderr, "error: unexpected parameters encountered\n");
6071 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
6072 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
6073 fprintf(stderr, "error: grace time is too large\n");
6078 rc = llapi_quotactl(mnt, &qctl);
6081 fprintf(stderr, "%s %s ", obd_type,
6082 obd_uuid2str(&qctl.obd_uuid));
6083 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6090 #define BSLIMIT (1 << 0)
6091 #define BHLIMIT (1 << 1)
6092 #define ISLIMIT (1 << 2)
6093 #define IHLIMIT (1 << 3)
6095 int lfs_setquota(int argc, char **argv)
6098 struct if_quotactl qctl;
6099 char *mnt, *obd_type = (char *)qctl.obd_type;
6100 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6101 struct option long_opts[] = {
6102 { .val = 'b', .name = "block-softlimit",
6103 .has_arg = required_argument },
6104 { .val = 'B', .name = "block-hardlimit",
6105 .has_arg = required_argument },
6106 { .val = 'd', .name = "default", .has_arg = no_argument },
6107 { .val = 'g', .name = "group", .has_arg = required_argument },
6108 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6109 { .val = 'i', .name = "inode-softlimit",
6110 .has_arg = required_argument },
6111 { .val = 'I', .name = "inode-hardlimit",
6112 .has_arg = required_argument },
6113 { .val = 'p', .name = "projid", .has_arg = required_argument },
6114 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6115 { .val = 'u', .name = "user", .has_arg = required_argument },
6116 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6118 unsigned limit_mask = 0;
6120 bool use_default = false;
6123 if (has_times_option(argc, argv))
6124 return lfs_setquota_times(argc, argv);
6126 memset(&qctl, 0, sizeof(qctl));
6127 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6128 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6129 * so it can be used as a marker that qc_type
6130 * isn't reinitialized from command line */
6132 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6133 long_opts, NULL)) != -1) {
6136 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6139 goto quota_type_def;
6142 rc = name2uid(&qctl.qc_id, optarg);
6145 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6148 goto quota_type_def;
6151 rc = name2gid(&qctl.qc_id, optarg);
6154 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6157 goto quota_type_def;
6160 rc = name2projid(&qctl.qc_id, optarg);
6163 qctl.qc_id = strtoul(optarg, &endptr, 10);
6164 if (*endptr != '\0') {
6165 fprintf(stderr, "%s setquota: invalid"
6166 " id '%s'\n", progname, optarg);
6171 if (qctl.qc_id == 0) {
6172 fprintf(stderr, "%s setquota: can't set quota"
6173 " for root usr/group/project.\n",
6179 if (qctl.qc_type != ALLQUOTA) {
6181 "%s setquota: only one of -u, -U, -g,"
6182 " -G, -p or -P may be specified\n",
6186 qctl.qc_type = qtype;
6189 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6193 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6194 dqb->dqb_bsoftlimit >>= 10;
6195 limit_mask |= BSLIMIT;
6196 if (dqb->dqb_bsoftlimit &&
6197 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6199 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6200 "See '%s help setquota' or Lustre manual for details\n",
6201 progname, dqb->dqb_bsoftlimit,
6205 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6206 dqb->dqb_bhardlimit >>= 10;
6207 limit_mask |= BHLIMIT;
6208 if (dqb->dqb_bhardlimit &&
6209 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6211 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6212 "See '%s help setquota' or Lustre manual for details\n",
6213 progname, dqb->dqb_bhardlimit,
6217 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6218 limit_mask |= ISLIMIT;
6219 if (dqb->dqb_isoftlimit &&
6220 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6222 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6223 "See '%s help setquota' or Lustre manual for details\n",
6224 progname, dqb->dqb_isoftlimit,
6228 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6229 limit_mask |= IHLIMIT;
6230 if (dqb->dqb_ihardlimit &&
6231 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6233 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6234 "See '%s help setquota' or Lustre manual for details\n",
6235 progname, dqb->dqb_ihardlimit,
6240 "%s setquota: unrecognized option '%s'\n",
6241 progname, argv[optind - 1]);
6246 if (qctl.qc_type == ALLQUOTA) {
6248 "%s setquota: either -u or -g must be specified\n",
6253 if (!use_default && limit_mask == 0) {
6255 "%s setquota: at least one limit must be specified\n",
6260 if (use_default && limit_mask != 0) {
6262 "%s setquota: limits should not be specified when"
6263 " using default quota\n",
6268 if (use_default && qctl.qc_id == 0) {
6270 "%s setquota: can not set default quota for root"
6271 " user/group/project\n",
6276 if (optind != argc - 1) {
6278 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6279 progname, argv[optind]);
6286 dqb->dqb_bhardlimit = 0;
6287 dqb->dqb_bsoftlimit = 0;
6288 dqb->dqb_ihardlimit = 0;
6289 dqb->dqb_isoftlimit = 0;
6292 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6293 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6294 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6295 /* sigh, we can't just set blimits/ilimits */
6296 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6297 .qc_type = qctl.qc_type,
6298 .qc_id = qctl.qc_id};
6300 rc = llapi_quotactl(mnt, &tmp_qctl);
6304 if (!(limit_mask & BHLIMIT))
6305 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6306 if (!(limit_mask & BSLIMIT))
6307 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6308 if (!(limit_mask & IHLIMIT))
6309 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6310 if (!(limit_mask & ISLIMIT))
6311 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6313 /* Keep grace times if we have got no softlimit arguments */
6314 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6315 dqb->dqb_valid |= QIF_BTIME;
6316 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6319 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6320 dqb->dqb_valid |= QIF_ITIME;
6321 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6325 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6326 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6328 rc = llapi_quotactl(mnt, &qctl);
6332 "%s setquota: cannot quotactl '%s' '%s': %s",
6334 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6341 /* Converts seconds value into format string
6342 * result is returned in buf
6344 * 1. result is in descenting order: 1w2d3h4m5s
6345 * 2. zero fields are not filled (except for p. 3): 5d1s
6346 * 3. zero seconds value is presented as "0s"
6348 static char * __sec2str(time_t seconds, char *buf)
6350 const char spec[] = "smhdw";
6351 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6356 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
6357 c = seconds / mult[i];
6359 if (c > 0 || (i == 0 && buf == tail))
6360 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6368 static void sec2str(time_t seconds, char *buf, int rc)
6375 tail = __sec2str(seconds, tail);
6377 if (rc && tail - buf < 39) {
6383 static void diff2str(time_t seconds, char *buf, time_t now)
6389 if (seconds <= now) {
6390 strcpy(buf, "none");
6393 __sec2str(seconds - now, buf);
6396 static void print_quota_title(char *name, struct if_quotactl *qctl,
6397 bool human_readable, bool show_default)
6400 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6401 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6402 "Filesystem", "bquota", "blimit", "bgrace",
6403 "iquota", "ilimit", "igrace");
6405 printf("Disk quotas for %s %s (%cid %u):\n",
6406 qtype_name(qctl->qc_type), name,
6407 *qtype_name(qctl->qc_type), qctl->qc_id);
6408 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6409 "Filesystem", human_readable ? "used" : "kbytes",
6410 "quota", "limit", "grace",
6411 "files", "quota", "limit", "grace");
6415 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6418 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6421 snprintf(buf, buflen, "%5.4gP",
6422 (double)num / ((__u64)1 << 40));
6424 snprintf(buf, buflen, "%5.4gT",
6425 (double)num / (1 << 30));
6427 snprintf(buf, buflen, "%5.4gG",
6428 (double)num / (1 << 20));
6430 snprintf(buf, buflen, "%5.4gM",
6431 (double)num / (1 << 10));
6433 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6437 #define STRBUF_LEN 32
6438 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6439 int rc, bool h, bool show_default)
6445 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6446 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6447 int bover = 0, iover = 0;
6448 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6449 char numbuf[3][STRBUF_LEN];
6451 char strbuf[STRBUF_LEN];
6453 if (dqb->dqb_bhardlimit &&
6454 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6456 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6457 if (dqb->dqb_btime > now) {
6464 if (dqb->dqb_ihardlimit &&
6465 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6467 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6468 if (dqb->dqb_itime > now) {
6476 if (strlen(mnt) > 15)
6477 printf("%s\n%15s", mnt, "");
6479 printf("%15s", mnt);
6482 diff2str(dqb->dqb_btime, timebuf, now);
6483 else if (show_default)
6484 snprintf(timebuf, sizeof(timebuf), "%llu",
6487 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6488 strbuf, sizeof(strbuf), h);
6489 if (rc == -EREMOTEIO)
6490 sprintf(numbuf[0], "%s*", strbuf);
6492 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6493 "%s" : "[%s]", strbuf);
6495 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6496 if (type == QC_GENERAL)
6497 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6498 "%s" : "[%s]", strbuf);
6500 sprintf(numbuf[1], "%s", "-");
6502 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6503 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6504 "%s" : "[%s]", strbuf);
6507 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6509 printf(" %7s%c %6s %7s %7s",
6510 numbuf[0], bover ? '*' : ' ', numbuf[1],
6511 numbuf[2], bover > 1 ? timebuf : "-");
6515 diff2str(dqb->dqb_itime, timebuf, now);
6516 else if (show_default)
6517 snprintf(timebuf, sizeof(timebuf), "%llu",
6520 snprintf(numbuf[0], sizeof(numbuf),
6521 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6522 (uintmax_t)dqb->dqb_curinodes);
6524 if (type == QC_GENERAL)
6525 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6527 (uintmax_t)dqb->dqb_isoftlimit);
6529 sprintf(numbuf[1], "%s", "-");
6531 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6532 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6535 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6536 else if (type != QC_OSTIDX)
6537 printf(" %7s%c %6s %7s %7s",
6538 numbuf[0], iover ? '*' : ' ', numbuf[1],
6539 numbuf[2], iover > 1 ? timebuf : "-");
6541 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6544 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6545 qctl->qc_cmd == Q_GETOINFO) {
6549 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6550 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6551 printf("Block grace time: %s; Inode grace time: %s\n",
6552 bgtimebuf, igtimebuf);
6556 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6557 bool h, __u64 *total)
6559 int rc = 0, rc1 = 0, count = 0;
6560 __u32 valid = qctl->qc_valid;
6562 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6564 fprintf(stderr, "can not get %s count: %s\n",
6565 is_mdt ? "mdt": "ost", strerror(-rc));
6569 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6570 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6571 rc = llapi_quotactl(mnt, qctl);
6573 /* It is remote client case. */
6574 if (rc == -EOPNOTSUPP) {
6581 fprintf(stderr, "quotactl %s%d failed.\n",
6582 is_mdt ? "mdt": "ost", qctl->qc_idx);
6586 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6587 qctl->qc_valid, 0, h, false);
6588 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6589 qctl->qc_dqblk.dqb_bhardlimit;
6592 qctl->qc_valid = valid;
6596 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6597 int verbose, int quiet, bool human_readable,
6600 int rc1 = 0, rc2 = 0, rc3 = 0;
6601 char *obd_type = (char *)qctl->obd_type;
6602 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6603 __u64 total_ialloc = 0, total_balloc = 0;
6604 bool use_default_for_blk = false;
6605 bool use_default_for_file = false;
6608 rc1 = llapi_quotactl(mnt, qctl);
6612 fprintf(stderr, "%s quotas are not enabled.\n",
6613 qtype_name(qctl->qc_type));
6616 fprintf(stderr, "Permission denied.\n");
6619 /* We already got error message. */
6622 fprintf(stderr, "Unexpected quotactl error: %s\n",
6627 if (!show_default && qctl->qc_id == 0) {
6628 qctl->qc_dqblk.dqb_bhardlimit = 0;
6629 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6630 qctl->qc_dqblk.dqb_ihardlimit = 0;
6631 qctl->qc_dqblk.dqb_isoftlimit = 0;
6632 qctl->qc_dqblk.dqb_btime = 0;
6633 qctl->qc_dqblk.dqb_itime = 0;
6634 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6637 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6638 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6639 use_default_for_blk = true;
6640 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6643 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6644 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6645 use_default_for_file = true;
6646 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6649 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6650 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6651 print_quota_title(name, qctl, human_readable, show_default);
6653 if (rc1 && *obd_type)
6654 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6656 if (qctl->qc_valid != QC_GENERAL)
6659 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6660 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6661 (QIF_LIMITS|QIF_USAGE));
6663 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6665 if (!show_default && verbose &&
6666 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6667 char strbuf[STRBUF_LEN];
6669 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6671 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6673 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6675 printf("Total allocated inode limit: %ju, total "
6676 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6680 if (use_default_for_blk)
6681 printf("%cid %u is using default block quota setting\n",
6682 *qtype_name(qctl->qc_type), qctl->qc_id);
6684 if (use_default_for_file)
6685 printf("%cid %u is using default file quota setting\n",
6686 *qtype_name(qctl->qc_type), qctl->qc_id);
6688 if (rc1 || rc2 || rc3 || inacc)
6689 printf("Some errors happened when getting quota info. "
6690 "Some devices may be not working or deactivated. "
6691 "The data in \"[]\" is inaccurate.\n");
6697 static int lfs_project(int argc, char **argv)
6699 int ret = 0, err = 0, c, i;
6700 struct project_handle_control phc = { 0 };
6701 enum lfs_project_ops_t op;
6704 phc.assign_projid = false;
6705 /* default action */
6706 op = LFS_PROJECT_LIST;
6708 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6711 if (op != LFS_PROJECT_LIST) {
6713 "%s: cannot specify '-c' '-C' '-s' together\n",
6718 op = LFS_PROJECT_CHECK;
6721 if (op != LFS_PROJECT_LIST) {
6723 "%s: cannot specify '-c' '-C' '-s' together\n",
6728 op = LFS_PROJECT_CLEAR;
6731 if (op != LFS_PROJECT_LIST) {
6733 "%s: cannot specify '-c' '-C' '-s' together\n",
6738 phc.set_inherit = true;
6739 op = LFS_PROJECT_SET;
6745 phc.keep_projid = true;
6748 phc.recursive = true;
6751 phc.projid = strtoul(optarg, NULL, 0);
6752 phc.assign_projid = true;
6756 phc.newline = false;
6759 fprintf(stderr, "%s: invalid option '%c'\n",
6765 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6766 op = LFS_PROJECT_SET;
6767 phc.set_projid = true;
6768 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6769 phc.set_projid = true;
6773 case LFS_PROJECT_CHECK:
6774 if (phc.keep_projid) {
6776 "%s: '-k' is useless together with '-c'\n",
6781 case LFS_PROJECT_CLEAR:
6784 "%s: '-0' is useless together with '-C'\n",
6788 if (phc.assign_projid) {
6790 "%s: '-p' is useless together with '-C'\n",
6795 case LFS_PROJECT_SET:
6798 "%s: '-0' is useless together with '-s'\n",
6802 if (phc.keep_projid) {
6804 "%s: '-k' is useless together with '-s'\n",
6812 "%s: '-0' is useless for list operations\n",
6822 fprintf(stderr, "%s: missing file or directory target(s)\n",
6827 for (i = 0; i < argc; i++) {
6829 case LFS_PROJECT_CHECK:
6830 err = lfs_project_check(argv[i], &phc);
6832 case LFS_PROJECT_LIST:
6833 err = lfs_project_list(argv[i], &phc);
6835 case LFS_PROJECT_CLEAR:
6836 err = lfs_project_clear(argv[i], &phc);
6838 case LFS_PROJECT_SET:
6839 err = lfs_project_set(argv[i], &phc);
6851 static int lfs_quota(int argc, char **argv)
6854 char *mnt, *name = NULL;
6855 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6856 .qc_type = ALLQUOTA };
6857 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6858 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6860 __u32 valid = QC_GENERAL, idx = 0;
6861 bool human_readable = false;
6862 bool show_default = false;
6865 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6868 show_default = true;
6873 show_default = true;
6878 show_default = true;
6882 if (qctl.qc_type != ALLQUOTA) {
6884 "%s quota: only one of -u, -g, or -p may be specified\n",
6888 qctl.qc_type = qtype;
6891 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6894 valid = qctl.qc_valid = QC_UUID;
6895 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6898 valid = qctl.qc_valid = QC_MDTIDX;
6899 idx = qctl.qc_idx = atoi(optarg);
6900 if (idx == 0 && *optarg != '0') {
6902 "%s quota: invalid MDT index '%s'\n",
6908 valid = qctl.qc_valid = QC_OSTIDX;
6909 idx = qctl.qc_idx = atoi(optarg);
6910 if (idx == 0 && *optarg != '0') {
6912 "%s quota: invalid OST index '%s'\n",
6924 human_readable = true;
6927 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6928 progname, argv[optind - 1]);
6933 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6934 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6935 optind == argc - 1 && !show_default) {
6937 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6938 qctl.qc_valid = valid;
6941 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6942 qctl.qc_type = qtype;
6943 if (qtype == USRQUOTA) {
6944 qctl.qc_id = geteuid();
6945 rc = uid2name(&name, qctl.qc_id);
6947 qctl.qc_id = getegid();
6948 rc = gid2name(&name, qctl.qc_id);
6953 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6954 human_readable, show_default);
6959 /* lfs quota -u username /path/to/lustre/mount */
6960 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6961 /* options should be followed by u/g-name and mntpoint */
6962 if ((!show_default && optind + 2 != argc) ||
6963 (show_default && optind + 1 != argc) ||
6964 qctl.qc_type == ALLQUOTA) {
6966 "%s quota: name and mount point must be specified\n",
6971 if (!show_default) {
6972 name = argv[optind++];
6973 switch (qctl.qc_type) {
6975 rc = name2uid(&qctl.qc_id, name);
6978 rc = name2gid(&qctl.qc_id, name);
6981 rc = name2projid(&qctl.qc_id, name);
6988 qctl.qc_valid = QC_GENERAL;
6989 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6994 qctl.qc_id = strtoul(name, &endptr, 10);
6995 if (*endptr != '\0') {
6996 fprintf(stderr, "%s quota: invalid id '%s'\n",
7001 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7002 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7008 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7009 human_readable, show_default);
7012 #endif /* HAVE_SYS_QUOTA_H! */
7014 static int flushctx_ioctl(char *mp)
7018 fd = open(mp, O_RDONLY);
7020 fprintf(stderr, "flushctx: error open %s: %s\n",
7021 mp, strerror(errno));
7025 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7027 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7028 mp, strerror(errno));
7034 static int lfs_flushctx(int argc, char **argv)
7036 int kdestroy = 0, c;
7037 char mntdir[PATH_MAX] = {'\0'};
7041 while ((c = getopt(argc, argv, "k")) != -1) {
7047 fprintf(stderr, "error: %s: option '-%c' "
7048 "unrecognized\n", argv[0], c);
7054 if ((rc = system("kdestroy > /dev/null")) != 0) {
7055 rc = WEXITSTATUS(rc);
7056 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7060 if (optind >= argc) {
7061 /* flush for all mounted lustre fs. */
7062 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7063 /* Check if we have a mount point */
7064 if (mntdir[0] == '\0')
7067 if (flushctx_ioctl(mntdir))
7070 mntdir[0] = '\0'; /* avoid matching in next loop */
7073 /* flush fs as specified */
7074 while (optind < argc) {
7075 if (flushctx_ioctl(argv[optind++]))
7082 static int lfs_changelog(int argc, char **argv)
7084 void *changelog_priv;
7085 struct changelog_rec *rec;
7086 long long startrec = 0, endrec = 0;
7088 struct option long_opts[] = {
7089 { .val = 'f', .name = "follow", .has_arg = no_argument },
7091 char short_opts[] = "f";
7094 while ((rc = getopt_long(argc, argv, short_opts,
7095 long_opts, NULL)) != -1) {
7102 "%s changelog: unrecognized option '%s'\n",
7103 progname, argv[optind - 1]);
7107 if (optind >= argc) {
7108 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7113 mdd = argv[optind++];
7115 startrec = strtoll(argv[optind++], NULL, 10);
7117 endrec = strtoll(argv[optind++], NULL, 10);
7119 rc = llapi_changelog_start(&changelog_priv,
7120 CHANGELOG_FLAG_BLOCK |
7121 CHANGELOG_FLAG_JOBID |
7122 CHANGELOG_FLAG_EXTRA_FLAGS |
7123 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7126 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7127 progname, strerror(errno = -rc));
7131 rc = llapi_changelog_set_xflags(changelog_priv,
7132 CHANGELOG_EXTRA_FLAG_UIDGID |
7133 CHANGELOG_EXTRA_FLAG_NID |
7134 CHANGELOG_EXTRA_FLAG_OMODE |
7135 CHANGELOG_EXTRA_FLAG_XATTR);
7138 "%s changelog: cannot set xflags for changelog: %s\n",
7139 progname, strerror(errno = -rc));
7143 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7147 if (endrec && rec->cr_index > endrec) {
7148 llapi_changelog_free(&rec);
7151 if (rec->cr_index < startrec) {
7152 llapi_changelog_free(&rec);
7156 secs = rec->cr_time >> 30;
7157 gmtime_r(&secs, &ts);
7158 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7159 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7160 changelog_type2str(rec->cr_type),
7161 ts.tm_hour, ts.tm_min, ts.tm_sec,
7162 (int)(rec->cr_time & ((1 << 30) - 1)),
7163 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7164 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7166 if (rec->cr_flags & CLF_JOBID) {
7167 struct changelog_ext_jobid *jid =
7168 changelog_rec_jobid(rec);
7170 if (jid->cr_jobid[0] != '\0')
7171 printf(" j=%s", jid->cr_jobid);
7174 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7175 struct changelog_ext_extra_flags *ef =
7176 changelog_rec_extra_flags(rec);
7178 printf(" ef=0x%llx", ef->cr_extra_flags);
7180 if (ef->cr_extra_flags & CLFE_UIDGID) {
7181 struct changelog_ext_uidgid *uidgid =
7182 changelog_rec_uidgid(rec);
7184 printf(" u=%llu:%llu",
7185 uidgid->cr_uid, uidgid->cr_gid);
7187 if (ef->cr_extra_flags & CLFE_NID) {
7188 struct changelog_ext_nid *nid =
7189 changelog_rec_nid(rec);
7192 libcfs_nid2str(nid->cr_nid));
7195 if (ef->cr_extra_flags & CLFE_OPEN) {
7196 struct changelog_ext_openmode *omd =
7197 changelog_rec_openmode(rec);
7198 char mode[] = "---";
7200 /* exec mode must be exclusive */
7201 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7204 if (omd->cr_openflags & MDS_FMODE_READ)
7206 if (omd->cr_openflags &
7213 if (strcmp(mode, "---") != 0)
7214 printf(" m=%s", mode);
7218 if (ef->cr_extra_flags & CLFE_XATTR) {
7219 struct changelog_ext_xattr *xattr =
7220 changelog_rec_xattr(rec);
7222 if (xattr->cr_xattr[0] != '\0')
7223 printf(" x=%s", xattr->cr_xattr);
7227 if (rec->cr_namelen)
7228 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7229 rec->cr_namelen, changelog_rec_name(rec));
7231 if (rec->cr_flags & CLF_RENAME) {
7232 struct changelog_ext_rename *rnm =
7233 changelog_rec_rename(rec);
7235 if (!fid_is_zero(&rnm->cr_sfid))
7236 printf(" s="DFID" sp="DFID" %.*s",
7237 PFID(&rnm->cr_sfid),
7238 PFID(&rnm->cr_spfid),
7239 (int)changelog_rec_snamelen(rec),
7240 changelog_rec_sname(rec));
7244 llapi_changelog_free(&rec);
7247 llapi_changelog_fini(&changelog_priv);
7250 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7251 progname, strerror(errno = -rc));
7253 return (rc == 1 ? 0 : rc);
7256 static int lfs_changelog_clear(int argc, char **argv)
7264 endrec = strtoll(argv[3], NULL, 10);
7266 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7269 fprintf(stderr, "%s: record out of range: %llu\n",
7271 else if (rc == -ENOENT)
7272 fprintf(stderr, "%s: no changelog user: %s\n",
7275 fprintf(stderr, "%s error: %s\n", argv[0],
7284 static int lfs_fid2path(int argc, char **argv)
7286 struct option long_opts[] = {
7287 { .val = 'c', .name = "cur", .has_arg = no_argument },
7288 { .val = 'l', .name = "link", .has_arg = required_argument },
7289 { .val = 'r', .name = "rec", .has_arg = required_argument },
7291 char short_opts[] = "cl:r:";
7292 char *device, *fid, *path;
7293 long long recno = -1;
7298 char *endptr = NULL;
7300 while ((rc = getopt_long(argc, argv, short_opts,
7301 long_opts, NULL)) != -1) {
7307 linkno = strtol(optarg, &endptr, 10);
7308 if (*endptr != '\0') {
7310 "%s fid2path: invalid linkno '%s'\n",
7316 recno = strtoll(optarg, &endptr, 10);
7317 if (*endptr != '\0') {
7319 "%s fid2path: invalid recno '%s'\n",
7326 "%s fid2path: unrecognized option '%s'\n",
7327 progname, argv[optind - 1]);
7334 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7339 device = argv[optind++];
7340 path = calloc(1, PATH_MAX);
7344 "%s fid2path: cannot allocate memory for path: %s\n",
7345 progname, strerror(-rc));
7350 while (optind < argc) {
7351 fid = argv[optind++];
7353 lnktmp = (linkno >= 0) ? linkno : 0;
7355 int oldtmp = lnktmp;
7356 long long rectmp = recno;
7358 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7362 "%s fid2path: cannot find '%s': %s\n",
7363 progname, fid, strerror(errno = -rc2));
7370 fprintf(stdout, "%lld ", rectmp);
7371 if (device[0] == '/') {
7372 fprintf(stdout, "%s", device);
7373 if (device[strlen(device) - 1] != '/')
7374 fprintf(stdout, "/");
7375 } else if (path[0] == '\0') {
7376 fprintf(stdout, "/");
7378 fprintf(stdout, "%s\n", path);
7381 /* specified linkno */
7383 if (oldtmp == lnktmp)
7393 static int lfs_path2fid(int argc, char **argv)
7395 struct option long_opts[] = {
7396 { .val = 'p', .name = "parents", .has_arg = no_argument },
7399 const char short_opts[] = "p";
7400 const char *sep = "";
7403 bool show_parents = false;
7405 while ((rc = getopt_long(argc, argv, short_opts,
7406 long_opts, NULL)) != -1) {
7409 show_parents = true;
7413 "%s path2fid: unrecognized option '%s'\n",
7414 progname, argv[optind - 1]);
7419 if (optind > argc - 1) {
7420 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7424 else if (optind < argc - 1)
7428 for (path = argv + optind; *path != NULL; path++) {
7430 if (!show_parents) {
7431 err = llapi_path2fid(*path, &fid);
7433 printf("%s%s"DFID"\n",
7434 *sep != '\0' ? *path : "", sep,
7437 char name[NAME_MAX + 1];
7438 unsigned int linkno = 0;
7440 while ((err = llapi_path2parent(*path, linkno, &fid,
7441 name, sizeof(name))) == 0) {
7442 if (*sep != '\0' && linkno == 0)
7443 printf("%s%s", *path, sep);
7445 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7450 /* err == -ENODATA is end-of-loop */
7451 if (linkno > 0 && err == -ENODATA) {
7459 "%s path2fid: cannot get %sfid for '%s': %s\n",
7460 progname, show_parents ? "parent " : "", *path,
7472 static int lfs_data_version(int argc, char **argv)
7479 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7482 fprintf(stderr, "%s data_version: FILE must be specified\n",
7487 while ((c = getopt(argc, argv, "nrw")) != -1) {
7490 data_version_flags = 0;
7493 data_version_flags |= LL_DV_RD_FLUSH;
7496 data_version_flags |= LL_DV_WR_FLUSH;
7500 "%s data_version: unrecognized option '%s'\n",
7501 progname, argv[optind - 1]);
7505 if (optind == argc) {
7506 fprintf(stderr, "%s data_version: FILE must be specified\n",
7511 path = argv[optind];
7512 fd = open(path, O_RDONLY);
7515 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7516 progname, path, strerror(-rc));
7520 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7523 "%s data_version: cannot get version for '%s': %s\n",
7524 progname, path, strerror(-rc));
7526 printf("%ju" "\n", (uintmax_t)data_version);
7532 static int lfs_hsm_state(int argc, char **argv)
7537 struct hsm_user_state hus;
7545 rc = llapi_hsm_state_get(path, &hus);
7547 fprintf(stderr, "can't get hsm state for %s: %s\n",
7548 path, strerror(errno = -rc));
7552 /* Display path name and status flags */
7553 printf("%s: (0x%08x)", path, hus.hus_states);
7555 if (hus.hus_states & HS_RELEASED)
7556 printf(" released");
7557 if (hus.hus_states & HS_EXISTS)
7559 if (hus.hus_states & HS_DIRTY)
7561 if (hus.hus_states & HS_ARCHIVED)
7562 printf(" archived");
7563 /* Display user-settable flags */
7564 if (hus.hus_states & HS_NORELEASE)
7565 printf(" never_release");
7566 if (hus.hus_states & HS_NOARCHIVE)
7567 printf(" never_archive");
7568 if (hus.hus_states & HS_LOST)
7569 printf(" lost_from_hsm");
7571 if (hus.hus_archive_id != 0)
7572 printf(", archive_id:%d", hus.hus_archive_id);
7575 } while (++i < argc);
7580 #define LFS_HSM_SET 0
7581 #define LFS_HSM_CLEAR 1
7584 * Generic function to set or clear HSM flags.
7585 * Used by hsm_set and hsm_clear.
7587 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7589 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7591 struct option long_opts[] = {
7592 { .val = 'A', .name = "archived", .has_arg = no_argument },
7593 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7594 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7595 { .val = 'e', .name = "exists", .has_arg = no_argument },
7596 { .val = 'l', .name = "lost", .has_arg = no_argument },
7597 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7598 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7600 char short_opts[] = "lraAdei:";
7604 __u32 archive_id = 0;
7610 while ((c = getopt_long(argc, argv, short_opts,
7611 long_opts, NULL)) != -1) {
7617 mask |= HS_NOARCHIVE;
7620 mask |= HS_ARCHIVED;
7623 mask |= HS_NORELEASE;
7632 archive_id = strtol(optarg, &end, 10);
7634 fprintf(stderr, "invalid archive_id: '%s'\n",
7642 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7643 argv[0], argv[optind - 1]);
7648 /* User should have specified a flag */
7652 while (optind < argc) {
7654 path = argv[optind];
7656 /* If mode == 0, this means we apply the mask. */
7657 if (mode == LFS_HSM_SET)
7658 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7660 rc = llapi_hsm_state_set(path, 0, mask, 0);
7663 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7664 path, strerror(errno = -rc));
7673 static int lfs_hsm_action(int argc, char **argv)
7678 struct hsm_current_action hca;
7679 struct hsm_extent he;
7680 enum hsm_user_action hua;
7681 enum hsm_progress_states hps;
7689 rc = llapi_hsm_current_action(path, &hca);
7691 fprintf(stderr, "can't get hsm action for %s: %s\n",
7692 path, strerror(errno = -rc));
7695 he = hca.hca_location;
7696 hua = hca.hca_action;
7697 hps = hca.hca_state;
7699 printf("%s: %s", path, hsm_user_action2name(hua));
7701 /* Skip file without action */
7702 if (hca.hca_action == HUA_NONE) {
7707 printf(" %s ", hsm_progress_state2name(hps));
7709 if ((hps == HPS_RUNNING) &&
7710 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7711 printf("(%llu bytes moved)\n",
7712 (unsigned long long)he.length);
7713 else if ((he.offset + he.length) == LUSTRE_EOF)
7714 printf("(from %llu to EOF)\n",
7715 (unsigned long long)he.offset);
7717 printf("(from %llu to %llu)\n",
7718 (unsigned long long)he.offset,
7719 (unsigned long long)(he.offset + he.length));
7721 } while (++i < argc);
7726 static int lfs_hsm_set(int argc, char **argv)
7728 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7731 static int lfs_hsm_clear(int argc, char **argv)
7733 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7737 * Check file state and return its fid, to be used by lfs_hsm_request().
7739 * \param[in] file Path to file to check
7740 * \param[in,out] fid Pointer to allocated lu_fid struct.
7741 * \param[in,out] last_dev Pointer to last device id used.
7743 * \return 0 on success.
7745 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7751 rc = lstat(file, &st);
7753 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7756 /* Checking for regular file as archiving as posix copytool
7757 * rejects archiving files other than regular files
7759 if (!S_ISREG(st.st_mode)) {
7760 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7763 /* A request should be ... */
7764 if (*last_dev != st.st_dev && *last_dev != 0) {
7765 fprintf(stderr, "All files should be "
7766 "on the same filesystem: %s\n", file);
7769 *last_dev = st.st_dev;
7771 rc = llapi_path2fid(file, fid);
7773 fprintf(stderr, "Cannot read FID of %s: %s\n",
7774 file, strerror(-rc));
7780 /* Fill an HSM HUR item with a given file name.
7782 * If mntpath is set, then the filename is actually a FID, and no
7783 * lookup on the filesystem will be performed.
7785 * \param[in] hur the user request to fill
7786 * \param[in] idx index of the item inside the HUR to fill
7787 * \param[in] mntpath mountpoint of Lustre
7788 * \param[in] fname filename (if mtnpath is NULL)
7789 * or FID (if mntpath is set)
7790 * \param[in] last_dev pointer to last device id used
7792 * \retval 0 on success
7793 * \retval CMD_HELP or a negative errno on error
7795 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7796 const char *mntpath, const char *fname,
7799 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7802 hui->hui_extent.length = -1;
7804 if (mntpath != NULL) {
7807 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7811 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7816 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7820 hur->hur_request.hr_itemcount++;
7825 static int lfs_hsm_request(int argc, char **argv, int action)
7827 struct option long_opts[] = {
7828 { .val = 'a', .name = "archive", .has_arg = required_argument },
7829 { .val = 'D', .name = "data", .has_arg = required_argument },
7830 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7831 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7834 char short_opts[] = "l:D:a:m:";
7835 struct hsm_user_request *hur, *oldhur;
7840 char *filelist = NULL;
7841 char fullpath[PATH_MAX];
7842 char *opaque = NULL;
7846 int nbfile_alloc = 0;
7847 char *some_file = NULL;
7848 char *mntpath = NULL;
7854 while ((c = getopt_long(argc, argv, short_opts,
7855 long_opts, NULL)) != -1) {
7864 if (action != HUA_ARCHIVE &&
7865 action != HUA_REMOVE) {
7867 "error: -a is supported only "
7868 "when archiving or removing\n");
7871 archive_id = atoi(optarg);
7874 if (some_file == NULL) {
7876 some_file = strdup(optarg);
7882 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7883 argv[0], argv[optind - 1]);
7888 /* All remaining args are files, so we have at least nbfile */
7889 nbfile = argc - optind;
7891 if ((nbfile == 0) && (filelist == NULL))
7895 opaque_len = strlen(opaque);
7897 /* Alloc the request structure with enough place to store all files
7898 * from command line. */
7899 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7901 fprintf(stderr, "Cannot create the request: %s\n",
7905 nbfile_alloc = nbfile;
7907 hur->hur_request.hr_action = action;
7908 hur->hur_request.hr_archive_id = archive_id;
7909 hur->hur_request.hr_flags = 0;
7911 /* All remaining args are files, add them */
7912 if (nbfile != 0 && some_file == NULL)
7913 some_file = strdup(argv[optind]);
7915 for (i = 0; i < nbfile; i++) {
7916 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7922 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7924 /* If a filelist was specified, read the filelist from it. */
7925 if (filelist != NULL) {
7926 fp = fopen(filelist, "r");
7928 fprintf(stderr, "Cannot read the file list %s: %s\n",
7929 filelist, strerror(errno));
7934 while ((rc = getline(&line, &len, fp)) != -1) {
7935 /* If allocated buffer was too small, get something
7937 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7940 nbfile_alloc = nbfile_alloc * 2 + 1;
7942 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7945 fprintf(stderr, "hsm: cannot allocate "
7946 "the request: %s\n",
7953 size = hur_len(oldhur);
7955 fprintf(stderr, "hsm: cannot allocate "
7956 "%u files + %u bytes data\n",
7957 oldhur->hur_request.hr_itemcount,
7958 oldhur->hur_request.hr_data_len);
7965 memcpy(hur, oldhur, size);
7970 if (line[strlen(line) - 1] == '\n')
7971 line[strlen(line) - 1] = '\0';
7973 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7974 mntpath, line, &last_dev);
7980 if (some_file == NULL) {
7990 /* If a --data was used, add it to the request */
7991 hur->hur_request.hr_data_len = opaque_len;
7993 memcpy(hur_data(hur), opaque, opaque_len);
7995 /* Send the HSM request */
7996 if (realpath(some_file, fullpath) == NULL) {
7997 fprintf(stderr, "Could not find path '%s': %s\n",
7998 some_file, strerror(errno));
8000 rc = llapi_hsm_request(fullpath, hur);
8002 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8003 some_file, strerror(-rc));
8013 static int lfs_hsm_archive(int argc, char **argv)
8015 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8018 static int lfs_hsm_restore(int argc, char **argv)
8020 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8023 static int lfs_hsm_release(int argc, char **argv)
8025 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8028 static int lfs_hsm_remove(int argc, char **argv)
8030 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8033 static int lfs_hsm_cancel(int argc, char **argv)
8035 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8038 static int lfs_swap_layouts(int argc, char **argv)
8043 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8044 SWAP_LAYOUTS_KEEP_MTIME |
8045 SWAP_LAYOUTS_KEEP_ATIME);
8048 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8050 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8052 int lfs_get_mode(const char *string)
8054 enum lock_mode_user mode;
8056 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8057 if (lock_mode_names[mode] == NULL)
8059 if (strcmp(string, lock_mode_names[mode]) == 0)
8066 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8068 enum lu_ladvise_type advice;
8071 advice < ARRAY_SIZE(ladvise_names); advice++) {
8072 if (ladvise_names[advice] == NULL)
8074 if (strcmp(string, ladvise_names[advice]) == 0)
8078 return LU_LADVISE_INVALID;
8081 static int lfs_ladvise(int argc, char **argv)
8083 struct option long_opts[] = {
8084 { .val = 'a', .name = "advice", .has_arg = required_argument },
8085 { .val = 'b', .name = "background", .has_arg = no_argument },
8086 { .val = 'e', .name = "end", .has_arg = required_argument },
8087 { .val = 'l', .name = "length", .has_arg = required_argument },
8088 { .val = 'm', .name = "mode", .has_arg = required_argument },
8089 { .val = 's', .name = "start", .has_arg = required_argument },
8090 { .val = 'u', .name = "unset", .has_arg = no_argument },
8092 char short_opts[] = "a:be:l:m:s:u";
8097 struct llapi_lu_ladvise advice;
8098 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8099 unsigned long long start = 0;
8100 unsigned long long end = LUSTRE_EOF;
8101 unsigned long long length = 0;
8102 unsigned long long size_units;
8103 unsigned long long flags = 0;
8107 while ((c = getopt_long(argc, argv, short_opts,
8108 long_opts, NULL)) != -1) {
8111 advice_type = lfs_get_ladvice(optarg);
8112 if (advice_type == LU_LADVISE_INVALID) {
8113 fprintf(stderr, "%s: invalid advice type "
8114 "'%s'\n", argv[0], optarg);
8115 fprintf(stderr, "Valid types:");
8117 for (advice_type = 0;
8118 advice_type < ARRAY_SIZE(ladvise_names);
8120 if (ladvise_names[advice_type] == NULL)
8122 fprintf(stderr, " %s",
8123 ladvise_names[advice_type]);
8125 fprintf(stderr, "\n");
8138 rc = llapi_parse_size(optarg, &end,
8141 fprintf(stderr, "%s: bad end offset '%s'\n",
8148 rc = llapi_parse_size(optarg, &start,
8151 fprintf(stderr, "%s: bad start offset "
8152 "'%s'\n", argv[0], optarg);
8158 rc = llapi_parse_size(optarg, &length,
8161 fprintf(stderr, "%s: bad length '%s'\n",
8167 mode = lfs_get_mode(optarg);
8169 fprintf(stderr, "%s: bad mode '%s', valid "
8170 "modes are READ or WRITE\n",
8178 fprintf(stderr, "%s: option '%s' unrecognized\n",
8179 argv[0], argv[optind - 1]);
8184 if (advice_type == LU_LADVISE_INVALID) {
8185 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8186 fprintf(stderr, "Valid types:");
8187 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8189 if (ladvise_names[advice_type] == NULL)
8191 fprintf(stderr, " %s", ladvise_names[advice_type]);
8193 fprintf(stderr, "\n");
8197 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8198 fprintf(stderr, "%s: Lock no expand advice is a per file "
8199 "descriptor advice, so when called from lfs, "
8200 "it does nothing.\n", argv[0]);
8204 if (argc <= optind) {
8205 fprintf(stderr, "%s: please give one or more file names\n",
8210 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8211 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8216 if (end == LUSTRE_EOF && length != 0)
8217 end = start + length;
8220 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8221 argv[0], start, end);
8225 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8226 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8231 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8232 fprintf(stderr, "%s: mode is required with lockahead\n",
8237 while (optind < argc) {
8240 path = argv[optind++];
8242 fd = open(path, O_RDONLY);
8244 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8245 argv[0], path, strerror(errno));
8250 advice.lla_start = start;
8251 advice.lla_end = end;
8252 advice.lla_advice = advice_type;
8253 advice.lla_value1 = 0;
8254 advice.lla_value2 = 0;
8255 advice.lla_value3 = 0;
8256 advice.lla_value4 = 0;
8257 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8258 advice.lla_lockahead_mode = mode;
8259 advice.lla_peradvice_flags = flags;
8262 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8265 fprintf(stderr, "%s: cannot give advice '%s' to file "
8266 "'%s': %s\n", argv[0],
8267 ladvise_names[advice_type],
8268 path, strerror(errno));
8274 if (rc == 0 && rc2 < 0)
8280 static const char *const heat_names[] = LU_HEAT_NAMES;
8282 static int lfs_heat_get(int argc, char **argv)
8284 struct lu_heat *heat;
8293 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8295 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8300 while (optind < argc) {
8301 path = argv[optind++];
8303 fd = open(path, O_RDONLY);
8305 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8306 argv[0], path, strerror(errno));
8311 heat->lh_count = OBD_HEAT_COUNT;
8312 rc2 = llapi_heat_get(fd, heat);
8315 fprintf(stderr, "%s: cannot get heat of file '%s'"
8316 ": %s\n", argv[0], path, strerror(errno));
8320 printf("flags: %x\n", heat->lh_flags);
8321 for (i = 0; i < heat->lh_count; i++)
8322 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
8324 if (rc == 0 && rc2 < 0)
8332 static int lfs_heat_set(int argc, char **argv)
8334 struct option long_opts[] = {
8335 { .val = 'c', .name = "clear", .has_arg = no_argument },
8336 { .val = 'o', .name = "off", .has_arg = no_argument },
8337 { .val = 'O', .name = "on", .has_arg = no_argument },
8339 enum lu_heat_flag flags = 0;
8349 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8352 flags |= LU_HEAT_FLAG_CLEAR;
8355 flags |= LU_HEAT_FLAG_CLEAR;
8356 flags |= LU_HEAT_FLAG_OFF;
8359 flags &= ~LU_HEAT_FLAG_OFF;
8364 fprintf(stderr, "%s: option '%s' unrecognized\n",
8365 argv[0], argv[optind - 1]);
8370 if (argc <= optind) {
8371 fprintf(stderr, "%s: please give one or more file names\n",
8376 while (optind < argc) {
8377 path = argv[optind++];
8379 fd = open(path, O_RDONLY);
8381 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8382 argv[0], path, strerror(errno));
8387 rc2 = llapi_heat_set(fd, flags);
8390 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8391 ": %s\n", argv[0], path, strerror(errno));
8395 if (rc == 0 && rc2 < 0)
8401 /** The input string contains a comma delimited list of component ids and
8402 * ranges, for example "1,2-4,7".
8404 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8406 bool end_of_loop = false;
8414 while (!end_of_loop) {
8418 char *endptr = NULL;
8421 ptr = strchrnul(arg, ',');
8422 end_of_loop = *ptr == '\0';
8425 start_index = strtol(arg, &endptr, 0);
8426 if (endptr == arg) /* no data at all */
8428 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8430 if (start_index < 0)
8433 end_index = start_index;
8434 if (*endptr == '-') {
8435 end_index = strtol(endptr + 1, &endptr, 0);
8436 if (*endptr != '\0')
8438 if (end_index < start_index)
8442 for (i = start_index; i <= end_index && size > 0; i++) {
8445 /* remove duplicate */
8446 for (j = 0; j < nr; j++) {
8450 if (j == nr) { /* no duplicate */
8456 if (size == 0 && i < end_index)
8463 if (!end_of_loop && ptr != NULL)
8466 return rc < 0 ? rc : nr;
8470 * struct verify_mirror_id - Mirror id to be verified.
8471 * @mirror_id: A specified mirror id.
8472 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8474 struct verify_mirror_id {
8480 * compare_mirror_ids() - Compare mirror ids.
8481 * @layout: Mirror component list.
8482 * @cbdata: Callback data in verify_mirror_id structure.
8484 * This is a callback function called by llapi_layout_comp_iterate()
8485 * to compare the specified mirror id with the one in the current
8486 * component of @layout. If they are the same, then the specified
8487 * mirror id is valid.
8489 * Return: a negative error code on failure or
8490 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8491 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8494 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8496 struct verify_mirror_id *mirror_id_cbdata =
8497 (struct verify_mirror_id *)cbdata;
8501 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8505 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8506 progname, strerror(errno));
8510 if (mirror_id_cbdata->mirror_id == mirror_id) {
8511 mirror_id_cbdata->is_valid_id = true;
8512 return LLAPI_LAYOUT_ITER_STOP;
8515 return LLAPI_LAYOUT_ITER_CONT;
8519 * verify_mirror_ids() - Verify specified mirror ids.
8520 * @fname: Mirrored file name.
8521 * @mirror_ids: Specified mirror ids to be verified.
8522 * @ids_nr: Number of specified mirror ids.
8524 * This function verifies that specified @mirror_ids are valid
8525 * in the mirrored file @fname.
8527 * Return: 0 on success or a negative error code on failure.
8530 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8532 struct llapi_layout *layout = NULL;
8533 struct verify_mirror_id mirror_id_cbdata = { 0 };
8544 if (stat(fname, &stbuf) < 0) {
8545 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8546 progname, fname, strerror(errno));
8551 if (!S_ISREG(stbuf.st_mode)) {
8552 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8558 fd = open(fname, O_DIRECT | O_RDONLY);
8560 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8561 progname, fname, strerror(errno));
8566 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8568 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8569 progname, fname, strerror(errno));
8573 layout = llapi_layout_get_by_fd(fd, 0);
8574 if (layout == NULL) {
8575 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8576 progname, fname, strerror(errno));
8578 llapi_lease_release(fd);
8582 rc = llapi_layout_flags_get(layout, &flr_state);
8584 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8585 progname, fname, strerror(errno));
8590 flr_state &= LCM_FL_FLR_MASK;
8591 switch (flr_state) {
8594 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8595 progname, fname, llapi_layout_flags_string(flr_state));
8602 for (i = 0; i < ids_nr; i++) {
8603 mirror_id_cbdata.mirror_id = mirror_ids[i];
8604 mirror_id_cbdata.is_valid_id = false;
8606 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8611 "%s: '%s' failed to verify mirror id: %u.\n",
8612 progname, fname, mirror_ids[i]);
8616 if (!mirror_id_cbdata.is_valid_id) {
8619 "%s: '%s' invalid specified mirror id: %u.\n",
8620 progname, fname, mirror_ids[i]);
8626 llapi_layout_free(layout);
8627 llapi_lease_release(fd);
8635 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8636 __u16 *mirror_ids, int ids_nr)
8638 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8639 struct llapi_layout *layout;
8649 if (stat(fname, &stbuf) < 0) {
8650 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8651 progname, fname, strerror(errno));
8655 if (!S_ISREG(stbuf.st_mode)) {
8656 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8662 fd = open(fname, O_DIRECT | O_RDWR);
8664 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8665 progname, fname, strerror(errno));
8670 layout = llapi_layout_get_by_fd(fd, 0);
8671 if (layout == NULL) {
8672 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8673 progname, fname, strerror(errno));
8678 rc = llapi_layout_flags_get(layout, &flr_state);
8680 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8681 progname, fname, strerror(errno));
8686 flr_state &= LCM_FL_FLR_MASK;
8687 if (flr_state == LCM_FL_NONE) {
8689 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8694 /* get stale component info */
8695 comp_size = llapi_mirror_find_stale(layout, comp_array,
8696 ARRAY_SIZE(comp_array),
8697 mirror_ids, ids_nr);
8698 if (comp_size <= 0) {
8703 ioc->lil_mode = LL_LEASE_WRLCK;
8704 ioc->lil_flags = LL_LEASE_RESYNC;
8705 rc = llapi_lease_set(fd, ioc);
8707 if (rc == -EALREADY)
8711 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8712 progname, fname, strerror(errno));
8716 /* get the read range [start, end) */
8717 start = comp_array[0].lrc_start;
8718 end = comp_array[0].lrc_end;
8719 for (idx = 1; idx < comp_size; idx++) {
8720 if (comp_array[idx].lrc_start < start)
8721 start = comp_array[idx].lrc_start;
8722 if (end < comp_array[idx].lrc_end)
8723 end = comp_array[idx].lrc_end;
8726 rc = llapi_lease_check(fd);
8727 if (rc != LL_LEASE_WRLCK) {
8728 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8733 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8736 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8737 progname, fname, rc);
8739 /* prepare ioc for lease put */
8740 ioc->lil_mode = LL_LEASE_UNLCK;
8741 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8743 for (idx = 0; idx < comp_size; idx++) {
8744 if (comp_array[idx].lrc_synced) {
8745 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8750 rc = llapi_lease_set(fd, ioc);
8752 if (rc == 0) /* lost lease lock */
8754 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8755 progname, fname, strerror(errno));
8759 * llapi_lease_set returns lease mode when it request to unlock
8765 llapi_layout_free(layout);
8772 static inline int lfs_mirror_resync(int argc, char **argv)
8774 struct ll_ioc_lease *ioc = NULL;
8775 __u16 mirror_ids[128] = { 0 };
8780 struct option long_opts[] = {
8781 { .val = 'o', .name = "only", .has_arg = required_argument },
8784 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8787 rc = parse_mirror_ids(mirror_ids,
8788 sizeof(mirror_ids) / sizeof(__u16),
8792 "%s: bad mirror ids '%s'.\n",
8799 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8800 argv[0], argv[optind - 1]);
8806 if (argc == optind) {
8807 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8812 if (ids_nr > 0 && argc > optind + 1) {
8814 "%s: option '--only' cannot be used upon multiple files.\n",
8822 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8827 /* set the lease on the file */
8828 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8830 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8831 argv[0], strerror(errno));
8836 for (; optind < argc; optind++) {
8837 rc = lfs_mirror_resync_file(argv[optind], ioc,
8838 mirror_ids, ids_nr);
8839 /* ignore previous file's error, continue with next file */
8842 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8850 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8852 struct llapi_layout *layout;
8855 layout = llapi_layout_get_by_fd(fd, 0);
8856 if (layout == NULL) {
8857 fprintf(stderr, "could not get layout.\n");
8861 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8863 fprintf(stderr, "failed to iterate layout\n");
8864 llapi_layout_free(layout);
8867 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8868 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8869 llapi_layout_free(layout);
8873 llapi_layout_free(layout);
8879 * Check whether two files are the same file
8880 * \retval 0 same file
8881 * \retval 1 not the same file
8882 * \retval <0 error code
8884 static inline int check_same_file(const char *f1, const char *f2)
8889 if (stat(f1, &stbuf1) < 0) {
8890 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8891 progname, f1, strerror(errno));
8895 if (stat(f2, &stbuf2) < 0) {
8896 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8897 progname, f2, strerror(errno));
8901 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8902 stbuf1.st_ino == stbuf2.st_ino)
8908 static inline int lfs_mirror_read(int argc, char **argv)
8911 __u16 mirror_id = 0;
8912 const char *outfile = NULL;
8918 const size_t buflen = 4 << 20;
8920 struct option long_opts[] = {
8921 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8922 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8925 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8930 mirror_id = strtoul(optarg, &end, 0);
8931 if (*end != '\0' || mirror_id == 0) {
8933 "%s %s: invalid mirror ID '%s'\n",
8934 progname, argv[0], optarg);
8942 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8943 progname, argv[optind - 1]);
8948 if (argc == optind) {
8949 fprintf(stderr, "%s %s: no mirrored file provided\n",
8952 } else if (argc > optind + 1) {
8953 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8957 if (mirror_id == 0) {
8958 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8963 /* open mirror file */
8964 fname = argv[optind];
8967 rc = check_same_file(fname, outfile);
8970 "%s %s: output file cannot be the mirrored file\n",
8978 fd = open(fname, O_DIRECT | O_RDONLY);
8980 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8981 progname, argv[0], fname, strerror(errno));
8985 /* verify mirror id */
8986 rc = verify_mirror_id_by_fd(fd, mirror_id);
8989 "%s %s: cannot find mirror with ID %u in '%s'\n",
8990 progname, argv[0], mirror_id, fname);
8994 /* open output file */
8996 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8998 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8999 progname, argv[0], outfile, strerror(errno));
9004 outfd = STDOUT_FILENO;
9007 /* allocate buffer */
9008 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9010 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9011 progname, argv[0], rc);
9018 ssize_t written = 0;
9020 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9021 if (bytes_read < 0) {
9024 "%s %s: fail to read data from mirror %u: %s\n",
9025 progname, argv[0], mirror_id, strerror(-rc));
9030 if (bytes_read == 0)
9033 while (written < bytes_read) {
9036 written2 = write(outfd, buf + written,
9037 bytes_read - written);
9040 "%s %s: fail to write %s: %s\n",
9041 progname, argv[0], outfile ? : "STDOUT",
9046 written += written2;
9049 if (written != bytes_read) {
9051 "%s %s: written %ld bytes does not match with %ld read.\n",
9052 progname, argv[0], written, bytes_read);
9074 static inline int lfs_mirror_write(int argc, char **argv)
9077 __u16 mirror_id = 0;
9078 const char *inputfile = NULL;
9084 const size_t buflen = 4 << 20;
9086 size_t page_size = sysconf(_SC_PAGESIZE);
9087 struct ll_ioc_lease_id ioc;
9089 struct option long_opts[] = {
9090 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9091 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9094 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9099 mirror_id = strtoul(optarg, &end, 0);
9100 if (*end != '\0' || mirror_id == 0) {
9102 "%s %s: invalid mirror ID '%s'\n",
9103 progname, argv[0], optarg);
9111 fprintf(stderr, "%s: option '%s' unrecognized\n",
9112 progname, argv[optind - 1]);
9117 if (argc == optind) {
9118 fprintf(stderr, "%s %s: no mirrored file provided\n",
9121 } else if (argc > optind + 1) {
9122 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9126 if (mirror_id == 0) {
9127 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9132 /* open mirror file */
9133 fname = argv[optind];
9136 rc = check_same_file(fname, inputfile);
9139 "%s %s: input file cannot be the mirrored file\n",
9147 fd = open(fname, O_DIRECT | O_WRONLY);
9149 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9150 progname, argv[0], fname, strerror(errno));
9154 /* verify mirror id */
9155 rc = verify_mirror_id_by_fd(fd, mirror_id);
9158 "%s %s: cannot find mirror with ID %u in '%s'\n",
9159 progname, argv[0], mirror_id, fname);
9163 /* open input file */
9165 inputfd = open(inputfile, O_RDONLY, 0644);
9167 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9168 progname, argv[0], inputfile, strerror(errno));
9173 inputfd = STDIN_FILENO;
9176 /* allocate buffer */
9177 rc = posix_memalign(&buf, page_size, buflen);
9179 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9180 progname, argv[0], rc);
9184 /* prepare target mirror components instantiation */
9185 ioc.lil_mode = LL_LEASE_WRLCK;
9186 ioc.lil_flags = LL_LEASE_RESYNC;
9187 ioc.lil_mirror_id = mirror_id;
9188 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9191 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9192 progname, argv[0], fname, strerror(errno));
9202 rc = llapi_lease_check(fd);
9203 if (rc != LL_LEASE_WRLCK) {
9204 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9205 progname, argv[0], fname);
9209 bytes_read = read(inputfd, buf, buflen);
9210 if (bytes_read < 0) {
9213 "%s %s: fail to read data from '%s': %s\n",
9214 progname, argv[0], inputfile ? : "STDIN",
9221 if (bytes_read == 0)
9224 /* round up to page align to make direct IO happy. */
9225 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9227 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9232 "%s %s: fail to write to mirror %u: %s\n",
9233 progname, argv[0], mirror_id,
9241 if (pos & (page_size - 1)) {
9242 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9247 ioc.lil_mode = LL_LEASE_UNLCK;
9248 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9250 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9255 "%s %s: release lease lock of '%s' failed: %s\n",
9256 progname, argv[0], fname, strerror(errno));
9273 struct collect_ids_data {
9279 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
9281 struct collect_ids_data *cid = cbdata;
9285 rc = llapi_layout_mirror_id_get(layout, &id);
9289 if ((__u16)id != cid->cid_exclude) {
9292 for (i = 0; i < cid->cid_count; i++) {
9293 /* already collected the mirror id */
9294 if (id == cid->cid_ids[i])
9295 return LLAPI_LAYOUT_ITER_CONT;
9297 cid->cid_ids[cid->cid_count] = id;
9301 return LLAPI_LAYOUT_ITER_CONT;
9304 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9306 struct llapi_layout *layout;
9307 struct collect_ids_data cid = { .cid_ids = ids,
9309 .cid_exclude = exclude_id, };
9312 layout = llapi_layout_get_by_fd(fd, 0);
9313 if (layout == NULL) {
9314 fprintf(stderr, "could not get layout\n");
9318 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9320 fprintf(stderr, "failed to iterate layout\n");
9321 llapi_layout_free(layout);
9325 llapi_layout_free(layout);
9327 return cid.cid_count;
9330 static inline int lfs_mirror_copy(int argc, char **argv)
9333 __u16 read_mirror_id = 0;
9334 __u16 ids[128] = { 0 };
9336 struct llapi_layout *layout = NULL;
9337 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9344 struct ll_ioc_lease *ioc = NULL;
9345 struct ll_ioc_lease_id *resync_ioc;
9347 struct option long_opts[] = {
9348 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9349 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9352 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9357 read_mirror_id = strtoul(optarg, &end, 0);
9358 if (*end != '\0' || read_mirror_id == 0) {
9360 "%s %s: invalid read mirror ID '%s'\n",
9361 progname, argv[0], optarg);
9366 if (!strcmp(optarg, "-1")) {
9367 /* specify all other mirrors */
9371 count = parse_mirror_ids((__u16 *)ids,
9379 fprintf(stderr, "%s: option '%s' unrecognized\n",
9380 progname, argv[optind - 1]);
9385 if (argc == optind) {
9386 fprintf(stderr, "%s %s: no mirrored file provided\n",
9389 } else if (argc > optind + 1) {
9390 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9394 if (read_mirror_id == 0) {
9396 "%s %s: no valid read mirror ID %d is provided\n",
9397 progname, argv[0], read_mirror_id);
9403 "%s %s: no write mirror ID is provided\n",
9408 for (i = 0; i < count; i++) {
9409 if (read_mirror_id == ids[i]) {
9411 "%s %s: read and write mirror ID cannot be the same\n",
9417 /* open mirror file */
9418 fname = argv[optind];
9420 fd = open(fname, O_DIRECT | O_RDWR);
9422 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9423 progname, argv[0], fname, strerror(errno));
9427 /* write to all other mirrors */
9428 if (ids[0] == (__u16)-1) {
9429 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9433 "%s %s: failed to get other mirror ids in '%s': %d\n",
9434 progname, argv[0], fname, rc);
9439 /* verify mirror id */
9440 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9443 "%s %s: cannot find mirror with ID %u in '%s'\n",
9444 progname, argv[0], read_mirror_id, fname);
9448 for (i = 0; i < count; i++) {
9449 rc = verify_mirror_id_by_fd(fd, ids[i]);
9452 "%s %s: cannot find mirror with ID %u in '%s'\n",
9453 progname, argv[0], ids[i], fname);
9458 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9461 "%s %s: cannot alloc comp id array for ioc: %s\n",
9462 progname, argv[0], strerror(errno));
9467 /* get stale component info */
9468 layout = llapi_layout_get_by_fd(fd, 0);
9469 if (layout == NULL) {
9470 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9471 progname, argv[0], fname, strerror(errno));
9475 comp_size = llapi_mirror_find_stale(layout, comp_array,
9476 ARRAY_SIZE(comp_array),
9478 llapi_layout_free(layout);
9479 if (comp_size < 0) {
9484 /* prepare target mirror components instantiation */
9485 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9486 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9487 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9489 resync_ioc->lil_mirror_id = ids[0];
9491 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9492 rc = llapi_lease_set(fd, ioc);
9495 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9496 progname, argv[0], fname, strerror(errno));
9500 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9503 fprintf(stderr, "%s %s: copy error: %d\n",
9504 progname, argv[0], rc);
9508 fprintf(stdout, "mirror copied successfully: ");
9509 for (i = 0; i < copied; i++)
9510 fprintf(stdout, "%d ", ids[i]);
9511 fprintf(stdout, "\n");
9513 ioc->lil_mode = LL_LEASE_UNLCK;
9514 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9516 for (i = 0; i < comp_size; i++) {
9519 for (j = 0; j < copied; j++) {
9520 if (comp_array[i].lrc_mirror_id != ids[j])
9523 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9527 rc = llapi_lease_set(fd, ioc);
9532 "%s %s: release lease lock of '%s' failed: %s\n",
9533 progname, argv[0], fname, strerror(errno));
9547 * struct verify_chunk - Mirror chunk to be verified.
9548 * @chunk: [start, end) of the chunk.
9549 * @mirror_count: Number of mirror ids in @mirror_id array.
9550 * @mirror_id: Array of valid mirror ids that cover the chunk.
9552 struct verify_chunk {
9553 struct lu_extent chunk;
9554 unsigned int mirror_count;
9555 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9559 * print_chunks() - Print chunk information.
9560 * @fname: Mirrored file name.
9561 * @chunks: Array of chunks.
9562 * @chunk_count: Number of chunks in @chunks array.
9564 * This function prints [start, end) of each chunk in @chunks
9565 * for mirrored file @fname, and also prints the valid mirror ids
9566 * that cover the chunk.
9571 void print_chunks(const char *fname, struct verify_chunk *chunks,
9577 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9578 for (i = 0; i < chunk_count; i++) {
9579 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9581 if (chunks[i].mirror_count == 0)
9582 fprintf(stdout, "\t[");
9584 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9585 for (j = 1; j < chunks[i].mirror_count; j++)
9586 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9588 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9590 fprintf(stdout, "\n");
9594 * print_checksums() - Print CRC-32 checksum values.
9595 * @chunk: A chunk and its corresponding valid mirror ids.
9596 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9598 * This function prints CRC-32 checksum values on @chunk for
9599 * each valid mirror that covers it.
9604 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9609 "CRC-32 checksum value for chunk "DEXT":\n",
9610 PEXT(&chunk->chunk));
9611 for (i = 0; i < chunk->mirror_count; i++)
9612 fprintf(stdout, "Mirror %u:\t%#lx\n",
9613 chunk->mirror_id[i], crc[i]);
9614 fprintf(stdout, "\n");
9618 * filter_mirror_id() - Filter specified mirror ids.
9619 * @chunks: Array of chunks.
9620 * @chunk_count: Number of chunks in @chunks array.
9621 * @mirror_ids: Specified mirror ids to be verified.
9622 * @ids_nr: Number of specified mirror ids.
9624 * This function scans valid mirror ids that cover each chunk in @chunks
9625 * and filters specified mirror ids.
9630 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9631 __u16 *mirror_ids, int ids_nr)
9636 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9637 unsigned int valid_count = 0;
9639 for (i = 0; i < chunk_count; i++) {
9640 if (chunks[i].mirror_count == 0)
9644 for (j = 0; j < ids_nr; j++) {
9645 for (k = 0; k < chunks[i].mirror_count; k++) {
9646 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9647 valid_id[valid_count] = mirror_ids[j];
9654 memcpy(chunks[i].mirror_id, valid_id,
9655 sizeof(__u16) * valid_count);
9656 chunks[i].mirror_count = valid_count;
9661 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9662 * @layout: Mirror component list.
9663 * @chunks: Array of chunks.
9664 * @chunks_size: Array size of @chunks.
9666 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9667 * to find out chunk segments and store them in @chunks array.
9669 * The @mirror_id array in each element of @chunks will store the valid
9670 * mirror ids that cover the chunk. If a mirror component covering the
9671 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9672 * will not be stored into the @mirror_id array, and the chunk for that
9673 * mirror will not be verified.
9675 * The @mirror_count in each element of @chunks will store the number of
9676 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9677 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9678 * indicates the chunk is valid in only one mirror. In both cases, the
9679 * chunk will not be verified.
9681 * Here is an example:
9684 * +------+-------------+--------------------+
9686 * +------+------+------+------+-------------+
9687 * | | S | S | | mirror2
9688 * +-------------+------+------+-------------+
9690 * prepared @chunks array will contain 5 elements:
9691 * (([0, 1M), [1, 2], 2),
9692 * ([1M, 2M), [1, 2], 2),
9693 * ([2M, 3M), [1], 1),
9694 * ([3M, 4M], [], 0),
9695 * ([4M, EOF), [2], 1))
9697 * Return: the actual array size of @chunks on success
9698 * or a negative error code on failure.
9701 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9702 struct verify_chunk *chunks,
9713 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9716 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9719 "%s: move to the first layout component: %s.\n",
9720 progname, strerror(errno));
9726 chunks[idx].chunk.e_end = LUSTRE_EOF;
9728 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9731 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9732 progname, strerror(errno));
9736 if (start > chunks[idx].chunk.e_start ||
9737 end <= chunks[idx].chunk.e_start)
9740 if (end < chunks[idx].chunk.e_end)
9741 chunks[idx].chunk.e_end = end;
9743 rc = llapi_layout_comp_flags_get(layout, &flags);
9746 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9747 progname, strerror(errno));
9751 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9754 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9757 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9758 progname, strerror(errno));
9762 chunks[idx].mirror_id[i] = mirror_id;
9764 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9766 "%s: mirror_id array is too small.\n",
9773 rc = llapi_layout_comp_use(layout,
9774 LLAPI_LAYOUT_COMP_USE_NEXT);
9777 "%s: move to the next layout component: %s.\n",
9778 progname, strerror(errno));
9781 } /* loop through all components */
9783 chunks[idx].mirror_count = i;
9785 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9789 if (idx >= chunks_size) {
9790 fprintf(stderr, "%s: chunks array is too small.\n",
9796 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9800 return rc < 0 ? rc : idx + 1;
9804 * lfs_mirror_verify_chunk() - Verify a chunk.
9805 * @fd: File descriptor of the mirrored file.
9806 * @file_size: Size of the mirrored file.
9807 * @chunk: A chunk and its corresponding valid mirror ids.
9808 * @verbose: Verbose mode.
9810 * This function verifies a @chunk contains exactly the same data
9811 * ammong the mirrors that cover it.
9813 * If @verbose is specified, then the function will print where the
9814 * differences are if the data do not match. Otherwise, it will
9815 * just return an error in that case.
9817 * Return: 0 on success or a negative error code on failure.
9820 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9821 struct verify_chunk *chunk, int verbose)
9823 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9825 size_t page_size = sysconf(_SC_PAGESIZE);
9831 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9838 rc = posix_memalign(&buf, page_size, buflen);
9839 if (rc) /* error code is returned directly */
9843 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9844 PEXT(&chunk->chunk));
9845 for (i = 0; i < chunk->mirror_count; i++)
9846 fprintf(stdout, " %u", chunk->mirror_id[i]);
9847 fprintf(stdout, "\n");
9851 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9852 pos = chunk->chunk.e_start;
9853 while (bytes_done < count) {
9854 /* compute initial CRC-32 checksum */
9855 crc = crc32(0L, Z_NULL, 0);
9856 memset(crc_array, 0, sizeof(crc_array));
9859 for (i = 0; i < chunk->mirror_count; i++) {
9860 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9862 if (bytes_read < 0) {
9865 "%s: failed to read data from mirror %u: %s.\n",
9866 progname, chunk->mirror_id[i],
9871 /* compute new CRC-32 checksum */
9872 crc_array[i] = crc32(crc, buf, bytes_read);
9876 print_checksums(chunk, crc_array);
9878 /* compare CRC-32 checksum values */
9879 for (i = 1; i < chunk->mirror_count; i++) {
9880 if (crc_array[i] != crc_array[0]) {
9884 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9885 progname, PEXT(&chunk->chunk),
9886 chunk->mirror_id[0],
9887 chunk->mirror_id[i]);
9892 bytes_done += bytes_read;
9895 if (verbose > 1 && rc == 0) {
9896 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9897 PEXT(&chunk->chunk));
9898 for (i = 0; i < chunk->mirror_count; i++)
9899 fprintf(stdout, " %u", chunk->mirror_id[i]);
9900 fprintf(stdout, " PASS\n\n");
9909 * lfs_mirror_verify_file() - Verify a mirrored file.
9910 * @fname: Mirrored file name.
9911 * @mirror_ids: Specified mirror ids to be verified.
9912 * @ids_nr: Number of specified mirror ids.
9913 * @verbose: Verbose mode.
9915 * This function verifies that each SYNC mirror of a mirrored file
9916 * specified by @fname contains exactly the same data.
9918 * If @mirror_ids is specified, then the function will verify the
9919 * mirrors specified by @mirror_ids contain exactly the same data.
9921 * If @verbose is specified, then the function will print where the
9922 * differences are if the data do not match. Otherwise, it will
9923 * just return an error in that case.
9925 * Return: 0 on success or a negative error code on failure.
9928 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9931 struct verify_chunk chunks_array[1024] = { };
9932 struct llapi_layout *layout = NULL;
9936 int chunk_count = 0;
9942 if (stat(fname, &stbuf) < 0) {
9943 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9944 progname, fname, strerror(errno));
9949 if (!S_ISREG(stbuf.st_mode)) {
9950 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9956 if (stbuf.st_size == 0) {
9958 fprintf(stdout, "%s: '%s' file size is 0.\n",
9964 fd = open(fname, O_DIRECT | O_RDONLY);
9966 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9967 progname, fname, strerror(errno));
9972 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9974 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9975 progname, fname, strerror(errno));
9979 layout = llapi_layout_get_by_fd(fd, 0);
9980 if (layout == NULL) {
9981 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9982 progname, fname, strerror(errno));
9984 llapi_lease_release(fd);
9988 rc = llapi_layout_flags_get(layout, &flr_state);
9990 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9991 progname, fname, strerror(errno));
9996 flr_state &= LCM_FL_FLR_MASK;
9997 switch (flr_state) {
10000 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10001 progname, fname, llapi_layout_flags_string(flr_state));
10007 /* find out mirror chunks to be verified */
10008 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10009 ARRAY_SIZE(chunks_array));
10010 if (chunk_count < 0) {
10016 /* filter specified mirror ids */
10017 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10020 print_chunks(fname, chunks_array, chunk_count);
10022 for (idx = 0; idx < chunk_count; idx++) {
10023 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10026 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10028 PEXT(&chunks_array[idx].chunk),
10029 (unsigned long long)stbuf.st_size);
10033 if (chunks_array[idx].mirror_count == 0) {
10035 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10037 PEXT(&chunks_array[idx].chunk));
10039 fprintf(stderr, "skipped\n");
10043 fprintf(stderr, "failed\n");
10047 if (chunks_array[idx].mirror_count == 1) {
10050 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10052 PEXT(&chunks_array[idx].chunk),
10053 chunks_array[idx].mirror_id[0]);
10057 rc = llapi_lease_check(fd);
10058 if (rc != LL_LEASE_RDLCK) {
10059 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10064 /* verify one chunk */
10065 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10066 &chunks_array[idx], verbose);
10080 llapi_layout_free(layout);
10081 llapi_lease_release(fd);
10089 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10090 * @argc: The count of lfs mirror verify command line arguments.
10091 * @argv: Array of strings for lfs mirror verify command line arguments.
10093 * This function parses lfs mirror verify command and verifies the
10094 * specified mirrored file(s).
10096 * Return: 0 on success or a negative error code on failure.
10098 static inline int lfs_mirror_verify(int argc, char **argv)
10100 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10106 char cmd[PATH_MAX];
10108 struct option long_opts[] = {
10109 { .val = 'o', .name = "only", .has_arg = required_argument },
10110 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10111 { .name = NULL } };
10113 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10115 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10118 rc = parse_mirror_ids(mirror_ids,
10119 ARRAY_SIZE(mirror_ids),
10123 "%s: bad mirror ids '%s'.\n",
10130 "%s: at least 2 mirror ids needed with '--only' option.\n",
10140 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10141 progname, argv[optind - 1]);
10147 if (argc == optind) {
10148 fprintf(stderr, "%s: no file name given.\n", progname);
10153 if (ids_nr > 0 && argc > optind + 1) {
10155 "%s: '--only' cannot be used upon multiple files.\n",
10163 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10169 for (; optind < argc; optind++) {
10170 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10180 * lfs_mirror() - Parse and execute lfs mirror commands.
10181 * @argc: The count of lfs mirror command line arguments.
10182 * @argv: Array of strings for lfs mirror command line arguments.
10184 * This function parses lfs mirror commands and performs the
10185 * corresponding functions specified in mirror_cmdlist[].
10187 * Return: 0 on success or an error code on failure.
10189 static int lfs_mirror(int argc, char **argv)
10191 char cmd[PATH_MAX];
10194 setlinebuf(stdout);
10196 Parser_init("lfs-mirror > ", mirror_cmdlist);
10198 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10200 program_invocation_short_name = cmd;
10202 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10204 rc = Parser_commands();
10206 return rc < 0 ? -rc : rc;
10209 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10211 #if __BYTE_ORDER == __BIG_ENDIAN
10212 __swab16s(&attrs->lsa_valid);
10213 __swab64s(&attrs->lsa_size);
10214 __swab64s(&attrs->lsa_blocks);
10218 enum lfs_som_type {
10219 LFS_SOM_SIZE = 0x1,
10220 LFS_SOM_BLOCKS = 0x2,
10221 LFS_SOM_FLAGS = 0x4,
10222 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10226 static int lfs_getsom(int argc, char **argv)
10229 struct lustre_som_attrs *attrs;
10230 char buf[sizeof(*attrs) + 64];
10231 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10234 while ((c = getopt(argc, argv, "sbf")) != -1) {
10237 type = LFS_SOM_SIZE;
10240 type = LFS_SOM_BLOCKS;
10243 type = LFS_SOM_FLAGS;
10246 fprintf(stderr, "%s: invalid option '%c'\n",
10256 fprintf(stderr, "%s: %s\n",
10257 progname, argc == 0 ? "miss file target" :
10258 "input more than 2 files");
10263 attrs = (void *)buf;
10264 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10267 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10268 argv[0], strerror(errno), errno);
10272 lustre_som_swab(attrs);
10275 case LFS_SOM_ATTR_ALL:
10276 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10277 path, attrs->lsa_size, attrs->lsa_blocks,
10281 printf("%llu\n", attrs->lsa_size);
10283 case LFS_SOM_BLOCKS:
10284 printf("%llu\n", attrs->lsa_blocks);
10286 case LFS_SOM_FLAGS:
10287 printf("%x\n", attrs->lsa_valid);
10290 fprintf(stderr, "%s: unknown option\n", progname);
10298 * lfs_mirror_list_commands() - List lfs mirror commands.
10299 * @argc: The count of command line arguments.
10300 * @argv: Array of strings for command line arguments.
10302 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10304 * Return: 0 on success.
10306 static int lfs_mirror_list_commands(int argc, char **argv)
10308 char buffer[81] = "";
10310 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10316 static int lfs_list_commands(int argc, char **argv)
10318 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
10320 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
10325 int main(int argc, char **argv)
10329 /* Ensure that liblustreapi constructor has run */
10330 if (!llapi_liblustreapi_initialized())
10331 fprintf(stderr, "liblustreapi was not properly initialized\n");
10333 setlinebuf(stdout);
10336 Parser_init("lfs > ", cmdlist);
10338 progname = program_invocation_short_name; /* Used in error messages */
10340 llapi_set_command_name(argv[1]);
10341 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
10342 llapi_clear_command_name();
10344 rc = Parser_commands();
10347 return rc < 0 ? -rc : rc;
10350 #ifdef _LUSTRE_IDL_H_
10351 /* Everything we need here should be included by lustreapi.h. */
10352 # error "lfs should not depend on lustre_idl.h"
10353 #endif /* _LUSTRE_IDL_H_ */