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 " [--stripe-index|-i <start_ost_idx>]\n" \
168 " [--stripe-size|-S <stripe_size>]\n" \
169 " [--layout|-L <pattern>]\n" \
170 " [--pool|-p <pool_name>]\n" \
171 " [--ost|-o <ost_indices>]\n" \
172 " [--yaml|-y <yaml_template_file>]\n" \
173 " [--copy=<lustre_src>]\n"
175 #define SSM_HELP_COMMON \
176 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
177 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
178 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
179 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
180 "\t respectively)\n" \
181 "\tpool_name: Name of OST pool to use (default none)\n" \
182 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
183 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
184 "\t Indices be specified in a format of:\n" \
185 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
187 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
188 "\t If --pool is set with --ost, then the OSTs\n" \
189 "\t must be the members of the pool.\n" \
190 "\tcomp_end: Extent end of component, start after previous end.\n"\
191 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
192 "\t respectively, -1 for EOF). Must be a multiple of\n"\
193 "\t stripe_size.\n" \
194 "\tyaml_template_file:\n" \
195 "\t YAML layout template file, can't be used with -c,\n" \
196 "\t -i, -S, -p, -o, or -E arguments.\n" \
197 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
198 "\t another lustre file or directory, can't used with\n" \
199 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
201 #define MIRROR_CREATE_HELP \
202 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
203 "\t setstripe layout options\n" \
204 "\t It defaults to 1 if not specified; if specified,\n" \
205 "\t it must follow the option without a space.\n" \
206 "\t The option can also be repeated multiple times to\n" \
207 "\t separate mirrors that have different layouts.\n" \
208 "\tsetstripe options: Mirror layout\n" \
209 "\t It can be a plain layout or a composite layout.\n" \
210 "\t If not specified, the stripe options inherited\n" \
211 "\t from the previous component will be used.\n" \
212 "\tflags: set flags to the component of the current mirror.\n" \
213 "\t Only \"prefer\" flag is supported so far.\n"
215 #define MIRROR_EXTEND_HELP \
217 "\tvictim_file: The layout of victim_file will be split and used\n" \
218 "\t as a mirror added to the mirrored file.\n" \
219 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
220 "\t from victim file(s) in case the victim file(s)\n" \
221 "\t contains the same data as the original mirrored\n" \
224 #define MIRROR_EXTEND_USAGE \
225 " <--mirror-count|-N[mirror_count]>\n" \
226 " [setstripe options|-f <victim_file>]\n" \
229 #define SETSTRIPE_USAGE \
230 SSM_CMD_COMMON("setstripe") \
231 MIRROR_EXTEND_USAGE \
232 " <directory|filename>\n" \
236 #define MIGRATE_USAGE \
237 SSM_CMD_COMMON("migrate ") \
238 " [--block|-b] [--non-block|-n]\n" \
239 " [--non-direct|-D] [--verbose|-v]\n" \
243 "\tblock: Block file access during data migration (default)\n" \
244 "\tnon-block: Abort migrations if concurrent access is detected\n" \
245 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
246 "\tverbose: Print each filename as it is migrated\n" \
248 #define SETDIRSTRIPE_USAGE \
249 " [--mdt-count|-c stripe_count>\n" \
250 " [--mdt-hash|-H mdt_hash]\n" \
251 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
252 " [--default|-D] [--mode|-o mode] <dir>\n" \
253 "\tstripe_count: stripe count of the striped directory\n" \
254 "\tmdt_index: MDT index of first stripe\n" \
255 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
256 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
257 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
258 "\tdefault_stripe: set default dirstripe of the directory\n" \
259 "\tmode: the file access permission of the directory (octal)\n"
262 * command_t mirror_cmdlist - lfs mirror commands.
264 command_t mirror_cmdlist[] = {
265 { .pc_name = "create", .pc_func = lfs_mirror_create,
266 .pc_help = "Create a mirrored file.\n"
267 "usage: lfs mirror create "
268 "<--mirror-count|-N[mirror_count]> "
269 "[setstripe options] ... <filename|directory>\n"
270 MIRROR_CREATE_HELP },
271 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
272 .pc_help = "Extend a mirrored file.\n"
273 "usage: lfs mirror extend "
274 "<--mirror-count|-N[mirror_count]> [--no-verify] "
275 "[setstripe options|-f <victim_file>] ... <filename>\n"
276 MIRROR_EXTEND_HELP },
277 { .pc_name = "split", .pc_func = lfs_mirror_split,
278 .pc_help = "Split a mirrored file.\n"
279 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
280 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
281 "\t [-f <new_file>] <mirrored file>\n"
282 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
283 "\t can be fetched by lfs getstripe command.\n"
284 "\tcomp_id: Unique component ID within a mirror.\n"
285 "\tnew_file: This option indicates the layout of the split\n"
286 "\t mirror will be stored into. If not specified,\n"
287 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
288 "\t will be used.\n" },
289 { .pc_name = "read", .pc_func = lfs_mirror_read,
290 .pc_help = "Read the content of a specified mirror of a file.\n"
291 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
292 "[--outfile|-o <output_file>] <mirrored_file>\n" },
293 { .pc_name = "write", .pc_func = lfs_mirror_write,
294 .pc_help = "Write to a specified mirror of a file.\n"
295 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
296 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
297 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
298 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
299 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
300 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
301 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
302 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
303 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
304 "<mirrored file> [<mirrored file2>...]\n"},
305 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
306 .pc_help = "Verify mirrored file(s).\n"
307 "usage: lfs mirror verify "
308 "[--only <mirror_id,mirror_id2[,...]>] "
309 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
310 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
311 .pc_help = "list commands supported by lfs mirror"},
312 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
313 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
314 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
318 /* all available commands */
319 command_t cmdlist[] = {
320 {"setstripe", lfs_setstripe, 0,
321 "To create a file with specified striping/composite layout, or\n"
322 "create/replace the default layout on an existing directory:\n"
323 SSM_CMD_COMMON("setstripe")
324 " <directory|filename>\n"
326 "To add component(s) to an existing composite file:\n"
327 SSM_CMD_COMMON("setstripe --component-add")
329 "To totally delete the default striping from an existing directory:\n"
330 "usage: setstripe -d <directory>\n"
332 "To create a mirrored file or set s default mirror layout on a directory:\n"
333 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
335 "To delete the last component(s) from an existing composite file\n"
336 "(note that this will also delete any data in those components):\n"
337 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
338 " [--component-flags|-F <comp_flags>]\n"
340 "\tcomp_id: Unique component ID to delete\n"
341 "\tcomp_flags: 'init' indicating all instantiated components\n"
342 "\t '^init' indicating all uninstantiated components\n"
343 "\t-I and -F cannot be specified at the same time\n"},
344 {"getstripe", lfs_getstripe, 0,
345 "To list the layout pattern for a given file or files in a\n"
346 "directory or recursively for all files in a directory tree.\n"
347 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
348 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
349 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
350 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
351 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
352 " [--component-id[=comp_id]|-I[comp_id]]\n"
353 " [--component-flags[=comp_flags]]\n"
354 " [--component-count]\n"
355 " [--component-start[=[+-]comp_start]]\n"
356 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
357 " [[!] --mirror-index=[+-]<index> |\n"
358 " [!] --mirror-id=[+-]<id>]\n"
359 " <directory|filename> ..."},
360 {"setdirstripe", lfs_setdirstripe, 0,
361 "To create a striped directory on a specified MDT. This can only\n"
362 "be done on MDT0 with the right of administrator.\n"
363 "usage: setdirstripe [OPTION] <directory>\n"
365 {"getdirstripe", lfs_getdirstripe, 0,
366 "To list the layout pattern info for a given directory\n"
367 "or recursively for all directories in a directory tree.\n"
368 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
369 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
370 " [--recursive|-r] [--yaml|-y]\n"
371 " [--default|-D] <dir> ..."},
372 {"mkdir", lfs_setdirstripe, 0,
373 "To create a striped directory on a specified MDT. This can only\n"
374 "be done on MDT0 with the right of administrator.\n"
375 "usage: mkdir [OPTION] <directory>\n"
377 {"rm_entry", lfs_rmentry, 0,
378 "To remove the name entry of the remote directory. Note: This\n"
379 "command will only delete the name entry, i.e. the remote directory\n"
380 "will become inaccessable after this command. This can only be done\n"
381 "by the administrator\n"
382 "usage: rm_entry <dir>\n"},
383 {"pool_list", lfs_poollist, 0,
384 "List pools or pool OSTs\n"
385 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
386 {"find", lfs_find, 0,
387 "find files matching given attributes recursively in directory tree.\n"
388 "usage: find <directory|filename> ...\n"
389 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
390 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
391 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
392 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
393 " [[!] --stripe-count|-c [+-]<stripes>]\n"
394 " [[!] --stripe-index|-i <index,...>]\n"
395 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
396 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
397 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
398 " [[!] --projid <projid>]\n"
399 " [[!] --layout|-L released,raid0,mdt]\n"
400 " [[!] --component-count [+-]<comp_cnt>]\n"
401 " [[!] --component-start [+-]N[kMGTPE]]\n"
402 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
403 " [[!] --component-flags <comp_flags>]\n"
404 " [[!] --mirror-count|-N [+-]<n>]\n"
405 " [[!] --mirror-state <[^]state>]\n"
406 " [[!] --mdt-count|-T [+-]<stripes>]\n"
407 " [[!] --mdt-hash|-H <hashtype>\n"
408 " [[!] --mdt-index|-m <uuid|index,...>]\n"
409 "\t !: used before an option indicates 'NOT' requested attribute\n"
410 "\t -: used before a value indicates less than requested value\n"
411 "\t +: used before a value indicates more than requested value\n"
412 "\thashtype: hash type of the striped directory.\n"
413 "\t fnv_1a_64 FNV-1a hash algorithm\n"
414 "\t all_char sum of characters % MDT_COUNT\n"},
415 {"check", lfs_check, 0,
416 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
417 "or all the servers (MGTs, MDTs and OSTs).\n"
418 "usage: check <mgts|osts|mdts|all>"},
419 {"osts", lfs_osts, 0, "list OSTs connected to client "
420 "[for specified path only]\n" "usage: osts [path]"},
421 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
422 "[for specified path only]\n" "usage: mdts [path]"},
424 "report filesystem disk space usage or inodes usage"
425 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
426 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
427 {"getname", lfs_getname, 0, "list instances and specified mount points "
428 "[for specified path only]\n"
429 "Usage: getname [-h]|[path ...] "},
430 #ifdef HAVE_SYS_QUOTA_H
431 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
432 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
433 " -b <block-softlimit> -B <block-hardlimit>\n"
434 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
435 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
436 " [--block-softlimit <block-softlimit>]\n"
437 " [--block-hardlimit <block-hardlimit>]\n"
438 " [--inode-softlimit <inode-softlimit>]\n"
439 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
440 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
441 " [--block-grace <block-grace>]\n"
442 " [--inode-grace <inode-grace>] <filesystem>\n"
443 " setquota <-U|-G|-P>\n"
444 " -b <block-softlimit> -B <block-hardlimit>\n"
445 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
446 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
447 " [--block-softlimit <block-softlimit>]\n"
448 " [--block-hardlimit <block-hardlimit>]\n"
449 " [--inode-softlimit <inode-softlimit>]\n"
450 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
451 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
453 " -b can be used instead of --block-softlimit/--block-grace\n"
454 " -B can be used instead of --block-hardlimit\n"
455 " -i can be used instead of --inode-softlimit/--inode-grace\n"
456 " -I can be used instead of --inode-hardlimit\n"
457 " -d can be used instead of --default\n\n"
458 "Note: The total quota space will be split into many qunits and\n"
459 " balanced over all server targets, the minimal qunit size is\n"
460 " 1M bytes for block space and 1K inodes for inode space.\n\n"
461 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
462 " Quota space rebalancing process will stop when this mininum\n"
463 " value is reached. As a result, quota exceeded can be returned\n"
464 " while many targets still have 1MB or 1K inodes of spare\n"
466 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
467 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
469 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
470 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
471 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
472 {"project", lfs_project, 0,
473 "Change or list project attribute for specified file or directory.\n"
474 "usage: project [-d|-r] <file|directory...>\n"
475 " list project ID and flags on file(s) or directories\n"
476 " project [-p id] [-s] [-r] <file|directory...>\n"
477 " set project ID and/or inherit flag for specified file(s) or directories\n"
478 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
479 " check project ID and flags on file(s) or directories, print outliers\n"
480 " project -C [-r] [-k] <file|directory...>\n"
481 " clear the project inherit flag and ID on the file or directory\n"
484 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
485 "usage: flushctx [-k] [mountpoint...]"},
486 {"changelog", lfs_changelog, 0,
487 "Show the metadata changes on an MDT."
488 "\nusage: changelog <mdtname> [startrec [endrec]]"},
489 {"changelog_clear", lfs_changelog_clear, 0,
490 "Indicate that old changelog records up to <endrec> are no longer of "
491 "interest to consumer <id>, allowing the system to free up space.\n"
492 "An <endrec> of 0 means all records.\n"
493 "usage: changelog_clear <mdtname> <id> <endrec>"},
494 {"fid2path", lfs_fid2path, 0,
495 "Resolve the full path(s) for given FID(s). For a specific hardlink "
496 "specify link number <linkno>.\n"
497 /* "For a historical link name, specify changelog record <recno>.\n" */
498 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
499 /* [ --rec <recno> ] */ },
500 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
501 "usage: path2fid [--parents] <path> ..."},
502 {"data_version", lfs_data_version, 0, "Display file data version for "
503 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
504 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
505 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
506 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
507 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
508 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
509 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
511 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
512 "[--archived] [--lost] <file> ..."},
513 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
514 "given files.\n" "usage: hsm_action <file> ..."},
515 {"hsm_archive", lfs_hsm_archive, 0,
516 "Archive file to external storage.\n"
517 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
519 {"hsm_restore", lfs_hsm_restore, 0,
520 "Restore file from external storage.\n"
521 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
522 {"hsm_release", lfs_hsm_release, 0,
523 "Release files from Lustre.\n"
524 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
525 {"hsm_remove", lfs_hsm_remove, 0,
526 "Remove file copy from external storage.\n"
527 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
529 " (FILE [FILE ...] | "
530 "--mntpath MOUNTPATH FID [FID ...])\n"
532 "Note: To remove an archived copy of a file already deleted from a "
534 "--mntpath option and a list of FIDs must be specified"
536 {"hsm_cancel", lfs_hsm_cancel, 0,
537 "Cancel requests related to specified files.\n"
538 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
539 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
540 "usage: swap_layouts <path1> <path2>"},
541 {"migrate", lfs_setstripe_migrate, 0,
542 "migrate a directory between MDTs.\n"
543 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
544 " [--mdt-hash|-H] <hash_type>\n"
545 " [--mdt-index|-m] <start_mdt_index>\n"
548 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
549 " it's the MDT index of first stripe\n"
550 "\tmdt_count: number of MDTs to stripe a directory over\n"
551 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
552 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
553 " all_char sum of characters % MDT_COUNT\n"
555 "migrate file objects from one OST "
556 "layout\nto another (may be not safe with concurent writes).\n"
558 "[--stripe-count|-c] <stripe_count>\n"
559 " [--stripe-index|-i] <start_ost_index>\n"
560 " [--stripe-size|-S] <stripe_size>\n"
561 " [--pool|-p] <pool_name>\n"
562 " [--ost|-o] <ost_indices>\n"
564 " [--non-block|-n]\n"
565 " [--non-direct|-D]\n"
566 " <file|directory>\n"
567 "\tstripe_count: number of OSTs to stripe a file over\n"
568 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
569 "\tstripe_size: number of bytes to store before moving to the next OST\n"
570 "\tpool_name: name of the predefined pool of OSTs\n"
571 "\tost_indices: OSTs to stripe over, in order\n"
572 "\tblock: Block file access during data migration (default)\n"
573 "\tnon-block: Abort migrations if concurrent access is detected\n"
574 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
576 "To move directories between MDTs. This command is deprecated, "
577 "use \"migrate\" instead.\n"
578 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
580 {"ladvise", lfs_ladvise, 0,
581 "Provide servers with advice about access patterns for a file.\n"
582 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
583 " [--background|-b] [--unset|-u]\n\n"
584 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
585 " {[--mode|-m [READ,WRITE]}\n"
587 {"mirror", lfs_mirror, mirror_cmdlist,
588 "lfs commands used to manage files with mirrored components:\n"
589 "lfs mirror create - create a mirrored file or directory\n"
590 "lfs mirror extend - add mirror(s) to an existing file\n"
591 "lfs mirror split - split a mirror from an existing mirrored file\n"
592 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
593 "lfs mirror read - read a mirror content of a mirrored file\n"
594 "lfs mirror write - write to a mirror of a mirrored file\n"
595 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
596 "lfs mirror verify - verify mirrored file(s)\n"},
597 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
598 "usage: getsom [-s] [-b] [-f] <path>\n"
599 "\t-s: Only show the size value of the SOM data for a given file\n"
600 "\t-b: Only show the blocks value of the SOM data for a given file\n"
601 "\t-f: Only show the flags value of the SOM data for a given file\n"},
602 {"heat_get", lfs_heat_get, 0,
603 "To get heat of files.\n"
604 "usage: heat_get <file> ...\n"},
605 {"heat_set", lfs_heat_set, 0,
606 "To set heat flags of files.\n"
607 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
608 "\t--clear|-c: Clear file heat for given files\n"
609 "\t--off|-o: Turn off file heat for given files\n"
610 "\t--on|-O: Turn on file heat for given files\n"},
611 {"help", Parser_help, 0, "help"},
612 {"exit", Parser_quit, 0, "quit"},
613 {"quit", Parser_quit, 0, "quit"},
614 {"--version", Parser_version, 0,
615 "output build version of the utility and exit"},
616 {"--list-commands", lfs_list_commands, 0,
617 "list commands supported by the utility and exit"},
622 static int check_hashtype(const char *hashtype)
626 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
627 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
634 static const char *error_loc = "syserror";
637 MIGRATION_NONBLOCK = 0x0001,
638 MIGRATION_MIRROR = 0x0002,
639 MIGRATION_NONDIRECT = 0x0004,
640 MIGRATION_VERBOSE = 0x0008,
643 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
644 struct llapi_layout *layout);
647 migrate_open_files(const char *name, __u64 migration_flags,
648 const struct llapi_stripe_param *param,
649 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
656 char parent[PATH_MAX];
657 char volatile_file[PATH_MAX];
663 if (param == NULL && layout == NULL) {
664 error_loc = "layout information";
668 /* search for file directory pathname */
669 if (strlen(name) > sizeof(parent) - 1) {
670 error_loc = "source file name";
674 strncpy(parent, name, sizeof(parent));
675 ptr = strrchr(parent, '/');
677 if (getcwd(parent, sizeof(parent)) == NULL) {
678 error_loc = "getcwd";
682 if (ptr == parent) /* leading '/' */
687 /* open file, direct io */
688 /* even if the file is only read, WR mode is nedeed to allow
689 * layout swap on fd */
691 if (!(migration_flags & MIGRATION_NONDIRECT))
693 fd = open(name, rflags);
696 error_loc = "cannot open source file";
700 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
702 error_loc = "cannot get MDT index";
707 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
708 mode_t open_mode = S_IRUSR | S_IWUSR;
710 random_value = random();
711 rc = snprintf(volatile_file, sizeof(volatile_file),
712 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
713 mdt_index, random_value);
714 if (rc >= sizeof(volatile_file)) {
719 /* create, open a volatile file, use caching (ie no directio) */
721 fdv = llapi_file_open_param(volatile_file, open_flags,
724 fdv = lfs_component_create(volatile_file, open_flags,
726 } while (fdv < 0 && (rc = fdv) == -EEXIST);
729 error_loc = "cannot create volatile file";
733 /* In case the MDT does not support creation of volatile files
734 * we should try to unlink it. */
735 (void)unlink(volatile_file);
737 /* Not-owner (root?) special case.
738 * Need to set owner/group of volatile file like original.
739 * This will allow to pass related check during layout_swap.
744 error_loc = "cannot stat source file";
748 rc = fstat(fdv, &stv);
751 error_loc = "cannot stat volatile";
755 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
756 rc = fchown(fdv, st.st_uid, st.st_gid);
759 error_loc = "cannot change ownwership of volatile";
778 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
780 struct llapi_layout *layout;
781 size_t buf_size = 4 * 1024 * 1024;
790 layout = llapi_layout_get_by_fd(fd_src, 0);
791 if (layout != NULL) {
792 uint64_t stripe_size;
794 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
796 buf_size = stripe_size;
798 llapi_layout_free(layout);
801 /* Use a page-aligned buffer for direct I/O */
802 rc = posix_memalign(&buf, getpagesize(), buf_size);
807 /* read new data only if we have written all
808 * previously read data */
811 rc = check_file(fd_src);
816 rsize = read(fd_src, buf, buf_size);
828 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
847 static int migrate_copy_timestamps(int fd, int fdv)
851 if (fstat(fd, &st) == 0) {
852 struct timeval tv[2] = {
853 {.tv_sec = st.st_atime},
854 {.tv_sec = st.st_mtime}
857 return futimes(fdv, tv);
863 static int migrate_block(int fd, int fdv)
870 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
872 error_loc = "cannot get dataversion";
880 /* The grouplock blocks all concurrent accesses to the file.
881 * It has to be taken after llapi_get_data_version as it would
883 rc = llapi_group_lock(fd, gid);
885 error_loc = "cannot get group lock";
889 rc = migrate_copy_data(fd, fdv, NULL);
891 error_loc = "data copy failed";
895 /* Make sure we keep original atime/mtime values */
896 rc = migrate_copy_timestamps(fd, fdv);
898 error_loc = "timestamp copy failed";
903 * for a migration we need to check data version on file did
906 * Pass in gid=0 since we already own grouplock. */
907 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
908 SWAP_LAYOUTS_CHECK_DV1);
910 error_loc = "file changed";
913 error_loc = "cannot swap layout";
918 rc2 = llapi_group_unlock(fd, gid);
919 if (rc2 < 0 && rc == 0) {
920 error_loc = "unlock group lock";
928 * Internal helper for migrate_copy_data(). Check lease and report error if
931 * \param[in] fd File descriptor on which to check the lease.
933 * \retval 0 Migration can keep on going.
934 * \retval -errno Error occurred, abort migration.
936 static int check_lease(int fd)
940 rc = llapi_lease_check(fd);
942 return 0; /* llapi_check_lease returns > 0 on success. */
947 static int migrate_nonblock(int fd, int fdv)
953 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
955 error_loc = "cannot get data version";
959 rc = migrate_copy_data(fd, fdv, check_lease);
961 error_loc = "data copy failed";
965 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
967 error_loc = "cannot get data version";
973 error_loc = "source file changed";
977 /* Make sure we keep original atime/mtime values */
978 rc = migrate_copy_timestamps(fd, fdv);
980 error_loc = "timestamp copy failed";
987 static int lfs_component_set(char *fname, int comp_id,
988 __u32 flags, __u32 neg_flags)
991 __u32 flags_array[2];
996 ids[count] = comp_id;
997 flags_array[count] = flags;
1002 ids[count] = comp_id;
1003 flags_array[count] = neg_flags | LCME_FL_NEG;
1007 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1010 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1011 progname, comp_id, fname, flags, neg_flags);
1016 static int lfs_component_del(char *fname, __u32 comp_id,
1017 __u32 flags, __u32 neg_flags)
1021 if (flags && neg_flags)
1024 if (!flags && neg_flags)
1025 flags = neg_flags | LCME_FL_NEG;
1027 if ((flags && comp_id) || (!flags && !comp_id))
1030 /* LCME_FL_INIT is the only supported flag in PFL */
1032 if (flags & ~LCME_KNOWN_FLAGS) {
1034 "%s setstripe: unknown flags %#x\n",
1038 } else if (comp_id > LCME_ID_MAX) {
1039 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1044 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1047 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1048 progname, comp_id, fname, strerror(errno));
1052 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1059 rc = llapi_layout_file_comp_add(fname, layout);
1061 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1062 fname, strerror(errno));
1066 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1067 struct llapi_layout *layout)
1075 fd = lstat(fname, &st);
1076 if (fd == 0 && S_ISDIR(st.st_mode))
1077 open_flags = O_DIRECTORY | O_RDONLY;
1079 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1081 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1082 S_ISDIR(st.st_mode) ?
1083 "set default composite layout for" :
1084 "create composite file",
1085 fname, strerror(errno));
1089 static int lfs_migrate(char *name, __u64 migration_flags,
1090 struct llapi_stripe_param *param,
1091 struct llapi_layout *layout)
1097 rc = migrate_open_files(name, migration_flags, param, layout,
1102 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1103 /* Blocking mode (forced if servers do not support file lease).
1104 * It is also the default mode, since we cannot distinguish
1105 * between a broken lease and a server that does not support
1106 * atomic swap/close (LU-6785) */
1107 rc = migrate_block(fd, fdv);
1111 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1113 error_loc = "cannot get lease";
1117 rc = migrate_nonblock(fd, fdv);
1119 llapi_lease_release(fd);
1123 /* Atomically put lease, swap layouts and close.
1124 * for a migration we need to check data version on file did
1126 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1128 error_loc = "cannot swap layout";
1140 fprintf(stderr, "error: %s: %s: %s: %s\n",
1141 progname, name, error_loc, strerror(-rc));
1142 else if (migration_flags & MIGRATION_VERBOSE)
1143 printf("%s\n", name);
1148 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1157 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1161 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1162 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1163 const char *comp_name = comp_flags_table[i].cfn_name;
1165 if (strcmp(name, comp_name) == 0) {
1166 *flags |= comp_flag;
1168 } else if (strncmp(name, "^", 1) == 0 &&
1169 strcmp(name + 1, comp_name) == 0) {
1170 *neg_flags |= comp_flag;
1175 llapi_printf(LLAPI_MSG_ERROR,
1176 "%s: component flag '%s' not supported\n",
1182 if (!*flags && !*neg_flags)
1185 /* don't allow to set and exclude the same flag */
1186 if (*flags & *neg_flags)
1192 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1200 if (strncmp(string, "^", 1) == 0) {
1201 *neg_state = llapi_layout_string_flags(string + 1);
1202 if (*neg_state != 0)
1205 *state = llapi_layout_string_flags(string);
1210 llapi_printf(LLAPI_MSG_ERROR,
1211 "%s: mirrored file state '%s' not supported\n",
1217 * struct mirror_args - Command-line arguments for mirror(s).
1218 * @m_count: Number of mirrors to be created with this layout.
1219 * @m_flags: Mirror level flags, only 'prefer' is supported.
1220 * @m_layout: Mirror layout.
1221 * @m_file: A victim file. Its layout will be split and used as a mirror.
1222 * @m_next: Point to the next node of the list.
1224 * Command-line arguments for mirror(s) will be parsed and stored in
1225 * a linked list that consists of this structure.
1227 struct mirror_args {
1230 struct llapi_layout *m_layout;
1232 struct mirror_args *m_next;
1235 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1240 rc = llapi_layout_comp_flags_get(layout, &flags);
1244 if (flags & LCME_FL_NEG) {
1245 fprintf(stderr, "error: %s: negative flags are not supported\n",
1250 if (flags & LCME_FL_STALE) {
1251 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1252 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1256 return LLAPI_LAYOUT_ITER_CONT;
1259 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1261 uint64_t start, end;
1265 /* LU-10112: do not support dom+flr in phase 1 */
1266 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1270 rc = llapi_layout_pattern_get(layout, &pattern);
1274 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1275 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1280 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1284 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1288 if (end != LUSTRE_EOF) {
1289 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1294 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1299 * enum mirror_flags - Flags for extending a mirrored file.
1300 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1301 * in case the victim file(s) contains the same data as the
1302 * original mirrored file.
1303 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1304 * @MF_COMP_ID: specified component id instead of mirror id
1306 * Flags for extending a mirrored file.
1315 * mirror_create_sanity_check() - Check mirror list.
1316 * @list: A linked list that stores the mirror arguments.
1318 * This function does a sanity check on @list for creating
1321 * Return: 0 on success or a negative error code on failure.
1323 static int mirror_create_sanity_check(const char *fname,
1324 struct mirror_args *list)
1327 bool has_m_file = false;
1328 bool has_m_layout = false;
1334 struct llapi_layout *layout;
1336 layout = llapi_layout_get_by_path(fname, 0);
1339 "error: %s: file '%s' couldn't get layout\n",
1344 rc = mirror_sanity_check_one(layout);
1345 llapi_layout_free(layout);
1351 while (list != NULL) {
1352 if (list->m_file != NULL) {
1354 llapi_layout_free(list->m_layout);
1357 llapi_layout_get_by_path(list->m_file, 0);
1358 if (list->m_layout == NULL) {
1360 "error: %s: file '%s' has no layout\n",
1361 progname, list->m_file);
1365 has_m_layout = true;
1366 if (list->m_layout == NULL) {
1367 fprintf(stderr, "error: %s: no mirror layout\n",
1373 rc = mirror_sanity_check_one(list->m_layout);
1377 list = list->m_next;
1380 if (has_m_file && has_m_layout) {
1382 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1390 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1392 __u32 mirror_flags = *(__u32 *)cbdata;
1396 rc = llapi_layout_comp_flags_get(layout, &flags);
1401 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1406 return LLAPI_LAYOUT_ITER_CONT;
1410 * mirror_create() - Create a mirrored file.
1411 * @fname: The file to be created.
1412 * @mirror_list: A linked list that stores the mirror arguments.
1414 * This function creates a mirrored file @fname with the mirror(s)
1415 * from @mirror_list.
1417 * Return: 0 on success or a negative error code on failure.
1419 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1421 struct llapi_layout *layout = NULL;
1422 struct mirror_args *cur_mirror = NULL;
1423 uint16_t mirror_count = 0;
1427 rc = mirror_create_sanity_check(NULL, mirror_list);
1431 cur_mirror = mirror_list;
1432 while (cur_mirror != NULL) {
1433 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1435 &cur_mirror->m_flags);
1438 fprintf(stderr, "%s: failed to set mirror flags\n",
1443 for (i = 0; i < cur_mirror->m_count; i++) {
1444 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1447 fprintf(stderr, "error: %s: "
1448 "merge layout failed: %s\n",
1449 progname, strerror(errno));
1453 mirror_count += cur_mirror->m_count;
1454 cur_mirror = cur_mirror->m_next;
1457 if (layout == NULL) {
1458 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1462 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1465 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1466 progname, strerror(errno));
1470 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1478 llapi_layout_free(layout);
1483 * Compare files and check lease on @fd.
1485 * \retval bytes number of bytes are the same
1487 static ssize_t mirror_file_compare(int fd, int fdv)
1489 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1491 ssize_t bytes_done = 0;
1492 ssize_t bytes_read = 0;
1494 buf = malloc(buflen * 2);
1499 if (!llapi_lease_check(fd)) {
1500 bytes_done = -EBUSY;
1504 bytes_read = read(fd, buf, buflen);
1505 if (bytes_read <= 0)
1508 if (bytes_read != read(fdv, buf + buflen, buflen))
1511 /* XXX: should compute the checksum on each buffer and then
1512 * compare checksum to avoid cache collision */
1513 if (memcmp(buf, buf + buflen, bytes_read))
1516 bytes_done += bytes_read;
1524 static int mirror_extend_file(const char *fname, const char *victim_file,
1525 enum mirror_flags mirror_flags)
1530 struct stat stbuf_v;
1531 struct ll_ioc_lease *data = NULL;
1534 fd = open(fname, O_RDWR);
1536 error_loc = "open source file";
1541 fdv = open(victim_file, O_RDWR);
1543 error_loc = "open target file";
1548 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1549 error_loc = "stat source or target file";
1554 if (stbuf.st_dev != stbuf_v.st_dev) {
1555 error_loc = "stat source and target file";
1560 /* mirrors should be of the same size */
1561 if (stbuf.st_size != stbuf_v.st_size) {
1562 error_loc = "file sizes don't match";
1567 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1569 error_loc = "cannot get lease";
1573 if (!(mirror_flags & MF_NO_VERIFY)) {
1575 /* mirrors should have the same contents */
1576 ret = mirror_file_compare(fd, fdv);
1577 if (ret != stbuf.st_size) {
1578 error_loc = "file busy or contents don't match";
1579 rc = ret < 0 ? ret : -EINVAL;
1584 /* Get rid of caching pages from clients */
1585 rc = llapi_file_flush(fd);
1587 error_loc = "cannot get data version";
1591 rc = llapi_file_flush(fdv);
1593 error_loc = "cannot get data version";
1598 /* Make sure we keep original atime/mtime values */
1599 rc = migrate_copy_timestamps(fd, fdv);
1601 error_loc = "cannot copy timestamp";
1605 /* Atomically put lease, merge layouts and close. */
1606 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1608 error_loc = "memory allocation";
1611 data->lil_mode = LL_LEASE_UNLCK;
1612 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1613 data->lil_count = 1;
1614 data->lil_ids[0] = fdv;
1615 rc = llapi_lease_set(fd, data);
1617 error_loc = "cannot merge layout";
1619 } else if (rc == 0) {
1621 error_loc = "lost lease lock";
1634 (void) unlink(victim_file);
1636 fprintf(stderr, "error: %s: %s: %s: %s\n",
1637 progname, fname, error_loc, strerror(-rc));
1641 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1643 struct ll_ioc_lease *data = NULL;
1648 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1652 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1654 error_loc = "cannot get lease";
1658 rc = migrate_nonblock(fd, fdv);
1660 llapi_lease_release(fd);
1664 /* Atomically put lease, merge layouts and close. */
1665 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1667 error_loc = "memory allocation";
1670 data->lil_mode = LL_LEASE_UNLCK;
1671 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1672 data->lil_count = 1;
1673 data->lil_ids[0] = fdv;
1674 rc = llapi_lease_set(fd, data);
1676 error_loc = "cannot merge layout";
1678 } else if (rc == 0) {
1680 error_loc = "lost lease lock";
1693 fprintf(stderr, "error: %s: %s: %s: %s\n",
1694 progname, name, error_loc, strerror(-rc));
1698 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1699 enum mirror_flags mirror_flags)
1703 rc = mirror_create_sanity_check(fname, mirror_list);
1707 while (mirror_list) {
1708 if (mirror_list->m_file != NULL) {
1709 rc = mirror_extend_file(fname, mirror_list->m_file,
1712 __u32 mirror_count = mirror_list->m_count;
1714 while (mirror_count > 0) {
1715 rc = mirror_extend_layout(fname,
1716 mirror_list->m_layout);
1726 mirror_list = mirror_list->m_next;
1732 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1737 rc = llapi_layout_mirror_id_get(layout, &id);
1741 if ((__u16)id == *(__u16 *)cbdata)
1742 return LLAPI_LAYOUT_ITER_STOP;
1744 return LLAPI_LAYOUT_ITER_CONT;
1747 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1752 rc = llapi_layout_comp_id_get(layout, &id);
1756 if (id == *(__u32 *)cbdata)
1757 return LLAPI_LAYOUT_ITER_STOP;
1759 return LLAPI_LAYOUT_ITER_CONT;
1761 static int mirror_split(const char *fname, __u32 id,
1762 enum mirror_flags mflags, const char *victim_file)
1764 struct llapi_layout *layout;
1765 char parent[PATH_MAX];
1766 char victim[PATH_MAX];
1767 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1769 struct ll_ioc_lease *data;
1770 uint16_t mirror_count;
1775 /* check fname contains mirror with mirror_id/comp_id */
1776 layout = llapi_layout_get_by_path(fname, 0);
1779 "error %s: file '%s' couldn't get layout\n",
1784 rc = mirror_sanity_check_one(layout);
1788 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1791 "error %s: file '%s' couldn't get mirror count\n",
1795 if (mirror_count < 2) {
1797 "error %s: file '%s' has %d component, cannot split\n",
1798 progname, fname, mirror_count);
1802 if (mflags & MF_COMP_ID) {
1803 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1804 id = mirror_id_of(id);
1806 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1809 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1812 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1814 "error %s: file '%s' does not contain mirror with id %u\n",
1815 progname, fname, id);
1819 fd = open(fname, O_RDWR);
1822 "error %s: open file '%s' failed: %s\n",
1823 progname, fname, strerror(errno));
1827 /* get victim file directory pathname */
1828 if (strlen(fname) > sizeof(parent) - 1) {
1829 fprintf(stderr, "error %s: file name of '%s' too long\n",
1834 strncpy(parent, fname, sizeof(parent));
1835 ptr = strrchr(parent, '/');
1837 if (getcwd(parent, sizeof(parent)) == NULL) {
1838 fprintf(stderr, "error %s: getcwd failed: %s\n",
1839 progname, strerror(errno));
1849 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1851 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1856 if (victim_file == NULL) {
1857 /* use a temp file to store the splitted layout */
1858 if (mflags & MF_DESTROY) {
1859 fdv = llapi_create_volatile_idx(parent, mdt_index,
1860 O_LOV_DELAY_CREATE);
1862 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1864 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1867 /* user specified victim file */
1868 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1873 "error %s: create victim file failed: %s\n",
1874 progname, strerror(errno));
1878 /* get lease lock of fname */
1879 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1882 "error %s: cannot get lease of file '%s': %d\n",
1883 progname, fname, rc);
1887 /* Atomatically put lease, split layouts and close. */
1888 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1894 data->lil_mode = LL_LEASE_UNLCK;
1895 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1896 data->lil_count = 2;
1897 data->lil_ids[0] = fdv;
1898 data->lil_ids[1] = id;
1899 rc = llapi_lease_set(fd, data);
1901 if (rc == 0) /* lost lease lock */
1904 "error %s: cannot split '%s': %s\n",
1905 progname, fname, strerror(-rc));
1916 llapi_layout_free(layout);
1921 * Parse a string containing an target index list into an array of integers.
1923 * The input string contains a comma delimited list of individual
1924 * indices and ranges, for example "1,2-4,7". Add the indices into the
1925 * \a tgts array and remove duplicates.
1927 * \param[out] tgts array to store indices in
1928 * \param[in] size size of \a tgts array
1929 * \param[in] offset starting index in \a tgts
1930 * \param[in] arg string containing OST index list
1932 * \retval positive number of indices in \a tgts
1933 * \retval -EINVAL unable to parse \a arg
1935 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1939 int slots = size - offset;
1946 end_of_loop = false;
1947 while (!end_of_loop) {
1951 char *endptr = NULL;
1955 ptr = strchrnul(arg, ',');
1957 end_of_loop = *ptr == '\0';
1960 start_index = strtol(arg, &endptr, 0);
1961 if (endptr == arg) /* no data at all */
1963 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1966 end_index = start_index;
1967 if (*endptr == '-') {
1968 end_index = strtol(endptr + 1, &endptr, 0);
1969 if (*endptr != '\0')
1971 if (end_index < start_index)
1975 for (i = start_index; i <= end_index && slots > 0; i++) {
1978 /* remove duplicate */
1979 for (j = 0; j < offset; j++) {
1983 if (j == offset) { /* no duplicate */
1988 if (slots == 0 && i < end_index)
1996 if (!end_of_loop && ptr != NULL)
1999 return rc < 0 ? rc : nr;
2002 struct lfs_setstripe_args {
2003 unsigned long long lsa_comp_end;
2004 unsigned long long lsa_stripe_size;
2005 long long lsa_stripe_count;
2006 long long lsa_stripe_off;
2007 __u32 lsa_comp_flags;
2008 __u32 lsa_comp_neg_flags;
2009 unsigned long long lsa_pattern;
2010 unsigned int lsa_mirror_count;
2012 bool lsa_first_comp;
2014 char *lsa_pool_name;
2017 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2019 unsigned int mirror_count = lsa->lsa_mirror_count;
2020 bool first_comp = lsa->lsa_first_comp;
2022 memset(lsa, 0, sizeof(*lsa));
2024 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2025 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2026 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2027 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2028 lsa->lsa_pool_name = NULL;
2030 lsa->lsa_mirror_count = mirror_count;
2031 lsa->lsa_first_comp = first_comp;
2035 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2036 * @lsa: Stripe options to be initialized and inherited.
2038 * This function initializes stripe options in @lsa and inherit
2039 * stripe_size, stripe_count and OST pool_name options.
2043 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2045 unsigned long long stripe_size;
2046 long long stripe_count;
2047 char *pool_name = NULL;
2049 stripe_size = lsa->lsa_stripe_size;
2050 stripe_count = lsa->lsa_stripe_count;
2051 pool_name = lsa->lsa_pool_name;
2053 setstripe_args_init(lsa);
2055 lsa->lsa_stripe_size = stripe_size;
2056 lsa->lsa_stripe_count = stripe_count;
2057 lsa->lsa_pool_name = pool_name;
2060 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2062 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2063 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2064 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2065 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2066 lsa->lsa_pool_name != NULL ||
2067 lsa->lsa_comp_end != 0);
2071 * comp_args_to_layout() - Create or extend a composite layout.
2072 * @composite: Pointer to the composite layout.
2073 * @lsa: Stripe options for the new component.
2075 * This function creates or extends a composite layout by adding a new
2076 * component with stripe options from @lsa.
2078 * Return: 0 on success or an error code on failure.
2080 static int comp_args_to_layout(struct llapi_layout **composite,
2081 struct lfs_setstripe_args *lsa,
2084 struct llapi_layout *layout = *composite;
2085 uint64_t prev_end = 0;
2088 if (layout == NULL) {
2089 layout = llapi_layout_alloc();
2090 if (layout == NULL) {
2091 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2095 *composite = layout;
2099 /* Get current component extent, current component
2100 * must be the tail component. */
2101 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2103 fprintf(stderr, "Get comp extent failed. %s\n",
2108 if (lsa->lsa_first_comp)
2111 if (lsa->lsa_first_comp)
2112 rc = llapi_layout_add_first_comp(layout);
2114 rc = llapi_layout_comp_add(layout);
2116 fprintf(stderr, "Add component failed. %s\n",
2121 /* reset lsa_first_comp */
2122 lsa->lsa_first_comp = false;
2125 rc = llapi_layout_comp_extent_set(layout, prev_end,
2128 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2129 prev_end, lsa->lsa_comp_end, strerror(errno));
2134 /* Data-on-MDT component setting */
2135 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2136 /* In case of Data-on-MDT patterns the only extra option
2137 * applicable is stripe size option. */
2138 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2139 fprintf(stderr, "Option 'stripe-count' can't be "
2140 "specified with Data-on-MDT component: %lld\n",
2141 lsa->lsa_stripe_count);
2144 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2145 fprintf(stderr, "Option 'stripe-size' can't be "
2146 "specified with Data-on-MDT component: %llu\n",
2147 lsa->lsa_stripe_size);
2150 if (lsa->lsa_nr_tgts != 0) {
2151 fprintf(stderr, "Option 'ost-list' can't be specified "
2152 "with Data-on-MDT component: '%i'\n",
2156 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2157 fprintf(stderr, "Option 'stripe-offset' can't be "
2158 "specified with Data-on-MDT component: %lld\n",
2159 lsa->lsa_stripe_off);
2162 if (lsa->lsa_pool_name != 0) {
2163 fprintf(stderr, "Option 'pool' can't be specified "
2164 "with Data-on-MDT component: '%s'\n",
2165 lsa->lsa_pool_name);
2169 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2171 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2172 lsa->lsa_pattern, strerror(errno));
2175 /* Data-on-MDT component has always single stripe up to end */
2176 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2179 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2181 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2182 lsa->lsa_stripe_size, strerror(errno));
2186 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2188 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2189 lsa->lsa_stripe_count, strerror(errno));
2193 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2195 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2196 lsa->lsa_comp_flags, strerror(errno));
2200 if (lsa->lsa_pool_name != NULL) {
2201 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2203 fprintf(stderr, "Set pool name: %s failed. %s\n",
2204 lsa->lsa_pool_name, strerror(errno));
2208 rc = llapi_layout_pool_name_set(layout, "");
2210 fprintf(stderr, "Clear pool name failed: %s\n",
2216 if (lsa->lsa_nr_tgts > 0) {
2217 if (lsa->lsa_stripe_count > 0 &&
2218 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2219 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2220 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2221 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2222 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2225 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2226 rc = llapi_layout_ost_index_set(layout, i,
2231 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2232 lsa->lsa_stripe_off != -1) {
2233 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2236 fprintf(stderr, "Set ost index %d failed. %s\n",
2237 i, strerror(errno));
2244 static int build_component(struct llapi_layout **layout,
2245 struct lfs_setstripe_args *lsa, bool set_extent)
2249 rc = comp_args_to_layout(layout, lsa, set_extent);
2253 if (lsa->lsa_mirror_count > 0) {
2254 rc = llapi_layout_mirror_count_set(*layout,
2255 lsa->lsa_mirror_count);
2259 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2262 lsa->lsa_mirror_count = 0;
2268 static int build_layout_from_yaml_node(struct cYAML *node,
2269 struct llapi_layout **layout,
2270 struct lfs_setstripe_args *lsa,
2277 if (node->cy_type == CYAML_TYPE_OBJECT) {
2278 /* go deep to sub blocks */
2279 rc = build_layout_from_yaml_node(node->cy_child, layout,
2284 if (node->cy_string == NULL)
2287 string = node->cy_string;
2288 /* skip leading lmm_ if present, to simplify parsing */
2289 if (strncmp(string, "lmm_", 4) == 0)
2292 if (node->cy_type == CYAML_TYPE_STRING) {
2293 if (!strcmp(string, "lcme_extent.e_end")) {
2294 if (!strcmp(node->cy_valuestring, "EOF") ||
2295 !strcmp(node->cy_valuestring, "eof"))
2296 lsa->lsa_comp_end = LUSTRE_EOF;
2297 } else if (!strcmp(string, "pool")) {
2298 lsa->lsa_pool_name = node->cy_valuestring;
2299 } else if (!strcmp(string, "pattern")) {
2300 if (!strcmp(node->cy_valuestring, "mdt"))
2301 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2302 } else if (!strcmp(string, "lcme_flags")) {
2303 rc = comp_str2flags(node->cy_valuestring,
2304 &lsa->lsa_comp_flags,
2305 &lsa->lsa_comp_neg_flags);
2308 /* Only template flags have meaning in
2309 * the layout for a new file
2311 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2313 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2314 if (!strcmp(string, "lcm_mirror_count")) {
2315 lsa->lsa_mirror_count = node->cy_valueint;
2316 } else if (!strcmp(string, "lcme_extent.e_start")) {
2317 if (node->cy_valueint != 0 || *layout != NULL) {
2318 rc = build_component(layout, lsa, true);
2323 if (node->cy_valueint == 0)
2324 lsa->lsa_first_comp = true;
2326 /* initialize lsa */
2327 setstripe_args_init(lsa);
2328 lsa->lsa_tgts = osts;
2329 } else if (!strcmp(string, "lcme_extent.e_end")) {
2330 if (node->cy_valueint == -1)
2331 lsa->lsa_comp_end = LUSTRE_EOF;
2333 lsa->lsa_comp_end = node->cy_valueint;
2334 } else if (!strcmp(string, "stripe_count")) {
2335 lsa->lsa_stripe_count = node->cy_valueint;
2336 } else if (!strcmp(string, "stripe_size")) {
2337 lsa->lsa_stripe_size = node->cy_valueint;
2338 } else if (!strcmp(string, "stripe_offset")) {
2339 lsa->lsa_stripe_off = node->cy_valueint;
2340 } else if (!strcmp(string, "l_ost_idx")) {
2341 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2346 node = node->cy_next;
2352 static int lfs_comp_create_from_yaml(char *template,
2353 struct llapi_layout **layout,
2354 struct lfs_setstripe_args *lsa,
2357 struct cYAML *tree = NULL, *err_rc = NULL;
2360 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2362 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2363 progname, template);
2364 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2365 "can't parse", &err_rc);
2366 cYAML_print_tree2file(stderr, err_rc);
2367 cYAML_free_tree(err_rc);
2372 /* initialize lsa for plain file */
2373 setstripe_args_init(lsa);
2374 lsa->lsa_tgts = osts;
2376 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2378 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2379 progname, template);
2382 rc = build_component(layout, lsa, *layout != NULL);
2384 /* clean clean lsa */
2385 setstripe_args_init(lsa);
2389 cYAML_free_tree(tree);
2393 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2394 * end of the last component in the existing file, and adjust the
2395 * first extent start of the components to be added accordingly. */
2396 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2398 struct llapi_layout *head;
2399 uint64_t start, end, stripe_size, prev_end = 0;
2402 if (layout == NULL) {
2404 "%s setstripe: layout must be specified\n",
2410 head = llapi_layout_get_by_path(fname, 0);
2413 "%s setstripe: cannot read layout from '%s': %s\n",
2414 progname, fname, strerror(errno));
2416 } else if (errno == ENODATA) {
2417 /* file without LOVEA, this component-add will be turned
2418 * into a component-create. */
2419 llapi_layout_free(head);
2421 } else if (!llapi_layout_is_composite(head)) {
2422 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2424 llapi_layout_free(head);
2428 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2430 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2431 progname, strerror(errno));
2432 llapi_layout_free(head);
2436 llapi_layout_free(head);
2438 /* Make sure we use the first component of the layout to be added. */
2439 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2442 "%s setstripe: cannot move component cursor: %s\n",
2443 progname, strerror(errno));
2447 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2449 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2450 progname, strerror(errno));
2454 if (start > prev_end || end <= prev_end) {
2456 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2457 progname, start, end, prev_end);
2461 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2463 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2464 progname, strerror(errno));
2468 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2469 (prev_end & (stripe_size - 1))) {
2471 "%s setstripe: stripe size %lu not aligned with %lu\n",
2472 progname, stripe_size, prev_end);
2476 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2479 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2480 progname, prev_end, end, strerror(errno));
2487 static inline bool arg_is_eof(char *arg)
2489 return !strncmp(arg, "-1", strlen("-1")) ||
2490 !strncmp(arg, "EOF", strlen("EOF")) ||
2491 !strncmp(arg, "eof", strlen("eof"));
2495 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2497 * Return: Valid mirror_args pointer on success and
2498 * NULL if memory allocation fails.
2500 static struct mirror_args *lfs_mirror_alloc(void)
2502 struct mirror_args *mirror = NULL;
2505 mirror = calloc(1, sizeof(*mirror));
2516 * lfs_mirror_free() - Free memory allocated for a mirror argument
2518 * @mirror: Previously allocated mirror argument structure by
2519 * lfs_mirror_alloc().
2521 * Free memory allocated for @mirror.
2525 static void lfs_mirror_free(struct mirror_args *mirror)
2527 if (mirror->m_layout != NULL)
2528 llapi_layout_free(mirror->m_layout);
2533 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2534 * @mirror_list: Previously allocated mirror list.
2536 * Free memory allocated for @mirror_list.
2540 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2542 struct mirror_args *next_mirror = NULL;
2544 while (mirror_list != NULL) {
2545 next_mirror = mirror_list->m_next;
2546 lfs_mirror_free(mirror_list);
2547 mirror_list = next_mirror;
2559 LFS_COMP_NO_VERIFY_OPT,
2561 LFS_MIRROR_FLAGS_OPT,
2563 LFS_MIRROR_STATE_OPT,
2565 LFS_MIRROR_INDEX_OPT,
2569 static int lfs_setstripe_internal(int argc, char **argv,
2570 enum setstripe_origin opc)
2572 struct lfs_setstripe_args lsa = { 0 };
2573 struct llapi_stripe_param *param = NULL;
2574 struct find_param migrate_mdt_param = {
2584 unsigned long long size_units = 1;
2585 bool migrate_mode = false;
2586 bool migrate_mdt_mode = false;
2587 bool migration_block = false;
2588 __u64 migration_flags = 0;
2589 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2590 int comp_del = 0, comp_set = 0;
2593 struct llapi_layout *layout = NULL;
2594 struct llapi_layout **lpp = &layout;
2595 bool mirror_mode = false;
2596 bool has_m_file = false;
2597 __u32 mirror_count = 0;
2598 enum mirror_flags mirror_flags = 0;
2599 struct mirror_args *mirror_list = NULL;
2600 struct mirror_args *new_mirror = NULL;
2601 struct mirror_args *last_mirror = NULL;
2602 __u16 mirror_id = 0;
2604 bool from_yaml = false;
2605 bool from_copy = false;
2606 char *template = NULL;
2608 struct option long_opts[] = {
2609 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2610 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2611 /* --block is only valid in migrate mode */
2612 { .val = 'b', .name = "block", .has_arg = no_argument },
2613 { .val = LFS_COMP_ADD_OPT,
2614 .name = "comp-add", .has_arg = no_argument },
2615 { .val = LFS_COMP_ADD_OPT,
2616 .name = "component-add", .has_arg = no_argument },
2617 { .val = LFS_COMP_DEL_OPT,
2618 .name = "comp-del", .has_arg = no_argument },
2619 { .val = LFS_COMP_DEL_OPT,
2620 .name = "component-del", .has_arg = no_argument },
2621 { .val = LFS_COMP_FLAGS_OPT,
2622 .name = "comp-flags", .has_arg = required_argument },
2623 { .val = LFS_COMP_FLAGS_OPT,
2624 .name = "component-flags",
2625 .has_arg = required_argument },
2626 { .val = LFS_COMP_SET_OPT,
2627 .name = "comp-set", .has_arg = no_argument },
2628 { .val = LFS_COMP_SET_OPT,
2629 .name = "component-set",
2630 .has_arg = no_argument},
2631 { .val = LFS_COMP_NO_VERIFY_OPT,
2632 .name = "no-verify", .has_arg = no_argument},
2633 { .val = LFS_MIRROR_FLAGS_OPT,
2634 .name = "flags", .has_arg = required_argument},
2635 { .val = LFS_MIRROR_ID_OPT,
2636 .name = "mirror-id", .has_arg = required_argument},
2637 { .val = LFS_LAYOUT_COPY,
2638 .name = "copy", .has_arg = required_argument},
2639 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2640 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2641 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2642 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2643 { .val = 'd', .name = "delete", .has_arg = no_argument},
2644 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2645 /* --non-direct is only valid in migrate mode */
2646 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2647 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2648 { .val = 'E', .name = "component-end",
2649 .has_arg = required_argument},
2650 { .val = 'f', .name = "file", .has_arg = required_argument },
2651 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2652 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2653 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2654 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2655 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2656 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2657 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2658 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2659 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2660 { .val = 'L', .name = "layout", .has_arg = required_argument },
2661 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2662 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2663 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2664 /* --non-block is only valid in migrate mode */
2665 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2666 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2667 { .val = 'o', .name = "ost", .has_arg = required_argument },
2668 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2669 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2670 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2672 { .val = 'p', .name = "pool", .has_arg = required_argument },
2673 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2674 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2675 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2676 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2677 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2678 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2679 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2680 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2681 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2682 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2683 /* --verbose is only valid in migrate mode */
2684 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2685 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2688 setstripe_args_init(&lsa);
2690 migrate_mode = (opc == SO_MIGRATE);
2691 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2693 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2695 while ((c = getopt_long(argc, argv,
2696 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2702 case LFS_COMP_ADD_OPT:
2705 case LFS_COMP_DEL_OPT:
2708 case LFS_COMP_FLAGS_OPT:
2709 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2710 &lsa.lsa_comp_neg_flags);
2713 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2714 fprintf(stderr, "%s: inverted flags are not supported\n",
2718 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2720 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2727 case LFS_COMP_SET_OPT:
2730 case LFS_COMP_NO_VERIFY_OPT:
2731 mirror_flags |= MF_NO_VERIFY;
2733 case LFS_MIRROR_ID_OPT:
2734 mirror_id = strtoul(optarg, &end, 0);
2735 if (*end != '\0' || mirror_id == 0) {
2737 "%s %s: invalid mirror ID '%s'\n",
2738 progname, argv[0], optarg);
2742 case LFS_MIRROR_FLAGS_OPT: {
2745 if (!mirror_mode || !last_mirror) {
2746 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2751 result = comp_str2flags(optarg, &last_mirror->m_flags,
2757 fprintf(stderr, "%s: inverted flags are not supported\n",
2762 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2764 "%s: unsupported mirror flags: %s\n",
2771 case LFS_LAYOUT_COPY:
2776 if (!migrate_mode) {
2778 "%s %s: -b|--block valid only for migrate command\n",
2782 migration_block = true;
2785 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2788 "%s %s: invalid stripe count '%s'\n",
2789 progname, argv[0], optarg);
2793 if (lsa.lsa_stripe_count == -1)
2794 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2797 /* delete the default striping pattern */
2799 if (opc == SO_MIRROR_SPLIT) {
2802 "%s %s: -d cannot used with -f\n",
2806 mirror_flags |= MF_DESTROY;
2810 if (!migrate_mode) {
2812 "%s %s: -D|--non-direct is valid "
2813 "only for migrate command\n",
2817 migration_flags |= MIGRATION_NONDIRECT;
2820 if (lsa.lsa_comp_end != 0) {
2821 result = comp_args_to_layout(lpp, &lsa, true);
2824 "%s %s: invalid layout\n",
2829 setstripe_args_init_inherit(&lsa);
2832 if (arg_is_eof(optarg)) {
2833 lsa.lsa_comp_end = LUSTRE_EOF;
2835 result = llapi_parse_size(optarg,
2840 "%s %s: invalid component end '%s'\n",
2841 progname, argv[0], optarg);
2847 if (!migrate_mode) {
2848 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2852 lsa.lsa_pattern = check_hashtype(optarg);
2853 if (lsa.lsa_pattern == 0) {
2855 "%s %s: bad stripe hash type '%s'\n",
2856 progname, argv[0], optarg);
2861 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2864 "%s %s: invalid stripe offset '%s'\n",
2865 progname, argv[0], optarg);
2868 if (lsa.lsa_stripe_off == -1)
2869 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2872 comp_id = strtoul(optarg, &end, 0);
2873 if (*end != '\0' || comp_id == 0 ||
2874 comp_id > LCME_ID_MAX) {
2876 "%s %s: invalid component ID '%s'\n",
2877 progname, argv[0], optarg);
2882 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2884 "error: %s: invalid option: %s\n",
2885 progname, argv[optopt + 1]);
2888 if (opc == SO_MIRROR_EXTEND) {
2889 if (last_mirror == NULL) {
2891 "error: %s: '-N' must exist in front of '%s'\n",
2892 progname, argv[optopt + 1]);
2895 last_mirror->m_file = optarg;
2896 last_mirror->m_count = 1;
2899 if (mirror_list == NULL)
2900 mirror_list = lfs_mirror_alloc();
2901 mirror_list->m_file = optarg;
2906 if (strcmp(argv[optind - 1], "mdt") == 0) {
2907 /* Can be only the first component */
2908 if (layout != NULL) {
2910 fprintf(stderr, "error: 'mdt' layout "
2911 "can be only the first one\n");
2914 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2916 fprintf(stderr, "error: 'mdt' layout "
2917 "size is too big\n");
2920 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2921 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2923 fprintf(stderr, "error: layout '%s' is "
2924 "unknown, supported layouts are: "
2925 "'mdt', 'raid0'\n", argv[optind]);
2930 if (!migrate_mode) {
2932 "%s %s: -m|--mdt-index is valid only for migrate command\n",
2936 migrate_mdt_mode = true;
2937 lsa.lsa_nr_tgts = parse_targets(tgts,
2938 sizeof(tgts) / sizeof(__u32),
2939 lsa.lsa_nr_tgts, optarg);
2940 if (lsa.lsa_nr_tgts < 0) {
2942 "%s %s: invalid MDT target(s) '%s'\n",
2943 progname, argv[0], optarg);
2947 lsa.lsa_tgts = tgts;
2948 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2949 lsa.lsa_stripe_off = tgts[0];
2952 if (!migrate_mode) {
2954 "%s %s: -n|--non-block valid only for migrate command\n",
2958 migration_flags |= MIGRATION_NONBLOCK;
2961 if (opc == SO_SETSTRIPE) {
2962 opc = SO_MIRROR_CREATE;
2966 if (optarg != NULL) {
2967 mirror_count = strtoul(optarg, &end, 0);
2968 if (*end != '\0' || mirror_count == 0) {
2970 "error: %s: bad mirror count: %s\n",
2977 new_mirror = lfs_mirror_alloc();
2978 new_mirror->m_count = mirror_count;
2980 if (mirror_list == NULL)
2981 mirror_list = new_mirror;
2983 if (last_mirror != NULL) {
2984 /* wrap up last mirror */
2985 if (lsa.lsa_comp_end == 0)
2986 lsa.lsa_comp_end = LUSTRE_EOF;
2988 result = comp_args_to_layout(lpp, &lsa, true);
2990 lfs_mirror_free(new_mirror);
2994 setstripe_args_init_inherit(&lsa);
2996 last_mirror->m_next = new_mirror;
2999 last_mirror = new_mirror;
3000 lpp = &last_mirror->m_layout;
3003 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3004 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3005 fprintf(stderr, "warning: '--ost-list' is "
3006 "deprecated, use '--ost' instead\n");
3008 lsa.lsa_nr_tgts = parse_targets(tgts,
3009 sizeof(tgts) / sizeof(__u32),
3010 lsa.lsa_nr_tgts, optarg);
3011 if (lsa.lsa_nr_tgts < 0) {
3013 "%s %s: invalid OST target(s) '%s'\n",
3014 progname, argv[0], optarg);
3018 lsa.lsa_tgts = tgts;
3019 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3020 lsa.lsa_stripe_off = tgts[0];
3025 lsa.lsa_pool_name = optarg;
3027 if (strlen(lsa.lsa_pool_name) == 0 ||
3028 strncmp(lsa.lsa_pool_name, "none",
3029 LOV_MAXPOOLNAME) == 0)
3030 lsa.lsa_pool_name = NULL;
3033 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3037 "%s %s: invalid stripe size '%s'\n",
3038 progname, argv[0], optarg);
3043 if (!migrate_mode) {
3045 "%s %s: -v|--verbose valid only for migrate command\n",
3049 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3050 migration_flags = MIGRATION_VERBOSE;
3057 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3058 progname, argv[0], argv[optind - 1]);
3063 fname = argv[optind];
3065 if (optind == argc) {
3066 fprintf(stderr, "%s %s: FILE must be specified\n",
3071 if (mirror_mode && mirror_count == 0) {
3073 "error: %s: --mirror-count|-N option is required\n",
3080 if (lsa.lsa_comp_end == 0)
3081 lsa.lsa_comp_end = LUSTRE_EOF;
3084 if (lsa.lsa_comp_end != 0) {
3085 result = comp_args_to_layout(lpp, &lsa, true);
3090 if (mirror_flags & MF_NO_VERIFY) {
3091 if (opc != SO_MIRROR_EXTEND) {
3093 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3097 } else if (!has_m_file) {
3099 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3106 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3107 * altered by user space tool, so we don't need to support the
3108 * --component-set for this moment. */
3109 if (comp_set && !comp_id) {
3110 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3115 if ((delete + comp_set + comp_del + comp_add) > 1) {
3117 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3122 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3123 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3125 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3130 if ((comp_set || comp_del) &&
3131 (setstripe_args_specified(&lsa) || layout != NULL)) {
3133 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3138 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3140 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3145 if (comp_add || comp_del) {
3148 result = lstat(fname, &st);
3149 if (result == 0 && S_ISDIR(st.st_mode)) {
3151 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3157 fprintf(stderr, "error: %s: can't use --component-add "
3158 "or --component-del for mirror operation\n",
3165 if (layout == NULL) {
3167 "%s %s: option -E must be specified with --component-add\n",
3172 result = adjust_first_extent(fname, layout);
3173 if (result == -ENODATA)
3175 else if (result != 0)
3179 if (from_yaml && from_copy) {
3181 "%s: can't specify --yaml and --copy together\n",
3186 if ((from_yaml || from_copy) &&
3187 (setstripe_args_specified(&lsa) || layout != NULL)) {
3188 fprintf(stderr, "error: %s: can't specify --yaml with "
3189 "-c, -S, -i, -o, -p or -E options.\n",
3194 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3196 "%s %s: options --non-block and --block are mutually exclusive\n",
3201 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3204 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3209 if (migrate_mdt_mode) {
3210 struct lmv_user_md *lmu;
3212 /* initialize migrate mdt parameters */
3213 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3214 LMV_USER_MAGIC_SPECIFIC));
3217 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3218 progname, argv[0], strerror(ENOMEM));
3222 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3223 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3224 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3226 "%s %s: migrate should specify MDT index\n",
3231 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3232 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3233 lmu->lum_hash_type = lsa.lsa_pattern;
3235 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3236 if (lsa.lsa_pool_name)
3237 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3238 sizeof(lmu->lum_pool_name));
3239 if (lsa.lsa_nr_tgts > 1) {
3242 if (lsa.lsa_stripe_count > 0 &&
3243 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3244 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3246 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3247 progname, lsa.lsa_stripe_count,
3253 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3254 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3255 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3256 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3258 lmu->lum_magic = LMV_USER_MAGIC;
3261 migrate_mdt_param.fp_lmv_md = lmu;
3262 migrate_mdt_param.fp_migrate = 1;
3263 } else if (layout == NULL) {
3264 /* initialize stripe parameters */
3265 param = calloc(1, offsetof(typeof(*param),
3266 lsp_osts[lsa.lsa_nr_tgts]));
3267 if (param == NULL) {
3269 "%s %s: cannot allocate memory for parameters: %s\n",
3270 progname, argv[0], strerror(ENOMEM));
3275 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3276 param->lsp_stripe_size = lsa.lsa_stripe_size;
3277 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3278 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3279 param->lsp_stripe_count = -1;
3281 param->lsp_stripe_count = lsa.lsa_stripe_count;
3283 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3284 param->lsp_stripe_offset = -1;
3286 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3287 param->lsp_pool = lsa.lsa_pool_name;
3288 param->lsp_is_specific = false;
3289 if (lsa.lsa_nr_tgts > 0) {
3290 if (lsa.lsa_stripe_count > 0 &&
3291 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3292 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3293 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3294 fprintf(stderr, "error: %s: stripe count %lld "
3295 "doesn't match the number of OSTs: %d\n"
3296 , argv[0], lsa.lsa_stripe_count,
3302 param->lsp_is_specific = true;
3303 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3304 memcpy(param->lsp_osts, tgts,
3305 sizeof(*tgts) * lsa.lsa_nr_tgts);
3310 /* generate a layout from a YAML template */
3311 result = lfs_comp_create_from_yaml(template, &layout,
3314 fprintf(stderr, "error: %s: can't create composite "
3315 "layout from template file %s\n",
3319 } else if (from_copy) {
3320 layout = llapi_layout_get_by_path(template, 0);
3321 if (layout == NULL) {
3323 "%s: can't create composite layout from file %s.\n",
3324 progname, template);
3329 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3330 if (migrate_mdt_mode) {
3331 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3332 } else if (migrate_mode) {
3333 result = lfs_migrate(fname, migration_flags, param,
3335 } else if (comp_set != 0) {
3336 result = lfs_component_set(fname, comp_id,
3338 lsa.lsa_comp_neg_flags);
3339 } else if (comp_del != 0) {
3340 result = lfs_component_del(fname, comp_id,
3342 lsa.lsa_comp_neg_flags);
3343 } else if (comp_add != 0) {
3344 result = lfs_component_add(fname, layout);
3345 } else if (opc == SO_MIRROR_CREATE) {
3346 result = mirror_create(fname, mirror_list);
3347 } else if (opc == SO_MIRROR_EXTEND) {
3348 result = mirror_extend(fname, mirror_list,
3350 } else if (opc == SO_MIRROR_SPLIT) {
3351 if (mirror_id == 0 && comp_id == 0) {
3353 "%s %s: no mirror id or component id is specified\n",
3358 comp_id = mirror_id;
3360 mirror_flags |= MF_COMP_ID;
3361 result = mirror_split(fname, comp_id, mirror_flags,
3362 has_m_file ? mirror_list->m_file :
3364 } else if (layout != NULL) {
3365 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3372 result = llapi_file_open_param(fname,
3381 /* Save the first error encountered. */
3389 free(migrate_mdt_param.fp_lmv_md);
3390 llapi_layout_free(layout);
3391 lfs_mirror_list_free(mirror_list);
3396 llapi_layout_free(layout);
3397 lfs_mirror_list_free(mirror_list);
3401 static int lfs_poollist(int argc, char **argv)
3406 return llapi_poollist(argv[1]);
3409 static int set_time(time_t *time, time_t *set, char *str)
3417 else if (str[0] == '-')
3423 t = strtol(str, &endptr, 0);
3424 if (*endptr != '\0') {
3426 "%s find: bad time '%s': %s\n",
3427 progname, str, strerror(EINVAL));
3430 if (*time < t * 24 * 60 * 60) {
3434 "%s find: bad time '%s': too large\n",
3439 *set = *time - t * 24 * 60 * 60;
3443 static int name2uid(unsigned int *id, const char *name)
3445 struct passwd *passwd;
3447 passwd = getpwnam(name);
3450 *id = passwd->pw_uid;
3455 static int name2gid(unsigned int *id, const char *name)
3457 struct group *group;
3459 group = getgrnam(name);
3462 *id = group->gr_gid;
3467 static inline int name2projid(unsigned int *id, const char *name)
3472 static int uid2name(char **name, unsigned int id)
3474 struct passwd *passwd;
3476 passwd = getpwuid(id);
3479 *name = passwd->pw_name;
3484 static inline int gid2name(char **name, unsigned int id)
3486 struct group *group;
3488 group = getgrgid(id);
3491 *name = group->gr_name;
3496 static int name2layout(__u32 *layout, char *name)
3498 char *ptr, *layout_name;
3501 for (ptr = name; ; ptr = NULL) {
3502 layout_name = strtok(ptr, ",");
3503 if (layout_name == NULL)
3505 if (strcmp(layout_name, "released") == 0)
3506 *layout |= LOV_PATTERN_F_RELEASED;
3507 else if (strcmp(layout_name, "raid0") == 0)
3508 *layout |= LOV_PATTERN_RAID0;
3509 else if (strcmp(layout_name, "mdt") == 0)
3510 *layout |= LOV_PATTERN_MDT;
3517 static int lfs_find(int argc, char **argv)
3522 struct find_param param = {
3526 struct option long_opts[] = {
3527 { .val = 'A', .name = "atime", .has_arg = required_argument },
3528 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3529 { .val = LFS_COMP_COUNT_OPT,
3530 .name = "comp-count", .has_arg = required_argument },
3531 { .val = LFS_COMP_COUNT_OPT,
3532 .name = "component-count",
3533 .has_arg = required_argument },
3534 { .val = LFS_COMP_FLAGS_OPT,
3535 .name = "comp-flags", .has_arg = required_argument },
3536 { .val = LFS_COMP_FLAGS_OPT,
3537 .name = "component-flags",
3538 .has_arg = required_argument },
3539 { .val = LFS_COMP_START_OPT,
3540 .name = "comp-start", .has_arg = required_argument },
3541 { .val = LFS_COMP_START_OPT,
3542 .name = "component-start",
3543 .has_arg = required_argument },
3544 { .val = LFS_MIRROR_STATE_OPT,
3545 .name = "mirror-state", .has_arg = required_argument },
3546 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3547 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3548 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3549 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3550 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3551 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3552 { .val = 'E', .name = "component-end",
3553 .has_arg = required_argument },
3554 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3555 { .val = 'g', .name = "gid", .has_arg = required_argument },
3556 { .val = 'G', .name = "group", .has_arg = required_argument },
3557 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3558 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3559 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3560 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3561 { .val = 'L', .name = "layout", .has_arg = required_argument },
3562 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3563 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3564 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3565 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3566 { .val = 'n', .name = "name", .has_arg = required_argument },
3567 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3568 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3569 { .val = 'O', .name = "obd", .has_arg = required_argument },
3570 { .val = 'O', .name = "ost", .has_arg = required_argument },
3571 /* no short option for pool yet, can be 'p' after 2.18 */
3572 { .val = LFS_POOL_OPT,
3573 .name = "pool", .has_arg = required_argument },
3574 { .val = '0', .name = "print0", .has_arg = no_argument },
3575 { .val = 'P', .name = "print", .has_arg = no_argument },
3576 { .val = LFS_PROJID_OPT,
3577 .name = "projid", .has_arg = required_argument },
3578 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3579 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3580 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3581 { .val = 's', .name = "size", .has_arg = required_argument },
3582 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3583 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3584 { .val = 't', .name = "type", .has_arg = required_argument },
3585 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3586 { .val = 'u', .name = "uid", .has_arg = required_argument },
3587 { .val = 'U', .name = "user", .has_arg = required_argument },
3588 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3589 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3601 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3602 while ((c = getopt_long_only(argc, argv,
3603 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3604 long_opts, NULL)) >= 0) {
3609 /* '!' is part of option */
3610 /* when getopt_long_only() finds a string which is not
3611 * an option nor a known option argument it returns 1
3612 * in that case if we already have found pathstart and pathend
3613 * (i.e. we have the list of pathnames),
3614 * the only supported value is "!"
3616 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3617 if (!isoption && pathend != -1) {
3618 fprintf(stderr, "err: %s: filename|dirname must either "
3619 "precede options or follow options\n",
3624 if (!isoption && pathstart == -1)
3625 pathstart = optind - 1;
3626 if (isoption && pathstart != -1 && pathend == -1)
3627 pathend = optind - 2;
3633 /* unknown; opt is "!" or path component,
3634 * checking done above.
3636 if (strcmp(optarg, "!") == 0)
3640 xtime = ¶m.fp_atime;
3641 xsign = ¶m.fp_asign;
3642 param.fp_exclude_atime = !!neg_opt;
3643 /* no break, this falls through to 'C' for ctime */
3646 xtime = ¶m.fp_ctime;
3647 xsign = ¶m.fp_csign;
3648 param.fp_exclude_ctime = !!neg_opt;
3650 /* no break, this falls through to 'M' for mtime */
3653 xtime = ¶m.fp_mtime;
3654 xsign = ¶m.fp_msign;
3655 param.fp_exclude_mtime = !!neg_opt;
3657 rc = set_time(&t, xtime, optarg);
3658 if (rc == INT_MAX) {
3666 if (optarg[0] == '+') {
3667 param.fp_blocks_sign = -1;
3669 } else if (optarg[0] == '-') {
3670 param.fp_blocks_sign = 1;
3674 param.fp_blocks_units = 1024;
3675 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3676 ¶m.fp_blocks_units, 0);
3678 fprintf(stderr, "error: bad blocks '%s'\n",
3682 param.fp_check_blocks = 1;
3683 param.fp_exclude_blocks = !!neg_opt;
3685 case LFS_COMP_COUNT_OPT:
3686 if (optarg[0] == '+') {
3687 param.fp_comp_count_sign = -1;
3689 } else if (optarg[0] == '-') {
3690 param.fp_comp_count_sign = 1;
3694 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3695 if (*endptr != '\0') {
3696 fprintf(stderr, "error: bad component count "
3700 param.fp_check_comp_count = 1;
3701 param.fp_exclude_comp_count = !!neg_opt;
3703 case LFS_COMP_FLAGS_OPT:
3704 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3705 ¶m.fp_comp_neg_flags);
3707 fprintf(stderr, "error: bad component flags "
3711 param.fp_check_comp_flags = 1;
3713 __u32 flags = param.fp_comp_neg_flags;
3714 param.fp_comp_neg_flags = param.fp_comp_flags;
3715 param.fp_comp_flags = flags;
3718 case LFS_COMP_START_OPT:
3719 if (optarg[0] == '+') {
3720 param.fp_comp_start_sign = -1;
3722 } else if (optarg[0] == '-') {
3723 param.fp_comp_start_sign = 1;
3727 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3728 ¶m.fp_comp_start_units, 0);
3730 fprintf(stderr, "error: bad component start "
3734 param.fp_check_comp_start = 1;
3735 param.fp_exclude_comp_start = !!neg_opt;
3737 case LFS_MIRROR_STATE_OPT:
3738 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3739 ¶m.fp_mirror_neg_state);
3742 "error: bad mirrored file state '%s'\n",
3746 param.fp_check_mirror_state = 1;
3748 __u16 state = param.fp_mirror_neg_state;
3749 param.fp_mirror_neg_state =
3750 param.fp_mirror_state;
3751 param.fp_mirror_state = state;
3755 if (optarg[0] == '+') {
3756 param.fp_stripe_count_sign = -1;
3758 } else if (optarg[0] == '-') {
3759 param.fp_stripe_count_sign = 1;
3763 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3764 if (*endptr != '\0') {
3765 fprintf(stderr,"error: bad stripe_count '%s'\n",
3770 param.fp_check_stripe_count = 1;
3771 param.fp_exclude_stripe_count = !!neg_opt;
3774 param.fp_max_depth = strtol(optarg, 0, 0);
3777 if (optarg[0] == '+') {
3778 param.fp_comp_end_sign = -1;
3780 } else if (optarg[0] == '-') {
3781 param.fp_comp_end_sign = 1;
3785 if (arg_is_eof(optarg)) {
3786 param.fp_comp_end = LUSTRE_EOF;
3787 param.fp_comp_end_units = 1;
3790 rc = llapi_parse_size(optarg,
3792 ¶m.fp_comp_end_units, 0);
3795 fprintf(stderr, "error: bad component end "
3799 param.fp_check_comp_end = 1;
3800 param.fp_exclude_comp_end = !!neg_opt;
3804 rc = name2gid(¶m.fp_gid, optarg);
3806 param.fp_gid = strtoul(optarg, &endptr, 10);
3807 if (*endptr != '\0') {
3808 fprintf(stderr, "Group/GID: %s cannot "
3809 "be found.\n", optarg);
3814 param.fp_exclude_gid = !!neg_opt;
3815 param.fp_check_gid = 1;
3818 param.fp_hash_type = check_hashtype(optarg);
3819 if (param.fp_hash_type == 0) {
3820 fprintf(stderr, "error: bad hash_type '%s'\n",
3825 param.fp_check_hash_type = 1;
3826 param.fp_exclude_hash_type = !!neg_opt;
3829 ret = name2layout(¶m.fp_layout, optarg);
3832 param.fp_exclude_layout = !!neg_opt;
3833 param.fp_check_layout = 1;
3837 rc = name2uid(¶m.fp_uid, optarg);
3839 param.fp_uid = strtoul(optarg, &endptr, 10);
3840 if (*endptr != '\0') {
3841 fprintf(stderr, "User/UID: %s cannot "
3842 "be found.\n", optarg);
3847 param.fp_exclude_uid = !!neg_opt;
3848 param.fp_check_uid = 1;
3851 param.fp_pattern = (char *)optarg;
3852 param.fp_exclude_pattern = !!neg_opt;
3855 if (optarg[0] == '+') {
3856 param.fp_mirror_count_sign = -1;
3858 } else if (optarg[0] == '-') {
3859 param.fp_mirror_count_sign = 1;
3863 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3864 if (*endptr != '\0') {
3866 "error: bad mirror count '%s'\n",
3870 param.fp_check_mirror_count = 1;
3871 param.fp_exclude_mirror_count = !!neg_opt;
3876 char *buf, *token, *next, *p;
3880 buf = strdup(optarg);
3886 param.fp_exclude_obd = !!neg_opt;
3889 while (token && *token) {
3890 token = strchr(token, ',');
3897 param.fp_exclude_mdt = !!neg_opt;
3898 param.fp_num_alloc_mdts += len;
3899 tmp = realloc(param.fp_mdt_uuid,
3900 param.fp_num_alloc_mdts *
3901 sizeof(*param.fp_mdt_uuid));
3907 param.fp_mdt_uuid = tmp;
3909 param.fp_exclude_obd = !!neg_opt;
3910 param.fp_num_alloc_obds += len;
3911 tmp = realloc(param.fp_obd_uuid,
3912 param.fp_num_alloc_obds *
3913 sizeof(*param.fp_obd_uuid));
3919 param.fp_obd_uuid = tmp;
3921 for (token = buf; token && *token; token = next) {
3922 struct obd_uuid *puuid;
3925 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3928 ¶m.fp_obd_uuid[param.fp_num_obds++];
3930 p = strchr(token, ',');
3937 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3942 strncpy(puuid->uuid, token,
3943 sizeof(puuid->uuid));
3950 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3954 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3956 "Pool name %s is too long (max %d)\n",
3957 optarg, LOV_MAXPOOLNAME);
3962 * We do check for empty pool because empty pool
3963 * is used to find V1 LOV attributes
3965 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3966 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3967 param.fp_exclude_pool = !!neg_opt;
3968 param.fp_check_pool = 1;
3970 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3971 case 'p': /* want this for --pool, to match getstripe/find */
3973 "warning: -p deprecated, use --print0 or -0\n");
3976 param.fp_zero_end = 1;
3978 case 'P': /* we always print, this option is a no-op */
3980 case LFS_PROJID_OPT:
3981 rc = name2projid(¶m.fp_projid, optarg);
3983 param.fp_projid = strtoul(optarg, &endptr, 10);
3984 if (*endptr != '\0') {
3986 "Invalid project ID: %s",
3992 param.fp_exclude_projid = !!neg_opt;
3993 param.fp_check_projid = 1;
3996 if (optarg[0] == '+') {
3997 param.fp_size_sign = -1;
3999 } else if (optarg[0] == '-') {
4000 param.fp_size_sign = 1;
4004 ret = llapi_parse_size(optarg, ¶m.fp_size,
4005 ¶m.fp_size_units, 0);
4007 fprintf(stderr, "error: bad file size '%s'\n",
4011 param.fp_check_size = 1;
4012 param.fp_exclude_size = !!neg_opt;
4015 if (optarg[0] == '+') {
4016 param.fp_stripe_size_sign = -1;
4018 } else if (optarg[0] == '-') {
4019 param.fp_stripe_size_sign = 1;
4023 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4024 ¶m.fp_stripe_size_units, 0);
4026 fprintf(stderr, "error: bad stripe_size '%s'\n",
4030 param.fp_check_stripe_size = 1;
4031 param.fp_exclude_stripe_size = !!neg_opt;
4034 param.fp_exclude_type = !!neg_opt;
4035 switch (optarg[0]) {
4037 param.fp_type = S_IFBLK;
4040 param.fp_type = S_IFCHR;
4043 param.fp_type = S_IFDIR;
4046 param.fp_type = S_IFREG;
4049 param.fp_type = S_IFLNK;
4052 param.fp_type = S_IFIFO;
4055 param.fp_type = S_IFSOCK;
4058 fprintf(stderr, "error: %s: bad type '%s'\n",
4065 if (optarg[0] == '+') {
4066 param.fp_mdt_count_sign = -1;
4068 } else if (optarg[0] == '-') {
4069 param.fp_mdt_count_sign = 1;
4073 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4074 if (*endptr != '\0') {
4075 fprintf(stderr, "error: bad mdt_count '%s'\n",
4080 param.fp_check_mdt_count = 1;
4081 param.fp_exclude_mdt_count = !!neg_opt;
4089 if (pathstart == -1) {
4090 fprintf(stderr, "error: %s: no filename|pathname\n",
4094 } else if (pathend == -1) {
4100 rc = llapi_find(argv[pathstart], ¶m);
4101 if (rc != 0 && ret == 0)
4103 } while (++pathstart < pathend);
4106 fprintf(stderr, "error: %s failed for %s.\n",
4107 argv[0], argv[optind - 1]);
4109 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4110 free(param.fp_obd_uuid);
4112 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4113 free(param.fp_mdt_uuid);
4118 static int lfs_getstripe_internal(int argc, char **argv,
4119 struct find_param *param)
4121 struct option long_opts[] = {
4122 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4123 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4124 { .val = LFS_COMP_COUNT_OPT,
4125 .name = "comp-count", .has_arg = no_argument },
4126 { .val = LFS_COMP_COUNT_OPT,
4127 .name = "component-count", .has_arg = no_argument },
4128 { .val = LFS_COMP_FLAGS_OPT,
4129 .name = "comp-flags", .has_arg = optional_argument },
4130 { .val = LFS_COMP_FLAGS_OPT,
4131 .name = "component-flags", .has_arg = optional_argument },
4132 { .val = LFS_COMP_START_OPT,
4133 .name = "comp-start", .has_arg = optional_argument },
4134 { .val = LFS_COMP_START_OPT,
4135 .name = "component-start", .has_arg = optional_argument },
4136 { .val = LFS_MIRROR_INDEX_OPT,
4137 .name = "mirror-index", .has_arg = required_argument },
4138 { .val = LFS_MIRROR_ID_OPT,
4139 .name = "mirror-id", .has_arg = required_argument },
4140 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4141 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4142 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4143 { .val = 'd', .name = "directory", .has_arg = no_argument },
4144 { .val = 'D', .name = "default", .has_arg = no_argument },
4145 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4146 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4147 { .val = 'F', .name = "fid", .has_arg = no_argument },
4148 { .val = 'g', .name = "generation", .has_arg = no_argument },
4149 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4150 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4151 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4152 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4153 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4154 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4155 { .val = 'L', .name = "layout", .has_arg = no_argument },
4156 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4157 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4158 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4159 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4160 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4161 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4162 { .val = 'O', .name = "obd", .has_arg = required_argument },
4163 { .val = 'O', .name = "ost", .has_arg = required_argument },
4164 { .val = 'p', .name = "pool", .has_arg = no_argument },
4165 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4166 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4167 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4168 { .val = 'R', .name = "raw", .has_arg = no_argument },
4169 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4170 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4171 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4172 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4173 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4174 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4175 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4176 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4180 int pathstart = -1, pathend = -1;
4184 while ((c = getopt_long(argc, argv,
4185 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4186 long_opts, NULL)) != -1) {
4190 /* '!' is part of option */
4191 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4192 if (!isoption && pathend != -1) {
4194 "error: %s: filename|dirname must either precede options or follow options\n",
4198 if (!isoption && pathstart == -1)
4199 pathstart = optind - 1;
4200 if (isoption && pathstart != -1 && pathend == -1)
4201 pathend = optind - 2;
4205 /* unknown: opt is "!" */
4206 if (strcmp(optarg, "!") == 0)
4210 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4211 param->fp_verbose |= VERBOSE_COUNT;
4212 param->fp_max_depth = 0;
4215 case LFS_COMP_COUNT_OPT:
4216 param->fp_verbose |= VERBOSE_COMP_COUNT;
4217 param->fp_max_depth = 0;
4219 case LFS_COMP_FLAGS_OPT:
4220 if (optarg != NULL) {
4221 rc = comp_str2flags(optarg,
4222 ¶m->fp_comp_flags,
4223 ¶m->fp_comp_neg_flags);
4225 fprintf(stderr, "error: %s bad "
4226 "component flags '%s'.\n",
4230 param->fp_check_comp_flags = 1;
4232 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4233 param->fp_max_depth = 0;
4236 case LFS_COMP_START_OPT:
4237 if (optarg != NULL) {
4239 if (tmp[0] == '+') {
4240 param->fp_comp_start_sign = -1;
4242 } else if (tmp[0] == '-') {
4243 param->fp_comp_start_sign = 1;
4246 rc = llapi_parse_size(tmp,
4247 ¶m->fp_comp_start,
4248 ¶m->fp_comp_start_units, 0);
4250 fprintf(stderr, "error: %s bad "
4251 "component start '%s'.\n",
4255 param->fp_check_comp_start = 1;
4258 param->fp_verbose |= VERBOSE_COMP_START;
4259 param->fp_max_depth = 0;
4262 case LFS_MIRROR_INDEX_OPT:
4263 if (optarg[0] == '+') {
4264 param->fp_mirror_index_sign = -1;
4266 } else if (optarg[0] == '-') {
4267 param->fp_mirror_index_sign = 1;
4271 param->fp_mirror_index = strtoul(optarg, &end, 0);
4272 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4273 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4275 "%s %s: invalid mirror index '%s'\n",
4276 progname, argv[0], optarg);
4279 if (param->fp_mirror_id != 0) {
4281 "%s %s: can't specify both mirror index and mirror ID\n",
4285 param->fp_check_mirror_index = 1;
4286 param->fp_exclude_mirror_index = !!neg_opt;
4288 case LFS_MIRROR_ID_OPT:
4289 if (optarg[0] == '+') {
4290 param->fp_mirror_id_sign = -1;
4292 } else if (optarg[0] == '-') {
4293 param->fp_mirror_id_sign = 1;
4297 param->fp_mirror_id = strtoul(optarg, &end, 0);
4298 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4299 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4301 "%s %s: invalid mirror ID '%s'\n",
4302 progname, argv[0], optarg);
4305 if (param->fp_mirror_index != 0) {
4307 "%s %s: can't specify both mirror index and mirror ID\n",
4311 param->fp_check_mirror_id = 1;
4312 param->fp_exclude_mirror_id = !!neg_opt;
4315 param->fp_max_depth = 0;
4318 param->fp_get_default_lmv = 1;
4321 if (optarg != NULL) {
4323 if (tmp[0] == '+') {
4324 param->fp_comp_end_sign = -1;
4326 } else if (tmp[0] == '-') {
4327 param->fp_comp_end_sign = 1;
4331 if (arg_is_eof(tmp)) {
4332 param->fp_comp_end = LUSTRE_EOF;
4333 param->fp_comp_end_units = 1;
4336 rc = llapi_parse_size(tmp,
4337 ¶m->fp_comp_end,
4338 ¶m->fp_comp_end_units, 0);
4341 fprintf(stderr, "error: %s bad "
4342 "component end '%s'.\n",
4346 param->fp_check_comp_end = 1;
4348 param->fp_verbose |= VERBOSE_COMP_END;
4349 param->fp_max_depth = 0;
4353 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4354 param->fp_verbose |= VERBOSE_DFID;
4355 param->fp_max_depth = 0;
4359 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4360 param->fp_verbose |= VERBOSE_GENERATION;
4361 param->fp_max_depth = 0;
4365 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4366 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4367 param->fp_max_depth = 0;
4371 if (optarg != NULL) {
4372 param->fp_comp_id = strtoul(optarg, &end, 0);
4373 if (*end != '\0' || param->fp_comp_id == 0 ||
4374 param->fp_comp_id > LCME_ID_MAX) {
4375 fprintf(stderr, "error: %s bad "
4376 "component id '%s'\n",
4380 param->fp_check_comp_id = 1;
4383 param->fp_max_depth = 0;
4384 param->fp_verbose |= VERBOSE_COMP_ID;
4388 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4389 param->fp_verbose |= VERBOSE_PATTERN;
4390 param->fp_max_depth = 0;
4393 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4395 fprintf(stderr, "warning: '-M' deprecated"
4396 ", use '--mdt-index' or '-m' instead\n");
4399 if (!(param->fp_verbose & VERBOSE_DETAIL))
4400 param->fp_max_depth = 0;
4401 param->fp_verbose |= VERBOSE_MDTINDEX;
4404 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4405 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4406 param->fp_max_depth = 0;
4410 if (param->fp_obd_uuid) {
4412 "error: %s: only one obduuid allowed",
4416 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4419 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4420 param->fp_verbose |= VERBOSE_POOL;
4421 param->fp_max_depth = 0;
4428 param->fp_recursive = 1;
4434 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4435 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4436 param->fp_max_depth = 0;
4440 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4450 if (pathstart == -1) {
4451 fprintf(stderr, "error: %s: no filename|pathname\n",
4454 } else if (pathend == -1) {
4462 if (param->fp_recursive)
4463 param->fp_max_depth = -1;
4464 else if (param->fp_verbose & VERBOSE_DETAIL)
4465 param->fp_max_depth = 1;
4467 if (!param->fp_verbose)
4468 param->fp_verbose = VERBOSE_DEFAULT;
4469 if (param->fp_quiet)
4470 param->fp_verbose = VERBOSE_OBJID;
4473 rc = llapi_getstripe(argv[pathstart], param);
4474 } while (++pathstart < pathend && !rc);
4477 fprintf(stderr, "error: %s failed for %s.\n",
4478 argv[0], argv[optind - 1]);
4482 static int lfs_tgts(int argc, char **argv)
4484 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4485 struct find_param param;
4486 int index = 0, rc=0;
4491 if (argc == 2 && !realpath(argv[1], path)) {
4493 fprintf(stderr, "error: invalid path '%s': %s\n",
4494 argv[1], strerror(-rc));
4498 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4499 /* Check if we have a mount point */
4500 if (mntdir[0] == '\0')
4503 memset(¶m, 0, sizeof(param));
4504 if (!strcmp(argv[0], "mdts"))
4505 param.fp_get_lmv = 1;
4507 rc = llapi_ostlist(mntdir, ¶m);
4509 fprintf(stderr, "error: %s: failed on %s\n",
4512 if (path[0] != '\0')
4514 memset(mntdir, 0, PATH_MAX);
4520 static int lfs_getstripe(int argc, char **argv)
4522 struct find_param param = { 0 };
4524 param.fp_max_depth = 1;
4525 return lfs_getstripe_internal(argc, argv, ¶m);
4529 static int lfs_getdirstripe(int argc, char **argv)
4531 struct find_param param = { 0 };
4532 struct option long_opts[] = {
4533 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4534 { .val = 'D', .name = "default", .has_arg = no_argument },
4535 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4536 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4537 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4538 { .val = 'O', .name = "obd", .has_arg = required_argument },
4539 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4540 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4541 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4545 param.fp_get_lmv = 1;
4547 while ((c = getopt_long(argc, argv,
4548 "cDHimO:rtTy", long_opts, NULL)) != -1)
4553 param.fp_verbose |= VERBOSE_COUNT;
4556 param.fp_get_default_lmv = 1;
4560 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4562 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4564 fprintf(stderr, "warning: '-t' deprecated, "
4565 "use '--mdt-hash' or '-H' instead\n");
4568 param.fp_verbose |= VERBOSE_HASH_TYPE;
4571 if (param.fp_obd_uuid) {
4573 "error: %s: only one obduuid allowed",
4577 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4580 param.fp_recursive = 1;
4593 if (param.fp_recursive)
4594 param.fp_max_depth = -1;
4596 if (!param.fp_verbose)
4597 param.fp_verbose = VERBOSE_DEFAULT;
4600 rc = llapi_getstripe(argv[optind], ¶m);
4601 } while (++optind < argc && !rc);
4604 fprintf(stderr, "error: %s failed for %s.\n",
4605 argv[0], argv[optind - 1]);
4610 MNTDF_INODES = 0x0001,
4611 MNTDF_COOKED = 0x0002,
4612 MNTDF_LAZY = 0x0004,
4613 MNTDF_VERBOSE = 0x0008,
4614 MNTDF_SHOW = 0x0010,
4617 #define COOK(value) \
4620 while (value > 1024) { \
4628 #define CDF "%11llu"
4629 #define HDF "%8.1f%c"
4633 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4635 double avail, used, ratio = 0;
4638 avail = st->os_ffree;
4639 used = st->os_files - st->os_ffree;
4641 avail = st->os_bavail;
4642 used = st->os_blocks - st->os_bfree;
4644 if (avail + used > 0)
4645 ratio = used / (used + avail) * 100;
4647 /* Round up to match df(1) usage percentage */
4648 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4651 static int showdf(char *mntdir, struct obd_statfs *stat,
4652 char *uuid, enum mntdf_flags flags,
4653 char *type, int index, int rc)
4655 long long avail, used, total;
4657 char *suffix = "KMGTPEZY";
4658 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4659 char tbuf[3 * sizeof(__u64)];
4660 char ubuf[3 * sizeof(__u64)];
4661 char abuf[3 * sizeof(__u64)];
4662 char rbuf[3 * sizeof(__u64)];
4669 if (flags & MNTDF_INODES) {
4670 avail = stat->os_ffree;
4671 used = stat->os_files - stat->os_ffree;
4672 total = stat->os_files;
4674 int shift = flags & MNTDF_COOKED ? 0 : 10;
4676 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4677 used = ((stat->os_blocks - stat->os_bfree) *
4678 stat->os_bsize) >> shift;
4679 total = (stat->os_blocks * stat->os_bsize) >> shift;
4682 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4684 if (flags & MNTDF_COOKED) {
4688 cook_val = (double)total;
4691 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4694 snprintf(tbuf, sizeof(tbuf), CDF, total);
4696 cook_val = (double)used;
4699 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4702 snprintf(ubuf, sizeof(ubuf), CDF, used);
4704 cook_val = (double)avail;
4707 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4710 snprintf(abuf, sizeof(abuf), CDF, avail);
4712 snprintf(tbuf, sizeof(tbuf), CDF, total);
4713 snprintf(ubuf, sizeof(tbuf), CDF, used);
4714 snprintf(abuf, sizeof(tbuf), CDF, avail);
4717 sprintf(rbuf, RDF, ratio);
4718 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4719 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4721 printf("[%s:%d]", type, index);
4723 if (stat->os_state) {
4725 * Each character represents the matching
4728 const char state_names[] = "DRSI";
4733 for (i = 0, state = stat->os_state;
4734 state && i < sizeof(state_names); i++) {
4735 if (!(state & (1 << i)))
4737 printf("%c", state_names[i]);
4745 printf(UUF": inactive device\n", uuid);
4748 printf(UUF": %s\n", uuid, strerror(-rc));
4755 struct ll_stat_type {
4760 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4762 struct ll_statfs_data {
4764 struct obd_statfs sd_st;
4767 struct ll_statfs_buf {
4769 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4772 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4773 int ops, struct ll_statfs_buf *lsb)
4775 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4776 struct obd_uuid uuid_buf;
4777 char *poolname = NULL;
4778 struct ll_stat_type types[] = {
4779 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4780 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4781 { .st_name = NULL } };
4782 struct ll_stat_type *tp;
4783 __u64 ost_ffree = 0;
4791 poolname = strchr(pool, '.');
4792 if (poolname != NULL) {
4793 if (strncmp(fsname, pool, strlen(fsname))) {
4794 fprintf(stderr, "filesystem name incorrect\n");
4802 fd = open(mntdir, O_RDONLY);
4805 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4810 if (flags & MNTDF_SHOW) {
4811 if (flags & MNTDF_INODES)
4812 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4813 "UUID", "Inodes", "IUsed", "IFree",
4814 "IUse%", "Mounted on");
4816 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4818 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4819 "Used", "Available", "Use%", "Mounted on");
4822 for (tp = types; tp->st_name != NULL; tp++) {
4823 bool have_ost = false;
4825 if (!(tp->st_op & ops))
4828 for (index = 0; ; index++) {
4829 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4830 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4831 type = flags & MNTDF_LAZY ?
4832 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4833 rc2 = llapi_obd_fstatfs(fd, type, index,
4834 &stat_buf, &uuid_buf);
4839 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4840 if (!(flags & MNTDF_VERBOSE))
4842 } else if (rc2 < 0 && rc == 0) {
4846 /* If we have OSTs then don't report MDT block counts.
4847 * For MDT-only filesystems the expectation is that all
4848 * layouts have a DoM component. For filesystems with
4849 * OSTs, files are not necessarily going to store data
4850 * on MDTs, and MDT space is limited to a fraction of
4851 * OST space, so don't include it in the summary.
4853 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
4860 if (poolname && tp->st_op == LL_STATFS_LOV &&
4861 llapi_search_ost(fsname, poolname,
4862 obd_uuid2str(&uuid_buf)) != 1)
4865 /* the llapi_obd_statfs() call may have returned with
4866 * an error, but if it filled in uuid_buf we will at
4867 * lease use that to print out a message for that OBD.
4868 * If we didn't get anything in the uuid_buf, then fill
4869 * it in so that we can print an error message. */
4870 if (uuid_buf.uuid[0] == '\0')
4871 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4872 "%s%04x", tp->st_name, index);
4874 lsb->sb_buf[lsb->sb_count].sd_index = index;
4875 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4878 if (flags & MNTDF_SHOW)
4879 showdf(mntdir, &stat_buf,
4880 obd_uuid2str(&uuid_buf), flags,
4881 tp->st_name, index, rc2);
4886 if (tp->st_op == LL_STATFS_LMV) {
4887 sum.os_ffree += stat_buf.os_ffree;
4888 sum.os_files += stat_buf.os_files;
4889 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4890 ost_ffree += stat_buf.os_ffree;
4892 sum.os_blocks += stat_buf.os_blocks *
4894 sum.os_bfree += stat_buf.os_bfree *
4896 sum.os_bavail += stat_buf.os_bavail *
4903 /* If we don't have as many objects free on the OST as inodes
4904 * on the MDS, we reduce the total number of inodes to
4905 * compensate, so that the "inodes in use" number is correct.
4906 * Matches ll_statfs_internal() so the results are consistent. */
4907 if (ost_ffree < sum.os_ffree) {
4908 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4909 sum.os_ffree = ost_ffree;
4911 if (flags & MNTDF_SHOW) {
4913 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4920 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4922 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4924 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4926 int r1 = obd_statfs_ratio(st1, false);
4927 int r2 = obd_statfs_ratio(st2, false);
4928 int64_t result = r1 - r2;
4930 /* if both space usage are above 90, compare free inodes */
4931 if (r1 > 90 && r2 > 90)
4932 result = st2->os_ffree - st1->os_ffree;
4936 else if (result == 0)
4943 static int lfs_setdirstripe(int argc, char **argv)
4947 struct lfs_setstripe_args lsa = { 0 };
4948 struct llapi_stripe_param *param = NULL;
4949 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4952 char *mode_opt = NULL;
4953 bool default_stripe = false;
4954 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4955 mode_t previous_mode = 0;
4956 bool delete = false;
4957 struct ll_statfs_buf *lsb = NULL;
4958 char mntdir[PATH_MAX] = "";
4959 bool auto_distributed = false;
4961 struct option long_opts[] = {
4962 { .val = 'c', .name = "count", .has_arg = required_argument },
4963 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4964 { .val = 'd', .name = "delete", .has_arg = no_argument },
4965 { .val = 'D', .name = "default", .has_arg = no_argument },
4966 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4967 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4968 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
4969 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4970 { .val = 'i', .name = "mdt", .has_arg = required_argument },
4972 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4973 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4975 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4976 { .val = 'i', .name = "index", .has_arg = required_argument },
4978 { .val = 'o', .name = "mode", .has_arg = required_argument },
4979 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4980 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4982 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4983 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4986 setstripe_args_init(&lsa);
4988 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4996 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4999 "%s %s: invalid stripe count '%s'\n",
5000 progname, argv[0], optarg);
5006 default_stripe = true;
5009 default_stripe = true;
5011 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5013 fprintf(stderr, "warning: '--hash-type' and '-t' "
5014 "deprecated, use '--mdt-hash' or '-H' instead\n");
5017 lsa.lsa_pattern = check_hashtype(optarg);
5018 if (lsa.lsa_pattern == 0) {
5020 "%s %s: bad stripe hash type '%s'\n",
5021 progname, argv[0], optarg);
5026 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5029 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5030 if (strcmp(argv[optind - 1], "--index") == 0)
5032 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5035 lsa.lsa_nr_tgts = parse_targets(mdts,
5036 sizeof(mdts) / sizeof(__u32),
5037 lsa.lsa_nr_tgts, optarg);
5038 if (lsa.lsa_nr_tgts < 0) {
5040 "%s %s: invalid MDT target(s) '%s'\n",
5041 progname, argv[0], optarg);
5045 lsa.lsa_tgts = mdts;
5046 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5047 lsa.lsa_stripe_off = mdts[0];
5049 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5051 fprintf(stderr, "warning: '-m' is deprecated, "
5052 "use '--mode' or '-o' instead\n");
5058 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5059 progname, argv[0], argv[optind - 1]);
5064 if (optind == argc) {
5065 fprintf(stderr, "%s %s: DIR must be specified\n",
5070 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5071 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5073 "%s %s: stripe offset and count must be specified\n",
5079 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5080 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5082 "%s %s: cannot specify -d with -c or -i options\n",
5087 if (mode_opt != NULL) {
5088 mode = strtoul(mode_opt, &end, 8);
5091 "%s %s: bad MODE '%s'\n",
5092 progname, argv[0], mode_opt);
5095 previous_mode = umask(0);
5099 * initialize stripe parameters, in case param is converted to specific,
5100 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5102 param = calloc(1, offsetof(typeof(*param),
5103 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5104 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5105 if (param == NULL) {
5107 "%s %s: cannot allocate memory for parameters: %s\n",
5108 progname, argv[0], strerror(ENOMEM));
5112 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5113 param->lsp_stripe_count = lsa.lsa_stripe_count;
5114 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5115 param->lsp_stripe_offset = -1;
5117 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5118 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5119 param->lsp_stripe_pattern = lsa.lsa_pattern;
5121 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5122 param->lsp_pool = lsa.lsa_pool_name;
5123 param->lsp_is_specific = false;
5124 if (lsa.lsa_nr_tgts > 1) {
5125 if (lsa.lsa_stripe_count > 0 &&
5126 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5127 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5128 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5129 "match the number of MDTs: %d\n",
5130 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5135 param->lsp_is_specific = true;
5136 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5137 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5140 dname = argv[optind];
5142 if (default_stripe) {
5143 result = llapi_dir_set_default_lmv(dname, param);
5145 /* if current \a dname isn't under the same \a mntdir
5146 * as the last one, and the last one was
5147 * auto-distributed, restore \a param.
5149 if (mntdir[0] != '\0' &&
5150 strncmp(dname, mntdir, strlen(mntdir)) &&
5152 param->lsp_is_specific = false;
5153 param->lsp_stripe_offset = -1;
5154 auto_distributed = false;
5157 if (!param->lsp_is_specific &&
5158 param->lsp_stripe_offset == -1) {
5159 char path[PATH_MAX] = "";
5162 lsb = malloc(sizeof(*lsb));
5170 /* use mntdir for dirname() temporarily */
5171 strncpy(mntdir, dname, sizeof(mntdir));
5172 if (!realpath(dirname(mntdir), path)) {
5175 "error: invalid path '%s': %s\n",
5176 argv[optind], strerror(errno));
5181 result = llapi_search_mounts(path, 0, mntdir,
5183 if (result < 0 || mntdir[0] == '\0') {
5185 "No suitable Lustre mount found\n");
5189 result = mntdf(mntdir, NULL, NULL, 0,
5190 LL_STATFS_LMV, lsb);
5194 if (param->lsp_stripe_count > lsb->sb_count) {
5196 "error: stripe count %d is too big\n",
5197 param->lsp_stripe_count);
5202 qsort(lsb->sb_buf, lsb->sb_count,
5203 sizeof(struct ll_statfs_data),
5204 ll_statfs_data_comp);
5206 auto_distributed = true;
5209 if (auto_distributed) {
5211 int nr = MAX(param->lsp_stripe_count,
5214 /* don't use server whose usage is above 90% */
5215 while (nr != param->lsp_stripe_count &&
5216 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5218 nr = MAX(param->lsp_stripe_count,
5221 /* get \a r between [0, nr) */
5224 param->lsp_stripe_offset =
5225 lsb->sb_buf[r].sd_index;
5226 if (param->lsp_stripe_count > 1) {
5229 param->lsp_is_specific = true;
5230 for (; i < param->lsp_stripe_count; i++)
5231 param->lsp_tgts[(i + r) % nr] =
5232 lsb->sb_buf[i].sd_index;
5236 result = llapi_dir_create(dname, mode, param);
5241 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5242 progname, dname, strerror(-result));
5245 dname = argv[++optind];
5246 } while (dname != NULL);
5248 if (mode_opt != NULL)
5249 umask(previous_mode);
5257 static int lfs_rmentry(int argc, char **argv)
5264 fprintf(stderr, "error: %s: missing dirname\n",
5270 dname = argv[index];
5271 while (dname != NULL) {
5272 result = llapi_direntry_remove(dname);
5274 fprintf(stderr, "error: %s: remove dir entry '%s' "
5275 "failed\n", argv[0], dname);
5278 dname = argv[++index];
5283 static int lfs_mv(int argc, char **argv)
5285 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5286 struct find_param param = {
5293 struct option long_opts[] = {
5294 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5295 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5296 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5299 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5301 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5303 fprintf(stderr, "warning: '-M' deprecated"
5304 ", use '--mdt-index' or '-m' instead\n");
5307 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5309 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5315 param.fp_verbose = VERBOSE_DETAIL;
5318 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5319 progname, argv[optind - 1]);
5324 if (lmu.lum_stripe_offset == -1) {
5325 fprintf(stderr, "%s mv: MDT index must be specified\n",
5330 if (optind >= argc) {
5331 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5336 /* initialize migrate mdt parameters */
5337 param.fp_lmv_md = &lmu;
5338 param.fp_migrate = 1;
5339 rc = llapi_migrate_mdt(argv[optind], ¶m);
5341 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5342 progname, argv[optind], param.fp_mdt_index,
5347 static int lfs_osts(int argc, char **argv)
5349 return lfs_tgts(argc, argv);
5352 static int lfs_mdts(int argc, char **argv)
5354 return lfs_tgts(argc, argv);
5357 static int lfs_df(int argc, char **argv)
5359 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5360 enum mntdf_flags flags = MNTDF_SHOW;
5361 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5362 int c, rc = 0, index = 0;
5363 char fsname[PATH_MAX] = "", *pool_name = NULL;
5364 struct option long_opts[] = {
5365 { .val = 'h', .name = "human-readable",
5366 .has_arg = no_argument },
5367 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5368 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5369 { .val = 'p', .name = "pool", .has_arg = required_argument },
5370 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5373 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5376 flags |= MNTDF_COOKED;
5379 flags |= MNTDF_INODES;
5382 flags |= MNTDF_LAZY;
5388 flags |= MNTDF_VERBOSE;
5394 if (optind < argc && !realpath(argv[optind], path)) {
5396 fprintf(stderr, "error: invalid path '%s': %s\n",
5397 argv[optind], strerror(-rc));
5401 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5402 /* Check if we have a mount point */
5403 if (mntdir[0] == '\0')
5406 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5407 if (rc || path[0] != '\0')
5409 fsname[0] = '\0'; /* avoid matching in next loop */
5410 mntdir[0] = '\0'; /* avoid matching in next loop */
5416 static int lfs_getname(int argc, char **argv)
5418 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5419 int rc = 0, index = 0, c;
5420 char buf[sizeof(struct obd_uuid)];
5422 while ((c = getopt(argc, argv, "h")) != -1)
5425 if (optind == argc) { /* no paths specified, get all paths. */
5426 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5427 rc = llapi_getname(mntdir, buf, sizeof(buf));
5430 "cannot get name for `%s': %s\n",
5431 mntdir, strerror(-rc));
5435 printf("%s %s\n", buf, mntdir);
5437 path[0] = fsname[0] = mntdir[0] = 0;
5439 } else { /* paths specified, only attempt to search these. */
5440 for (; optind < argc; optind++) {
5441 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5444 "cannot get name for `%s': %s\n",
5445 argv[optind], strerror(-rc));
5449 printf("%s %s\n", buf, argv[optind]);
5455 static int lfs_check(int argc, char **argv)
5457 char mntdir[PATH_MAX] = {'\0'};
5466 fprintf(stderr, "%s check: server type must be specified\n",
5471 obd_types[0] = obd_type1;
5472 obd_types[1] = obd_type2;
5473 obd_types[2] = obd_type3;
5475 if (strcmp(argv[1], "osts") == 0) {
5476 strcpy(obd_types[0], "osc");
5477 } else if (strcmp(argv[1], "mdts") == 0 ||
5478 strcmp(argv[1], "mds") == 0) {
5479 strcpy(obd_types[0], "mdc");
5480 } else if (strcmp(argv[1], "mgts") == 0) {
5481 strcpy(obd_types[0], "mgc");
5482 } else if (strcmp(argv[1], "all") == 0 ||
5483 strcmp(argv[1], "servers") == 0) {
5485 strcpy(obd_types[0], "osc");
5486 strcpy(obd_types[1], "mdc");
5487 strcpy(obd_types[2], "mgc");
5489 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5494 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5495 if (rc < 0 || mntdir[0] == '\0') {
5497 "%s check: cannot find mounted Lustre filesystem: %s\n",
5498 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5502 rc = llapi_target_check(num_types, obd_types, mntdir);
5504 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5505 progname, argv[1], strerror(-rc));
5511 #ifdef HAVE_SYS_QUOTA_H
5512 #define ARG2INT(nr, str, msg) \
5515 nr = strtol(str, &endp, 0); \
5516 if (*endp != '\0') { \
5517 fprintf(stderr, "%s: bad %s '%s'\n", \
5518 progname, msg, str); \
5523 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5525 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5526 * returns the value or ULONG_MAX on integer overflow or incorrect format
5528 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5529 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5530 * 3. empty integer value is interpreted as 0
5532 static unsigned long str2sec(const char* timestr)
5534 const char spec[] = "smhdw";
5535 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5536 unsigned long val = 0;
5539 if (strpbrk(timestr, spec) == NULL) {
5540 /* no specifiers inside the time string,
5541 should treat it as an integer value */
5542 val = strtoul(timestr, &tail, 10);
5543 return *tail ? ULONG_MAX : val;
5546 /* format string is XXwXXdXXhXXmXXs */
5552 v = strtoul(timestr, &tail, 10);
5553 if (v == ULONG_MAX || *tail == '\0')
5554 /* value too large (ULONG_MAX or more)
5555 or missing specifier */
5558 ptr = strchr(spec, *tail);
5560 /* unknown specifier */
5565 /* check if product will overflow the type */
5566 if (!(v < ULONG_MAX / mult[ind]))
5569 ADD_OVERFLOW(val, mult[ind] * v);
5570 if (val == ULONG_MAX)
5582 #define ARG2ULL(nr, str, def_units) \
5584 unsigned long long limit, units = def_units; \
5587 rc = llapi_parse_size(str, &limit, &units, 1); \
5589 fprintf(stderr, "%s: invalid limit '%s'\n", \
5596 static inline int has_times_option(int argc, char **argv)
5600 for (i = 1; i < argc; i++)
5601 if (!strcmp(argv[i], "-t"))
5607 int lfs_setquota_times(int argc, char **argv)
5610 struct if_quotactl qctl;
5611 char *mnt, *obd_type = (char *)qctl.obd_type;
5612 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5613 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5614 struct option long_opts[] = {
5615 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5616 { .val = 'g', .name = "group", .has_arg = no_argument },
5617 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5618 { .val = 'p', .name = "projid", .has_arg = no_argument },
5619 { .val = 't', .name = "times", .has_arg = no_argument },
5620 { .val = 'u', .name = "user", .has_arg = no_argument },
5624 memset(&qctl, 0, sizeof(qctl));
5625 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5626 qctl.qc_type = ALLQUOTA;
5628 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5629 long_opts, NULL)) != -1) {
5640 if (qctl.qc_type != ALLQUOTA) {
5641 fprintf(stderr, "error: -u/g/p can't be used "
5642 "more than once\n");
5645 qctl.qc_type = qtype;
5648 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5649 fprintf(stderr, "error: bad block-grace: %s\n",
5653 dqb->dqb_valid |= QIF_BTIME;
5656 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5657 fprintf(stderr, "error: bad inode-grace: %s\n",
5661 dqb->dqb_valid |= QIF_ITIME;
5663 case 't': /* Yes, of course! */
5665 default: /* getopt prints error message for us when opterr != 0 */
5670 if (qctl.qc_type == ALLQUOTA) {
5671 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5675 if (optind != argc - 1) {
5676 fprintf(stderr, "error: unexpected parameters encountered\n");
5680 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5681 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5682 fprintf(stderr, "error: grace time is too large\n");
5687 rc = llapi_quotactl(mnt, &qctl);
5690 fprintf(stderr, "%s %s ", obd_type,
5691 obd_uuid2str(&qctl.obd_uuid));
5692 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5699 #define BSLIMIT (1 << 0)
5700 #define BHLIMIT (1 << 1)
5701 #define ISLIMIT (1 << 2)
5702 #define IHLIMIT (1 << 3)
5704 int lfs_setquota(int argc, char **argv)
5707 struct if_quotactl qctl;
5708 char *mnt, *obd_type = (char *)qctl.obd_type;
5709 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5710 struct option long_opts[] = {
5711 { .val = 'b', .name = "block-softlimit",
5712 .has_arg = required_argument },
5713 { .val = 'B', .name = "block-hardlimit",
5714 .has_arg = required_argument },
5715 { .val = 'd', .name = "default", .has_arg = no_argument },
5716 { .val = 'g', .name = "group", .has_arg = required_argument },
5717 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5718 { .val = 'i', .name = "inode-softlimit",
5719 .has_arg = required_argument },
5720 { .val = 'I', .name = "inode-hardlimit",
5721 .has_arg = required_argument },
5722 { .val = 'p', .name = "projid", .has_arg = required_argument },
5723 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5724 { .val = 'u', .name = "user", .has_arg = required_argument },
5725 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5727 unsigned limit_mask = 0;
5729 bool use_default = false;
5732 if (has_times_option(argc, argv))
5733 return lfs_setquota_times(argc, argv);
5735 memset(&qctl, 0, sizeof(qctl));
5736 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5737 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5738 * so it can be used as a marker that qc_type
5739 * isn't reinitialized from command line */
5741 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5742 long_opts, NULL)) != -1) {
5745 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5748 goto quota_type_def;
5751 rc = name2uid(&qctl.qc_id, optarg);
5754 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5757 goto quota_type_def;
5760 rc = name2gid(&qctl.qc_id, optarg);
5763 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5766 goto quota_type_def;
5769 rc = name2projid(&qctl.qc_id, optarg);
5772 qctl.qc_id = strtoul(optarg, &endptr, 10);
5773 if (*endptr != '\0') {
5774 fprintf(stderr, "%s setquota: invalid"
5775 " id '%s'\n", progname, optarg);
5780 if (qctl.qc_id == 0) {
5781 fprintf(stderr, "%s setquota: can't set quota"
5782 " for root usr/group/project.\n",
5788 if (qctl.qc_type != ALLQUOTA) {
5790 "%s setquota: only one of -u, -U, -g,"
5791 " -G, -p or -P may be specified\n",
5795 qctl.qc_type = qtype;
5798 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5802 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5803 dqb->dqb_bsoftlimit >>= 10;
5804 limit_mask |= BSLIMIT;
5805 if (dqb->dqb_bsoftlimit &&
5806 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5808 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5809 "See '%s help setquota' or Lustre manual for details\n",
5810 progname, dqb->dqb_bsoftlimit,
5814 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5815 dqb->dqb_bhardlimit >>= 10;
5816 limit_mask |= BHLIMIT;
5817 if (dqb->dqb_bhardlimit &&
5818 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5820 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5821 "See '%s help setquota' or Lustre manual for details\n",
5822 progname, dqb->dqb_bhardlimit,
5826 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5827 limit_mask |= ISLIMIT;
5828 if (dqb->dqb_isoftlimit &&
5829 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5831 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5832 "See '%s help setquota' or Lustre manual for details\n",
5833 progname, dqb->dqb_isoftlimit,
5837 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5838 limit_mask |= IHLIMIT;
5839 if (dqb->dqb_ihardlimit &&
5840 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5842 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5843 "See '%s help setquota' or Lustre manual for details\n",
5844 progname, dqb->dqb_ihardlimit,
5849 "%s setquota: unrecognized option '%s'\n",
5850 progname, argv[optind - 1]);
5855 if (qctl.qc_type == ALLQUOTA) {
5857 "%s setquota: either -u or -g must be specified\n",
5862 if (!use_default && limit_mask == 0) {
5864 "%s setquota: at least one limit must be specified\n",
5869 if (use_default && limit_mask != 0) {
5871 "%s setquota: limits should not be specified when"
5872 " using default quota\n",
5877 if (use_default && qctl.qc_id == 0) {
5879 "%s setquota: can not set default quota for root"
5880 " user/group/project\n",
5885 if (optind != argc - 1) {
5887 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5888 progname, argv[optind]);
5895 dqb->dqb_bhardlimit = 0;
5896 dqb->dqb_bsoftlimit = 0;
5897 dqb->dqb_ihardlimit = 0;
5898 dqb->dqb_isoftlimit = 0;
5901 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5902 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5903 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5904 /* sigh, we can't just set blimits/ilimits */
5905 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5906 .qc_type = qctl.qc_type,
5907 .qc_id = qctl.qc_id};
5909 rc = llapi_quotactl(mnt, &tmp_qctl);
5913 if (!(limit_mask & BHLIMIT))
5914 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5915 if (!(limit_mask & BSLIMIT))
5916 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5917 if (!(limit_mask & IHLIMIT))
5918 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5919 if (!(limit_mask & ISLIMIT))
5920 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5922 /* Keep grace times if we have got no softlimit arguments */
5923 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5924 dqb->dqb_valid |= QIF_BTIME;
5925 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5928 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5929 dqb->dqb_valid |= QIF_ITIME;
5930 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5934 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5935 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5937 rc = llapi_quotactl(mnt, &qctl);
5941 "%s setquota: cannot quotactl '%s' '%s': %s",
5943 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5950 /* Converts seconds value into format string
5951 * result is returned in buf
5953 * 1. result is in descenting order: 1w2d3h4m5s
5954 * 2. zero fields are not filled (except for p. 3): 5d1s
5955 * 3. zero seconds value is presented as "0s"
5957 static char * __sec2str(time_t seconds, char *buf)
5959 const char spec[] = "smhdw";
5960 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5965 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5966 c = seconds / mult[i];
5968 if (c > 0 || (i == 0 && buf == tail))
5969 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5977 static void sec2str(time_t seconds, char *buf, int rc)
5984 tail = __sec2str(seconds, tail);
5986 if (rc && tail - buf < 39) {
5992 static void diff2str(time_t seconds, char *buf, time_t now)
5998 if (seconds <= now) {
5999 strcpy(buf, "none");
6002 __sec2str(seconds - now, buf);
6005 static void print_quota_title(char *name, struct if_quotactl *qctl,
6006 bool human_readable, bool show_default)
6009 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6010 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6011 "Filesystem", "bquota", "blimit", "bgrace",
6012 "iquota", "ilimit", "igrace");
6014 printf("Disk quotas for %s %s (%cid %u):\n",
6015 qtype_name(qctl->qc_type), name,
6016 *qtype_name(qctl->qc_type), qctl->qc_id);
6017 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6018 "Filesystem", human_readable ? "used" : "kbytes",
6019 "quota", "limit", "grace",
6020 "files", "quota", "limit", "grace");
6024 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6027 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6030 snprintf(buf, buflen, "%5.4gP",
6031 (double)num / ((__u64)1 << 40));
6033 snprintf(buf, buflen, "%5.4gT",
6034 (double)num / (1 << 30));
6036 snprintf(buf, buflen, "%5.4gG",
6037 (double)num / (1 << 20));
6039 snprintf(buf, buflen, "%5.4gM",
6040 (double)num / (1 << 10));
6042 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6046 #define STRBUF_LEN 32
6047 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6048 int rc, bool h, bool show_default)
6054 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6055 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6056 int bover = 0, iover = 0;
6057 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6058 char numbuf[3][STRBUF_LEN];
6060 char strbuf[STRBUF_LEN];
6062 if (dqb->dqb_bhardlimit &&
6063 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6065 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6066 if (dqb->dqb_btime > now) {
6073 if (dqb->dqb_ihardlimit &&
6074 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6076 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6077 if (dqb->dqb_itime > now) {
6085 if (strlen(mnt) > 15)
6086 printf("%s\n%15s", mnt, "");
6088 printf("%15s", mnt);
6091 diff2str(dqb->dqb_btime, timebuf, now);
6092 else if (show_default)
6093 snprintf(timebuf, sizeof(timebuf), "%llu",
6096 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6097 strbuf, sizeof(strbuf), h);
6098 if (rc == -EREMOTEIO)
6099 sprintf(numbuf[0], "%s*", strbuf);
6101 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6102 "%s" : "[%s]", strbuf);
6104 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6105 if (type == QC_GENERAL)
6106 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6107 "%s" : "[%s]", strbuf);
6109 sprintf(numbuf[1], "%s", "-");
6111 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6112 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6113 "%s" : "[%s]", strbuf);
6116 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6118 printf(" %7s%c %6s %7s %7s",
6119 numbuf[0], bover ? '*' : ' ', numbuf[1],
6120 numbuf[2], bover > 1 ? timebuf : "-");
6124 diff2str(dqb->dqb_itime, timebuf, now);
6125 else if (show_default)
6126 snprintf(timebuf, sizeof(timebuf), "%llu",
6129 snprintf(numbuf[0], sizeof(numbuf),
6130 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6131 (uintmax_t)dqb->dqb_curinodes);
6133 if (type == QC_GENERAL)
6134 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6136 (uintmax_t)dqb->dqb_isoftlimit);
6138 sprintf(numbuf[1], "%s", "-");
6140 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6141 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6144 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6145 else if (type != QC_OSTIDX)
6146 printf(" %7s%c %6s %7s %7s",
6147 numbuf[0], iover ? '*' : ' ', numbuf[1],
6148 numbuf[2], iover > 1 ? timebuf : "-");
6150 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6153 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6154 qctl->qc_cmd == Q_GETOINFO) {
6158 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6159 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6160 printf("Block grace time: %s; Inode grace time: %s\n",
6161 bgtimebuf, igtimebuf);
6165 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6166 bool h, __u64 *total)
6168 int rc = 0, rc1 = 0, count = 0;
6169 __u32 valid = qctl->qc_valid;
6171 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6173 fprintf(stderr, "can not get %s count: %s\n",
6174 is_mdt ? "mdt": "ost", strerror(-rc));
6178 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6179 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6180 rc = llapi_quotactl(mnt, qctl);
6182 /* It is remote client case. */
6183 if (rc == -EOPNOTSUPP) {
6190 fprintf(stderr, "quotactl %s%d failed.\n",
6191 is_mdt ? "mdt": "ost", qctl->qc_idx);
6195 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6196 qctl->qc_valid, 0, h, false);
6197 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6198 qctl->qc_dqblk.dqb_bhardlimit;
6201 qctl->qc_valid = valid;
6205 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6206 int verbose, int quiet, bool human_readable,
6209 int rc1 = 0, rc2 = 0, rc3 = 0;
6210 char *obd_type = (char *)qctl->obd_type;
6211 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6212 __u64 total_ialloc = 0, total_balloc = 0;
6213 bool use_default_for_blk = false;
6214 bool use_default_for_file = false;
6217 rc1 = llapi_quotactl(mnt, qctl);
6221 fprintf(stderr, "%s quotas are not enabled.\n",
6222 qtype_name(qctl->qc_type));
6225 fprintf(stderr, "Permission denied.\n");
6228 /* We already got error message. */
6231 fprintf(stderr, "Unexpected quotactl error: %s\n",
6236 if (!show_default && qctl->qc_id == 0) {
6237 qctl->qc_dqblk.dqb_bhardlimit = 0;
6238 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6239 qctl->qc_dqblk.dqb_ihardlimit = 0;
6240 qctl->qc_dqblk.dqb_isoftlimit = 0;
6241 qctl->qc_dqblk.dqb_btime = 0;
6242 qctl->qc_dqblk.dqb_itime = 0;
6243 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6246 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6247 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6248 use_default_for_blk = true;
6249 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6252 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6253 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6254 use_default_for_file = true;
6255 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6258 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6259 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6260 print_quota_title(name, qctl, human_readable, show_default);
6262 if (rc1 && *obd_type)
6263 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6265 if (qctl->qc_valid != QC_GENERAL)
6268 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6269 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6270 (QIF_LIMITS|QIF_USAGE));
6272 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6274 if (!show_default && verbose &&
6275 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6276 char strbuf[STRBUF_LEN];
6278 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6280 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6282 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6284 printf("Total allocated inode limit: %ju, total "
6285 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6289 if (use_default_for_blk)
6290 printf("%cid %u is using default block quota setting\n",
6291 *qtype_name(qctl->qc_type), qctl->qc_id);
6293 if (use_default_for_file)
6294 printf("%cid %u is using default file quota setting\n",
6295 *qtype_name(qctl->qc_type), qctl->qc_id);
6297 if (rc1 || rc2 || rc3 || inacc)
6298 printf("Some errors happened when getting quota info. "
6299 "Some devices may be not working or deactivated. "
6300 "The data in \"[]\" is inaccurate.\n");
6306 static int lfs_project(int argc, char **argv)
6308 int ret = 0, err = 0, c, i;
6309 struct project_handle_control phc = { 0 };
6310 enum lfs_project_ops_t op;
6313 phc.assign_projid = false;
6314 /* default action */
6315 op = LFS_PROJECT_LIST;
6317 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6320 if (op != LFS_PROJECT_LIST) {
6322 "%s: cannot specify '-c' '-C' '-s' together\n",
6327 op = LFS_PROJECT_CHECK;
6330 if (op != LFS_PROJECT_LIST) {
6332 "%s: cannot specify '-c' '-C' '-s' together\n",
6337 op = LFS_PROJECT_CLEAR;
6340 if (op != LFS_PROJECT_LIST) {
6342 "%s: cannot specify '-c' '-C' '-s' together\n",
6347 phc.set_inherit = true;
6348 op = LFS_PROJECT_SET;
6354 phc.keep_projid = true;
6357 phc.recursive = true;
6360 phc.projid = strtoul(optarg, NULL, 0);
6361 phc.assign_projid = true;
6365 phc.newline = false;
6368 fprintf(stderr, "%s: invalid option '%c'\n",
6374 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6375 op = LFS_PROJECT_SET;
6376 phc.set_projid = true;
6377 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6378 phc.set_projid = true;
6382 case LFS_PROJECT_CHECK:
6383 if (phc.keep_projid) {
6385 "%s: '-k' is useless together with '-c'\n",
6390 case LFS_PROJECT_CLEAR:
6393 "%s: '-0' is useless together with '-C'\n",
6397 if (phc.assign_projid) {
6399 "%s: '-p' is useless together with '-C'\n",
6404 case LFS_PROJECT_SET:
6407 "%s: '-0' is useless together with '-s'\n",
6411 if (phc.keep_projid) {
6413 "%s: '-k' is useless together with '-s'\n",
6421 "%s: '-0' is useless for list operations\n",
6431 fprintf(stderr, "%s: missing file or directory target(s)\n",
6436 for (i = 0; i < argc; i++) {
6438 case LFS_PROJECT_CHECK:
6439 err = lfs_project_check(argv[i], &phc);
6441 case LFS_PROJECT_LIST:
6442 err = lfs_project_list(argv[i], &phc);
6444 case LFS_PROJECT_CLEAR:
6445 err = lfs_project_clear(argv[i], &phc);
6447 case LFS_PROJECT_SET:
6448 err = lfs_project_set(argv[i], &phc);
6460 static int lfs_quota(int argc, char **argv)
6463 char *mnt, *name = NULL;
6464 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6465 .qc_type = ALLQUOTA };
6466 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6467 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6469 __u32 valid = QC_GENERAL, idx = 0;
6470 bool human_readable = false;
6471 bool show_default = false;
6474 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6477 show_default = true;
6482 show_default = true;
6487 show_default = true;
6491 if (qctl.qc_type != ALLQUOTA) {
6493 "%s quota: only one of -u, -g, or -p may be specified\n",
6497 qctl.qc_type = qtype;
6500 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6503 valid = qctl.qc_valid = QC_UUID;
6504 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6507 valid = qctl.qc_valid = QC_MDTIDX;
6508 idx = qctl.qc_idx = atoi(optarg);
6509 if (idx == 0 && *optarg != '0') {
6511 "%s quota: invalid MDT index '%s'\n",
6517 valid = qctl.qc_valid = QC_OSTIDX;
6518 idx = qctl.qc_idx = atoi(optarg);
6519 if (idx == 0 && *optarg != '0') {
6521 "%s quota: invalid OST index '%s'\n",
6533 human_readable = true;
6536 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6537 progname, argv[optind - 1]);
6542 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6543 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6544 optind == argc - 1 && !show_default) {
6546 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6547 qctl.qc_valid = valid;
6550 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6551 qctl.qc_type = qtype;
6552 if (qtype == USRQUOTA) {
6553 qctl.qc_id = geteuid();
6554 rc = uid2name(&name, qctl.qc_id);
6556 qctl.qc_id = getegid();
6557 rc = gid2name(&name, qctl.qc_id);
6562 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6563 human_readable, show_default);
6568 /* lfs quota -u username /path/to/lustre/mount */
6569 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6570 /* options should be followed by u/g-name and mntpoint */
6571 if ((!show_default && optind + 2 != argc) ||
6572 (show_default && optind + 1 != argc) ||
6573 qctl.qc_type == ALLQUOTA) {
6575 "%s quota: name and mount point must be specified\n",
6580 if (!show_default) {
6581 name = argv[optind++];
6582 switch (qctl.qc_type) {
6584 rc = name2uid(&qctl.qc_id, name);
6587 rc = name2gid(&qctl.qc_id, name);
6590 rc = name2projid(&qctl.qc_id, name);
6597 qctl.qc_valid = QC_GENERAL;
6598 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6603 qctl.qc_id = strtoul(name, &endptr, 10);
6604 if (*endptr != '\0') {
6605 fprintf(stderr, "%s quota: invalid id '%s'\n",
6610 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6611 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6617 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6618 human_readable, show_default);
6621 #endif /* HAVE_SYS_QUOTA_H! */
6623 static int flushctx_ioctl(char *mp)
6627 fd = open(mp, O_RDONLY);
6629 fprintf(stderr, "flushctx: error open %s: %s\n",
6630 mp, strerror(errno));
6634 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6636 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6637 mp, strerror(errno));
6643 static int lfs_flushctx(int argc, char **argv)
6645 int kdestroy = 0, c;
6646 char mntdir[PATH_MAX] = {'\0'};
6650 while ((c = getopt(argc, argv, "k")) != -1) {
6656 fprintf(stderr, "error: %s: option '-%c' "
6657 "unrecognized\n", argv[0], c);
6663 if ((rc = system("kdestroy > /dev/null")) != 0) {
6664 rc = WEXITSTATUS(rc);
6665 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6669 if (optind >= argc) {
6670 /* flush for all mounted lustre fs. */
6671 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6672 /* Check if we have a mount point */
6673 if (mntdir[0] == '\0')
6676 if (flushctx_ioctl(mntdir))
6679 mntdir[0] = '\0'; /* avoid matching in next loop */
6682 /* flush fs as specified */
6683 while (optind < argc) {
6684 if (flushctx_ioctl(argv[optind++]))
6691 static int lfs_changelog(int argc, char **argv)
6693 void *changelog_priv;
6694 struct changelog_rec *rec;
6695 long long startrec = 0, endrec = 0;
6697 struct option long_opts[] = {
6698 { .val = 'f', .name = "follow", .has_arg = no_argument },
6700 char short_opts[] = "f";
6703 while ((rc = getopt_long(argc, argv, short_opts,
6704 long_opts, NULL)) != -1) {
6711 "%s changelog: unrecognized option '%s'\n",
6712 progname, argv[optind - 1]);
6716 if (optind >= argc) {
6717 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6722 mdd = argv[optind++];
6724 startrec = strtoll(argv[optind++], NULL, 10);
6726 endrec = strtoll(argv[optind++], NULL, 10);
6728 rc = llapi_changelog_start(&changelog_priv,
6729 CHANGELOG_FLAG_BLOCK |
6730 CHANGELOG_FLAG_JOBID |
6731 CHANGELOG_FLAG_EXTRA_FLAGS |
6732 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6735 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6736 progname, strerror(errno = -rc));
6740 rc = llapi_changelog_set_xflags(changelog_priv,
6741 CHANGELOG_EXTRA_FLAG_UIDGID |
6742 CHANGELOG_EXTRA_FLAG_NID |
6743 CHANGELOG_EXTRA_FLAG_OMODE |
6744 CHANGELOG_EXTRA_FLAG_XATTR);
6747 "%s changelog: cannot set xflags for changelog: %s\n",
6748 progname, strerror(errno = -rc));
6752 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6756 if (endrec && rec->cr_index > endrec) {
6757 llapi_changelog_free(&rec);
6760 if (rec->cr_index < startrec) {
6761 llapi_changelog_free(&rec);
6765 secs = rec->cr_time >> 30;
6766 gmtime_r(&secs, &ts);
6767 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6768 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6769 changelog_type2str(rec->cr_type),
6770 ts.tm_hour, ts.tm_min, ts.tm_sec,
6771 (int)(rec->cr_time & ((1 << 30) - 1)),
6772 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6773 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6775 if (rec->cr_flags & CLF_JOBID) {
6776 struct changelog_ext_jobid *jid =
6777 changelog_rec_jobid(rec);
6779 if (jid->cr_jobid[0] != '\0')
6780 printf(" j=%s", jid->cr_jobid);
6783 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6784 struct changelog_ext_extra_flags *ef =
6785 changelog_rec_extra_flags(rec);
6787 printf(" ef=0x%llx", ef->cr_extra_flags);
6789 if (ef->cr_extra_flags & CLFE_UIDGID) {
6790 struct changelog_ext_uidgid *uidgid =
6791 changelog_rec_uidgid(rec);
6793 printf(" u=%llu:%llu",
6794 uidgid->cr_uid, uidgid->cr_gid);
6796 if (ef->cr_extra_flags & CLFE_NID) {
6797 struct changelog_ext_nid *nid =
6798 changelog_rec_nid(rec);
6801 libcfs_nid2str(nid->cr_nid));
6804 if (ef->cr_extra_flags & CLFE_OPEN) {
6805 struct changelog_ext_openmode *omd =
6806 changelog_rec_openmode(rec);
6807 char mode[] = "---";
6809 /* exec mode must be exclusive */
6810 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6813 if (omd->cr_openflags & MDS_FMODE_READ)
6815 if (omd->cr_openflags &
6822 if (strcmp(mode, "---") != 0)
6823 printf(" m=%s", mode);
6827 if (ef->cr_extra_flags & CLFE_XATTR) {
6828 struct changelog_ext_xattr *xattr =
6829 changelog_rec_xattr(rec);
6831 if (xattr->cr_xattr[0] != '\0')
6832 printf(" x=%s", xattr->cr_xattr);
6836 if (rec->cr_namelen)
6837 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6838 rec->cr_namelen, changelog_rec_name(rec));
6840 if (rec->cr_flags & CLF_RENAME) {
6841 struct changelog_ext_rename *rnm =
6842 changelog_rec_rename(rec);
6844 if (!fid_is_zero(&rnm->cr_sfid))
6845 printf(" s="DFID" sp="DFID" %.*s",
6846 PFID(&rnm->cr_sfid),
6847 PFID(&rnm->cr_spfid),
6848 (int)changelog_rec_snamelen(rec),
6849 changelog_rec_sname(rec));
6853 llapi_changelog_free(&rec);
6856 llapi_changelog_fini(&changelog_priv);
6859 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6860 progname, strerror(errno = -rc));
6862 return (rc == 1 ? 0 : rc);
6865 static int lfs_changelog_clear(int argc, char **argv)
6873 endrec = strtoll(argv[3], NULL, 10);
6875 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6878 fprintf(stderr, "%s: record out of range: %llu\n",
6880 else if (rc == -ENOENT)
6881 fprintf(stderr, "%s: no changelog user: %s\n",
6884 fprintf(stderr, "%s error: %s\n", argv[0],
6893 static int lfs_fid2path(int argc, char **argv)
6895 struct option long_opts[] = {
6896 { .val = 'c', .name = "cur", .has_arg = no_argument },
6897 { .val = 'l', .name = "link", .has_arg = required_argument },
6898 { .val = 'r', .name = "rec", .has_arg = required_argument },
6900 char short_opts[] = "cl:r:";
6901 char *device, *fid, *path;
6902 long long recno = -1;
6907 char *endptr = NULL;
6909 while ((rc = getopt_long(argc, argv, short_opts,
6910 long_opts, NULL)) != -1) {
6916 linkno = strtol(optarg, &endptr, 10);
6917 if (*endptr != '\0') {
6919 "%s fid2path: invalid linkno '%s'\n",
6925 recno = strtoll(optarg, &endptr, 10);
6926 if (*endptr != '\0') {
6928 "%s fid2path: invalid recno '%s'\n",
6935 "%s fid2path: unrecognized option '%s'\n",
6936 progname, argv[optind - 1]);
6943 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6948 device = argv[optind++];
6949 path = calloc(1, PATH_MAX);
6953 "%s fid2path: cannot allocate memory for path: %s\n",
6954 progname, strerror(-rc));
6959 while (optind < argc) {
6960 fid = argv[optind++];
6962 lnktmp = (linkno >= 0) ? linkno : 0;
6964 int oldtmp = lnktmp;
6965 long long rectmp = recno;
6967 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6971 "%s fid2path: cannot find '%s': %s\n",
6972 progname, fid, strerror(errno = -rc2));
6979 fprintf(stdout, "%lld ", rectmp);
6980 if (device[0] == '/') {
6981 fprintf(stdout, "%s", device);
6982 if (device[strlen(device) - 1] != '/')
6983 fprintf(stdout, "/");
6984 } else if (path[0] == '\0') {
6985 fprintf(stdout, "/");
6987 fprintf(stdout, "%s\n", path);
6990 /* specified linkno */
6992 if (oldtmp == lnktmp)
7002 static int lfs_path2fid(int argc, char **argv)
7004 struct option long_opts[] = {
7005 { .val = 'p', .name = "parents", .has_arg = no_argument },
7008 const char short_opts[] = "p";
7009 const char *sep = "";
7012 bool show_parents = false;
7014 while ((rc = getopt_long(argc, argv, short_opts,
7015 long_opts, NULL)) != -1) {
7018 show_parents = true;
7022 "%s path2fid: unrecognized option '%s'\n",
7023 progname, argv[optind - 1]);
7028 if (optind > argc - 1) {
7029 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7033 else if (optind < argc - 1)
7037 for (path = argv + optind; *path != NULL; path++) {
7039 if (!show_parents) {
7040 err = llapi_path2fid(*path, &fid);
7042 printf("%s%s"DFID"\n",
7043 *sep != '\0' ? *path : "", sep,
7046 char name[NAME_MAX + 1];
7047 unsigned int linkno = 0;
7049 while ((err = llapi_path2parent(*path, linkno, &fid,
7050 name, sizeof(name))) == 0) {
7051 if (*sep != '\0' && linkno == 0)
7052 printf("%s%s", *path, sep);
7054 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7059 /* err == -ENODATA is end-of-loop */
7060 if (linkno > 0 && err == -ENODATA) {
7068 "%s path2fid: cannot get %sfid for '%s': %s\n",
7069 progname, show_parents ? "parent " : "", *path,
7081 static int lfs_data_version(int argc, char **argv)
7088 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7091 fprintf(stderr, "%s data_version: FILE must be specified\n",
7096 while ((c = getopt(argc, argv, "nrw")) != -1) {
7099 data_version_flags = 0;
7102 data_version_flags |= LL_DV_RD_FLUSH;
7105 data_version_flags |= LL_DV_WR_FLUSH;
7109 "%s data_version: unrecognized option '%s'\n",
7110 progname, argv[optind - 1]);
7114 if (optind == argc) {
7115 fprintf(stderr, "%s data_version: FILE must be specified\n",
7120 path = argv[optind];
7121 fd = open(path, O_RDONLY);
7124 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7125 progname, path, strerror(-rc));
7129 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7132 "%s data_version: cannot get version for '%s': %s\n",
7133 progname, path, strerror(-rc));
7135 printf("%ju" "\n", (uintmax_t)data_version);
7141 static int lfs_hsm_state(int argc, char **argv)
7146 struct hsm_user_state hus;
7154 rc = llapi_hsm_state_get(path, &hus);
7156 fprintf(stderr, "can't get hsm state for %s: %s\n",
7157 path, strerror(errno = -rc));
7161 /* Display path name and status flags */
7162 printf("%s: (0x%08x)", path, hus.hus_states);
7164 if (hus.hus_states & HS_RELEASED)
7165 printf(" released");
7166 if (hus.hus_states & HS_EXISTS)
7168 if (hus.hus_states & HS_DIRTY)
7170 if (hus.hus_states & HS_ARCHIVED)
7171 printf(" archived");
7172 /* Display user-settable flags */
7173 if (hus.hus_states & HS_NORELEASE)
7174 printf(" never_release");
7175 if (hus.hus_states & HS_NOARCHIVE)
7176 printf(" never_archive");
7177 if (hus.hus_states & HS_LOST)
7178 printf(" lost_from_hsm");
7180 if (hus.hus_archive_id != 0)
7181 printf(", archive_id:%d", hus.hus_archive_id);
7184 } while (++i < argc);
7189 #define LFS_HSM_SET 0
7190 #define LFS_HSM_CLEAR 1
7193 * Generic function to set or clear HSM flags.
7194 * Used by hsm_set and hsm_clear.
7196 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7198 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7200 struct option long_opts[] = {
7201 { .val = 'A', .name = "archived", .has_arg = no_argument },
7202 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7203 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7204 { .val = 'e', .name = "exists", .has_arg = no_argument },
7205 { .val = 'l', .name = "lost", .has_arg = no_argument },
7206 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7207 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7209 char short_opts[] = "lraAdei:";
7213 __u32 archive_id = 0;
7219 while ((c = getopt_long(argc, argv, short_opts,
7220 long_opts, NULL)) != -1) {
7226 mask |= HS_NOARCHIVE;
7229 mask |= HS_ARCHIVED;
7232 mask |= HS_NORELEASE;
7241 archive_id = strtol(optarg, &end, 10);
7243 fprintf(stderr, "invalid archive_id: '%s'\n",
7251 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7252 argv[0], argv[optind - 1]);
7257 /* User should have specified a flag */
7261 while (optind < argc) {
7263 path = argv[optind];
7265 /* If mode == 0, this means we apply the mask. */
7266 if (mode == LFS_HSM_SET)
7267 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7269 rc = llapi_hsm_state_set(path, 0, mask, 0);
7272 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7273 path, strerror(errno = -rc));
7282 static int lfs_hsm_action(int argc, char **argv)
7287 struct hsm_current_action hca;
7288 struct hsm_extent he;
7289 enum hsm_user_action hua;
7290 enum hsm_progress_states hps;
7298 rc = llapi_hsm_current_action(path, &hca);
7300 fprintf(stderr, "can't get hsm action for %s: %s\n",
7301 path, strerror(errno = -rc));
7304 he = hca.hca_location;
7305 hua = hca.hca_action;
7306 hps = hca.hca_state;
7308 printf("%s: %s", path, hsm_user_action2name(hua));
7310 /* Skip file without action */
7311 if (hca.hca_action == HUA_NONE) {
7316 printf(" %s ", hsm_progress_state2name(hps));
7318 if ((hps == HPS_RUNNING) &&
7319 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7320 printf("(%llu bytes moved)\n",
7321 (unsigned long long)he.length);
7322 else if ((he.offset + he.length) == LUSTRE_EOF)
7323 printf("(from %llu to EOF)\n",
7324 (unsigned long long)he.offset);
7326 printf("(from %llu to %llu)\n",
7327 (unsigned long long)he.offset,
7328 (unsigned long long)(he.offset + he.length));
7330 } while (++i < argc);
7335 static int lfs_hsm_set(int argc, char **argv)
7337 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7340 static int lfs_hsm_clear(int argc, char **argv)
7342 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7346 * Check file state and return its fid, to be used by lfs_hsm_request().
7348 * \param[in] file Path to file to check
7349 * \param[in,out] fid Pointer to allocated lu_fid struct.
7350 * \param[in,out] last_dev Pointer to last device id used.
7352 * \return 0 on success.
7354 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7360 rc = lstat(file, &st);
7362 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7365 /* Checking for regular file as archiving as posix copytool
7366 * rejects archiving files other than regular files
7368 if (!S_ISREG(st.st_mode)) {
7369 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7372 /* A request should be ... */
7373 if (*last_dev != st.st_dev && *last_dev != 0) {
7374 fprintf(stderr, "All files should be "
7375 "on the same filesystem: %s\n", file);
7378 *last_dev = st.st_dev;
7380 rc = llapi_path2fid(file, fid);
7382 fprintf(stderr, "Cannot read FID of %s: %s\n",
7383 file, strerror(-rc));
7389 /* Fill an HSM HUR item with a given file name.
7391 * If mntpath is set, then the filename is actually a FID, and no
7392 * lookup on the filesystem will be performed.
7394 * \param[in] hur the user request to fill
7395 * \param[in] idx index of the item inside the HUR to fill
7396 * \param[in] mntpath mountpoint of Lustre
7397 * \param[in] fname filename (if mtnpath is NULL)
7398 * or FID (if mntpath is set)
7399 * \param[in] last_dev pointer to last device id used
7401 * \retval 0 on success
7402 * \retval CMD_HELP or a negative errno on error
7404 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7405 const char *mntpath, const char *fname,
7408 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7411 hui->hui_extent.length = -1;
7413 if (mntpath != NULL) {
7416 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7420 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7425 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7429 hur->hur_request.hr_itemcount++;
7434 static int lfs_hsm_request(int argc, char **argv, int action)
7436 struct option long_opts[] = {
7437 { .val = 'a', .name = "archive", .has_arg = required_argument },
7438 { .val = 'D', .name = "data", .has_arg = required_argument },
7439 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7440 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7443 char short_opts[] = "l:D:a:m:";
7444 struct hsm_user_request *hur, *oldhur;
7449 char *filelist = NULL;
7450 char fullpath[PATH_MAX];
7451 char *opaque = NULL;
7455 int nbfile_alloc = 0;
7456 char *some_file = NULL;
7457 char *mntpath = NULL;
7463 while ((c = getopt_long(argc, argv, short_opts,
7464 long_opts, NULL)) != -1) {
7473 if (action != HUA_ARCHIVE &&
7474 action != HUA_REMOVE) {
7476 "error: -a is supported only "
7477 "when archiving or removing\n");
7480 archive_id = atoi(optarg);
7483 if (some_file == NULL) {
7485 some_file = strdup(optarg);
7491 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7492 argv[0], argv[optind - 1]);
7497 /* All remaining args are files, so we have at least nbfile */
7498 nbfile = argc - optind;
7500 if ((nbfile == 0) && (filelist == NULL))
7504 opaque_len = strlen(opaque);
7506 /* Alloc the request structure with enough place to store all files
7507 * from command line. */
7508 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7510 fprintf(stderr, "Cannot create the request: %s\n",
7514 nbfile_alloc = nbfile;
7516 hur->hur_request.hr_action = action;
7517 hur->hur_request.hr_archive_id = archive_id;
7518 hur->hur_request.hr_flags = 0;
7520 /* All remaining args are files, add them */
7521 if (nbfile != 0 && some_file == NULL)
7522 some_file = strdup(argv[optind]);
7524 for (i = 0; i < nbfile; i++) {
7525 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7531 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7533 /* If a filelist was specified, read the filelist from it. */
7534 if (filelist != NULL) {
7535 fp = fopen(filelist, "r");
7537 fprintf(stderr, "Cannot read the file list %s: %s\n",
7538 filelist, strerror(errno));
7543 while ((rc = getline(&line, &len, fp)) != -1) {
7544 /* If allocated buffer was too small, get something
7546 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7549 nbfile_alloc = nbfile_alloc * 2 + 1;
7551 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7554 fprintf(stderr, "hsm: cannot allocate "
7555 "the request: %s\n",
7562 size = hur_len(oldhur);
7564 fprintf(stderr, "hsm: cannot allocate "
7565 "%u files + %u bytes data\n",
7566 oldhur->hur_request.hr_itemcount,
7567 oldhur->hur_request.hr_data_len);
7574 memcpy(hur, oldhur, size);
7579 if (line[strlen(line) - 1] == '\n')
7580 line[strlen(line) - 1] = '\0';
7582 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7583 mntpath, line, &last_dev);
7589 if (some_file == NULL) {
7599 /* If a --data was used, add it to the request */
7600 hur->hur_request.hr_data_len = opaque_len;
7602 memcpy(hur_data(hur), opaque, opaque_len);
7604 /* Send the HSM request */
7605 if (realpath(some_file, fullpath) == NULL) {
7606 fprintf(stderr, "Could not find path '%s': %s\n",
7607 some_file, strerror(errno));
7609 rc = llapi_hsm_request(fullpath, hur);
7611 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7612 some_file, strerror(-rc));
7622 static int lfs_hsm_archive(int argc, char **argv)
7624 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7627 static int lfs_hsm_restore(int argc, char **argv)
7629 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7632 static int lfs_hsm_release(int argc, char **argv)
7634 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7637 static int lfs_hsm_remove(int argc, char **argv)
7639 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7642 static int lfs_hsm_cancel(int argc, char **argv)
7644 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7647 static int lfs_swap_layouts(int argc, char **argv)
7652 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7653 SWAP_LAYOUTS_KEEP_MTIME |
7654 SWAP_LAYOUTS_KEEP_ATIME);
7657 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7659 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7661 int lfs_get_mode(const char *string)
7663 enum lock_mode_user mode;
7665 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7666 if (lock_mode_names[mode] == NULL)
7668 if (strcmp(string, lock_mode_names[mode]) == 0)
7675 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7677 enum lu_ladvise_type advice;
7680 advice < ARRAY_SIZE(ladvise_names); advice++) {
7681 if (ladvise_names[advice] == NULL)
7683 if (strcmp(string, ladvise_names[advice]) == 0)
7687 return LU_LADVISE_INVALID;
7690 static int lfs_ladvise(int argc, char **argv)
7692 struct option long_opts[] = {
7693 { .val = 'a', .name = "advice", .has_arg = required_argument },
7694 { .val = 'b', .name = "background", .has_arg = no_argument },
7695 { .val = 'e', .name = "end", .has_arg = required_argument },
7696 { .val = 'l', .name = "length", .has_arg = required_argument },
7697 { .val = 'm', .name = "mode", .has_arg = required_argument },
7698 { .val = 's', .name = "start", .has_arg = required_argument },
7699 { .val = 'u', .name = "unset", .has_arg = no_argument },
7701 char short_opts[] = "a:be:l:m:s:u";
7706 struct llapi_lu_ladvise advice;
7707 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7708 unsigned long long start = 0;
7709 unsigned long long end = LUSTRE_EOF;
7710 unsigned long long length = 0;
7711 unsigned long long size_units;
7712 unsigned long long flags = 0;
7716 while ((c = getopt_long(argc, argv, short_opts,
7717 long_opts, NULL)) != -1) {
7720 advice_type = lfs_get_ladvice(optarg);
7721 if (advice_type == LU_LADVISE_INVALID) {
7722 fprintf(stderr, "%s: invalid advice type "
7723 "'%s'\n", argv[0], optarg);
7724 fprintf(stderr, "Valid types:");
7726 for (advice_type = 0;
7727 advice_type < ARRAY_SIZE(ladvise_names);
7729 if (ladvise_names[advice_type] == NULL)
7731 fprintf(stderr, " %s",
7732 ladvise_names[advice_type]);
7734 fprintf(stderr, "\n");
7747 rc = llapi_parse_size(optarg, &end,
7750 fprintf(stderr, "%s: bad end offset '%s'\n",
7757 rc = llapi_parse_size(optarg, &start,
7760 fprintf(stderr, "%s: bad start offset "
7761 "'%s'\n", argv[0], optarg);
7767 rc = llapi_parse_size(optarg, &length,
7770 fprintf(stderr, "%s: bad length '%s'\n",
7776 mode = lfs_get_mode(optarg);
7778 fprintf(stderr, "%s: bad mode '%s', valid "
7779 "modes are READ or WRITE\n",
7787 fprintf(stderr, "%s: option '%s' unrecognized\n",
7788 argv[0], argv[optind - 1]);
7793 if (advice_type == LU_LADVISE_INVALID) {
7794 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7795 fprintf(stderr, "Valid types:");
7796 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7798 if (ladvise_names[advice_type] == NULL)
7800 fprintf(stderr, " %s", ladvise_names[advice_type]);
7802 fprintf(stderr, "\n");
7806 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7807 fprintf(stderr, "%s: Lock no expand advice is a per file "
7808 "descriptor advice, so when called from lfs, "
7809 "it does nothing.\n", argv[0]);
7813 if (argc <= optind) {
7814 fprintf(stderr, "%s: please give one or more file names\n",
7819 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7820 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7825 if (end == LUSTRE_EOF && length != 0)
7826 end = start + length;
7829 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7830 argv[0], start, end);
7834 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7835 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7840 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7841 fprintf(stderr, "%s: mode is required with lockahead\n",
7846 while (optind < argc) {
7849 path = argv[optind++];
7851 fd = open(path, O_RDONLY);
7853 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7854 argv[0], path, strerror(errno));
7859 advice.lla_start = start;
7860 advice.lla_end = end;
7861 advice.lla_advice = advice_type;
7862 advice.lla_value1 = 0;
7863 advice.lla_value2 = 0;
7864 advice.lla_value3 = 0;
7865 advice.lla_value4 = 0;
7866 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7867 advice.lla_lockahead_mode = mode;
7868 advice.lla_peradvice_flags = flags;
7871 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7874 fprintf(stderr, "%s: cannot give advice '%s' to file "
7875 "'%s': %s\n", argv[0],
7876 ladvise_names[advice_type],
7877 path, strerror(errno));
7883 if (rc == 0 && rc2 < 0)
7890 static const char *const heat_names[] = LU_HEAT_NAMES;
7892 static int lfs_heat_get(int argc, char **argv)
7894 struct lu_heat *heat;
7903 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
7905 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
7910 while (optind < argc) {
7911 path = argv[optind++];
7913 fd = open(path, O_RDONLY);
7915 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7916 argv[0], path, strerror(errno));
7921 heat->lh_count = OBD_HEAT_COUNT;
7922 rc2 = llapi_heat_get(fd, heat);
7925 fprintf(stderr, "%s: cannot get heat of file '%s'"
7926 ": %s\n", argv[0], path, strerror(errno));
7930 printf("flags: %x\n", heat->lh_flags);
7931 for (i = 0; i < heat->lh_count; i++)
7932 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
7934 if (rc == 0 && rc2 < 0)
7942 static int lfs_heat_set(int argc, char **argv)
7944 struct option long_opts[] = {
7945 {"clear", no_argument, 0, 'c'},
7946 {"off", no_argument, 0, 'o'},
7947 {"on", no_argument, 0, 'O'},
7950 char short_opts[] = "coO";
7961 while ((c = getopt_long(argc, argv, short_opts,
7962 long_opts, NULL)) != -1) {
7965 flags |= LU_HEAT_FLAG_CLEAR;
7968 flags |= LU_HEAT_FLAG_CLEAR;
7969 flags |= LU_HEAT_FLAG_OFF;
7972 flags &= ~LU_HEAT_FLAG_OFF;
7977 fprintf(stderr, "%s: option '%s' unrecognized\n",
7978 argv[0], argv[optind - 1]);
7983 if (argc <= optind) {
7984 fprintf(stderr, "%s: please give one or more file names\n",
7989 while (optind < argc) {
7990 path = argv[optind++];
7992 fd = open(path, O_RDONLY);
7994 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7995 argv[0], path, strerror(errno));
8000 rc2 = llapi_heat_set(fd, flags);
8003 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8004 ": %s\n", argv[0], path, strerror(errno));
8008 if (rc == 0 && rc2 < 0)
8014 /** The input string contains a comma delimited list of component ids and
8015 * ranges, for example "1,2-4,7".
8017 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8019 bool end_of_loop = false;
8027 while (!end_of_loop) {
8031 char *endptr = NULL;
8034 ptr = strchrnul(arg, ',');
8035 end_of_loop = *ptr == '\0';
8038 start_index = strtol(arg, &endptr, 0);
8039 if (endptr == arg) /* no data at all */
8041 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8043 if (start_index < 0)
8046 end_index = start_index;
8047 if (*endptr == '-') {
8048 end_index = strtol(endptr + 1, &endptr, 0);
8049 if (*endptr != '\0')
8051 if (end_index < start_index)
8055 for (i = start_index; i <= end_index && size > 0; i++) {
8058 /* remove duplicate */
8059 for (j = 0; j < nr; j++) {
8063 if (j == nr) { /* no duplicate */
8069 if (size == 0 && i < end_index)
8076 if (!end_of_loop && ptr != NULL)
8079 return rc < 0 ? rc : nr;
8083 * struct verify_mirror_id - Mirror id to be verified.
8084 * @mirror_id: A specified mirror id.
8085 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8087 struct verify_mirror_id {
8093 * compare_mirror_ids() - Compare mirror ids.
8094 * @layout: Mirror component list.
8095 * @cbdata: Callback data in verify_mirror_id structure.
8097 * This is a callback function called by llapi_layout_comp_iterate()
8098 * to compare the specified mirror id with the one in the current
8099 * component of @layout. If they are the same, then the specified
8100 * mirror id is valid.
8102 * Return: a negative error code on failure or
8103 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8104 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8107 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8109 struct verify_mirror_id *mirror_id_cbdata =
8110 (struct verify_mirror_id *)cbdata;
8114 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8118 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8119 progname, strerror(errno));
8123 if (mirror_id_cbdata->mirror_id == mirror_id) {
8124 mirror_id_cbdata->is_valid_id = true;
8125 return LLAPI_LAYOUT_ITER_STOP;
8128 return LLAPI_LAYOUT_ITER_CONT;
8132 * verify_mirror_ids() - Verify specified mirror ids.
8133 * @fname: Mirrored file name.
8134 * @mirror_ids: Specified mirror ids to be verified.
8135 * @ids_nr: Number of specified mirror ids.
8137 * This function verifies that specified @mirror_ids are valid
8138 * in the mirrored file @fname.
8140 * Return: 0 on success or a negative error code on failure.
8143 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8145 struct llapi_layout *layout = NULL;
8146 struct verify_mirror_id mirror_id_cbdata = { 0 };
8157 if (stat(fname, &stbuf) < 0) {
8158 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8159 progname, fname, strerror(errno));
8164 if (!S_ISREG(stbuf.st_mode)) {
8165 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8171 fd = open(fname, O_DIRECT | O_RDONLY);
8173 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8174 progname, fname, strerror(errno));
8179 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8181 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8182 progname, fname, strerror(errno));
8186 layout = llapi_layout_get_by_fd(fd, 0);
8187 if (layout == NULL) {
8188 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8189 progname, fname, strerror(errno));
8191 llapi_lease_release(fd);
8195 rc = llapi_layout_flags_get(layout, &flr_state);
8197 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8198 progname, fname, strerror(errno));
8203 flr_state &= LCM_FL_FLR_MASK;
8204 switch (flr_state) {
8207 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8208 progname, fname, llapi_layout_flags_string(flr_state));
8215 for (i = 0; i < ids_nr; i++) {
8216 mirror_id_cbdata.mirror_id = mirror_ids[i];
8217 mirror_id_cbdata.is_valid_id = false;
8219 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8224 "%s: '%s' failed to verify mirror id: %u.\n",
8225 progname, fname, mirror_ids[i]);
8229 if (!mirror_id_cbdata.is_valid_id) {
8232 "%s: '%s' invalid specified mirror id: %u.\n",
8233 progname, fname, mirror_ids[i]);
8239 llapi_layout_free(layout);
8240 llapi_lease_release(fd);
8248 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8249 __u16 *mirror_ids, int ids_nr)
8251 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8252 struct llapi_layout *layout;
8262 if (stat(fname, &stbuf) < 0) {
8263 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8264 progname, fname, strerror(errno));
8268 if (!S_ISREG(stbuf.st_mode)) {
8269 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8275 fd = open(fname, O_DIRECT | O_RDWR);
8277 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8278 progname, fname, strerror(errno));
8283 layout = llapi_layout_get_by_fd(fd, 0);
8284 if (layout == NULL) {
8285 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8286 progname, fname, strerror(errno));
8291 rc = llapi_layout_flags_get(layout, &flr_state);
8293 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8294 progname, fname, strerror(errno));
8299 flr_state &= LCM_FL_FLR_MASK;
8300 if (flr_state == LCM_FL_NONE) {
8302 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8307 /* get stale component info */
8308 comp_size = llapi_mirror_find_stale(layout, comp_array,
8309 ARRAY_SIZE(comp_array),
8310 mirror_ids, ids_nr);
8311 if (comp_size <= 0) {
8316 ioc->lil_mode = LL_LEASE_WRLCK;
8317 ioc->lil_flags = LL_LEASE_RESYNC;
8318 rc = llapi_lease_set(fd, ioc);
8320 if (rc == -EALREADY)
8324 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8325 progname, fname, strerror(errno));
8329 /* get the read range [start, end) */
8330 start = comp_array[0].lrc_start;
8331 end = comp_array[0].lrc_end;
8332 for (idx = 1; idx < comp_size; idx++) {
8333 if (comp_array[idx].lrc_start < start)
8334 start = comp_array[idx].lrc_start;
8335 if (end < comp_array[idx].lrc_end)
8336 end = comp_array[idx].lrc_end;
8339 rc = llapi_lease_check(fd);
8340 if (rc != LL_LEASE_WRLCK) {
8341 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8346 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8349 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8350 progname, fname, rc);
8352 /* prepare ioc for lease put */
8353 ioc->lil_mode = LL_LEASE_UNLCK;
8354 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8356 for (idx = 0; idx < comp_size; idx++) {
8357 if (comp_array[idx].lrc_synced) {
8358 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8363 rc = llapi_lease_set(fd, ioc);
8365 if (rc == 0) /* lost lease lock */
8367 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8368 progname, fname, strerror(errno));
8372 * llapi_lease_set returns lease mode when it request to unlock
8378 llapi_layout_free(layout);
8385 static inline int lfs_mirror_resync(int argc, char **argv)
8387 struct ll_ioc_lease *ioc = NULL;
8388 __u16 mirror_ids[128] = { 0 };
8393 struct option long_opts[] = {
8394 { .val = 'o', .name = "only", .has_arg = required_argument },
8397 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8400 rc = parse_mirror_ids(mirror_ids,
8401 sizeof(mirror_ids) / sizeof(__u16),
8405 "%s: bad mirror ids '%s'.\n",
8412 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8413 argv[0], argv[optind - 1]);
8419 if (argc == optind) {
8420 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8425 if (ids_nr > 0 && argc > optind + 1) {
8427 "%s: option '--only' cannot be used upon multiple files.\n",
8435 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8440 /* set the lease on the file */
8441 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8443 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8444 argv[0], strerror(errno));
8449 for (; optind < argc; optind++) {
8450 rc = lfs_mirror_resync_file(argv[optind], ioc,
8451 mirror_ids, ids_nr);
8452 /* ignore previous file's error, continue with next file */
8455 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8463 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8465 struct llapi_layout *layout;
8468 layout = llapi_layout_get_by_fd(fd, 0);
8469 if (layout == NULL) {
8470 fprintf(stderr, "could not get layout.\n");
8474 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8476 fprintf(stderr, "failed to iterate layout\n");
8477 llapi_layout_free(layout);
8480 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8481 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8482 llapi_layout_free(layout);
8486 llapi_layout_free(layout);
8492 * Check whether two files are the same file
8493 * \retval 0 same file
8494 * \retval 1 not the same file
8495 * \retval <0 error code
8497 static inline int check_same_file(const char *f1, const char *f2)
8502 if (stat(f1, &stbuf1) < 0) {
8503 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8504 progname, f1, strerror(errno));
8508 if (stat(f2, &stbuf2) < 0) {
8509 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8510 progname, f2, strerror(errno));
8514 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8515 stbuf1.st_ino == stbuf2.st_ino)
8521 static inline int lfs_mirror_read(int argc, char **argv)
8524 __u16 mirror_id = 0;
8525 const char *outfile = NULL;
8531 const size_t buflen = 4 << 20;
8533 struct option long_opts[] = {
8534 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8535 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8538 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8543 mirror_id = strtoul(optarg, &end, 0);
8544 if (*end != '\0' || mirror_id == 0) {
8546 "%s %s: invalid mirror ID '%s'\n",
8547 progname, argv[0], optarg);
8555 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8556 progname, argv[optind - 1]);
8561 if (argc == optind) {
8562 fprintf(stderr, "%s %s: no mirrored file provided\n",
8565 } else if (argc > optind + 1) {
8566 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8570 if (mirror_id == 0) {
8571 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8576 /* open mirror file */
8577 fname = argv[optind];
8580 rc = check_same_file(fname, outfile);
8583 "%s %s: output file cannot be the mirrored file\n",
8591 fd = open(fname, O_DIRECT | O_RDONLY);
8593 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8594 progname, argv[0], fname, strerror(errno));
8598 /* verify mirror id */
8599 rc = verify_mirror_id_by_fd(fd, mirror_id);
8602 "%s %s: cannot find mirror with ID %u in '%s'\n",
8603 progname, argv[0], mirror_id, fname);
8607 /* open output file */
8609 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8611 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8612 progname, argv[0], outfile, strerror(errno));
8617 outfd = STDOUT_FILENO;
8620 /* allocate buffer */
8621 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8623 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8624 progname, argv[0], rc);
8631 ssize_t written = 0;
8633 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8634 if (bytes_read < 0) {
8637 "%s %s: fail to read data from mirror %u: %s\n",
8638 progname, argv[0], mirror_id, strerror(-rc));
8643 if (bytes_read == 0)
8646 while (written < bytes_read) {
8649 written2 = write(outfd, buf + written,
8650 bytes_read - written);
8653 "%s %s: fail to write %s: %s\n",
8654 progname, argv[0], outfile ? : "STDOUT",
8659 written += written2;
8662 if (written != bytes_read) {
8664 "%s %s: written %ld bytes does not match with %ld read.\n",
8665 progname, argv[0], written, bytes_read);
8687 static inline int lfs_mirror_write(int argc, char **argv)
8690 __u16 mirror_id = 0;
8691 const char *inputfile = NULL;
8697 const size_t buflen = 4 << 20;
8699 size_t page_size = sysconf(_SC_PAGESIZE);
8700 struct ll_ioc_lease_id ioc;
8702 struct option long_opts[] = {
8703 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8704 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8707 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8712 mirror_id = strtoul(optarg, &end, 0);
8713 if (*end != '\0' || mirror_id == 0) {
8715 "%s %s: invalid mirror ID '%s'\n",
8716 progname, argv[0], optarg);
8724 fprintf(stderr, "%s: option '%s' unrecognized\n",
8725 progname, argv[optind - 1]);
8730 if (argc == optind) {
8731 fprintf(stderr, "%s %s: no mirrored file provided\n",
8734 } else if (argc > optind + 1) {
8735 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8739 if (mirror_id == 0) {
8740 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8745 /* open mirror file */
8746 fname = argv[optind];
8749 rc = check_same_file(fname, inputfile);
8752 "%s %s: input file cannot be the mirrored file\n",
8760 fd = open(fname, O_DIRECT | O_WRONLY);
8762 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8763 progname, argv[0], fname, strerror(errno));
8767 /* verify mirror id */
8768 rc = verify_mirror_id_by_fd(fd, mirror_id);
8771 "%s %s: cannot find mirror with ID %u in '%s'\n",
8772 progname, argv[0], mirror_id, fname);
8776 /* open input file */
8778 inputfd = open(inputfile, O_RDONLY, 0644);
8780 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
8781 progname, argv[0], inputfile, strerror(errno));
8786 inputfd = STDIN_FILENO;
8789 /* allocate buffer */
8790 rc = posix_memalign(&buf, page_size, buflen);
8792 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8793 progname, argv[0], rc);
8797 /* prepare target mirror components instantiation */
8798 ioc.lil_mode = LL_LEASE_WRLCK;
8799 ioc.lil_flags = LL_LEASE_RESYNC;
8800 ioc.lil_mirror_id = mirror_id;
8801 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8804 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8805 progname, argv[0], fname, strerror(errno));
8815 rc = llapi_lease_check(fd);
8816 if (rc != LL_LEASE_WRLCK) {
8817 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
8818 progname, argv[0], fname);
8822 bytes_read = read(inputfd, buf, buflen);
8823 if (bytes_read < 0) {
8826 "%s %s: fail to read data from '%s': %s\n",
8827 progname, argv[0], inputfile ? : "STDIN",
8834 if (bytes_read == 0)
8837 /* round up to page align to make direct IO happy. */
8838 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
8840 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
8845 "%s %s: fail to write to mirror %u: %s\n",
8846 progname, argv[0], mirror_id,
8854 if (pos & (page_size - 1)) {
8855 rc = llapi_mirror_truncate(fd, mirror_id, pos);
8860 ioc.lil_mode = LL_LEASE_UNLCK;
8861 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
8863 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8868 "%s %s: release lease lock of '%s' failed: %s\n",
8869 progname, argv[0], fname, strerror(errno));
8886 struct collect_ids_data {
8892 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
8894 struct collect_ids_data *cid = cbdata;
8898 rc = llapi_layout_mirror_id_get(layout, &id);
8902 if ((__u16)id != cid->cid_exclude) {
8905 for (i = 0; i < cid->cid_count; i++) {
8906 /* already collected the mirror id */
8907 if (id == cid->cid_ids[i])
8908 return LLAPI_LAYOUT_ITER_CONT;
8910 cid->cid_ids[cid->cid_count] = id;
8914 return LLAPI_LAYOUT_ITER_CONT;
8917 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
8919 struct llapi_layout *layout;
8920 struct collect_ids_data cid = { .cid_ids = ids,
8922 .cid_exclude = exclude_id, };
8925 layout = llapi_layout_get_by_fd(fd, 0);
8926 if (layout == NULL) {
8927 fprintf(stderr, "could not get layout\n");
8931 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
8933 fprintf(stderr, "failed to iterate layout\n");
8934 llapi_layout_free(layout);
8938 llapi_layout_free(layout);
8940 return cid.cid_count;
8943 static inline int lfs_mirror_copy(int argc, char **argv)
8946 __u16 read_mirror_id = 0;
8947 __u16 ids[128] = { 0 };
8949 struct llapi_layout *layout = NULL;
8950 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8957 struct ll_ioc_lease *ioc = NULL;
8958 struct ll_ioc_lease_id *resync_ioc;
8960 struct option long_opts[] = {
8961 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
8962 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
8965 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
8970 read_mirror_id = strtoul(optarg, &end, 0);
8971 if (*end != '\0' || read_mirror_id == 0) {
8973 "%s %s: invalid read mirror ID '%s'\n",
8974 progname, argv[0], optarg);
8979 if (!strcmp(optarg, "-1")) {
8980 /* specify all other mirrors */
8984 count = parse_mirror_ids((__u16 *)ids,
8992 fprintf(stderr, "%s: option '%s' unrecognized\n",
8993 progname, argv[optind - 1]);
8998 if (argc == optind) {
8999 fprintf(stderr, "%s %s: no mirrored file provided\n",
9002 } else if (argc > optind + 1) {
9003 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9007 if (read_mirror_id == 0) {
9009 "%s %s: no valid read mirror ID %d is provided\n",
9010 progname, argv[0], read_mirror_id);
9016 "%s %s: no write mirror ID is provided\n",
9021 for (i = 0; i < count; i++) {
9022 if (read_mirror_id == ids[i]) {
9024 "%s %s: read and write mirror ID cannot be the same\n",
9030 /* open mirror file */
9031 fname = argv[optind];
9033 fd = open(fname, O_DIRECT | O_RDWR);
9035 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9036 progname, argv[0], fname, strerror(errno));
9040 /* write to all other mirrors */
9041 if (ids[0] == (__u16)-1) {
9042 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9046 "%s %s: failed to get other mirror ids in '%s': %d\n",
9047 progname, argv[0], fname, rc);
9052 /* verify mirror id */
9053 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9056 "%s %s: cannot find mirror with ID %u in '%s'\n",
9057 progname, argv[0], read_mirror_id, fname);
9061 for (i = 0; i < count; i++) {
9062 rc = verify_mirror_id_by_fd(fd, ids[i]);
9065 "%s %s: cannot find mirror with ID %u in '%s'\n",
9066 progname, argv[0], ids[i], fname);
9071 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9074 "%s %s: cannot alloc comp id array for ioc: %s\n",
9075 progname, argv[0], strerror(errno));
9080 /* get stale component info */
9081 layout = llapi_layout_get_by_fd(fd, 0);
9082 if (layout == NULL) {
9083 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9084 progname, argv[0], fname, strerror(errno));
9088 comp_size = llapi_mirror_find_stale(layout, comp_array,
9089 ARRAY_SIZE(comp_array),
9091 llapi_layout_free(layout);
9092 if (comp_size < 0) {
9097 /* prepare target mirror components instantiation */
9098 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9099 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9100 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9102 resync_ioc->lil_mirror_id = ids[0];
9104 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9105 rc = llapi_lease_set(fd, ioc);
9108 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9109 progname, argv[0], fname, strerror(errno));
9113 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9116 fprintf(stderr, "%s %s: copy error: %d\n",
9117 progname, argv[0], rc);
9121 fprintf(stdout, "mirror copied successfully: ");
9122 for (i = 0; i < copied; i++)
9123 fprintf(stdout, "%d ", ids[i]);
9124 fprintf(stdout, "\n");
9126 ioc->lil_mode = LL_LEASE_UNLCK;
9127 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9129 for (i = 0; i < comp_size; i++) {
9132 for (j = 0; j < copied; j++) {
9133 if (comp_array[i].lrc_mirror_id != ids[j])
9136 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9140 rc = llapi_lease_set(fd, ioc);
9145 "%s %s: release lease lock of '%s' failed: %s\n",
9146 progname, argv[0], fname, strerror(errno));
9160 * struct verify_chunk - Mirror chunk to be verified.
9161 * @chunk: [start, end) of the chunk.
9162 * @mirror_count: Number of mirror ids in @mirror_id array.
9163 * @mirror_id: Array of valid mirror ids that cover the chunk.
9165 struct verify_chunk {
9166 struct lu_extent chunk;
9167 unsigned int mirror_count;
9168 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9172 * print_chunks() - Print chunk information.
9173 * @fname: Mirrored file name.
9174 * @chunks: Array of chunks.
9175 * @chunk_count: Number of chunks in @chunks array.
9177 * This function prints [start, end) of each chunk in @chunks
9178 * for mirrored file @fname, and also prints the valid mirror ids
9179 * that cover the chunk.
9184 void print_chunks(const char *fname, struct verify_chunk *chunks,
9190 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9191 for (i = 0; i < chunk_count; i++) {
9192 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9194 if (chunks[i].mirror_count == 0)
9195 fprintf(stdout, "\t[");
9197 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9198 for (j = 1; j < chunks[i].mirror_count; j++)
9199 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9201 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9203 fprintf(stdout, "\n");
9207 * print_checksums() - Print CRC-32 checksum values.
9208 * @chunk: A chunk and its corresponding valid mirror ids.
9209 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9211 * This function prints CRC-32 checksum values on @chunk for
9212 * each valid mirror that covers it.
9217 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9222 "CRC-32 checksum value for chunk "DEXT":\n",
9223 PEXT(&chunk->chunk));
9224 for (i = 0; i < chunk->mirror_count; i++)
9225 fprintf(stdout, "Mirror %u:\t%#lx\n",
9226 chunk->mirror_id[i], crc[i]);
9227 fprintf(stdout, "\n");
9231 * filter_mirror_id() - Filter specified mirror ids.
9232 * @chunks: Array of chunks.
9233 * @chunk_count: Number of chunks in @chunks array.
9234 * @mirror_ids: Specified mirror ids to be verified.
9235 * @ids_nr: Number of specified mirror ids.
9237 * This function scans valid mirror ids that cover each chunk in @chunks
9238 * and filters specified mirror ids.
9243 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9244 __u16 *mirror_ids, int ids_nr)
9249 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9250 unsigned int valid_count = 0;
9252 for (i = 0; i < chunk_count; i++) {
9253 if (chunks[i].mirror_count == 0)
9257 for (j = 0; j < ids_nr; j++) {
9258 for (k = 0; k < chunks[i].mirror_count; k++) {
9259 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9260 valid_id[valid_count] = mirror_ids[j];
9267 memcpy(chunks[i].mirror_id, valid_id,
9268 sizeof(__u16) * valid_count);
9269 chunks[i].mirror_count = valid_count;
9274 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9275 * @layout: Mirror component list.
9276 * @chunks: Array of chunks.
9277 * @chunks_size: Array size of @chunks.
9279 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9280 * to find out chunk segments and store them in @chunks array.
9282 * The @mirror_id array in each element of @chunks will store the valid
9283 * mirror ids that cover the chunk. If a mirror component covering the
9284 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9285 * will not be stored into the @mirror_id array, and the chunk for that
9286 * mirror will not be verified.
9288 * The @mirror_count in each element of @chunks will store the number of
9289 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9290 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9291 * indicates the chunk is valid in only one mirror. In both cases, the
9292 * chunk will not be verified.
9294 * Here is an example:
9297 * +------+-------------+--------------------+
9299 * +------+------+------+------+-------------+
9300 * | | S | S | | mirror2
9301 * +-------------+------+------+-------------+
9303 * prepared @chunks array will contain 5 elements:
9304 * (([0, 1M), [1, 2], 2),
9305 * ([1M, 2M), [1, 2], 2),
9306 * ([2M, 3M), [1], 1),
9307 * ([3M, 4M], [], 0),
9308 * ([4M, EOF), [2], 1))
9310 * Return: the actual array size of @chunks on success
9311 * or a negative error code on failure.
9314 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9315 struct verify_chunk *chunks,
9326 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9329 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9332 "%s: move to the first layout component: %s.\n",
9333 progname, strerror(errno));
9339 chunks[idx].chunk.e_end = LUSTRE_EOF;
9341 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9344 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9345 progname, strerror(errno));
9349 if (start > chunks[idx].chunk.e_start ||
9350 end <= chunks[idx].chunk.e_start)
9353 if (end < chunks[idx].chunk.e_end)
9354 chunks[idx].chunk.e_end = end;
9356 rc = llapi_layout_comp_flags_get(layout, &flags);
9359 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9360 progname, strerror(errno));
9364 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9367 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9370 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9371 progname, strerror(errno));
9375 chunks[idx].mirror_id[i] = mirror_id;
9377 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9379 "%s: mirror_id array is too small.\n",
9386 rc = llapi_layout_comp_use(layout,
9387 LLAPI_LAYOUT_COMP_USE_NEXT);
9390 "%s: move to the next layout component: %s.\n",
9391 progname, strerror(errno));
9394 } /* loop through all components */
9396 chunks[idx].mirror_count = i;
9398 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9402 if (idx >= chunks_size) {
9403 fprintf(stderr, "%s: chunks array is too small.\n",
9409 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9413 return rc < 0 ? rc : idx + 1;
9417 * lfs_mirror_verify_chunk() - Verify a chunk.
9418 * @fd: File descriptor of the mirrored file.
9419 * @file_size: Size of the mirrored file.
9420 * @chunk: A chunk and its corresponding valid mirror ids.
9421 * @verbose: Verbose mode.
9423 * This function verifies a @chunk contains exactly the same data
9424 * ammong the mirrors that cover it.
9426 * If @verbose is specified, then the function will print where the
9427 * differences are if the data do not match. Otherwise, it will
9428 * just return an error in that case.
9430 * Return: 0 on success or a negative error code on failure.
9433 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9434 struct verify_chunk *chunk, int verbose)
9436 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9438 size_t page_size = sysconf(_SC_PAGESIZE);
9444 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9451 rc = posix_memalign(&buf, page_size, buflen);
9452 if (rc) /* error code is returned directly */
9456 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9457 PEXT(&chunk->chunk));
9458 for (i = 0; i < chunk->mirror_count; i++)
9459 fprintf(stdout, " %u", chunk->mirror_id[i]);
9460 fprintf(stdout, "\n");
9464 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9465 pos = chunk->chunk.e_start;
9466 while (bytes_done < count) {
9467 /* compute initial CRC-32 checksum */
9468 crc = crc32(0L, Z_NULL, 0);
9469 memset(crc_array, 0, sizeof(crc_array));
9472 for (i = 0; i < chunk->mirror_count; i++) {
9473 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9475 if (bytes_read < 0) {
9478 "%s: failed to read data from mirror %u: %s.\n",
9479 progname, chunk->mirror_id[i],
9484 /* compute new CRC-32 checksum */
9485 crc_array[i] = crc32(crc, buf, bytes_read);
9489 print_checksums(chunk, crc_array);
9491 /* compare CRC-32 checksum values */
9492 for (i = 1; i < chunk->mirror_count; i++) {
9493 if (crc_array[i] != crc_array[0]) {
9497 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9498 progname, PEXT(&chunk->chunk),
9499 chunk->mirror_id[0],
9500 chunk->mirror_id[i]);
9505 bytes_done += bytes_read;
9508 if (verbose > 1 && rc == 0) {
9509 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9510 PEXT(&chunk->chunk));
9511 for (i = 0; i < chunk->mirror_count; i++)
9512 fprintf(stdout, " %u", chunk->mirror_id[i]);
9513 fprintf(stdout, " PASS\n\n");
9522 * lfs_mirror_verify_file() - Verify a mirrored file.
9523 * @fname: Mirrored file name.
9524 * @mirror_ids: Specified mirror ids to be verified.
9525 * @ids_nr: Number of specified mirror ids.
9526 * @verbose: Verbose mode.
9528 * This function verifies that each SYNC mirror of a mirrored file
9529 * specified by @fname contains exactly the same data.
9531 * If @mirror_ids is specified, then the function will verify the
9532 * mirrors specified by @mirror_ids contain exactly the same data.
9534 * If @verbose is specified, then the function will print where the
9535 * differences are if the data do not match. Otherwise, it will
9536 * just return an error in that case.
9538 * Return: 0 on success or a negative error code on failure.
9541 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9544 struct verify_chunk chunks_array[1024] = { };
9545 struct llapi_layout *layout = NULL;
9549 int chunk_count = 0;
9555 if (stat(fname, &stbuf) < 0) {
9556 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9557 progname, fname, strerror(errno));
9562 if (!S_ISREG(stbuf.st_mode)) {
9563 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9569 if (stbuf.st_size == 0) {
9571 fprintf(stdout, "%s: '%s' file size is 0.\n",
9577 fd = open(fname, O_DIRECT | O_RDONLY);
9579 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9580 progname, fname, strerror(errno));
9585 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9587 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9588 progname, fname, strerror(errno));
9592 layout = llapi_layout_get_by_fd(fd, 0);
9593 if (layout == NULL) {
9594 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9595 progname, fname, strerror(errno));
9597 llapi_lease_release(fd);
9601 rc = llapi_layout_flags_get(layout, &flr_state);
9603 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9604 progname, fname, strerror(errno));
9609 flr_state &= LCM_FL_FLR_MASK;
9610 switch (flr_state) {
9613 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9614 progname, fname, llapi_layout_flags_string(flr_state));
9620 /* find out mirror chunks to be verified */
9621 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9622 ARRAY_SIZE(chunks_array));
9623 if (chunk_count < 0) {
9629 /* filter specified mirror ids */
9630 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9633 print_chunks(fname, chunks_array, chunk_count);
9635 for (idx = 0; idx < chunk_count; idx++) {
9636 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9639 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9641 PEXT(&chunks_array[idx].chunk),
9642 (unsigned long long)stbuf.st_size);
9646 if (chunks_array[idx].mirror_count == 0) {
9648 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9650 PEXT(&chunks_array[idx].chunk));
9652 fprintf(stderr, "skipped\n");
9656 fprintf(stderr, "failed\n");
9660 if (chunks_array[idx].mirror_count == 1) {
9663 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9665 PEXT(&chunks_array[idx].chunk),
9666 chunks_array[idx].mirror_id[0]);
9670 rc = llapi_lease_check(fd);
9671 if (rc != LL_LEASE_RDLCK) {
9672 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9677 /* verify one chunk */
9678 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9679 &chunks_array[idx], verbose);
9693 llapi_layout_free(layout);
9694 llapi_lease_release(fd);
9702 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9703 * @argc: The count of lfs mirror verify command line arguments.
9704 * @argv: Array of strings for lfs mirror verify command line arguments.
9706 * This function parses lfs mirror verify command and verifies the
9707 * specified mirrored file(s).
9709 * Return: 0 on success or a negative error code on failure.
9711 static inline int lfs_mirror_verify(int argc, char **argv)
9713 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9721 struct option long_opts[] = {
9722 { .val = 'o', .name = "only", .has_arg = required_argument },
9723 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9726 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9728 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9731 rc = parse_mirror_ids(mirror_ids,
9732 ARRAY_SIZE(mirror_ids),
9736 "%s: bad mirror ids '%s'.\n",
9743 "%s: at least 2 mirror ids needed with '--only' option.\n",
9753 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9754 progname, argv[optind - 1]);
9760 if (argc == optind) {
9761 fprintf(stderr, "%s: no file name given.\n", progname);
9766 if (ids_nr > 0 && argc > optind + 1) {
9768 "%s: '--only' cannot be used upon multiple files.\n",
9776 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9782 for (; optind < argc; optind++) {
9783 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9793 * lfs_mirror() - Parse and execute lfs mirror commands.
9794 * @argc: The count of lfs mirror command line arguments.
9795 * @argv: Array of strings for lfs mirror command line arguments.
9797 * This function parses lfs mirror commands and performs the
9798 * corresponding functions specified in mirror_cmdlist[].
9800 * Return: 0 on success or an error code on failure.
9802 static int lfs_mirror(int argc, char **argv)
9809 Parser_init("lfs-mirror > ", mirror_cmdlist);
9811 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9813 program_invocation_short_name = cmd;
9815 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9817 rc = Parser_commands();
9819 return rc < 0 ? -rc : rc;
9822 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9824 #if __BYTE_ORDER == __BIG_ENDIAN
9825 __swab16s(&attrs->lsa_valid);
9826 __swab64s(&attrs->lsa_size);
9827 __swab64s(&attrs->lsa_blocks);
9833 LFS_SOM_BLOCKS = 0x2,
9834 LFS_SOM_FLAGS = 0x4,
9835 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9839 static int lfs_getsom(int argc, char **argv)
9842 struct lustre_som_attrs *attrs;
9843 char buf[sizeof(*attrs) + 64];
9844 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9847 while ((c = getopt(argc, argv, "sbf")) != -1) {
9850 type = LFS_SOM_SIZE;
9853 type = LFS_SOM_BLOCKS;
9856 type = LFS_SOM_FLAGS;
9859 fprintf(stderr, "%s: invalid option '%c'\n",
9869 fprintf(stderr, "%s: %s\n",
9870 progname, argc == 0 ? "miss file target" :
9871 "input more than 2 files");
9876 attrs = (void *)buf;
9877 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9880 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
9881 argv[0], strerror(errno), errno);
9885 lustre_som_swab(attrs);
9888 case LFS_SOM_ATTR_ALL:
9889 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9890 path, attrs->lsa_size, attrs->lsa_blocks,
9894 printf("%llu\n", attrs->lsa_size);
9896 case LFS_SOM_BLOCKS:
9897 printf("%llu\n", attrs->lsa_blocks);
9900 printf("%x\n", attrs->lsa_valid);
9903 fprintf(stderr, "%s: unknown option\n", progname);
9911 * lfs_mirror_list_commands() - List lfs mirror commands.
9912 * @argc: The count of command line arguments.
9913 * @argv: Array of strings for command line arguments.
9915 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9917 * Return: 0 on success.
9919 static int lfs_mirror_list_commands(int argc, char **argv)
9921 char buffer[81] = "";
9923 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9929 static int lfs_list_commands(int argc, char **argv)
9931 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9933 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9938 int main(int argc, char **argv)
9942 /* Ensure that liblustreapi constructor has run */
9943 if (!llapi_liblustreapi_initialized())
9944 fprintf(stderr, "liblustreapi was not properly initialized\n");
9949 Parser_init("lfs > ", cmdlist);
9951 progname = program_invocation_short_name; /* Used in error messages */
9953 llapi_set_command_name(argv[1]);
9954 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9955 llapi_clear_command_name();
9957 rc = Parser_commands();
9960 return rc < 0 ? -rc : rc;
9963 #ifdef _LUSTRE_IDL_H_
9964 /* Everything we need here should be included by lustreapi.h. */
9965 # error "lfs should not depend on lustre_idl.h"
9966 #endif /* _LUSTRE_IDL_H_ */