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>
67 #include "lfs_project.h"
69 #include <libcfs/util/string.h>
70 #include <libcfs/util/ioctl.h>
71 #include <libcfs/util/parser.h>
72 #include <lustre/lustreapi.h>
73 #include <linux/lustre/lustre_ver.h>
74 #include <linux/lustre/lustre_param.h>
75 #include <linux/lnet/nidstr.h>
79 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
80 #endif /* !ARRAY_SIZE */
83 static int lfs_find(int argc, char **argv);
84 static int lfs_getstripe(int argc, char **argv);
85 static int lfs_getdirstripe(int argc, char **argv);
86 static int lfs_setdirstripe(int argc, char **argv);
87 static int lfs_rmentry(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_cp(int argc, char **argv);
100 static int lfs_ls(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_mirror(int argc, char **argv);
120 static int lfs_mirror_list_commands(int argc, char **argv);
121 static int lfs_list_commands(int argc, char **argv);
122 static inline int lfs_mirror_resync(int argc, char **argv);
123 static inline int lfs_mirror_verify(int argc, char **argv);
125 enum setstripe_origin {
132 static int lfs_setstripe_internal(int argc, char **argv,
133 enum setstripe_origin opc);
135 static inline int lfs_setstripe(int argc, char **argv)
137 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
139 static inline int lfs_setstripe_migrate(int argc, char **argv)
141 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
143 static inline int lfs_mirror_create(int argc, char **argv)
145 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
147 static inline int lfs_mirror_extend(int argc, char **argv)
149 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
151 static inline int lfs_mirror_split(int argc, char **argv)
153 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
156 /* Setstripe and migrate share mostly the same parameters */
157 #define SSM_CMD_COMMON(cmd) \
158 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
159 " [--stripe-count|-c <stripe_count>]\n" \
160 " [--stripe-index|-i <start_ost_idx>]\n" \
161 " [--stripe-size|-S <stripe_size>]\n" \
162 " [--layout|-L <pattern>]\n" \
163 " [--pool|-p <pool_name>]\n" \
164 " [--ost|-o <ost_indices>]\n" \
165 " [--yaml|-y <yaml_template_file>]\n"
167 #define SSM_HELP_COMMON \
168 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
169 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
170 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
171 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
172 "\t respectively)\n" \
173 "\tpool_name: Name of OST pool to use (default none)\n" \
174 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
175 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
176 "\t Indices be specified in a format of:\n" \
177 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
179 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
180 "\t If --pool is set with --ost, then the OSTs\n" \
181 "\t must be the members of the pool.\n" \
182 "\tcomp_end: Extent end of component, start after previous end.\n"\
183 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
184 "\t respectively, -1 for EOF). Must be a multiple of\n"\
185 "\t stripe_size.\n" \
186 "\tyaml_template_file:\n" \
187 "\t YAML layout template file, can't be used with -c,\n" \
188 "\t -i, -S, -p, -o, or -E arguments.\n"
190 #define MIRROR_CREATE_HELP \
191 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
192 "\t setstripe layout options\n" \
193 "\t It defaults to 1 if not specified; if specified,\n" \
194 "\t it must follow the option without a space.\n" \
195 "\t The option can also be repeated multiple times to\n" \
196 "\t separate mirrors that have different layouts.\n" \
197 "\tsetstripe options: Mirror layout\n" \
198 "\t It can be a plain layout or a composite layout.\n" \
199 "\t If not specified, the stripe options inherited\n" \
200 "\t from the previous component will be used.\n" \
201 "\tflags: set flags to the component of the current mirror.\n" \
202 "\t Only \"prefer\" flag is supported so far.\n"
204 #define MIRROR_EXTEND_HELP \
206 "\tvictim_file: The layout of victim_file will be split and used\n" \
207 "\t as a mirror added to the mirrored file.\n" \
208 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
209 "\t from victim file(s) in case the victim file(s)\n" \
210 "\t contains the same data as the original mirrored\n" \
213 #define MIRROR_EXTEND_USAGE \
214 " <--mirror-count|-N[mirror_count]>\n" \
215 " [setstripe options|-f <victim_file>]\n" \
218 #define SETSTRIPE_USAGE \
219 SSM_CMD_COMMON("setstripe") \
220 MIRROR_EXTEND_USAGE \
221 " <directory|filename>\n" \
225 #define MIGRATE_USAGE \
226 SSM_CMD_COMMON("migrate ") \
228 " [--non-block|-n]\n" \
229 " [--non-direct|-D]\n" \
233 "\tblock: Block file access during data migration (default)\n" \
234 "\tnon-block: Abort migrations if concurrent access is detected\n" \
235 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
237 #define SETDIRSTRIPE_USAGE \
238 " [--mdt-count|-c stripe_count>\n" \
239 " [--mdt-index|-i mdt_index]\n" \
240 " [--mdt-hash|-H mdt_hash]\n" \
241 " [--default|-D] [--mode|-m mode] <dir>\n" \
242 "\tstripe_count: stripe count of the striped directory\n" \
243 "\tmdt_index: MDT index of first stripe\n" \
244 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
245 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
246 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
247 "\tdefault_stripe: set default dirstripe of the directory\n" \
248 "\tmode: the mode of the directory\n"
251 * command_t mirror_cmdlist - lfs mirror commands.
253 command_t mirror_cmdlist[] = {
254 { .pc_name = "create", .pc_func = lfs_mirror_create,
255 .pc_help = "Create a mirrored file.\n"
256 "usage: lfs mirror create "
257 "<--mirror-count|-N[mirror_count]> "
258 "[setstripe options] ... <filename|directory>\n"
259 MIRROR_CREATE_HELP },
260 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
261 .pc_help = "Extend a mirrored file.\n"
262 "usage: lfs mirror extend "
263 "<--mirror-count|-N[mirror_count]> [--no-verify] "
264 "[setstripe options|-f <victim_file>] ... <filename>\n"
265 MIRROR_EXTEND_HELP },
266 { .pc_name = "split", .pc_func = lfs_mirror_split,
267 .pc_help = "Split a mirrored file.\n"
268 "usage: lfs mirror split <--mirror-id <mirror_id>> [--destroy|-d] "
269 "[-f <new_file>] <mirrored file>\n"
270 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
271 "\t can be fetched by lfs getstripe command.\n"
272 "\tnew_file: This option indicates the layout of the split\n"
273 "\t mirror will be stored into. If not specified,\n"
274 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
275 "\t will be used.\n" },
276 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
277 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
278 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
279 "<mirrored file> [<mirrored file2>...]\n"},
280 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
281 .pc_help = "Verify mirrored file(s).\n"
282 "usage: lfs mirror verify "
283 "[--only <mirror_id,mirror_id2[,...]>] "
284 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
285 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
286 .pc_help = "list commands supported by lfs mirror"},
287 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
288 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
289 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
293 /* all available commands */
294 command_t cmdlist[] = {
295 {"setstripe", lfs_setstripe, 0,
296 "To create a file with specified striping/composite layout, or\n"
297 "create/replace the default layout on an existing directory:\n"
298 SSM_CMD_COMMON("setstripe")
299 " <directory|filename>\n"
301 "To add component(s) to an existing composite file:\n"
302 SSM_CMD_COMMON("setstripe --component-add")
304 "To totally delete the default striping from an existing directory:\n"
305 "usage: setstripe -d <directory>\n"
307 "To delete the last component(s) from an existing composite file\n"
308 "(note that this will also delete any data in those components):\n"
309 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
310 " [--component-flags|-F <comp_flags>]\n"
312 "\tcomp_id: Unique component ID to delete\n"
313 "\tcomp_flags: 'init' indicating all instantiated components\n"
314 "\t '^init' indicating all uninstantiated components\n"
315 "\t-I and -F cannot be specified at the same time\n"},
316 {"getstripe", lfs_getstripe, 0,
317 "To list the layout pattern for a given file or files in a\n"
318 "directory or recursively for all files in a directory tree.\n"
319 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
320 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
321 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
322 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
323 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
324 " [--component-id[=comp_id]|-I[comp_id]]\n"
325 " [--component-flags[=comp_flags]]\n"
326 " [--component-count]\n"
327 " [--component-start[=[+-]comp_start]]\n"
328 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
329 " <directory|filename> ..."},
330 {"setdirstripe", lfs_setdirstripe, 0,
331 "To create a striped directory on a specified MDT. This can only\n"
332 "be done on MDT0 with the right of administrator.\n"
333 "usage: setdirstripe [OPTION] <directory>\n"
335 {"getdirstripe", lfs_getdirstripe, 0,
336 "To list the layout pattern info for a given directory\n"
337 "or recursively for all directories in a directory tree.\n"
338 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
339 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
340 " [--recursive|-r] [--yaml|-y]\n"
341 " [--default|-D] <dir> ..."},
342 {"mkdir", lfs_setdirstripe, 0,
343 "To create a striped directory on a specified MDT. This can only\n"
344 "be done on MDT0 with the right of administrator.\n"
345 "usage: mkdir [OPTION] <directory>\n"
347 {"rm_entry", lfs_rmentry, 0,
348 "To remove the name entry of the remote directory. Note: This\n"
349 "command will only delete the name entry, i.e. the remote directory\n"
350 "will become inaccessable after this command. This can only be done\n"
351 "by the administrator\n"
352 "usage: rm_entry <dir>\n"},
353 {"pool_list", lfs_poollist, 0,
354 "List pools or pool OSTs\n"
355 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
356 {"find", lfs_find, 0,
357 "find files matching given attributes recursively in directory tree.\n"
358 "usage: find <directory|filename> ...\n"
359 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
360 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N]\n"
361 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
362 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
363 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
364 " [[!] --stripe-count|-c [+-]<stripes>]\n"
365 " [[!] --stripe-index|-i <index,...>]\n"
366 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
367 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
368 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
369 " [[!] --projid <projid>]\n"
370 " [[!] --layout|-L released,raid0,mdt]\n"
371 " [[!] --component-count [+-]<comp_cnt>]\n"
372 " [[!] --component-start [+-]N[kMGTPE]]\n"
373 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
374 " [[!] --component-flags <comp_flags>]\n"
375 " [[!] --mirror-count|-N [+-]<n>]\n"
376 " [[!] --mirror-state <[^]state>]\n"
377 " [[!] --mdt-count|-T [+-]<stripes>]\n"
378 " [[!] --mdt-hash|-H <hashtype>\n"
379 "\t !: used before an option indicates 'NOT' requested attribute\n"
380 "\t -: used before a value indicates less than requested value\n"
381 "\t +: used before a value indicates more than requested value\n"
382 "\thashtype: hash type of the striped directory.\n"
383 "\t fnv_1a_64 FNV-1a hash algorithm\n"
384 "\t all_char sum of characters % MDT_COUNT\n"},
385 {"check", lfs_check, 0,
386 "Display the status of MDS or OSTs (as specified in the command)\n"
387 "or all the servers (MDS and OSTs).\n"
388 "usage: check <osts|mds|servers>"},
389 {"osts", lfs_osts, 0, "list OSTs connected to client "
390 "[for specified path only]\n" "usage: osts [path]"},
391 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
392 "[for specified path only]\n" "usage: mdts [path]"},
394 "report filesystem disk space usage or inodes usage"
395 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
396 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
397 {"getname", lfs_getname, 0, "list instances and specified mount points "
398 "[for specified path only]\n"
399 "Usage: getname [-h]|[path ...] "},
400 #ifdef HAVE_SYS_QUOTA_H
401 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
402 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
403 " -b <block-softlimit> -B <block-hardlimit>\n"
404 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
405 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
406 " [--block-softlimit <block-softlimit>]\n"
407 " [--block-hardlimit <block-hardlimit>]\n"
408 " [--inode-softlimit <inode-softlimit>]\n"
409 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
410 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
411 " [--block-grace <block-grace>]\n"
412 " [--inode-grace <inode-grace>] <filesystem>\n"
413 " -b can be used instead of --block-softlimit/--block-grace\n"
414 " -B can be used instead of --block-hardlimit\n"
415 " -i can be used instead of --inode-softlimit/--inode-grace\n"
416 " -I can be used instead of --inode-hardlimit\n\n"
417 "Note: The total quota space will be split into many qunits and\n"
418 " balanced over all server targets, the minimal qunit size is\n"
419 " 1M bytes for block space and 1K inodes for inode space.\n\n"
420 " Quota space rebalancing process will stop when this mininum\n"
421 " value is reached. As a result, quota exceeded can be returned\n"
422 " while many targets still have 1MB or 1K inodes of spare\n"
424 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
425 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
427 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
428 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
429 {"project", lfs_project, 0,
430 "Change or list project attribute for specified file or directory.\n"
431 "usage: project [-d|-r] <file|directory...>\n"
432 " list project ID and flags on file(s) or directories\n"
433 " project [-p id] [-s] [-r] <file|directory...>\n"
434 " set project ID and/or inherit flag for specified file(s) or directories\n"
435 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
436 " check project ID and flags on file(s) or directories, print outliers\n"
437 " project -C [-r] [-k] <file|directory...>\n"
438 " clear the project inherit flag and ID on the file or directory\n"
441 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
442 "usage: flushctx [-k] [mountpoint...]"},
444 "Remote user copy files and directories.\n"
445 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
447 "Remote user list directory contents.\n"
448 "usage: ls [OPTION]... [FILE]..."},
449 {"changelog", lfs_changelog, 0,
450 "Show the metadata changes on an MDT."
451 "\nusage: changelog <mdtname> [startrec [endrec]]"},
452 {"changelog_clear", lfs_changelog_clear, 0,
453 "Indicate that old changelog records up to <endrec> are no longer of "
454 "interest to consumer <id>, allowing the system to free up space.\n"
455 "An <endrec> of 0 means all records.\n"
456 "usage: changelog_clear <mdtname> <id> <endrec>"},
457 {"fid2path", lfs_fid2path, 0,
458 "Resolve the full path(s) for given FID(s). For a specific hardlink "
459 "specify link number <linkno>.\n"
460 /* "For a historical link name, specify changelog record <recno>.\n" */
461 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
462 /* [ --rec <recno> ] */ },
463 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
464 "usage: path2fid [--parents] <path> ..."},
465 {"data_version", lfs_data_version, 0, "Display file data version for "
466 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
467 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
468 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
469 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
470 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
471 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
472 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
474 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
475 "[--archived] [--lost] <file> ..."},
476 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
477 "given files.\n" "usage: hsm_action <file> ..."},
478 {"hsm_archive", lfs_hsm_archive, 0,
479 "Archive file to external storage.\n"
480 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
482 {"hsm_restore", lfs_hsm_restore, 0,
483 "Restore file from external storage.\n"
484 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
485 {"hsm_release", lfs_hsm_release, 0,
486 "Release files from Lustre.\n"
487 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
488 {"hsm_remove", lfs_hsm_remove, 0,
489 "Remove file copy from external storage.\n"
490 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
492 " (FILE [FILE ...] | "
493 "--mntpath MOUNTPATH FID [FID ...])\n"
495 "Note: To remove an archived copy of a file already deleted from a "
497 "--mntpath option and a list of FIDs must be specified"
499 {"hsm_cancel", lfs_hsm_cancel, 0,
500 "Cancel requests related to specified files.\n"
501 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
502 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
503 "usage: swap_layouts <path1> <path2>"},
504 {"migrate", lfs_setstripe_migrate, 0,
505 "migrate a directory between MDTs.\n"
506 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
508 "\tmdt_idx: index of the destination MDT\n"
510 "migrate file objects from one OST "
511 "layout\nto another (may be not safe with concurent writes).\n"
513 "[--stripe-count|-c] <stripe_count>\n"
514 " [--stripe-index|-i] <start_ost_index>\n"
515 " [--stripe-size|-S] <stripe_size>\n"
516 " [--pool|-p] <pool_name>\n"
517 " [--ost|-o] <ost_indices>\n"
519 " [--non-block|-n]\n"
520 " [--non-direct|-D]\n"
521 " <file|directory>\n"
522 "\tstripe_count: number of OSTs to stripe a file over\n"
523 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
524 "\tstripe_size: number of bytes to store before moving to the next OST\n"
525 "\tpool_name: name of the predefined pool of OSTs\n"
526 "\tost_indices: OSTs to stripe over, in order\n"
527 "\tblock: wait for the operation to return before continuing\n"
528 "\tnon-block: do not wait for the operation to return\n"
529 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
531 "To move directories between MDTs. This command is deprecated, "
532 "use \"migrate\" instead.\n"
533 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
535 {"ladvise", lfs_ladvise, 0,
536 "Provide servers with advice about access patterns for a file.\n"
537 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
538 " [--background|-b] [--unset|-u]\n\n"
539 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
540 " {[--mode|-m [READ,WRITE]}\n"
542 {"mirror", lfs_mirror, mirror_cmdlist,
543 "lfs commands used to manage files with mirrored components:\n"
544 "lfs mirror create - create a mirrored file or directory\n"
545 "lfs mirror extend - add mirror(s) to an existing file\n"
546 "lfs mirror split - split a mirror from an existing mirrored file\n"
547 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
548 "lfs mirror verify - verify mirrored file(s)\n"},
549 {"help", Parser_help, 0, "help"},
550 {"exit", Parser_quit, 0, "quit"},
551 {"quit", Parser_quit, 0, "quit"},
552 {"--version", Parser_version, 0,
553 "output build version of the utility and exit"},
554 {"--list-commands", lfs_list_commands, 0,
555 "list commands supported by the utility and exit"},
560 static int check_hashtype(const char *hashtype)
564 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
565 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
572 static const char *error_loc = "syserror";
575 MIGRATION_NONBLOCK = 1 << 0,
576 MIGRATION_MIRROR = 1 << 1,
577 MIGRATION_NONDIRECT = 1 << 2,
580 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
581 struct llapi_layout *layout);
584 migrate_open_files(const char *name, __u64 migration_flags,
585 const struct llapi_stripe_param *param,
586 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
593 char parent[PATH_MAX];
594 char volatile_file[PATH_MAX];
600 if (param == NULL && layout == NULL) {
601 error_loc = "layout information";
605 /* search for file directory pathname */
606 if (strlen(name) > sizeof(parent) - 1) {
607 error_loc = "source file name";
611 strncpy(parent, name, sizeof(parent));
612 ptr = strrchr(parent, '/');
614 if (getcwd(parent, sizeof(parent)) == NULL) {
615 error_loc = "getcwd";
619 if (ptr == parent) /* leading '/' */
624 /* open file, direct io */
625 /* even if the file is only read, WR mode is nedeed to allow
626 * layout swap on fd */
628 if (!(migration_flags & MIGRATION_NONDIRECT))
630 fd = open(name, rflags);
633 error_loc = "cannot open source file";
637 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
639 error_loc = "cannot get MDT index";
644 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
645 mode_t open_mode = S_IRUSR | S_IWUSR;
647 random_value = random();
648 rc = snprintf(volatile_file, sizeof(volatile_file),
649 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
650 mdt_index, random_value);
651 if (rc >= sizeof(volatile_file)) {
656 /* create, open a volatile file, use caching (ie no directio) */
658 fdv = llapi_file_open_param(volatile_file, open_flags,
661 fdv = lfs_component_create(volatile_file, open_flags,
663 } while (fdv < 0 && (rc = fdv) == -EEXIST);
666 error_loc = "cannot create volatile file";
670 /* In case the MDT does not support creation of volatile files
671 * we should try to unlink it. */
672 (void)unlink(volatile_file);
674 /* Not-owner (root?) special case.
675 * Need to set owner/group of volatile file like original.
676 * This will allow to pass related check during layout_swap.
681 error_loc = "cannot stat source file";
685 rc = fstat(fdv, &stv);
688 error_loc = "cannot stat volatile";
692 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
693 rc = fchown(fdv, st.st_uid, st.st_gid);
696 error_loc = "cannot change ownwership of volatile";
715 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
717 struct llapi_layout *layout;
718 size_t buf_size = 4 * 1024 * 1024;
727 layout = llapi_layout_get_by_fd(fd_src, 0);
728 if (layout != NULL) {
729 uint64_t stripe_size;
731 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
733 buf_size = stripe_size;
735 llapi_layout_free(layout);
738 /* Use a page-aligned buffer for direct I/O */
739 rc = posix_memalign(&buf, getpagesize(), buf_size);
744 /* read new data only if we have written all
745 * previously read data */
748 rc = check_file(fd_src);
753 rsize = read(fd_src, buf, buf_size);
765 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
784 static int migrate_copy_timestamps(int fd, int fdv)
788 if (fstat(fd, &st) == 0) {
789 struct timeval tv[2] = {
790 {.tv_sec = st.st_atime},
791 {.tv_sec = st.st_mtime}
794 return futimes(fdv, tv);
800 static int migrate_block(int fd, int fdv)
807 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
809 error_loc = "cannot get dataversion";
817 /* The grouplock blocks all concurrent accesses to the file.
818 * It has to be taken after llapi_get_data_version as it would
820 rc = llapi_group_lock(fd, gid);
822 error_loc = "cannot get group lock";
826 rc = migrate_copy_data(fd, fdv, NULL);
828 error_loc = "data copy failed";
832 /* Make sure we keep original atime/mtime values */
833 rc = migrate_copy_timestamps(fd, fdv);
835 error_loc = "timestamp copy failed";
840 * for a migration we need to check data version on file did
843 * Pass in gid=0 since we already own grouplock. */
844 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
845 SWAP_LAYOUTS_CHECK_DV1);
847 error_loc = "file changed";
850 error_loc = "cannot swap layout";
855 rc2 = llapi_group_unlock(fd, gid);
856 if (rc2 < 0 && rc == 0) {
857 error_loc = "unlock group lock";
865 * Internal helper for migrate_copy_data(). Check lease and report error if
868 * \param[in] fd File descriptor on which to check the lease.
870 * \retval 0 Migration can keep on going.
871 * \retval -errno Error occurred, abort migration.
873 static int check_lease(int fd)
877 rc = llapi_lease_check(fd);
879 return 0; /* llapi_check_lease returns > 0 on success. */
884 static int migrate_nonblock(int fd, int fdv)
890 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
892 error_loc = "cannot get data version";
896 rc = migrate_copy_data(fd, fdv, check_lease);
898 error_loc = "data copy failed";
902 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
904 error_loc = "cannot get data version";
910 error_loc = "source file changed";
914 /* Make sure we keep original atime/mtime values */
915 rc = migrate_copy_timestamps(fd, fdv);
917 error_loc = "timestamp copy failed";
924 static int lfs_component_set(char *fname, int comp_id,
925 __u32 flags, __u32 neg_flags)
928 __u32 flags_array[2];
933 ids[count] = comp_id;
934 flags_array[count] = flags;
939 ids[count] = comp_id;
940 flags_array[count] = neg_flags | LCME_FL_NEG;
944 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
947 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
948 progname, comp_id, fname, flags, neg_flags);
953 static int lfs_component_del(char *fname, __u32 comp_id,
954 __u32 flags, __u32 neg_flags)
958 if (flags && neg_flags)
961 if (!flags && neg_flags)
962 flags = neg_flags | LCME_FL_NEG;
964 if ((flags && comp_id) || (!flags && !comp_id))
967 /* LCME_FL_INIT is the only supported flag in PFL */
969 if (flags & ~LCME_KNOWN_FLAGS) {
971 "%s setstripe: unknown flags %#x\n",
975 } else if (comp_id > LCME_ID_MAX) {
976 fprintf(stderr, "%s setstripe: invalid component id %u\n",
981 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
984 "%s setstripe: cannot delete component %#x from '%s': %s\n",
985 progname, comp_id, fname, strerror(errno));
989 static int lfs_component_add(char *fname, struct llapi_layout *layout)
996 rc = llapi_layout_file_comp_add(fname, layout);
998 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
999 fname, strerror(errno));
1003 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1004 struct llapi_layout *layout)
1012 fd = lstat(fname, &st);
1013 if (fd == 0 && S_ISDIR(st.st_mode))
1014 open_flags = O_DIRECTORY | O_RDONLY;
1016 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1018 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1019 S_ISDIR(st.st_mode) ?
1020 "set default composite layout for" :
1021 "create composite file",
1022 fname, strerror(errno));
1026 static int lfs_migrate(char *name, __u64 migration_flags,
1027 struct llapi_stripe_param *param,
1028 struct llapi_layout *layout)
1034 rc = migrate_open_files(name, migration_flags, param, layout,
1039 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1040 /* Blocking mode (forced if servers do not support file lease).
1041 * It is also the default mode, since we cannot distinguish
1042 * between a broken lease and a server that does not support
1043 * atomic swap/close (LU-6785) */
1044 rc = migrate_block(fd, fdv);
1048 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1050 error_loc = "cannot get lease";
1054 rc = migrate_nonblock(fd, fdv);
1056 llapi_lease_release(fd);
1060 /* Atomically put lease, swap layouts and close.
1061 * for a migration we need to check data version on file did
1063 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1065 error_loc = "cannot swap layout";
1077 fprintf(stderr, "error: %s: %s: %s: %s\n",
1078 progname, name, error_loc, strerror(-rc));
1082 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1091 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1095 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1096 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1097 const char *comp_name = comp_flags_table[i].cfn_name;
1099 if (strcmp(name, comp_name) == 0) {
1100 *flags |= comp_flag;
1102 } else if (strncmp(name, "^", 1) == 0 &&
1103 strcmp(name + 1, comp_name) == 0) {
1104 *neg_flags |= comp_flag;
1109 llapi_printf(LLAPI_MSG_ERROR,
1110 "%s: component flag '%s' not supported\n",
1116 if (!*flags && !*neg_flags)
1119 /* don't allow to set and exclude the same flag */
1120 if (*flags & *neg_flags)
1126 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1134 if (strncmp(string, "^", 1) == 0) {
1135 *neg_state = llapi_layout_string_flags(string + 1);
1136 if (*neg_state != 0)
1139 *state = llapi_layout_string_flags(string);
1144 llapi_printf(LLAPI_MSG_ERROR,
1145 "%s: mirrored file state '%s' not supported\n",
1151 * struct mirror_args - Command-line arguments for mirror(s).
1152 * @m_count: Number of mirrors to be created with this layout.
1153 * @m_flags: Mirror level flags, only 'prefer' is supported.
1154 * @m_layout: Mirror layout.
1155 * @m_file: A victim file. Its layout will be split and used as a mirror.
1156 * @m_next: Point to the next node of the list.
1158 * Command-line arguments for mirror(s) will be parsed and stored in
1159 * a linked list that consists of this structure.
1161 struct mirror_args {
1164 struct llapi_layout *m_layout;
1166 struct mirror_args *m_next;
1169 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1174 rc = llapi_layout_comp_flags_get(layout, &flags);
1178 if (flags & LCME_FL_NEG) {
1179 fprintf(stderr, "error: %s: negative flags are not supported\n",
1184 if (flags & LCME_FL_STALE) {
1185 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1186 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1190 return LLAPI_LAYOUT_ITER_CONT;
1193 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1195 uint64_t start, end;
1199 /* LU-10112: do not support dom+flr in phase 1 */
1200 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1204 rc = llapi_layout_pattern_get(layout, &pattern);
1208 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1209 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1214 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1218 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1222 if (end != LUSTRE_EOF) {
1223 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1228 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1233 * enum mirror_flags - Flags for extending a mirrored file.
1234 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1235 * in case the victim file(s) contains the same data as the
1236 * original mirrored file.
1237 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1239 * Flags for extending a mirrored file.
1247 * mirror_create_sanity_check() - Check mirror list.
1248 * @list: A linked list that stores the mirror arguments.
1250 * This function does a sanity check on @list for creating
1253 * Return: 0 on success or a negative error code on failure.
1255 static int mirror_create_sanity_check(const char *fname,
1256 struct mirror_args *list)
1259 bool has_m_file = false;
1260 bool has_m_layout = false;
1266 struct llapi_layout *layout;
1268 layout = llapi_layout_get_by_path(fname, 0);
1271 "error: %s: file '%s' couldn't get layout\n",
1276 rc = mirror_sanity_check_one(layout);
1277 llapi_layout_free(layout);
1283 while (list != NULL) {
1284 if (list->m_file != NULL) {
1286 llapi_layout_free(list->m_layout);
1289 llapi_layout_get_by_path(list->m_file, 0);
1290 if (list->m_layout == NULL) {
1292 "error: %s: file '%s' has no layout\n",
1293 progname, list->m_file);
1297 has_m_layout = true;
1298 if (list->m_layout == NULL) {
1299 fprintf(stderr, "error: %s: no mirror layout\n",
1305 rc = mirror_sanity_check_one(list->m_layout);
1309 list = list->m_next;
1312 if (has_m_file && has_m_layout) {
1314 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1322 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1324 __u32 mirror_flags = *(__u32 *)cbdata;
1328 rc = llapi_layout_comp_flags_get(layout, &flags);
1333 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1338 return LLAPI_LAYOUT_ITER_CONT;
1342 * mirror_create() - Create a mirrored file.
1343 * @fname: The file to be created.
1344 * @mirror_list: A linked list that stores the mirror arguments.
1346 * This function creates a mirrored file @fname with the mirror(s)
1347 * from @mirror_list.
1349 * Return: 0 on success or a negative error code on failure.
1351 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1353 struct llapi_layout *layout = NULL;
1354 struct mirror_args *cur_mirror = NULL;
1355 uint16_t mirror_count = 0;
1359 rc = mirror_create_sanity_check(NULL, mirror_list);
1363 cur_mirror = mirror_list;
1364 while (cur_mirror != NULL) {
1365 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1367 &cur_mirror->m_flags);
1370 fprintf(stderr, "%s: failed to set mirror flags\n",
1375 for (i = 0; i < cur_mirror->m_count; i++) {
1376 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1379 fprintf(stderr, "error: %s: "
1380 "merge layout failed: %s\n",
1381 progname, strerror(errno));
1385 mirror_count += cur_mirror->m_count;
1386 cur_mirror = cur_mirror->m_next;
1389 if (layout == NULL) {
1390 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1394 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1397 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1398 progname, strerror(errno));
1402 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0644,
1410 llapi_layout_free(layout);
1415 * Compare files and check lease on @fd.
1417 * \retval bytes number of bytes are the same
1419 static ssize_t mirror_file_compare(int fd, int fdv)
1421 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1423 ssize_t bytes_done = 0;
1424 ssize_t bytes_read = 0;
1426 buf = malloc(buflen * 2);
1431 if (!llapi_lease_check(fd)) {
1432 bytes_done = -EBUSY;
1436 bytes_read = read(fd, buf, buflen);
1437 if (bytes_read <= 0)
1440 if (bytes_read != read(fdv, buf + buflen, buflen))
1443 /* XXX: should compute the checksum on each buffer and then
1444 * compare checksum to avoid cache collision */
1445 if (memcmp(buf, buf + buflen, bytes_read))
1448 bytes_done += bytes_read;
1456 static int mirror_extend_file(const char *fname, const char *victim_file,
1457 enum mirror_flags mirror_flags)
1462 struct stat stbuf_v;
1463 struct ll_ioc_lease *data = NULL;
1466 fd = open(fname, O_RDWR);
1468 error_loc = "open source file";
1473 fdv = open(victim_file, O_RDWR);
1475 error_loc = "open target file";
1480 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1481 error_loc = "stat source or target file";
1486 if (stbuf.st_dev != stbuf_v.st_dev) {
1487 error_loc = "stat source and target file";
1492 /* mirrors should be of the same size */
1493 if (stbuf.st_size != stbuf_v.st_size) {
1494 error_loc = "file sizes don't match";
1499 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1501 error_loc = "cannot get lease";
1505 if (!(mirror_flags & MF_NO_VERIFY)) {
1507 /* mirrors should have the same contents */
1508 ret = mirror_file_compare(fd, fdv);
1509 if (ret != stbuf.st_size) {
1510 error_loc = "file busy or contents don't match";
1511 rc = ret < 0 ? ret : -EINVAL;
1516 /* Get rid of caching pages from clients */
1517 rc = llapi_file_flush(fd);
1519 error_loc = "cannot get data version";
1523 rc = llapi_file_flush(fdv);
1525 error_loc = "cannot get data version";
1530 /* Make sure we keep original atime/mtime values */
1531 rc = migrate_copy_timestamps(fd, fdv);
1533 error_loc = "cannot copy timestamp";
1537 /* Atomically put lease, merge layouts and close. */
1538 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1540 error_loc = "memory allocation";
1543 data->lil_mode = LL_LEASE_UNLCK;
1544 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1545 data->lil_count = 1;
1546 data->lil_ids[0] = fdv;
1547 rc = llapi_lease_set(fd, data);
1549 error_loc = "cannot merge layout";
1551 } else if (rc == 0) {
1553 error_loc = "lost lease lock";
1566 (void) unlink(victim_file);
1568 fprintf(stderr, "error: %s: %s: %s: %s\n",
1569 progname, fname, error_loc, strerror(-rc));
1573 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1575 struct ll_ioc_lease *data = NULL;
1580 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1584 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1586 error_loc = "cannot get lease";
1590 rc = migrate_nonblock(fd, fdv);
1592 llapi_lease_release(fd);
1596 /* Atomically put lease, merge layouts and close. */
1597 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1599 error_loc = "memory allocation";
1602 data->lil_mode = LL_LEASE_UNLCK;
1603 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1604 data->lil_count = 1;
1605 data->lil_ids[0] = fdv;
1606 rc = llapi_lease_set(fd, data);
1608 error_loc = "cannot merge layout";
1610 } else if (rc == 0) {
1612 error_loc = "lost lease lock";
1625 fprintf(stderr, "error: %s: %s: %s: %s\n",
1626 progname, name, error_loc, strerror(-rc));
1630 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1631 enum mirror_flags mirror_flags)
1635 rc = mirror_create_sanity_check(fname, mirror_list);
1639 while (mirror_list) {
1640 if (mirror_list->m_file != NULL) {
1641 rc = mirror_extend_file(fname, mirror_list->m_file,
1644 __u32 mirror_count = mirror_list->m_count;
1646 while (mirror_count > 0) {
1647 rc = mirror_extend_layout(fname,
1648 mirror_list->m_layout);
1658 mirror_list = mirror_list->m_next;
1664 static int verify_id(struct llapi_layout *layout, void *cbdata)
1669 rc = llapi_layout_mirror_id_get(layout, &id);
1673 if ((__u16)id == *(__u16 *)cbdata)
1674 return LLAPI_LAYOUT_ITER_STOP;
1676 return LLAPI_LAYOUT_ITER_CONT;
1679 static int mirror_split(const char *fname, __u16 mirror_id,
1680 enum mirror_flags mflags, const char *victim_file)
1682 struct llapi_layout *layout;
1683 char parent[PATH_MAX];
1684 char victim[PATH_MAX];
1685 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1687 struct ll_ioc_lease *data;
1688 uint16_t mirror_count;
1693 /* check fname contains mirror with mirror_id */
1694 layout = llapi_layout_get_by_path(fname, 0);
1697 "error %s: file '%s' couldn't get layout\n",
1702 rc = mirror_sanity_check_one(layout);
1706 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1709 "error %s: file '%s' couldn't get mirror count\n",
1713 if (mirror_count < 2) {
1715 "error %s: file '%s' has %d component, cannot split\n",
1716 progname, fname, mirror_count);
1720 rc = llapi_layout_comp_iterate(layout, verify_id, &mirror_id);
1722 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1725 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1727 "error %s: file '%s' does not contain mirror with id %u\n",
1728 progname, fname, mirror_id);
1732 fd = open(fname, O_RDWR);
1735 "error %s: open file '%s' failed: %s\n",
1736 progname, fname, strerror(errno));
1740 /* get victim file directory pathname */
1741 if (strlen(fname) > sizeof(parent) - 1) {
1742 fprintf(stderr, "error %s: file name of '%s' too long\n",
1747 strncpy(parent, fname, sizeof(parent));
1748 ptr = strrchr(parent, '/');
1750 if (getcwd(parent, sizeof(parent)) == NULL) {
1751 fprintf(stderr, "error %s: getcwd failed: %s\n",
1752 progname, strerror(errno));
1762 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1764 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1769 if (victim_file == NULL) {
1770 /* use a temp file to store the splitted layout */
1771 if (mflags & MF_DESTROY) {
1772 fdv = llapi_create_volatile_idx(parent, mdt_index,
1773 O_LOV_DELAY_CREATE);
1775 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1777 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1780 /* user specified victim file */
1781 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1786 "error %s: create victim file failed: %s\n",
1787 progname, strerror(errno));
1791 /* get lease lock of fname */
1792 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1795 "error %s: cannot get lease of file '%s': %d\n",
1796 progname, fname, rc);
1800 /* Atomatically put lease, split layouts and close. */
1801 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1807 data->lil_mode = LL_LEASE_UNLCK;
1808 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1809 data->lil_count = 2;
1810 data->lil_ids[0] = fdv;
1811 data->lil_ids[1] = mirror_id;
1812 rc = llapi_lease_set(fd, data);
1814 if (rc == 0) /* lost lease lock */
1817 "error %s: cannot split '%s': %s\n",
1818 progname, fname, strerror(-rc));
1829 llapi_layout_free(layout);
1834 * Parse a string containing an target index list into an array of integers.
1836 * The input string contains a comma delimited list of individual
1837 * indices and ranges, for example "1,2-4,7". Add the indices into the
1838 * \a tgts array and remove duplicates.
1840 * \param[out] tgts array to store indices in
1841 * \param[in] size size of \a tgts array
1842 * \param[in] offset starting index in \a tgts
1843 * \param[in] arg string containing OST index list
1845 * \retval positive number of indices in \a tgts
1846 * \retval -EINVAL unable to parse \a arg
1848 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1852 int slots = size - offset;
1859 end_of_loop = false;
1860 while (!end_of_loop) {
1864 char *endptr = NULL;
1868 ptr = strchrnul(arg, ',');
1870 end_of_loop = *ptr == '\0';
1873 start_index = strtol(arg, &endptr, 0);
1874 if (endptr == arg) /* no data at all */
1876 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1879 end_index = start_index;
1880 if (*endptr == '-') {
1881 end_index = strtol(endptr + 1, &endptr, 0);
1882 if (*endptr != '\0')
1884 if (end_index < start_index)
1888 for (i = start_index; i <= end_index && slots > 0; i++) {
1891 /* remove duplicate */
1892 for (j = 0; j < offset; j++) {
1896 if (j == offset) { /* no duplicate */
1901 if (slots == 0 && i < end_index)
1909 if (!end_of_loop && ptr != NULL)
1912 return rc < 0 ? rc : nr;
1915 struct lfs_setstripe_args {
1916 unsigned long long lsa_comp_end;
1917 unsigned long long lsa_stripe_size;
1918 long long lsa_stripe_count;
1919 long long lsa_stripe_off;
1920 __u32 lsa_comp_flags;
1921 __u32 lsa_comp_neg_flags;
1922 unsigned long long lsa_pattern;
1923 unsigned int lsa_mirror_count;
1925 bool lsa_first_comp;
1927 char *lsa_pool_name;
1930 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1932 unsigned int mirror_count = lsa->lsa_mirror_count;
1933 bool first_comp = lsa->lsa_first_comp;
1935 memset(lsa, 0, sizeof(*lsa));
1937 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1938 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1939 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1940 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1941 lsa->lsa_pool_name = NULL;
1943 lsa->lsa_mirror_count = mirror_count;
1944 lsa->lsa_first_comp = first_comp;
1948 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1949 * @lsa: Stripe options to be initialized and inherited.
1951 * This function initializes stripe options in @lsa and inherit
1952 * stripe_size, stripe_count and OST pool_name options.
1956 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1958 unsigned long long stripe_size;
1959 long long stripe_count;
1960 char *pool_name = NULL;
1962 stripe_size = lsa->lsa_stripe_size;
1963 stripe_count = lsa->lsa_stripe_count;
1964 pool_name = lsa->lsa_pool_name;
1966 setstripe_args_init(lsa);
1968 lsa->lsa_stripe_size = stripe_size;
1969 lsa->lsa_stripe_count = stripe_count;
1970 lsa->lsa_pool_name = pool_name;
1973 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1975 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1976 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1977 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1978 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1979 lsa->lsa_pool_name != NULL ||
1980 lsa->lsa_comp_end != 0);
1984 * comp_args_to_layout() - Create or extend a composite layout.
1985 * @composite: Pointer to the composite layout.
1986 * @lsa: Stripe options for the new component.
1988 * This function creates or extends a composite layout by adding a new
1989 * component with stripe options from @lsa.
1991 * Return: 0 on success or an error code on failure.
1993 static int comp_args_to_layout(struct llapi_layout **composite,
1994 struct lfs_setstripe_args *lsa,
1997 struct llapi_layout *layout = *composite;
1998 uint64_t prev_end = 0;
2001 if (layout == NULL) {
2002 layout = llapi_layout_alloc();
2003 if (layout == NULL) {
2004 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2008 *composite = layout;
2012 /* Get current component extent, current component
2013 * must be the tail component. */
2014 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2016 fprintf(stderr, "Get comp extent failed. %s\n",
2021 if (lsa->lsa_first_comp)
2024 if (lsa->lsa_first_comp)
2025 rc = llapi_layout_add_first_comp(layout);
2027 rc = llapi_layout_comp_add(layout);
2029 fprintf(stderr, "Add component failed. %s\n",
2034 /* reset lsa_first_comp */
2035 lsa->lsa_first_comp = false;
2038 rc = llapi_layout_comp_extent_set(layout, prev_end,
2041 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2042 prev_end, lsa->lsa_comp_end, strerror(errno));
2047 /* Data-on-MDT component setting */
2048 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2049 /* In case of Data-on-MDT patterns the only extra option
2050 * applicable is stripe size option. */
2051 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2052 fprintf(stderr, "Option 'stripe-count' can't be "
2053 "specified with Data-on-MDT component: %lld\n",
2054 lsa->lsa_stripe_count);
2057 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2058 fprintf(stderr, "Option 'stripe-size' can't be "
2059 "specified with Data-on-MDT component: %llu\n",
2060 lsa->lsa_stripe_size);
2063 if (lsa->lsa_nr_tgts != 0) {
2064 fprintf(stderr, "Option 'ost-list' can't be specified "
2065 "with Data-on-MDT component: '%i'\n",
2069 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2070 fprintf(stderr, "Option 'stripe-offset' can't be "
2071 "specified with Data-on-MDT component: %lld\n",
2072 lsa->lsa_stripe_off);
2075 if (lsa->lsa_pool_name != 0) {
2076 fprintf(stderr, "Option 'pool' can't be specified "
2077 "with Data-on-MDT component: '%s'\n",
2078 lsa->lsa_pool_name);
2082 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2084 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2085 lsa->lsa_pattern, strerror(errno));
2088 /* Data-on-MDT component has always single stripe up to end */
2089 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2092 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2094 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2095 lsa->lsa_stripe_size, strerror(errno));
2099 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2101 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2102 lsa->lsa_stripe_count, strerror(errno));
2106 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2108 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2109 lsa->lsa_comp_flags, strerror(errno));
2113 if (lsa->lsa_pool_name != NULL) {
2114 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2116 fprintf(stderr, "Set pool name: %s failed. %s\n",
2117 lsa->lsa_pool_name, strerror(errno));
2121 rc = llapi_layout_pool_name_set(layout, "");
2123 fprintf(stderr, "Clear pool name failed: %s\n",
2129 if (lsa->lsa_nr_tgts > 0) {
2130 if (lsa->lsa_stripe_count > 0 &&
2131 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2132 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2133 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2134 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2135 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2138 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2139 rc = llapi_layout_ost_index_set(layout, i,
2144 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2145 lsa->lsa_stripe_off != -1) {
2146 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2149 fprintf(stderr, "Set ost index %d failed. %s\n",
2150 i, strerror(errno));
2157 static int build_component(struct llapi_layout **layout,
2158 struct lfs_setstripe_args *lsa, bool set_extent)
2162 rc = comp_args_to_layout(layout, lsa, set_extent);
2166 if (lsa->lsa_mirror_count > 0) {
2167 rc = llapi_layout_mirror_count_set(*layout,
2168 lsa->lsa_mirror_count);
2172 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2175 lsa->lsa_mirror_count = 0;
2181 static int build_layout_from_yaml_node(struct cYAML *node,
2182 struct llapi_layout **layout,
2183 struct lfs_setstripe_args *lsa,
2190 string = node->cy_string;
2191 /* skip leading lmm_ if present, to simplify parsing */
2192 if (string != NULL && strncmp(string, "lmm_", 4) == 0)
2195 if (node->cy_type == CYAML_TYPE_STRING) {
2196 if (!strcmp(string, "lcme_extent.e_end")) {
2197 if (!strcmp(node->cy_valuestring, "EOF") ||
2198 !strcmp(node->cy_valuestring, "eof"))
2199 lsa->lsa_comp_end = LUSTRE_EOF;
2200 } else if (!strcmp(string, "pool")) {
2201 lsa->lsa_pool_name = node->cy_valuestring;
2202 } else if (!strcmp(string, "pattern")) {
2203 if (!strcmp(node->cy_valuestring, "mdt"))
2204 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2206 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2207 if (!strcmp(string, "lcm_mirror_count")) {
2208 lsa->lsa_mirror_count = node->cy_valueint;
2209 } else if (!strcmp(string, "lcme_extent.e_start")) {
2210 if (node->cy_valueint != 0 || *layout != NULL) {
2211 rc = build_component(layout, lsa, true);
2216 if (node->cy_valueint == 0)
2217 lsa->lsa_first_comp = true;
2219 /* initialize lsa */
2220 setstripe_args_init(lsa);
2221 lsa->lsa_tgts = osts;
2222 } else if (!strcmp(string, "lcme_extent.e_end")) {
2223 if (node->cy_valueint == -1)
2224 lsa->lsa_comp_end = LUSTRE_EOF;
2226 lsa->lsa_comp_end = node->cy_valueint;
2227 } else if (!strcmp(string, "stripe_count")) {
2228 lsa->lsa_stripe_count = node->cy_valueint;
2229 } else if (!strcmp(string, "stripe_size")) {
2230 lsa->lsa_stripe_size = node->cy_valueint;
2231 } else if (!strcmp(string, "stripe_offset")) {
2232 lsa->lsa_stripe_off = node->cy_valueint;
2233 } else if (!strcmp(string, "l_ost_idx")) {
2234 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2237 } else if (node->cy_type == CYAML_TYPE_OBJECT) {
2238 /* go deep to sub blocks */
2239 rc = build_layout_from_yaml_node(node->cy_child, layout,
2244 node = node->cy_next;
2250 static int lfs_comp_create_from_yaml(char *template,
2251 struct llapi_layout **layout,
2252 struct lfs_setstripe_args *lsa,
2255 struct cYAML *tree = NULL, *err_rc = NULL;
2258 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2260 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2261 progname, template);
2262 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2263 "can't parse", &err_rc);
2264 cYAML_print_tree2file(stderr, err_rc);
2265 cYAML_free_tree(err_rc);
2270 /* initialize lsa for plain file */
2271 setstripe_args_init(lsa);
2272 lsa->lsa_tgts = osts;
2274 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2276 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2277 progname, template);
2280 rc = build_component(layout, lsa, *layout != NULL);
2282 /* clean clean lsa */
2283 setstripe_args_init(lsa);
2287 cYAML_free_tree(tree);
2291 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2292 * end of the last component in the existing file, and adjust the
2293 * first extent start of the components to be added accordingly. */
2294 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2296 struct llapi_layout *head;
2297 uint64_t start, end, stripe_size, prev_end = 0;
2300 if (layout == NULL) {
2302 "%s setstripe: layout must be specified\n",
2308 head = llapi_layout_get_by_path(fname, 0);
2311 "%s setstripe: cannot read layout from '%s': %s\n",
2312 progname, fname, strerror(errno));
2314 } else if (errno == ENODATA) {
2315 /* file without LOVEA, this component-add will be turned
2316 * into a component-create. */
2317 llapi_layout_free(head);
2319 } else if (!llapi_layout_is_composite(head)) {
2320 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2322 llapi_layout_free(head);
2326 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2328 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2329 progname, strerror(errno));
2330 llapi_layout_free(head);
2334 llapi_layout_free(head);
2336 /* Make sure we use the first component of the layout to be added. */
2337 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2340 "%s setstripe: cannot move component cursor: %s\n",
2341 progname, strerror(errno));
2345 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2347 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2348 progname, strerror(errno));
2352 if (start > prev_end || end <= prev_end) {
2354 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2355 progname, start, end, prev_end);
2359 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2361 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2362 progname, strerror(errno));
2366 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2367 (prev_end & (stripe_size - 1))) {
2369 "%s setstripe: stripe size %lu not aligned with %lu\n",
2370 progname, stripe_size, prev_end);
2374 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2377 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2378 progname, prev_end, end, strerror(errno));
2385 static inline bool arg_is_eof(char *arg)
2387 return !strncmp(arg, "-1", strlen("-1")) ||
2388 !strncmp(arg, "EOF", strlen("EOF")) ||
2389 !strncmp(arg, "eof", strlen("eof"));
2393 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2395 * Return: Valid mirror_args pointer on success and
2396 * NULL if memory allocation fails.
2398 static struct mirror_args *lfs_mirror_alloc(void)
2400 struct mirror_args *mirror = NULL;
2403 mirror = calloc(1, sizeof(*mirror));
2414 * lfs_mirror_free() - Free memory allocated for a mirror argument
2416 * @mirror: Previously allocated mirror argument structure by
2417 * lfs_mirror_alloc().
2419 * Free memory allocated for @mirror.
2423 static void lfs_mirror_free(struct mirror_args *mirror)
2425 if (mirror->m_layout != NULL)
2426 llapi_layout_free(mirror->m_layout);
2431 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2432 * @mirror_list: Previously allocated mirror list.
2434 * Free memory allocated for @mirror_list.
2438 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2440 struct mirror_args *next_mirror = NULL;
2442 while (mirror_list != NULL) {
2443 next_mirror = mirror_list->m_next;
2444 lfs_mirror_free(mirror_list);
2445 mirror_list = next_mirror;
2457 LFS_COMP_NO_VERIFY_OPT,
2459 LFS_MIRROR_FLAGS_OPT,
2461 LFS_MIRROR_STATE_OPT,
2465 static int lfs_setstripe_internal(int argc, char **argv,
2466 enum setstripe_origin opc)
2468 struct lfs_setstripe_args lsa = { 0 };
2469 struct llapi_stripe_param *param = NULL;
2470 struct find_param migrate_mdt_param = {
2480 char *mdt_idx_arg = NULL;
2481 unsigned long long size_units = 1;
2482 bool migrate_mode = false;
2483 bool migration_block = false;
2484 __u64 migration_flags = 0;
2485 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2486 int comp_del = 0, comp_set = 0;
2489 struct llapi_layout *layout = NULL;
2490 struct llapi_layout **lpp = &layout;
2491 bool mirror_mode = false;
2492 bool has_m_file = false;
2493 __u32 mirror_count = 0;
2494 enum mirror_flags mirror_flags = 0;
2495 struct mirror_args *mirror_list = NULL;
2496 struct mirror_args *new_mirror = NULL;
2497 struct mirror_args *last_mirror = NULL;
2498 __u16 mirror_id = 0;
2500 bool from_yaml = false;
2501 char *template = NULL;
2503 struct option long_opts[] = {
2504 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2505 /* --block is only valid in migrate mode */
2506 { .val = 'b', .name = "block", .has_arg = no_argument },
2507 { .val = LFS_COMP_ADD_OPT,
2508 .name = "comp-add", .has_arg = no_argument },
2509 { .val = LFS_COMP_ADD_OPT,
2510 .name = "component-add", .has_arg = no_argument },
2511 { .val = LFS_COMP_DEL_OPT,
2512 .name = "comp-del", .has_arg = no_argument },
2513 { .val = LFS_COMP_DEL_OPT,
2514 .name = "component-del", .has_arg = no_argument },
2515 { .val = LFS_COMP_FLAGS_OPT,
2516 .name = "comp-flags", .has_arg = required_argument },
2517 { .val = LFS_COMP_FLAGS_OPT,
2518 .name = "component-flags",
2519 .has_arg = required_argument },
2520 { .val = LFS_COMP_SET_OPT,
2521 .name = "comp-set", .has_arg = no_argument },
2522 { .val = LFS_COMP_SET_OPT,
2523 .name = "component-set",
2524 .has_arg = no_argument},
2525 { .val = LFS_COMP_NO_VERIFY_OPT,
2526 .name = "no-verify", .has_arg = no_argument},
2527 { .val = LFS_MIRROR_FLAGS_OPT,
2528 .name = "flags", .has_arg = required_argument},
2529 { .val = LFS_MIRROR_ID_OPT,
2530 .name = "mirror-id", .has_arg = required_argument},
2531 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2532 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2533 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2534 { .val = 'd', .name = "delete", .has_arg = no_argument},
2535 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2536 /* --non-direct is only valid in migrate mode */
2537 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2538 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2539 { .val = 'E', .name = "component-end",
2540 .has_arg = required_argument},
2541 { .val = 'f', .name = "file", .has_arg = required_argument },
2542 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2543 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2544 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2545 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
2546 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2547 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2548 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2549 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2550 { .val = 'L', .name = "layout", .has_arg = required_argument },
2551 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2552 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2553 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2554 /* --non-block is only valid in migrate mode */
2555 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2556 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2557 { .val = 'o', .name = "ost", .has_arg = required_argument },
2558 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2559 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2560 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2562 { .val = 'p', .name = "pool", .has_arg = required_argument },
2563 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2564 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2565 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2566 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2567 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2568 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2569 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2570 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2571 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2572 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2573 /* --verbose is only valid in migrate mode */
2574 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2575 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2578 setstripe_args_init(&lsa);
2580 migrate_mode = (opc == SO_MIGRATE);
2581 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2583 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2585 while ((c = getopt_long(argc, argv, "bc:dDE:f:i:I:m:N::no:p:L:s:S:vy:",
2586 long_opts, NULL)) >= 0) {
2591 case LFS_COMP_ADD_OPT:
2594 case LFS_COMP_DEL_OPT:
2597 case LFS_COMP_FLAGS_OPT:
2598 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2599 &lsa.lsa_comp_neg_flags);
2602 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2603 fprintf(stderr, "%s: inverted flags are not supported\n",
2607 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2609 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2616 case LFS_COMP_SET_OPT:
2619 case LFS_COMP_NO_VERIFY_OPT:
2620 mirror_flags |= MF_NO_VERIFY;
2622 case LFS_MIRROR_ID_OPT:
2623 mirror_id = strtoul(optarg, &end, 0);
2624 if (*end != '\0' || mirror_id == 0) {
2626 "%s %s: invalid mirror ID '%s'\n",
2627 progname, argv[0], optarg);
2631 case LFS_MIRROR_FLAGS_OPT: {
2634 if (!mirror_mode || !last_mirror) {
2635 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2640 result = comp_str2flags(optarg, &last_mirror->m_flags,
2646 fprintf(stderr, "%s: inverted flags are not supported\n",
2651 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2653 "%s: unsupported mirror flags: %s\n",
2661 if (!migrate_mode) {
2663 "%s %s: -b|--block valid only for migrate command\n",
2667 migration_block = true;
2670 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2673 "%s %s: invalid stripe count '%s'\n",
2674 progname, argv[0], optarg);
2678 if (lsa.lsa_stripe_count == -1)
2679 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2682 /* delete the default striping pattern */
2684 if (opc == SO_MIRROR_SPLIT) {
2687 "%s %s: -d cannot used with -f\n",
2691 mirror_flags |= MF_DESTROY;
2695 if (!migrate_mode) {
2697 "%s %s: -D|--non-direct is valid "
2698 "only for migrate command\n",
2702 migration_flags |= MIGRATION_NONDIRECT;
2705 if (lsa.lsa_comp_end != 0) {
2706 result = comp_args_to_layout(lpp, &lsa, true);
2709 "%s %s: invalid layout\n",
2714 setstripe_args_init_inherit(&lsa);
2717 if (arg_is_eof(optarg)) {
2718 lsa.lsa_comp_end = LUSTRE_EOF;
2720 result = llapi_parse_size(optarg,
2725 "%s %s: invalid component end '%s'\n",
2726 progname, argv[0], optarg);
2732 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2735 "%s %s: invalid stripe offset '%s'\n",
2736 progname, argv[0], optarg);
2739 if (lsa.lsa_stripe_off == -1)
2740 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2743 comp_id = strtoul(optarg, &end, 0);
2744 if (*end != '\0' || comp_id == 0 ||
2745 comp_id > LCME_ID_MAX) {
2747 "%s %s: invalid component ID '%s'\n",
2748 progname, argv[0], optarg);
2753 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2755 "error: %s: invalid option: %s\n",
2756 progname, argv[optopt + 1]);
2759 if (opc == SO_MIRROR_EXTEND) {
2760 if (last_mirror == NULL) {
2762 "error: %s: '-N' must exist in front of '%s'\n",
2763 progname, argv[optopt + 1]);
2766 last_mirror->m_file = optarg;
2767 last_mirror->m_count = 1;
2770 if (mirror_list == NULL)
2771 mirror_list = lfs_mirror_alloc();
2772 mirror_list->m_file = optarg;
2777 if (strcmp(argv[optind - 1], "mdt") == 0) {
2778 /* Can be only the first component */
2779 if (layout != NULL) {
2781 fprintf(stderr, "error: 'mdt' layout "
2782 "can be only the first one\n");
2785 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2787 fprintf(stderr, "error: 'mdt' layout "
2788 "size is too big\n");
2791 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2792 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2794 fprintf(stderr, "error: layout '%s' is "
2795 "unknown, supported layouts are: "
2796 "'mdt', 'raid0'\n", argv[optind]);
2801 if (!migrate_mode) {
2803 "%s %s: -m|--mdt-index valid only for migrate command\n",
2807 mdt_idx_arg = optarg;
2810 if (!migrate_mode) {
2812 "%s %s: -n|--non-block valid only for migrate command\n",
2816 migration_flags |= MIGRATION_NONBLOCK;
2819 if (opc == SO_SETSTRIPE) {
2820 opc = SO_MIRROR_CREATE;
2824 if (optarg != NULL) {
2825 mirror_count = strtoul(optarg, &end, 0);
2826 if (*end != '\0' || mirror_count == 0) {
2828 "error: %s: bad mirror count: %s\n",
2835 new_mirror = lfs_mirror_alloc();
2836 new_mirror->m_count = mirror_count;
2838 if (mirror_list == NULL)
2839 mirror_list = new_mirror;
2841 if (last_mirror != NULL) {
2842 /* wrap up last mirror */
2843 if (lsa.lsa_comp_end == 0)
2844 lsa.lsa_comp_end = LUSTRE_EOF;
2846 result = comp_args_to_layout(lpp, &lsa, true);
2848 lfs_mirror_free(new_mirror);
2852 setstripe_args_init_inherit(&lsa);
2854 last_mirror->m_next = new_mirror;
2857 last_mirror = new_mirror;
2858 lpp = &last_mirror->m_layout;
2861 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2862 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2863 fprintf(stderr, "warning: '--ost-list' is "
2864 "deprecated, use '--ost' instead\n");
2866 lsa.lsa_nr_tgts = parse_targets(osts,
2867 sizeof(osts) / sizeof(__u32),
2868 lsa.lsa_nr_tgts, optarg);
2869 if (lsa.lsa_nr_tgts < 0) {
2871 "%s %s: invalid OST target(s) '%s'\n",
2872 progname, argv[0], optarg);
2876 lsa.lsa_tgts = osts;
2877 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2878 lsa.lsa_stripe_off = osts[0];
2883 lsa.lsa_pool_name = optarg;
2885 if (strlen(lsa.lsa_pool_name) == 0 ||
2886 strncmp(lsa.lsa_pool_name, "none",
2887 LOV_MAXPOOLNAME) == 0)
2888 lsa.lsa_pool_name = NULL;
2891 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2895 "%s %s: invalid stripe size '%s'\n",
2896 progname, argv[0], optarg);
2901 if (!migrate_mode) {
2903 "%s %s: -v|--verbose valid only for migrate command\n",
2907 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2914 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2915 progname, argv[0], argv[optind - 1]);
2920 fname = argv[optind];
2922 if (optind == argc) {
2923 fprintf(stderr, "%s %s: FILE must be specified\n",
2928 if (mirror_mode && mirror_count == 0) {
2930 "error: %s: --mirror-count|-N option is required\n",
2937 if (lsa.lsa_comp_end == 0)
2938 lsa.lsa_comp_end = LUSTRE_EOF;
2941 if (lsa.lsa_comp_end != 0) {
2942 result = comp_args_to_layout(lpp, &lsa, true);
2947 if (mirror_flags & MF_NO_VERIFY) {
2948 if (opc != SO_MIRROR_EXTEND) {
2950 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2954 } else if (!has_m_file) {
2956 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2963 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2964 * altered by user space tool, so we don't need to support the
2965 * --component-set for this moment. */
2966 if (comp_set && !comp_id) {
2967 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2972 if ((delete + comp_set + comp_del + comp_add) > 1) {
2974 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2979 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2980 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2982 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2987 if ((comp_set || comp_del) &&
2988 (setstripe_args_specified(&lsa) || layout != NULL)) {
2990 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
2995 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
2997 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3002 if (comp_add || comp_del) {
3005 result = lstat(fname, &st);
3006 if (result == 0 && S_ISDIR(st.st_mode)) {
3008 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3014 fprintf(stderr, "error: %s: can't use --component-add "
3015 "or --component-del for mirror operation\n",
3022 if (layout == NULL) {
3024 "%s %s: option -E must be specified with --component-add\n",
3029 result = adjust_first_extent(fname, layout);
3030 if (result == -ENODATA)
3032 else if (result != 0)
3036 if (from_yaml && (setstripe_args_specified(&lsa) || layout != NULL)) {
3037 fprintf(stderr, "error: %s: can't specify --yaml with "
3038 "-c, -S, -i, -o, -p or -E options.\n",
3043 if (mdt_idx_arg != NULL && optind > 3) {
3045 "%s %s: option -m cannot be used with other options\n",
3050 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3052 "%s %s: options --non-block and --block are mutually exclusive\n",
3057 if (!comp_del && !comp_set && comp_id != 0) {
3059 "%s %s: option -I can only be used with --component-del\n",
3064 if (mdt_idx_arg != NULL) {
3065 /* initialize migrate mdt parameters */
3066 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
3068 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
3069 progname, argv[0], mdt_idx_arg);
3072 migrate_mdt_param.fp_migrate = 1;
3073 } else if (layout == NULL) {
3074 /* initialize stripe parameters */
3075 param = calloc(1, offsetof(typeof(*param),
3076 lsp_osts[lsa.lsa_nr_tgts]));
3077 if (param == NULL) {
3079 "%s %s: cannot allocate memory for parameters: %s\n",
3080 progname, argv[0], strerror(ENOMEM));
3085 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3086 param->lsp_stripe_size = lsa.lsa_stripe_size;
3087 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3088 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3089 param->lsp_stripe_count = -1;
3091 param->lsp_stripe_count = lsa.lsa_stripe_count;
3093 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3094 param->lsp_stripe_offset = -1;
3096 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3097 param->lsp_pool = lsa.lsa_pool_name;
3098 param->lsp_is_specific = false;
3099 if (lsa.lsa_nr_tgts > 0) {
3100 if (lsa.lsa_stripe_count > 0 &&
3101 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3102 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3103 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3104 fprintf(stderr, "error: %s: stripe count %lld "
3105 "doesn't match the number of OSTs: %d\n"
3106 , argv[0], lsa.lsa_stripe_count,
3112 param->lsp_is_specific = true;
3113 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3114 memcpy(param->lsp_osts, osts,
3115 sizeof(*osts) * lsa.lsa_nr_tgts);
3120 /* generate a layout from a YAML template */
3121 result = lfs_comp_create_from_yaml(template, &layout,
3124 fprintf(stderr, "error: %s: can't create composite "
3125 "layout from template file %s\n",
3131 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3132 if (mdt_idx_arg != NULL) {
3133 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3134 } else if (migrate_mode) {
3135 result = lfs_migrate(fname, migration_flags, param,
3137 } else if (comp_set != 0) {
3138 result = lfs_component_set(fname, comp_id,
3140 lsa.lsa_comp_neg_flags);
3141 } else if (comp_del != 0) {
3142 result = lfs_component_del(fname, comp_id,
3144 lsa.lsa_comp_neg_flags);
3145 } else if (comp_add != 0) {
3146 result = lfs_component_add(fname, layout);
3147 } else if (opc == SO_MIRROR_CREATE) {
3148 result = mirror_create(fname, mirror_list);
3149 } else if (opc == SO_MIRROR_EXTEND) {
3150 result = mirror_extend(fname, mirror_list,
3152 } else if (opc == SO_MIRROR_SPLIT) {
3153 if (mirror_id == 0) {
3155 "%s %s: no mirror id is specified\n",
3159 result = mirror_split(fname, mirror_id, mirror_flags,
3160 has_m_file ? mirror_list->m_file :
3162 } else if (layout != NULL) {
3163 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3170 result = llapi_file_open_param(fname,
3179 /* Save the first error encountered. */
3187 llapi_layout_free(layout);
3188 lfs_mirror_list_free(mirror_list);
3193 llapi_layout_free(layout);
3194 lfs_mirror_list_free(mirror_list);
3198 static int lfs_poollist(int argc, char **argv)
3203 return llapi_poollist(argv[1]);
3206 static int set_time(time_t *time, time_t *set, char *str)
3213 else if (str[0] == '-')
3219 t = strtol(str, NULL, 0);
3220 if (*time < t * 24 * 60 * 60) {
3223 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
3227 *set = *time - t * 24 * 60 * 60;
3230 static int name2uid(unsigned int *id, const char *name)
3232 struct passwd *passwd;
3234 passwd = getpwnam(name);
3237 *id = passwd->pw_uid;
3242 static int name2gid(unsigned int *id, const char *name)
3244 struct group *group;
3246 group = getgrnam(name);
3249 *id = group->gr_gid;
3254 static inline int name2projid(unsigned int *id, const char *name)
3259 static int uid2name(char **name, unsigned int id)
3261 struct passwd *passwd;
3263 passwd = getpwuid(id);
3266 *name = passwd->pw_name;
3271 static inline int gid2name(char **name, unsigned int id)
3273 struct group *group;
3275 group = getgrgid(id);
3278 *name = group->gr_name;
3283 static int name2layout(__u32 *layout, char *name)
3285 char *ptr, *layout_name;
3288 for (ptr = name; ; ptr = NULL) {
3289 layout_name = strtok(ptr, ",");
3290 if (layout_name == NULL)
3292 if (strcmp(layout_name, "released") == 0)
3293 *layout |= LOV_PATTERN_F_RELEASED;
3294 else if (strcmp(layout_name, "raid0") == 0)
3295 *layout |= LOV_PATTERN_RAID0;
3296 else if (strcmp(layout_name, "mdt") == 0)
3297 *layout |= LOV_PATTERN_MDT;
3304 static int lfs_find(int argc, char **argv)
3309 struct find_param param = {
3313 struct option long_opts[] = {
3314 { .val = 'A', .name = "atime", .has_arg = required_argument },
3315 { .val = LFS_COMP_COUNT_OPT,
3316 .name = "comp-count", .has_arg = required_argument },
3317 { .val = LFS_COMP_COUNT_OPT,
3318 .name = "component-count",
3319 .has_arg = required_argument },
3320 { .val = LFS_COMP_FLAGS_OPT,
3321 .name = "comp-flags", .has_arg = required_argument },
3322 { .val = LFS_COMP_FLAGS_OPT,
3323 .name = "component-flags",
3324 .has_arg = required_argument },
3325 { .val = LFS_COMP_START_OPT,
3326 .name = "comp-start", .has_arg = required_argument },
3327 { .val = LFS_COMP_START_OPT,
3328 .name = "component-start",
3329 .has_arg = required_argument },
3330 { .val = LFS_MIRROR_STATE_OPT,
3331 .name = "mirror-state", .has_arg = required_argument },
3332 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3333 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3334 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3335 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3336 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3337 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3338 { .val = 'E', .name = "component-end",
3339 .has_arg = required_argument },
3340 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3341 { .val = 'g', .name = "gid", .has_arg = required_argument },
3342 { .val = 'G', .name = "group", .has_arg = required_argument },
3343 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3344 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3345 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3346 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3347 { .val = 'L', .name = "layout", .has_arg = required_argument },
3348 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3349 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3350 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3351 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3352 { .val = 'n', .name = "name", .has_arg = required_argument },
3353 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3354 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3355 { .val = 'O', .name = "obd", .has_arg = required_argument },
3356 { .val = 'O', .name = "ost", .has_arg = required_argument },
3357 /* no short option for pool yet, can be 'p' after 2.18 */
3358 { .val = LFS_POOL_OPT,
3359 .name = "pool", .has_arg = required_argument },
3360 { .val = '0', .name = "print0", .has_arg = no_argument },
3361 { .val = 'P', .name = "print", .has_arg = no_argument },
3362 { .val = LFS_PROJID_OPT,
3363 .name = "projid", .has_arg = required_argument },
3364 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3365 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3366 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3367 { .val = 's', .name = "size", .has_arg = required_argument },
3368 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3369 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3370 { .val = 't', .name = "type", .has_arg = required_argument },
3371 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3372 { .val = 'u', .name = "uid", .has_arg = required_argument },
3373 { .val = 'U', .name = "user", .has_arg = required_argument },
3374 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3375 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3387 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3388 while ((c = getopt_long_only(argc, argv,
3389 "-0A:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3390 long_opts, NULL)) >= 0) {
3395 /* '!' is part of option */
3396 /* when getopt_long_only() finds a string which is not
3397 * an option nor a known option argument it returns 1
3398 * in that case if we already have found pathstart and pathend
3399 * (i.e. we have the list of pathnames),
3400 * the only supported value is "!"
3402 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3403 if (!isoption && pathend != -1) {
3404 fprintf(stderr, "err: %s: filename|dirname must either "
3405 "precede options or follow options\n",
3410 if (!isoption && pathstart == -1)
3411 pathstart = optind - 1;
3412 if (isoption && pathstart != -1 && pathend == -1)
3413 pathend = optind - 2;
3419 /* unknown; opt is "!" or path component,
3420 * checking done above.
3422 if (strcmp(optarg, "!") == 0)
3426 xtime = ¶m.fp_atime;
3427 xsign = ¶m.fp_asign;
3428 param.fp_exclude_atime = !!neg_opt;
3429 /* no break, this falls through to 'C' for ctime */
3432 xtime = ¶m.fp_ctime;
3433 xsign = ¶m.fp_csign;
3434 param.fp_exclude_ctime = !!neg_opt;
3436 /* no break, this falls through to 'M' for mtime */
3439 xtime = ¶m.fp_mtime;
3440 xsign = ¶m.fp_msign;
3441 param.fp_exclude_mtime = !!neg_opt;
3443 rc = set_time(&t, xtime, optarg);
3444 if (rc == INT_MAX) {
3451 case LFS_COMP_COUNT_OPT:
3452 if (optarg[0] == '+') {
3453 param.fp_comp_count_sign = -1;
3455 } else if (optarg[0] == '-') {
3456 param.fp_comp_count_sign = 1;
3460 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3461 if (*endptr != '\0') {
3462 fprintf(stderr, "error: bad component count "
3466 param.fp_check_comp_count = 1;
3467 param.fp_exclude_comp_count = !!neg_opt;
3469 case LFS_COMP_FLAGS_OPT:
3470 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3471 ¶m.fp_comp_neg_flags);
3473 fprintf(stderr, "error: bad component flags "
3477 param.fp_check_comp_flags = 1;
3479 __u32 flags = param.fp_comp_neg_flags;
3480 param.fp_comp_neg_flags = param.fp_comp_flags;
3481 param.fp_comp_flags = flags;
3484 case LFS_COMP_START_OPT:
3485 if (optarg[0] == '+') {
3486 param.fp_comp_start_sign = -1;
3488 } else if (optarg[0] == '-') {
3489 param.fp_comp_start_sign = 1;
3493 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3494 ¶m.fp_comp_start_units, 0);
3496 fprintf(stderr, "error: bad component start "
3500 param.fp_check_comp_start = 1;
3501 param.fp_exclude_comp_start = !!neg_opt;
3503 case LFS_MIRROR_STATE_OPT:
3504 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3505 ¶m.fp_mirror_neg_state);
3508 "error: bad mirrored file state '%s'\n",
3512 param.fp_check_mirror_state = 1;
3514 __u16 state = param.fp_mirror_neg_state;
3515 param.fp_mirror_neg_state =
3516 param.fp_mirror_state;
3517 param.fp_mirror_state = state;
3521 if (optarg[0] == '+') {
3522 param.fp_stripe_count_sign = -1;
3524 } else if (optarg[0] == '-') {
3525 param.fp_stripe_count_sign = 1;
3529 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3530 if (*endptr != '\0') {
3531 fprintf(stderr,"error: bad stripe_count '%s'\n",
3536 param.fp_check_stripe_count = 1;
3537 param.fp_exclude_stripe_count = !!neg_opt;
3540 param.fp_max_depth = strtol(optarg, 0, 0);
3543 if (optarg[0] == '+') {
3544 param.fp_comp_end_sign = -1;
3546 } else if (optarg[0] == '-') {
3547 param.fp_comp_end_sign = 1;
3551 if (arg_is_eof(optarg)) {
3552 param.fp_comp_end = LUSTRE_EOF;
3553 param.fp_comp_end_units = 1;
3556 rc = llapi_parse_size(optarg,
3558 ¶m.fp_comp_end_units, 0);
3561 fprintf(stderr, "error: bad component end "
3565 param.fp_check_comp_end = 1;
3566 param.fp_exclude_comp_end = !!neg_opt;
3570 rc = name2gid(¶m.fp_gid, optarg);
3572 param.fp_gid = strtoul(optarg, &endptr, 10);
3573 if (*endptr != '\0') {
3574 fprintf(stderr, "Group/GID: %s cannot "
3575 "be found.\n", optarg);
3580 param.fp_exclude_gid = !!neg_opt;
3581 param.fp_check_gid = 1;
3584 param.fp_hash_type = check_hashtype(optarg);
3585 if (param.fp_hash_type == 0) {
3586 fprintf(stderr, "error: bad hash_type '%s'\n",
3591 param.fp_check_hash_type = 1;
3592 param.fp_exclude_hash_type = !!neg_opt;
3595 ret = name2layout(¶m.fp_layout, optarg);
3598 param.fp_exclude_layout = !!neg_opt;
3599 param.fp_check_layout = 1;
3603 rc = name2uid(¶m.fp_uid, optarg);
3605 param.fp_uid = strtoul(optarg, &endptr, 10);
3606 if (*endptr != '\0') {
3607 fprintf(stderr, "User/UID: %s cannot "
3608 "be found.\n", optarg);
3613 param.fp_exclude_uid = !!neg_opt;
3614 param.fp_check_uid = 1;
3617 param.fp_pattern = (char *)optarg;
3618 param.fp_exclude_pattern = !!neg_opt;
3621 if (optarg[0] == '+') {
3622 param.fp_mirror_count_sign = -1;
3624 } else if (optarg[0] == '-') {
3625 param.fp_mirror_count_sign = 1;
3629 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3630 if (*endptr != '\0') {
3632 "error: bad mirror count '%s'\n",
3636 param.fp_check_mirror_count = 1;
3637 param.fp_exclude_mirror_count = !!neg_opt;
3642 char *buf, *token, *next, *p;
3646 buf = strdup(optarg);
3652 param.fp_exclude_obd = !!neg_opt;
3655 while (token && *token) {
3656 token = strchr(token, ',');
3663 param.fp_exclude_mdt = !!neg_opt;
3664 param.fp_num_alloc_mdts += len;
3665 tmp = realloc(param.fp_mdt_uuid,
3666 param.fp_num_alloc_mdts *
3667 sizeof(*param.fp_mdt_uuid));
3673 param.fp_mdt_uuid = tmp;
3675 param.fp_exclude_obd = !!neg_opt;
3676 param.fp_num_alloc_obds += len;
3677 tmp = realloc(param.fp_obd_uuid,
3678 param.fp_num_alloc_obds *
3679 sizeof(*param.fp_obd_uuid));
3685 param.fp_obd_uuid = tmp;
3687 for (token = buf; token && *token; token = next) {
3688 struct obd_uuid *puuid;
3691 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3694 ¶m.fp_obd_uuid[param.fp_num_obds++];
3696 p = strchr(token, ',');
3703 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3708 strncpy(puuid->uuid, token,
3709 sizeof(puuid->uuid));
3716 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3720 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3722 "Pool name %s is too long (max %d)\n",
3723 optarg, LOV_MAXPOOLNAME);
3728 * We do check for empty pool because empty pool
3729 * is used to find V1 LOV attributes
3731 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3732 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3733 param.fp_exclude_pool = !!neg_opt;
3734 param.fp_check_pool = 1;
3736 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3737 case 'p': /* want this for --pool, to match getstripe/find */
3739 "warning: -p deprecated, use --print0 or -0\n");
3742 param.fp_zero_end = 1;
3744 case 'P': /* we always print, this option is a no-op */
3746 case LFS_PROJID_OPT:
3747 rc = name2projid(¶m.fp_projid, optarg);
3749 param.fp_projid = strtoul(optarg, &endptr, 10);
3750 if (*endptr != '\0') {
3752 "Invalid project ID: %s",
3758 param.fp_exclude_projid = !!neg_opt;
3759 param.fp_check_projid = 1;
3762 if (optarg[0] == '+') {
3763 param.fp_size_sign = -1;
3765 } else if (optarg[0] == '-') {
3766 param.fp_size_sign = 1;
3770 ret = llapi_parse_size(optarg, ¶m.fp_size,
3771 ¶m.fp_size_units, 0);
3773 fprintf(stderr, "error: bad file size '%s'\n",
3777 param.fp_check_size = 1;
3778 param.fp_exclude_size = !!neg_opt;
3781 if (optarg[0] == '+') {
3782 param.fp_stripe_size_sign = -1;
3784 } else if (optarg[0] == '-') {
3785 param.fp_stripe_size_sign = 1;
3789 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3790 ¶m.fp_stripe_size_units, 0);
3792 fprintf(stderr, "error: bad stripe_size '%s'\n",
3796 param.fp_check_stripe_size = 1;
3797 param.fp_exclude_stripe_size = !!neg_opt;
3800 param.fp_exclude_type = !!neg_opt;
3801 switch (optarg[0]) {
3803 param.fp_type = S_IFBLK;
3806 param.fp_type = S_IFCHR;
3809 param.fp_type = S_IFDIR;
3812 param.fp_type = S_IFREG;
3815 param.fp_type = S_IFLNK;
3818 param.fp_type = S_IFIFO;
3821 param.fp_type = S_IFSOCK;
3824 fprintf(stderr, "error: %s: bad type '%s'\n",
3831 if (optarg[0] == '+') {
3832 param.fp_mdt_count_sign = -1;
3834 } else if (optarg[0] == '-') {
3835 param.fp_mdt_count_sign = 1;
3839 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3840 if (*endptr != '\0') {
3841 fprintf(stderr, "error: bad mdt_count '%s'\n",
3846 param.fp_check_mdt_count = 1;
3847 param.fp_exclude_mdt_count = !!neg_opt;
3855 if (pathstart == -1) {
3856 fprintf(stderr, "error: %s: no filename|pathname\n",
3860 } else if (pathend == -1) {
3866 rc = llapi_find(argv[pathstart], ¶m);
3867 if (rc != 0 && ret == 0)
3869 } while (++pathstart < pathend);
3872 fprintf(stderr, "error: %s failed for %s.\n",
3873 argv[0], argv[optind - 1]);
3875 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3876 free(param.fp_obd_uuid);
3878 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3879 free(param.fp_mdt_uuid);
3884 static int lfs_getstripe_internal(int argc, char **argv,
3885 struct find_param *param)
3887 struct option long_opts[] = {
3888 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3889 { .val = LFS_COMP_COUNT_OPT,
3890 .name = "comp-count", .has_arg = no_argument },
3891 { .val = LFS_COMP_COUNT_OPT,
3892 .name = "component-count", .has_arg = no_argument },
3893 { .val = LFS_COMP_FLAGS_OPT,
3894 .name = "comp-flags", .has_arg = optional_argument },
3895 { .val = LFS_COMP_FLAGS_OPT,
3896 .name = "component-flags", .has_arg = optional_argument },
3897 { .val = LFS_COMP_START_OPT,
3898 .name = "comp-start", .has_arg = optional_argument },
3899 { .val = LFS_COMP_START_OPT,
3900 .name = "component-start", .has_arg = optional_argument },
3901 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3902 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3903 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
3904 { .val = 'd', .name = "directory", .has_arg = no_argument },
3905 { .val = 'D', .name = "default", .has_arg = no_argument },
3906 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3907 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
3908 { .val = 'F', .name = "fid", .has_arg = no_argument },
3909 { .val = 'g', .name = "generation", .has_arg = no_argument },
3910 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3911 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
3912 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3913 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3914 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3915 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3916 { .val = 'L', .name = "layout", .has_arg = no_argument },
3917 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3918 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3919 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3920 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
3921 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
3922 { .val = 'O', .name = "obd", .has_arg = required_argument },
3923 { .val = 'O', .name = "ost", .has_arg = required_argument },
3924 { .val = 'p', .name = "pool", .has_arg = no_argument },
3925 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3926 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3927 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3928 { .val = 'R', .name = "raw", .has_arg = no_argument },
3929 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3930 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3931 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3932 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3933 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3934 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3935 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3936 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3941 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
3942 long_opts, NULL)) != -1) {
3945 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3946 param->fp_verbose |= VERBOSE_COUNT;
3947 param->fp_max_depth = 0;
3950 case LFS_COMP_COUNT_OPT:
3951 param->fp_verbose |= VERBOSE_COMP_COUNT;
3952 param->fp_max_depth = 0;
3954 case LFS_COMP_FLAGS_OPT:
3955 if (optarg != NULL) {
3956 rc = comp_str2flags(optarg,
3957 ¶m->fp_comp_flags,
3958 ¶m->fp_comp_neg_flags);
3960 fprintf(stderr, "error: %s bad "
3961 "component flags '%s'.\n",
3965 param->fp_check_comp_flags = 1;
3967 param->fp_verbose |= VERBOSE_COMP_FLAGS;
3968 param->fp_max_depth = 0;
3971 case LFS_COMP_START_OPT:
3972 if (optarg != NULL) {
3974 if (tmp[0] == '+') {
3975 param->fp_comp_start_sign = -1;
3977 } else if (tmp[0] == '-') {
3978 param->fp_comp_start_sign = 1;
3981 rc = llapi_parse_size(tmp,
3982 ¶m->fp_comp_start,
3983 ¶m->fp_comp_start_units, 0);
3985 fprintf(stderr, "error: %s bad "
3986 "component start '%s'.\n",
3990 param->fp_check_comp_start = 1;
3993 param->fp_verbose |= VERBOSE_COMP_START;
3994 param->fp_max_depth = 0;
3998 param->fp_max_depth = 0;
4001 param->fp_get_default_lmv = 1;
4004 if (optarg != NULL) {
4006 if (tmp[0] == '+') {
4007 param->fp_comp_end_sign = -1;
4009 } else if (tmp[0] == '-') {
4010 param->fp_comp_end_sign = 1;
4014 if (arg_is_eof(tmp)) {
4015 param->fp_comp_end = LUSTRE_EOF;
4016 param->fp_comp_end_units = 1;
4019 rc = llapi_parse_size(tmp,
4020 ¶m->fp_comp_end,
4021 ¶m->fp_comp_end_units, 0);
4024 fprintf(stderr, "error: %s bad "
4025 "component end '%s'.\n",
4029 param->fp_check_comp_end = 1;
4031 param->fp_verbose |= VERBOSE_COMP_END;
4032 param->fp_max_depth = 0;
4036 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4037 param->fp_verbose |= VERBOSE_DFID;
4038 param->fp_max_depth = 0;
4042 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4043 param->fp_verbose |= VERBOSE_GENERATION;
4044 param->fp_max_depth = 0;
4048 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4049 param->fp_verbose |= VERBOSE_OFFSET;
4050 param->fp_max_depth = 0;
4054 if (optarg != NULL) {
4055 param->fp_comp_id = strtoul(optarg, &end, 0);
4056 if (*end != '\0' || param->fp_comp_id == 0 ||
4057 param->fp_comp_id > LCME_ID_MAX) {
4058 fprintf(stderr, "error: %s bad "
4059 "component id '%s'\n",
4063 param->fp_check_comp_id = 1;
4066 param->fp_max_depth = 0;
4067 param->fp_verbose |= VERBOSE_COMP_ID;
4071 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4072 param->fp_verbose |= VERBOSE_LAYOUT;
4073 param->fp_max_depth = 0;
4076 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4078 fprintf(stderr, "warning: '-M' deprecated"
4079 ", use '--mdt-index' or '-m' instead\n");
4082 if (!(param->fp_verbose & VERBOSE_DETAIL))
4083 param->fp_max_depth = 0;
4084 param->fp_verbose |= VERBOSE_MDTINDEX;
4087 if (param->fp_obd_uuid) {
4089 "error: %s: only one obduuid allowed",
4093 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4096 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4097 param->fp_verbose |= VERBOSE_POOL;
4098 param->fp_max_depth = 0;
4105 param->fp_recursive = 1;
4111 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4112 param->fp_verbose |= VERBOSE_SIZE;
4113 param->fp_max_depth = 0;
4117 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4130 if (param->fp_recursive)
4131 param->fp_max_depth = -1;
4132 else if (param->fp_verbose & VERBOSE_DETAIL)
4133 param->fp_max_depth = 1;
4135 if (!param->fp_verbose)
4136 param->fp_verbose = VERBOSE_DEFAULT;
4137 if (param->fp_quiet)
4138 param->fp_verbose = VERBOSE_OBJID;
4141 rc = llapi_getstripe(argv[optind], param);
4142 } while (++optind < argc && !rc);
4145 fprintf(stderr, "error: %s failed for %s.\n",
4146 argv[0], argv[optind - 1]);
4150 static int lfs_tgts(int argc, char **argv)
4152 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4153 struct find_param param;
4154 int index = 0, rc=0;
4159 if (argc == 2 && !realpath(argv[1], path)) {
4161 fprintf(stderr, "error: invalid path '%s': %s\n",
4162 argv[1], strerror(-rc));
4166 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4167 /* Check if we have a mount point */
4168 if (mntdir[0] == '\0')
4171 memset(¶m, 0, sizeof(param));
4172 if (!strcmp(argv[0], "mdts"))
4173 param.fp_get_lmv = 1;
4175 rc = llapi_ostlist(mntdir, ¶m);
4177 fprintf(stderr, "error: %s: failed on %s\n",
4180 if (path[0] != '\0')
4182 memset(mntdir, 0, PATH_MAX);
4188 static int lfs_getstripe(int argc, char **argv)
4190 struct find_param param = { 0 };
4192 param.fp_max_depth = 1;
4193 return lfs_getstripe_internal(argc, argv, ¶m);
4197 static int lfs_getdirstripe(int argc, char **argv)
4199 struct find_param param = { 0 };
4200 struct option long_opts[] = {
4201 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4202 { .val = 'D', .name = "default", .has_arg = no_argument },
4203 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4204 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4205 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4206 { .val = 'O', .name = "obd", .has_arg = required_argument },
4207 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4208 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4209 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4213 param.fp_get_lmv = 1;
4215 while ((c = getopt_long(argc, argv,
4216 "cDHimO:rtTy", long_opts, NULL)) != -1)
4221 param.fp_verbose |= VERBOSE_COUNT;
4224 param.fp_get_default_lmv = 1;
4228 param.fp_verbose |= VERBOSE_OFFSET;
4230 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4232 fprintf(stderr, "warning: '-t' deprecated, "
4233 "use '--mdt-hash' or '-H' instead\n");
4236 param.fp_verbose |= VERBOSE_HASH_TYPE;
4239 if (param.fp_obd_uuid) {
4241 "error: %s: only one obduuid allowed",
4245 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4248 param.fp_recursive = 1;
4261 if (param.fp_recursive)
4262 param.fp_max_depth = -1;
4264 if (!param.fp_verbose)
4265 param.fp_verbose = VERBOSE_DEFAULT;
4268 rc = llapi_getstripe(argv[optind], ¶m);
4269 } while (++optind < argc && !rc);
4272 fprintf(stderr, "error: %s failed for %s.\n",
4273 argv[0], argv[optind - 1]);
4278 MNTDF_INODES = 0x0001,
4279 MNTDF_COOKED = 0x0002,
4280 MNTDF_LAZY = 0x0004,
4281 MNTDF_VERBOSE = 0x0008,
4282 MNTDF_SHOW = 0x0010,
4285 #define COOK(value) \
4288 while (value > 1024) { \
4296 #define CDF "%11llu"
4297 #define HDF "%8.1f%c"
4301 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4303 double avail, used, ratio = 0;
4305 avail = st->os_bavail;
4306 used = st->os_blocks - st->os_bfree;
4307 if (avail + used > 0)
4308 ratio = used / (used + avail) * 100 + 0.5;
4313 static int showdf(char *mntdir, struct obd_statfs *stat,
4314 char *uuid, enum mntdf_flags flags,
4315 char *type, int index, int rc)
4317 long long avail, used, total;
4319 char *suffix = "KMGTPEZY";
4320 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4321 char tbuf[3 * sizeof(__u64)];
4322 char ubuf[3 * sizeof(__u64)];
4323 char abuf[3 * sizeof(__u64)];
4324 char rbuf[3 * sizeof(__u64)];
4331 if (flags & MNTDF_INODES) {
4332 avail = stat->os_ffree;
4333 used = stat->os_files - stat->os_ffree;
4334 total = stat->os_files;
4336 int shift = flags & MNTDF_COOKED ? 0 : 10;
4338 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4339 used = ((stat->os_blocks - stat->os_bfree) *
4340 stat->os_bsize) >> shift;
4341 total = (stat->os_blocks * stat->os_bsize) >> shift;
4344 ratio = obd_statfs_ratio(stat);
4346 if (flags & MNTDF_COOKED) {
4350 cook_val = (double)total;
4353 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4356 snprintf(tbuf, sizeof(tbuf), CDF, total);
4358 cook_val = (double)used;
4361 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4364 snprintf(ubuf, sizeof(ubuf), CDF, used);
4366 cook_val = (double)avail;
4369 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4372 snprintf(abuf, sizeof(abuf), CDF, avail);
4374 snprintf(tbuf, sizeof(tbuf), CDF, total);
4375 snprintf(ubuf, sizeof(tbuf), CDF, used);
4376 snprintf(abuf, sizeof(tbuf), CDF, avail);
4379 sprintf(rbuf, RDF, ratio);
4380 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4381 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4383 printf("[%s:%d]", type, index);
4385 if (stat->os_state) {
4387 * Each character represents the matching
4390 const char state_names[] = "DRSI";
4395 for (i = 0, state = stat->os_state;
4396 state && i < sizeof(state_names); i++) {
4397 if (!(state & (1 << i)))
4399 printf("%c", state_names[i]);
4407 printf(UUF": inactive device\n", uuid);
4410 printf(UUF": %s\n", uuid, strerror(-rc));
4417 struct ll_stat_type {
4422 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4424 struct ll_statfs_data {
4426 struct obd_statfs sd_st;
4429 struct ll_statfs_buf {
4431 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4434 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4435 int ops, struct ll_statfs_buf *lsb)
4437 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4438 struct obd_uuid uuid_buf;
4439 char *poolname = NULL;
4440 struct ll_stat_type types[] = {
4441 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4442 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4443 { .st_name = NULL } };
4444 struct ll_stat_type *tp;
4445 __u64 ost_ffree = 0;
4453 poolname = strchr(pool, '.');
4454 if (poolname != NULL) {
4455 if (strncmp(fsname, pool, strlen(fsname))) {
4456 fprintf(stderr, "filesystem name incorrect\n");
4464 fd = open(mntdir, O_RDONLY);
4467 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4472 if (flags & MNTDF_SHOW) {
4473 if (flags & MNTDF_INODES)
4474 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4475 "UUID", "Inodes", "IUsed", "IFree",
4476 "IUse%", "Mounted on");
4478 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4480 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4481 "Used", "Available", "Use%", "Mounted on");
4484 for (tp = types; tp->st_name != NULL; tp++) {
4485 if (!(tp->st_op & ops))
4488 for (index = 0; ; index++) {
4489 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4490 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4491 type = flags & MNTDF_LAZY ?
4492 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4493 rc2 = llapi_obd_fstatfs(fd, type, index,
4494 &stat_buf, &uuid_buf);
4499 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4500 if (!(flags & MNTDF_VERBOSE))
4502 } else if (rc2 < 0 && rc == 0) {
4506 if (poolname && tp->st_op == LL_STATFS_LOV &&
4507 llapi_search_ost(fsname, poolname,
4508 obd_uuid2str(&uuid_buf)) != 1)
4511 /* the llapi_obd_statfs() call may have returned with
4512 * an error, but if it filled in uuid_buf we will at
4513 * lease use that to print out a message for that OBD.
4514 * If we didn't get anything in the uuid_buf, then fill
4515 * it in so that we can print an error message. */
4516 if (uuid_buf.uuid[0] == '\0')
4517 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4518 "%s%04x", tp->st_name, index);
4520 lsb->sb_buf[lsb->sb_count].sd_index = index;
4521 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4524 if (flags & MNTDF_SHOW)
4525 showdf(mntdir, &stat_buf,
4526 obd_uuid2str(&uuid_buf), flags,
4527 tp->st_name, index, rc2);
4530 if (tp->st_op == LL_STATFS_LMV) {
4531 sum.os_ffree += stat_buf.os_ffree;
4532 sum.os_files += stat_buf.os_files;
4533 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4534 sum.os_blocks += stat_buf.os_blocks *
4536 sum.os_bfree += stat_buf.os_bfree *
4538 sum.os_bavail += stat_buf.os_bavail *
4540 ost_ffree += stat_buf.os_ffree;
4548 /* If we don't have as many objects free on the OST as inodes
4549 * on the MDS, we reduce the total number of inodes to
4550 * compensate, so that the "inodes in use" number is correct.
4551 * Matches ll_statfs_internal() so the results are consistent. */
4552 if (ost_ffree < sum.os_ffree) {
4553 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4554 sum.os_ffree = ost_ffree;
4556 if (flags & MNTDF_SHOW) {
4558 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4565 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4567 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4569 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4571 int r1 = obd_statfs_ratio(st1);
4572 int r2 = obd_statfs_ratio(st2);
4573 int64_t result = r1 - r2;
4575 /* if both space usage are above 90, compare free inodes */
4576 if (r1 > 90 && r2 > 90)
4577 result = st2->os_ffree - st1->os_ffree;
4581 else if (result == 0)
4588 static int lfs_setdirstripe(int argc, char **argv)
4592 struct lfs_setstripe_args lsa = { 0 };
4593 struct llapi_stripe_param *param = NULL;
4594 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4597 char *mode_opt = NULL;
4598 bool default_stripe = false;
4599 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4600 mode_t previous_mode = 0;
4601 bool delete = false;
4602 struct ll_statfs_buf *lsb = NULL;
4603 char mntdir[PATH_MAX] = "";
4604 bool auto_distributed = false;
4606 struct option long_opts[] = {
4607 { .val = 'c', .name = "count", .has_arg = required_argument },
4608 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4609 { .val = 'd', .name = "delete", .has_arg = no_argument },
4610 { .val = 'D', .name = "default", .has_arg = no_argument },
4611 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4612 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4613 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4614 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4615 { .val = 'i', .name = "index", .has_arg = required_argument },
4617 { .val = 'o', .name = "mode", .has_arg = required_argument },
4618 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4619 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4621 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4622 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4625 setstripe_args_init(&lsa);
4627 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4635 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4638 "%s %s: invalid stripe count '%s'\n",
4639 progname, argv[0], optarg);
4645 default_stripe = true;
4648 default_stripe = true;
4650 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4652 fprintf(stderr, "warning: '--hash-type' and '-t' "
4653 "deprecated, use '--mdt-hash' or '-H' instead\n");
4656 lsa.lsa_pattern = check_hashtype(optarg);
4657 if (lsa.lsa_pattern == 0) {
4659 "%s %s: bad stripe hash type '%s'\n",
4660 progname, argv[0], optarg);
4665 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4666 if (strcmp(argv[optind - 1], "--index") == 0)
4668 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4671 lsa.lsa_nr_tgts = parse_targets(mdts,
4672 sizeof(mdts) / sizeof(__u32),
4673 lsa.lsa_nr_tgts, optarg);
4674 if (lsa.lsa_nr_tgts < 0) {
4676 "%s %s: invalid MDT target(s) '%s'\n",
4677 progname, argv[0], optarg);
4681 lsa.lsa_tgts = mdts;
4682 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4683 lsa.lsa_stripe_off = mdts[0];
4685 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4687 fprintf(stderr, "warning: '-m' is deprecated, "
4688 "use '--mode' or '-o' instead\n");
4694 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4695 progname, argv[0], argv[optind - 1]);
4700 if (optind == argc) {
4701 fprintf(stderr, "%s %s: DIR must be specified\n",
4706 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
4707 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
4709 "%s %s: stripe offset and count must be specified\n",
4715 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
4716 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
4718 "%s %s: cannot specify -d with -c or -i options\n",
4723 if (mode_opt != NULL) {
4724 mode = strtoul(mode_opt, &end, 8);
4727 "%s %s: bad MODE '%s'\n",
4728 progname, argv[0], mode_opt);
4731 previous_mode = umask(0);
4735 * initialize stripe parameters, in case param is converted to specific,
4736 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
4738 param = calloc(1, offsetof(typeof(*param),
4739 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
4740 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
4741 if (param == NULL) {
4743 "%s %s: cannot allocate memory for parameters: %s\n",
4744 progname, argv[0], strerror(ENOMEM));
4748 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4749 param->lsp_stripe_count = lsa.lsa_stripe_count;
4750 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4751 param->lsp_stripe_offset = -1;
4753 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4754 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4755 param->lsp_stripe_pattern = lsa.lsa_pattern;
4757 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
4758 param->lsp_pool = lsa.lsa_pool_name;
4759 param->lsp_is_specific = false;
4760 if (lsa.lsa_nr_tgts > 1) {
4761 if (lsa.lsa_stripe_count > 0 &&
4762 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4763 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4764 fprintf(stderr, "error: %s: stripe count %lld doesn't "
4765 "match the number of MDTs: %d\n",
4766 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
4771 param->lsp_is_specific = true;
4772 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4773 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
4776 dname = argv[optind];
4778 if (default_stripe) {
4779 result = llapi_dir_set_default_lmv(dname, param);
4781 /* if current \a dname isn't under the same \a mntdir
4782 * as the last one, and the last one was
4783 * auto-distributed, restore \a param.
4785 if (mntdir[0] != '\0' &&
4786 strncmp(dname, mntdir, strlen(mntdir)) &&
4788 param->lsp_is_specific = false;
4789 param->lsp_stripe_offset = -1;
4790 auto_distributed = false;
4793 if (!param->lsp_is_specific &&
4794 param->lsp_stripe_offset == -1) {
4795 char path[PATH_MAX] = "";
4798 lsb = malloc(sizeof(*lsb));
4806 /* use mntdir for dirname() temporarily */
4807 strncpy(mntdir, dname, sizeof(mntdir));
4808 if (!realpath(dirname(mntdir), path)) {
4811 "error: invalid path '%s': %s\n",
4812 argv[optind], strerror(errno));
4817 result = llapi_search_mounts(path, 0, mntdir,
4819 if (result < 0 || mntdir[0] == '\0') {
4821 "No suitable Lustre mount found\n");
4825 result = mntdf(mntdir, NULL, NULL, 0,
4826 LL_STATFS_LMV, lsb);
4830 if (param->lsp_stripe_count > lsb->sb_count) {
4832 "error: stripe count %d is too big\n",
4833 param->lsp_stripe_count);
4838 qsort(lsb->sb_buf, lsb->sb_count,
4839 sizeof(struct ll_statfs_data),
4840 ll_statfs_data_comp);
4842 auto_distributed = true;
4845 if (auto_distributed) {
4847 int nr = MAX(param->lsp_stripe_count,
4850 /* don't use server whose usage is above 90% */
4851 while (nr != param->lsp_stripe_count &&
4852 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
4854 nr = MAX(param->lsp_stripe_count,
4857 /* get \a r between [0, nr) */
4860 param->lsp_stripe_offset =
4861 lsb->sb_buf[r].sd_index;
4862 if (param->lsp_stripe_count > 1) {
4865 param->lsp_is_specific = true;
4866 for (; i < param->lsp_stripe_count; i++)
4867 param->lsp_tgts[(i + r) % nr] =
4868 lsb->sb_buf[i].sd_index;
4872 result = llapi_dir_create(dname, mode, param);
4877 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
4878 progname, dname, strerror(-result));
4881 dname = argv[++optind];
4882 } while (dname != NULL);
4884 if (mode_opt != NULL)
4885 umask(previous_mode);
4893 static int lfs_rmentry(int argc, char **argv)
4900 fprintf(stderr, "error: %s: missing dirname\n",
4906 dname = argv[index];
4907 while (dname != NULL) {
4908 result = llapi_direntry_remove(dname);
4910 fprintf(stderr, "error: %s: remove dir entry '%s' "
4911 "failed\n", argv[0], dname);
4914 dname = argv[++index];
4919 static int lfs_mv(int argc, char **argv)
4921 struct find_param param = {
4928 struct option long_opts[] = {
4929 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4930 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4933 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
4935 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4937 fprintf(stderr, "warning: '-M' deprecated"
4938 ", use '--mdt-index' or '-m' instead\n");
4941 param.fp_mdt_index = strtoul(optarg, &end, 0);
4943 fprintf(stderr, "%s: invalid MDT index'%s'\n",
4949 param.fp_verbose = VERBOSE_DETAIL;
4952 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
4953 argv[0], argv[optind - 1]);
4958 if (param.fp_mdt_index == -1) {
4959 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
4963 if (optind >= argc) {
4964 fprintf(stderr, "%s: missing operand path\n", argv[0]);
4968 param.fp_migrate = 1;
4969 rc = llapi_migrate_mdt(argv[optind], ¶m);
4971 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
4972 argv[0], argv[optind], param.fp_mdt_index,
4977 static int lfs_osts(int argc, char **argv)
4979 return lfs_tgts(argc, argv);
4982 static int lfs_mdts(int argc, char **argv)
4984 return lfs_tgts(argc, argv);
4987 static int lfs_df(int argc, char **argv)
4989 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4990 enum mntdf_flags flags = MNTDF_SHOW;
4991 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
4992 int c, rc = 0, index = 0;
4993 char fsname[PATH_MAX] = "", *pool_name = NULL;
4994 struct option long_opts[] = {
4995 { .val = 'h', .name = "human-readable",
4996 .has_arg = no_argument },
4997 { .val = 'i', .name = "inodes", .has_arg = no_argument },
4998 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4999 { .val = 'p', .name = "pool", .has_arg = required_argument },
5000 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5003 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5006 flags |= MNTDF_COOKED;
5009 flags |= MNTDF_INODES;
5012 flags |= MNTDF_LAZY;
5018 flags |= MNTDF_VERBOSE;
5024 if (optind < argc && !realpath(argv[optind], path)) {
5026 fprintf(stderr, "error: invalid path '%s': %s\n",
5027 argv[optind], strerror(-rc));
5031 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5032 /* Check if we have a mount point */
5033 if (mntdir[0] == '\0')
5036 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5037 if (rc || path[0] != '\0')
5039 fsname[0] = '\0'; /* avoid matching in next loop */
5040 mntdir[0] = '\0'; /* avoid matching in next loop */
5046 static int lfs_getname(int argc, char **argv)
5048 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5049 int rc = 0, index = 0, c;
5050 char buf[sizeof(struct obd_uuid)];
5052 while ((c = getopt(argc, argv, "h")) != -1)
5055 if (optind == argc) { /* no paths specified, get all paths. */
5056 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5057 rc = llapi_getname(mntdir, buf, sizeof(buf));
5060 "cannot get name for `%s': %s\n",
5061 mntdir, strerror(-rc));
5065 printf("%s %s\n", buf, mntdir);
5067 path[0] = fsname[0] = mntdir[0] = 0;
5069 } else { /* paths specified, only attempt to search these. */
5070 for (; optind < argc; optind++) {
5071 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5074 "cannot get name for `%s': %s\n",
5075 argv[optind], strerror(-rc));
5079 printf("%s %s\n", buf, argv[optind]);
5085 static int lfs_check(int argc, char **argv)
5088 char mntdir[PATH_MAX] = {'\0'};
5095 fprintf(stderr, "%s check: server type must be specified\n",
5100 obd_types[0] = obd_type1;
5101 obd_types[1] = obd_type2;
5103 if (strcmp(argv[1], "osts") == 0) {
5104 strcpy(obd_types[0], "osc");
5105 } else if (strcmp(argv[1], "mds") == 0) {
5106 strcpy(obd_types[0], "mdc");
5107 } else if (strcmp(argv[1], "servers") == 0) {
5109 strcpy(obd_types[0], "osc");
5110 strcpy(obd_types[1], "mdc");
5112 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5117 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5118 if (rc < 0 || mntdir[0] == '\0') {
5120 "%s check: cannot find mounted Lustre filesystem: %s\n",
5121 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5125 rc = llapi_target_check(num_types, obd_types, mntdir);
5127 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5128 progname, argv[1], strerror(-rc));
5134 #ifdef HAVE_SYS_QUOTA_H
5135 #define ARG2INT(nr, str, msg) \
5138 nr = strtol(str, &endp, 0); \
5139 if (*endp != '\0') { \
5140 fprintf(stderr, "%s: bad %s '%s'\n", \
5141 progname, msg, str); \
5146 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5148 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5149 * returns the value or ULONG_MAX on integer overflow or incorrect format
5151 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5152 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5153 * 3. empty integer value is interpreted as 0
5155 static unsigned long str2sec(const char* timestr)
5157 const char spec[] = "smhdw";
5158 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5159 unsigned long val = 0;
5162 if (strpbrk(timestr, spec) == NULL) {
5163 /* no specifiers inside the time string,
5164 should treat it as an integer value */
5165 val = strtoul(timestr, &tail, 10);
5166 return *tail ? ULONG_MAX : val;
5169 /* format string is XXwXXdXXhXXmXXs */
5175 v = strtoul(timestr, &tail, 10);
5176 if (v == ULONG_MAX || *tail == '\0')
5177 /* value too large (ULONG_MAX or more)
5178 or missing specifier */
5181 ptr = strchr(spec, *tail);
5183 /* unknown specifier */
5188 /* check if product will overflow the type */
5189 if (!(v < ULONG_MAX / mult[ind]))
5192 ADD_OVERFLOW(val, mult[ind] * v);
5193 if (val == ULONG_MAX)
5205 #define ARG2ULL(nr, str, def_units) \
5207 unsigned long long limit, units = def_units; \
5210 rc = llapi_parse_size(str, &limit, &units, 1); \
5212 fprintf(stderr, "%s: invalid limit '%s'\n", \
5219 static inline int has_times_option(int argc, char **argv)
5223 for (i = 1; i < argc; i++)
5224 if (!strcmp(argv[i], "-t"))
5230 int lfs_setquota_times(int argc, char **argv)
5233 struct if_quotactl qctl;
5234 char *mnt, *obd_type = (char *)qctl.obd_type;
5235 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5236 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5237 struct option long_opts[] = {
5238 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5239 { .val = 'g', .name = "group", .has_arg = no_argument },
5240 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5241 { .val = 'p', .name = "projid", .has_arg = no_argument },
5242 { .val = 't', .name = "times", .has_arg = no_argument },
5243 { .val = 'u', .name = "user", .has_arg = no_argument },
5247 memset(&qctl, 0, sizeof(qctl));
5248 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5249 qctl.qc_type = ALLQUOTA;
5251 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5252 long_opts, NULL)) != -1) {
5263 if (qctl.qc_type != ALLQUOTA) {
5264 fprintf(stderr, "error: -u/g/p can't be used "
5265 "more than once\n");
5268 qctl.qc_type = qtype;
5271 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5272 fprintf(stderr, "error: bad block-grace: %s\n",
5276 dqb->dqb_valid |= QIF_BTIME;
5279 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5280 fprintf(stderr, "error: bad inode-grace: %s\n",
5284 dqb->dqb_valid |= QIF_ITIME;
5286 case 't': /* Yes, of course! */
5288 default: /* getopt prints error message for us when opterr != 0 */
5293 if (qctl.qc_type == ALLQUOTA) {
5294 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5298 if (optind != argc - 1) {
5299 fprintf(stderr, "error: unexpected parameters encountered\n");
5304 rc = llapi_quotactl(mnt, &qctl);
5307 fprintf(stderr, "%s %s ", obd_type,
5308 obd_uuid2str(&qctl.obd_uuid));
5309 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5316 #define BSLIMIT (1 << 0)
5317 #define BHLIMIT (1 << 1)
5318 #define ISLIMIT (1 << 2)
5319 #define IHLIMIT (1 << 3)
5321 int lfs_setquota(int argc, char **argv)
5324 struct if_quotactl qctl;
5325 char *mnt, *obd_type = (char *)qctl.obd_type;
5326 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5327 struct option long_opts[] = {
5328 { .val = 'b', .name = "block-softlimit",
5329 .has_arg = required_argument },
5330 { .val = 'B', .name = "block-hardlimit",
5331 .has_arg = required_argument },
5332 { .val = 'g', .name = "group", .has_arg = required_argument },
5333 { .val = 'i', .name = "inode-softlimit",
5334 .has_arg = required_argument },
5335 { .val = 'I', .name = "inode-hardlimit",
5336 .has_arg = required_argument },
5337 { .val = 'p', .name = "projid", .has_arg = required_argument },
5338 { .val = 'u', .name = "user", .has_arg = required_argument },
5340 unsigned limit_mask = 0;
5344 if (has_times_option(argc, argv))
5345 return lfs_setquota_times(argc, argv);
5347 memset(&qctl, 0, sizeof(qctl));
5348 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5349 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5350 * so it can be used as a marker that qc_type
5351 * isn't reinitialized from command line */
5353 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
5354 long_opts, NULL)) != -1) {
5358 rc = name2uid(&qctl.qc_id, optarg);
5362 rc = name2gid(&qctl.qc_id, optarg);
5366 rc = name2projid(&qctl.qc_id, optarg);
5368 if (qctl.qc_type != ALLQUOTA) {
5370 "%s setquota: only one of -u, -g or -p may be specified\n",
5374 qctl.qc_type = qtype;
5376 qctl.qc_id = strtoul(optarg, &endptr, 10);
5377 if (*endptr != '\0') {
5379 "%s setquota: invalid id '%s'\n",
5386 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5387 dqb->dqb_bsoftlimit >>= 10;
5388 limit_mask |= BSLIMIT;
5389 if (dqb->dqb_bsoftlimit &&
5390 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5392 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5393 "See '%s help setquota' or Lustre manual for details\n",
5394 progname, dqb->dqb_bsoftlimit,
5398 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5399 dqb->dqb_bhardlimit >>= 10;
5400 limit_mask |= BHLIMIT;
5401 if (dqb->dqb_bhardlimit &&
5402 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5404 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5405 "See '%s help setquota' or Lustre manual for details\n",
5406 progname, dqb->dqb_bhardlimit,
5410 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5411 limit_mask |= ISLIMIT;
5412 if (dqb->dqb_isoftlimit &&
5413 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5415 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5416 "See '%s help setquota' or Lustre manual for details\n",
5417 progname, dqb->dqb_isoftlimit,
5421 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5422 limit_mask |= IHLIMIT;
5423 if (dqb->dqb_ihardlimit &&
5424 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5426 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5427 "See '%s help setquota' or Lustre manual for details\n",
5428 progname, dqb->dqb_ihardlimit,
5433 "%s setquota: unrecognized option '%s'\n",
5434 progname, argv[optind - 1]);
5439 if (qctl.qc_type == ALLQUOTA) {
5441 "%s setquota: either -u or -g must be specified\n",
5446 if (limit_mask == 0) {
5448 "%s setquota: at least one limit must be specified\n",
5453 if (optind != argc - 1) {
5455 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5456 progname, argv[optind]);
5462 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5463 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5464 /* sigh, we can't just set blimits/ilimits */
5465 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5466 .qc_type = qctl.qc_type,
5467 .qc_id = qctl.qc_id};
5469 rc = llapi_quotactl(mnt, &tmp_qctl);
5473 if (!(limit_mask & BHLIMIT))
5474 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5475 if (!(limit_mask & BSLIMIT))
5476 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5477 if (!(limit_mask & IHLIMIT))
5478 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5479 if (!(limit_mask & ISLIMIT))
5480 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5482 /* Keep grace times if we have got no softlimit arguments */
5483 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5484 dqb->dqb_valid |= QIF_BTIME;
5485 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5488 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5489 dqb->dqb_valid |= QIF_ITIME;
5490 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5494 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5495 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5497 rc = llapi_quotactl(mnt, &qctl);
5501 "%s setquota: cannot quotactl '%s' '%s': %s",
5503 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5510 /* Converts seconds value into format string
5511 * result is returned in buf
5513 * 1. result is in descenting order: 1w2d3h4m5s
5514 * 2. zero fields are not filled (except for p. 3): 5d1s
5515 * 3. zero seconds value is presented as "0s"
5517 static char * __sec2str(time_t seconds, char *buf)
5519 const char spec[] = "smhdw";
5520 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5525 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5526 c = seconds / mult[i];
5528 if (c > 0 || (i == 0 && buf == tail))
5529 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5537 static void sec2str(time_t seconds, char *buf, int rc)
5544 tail = __sec2str(seconds, tail);
5546 if (rc && tail - buf < 39) {
5552 static void diff2str(time_t seconds, char *buf, time_t now)
5558 if (seconds <= now) {
5559 strcpy(buf, "none");
5562 __sec2str(seconds - now, buf);
5565 static void print_quota_title(char *name, struct if_quotactl *qctl,
5566 bool human_readable)
5568 printf("Disk quotas for %s %s (%cid %u):\n",
5569 qtype_name(qctl->qc_type), name,
5570 *qtype_name(qctl->qc_type), qctl->qc_id);
5571 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5572 "Filesystem", human_readable ? "used" : "kbytes",
5573 "quota", "limit", "grace",
5574 "files", "quota", "limit", "grace");
5577 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5580 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5583 snprintf(buf, buflen, "%5.4gP",
5584 (double)num / ((__u64)1 << 40));
5586 snprintf(buf, buflen, "%5.4gT",
5587 (double)num / (1 << 30));
5589 snprintf(buf, buflen, "%5.4gG",
5590 (double)num / (1 << 20));
5592 snprintf(buf, buflen, "%5.4gM",
5593 (double)num / (1 << 10));
5595 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5599 #define STRBUF_LEN 32
5600 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5607 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
5608 int bover = 0, iover = 0;
5609 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5610 char numbuf[3][STRBUF_LEN];
5612 char strbuf[STRBUF_LEN];
5614 if (dqb->dqb_bhardlimit &&
5615 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5617 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5618 if (dqb->dqb_btime > now) {
5625 if (dqb->dqb_ihardlimit &&
5626 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5628 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5629 if (dqb->dqb_itime > now) {
5637 if (strlen(mnt) > 15)
5638 printf("%s\n%15s", mnt, "");
5640 printf("%15s", mnt);
5643 diff2str(dqb->dqb_btime, timebuf, now);
5645 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5646 strbuf, sizeof(strbuf), h);
5647 if (rc == -EREMOTEIO)
5648 sprintf(numbuf[0], "%s*", strbuf);
5650 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5651 "%s" : "[%s]", strbuf);
5653 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5654 if (type == QC_GENERAL)
5655 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5656 "%s" : "[%s]", strbuf);
5658 sprintf(numbuf[1], "%s", "-");
5660 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5661 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5662 "%s" : "[%s]", strbuf);
5664 printf(" %7s%c %6s %7s %7s",
5665 numbuf[0], bover ? '*' : ' ', numbuf[1],
5666 numbuf[2], bover > 1 ? timebuf : "-");
5669 diff2str(dqb->dqb_itime, timebuf, now);
5671 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
5672 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
5674 if (type == QC_GENERAL)
5675 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5677 (uintmax_t)dqb->dqb_isoftlimit);
5679 sprintf(numbuf[1], "%s", "-");
5681 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5682 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5684 if (type != QC_OSTIDX)
5685 printf(" %7s%c %6s %7s %7s",
5686 numbuf[0], iover ? '*' : ' ', numbuf[1],
5687 numbuf[2], iover > 1 ? timebuf : "-");
5689 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5692 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5693 qctl->qc_cmd == Q_GETOINFO) {
5697 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5698 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5699 printf("Block grace time: %s; Inode grace time: %s\n",
5700 bgtimebuf, igtimebuf);
5704 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5705 bool h, __u64 *total)
5707 int rc = 0, rc1 = 0, count = 0;
5708 __u32 valid = qctl->qc_valid;
5710 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5712 fprintf(stderr, "can not get %s count: %s\n",
5713 is_mdt ? "mdt": "ost", strerror(-rc));
5717 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5718 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5719 rc = llapi_quotactl(mnt, qctl);
5721 /* It is remote client case. */
5722 if (rc == -EOPNOTSUPP) {
5729 fprintf(stderr, "quotactl %s%d failed.\n",
5730 is_mdt ? "mdt": "ost", qctl->qc_idx);
5734 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5735 qctl->qc_valid, 0, h);
5736 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5737 qctl->qc_dqblk.dqb_bhardlimit;
5740 qctl->qc_valid = valid;
5744 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5745 int verbose, int quiet, bool human_readable)
5747 int rc1 = 0, rc2 = 0, rc3 = 0;
5748 char *obd_type = (char *)qctl->obd_type;
5749 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5750 __u64 total_ialloc = 0, total_balloc = 0;
5753 rc1 = llapi_quotactl(mnt, qctl);
5757 fprintf(stderr, "%s quotas are not enabled.\n",
5758 qtype_name(qctl->qc_type));
5761 fprintf(stderr, "Permission denied.\n");
5764 /* We already got error message. */
5767 fprintf(stderr, "Unexpected quotactl error: %s\n",
5772 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
5773 print_quota_title(name, qctl, human_readable);
5775 if (rc1 && *obd_type)
5776 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5778 if (qctl->qc_valid != QC_GENERAL)
5781 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5782 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5783 (QIF_LIMITS|QIF_USAGE));
5785 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
5787 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
5789 char strbuf[STRBUF_LEN];
5791 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5793 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5795 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5797 printf("Total allocated inode limit: %ju, total "
5798 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5802 if (rc1 || rc2 || rc3 || inacc)
5803 printf("Some errors happened when getting quota info. "
5804 "Some devices may be not working or deactivated. "
5805 "The data in \"[]\" is inaccurate.\n");
5811 static int lfs_project(int argc, char **argv)
5813 int ret = 0, err = 0, c, i;
5814 struct project_handle_control phc = { 0 };
5815 enum lfs_project_ops_t op;
5818 phc.assign_projid = false;
5819 /* default action */
5820 op = LFS_PROJECT_LIST;
5822 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
5825 if (op != LFS_PROJECT_LIST) {
5827 "%s: cannot specify '-c' '-C' '-s' together\n",
5832 op = LFS_PROJECT_CHECK;
5835 if (op != LFS_PROJECT_LIST) {
5837 "%s: cannot specify '-c' '-C' '-s' together\n",
5842 op = LFS_PROJECT_CLEAR;
5845 if (op != LFS_PROJECT_LIST) {
5847 "%s: cannot specify '-c' '-C' '-s' together\n",
5852 phc.set_inherit = true;
5853 op = LFS_PROJECT_SET;
5859 phc.keep_projid = true;
5862 phc.recursive = true;
5865 phc.projid = strtoul(optarg, NULL, 0);
5866 phc.assign_projid = true;
5870 phc.newline = false;
5873 fprintf(stderr, "%s: invalid option '%c'\n",
5879 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
5880 op = LFS_PROJECT_SET;
5881 phc.set_projid = true;
5882 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
5883 phc.set_projid = true;
5887 case LFS_PROJECT_CHECK:
5888 if (phc.keep_projid) {
5890 "%s: '-k' is useless together with '-c'\n",
5895 case LFS_PROJECT_CLEAR:
5898 "%s: '-0' is useless together with '-C'\n",
5902 if (phc.assign_projid) {
5904 "%s: '-p' is useless together with '-C'\n",
5909 case LFS_PROJECT_SET:
5912 "%s: '-0' is useless together with '-s'\n",
5916 if (phc.keep_projid) {
5918 "%s: '-k' is useless together with '-s'\n",
5926 "%s: '-0' is useless for list operations\n",
5936 fprintf(stderr, "%s: missing file or directory target(s)\n",
5941 for (i = 0; i < argc; i++) {
5943 case LFS_PROJECT_CHECK:
5944 err = lfs_project_check(argv[i], &phc);
5946 case LFS_PROJECT_LIST:
5947 err = lfs_project_list(argv[i], &phc);
5949 case LFS_PROJECT_CLEAR:
5950 err = lfs_project_clear(argv[i], &phc);
5952 case LFS_PROJECT_SET:
5953 err = lfs_project_set(argv[i], &phc);
5965 static int lfs_quota(int argc, char **argv)
5968 char *mnt, *name = NULL;
5969 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
5970 .qc_type = ALLQUOTA };
5971 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
5972 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
5974 __u32 valid = QC_GENERAL, idx = 0;
5975 bool human_readable = false;
5978 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
5989 if (qctl.qc_type != ALLQUOTA) {
5991 "%s quota: only one of -u, -g, or -p may be specified\n",
5995 qctl.qc_type = qtype;
5998 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6001 valid = qctl.qc_valid = QC_UUID;
6002 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6005 valid = qctl.qc_valid = QC_MDTIDX;
6006 idx = qctl.qc_idx = atoi(optarg);
6007 if (idx == 0 && *optarg != '0') {
6009 "%s quota: invalid MDT index '%s'\n",
6015 valid = qctl.qc_valid = QC_OSTIDX;
6016 idx = qctl.qc_idx = atoi(optarg);
6017 if (idx == 0 && *optarg != '0') {
6019 "%s quota: invalid OST index '%s'\n",
6031 human_readable = true;
6034 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6035 progname, argv[optind - 1]);
6040 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6041 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6042 optind == argc - 1) {
6044 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6045 qctl.qc_valid = valid;
6048 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6049 qctl.qc_type = qtype;
6050 if (qtype == USRQUOTA) {
6051 qctl.qc_id = geteuid();
6052 rc = uid2name(&name, qctl.qc_id);
6054 qctl.qc_id = getegid();
6055 rc = gid2name(&name, qctl.qc_id);
6060 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6065 /* lfs quota -u username /path/to/lustre/mount */
6066 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6067 /* options should be followed by u/g-name and mntpoint */
6068 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
6070 "%s quota: name and mount point must be specified\n",
6075 name = argv[optind++];
6076 switch (qctl.qc_type) {
6078 rc = name2uid(&qctl.qc_id, name);
6081 rc = name2gid(&qctl.qc_id, name);
6084 rc = name2projid(&qctl.qc_id, name);
6091 qctl.qc_id = strtoul(name, &endptr, 10);
6092 if (*endptr != '\0') {
6093 fprintf(stderr, "%s quota: invalid id '%s'\n",
6099 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6101 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6102 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6109 #endif /* HAVE_SYS_QUOTA_H! */
6111 static int flushctx_ioctl(char *mp)
6115 fd = open(mp, O_RDONLY);
6117 fprintf(stderr, "flushctx: error open %s: %s\n",
6118 mp, strerror(errno));
6122 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6124 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6125 mp, strerror(errno));
6131 static int lfs_flushctx(int argc, char **argv)
6133 int kdestroy = 0, c;
6134 char mntdir[PATH_MAX] = {'\0'};
6138 while ((c = getopt(argc, argv, "k")) != -1) {
6144 fprintf(stderr, "error: %s: option '-%c' "
6145 "unrecognized\n", argv[0], c);
6151 if ((rc = system("kdestroy > /dev/null")) != 0) {
6152 rc = WEXITSTATUS(rc);
6153 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6157 if (optind >= argc) {
6158 /* flush for all mounted lustre fs. */
6159 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6160 /* Check if we have a mount point */
6161 if (mntdir[0] == '\0')
6164 if (flushctx_ioctl(mntdir))
6167 mntdir[0] = '\0'; /* avoid matching in next loop */
6170 /* flush fs as specified */
6171 while (optind < argc) {
6172 if (flushctx_ioctl(argv[optind++]))
6179 static int lfs_cp(int argc, char **argv)
6181 fprintf(stderr, "remote client copy file(s).\n"
6182 "obsolete, does not support it anymore.\n");
6186 static int lfs_ls(int argc, char **argv)
6188 fprintf(stderr, "remote client lists directory contents.\n"
6189 "obsolete, does not support it anymore.\n");
6193 static int lfs_changelog(int argc, char **argv)
6195 void *changelog_priv;
6196 struct changelog_rec *rec;
6197 long long startrec = 0, endrec = 0;
6199 struct option long_opts[] = {
6200 { .val = 'f', .name = "follow", .has_arg = no_argument },
6202 char short_opts[] = "f";
6205 while ((rc = getopt_long(argc, argv, short_opts,
6206 long_opts, NULL)) != -1) {
6214 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6215 argv[0], argv[optind - 1]);
6222 mdd = argv[optind++];
6224 startrec = strtoll(argv[optind++], NULL, 10);
6226 endrec = strtoll(argv[optind++], NULL, 10);
6228 rc = llapi_changelog_start(&changelog_priv,
6229 CHANGELOG_FLAG_BLOCK |
6230 CHANGELOG_FLAG_JOBID |
6231 CHANGELOG_FLAG_EXTRA_FLAGS |
6232 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6235 fprintf(stderr, "Can't start changelog: %s\n",
6236 strerror(errno = -rc));
6240 rc = llapi_changelog_set_xflags(changelog_priv,
6241 CHANGELOG_EXTRA_FLAG_UIDGID |
6242 CHANGELOG_EXTRA_FLAG_NID |
6243 CHANGELOG_EXTRA_FLAG_OMODE |
6244 CHANGELOG_EXTRA_FLAG_XATTR);
6246 fprintf(stderr, "Can't set xflags for changelog: %s\n",
6247 strerror(errno = -rc));
6251 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6255 if (endrec && rec->cr_index > endrec) {
6256 llapi_changelog_free(&rec);
6259 if (rec->cr_index < startrec) {
6260 llapi_changelog_free(&rec);
6264 secs = rec->cr_time >> 30;
6265 gmtime_r(&secs, &ts);
6266 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6267 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6268 changelog_type2str(rec->cr_type),
6269 ts.tm_hour, ts.tm_min, ts.tm_sec,
6270 (int)(rec->cr_time & ((1 << 30) - 1)),
6271 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6272 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6274 if (rec->cr_flags & CLF_JOBID) {
6275 struct changelog_ext_jobid *jid =
6276 changelog_rec_jobid(rec);
6278 if (jid->cr_jobid[0] != '\0')
6279 printf(" j=%s", jid->cr_jobid);
6282 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6283 struct changelog_ext_extra_flags *ef =
6284 changelog_rec_extra_flags(rec);
6286 printf(" ef=0x%llx", ef->cr_extra_flags);
6288 if (ef->cr_extra_flags & CLFE_UIDGID) {
6289 struct changelog_ext_uidgid *uidgid =
6290 changelog_rec_uidgid(rec);
6292 printf(" u=%llu:%llu",
6293 uidgid->cr_uid, uidgid->cr_gid);
6295 if (ef->cr_extra_flags & CLFE_NID) {
6296 struct changelog_ext_nid *nid =
6297 changelog_rec_nid(rec);
6300 libcfs_nid2str(nid->cr_nid));
6303 if (ef->cr_extra_flags & CLFE_OPEN) {
6304 struct changelog_ext_openmode *omd =
6305 changelog_rec_openmode(rec);
6306 char mode[] = "---";
6308 /* exec mode must be exclusive */
6309 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6312 if (omd->cr_openflags & FMODE_READ)
6314 if (omd->cr_openflags &
6321 if (strcmp(mode, "---") != 0)
6322 printf(" m=%s", mode);
6326 if (ef->cr_extra_flags & CLFE_XATTR) {
6327 struct changelog_ext_xattr *xattr =
6328 changelog_rec_xattr(rec);
6330 if (xattr->cr_xattr[0] != '\0')
6331 printf(" x=%s", xattr->cr_xattr);
6335 if (rec->cr_namelen)
6336 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6337 rec->cr_namelen, changelog_rec_name(rec));
6339 if (rec->cr_flags & CLF_RENAME) {
6340 struct changelog_ext_rename *rnm =
6341 changelog_rec_rename(rec);
6343 if (!fid_is_zero(&rnm->cr_sfid))
6344 printf(" s="DFID" sp="DFID" %.*s",
6345 PFID(&rnm->cr_sfid),
6346 PFID(&rnm->cr_spfid),
6347 (int)changelog_rec_snamelen(rec),
6348 changelog_rec_sname(rec));
6352 llapi_changelog_free(&rec);
6355 llapi_changelog_fini(&changelog_priv);
6358 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
6360 return (rc == 1 ? 0 : rc);
6363 static int lfs_changelog_clear(int argc, char **argv)
6371 endrec = strtoll(argv[3], NULL, 10);
6373 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6376 fprintf(stderr, "%s: record out of range: %llu\n",
6378 else if (rc == -ENOENT)
6379 fprintf(stderr, "%s: no changelog user: %s\n",
6382 fprintf(stderr, "%s error: %s\n", argv[0],
6391 static int lfs_fid2path(int argc, char **argv)
6393 struct option long_opts[] = {
6394 { .val = 'c', .name = "cur", .has_arg = no_argument },
6395 { .val = 'l', .name = "link", .has_arg = required_argument },
6396 { .val = 'r', .name = "rec", .has_arg = required_argument },
6398 char short_opts[] = "cl:r:";
6399 char *device, *fid, *path;
6400 long long recno = -1;
6406 while ((rc = getopt_long(argc, argv, short_opts,
6407 long_opts, NULL)) != -1) {
6413 linkno = strtol(optarg, NULL, 10);
6416 recno = strtoll(optarg, NULL, 10);
6421 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6422 argv[0], argv[optind - 1]);
6430 device = argv[optind++];
6431 path = calloc(1, PATH_MAX);
6433 fprintf(stderr, "error: Not enough memory\n");
6438 while (optind < argc) {
6439 fid = argv[optind++];
6441 lnktmp = (linkno >= 0) ? linkno : 0;
6443 int oldtmp = lnktmp;
6444 long long rectmp = recno;
6446 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6449 fprintf(stderr, "%s: error on FID %s: %s\n",
6450 argv[0], fid, strerror(errno = -rc2));
6457 fprintf(stdout, "%lld ", rectmp);
6458 if (device[0] == '/') {
6459 fprintf(stdout, "%s", device);
6460 if (device[strlen(device) - 1] != '/')
6461 fprintf(stdout, "/");
6462 } else if (path[0] == '\0') {
6463 fprintf(stdout, "/");
6465 fprintf(stdout, "%s\n", path);
6468 /* specified linkno */
6470 if (oldtmp == lnktmp)
6480 static int lfs_path2fid(int argc, char **argv)
6482 struct option long_opts[] = {
6483 { .val = 'p', .name = "parents", .has_arg = no_argument },
6486 const char short_opts[] = "p";
6487 const char *sep = "";
6490 bool show_parents = false;
6492 while ((rc = getopt_long(argc, argv, short_opts,
6493 long_opts, NULL)) != -1) {
6496 show_parents = true;
6499 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6500 argv[0], argv[optind - 1]);
6505 if (optind > argc - 1)
6507 else if (optind < argc - 1)
6511 for (path = argv + optind; *path != NULL; path++) {
6513 if (!show_parents) {
6514 err = llapi_path2fid(*path, &fid);
6516 printf("%s%s"DFID"\n",
6517 *sep != '\0' ? *path : "", sep,
6520 char name[NAME_MAX + 1];
6521 unsigned int linkno = 0;
6523 while ((err = llapi_path2parent(*path, linkno, &fid,
6524 name, sizeof(name))) == 0) {
6525 if (*sep != '\0' && linkno == 0)
6526 printf("%s%s", *path, sep);
6528 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6533 /* err == -ENODATA is end-of-loop */
6534 if (linkno > 0 && err == -ENODATA) {
6541 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
6542 argv[0], show_parents ? "parent " : "", *path,
6554 static int lfs_data_version(int argc, char **argv)
6561 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
6566 while ((c = getopt(argc, argv, "nrw")) != -1) {
6569 data_version_flags = 0;
6572 data_version_flags |= LL_DV_RD_FLUSH;
6575 data_version_flags |= LL_DV_WR_FLUSH;
6584 path = argv[optind];
6585 fd = open(path, O_RDONLY);
6587 err(errno, "cannot open file %s", path);
6589 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
6591 err(errno, "cannot get version for %s", path);
6593 printf("%ju" "\n", (uintmax_t)data_version);
6599 static int lfs_hsm_state(int argc, char **argv)
6604 struct hsm_user_state hus;
6612 rc = llapi_hsm_state_get(path, &hus);
6614 fprintf(stderr, "can't get hsm state for %s: %s\n",
6615 path, strerror(errno = -rc));
6619 /* Display path name and status flags */
6620 printf("%s: (0x%08x)", path, hus.hus_states);
6622 if (hus.hus_states & HS_RELEASED)
6623 printf(" released");
6624 if (hus.hus_states & HS_EXISTS)
6626 if (hus.hus_states & HS_DIRTY)
6628 if (hus.hus_states & HS_ARCHIVED)
6629 printf(" archived");
6630 /* Display user-settable flags */
6631 if (hus.hus_states & HS_NORELEASE)
6632 printf(" never_release");
6633 if (hus.hus_states & HS_NOARCHIVE)
6634 printf(" never_archive");
6635 if (hus.hus_states & HS_LOST)
6636 printf(" lost_from_hsm");
6638 if (hus.hus_archive_id != 0)
6639 printf(", archive_id:%d", hus.hus_archive_id);
6642 } while (++i < argc);
6647 #define LFS_HSM_SET 0
6648 #define LFS_HSM_CLEAR 1
6651 * Generic function to set or clear HSM flags.
6652 * Used by hsm_set and hsm_clear.
6654 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
6656 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
6658 struct option long_opts[] = {
6659 { .val = 'A', .name = "archived", .has_arg = no_argument },
6660 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
6661 { .val = 'd', .name = "dirty", .has_arg = no_argument },
6662 { .val = 'e', .name = "exists", .has_arg = no_argument },
6663 { .val = 'l', .name = "lost", .has_arg = no_argument },
6664 { .val = 'r', .name = "norelease", .has_arg = no_argument },
6665 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
6667 char short_opts[] = "lraAdei:";
6671 __u32 archive_id = 0;
6677 while ((c = getopt_long(argc, argv, short_opts,
6678 long_opts, NULL)) != -1) {
6684 mask |= HS_NOARCHIVE;
6687 mask |= HS_ARCHIVED;
6690 mask |= HS_NORELEASE;
6699 archive_id = strtol(optarg, &end, 10);
6701 fprintf(stderr, "invalid archive_id: '%s'\n",
6709 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6710 argv[0], argv[optind - 1]);
6715 /* User should have specified a flag */
6719 while (optind < argc) {
6721 path = argv[optind];
6723 /* If mode == 0, this means we apply the mask. */
6724 if (mode == LFS_HSM_SET)
6725 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6727 rc = llapi_hsm_state_set(path, 0, mask, 0);
6730 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6731 path, strerror(errno = -rc));
6740 static int lfs_hsm_action(int argc, char **argv)
6745 struct hsm_current_action hca;
6746 struct hsm_extent he;
6747 enum hsm_user_action hua;
6748 enum hsm_progress_states hps;
6756 rc = llapi_hsm_current_action(path, &hca);
6758 fprintf(stderr, "can't get hsm action for %s: %s\n",
6759 path, strerror(errno = -rc));
6762 he = hca.hca_location;
6763 hua = hca.hca_action;
6764 hps = hca.hca_state;
6766 printf("%s: %s", path, hsm_user_action2name(hua));
6768 /* Skip file without action */
6769 if (hca.hca_action == HUA_NONE) {
6774 printf(" %s ", hsm_progress_state2name(hps));
6776 if ((hps == HPS_RUNNING) &&
6777 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
6778 printf("(%llu bytes moved)\n",
6779 (unsigned long long)he.length);
6780 else if ((he.offset + he.length) == LUSTRE_EOF)
6781 printf("(from %llu to EOF)\n",
6782 (unsigned long long)he.offset);
6784 printf("(from %llu to %llu)\n",
6785 (unsigned long long)he.offset,
6786 (unsigned long long)(he.offset + he.length));
6788 } while (++i < argc);
6793 static int lfs_hsm_set(int argc, char **argv)
6795 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
6798 static int lfs_hsm_clear(int argc, char **argv)
6800 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
6804 * Check file state and return its fid, to be used by lfs_hsm_request().
6806 * \param[in] file Path to file to check
6807 * \param[in,out] fid Pointer to allocated lu_fid struct.
6808 * \param[in,out] last_dev Pointer to last device id used.
6810 * \return 0 on success.
6812 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
6818 rc = lstat(file, &st);
6820 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
6823 /* Checking for regular file as archiving as posix copytool
6824 * rejects archiving files other than regular files
6826 if (!S_ISREG(st.st_mode)) {
6827 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
6830 /* A request should be ... */
6831 if (*last_dev != st.st_dev && *last_dev != 0) {
6832 fprintf(stderr, "All files should be "
6833 "on the same filesystem: %s\n", file);
6836 *last_dev = st.st_dev;
6838 rc = llapi_path2fid(file, fid);
6840 fprintf(stderr, "Cannot read FID of %s: %s\n",
6841 file, strerror(-rc));
6847 /* Fill an HSM HUR item with a given file name.
6849 * If mntpath is set, then the filename is actually a FID, and no
6850 * lookup on the filesystem will be performed.
6852 * \param[in] hur the user request to fill
6853 * \param[in] idx index of the item inside the HUR to fill
6854 * \param[in] mntpath mountpoint of Lustre
6855 * \param[in] fname filename (if mtnpath is NULL)
6856 * or FID (if mntpath is set)
6857 * \param[in] last_dev pointer to last device id used
6859 * \retval 0 on success
6860 * \retval CMD_HELP or a negative errno on error
6862 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
6863 const char *mntpath, const char *fname,
6866 struct hsm_user_item *hui = &hur->hur_user_item[idx];
6869 hui->hui_extent.length = -1;
6871 if (mntpath != NULL) {
6874 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
6878 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
6883 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
6887 hur->hur_request.hr_itemcount++;
6892 static int lfs_hsm_request(int argc, char **argv, int action)
6894 struct option long_opts[] = {
6895 { .val = 'a', .name = "archive", .has_arg = required_argument },
6896 { .val = 'D', .name = "data", .has_arg = required_argument },
6897 { .val = 'l', .name = "filelist", .has_arg = required_argument },
6898 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
6901 char short_opts[] = "l:D:a:m:";
6902 struct hsm_user_request *hur, *oldhur;
6907 char *filelist = NULL;
6908 char fullpath[PATH_MAX];
6909 char *opaque = NULL;
6913 int nbfile_alloc = 0;
6914 char *some_file = NULL;
6915 char *mntpath = NULL;
6921 while ((c = getopt_long(argc, argv, short_opts,
6922 long_opts, NULL)) != -1) {
6931 if (action != HUA_ARCHIVE &&
6932 action != HUA_REMOVE) {
6934 "error: -a is supported only "
6935 "when archiving or removing\n");
6938 archive_id = atoi(optarg);
6941 if (some_file == NULL) {
6943 some_file = strdup(optarg);
6949 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6950 argv[0], argv[optind - 1]);
6955 /* All remaining args are files, so we have at least nbfile */
6956 nbfile = argc - optind;
6958 if ((nbfile == 0) && (filelist == NULL))
6962 opaque_len = strlen(opaque);
6964 /* Alloc the request structure with enough place to store all files
6965 * from command line. */
6966 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
6968 fprintf(stderr, "Cannot create the request: %s\n",
6972 nbfile_alloc = nbfile;
6974 hur->hur_request.hr_action = action;
6975 hur->hur_request.hr_archive_id = archive_id;
6976 hur->hur_request.hr_flags = 0;
6978 /* All remaining args are files, add them */
6979 if (nbfile != 0 && some_file == NULL)
6980 some_file = strdup(argv[optind]);
6982 for (i = 0; i < nbfile; i++) {
6983 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
6989 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
6991 /* If a filelist was specified, read the filelist from it. */
6992 if (filelist != NULL) {
6993 fp = fopen(filelist, "r");
6995 fprintf(stderr, "Cannot read the file list %s: %s\n",
6996 filelist, strerror(errno));
7001 while ((rc = getline(&line, &len, fp)) != -1) {
7002 /* If allocated buffer was too small, get something
7004 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7007 nbfile_alloc = nbfile_alloc * 2 + 1;
7009 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7012 fprintf(stderr, "hsm: cannot allocate "
7013 "the request: %s\n",
7020 size = hur_len(oldhur);
7022 fprintf(stderr, "hsm: cannot allocate "
7023 "%u files + %u bytes data\n",
7024 oldhur->hur_request.hr_itemcount,
7025 oldhur->hur_request.hr_data_len);
7032 memcpy(hur, oldhur, size);
7037 if (line[strlen(line) - 1] == '\n')
7038 line[strlen(line) - 1] = '\0';
7040 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7041 mntpath, line, &last_dev);
7047 if (some_file == NULL) {
7057 /* If a --data was used, add it to the request */
7058 hur->hur_request.hr_data_len = opaque_len;
7060 memcpy(hur_data(hur), opaque, opaque_len);
7062 /* Send the HSM request */
7063 if (realpath(some_file, fullpath) == NULL) {
7064 fprintf(stderr, "Could not find path '%s': %s\n",
7065 some_file, strerror(errno));
7067 rc = llapi_hsm_request(fullpath, hur);
7069 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7070 some_file, strerror(-rc));
7080 static int lfs_hsm_archive(int argc, char **argv)
7082 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7085 static int lfs_hsm_restore(int argc, char **argv)
7087 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7090 static int lfs_hsm_release(int argc, char **argv)
7092 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7095 static int lfs_hsm_remove(int argc, char **argv)
7097 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7100 static int lfs_hsm_cancel(int argc, char **argv)
7102 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7105 static int lfs_swap_layouts(int argc, char **argv)
7110 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7111 SWAP_LAYOUTS_KEEP_MTIME |
7112 SWAP_LAYOUTS_KEEP_ATIME);
7115 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7117 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7119 int lfs_get_mode(const char *string)
7121 enum lock_mode_user mode;
7123 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7124 if (lock_mode_names[mode] == NULL)
7126 if (strcmp(string, lock_mode_names[mode]) == 0)
7133 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7135 enum lu_ladvise_type advice;
7138 advice < ARRAY_SIZE(ladvise_names); advice++) {
7139 if (ladvise_names[advice] == NULL)
7141 if (strcmp(string, ladvise_names[advice]) == 0)
7145 return LU_LADVISE_INVALID;
7148 static int lfs_ladvise(int argc, char **argv)
7150 struct option long_opts[] = {
7151 { .val = 'a', .name = "advice", .has_arg = required_argument },
7152 { .val = 'b', .name = "background", .has_arg = no_argument },
7153 { .val = 'e', .name = "end", .has_arg = required_argument },
7154 { .val = 'l', .name = "length", .has_arg = required_argument },
7155 { .val = 'm', .name = "mode", .has_arg = required_argument },
7156 { .val = 's', .name = "start", .has_arg = required_argument },
7157 { .val = 'u', .name = "unset", .has_arg = no_argument },
7159 char short_opts[] = "a:be:l:m:s:u";
7164 struct llapi_lu_ladvise advice;
7165 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7166 unsigned long long start = 0;
7167 unsigned long long end = LUSTRE_EOF;
7168 unsigned long long length = 0;
7169 unsigned long long size_units;
7170 unsigned long long flags = 0;
7174 while ((c = getopt_long(argc, argv, short_opts,
7175 long_opts, NULL)) != -1) {
7178 advice_type = lfs_get_ladvice(optarg);
7179 if (advice_type == LU_LADVISE_INVALID) {
7180 fprintf(stderr, "%s: invalid advice type "
7181 "'%s'\n", argv[0], optarg);
7182 fprintf(stderr, "Valid types:");
7184 for (advice_type = 0;
7185 advice_type < ARRAY_SIZE(ladvise_names);
7187 if (ladvise_names[advice_type] == NULL)
7189 fprintf(stderr, " %s",
7190 ladvise_names[advice_type]);
7192 fprintf(stderr, "\n");
7205 rc = llapi_parse_size(optarg, &end,
7208 fprintf(stderr, "%s: bad end offset '%s'\n",
7215 rc = llapi_parse_size(optarg, &start,
7218 fprintf(stderr, "%s: bad start offset "
7219 "'%s'\n", argv[0], optarg);
7225 rc = llapi_parse_size(optarg, &length,
7228 fprintf(stderr, "%s: bad length '%s'\n",
7234 mode = lfs_get_mode(optarg);
7236 fprintf(stderr, "%s: bad mode '%s', valid "
7237 "modes are READ or WRITE\n",
7245 fprintf(stderr, "%s: option '%s' unrecognized\n",
7246 argv[0], argv[optind - 1]);
7251 if (advice_type == LU_LADVISE_INVALID) {
7252 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7253 fprintf(stderr, "Valid types:");
7254 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7256 if (ladvise_names[advice_type] == NULL)
7258 fprintf(stderr, " %s", ladvise_names[advice_type]);
7260 fprintf(stderr, "\n");
7264 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7265 fprintf(stderr, "%s: Lock no expand advice is a per file "
7266 "descriptor advice, so when called from lfs, "
7267 "it does nothing.\n", argv[0]);
7271 if (argc <= optind) {
7272 fprintf(stderr, "%s: please give one or more file names\n",
7277 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7278 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7283 if (end == LUSTRE_EOF && length != 0)
7284 end = start + length;
7287 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7288 argv[0], start, end);
7292 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7293 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7298 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7299 fprintf(stderr, "%s: mode is required with lockahead\n",
7304 while (optind < argc) {
7307 path = argv[optind++];
7309 fd = open(path, O_RDONLY);
7311 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7312 argv[0], path, strerror(errno));
7317 advice.lla_start = start;
7318 advice.lla_end = end;
7319 advice.lla_advice = advice_type;
7320 advice.lla_value1 = 0;
7321 advice.lla_value2 = 0;
7322 advice.lla_value3 = 0;
7323 advice.lla_value4 = 0;
7324 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7325 advice.lla_lockahead_mode = mode;
7326 advice.lla_peradvice_flags = flags;
7329 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7332 fprintf(stderr, "%s: cannot give advice '%s' to file "
7333 "'%s': %s\n", argv[0],
7334 ladvise_names[advice_type],
7335 path, strerror(errno));
7341 if (rc == 0 && rc2 < 0)
7347 /** The input string contains a comma delimited list of component ids and
7348 * ranges, for example "1,2-4,7".
7350 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7352 bool end_of_loop = false;
7360 while (!end_of_loop) {
7364 char *endptr = NULL;
7367 ptr = strchrnul(arg, ',');
7368 end_of_loop = *ptr == '\0';
7371 start_index = strtol(arg, &endptr, 0);
7372 if (endptr == arg) /* no data at all */
7374 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7376 if (start_index < 0)
7379 end_index = start_index;
7380 if (*endptr == '-') {
7381 end_index = strtol(endptr + 1, &endptr, 0);
7382 if (*endptr != '\0')
7384 if (end_index < start_index)
7388 for (i = start_index; i <= end_index && size > 0; i++) {
7391 /* remove duplicate */
7392 for (j = 0; j < nr; j++) {
7396 if (j == nr) { /* no duplicate */
7402 if (size == 0 && i < end_index)
7409 if (!end_of_loop && ptr != NULL)
7412 return rc < 0 ? rc : nr;
7416 * struct verify_mirror_id - Mirror id to be verified.
7417 * @mirror_id: A specified mirror id.
7418 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7420 struct verify_mirror_id {
7426 * compare_mirror_ids() - Compare mirror ids.
7427 * @layout: Mirror component list.
7428 * @cbdata: Callback data in verify_mirror_id structure.
7430 * This is a callback function called by llapi_layout_comp_iterate()
7431 * to compare the specified mirror id with the one in the current
7432 * component of @layout. If they are the same, then the specified
7433 * mirror id is valid.
7435 * Return: a negative error code on failure or
7436 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7437 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7440 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7442 struct verify_mirror_id *mirror_id_cbdata =
7443 (struct verify_mirror_id *)cbdata;
7447 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7451 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7452 progname, strerror(errno));
7456 if (mirror_id_cbdata->mirror_id == mirror_id) {
7457 mirror_id_cbdata->is_valid_id = true;
7458 return LLAPI_LAYOUT_ITER_STOP;
7461 return LLAPI_LAYOUT_ITER_CONT;
7465 * verify_mirror_ids() - Verify specified mirror ids.
7466 * @fname: Mirrored file name.
7467 * @mirror_ids: Specified mirror ids to be verified.
7468 * @ids_nr: Number of specified mirror ids.
7470 * This function verifies that specified @mirror_ids are valid
7471 * in the mirrored file @fname.
7473 * Return: 0 on success or a negative error code on failure.
7476 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7478 struct llapi_layout *layout = NULL;
7479 struct verify_mirror_id mirror_id_cbdata = { 0 };
7490 if (stat(fname, &stbuf) < 0) {
7491 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7492 progname, fname, strerror(errno));
7497 if (!S_ISREG(stbuf.st_mode)) {
7498 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7504 fd = open(fname, O_DIRECT | O_RDONLY);
7506 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7507 progname, fname, strerror(errno));
7512 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
7514 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
7515 progname, fname, strerror(errno));
7519 layout = llapi_layout_get_by_fd(fd, 0);
7520 if (layout == NULL) {
7521 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7522 progname, fname, strerror(errno));
7524 llapi_lease_release(fd);
7528 rc = llapi_layout_flags_get(layout, &flr_state);
7530 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7531 progname, fname, strerror(errno));
7536 flr_state &= LCM_FL_FLR_MASK;
7537 switch (flr_state) {
7540 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7541 progname, fname, llapi_layout_flags_string(flr_state));
7548 for (i = 0; i < ids_nr; i++) {
7549 mirror_id_cbdata.mirror_id = mirror_ids[i];
7550 mirror_id_cbdata.is_valid_id = false;
7552 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
7557 "%s: '%s' failed to verify mirror id: %u.\n",
7558 progname, fname, mirror_ids[i]);
7562 if (!mirror_id_cbdata.is_valid_id) {
7565 "%s: '%s' invalid specified mirror id: %u.\n",
7566 progname, fname, mirror_ids[i]);
7572 llapi_layout_free(layout);
7573 llapi_lease_release(fd);
7581 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
7582 __u16 *mirror_ids, int ids_nr)
7584 struct llapi_resync_comp comp_array[1024] = { { 0 } };
7585 struct llapi_layout *layout;
7593 if (stat(fname, &stbuf) < 0) {
7594 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7595 progname, fname, strerror(errno));
7599 if (!S_ISREG(stbuf.st_mode)) {
7600 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7606 fd = open(fname, O_DIRECT | O_RDWR);
7608 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7609 progname, fname, strerror(errno));
7614 layout = llapi_layout_get_by_fd(fd, 0);
7615 if (layout == NULL) {
7616 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7617 progname, fname, strerror(errno));
7622 rc = llapi_layout_flags_get(layout, &flr_state);
7624 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7625 progname, fname, strerror(errno));
7630 flr_state &= LCM_FL_FLR_MASK;
7631 if (flr_state == LCM_FL_NONE) {
7633 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
7638 /* get stale component info */
7639 comp_size = llapi_mirror_find_stale(layout, comp_array,
7640 ARRAY_SIZE(comp_array),
7641 mirror_ids, ids_nr);
7642 if (comp_size <= 0) {
7647 ioc->lil_mode = LL_LEASE_WRLCK;
7648 ioc->lil_flags = LL_LEASE_RESYNC;
7649 rc = llapi_lease_set(fd, ioc);
7651 if (rc == -EALREADY)
7655 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
7656 progname, fname, strerror(errno));
7661 while (idx < comp_size) {
7667 rc = llapi_lease_check(fd);
7668 if (rc != LL_LEASE_WRLCK) {
7669 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7674 mirror_id = comp_array[idx].lrc_mirror_id;
7675 end = comp_array[idx].lrc_end;
7677 /* try to combine adjacent component */
7678 for (i = idx + 1; i < comp_size; i++) {
7679 if (mirror_id != comp_array[i].lrc_mirror_id ||
7680 end != comp_array[i].lrc_start)
7682 end = comp_array[i].lrc_end;
7685 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7686 comp_array[idx].lrc_start,
7689 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7690 "%ld.\n", progname, fname, result);
7693 } else if (result > 0) {
7696 /* mark synced components */
7697 for (j = idx; j < i; j++)
7698 comp_array[j].lrc_synced = true;
7705 /* prepare ioc for lease put */
7706 ioc->lil_mode = LL_LEASE_UNLCK;
7707 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7709 for (idx = 0; idx < comp_size; idx++) {
7710 if (comp_array[idx].lrc_synced) {
7711 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7716 rc = llapi_lease_set(fd, ioc);
7718 if (rc == 0) /* lost lease lock */
7720 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7721 progname, fname, strerror(errno));
7725 * llapi_lease_set returns lease mode when it request to unlock
7731 llapi_layout_free(layout);
7738 static inline int lfs_mirror_resync(int argc, char **argv)
7740 struct ll_ioc_lease *ioc = NULL;
7741 __u16 mirror_ids[128] = { 0 };
7746 struct option long_opts[] = {
7747 { .val = 'o', .name = "only", .has_arg = required_argument },
7750 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
7753 rc = parse_mirror_ids(mirror_ids,
7754 sizeof(mirror_ids) / sizeof(__u16),
7758 "%s: bad mirror ids '%s'.\n",
7765 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7766 argv[0], argv[optind - 1]);
7772 if (argc == optind) {
7773 fprintf(stderr, "%s: no file name given.\n", argv[0]);
7778 if (ids_nr > 0 && argc > optind + 1) {
7780 "%s: option '--only' cannot be used upon multiple files.\n",
7788 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7793 /* set the lease on the file */
7794 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
7796 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
7797 argv[0], strerror(errno));
7802 for (; optind < argc; optind++) {
7803 rc = lfs_mirror_resync_file(argv[optind], ioc,
7804 mirror_ids, ids_nr);
7805 /* ignore previous file's error, continue with next file */
7808 memset(ioc, 0, sizeof(__u32) * 4096);
7817 * struct verify_chunk - Mirror chunk to be verified.
7818 * @chunk: [start, end) of the chunk.
7819 * @mirror_count: Number of mirror ids in @mirror_id array.
7820 * @mirror_id: Array of valid mirror ids that cover the chunk.
7822 struct verify_chunk {
7823 struct lu_extent chunk;
7824 unsigned int mirror_count;
7825 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
7829 * print_chunks() - Print chunk information.
7830 * @fname: Mirrored file name.
7831 * @chunks: Array of chunks.
7832 * @chunk_count: Number of chunks in @chunks array.
7834 * This function prints [start, end) of each chunk in @chunks
7835 * for mirrored file @fname, and also prints the valid mirror ids
7836 * that cover the chunk.
7841 void print_chunks(const char *fname, struct verify_chunk *chunks,
7847 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
7848 for (i = 0; i < chunk_count; i++) {
7849 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
7851 if (chunks[i].mirror_count == 0)
7852 fprintf(stdout, "\t[");
7854 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
7855 for (j = 1; j < chunks[i].mirror_count; j++)
7856 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
7858 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
7860 fprintf(stdout, "\n");
7864 * print_checksums() - Print CRC-32 checksum values.
7865 * @chunk: A chunk and its corresponding valid mirror ids.
7866 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
7868 * This function prints CRC-32 checksum values on @chunk for
7869 * each valid mirror that covers it.
7874 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
7879 "CRC-32 checksum value for chunk "DEXT":\n",
7880 PEXT(&chunk->chunk));
7881 for (i = 0; i < chunk->mirror_count; i++)
7882 fprintf(stdout, "Mirror %u:\t%#lx\n",
7883 chunk->mirror_id[i], crc[i]);
7884 fprintf(stdout, "\n");
7888 * filter_mirror_id() - Filter specified mirror ids.
7889 * @chunks: Array of chunks.
7890 * @chunk_count: Number of chunks in @chunks array.
7891 * @mirror_ids: Specified mirror ids to be verified.
7892 * @ids_nr: Number of specified mirror ids.
7894 * This function scans valid mirror ids that cover each chunk in @chunks
7895 * and filters specified mirror ids.
7900 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
7901 __u16 *mirror_ids, int ids_nr)
7906 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7907 unsigned int valid_count = 0;
7909 for (i = 0; i < chunk_count; i++) {
7910 if (chunks[i].mirror_count == 0)
7914 for (j = 0; j < ids_nr; j++) {
7915 for (k = 0; k < chunks[i].mirror_count; k++) {
7916 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
7917 valid_id[valid_count] = mirror_ids[j];
7924 memcpy(chunks[i].mirror_id, valid_id,
7925 sizeof(__u16) * valid_count);
7926 chunks[i].mirror_count = valid_count;
7931 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
7932 * @layout: Mirror component list.
7933 * @chunks: Array of chunks.
7934 * @chunks_size: Array size of @chunks.
7936 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
7937 * to find out chunk segments and store them in @chunks array.
7939 * The @mirror_id array in each element of @chunks will store the valid
7940 * mirror ids that cover the chunk. If a mirror component covering the
7941 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
7942 * will not be stored into the @mirror_id array, and the chunk for that
7943 * mirror will not be verified.
7945 * The @mirror_count in each element of @chunks will store the number of
7946 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
7947 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
7948 * indicates the chunk is valid in only one mirror. In both cases, the
7949 * chunk will not be verified.
7951 * Here is an example:
7954 * +------+-------------+--------------------+
7956 * +------+------+------+------+-------------+
7957 * | | S | S | | mirror2
7958 * +-------------+------+------+-------------+
7960 * prepared @chunks array will contain 5 elements:
7961 * (([0, 1M), [1, 2], 2),
7962 * ([1M, 2M), [1, 2], 2),
7963 * ([2M, 3M), [1], 1),
7964 * ([3M, 4M], [], 0),
7965 * ([4M, EOF), [2], 1))
7967 * Return: the actual array size of @chunks on success
7968 * or a negative error code on failure.
7971 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
7972 struct verify_chunk *chunks,
7983 memset(chunks, 0, sizeof(*chunks) * chunks_size);
7986 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
7989 "%s: move to the first layout component: %s.\n",
7990 progname, strerror(errno));
7996 chunks[idx].chunk.e_end = LUSTRE_EOF;
7998 rc = llapi_layout_comp_extent_get(layout, &start, &end);
8001 "%s: llapi_layout_comp_extent_get failed: %s.\n",
8002 progname, strerror(errno));
8006 if (start > chunks[idx].chunk.e_start ||
8007 end <= chunks[idx].chunk.e_start)
8010 if (end < chunks[idx].chunk.e_end)
8011 chunks[idx].chunk.e_end = end;
8013 rc = llapi_layout_comp_flags_get(layout, &flags);
8016 "%s: llapi_layout_comp_flags_get failed: %s.\n",
8017 progname, strerror(errno));
8021 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8024 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8027 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8028 progname, strerror(errno));
8032 chunks[idx].mirror_id[i] = mirror_id;
8034 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8036 "%s: mirror_id array is too small.\n",
8043 rc = llapi_layout_comp_use(layout,
8044 LLAPI_LAYOUT_COMP_USE_NEXT);
8047 "%s: move to the next layout component: %s.\n",
8048 progname, strerror(errno));
8051 } /* loop through all components */
8053 chunks[idx].mirror_count = i;
8055 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8059 if (idx >= chunks_size) {
8060 fprintf(stderr, "%s: chunks array is too small.\n",
8066 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
8070 return rc < 0 ? rc : idx + 1;
8074 * lfs_mirror_verify_chunk() - Verify a chunk.
8075 * @fd: File descriptor of the mirrored file.
8076 * @file_size: Size of the mirrored file.
8077 * @chunk: A chunk and its corresponding valid mirror ids.
8078 * @verbose: Verbose mode.
8080 * This function verifies a @chunk contains exactly the same data
8081 * ammong the mirrors that cover it.
8083 * If @verbose is specified, then the function will print where the
8084 * differences are if the data do not match. Otherwise, it will
8085 * just return an error in that case.
8087 * Return: 0 on success or a negative error code on failure.
8090 int lfs_mirror_verify_chunk(int fd, size_t file_size,
8091 struct verify_chunk *chunk, int verbose)
8093 const size_t buflen = 4 * 1024 * 1024; /* 4M */
8095 size_t page_size = sysconf(_SC_PAGESIZE);
8101 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8108 rc = posix_memalign(&buf, page_size, buflen);
8109 if (rc) /* error code is returned directly */
8113 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8114 PEXT(&chunk->chunk));
8115 for (i = 0; i < chunk->mirror_count; i++)
8116 fprintf(stdout, " %u", chunk->mirror_id[i]);
8117 fprintf(stdout, "\n");
8121 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
8122 pos = chunk->chunk.e_start;
8123 while (bytes_done < count) {
8124 /* compute initial CRC-32 checksum */
8125 crc = crc32(0L, Z_NULL, 0);
8126 memset(crc_array, 0, sizeof(crc_array));
8129 for (i = 0; i < chunk->mirror_count; i++) {
8130 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
8132 if (bytes_read < 0) {
8135 "%s: failed to read data from mirror %u: %s.\n",
8136 progname, chunk->mirror_id[i],
8141 /* compute new CRC-32 checksum */
8142 crc_array[i] = crc32(crc, buf, bytes_read);
8146 print_checksums(chunk, crc_array);
8148 /* compare CRC-32 checksum values */
8149 for (i = 1; i < chunk->mirror_count; i++) {
8150 if (crc_array[i] != crc_array[0]) {
8156 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
8157 progname, PEXT(&chunk->chunk),
8158 chunk->mirror_id[0],
8159 chunk->mirror_id[i]);
8164 bytes_done += bytes_read;
8167 if (verbose > 1 && rc == 0) {
8168 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8169 PEXT(&chunk->chunk));
8170 for (i = 0; i < chunk->mirror_count; i++)
8171 fprintf(stdout, " %u", chunk->mirror_id[i]);
8172 fprintf(stdout, " PASS\n\n");
8181 * lfs_mirror_verify_file() - Verify a mirrored file.
8182 * @fname: Mirrored file name.
8183 * @mirror_ids: Specified mirror ids to be verified.
8184 * @ids_nr: Number of specified mirror ids.
8185 * @verbose: Verbose mode.
8187 * This function verifies that each SYNC mirror of a mirrored file
8188 * specified by @fname contains exactly the same data.
8190 * If @mirror_ids is specified, then the function will verify the
8191 * mirrors specified by @mirror_ids contain exactly the same data.
8193 * If @verbose is specified, then the function will print where the
8194 * differences are if the data do not match. Otherwise, it will
8195 * just return an error in that case.
8197 * Return: 0 on success or a negative error code on failure.
8200 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8203 struct verify_chunk chunks_array[1024] = { };
8204 struct llapi_layout *layout = NULL;
8208 int chunk_count = 0;
8214 if (stat(fname, &stbuf) < 0) {
8215 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8216 progname, fname, strerror(errno));
8221 if (!S_ISREG(stbuf.st_mode)) {
8222 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8228 if (stbuf.st_size == 0) {
8230 fprintf(stdout, "%s: '%s' file size is 0.\n",
8236 fd = open(fname, O_DIRECT | O_RDONLY);
8238 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8239 progname, fname, strerror(errno));
8244 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8246 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8247 progname, fname, strerror(errno));
8251 layout = llapi_layout_get_by_fd(fd, 0);
8252 if (layout == NULL) {
8253 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8254 progname, fname, strerror(errno));
8256 llapi_lease_release(fd);
8260 rc = llapi_layout_flags_get(layout, &flr_state);
8262 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8263 progname, fname, strerror(errno));
8268 flr_state &= LCM_FL_FLR_MASK;
8269 switch (flr_state) {
8272 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8273 progname, fname, llapi_layout_flags_string(flr_state));
8279 /* find out mirror chunks to be verified */
8280 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8281 ARRAY_SIZE(chunks_array));
8282 if (chunk_count < 0) {
8288 /* filter specified mirror ids */
8289 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8292 print_chunks(fname, chunks_array, chunk_count);
8294 for (idx = 0; idx < chunk_count; idx++) {
8295 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8298 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8300 PEXT(&chunks_array[idx].chunk),
8301 (unsigned long long)stbuf.st_size);
8305 if (chunks_array[idx].mirror_count == 0) {
8307 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8309 PEXT(&chunks_array[idx].chunk));
8311 fprintf(stderr, "skipped\n");
8315 fprintf(stderr, "failed\n");
8319 if (chunks_array[idx].mirror_count == 1) {
8322 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8324 PEXT(&chunks_array[idx].chunk),
8325 chunks_array[idx].mirror_id[0]);
8329 rc = llapi_lease_check(fd);
8330 if (rc != LL_LEASE_RDLCK) {
8331 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8336 /* verify one chunk */
8337 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8338 &chunks_array[idx], verbose);
8352 llapi_layout_free(layout);
8353 llapi_lease_release(fd);
8361 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
8362 * @argc: The count of lfs mirror verify command line arguments.
8363 * @argv: Array of strings for lfs mirror verify command line arguments.
8365 * This function parses lfs mirror verify command and verifies the
8366 * specified mirrored file(s).
8368 * Return: 0 on success or a negative error code on failure.
8370 static inline int lfs_mirror_verify(int argc, char **argv)
8372 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8380 struct option long_opts[] = {
8381 { .val = 'o', .name = "only", .has_arg = required_argument },
8382 { .val = 'v', .name = "verbose", .has_arg = no_argument },
8385 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8387 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
8390 rc = parse_mirror_ids(mirror_ids,
8391 ARRAY_SIZE(mirror_ids),
8395 "%s: bad mirror ids '%s'.\n",
8402 "%s: at least 2 mirror ids needed with '--only' option.\n",
8412 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8413 progname, argv[optind - 1]);
8419 if (argc == optind) {
8420 fprintf(stderr, "%s: no file name given.\n", progname);
8425 if (ids_nr > 0 && argc > optind + 1) {
8427 "%s: '--only' cannot be used upon multiple files.\n",
8435 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8441 for (; optind < argc; optind++) {
8442 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
8452 * lfs_mirror() - Parse and execute lfs mirror commands.
8453 * @argc: The count of lfs mirror command line arguments.
8454 * @argv: Array of strings for lfs mirror command line arguments.
8456 * This function parses lfs mirror commands and performs the
8457 * corresponding functions specified in mirror_cmdlist[].
8459 * Return: 0 on success or an error code on failure.
8461 static int lfs_mirror(int argc, char **argv)
8468 Parser_init("lfs-mirror > ", mirror_cmdlist);
8470 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8472 program_invocation_short_name = cmd;
8474 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
8476 rc = Parser_commands();
8478 return rc < 0 ? -rc : rc;
8482 * lfs_mirror_list_commands() - List lfs mirror commands.
8483 * @argc: The count of command line arguments.
8484 * @argv: Array of strings for command line arguments.
8486 * This function lists lfs mirror commands defined in mirror_cmdlist[].
8488 * Return: 0 on success.
8490 static int lfs_mirror_list_commands(int argc, char **argv)
8492 char buffer[81] = "";
8494 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
8500 static int lfs_list_commands(int argc, char **argv)
8502 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
8504 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
8509 int main(int argc, char **argv)
8513 /* Ensure that liblustreapi constructor has run */
8514 if (!llapi_liblustreapi_initialized())
8515 fprintf(stderr, "liblustreapi was not properly initialized\n");
8520 Parser_init("lfs > ", cmdlist);
8522 progname = program_invocation_short_name; /* Used in error messages */
8524 llapi_set_command_name(argv[1]);
8525 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
8526 llapi_clear_command_name();
8528 rc = Parser_commands();
8531 return rc < 0 ? -rc : rc;
8534 #ifdef _LUSTRE_IDL_H_
8535 /* Everything we need here should be included by lustreapi.h. */
8536 # error "lfs should not depend on lustre_idl.h"
8537 #endif /* _LUSTRE_IDL_H_ */