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[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
390 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
391 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
392 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
393 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
394 " [[!] --stripe-count|-c [+-]<stripes>]\n"
395 " [[!] --stripe-index|-i <index,...>]\n"
396 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
397 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
398 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
399 " [[!] --projid <projid>]\n"
400 " [[!] --layout|-L released,raid0,mdt]\n"
401 " [[!] --component-count [+-]<comp_cnt>]\n"
402 " [[!] --component-start [+-]N[kMGTPE]]\n"
403 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
404 " [[!] --component-flags <comp_flags>]\n"
405 " [[!] --mirror-count|-N [+-]<n>]\n"
406 " [[!] --mirror-state <[^]state>]\n"
407 " [[!] --mdt-count|-T [+-]<stripes>]\n"
408 " [[!] --mdt-hash|-H <hashtype>\n"
409 " [[!] --mdt-index|-m <uuid|index,...>]\n"
410 "\t !: used before an option indicates 'NOT' requested attribute\n"
411 "\t -: used before a value indicates less than requested value\n"
412 "\t +: used before a value indicates more than requested value\n"
413 "\thashtype: hash type of the striped directory.\n"
414 "\t fnv_1a_64 FNV-1a hash algorithm\n"
415 "\t all_char sum of characters % MDT_COUNT\n"},
416 {"check", lfs_check, 0,
417 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
418 "or all the servers (MGTs, MDTs and OSTs).\n"
419 "usage: check <mgts|osts|mdts|all>"},
420 {"osts", lfs_osts, 0, "list OSTs connected to client "
421 "[for specified path only]\n" "usage: osts [path]"},
422 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
423 "[for specified path only]\n" "usage: mdts [path]"},
425 "report filesystem disk space usage or inodes usage"
426 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
427 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
428 {"getname", lfs_getname, 0, "list instances and specified mount points "
429 "[for specified path only]\n"
430 "Usage: getname [-h]|[path ...] "},
431 #ifdef HAVE_SYS_QUOTA_H
432 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
433 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
434 " -b <block-softlimit> -B <block-hardlimit>\n"
435 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
436 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
437 " [--block-softlimit <block-softlimit>]\n"
438 " [--block-hardlimit <block-hardlimit>]\n"
439 " [--inode-softlimit <inode-softlimit>]\n"
440 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
441 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
442 " [--block-grace <block-grace>]\n"
443 " [--inode-grace <inode-grace>] <filesystem>\n"
444 " setquota <-U|-G|-P>\n"
445 " -b <block-softlimit> -B <block-hardlimit>\n"
446 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
447 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
448 " [--block-softlimit <block-softlimit>]\n"
449 " [--block-hardlimit <block-hardlimit>]\n"
450 " [--inode-softlimit <inode-softlimit>]\n"
451 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
452 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
454 " -b can be used instead of --block-softlimit/--block-grace\n"
455 " -B can be used instead of --block-hardlimit\n"
456 " -i can be used instead of --inode-softlimit/--inode-grace\n"
457 " -I can be used instead of --inode-hardlimit\n"
458 " -d can be used instead of --default\n\n"
459 "Note: The total quota space will be split into many qunits and\n"
460 " balanced over all server targets, the minimal qunit size is\n"
461 " 1M bytes for block space and 1K inodes for inode space.\n\n"
462 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
463 " Quota space rebalancing process will stop when this mininum\n"
464 " value is reached. As a result, quota exceeded can be returned\n"
465 " while many targets still have 1MB or 1K inodes of spare\n"
467 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
468 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
470 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
471 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
472 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
473 {"project", lfs_project, 0,
474 "Change or list project attribute for specified file or directory.\n"
475 "usage: project [-d|-r] <file|directory...>\n"
476 " list project ID and flags on file(s) or directories\n"
477 " project [-p id] [-s] [-r] <file|directory...>\n"
478 " set project ID and/or inherit flag for specified file(s) or directories\n"
479 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
480 " check project ID and flags on file(s) or directories, print outliers\n"
481 " project -C [-r] [-k] <file|directory...>\n"
482 " clear the project inherit flag and ID on the file or directory\n"
485 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
486 "usage: flushctx [-k] [mountpoint...]"},
487 {"changelog", lfs_changelog, 0,
488 "Show the metadata changes on an MDT."
489 "\nusage: changelog <mdtname> [startrec [endrec]]"},
490 {"changelog_clear", lfs_changelog_clear, 0,
491 "Indicate that old changelog records up to <endrec> are no longer of "
492 "interest to consumer <id>, allowing the system to free up space.\n"
493 "An <endrec> of 0 means all records.\n"
494 "usage: changelog_clear <mdtname> <id> <endrec>"},
495 {"fid2path", lfs_fid2path, 0,
496 "Resolve the full path(s) for given FID(s). For a specific hardlink "
497 "specify link number <linkno>.\n"
498 /* "For a historical link name, specify changelog record <recno>.\n" */
499 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
500 /* [ --rec <recno> ] */ },
501 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
502 "usage: path2fid [--parents] <path> ..."},
503 {"data_version", lfs_data_version, 0, "Display file data version for "
504 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
505 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
506 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
507 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
508 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
509 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
510 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
512 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
513 "[--archived] [--lost] <file> ..."},
514 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
515 "given files.\n" "usage: hsm_action <file> ..."},
516 {"hsm_archive", lfs_hsm_archive, 0,
517 "Archive file to external storage.\n"
518 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
520 {"hsm_restore", lfs_hsm_restore, 0,
521 "Restore file from external storage.\n"
522 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
523 {"hsm_release", lfs_hsm_release, 0,
524 "Release files from Lustre.\n"
525 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
526 {"hsm_remove", lfs_hsm_remove, 0,
527 "Remove file copy from external storage.\n"
528 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
530 " (FILE [FILE ...] | "
531 "--mntpath MOUNTPATH FID [FID ...])\n"
533 "Note: To remove an archived copy of a file already deleted from a "
535 "--mntpath option and a list of FIDs must be specified"
537 {"hsm_cancel", lfs_hsm_cancel, 0,
538 "Cancel requests related to specified files.\n"
539 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
540 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
541 "usage: swap_layouts <path1> <path2>"},
542 {"migrate", lfs_setstripe_migrate, 0,
543 "migrate a directory between MDTs.\n"
544 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
545 " [--mdt-hash|-H] <hash_type>\n"
546 " [--mdt-index|-m] <start_mdt_index>\n"
549 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
550 " it's the MDT index of first stripe\n"
551 "\tmdt_count: number of MDTs to stripe a directory over\n"
552 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
553 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
554 " all_char sum of characters % MDT_COUNT\n"
556 "migrate file objects from one OST "
557 "layout\nto another (may be not safe with concurent writes).\n"
559 "[--stripe-count|-c] <stripe_count>\n"
560 " [--stripe-index|-i] <start_ost_index>\n"
561 " [--stripe-size|-S] <stripe_size>\n"
562 " [--pool|-p] <pool_name>\n"
563 " [--ost|-o] <ost_indices>\n"
565 " [--non-block|-n]\n"
566 " [--non-direct|-D]\n"
567 " <file|directory>\n"
568 "\tstripe_count: number of OSTs to stripe a file over\n"
569 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
570 "\tstripe_size: number of bytes to store before moving to the next OST\n"
571 "\tpool_name: name of the predefined pool of OSTs\n"
572 "\tost_indices: OSTs to stripe over, in order\n"
573 "\tblock: Block file access during data migration (default)\n"
574 "\tnon-block: Abort migrations if concurrent access is detected\n"
575 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
577 "To move directories between MDTs. This command is deprecated, "
578 "use \"migrate\" instead.\n"
579 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
581 {"ladvise", lfs_ladvise, 0,
582 "Provide servers with advice about access patterns for a file.\n"
583 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
584 " [--background|-b] [--unset|-u]\n\n"
585 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
586 " {[--mode|-m [READ,WRITE]}\n"
588 {"mirror", lfs_mirror, mirror_cmdlist,
589 "lfs commands used to manage files with mirrored components:\n"
590 "lfs mirror create - create a mirrored file or directory\n"
591 "lfs mirror extend - add mirror(s) to an existing file\n"
592 "lfs mirror split - split a mirror from an existing mirrored file\n"
593 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
594 "lfs mirror read - read a mirror content of a mirrored file\n"
595 "lfs mirror write - write to a mirror of a mirrored file\n"
596 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
597 "lfs mirror verify - verify mirrored file(s)\n"},
598 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
599 "usage: getsom [-s] [-b] [-f] <path>\n"
600 "\t-s: Only show the size value of the SOM data for a given file\n"
601 "\t-b: Only show the blocks value of the SOM data for a given file\n"
602 "\t-f: Only show the flags value of the SOM data for a given file\n"},
603 {"heat_get", lfs_heat_get, 0,
604 "To get heat of files.\n"
605 "usage: heat_get <file> ...\n"},
606 {"heat_set", lfs_heat_set, 0,
607 "To set heat flags of files.\n"
608 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
609 "\t--clear|-c: Clear file heat for given files\n"
610 "\t--off|-o: Turn off file heat for given files\n"
611 "\t--on|-O: Turn on file heat for given files\n"},
612 {"help", Parser_help, 0, "help"},
613 {"exit", Parser_quit, 0, "quit"},
614 {"quit", Parser_quit, 0, "quit"},
615 {"--version", Parser_version, 0,
616 "output build version of the utility and exit"},
617 {"--list-commands", lfs_list_commands, 0,
618 "list commands supported by the utility and exit"},
623 static int check_hashtype(const char *hashtype)
627 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
628 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
635 static const char *error_loc = "syserror";
638 MIGRATION_NONBLOCK = 0x0001,
639 MIGRATION_MIRROR = 0x0002,
640 MIGRATION_NONDIRECT = 0x0004,
641 MIGRATION_VERBOSE = 0x0008,
644 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
645 struct llapi_layout *layout);
648 migrate_open_files(const char *name, __u64 migration_flags,
649 const struct llapi_stripe_param *param,
650 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
657 char parent[PATH_MAX];
658 char volatile_file[PATH_MAX];
664 if (param == NULL && layout == NULL) {
665 error_loc = "layout information";
669 /* search for file directory pathname */
670 if (strlen(name) > sizeof(parent) - 1) {
671 error_loc = "source file name";
675 strncpy(parent, name, sizeof(parent));
676 ptr = strrchr(parent, '/');
678 if (getcwd(parent, sizeof(parent)) == NULL) {
679 error_loc = "getcwd";
683 if (ptr == parent) /* leading '/' */
688 /* open file, direct io */
689 /* even if the file is only read, WR mode is nedeed to allow
690 * layout swap on fd */
692 if (!(migration_flags & MIGRATION_NONDIRECT))
694 fd = open(name, rflags);
697 error_loc = "cannot open source file";
701 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
703 error_loc = "cannot get MDT index";
708 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
709 mode_t open_mode = S_IRUSR | S_IWUSR;
711 random_value = random();
712 rc = snprintf(volatile_file, sizeof(volatile_file),
713 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
714 mdt_index, random_value);
715 if (rc >= sizeof(volatile_file)) {
720 /* create, open a volatile file, use caching (ie no directio) */
722 fdv = llapi_file_open_param(volatile_file, open_flags,
725 fdv = lfs_component_create(volatile_file, open_flags,
727 } while (fdv < 0 && (rc = fdv) == -EEXIST);
730 error_loc = "cannot create volatile file";
734 /* In case the MDT does not support creation of volatile files
735 * we should try to unlink it. */
736 (void)unlink(volatile_file);
738 /* Not-owner (root?) special case.
739 * Need to set owner/group of volatile file like original.
740 * This will allow to pass related check during layout_swap.
745 error_loc = "cannot stat source file";
749 rc = fstat(fdv, &stv);
752 error_loc = "cannot stat volatile";
756 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
757 rc = fchown(fdv, st.st_uid, st.st_gid);
760 error_loc = "cannot change ownwership of volatile";
779 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
781 struct llapi_layout *layout;
782 size_t buf_size = 4 * 1024 * 1024;
791 layout = llapi_layout_get_by_fd(fd_src, 0);
792 if (layout != NULL) {
793 uint64_t stripe_size;
795 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
797 buf_size = stripe_size;
799 llapi_layout_free(layout);
802 /* Use a page-aligned buffer for direct I/O */
803 rc = posix_memalign(&buf, getpagesize(), buf_size);
808 /* read new data only if we have written all
809 * previously read data */
812 rc = check_file(fd_src);
817 rsize = read(fd_src, buf, buf_size);
829 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
848 static int migrate_copy_timestamps(int fd, int fdv)
852 if (fstat(fd, &st) == 0) {
853 struct timeval tv[2] = {
854 {.tv_sec = st.st_atime},
855 {.tv_sec = st.st_mtime}
858 return futimes(fdv, tv);
864 static int migrate_block(int fd, int fdv)
871 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
873 error_loc = "cannot get dataversion";
881 /* The grouplock blocks all concurrent accesses to the file.
882 * It has to be taken after llapi_get_data_version as it would
884 rc = llapi_group_lock(fd, gid);
886 error_loc = "cannot get group lock";
890 rc = migrate_copy_data(fd, fdv, NULL);
892 error_loc = "data copy failed";
896 /* Make sure we keep original atime/mtime values */
897 rc = migrate_copy_timestamps(fd, fdv);
899 error_loc = "timestamp copy failed";
904 * for a migration we need to check data version on file did
907 * Pass in gid=0 since we already own grouplock. */
908 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
909 SWAP_LAYOUTS_CHECK_DV1);
911 error_loc = "file changed";
914 error_loc = "cannot swap layout";
919 rc2 = llapi_group_unlock(fd, gid);
920 if (rc2 < 0 && rc == 0) {
921 error_loc = "unlock group lock";
929 * Internal helper for migrate_copy_data(). Check lease and report error if
932 * \param[in] fd File descriptor on which to check the lease.
934 * \retval 0 Migration can keep on going.
935 * \retval -errno Error occurred, abort migration.
937 static int check_lease(int fd)
941 rc = llapi_lease_check(fd);
943 return 0; /* llapi_check_lease returns > 0 on success. */
948 static int migrate_nonblock(int fd, int fdv)
954 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
956 error_loc = "cannot get data version";
960 rc = migrate_copy_data(fd, fdv, check_lease);
962 error_loc = "data copy failed";
966 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
968 error_loc = "cannot get data version";
974 error_loc = "source file changed";
978 /* Make sure we keep original atime/mtime values */
979 rc = migrate_copy_timestamps(fd, fdv);
981 error_loc = "timestamp copy failed";
988 static int lfs_component_set(char *fname, int comp_id,
989 __u32 flags, __u32 neg_flags)
992 __u32 flags_array[2];
997 ids[count] = comp_id;
998 flags_array[count] = flags;
1003 ids[count] = comp_id;
1004 flags_array[count] = neg_flags | LCME_FL_NEG;
1008 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1011 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1012 progname, comp_id, fname, flags, neg_flags);
1017 static int lfs_component_del(char *fname, __u32 comp_id,
1018 __u32 flags, __u32 neg_flags)
1022 if (flags && neg_flags)
1025 if (!flags && neg_flags)
1026 flags = neg_flags | LCME_FL_NEG;
1028 if ((flags && comp_id) || (!flags && !comp_id))
1031 /* LCME_FL_INIT is the only supported flag in PFL */
1033 if (flags & ~LCME_KNOWN_FLAGS) {
1035 "%s setstripe: unknown flags %#x\n",
1039 } else if (comp_id > LCME_ID_MAX) {
1040 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1045 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1048 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1049 progname, comp_id, fname, strerror(errno));
1053 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1060 rc = llapi_layout_file_comp_add(fname, layout);
1062 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1063 fname, strerror(errno));
1067 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1068 struct llapi_layout *layout)
1076 fd = lstat(fname, &st);
1077 if (fd == 0 && S_ISDIR(st.st_mode))
1078 open_flags = O_DIRECTORY | O_RDONLY;
1080 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1082 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1083 S_ISDIR(st.st_mode) ?
1084 "set default composite layout for" :
1085 "create composite file",
1086 fname, strerror(errno));
1090 static int lfs_migrate(char *name, __u64 migration_flags,
1091 struct llapi_stripe_param *param,
1092 struct llapi_layout *layout)
1098 rc = migrate_open_files(name, migration_flags, param, layout,
1103 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1104 /* Blocking mode (forced if servers do not support file lease).
1105 * It is also the default mode, since we cannot distinguish
1106 * between a broken lease and a server that does not support
1107 * atomic swap/close (LU-6785) */
1108 rc = migrate_block(fd, fdv);
1112 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1114 error_loc = "cannot get lease";
1118 rc = migrate_nonblock(fd, fdv);
1120 llapi_lease_release(fd);
1124 /* Atomically put lease, swap layouts and close.
1125 * for a migration we need to check data version on file did
1127 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1129 error_loc = "cannot swap layout";
1141 fprintf(stderr, "error: %s: %s: %s: %s\n",
1142 progname, name, error_loc, strerror(-rc));
1143 else if (migration_flags & MIGRATION_VERBOSE)
1144 printf("%s\n", name);
1149 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1158 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1162 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1163 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1164 const char *comp_name = comp_flags_table[i].cfn_name;
1166 if (strcmp(name, comp_name) == 0) {
1167 *flags |= comp_flag;
1169 } else if (strncmp(name, "^", 1) == 0 &&
1170 strcmp(name + 1, comp_name) == 0) {
1171 *neg_flags |= comp_flag;
1176 llapi_printf(LLAPI_MSG_ERROR,
1177 "%s: component flag '%s' not supported\n",
1183 if (!*flags && !*neg_flags)
1186 /* don't allow to set and exclude the same flag */
1187 if (*flags & *neg_flags)
1193 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1201 if (strncmp(string, "^", 1) == 0) {
1202 *neg_state = llapi_layout_string_flags(string + 1);
1203 if (*neg_state != 0)
1206 *state = llapi_layout_string_flags(string);
1211 llapi_printf(LLAPI_MSG_ERROR,
1212 "%s: mirrored file state '%s' not supported\n",
1218 * struct mirror_args - Command-line arguments for mirror(s).
1219 * @m_count: Number of mirrors to be created with this layout.
1220 * @m_flags: Mirror level flags, only 'prefer' is supported.
1221 * @m_layout: Mirror layout.
1222 * @m_file: A victim file. Its layout will be split and used as a mirror.
1223 * @m_next: Point to the next node of the list.
1225 * Command-line arguments for mirror(s) will be parsed and stored in
1226 * a linked list that consists of this structure.
1228 struct mirror_args {
1231 struct llapi_layout *m_layout;
1233 struct mirror_args *m_next;
1236 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1241 rc = llapi_layout_comp_flags_get(layout, &flags);
1245 if (flags & LCME_FL_NEG) {
1246 fprintf(stderr, "error: %s: negative flags are not supported\n",
1251 if (flags & LCME_FL_STALE) {
1252 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1253 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1257 return LLAPI_LAYOUT_ITER_CONT;
1260 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1262 uint64_t start, end;
1266 /* LU-10112: do not support dom+flr in phase 1 */
1267 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1271 rc = llapi_layout_pattern_get(layout, &pattern);
1275 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1276 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1281 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1285 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1289 if (end != LUSTRE_EOF) {
1290 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1295 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1300 * enum mirror_flags - Flags for extending a mirrored file.
1301 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1302 * in case the victim file(s) contains the same data as the
1303 * original mirrored file.
1304 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1305 * @MF_COMP_ID: specified component id instead of mirror id
1307 * Flags for extending a mirrored file.
1316 * mirror_create_sanity_check() - Check mirror list.
1317 * @list: A linked list that stores the mirror arguments.
1319 * This function does a sanity check on @list for creating
1322 * Return: 0 on success or a negative error code on failure.
1324 static int mirror_create_sanity_check(const char *fname,
1325 struct mirror_args *list)
1328 bool has_m_file = false;
1329 bool has_m_layout = false;
1335 struct llapi_layout *layout;
1337 layout = llapi_layout_get_by_path(fname, 0);
1340 "error: %s: file '%s' couldn't get layout\n",
1345 rc = mirror_sanity_check_one(layout);
1346 llapi_layout_free(layout);
1352 while (list != NULL) {
1353 if (list->m_file != NULL) {
1355 llapi_layout_free(list->m_layout);
1358 llapi_layout_get_by_path(list->m_file, 0);
1359 if (list->m_layout == NULL) {
1361 "error: %s: file '%s' has no layout\n",
1362 progname, list->m_file);
1366 has_m_layout = true;
1367 if (list->m_layout == NULL) {
1368 fprintf(stderr, "error: %s: no mirror layout\n",
1374 rc = mirror_sanity_check_one(list->m_layout);
1378 list = list->m_next;
1381 if (has_m_file && has_m_layout) {
1383 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1391 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1393 __u32 mirror_flags = *(__u32 *)cbdata;
1397 rc = llapi_layout_comp_flags_get(layout, &flags);
1402 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1407 return LLAPI_LAYOUT_ITER_CONT;
1411 * mirror_create() - Create a mirrored file.
1412 * @fname: The file to be created.
1413 * @mirror_list: A linked list that stores the mirror arguments.
1415 * This function creates a mirrored file @fname with the mirror(s)
1416 * from @mirror_list.
1418 * Return: 0 on success or a negative error code on failure.
1420 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1422 struct llapi_layout *layout = NULL;
1423 struct mirror_args *cur_mirror = NULL;
1424 uint16_t mirror_count = 0;
1428 rc = mirror_create_sanity_check(NULL, mirror_list);
1432 cur_mirror = mirror_list;
1433 while (cur_mirror != NULL) {
1434 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1436 &cur_mirror->m_flags);
1439 fprintf(stderr, "%s: failed to set mirror flags\n",
1444 for (i = 0; i < cur_mirror->m_count; i++) {
1445 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1448 fprintf(stderr, "error: %s: "
1449 "merge layout failed: %s\n",
1450 progname, strerror(errno));
1454 mirror_count += cur_mirror->m_count;
1455 cur_mirror = cur_mirror->m_next;
1458 if (layout == NULL) {
1459 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1463 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1466 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1467 progname, strerror(errno));
1471 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1479 llapi_layout_free(layout);
1484 * Compare files and check lease on @fd.
1486 * \retval bytes number of bytes are the same
1488 static ssize_t mirror_file_compare(int fd, int fdv)
1490 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1492 ssize_t bytes_done = 0;
1493 ssize_t bytes_read = 0;
1495 buf = malloc(buflen * 2);
1500 if (!llapi_lease_check(fd)) {
1501 bytes_done = -EBUSY;
1505 bytes_read = read(fd, buf, buflen);
1506 if (bytes_read <= 0)
1509 if (bytes_read != read(fdv, buf + buflen, buflen))
1512 /* XXX: should compute the checksum on each buffer and then
1513 * compare checksum to avoid cache collision */
1514 if (memcmp(buf, buf + buflen, bytes_read))
1517 bytes_done += bytes_read;
1525 static int mirror_extend_file(const char *fname, const char *victim_file,
1526 enum mirror_flags mirror_flags)
1531 struct stat stbuf_v;
1532 struct ll_ioc_lease *data = NULL;
1535 fd = open(fname, O_RDWR);
1537 error_loc = "open source file";
1542 fdv = open(victim_file, O_RDWR);
1544 error_loc = "open target file";
1549 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1550 error_loc = "stat source or target file";
1555 if (stbuf.st_dev != stbuf_v.st_dev) {
1556 error_loc = "stat source and target file";
1561 /* mirrors should be of the same size */
1562 if (stbuf.st_size != stbuf_v.st_size) {
1563 error_loc = "file sizes don't match";
1568 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1570 error_loc = "cannot get lease";
1574 if (!(mirror_flags & MF_NO_VERIFY)) {
1576 /* mirrors should have the same contents */
1577 ret = mirror_file_compare(fd, fdv);
1578 if (ret != stbuf.st_size) {
1579 error_loc = "file busy or contents don't match";
1580 rc = ret < 0 ? ret : -EINVAL;
1585 /* Get rid of caching pages from clients */
1586 rc = llapi_file_flush(fd);
1588 error_loc = "cannot get data version";
1592 rc = llapi_file_flush(fdv);
1594 error_loc = "cannot get data version";
1599 /* Make sure we keep original atime/mtime values */
1600 rc = migrate_copy_timestamps(fd, fdv);
1602 error_loc = "cannot copy timestamp";
1606 /* Atomically put lease, merge layouts and close. */
1607 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1609 error_loc = "memory allocation";
1612 data->lil_mode = LL_LEASE_UNLCK;
1613 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1614 data->lil_count = 1;
1615 data->lil_ids[0] = fdv;
1616 rc = llapi_lease_set(fd, data);
1618 error_loc = "cannot merge layout";
1620 } else if (rc == 0) {
1622 error_loc = "lost lease lock";
1635 (void) unlink(victim_file);
1637 fprintf(stderr, "error: %s: %s: %s: %s\n",
1638 progname, fname, error_loc, strerror(-rc));
1642 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1644 struct ll_ioc_lease *data = NULL;
1649 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1653 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1655 error_loc = "cannot get lease";
1659 rc = migrate_nonblock(fd, fdv);
1661 llapi_lease_release(fd);
1665 /* Atomically put lease, merge layouts and close. */
1666 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1668 error_loc = "memory allocation";
1671 data->lil_mode = LL_LEASE_UNLCK;
1672 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1673 data->lil_count = 1;
1674 data->lil_ids[0] = fdv;
1675 rc = llapi_lease_set(fd, data);
1677 error_loc = "cannot merge layout";
1679 } else if (rc == 0) {
1681 error_loc = "lost lease lock";
1694 fprintf(stderr, "error: %s: %s: %s: %s\n",
1695 progname, name, error_loc, strerror(-rc));
1699 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1700 enum mirror_flags mirror_flags)
1704 rc = mirror_create_sanity_check(fname, mirror_list);
1708 while (mirror_list) {
1709 if (mirror_list->m_file != NULL) {
1710 rc = mirror_extend_file(fname, mirror_list->m_file,
1713 __u32 mirror_count = mirror_list->m_count;
1715 while (mirror_count > 0) {
1716 rc = mirror_extend_layout(fname,
1717 mirror_list->m_layout);
1727 mirror_list = mirror_list->m_next;
1733 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1738 rc = llapi_layout_mirror_id_get(layout, &id);
1742 if ((__u16)id == *(__u16 *)cbdata)
1743 return LLAPI_LAYOUT_ITER_STOP;
1745 return LLAPI_LAYOUT_ITER_CONT;
1748 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1753 rc = llapi_layout_comp_id_get(layout, &id);
1757 if (id == *(__u32 *)cbdata)
1758 return LLAPI_LAYOUT_ITER_STOP;
1760 return LLAPI_LAYOUT_ITER_CONT;
1762 static int mirror_split(const char *fname, __u32 id,
1763 enum mirror_flags mflags, const char *victim_file)
1765 struct llapi_layout *layout;
1766 char parent[PATH_MAX];
1767 char victim[PATH_MAX];
1768 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1770 struct ll_ioc_lease *data;
1771 uint16_t mirror_count;
1776 /* check fname contains mirror with mirror_id/comp_id */
1777 layout = llapi_layout_get_by_path(fname, 0);
1780 "error %s: file '%s' couldn't get layout\n",
1785 rc = mirror_sanity_check_one(layout);
1789 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1792 "error %s: file '%s' couldn't get mirror count\n",
1796 if (mirror_count < 2) {
1798 "error %s: file '%s' has %d component, cannot split\n",
1799 progname, fname, mirror_count);
1803 if (mflags & MF_COMP_ID) {
1804 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1805 id = mirror_id_of(id);
1807 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1810 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1813 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1815 "error %s: file '%s' does not contain mirror with id %u\n",
1816 progname, fname, id);
1820 fd = open(fname, O_RDWR);
1823 "error %s: open file '%s' failed: %s\n",
1824 progname, fname, strerror(errno));
1828 /* get victim file directory pathname */
1829 if (strlen(fname) > sizeof(parent) - 1) {
1830 fprintf(stderr, "error %s: file name of '%s' too long\n",
1835 strncpy(parent, fname, sizeof(parent));
1836 ptr = strrchr(parent, '/');
1838 if (getcwd(parent, sizeof(parent)) == NULL) {
1839 fprintf(stderr, "error %s: getcwd failed: %s\n",
1840 progname, strerror(errno));
1850 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1852 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1857 if (victim_file == NULL) {
1858 /* use a temp file to store the splitted layout */
1859 if (mflags & MF_DESTROY) {
1860 fdv = llapi_create_volatile_idx(parent, mdt_index,
1861 O_LOV_DELAY_CREATE);
1863 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1865 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1868 /* user specified victim file */
1869 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1874 "error %s: create victim file failed: %s\n",
1875 progname, strerror(errno));
1879 /* get lease lock of fname */
1880 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1883 "error %s: cannot get lease of file '%s': %d\n",
1884 progname, fname, rc);
1888 /* Atomatically put lease, split layouts and close. */
1889 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1895 data->lil_mode = LL_LEASE_UNLCK;
1896 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1897 data->lil_count = 2;
1898 data->lil_ids[0] = fdv;
1899 data->lil_ids[1] = id;
1900 rc = llapi_lease_set(fd, data);
1902 if (rc == 0) /* lost lease lock */
1905 "error %s: cannot split '%s': %s\n",
1906 progname, fname, strerror(-rc));
1917 llapi_layout_free(layout);
1922 * Parse a string containing an target index list into an array of integers.
1924 * The input string contains a comma delimited list of individual
1925 * indices and ranges, for example "1,2-4,7". Add the indices into the
1926 * \a tgts array and remove duplicates.
1928 * \param[out] tgts array to store indices in
1929 * \param[in] size size of \a tgts array
1930 * \param[in] offset starting index in \a tgts
1931 * \param[in] arg string containing OST index list
1933 * \retval positive number of indices in \a tgts
1934 * \retval -EINVAL unable to parse \a arg
1936 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1940 int slots = size - offset;
1947 end_of_loop = false;
1948 while (!end_of_loop) {
1952 char *endptr = NULL;
1956 ptr = strchrnul(arg, ',');
1958 end_of_loop = *ptr == '\0';
1961 start_index = strtol(arg, &endptr, 0);
1962 if (endptr == arg) /* no data at all */
1964 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1967 end_index = start_index;
1968 if (*endptr == '-') {
1969 end_index = strtol(endptr + 1, &endptr, 0);
1970 if (*endptr != '\0')
1972 if (end_index < start_index)
1976 for (i = start_index; i <= end_index && slots > 0; i++) {
1979 /* remove duplicate */
1980 for (j = 0; j < offset; j++) {
1984 if (j == offset) { /* no duplicate */
1989 if (slots == 0 && i < end_index)
1997 if (!end_of_loop && ptr != NULL)
2000 return rc < 0 ? rc : nr;
2003 struct lfs_setstripe_args {
2004 unsigned long long lsa_comp_end;
2005 unsigned long long lsa_stripe_size;
2006 long long lsa_stripe_count;
2007 long long lsa_stripe_off;
2008 __u32 lsa_comp_flags;
2009 __u32 lsa_comp_neg_flags;
2010 unsigned long long lsa_pattern;
2011 unsigned int lsa_mirror_count;
2013 bool lsa_first_comp;
2015 char *lsa_pool_name;
2018 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2020 unsigned int mirror_count = lsa->lsa_mirror_count;
2021 bool first_comp = lsa->lsa_first_comp;
2023 memset(lsa, 0, sizeof(*lsa));
2025 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2026 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2027 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2028 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2029 lsa->lsa_pool_name = NULL;
2031 lsa->lsa_mirror_count = mirror_count;
2032 lsa->lsa_first_comp = first_comp;
2036 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2037 * @lsa: Stripe options to be initialized and inherited.
2039 * This function initializes stripe options in @lsa and inherit
2040 * stripe_size, stripe_count and OST pool_name options.
2044 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2046 unsigned long long stripe_size;
2047 long long stripe_count;
2048 char *pool_name = NULL;
2050 stripe_size = lsa->lsa_stripe_size;
2051 stripe_count = lsa->lsa_stripe_count;
2052 pool_name = lsa->lsa_pool_name;
2054 setstripe_args_init(lsa);
2056 lsa->lsa_stripe_size = stripe_size;
2057 lsa->lsa_stripe_count = stripe_count;
2058 lsa->lsa_pool_name = pool_name;
2061 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2063 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2064 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2065 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2066 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2067 lsa->lsa_pool_name != NULL ||
2068 lsa->lsa_comp_end != 0);
2072 * comp_args_to_layout() - Create or extend a composite layout.
2073 * @composite: Pointer to the composite layout.
2074 * @lsa: Stripe options for the new component.
2076 * This function creates or extends a composite layout by adding a new
2077 * component with stripe options from @lsa.
2079 * Return: 0 on success or an error code on failure.
2081 static int comp_args_to_layout(struct llapi_layout **composite,
2082 struct lfs_setstripe_args *lsa,
2085 struct llapi_layout *layout = *composite;
2086 uint64_t prev_end = 0;
2089 if (layout == NULL) {
2090 layout = llapi_layout_alloc();
2091 if (layout == NULL) {
2092 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2096 *composite = layout;
2100 /* Get current component extent, current component
2101 * must be the tail component. */
2102 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2104 fprintf(stderr, "Get comp extent failed. %s\n",
2109 if (lsa->lsa_first_comp)
2112 if (lsa->lsa_first_comp)
2113 rc = llapi_layout_add_first_comp(layout);
2115 rc = llapi_layout_comp_add(layout);
2117 fprintf(stderr, "Add component failed. %s\n",
2122 /* reset lsa_first_comp */
2123 lsa->lsa_first_comp = false;
2126 rc = llapi_layout_comp_extent_set(layout, prev_end,
2129 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2130 prev_end, lsa->lsa_comp_end, strerror(errno));
2135 /* Data-on-MDT component setting */
2136 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2137 /* In case of Data-on-MDT patterns the only extra option
2138 * applicable is stripe size option. */
2139 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2140 fprintf(stderr, "Option 'stripe-count' can't be "
2141 "specified with Data-on-MDT component: %lld\n",
2142 lsa->lsa_stripe_count);
2145 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2146 fprintf(stderr, "Option 'stripe-size' can't be "
2147 "specified with Data-on-MDT component: %llu\n",
2148 lsa->lsa_stripe_size);
2151 if (lsa->lsa_nr_tgts != 0) {
2152 fprintf(stderr, "Option 'ost-list' can't be specified "
2153 "with Data-on-MDT component: '%i'\n",
2157 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2158 fprintf(stderr, "Option 'stripe-offset' can't be "
2159 "specified with Data-on-MDT component: %lld\n",
2160 lsa->lsa_stripe_off);
2163 if (lsa->lsa_pool_name != 0) {
2164 fprintf(stderr, "Option 'pool' can't be specified "
2165 "with Data-on-MDT component: '%s'\n",
2166 lsa->lsa_pool_name);
2170 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2172 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2173 lsa->lsa_pattern, strerror(errno));
2176 /* Data-on-MDT component has always single stripe up to end */
2177 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2180 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2182 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2183 lsa->lsa_stripe_size, strerror(errno));
2187 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2189 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2190 lsa->lsa_stripe_count, strerror(errno));
2194 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2196 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2197 lsa->lsa_comp_flags, strerror(errno));
2201 if (lsa->lsa_pool_name != NULL) {
2202 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2204 fprintf(stderr, "Set pool name: %s failed. %s\n",
2205 lsa->lsa_pool_name, strerror(errno));
2209 rc = llapi_layout_pool_name_set(layout, "");
2211 fprintf(stderr, "Clear pool name failed: %s\n",
2217 if (lsa->lsa_nr_tgts > 0) {
2218 if (lsa->lsa_stripe_count > 0 &&
2219 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2220 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2221 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2222 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2223 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2226 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2227 rc = llapi_layout_ost_index_set(layout, i,
2232 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2233 lsa->lsa_stripe_off != -1) {
2234 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2237 fprintf(stderr, "Set ost index %d failed. %s\n",
2238 i, strerror(errno));
2245 static int build_component(struct llapi_layout **layout,
2246 struct lfs_setstripe_args *lsa, bool set_extent)
2250 rc = comp_args_to_layout(layout, lsa, set_extent);
2254 if (lsa->lsa_mirror_count > 0) {
2255 rc = llapi_layout_mirror_count_set(*layout,
2256 lsa->lsa_mirror_count);
2260 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2263 lsa->lsa_mirror_count = 0;
2269 static int build_layout_from_yaml_node(struct cYAML *node,
2270 struct llapi_layout **layout,
2271 struct lfs_setstripe_args *lsa,
2278 if (node->cy_type == CYAML_TYPE_OBJECT) {
2279 /* go deep to sub blocks */
2280 rc = build_layout_from_yaml_node(node->cy_child, layout,
2285 if (node->cy_string == NULL)
2288 string = node->cy_string;
2289 /* skip leading lmm_ if present, to simplify parsing */
2290 if (strncmp(string, "lmm_", 4) == 0)
2293 if (node->cy_type == CYAML_TYPE_STRING) {
2294 if (!strcmp(string, "lcme_extent.e_end")) {
2295 if (!strcmp(node->cy_valuestring, "EOF") ||
2296 !strcmp(node->cy_valuestring, "eof"))
2297 lsa->lsa_comp_end = LUSTRE_EOF;
2298 } else if (!strcmp(string, "pool")) {
2299 lsa->lsa_pool_name = node->cy_valuestring;
2300 } else if (!strcmp(string, "pattern")) {
2301 if (!strcmp(node->cy_valuestring, "mdt"))
2302 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2303 } else if (!strcmp(string, "lcme_flags")) {
2304 rc = comp_str2flags(node->cy_valuestring,
2305 &lsa->lsa_comp_flags,
2306 &lsa->lsa_comp_neg_flags);
2309 /* Only template flags have meaning in
2310 * the layout for a new file
2312 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2314 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2315 if (!strcmp(string, "lcm_mirror_count")) {
2316 lsa->lsa_mirror_count = node->cy_valueint;
2317 } else if (!strcmp(string, "lcme_extent.e_start")) {
2318 if (node->cy_valueint != 0 || *layout != NULL) {
2319 rc = build_component(layout, lsa, true);
2324 if (node->cy_valueint == 0)
2325 lsa->lsa_first_comp = true;
2327 /* initialize lsa */
2328 setstripe_args_init(lsa);
2329 lsa->lsa_tgts = osts;
2330 } else if (!strcmp(string, "lcme_extent.e_end")) {
2331 if (node->cy_valueint == -1)
2332 lsa->lsa_comp_end = LUSTRE_EOF;
2334 lsa->lsa_comp_end = node->cy_valueint;
2335 } else if (!strcmp(string, "stripe_count")) {
2336 lsa->lsa_stripe_count = node->cy_valueint;
2337 } else if (!strcmp(string, "stripe_size")) {
2338 lsa->lsa_stripe_size = node->cy_valueint;
2339 } else if (!strcmp(string, "stripe_offset")) {
2340 lsa->lsa_stripe_off = node->cy_valueint;
2341 } else if (!strcmp(string, "l_ost_idx")) {
2342 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2347 node = node->cy_next;
2353 static int lfs_comp_create_from_yaml(char *template,
2354 struct llapi_layout **layout,
2355 struct lfs_setstripe_args *lsa,
2358 struct cYAML *tree = NULL, *err_rc = NULL;
2361 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2363 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2364 progname, template);
2365 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2366 "can't parse", &err_rc);
2367 cYAML_print_tree2file(stderr, err_rc);
2368 cYAML_free_tree(err_rc);
2373 /* initialize lsa for plain file */
2374 setstripe_args_init(lsa);
2375 lsa->lsa_tgts = osts;
2377 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2379 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2380 progname, template);
2383 rc = build_component(layout, lsa, *layout != NULL);
2385 /* clean clean lsa */
2386 setstripe_args_init(lsa);
2390 cYAML_free_tree(tree);
2394 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2395 * end of the last component in the existing file, and adjust the
2396 * first extent start of the components to be added accordingly. */
2397 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2399 struct llapi_layout *head;
2400 uint64_t start, end, stripe_size, prev_end = 0;
2403 if (layout == NULL) {
2405 "%s setstripe: layout must be specified\n",
2411 head = llapi_layout_get_by_path(fname, 0);
2414 "%s setstripe: cannot read layout from '%s': %s\n",
2415 progname, fname, strerror(errno));
2417 } else if (errno == ENODATA) {
2418 /* file without LOVEA, this component-add will be turned
2419 * into a component-create. */
2420 llapi_layout_free(head);
2422 } else if (!llapi_layout_is_composite(head)) {
2423 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2425 llapi_layout_free(head);
2429 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2431 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2432 progname, strerror(errno));
2433 llapi_layout_free(head);
2437 llapi_layout_free(head);
2439 /* Make sure we use the first component of the layout to be added. */
2440 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2443 "%s setstripe: cannot move component cursor: %s\n",
2444 progname, strerror(errno));
2448 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2450 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2451 progname, strerror(errno));
2455 if (start > prev_end || end <= prev_end) {
2457 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2458 progname, start, end, prev_end);
2462 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2464 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2465 progname, strerror(errno));
2469 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2470 (prev_end & (stripe_size - 1))) {
2472 "%s setstripe: stripe size %lu not aligned with %lu\n",
2473 progname, stripe_size, prev_end);
2477 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2480 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2481 progname, prev_end, end, strerror(errno));
2488 static inline bool arg_is_eof(char *arg)
2490 return !strncmp(arg, "-1", strlen("-1")) ||
2491 !strncmp(arg, "EOF", strlen("EOF")) ||
2492 !strncmp(arg, "eof", strlen("eof"));
2496 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2498 * Return: Valid mirror_args pointer on success and
2499 * NULL if memory allocation fails.
2501 static struct mirror_args *lfs_mirror_alloc(void)
2503 struct mirror_args *mirror = NULL;
2506 mirror = calloc(1, sizeof(*mirror));
2517 * lfs_mirror_free() - Free memory allocated for a mirror argument
2519 * @mirror: Previously allocated mirror argument structure by
2520 * lfs_mirror_alloc().
2522 * Free memory allocated for @mirror.
2526 static void lfs_mirror_free(struct mirror_args *mirror)
2528 if (mirror->m_layout != NULL)
2529 llapi_layout_free(mirror->m_layout);
2534 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2535 * @mirror_list: Previously allocated mirror list.
2537 * Free memory allocated for @mirror_list.
2541 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2543 struct mirror_args *next_mirror = NULL;
2545 while (mirror_list != NULL) {
2546 next_mirror = mirror_list->m_next;
2547 lfs_mirror_free(mirror_list);
2548 mirror_list = next_mirror;
2560 LFS_COMP_NO_VERIFY_OPT,
2562 LFS_MIRROR_FLAGS_OPT,
2564 LFS_MIRROR_STATE_OPT,
2566 LFS_MIRROR_INDEX_OPT,
2570 static int lfs_setstripe_internal(int argc, char **argv,
2571 enum setstripe_origin opc)
2573 struct lfs_setstripe_args lsa = { 0 };
2574 struct llapi_stripe_param *param = NULL;
2575 struct find_param migrate_mdt_param = {
2585 unsigned long long size_units = 1;
2586 bool migrate_mode = false;
2587 bool migrate_mdt_mode = false;
2588 bool migration_block = false;
2589 __u64 migration_flags = 0;
2590 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2591 int comp_del = 0, comp_set = 0;
2594 struct llapi_layout *layout = NULL;
2595 struct llapi_layout **lpp = &layout;
2596 bool mirror_mode = false;
2597 bool has_m_file = false;
2598 __u32 mirror_count = 0;
2599 enum mirror_flags mirror_flags = 0;
2600 struct mirror_args *mirror_list = NULL;
2601 struct mirror_args *new_mirror = NULL;
2602 struct mirror_args *last_mirror = NULL;
2603 __u16 mirror_id = 0;
2605 bool from_yaml = false;
2606 bool from_copy = false;
2607 char *template = NULL;
2609 struct option long_opts[] = {
2610 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2611 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2612 /* --block is only valid in migrate mode */
2613 { .val = 'b', .name = "block", .has_arg = no_argument },
2614 { .val = LFS_COMP_ADD_OPT,
2615 .name = "comp-add", .has_arg = no_argument },
2616 { .val = LFS_COMP_ADD_OPT,
2617 .name = "component-add", .has_arg = no_argument },
2618 { .val = LFS_COMP_DEL_OPT,
2619 .name = "comp-del", .has_arg = no_argument },
2620 { .val = LFS_COMP_DEL_OPT,
2621 .name = "component-del", .has_arg = no_argument },
2622 { .val = LFS_COMP_FLAGS_OPT,
2623 .name = "comp-flags", .has_arg = required_argument },
2624 { .val = LFS_COMP_FLAGS_OPT,
2625 .name = "component-flags",
2626 .has_arg = required_argument },
2627 { .val = LFS_COMP_SET_OPT,
2628 .name = "comp-set", .has_arg = no_argument },
2629 { .val = LFS_COMP_SET_OPT,
2630 .name = "component-set",
2631 .has_arg = no_argument},
2632 { .val = LFS_COMP_NO_VERIFY_OPT,
2633 .name = "no-verify", .has_arg = no_argument},
2634 { .val = LFS_MIRROR_FLAGS_OPT,
2635 .name = "flags", .has_arg = required_argument},
2636 { .val = LFS_MIRROR_ID_OPT,
2637 .name = "mirror-id", .has_arg = required_argument},
2638 { .val = LFS_LAYOUT_COPY,
2639 .name = "copy", .has_arg = required_argument},
2640 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2641 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2642 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2643 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2644 { .val = 'd', .name = "delete", .has_arg = no_argument},
2645 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2646 /* --non-direct is only valid in migrate mode */
2647 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2648 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2649 { .val = 'E', .name = "component-end",
2650 .has_arg = required_argument},
2651 { .val = 'f', .name = "file", .has_arg = required_argument },
2652 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2653 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2654 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2655 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2656 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2657 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2658 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2659 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2660 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2661 { .val = 'L', .name = "layout", .has_arg = required_argument },
2662 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2663 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2664 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2665 /* --non-block is only valid in migrate mode */
2666 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2667 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2668 { .val = 'o', .name = "ost", .has_arg = required_argument },
2669 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2670 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2671 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2673 { .val = 'p', .name = "pool", .has_arg = required_argument },
2674 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2675 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2676 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2677 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2678 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2679 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2680 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2681 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2682 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2683 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2684 /* --verbose is only valid in migrate mode */
2685 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2686 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2689 setstripe_args_init(&lsa);
2691 migrate_mode = (opc == SO_MIGRATE);
2692 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2694 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2696 while ((c = getopt_long(argc, argv,
2697 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2703 case LFS_COMP_ADD_OPT:
2706 case LFS_COMP_DEL_OPT:
2709 case LFS_COMP_FLAGS_OPT:
2710 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2711 &lsa.lsa_comp_neg_flags);
2714 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2715 fprintf(stderr, "%s: inverted flags are not supported\n",
2719 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2721 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2728 case LFS_COMP_SET_OPT:
2731 case LFS_COMP_NO_VERIFY_OPT:
2732 mirror_flags |= MF_NO_VERIFY;
2734 case LFS_MIRROR_ID_OPT:
2735 mirror_id = strtoul(optarg, &end, 0);
2736 if (*end != '\0' || mirror_id == 0) {
2738 "%s %s: invalid mirror ID '%s'\n",
2739 progname, argv[0], optarg);
2743 case LFS_MIRROR_FLAGS_OPT: {
2746 if (!mirror_mode || !last_mirror) {
2747 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2752 result = comp_str2flags(optarg, &last_mirror->m_flags,
2758 fprintf(stderr, "%s: inverted flags are not supported\n",
2763 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2765 "%s: unsupported mirror flags: %s\n",
2772 case LFS_LAYOUT_COPY:
2777 if (!migrate_mode) {
2779 "%s %s: -b|--block valid only for migrate command\n",
2783 migration_block = true;
2786 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2789 "%s %s: invalid stripe count '%s'\n",
2790 progname, argv[0], optarg);
2794 if (lsa.lsa_stripe_count == -1)
2795 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2798 /* delete the default striping pattern */
2800 if (opc == SO_MIRROR_SPLIT) {
2803 "%s %s: -d cannot used with -f\n",
2807 mirror_flags |= MF_DESTROY;
2811 if (!migrate_mode) {
2813 "%s %s: -D|--non-direct is valid "
2814 "only for migrate command\n",
2818 migration_flags |= MIGRATION_NONDIRECT;
2821 if (lsa.lsa_comp_end != 0) {
2822 result = comp_args_to_layout(lpp, &lsa, true);
2825 "%s %s: invalid layout\n",
2830 setstripe_args_init_inherit(&lsa);
2833 if (arg_is_eof(optarg)) {
2834 lsa.lsa_comp_end = LUSTRE_EOF;
2836 result = llapi_parse_size(optarg,
2841 "%s %s: invalid component end '%s'\n",
2842 progname, argv[0], optarg);
2848 if (!migrate_mode) {
2849 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2853 lsa.lsa_pattern = check_hashtype(optarg);
2854 if (lsa.lsa_pattern == 0) {
2856 "%s %s: bad stripe hash type '%s'\n",
2857 progname, argv[0], optarg);
2862 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2865 "%s %s: invalid stripe offset '%s'\n",
2866 progname, argv[0], optarg);
2869 if (lsa.lsa_stripe_off == -1)
2870 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2873 comp_id = strtoul(optarg, &end, 0);
2874 if (*end != '\0' || comp_id == 0 ||
2875 comp_id > LCME_ID_MAX) {
2877 "%s %s: invalid component ID '%s'\n",
2878 progname, argv[0], optarg);
2883 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2885 "error: %s: invalid option: %s\n",
2886 progname, argv[optopt + 1]);
2889 if (opc == SO_MIRROR_EXTEND) {
2890 if (last_mirror == NULL) {
2892 "error: %s: '-N' must exist in front of '%s'\n",
2893 progname, argv[optopt + 1]);
2896 last_mirror->m_file = optarg;
2897 last_mirror->m_count = 1;
2900 if (mirror_list == NULL)
2901 mirror_list = lfs_mirror_alloc();
2902 mirror_list->m_file = optarg;
2907 if (strcmp(argv[optind - 1], "mdt") == 0) {
2908 /* Can be only the first component */
2909 if (layout != NULL) {
2911 fprintf(stderr, "error: 'mdt' layout "
2912 "can be only the first one\n");
2915 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2917 fprintf(stderr, "error: 'mdt' layout "
2918 "size is too big\n");
2921 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2922 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2924 fprintf(stderr, "error: layout '%s' is "
2925 "unknown, supported layouts are: "
2926 "'mdt', 'raid0'\n", argv[optind]);
2931 if (!migrate_mode) {
2933 "%s %s: -m|--mdt-index is valid only for migrate command\n",
2937 migrate_mdt_mode = true;
2938 lsa.lsa_nr_tgts = parse_targets(tgts,
2939 sizeof(tgts) / sizeof(__u32),
2940 lsa.lsa_nr_tgts, optarg);
2941 if (lsa.lsa_nr_tgts < 0) {
2943 "%s %s: invalid MDT target(s) '%s'\n",
2944 progname, argv[0], optarg);
2948 lsa.lsa_tgts = tgts;
2949 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2950 lsa.lsa_stripe_off = tgts[0];
2953 if (!migrate_mode) {
2955 "%s %s: -n|--non-block valid only for migrate command\n",
2959 migration_flags |= MIGRATION_NONBLOCK;
2962 if (opc == SO_SETSTRIPE) {
2963 opc = SO_MIRROR_CREATE;
2967 if (optarg != NULL) {
2968 mirror_count = strtoul(optarg, &end, 0);
2969 if (*end != '\0' || mirror_count == 0) {
2971 "error: %s: bad mirror count: %s\n",
2978 new_mirror = lfs_mirror_alloc();
2979 new_mirror->m_count = mirror_count;
2981 if (mirror_list == NULL)
2982 mirror_list = new_mirror;
2984 if (last_mirror != NULL) {
2985 /* wrap up last mirror */
2986 if (lsa.lsa_comp_end == 0)
2987 lsa.lsa_comp_end = LUSTRE_EOF;
2989 result = comp_args_to_layout(lpp, &lsa, true);
2991 lfs_mirror_free(new_mirror);
2995 setstripe_args_init_inherit(&lsa);
2997 last_mirror->m_next = new_mirror;
3000 last_mirror = new_mirror;
3001 lpp = &last_mirror->m_layout;
3004 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3005 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3006 fprintf(stderr, "warning: '--ost-list' is "
3007 "deprecated, use '--ost' instead\n");
3009 lsa.lsa_nr_tgts = parse_targets(tgts,
3010 sizeof(tgts) / sizeof(__u32),
3011 lsa.lsa_nr_tgts, optarg);
3012 if (lsa.lsa_nr_tgts < 0) {
3014 "%s %s: invalid OST target(s) '%s'\n",
3015 progname, argv[0], optarg);
3019 lsa.lsa_tgts = tgts;
3020 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3021 lsa.lsa_stripe_off = tgts[0];
3026 lsa.lsa_pool_name = optarg;
3028 if (strlen(lsa.lsa_pool_name) == 0 ||
3029 strncmp(lsa.lsa_pool_name, "none",
3030 LOV_MAXPOOLNAME) == 0)
3031 lsa.lsa_pool_name = NULL;
3034 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3038 "%s %s: invalid stripe size '%s'\n",
3039 progname, argv[0], optarg);
3044 if (!migrate_mode) {
3046 "%s %s: -v|--verbose valid only for migrate command\n",
3050 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3051 migration_flags = MIGRATION_VERBOSE;
3058 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3059 progname, argv[0], argv[optind - 1]);
3064 fname = argv[optind];
3066 if (optind == argc) {
3067 fprintf(stderr, "%s %s: FILE must be specified\n",
3072 if (mirror_mode && mirror_count == 0) {
3074 "error: %s: --mirror-count|-N option is required\n",
3081 if (lsa.lsa_comp_end == 0)
3082 lsa.lsa_comp_end = LUSTRE_EOF;
3085 if (lsa.lsa_comp_end != 0) {
3086 result = comp_args_to_layout(lpp, &lsa, true);
3091 if (mirror_flags & MF_NO_VERIFY) {
3092 if (opc != SO_MIRROR_EXTEND) {
3094 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3098 } else if (!has_m_file) {
3100 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3107 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3108 * altered by user space tool, so we don't need to support the
3109 * --component-set for this moment. */
3110 if (comp_set && !comp_id) {
3111 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3116 if ((delete + comp_set + comp_del + comp_add) > 1) {
3118 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3123 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3124 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3126 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3131 if ((comp_set || comp_del) &&
3132 (setstripe_args_specified(&lsa) || layout != NULL)) {
3134 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3139 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3141 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3146 if (comp_add || comp_del) {
3149 result = lstat(fname, &st);
3150 if (result == 0 && S_ISDIR(st.st_mode)) {
3152 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3158 fprintf(stderr, "error: %s: can't use --component-add "
3159 "or --component-del for mirror operation\n",
3166 if (layout == NULL) {
3168 "%s %s: option -E must be specified with --component-add\n",
3173 result = adjust_first_extent(fname, layout);
3174 if (result == -ENODATA)
3176 else if (result != 0)
3180 if (from_yaml && from_copy) {
3182 "%s: can't specify --yaml and --copy together\n",
3187 if ((from_yaml || from_copy) &&
3188 (setstripe_args_specified(&lsa) || layout != NULL)) {
3189 fprintf(stderr, "error: %s: can't specify --yaml with "
3190 "-c, -S, -i, -o, -p or -E options.\n",
3195 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3197 "%s %s: options --non-block and --block are mutually exclusive\n",
3202 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3205 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3210 if (migrate_mdt_mode) {
3211 struct lmv_user_md *lmu;
3213 /* initialize migrate mdt parameters */
3214 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3215 LMV_USER_MAGIC_SPECIFIC));
3218 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3219 progname, argv[0], strerror(ENOMEM));
3223 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3224 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3225 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3227 "%s %s: migrate should specify MDT index\n",
3232 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3233 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3234 lmu->lum_hash_type = lsa.lsa_pattern;
3236 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3237 if (lsa.lsa_pool_name)
3238 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3239 sizeof(lmu->lum_pool_name));
3240 if (lsa.lsa_nr_tgts > 1) {
3243 if (lsa.lsa_stripe_count > 0 &&
3244 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3245 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3247 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3248 progname, lsa.lsa_stripe_count,
3254 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3255 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3256 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3257 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3259 lmu->lum_magic = LMV_USER_MAGIC;
3262 migrate_mdt_param.fp_lmv_md = lmu;
3263 migrate_mdt_param.fp_migrate = 1;
3264 } else if (layout == NULL) {
3265 /* initialize stripe parameters */
3266 param = calloc(1, offsetof(typeof(*param),
3267 lsp_osts[lsa.lsa_nr_tgts]));
3268 if (param == NULL) {
3270 "%s %s: cannot allocate memory for parameters: %s\n",
3271 progname, argv[0], strerror(ENOMEM));
3276 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3277 param->lsp_stripe_size = lsa.lsa_stripe_size;
3278 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3279 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3280 param->lsp_stripe_count = -1;
3282 param->lsp_stripe_count = lsa.lsa_stripe_count;
3284 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3285 param->lsp_stripe_offset = -1;
3287 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3288 param->lsp_pool = lsa.lsa_pool_name;
3289 param->lsp_is_specific = false;
3290 if (lsa.lsa_nr_tgts > 0) {
3291 if (lsa.lsa_stripe_count > 0 &&
3292 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3293 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3294 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3295 fprintf(stderr, "error: %s: stripe count %lld "
3296 "doesn't match the number of OSTs: %d\n"
3297 , argv[0], lsa.lsa_stripe_count,
3303 param->lsp_is_specific = true;
3304 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3305 memcpy(param->lsp_osts, tgts,
3306 sizeof(*tgts) * lsa.lsa_nr_tgts);
3311 /* generate a layout from a YAML template */
3312 result = lfs_comp_create_from_yaml(template, &layout,
3315 fprintf(stderr, "error: %s: can't create composite "
3316 "layout from template file %s\n",
3320 } else if (from_copy) {
3321 layout = llapi_layout_get_by_path(template, 0);
3322 if (layout == NULL) {
3324 "%s: can't create composite layout from file %s.\n",
3325 progname, template);
3330 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3331 if (migrate_mdt_mode) {
3332 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3333 } else if (migrate_mode) {
3334 result = lfs_migrate(fname, migration_flags, param,
3336 } else if (comp_set != 0) {
3337 result = lfs_component_set(fname, comp_id,
3339 lsa.lsa_comp_neg_flags);
3340 } else if (comp_del != 0) {
3341 result = lfs_component_del(fname, comp_id,
3343 lsa.lsa_comp_neg_flags);
3344 } else if (comp_add != 0) {
3345 result = lfs_component_add(fname, layout);
3346 } else if (opc == SO_MIRROR_CREATE) {
3347 result = mirror_create(fname, mirror_list);
3348 } else if (opc == SO_MIRROR_EXTEND) {
3349 result = mirror_extend(fname, mirror_list,
3351 } else if (opc == SO_MIRROR_SPLIT) {
3352 if (mirror_id == 0 && comp_id == 0) {
3354 "%s %s: no mirror id or component id is specified\n",
3359 comp_id = mirror_id;
3361 mirror_flags |= MF_COMP_ID;
3362 result = mirror_split(fname, comp_id, mirror_flags,
3363 has_m_file ? mirror_list->m_file :
3365 } else if (layout != NULL) {
3366 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3373 result = llapi_file_open_param(fname,
3382 /* Save the first error encountered. */
3390 free(migrate_mdt_param.fp_lmv_md);
3391 llapi_layout_free(layout);
3392 lfs_mirror_list_free(mirror_list);
3397 llapi_layout_free(layout);
3398 lfs_mirror_list_free(mirror_list);
3402 static int lfs_poollist(int argc, char **argv)
3407 return llapi_poollist(argv[1]);
3410 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3415 char *endptr = "AD";
3420 else if (str[0] == '-')
3426 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3427 long long val = strtoll(timebuf, &endptr, 0);
3432 unit *= 52; /* 52 weeks + 1 day below */
3433 case 'w': /* fallthrough */
3435 case '\0': /* days are default unit if none used */
3436 case 'd': /* fallthrough */
3437 unit = (unit + (*endptr == 'y')) * 24;
3438 case 'h': /* fallthrough */
3440 case 'm': /* fallthrough */
3442 case 's': /* fallthrough */
3444 /* don't need to multiply by 1 for seconds */
3447 "%s find: bad time string '%s': %s\n",
3448 progname, timebuf, strerror(EINVAL));
3452 if (param->fp_time_margin == 0 ||
3453 (*endptr && unit < param->fp_time_margin))
3454 param->fp_time_margin = unit;
3461 fprintf(stderr, "%s find: bad time '%s': too large\n",
3471 static int name2uid(unsigned int *id, const char *name)
3473 struct passwd *passwd;
3475 passwd = getpwnam(name);
3478 *id = passwd->pw_uid;
3483 static int name2gid(unsigned int *id, const char *name)
3485 struct group *group;
3487 group = getgrnam(name);
3490 *id = group->gr_gid;
3495 static inline int name2projid(unsigned int *id, const char *name)
3500 static int uid2name(char **name, unsigned int id)
3502 struct passwd *passwd;
3504 passwd = getpwuid(id);
3507 *name = passwd->pw_name;
3512 static inline int gid2name(char **name, unsigned int id)
3514 struct group *group;
3516 group = getgrgid(id);
3519 *name = group->gr_name;
3524 static int name2layout(__u32 *layout, char *name)
3526 char *ptr, *layout_name;
3529 for (ptr = name; ; ptr = NULL) {
3530 layout_name = strtok(ptr, ",");
3531 if (layout_name == NULL)
3533 if (strcmp(layout_name, "released") == 0)
3534 *layout |= LOV_PATTERN_F_RELEASED;
3535 else if (strcmp(layout_name, "raid0") == 0)
3536 *layout |= LOV_PATTERN_RAID0;
3537 else if (strcmp(layout_name, "mdt") == 0)
3538 *layout |= LOV_PATTERN_MDT;
3545 static int lfs_find(int argc, char **argv)
3550 struct find_param param = {
3553 .fp_time_margin = 24 * 60 * 60,
3555 struct option long_opts[] = {
3556 { .val = 'A', .name = "atime", .has_arg = required_argument },
3557 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3558 { .val = LFS_COMP_COUNT_OPT,
3559 .name = "comp-count", .has_arg = required_argument },
3560 { .val = LFS_COMP_COUNT_OPT,
3561 .name = "component-count",
3562 .has_arg = required_argument },
3563 { .val = LFS_COMP_FLAGS_OPT,
3564 .name = "comp-flags", .has_arg = required_argument },
3565 { .val = LFS_COMP_FLAGS_OPT,
3566 .name = "component-flags",
3567 .has_arg = required_argument },
3568 { .val = LFS_COMP_START_OPT,
3569 .name = "comp-start", .has_arg = required_argument },
3570 { .val = LFS_COMP_START_OPT,
3571 .name = "component-start",
3572 .has_arg = required_argument },
3573 { .val = LFS_MIRROR_STATE_OPT,
3574 .name = "mirror-state", .has_arg = required_argument },
3575 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3576 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3577 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3578 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3579 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3580 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3581 { .val = 'E', .name = "component-end",
3582 .has_arg = required_argument },
3583 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3584 { .val = 'g', .name = "gid", .has_arg = required_argument },
3585 { .val = 'G', .name = "group", .has_arg = required_argument },
3586 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3587 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3588 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3589 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3590 { .val = 'L', .name = "layout", .has_arg = required_argument },
3591 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3592 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3593 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3594 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3595 { .val = 'n', .name = "name", .has_arg = required_argument },
3596 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3597 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3598 { .val = 'O', .name = "obd", .has_arg = required_argument },
3599 { .val = 'O', .name = "ost", .has_arg = required_argument },
3600 /* no short option for pool yet, can be 'p' after 2.18 */
3601 { .val = LFS_POOL_OPT,
3602 .name = "pool", .has_arg = required_argument },
3603 { .val = '0', .name = "print0", .has_arg = no_argument },
3604 { .val = 'P', .name = "print", .has_arg = no_argument },
3605 { .val = LFS_PROJID_OPT,
3606 .name = "projid", .has_arg = required_argument },
3607 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3608 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3609 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3610 { .val = 's', .name = "size", .has_arg = required_argument },
3611 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3612 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3613 { .val = 't', .name = "type", .has_arg = required_argument },
3614 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3615 { .val = 'u', .name = "uid", .has_arg = required_argument },
3616 { .val = 'U', .name = "user", .has_arg = required_argument },
3617 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3618 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3619 { .val = 'z', .name = "lazy", .has_arg = no_argument },
3631 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3632 while ((c = getopt_long_only(argc, argv,
3633 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz",
3634 long_opts, NULL)) >= 0) {
3639 /* '!' is part of option */
3640 /* when getopt_long_only() finds a string which is not
3641 * an option nor a known option argument it returns 1
3642 * in that case if we already have found pathstart and pathend
3643 * (i.e. we have the list of pathnames),
3644 * the only supported value is "!"
3646 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3647 if (!isoption && pathend != -1) {
3648 fprintf(stderr, "err: %s: filename|dirname must either "
3649 "precede options or follow options\n",
3654 if (!isoption && pathstart == -1)
3655 pathstart = optind - 1;
3656 if (isoption && pathstart != -1 && pathend == -1)
3657 pathend = optind - 2;
3663 /* unknown; opt is "!" or path component,
3664 * checking done above.
3666 if (strcmp(optarg, "!") == 0)
3670 xtime = ¶m.fp_atime;
3671 xsign = ¶m.fp_asign;
3672 param.fp_exclude_atime = !!neg_opt;
3673 /* no break, this falls through to 'C' for ctime */
3676 xtime = ¶m.fp_ctime;
3677 xsign = ¶m.fp_csign;
3678 param.fp_exclude_ctime = !!neg_opt;
3680 /* no break, this falls through to 'M' for mtime */
3683 xtime = ¶m.fp_mtime;
3684 xsign = ¶m.fp_msign;
3685 param.fp_exclude_mtime = !!neg_opt;
3687 rc = set_time(¶m, &t, xtime, optarg);
3688 if (rc == LONG_MAX) {
3696 if (optarg[0] == '+') {
3697 param.fp_blocks_sign = -1;
3699 } else if (optarg[0] == '-') {
3700 param.fp_blocks_sign = 1;
3704 param.fp_blocks_units = 1024;
3705 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3706 ¶m.fp_blocks_units, 0);
3708 fprintf(stderr, "error: bad blocks '%s'\n",
3712 param.fp_check_blocks = 1;
3713 param.fp_exclude_blocks = !!neg_opt;
3715 case LFS_COMP_COUNT_OPT:
3716 if (optarg[0] == '+') {
3717 param.fp_comp_count_sign = -1;
3719 } else if (optarg[0] == '-') {
3720 param.fp_comp_count_sign = 1;
3724 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3725 if (*endptr != '\0') {
3726 fprintf(stderr, "error: bad component count "
3730 param.fp_check_comp_count = 1;
3731 param.fp_exclude_comp_count = !!neg_opt;
3733 case LFS_COMP_FLAGS_OPT:
3734 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3735 ¶m.fp_comp_neg_flags);
3737 fprintf(stderr, "error: bad component flags "
3741 param.fp_check_comp_flags = 1;
3743 __u32 flags = param.fp_comp_neg_flags;
3744 param.fp_comp_neg_flags = param.fp_comp_flags;
3745 param.fp_comp_flags = flags;
3748 case LFS_COMP_START_OPT:
3749 if (optarg[0] == '+') {
3750 param.fp_comp_start_sign = -1;
3752 } else if (optarg[0] == '-') {
3753 param.fp_comp_start_sign = 1;
3757 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3758 ¶m.fp_comp_start_units, 0);
3760 fprintf(stderr, "error: bad component start "
3764 param.fp_check_comp_start = 1;
3765 param.fp_exclude_comp_start = !!neg_opt;
3767 case LFS_MIRROR_STATE_OPT:
3768 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3769 ¶m.fp_mirror_neg_state);
3772 "error: bad mirrored file state '%s'\n",
3776 param.fp_check_mirror_state = 1;
3778 __u16 state = param.fp_mirror_neg_state;
3779 param.fp_mirror_neg_state =
3780 param.fp_mirror_state;
3781 param.fp_mirror_state = state;
3785 if (optarg[0] == '+') {
3786 param.fp_stripe_count_sign = -1;
3788 } else if (optarg[0] == '-') {
3789 param.fp_stripe_count_sign = 1;
3793 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3794 if (*endptr != '\0') {
3795 fprintf(stderr,"error: bad stripe_count '%s'\n",
3800 param.fp_check_stripe_count = 1;
3801 param.fp_exclude_stripe_count = !!neg_opt;
3804 param.fp_max_depth = strtol(optarg, 0, 0);
3807 if (optarg[0] == '+') {
3808 param.fp_comp_end_sign = -1;
3810 } else if (optarg[0] == '-') {
3811 param.fp_comp_end_sign = 1;
3815 if (arg_is_eof(optarg)) {
3816 param.fp_comp_end = LUSTRE_EOF;
3817 param.fp_comp_end_units = 1;
3820 rc = llapi_parse_size(optarg,
3822 ¶m.fp_comp_end_units, 0);
3825 fprintf(stderr, "error: bad component end "
3829 param.fp_check_comp_end = 1;
3830 param.fp_exclude_comp_end = !!neg_opt;
3834 rc = name2gid(¶m.fp_gid, optarg);
3836 param.fp_gid = strtoul(optarg, &endptr, 10);
3837 if (*endptr != '\0') {
3838 fprintf(stderr, "Group/GID: %s cannot "
3839 "be found.\n", optarg);
3844 param.fp_exclude_gid = !!neg_opt;
3845 param.fp_check_gid = 1;
3848 param.fp_hash_type = check_hashtype(optarg);
3849 if (param.fp_hash_type == 0) {
3850 fprintf(stderr, "error: bad hash_type '%s'\n",
3855 param.fp_check_hash_type = 1;
3856 param.fp_exclude_hash_type = !!neg_opt;
3859 ret = name2layout(¶m.fp_layout, optarg);
3862 param.fp_exclude_layout = !!neg_opt;
3863 param.fp_check_layout = 1;
3867 rc = name2uid(¶m.fp_uid, optarg);
3869 param.fp_uid = strtoul(optarg, &endptr, 10);
3870 if (*endptr != '\0') {
3871 fprintf(stderr, "User/UID: %s cannot "
3872 "be found.\n", optarg);
3877 param.fp_exclude_uid = !!neg_opt;
3878 param.fp_check_uid = 1;
3881 param.fp_pattern = (char *)optarg;
3882 param.fp_exclude_pattern = !!neg_opt;
3885 if (optarg[0] == '+') {
3886 param.fp_mirror_count_sign = -1;
3888 } else if (optarg[0] == '-') {
3889 param.fp_mirror_count_sign = 1;
3893 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3894 if (*endptr != '\0') {
3896 "error: bad mirror count '%s'\n",
3900 param.fp_check_mirror_count = 1;
3901 param.fp_exclude_mirror_count = !!neg_opt;
3906 char *buf, *token, *next, *p;
3910 buf = strdup(optarg);
3916 param.fp_exclude_obd = !!neg_opt;
3919 while (token && *token) {
3920 token = strchr(token, ',');
3927 param.fp_exclude_mdt = !!neg_opt;
3928 param.fp_num_alloc_mdts += len;
3929 tmp = realloc(param.fp_mdt_uuid,
3930 param.fp_num_alloc_mdts *
3931 sizeof(*param.fp_mdt_uuid));
3937 param.fp_mdt_uuid = tmp;
3939 param.fp_exclude_obd = !!neg_opt;
3940 param.fp_num_alloc_obds += len;
3941 tmp = realloc(param.fp_obd_uuid,
3942 param.fp_num_alloc_obds *
3943 sizeof(*param.fp_obd_uuid));
3949 param.fp_obd_uuid = tmp;
3951 for (token = buf; token && *token; token = next) {
3952 struct obd_uuid *puuid;
3955 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3958 ¶m.fp_obd_uuid[param.fp_num_obds++];
3960 p = strchr(token, ',');
3967 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3972 strncpy(puuid->uuid, token,
3973 sizeof(puuid->uuid));
3980 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3984 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3986 "Pool name %s is too long (max %d)\n",
3987 optarg, LOV_MAXPOOLNAME);
3992 * We do check for empty pool because empty pool
3993 * is used to find V1 LOV attributes
3995 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3996 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3997 param.fp_exclude_pool = !!neg_opt;
3998 param.fp_check_pool = 1;
4000 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4001 case 'p': /* want this for --pool, to match getstripe/find */
4003 "warning: -p deprecated, use --print0 or -0\n");
4006 param.fp_zero_end = 1;
4008 case 'P': /* we always print, this option is a no-op */
4010 case LFS_PROJID_OPT:
4011 rc = name2projid(¶m.fp_projid, optarg);
4013 param.fp_projid = strtoul(optarg, &endptr, 10);
4014 if (*endptr != '\0') {
4016 "Invalid project ID: %s",
4022 param.fp_exclude_projid = !!neg_opt;
4023 param.fp_check_projid = 1;
4026 if (optarg[0] == '+') {
4027 param.fp_size_sign = -1;
4029 } else if (optarg[0] == '-') {
4030 param.fp_size_sign = 1;
4034 ret = llapi_parse_size(optarg, ¶m.fp_size,
4035 ¶m.fp_size_units, 0);
4037 fprintf(stderr, "error: bad file size '%s'\n",
4041 param.fp_check_size = 1;
4042 param.fp_exclude_size = !!neg_opt;
4045 if (optarg[0] == '+') {
4046 param.fp_stripe_size_sign = -1;
4048 } else if (optarg[0] == '-') {
4049 param.fp_stripe_size_sign = 1;
4053 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4054 ¶m.fp_stripe_size_units, 0);
4056 fprintf(stderr, "error: bad stripe_size '%s'\n",
4060 param.fp_check_stripe_size = 1;
4061 param.fp_exclude_stripe_size = !!neg_opt;
4064 param.fp_exclude_type = !!neg_opt;
4065 switch (optarg[0]) {
4067 param.fp_type = S_IFBLK;
4070 param.fp_type = S_IFCHR;
4073 param.fp_type = S_IFDIR;
4076 param.fp_type = S_IFREG;
4079 param.fp_type = S_IFLNK;
4082 param.fp_type = S_IFIFO;
4085 param.fp_type = S_IFSOCK;
4088 fprintf(stderr, "error: %s: bad type '%s'\n",
4095 if (optarg[0] == '+') {
4096 param.fp_mdt_count_sign = -1;
4098 } else if (optarg[0] == '-') {
4099 param.fp_mdt_count_sign = 1;
4103 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4104 if (*endptr != '\0') {
4105 fprintf(stderr, "error: bad mdt_count '%s'\n",
4110 param.fp_check_mdt_count = 1;
4111 param.fp_exclude_mdt_count = !!neg_opt;
4122 if (pathstart == -1) {
4123 fprintf(stderr, "error: %s: no filename|pathname\n",
4127 } else if (pathend == -1) {
4133 rc = llapi_find(argv[pathstart], ¶m);
4134 if (rc != 0 && ret == 0)
4136 } while (++pathstart < pathend);
4139 fprintf(stderr, "error: %s failed for %s.\n",
4140 argv[0], argv[optind - 1]);
4142 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4143 free(param.fp_obd_uuid);
4145 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4146 free(param.fp_mdt_uuid);
4151 static int lfs_getstripe_internal(int argc, char **argv,
4152 struct find_param *param)
4154 struct option long_opts[] = {
4155 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4156 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4157 { .val = LFS_COMP_COUNT_OPT,
4158 .name = "comp-count", .has_arg = no_argument },
4159 { .val = LFS_COMP_COUNT_OPT,
4160 .name = "component-count", .has_arg = no_argument },
4161 { .val = LFS_COMP_FLAGS_OPT,
4162 .name = "comp-flags", .has_arg = optional_argument },
4163 { .val = LFS_COMP_FLAGS_OPT,
4164 .name = "component-flags", .has_arg = optional_argument },
4165 { .val = LFS_COMP_START_OPT,
4166 .name = "comp-start", .has_arg = optional_argument },
4167 { .val = LFS_COMP_START_OPT,
4168 .name = "component-start", .has_arg = optional_argument },
4169 { .val = LFS_MIRROR_INDEX_OPT,
4170 .name = "mirror-index", .has_arg = required_argument },
4171 { .val = LFS_MIRROR_ID_OPT,
4172 .name = "mirror-id", .has_arg = required_argument },
4173 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4174 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4175 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4176 { .val = 'd', .name = "directory", .has_arg = no_argument },
4177 { .val = 'D', .name = "default", .has_arg = no_argument },
4178 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4179 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4180 { .val = 'F', .name = "fid", .has_arg = no_argument },
4181 { .val = 'g', .name = "generation", .has_arg = no_argument },
4182 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4183 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4184 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4185 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4186 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4187 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4188 { .val = 'L', .name = "layout", .has_arg = no_argument },
4189 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4190 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4191 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4192 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4193 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4194 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4195 { .val = 'O', .name = "obd", .has_arg = required_argument },
4196 { .val = 'O', .name = "ost", .has_arg = required_argument },
4197 { .val = 'p', .name = "pool", .has_arg = no_argument },
4198 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4199 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4200 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4201 { .val = 'R', .name = "raw", .has_arg = no_argument },
4202 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4203 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4204 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4205 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4206 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4207 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4208 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4209 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4213 int pathstart = -1, pathend = -1;
4217 while ((c = getopt_long(argc, argv,
4218 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4219 long_opts, NULL)) != -1) {
4223 /* '!' is part of option */
4224 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4225 if (!isoption && pathend != -1) {
4227 "error: %s: filename|dirname must either precede options or follow options\n",
4231 if (!isoption && pathstart == -1)
4232 pathstart = optind - 1;
4233 if (isoption && pathstart != -1 && pathend == -1)
4234 pathend = optind - 2;
4238 /* unknown: opt is "!" */
4239 if (strcmp(optarg, "!") == 0)
4243 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4244 param->fp_verbose |= VERBOSE_COUNT;
4245 param->fp_max_depth = 0;
4248 case LFS_COMP_COUNT_OPT:
4249 param->fp_verbose |= VERBOSE_COMP_COUNT;
4250 param->fp_max_depth = 0;
4252 case LFS_COMP_FLAGS_OPT:
4253 if (optarg != NULL) {
4254 rc = comp_str2flags(optarg,
4255 ¶m->fp_comp_flags,
4256 ¶m->fp_comp_neg_flags);
4258 fprintf(stderr, "error: %s bad "
4259 "component flags '%s'.\n",
4263 param->fp_check_comp_flags = 1;
4265 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4266 param->fp_max_depth = 0;
4269 case LFS_COMP_START_OPT:
4270 if (optarg != NULL) {
4272 if (tmp[0] == '+') {
4273 param->fp_comp_start_sign = -1;
4275 } else if (tmp[0] == '-') {
4276 param->fp_comp_start_sign = 1;
4279 rc = llapi_parse_size(tmp,
4280 ¶m->fp_comp_start,
4281 ¶m->fp_comp_start_units, 0);
4283 fprintf(stderr, "error: %s bad "
4284 "component start '%s'.\n",
4288 param->fp_check_comp_start = 1;
4291 param->fp_verbose |= VERBOSE_COMP_START;
4292 param->fp_max_depth = 0;
4295 case LFS_MIRROR_INDEX_OPT:
4296 if (optarg[0] == '+') {
4297 param->fp_mirror_index_sign = -1;
4299 } else if (optarg[0] == '-') {
4300 param->fp_mirror_index_sign = 1;
4304 param->fp_mirror_index = strtoul(optarg, &end, 0);
4305 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4306 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4308 "%s %s: invalid mirror index '%s'\n",
4309 progname, argv[0], optarg);
4312 if (param->fp_mirror_id != 0) {
4314 "%s %s: can't specify both mirror index and mirror ID\n",
4318 param->fp_check_mirror_index = 1;
4319 param->fp_exclude_mirror_index = !!neg_opt;
4321 case LFS_MIRROR_ID_OPT:
4322 if (optarg[0] == '+') {
4323 param->fp_mirror_id_sign = -1;
4325 } else if (optarg[0] == '-') {
4326 param->fp_mirror_id_sign = 1;
4330 param->fp_mirror_id = strtoul(optarg, &end, 0);
4331 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4332 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4334 "%s %s: invalid mirror ID '%s'\n",
4335 progname, argv[0], optarg);
4338 if (param->fp_mirror_index != 0) {
4340 "%s %s: can't specify both mirror index and mirror ID\n",
4344 param->fp_check_mirror_id = 1;
4345 param->fp_exclude_mirror_id = !!neg_opt;
4348 param->fp_max_depth = 0;
4351 param->fp_get_default_lmv = 1;
4354 if (optarg != NULL) {
4356 if (tmp[0] == '+') {
4357 param->fp_comp_end_sign = -1;
4359 } else if (tmp[0] == '-') {
4360 param->fp_comp_end_sign = 1;
4364 if (arg_is_eof(tmp)) {
4365 param->fp_comp_end = LUSTRE_EOF;
4366 param->fp_comp_end_units = 1;
4369 rc = llapi_parse_size(tmp,
4370 ¶m->fp_comp_end,
4371 ¶m->fp_comp_end_units, 0);
4374 fprintf(stderr, "error: %s bad "
4375 "component end '%s'.\n",
4379 param->fp_check_comp_end = 1;
4381 param->fp_verbose |= VERBOSE_COMP_END;
4382 param->fp_max_depth = 0;
4386 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4387 param->fp_verbose |= VERBOSE_DFID;
4388 param->fp_max_depth = 0;
4392 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4393 param->fp_verbose |= VERBOSE_GENERATION;
4394 param->fp_max_depth = 0;
4398 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4399 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4400 param->fp_max_depth = 0;
4404 if (optarg != NULL) {
4405 param->fp_comp_id = strtoul(optarg, &end, 0);
4406 if (*end != '\0' || param->fp_comp_id == 0 ||
4407 param->fp_comp_id > LCME_ID_MAX) {
4408 fprintf(stderr, "error: %s bad "
4409 "component id '%s'\n",
4413 param->fp_check_comp_id = 1;
4416 param->fp_max_depth = 0;
4417 param->fp_verbose |= VERBOSE_COMP_ID;
4421 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4422 param->fp_verbose |= VERBOSE_PATTERN;
4423 param->fp_max_depth = 0;
4426 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4428 fprintf(stderr, "warning: '-M' deprecated"
4429 ", use '--mdt-index' or '-m' instead\n");
4432 if (!(param->fp_verbose & VERBOSE_DETAIL))
4433 param->fp_max_depth = 0;
4434 param->fp_verbose |= VERBOSE_MDTINDEX;
4437 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4438 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4439 param->fp_max_depth = 0;
4443 if (param->fp_obd_uuid) {
4445 "error: %s: only one obduuid allowed",
4449 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4452 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4453 param->fp_verbose |= VERBOSE_POOL;
4454 param->fp_max_depth = 0;
4461 param->fp_recursive = 1;
4467 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4468 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4469 param->fp_max_depth = 0;
4473 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4483 if (pathstart == -1) {
4484 fprintf(stderr, "error: %s: no filename|pathname\n",
4487 } else if (pathend == -1) {
4495 if (param->fp_recursive)
4496 param->fp_max_depth = -1;
4497 else if (param->fp_verbose & VERBOSE_DETAIL)
4498 param->fp_max_depth = 1;
4500 if (!param->fp_verbose)
4501 param->fp_verbose = VERBOSE_DEFAULT;
4502 if (param->fp_quiet)
4503 param->fp_verbose = VERBOSE_OBJID;
4506 rc = llapi_getstripe(argv[pathstart], param);
4507 } while (++pathstart < pathend && !rc);
4510 fprintf(stderr, "error: %s failed for %s.\n",
4511 argv[0], argv[optind - 1]);
4515 static int lfs_tgts(int argc, char **argv)
4517 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4518 struct find_param param;
4519 int index = 0, rc=0;
4524 if (argc == 2 && !realpath(argv[1], path)) {
4526 fprintf(stderr, "error: invalid path '%s': %s\n",
4527 argv[1], strerror(-rc));
4531 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4532 /* Check if we have a mount point */
4533 if (mntdir[0] == '\0')
4536 memset(¶m, 0, sizeof(param));
4537 if (!strcmp(argv[0], "mdts"))
4538 param.fp_get_lmv = 1;
4540 rc = llapi_ostlist(mntdir, ¶m);
4542 fprintf(stderr, "error: %s: failed on %s\n",
4545 if (path[0] != '\0')
4547 memset(mntdir, 0, PATH_MAX);
4553 static int lfs_getstripe(int argc, char **argv)
4555 struct find_param param = { 0 };
4557 param.fp_max_depth = 1;
4558 return lfs_getstripe_internal(argc, argv, ¶m);
4562 static int lfs_getdirstripe(int argc, char **argv)
4564 struct find_param param = { 0 };
4565 struct option long_opts[] = {
4566 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4567 { .val = 'D', .name = "default", .has_arg = no_argument },
4568 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4569 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4570 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4571 { .val = 'O', .name = "obd", .has_arg = required_argument },
4572 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4573 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4574 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4578 param.fp_get_lmv = 1;
4580 while ((c = getopt_long(argc, argv,
4581 "cDHimO:rtTy", long_opts, NULL)) != -1)
4586 param.fp_verbose |= VERBOSE_COUNT;
4589 param.fp_get_default_lmv = 1;
4593 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4595 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4597 fprintf(stderr, "warning: '-t' deprecated, "
4598 "use '--mdt-hash' or '-H' instead\n");
4601 param.fp_verbose |= VERBOSE_HASH_TYPE;
4604 if (param.fp_obd_uuid) {
4606 "error: %s: only one obduuid allowed",
4610 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4613 param.fp_recursive = 1;
4626 if (param.fp_recursive)
4627 param.fp_max_depth = -1;
4629 if (!param.fp_verbose)
4630 param.fp_verbose = VERBOSE_DEFAULT;
4633 rc = llapi_getstripe(argv[optind], ¶m);
4634 } while (++optind < argc && !rc);
4637 fprintf(stderr, "error: %s failed for %s.\n",
4638 argv[0], argv[optind - 1]);
4643 MNTDF_INODES = 0x0001,
4644 MNTDF_COOKED = 0x0002,
4645 MNTDF_LAZY = 0x0004,
4646 MNTDF_VERBOSE = 0x0008,
4647 MNTDF_SHOW = 0x0010,
4650 #define COOK(value) \
4653 while (value > 1024) { \
4661 #define CDF "%11llu"
4662 #define HDF "%8.1f%c"
4666 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4668 double avail, used, ratio = 0;
4671 avail = st->os_ffree;
4672 used = st->os_files - st->os_ffree;
4674 avail = st->os_bavail;
4675 used = st->os_blocks - st->os_bfree;
4677 if (avail + used > 0)
4678 ratio = used / (used + avail) * 100;
4680 /* Round up to match df(1) usage percentage */
4681 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4684 static int showdf(char *mntdir, struct obd_statfs *stat,
4685 char *uuid, enum mntdf_flags flags,
4686 char *type, int index, int rc)
4688 long long avail, used, total;
4690 char *suffix = "KMGTPEZY";
4691 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4692 char tbuf[3 * sizeof(__u64)];
4693 char ubuf[3 * sizeof(__u64)];
4694 char abuf[3 * sizeof(__u64)];
4695 char rbuf[3 * sizeof(__u64)];
4702 if (flags & MNTDF_INODES) {
4703 avail = stat->os_ffree;
4704 used = stat->os_files - stat->os_ffree;
4705 total = stat->os_files;
4707 int shift = flags & MNTDF_COOKED ? 0 : 10;
4709 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4710 used = ((stat->os_blocks - stat->os_bfree) *
4711 stat->os_bsize) >> shift;
4712 total = (stat->os_blocks * stat->os_bsize) >> shift;
4715 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4717 if (flags & MNTDF_COOKED) {
4721 cook_val = (double)total;
4724 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4727 snprintf(tbuf, sizeof(tbuf), CDF, total);
4729 cook_val = (double)used;
4732 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4735 snprintf(ubuf, sizeof(ubuf), CDF, used);
4737 cook_val = (double)avail;
4740 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4743 snprintf(abuf, sizeof(abuf), CDF, avail);
4745 snprintf(tbuf, sizeof(tbuf), CDF, total);
4746 snprintf(ubuf, sizeof(tbuf), CDF, used);
4747 snprintf(abuf, sizeof(tbuf), CDF, avail);
4750 sprintf(rbuf, RDF, ratio);
4751 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4752 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4754 printf("[%s:%d]", type, index);
4756 if (stat->os_state) {
4758 * Each character represents the matching
4761 const char state_names[] = "DRSI";
4766 for (i = 0, state = stat->os_state;
4767 state && i < sizeof(state_names); i++) {
4768 if (!(state & (1 << i)))
4770 printf("%c", state_names[i]);
4778 printf(UUF": inactive device\n", uuid);
4781 printf(UUF": %s\n", uuid, strerror(-rc));
4788 struct ll_stat_type {
4793 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4795 struct ll_statfs_data {
4797 struct obd_statfs sd_st;
4800 struct ll_statfs_buf {
4802 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4805 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4806 int ops, struct ll_statfs_buf *lsb)
4808 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4809 struct obd_uuid uuid_buf;
4810 char *poolname = NULL;
4811 struct ll_stat_type types[] = {
4812 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4813 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4814 { .st_name = NULL } };
4815 struct ll_stat_type *tp;
4816 __u64 ost_ffree = 0;
4824 poolname = strchr(pool, '.');
4825 if (poolname != NULL) {
4826 if (strncmp(fsname, pool, strlen(fsname))) {
4827 fprintf(stderr, "filesystem name incorrect\n");
4835 fd = open(mntdir, O_RDONLY);
4838 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4843 if (flags & MNTDF_SHOW) {
4844 if (flags & MNTDF_INODES)
4845 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4846 "UUID", "Inodes", "IUsed", "IFree",
4847 "IUse%", "Mounted on");
4849 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4851 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4852 "Used", "Available", "Use%", "Mounted on");
4855 for (tp = types; tp->st_name != NULL; tp++) {
4856 bool have_ost = false;
4858 if (!(tp->st_op & ops))
4861 for (index = 0; ; index++) {
4862 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4863 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4864 type = flags & MNTDF_LAZY ?
4865 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4866 rc2 = llapi_obd_fstatfs(fd, type, index,
4867 &stat_buf, &uuid_buf);
4872 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4873 if (!(flags & MNTDF_VERBOSE))
4875 } else if (rc2 < 0 && rc == 0) {
4879 /* If we have OSTs then don't report MDT block counts.
4880 * For MDT-only filesystems the expectation is that all
4881 * layouts have a DoM component. For filesystems with
4882 * OSTs, files are not necessarily going to store data
4883 * on MDTs, and MDT space is limited to a fraction of
4884 * OST space, so don't include it in the summary.
4886 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
4893 if (poolname && tp->st_op == LL_STATFS_LOV &&
4894 llapi_search_ost(fsname, poolname,
4895 obd_uuid2str(&uuid_buf)) != 1)
4898 /* the llapi_obd_statfs() call may have returned with
4899 * an error, but if it filled in uuid_buf we will at
4900 * lease use that to print out a message for that OBD.
4901 * If we didn't get anything in the uuid_buf, then fill
4902 * it in so that we can print an error message. */
4903 if (uuid_buf.uuid[0] == '\0')
4904 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4905 "%s%04x", tp->st_name, index);
4907 lsb->sb_buf[lsb->sb_count].sd_index = index;
4908 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4911 if (flags & MNTDF_SHOW)
4912 showdf(mntdir, &stat_buf,
4913 obd_uuid2str(&uuid_buf), flags,
4914 tp->st_name, index, rc2);
4919 if (tp->st_op == LL_STATFS_LMV) {
4920 sum.os_ffree += stat_buf.os_ffree;
4921 sum.os_files += stat_buf.os_files;
4922 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4923 ost_ffree += stat_buf.os_ffree;
4925 sum.os_blocks += stat_buf.os_blocks *
4927 sum.os_bfree += stat_buf.os_bfree *
4929 sum.os_bavail += stat_buf.os_bavail *
4936 /* If we don't have as many objects free on the OST as inodes
4937 * on the MDS, we reduce the total number of inodes to
4938 * compensate, so that the "inodes in use" number is correct.
4939 * Matches ll_statfs_internal() so the results are consistent. */
4940 if (ost_ffree < sum.os_ffree) {
4941 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4942 sum.os_ffree = ost_ffree;
4944 if (flags & MNTDF_SHOW) {
4946 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4953 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4955 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4957 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4959 int r1 = obd_statfs_ratio(st1, false);
4960 int r2 = obd_statfs_ratio(st2, false);
4961 int64_t result = r1 - r2;
4963 /* if both space usage are above 90, compare free inodes */
4964 if (r1 > 90 && r2 > 90)
4965 result = st2->os_ffree - st1->os_ffree;
4969 else if (result == 0)
4976 static int lfs_setdirstripe(int argc, char **argv)
4980 struct lfs_setstripe_args lsa = { 0 };
4981 struct llapi_stripe_param *param = NULL;
4982 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4985 char *mode_opt = NULL;
4986 bool default_stripe = false;
4987 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4988 mode_t previous_mode = 0;
4989 bool delete = false;
4990 struct ll_statfs_buf *lsb = NULL;
4991 char mntdir[PATH_MAX] = "";
4992 bool auto_distributed = false;
4994 struct option long_opts[] = {
4995 { .val = 'c', .name = "count", .has_arg = required_argument },
4996 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4997 { .val = 'd', .name = "delete", .has_arg = no_argument },
4998 { .val = 'D', .name = "default", .has_arg = no_argument },
4999 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5000 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5001 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5002 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5003 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5005 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5006 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5008 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5009 { .val = 'i', .name = "index", .has_arg = required_argument },
5011 { .val = 'o', .name = "mode", .has_arg = required_argument },
5012 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5013 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5015 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5016 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5019 setstripe_args_init(&lsa);
5021 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
5029 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5032 "%s %s: invalid stripe count '%s'\n",
5033 progname, argv[0], optarg);
5039 default_stripe = true;
5042 default_stripe = true;
5044 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5046 fprintf(stderr, "warning: '--hash-type' and '-t' "
5047 "deprecated, use '--mdt-hash' or '-H' instead\n");
5050 lsa.lsa_pattern = check_hashtype(optarg);
5051 if (lsa.lsa_pattern == 0) {
5053 "%s %s: bad stripe hash type '%s'\n",
5054 progname, argv[0], optarg);
5059 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5062 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5063 if (strcmp(argv[optind - 1], "--index") == 0)
5065 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5068 lsa.lsa_nr_tgts = parse_targets(mdts,
5069 sizeof(mdts) / sizeof(__u32),
5070 lsa.lsa_nr_tgts, optarg);
5071 if (lsa.lsa_nr_tgts < 0) {
5073 "%s %s: invalid MDT target(s) '%s'\n",
5074 progname, argv[0], optarg);
5078 lsa.lsa_tgts = mdts;
5079 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5080 lsa.lsa_stripe_off = mdts[0];
5082 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5084 fprintf(stderr, "warning: '-m' is deprecated, "
5085 "use '--mode' or '-o' instead\n");
5091 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5092 progname, argv[0], argv[optind - 1]);
5097 if (optind == argc) {
5098 fprintf(stderr, "%s %s: DIR must be specified\n",
5103 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5104 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5106 "%s %s: stripe offset and count must be specified\n",
5112 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5113 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5115 "%s %s: cannot specify -d with -c or -i options\n",
5120 if (mode_opt != NULL) {
5121 mode = strtoul(mode_opt, &end, 8);
5124 "%s %s: bad MODE '%s'\n",
5125 progname, argv[0], mode_opt);
5128 previous_mode = umask(0);
5132 * initialize stripe parameters, in case param is converted to specific,
5133 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5135 param = calloc(1, offsetof(typeof(*param),
5136 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5137 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5138 if (param == NULL) {
5140 "%s %s: cannot allocate memory for parameters: %s\n",
5141 progname, argv[0], strerror(ENOMEM));
5145 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5146 param->lsp_stripe_count = lsa.lsa_stripe_count;
5147 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5148 param->lsp_stripe_offset = -1;
5150 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5151 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5152 param->lsp_stripe_pattern = lsa.lsa_pattern;
5154 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5155 param->lsp_pool = lsa.lsa_pool_name;
5156 param->lsp_is_specific = false;
5157 if (lsa.lsa_nr_tgts > 1) {
5158 if (lsa.lsa_stripe_count > 0 &&
5159 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5160 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5161 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5162 "match the number of MDTs: %d\n",
5163 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5168 param->lsp_is_specific = true;
5169 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5170 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5173 dname = argv[optind];
5175 if (default_stripe) {
5176 result = llapi_dir_set_default_lmv(dname, param);
5178 /* if current \a dname isn't under the same \a mntdir
5179 * as the last one, and the last one was
5180 * auto-distributed, restore \a param.
5182 if (mntdir[0] != '\0' &&
5183 strncmp(dname, mntdir, strlen(mntdir)) &&
5185 param->lsp_is_specific = false;
5186 param->lsp_stripe_offset = -1;
5187 auto_distributed = false;
5190 if (!param->lsp_is_specific &&
5191 param->lsp_stripe_offset == -1) {
5192 char path[PATH_MAX] = "";
5195 lsb = malloc(sizeof(*lsb));
5203 /* use mntdir for dirname() temporarily */
5204 strncpy(mntdir, dname, sizeof(mntdir));
5205 if (!realpath(dirname(mntdir), path)) {
5208 "error: invalid path '%s': %s\n",
5209 argv[optind], strerror(errno));
5214 result = llapi_search_mounts(path, 0, mntdir,
5216 if (result < 0 || mntdir[0] == '\0') {
5218 "No suitable Lustre mount found\n");
5222 result = mntdf(mntdir, NULL, NULL, 0,
5223 LL_STATFS_LMV, lsb);
5227 if (param->lsp_stripe_count > lsb->sb_count) {
5229 "error: stripe count %d is too big\n",
5230 param->lsp_stripe_count);
5235 qsort(lsb->sb_buf, lsb->sb_count,
5236 sizeof(struct ll_statfs_data),
5237 ll_statfs_data_comp);
5239 auto_distributed = true;
5242 if (auto_distributed) {
5244 int nr = MAX(param->lsp_stripe_count,
5247 /* don't use server whose usage is above 90% */
5248 while (nr != param->lsp_stripe_count &&
5249 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5251 nr = MAX(param->lsp_stripe_count,
5254 /* get \a r between [0, nr) */
5257 param->lsp_stripe_offset =
5258 lsb->sb_buf[r].sd_index;
5259 if (param->lsp_stripe_count > 1) {
5262 param->lsp_is_specific = true;
5263 for (; i < param->lsp_stripe_count; i++)
5264 param->lsp_tgts[(i + r) % nr] =
5265 lsb->sb_buf[i].sd_index;
5269 result = llapi_dir_create(dname, mode, param);
5274 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5275 progname, dname, strerror(-result));
5278 dname = argv[++optind];
5279 } while (dname != NULL);
5281 if (mode_opt != NULL)
5282 umask(previous_mode);
5290 static int lfs_rmentry(int argc, char **argv)
5297 fprintf(stderr, "error: %s: missing dirname\n",
5303 dname = argv[index];
5304 while (dname != NULL) {
5305 result = llapi_direntry_remove(dname);
5307 fprintf(stderr, "error: %s: remove dir entry '%s' "
5308 "failed\n", argv[0], dname);
5311 dname = argv[++index];
5316 static int lfs_mv(int argc, char **argv)
5318 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5319 struct find_param param = {
5326 struct option long_opts[] = {
5327 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5328 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5329 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5332 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5334 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5336 fprintf(stderr, "warning: '-M' deprecated"
5337 ", use '--mdt-index' or '-m' instead\n");
5340 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5342 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5348 param.fp_verbose = VERBOSE_DETAIL;
5351 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5352 progname, argv[optind - 1]);
5357 if (lmu.lum_stripe_offset == -1) {
5358 fprintf(stderr, "%s mv: MDT index must be specified\n",
5363 if (optind >= argc) {
5364 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5369 /* initialize migrate mdt parameters */
5370 param.fp_lmv_md = &lmu;
5371 param.fp_migrate = 1;
5372 rc = llapi_migrate_mdt(argv[optind], ¶m);
5374 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5375 progname, argv[optind], param.fp_mdt_index,
5380 static int lfs_osts(int argc, char **argv)
5382 return lfs_tgts(argc, argv);
5385 static int lfs_mdts(int argc, char **argv)
5387 return lfs_tgts(argc, argv);
5390 static int lfs_df(int argc, char **argv)
5392 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5393 enum mntdf_flags flags = MNTDF_SHOW;
5394 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5395 int c, rc = 0, index = 0;
5396 char fsname[PATH_MAX] = "", *pool_name = NULL;
5397 struct option long_opts[] = {
5398 { .val = 'h', .name = "human-readable",
5399 .has_arg = no_argument },
5400 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5401 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5402 { .val = 'p', .name = "pool", .has_arg = required_argument },
5403 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5406 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5409 flags |= MNTDF_COOKED;
5412 flags |= MNTDF_INODES;
5415 flags |= MNTDF_LAZY;
5421 flags |= MNTDF_VERBOSE;
5427 if (optind < argc && !realpath(argv[optind], path)) {
5429 fprintf(stderr, "error: invalid path '%s': %s\n",
5430 argv[optind], strerror(-rc));
5434 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5435 /* Check if we have a mount point */
5436 if (mntdir[0] == '\0')
5439 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5440 if (rc || path[0] != '\0')
5442 fsname[0] = '\0'; /* avoid matching in next loop */
5443 mntdir[0] = '\0'; /* avoid matching in next loop */
5449 static int lfs_getname(int argc, char **argv)
5451 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5452 int rc = 0, index = 0, c;
5453 char buf[sizeof(struct obd_uuid)];
5455 while ((c = getopt(argc, argv, "h")) != -1)
5458 if (optind == argc) { /* no paths specified, get all paths. */
5459 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5460 rc = llapi_getname(mntdir, buf, sizeof(buf));
5463 "cannot get name for `%s': %s\n",
5464 mntdir, strerror(-rc));
5468 printf("%s %s\n", buf, mntdir);
5470 path[0] = fsname[0] = mntdir[0] = 0;
5472 } else { /* paths specified, only attempt to search these. */
5473 for (; optind < argc; optind++) {
5474 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5477 "cannot get name for `%s': %s\n",
5478 argv[optind], strerror(-rc));
5482 printf("%s %s\n", buf, argv[optind]);
5488 static int lfs_check(int argc, char **argv)
5490 char mntdir[PATH_MAX] = {'\0'};
5499 fprintf(stderr, "%s check: server type must be specified\n",
5504 obd_types[0] = obd_type1;
5505 obd_types[1] = obd_type2;
5506 obd_types[2] = obd_type3;
5508 if (strcmp(argv[1], "osts") == 0) {
5509 strcpy(obd_types[0], "osc");
5510 } else if (strcmp(argv[1], "mdts") == 0 ||
5511 strcmp(argv[1], "mds") == 0) {
5512 strcpy(obd_types[0], "mdc");
5513 } else if (strcmp(argv[1], "mgts") == 0) {
5514 strcpy(obd_types[0], "mgc");
5515 } else if (strcmp(argv[1], "all") == 0 ||
5516 strcmp(argv[1], "servers") == 0) {
5518 strcpy(obd_types[0], "osc");
5519 strcpy(obd_types[1], "mdc");
5520 strcpy(obd_types[2], "mgc");
5522 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5527 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5528 if (rc < 0 || mntdir[0] == '\0') {
5530 "%s check: cannot find mounted Lustre filesystem: %s\n",
5531 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5535 rc = llapi_target_check(num_types, obd_types, mntdir);
5537 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5538 progname, argv[1], strerror(-rc));
5544 #ifdef HAVE_SYS_QUOTA_H
5545 #define ARG2INT(nr, str, msg) \
5548 nr = strtol(str, &endp, 0); \
5549 if (*endp != '\0') { \
5550 fprintf(stderr, "%s: bad %s '%s'\n", \
5551 progname, msg, str); \
5556 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5558 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5559 * returns the value or ULONG_MAX on integer overflow or incorrect format
5561 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5562 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5563 * 3. empty integer value is interpreted as 0
5565 static unsigned long str2sec(const char* timestr)
5567 const char spec[] = "smhdw";
5568 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5569 unsigned long val = 0;
5572 if (strpbrk(timestr, spec) == NULL) {
5573 /* no specifiers inside the time string,
5574 should treat it as an integer value */
5575 val = strtoul(timestr, &tail, 10);
5576 return *tail ? ULONG_MAX : val;
5579 /* format string is XXwXXdXXhXXmXXs */
5585 v = strtoul(timestr, &tail, 10);
5586 if (v == ULONG_MAX || *tail == '\0')
5587 /* value too large (ULONG_MAX or more)
5588 or missing specifier */
5591 ptr = strchr(spec, *tail);
5593 /* unknown specifier */
5598 /* check if product will overflow the type */
5599 if (!(v < ULONG_MAX / mult[ind]))
5602 ADD_OVERFLOW(val, mult[ind] * v);
5603 if (val == ULONG_MAX)
5615 #define ARG2ULL(nr, str, def_units) \
5617 unsigned long long limit, units = def_units; \
5620 rc = llapi_parse_size(str, &limit, &units, 1); \
5622 fprintf(stderr, "%s: invalid limit '%s'\n", \
5629 static inline int has_times_option(int argc, char **argv)
5633 for (i = 1; i < argc; i++)
5634 if (!strcmp(argv[i], "-t"))
5640 int lfs_setquota_times(int argc, char **argv)
5643 struct if_quotactl qctl;
5644 char *mnt, *obd_type = (char *)qctl.obd_type;
5645 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5646 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5647 struct option long_opts[] = {
5648 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5649 { .val = 'g', .name = "group", .has_arg = no_argument },
5650 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5651 { .val = 'p', .name = "projid", .has_arg = no_argument },
5652 { .val = 't', .name = "times", .has_arg = no_argument },
5653 { .val = 'u', .name = "user", .has_arg = no_argument },
5657 memset(&qctl, 0, sizeof(qctl));
5658 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5659 qctl.qc_type = ALLQUOTA;
5661 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5662 long_opts, NULL)) != -1) {
5673 if (qctl.qc_type != ALLQUOTA) {
5674 fprintf(stderr, "error: -u/g/p can't be used "
5675 "more than once\n");
5678 qctl.qc_type = qtype;
5681 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5682 fprintf(stderr, "error: bad block-grace: %s\n",
5686 dqb->dqb_valid |= QIF_BTIME;
5689 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5690 fprintf(stderr, "error: bad inode-grace: %s\n",
5694 dqb->dqb_valid |= QIF_ITIME;
5696 case 't': /* Yes, of course! */
5698 default: /* getopt prints error message for us when opterr != 0 */
5703 if (qctl.qc_type == ALLQUOTA) {
5704 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5708 if (optind != argc - 1) {
5709 fprintf(stderr, "error: unexpected parameters encountered\n");
5713 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5714 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5715 fprintf(stderr, "error: grace time is too large\n");
5720 rc = llapi_quotactl(mnt, &qctl);
5723 fprintf(stderr, "%s %s ", obd_type,
5724 obd_uuid2str(&qctl.obd_uuid));
5725 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5732 #define BSLIMIT (1 << 0)
5733 #define BHLIMIT (1 << 1)
5734 #define ISLIMIT (1 << 2)
5735 #define IHLIMIT (1 << 3)
5737 int lfs_setquota(int argc, char **argv)
5740 struct if_quotactl qctl;
5741 char *mnt, *obd_type = (char *)qctl.obd_type;
5742 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5743 struct option long_opts[] = {
5744 { .val = 'b', .name = "block-softlimit",
5745 .has_arg = required_argument },
5746 { .val = 'B', .name = "block-hardlimit",
5747 .has_arg = required_argument },
5748 { .val = 'd', .name = "default", .has_arg = no_argument },
5749 { .val = 'g', .name = "group", .has_arg = required_argument },
5750 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5751 { .val = 'i', .name = "inode-softlimit",
5752 .has_arg = required_argument },
5753 { .val = 'I', .name = "inode-hardlimit",
5754 .has_arg = required_argument },
5755 { .val = 'p', .name = "projid", .has_arg = required_argument },
5756 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5757 { .val = 'u', .name = "user", .has_arg = required_argument },
5758 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5760 unsigned limit_mask = 0;
5762 bool use_default = false;
5765 if (has_times_option(argc, argv))
5766 return lfs_setquota_times(argc, argv);
5768 memset(&qctl, 0, sizeof(qctl));
5769 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5770 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5771 * so it can be used as a marker that qc_type
5772 * isn't reinitialized from command line */
5774 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5775 long_opts, NULL)) != -1) {
5778 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5781 goto quota_type_def;
5784 rc = name2uid(&qctl.qc_id, optarg);
5787 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5790 goto quota_type_def;
5793 rc = name2gid(&qctl.qc_id, optarg);
5796 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5799 goto quota_type_def;
5802 rc = name2projid(&qctl.qc_id, optarg);
5805 qctl.qc_id = strtoul(optarg, &endptr, 10);
5806 if (*endptr != '\0') {
5807 fprintf(stderr, "%s setquota: invalid"
5808 " id '%s'\n", progname, optarg);
5813 if (qctl.qc_id == 0) {
5814 fprintf(stderr, "%s setquota: can't set quota"
5815 " for root usr/group/project.\n",
5821 if (qctl.qc_type != ALLQUOTA) {
5823 "%s setquota: only one of -u, -U, -g,"
5824 " -G, -p or -P may be specified\n",
5828 qctl.qc_type = qtype;
5831 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5835 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5836 dqb->dqb_bsoftlimit >>= 10;
5837 limit_mask |= BSLIMIT;
5838 if (dqb->dqb_bsoftlimit &&
5839 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5841 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5842 "See '%s help setquota' or Lustre manual for details\n",
5843 progname, dqb->dqb_bsoftlimit,
5847 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5848 dqb->dqb_bhardlimit >>= 10;
5849 limit_mask |= BHLIMIT;
5850 if (dqb->dqb_bhardlimit &&
5851 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5853 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5854 "See '%s help setquota' or Lustre manual for details\n",
5855 progname, dqb->dqb_bhardlimit,
5859 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5860 limit_mask |= ISLIMIT;
5861 if (dqb->dqb_isoftlimit &&
5862 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5864 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5865 "See '%s help setquota' or Lustre manual for details\n",
5866 progname, dqb->dqb_isoftlimit,
5870 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5871 limit_mask |= IHLIMIT;
5872 if (dqb->dqb_ihardlimit &&
5873 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5875 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5876 "See '%s help setquota' or Lustre manual for details\n",
5877 progname, dqb->dqb_ihardlimit,
5882 "%s setquota: unrecognized option '%s'\n",
5883 progname, argv[optind - 1]);
5888 if (qctl.qc_type == ALLQUOTA) {
5890 "%s setquota: either -u or -g must be specified\n",
5895 if (!use_default && limit_mask == 0) {
5897 "%s setquota: at least one limit must be specified\n",
5902 if (use_default && limit_mask != 0) {
5904 "%s setquota: limits should not be specified when"
5905 " using default quota\n",
5910 if (use_default && qctl.qc_id == 0) {
5912 "%s setquota: can not set default quota for root"
5913 " user/group/project\n",
5918 if (optind != argc - 1) {
5920 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5921 progname, argv[optind]);
5928 dqb->dqb_bhardlimit = 0;
5929 dqb->dqb_bsoftlimit = 0;
5930 dqb->dqb_ihardlimit = 0;
5931 dqb->dqb_isoftlimit = 0;
5934 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5935 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5936 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5937 /* sigh, we can't just set blimits/ilimits */
5938 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5939 .qc_type = qctl.qc_type,
5940 .qc_id = qctl.qc_id};
5942 rc = llapi_quotactl(mnt, &tmp_qctl);
5946 if (!(limit_mask & BHLIMIT))
5947 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5948 if (!(limit_mask & BSLIMIT))
5949 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5950 if (!(limit_mask & IHLIMIT))
5951 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5952 if (!(limit_mask & ISLIMIT))
5953 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5955 /* Keep grace times if we have got no softlimit arguments */
5956 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5957 dqb->dqb_valid |= QIF_BTIME;
5958 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5961 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5962 dqb->dqb_valid |= QIF_ITIME;
5963 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5967 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5968 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5970 rc = llapi_quotactl(mnt, &qctl);
5974 "%s setquota: cannot quotactl '%s' '%s': %s",
5976 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5983 /* Converts seconds value into format string
5984 * result is returned in buf
5986 * 1. result is in descenting order: 1w2d3h4m5s
5987 * 2. zero fields are not filled (except for p. 3): 5d1s
5988 * 3. zero seconds value is presented as "0s"
5990 static char * __sec2str(time_t seconds, char *buf)
5992 const char spec[] = "smhdw";
5993 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5998 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5999 c = seconds / mult[i];
6001 if (c > 0 || (i == 0 && buf == tail))
6002 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
6010 static void sec2str(time_t seconds, char *buf, int rc)
6017 tail = __sec2str(seconds, tail);
6019 if (rc && tail - buf < 39) {
6025 static void diff2str(time_t seconds, char *buf, time_t now)
6031 if (seconds <= now) {
6032 strcpy(buf, "none");
6035 __sec2str(seconds - now, buf);
6038 static void print_quota_title(char *name, struct if_quotactl *qctl,
6039 bool human_readable, bool show_default)
6042 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6043 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6044 "Filesystem", "bquota", "blimit", "bgrace",
6045 "iquota", "ilimit", "igrace");
6047 printf("Disk quotas for %s %s (%cid %u):\n",
6048 qtype_name(qctl->qc_type), name,
6049 *qtype_name(qctl->qc_type), qctl->qc_id);
6050 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6051 "Filesystem", human_readable ? "used" : "kbytes",
6052 "quota", "limit", "grace",
6053 "files", "quota", "limit", "grace");
6057 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6060 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6063 snprintf(buf, buflen, "%5.4gP",
6064 (double)num / ((__u64)1 << 40));
6066 snprintf(buf, buflen, "%5.4gT",
6067 (double)num / (1 << 30));
6069 snprintf(buf, buflen, "%5.4gG",
6070 (double)num / (1 << 20));
6072 snprintf(buf, buflen, "%5.4gM",
6073 (double)num / (1 << 10));
6075 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6079 #define STRBUF_LEN 32
6080 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6081 int rc, bool h, bool show_default)
6087 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6088 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6089 int bover = 0, iover = 0;
6090 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6091 char numbuf[3][STRBUF_LEN];
6093 char strbuf[STRBUF_LEN];
6095 if (dqb->dqb_bhardlimit &&
6096 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6098 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6099 if (dqb->dqb_btime > now) {
6106 if (dqb->dqb_ihardlimit &&
6107 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6109 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6110 if (dqb->dqb_itime > now) {
6118 if (strlen(mnt) > 15)
6119 printf("%s\n%15s", mnt, "");
6121 printf("%15s", mnt);
6124 diff2str(dqb->dqb_btime, timebuf, now);
6125 else if (show_default)
6126 snprintf(timebuf, sizeof(timebuf), "%llu",
6129 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6130 strbuf, sizeof(strbuf), h);
6131 if (rc == -EREMOTEIO)
6132 sprintf(numbuf[0], "%s*", strbuf);
6134 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6135 "%s" : "[%s]", strbuf);
6137 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6138 if (type == QC_GENERAL)
6139 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6140 "%s" : "[%s]", strbuf);
6142 sprintf(numbuf[1], "%s", "-");
6144 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6145 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6146 "%s" : "[%s]", strbuf);
6149 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6151 printf(" %7s%c %6s %7s %7s",
6152 numbuf[0], bover ? '*' : ' ', numbuf[1],
6153 numbuf[2], bover > 1 ? timebuf : "-");
6157 diff2str(dqb->dqb_itime, timebuf, now);
6158 else if (show_default)
6159 snprintf(timebuf, sizeof(timebuf), "%llu",
6162 snprintf(numbuf[0], sizeof(numbuf),
6163 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6164 (uintmax_t)dqb->dqb_curinodes);
6166 if (type == QC_GENERAL)
6167 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6169 (uintmax_t)dqb->dqb_isoftlimit);
6171 sprintf(numbuf[1], "%s", "-");
6173 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6174 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6177 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6178 else if (type != QC_OSTIDX)
6179 printf(" %7s%c %6s %7s %7s",
6180 numbuf[0], iover ? '*' : ' ', numbuf[1],
6181 numbuf[2], iover > 1 ? timebuf : "-");
6183 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6186 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6187 qctl->qc_cmd == Q_GETOINFO) {
6191 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6192 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6193 printf("Block grace time: %s; Inode grace time: %s\n",
6194 bgtimebuf, igtimebuf);
6198 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6199 bool h, __u64 *total)
6201 int rc = 0, rc1 = 0, count = 0;
6202 __u32 valid = qctl->qc_valid;
6204 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6206 fprintf(stderr, "can not get %s count: %s\n",
6207 is_mdt ? "mdt": "ost", strerror(-rc));
6211 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6212 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6213 rc = llapi_quotactl(mnt, qctl);
6215 /* It is remote client case. */
6216 if (rc == -EOPNOTSUPP) {
6223 fprintf(stderr, "quotactl %s%d failed.\n",
6224 is_mdt ? "mdt": "ost", qctl->qc_idx);
6228 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6229 qctl->qc_valid, 0, h, false);
6230 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6231 qctl->qc_dqblk.dqb_bhardlimit;
6234 qctl->qc_valid = valid;
6238 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6239 int verbose, int quiet, bool human_readable,
6242 int rc1 = 0, rc2 = 0, rc3 = 0;
6243 char *obd_type = (char *)qctl->obd_type;
6244 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6245 __u64 total_ialloc = 0, total_balloc = 0;
6246 bool use_default_for_blk = false;
6247 bool use_default_for_file = false;
6250 rc1 = llapi_quotactl(mnt, qctl);
6254 fprintf(stderr, "%s quotas are not enabled.\n",
6255 qtype_name(qctl->qc_type));
6258 fprintf(stderr, "Permission denied.\n");
6261 /* We already got error message. */
6264 fprintf(stderr, "Unexpected quotactl error: %s\n",
6269 if (!show_default && qctl->qc_id == 0) {
6270 qctl->qc_dqblk.dqb_bhardlimit = 0;
6271 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6272 qctl->qc_dqblk.dqb_ihardlimit = 0;
6273 qctl->qc_dqblk.dqb_isoftlimit = 0;
6274 qctl->qc_dqblk.dqb_btime = 0;
6275 qctl->qc_dqblk.dqb_itime = 0;
6276 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6279 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6280 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6281 use_default_for_blk = true;
6282 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6285 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6286 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6287 use_default_for_file = true;
6288 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6291 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6292 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6293 print_quota_title(name, qctl, human_readable, show_default);
6295 if (rc1 && *obd_type)
6296 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6298 if (qctl->qc_valid != QC_GENERAL)
6301 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6302 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6303 (QIF_LIMITS|QIF_USAGE));
6305 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6307 if (!show_default && verbose &&
6308 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6309 char strbuf[STRBUF_LEN];
6311 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6313 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6315 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6317 printf("Total allocated inode limit: %ju, total "
6318 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6322 if (use_default_for_blk)
6323 printf("%cid %u is using default block quota setting\n",
6324 *qtype_name(qctl->qc_type), qctl->qc_id);
6326 if (use_default_for_file)
6327 printf("%cid %u is using default file quota setting\n",
6328 *qtype_name(qctl->qc_type), qctl->qc_id);
6330 if (rc1 || rc2 || rc3 || inacc)
6331 printf("Some errors happened when getting quota info. "
6332 "Some devices may be not working or deactivated. "
6333 "The data in \"[]\" is inaccurate.\n");
6339 static int lfs_project(int argc, char **argv)
6341 int ret = 0, err = 0, c, i;
6342 struct project_handle_control phc = { 0 };
6343 enum lfs_project_ops_t op;
6346 phc.assign_projid = false;
6347 /* default action */
6348 op = LFS_PROJECT_LIST;
6350 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6353 if (op != LFS_PROJECT_LIST) {
6355 "%s: cannot specify '-c' '-C' '-s' together\n",
6360 op = LFS_PROJECT_CHECK;
6363 if (op != LFS_PROJECT_LIST) {
6365 "%s: cannot specify '-c' '-C' '-s' together\n",
6370 op = LFS_PROJECT_CLEAR;
6373 if (op != LFS_PROJECT_LIST) {
6375 "%s: cannot specify '-c' '-C' '-s' together\n",
6380 phc.set_inherit = true;
6381 op = LFS_PROJECT_SET;
6387 phc.keep_projid = true;
6390 phc.recursive = true;
6393 phc.projid = strtoul(optarg, NULL, 0);
6394 phc.assign_projid = true;
6398 phc.newline = false;
6401 fprintf(stderr, "%s: invalid option '%c'\n",
6407 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6408 op = LFS_PROJECT_SET;
6409 phc.set_projid = true;
6410 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6411 phc.set_projid = true;
6415 case LFS_PROJECT_CHECK:
6416 if (phc.keep_projid) {
6418 "%s: '-k' is useless together with '-c'\n",
6423 case LFS_PROJECT_CLEAR:
6426 "%s: '-0' is useless together with '-C'\n",
6430 if (phc.assign_projid) {
6432 "%s: '-p' is useless together with '-C'\n",
6437 case LFS_PROJECT_SET:
6440 "%s: '-0' is useless together with '-s'\n",
6444 if (phc.keep_projid) {
6446 "%s: '-k' is useless together with '-s'\n",
6454 "%s: '-0' is useless for list operations\n",
6464 fprintf(stderr, "%s: missing file or directory target(s)\n",
6469 for (i = 0; i < argc; i++) {
6471 case LFS_PROJECT_CHECK:
6472 err = lfs_project_check(argv[i], &phc);
6474 case LFS_PROJECT_LIST:
6475 err = lfs_project_list(argv[i], &phc);
6477 case LFS_PROJECT_CLEAR:
6478 err = lfs_project_clear(argv[i], &phc);
6480 case LFS_PROJECT_SET:
6481 err = lfs_project_set(argv[i], &phc);
6493 static int lfs_quota(int argc, char **argv)
6496 char *mnt, *name = NULL;
6497 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6498 .qc_type = ALLQUOTA };
6499 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6500 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6502 __u32 valid = QC_GENERAL, idx = 0;
6503 bool human_readable = false;
6504 bool show_default = false;
6507 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6510 show_default = true;
6515 show_default = true;
6520 show_default = true;
6524 if (qctl.qc_type != ALLQUOTA) {
6526 "%s quota: only one of -u, -g, or -p may be specified\n",
6530 qctl.qc_type = qtype;
6533 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6536 valid = qctl.qc_valid = QC_UUID;
6537 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6540 valid = qctl.qc_valid = QC_MDTIDX;
6541 idx = qctl.qc_idx = atoi(optarg);
6542 if (idx == 0 && *optarg != '0') {
6544 "%s quota: invalid MDT index '%s'\n",
6550 valid = qctl.qc_valid = QC_OSTIDX;
6551 idx = qctl.qc_idx = atoi(optarg);
6552 if (idx == 0 && *optarg != '0') {
6554 "%s quota: invalid OST index '%s'\n",
6566 human_readable = true;
6569 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6570 progname, argv[optind - 1]);
6575 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6576 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6577 optind == argc - 1 && !show_default) {
6579 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6580 qctl.qc_valid = valid;
6583 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6584 qctl.qc_type = qtype;
6585 if (qtype == USRQUOTA) {
6586 qctl.qc_id = geteuid();
6587 rc = uid2name(&name, qctl.qc_id);
6589 qctl.qc_id = getegid();
6590 rc = gid2name(&name, qctl.qc_id);
6595 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6596 human_readable, show_default);
6601 /* lfs quota -u username /path/to/lustre/mount */
6602 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6603 /* options should be followed by u/g-name and mntpoint */
6604 if ((!show_default && optind + 2 != argc) ||
6605 (show_default && optind + 1 != argc) ||
6606 qctl.qc_type == ALLQUOTA) {
6608 "%s quota: name and mount point must be specified\n",
6613 if (!show_default) {
6614 name = argv[optind++];
6615 switch (qctl.qc_type) {
6617 rc = name2uid(&qctl.qc_id, name);
6620 rc = name2gid(&qctl.qc_id, name);
6623 rc = name2projid(&qctl.qc_id, name);
6630 qctl.qc_valid = QC_GENERAL;
6631 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6636 qctl.qc_id = strtoul(name, &endptr, 10);
6637 if (*endptr != '\0') {
6638 fprintf(stderr, "%s quota: invalid id '%s'\n",
6643 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6644 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6650 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6651 human_readable, show_default);
6654 #endif /* HAVE_SYS_QUOTA_H! */
6656 static int flushctx_ioctl(char *mp)
6660 fd = open(mp, O_RDONLY);
6662 fprintf(stderr, "flushctx: error open %s: %s\n",
6663 mp, strerror(errno));
6667 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6669 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6670 mp, strerror(errno));
6676 static int lfs_flushctx(int argc, char **argv)
6678 int kdestroy = 0, c;
6679 char mntdir[PATH_MAX] = {'\0'};
6683 while ((c = getopt(argc, argv, "k")) != -1) {
6689 fprintf(stderr, "error: %s: option '-%c' "
6690 "unrecognized\n", argv[0], c);
6696 if ((rc = system("kdestroy > /dev/null")) != 0) {
6697 rc = WEXITSTATUS(rc);
6698 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6702 if (optind >= argc) {
6703 /* flush for all mounted lustre fs. */
6704 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6705 /* Check if we have a mount point */
6706 if (mntdir[0] == '\0')
6709 if (flushctx_ioctl(mntdir))
6712 mntdir[0] = '\0'; /* avoid matching in next loop */
6715 /* flush fs as specified */
6716 while (optind < argc) {
6717 if (flushctx_ioctl(argv[optind++]))
6724 static int lfs_changelog(int argc, char **argv)
6726 void *changelog_priv;
6727 struct changelog_rec *rec;
6728 long long startrec = 0, endrec = 0;
6730 struct option long_opts[] = {
6731 { .val = 'f', .name = "follow", .has_arg = no_argument },
6733 char short_opts[] = "f";
6736 while ((rc = getopt_long(argc, argv, short_opts,
6737 long_opts, NULL)) != -1) {
6744 "%s changelog: unrecognized option '%s'\n",
6745 progname, argv[optind - 1]);
6749 if (optind >= argc) {
6750 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6755 mdd = argv[optind++];
6757 startrec = strtoll(argv[optind++], NULL, 10);
6759 endrec = strtoll(argv[optind++], NULL, 10);
6761 rc = llapi_changelog_start(&changelog_priv,
6762 CHANGELOG_FLAG_BLOCK |
6763 CHANGELOG_FLAG_JOBID |
6764 CHANGELOG_FLAG_EXTRA_FLAGS |
6765 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6768 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6769 progname, strerror(errno = -rc));
6773 rc = llapi_changelog_set_xflags(changelog_priv,
6774 CHANGELOG_EXTRA_FLAG_UIDGID |
6775 CHANGELOG_EXTRA_FLAG_NID |
6776 CHANGELOG_EXTRA_FLAG_OMODE |
6777 CHANGELOG_EXTRA_FLAG_XATTR);
6780 "%s changelog: cannot set xflags for changelog: %s\n",
6781 progname, strerror(errno = -rc));
6785 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6789 if (endrec && rec->cr_index > endrec) {
6790 llapi_changelog_free(&rec);
6793 if (rec->cr_index < startrec) {
6794 llapi_changelog_free(&rec);
6798 secs = rec->cr_time >> 30;
6799 gmtime_r(&secs, &ts);
6800 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6801 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6802 changelog_type2str(rec->cr_type),
6803 ts.tm_hour, ts.tm_min, ts.tm_sec,
6804 (int)(rec->cr_time & ((1 << 30) - 1)),
6805 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6806 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6808 if (rec->cr_flags & CLF_JOBID) {
6809 struct changelog_ext_jobid *jid =
6810 changelog_rec_jobid(rec);
6812 if (jid->cr_jobid[0] != '\0')
6813 printf(" j=%s", jid->cr_jobid);
6816 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6817 struct changelog_ext_extra_flags *ef =
6818 changelog_rec_extra_flags(rec);
6820 printf(" ef=0x%llx", ef->cr_extra_flags);
6822 if (ef->cr_extra_flags & CLFE_UIDGID) {
6823 struct changelog_ext_uidgid *uidgid =
6824 changelog_rec_uidgid(rec);
6826 printf(" u=%llu:%llu",
6827 uidgid->cr_uid, uidgid->cr_gid);
6829 if (ef->cr_extra_flags & CLFE_NID) {
6830 struct changelog_ext_nid *nid =
6831 changelog_rec_nid(rec);
6834 libcfs_nid2str(nid->cr_nid));
6837 if (ef->cr_extra_flags & CLFE_OPEN) {
6838 struct changelog_ext_openmode *omd =
6839 changelog_rec_openmode(rec);
6840 char mode[] = "---";
6842 /* exec mode must be exclusive */
6843 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6846 if (omd->cr_openflags & MDS_FMODE_READ)
6848 if (omd->cr_openflags &
6855 if (strcmp(mode, "---") != 0)
6856 printf(" m=%s", mode);
6860 if (ef->cr_extra_flags & CLFE_XATTR) {
6861 struct changelog_ext_xattr *xattr =
6862 changelog_rec_xattr(rec);
6864 if (xattr->cr_xattr[0] != '\0')
6865 printf(" x=%s", xattr->cr_xattr);
6869 if (rec->cr_namelen)
6870 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6871 rec->cr_namelen, changelog_rec_name(rec));
6873 if (rec->cr_flags & CLF_RENAME) {
6874 struct changelog_ext_rename *rnm =
6875 changelog_rec_rename(rec);
6877 if (!fid_is_zero(&rnm->cr_sfid))
6878 printf(" s="DFID" sp="DFID" %.*s",
6879 PFID(&rnm->cr_sfid),
6880 PFID(&rnm->cr_spfid),
6881 (int)changelog_rec_snamelen(rec),
6882 changelog_rec_sname(rec));
6886 llapi_changelog_free(&rec);
6889 llapi_changelog_fini(&changelog_priv);
6892 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6893 progname, strerror(errno = -rc));
6895 return (rc == 1 ? 0 : rc);
6898 static int lfs_changelog_clear(int argc, char **argv)
6906 endrec = strtoll(argv[3], NULL, 10);
6908 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6911 fprintf(stderr, "%s: record out of range: %llu\n",
6913 else if (rc == -ENOENT)
6914 fprintf(stderr, "%s: no changelog user: %s\n",
6917 fprintf(stderr, "%s error: %s\n", argv[0],
6926 static int lfs_fid2path(int argc, char **argv)
6928 struct option long_opts[] = {
6929 { .val = 'c', .name = "cur", .has_arg = no_argument },
6930 { .val = 'l', .name = "link", .has_arg = required_argument },
6931 { .val = 'r', .name = "rec", .has_arg = required_argument },
6933 char short_opts[] = "cl:r:";
6934 char *device, *fid, *path;
6935 long long recno = -1;
6940 char *endptr = NULL;
6942 while ((rc = getopt_long(argc, argv, short_opts,
6943 long_opts, NULL)) != -1) {
6949 linkno = strtol(optarg, &endptr, 10);
6950 if (*endptr != '\0') {
6952 "%s fid2path: invalid linkno '%s'\n",
6958 recno = strtoll(optarg, &endptr, 10);
6959 if (*endptr != '\0') {
6961 "%s fid2path: invalid recno '%s'\n",
6968 "%s fid2path: unrecognized option '%s'\n",
6969 progname, argv[optind - 1]);
6976 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6981 device = argv[optind++];
6982 path = calloc(1, PATH_MAX);
6986 "%s fid2path: cannot allocate memory for path: %s\n",
6987 progname, strerror(-rc));
6992 while (optind < argc) {
6993 fid = argv[optind++];
6995 lnktmp = (linkno >= 0) ? linkno : 0;
6997 int oldtmp = lnktmp;
6998 long long rectmp = recno;
7000 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7004 "%s fid2path: cannot find '%s': %s\n",
7005 progname, fid, strerror(errno = -rc2));
7012 fprintf(stdout, "%lld ", rectmp);
7013 if (device[0] == '/') {
7014 fprintf(stdout, "%s", device);
7015 if (device[strlen(device) - 1] != '/')
7016 fprintf(stdout, "/");
7017 } else if (path[0] == '\0') {
7018 fprintf(stdout, "/");
7020 fprintf(stdout, "%s\n", path);
7023 /* specified linkno */
7025 if (oldtmp == lnktmp)
7035 static int lfs_path2fid(int argc, char **argv)
7037 struct option long_opts[] = {
7038 { .val = 'p', .name = "parents", .has_arg = no_argument },
7041 const char short_opts[] = "p";
7042 const char *sep = "";
7045 bool show_parents = false;
7047 while ((rc = getopt_long(argc, argv, short_opts,
7048 long_opts, NULL)) != -1) {
7051 show_parents = true;
7055 "%s path2fid: unrecognized option '%s'\n",
7056 progname, argv[optind - 1]);
7061 if (optind > argc - 1) {
7062 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7066 else if (optind < argc - 1)
7070 for (path = argv + optind; *path != NULL; path++) {
7072 if (!show_parents) {
7073 err = llapi_path2fid(*path, &fid);
7075 printf("%s%s"DFID"\n",
7076 *sep != '\0' ? *path : "", sep,
7079 char name[NAME_MAX + 1];
7080 unsigned int linkno = 0;
7082 while ((err = llapi_path2parent(*path, linkno, &fid,
7083 name, sizeof(name))) == 0) {
7084 if (*sep != '\0' && linkno == 0)
7085 printf("%s%s", *path, sep);
7087 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7092 /* err == -ENODATA is end-of-loop */
7093 if (linkno > 0 && err == -ENODATA) {
7101 "%s path2fid: cannot get %sfid for '%s': %s\n",
7102 progname, show_parents ? "parent " : "", *path,
7114 static int lfs_data_version(int argc, char **argv)
7121 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7124 fprintf(stderr, "%s data_version: FILE must be specified\n",
7129 while ((c = getopt(argc, argv, "nrw")) != -1) {
7132 data_version_flags = 0;
7135 data_version_flags |= LL_DV_RD_FLUSH;
7138 data_version_flags |= LL_DV_WR_FLUSH;
7142 "%s data_version: unrecognized option '%s'\n",
7143 progname, argv[optind - 1]);
7147 if (optind == argc) {
7148 fprintf(stderr, "%s data_version: FILE must be specified\n",
7153 path = argv[optind];
7154 fd = open(path, O_RDONLY);
7157 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7158 progname, path, strerror(-rc));
7162 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7165 "%s data_version: cannot get version for '%s': %s\n",
7166 progname, path, strerror(-rc));
7168 printf("%ju" "\n", (uintmax_t)data_version);
7174 static int lfs_hsm_state(int argc, char **argv)
7179 struct hsm_user_state hus;
7187 rc = llapi_hsm_state_get(path, &hus);
7189 fprintf(stderr, "can't get hsm state for %s: %s\n",
7190 path, strerror(errno = -rc));
7194 /* Display path name and status flags */
7195 printf("%s: (0x%08x)", path, hus.hus_states);
7197 if (hus.hus_states & HS_RELEASED)
7198 printf(" released");
7199 if (hus.hus_states & HS_EXISTS)
7201 if (hus.hus_states & HS_DIRTY)
7203 if (hus.hus_states & HS_ARCHIVED)
7204 printf(" archived");
7205 /* Display user-settable flags */
7206 if (hus.hus_states & HS_NORELEASE)
7207 printf(" never_release");
7208 if (hus.hus_states & HS_NOARCHIVE)
7209 printf(" never_archive");
7210 if (hus.hus_states & HS_LOST)
7211 printf(" lost_from_hsm");
7213 if (hus.hus_archive_id != 0)
7214 printf(", archive_id:%d", hus.hus_archive_id);
7217 } while (++i < argc);
7222 #define LFS_HSM_SET 0
7223 #define LFS_HSM_CLEAR 1
7226 * Generic function to set or clear HSM flags.
7227 * Used by hsm_set and hsm_clear.
7229 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7231 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7233 struct option long_opts[] = {
7234 { .val = 'A', .name = "archived", .has_arg = no_argument },
7235 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7236 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7237 { .val = 'e', .name = "exists", .has_arg = no_argument },
7238 { .val = 'l', .name = "lost", .has_arg = no_argument },
7239 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7240 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7242 char short_opts[] = "lraAdei:";
7246 __u32 archive_id = 0;
7252 while ((c = getopt_long(argc, argv, short_opts,
7253 long_opts, NULL)) != -1) {
7259 mask |= HS_NOARCHIVE;
7262 mask |= HS_ARCHIVED;
7265 mask |= HS_NORELEASE;
7274 archive_id = strtol(optarg, &end, 10);
7276 fprintf(stderr, "invalid archive_id: '%s'\n",
7284 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7285 argv[0], argv[optind - 1]);
7290 /* User should have specified a flag */
7294 while (optind < argc) {
7296 path = argv[optind];
7298 /* If mode == 0, this means we apply the mask. */
7299 if (mode == LFS_HSM_SET)
7300 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7302 rc = llapi_hsm_state_set(path, 0, mask, 0);
7305 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7306 path, strerror(errno = -rc));
7315 static int lfs_hsm_action(int argc, char **argv)
7320 struct hsm_current_action hca;
7321 struct hsm_extent he;
7322 enum hsm_user_action hua;
7323 enum hsm_progress_states hps;
7331 rc = llapi_hsm_current_action(path, &hca);
7333 fprintf(stderr, "can't get hsm action for %s: %s\n",
7334 path, strerror(errno = -rc));
7337 he = hca.hca_location;
7338 hua = hca.hca_action;
7339 hps = hca.hca_state;
7341 printf("%s: %s", path, hsm_user_action2name(hua));
7343 /* Skip file without action */
7344 if (hca.hca_action == HUA_NONE) {
7349 printf(" %s ", hsm_progress_state2name(hps));
7351 if ((hps == HPS_RUNNING) &&
7352 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7353 printf("(%llu bytes moved)\n",
7354 (unsigned long long)he.length);
7355 else if ((he.offset + he.length) == LUSTRE_EOF)
7356 printf("(from %llu to EOF)\n",
7357 (unsigned long long)he.offset);
7359 printf("(from %llu to %llu)\n",
7360 (unsigned long long)he.offset,
7361 (unsigned long long)(he.offset + he.length));
7363 } while (++i < argc);
7368 static int lfs_hsm_set(int argc, char **argv)
7370 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7373 static int lfs_hsm_clear(int argc, char **argv)
7375 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7379 * Check file state and return its fid, to be used by lfs_hsm_request().
7381 * \param[in] file Path to file to check
7382 * \param[in,out] fid Pointer to allocated lu_fid struct.
7383 * \param[in,out] last_dev Pointer to last device id used.
7385 * \return 0 on success.
7387 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7393 rc = lstat(file, &st);
7395 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7398 /* Checking for regular file as archiving as posix copytool
7399 * rejects archiving files other than regular files
7401 if (!S_ISREG(st.st_mode)) {
7402 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7405 /* A request should be ... */
7406 if (*last_dev != st.st_dev && *last_dev != 0) {
7407 fprintf(stderr, "All files should be "
7408 "on the same filesystem: %s\n", file);
7411 *last_dev = st.st_dev;
7413 rc = llapi_path2fid(file, fid);
7415 fprintf(stderr, "Cannot read FID of %s: %s\n",
7416 file, strerror(-rc));
7422 /* Fill an HSM HUR item with a given file name.
7424 * If mntpath is set, then the filename is actually a FID, and no
7425 * lookup on the filesystem will be performed.
7427 * \param[in] hur the user request to fill
7428 * \param[in] idx index of the item inside the HUR to fill
7429 * \param[in] mntpath mountpoint of Lustre
7430 * \param[in] fname filename (if mtnpath is NULL)
7431 * or FID (if mntpath is set)
7432 * \param[in] last_dev pointer to last device id used
7434 * \retval 0 on success
7435 * \retval CMD_HELP or a negative errno on error
7437 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7438 const char *mntpath, const char *fname,
7441 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7444 hui->hui_extent.length = -1;
7446 if (mntpath != NULL) {
7449 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7453 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7458 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7462 hur->hur_request.hr_itemcount++;
7467 static int lfs_hsm_request(int argc, char **argv, int action)
7469 struct option long_opts[] = {
7470 { .val = 'a', .name = "archive", .has_arg = required_argument },
7471 { .val = 'D', .name = "data", .has_arg = required_argument },
7472 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7473 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7476 char short_opts[] = "l:D:a:m:";
7477 struct hsm_user_request *hur, *oldhur;
7482 char *filelist = NULL;
7483 char fullpath[PATH_MAX];
7484 char *opaque = NULL;
7488 int nbfile_alloc = 0;
7489 char *some_file = NULL;
7490 char *mntpath = NULL;
7496 while ((c = getopt_long(argc, argv, short_opts,
7497 long_opts, NULL)) != -1) {
7506 if (action != HUA_ARCHIVE &&
7507 action != HUA_REMOVE) {
7509 "error: -a is supported only "
7510 "when archiving or removing\n");
7513 archive_id = atoi(optarg);
7516 if (some_file == NULL) {
7518 some_file = strdup(optarg);
7524 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7525 argv[0], argv[optind - 1]);
7530 /* All remaining args are files, so we have at least nbfile */
7531 nbfile = argc - optind;
7533 if ((nbfile == 0) && (filelist == NULL))
7537 opaque_len = strlen(opaque);
7539 /* Alloc the request structure with enough place to store all files
7540 * from command line. */
7541 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7543 fprintf(stderr, "Cannot create the request: %s\n",
7547 nbfile_alloc = nbfile;
7549 hur->hur_request.hr_action = action;
7550 hur->hur_request.hr_archive_id = archive_id;
7551 hur->hur_request.hr_flags = 0;
7553 /* All remaining args are files, add them */
7554 if (nbfile != 0 && some_file == NULL)
7555 some_file = strdup(argv[optind]);
7557 for (i = 0; i < nbfile; i++) {
7558 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7564 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7566 /* If a filelist was specified, read the filelist from it. */
7567 if (filelist != NULL) {
7568 fp = fopen(filelist, "r");
7570 fprintf(stderr, "Cannot read the file list %s: %s\n",
7571 filelist, strerror(errno));
7576 while ((rc = getline(&line, &len, fp)) != -1) {
7577 /* If allocated buffer was too small, get something
7579 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7582 nbfile_alloc = nbfile_alloc * 2 + 1;
7584 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7587 fprintf(stderr, "hsm: cannot allocate "
7588 "the request: %s\n",
7595 size = hur_len(oldhur);
7597 fprintf(stderr, "hsm: cannot allocate "
7598 "%u files + %u bytes data\n",
7599 oldhur->hur_request.hr_itemcount,
7600 oldhur->hur_request.hr_data_len);
7607 memcpy(hur, oldhur, size);
7612 if (line[strlen(line) - 1] == '\n')
7613 line[strlen(line) - 1] = '\0';
7615 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7616 mntpath, line, &last_dev);
7622 if (some_file == NULL) {
7632 /* If a --data was used, add it to the request */
7633 hur->hur_request.hr_data_len = opaque_len;
7635 memcpy(hur_data(hur), opaque, opaque_len);
7637 /* Send the HSM request */
7638 if (realpath(some_file, fullpath) == NULL) {
7639 fprintf(stderr, "Could not find path '%s': %s\n",
7640 some_file, strerror(errno));
7642 rc = llapi_hsm_request(fullpath, hur);
7644 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7645 some_file, strerror(-rc));
7655 static int lfs_hsm_archive(int argc, char **argv)
7657 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7660 static int lfs_hsm_restore(int argc, char **argv)
7662 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7665 static int lfs_hsm_release(int argc, char **argv)
7667 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7670 static int lfs_hsm_remove(int argc, char **argv)
7672 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7675 static int lfs_hsm_cancel(int argc, char **argv)
7677 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7680 static int lfs_swap_layouts(int argc, char **argv)
7685 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7686 SWAP_LAYOUTS_KEEP_MTIME |
7687 SWAP_LAYOUTS_KEEP_ATIME);
7690 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7692 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7694 int lfs_get_mode(const char *string)
7696 enum lock_mode_user mode;
7698 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7699 if (lock_mode_names[mode] == NULL)
7701 if (strcmp(string, lock_mode_names[mode]) == 0)
7708 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7710 enum lu_ladvise_type advice;
7713 advice < ARRAY_SIZE(ladvise_names); advice++) {
7714 if (ladvise_names[advice] == NULL)
7716 if (strcmp(string, ladvise_names[advice]) == 0)
7720 return LU_LADVISE_INVALID;
7723 static int lfs_ladvise(int argc, char **argv)
7725 struct option long_opts[] = {
7726 { .val = 'a', .name = "advice", .has_arg = required_argument },
7727 { .val = 'b', .name = "background", .has_arg = no_argument },
7728 { .val = 'e', .name = "end", .has_arg = required_argument },
7729 { .val = 'l', .name = "length", .has_arg = required_argument },
7730 { .val = 'm', .name = "mode", .has_arg = required_argument },
7731 { .val = 's', .name = "start", .has_arg = required_argument },
7732 { .val = 'u', .name = "unset", .has_arg = no_argument },
7734 char short_opts[] = "a:be:l:m:s:u";
7739 struct llapi_lu_ladvise advice;
7740 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7741 unsigned long long start = 0;
7742 unsigned long long end = LUSTRE_EOF;
7743 unsigned long long length = 0;
7744 unsigned long long size_units;
7745 unsigned long long flags = 0;
7749 while ((c = getopt_long(argc, argv, short_opts,
7750 long_opts, NULL)) != -1) {
7753 advice_type = lfs_get_ladvice(optarg);
7754 if (advice_type == LU_LADVISE_INVALID) {
7755 fprintf(stderr, "%s: invalid advice type "
7756 "'%s'\n", argv[0], optarg);
7757 fprintf(stderr, "Valid types:");
7759 for (advice_type = 0;
7760 advice_type < ARRAY_SIZE(ladvise_names);
7762 if (ladvise_names[advice_type] == NULL)
7764 fprintf(stderr, " %s",
7765 ladvise_names[advice_type]);
7767 fprintf(stderr, "\n");
7780 rc = llapi_parse_size(optarg, &end,
7783 fprintf(stderr, "%s: bad end offset '%s'\n",
7790 rc = llapi_parse_size(optarg, &start,
7793 fprintf(stderr, "%s: bad start offset "
7794 "'%s'\n", argv[0], optarg);
7800 rc = llapi_parse_size(optarg, &length,
7803 fprintf(stderr, "%s: bad length '%s'\n",
7809 mode = lfs_get_mode(optarg);
7811 fprintf(stderr, "%s: bad mode '%s', valid "
7812 "modes are READ or WRITE\n",
7820 fprintf(stderr, "%s: option '%s' unrecognized\n",
7821 argv[0], argv[optind - 1]);
7826 if (advice_type == LU_LADVISE_INVALID) {
7827 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7828 fprintf(stderr, "Valid types:");
7829 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7831 if (ladvise_names[advice_type] == NULL)
7833 fprintf(stderr, " %s", ladvise_names[advice_type]);
7835 fprintf(stderr, "\n");
7839 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7840 fprintf(stderr, "%s: Lock no expand advice is a per file "
7841 "descriptor advice, so when called from lfs, "
7842 "it does nothing.\n", argv[0]);
7846 if (argc <= optind) {
7847 fprintf(stderr, "%s: please give one or more file names\n",
7852 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7853 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7858 if (end == LUSTRE_EOF && length != 0)
7859 end = start + length;
7862 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7863 argv[0], start, end);
7867 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7868 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7873 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7874 fprintf(stderr, "%s: mode is required with lockahead\n",
7879 while (optind < argc) {
7882 path = argv[optind++];
7884 fd = open(path, O_RDONLY);
7886 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7887 argv[0], path, strerror(errno));
7892 advice.lla_start = start;
7893 advice.lla_end = end;
7894 advice.lla_advice = advice_type;
7895 advice.lla_value1 = 0;
7896 advice.lla_value2 = 0;
7897 advice.lla_value3 = 0;
7898 advice.lla_value4 = 0;
7899 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7900 advice.lla_lockahead_mode = mode;
7901 advice.lla_peradvice_flags = flags;
7904 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7907 fprintf(stderr, "%s: cannot give advice '%s' to file "
7908 "'%s': %s\n", argv[0],
7909 ladvise_names[advice_type],
7910 path, strerror(errno));
7916 if (rc == 0 && rc2 < 0)
7923 static const char *const heat_names[] = LU_HEAT_NAMES;
7925 static int lfs_heat_get(int argc, char **argv)
7927 struct lu_heat *heat;
7936 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
7938 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
7943 while (optind < argc) {
7944 path = argv[optind++];
7946 fd = open(path, O_RDONLY);
7948 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7949 argv[0], path, strerror(errno));
7954 heat->lh_count = OBD_HEAT_COUNT;
7955 rc2 = llapi_heat_get(fd, heat);
7958 fprintf(stderr, "%s: cannot get heat of file '%s'"
7959 ": %s\n", argv[0], path, strerror(errno));
7963 printf("flags: %x\n", heat->lh_flags);
7964 for (i = 0; i < heat->lh_count; i++)
7965 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
7967 if (rc == 0 && rc2 < 0)
7975 static int lfs_heat_set(int argc, char **argv)
7977 struct option long_opts[] = {
7978 {"clear", no_argument, 0, 'c'},
7979 {"off", no_argument, 0, 'o'},
7980 {"on", no_argument, 0, 'O'},
7983 char short_opts[] = "coO";
7994 while ((c = getopt_long(argc, argv, short_opts,
7995 long_opts, NULL)) != -1) {
7998 flags |= LU_HEAT_FLAG_CLEAR;
8001 flags |= LU_HEAT_FLAG_CLEAR;
8002 flags |= LU_HEAT_FLAG_OFF;
8005 flags &= ~LU_HEAT_FLAG_OFF;
8010 fprintf(stderr, "%s: option '%s' unrecognized\n",
8011 argv[0], argv[optind - 1]);
8016 if (argc <= optind) {
8017 fprintf(stderr, "%s: please give one or more file names\n",
8022 while (optind < argc) {
8023 path = argv[optind++];
8025 fd = open(path, O_RDONLY);
8027 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8028 argv[0], path, strerror(errno));
8033 rc2 = llapi_heat_set(fd, flags);
8036 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8037 ": %s\n", argv[0], path, strerror(errno));
8041 if (rc == 0 && rc2 < 0)
8047 /** The input string contains a comma delimited list of component ids and
8048 * ranges, for example "1,2-4,7".
8050 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8052 bool end_of_loop = false;
8060 while (!end_of_loop) {
8064 char *endptr = NULL;
8067 ptr = strchrnul(arg, ',');
8068 end_of_loop = *ptr == '\0';
8071 start_index = strtol(arg, &endptr, 0);
8072 if (endptr == arg) /* no data at all */
8074 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8076 if (start_index < 0)
8079 end_index = start_index;
8080 if (*endptr == '-') {
8081 end_index = strtol(endptr + 1, &endptr, 0);
8082 if (*endptr != '\0')
8084 if (end_index < start_index)
8088 for (i = start_index; i <= end_index && size > 0; i++) {
8091 /* remove duplicate */
8092 for (j = 0; j < nr; j++) {
8096 if (j == nr) { /* no duplicate */
8102 if (size == 0 && i < end_index)
8109 if (!end_of_loop && ptr != NULL)
8112 return rc < 0 ? rc : nr;
8116 * struct verify_mirror_id - Mirror id to be verified.
8117 * @mirror_id: A specified mirror id.
8118 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8120 struct verify_mirror_id {
8126 * compare_mirror_ids() - Compare mirror ids.
8127 * @layout: Mirror component list.
8128 * @cbdata: Callback data in verify_mirror_id structure.
8130 * This is a callback function called by llapi_layout_comp_iterate()
8131 * to compare the specified mirror id with the one in the current
8132 * component of @layout. If they are the same, then the specified
8133 * mirror id is valid.
8135 * Return: a negative error code on failure or
8136 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8137 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8140 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8142 struct verify_mirror_id *mirror_id_cbdata =
8143 (struct verify_mirror_id *)cbdata;
8147 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8151 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8152 progname, strerror(errno));
8156 if (mirror_id_cbdata->mirror_id == mirror_id) {
8157 mirror_id_cbdata->is_valid_id = true;
8158 return LLAPI_LAYOUT_ITER_STOP;
8161 return LLAPI_LAYOUT_ITER_CONT;
8165 * verify_mirror_ids() - Verify specified mirror ids.
8166 * @fname: Mirrored file name.
8167 * @mirror_ids: Specified mirror ids to be verified.
8168 * @ids_nr: Number of specified mirror ids.
8170 * This function verifies that specified @mirror_ids are valid
8171 * in the mirrored file @fname.
8173 * Return: 0 on success or a negative error code on failure.
8176 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8178 struct llapi_layout *layout = NULL;
8179 struct verify_mirror_id mirror_id_cbdata = { 0 };
8190 if (stat(fname, &stbuf) < 0) {
8191 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8192 progname, fname, strerror(errno));
8197 if (!S_ISREG(stbuf.st_mode)) {
8198 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8204 fd = open(fname, O_DIRECT | O_RDONLY);
8206 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8207 progname, fname, strerror(errno));
8212 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8214 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8215 progname, fname, strerror(errno));
8219 layout = llapi_layout_get_by_fd(fd, 0);
8220 if (layout == NULL) {
8221 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8222 progname, fname, strerror(errno));
8224 llapi_lease_release(fd);
8228 rc = llapi_layout_flags_get(layout, &flr_state);
8230 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8231 progname, fname, strerror(errno));
8236 flr_state &= LCM_FL_FLR_MASK;
8237 switch (flr_state) {
8240 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8241 progname, fname, llapi_layout_flags_string(flr_state));
8248 for (i = 0; i < ids_nr; i++) {
8249 mirror_id_cbdata.mirror_id = mirror_ids[i];
8250 mirror_id_cbdata.is_valid_id = false;
8252 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8257 "%s: '%s' failed to verify mirror id: %u.\n",
8258 progname, fname, mirror_ids[i]);
8262 if (!mirror_id_cbdata.is_valid_id) {
8265 "%s: '%s' invalid specified mirror id: %u.\n",
8266 progname, fname, mirror_ids[i]);
8272 llapi_layout_free(layout);
8273 llapi_lease_release(fd);
8281 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8282 __u16 *mirror_ids, int ids_nr)
8284 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8285 struct llapi_layout *layout;
8295 if (stat(fname, &stbuf) < 0) {
8296 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8297 progname, fname, strerror(errno));
8301 if (!S_ISREG(stbuf.st_mode)) {
8302 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8308 fd = open(fname, O_DIRECT | O_RDWR);
8310 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8311 progname, fname, strerror(errno));
8316 layout = llapi_layout_get_by_fd(fd, 0);
8317 if (layout == NULL) {
8318 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8319 progname, fname, strerror(errno));
8324 rc = llapi_layout_flags_get(layout, &flr_state);
8326 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8327 progname, fname, strerror(errno));
8332 flr_state &= LCM_FL_FLR_MASK;
8333 if (flr_state == LCM_FL_NONE) {
8335 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8340 /* get stale component info */
8341 comp_size = llapi_mirror_find_stale(layout, comp_array,
8342 ARRAY_SIZE(comp_array),
8343 mirror_ids, ids_nr);
8344 if (comp_size <= 0) {
8349 ioc->lil_mode = LL_LEASE_WRLCK;
8350 ioc->lil_flags = LL_LEASE_RESYNC;
8351 rc = llapi_lease_set(fd, ioc);
8353 if (rc == -EALREADY)
8357 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8358 progname, fname, strerror(errno));
8362 /* get the read range [start, end) */
8363 start = comp_array[0].lrc_start;
8364 end = comp_array[0].lrc_end;
8365 for (idx = 1; idx < comp_size; idx++) {
8366 if (comp_array[idx].lrc_start < start)
8367 start = comp_array[idx].lrc_start;
8368 if (end < comp_array[idx].lrc_end)
8369 end = comp_array[idx].lrc_end;
8372 rc = llapi_lease_check(fd);
8373 if (rc != LL_LEASE_WRLCK) {
8374 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8379 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8382 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8383 progname, fname, rc);
8385 /* prepare ioc for lease put */
8386 ioc->lil_mode = LL_LEASE_UNLCK;
8387 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8389 for (idx = 0; idx < comp_size; idx++) {
8390 if (comp_array[idx].lrc_synced) {
8391 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8396 rc = llapi_lease_set(fd, ioc);
8398 if (rc == 0) /* lost lease lock */
8400 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8401 progname, fname, strerror(errno));
8405 * llapi_lease_set returns lease mode when it request to unlock
8411 llapi_layout_free(layout);
8418 static inline int lfs_mirror_resync(int argc, char **argv)
8420 struct ll_ioc_lease *ioc = NULL;
8421 __u16 mirror_ids[128] = { 0 };
8426 struct option long_opts[] = {
8427 { .val = 'o', .name = "only", .has_arg = required_argument },
8430 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8433 rc = parse_mirror_ids(mirror_ids,
8434 sizeof(mirror_ids) / sizeof(__u16),
8438 "%s: bad mirror ids '%s'.\n",
8445 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8446 argv[0], argv[optind - 1]);
8452 if (argc == optind) {
8453 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8458 if (ids_nr > 0 && argc > optind + 1) {
8460 "%s: option '--only' cannot be used upon multiple files.\n",
8468 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8473 /* set the lease on the file */
8474 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8476 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8477 argv[0], strerror(errno));
8482 for (; optind < argc; optind++) {
8483 rc = lfs_mirror_resync_file(argv[optind], ioc,
8484 mirror_ids, ids_nr);
8485 /* ignore previous file's error, continue with next file */
8488 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8496 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8498 struct llapi_layout *layout;
8501 layout = llapi_layout_get_by_fd(fd, 0);
8502 if (layout == NULL) {
8503 fprintf(stderr, "could not get layout.\n");
8507 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8509 fprintf(stderr, "failed to iterate layout\n");
8510 llapi_layout_free(layout);
8513 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8514 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8515 llapi_layout_free(layout);
8519 llapi_layout_free(layout);
8525 * Check whether two files are the same file
8526 * \retval 0 same file
8527 * \retval 1 not the same file
8528 * \retval <0 error code
8530 static inline int check_same_file(const char *f1, const char *f2)
8535 if (stat(f1, &stbuf1) < 0) {
8536 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8537 progname, f1, strerror(errno));
8541 if (stat(f2, &stbuf2) < 0) {
8542 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8543 progname, f2, strerror(errno));
8547 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8548 stbuf1.st_ino == stbuf2.st_ino)
8554 static inline int lfs_mirror_read(int argc, char **argv)
8557 __u16 mirror_id = 0;
8558 const char *outfile = NULL;
8564 const size_t buflen = 4 << 20;
8566 struct option long_opts[] = {
8567 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8568 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8571 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8576 mirror_id = strtoul(optarg, &end, 0);
8577 if (*end != '\0' || mirror_id == 0) {
8579 "%s %s: invalid mirror ID '%s'\n",
8580 progname, argv[0], optarg);
8588 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8589 progname, argv[optind - 1]);
8594 if (argc == optind) {
8595 fprintf(stderr, "%s %s: no mirrored file provided\n",
8598 } else if (argc > optind + 1) {
8599 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8603 if (mirror_id == 0) {
8604 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8609 /* open mirror file */
8610 fname = argv[optind];
8613 rc = check_same_file(fname, outfile);
8616 "%s %s: output file cannot be the mirrored file\n",
8624 fd = open(fname, O_DIRECT | O_RDONLY);
8626 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8627 progname, argv[0], fname, strerror(errno));
8631 /* verify mirror id */
8632 rc = verify_mirror_id_by_fd(fd, mirror_id);
8635 "%s %s: cannot find mirror with ID %u in '%s'\n",
8636 progname, argv[0], mirror_id, fname);
8640 /* open output file */
8642 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8644 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8645 progname, argv[0], outfile, strerror(errno));
8650 outfd = STDOUT_FILENO;
8653 /* allocate buffer */
8654 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8656 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8657 progname, argv[0], rc);
8664 ssize_t written = 0;
8666 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8667 if (bytes_read < 0) {
8670 "%s %s: fail to read data from mirror %u: %s\n",
8671 progname, argv[0], mirror_id, strerror(-rc));
8676 if (bytes_read == 0)
8679 while (written < bytes_read) {
8682 written2 = write(outfd, buf + written,
8683 bytes_read - written);
8686 "%s %s: fail to write %s: %s\n",
8687 progname, argv[0], outfile ? : "STDOUT",
8692 written += written2;
8695 if (written != bytes_read) {
8697 "%s %s: written %ld bytes does not match with %ld read.\n",
8698 progname, argv[0], written, bytes_read);
8720 static inline int lfs_mirror_write(int argc, char **argv)
8723 __u16 mirror_id = 0;
8724 const char *inputfile = NULL;
8730 const size_t buflen = 4 << 20;
8732 size_t page_size = sysconf(_SC_PAGESIZE);
8733 struct ll_ioc_lease_id ioc;
8735 struct option long_opts[] = {
8736 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8737 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8740 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8745 mirror_id = strtoul(optarg, &end, 0);
8746 if (*end != '\0' || mirror_id == 0) {
8748 "%s %s: invalid mirror ID '%s'\n",
8749 progname, argv[0], optarg);
8757 fprintf(stderr, "%s: option '%s' unrecognized\n",
8758 progname, argv[optind - 1]);
8763 if (argc == optind) {
8764 fprintf(stderr, "%s %s: no mirrored file provided\n",
8767 } else if (argc > optind + 1) {
8768 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8772 if (mirror_id == 0) {
8773 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8778 /* open mirror file */
8779 fname = argv[optind];
8782 rc = check_same_file(fname, inputfile);
8785 "%s %s: input file cannot be the mirrored file\n",
8793 fd = open(fname, O_DIRECT | O_WRONLY);
8795 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8796 progname, argv[0], fname, strerror(errno));
8800 /* verify mirror id */
8801 rc = verify_mirror_id_by_fd(fd, mirror_id);
8804 "%s %s: cannot find mirror with ID %u in '%s'\n",
8805 progname, argv[0], mirror_id, fname);
8809 /* open input file */
8811 inputfd = open(inputfile, O_RDONLY, 0644);
8813 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
8814 progname, argv[0], inputfile, strerror(errno));
8819 inputfd = STDIN_FILENO;
8822 /* allocate buffer */
8823 rc = posix_memalign(&buf, page_size, buflen);
8825 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8826 progname, argv[0], rc);
8830 /* prepare target mirror components instantiation */
8831 ioc.lil_mode = LL_LEASE_WRLCK;
8832 ioc.lil_flags = LL_LEASE_RESYNC;
8833 ioc.lil_mirror_id = mirror_id;
8834 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8837 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8838 progname, argv[0], fname, strerror(errno));
8848 rc = llapi_lease_check(fd);
8849 if (rc != LL_LEASE_WRLCK) {
8850 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
8851 progname, argv[0], fname);
8855 bytes_read = read(inputfd, buf, buflen);
8856 if (bytes_read < 0) {
8859 "%s %s: fail to read data from '%s': %s\n",
8860 progname, argv[0], inputfile ? : "STDIN",
8867 if (bytes_read == 0)
8870 /* round up to page align to make direct IO happy. */
8871 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
8873 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
8878 "%s %s: fail to write to mirror %u: %s\n",
8879 progname, argv[0], mirror_id,
8887 if (pos & (page_size - 1)) {
8888 rc = llapi_mirror_truncate(fd, mirror_id, pos);
8893 ioc.lil_mode = LL_LEASE_UNLCK;
8894 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
8896 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8901 "%s %s: release lease lock of '%s' failed: %s\n",
8902 progname, argv[0], fname, strerror(errno));
8919 struct collect_ids_data {
8925 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
8927 struct collect_ids_data *cid = cbdata;
8931 rc = llapi_layout_mirror_id_get(layout, &id);
8935 if ((__u16)id != cid->cid_exclude) {
8938 for (i = 0; i < cid->cid_count; i++) {
8939 /* already collected the mirror id */
8940 if (id == cid->cid_ids[i])
8941 return LLAPI_LAYOUT_ITER_CONT;
8943 cid->cid_ids[cid->cid_count] = id;
8947 return LLAPI_LAYOUT_ITER_CONT;
8950 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
8952 struct llapi_layout *layout;
8953 struct collect_ids_data cid = { .cid_ids = ids,
8955 .cid_exclude = exclude_id, };
8958 layout = llapi_layout_get_by_fd(fd, 0);
8959 if (layout == NULL) {
8960 fprintf(stderr, "could not get layout\n");
8964 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
8966 fprintf(stderr, "failed to iterate layout\n");
8967 llapi_layout_free(layout);
8971 llapi_layout_free(layout);
8973 return cid.cid_count;
8976 static inline int lfs_mirror_copy(int argc, char **argv)
8979 __u16 read_mirror_id = 0;
8980 __u16 ids[128] = { 0 };
8982 struct llapi_layout *layout = NULL;
8983 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8990 struct ll_ioc_lease *ioc = NULL;
8991 struct ll_ioc_lease_id *resync_ioc;
8993 struct option long_opts[] = {
8994 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
8995 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
8998 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9003 read_mirror_id = strtoul(optarg, &end, 0);
9004 if (*end != '\0' || read_mirror_id == 0) {
9006 "%s %s: invalid read mirror ID '%s'\n",
9007 progname, argv[0], optarg);
9012 if (!strcmp(optarg, "-1")) {
9013 /* specify all other mirrors */
9017 count = parse_mirror_ids((__u16 *)ids,
9025 fprintf(stderr, "%s: option '%s' unrecognized\n",
9026 progname, argv[optind - 1]);
9031 if (argc == optind) {
9032 fprintf(stderr, "%s %s: no mirrored file provided\n",
9035 } else if (argc > optind + 1) {
9036 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9040 if (read_mirror_id == 0) {
9042 "%s %s: no valid read mirror ID %d is provided\n",
9043 progname, argv[0], read_mirror_id);
9049 "%s %s: no write mirror ID is provided\n",
9054 for (i = 0; i < count; i++) {
9055 if (read_mirror_id == ids[i]) {
9057 "%s %s: read and write mirror ID cannot be the same\n",
9063 /* open mirror file */
9064 fname = argv[optind];
9066 fd = open(fname, O_DIRECT | O_RDWR);
9068 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9069 progname, argv[0], fname, strerror(errno));
9073 /* write to all other mirrors */
9074 if (ids[0] == (__u16)-1) {
9075 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9079 "%s %s: failed to get other mirror ids in '%s': %d\n",
9080 progname, argv[0], fname, rc);
9085 /* verify mirror id */
9086 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9089 "%s %s: cannot find mirror with ID %u in '%s'\n",
9090 progname, argv[0], read_mirror_id, fname);
9094 for (i = 0; i < count; i++) {
9095 rc = verify_mirror_id_by_fd(fd, ids[i]);
9098 "%s %s: cannot find mirror with ID %u in '%s'\n",
9099 progname, argv[0], ids[i], fname);
9104 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9107 "%s %s: cannot alloc comp id array for ioc: %s\n",
9108 progname, argv[0], strerror(errno));
9113 /* get stale component info */
9114 layout = llapi_layout_get_by_fd(fd, 0);
9115 if (layout == NULL) {
9116 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9117 progname, argv[0], fname, strerror(errno));
9121 comp_size = llapi_mirror_find_stale(layout, comp_array,
9122 ARRAY_SIZE(comp_array),
9124 llapi_layout_free(layout);
9125 if (comp_size < 0) {
9130 /* prepare target mirror components instantiation */
9131 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9132 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9133 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9135 resync_ioc->lil_mirror_id = ids[0];
9137 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9138 rc = llapi_lease_set(fd, ioc);
9141 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9142 progname, argv[0], fname, strerror(errno));
9146 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9149 fprintf(stderr, "%s %s: copy error: %d\n",
9150 progname, argv[0], rc);
9154 fprintf(stdout, "mirror copied successfully: ");
9155 for (i = 0; i < copied; i++)
9156 fprintf(stdout, "%d ", ids[i]);
9157 fprintf(stdout, "\n");
9159 ioc->lil_mode = LL_LEASE_UNLCK;
9160 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9162 for (i = 0; i < comp_size; i++) {
9165 for (j = 0; j < copied; j++) {
9166 if (comp_array[i].lrc_mirror_id != ids[j])
9169 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9173 rc = llapi_lease_set(fd, ioc);
9178 "%s %s: release lease lock of '%s' failed: %s\n",
9179 progname, argv[0], fname, strerror(errno));
9193 * struct verify_chunk - Mirror chunk to be verified.
9194 * @chunk: [start, end) of the chunk.
9195 * @mirror_count: Number of mirror ids in @mirror_id array.
9196 * @mirror_id: Array of valid mirror ids that cover the chunk.
9198 struct verify_chunk {
9199 struct lu_extent chunk;
9200 unsigned int mirror_count;
9201 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9205 * print_chunks() - Print chunk information.
9206 * @fname: Mirrored file name.
9207 * @chunks: Array of chunks.
9208 * @chunk_count: Number of chunks in @chunks array.
9210 * This function prints [start, end) of each chunk in @chunks
9211 * for mirrored file @fname, and also prints the valid mirror ids
9212 * that cover the chunk.
9217 void print_chunks(const char *fname, struct verify_chunk *chunks,
9223 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9224 for (i = 0; i < chunk_count; i++) {
9225 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9227 if (chunks[i].mirror_count == 0)
9228 fprintf(stdout, "\t[");
9230 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9231 for (j = 1; j < chunks[i].mirror_count; j++)
9232 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9234 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9236 fprintf(stdout, "\n");
9240 * print_checksums() - Print CRC-32 checksum values.
9241 * @chunk: A chunk and its corresponding valid mirror ids.
9242 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9244 * This function prints CRC-32 checksum values on @chunk for
9245 * each valid mirror that covers it.
9250 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9255 "CRC-32 checksum value for chunk "DEXT":\n",
9256 PEXT(&chunk->chunk));
9257 for (i = 0; i < chunk->mirror_count; i++)
9258 fprintf(stdout, "Mirror %u:\t%#lx\n",
9259 chunk->mirror_id[i], crc[i]);
9260 fprintf(stdout, "\n");
9264 * filter_mirror_id() - Filter specified mirror ids.
9265 * @chunks: Array of chunks.
9266 * @chunk_count: Number of chunks in @chunks array.
9267 * @mirror_ids: Specified mirror ids to be verified.
9268 * @ids_nr: Number of specified mirror ids.
9270 * This function scans valid mirror ids that cover each chunk in @chunks
9271 * and filters specified mirror ids.
9276 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9277 __u16 *mirror_ids, int ids_nr)
9282 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9283 unsigned int valid_count = 0;
9285 for (i = 0; i < chunk_count; i++) {
9286 if (chunks[i].mirror_count == 0)
9290 for (j = 0; j < ids_nr; j++) {
9291 for (k = 0; k < chunks[i].mirror_count; k++) {
9292 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9293 valid_id[valid_count] = mirror_ids[j];
9300 memcpy(chunks[i].mirror_id, valid_id,
9301 sizeof(__u16) * valid_count);
9302 chunks[i].mirror_count = valid_count;
9307 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9308 * @layout: Mirror component list.
9309 * @chunks: Array of chunks.
9310 * @chunks_size: Array size of @chunks.
9312 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9313 * to find out chunk segments and store them in @chunks array.
9315 * The @mirror_id array in each element of @chunks will store the valid
9316 * mirror ids that cover the chunk. If a mirror component covering the
9317 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9318 * will not be stored into the @mirror_id array, and the chunk for that
9319 * mirror will not be verified.
9321 * The @mirror_count in each element of @chunks will store the number of
9322 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9323 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9324 * indicates the chunk is valid in only one mirror. In both cases, the
9325 * chunk will not be verified.
9327 * Here is an example:
9330 * +------+-------------+--------------------+
9332 * +------+------+------+------+-------------+
9333 * | | S | S | | mirror2
9334 * +-------------+------+------+-------------+
9336 * prepared @chunks array will contain 5 elements:
9337 * (([0, 1M), [1, 2], 2),
9338 * ([1M, 2M), [1, 2], 2),
9339 * ([2M, 3M), [1], 1),
9340 * ([3M, 4M], [], 0),
9341 * ([4M, EOF), [2], 1))
9343 * Return: the actual array size of @chunks on success
9344 * or a negative error code on failure.
9347 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9348 struct verify_chunk *chunks,
9359 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9362 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9365 "%s: move to the first layout component: %s.\n",
9366 progname, strerror(errno));
9372 chunks[idx].chunk.e_end = LUSTRE_EOF;
9374 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9377 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9378 progname, strerror(errno));
9382 if (start > chunks[idx].chunk.e_start ||
9383 end <= chunks[idx].chunk.e_start)
9386 if (end < chunks[idx].chunk.e_end)
9387 chunks[idx].chunk.e_end = end;
9389 rc = llapi_layout_comp_flags_get(layout, &flags);
9392 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9393 progname, strerror(errno));
9397 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9400 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9403 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9404 progname, strerror(errno));
9408 chunks[idx].mirror_id[i] = mirror_id;
9410 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9412 "%s: mirror_id array is too small.\n",
9419 rc = llapi_layout_comp_use(layout,
9420 LLAPI_LAYOUT_COMP_USE_NEXT);
9423 "%s: move to the next layout component: %s.\n",
9424 progname, strerror(errno));
9427 } /* loop through all components */
9429 chunks[idx].mirror_count = i;
9431 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9435 if (idx >= chunks_size) {
9436 fprintf(stderr, "%s: chunks array is too small.\n",
9442 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9446 return rc < 0 ? rc : idx + 1;
9450 * lfs_mirror_verify_chunk() - Verify a chunk.
9451 * @fd: File descriptor of the mirrored file.
9452 * @file_size: Size of the mirrored file.
9453 * @chunk: A chunk and its corresponding valid mirror ids.
9454 * @verbose: Verbose mode.
9456 * This function verifies a @chunk contains exactly the same data
9457 * ammong the mirrors that cover it.
9459 * If @verbose is specified, then the function will print where the
9460 * differences are if the data do not match. Otherwise, it will
9461 * just return an error in that case.
9463 * Return: 0 on success or a negative error code on failure.
9466 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9467 struct verify_chunk *chunk, int verbose)
9469 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9471 size_t page_size = sysconf(_SC_PAGESIZE);
9477 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9484 rc = posix_memalign(&buf, page_size, buflen);
9485 if (rc) /* error code is returned directly */
9489 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9490 PEXT(&chunk->chunk));
9491 for (i = 0; i < chunk->mirror_count; i++)
9492 fprintf(stdout, " %u", chunk->mirror_id[i]);
9493 fprintf(stdout, "\n");
9497 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9498 pos = chunk->chunk.e_start;
9499 while (bytes_done < count) {
9500 /* compute initial CRC-32 checksum */
9501 crc = crc32(0L, Z_NULL, 0);
9502 memset(crc_array, 0, sizeof(crc_array));
9505 for (i = 0; i < chunk->mirror_count; i++) {
9506 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9508 if (bytes_read < 0) {
9511 "%s: failed to read data from mirror %u: %s.\n",
9512 progname, chunk->mirror_id[i],
9517 /* compute new CRC-32 checksum */
9518 crc_array[i] = crc32(crc, buf, bytes_read);
9522 print_checksums(chunk, crc_array);
9524 /* compare CRC-32 checksum values */
9525 for (i = 1; i < chunk->mirror_count; i++) {
9526 if (crc_array[i] != crc_array[0]) {
9530 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9531 progname, PEXT(&chunk->chunk),
9532 chunk->mirror_id[0],
9533 chunk->mirror_id[i]);
9538 bytes_done += bytes_read;
9541 if (verbose > 1 && rc == 0) {
9542 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9543 PEXT(&chunk->chunk));
9544 for (i = 0; i < chunk->mirror_count; i++)
9545 fprintf(stdout, " %u", chunk->mirror_id[i]);
9546 fprintf(stdout, " PASS\n\n");
9555 * lfs_mirror_verify_file() - Verify a mirrored file.
9556 * @fname: Mirrored file name.
9557 * @mirror_ids: Specified mirror ids to be verified.
9558 * @ids_nr: Number of specified mirror ids.
9559 * @verbose: Verbose mode.
9561 * This function verifies that each SYNC mirror of a mirrored file
9562 * specified by @fname contains exactly the same data.
9564 * If @mirror_ids is specified, then the function will verify the
9565 * mirrors specified by @mirror_ids contain exactly the same data.
9567 * If @verbose is specified, then the function will print where the
9568 * differences are if the data do not match. Otherwise, it will
9569 * just return an error in that case.
9571 * Return: 0 on success or a negative error code on failure.
9574 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9577 struct verify_chunk chunks_array[1024] = { };
9578 struct llapi_layout *layout = NULL;
9582 int chunk_count = 0;
9588 if (stat(fname, &stbuf) < 0) {
9589 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9590 progname, fname, strerror(errno));
9595 if (!S_ISREG(stbuf.st_mode)) {
9596 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9602 if (stbuf.st_size == 0) {
9604 fprintf(stdout, "%s: '%s' file size is 0.\n",
9610 fd = open(fname, O_DIRECT | O_RDONLY);
9612 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9613 progname, fname, strerror(errno));
9618 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9620 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9621 progname, fname, strerror(errno));
9625 layout = llapi_layout_get_by_fd(fd, 0);
9626 if (layout == NULL) {
9627 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9628 progname, fname, strerror(errno));
9630 llapi_lease_release(fd);
9634 rc = llapi_layout_flags_get(layout, &flr_state);
9636 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9637 progname, fname, strerror(errno));
9642 flr_state &= LCM_FL_FLR_MASK;
9643 switch (flr_state) {
9646 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9647 progname, fname, llapi_layout_flags_string(flr_state));
9653 /* find out mirror chunks to be verified */
9654 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9655 ARRAY_SIZE(chunks_array));
9656 if (chunk_count < 0) {
9662 /* filter specified mirror ids */
9663 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9666 print_chunks(fname, chunks_array, chunk_count);
9668 for (idx = 0; idx < chunk_count; idx++) {
9669 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9672 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9674 PEXT(&chunks_array[idx].chunk),
9675 (unsigned long long)stbuf.st_size);
9679 if (chunks_array[idx].mirror_count == 0) {
9681 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9683 PEXT(&chunks_array[idx].chunk));
9685 fprintf(stderr, "skipped\n");
9689 fprintf(stderr, "failed\n");
9693 if (chunks_array[idx].mirror_count == 1) {
9696 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9698 PEXT(&chunks_array[idx].chunk),
9699 chunks_array[idx].mirror_id[0]);
9703 rc = llapi_lease_check(fd);
9704 if (rc != LL_LEASE_RDLCK) {
9705 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9710 /* verify one chunk */
9711 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9712 &chunks_array[idx], verbose);
9726 llapi_layout_free(layout);
9727 llapi_lease_release(fd);
9735 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9736 * @argc: The count of lfs mirror verify command line arguments.
9737 * @argv: Array of strings for lfs mirror verify command line arguments.
9739 * This function parses lfs mirror verify command and verifies the
9740 * specified mirrored file(s).
9742 * Return: 0 on success or a negative error code on failure.
9744 static inline int lfs_mirror_verify(int argc, char **argv)
9746 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9754 struct option long_opts[] = {
9755 { .val = 'o', .name = "only", .has_arg = required_argument },
9756 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9759 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9761 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9764 rc = parse_mirror_ids(mirror_ids,
9765 ARRAY_SIZE(mirror_ids),
9769 "%s: bad mirror ids '%s'.\n",
9776 "%s: at least 2 mirror ids needed with '--only' option.\n",
9786 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9787 progname, argv[optind - 1]);
9793 if (argc == optind) {
9794 fprintf(stderr, "%s: no file name given.\n", progname);
9799 if (ids_nr > 0 && argc > optind + 1) {
9801 "%s: '--only' cannot be used upon multiple files.\n",
9809 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9815 for (; optind < argc; optind++) {
9816 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9826 * lfs_mirror() - Parse and execute lfs mirror commands.
9827 * @argc: The count of lfs mirror command line arguments.
9828 * @argv: Array of strings for lfs mirror command line arguments.
9830 * This function parses lfs mirror commands and performs the
9831 * corresponding functions specified in mirror_cmdlist[].
9833 * Return: 0 on success or an error code on failure.
9835 static int lfs_mirror(int argc, char **argv)
9842 Parser_init("lfs-mirror > ", mirror_cmdlist);
9844 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9846 program_invocation_short_name = cmd;
9848 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9850 rc = Parser_commands();
9852 return rc < 0 ? -rc : rc;
9855 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9857 #if __BYTE_ORDER == __BIG_ENDIAN
9858 __swab16s(&attrs->lsa_valid);
9859 __swab64s(&attrs->lsa_size);
9860 __swab64s(&attrs->lsa_blocks);
9866 LFS_SOM_BLOCKS = 0x2,
9867 LFS_SOM_FLAGS = 0x4,
9868 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9872 static int lfs_getsom(int argc, char **argv)
9875 struct lustre_som_attrs *attrs;
9876 char buf[sizeof(*attrs) + 64];
9877 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9880 while ((c = getopt(argc, argv, "sbf")) != -1) {
9883 type = LFS_SOM_SIZE;
9886 type = LFS_SOM_BLOCKS;
9889 type = LFS_SOM_FLAGS;
9892 fprintf(stderr, "%s: invalid option '%c'\n",
9902 fprintf(stderr, "%s: %s\n",
9903 progname, argc == 0 ? "miss file target" :
9904 "input more than 2 files");
9909 attrs = (void *)buf;
9910 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9913 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
9914 argv[0], strerror(errno), errno);
9918 lustre_som_swab(attrs);
9921 case LFS_SOM_ATTR_ALL:
9922 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9923 path, attrs->lsa_size, attrs->lsa_blocks,
9927 printf("%llu\n", attrs->lsa_size);
9929 case LFS_SOM_BLOCKS:
9930 printf("%llu\n", attrs->lsa_blocks);
9933 printf("%x\n", attrs->lsa_valid);
9936 fprintf(stderr, "%s: unknown option\n", progname);
9944 * lfs_mirror_list_commands() - List lfs mirror commands.
9945 * @argc: The count of command line arguments.
9946 * @argv: Array of strings for command line arguments.
9948 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9950 * Return: 0 on success.
9952 static int lfs_mirror_list_commands(int argc, char **argv)
9954 char buffer[81] = "";
9956 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9962 static int lfs_list_commands(int argc, char **argv)
9964 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9966 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9971 int main(int argc, char **argv)
9975 /* Ensure that liblustreapi constructor has run */
9976 if (!llapi_liblustreapi_initialized())
9977 fprintf(stderr, "liblustreapi was not properly initialized\n");
9982 Parser_init("lfs > ", cmdlist);
9984 progname = program_invocation_short_name; /* Used in error messages */
9986 llapi_set_command_name(argv[1]);
9987 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9988 llapi_clear_command_name();
9990 rc = Parser_commands();
9993 return rc < 0 ? -rc : rc;
9996 #ifdef _LUSTRE_IDL_H_
9997 /* Everything we need here should be included by lustreapi.h. */
9998 # error "lfs should not depend on lustre_idl.h"
9999 #endif /* _LUSTRE_IDL_H_ */