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" \
166 " [--copy=<lustre_src>]\n"
168 #define SSM_HELP_COMMON \
169 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
170 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
171 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
172 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
173 "\t respectively)\n" \
174 "\tpool_name: Name of OST pool to use (default none)\n" \
175 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
176 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
177 "\t Indices be specified in a format of:\n" \
178 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
180 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
181 "\t If --pool is set with --ost, then the OSTs\n" \
182 "\t must be the members of the pool.\n" \
183 "\tcomp_end: Extent end of component, start after previous end.\n"\
184 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
185 "\t respectively, -1 for EOF). Must be a multiple of\n"\
186 "\t stripe_size.\n" \
187 "\tyaml_template_file:\n" \
188 "\t YAML layout template file, can't be used with -c,\n" \
189 "\t -i, -S, -p, -o, or -E arguments.\n" \
190 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
191 "\t another lustre file or directory, can't used with\n" \
192 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
194 #define MIRROR_CREATE_HELP \
195 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
196 "\t setstripe layout options\n" \
197 "\t It defaults to 1 if not specified; if specified,\n" \
198 "\t it must follow the option without a space.\n" \
199 "\t The option can also be repeated multiple times to\n" \
200 "\t separate mirrors that have different layouts.\n" \
201 "\tsetstripe options: Mirror layout\n" \
202 "\t It can be a plain layout or a composite layout.\n" \
203 "\t If not specified, the stripe options inherited\n" \
204 "\t from the previous component will be used.\n" \
205 "\tflags: set flags to the component of the current mirror.\n" \
206 "\t Only \"prefer\" flag is supported so far.\n"
208 #define MIRROR_EXTEND_HELP \
210 "\tvictim_file: The layout of victim_file will be split and used\n" \
211 "\t as a mirror added to the mirrored file.\n" \
212 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
213 "\t from victim file(s) in case the victim file(s)\n" \
214 "\t contains the same data as the original mirrored\n" \
217 #define MIRROR_EXTEND_USAGE \
218 " <--mirror-count|-N[mirror_count]>\n" \
219 " [setstripe options|-f <victim_file>]\n" \
222 #define SETSTRIPE_USAGE \
223 SSM_CMD_COMMON("setstripe") \
224 MIRROR_EXTEND_USAGE \
225 " <directory|filename>\n" \
229 #define MIGRATE_USAGE \
230 SSM_CMD_COMMON("migrate ") \
232 " [--non-block|-n]\n" \
233 " [--non-direct|-D]\n" \
237 "\tblock: Block file access during data migration (default)\n" \
238 "\tnon-block: Abort migrations if concurrent access is detected\n" \
239 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
241 #define SETDIRSTRIPE_USAGE \
242 " [--mdt-count|-c stripe_count>\n" \
243 " [--mdt-index|-i mdt_index]\n" \
244 " [--mdt-hash|-H mdt_hash]\n" \
245 " [--default|-D] [--mode|-m mode] <dir>\n" \
246 "\tstripe_count: stripe count of the striped directory\n" \
247 "\tmdt_index: MDT index of first stripe\n" \
248 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
249 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
250 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
251 "\tdefault_stripe: set default dirstripe of the directory\n" \
252 "\tmode: the mode of the directory\n"
255 * command_t mirror_cmdlist - lfs mirror commands.
257 command_t mirror_cmdlist[] = {
258 { .pc_name = "create", .pc_func = lfs_mirror_create,
259 .pc_help = "Create a mirrored file.\n"
260 "usage: lfs mirror create "
261 "<--mirror-count|-N[mirror_count]> "
262 "[setstripe options] ... <filename|directory>\n"
263 MIRROR_CREATE_HELP },
264 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
265 .pc_help = "Extend a mirrored file.\n"
266 "usage: lfs mirror extend "
267 "<--mirror-count|-N[mirror_count]> [--no-verify] "
268 "[setstripe options|-f <victim_file>] ... <filename>\n"
269 MIRROR_EXTEND_HELP },
270 { .pc_name = "split", .pc_func = lfs_mirror_split,
271 .pc_help = "Split a mirrored file.\n"
272 "usage: lfs mirror split <--mirror-id <mirror_id>> [--destroy|-d] "
273 "[-f <new_file>] <mirrored file>\n"
274 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
275 "\t can be fetched by lfs getstripe command.\n"
276 "\tnew_file: This option indicates the layout of the split\n"
277 "\t mirror will be stored into. If not specified,\n"
278 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
279 "\t will be used.\n" },
280 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
281 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
282 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
283 "<mirrored file> [<mirrored file2>...]\n"},
284 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
285 .pc_help = "Verify mirrored file(s).\n"
286 "usage: lfs mirror verify "
287 "[--only <mirror_id,mirror_id2[,...]>] "
288 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
289 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
290 .pc_help = "list commands supported by lfs mirror"},
291 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
292 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
293 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
297 /* all available commands */
298 command_t cmdlist[] = {
299 {"setstripe", lfs_setstripe, 0,
300 "To create a file with specified striping/composite layout, or\n"
301 "create/replace the default layout on an existing directory:\n"
302 SSM_CMD_COMMON("setstripe")
303 " <directory|filename>\n"
305 "To add component(s) to an existing composite file:\n"
306 SSM_CMD_COMMON("setstripe --component-add")
308 "To totally delete the default striping from an existing directory:\n"
309 "usage: setstripe -d <directory>\n"
311 "To create a mirrored file or set s default mirror layout on a directory:\n"
312 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
314 "To delete the last component(s) from an existing composite file\n"
315 "(note that this will also delete any data in those components):\n"
316 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
317 " [--component-flags|-F <comp_flags>]\n"
319 "\tcomp_id: Unique component ID to delete\n"
320 "\tcomp_flags: 'init' indicating all instantiated components\n"
321 "\t '^init' indicating all uninstantiated components\n"
322 "\t-I and -F cannot be specified at the same time\n"},
323 {"getstripe", lfs_getstripe, 0,
324 "To list the layout pattern for a given file or files in a\n"
325 "directory or recursively for all files in a directory tree.\n"
326 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
327 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
328 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
329 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
330 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
331 " [--component-id[=comp_id]|-I[comp_id]]\n"
332 " [--component-flags[=comp_flags]]\n"
333 " [--component-count]\n"
334 " [--component-start[=[+-]comp_start]]\n"
335 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
336 " <directory|filename> ..."},
337 {"setdirstripe", lfs_setdirstripe, 0,
338 "To create a striped directory on a specified MDT. This can only\n"
339 "be done on MDT0 with the right of administrator.\n"
340 "usage: setdirstripe [OPTION] <directory>\n"
342 {"getdirstripe", lfs_getdirstripe, 0,
343 "To list the layout pattern info for a given directory\n"
344 "or recursively for all directories in a directory tree.\n"
345 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
346 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
347 " [--recursive|-r] [--yaml|-y]\n"
348 " [--default|-D] <dir> ..."},
349 {"mkdir", lfs_setdirstripe, 0,
350 "To create a striped directory on a specified MDT. This can only\n"
351 "be done on MDT0 with the right of administrator.\n"
352 "usage: mkdir [OPTION] <directory>\n"
354 {"rm_entry", lfs_rmentry, 0,
355 "To remove the name entry of the remote directory. Note: This\n"
356 "command will only delete the name entry, i.e. the remote directory\n"
357 "will become inaccessable after this command. This can only be done\n"
358 "by the administrator\n"
359 "usage: rm_entry <dir>\n"},
360 {"pool_list", lfs_poollist, 0,
361 "List pools or pool OSTs\n"
362 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
363 {"find", lfs_find, 0,
364 "find files matching given attributes recursively in directory tree.\n"
365 "usage: find <directory|filename> ...\n"
366 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
367 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
368 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
369 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
370 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
371 " [[!] --stripe-count|-c [+-]<stripes>]\n"
372 " [[!] --stripe-index|-i <index,...>]\n"
373 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
374 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
375 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
376 " [[!] --projid <projid>]\n"
377 " [[!] --layout|-L released,raid0,mdt]\n"
378 " [[!] --component-count [+-]<comp_cnt>]\n"
379 " [[!] --component-start [+-]N[kMGTPE]]\n"
380 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
381 " [[!] --component-flags <comp_flags>]\n"
382 " [[!] --mirror-count|-N [+-]<n>]\n"
383 " [[!] --mirror-state <[^]state>]\n"
384 " [[!] --mdt-count|-T [+-]<stripes>]\n"
385 " [[!] --mdt-hash|-H <hashtype>\n"
386 "\t !: used before an option indicates 'NOT' requested attribute\n"
387 "\t -: used before a value indicates less than requested value\n"
388 "\t +: used before a value indicates more than requested value\n"
389 "\thashtype: hash type of the striped directory.\n"
390 "\t fnv_1a_64 FNV-1a hash algorithm\n"
391 "\t all_char sum of characters % MDT_COUNT\n"},
392 {"check", lfs_check, 0,
393 "Display the status of MDS or OSTs (as specified in the command)\n"
394 "or all the servers (MDS and OSTs).\n"
395 "usage: check <osts|mds|servers>"},
396 {"osts", lfs_osts, 0, "list OSTs connected to client "
397 "[for specified path only]\n" "usage: osts [path]"},
398 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
399 "[for specified path only]\n" "usage: mdts [path]"},
401 "report filesystem disk space usage or inodes usage"
402 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
403 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
404 {"getname", lfs_getname, 0, "list instances and specified mount points "
405 "[for specified path only]\n"
406 "Usage: getname [-h]|[path ...] "},
407 #ifdef HAVE_SYS_QUOTA_H
408 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
409 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
410 " -b <block-softlimit> -B <block-hardlimit>\n"
411 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
412 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
413 " [--block-softlimit <block-softlimit>]\n"
414 " [--block-hardlimit <block-hardlimit>]\n"
415 " [--inode-softlimit <inode-softlimit>]\n"
416 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
417 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
418 " [--block-grace <block-grace>]\n"
419 " [--inode-grace <inode-grace>] <filesystem>\n"
420 " setquota <-U|-G|-P>\n"
421 " -b <block-softlimit> -B <block-hardlimit>\n"
422 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
423 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
424 " [--block-softlimit <block-softlimit>]\n"
425 " [--block-hardlimit <block-hardlimit>]\n"
426 " [--inode-softlimit <inode-softlimit>]\n"
427 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
428 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
430 " -b can be used instead of --block-softlimit/--block-grace\n"
431 " -B can be used instead of --block-hardlimit\n"
432 " -i can be used instead of --inode-softlimit/--inode-grace\n"
433 " -I can be used instead of --inode-hardlimit\n"
434 " -d can be used instead of --default\n\n"
435 "Note: The total quota space will be split into many qunits and\n"
436 " balanced over all server targets, the minimal qunit size is\n"
437 " 1M bytes for block space and 1K inodes for inode space.\n\n"
438 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
439 " Quota space rebalancing process will stop when this mininum\n"
440 " value is reached. As a result, quota exceeded can be returned\n"
441 " while many targets still have 1MB or 1K inodes of spare\n"
443 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
444 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
446 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
447 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
448 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
449 {"project", lfs_project, 0,
450 "Change or list project attribute for specified file or directory.\n"
451 "usage: project [-d|-r] <file|directory...>\n"
452 " list project ID and flags on file(s) or directories\n"
453 " project [-p id] [-s] [-r] <file|directory...>\n"
454 " set project ID and/or inherit flag for specified file(s) or directories\n"
455 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
456 " check project ID and flags on file(s) or directories, print outliers\n"
457 " project -C [-r] [-k] <file|directory...>\n"
458 " clear the project inherit flag and ID on the file or directory\n"
461 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
462 "usage: flushctx [-k] [mountpoint...]"},
464 "Remote user copy files and directories.\n"
465 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
467 "Remote user list directory contents.\n"
468 "usage: ls [OPTION]... [FILE]..."},
469 {"changelog", lfs_changelog, 0,
470 "Show the metadata changes on an MDT."
471 "\nusage: changelog <mdtname> [startrec [endrec]]"},
472 {"changelog_clear", lfs_changelog_clear, 0,
473 "Indicate that old changelog records up to <endrec> are no longer of "
474 "interest to consumer <id>, allowing the system to free up space.\n"
475 "An <endrec> of 0 means all records.\n"
476 "usage: changelog_clear <mdtname> <id> <endrec>"},
477 {"fid2path", lfs_fid2path, 0,
478 "Resolve the full path(s) for given FID(s). For a specific hardlink "
479 "specify link number <linkno>.\n"
480 /* "For a historical link name, specify changelog record <recno>.\n" */
481 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
482 /* [ --rec <recno> ] */ },
483 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
484 "usage: path2fid [--parents] <path> ..."},
485 {"data_version", lfs_data_version, 0, "Display file data version for "
486 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
487 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
488 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
489 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
490 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
491 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
492 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
494 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
495 "[--archived] [--lost] <file> ..."},
496 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
497 "given files.\n" "usage: hsm_action <file> ..."},
498 {"hsm_archive", lfs_hsm_archive, 0,
499 "Archive file to external storage.\n"
500 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
502 {"hsm_restore", lfs_hsm_restore, 0,
503 "Restore file from external storage.\n"
504 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
505 {"hsm_release", lfs_hsm_release, 0,
506 "Release files from Lustre.\n"
507 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
508 {"hsm_remove", lfs_hsm_remove, 0,
509 "Remove file copy from external storage.\n"
510 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
512 " (FILE [FILE ...] | "
513 "--mntpath MOUNTPATH FID [FID ...])\n"
515 "Note: To remove an archived copy of a file already deleted from a "
517 "--mntpath option and a list of FIDs must be specified"
519 {"hsm_cancel", lfs_hsm_cancel, 0,
520 "Cancel requests related to specified files.\n"
521 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
522 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
523 "usage: swap_layouts <path1> <path2>"},
524 {"migrate", lfs_setstripe_migrate, 0,
525 "migrate a directory between MDTs.\n"
526 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
528 "\tmdt_idx: index of the destination MDT\n"
530 "migrate file objects from one OST "
531 "layout\nto another (may be not safe with concurent writes).\n"
533 "[--stripe-count|-c] <stripe_count>\n"
534 " [--stripe-index|-i] <start_ost_index>\n"
535 " [--stripe-size|-S] <stripe_size>\n"
536 " [--pool|-p] <pool_name>\n"
537 " [--ost|-o] <ost_indices>\n"
539 " [--non-block|-n]\n"
540 " [--non-direct|-D]\n"
541 " <file|directory>\n"
542 "\tstripe_count: number of OSTs to stripe a file over\n"
543 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
544 "\tstripe_size: number of bytes to store before moving to the next OST\n"
545 "\tpool_name: name of the predefined pool of OSTs\n"
546 "\tost_indices: OSTs to stripe over, in order\n"
547 "\tblock: wait for the operation to return before continuing\n"
548 "\tnon-block: do not wait for the operation to return\n"
549 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
551 "To move directories between MDTs. This command is deprecated, "
552 "use \"migrate\" instead.\n"
553 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
555 {"ladvise", lfs_ladvise, 0,
556 "Provide servers with advice about access patterns for a file.\n"
557 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
558 " [--background|-b] [--unset|-u]\n\n"
559 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
560 " {[--mode|-m [READ,WRITE]}\n"
562 {"mirror", lfs_mirror, mirror_cmdlist,
563 "lfs commands used to manage files with mirrored components:\n"
564 "lfs mirror create - create a mirrored file or directory\n"
565 "lfs mirror extend - add mirror(s) to an existing file\n"
566 "lfs mirror split - split a mirror from an existing mirrored file\n"
567 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
568 "lfs mirror verify - verify mirrored file(s)\n"},
569 {"help", Parser_help, 0, "help"},
570 {"exit", Parser_quit, 0, "quit"},
571 {"quit", Parser_quit, 0, "quit"},
572 {"--version", Parser_version, 0,
573 "output build version of the utility and exit"},
574 {"--list-commands", lfs_list_commands, 0,
575 "list commands supported by the utility and exit"},
580 static int check_hashtype(const char *hashtype)
584 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
585 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
592 static const char *error_loc = "syserror";
595 MIGRATION_NONBLOCK = 1 << 0,
596 MIGRATION_MIRROR = 1 << 1,
597 MIGRATION_NONDIRECT = 1 << 2,
600 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
601 struct llapi_layout *layout);
604 migrate_open_files(const char *name, __u64 migration_flags,
605 const struct llapi_stripe_param *param,
606 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
613 char parent[PATH_MAX];
614 char volatile_file[PATH_MAX];
620 if (param == NULL && layout == NULL) {
621 error_loc = "layout information";
625 /* search for file directory pathname */
626 if (strlen(name) > sizeof(parent) - 1) {
627 error_loc = "source file name";
631 strncpy(parent, name, sizeof(parent));
632 ptr = strrchr(parent, '/');
634 if (getcwd(parent, sizeof(parent)) == NULL) {
635 error_loc = "getcwd";
639 if (ptr == parent) /* leading '/' */
644 /* open file, direct io */
645 /* even if the file is only read, WR mode is nedeed to allow
646 * layout swap on fd */
648 if (!(migration_flags & MIGRATION_NONDIRECT))
650 fd = open(name, rflags);
653 error_loc = "cannot open source file";
657 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
659 error_loc = "cannot get MDT index";
664 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
665 mode_t open_mode = S_IRUSR | S_IWUSR;
667 random_value = random();
668 rc = snprintf(volatile_file, sizeof(volatile_file),
669 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
670 mdt_index, random_value);
671 if (rc >= sizeof(volatile_file)) {
676 /* create, open a volatile file, use caching (ie no directio) */
678 fdv = llapi_file_open_param(volatile_file, open_flags,
681 fdv = lfs_component_create(volatile_file, open_flags,
683 } while (fdv < 0 && (rc = fdv) == -EEXIST);
686 error_loc = "cannot create volatile file";
690 /* In case the MDT does not support creation of volatile files
691 * we should try to unlink it. */
692 (void)unlink(volatile_file);
694 /* Not-owner (root?) special case.
695 * Need to set owner/group of volatile file like original.
696 * This will allow to pass related check during layout_swap.
701 error_loc = "cannot stat source file";
705 rc = fstat(fdv, &stv);
708 error_loc = "cannot stat volatile";
712 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
713 rc = fchown(fdv, st.st_uid, st.st_gid);
716 error_loc = "cannot change ownwership of volatile";
735 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
737 struct llapi_layout *layout;
738 size_t buf_size = 4 * 1024 * 1024;
747 layout = llapi_layout_get_by_fd(fd_src, 0);
748 if (layout != NULL) {
749 uint64_t stripe_size;
751 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
753 buf_size = stripe_size;
755 llapi_layout_free(layout);
758 /* Use a page-aligned buffer for direct I/O */
759 rc = posix_memalign(&buf, getpagesize(), buf_size);
764 /* read new data only if we have written all
765 * previously read data */
768 rc = check_file(fd_src);
773 rsize = read(fd_src, buf, buf_size);
785 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
804 static int migrate_copy_timestamps(int fd, int fdv)
808 if (fstat(fd, &st) == 0) {
809 struct timeval tv[2] = {
810 {.tv_sec = st.st_atime},
811 {.tv_sec = st.st_mtime}
814 return futimes(fdv, tv);
820 static int migrate_block(int fd, int fdv)
827 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
829 error_loc = "cannot get dataversion";
837 /* The grouplock blocks all concurrent accesses to the file.
838 * It has to be taken after llapi_get_data_version as it would
840 rc = llapi_group_lock(fd, gid);
842 error_loc = "cannot get group lock";
846 rc = migrate_copy_data(fd, fdv, NULL);
848 error_loc = "data copy failed";
852 /* Make sure we keep original atime/mtime values */
853 rc = migrate_copy_timestamps(fd, fdv);
855 error_loc = "timestamp copy failed";
860 * for a migration we need to check data version on file did
863 * Pass in gid=0 since we already own grouplock. */
864 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
865 SWAP_LAYOUTS_CHECK_DV1);
867 error_loc = "file changed";
870 error_loc = "cannot swap layout";
875 rc2 = llapi_group_unlock(fd, gid);
876 if (rc2 < 0 && rc == 0) {
877 error_loc = "unlock group lock";
885 * Internal helper for migrate_copy_data(). Check lease and report error if
888 * \param[in] fd File descriptor on which to check the lease.
890 * \retval 0 Migration can keep on going.
891 * \retval -errno Error occurred, abort migration.
893 static int check_lease(int fd)
897 rc = llapi_lease_check(fd);
899 return 0; /* llapi_check_lease returns > 0 on success. */
904 static int migrate_nonblock(int fd, int fdv)
910 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
912 error_loc = "cannot get data version";
916 rc = migrate_copy_data(fd, fdv, check_lease);
918 error_loc = "data copy failed";
922 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
924 error_loc = "cannot get data version";
930 error_loc = "source file changed";
934 /* Make sure we keep original atime/mtime values */
935 rc = migrate_copy_timestamps(fd, fdv);
937 error_loc = "timestamp copy failed";
944 static int lfs_component_set(char *fname, int comp_id,
945 __u32 flags, __u32 neg_flags)
948 __u32 flags_array[2];
953 ids[count] = comp_id;
954 flags_array[count] = flags;
959 ids[count] = comp_id;
960 flags_array[count] = neg_flags | LCME_FL_NEG;
964 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
967 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
968 progname, comp_id, fname, flags, neg_flags);
973 static int lfs_component_del(char *fname, __u32 comp_id,
974 __u32 flags, __u32 neg_flags)
978 if (flags && neg_flags)
981 if (!flags && neg_flags)
982 flags = neg_flags | LCME_FL_NEG;
984 if ((flags && comp_id) || (!flags && !comp_id))
987 /* LCME_FL_INIT is the only supported flag in PFL */
989 if (flags & ~LCME_KNOWN_FLAGS) {
991 "%s setstripe: unknown flags %#x\n",
995 } else if (comp_id > LCME_ID_MAX) {
996 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1001 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1004 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1005 progname, comp_id, fname, strerror(errno));
1009 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1016 rc = llapi_layout_file_comp_add(fname, layout);
1018 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1019 fname, strerror(errno));
1023 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1024 struct llapi_layout *layout)
1032 fd = lstat(fname, &st);
1033 if (fd == 0 && S_ISDIR(st.st_mode))
1034 open_flags = O_DIRECTORY | O_RDONLY;
1036 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1038 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1039 S_ISDIR(st.st_mode) ?
1040 "set default composite layout for" :
1041 "create composite file",
1042 fname, strerror(errno));
1046 static int lfs_migrate(char *name, __u64 migration_flags,
1047 struct llapi_stripe_param *param,
1048 struct llapi_layout *layout)
1054 rc = migrate_open_files(name, migration_flags, param, layout,
1059 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1060 /* Blocking mode (forced if servers do not support file lease).
1061 * It is also the default mode, since we cannot distinguish
1062 * between a broken lease and a server that does not support
1063 * atomic swap/close (LU-6785) */
1064 rc = migrate_block(fd, fdv);
1068 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1070 error_loc = "cannot get lease";
1074 rc = migrate_nonblock(fd, fdv);
1076 llapi_lease_release(fd);
1080 /* Atomically put lease, swap layouts and close.
1081 * for a migration we need to check data version on file did
1083 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1085 error_loc = "cannot swap layout";
1097 fprintf(stderr, "error: %s: %s: %s: %s\n",
1098 progname, name, error_loc, strerror(-rc));
1102 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1111 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1115 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1116 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1117 const char *comp_name = comp_flags_table[i].cfn_name;
1119 if (strcmp(name, comp_name) == 0) {
1120 *flags |= comp_flag;
1122 } else if (strncmp(name, "^", 1) == 0 &&
1123 strcmp(name + 1, comp_name) == 0) {
1124 *neg_flags |= comp_flag;
1129 llapi_printf(LLAPI_MSG_ERROR,
1130 "%s: component flag '%s' not supported\n",
1136 if (!*flags && !*neg_flags)
1139 /* don't allow to set and exclude the same flag */
1140 if (*flags & *neg_flags)
1146 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1154 if (strncmp(string, "^", 1) == 0) {
1155 *neg_state = llapi_layout_string_flags(string + 1);
1156 if (*neg_state != 0)
1159 *state = llapi_layout_string_flags(string);
1164 llapi_printf(LLAPI_MSG_ERROR,
1165 "%s: mirrored file state '%s' not supported\n",
1171 * struct mirror_args - Command-line arguments for mirror(s).
1172 * @m_count: Number of mirrors to be created with this layout.
1173 * @m_flags: Mirror level flags, only 'prefer' is supported.
1174 * @m_layout: Mirror layout.
1175 * @m_file: A victim file. Its layout will be split and used as a mirror.
1176 * @m_next: Point to the next node of the list.
1178 * Command-line arguments for mirror(s) will be parsed and stored in
1179 * a linked list that consists of this structure.
1181 struct mirror_args {
1184 struct llapi_layout *m_layout;
1186 struct mirror_args *m_next;
1189 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1194 rc = llapi_layout_comp_flags_get(layout, &flags);
1198 if (flags & LCME_FL_NEG) {
1199 fprintf(stderr, "error: %s: negative flags are not supported\n",
1204 if (flags & LCME_FL_STALE) {
1205 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1206 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1210 return LLAPI_LAYOUT_ITER_CONT;
1213 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1215 uint64_t start, end;
1219 /* LU-10112: do not support dom+flr in phase 1 */
1220 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1224 rc = llapi_layout_pattern_get(layout, &pattern);
1228 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1229 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1234 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1238 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1242 if (end != LUSTRE_EOF) {
1243 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1248 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1253 * enum mirror_flags - Flags for extending a mirrored file.
1254 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1255 * in case the victim file(s) contains the same data as the
1256 * original mirrored file.
1257 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1259 * Flags for extending a mirrored file.
1267 * mirror_create_sanity_check() - Check mirror list.
1268 * @list: A linked list that stores the mirror arguments.
1270 * This function does a sanity check on @list for creating
1273 * Return: 0 on success or a negative error code on failure.
1275 static int mirror_create_sanity_check(const char *fname,
1276 struct mirror_args *list)
1279 bool has_m_file = false;
1280 bool has_m_layout = false;
1286 struct llapi_layout *layout;
1288 layout = llapi_layout_get_by_path(fname, 0);
1291 "error: %s: file '%s' couldn't get layout\n",
1296 rc = mirror_sanity_check_one(layout);
1297 llapi_layout_free(layout);
1303 while (list != NULL) {
1304 if (list->m_file != NULL) {
1306 llapi_layout_free(list->m_layout);
1309 llapi_layout_get_by_path(list->m_file, 0);
1310 if (list->m_layout == NULL) {
1312 "error: %s: file '%s' has no layout\n",
1313 progname, list->m_file);
1317 has_m_layout = true;
1318 if (list->m_layout == NULL) {
1319 fprintf(stderr, "error: %s: no mirror layout\n",
1325 rc = mirror_sanity_check_one(list->m_layout);
1329 list = list->m_next;
1332 if (has_m_file && has_m_layout) {
1334 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1342 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1344 __u32 mirror_flags = *(__u32 *)cbdata;
1348 rc = llapi_layout_comp_flags_get(layout, &flags);
1353 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1358 return LLAPI_LAYOUT_ITER_CONT;
1362 * mirror_create() - Create a mirrored file.
1363 * @fname: The file to be created.
1364 * @mirror_list: A linked list that stores the mirror arguments.
1366 * This function creates a mirrored file @fname with the mirror(s)
1367 * from @mirror_list.
1369 * Return: 0 on success or a negative error code on failure.
1371 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1373 struct llapi_layout *layout = NULL;
1374 struct mirror_args *cur_mirror = NULL;
1375 uint16_t mirror_count = 0;
1379 rc = mirror_create_sanity_check(NULL, mirror_list);
1383 cur_mirror = mirror_list;
1384 while (cur_mirror != NULL) {
1385 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1387 &cur_mirror->m_flags);
1390 fprintf(stderr, "%s: failed to set mirror flags\n",
1395 for (i = 0; i < cur_mirror->m_count; i++) {
1396 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1399 fprintf(stderr, "error: %s: "
1400 "merge layout failed: %s\n",
1401 progname, strerror(errno));
1405 mirror_count += cur_mirror->m_count;
1406 cur_mirror = cur_mirror->m_next;
1409 if (layout == NULL) {
1410 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1414 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1417 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1418 progname, strerror(errno));
1422 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1430 llapi_layout_free(layout);
1435 * Compare files and check lease on @fd.
1437 * \retval bytes number of bytes are the same
1439 static ssize_t mirror_file_compare(int fd, int fdv)
1441 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1443 ssize_t bytes_done = 0;
1444 ssize_t bytes_read = 0;
1446 buf = malloc(buflen * 2);
1451 if (!llapi_lease_check(fd)) {
1452 bytes_done = -EBUSY;
1456 bytes_read = read(fd, buf, buflen);
1457 if (bytes_read <= 0)
1460 if (bytes_read != read(fdv, buf + buflen, buflen))
1463 /* XXX: should compute the checksum on each buffer and then
1464 * compare checksum to avoid cache collision */
1465 if (memcmp(buf, buf + buflen, bytes_read))
1468 bytes_done += bytes_read;
1476 static int mirror_extend_file(const char *fname, const char *victim_file,
1477 enum mirror_flags mirror_flags)
1482 struct stat stbuf_v;
1483 struct ll_ioc_lease *data = NULL;
1486 fd = open(fname, O_RDWR);
1488 error_loc = "open source file";
1493 fdv = open(victim_file, O_RDWR);
1495 error_loc = "open target file";
1500 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1501 error_loc = "stat source or target file";
1506 if (stbuf.st_dev != stbuf_v.st_dev) {
1507 error_loc = "stat source and target file";
1512 /* mirrors should be of the same size */
1513 if (stbuf.st_size != stbuf_v.st_size) {
1514 error_loc = "file sizes don't match";
1519 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1521 error_loc = "cannot get lease";
1525 if (!(mirror_flags & MF_NO_VERIFY)) {
1527 /* mirrors should have the same contents */
1528 ret = mirror_file_compare(fd, fdv);
1529 if (ret != stbuf.st_size) {
1530 error_loc = "file busy or contents don't match";
1531 rc = ret < 0 ? ret : -EINVAL;
1536 /* Get rid of caching pages from clients */
1537 rc = llapi_file_flush(fd);
1539 error_loc = "cannot get data version";
1543 rc = llapi_file_flush(fdv);
1545 error_loc = "cannot get data version";
1550 /* Make sure we keep original atime/mtime values */
1551 rc = migrate_copy_timestamps(fd, fdv);
1553 error_loc = "cannot copy timestamp";
1557 /* Atomically put lease, merge layouts and close. */
1558 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1560 error_loc = "memory allocation";
1563 data->lil_mode = LL_LEASE_UNLCK;
1564 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1565 data->lil_count = 1;
1566 data->lil_ids[0] = fdv;
1567 rc = llapi_lease_set(fd, data);
1569 error_loc = "cannot merge layout";
1571 } else if (rc == 0) {
1573 error_loc = "lost lease lock";
1586 (void) unlink(victim_file);
1588 fprintf(stderr, "error: %s: %s: %s: %s\n",
1589 progname, fname, error_loc, strerror(-rc));
1593 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1595 struct ll_ioc_lease *data = NULL;
1600 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1604 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1606 error_loc = "cannot get lease";
1610 rc = migrate_nonblock(fd, fdv);
1612 llapi_lease_release(fd);
1616 /* Atomically put lease, merge layouts and close. */
1617 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1619 error_loc = "memory allocation";
1622 data->lil_mode = LL_LEASE_UNLCK;
1623 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1624 data->lil_count = 1;
1625 data->lil_ids[0] = fdv;
1626 rc = llapi_lease_set(fd, data);
1628 error_loc = "cannot merge layout";
1630 } else if (rc == 0) {
1632 error_loc = "lost lease lock";
1645 fprintf(stderr, "error: %s: %s: %s: %s\n",
1646 progname, name, error_loc, strerror(-rc));
1650 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1651 enum mirror_flags mirror_flags)
1655 rc = mirror_create_sanity_check(fname, mirror_list);
1659 while (mirror_list) {
1660 if (mirror_list->m_file != NULL) {
1661 rc = mirror_extend_file(fname, mirror_list->m_file,
1664 __u32 mirror_count = mirror_list->m_count;
1666 while (mirror_count > 0) {
1667 rc = mirror_extend_layout(fname,
1668 mirror_list->m_layout);
1678 mirror_list = mirror_list->m_next;
1684 static int verify_id(struct llapi_layout *layout, void *cbdata)
1689 rc = llapi_layout_mirror_id_get(layout, &id);
1693 if ((__u16)id == *(__u16 *)cbdata)
1694 return LLAPI_LAYOUT_ITER_STOP;
1696 return LLAPI_LAYOUT_ITER_CONT;
1699 static int mirror_split(const char *fname, __u16 mirror_id,
1700 enum mirror_flags mflags, const char *victim_file)
1702 struct llapi_layout *layout;
1703 char parent[PATH_MAX];
1704 char victim[PATH_MAX];
1705 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1707 struct ll_ioc_lease *data;
1708 uint16_t mirror_count;
1713 /* check fname contains mirror with mirror_id */
1714 layout = llapi_layout_get_by_path(fname, 0);
1717 "error %s: file '%s' couldn't get layout\n",
1722 rc = mirror_sanity_check_one(layout);
1726 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1729 "error %s: file '%s' couldn't get mirror count\n",
1733 if (mirror_count < 2) {
1735 "error %s: file '%s' has %d component, cannot split\n",
1736 progname, fname, mirror_count);
1740 rc = llapi_layout_comp_iterate(layout, verify_id, &mirror_id);
1742 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1745 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1747 "error %s: file '%s' does not contain mirror with id %u\n",
1748 progname, fname, mirror_id);
1752 fd = open(fname, O_RDWR);
1755 "error %s: open file '%s' failed: %s\n",
1756 progname, fname, strerror(errno));
1760 /* get victim file directory pathname */
1761 if (strlen(fname) > sizeof(parent) - 1) {
1762 fprintf(stderr, "error %s: file name of '%s' too long\n",
1767 strncpy(parent, fname, sizeof(parent));
1768 ptr = strrchr(parent, '/');
1770 if (getcwd(parent, sizeof(parent)) == NULL) {
1771 fprintf(stderr, "error %s: getcwd failed: %s\n",
1772 progname, strerror(errno));
1782 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1784 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1789 if (victim_file == NULL) {
1790 /* use a temp file to store the splitted layout */
1791 if (mflags & MF_DESTROY) {
1792 fdv = llapi_create_volatile_idx(parent, mdt_index,
1793 O_LOV_DELAY_CREATE);
1795 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1797 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1800 /* user specified victim file */
1801 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1806 "error %s: create victim file failed: %s\n",
1807 progname, strerror(errno));
1811 /* get lease lock of fname */
1812 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1815 "error %s: cannot get lease of file '%s': %d\n",
1816 progname, fname, rc);
1820 /* Atomatically put lease, split layouts and close. */
1821 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1827 data->lil_mode = LL_LEASE_UNLCK;
1828 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1829 data->lil_count = 2;
1830 data->lil_ids[0] = fdv;
1831 data->lil_ids[1] = mirror_id;
1832 rc = llapi_lease_set(fd, data);
1834 if (rc == 0) /* lost lease lock */
1837 "error %s: cannot split '%s': %s\n",
1838 progname, fname, strerror(-rc));
1849 llapi_layout_free(layout);
1854 * Parse a string containing an target index list into an array of integers.
1856 * The input string contains a comma delimited list of individual
1857 * indices and ranges, for example "1,2-4,7". Add the indices into the
1858 * \a tgts array and remove duplicates.
1860 * \param[out] tgts array to store indices in
1861 * \param[in] size size of \a tgts array
1862 * \param[in] offset starting index in \a tgts
1863 * \param[in] arg string containing OST index list
1865 * \retval positive number of indices in \a tgts
1866 * \retval -EINVAL unable to parse \a arg
1868 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1872 int slots = size - offset;
1879 end_of_loop = false;
1880 while (!end_of_loop) {
1884 char *endptr = NULL;
1888 ptr = strchrnul(arg, ',');
1890 end_of_loop = *ptr == '\0';
1893 start_index = strtol(arg, &endptr, 0);
1894 if (endptr == arg) /* no data at all */
1896 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1899 end_index = start_index;
1900 if (*endptr == '-') {
1901 end_index = strtol(endptr + 1, &endptr, 0);
1902 if (*endptr != '\0')
1904 if (end_index < start_index)
1908 for (i = start_index; i <= end_index && slots > 0; i++) {
1911 /* remove duplicate */
1912 for (j = 0; j < offset; j++) {
1916 if (j == offset) { /* no duplicate */
1921 if (slots == 0 && i < end_index)
1929 if (!end_of_loop && ptr != NULL)
1932 return rc < 0 ? rc : nr;
1935 struct lfs_setstripe_args {
1936 unsigned long long lsa_comp_end;
1937 unsigned long long lsa_stripe_size;
1938 long long lsa_stripe_count;
1939 long long lsa_stripe_off;
1940 __u32 lsa_comp_flags;
1941 __u32 lsa_comp_neg_flags;
1942 unsigned long long lsa_pattern;
1943 unsigned int lsa_mirror_count;
1945 bool lsa_first_comp;
1947 char *lsa_pool_name;
1950 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1952 unsigned int mirror_count = lsa->lsa_mirror_count;
1953 bool first_comp = lsa->lsa_first_comp;
1955 memset(lsa, 0, sizeof(*lsa));
1957 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1958 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1959 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1960 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1961 lsa->lsa_pool_name = NULL;
1963 lsa->lsa_mirror_count = mirror_count;
1964 lsa->lsa_first_comp = first_comp;
1968 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1969 * @lsa: Stripe options to be initialized and inherited.
1971 * This function initializes stripe options in @lsa and inherit
1972 * stripe_size, stripe_count and OST pool_name options.
1976 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1978 unsigned long long stripe_size;
1979 long long stripe_count;
1980 char *pool_name = NULL;
1982 stripe_size = lsa->lsa_stripe_size;
1983 stripe_count = lsa->lsa_stripe_count;
1984 pool_name = lsa->lsa_pool_name;
1986 setstripe_args_init(lsa);
1988 lsa->lsa_stripe_size = stripe_size;
1989 lsa->lsa_stripe_count = stripe_count;
1990 lsa->lsa_pool_name = pool_name;
1993 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1995 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1996 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1997 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1998 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1999 lsa->lsa_pool_name != NULL ||
2000 lsa->lsa_comp_end != 0);
2004 * comp_args_to_layout() - Create or extend a composite layout.
2005 * @composite: Pointer to the composite layout.
2006 * @lsa: Stripe options for the new component.
2008 * This function creates or extends a composite layout by adding a new
2009 * component with stripe options from @lsa.
2011 * Return: 0 on success or an error code on failure.
2013 static int comp_args_to_layout(struct llapi_layout **composite,
2014 struct lfs_setstripe_args *lsa,
2017 struct llapi_layout *layout = *composite;
2018 uint64_t prev_end = 0;
2021 if (layout == NULL) {
2022 layout = llapi_layout_alloc();
2023 if (layout == NULL) {
2024 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2028 *composite = layout;
2032 /* Get current component extent, current component
2033 * must be the tail component. */
2034 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2036 fprintf(stderr, "Get comp extent failed. %s\n",
2041 if (lsa->lsa_first_comp)
2044 if (lsa->lsa_first_comp)
2045 rc = llapi_layout_add_first_comp(layout);
2047 rc = llapi_layout_comp_add(layout);
2049 fprintf(stderr, "Add component failed. %s\n",
2054 /* reset lsa_first_comp */
2055 lsa->lsa_first_comp = false;
2058 rc = llapi_layout_comp_extent_set(layout, prev_end,
2061 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2062 prev_end, lsa->lsa_comp_end, strerror(errno));
2067 /* Data-on-MDT component setting */
2068 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2069 /* In case of Data-on-MDT patterns the only extra option
2070 * applicable is stripe size option. */
2071 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2072 fprintf(stderr, "Option 'stripe-count' can't be "
2073 "specified with Data-on-MDT component: %lld\n",
2074 lsa->lsa_stripe_count);
2077 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2078 fprintf(stderr, "Option 'stripe-size' can't be "
2079 "specified with Data-on-MDT component: %llu\n",
2080 lsa->lsa_stripe_size);
2083 if (lsa->lsa_nr_tgts != 0) {
2084 fprintf(stderr, "Option 'ost-list' can't be specified "
2085 "with Data-on-MDT component: '%i'\n",
2089 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2090 fprintf(stderr, "Option 'stripe-offset' can't be "
2091 "specified with Data-on-MDT component: %lld\n",
2092 lsa->lsa_stripe_off);
2095 if (lsa->lsa_pool_name != 0) {
2096 fprintf(stderr, "Option 'pool' can't be specified "
2097 "with Data-on-MDT component: '%s'\n",
2098 lsa->lsa_pool_name);
2102 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2104 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2105 lsa->lsa_pattern, strerror(errno));
2108 /* Data-on-MDT component has always single stripe up to end */
2109 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2112 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2114 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2115 lsa->lsa_stripe_size, strerror(errno));
2119 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2121 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2122 lsa->lsa_stripe_count, strerror(errno));
2126 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2128 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2129 lsa->lsa_comp_flags, strerror(errno));
2133 if (lsa->lsa_pool_name != NULL) {
2134 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2136 fprintf(stderr, "Set pool name: %s failed. %s\n",
2137 lsa->lsa_pool_name, strerror(errno));
2141 rc = llapi_layout_pool_name_set(layout, "");
2143 fprintf(stderr, "Clear pool name failed: %s\n",
2149 if (lsa->lsa_nr_tgts > 0) {
2150 if (lsa->lsa_stripe_count > 0 &&
2151 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2152 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2153 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2154 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2155 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2158 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2159 rc = llapi_layout_ost_index_set(layout, i,
2164 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2165 lsa->lsa_stripe_off != -1) {
2166 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2169 fprintf(stderr, "Set ost index %d failed. %s\n",
2170 i, strerror(errno));
2177 static int build_component(struct llapi_layout **layout,
2178 struct lfs_setstripe_args *lsa, bool set_extent)
2182 rc = comp_args_to_layout(layout, lsa, set_extent);
2186 if (lsa->lsa_mirror_count > 0) {
2187 rc = llapi_layout_mirror_count_set(*layout,
2188 lsa->lsa_mirror_count);
2192 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2195 lsa->lsa_mirror_count = 0;
2201 static int build_layout_from_yaml_node(struct cYAML *node,
2202 struct llapi_layout **layout,
2203 struct lfs_setstripe_args *lsa,
2210 string = node->cy_string;
2211 /* skip leading lmm_ if present, to simplify parsing */
2212 if (string != NULL && strncmp(string, "lmm_", 4) == 0)
2215 if (node->cy_type == CYAML_TYPE_STRING) {
2216 if (!strcmp(string, "lcme_extent.e_end")) {
2217 if (!strcmp(node->cy_valuestring, "EOF") ||
2218 !strcmp(node->cy_valuestring, "eof"))
2219 lsa->lsa_comp_end = LUSTRE_EOF;
2220 } else if (!strcmp(string, "pool")) {
2221 lsa->lsa_pool_name = node->cy_valuestring;
2222 } else if (!strcmp(string, "pattern")) {
2223 if (!strcmp(node->cy_valuestring, "mdt"))
2224 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2226 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2227 if (!strcmp(string, "lcm_mirror_count")) {
2228 lsa->lsa_mirror_count = node->cy_valueint;
2229 } else if (!strcmp(string, "lcme_extent.e_start")) {
2230 if (node->cy_valueint != 0 || *layout != NULL) {
2231 rc = build_component(layout, lsa, true);
2236 if (node->cy_valueint == 0)
2237 lsa->lsa_first_comp = true;
2239 /* initialize lsa */
2240 setstripe_args_init(lsa);
2241 lsa->lsa_tgts = osts;
2242 } else if (!strcmp(string, "lcme_extent.e_end")) {
2243 if (node->cy_valueint == -1)
2244 lsa->lsa_comp_end = LUSTRE_EOF;
2246 lsa->lsa_comp_end = node->cy_valueint;
2247 } else if (!strcmp(string, "stripe_count")) {
2248 lsa->lsa_stripe_count = node->cy_valueint;
2249 } else if (!strcmp(string, "stripe_size")) {
2250 lsa->lsa_stripe_size = node->cy_valueint;
2251 } else if (!strcmp(string, "stripe_offset")) {
2252 lsa->lsa_stripe_off = node->cy_valueint;
2253 } else if (!strcmp(string, "l_ost_idx")) {
2254 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2257 } else if (node->cy_type == CYAML_TYPE_OBJECT) {
2258 /* go deep to sub blocks */
2259 rc = build_layout_from_yaml_node(node->cy_child, layout,
2264 node = node->cy_next;
2270 static int lfs_comp_create_from_yaml(char *template,
2271 struct llapi_layout **layout,
2272 struct lfs_setstripe_args *lsa,
2275 struct cYAML *tree = NULL, *err_rc = NULL;
2278 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2280 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2281 progname, template);
2282 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2283 "can't parse", &err_rc);
2284 cYAML_print_tree2file(stderr, err_rc);
2285 cYAML_free_tree(err_rc);
2290 /* initialize lsa for plain file */
2291 setstripe_args_init(lsa);
2292 lsa->lsa_tgts = osts;
2294 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2296 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2297 progname, template);
2300 rc = build_component(layout, lsa, *layout != NULL);
2302 /* clean clean lsa */
2303 setstripe_args_init(lsa);
2307 cYAML_free_tree(tree);
2311 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2312 * end of the last component in the existing file, and adjust the
2313 * first extent start of the components to be added accordingly. */
2314 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2316 struct llapi_layout *head;
2317 uint64_t start, end, stripe_size, prev_end = 0;
2320 if (layout == NULL) {
2322 "%s setstripe: layout must be specified\n",
2328 head = llapi_layout_get_by_path(fname, 0);
2331 "%s setstripe: cannot read layout from '%s': %s\n",
2332 progname, fname, strerror(errno));
2334 } else if (errno == ENODATA) {
2335 /* file without LOVEA, this component-add will be turned
2336 * into a component-create. */
2337 llapi_layout_free(head);
2339 } else if (!llapi_layout_is_composite(head)) {
2340 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2342 llapi_layout_free(head);
2346 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2348 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2349 progname, strerror(errno));
2350 llapi_layout_free(head);
2354 llapi_layout_free(head);
2356 /* Make sure we use the first component of the layout to be added. */
2357 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2360 "%s setstripe: cannot move component cursor: %s\n",
2361 progname, strerror(errno));
2365 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2367 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2368 progname, strerror(errno));
2372 if (start > prev_end || end <= prev_end) {
2374 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2375 progname, start, end, prev_end);
2379 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2381 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2382 progname, strerror(errno));
2386 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2387 (prev_end & (stripe_size - 1))) {
2389 "%s setstripe: stripe size %lu not aligned with %lu\n",
2390 progname, stripe_size, prev_end);
2394 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2397 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2398 progname, prev_end, end, strerror(errno));
2405 static inline bool arg_is_eof(char *arg)
2407 return !strncmp(arg, "-1", strlen("-1")) ||
2408 !strncmp(arg, "EOF", strlen("EOF")) ||
2409 !strncmp(arg, "eof", strlen("eof"));
2413 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2415 * Return: Valid mirror_args pointer on success and
2416 * NULL if memory allocation fails.
2418 static struct mirror_args *lfs_mirror_alloc(void)
2420 struct mirror_args *mirror = NULL;
2423 mirror = calloc(1, sizeof(*mirror));
2434 * lfs_mirror_free() - Free memory allocated for a mirror argument
2436 * @mirror: Previously allocated mirror argument structure by
2437 * lfs_mirror_alloc().
2439 * Free memory allocated for @mirror.
2443 static void lfs_mirror_free(struct mirror_args *mirror)
2445 if (mirror->m_layout != NULL)
2446 llapi_layout_free(mirror->m_layout);
2451 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2452 * @mirror_list: Previously allocated mirror list.
2454 * Free memory allocated for @mirror_list.
2458 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2460 struct mirror_args *next_mirror = NULL;
2462 while (mirror_list != NULL) {
2463 next_mirror = mirror_list->m_next;
2464 lfs_mirror_free(mirror_list);
2465 mirror_list = next_mirror;
2477 LFS_COMP_NO_VERIFY_OPT,
2479 LFS_MIRROR_FLAGS_OPT,
2481 LFS_MIRROR_STATE_OPT,
2486 static int lfs_setstripe_internal(int argc, char **argv,
2487 enum setstripe_origin opc)
2489 struct lfs_setstripe_args lsa = { 0 };
2490 struct llapi_stripe_param *param = NULL;
2491 struct find_param migrate_mdt_param = {
2501 char *mdt_idx_arg = NULL;
2502 unsigned long long size_units = 1;
2503 bool migrate_mode = false;
2504 bool migration_block = false;
2505 __u64 migration_flags = 0;
2506 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2507 int comp_del = 0, comp_set = 0;
2510 struct llapi_layout *layout = NULL;
2511 struct llapi_layout **lpp = &layout;
2512 bool mirror_mode = false;
2513 bool has_m_file = false;
2514 __u32 mirror_count = 0;
2515 enum mirror_flags mirror_flags = 0;
2516 struct mirror_args *mirror_list = NULL;
2517 struct mirror_args *new_mirror = NULL;
2518 struct mirror_args *last_mirror = NULL;
2519 __u16 mirror_id = 0;
2521 bool from_yaml = false;
2522 bool from_copy = false;
2523 char *template = NULL;
2525 struct option long_opts[] = {
2526 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2527 /* --block is only valid in migrate mode */
2528 { .val = 'b', .name = "block", .has_arg = no_argument },
2529 { .val = LFS_COMP_ADD_OPT,
2530 .name = "comp-add", .has_arg = no_argument },
2531 { .val = LFS_COMP_ADD_OPT,
2532 .name = "component-add", .has_arg = no_argument },
2533 { .val = LFS_COMP_DEL_OPT,
2534 .name = "comp-del", .has_arg = no_argument },
2535 { .val = LFS_COMP_DEL_OPT,
2536 .name = "component-del", .has_arg = no_argument },
2537 { .val = LFS_COMP_FLAGS_OPT,
2538 .name = "comp-flags", .has_arg = required_argument },
2539 { .val = LFS_COMP_FLAGS_OPT,
2540 .name = "component-flags",
2541 .has_arg = required_argument },
2542 { .val = LFS_COMP_SET_OPT,
2543 .name = "comp-set", .has_arg = no_argument },
2544 { .val = LFS_COMP_SET_OPT,
2545 .name = "component-set",
2546 .has_arg = no_argument},
2547 { .val = LFS_COMP_NO_VERIFY_OPT,
2548 .name = "no-verify", .has_arg = no_argument},
2549 { .val = LFS_MIRROR_FLAGS_OPT,
2550 .name = "flags", .has_arg = required_argument},
2551 { .val = LFS_MIRROR_ID_OPT,
2552 .name = "mirror-id", .has_arg = required_argument},
2553 { .val = LFS_LAYOUT_COPY,
2554 .name = "copy", .has_arg = required_argument},
2555 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2556 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2557 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2558 { .val = 'd', .name = "delete", .has_arg = no_argument},
2559 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2560 /* --non-direct is only valid in migrate mode */
2561 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2562 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2563 { .val = 'E', .name = "component-end",
2564 .has_arg = required_argument},
2565 { .val = 'f', .name = "file", .has_arg = required_argument },
2566 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2567 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2568 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2569 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
2570 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2571 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2572 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2573 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2574 { .val = 'L', .name = "layout", .has_arg = required_argument },
2575 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2576 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2577 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2578 /* --non-block is only valid in migrate mode */
2579 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2580 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2581 { .val = 'o', .name = "ost", .has_arg = required_argument },
2582 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2583 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2584 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2586 { .val = 'p', .name = "pool", .has_arg = required_argument },
2587 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2588 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2589 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2590 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2591 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2592 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2593 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2594 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2595 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2596 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2597 /* --verbose is only valid in migrate mode */
2598 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2599 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2602 setstripe_args_init(&lsa);
2604 migrate_mode = (opc == SO_MIGRATE);
2605 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2607 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2609 while ((c = getopt_long(argc, argv, "bc:dDE:f:i:I:m:N::no:p:L:s:S:vy:",
2610 long_opts, NULL)) >= 0) {
2615 case LFS_COMP_ADD_OPT:
2618 case LFS_COMP_DEL_OPT:
2621 case LFS_COMP_FLAGS_OPT:
2622 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2623 &lsa.lsa_comp_neg_flags);
2626 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2627 fprintf(stderr, "%s: inverted flags are not supported\n",
2631 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2633 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2640 case LFS_COMP_SET_OPT:
2643 case LFS_COMP_NO_VERIFY_OPT:
2644 mirror_flags |= MF_NO_VERIFY;
2646 case LFS_MIRROR_ID_OPT:
2647 mirror_id = strtoul(optarg, &end, 0);
2648 if (*end != '\0' || mirror_id == 0) {
2650 "%s %s: invalid mirror ID '%s'\n",
2651 progname, argv[0], optarg);
2655 case LFS_MIRROR_FLAGS_OPT: {
2658 if (!mirror_mode || !last_mirror) {
2659 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2664 result = comp_str2flags(optarg, &last_mirror->m_flags,
2670 fprintf(stderr, "%s: inverted flags are not supported\n",
2675 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2677 "%s: unsupported mirror flags: %s\n",
2684 case LFS_LAYOUT_COPY:
2689 if (!migrate_mode) {
2691 "%s %s: -b|--block valid only for migrate command\n",
2695 migration_block = true;
2698 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2701 "%s %s: invalid stripe count '%s'\n",
2702 progname, argv[0], optarg);
2706 if (lsa.lsa_stripe_count == -1)
2707 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2710 /* delete the default striping pattern */
2712 if (opc == SO_MIRROR_SPLIT) {
2715 "%s %s: -d cannot used with -f\n",
2719 mirror_flags |= MF_DESTROY;
2723 if (!migrate_mode) {
2725 "%s %s: -D|--non-direct is valid "
2726 "only for migrate command\n",
2730 migration_flags |= MIGRATION_NONDIRECT;
2733 if (lsa.lsa_comp_end != 0) {
2734 result = comp_args_to_layout(lpp, &lsa, true);
2737 "%s %s: invalid layout\n",
2742 setstripe_args_init_inherit(&lsa);
2745 if (arg_is_eof(optarg)) {
2746 lsa.lsa_comp_end = LUSTRE_EOF;
2748 result = llapi_parse_size(optarg,
2753 "%s %s: invalid component end '%s'\n",
2754 progname, argv[0], optarg);
2760 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2763 "%s %s: invalid stripe offset '%s'\n",
2764 progname, argv[0], optarg);
2767 if (lsa.lsa_stripe_off == -1)
2768 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2771 comp_id = strtoul(optarg, &end, 0);
2772 if (*end != '\0' || comp_id == 0 ||
2773 comp_id > LCME_ID_MAX) {
2775 "%s %s: invalid component ID '%s'\n",
2776 progname, argv[0], optarg);
2781 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2783 "error: %s: invalid option: %s\n",
2784 progname, argv[optopt + 1]);
2787 if (opc == SO_MIRROR_EXTEND) {
2788 if (last_mirror == NULL) {
2790 "error: %s: '-N' must exist in front of '%s'\n",
2791 progname, argv[optopt + 1]);
2794 last_mirror->m_file = optarg;
2795 last_mirror->m_count = 1;
2798 if (mirror_list == NULL)
2799 mirror_list = lfs_mirror_alloc();
2800 mirror_list->m_file = optarg;
2805 if (strcmp(argv[optind - 1], "mdt") == 0) {
2806 /* Can be only the first component */
2807 if (layout != NULL) {
2809 fprintf(stderr, "error: 'mdt' layout "
2810 "can be only the first one\n");
2813 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2815 fprintf(stderr, "error: 'mdt' layout "
2816 "size is too big\n");
2819 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2820 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2822 fprintf(stderr, "error: layout '%s' is "
2823 "unknown, supported layouts are: "
2824 "'mdt', 'raid0'\n", argv[optind]);
2829 if (!migrate_mode) {
2831 "%s %s: -m|--mdt-index valid only for migrate command\n",
2835 mdt_idx_arg = optarg;
2838 if (!migrate_mode) {
2840 "%s %s: -n|--non-block valid only for migrate command\n",
2844 migration_flags |= MIGRATION_NONBLOCK;
2847 if (opc == SO_SETSTRIPE) {
2848 opc = SO_MIRROR_CREATE;
2852 if (optarg != NULL) {
2853 mirror_count = strtoul(optarg, &end, 0);
2854 if (*end != '\0' || mirror_count == 0) {
2856 "error: %s: bad mirror count: %s\n",
2863 new_mirror = lfs_mirror_alloc();
2864 new_mirror->m_count = mirror_count;
2866 if (mirror_list == NULL)
2867 mirror_list = new_mirror;
2869 if (last_mirror != NULL) {
2870 /* wrap up last mirror */
2871 if (lsa.lsa_comp_end == 0)
2872 lsa.lsa_comp_end = LUSTRE_EOF;
2874 result = comp_args_to_layout(lpp, &lsa, true);
2876 lfs_mirror_free(new_mirror);
2880 setstripe_args_init_inherit(&lsa);
2882 last_mirror->m_next = new_mirror;
2885 last_mirror = new_mirror;
2886 lpp = &last_mirror->m_layout;
2889 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2890 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2891 fprintf(stderr, "warning: '--ost-list' is "
2892 "deprecated, use '--ost' instead\n");
2894 lsa.lsa_nr_tgts = parse_targets(osts,
2895 sizeof(osts) / sizeof(__u32),
2896 lsa.lsa_nr_tgts, optarg);
2897 if (lsa.lsa_nr_tgts < 0) {
2899 "%s %s: invalid OST target(s) '%s'\n",
2900 progname, argv[0], optarg);
2904 lsa.lsa_tgts = osts;
2905 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2906 lsa.lsa_stripe_off = osts[0];
2911 lsa.lsa_pool_name = optarg;
2913 if (strlen(lsa.lsa_pool_name) == 0 ||
2914 strncmp(lsa.lsa_pool_name, "none",
2915 LOV_MAXPOOLNAME) == 0)
2916 lsa.lsa_pool_name = NULL;
2919 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2923 "%s %s: invalid stripe size '%s'\n",
2924 progname, argv[0], optarg);
2929 if (!migrate_mode) {
2931 "%s %s: -v|--verbose valid only for migrate command\n",
2935 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2942 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2943 progname, argv[0], argv[optind - 1]);
2948 fname = argv[optind];
2950 if (optind == argc) {
2951 fprintf(stderr, "%s %s: FILE must be specified\n",
2956 if (mirror_mode && mirror_count == 0) {
2958 "error: %s: --mirror-count|-N option is required\n",
2965 if (lsa.lsa_comp_end == 0)
2966 lsa.lsa_comp_end = LUSTRE_EOF;
2969 if (lsa.lsa_comp_end != 0) {
2970 result = comp_args_to_layout(lpp, &lsa, true);
2975 if (mirror_flags & MF_NO_VERIFY) {
2976 if (opc != SO_MIRROR_EXTEND) {
2978 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2982 } else if (!has_m_file) {
2984 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2991 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2992 * altered by user space tool, so we don't need to support the
2993 * --component-set for this moment. */
2994 if (comp_set && !comp_id) {
2995 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3000 if ((delete + comp_set + comp_del + comp_add) > 1) {
3002 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3007 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3008 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3010 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3015 if ((comp_set || comp_del) &&
3016 (setstripe_args_specified(&lsa) || layout != NULL)) {
3018 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3023 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3025 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3030 if (comp_add || comp_del) {
3033 result = lstat(fname, &st);
3034 if (result == 0 && S_ISDIR(st.st_mode)) {
3036 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3042 fprintf(stderr, "error: %s: can't use --component-add "
3043 "or --component-del for mirror operation\n",
3050 if (layout == NULL) {
3052 "%s %s: option -E must be specified with --component-add\n",
3057 result = adjust_first_extent(fname, layout);
3058 if (result == -ENODATA)
3060 else if (result != 0)
3064 if (from_yaml && from_copy) {
3066 "%s: can't specify --yaml and --copy together\n",
3071 if ((from_yaml || from_copy) &&
3072 (setstripe_args_specified(&lsa) || layout != NULL)) {
3073 fprintf(stderr, "error: %s: can't specify --yaml with "
3074 "-c, -S, -i, -o, -p or -E options.\n",
3079 if (mdt_idx_arg != NULL && optind > 3) {
3081 "%s %s: option -m cannot be used with other options\n",
3086 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3088 "%s %s: options --non-block and --block are mutually exclusive\n",
3093 if (!comp_del && !comp_set && comp_id != 0) {
3095 "%s %s: option -I can only be used with --component-del\n",
3100 if (mdt_idx_arg != NULL) {
3101 /* initialize migrate mdt parameters */
3102 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
3104 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
3105 progname, argv[0], mdt_idx_arg);
3108 migrate_mdt_param.fp_migrate = 1;
3109 } else if (layout == NULL) {
3110 /* initialize stripe parameters */
3111 param = calloc(1, offsetof(typeof(*param),
3112 lsp_osts[lsa.lsa_nr_tgts]));
3113 if (param == NULL) {
3115 "%s %s: cannot allocate memory for parameters: %s\n",
3116 progname, argv[0], strerror(ENOMEM));
3121 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3122 param->lsp_stripe_size = lsa.lsa_stripe_size;
3123 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3124 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3125 param->lsp_stripe_count = -1;
3127 param->lsp_stripe_count = lsa.lsa_stripe_count;
3129 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3130 param->lsp_stripe_offset = -1;
3132 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3133 param->lsp_pool = lsa.lsa_pool_name;
3134 param->lsp_is_specific = false;
3135 if (lsa.lsa_nr_tgts > 0) {
3136 if (lsa.lsa_stripe_count > 0 &&
3137 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3138 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3139 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3140 fprintf(stderr, "error: %s: stripe count %lld "
3141 "doesn't match the number of OSTs: %d\n"
3142 , argv[0], lsa.lsa_stripe_count,
3148 param->lsp_is_specific = true;
3149 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3150 memcpy(param->lsp_osts, osts,
3151 sizeof(*osts) * lsa.lsa_nr_tgts);
3156 /* generate a layout from a YAML template */
3157 result = lfs_comp_create_from_yaml(template, &layout,
3160 fprintf(stderr, "error: %s: can't create composite "
3161 "layout from template file %s\n",
3165 } else if (from_copy) {
3166 layout = llapi_layout_get_by_path(template, 0);
3167 if (layout == NULL) {
3169 "%s: can't create composite layout from file %s.\n",
3170 progname, template);
3175 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3176 if (mdt_idx_arg != NULL) {
3177 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3178 } else if (migrate_mode) {
3179 result = lfs_migrate(fname, migration_flags, param,
3181 } else if (comp_set != 0) {
3182 result = lfs_component_set(fname, comp_id,
3184 lsa.lsa_comp_neg_flags);
3185 } else if (comp_del != 0) {
3186 result = lfs_component_del(fname, comp_id,
3188 lsa.lsa_comp_neg_flags);
3189 } else if (comp_add != 0) {
3190 result = lfs_component_add(fname, layout);
3191 } else if (opc == SO_MIRROR_CREATE) {
3192 result = mirror_create(fname, mirror_list);
3193 } else if (opc == SO_MIRROR_EXTEND) {
3194 result = mirror_extend(fname, mirror_list,
3196 } else if (opc == SO_MIRROR_SPLIT) {
3197 if (mirror_id == 0) {
3199 "%s %s: no mirror id is specified\n",
3203 result = mirror_split(fname, mirror_id, mirror_flags,
3204 has_m_file ? mirror_list->m_file :
3206 } else if (layout != NULL) {
3207 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3214 result = llapi_file_open_param(fname,
3223 /* Save the first error encountered. */
3231 llapi_layout_free(layout);
3232 lfs_mirror_list_free(mirror_list);
3237 llapi_layout_free(layout);
3238 lfs_mirror_list_free(mirror_list);
3242 static int lfs_poollist(int argc, char **argv)
3247 return llapi_poollist(argv[1]);
3250 static int set_time(time_t *time, time_t *set, char *str)
3258 else if (str[0] == '-')
3264 t = strtol(str, &endptr, 0);
3265 if (*endptr != '\0') {
3267 "%s find: bad time '%s': %s\n",
3268 progname, str, strerror(EINVAL));
3271 if (*time < t * 24 * 60 * 60) {
3275 "%s find: bad time '%s': too large\n",
3280 *set = *time - t * 24 * 60 * 60;
3284 static int name2uid(unsigned int *id, const char *name)
3286 struct passwd *passwd;
3288 passwd = getpwnam(name);
3291 *id = passwd->pw_uid;
3296 static int name2gid(unsigned int *id, const char *name)
3298 struct group *group;
3300 group = getgrnam(name);
3303 *id = group->gr_gid;
3308 static inline int name2projid(unsigned int *id, const char *name)
3313 static int uid2name(char **name, unsigned int id)
3315 struct passwd *passwd;
3317 passwd = getpwuid(id);
3320 *name = passwd->pw_name;
3325 static inline int gid2name(char **name, unsigned int id)
3327 struct group *group;
3329 group = getgrgid(id);
3332 *name = group->gr_name;
3337 static int name2layout(__u32 *layout, char *name)
3339 char *ptr, *layout_name;
3342 for (ptr = name; ; ptr = NULL) {
3343 layout_name = strtok(ptr, ",");
3344 if (layout_name == NULL)
3346 if (strcmp(layout_name, "released") == 0)
3347 *layout |= LOV_PATTERN_F_RELEASED;
3348 else if (strcmp(layout_name, "raid0") == 0)
3349 *layout |= LOV_PATTERN_RAID0;
3350 else if (strcmp(layout_name, "mdt") == 0)
3351 *layout |= LOV_PATTERN_MDT;
3358 static int lfs_find(int argc, char **argv)
3363 struct find_param param = {
3367 struct option long_opts[] = {
3368 { .val = 'A', .name = "atime", .has_arg = required_argument },
3369 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3370 { .val = LFS_COMP_COUNT_OPT,
3371 .name = "comp-count", .has_arg = required_argument },
3372 { .val = LFS_COMP_COUNT_OPT,
3373 .name = "component-count",
3374 .has_arg = required_argument },
3375 { .val = LFS_COMP_FLAGS_OPT,
3376 .name = "comp-flags", .has_arg = required_argument },
3377 { .val = LFS_COMP_FLAGS_OPT,
3378 .name = "component-flags",
3379 .has_arg = required_argument },
3380 { .val = LFS_COMP_START_OPT,
3381 .name = "comp-start", .has_arg = required_argument },
3382 { .val = LFS_COMP_START_OPT,
3383 .name = "component-start",
3384 .has_arg = required_argument },
3385 { .val = LFS_MIRROR_STATE_OPT,
3386 .name = "mirror-state", .has_arg = required_argument },
3387 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3388 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3389 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3390 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3391 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3392 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3393 { .val = 'E', .name = "component-end",
3394 .has_arg = required_argument },
3395 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3396 { .val = 'g', .name = "gid", .has_arg = required_argument },
3397 { .val = 'G', .name = "group", .has_arg = required_argument },
3398 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3399 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3400 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3401 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3402 { .val = 'L', .name = "layout", .has_arg = required_argument },
3403 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3404 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3405 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3406 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3407 { .val = 'n', .name = "name", .has_arg = required_argument },
3408 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3409 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3410 { .val = 'O', .name = "obd", .has_arg = required_argument },
3411 { .val = 'O', .name = "ost", .has_arg = required_argument },
3412 /* no short option for pool yet, can be 'p' after 2.18 */
3413 { .val = LFS_POOL_OPT,
3414 .name = "pool", .has_arg = required_argument },
3415 { .val = '0', .name = "print0", .has_arg = no_argument },
3416 { .val = 'P', .name = "print", .has_arg = no_argument },
3417 { .val = LFS_PROJID_OPT,
3418 .name = "projid", .has_arg = required_argument },
3419 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3420 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3421 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3422 { .val = 's', .name = "size", .has_arg = required_argument },
3423 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3424 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3425 { .val = 't', .name = "type", .has_arg = required_argument },
3426 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3427 { .val = 'u', .name = "uid", .has_arg = required_argument },
3428 { .val = 'U', .name = "user", .has_arg = required_argument },
3429 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3430 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3442 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3443 while ((c = getopt_long_only(argc, argv,
3444 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3445 long_opts, NULL)) >= 0) {
3450 /* '!' is part of option */
3451 /* when getopt_long_only() finds a string which is not
3452 * an option nor a known option argument it returns 1
3453 * in that case if we already have found pathstart and pathend
3454 * (i.e. we have the list of pathnames),
3455 * the only supported value is "!"
3457 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3458 if (!isoption && pathend != -1) {
3459 fprintf(stderr, "err: %s: filename|dirname must either "
3460 "precede options or follow options\n",
3465 if (!isoption && pathstart == -1)
3466 pathstart = optind - 1;
3467 if (isoption && pathstart != -1 && pathend == -1)
3468 pathend = optind - 2;
3474 /* unknown; opt is "!" or path component,
3475 * checking done above.
3477 if (strcmp(optarg, "!") == 0)
3481 xtime = ¶m.fp_atime;
3482 xsign = ¶m.fp_asign;
3483 param.fp_exclude_atime = !!neg_opt;
3484 /* no break, this falls through to 'C' for ctime */
3487 xtime = ¶m.fp_ctime;
3488 xsign = ¶m.fp_csign;
3489 param.fp_exclude_ctime = !!neg_opt;
3491 /* no break, this falls through to 'M' for mtime */
3494 xtime = ¶m.fp_mtime;
3495 xsign = ¶m.fp_msign;
3496 param.fp_exclude_mtime = !!neg_opt;
3498 rc = set_time(&t, xtime, optarg);
3499 if (rc == INT_MAX) {
3507 if (optarg[0] == '+') {
3508 param.fp_blocks_sign = -1;
3510 } else if (optarg[0] == '-') {
3511 param.fp_blocks_sign = 1;
3515 param.fp_blocks_units = 1024;
3516 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3517 ¶m.fp_blocks_units, 0);
3519 fprintf(stderr, "error: bad blocks '%s'\n",
3523 param.fp_check_blocks = 1;
3524 param.fp_exclude_blocks = !!neg_opt;
3526 case LFS_COMP_COUNT_OPT:
3527 if (optarg[0] == '+') {
3528 param.fp_comp_count_sign = -1;
3530 } else if (optarg[0] == '-') {
3531 param.fp_comp_count_sign = 1;
3535 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3536 if (*endptr != '\0') {
3537 fprintf(stderr, "error: bad component count "
3541 param.fp_check_comp_count = 1;
3542 param.fp_exclude_comp_count = !!neg_opt;
3544 case LFS_COMP_FLAGS_OPT:
3545 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3546 ¶m.fp_comp_neg_flags);
3548 fprintf(stderr, "error: bad component flags "
3552 param.fp_check_comp_flags = 1;
3554 __u32 flags = param.fp_comp_neg_flags;
3555 param.fp_comp_neg_flags = param.fp_comp_flags;
3556 param.fp_comp_flags = flags;
3559 case LFS_COMP_START_OPT:
3560 if (optarg[0] == '+') {
3561 param.fp_comp_start_sign = -1;
3563 } else if (optarg[0] == '-') {
3564 param.fp_comp_start_sign = 1;
3568 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3569 ¶m.fp_comp_start_units, 0);
3571 fprintf(stderr, "error: bad component start "
3575 param.fp_check_comp_start = 1;
3576 param.fp_exclude_comp_start = !!neg_opt;
3578 case LFS_MIRROR_STATE_OPT:
3579 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3580 ¶m.fp_mirror_neg_state);
3583 "error: bad mirrored file state '%s'\n",
3587 param.fp_check_mirror_state = 1;
3589 __u16 state = param.fp_mirror_neg_state;
3590 param.fp_mirror_neg_state =
3591 param.fp_mirror_state;
3592 param.fp_mirror_state = state;
3596 if (optarg[0] == '+') {
3597 param.fp_stripe_count_sign = -1;
3599 } else if (optarg[0] == '-') {
3600 param.fp_stripe_count_sign = 1;
3604 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3605 if (*endptr != '\0') {
3606 fprintf(stderr,"error: bad stripe_count '%s'\n",
3611 param.fp_check_stripe_count = 1;
3612 param.fp_exclude_stripe_count = !!neg_opt;
3615 param.fp_max_depth = strtol(optarg, 0, 0);
3618 if (optarg[0] == '+') {
3619 param.fp_comp_end_sign = -1;
3621 } else if (optarg[0] == '-') {
3622 param.fp_comp_end_sign = 1;
3626 if (arg_is_eof(optarg)) {
3627 param.fp_comp_end = LUSTRE_EOF;
3628 param.fp_comp_end_units = 1;
3631 rc = llapi_parse_size(optarg,
3633 ¶m.fp_comp_end_units, 0);
3636 fprintf(stderr, "error: bad component end "
3640 param.fp_check_comp_end = 1;
3641 param.fp_exclude_comp_end = !!neg_opt;
3645 rc = name2gid(¶m.fp_gid, optarg);
3647 param.fp_gid = strtoul(optarg, &endptr, 10);
3648 if (*endptr != '\0') {
3649 fprintf(stderr, "Group/GID: %s cannot "
3650 "be found.\n", optarg);
3655 param.fp_exclude_gid = !!neg_opt;
3656 param.fp_check_gid = 1;
3659 param.fp_hash_type = check_hashtype(optarg);
3660 if (param.fp_hash_type == 0) {
3661 fprintf(stderr, "error: bad hash_type '%s'\n",
3666 param.fp_check_hash_type = 1;
3667 param.fp_exclude_hash_type = !!neg_opt;
3670 ret = name2layout(¶m.fp_layout, optarg);
3673 param.fp_exclude_layout = !!neg_opt;
3674 param.fp_check_layout = 1;
3678 rc = name2uid(¶m.fp_uid, optarg);
3680 param.fp_uid = strtoul(optarg, &endptr, 10);
3681 if (*endptr != '\0') {
3682 fprintf(stderr, "User/UID: %s cannot "
3683 "be found.\n", optarg);
3688 param.fp_exclude_uid = !!neg_opt;
3689 param.fp_check_uid = 1;
3692 param.fp_pattern = (char *)optarg;
3693 param.fp_exclude_pattern = !!neg_opt;
3696 if (optarg[0] == '+') {
3697 param.fp_mirror_count_sign = -1;
3699 } else if (optarg[0] == '-') {
3700 param.fp_mirror_count_sign = 1;
3704 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3705 if (*endptr != '\0') {
3707 "error: bad mirror count '%s'\n",
3711 param.fp_check_mirror_count = 1;
3712 param.fp_exclude_mirror_count = !!neg_opt;
3717 char *buf, *token, *next, *p;
3721 buf = strdup(optarg);
3727 param.fp_exclude_obd = !!neg_opt;
3730 while (token && *token) {
3731 token = strchr(token, ',');
3738 param.fp_exclude_mdt = !!neg_opt;
3739 param.fp_num_alloc_mdts += len;
3740 tmp = realloc(param.fp_mdt_uuid,
3741 param.fp_num_alloc_mdts *
3742 sizeof(*param.fp_mdt_uuid));
3748 param.fp_mdt_uuid = tmp;
3750 param.fp_exclude_obd = !!neg_opt;
3751 param.fp_num_alloc_obds += len;
3752 tmp = realloc(param.fp_obd_uuid,
3753 param.fp_num_alloc_obds *
3754 sizeof(*param.fp_obd_uuid));
3760 param.fp_obd_uuid = tmp;
3762 for (token = buf; token && *token; token = next) {
3763 struct obd_uuid *puuid;
3766 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3769 ¶m.fp_obd_uuid[param.fp_num_obds++];
3771 p = strchr(token, ',');
3778 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3783 strncpy(puuid->uuid, token,
3784 sizeof(puuid->uuid));
3791 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3795 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3797 "Pool name %s is too long (max %d)\n",
3798 optarg, LOV_MAXPOOLNAME);
3803 * We do check for empty pool because empty pool
3804 * is used to find V1 LOV attributes
3806 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3807 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3808 param.fp_exclude_pool = !!neg_opt;
3809 param.fp_check_pool = 1;
3811 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3812 case 'p': /* want this for --pool, to match getstripe/find */
3814 "warning: -p deprecated, use --print0 or -0\n");
3817 param.fp_zero_end = 1;
3819 case 'P': /* we always print, this option is a no-op */
3821 case LFS_PROJID_OPT:
3822 rc = name2projid(¶m.fp_projid, optarg);
3824 param.fp_projid = strtoul(optarg, &endptr, 10);
3825 if (*endptr != '\0') {
3827 "Invalid project ID: %s",
3833 param.fp_exclude_projid = !!neg_opt;
3834 param.fp_check_projid = 1;
3837 if (optarg[0] == '+') {
3838 param.fp_size_sign = -1;
3840 } else if (optarg[0] == '-') {
3841 param.fp_size_sign = 1;
3845 ret = llapi_parse_size(optarg, ¶m.fp_size,
3846 ¶m.fp_size_units, 0);
3848 fprintf(stderr, "error: bad file size '%s'\n",
3852 param.fp_check_size = 1;
3853 param.fp_exclude_size = !!neg_opt;
3856 if (optarg[0] == '+') {
3857 param.fp_stripe_size_sign = -1;
3859 } else if (optarg[0] == '-') {
3860 param.fp_stripe_size_sign = 1;
3864 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3865 ¶m.fp_stripe_size_units, 0);
3867 fprintf(stderr, "error: bad stripe_size '%s'\n",
3871 param.fp_check_stripe_size = 1;
3872 param.fp_exclude_stripe_size = !!neg_opt;
3875 param.fp_exclude_type = !!neg_opt;
3876 switch (optarg[0]) {
3878 param.fp_type = S_IFBLK;
3881 param.fp_type = S_IFCHR;
3884 param.fp_type = S_IFDIR;
3887 param.fp_type = S_IFREG;
3890 param.fp_type = S_IFLNK;
3893 param.fp_type = S_IFIFO;
3896 param.fp_type = S_IFSOCK;
3899 fprintf(stderr, "error: %s: bad type '%s'\n",
3906 if (optarg[0] == '+') {
3907 param.fp_mdt_count_sign = -1;
3909 } else if (optarg[0] == '-') {
3910 param.fp_mdt_count_sign = 1;
3914 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3915 if (*endptr != '\0') {
3916 fprintf(stderr, "error: bad mdt_count '%s'\n",
3921 param.fp_check_mdt_count = 1;
3922 param.fp_exclude_mdt_count = !!neg_opt;
3930 if (pathstart == -1) {
3931 fprintf(stderr, "error: %s: no filename|pathname\n",
3935 } else if (pathend == -1) {
3941 rc = llapi_find(argv[pathstart], ¶m);
3942 if (rc != 0 && ret == 0)
3944 } while (++pathstart < pathend);
3947 fprintf(stderr, "error: %s failed for %s.\n",
3948 argv[0], argv[optind - 1]);
3950 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3951 free(param.fp_obd_uuid);
3953 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3954 free(param.fp_mdt_uuid);
3959 static int lfs_getstripe_internal(int argc, char **argv,
3960 struct find_param *param)
3962 struct option long_opts[] = {
3963 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3964 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
3965 { .val = LFS_COMP_COUNT_OPT,
3966 .name = "comp-count", .has_arg = no_argument },
3967 { .val = LFS_COMP_COUNT_OPT,
3968 .name = "component-count", .has_arg = no_argument },
3969 { .val = LFS_COMP_FLAGS_OPT,
3970 .name = "comp-flags", .has_arg = optional_argument },
3971 { .val = LFS_COMP_FLAGS_OPT,
3972 .name = "component-flags", .has_arg = optional_argument },
3973 { .val = LFS_COMP_START_OPT,
3974 .name = "comp-start", .has_arg = optional_argument },
3975 { .val = LFS_COMP_START_OPT,
3976 .name = "component-start", .has_arg = optional_argument },
3977 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3978 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3979 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
3980 { .val = 'd', .name = "directory", .has_arg = no_argument },
3981 { .val = 'D', .name = "default", .has_arg = no_argument },
3982 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3983 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
3984 { .val = 'F', .name = "fid", .has_arg = no_argument },
3985 { .val = 'g', .name = "generation", .has_arg = no_argument },
3986 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3987 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
3988 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3989 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3990 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3991 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3992 { .val = 'L', .name = "layout", .has_arg = no_argument },
3993 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3994 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3995 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3996 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
3997 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
3998 { .val = 'O', .name = "obd", .has_arg = required_argument },
3999 { .val = 'O', .name = "ost", .has_arg = required_argument },
4000 { .val = 'p', .name = "pool", .has_arg = no_argument },
4001 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4002 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4003 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4004 { .val = 'R', .name = "raw", .has_arg = no_argument },
4005 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4006 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4007 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4008 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4009 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4010 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4011 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4012 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4017 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
4018 long_opts, NULL)) != -1) {
4021 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4022 param->fp_verbose |= VERBOSE_COUNT;
4023 param->fp_max_depth = 0;
4026 case LFS_COMP_COUNT_OPT:
4027 param->fp_verbose |= VERBOSE_COMP_COUNT;
4028 param->fp_max_depth = 0;
4030 case LFS_COMP_FLAGS_OPT:
4031 if (optarg != NULL) {
4032 rc = comp_str2flags(optarg,
4033 ¶m->fp_comp_flags,
4034 ¶m->fp_comp_neg_flags);
4036 fprintf(stderr, "error: %s bad "
4037 "component flags '%s'.\n",
4041 param->fp_check_comp_flags = 1;
4043 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4044 param->fp_max_depth = 0;
4047 case LFS_COMP_START_OPT:
4048 if (optarg != NULL) {
4050 if (tmp[0] == '+') {
4051 param->fp_comp_start_sign = -1;
4053 } else if (tmp[0] == '-') {
4054 param->fp_comp_start_sign = 1;
4057 rc = llapi_parse_size(tmp,
4058 ¶m->fp_comp_start,
4059 ¶m->fp_comp_start_units, 0);
4061 fprintf(stderr, "error: %s bad "
4062 "component start '%s'.\n",
4066 param->fp_check_comp_start = 1;
4069 param->fp_verbose |= VERBOSE_COMP_START;
4070 param->fp_max_depth = 0;
4074 param->fp_max_depth = 0;
4077 param->fp_get_default_lmv = 1;
4080 if (optarg != NULL) {
4082 if (tmp[0] == '+') {
4083 param->fp_comp_end_sign = -1;
4085 } else if (tmp[0] == '-') {
4086 param->fp_comp_end_sign = 1;
4090 if (arg_is_eof(tmp)) {
4091 param->fp_comp_end = LUSTRE_EOF;
4092 param->fp_comp_end_units = 1;
4095 rc = llapi_parse_size(tmp,
4096 ¶m->fp_comp_end,
4097 ¶m->fp_comp_end_units, 0);
4100 fprintf(stderr, "error: %s bad "
4101 "component end '%s'.\n",
4105 param->fp_check_comp_end = 1;
4107 param->fp_verbose |= VERBOSE_COMP_END;
4108 param->fp_max_depth = 0;
4112 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4113 param->fp_verbose |= VERBOSE_DFID;
4114 param->fp_max_depth = 0;
4118 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4119 param->fp_verbose |= VERBOSE_GENERATION;
4120 param->fp_max_depth = 0;
4124 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4125 param->fp_verbose |= VERBOSE_OFFSET;
4126 param->fp_max_depth = 0;
4130 if (optarg != NULL) {
4131 param->fp_comp_id = strtoul(optarg, &end, 0);
4132 if (*end != '\0' || param->fp_comp_id == 0 ||
4133 param->fp_comp_id > LCME_ID_MAX) {
4134 fprintf(stderr, "error: %s bad "
4135 "component id '%s'\n",
4139 param->fp_check_comp_id = 1;
4142 param->fp_max_depth = 0;
4143 param->fp_verbose |= VERBOSE_COMP_ID;
4147 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4148 param->fp_verbose |= VERBOSE_LAYOUT;
4149 param->fp_max_depth = 0;
4152 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4154 fprintf(stderr, "warning: '-M' deprecated"
4155 ", use '--mdt-index' or '-m' instead\n");
4158 if (!(param->fp_verbose & VERBOSE_DETAIL))
4159 param->fp_max_depth = 0;
4160 param->fp_verbose |= VERBOSE_MDTINDEX;
4163 if (param->fp_obd_uuid) {
4165 "error: %s: only one obduuid allowed",
4169 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4172 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4173 param->fp_verbose |= VERBOSE_POOL;
4174 param->fp_max_depth = 0;
4181 param->fp_recursive = 1;
4187 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4188 param->fp_verbose |= VERBOSE_SIZE;
4189 param->fp_max_depth = 0;
4193 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4206 if (param->fp_recursive)
4207 param->fp_max_depth = -1;
4208 else if (param->fp_verbose & VERBOSE_DETAIL)
4209 param->fp_max_depth = 1;
4211 if (!param->fp_verbose)
4212 param->fp_verbose = VERBOSE_DEFAULT;
4213 if (param->fp_quiet)
4214 param->fp_verbose = VERBOSE_OBJID;
4217 rc = llapi_getstripe(argv[optind], param);
4218 } while (++optind < argc && !rc);
4221 fprintf(stderr, "error: %s failed for %s.\n",
4222 argv[0], argv[optind - 1]);
4226 static int lfs_tgts(int argc, char **argv)
4228 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4229 struct find_param param;
4230 int index = 0, rc=0;
4235 if (argc == 2 && !realpath(argv[1], path)) {
4237 fprintf(stderr, "error: invalid path '%s': %s\n",
4238 argv[1], strerror(-rc));
4242 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4243 /* Check if we have a mount point */
4244 if (mntdir[0] == '\0')
4247 memset(¶m, 0, sizeof(param));
4248 if (!strcmp(argv[0], "mdts"))
4249 param.fp_get_lmv = 1;
4251 rc = llapi_ostlist(mntdir, ¶m);
4253 fprintf(stderr, "error: %s: failed on %s\n",
4256 if (path[0] != '\0')
4258 memset(mntdir, 0, PATH_MAX);
4264 static int lfs_getstripe(int argc, char **argv)
4266 struct find_param param = { 0 };
4268 param.fp_max_depth = 1;
4269 return lfs_getstripe_internal(argc, argv, ¶m);
4273 static int lfs_getdirstripe(int argc, char **argv)
4275 struct find_param param = { 0 };
4276 struct option long_opts[] = {
4277 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4278 { .val = 'D', .name = "default", .has_arg = no_argument },
4279 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4280 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4281 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4282 { .val = 'O', .name = "obd", .has_arg = required_argument },
4283 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4284 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4285 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4289 param.fp_get_lmv = 1;
4291 while ((c = getopt_long(argc, argv,
4292 "cDHimO:rtTy", long_opts, NULL)) != -1)
4297 param.fp_verbose |= VERBOSE_COUNT;
4300 param.fp_get_default_lmv = 1;
4304 param.fp_verbose |= VERBOSE_OFFSET;
4306 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4308 fprintf(stderr, "warning: '-t' deprecated, "
4309 "use '--mdt-hash' or '-H' instead\n");
4312 param.fp_verbose |= VERBOSE_HASH_TYPE;
4315 if (param.fp_obd_uuid) {
4317 "error: %s: only one obduuid allowed",
4321 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4324 param.fp_recursive = 1;
4337 if (param.fp_recursive)
4338 param.fp_max_depth = -1;
4340 if (!param.fp_verbose)
4341 param.fp_verbose = VERBOSE_DEFAULT;
4344 rc = llapi_getstripe(argv[optind], ¶m);
4345 } while (++optind < argc && !rc);
4348 fprintf(stderr, "error: %s failed for %s.\n",
4349 argv[0], argv[optind - 1]);
4354 MNTDF_INODES = 0x0001,
4355 MNTDF_COOKED = 0x0002,
4356 MNTDF_LAZY = 0x0004,
4357 MNTDF_VERBOSE = 0x0008,
4358 MNTDF_SHOW = 0x0010,
4361 #define COOK(value) \
4364 while (value > 1024) { \
4372 #define CDF "%11llu"
4373 #define HDF "%8.1f%c"
4377 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4379 double avail, used, ratio = 0;
4381 avail = st->os_bavail;
4382 used = st->os_blocks - st->os_bfree;
4383 if (avail + used > 0)
4384 ratio = used / (used + avail) * 100 + 0.5;
4389 static int showdf(char *mntdir, struct obd_statfs *stat,
4390 char *uuid, enum mntdf_flags flags,
4391 char *type, int index, int rc)
4393 long long avail, used, total;
4395 char *suffix = "KMGTPEZY";
4396 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4397 char tbuf[3 * sizeof(__u64)];
4398 char ubuf[3 * sizeof(__u64)];
4399 char abuf[3 * sizeof(__u64)];
4400 char rbuf[3 * sizeof(__u64)];
4407 if (flags & MNTDF_INODES) {
4408 avail = stat->os_ffree;
4409 used = stat->os_files - stat->os_ffree;
4410 total = stat->os_files;
4412 int shift = flags & MNTDF_COOKED ? 0 : 10;
4414 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4415 used = ((stat->os_blocks - stat->os_bfree) *
4416 stat->os_bsize) >> shift;
4417 total = (stat->os_blocks * stat->os_bsize) >> shift;
4420 ratio = obd_statfs_ratio(stat);
4422 if (flags & MNTDF_COOKED) {
4426 cook_val = (double)total;
4429 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4432 snprintf(tbuf, sizeof(tbuf), CDF, total);
4434 cook_val = (double)used;
4437 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4440 snprintf(ubuf, sizeof(ubuf), CDF, used);
4442 cook_val = (double)avail;
4445 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4448 snprintf(abuf, sizeof(abuf), CDF, avail);
4450 snprintf(tbuf, sizeof(tbuf), CDF, total);
4451 snprintf(ubuf, sizeof(tbuf), CDF, used);
4452 snprintf(abuf, sizeof(tbuf), CDF, avail);
4455 sprintf(rbuf, RDF, ratio);
4456 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4457 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4459 printf("[%s:%d]", type, index);
4461 if (stat->os_state) {
4463 * Each character represents the matching
4466 const char state_names[] = "DRSI";
4471 for (i = 0, state = stat->os_state;
4472 state && i < sizeof(state_names); i++) {
4473 if (!(state & (1 << i)))
4475 printf("%c", state_names[i]);
4483 printf(UUF": inactive device\n", uuid);
4486 printf(UUF": %s\n", uuid, strerror(-rc));
4493 struct ll_stat_type {
4498 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4500 struct ll_statfs_data {
4502 struct obd_statfs sd_st;
4505 struct ll_statfs_buf {
4507 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4510 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4511 int ops, struct ll_statfs_buf *lsb)
4513 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4514 struct obd_uuid uuid_buf;
4515 char *poolname = NULL;
4516 struct ll_stat_type types[] = {
4517 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4518 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4519 { .st_name = NULL } };
4520 struct ll_stat_type *tp;
4521 __u64 ost_ffree = 0;
4529 poolname = strchr(pool, '.');
4530 if (poolname != NULL) {
4531 if (strncmp(fsname, pool, strlen(fsname))) {
4532 fprintf(stderr, "filesystem name incorrect\n");
4540 fd = open(mntdir, O_RDONLY);
4543 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4548 if (flags & MNTDF_SHOW) {
4549 if (flags & MNTDF_INODES)
4550 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4551 "UUID", "Inodes", "IUsed", "IFree",
4552 "IUse%", "Mounted on");
4554 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4556 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4557 "Used", "Available", "Use%", "Mounted on");
4560 for (tp = types; tp->st_name != NULL; tp++) {
4561 if (!(tp->st_op & ops))
4564 for (index = 0; ; index++) {
4565 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4566 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4567 type = flags & MNTDF_LAZY ?
4568 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4569 rc2 = llapi_obd_fstatfs(fd, type, index,
4570 &stat_buf, &uuid_buf);
4575 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4576 if (!(flags & MNTDF_VERBOSE))
4578 } else if (rc2 < 0 && rc == 0) {
4582 if (poolname && tp->st_op == LL_STATFS_LOV &&
4583 llapi_search_ost(fsname, poolname,
4584 obd_uuid2str(&uuid_buf)) != 1)
4587 /* the llapi_obd_statfs() call may have returned with
4588 * an error, but if it filled in uuid_buf we will at
4589 * lease use that to print out a message for that OBD.
4590 * If we didn't get anything in the uuid_buf, then fill
4591 * it in so that we can print an error message. */
4592 if (uuid_buf.uuid[0] == '\0')
4593 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4594 "%s%04x", tp->st_name, index);
4596 lsb->sb_buf[lsb->sb_count].sd_index = index;
4597 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4600 if (flags & MNTDF_SHOW)
4601 showdf(mntdir, &stat_buf,
4602 obd_uuid2str(&uuid_buf), flags,
4603 tp->st_name, index, rc2);
4606 if (tp->st_op == LL_STATFS_LMV) {
4607 sum.os_ffree += stat_buf.os_ffree;
4608 sum.os_files += stat_buf.os_files;
4609 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4610 sum.os_blocks += stat_buf.os_blocks *
4612 sum.os_bfree += stat_buf.os_bfree *
4614 sum.os_bavail += stat_buf.os_bavail *
4616 ost_ffree += stat_buf.os_ffree;
4624 /* If we don't have as many objects free on the OST as inodes
4625 * on the MDS, we reduce the total number of inodes to
4626 * compensate, so that the "inodes in use" number is correct.
4627 * Matches ll_statfs_internal() so the results are consistent. */
4628 if (ost_ffree < sum.os_ffree) {
4629 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4630 sum.os_ffree = ost_ffree;
4632 if (flags & MNTDF_SHOW) {
4634 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4641 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4643 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4645 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4647 int r1 = obd_statfs_ratio(st1);
4648 int r2 = obd_statfs_ratio(st2);
4649 int64_t result = r1 - r2;
4651 /* if both space usage are above 90, compare free inodes */
4652 if (r1 > 90 && r2 > 90)
4653 result = st2->os_ffree - st1->os_ffree;
4657 else if (result == 0)
4664 static int lfs_setdirstripe(int argc, char **argv)
4668 struct lfs_setstripe_args lsa = { 0 };
4669 struct llapi_stripe_param *param = NULL;
4670 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4673 char *mode_opt = NULL;
4674 bool default_stripe = false;
4675 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4676 mode_t previous_mode = 0;
4677 bool delete = false;
4678 struct ll_statfs_buf *lsb = NULL;
4679 char mntdir[PATH_MAX] = "";
4680 bool auto_distributed = false;
4682 struct option long_opts[] = {
4683 { .val = 'c', .name = "count", .has_arg = required_argument },
4684 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4685 { .val = 'd', .name = "delete", .has_arg = no_argument },
4686 { .val = 'D', .name = "default", .has_arg = no_argument },
4687 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4688 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4689 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4690 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4691 { .val = 'i', .name = "index", .has_arg = required_argument },
4693 { .val = 'o', .name = "mode", .has_arg = required_argument },
4694 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4695 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4697 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4698 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4701 setstripe_args_init(&lsa);
4703 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4711 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4714 "%s %s: invalid stripe count '%s'\n",
4715 progname, argv[0], optarg);
4721 default_stripe = true;
4724 default_stripe = true;
4726 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4728 fprintf(stderr, "warning: '--hash-type' and '-t' "
4729 "deprecated, use '--mdt-hash' or '-H' instead\n");
4732 lsa.lsa_pattern = check_hashtype(optarg);
4733 if (lsa.lsa_pattern == 0) {
4735 "%s %s: bad stripe hash type '%s'\n",
4736 progname, argv[0], optarg);
4741 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4742 if (strcmp(argv[optind - 1], "--index") == 0)
4744 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4747 lsa.lsa_nr_tgts = parse_targets(mdts,
4748 sizeof(mdts) / sizeof(__u32),
4749 lsa.lsa_nr_tgts, optarg);
4750 if (lsa.lsa_nr_tgts < 0) {
4752 "%s %s: invalid MDT target(s) '%s'\n",
4753 progname, argv[0], optarg);
4757 lsa.lsa_tgts = mdts;
4758 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4759 lsa.lsa_stripe_off = mdts[0];
4761 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4763 fprintf(stderr, "warning: '-m' is deprecated, "
4764 "use '--mode' or '-o' instead\n");
4770 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4771 progname, argv[0], argv[optind - 1]);
4776 if (optind == argc) {
4777 fprintf(stderr, "%s %s: DIR must be specified\n",
4782 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
4783 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
4785 "%s %s: stripe offset and count must be specified\n",
4791 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
4792 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
4794 "%s %s: cannot specify -d with -c or -i options\n",
4799 if (mode_opt != NULL) {
4800 mode = strtoul(mode_opt, &end, 8);
4803 "%s %s: bad MODE '%s'\n",
4804 progname, argv[0], mode_opt);
4807 previous_mode = umask(0);
4811 * initialize stripe parameters, in case param is converted to specific,
4812 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
4814 param = calloc(1, offsetof(typeof(*param),
4815 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
4816 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
4817 if (param == NULL) {
4819 "%s %s: cannot allocate memory for parameters: %s\n",
4820 progname, argv[0], strerror(ENOMEM));
4824 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4825 param->lsp_stripe_count = lsa.lsa_stripe_count;
4826 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4827 param->lsp_stripe_offset = -1;
4829 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4830 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4831 param->lsp_stripe_pattern = lsa.lsa_pattern;
4833 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
4834 param->lsp_pool = lsa.lsa_pool_name;
4835 param->lsp_is_specific = false;
4836 if (lsa.lsa_nr_tgts > 1) {
4837 if (lsa.lsa_stripe_count > 0 &&
4838 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4839 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4840 fprintf(stderr, "error: %s: stripe count %lld doesn't "
4841 "match the number of MDTs: %d\n",
4842 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
4847 param->lsp_is_specific = true;
4848 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4849 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
4852 dname = argv[optind];
4854 if (default_stripe) {
4855 result = llapi_dir_set_default_lmv(dname, param);
4857 /* if current \a dname isn't under the same \a mntdir
4858 * as the last one, and the last one was
4859 * auto-distributed, restore \a param.
4861 if (mntdir[0] != '\0' &&
4862 strncmp(dname, mntdir, strlen(mntdir)) &&
4864 param->lsp_is_specific = false;
4865 param->lsp_stripe_offset = -1;
4866 auto_distributed = false;
4869 if (!param->lsp_is_specific &&
4870 param->lsp_stripe_offset == -1) {
4871 char path[PATH_MAX] = "";
4874 lsb = malloc(sizeof(*lsb));
4882 /* use mntdir for dirname() temporarily */
4883 strncpy(mntdir, dname, sizeof(mntdir));
4884 if (!realpath(dirname(mntdir), path)) {
4887 "error: invalid path '%s': %s\n",
4888 argv[optind], strerror(errno));
4893 result = llapi_search_mounts(path, 0, mntdir,
4895 if (result < 0 || mntdir[0] == '\0') {
4897 "No suitable Lustre mount found\n");
4901 result = mntdf(mntdir, NULL, NULL, 0,
4902 LL_STATFS_LMV, lsb);
4906 if (param->lsp_stripe_count > lsb->sb_count) {
4908 "error: stripe count %d is too big\n",
4909 param->lsp_stripe_count);
4914 qsort(lsb->sb_buf, lsb->sb_count,
4915 sizeof(struct ll_statfs_data),
4916 ll_statfs_data_comp);
4918 auto_distributed = true;
4921 if (auto_distributed) {
4923 int nr = MAX(param->lsp_stripe_count,
4926 /* don't use server whose usage is above 90% */
4927 while (nr != param->lsp_stripe_count &&
4928 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
4930 nr = MAX(param->lsp_stripe_count,
4933 /* get \a r between [0, nr) */
4936 param->lsp_stripe_offset =
4937 lsb->sb_buf[r].sd_index;
4938 if (param->lsp_stripe_count > 1) {
4941 param->lsp_is_specific = true;
4942 for (; i < param->lsp_stripe_count; i++)
4943 param->lsp_tgts[(i + r) % nr] =
4944 lsb->sb_buf[i].sd_index;
4948 result = llapi_dir_create(dname, mode, param);
4953 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
4954 progname, dname, strerror(-result));
4957 dname = argv[++optind];
4958 } while (dname != NULL);
4960 if (mode_opt != NULL)
4961 umask(previous_mode);
4969 static int lfs_rmentry(int argc, char **argv)
4976 fprintf(stderr, "error: %s: missing dirname\n",
4982 dname = argv[index];
4983 while (dname != NULL) {
4984 result = llapi_direntry_remove(dname);
4986 fprintf(stderr, "error: %s: remove dir entry '%s' "
4987 "failed\n", argv[0], dname);
4990 dname = argv[++index];
4995 static int lfs_mv(int argc, char **argv)
4997 struct find_param param = {
5004 struct option long_opts[] = {
5005 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5006 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5009 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5011 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5013 fprintf(stderr, "warning: '-M' deprecated"
5014 ", use '--mdt-index' or '-m' instead\n");
5017 param.fp_mdt_index = strtoul(optarg, &end, 0);
5019 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5025 param.fp_verbose = VERBOSE_DETAIL;
5028 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5029 progname, argv[optind - 1]);
5034 if (param.fp_mdt_index == -1) {
5035 fprintf(stderr, "%s mv: MDT index must be specified\n",
5040 if (optind >= argc) {
5041 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5045 param.fp_migrate = 1;
5046 rc = llapi_migrate_mdt(argv[optind], ¶m);
5048 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5049 progname, argv[optind], param.fp_mdt_index,
5054 static int lfs_osts(int argc, char **argv)
5056 return lfs_tgts(argc, argv);
5059 static int lfs_mdts(int argc, char **argv)
5061 return lfs_tgts(argc, argv);
5064 static int lfs_df(int argc, char **argv)
5066 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5067 enum mntdf_flags flags = MNTDF_SHOW;
5068 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5069 int c, rc = 0, index = 0;
5070 char fsname[PATH_MAX] = "", *pool_name = NULL;
5071 struct option long_opts[] = {
5072 { .val = 'h', .name = "human-readable",
5073 .has_arg = no_argument },
5074 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5075 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5076 { .val = 'p', .name = "pool", .has_arg = required_argument },
5077 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5080 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5083 flags |= MNTDF_COOKED;
5086 flags |= MNTDF_INODES;
5089 flags |= MNTDF_LAZY;
5095 flags |= MNTDF_VERBOSE;
5101 if (optind < argc && !realpath(argv[optind], path)) {
5103 fprintf(stderr, "error: invalid path '%s': %s\n",
5104 argv[optind], strerror(-rc));
5108 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5109 /* Check if we have a mount point */
5110 if (mntdir[0] == '\0')
5113 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5114 if (rc || path[0] != '\0')
5116 fsname[0] = '\0'; /* avoid matching in next loop */
5117 mntdir[0] = '\0'; /* avoid matching in next loop */
5123 static int lfs_getname(int argc, char **argv)
5125 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5126 int rc = 0, index = 0, c;
5127 char buf[sizeof(struct obd_uuid)];
5129 while ((c = getopt(argc, argv, "h")) != -1)
5132 if (optind == argc) { /* no paths specified, get all paths. */
5133 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5134 rc = llapi_getname(mntdir, buf, sizeof(buf));
5137 "cannot get name for `%s': %s\n",
5138 mntdir, strerror(-rc));
5142 printf("%s %s\n", buf, mntdir);
5144 path[0] = fsname[0] = mntdir[0] = 0;
5146 } else { /* paths specified, only attempt to search these. */
5147 for (; optind < argc; optind++) {
5148 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5151 "cannot get name for `%s': %s\n",
5152 argv[optind], strerror(-rc));
5156 printf("%s %s\n", buf, argv[optind]);
5162 static int lfs_check(int argc, char **argv)
5165 char mntdir[PATH_MAX] = {'\0'};
5172 fprintf(stderr, "%s check: server type must be specified\n",
5177 obd_types[0] = obd_type1;
5178 obd_types[1] = obd_type2;
5180 if (strcmp(argv[1], "osts") == 0) {
5181 strcpy(obd_types[0], "osc");
5182 } else if (strcmp(argv[1], "mds") == 0) {
5183 strcpy(obd_types[0], "mdc");
5184 } else if (strcmp(argv[1], "servers") == 0) {
5186 strcpy(obd_types[0], "osc");
5187 strcpy(obd_types[1], "mdc");
5189 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5194 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5195 if (rc < 0 || mntdir[0] == '\0') {
5197 "%s check: cannot find mounted Lustre filesystem: %s\n",
5198 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5202 rc = llapi_target_check(num_types, obd_types, mntdir);
5204 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5205 progname, argv[1], strerror(-rc));
5211 #ifdef HAVE_SYS_QUOTA_H
5212 #define ARG2INT(nr, str, msg) \
5215 nr = strtol(str, &endp, 0); \
5216 if (*endp != '\0') { \
5217 fprintf(stderr, "%s: bad %s '%s'\n", \
5218 progname, msg, str); \
5223 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5225 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5226 * returns the value or ULONG_MAX on integer overflow or incorrect format
5228 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5229 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5230 * 3. empty integer value is interpreted as 0
5232 static unsigned long str2sec(const char* timestr)
5234 const char spec[] = "smhdw";
5235 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5236 unsigned long val = 0;
5239 if (strpbrk(timestr, spec) == NULL) {
5240 /* no specifiers inside the time string,
5241 should treat it as an integer value */
5242 val = strtoul(timestr, &tail, 10);
5243 return *tail ? ULONG_MAX : val;
5246 /* format string is XXwXXdXXhXXmXXs */
5252 v = strtoul(timestr, &tail, 10);
5253 if (v == ULONG_MAX || *tail == '\0')
5254 /* value too large (ULONG_MAX or more)
5255 or missing specifier */
5258 ptr = strchr(spec, *tail);
5260 /* unknown specifier */
5265 /* check if product will overflow the type */
5266 if (!(v < ULONG_MAX / mult[ind]))
5269 ADD_OVERFLOW(val, mult[ind] * v);
5270 if (val == ULONG_MAX)
5282 #define ARG2ULL(nr, str, def_units) \
5284 unsigned long long limit, units = def_units; \
5287 rc = llapi_parse_size(str, &limit, &units, 1); \
5289 fprintf(stderr, "%s: invalid limit '%s'\n", \
5296 static inline int has_times_option(int argc, char **argv)
5300 for (i = 1; i < argc; i++)
5301 if (!strcmp(argv[i], "-t"))
5307 int lfs_setquota_times(int argc, char **argv)
5310 struct if_quotactl qctl;
5311 char *mnt, *obd_type = (char *)qctl.obd_type;
5312 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5313 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5314 struct option long_opts[] = {
5315 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5316 { .val = 'g', .name = "group", .has_arg = no_argument },
5317 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5318 { .val = 'p', .name = "projid", .has_arg = no_argument },
5319 { .val = 't', .name = "times", .has_arg = no_argument },
5320 { .val = 'u', .name = "user", .has_arg = no_argument },
5324 memset(&qctl, 0, sizeof(qctl));
5325 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5326 qctl.qc_type = ALLQUOTA;
5328 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5329 long_opts, NULL)) != -1) {
5340 if (qctl.qc_type != ALLQUOTA) {
5341 fprintf(stderr, "error: -u/g/p can't be used "
5342 "more than once\n");
5345 qctl.qc_type = qtype;
5348 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5349 fprintf(stderr, "error: bad block-grace: %s\n",
5353 dqb->dqb_valid |= QIF_BTIME;
5356 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5357 fprintf(stderr, "error: bad inode-grace: %s\n",
5361 dqb->dqb_valid |= QIF_ITIME;
5363 case 't': /* Yes, of course! */
5365 default: /* getopt prints error message for us when opterr != 0 */
5370 if (qctl.qc_type == ALLQUOTA) {
5371 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5375 if (optind != argc - 1) {
5376 fprintf(stderr, "error: unexpected parameters encountered\n");
5380 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5381 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5382 fprintf(stderr, "error: grace time is too large\n");
5387 rc = llapi_quotactl(mnt, &qctl);
5390 fprintf(stderr, "%s %s ", obd_type,
5391 obd_uuid2str(&qctl.obd_uuid));
5392 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5399 #define BSLIMIT (1 << 0)
5400 #define BHLIMIT (1 << 1)
5401 #define ISLIMIT (1 << 2)
5402 #define IHLIMIT (1 << 3)
5404 int lfs_setquota(int argc, char **argv)
5407 struct if_quotactl qctl;
5408 char *mnt, *obd_type = (char *)qctl.obd_type;
5409 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5410 struct option long_opts[] = {
5411 { .val = 'b', .name = "block-softlimit",
5412 .has_arg = required_argument },
5413 { .val = 'B', .name = "block-hardlimit",
5414 .has_arg = required_argument },
5415 { .val = 'd', .name = "default", .has_arg = no_argument },
5416 { .val = 'g', .name = "group", .has_arg = required_argument },
5417 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5418 { .val = 'i', .name = "inode-softlimit",
5419 .has_arg = required_argument },
5420 { .val = 'I', .name = "inode-hardlimit",
5421 .has_arg = required_argument },
5422 { .val = 'p', .name = "projid", .has_arg = required_argument },
5423 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5424 { .val = 'u', .name = "user", .has_arg = required_argument },
5425 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5427 unsigned limit_mask = 0;
5429 bool use_default = false;
5432 if (has_times_option(argc, argv))
5433 return lfs_setquota_times(argc, argv);
5435 memset(&qctl, 0, sizeof(qctl));
5436 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5437 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5438 * so it can be used as a marker that qc_type
5439 * isn't reinitialized from command line */
5441 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5442 long_opts, NULL)) != -1) {
5445 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5448 goto quota_type_def;
5451 rc = name2uid(&qctl.qc_id, optarg);
5454 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5457 goto quota_type_def;
5460 rc = name2gid(&qctl.qc_id, optarg);
5463 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5466 goto quota_type_def;
5469 rc = name2projid(&qctl.qc_id, optarg);
5472 qctl.qc_id = strtoul(optarg, &endptr, 10);
5473 if (*endptr != '\0') {
5474 fprintf(stderr, "%s setquota: invalid"
5475 " id '%s'\n", progname, optarg);
5480 if (qctl.qc_id == 0) {
5481 fprintf(stderr, "%s setquota: can't set quota"
5482 " for root usr/group/project.\n",
5488 if (qctl.qc_type != ALLQUOTA) {
5490 "%s setquota: only one of -u, -U, -g,"
5491 " -G, -p or -P may be specified\n",
5495 qctl.qc_type = qtype;
5498 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5502 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5503 dqb->dqb_bsoftlimit >>= 10;
5504 limit_mask |= BSLIMIT;
5505 if (dqb->dqb_bsoftlimit &&
5506 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5508 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5509 "See '%s help setquota' or Lustre manual for details\n",
5510 progname, dqb->dqb_bsoftlimit,
5514 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5515 dqb->dqb_bhardlimit >>= 10;
5516 limit_mask |= BHLIMIT;
5517 if (dqb->dqb_bhardlimit &&
5518 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5520 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5521 "See '%s help setquota' or Lustre manual for details\n",
5522 progname, dqb->dqb_bhardlimit,
5526 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5527 limit_mask |= ISLIMIT;
5528 if (dqb->dqb_isoftlimit &&
5529 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5531 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5532 "See '%s help setquota' or Lustre manual for details\n",
5533 progname, dqb->dqb_isoftlimit,
5537 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5538 limit_mask |= IHLIMIT;
5539 if (dqb->dqb_ihardlimit &&
5540 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5542 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5543 "See '%s help setquota' or Lustre manual for details\n",
5544 progname, dqb->dqb_ihardlimit,
5549 "%s setquota: unrecognized option '%s'\n",
5550 progname, argv[optind - 1]);
5555 if (qctl.qc_type == ALLQUOTA) {
5557 "%s setquota: either -u or -g must be specified\n",
5562 if (!use_default && limit_mask == 0) {
5564 "%s setquota: at least one limit must be specified\n",
5569 if (use_default && limit_mask != 0) {
5571 "%s setquota: limits should not be specified when"
5572 " using default quota\n",
5577 if (use_default && qctl.qc_id == 0) {
5579 "%s setquota: can not set default quota for root"
5580 " user/group/project\n",
5585 if (optind != argc - 1) {
5587 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5588 progname, argv[optind]);
5595 dqb->dqb_bhardlimit = 0;
5596 dqb->dqb_bsoftlimit = 0;
5597 dqb->dqb_ihardlimit = 0;
5598 dqb->dqb_isoftlimit = 0;
5601 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5602 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5603 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5604 /* sigh, we can't just set blimits/ilimits */
5605 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5606 .qc_type = qctl.qc_type,
5607 .qc_id = qctl.qc_id};
5609 rc = llapi_quotactl(mnt, &tmp_qctl);
5613 if (!(limit_mask & BHLIMIT))
5614 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5615 if (!(limit_mask & BSLIMIT))
5616 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5617 if (!(limit_mask & IHLIMIT))
5618 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5619 if (!(limit_mask & ISLIMIT))
5620 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5622 /* Keep grace times if we have got no softlimit arguments */
5623 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5624 dqb->dqb_valid |= QIF_BTIME;
5625 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5628 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5629 dqb->dqb_valid |= QIF_ITIME;
5630 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5634 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5635 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5637 rc = llapi_quotactl(mnt, &qctl);
5641 "%s setquota: cannot quotactl '%s' '%s': %s",
5643 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5650 /* Converts seconds value into format string
5651 * result is returned in buf
5653 * 1. result is in descenting order: 1w2d3h4m5s
5654 * 2. zero fields are not filled (except for p. 3): 5d1s
5655 * 3. zero seconds value is presented as "0s"
5657 static char * __sec2str(time_t seconds, char *buf)
5659 const char spec[] = "smhdw";
5660 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5665 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5666 c = seconds / mult[i];
5668 if (c > 0 || (i == 0 && buf == tail))
5669 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5677 static void sec2str(time_t seconds, char *buf, int rc)
5684 tail = __sec2str(seconds, tail);
5686 if (rc && tail - buf < 39) {
5692 static void diff2str(time_t seconds, char *buf, time_t now)
5698 if (seconds <= now) {
5699 strcpy(buf, "none");
5702 __sec2str(seconds - now, buf);
5705 static void print_quota_title(char *name, struct if_quotactl *qctl,
5706 bool human_readable, bool show_default)
5709 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
5710 printf("%15s %8s%8s%8s %8s%8s%8s\n",
5711 "Filesystem", "bquota", "blimit", "bgrace",
5712 "iquota", "ilimit", "igrace");
5714 printf("Disk quotas for %s %s (%cid %u):\n",
5715 qtype_name(qctl->qc_type), name,
5716 *qtype_name(qctl->qc_type), qctl->qc_id);
5717 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5718 "Filesystem", human_readable ? "used" : "kbytes",
5719 "quota", "limit", "grace",
5720 "files", "quota", "limit", "grace");
5724 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5727 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5730 snprintf(buf, buflen, "%5.4gP",
5731 (double)num / ((__u64)1 << 40));
5733 snprintf(buf, buflen, "%5.4gT",
5734 (double)num / (1 << 30));
5736 snprintf(buf, buflen, "%5.4gG",
5737 (double)num / (1 << 20));
5739 snprintf(buf, buflen, "%5.4gM",
5740 (double)num / (1 << 10));
5742 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5746 #define STRBUF_LEN 32
5747 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5748 int rc, bool h, bool show_default)
5754 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
5755 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
5756 int bover = 0, iover = 0;
5757 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5758 char numbuf[3][STRBUF_LEN];
5760 char strbuf[STRBUF_LEN];
5762 if (dqb->dqb_bhardlimit &&
5763 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5765 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5766 if (dqb->dqb_btime > now) {
5773 if (dqb->dqb_ihardlimit &&
5774 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5776 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5777 if (dqb->dqb_itime > now) {
5785 if (strlen(mnt) > 15)
5786 printf("%s\n%15s", mnt, "");
5788 printf("%15s", mnt);
5791 diff2str(dqb->dqb_btime, timebuf, now);
5792 else if (show_default)
5793 snprintf(timebuf, sizeof(timebuf), "%llu",
5796 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5797 strbuf, sizeof(strbuf), h);
5798 if (rc == -EREMOTEIO)
5799 sprintf(numbuf[0], "%s*", strbuf);
5801 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5802 "%s" : "[%s]", strbuf);
5804 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5805 if (type == QC_GENERAL)
5806 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5807 "%s" : "[%s]", strbuf);
5809 sprintf(numbuf[1], "%s", "-");
5811 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5812 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5813 "%s" : "[%s]", strbuf);
5816 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
5818 printf(" %7s%c %6s %7s %7s",
5819 numbuf[0], bover ? '*' : ' ', numbuf[1],
5820 numbuf[2], bover > 1 ? timebuf : "-");
5824 diff2str(dqb->dqb_itime, timebuf, now);
5825 else if (show_default)
5826 snprintf(timebuf, sizeof(timebuf), "%llu",
5829 snprintf(numbuf[0], sizeof(numbuf),
5830 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
5831 (uintmax_t)dqb->dqb_curinodes);
5833 if (type == QC_GENERAL)
5834 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5836 (uintmax_t)dqb->dqb_isoftlimit);
5838 sprintf(numbuf[1], "%s", "-");
5840 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5841 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5844 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
5845 else if (type != QC_OSTIDX)
5846 printf(" %7s%c %6s %7s %7s",
5847 numbuf[0], iover ? '*' : ' ', numbuf[1],
5848 numbuf[2], iover > 1 ? timebuf : "-");
5850 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5853 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5854 qctl->qc_cmd == Q_GETOINFO) {
5858 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5859 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5860 printf("Block grace time: %s; Inode grace time: %s\n",
5861 bgtimebuf, igtimebuf);
5865 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5866 bool h, __u64 *total)
5868 int rc = 0, rc1 = 0, count = 0;
5869 __u32 valid = qctl->qc_valid;
5871 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5873 fprintf(stderr, "can not get %s count: %s\n",
5874 is_mdt ? "mdt": "ost", strerror(-rc));
5878 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5879 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5880 rc = llapi_quotactl(mnt, qctl);
5882 /* It is remote client case. */
5883 if (rc == -EOPNOTSUPP) {
5890 fprintf(stderr, "quotactl %s%d failed.\n",
5891 is_mdt ? "mdt": "ost", qctl->qc_idx);
5895 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5896 qctl->qc_valid, 0, h, false);
5897 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5898 qctl->qc_dqblk.dqb_bhardlimit;
5901 qctl->qc_valid = valid;
5905 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5906 int verbose, int quiet, bool human_readable,
5909 int rc1 = 0, rc2 = 0, rc3 = 0;
5910 char *obd_type = (char *)qctl->obd_type;
5911 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5912 __u64 total_ialloc = 0, total_balloc = 0;
5913 bool use_default_for_blk = false;
5914 bool use_default_for_file = false;
5917 rc1 = llapi_quotactl(mnt, qctl);
5921 fprintf(stderr, "%s quotas are not enabled.\n",
5922 qtype_name(qctl->qc_type));
5925 fprintf(stderr, "Permission denied.\n");
5928 /* We already got error message. */
5931 fprintf(stderr, "Unexpected quotactl error: %s\n",
5936 if (!show_default && qctl->qc_id == 0) {
5937 qctl->qc_dqblk.dqb_bhardlimit = 0;
5938 qctl->qc_dqblk.dqb_bsoftlimit = 0;
5939 qctl->qc_dqblk.dqb_ihardlimit = 0;
5940 qctl->qc_dqblk.dqb_isoftlimit = 0;
5941 qctl->qc_dqblk.dqb_btime = 0;
5942 qctl->qc_dqblk.dqb_itime = 0;
5943 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
5946 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
5947 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
5948 use_default_for_blk = true;
5949 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
5952 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
5953 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
5954 use_default_for_file = true;
5955 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
5958 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
5959 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
5960 print_quota_title(name, qctl, human_readable, show_default);
5962 if (rc1 && *obd_type)
5963 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5965 if (qctl->qc_valid != QC_GENERAL)
5968 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5969 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5970 (QIF_LIMITS|QIF_USAGE));
5972 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
5974 if (!show_default && verbose &&
5975 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
5976 char strbuf[STRBUF_LEN];
5978 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5980 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5982 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5984 printf("Total allocated inode limit: %ju, total "
5985 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5989 if (use_default_for_blk)
5990 printf("%cid %u is using default block quota setting\n",
5991 *qtype_name(qctl->qc_type), qctl->qc_id);
5993 if (use_default_for_file)
5994 printf("%cid %u is using default file quota setting\n",
5995 *qtype_name(qctl->qc_type), qctl->qc_id);
5997 if (rc1 || rc2 || rc3 || inacc)
5998 printf("Some errors happened when getting quota info. "
5999 "Some devices may be not working or deactivated. "
6000 "The data in \"[]\" is inaccurate.\n");
6006 static int lfs_project(int argc, char **argv)
6008 int ret = 0, err = 0, c, i;
6009 struct project_handle_control phc = { 0 };
6010 enum lfs_project_ops_t op;
6013 phc.assign_projid = false;
6014 /* default action */
6015 op = LFS_PROJECT_LIST;
6017 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6020 if (op != LFS_PROJECT_LIST) {
6022 "%s: cannot specify '-c' '-C' '-s' together\n",
6027 op = LFS_PROJECT_CHECK;
6030 if (op != LFS_PROJECT_LIST) {
6032 "%s: cannot specify '-c' '-C' '-s' together\n",
6037 op = LFS_PROJECT_CLEAR;
6040 if (op != LFS_PROJECT_LIST) {
6042 "%s: cannot specify '-c' '-C' '-s' together\n",
6047 phc.set_inherit = true;
6048 op = LFS_PROJECT_SET;
6054 phc.keep_projid = true;
6057 phc.recursive = true;
6060 phc.projid = strtoul(optarg, NULL, 0);
6061 phc.assign_projid = true;
6065 phc.newline = false;
6068 fprintf(stderr, "%s: invalid option '%c'\n",
6074 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6075 op = LFS_PROJECT_SET;
6076 phc.set_projid = true;
6077 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6078 phc.set_projid = true;
6082 case LFS_PROJECT_CHECK:
6083 if (phc.keep_projid) {
6085 "%s: '-k' is useless together with '-c'\n",
6090 case LFS_PROJECT_CLEAR:
6093 "%s: '-0' is useless together with '-C'\n",
6097 if (phc.assign_projid) {
6099 "%s: '-p' is useless together with '-C'\n",
6104 case LFS_PROJECT_SET:
6107 "%s: '-0' is useless together with '-s'\n",
6111 if (phc.keep_projid) {
6113 "%s: '-k' is useless together with '-s'\n",
6121 "%s: '-0' is useless for list operations\n",
6131 fprintf(stderr, "%s: missing file or directory target(s)\n",
6136 for (i = 0; i < argc; i++) {
6138 case LFS_PROJECT_CHECK:
6139 err = lfs_project_check(argv[i], &phc);
6141 case LFS_PROJECT_LIST:
6142 err = lfs_project_list(argv[i], &phc);
6144 case LFS_PROJECT_CLEAR:
6145 err = lfs_project_clear(argv[i], &phc);
6147 case LFS_PROJECT_SET:
6148 err = lfs_project_set(argv[i], &phc);
6160 static int lfs_quota(int argc, char **argv)
6163 char *mnt, *name = NULL;
6164 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6165 .qc_type = ALLQUOTA };
6166 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6167 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6169 __u32 valid = QC_GENERAL, idx = 0;
6170 bool human_readable = false;
6171 bool show_default = false;
6174 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6177 show_default = true;
6182 show_default = true;
6187 show_default = true;
6191 if (qctl.qc_type != ALLQUOTA) {
6193 "%s quota: only one of -u, -g, or -p may be specified\n",
6197 qctl.qc_type = qtype;
6200 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6203 valid = qctl.qc_valid = QC_UUID;
6204 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6207 valid = qctl.qc_valid = QC_MDTIDX;
6208 idx = qctl.qc_idx = atoi(optarg);
6209 if (idx == 0 && *optarg != '0') {
6211 "%s quota: invalid MDT index '%s'\n",
6217 valid = qctl.qc_valid = QC_OSTIDX;
6218 idx = qctl.qc_idx = atoi(optarg);
6219 if (idx == 0 && *optarg != '0') {
6221 "%s quota: invalid OST index '%s'\n",
6233 human_readable = true;
6236 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6237 progname, argv[optind - 1]);
6242 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6243 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6244 optind == argc - 1 && !show_default) {
6246 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6247 qctl.qc_valid = valid;
6250 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6251 qctl.qc_type = qtype;
6252 if (qtype == USRQUOTA) {
6253 qctl.qc_id = geteuid();
6254 rc = uid2name(&name, qctl.qc_id);
6256 qctl.qc_id = getegid();
6257 rc = gid2name(&name, qctl.qc_id);
6262 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6263 human_readable, show_default);
6268 /* lfs quota -u username /path/to/lustre/mount */
6269 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6270 /* options should be followed by u/g-name and mntpoint */
6271 if ((!show_default && optind + 2 != argc) ||
6272 (show_default && optind + 1 != argc) ||
6273 qctl.qc_type == ALLQUOTA) {
6275 "%s quota: name and mount point must be specified\n",
6280 if (!show_default) {
6281 name = argv[optind++];
6282 switch (qctl.qc_type) {
6284 rc = name2uid(&qctl.qc_id, name);
6287 rc = name2gid(&qctl.qc_id, name);
6290 rc = name2projid(&qctl.qc_id, name);
6297 qctl.qc_valid = QC_GENERAL;
6298 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6303 qctl.qc_id = strtoul(name, &endptr, 10);
6304 if (*endptr != '\0') {
6305 fprintf(stderr, "%s quota: invalid id '%s'\n",
6310 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6311 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6317 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6318 human_readable, show_default);
6321 #endif /* HAVE_SYS_QUOTA_H! */
6323 static int flushctx_ioctl(char *mp)
6327 fd = open(mp, O_RDONLY);
6329 fprintf(stderr, "flushctx: error open %s: %s\n",
6330 mp, strerror(errno));
6334 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6336 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6337 mp, strerror(errno));
6343 static int lfs_flushctx(int argc, char **argv)
6345 int kdestroy = 0, c;
6346 char mntdir[PATH_MAX] = {'\0'};
6350 while ((c = getopt(argc, argv, "k")) != -1) {
6356 fprintf(stderr, "error: %s: option '-%c' "
6357 "unrecognized\n", argv[0], c);
6363 if ((rc = system("kdestroy > /dev/null")) != 0) {
6364 rc = WEXITSTATUS(rc);
6365 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6369 if (optind >= argc) {
6370 /* flush for all mounted lustre fs. */
6371 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6372 /* Check if we have a mount point */
6373 if (mntdir[0] == '\0')
6376 if (flushctx_ioctl(mntdir))
6379 mntdir[0] = '\0'; /* avoid matching in next loop */
6382 /* flush fs as specified */
6383 while (optind < argc) {
6384 if (flushctx_ioctl(argv[optind++]))
6391 static int lfs_cp(int argc, char **argv)
6393 fprintf(stderr, "remote client copy file(s).\n"
6394 "obsolete, does not support it anymore.\n");
6398 static int lfs_ls(int argc, char **argv)
6400 fprintf(stderr, "remote client lists directory contents.\n"
6401 "obsolete, does not support it anymore.\n");
6405 static int lfs_changelog(int argc, char **argv)
6407 void *changelog_priv;
6408 struct changelog_rec *rec;
6409 long long startrec = 0, endrec = 0;
6411 struct option long_opts[] = {
6412 { .val = 'f', .name = "follow", .has_arg = no_argument },
6414 char short_opts[] = "f";
6417 while ((rc = getopt_long(argc, argv, short_opts,
6418 long_opts, NULL)) != -1) {
6425 "%s changelog: unrecognized option '%s'\n",
6426 progname, argv[optind - 1]);
6430 if (optind >= argc) {
6431 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6436 mdd = argv[optind++];
6438 startrec = strtoll(argv[optind++], NULL, 10);
6440 endrec = strtoll(argv[optind++], NULL, 10);
6442 rc = llapi_changelog_start(&changelog_priv,
6443 CHANGELOG_FLAG_BLOCK |
6444 CHANGELOG_FLAG_JOBID |
6445 CHANGELOG_FLAG_EXTRA_FLAGS |
6446 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6449 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6450 progname, strerror(errno = -rc));
6454 rc = llapi_changelog_set_xflags(changelog_priv,
6455 CHANGELOG_EXTRA_FLAG_UIDGID |
6456 CHANGELOG_EXTRA_FLAG_NID |
6457 CHANGELOG_EXTRA_FLAG_OMODE |
6458 CHANGELOG_EXTRA_FLAG_XATTR);
6461 "%s changelog: cannot set xflags for changelog: %s\n",
6462 progname, strerror(errno = -rc));
6466 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6470 if (endrec && rec->cr_index > endrec) {
6471 llapi_changelog_free(&rec);
6474 if (rec->cr_index < startrec) {
6475 llapi_changelog_free(&rec);
6479 secs = rec->cr_time >> 30;
6480 gmtime_r(&secs, &ts);
6481 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6482 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6483 changelog_type2str(rec->cr_type),
6484 ts.tm_hour, ts.tm_min, ts.tm_sec,
6485 (int)(rec->cr_time & ((1 << 30) - 1)),
6486 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6487 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6489 if (rec->cr_flags & CLF_JOBID) {
6490 struct changelog_ext_jobid *jid =
6491 changelog_rec_jobid(rec);
6493 if (jid->cr_jobid[0] != '\0')
6494 printf(" j=%s", jid->cr_jobid);
6497 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6498 struct changelog_ext_extra_flags *ef =
6499 changelog_rec_extra_flags(rec);
6501 printf(" ef=0x%llx", ef->cr_extra_flags);
6503 if (ef->cr_extra_flags & CLFE_UIDGID) {
6504 struct changelog_ext_uidgid *uidgid =
6505 changelog_rec_uidgid(rec);
6507 printf(" u=%llu:%llu",
6508 uidgid->cr_uid, uidgid->cr_gid);
6510 if (ef->cr_extra_flags & CLFE_NID) {
6511 struct changelog_ext_nid *nid =
6512 changelog_rec_nid(rec);
6515 libcfs_nid2str(nid->cr_nid));
6518 if (ef->cr_extra_flags & CLFE_OPEN) {
6519 struct changelog_ext_openmode *omd =
6520 changelog_rec_openmode(rec);
6521 char mode[] = "---";
6523 /* exec mode must be exclusive */
6524 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6527 if (omd->cr_openflags & MDS_FMODE_READ)
6529 if (omd->cr_openflags &
6536 if (strcmp(mode, "---") != 0)
6537 printf(" m=%s", mode);
6541 if (ef->cr_extra_flags & CLFE_XATTR) {
6542 struct changelog_ext_xattr *xattr =
6543 changelog_rec_xattr(rec);
6545 if (xattr->cr_xattr[0] != '\0')
6546 printf(" x=%s", xattr->cr_xattr);
6550 if (rec->cr_namelen)
6551 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6552 rec->cr_namelen, changelog_rec_name(rec));
6554 if (rec->cr_flags & CLF_RENAME) {
6555 struct changelog_ext_rename *rnm =
6556 changelog_rec_rename(rec);
6558 if (!fid_is_zero(&rnm->cr_sfid))
6559 printf(" s="DFID" sp="DFID" %.*s",
6560 PFID(&rnm->cr_sfid),
6561 PFID(&rnm->cr_spfid),
6562 (int)changelog_rec_snamelen(rec),
6563 changelog_rec_sname(rec));
6567 llapi_changelog_free(&rec);
6570 llapi_changelog_fini(&changelog_priv);
6573 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6574 progname, strerror(errno = -rc));
6576 return (rc == 1 ? 0 : rc);
6579 static int lfs_changelog_clear(int argc, char **argv)
6587 endrec = strtoll(argv[3], NULL, 10);
6589 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6592 fprintf(stderr, "%s: record out of range: %llu\n",
6594 else if (rc == -ENOENT)
6595 fprintf(stderr, "%s: no changelog user: %s\n",
6598 fprintf(stderr, "%s error: %s\n", argv[0],
6607 static int lfs_fid2path(int argc, char **argv)
6609 struct option long_opts[] = {
6610 { .val = 'c', .name = "cur", .has_arg = no_argument },
6611 { .val = 'l', .name = "link", .has_arg = required_argument },
6612 { .val = 'r', .name = "rec", .has_arg = required_argument },
6614 char short_opts[] = "cl:r:";
6615 char *device, *fid, *path;
6616 long long recno = -1;
6621 char *endptr = NULL;
6623 while ((rc = getopt_long(argc, argv, short_opts,
6624 long_opts, NULL)) != -1) {
6630 linkno = strtol(optarg, &endptr, 10);
6631 if (*endptr != '\0') {
6633 "%s fid2path: invalid linkno '%s'\n",
6639 recno = strtoll(optarg, &endptr, 10);
6640 if (*endptr != '\0') {
6642 "%s fid2path: invalid recno '%s'\n",
6649 "%s fid2path: unrecognized option '%s'\n",
6650 progname, argv[optind - 1]);
6657 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6662 device = argv[optind++];
6663 path = calloc(1, PATH_MAX);
6667 "%s fid2path: cannot allocate memory for path: %s\n",
6668 progname, strerror(-rc));
6673 while (optind < argc) {
6674 fid = argv[optind++];
6676 lnktmp = (linkno >= 0) ? linkno : 0;
6678 int oldtmp = lnktmp;
6679 long long rectmp = recno;
6681 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6685 "%s fid2path: cannot find '%s': %s\n",
6686 progname, fid, strerror(errno = -rc2));
6693 fprintf(stdout, "%lld ", rectmp);
6694 if (device[0] == '/') {
6695 fprintf(stdout, "%s", device);
6696 if (device[strlen(device) - 1] != '/')
6697 fprintf(stdout, "/");
6698 } else if (path[0] == '\0') {
6699 fprintf(stdout, "/");
6701 fprintf(stdout, "%s\n", path);
6704 /* specified linkno */
6706 if (oldtmp == lnktmp)
6716 static int lfs_path2fid(int argc, char **argv)
6718 struct option long_opts[] = {
6719 { .val = 'p', .name = "parents", .has_arg = no_argument },
6722 const char short_opts[] = "p";
6723 const char *sep = "";
6726 bool show_parents = false;
6728 while ((rc = getopt_long(argc, argv, short_opts,
6729 long_opts, NULL)) != -1) {
6732 show_parents = true;
6736 "%s path2fid: unrecognized option '%s'\n",
6737 progname, argv[optind - 1]);
6742 if (optind > argc - 1) {
6743 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
6747 else if (optind < argc - 1)
6751 for (path = argv + optind; *path != NULL; path++) {
6753 if (!show_parents) {
6754 err = llapi_path2fid(*path, &fid);
6756 printf("%s%s"DFID"\n",
6757 *sep != '\0' ? *path : "", sep,
6760 char name[NAME_MAX + 1];
6761 unsigned int linkno = 0;
6763 while ((err = llapi_path2parent(*path, linkno, &fid,
6764 name, sizeof(name))) == 0) {
6765 if (*sep != '\0' && linkno == 0)
6766 printf("%s%s", *path, sep);
6768 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6773 /* err == -ENODATA is end-of-loop */
6774 if (linkno > 0 && err == -ENODATA) {
6782 "%s path2fid: cannot get %sfid for '%s': %s\n",
6783 progname, show_parents ? "parent " : "", *path,
6795 static int lfs_data_version(int argc, char **argv)
6802 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
6805 fprintf(stderr, "%s data_version: FILE must be specified\n",
6810 while ((c = getopt(argc, argv, "nrw")) != -1) {
6813 data_version_flags = 0;
6816 data_version_flags |= LL_DV_RD_FLUSH;
6819 data_version_flags |= LL_DV_WR_FLUSH;
6823 "%s data_version: unrecognized option '%s'\n",
6824 progname, argv[optind - 1]);
6828 if (optind == argc) {
6829 fprintf(stderr, "%s data_version: FILE must be specified\n",
6834 path = argv[optind];
6835 fd = open(path, O_RDONLY);
6838 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
6839 progname, path, strerror(-rc));
6843 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
6846 "%s data_version: cannot get version for '%s': %s\n",
6847 progname, path, strerror(-rc));
6849 printf("%ju" "\n", (uintmax_t)data_version);
6855 static int lfs_hsm_state(int argc, char **argv)
6860 struct hsm_user_state hus;
6868 rc = llapi_hsm_state_get(path, &hus);
6870 fprintf(stderr, "can't get hsm state for %s: %s\n",
6871 path, strerror(errno = -rc));
6875 /* Display path name and status flags */
6876 printf("%s: (0x%08x)", path, hus.hus_states);
6878 if (hus.hus_states & HS_RELEASED)
6879 printf(" released");
6880 if (hus.hus_states & HS_EXISTS)
6882 if (hus.hus_states & HS_DIRTY)
6884 if (hus.hus_states & HS_ARCHIVED)
6885 printf(" archived");
6886 /* Display user-settable flags */
6887 if (hus.hus_states & HS_NORELEASE)
6888 printf(" never_release");
6889 if (hus.hus_states & HS_NOARCHIVE)
6890 printf(" never_archive");
6891 if (hus.hus_states & HS_LOST)
6892 printf(" lost_from_hsm");
6894 if (hus.hus_archive_id != 0)
6895 printf(", archive_id:%d", hus.hus_archive_id);
6898 } while (++i < argc);
6903 #define LFS_HSM_SET 0
6904 #define LFS_HSM_CLEAR 1
6907 * Generic function to set or clear HSM flags.
6908 * Used by hsm_set and hsm_clear.
6910 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
6912 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
6914 struct option long_opts[] = {
6915 { .val = 'A', .name = "archived", .has_arg = no_argument },
6916 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
6917 { .val = 'd', .name = "dirty", .has_arg = no_argument },
6918 { .val = 'e', .name = "exists", .has_arg = no_argument },
6919 { .val = 'l', .name = "lost", .has_arg = no_argument },
6920 { .val = 'r', .name = "norelease", .has_arg = no_argument },
6921 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
6923 char short_opts[] = "lraAdei:";
6927 __u32 archive_id = 0;
6933 while ((c = getopt_long(argc, argv, short_opts,
6934 long_opts, NULL)) != -1) {
6940 mask |= HS_NOARCHIVE;
6943 mask |= HS_ARCHIVED;
6946 mask |= HS_NORELEASE;
6955 archive_id = strtol(optarg, &end, 10);
6957 fprintf(stderr, "invalid archive_id: '%s'\n",
6965 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6966 argv[0], argv[optind - 1]);
6971 /* User should have specified a flag */
6975 while (optind < argc) {
6977 path = argv[optind];
6979 /* If mode == 0, this means we apply the mask. */
6980 if (mode == LFS_HSM_SET)
6981 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6983 rc = llapi_hsm_state_set(path, 0, mask, 0);
6986 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6987 path, strerror(errno = -rc));
6996 static int lfs_hsm_action(int argc, char **argv)
7001 struct hsm_current_action hca;
7002 struct hsm_extent he;
7003 enum hsm_user_action hua;
7004 enum hsm_progress_states hps;
7012 rc = llapi_hsm_current_action(path, &hca);
7014 fprintf(stderr, "can't get hsm action for %s: %s\n",
7015 path, strerror(errno = -rc));
7018 he = hca.hca_location;
7019 hua = hca.hca_action;
7020 hps = hca.hca_state;
7022 printf("%s: %s", path, hsm_user_action2name(hua));
7024 /* Skip file without action */
7025 if (hca.hca_action == HUA_NONE) {
7030 printf(" %s ", hsm_progress_state2name(hps));
7032 if ((hps == HPS_RUNNING) &&
7033 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7034 printf("(%llu bytes moved)\n",
7035 (unsigned long long)he.length);
7036 else if ((he.offset + he.length) == LUSTRE_EOF)
7037 printf("(from %llu to EOF)\n",
7038 (unsigned long long)he.offset);
7040 printf("(from %llu to %llu)\n",
7041 (unsigned long long)he.offset,
7042 (unsigned long long)(he.offset + he.length));
7044 } while (++i < argc);
7049 static int lfs_hsm_set(int argc, char **argv)
7051 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7054 static int lfs_hsm_clear(int argc, char **argv)
7056 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7060 * Check file state and return its fid, to be used by lfs_hsm_request().
7062 * \param[in] file Path to file to check
7063 * \param[in,out] fid Pointer to allocated lu_fid struct.
7064 * \param[in,out] last_dev Pointer to last device id used.
7066 * \return 0 on success.
7068 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7074 rc = lstat(file, &st);
7076 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7079 /* Checking for regular file as archiving as posix copytool
7080 * rejects archiving files other than regular files
7082 if (!S_ISREG(st.st_mode)) {
7083 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7086 /* A request should be ... */
7087 if (*last_dev != st.st_dev && *last_dev != 0) {
7088 fprintf(stderr, "All files should be "
7089 "on the same filesystem: %s\n", file);
7092 *last_dev = st.st_dev;
7094 rc = llapi_path2fid(file, fid);
7096 fprintf(stderr, "Cannot read FID of %s: %s\n",
7097 file, strerror(-rc));
7103 /* Fill an HSM HUR item with a given file name.
7105 * If mntpath is set, then the filename is actually a FID, and no
7106 * lookup on the filesystem will be performed.
7108 * \param[in] hur the user request to fill
7109 * \param[in] idx index of the item inside the HUR to fill
7110 * \param[in] mntpath mountpoint of Lustre
7111 * \param[in] fname filename (if mtnpath is NULL)
7112 * or FID (if mntpath is set)
7113 * \param[in] last_dev pointer to last device id used
7115 * \retval 0 on success
7116 * \retval CMD_HELP or a negative errno on error
7118 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7119 const char *mntpath, const char *fname,
7122 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7125 hui->hui_extent.length = -1;
7127 if (mntpath != NULL) {
7130 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7134 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7139 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7143 hur->hur_request.hr_itemcount++;
7148 static int lfs_hsm_request(int argc, char **argv, int action)
7150 struct option long_opts[] = {
7151 { .val = 'a', .name = "archive", .has_arg = required_argument },
7152 { .val = 'D', .name = "data", .has_arg = required_argument },
7153 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7154 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7157 char short_opts[] = "l:D:a:m:";
7158 struct hsm_user_request *hur, *oldhur;
7163 char *filelist = NULL;
7164 char fullpath[PATH_MAX];
7165 char *opaque = NULL;
7169 int nbfile_alloc = 0;
7170 char *some_file = NULL;
7171 char *mntpath = NULL;
7177 while ((c = getopt_long(argc, argv, short_opts,
7178 long_opts, NULL)) != -1) {
7187 if (action != HUA_ARCHIVE &&
7188 action != HUA_REMOVE) {
7190 "error: -a is supported only "
7191 "when archiving or removing\n");
7194 archive_id = atoi(optarg);
7197 if (some_file == NULL) {
7199 some_file = strdup(optarg);
7205 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7206 argv[0], argv[optind - 1]);
7211 /* All remaining args are files, so we have at least nbfile */
7212 nbfile = argc - optind;
7214 if ((nbfile == 0) && (filelist == NULL))
7218 opaque_len = strlen(opaque);
7220 /* Alloc the request structure with enough place to store all files
7221 * from command line. */
7222 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7224 fprintf(stderr, "Cannot create the request: %s\n",
7228 nbfile_alloc = nbfile;
7230 hur->hur_request.hr_action = action;
7231 hur->hur_request.hr_archive_id = archive_id;
7232 hur->hur_request.hr_flags = 0;
7234 /* All remaining args are files, add them */
7235 if (nbfile != 0 && some_file == NULL)
7236 some_file = strdup(argv[optind]);
7238 for (i = 0; i < nbfile; i++) {
7239 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7245 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7247 /* If a filelist was specified, read the filelist from it. */
7248 if (filelist != NULL) {
7249 fp = fopen(filelist, "r");
7251 fprintf(stderr, "Cannot read the file list %s: %s\n",
7252 filelist, strerror(errno));
7257 while ((rc = getline(&line, &len, fp)) != -1) {
7258 /* If allocated buffer was too small, get something
7260 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7263 nbfile_alloc = nbfile_alloc * 2 + 1;
7265 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7268 fprintf(stderr, "hsm: cannot allocate "
7269 "the request: %s\n",
7276 size = hur_len(oldhur);
7278 fprintf(stderr, "hsm: cannot allocate "
7279 "%u files + %u bytes data\n",
7280 oldhur->hur_request.hr_itemcount,
7281 oldhur->hur_request.hr_data_len);
7288 memcpy(hur, oldhur, size);
7293 if (line[strlen(line) - 1] == '\n')
7294 line[strlen(line) - 1] = '\0';
7296 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7297 mntpath, line, &last_dev);
7303 if (some_file == NULL) {
7313 /* If a --data was used, add it to the request */
7314 hur->hur_request.hr_data_len = opaque_len;
7316 memcpy(hur_data(hur), opaque, opaque_len);
7318 /* Send the HSM request */
7319 if (realpath(some_file, fullpath) == NULL) {
7320 fprintf(stderr, "Could not find path '%s': %s\n",
7321 some_file, strerror(errno));
7323 rc = llapi_hsm_request(fullpath, hur);
7325 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7326 some_file, strerror(-rc));
7336 static int lfs_hsm_archive(int argc, char **argv)
7338 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7341 static int lfs_hsm_restore(int argc, char **argv)
7343 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7346 static int lfs_hsm_release(int argc, char **argv)
7348 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7351 static int lfs_hsm_remove(int argc, char **argv)
7353 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7356 static int lfs_hsm_cancel(int argc, char **argv)
7358 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7361 static int lfs_swap_layouts(int argc, char **argv)
7366 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7367 SWAP_LAYOUTS_KEEP_MTIME |
7368 SWAP_LAYOUTS_KEEP_ATIME);
7371 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7373 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7375 int lfs_get_mode(const char *string)
7377 enum lock_mode_user mode;
7379 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7380 if (lock_mode_names[mode] == NULL)
7382 if (strcmp(string, lock_mode_names[mode]) == 0)
7389 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7391 enum lu_ladvise_type advice;
7394 advice < ARRAY_SIZE(ladvise_names); advice++) {
7395 if (ladvise_names[advice] == NULL)
7397 if (strcmp(string, ladvise_names[advice]) == 0)
7401 return LU_LADVISE_INVALID;
7404 static int lfs_ladvise(int argc, char **argv)
7406 struct option long_opts[] = {
7407 { .val = 'a', .name = "advice", .has_arg = required_argument },
7408 { .val = 'b', .name = "background", .has_arg = no_argument },
7409 { .val = 'e', .name = "end", .has_arg = required_argument },
7410 { .val = 'l', .name = "length", .has_arg = required_argument },
7411 { .val = 'm', .name = "mode", .has_arg = required_argument },
7412 { .val = 's', .name = "start", .has_arg = required_argument },
7413 { .val = 'u', .name = "unset", .has_arg = no_argument },
7415 char short_opts[] = "a:be:l:m:s:u";
7420 struct llapi_lu_ladvise advice;
7421 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7422 unsigned long long start = 0;
7423 unsigned long long end = LUSTRE_EOF;
7424 unsigned long long length = 0;
7425 unsigned long long size_units;
7426 unsigned long long flags = 0;
7430 while ((c = getopt_long(argc, argv, short_opts,
7431 long_opts, NULL)) != -1) {
7434 advice_type = lfs_get_ladvice(optarg);
7435 if (advice_type == LU_LADVISE_INVALID) {
7436 fprintf(stderr, "%s: invalid advice type "
7437 "'%s'\n", argv[0], optarg);
7438 fprintf(stderr, "Valid types:");
7440 for (advice_type = 0;
7441 advice_type < ARRAY_SIZE(ladvise_names);
7443 if (ladvise_names[advice_type] == NULL)
7445 fprintf(stderr, " %s",
7446 ladvise_names[advice_type]);
7448 fprintf(stderr, "\n");
7461 rc = llapi_parse_size(optarg, &end,
7464 fprintf(stderr, "%s: bad end offset '%s'\n",
7471 rc = llapi_parse_size(optarg, &start,
7474 fprintf(stderr, "%s: bad start offset "
7475 "'%s'\n", argv[0], optarg);
7481 rc = llapi_parse_size(optarg, &length,
7484 fprintf(stderr, "%s: bad length '%s'\n",
7490 mode = lfs_get_mode(optarg);
7492 fprintf(stderr, "%s: bad mode '%s', valid "
7493 "modes are READ or WRITE\n",
7501 fprintf(stderr, "%s: option '%s' unrecognized\n",
7502 argv[0], argv[optind - 1]);
7507 if (advice_type == LU_LADVISE_INVALID) {
7508 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7509 fprintf(stderr, "Valid types:");
7510 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7512 if (ladvise_names[advice_type] == NULL)
7514 fprintf(stderr, " %s", ladvise_names[advice_type]);
7516 fprintf(stderr, "\n");
7520 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7521 fprintf(stderr, "%s: Lock no expand advice is a per file "
7522 "descriptor advice, so when called from lfs, "
7523 "it does nothing.\n", argv[0]);
7527 if (argc <= optind) {
7528 fprintf(stderr, "%s: please give one or more file names\n",
7533 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7534 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7539 if (end == LUSTRE_EOF && length != 0)
7540 end = start + length;
7543 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7544 argv[0], start, end);
7548 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7549 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7554 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7555 fprintf(stderr, "%s: mode is required with lockahead\n",
7560 while (optind < argc) {
7563 path = argv[optind++];
7565 fd = open(path, O_RDONLY);
7567 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7568 argv[0], path, strerror(errno));
7573 advice.lla_start = start;
7574 advice.lla_end = end;
7575 advice.lla_advice = advice_type;
7576 advice.lla_value1 = 0;
7577 advice.lla_value2 = 0;
7578 advice.lla_value3 = 0;
7579 advice.lla_value4 = 0;
7580 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7581 advice.lla_lockahead_mode = mode;
7582 advice.lla_peradvice_flags = flags;
7585 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7588 fprintf(stderr, "%s: cannot give advice '%s' to file "
7589 "'%s': %s\n", argv[0],
7590 ladvise_names[advice_type],
7591 path, strerror(errno));
7597 if (rc == 0 && rc2 < 0)
7603 /** The input string contains a comma delimited list of component ids and
7604 * ranges, for example "1,2-4,7".
7606 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7608 bool end_of_loop = false;
7616 while (!end_of_loop) {
7620 char *endptr = NULL;
7623 ptr = strchrnul(arg, ',');
7624 end_of_loop = *ptr == '\0';
7627 start_index = strtol(arg, &endptr, 0);
7628 if (endptr == arg) /* no data at all */
7630 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7632 if (start_index < 0)
7635 end_index = start_index;
7636 if (*endptr == '-') {
7637 end_index = strtol(endptr + 1, &endptr, 0);
7638 if (*endptr != '\0')
7640 if (end_index < start_index)
7644 for (i = start_index; i <= end_index && size > 0; i++) {
7647 /* remove duplicate */
7648 for (j = 0; j < nr; j++) {
7652 if (j == nr) { /* no duplicate */
7658 if (size == 0 && i < end_index)
7665 if (!end_of_loop && ptr != NULL)
7668 return rc < 0 ? rc : nr;
7672 * struct verify_mirror_id - Mirror id to be verified.
7673 * @mirror_id: A specified mirror id.
7674 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7676 struct verify_mirror_id {
7682 * compare_mirror_ids() - Compare mirror ids.
7683 * @layout: Mirror component list.
7684 * @cbdata: Callback data in verify_mirror_id structure.
7686 * This is a callback function called by llapi_layout_comp_iterate()
7687 * to compare the specified mirror id with the one in the current
7688 * component of @layout. If they are the same, then the specified
7689 * mirror id is valid.
7691 * Return: a negative error code on failure or
7692 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7693 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7696 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7698 struct verify_mirror_id *mirror_id_cbdata =
7699 (struct verify_mirror_id *)cbdata;
7703 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7707 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7708 progname, strerror(errno));
7712 if (mirror_id_cbdata->mirror_id == mirror_id) {
7713 mirror_id_cbdata->is_valid_id = true;
7714 return LLAPI_LAYOUT_ITER_STOP;
7717 return LLAPI_LAYOUT_ITER_CONT;
7721 * verify_mirror_ids() - Verify specified mirror ids.
7722 * @fname: Mirrored file name.
7723 * @mirror_ids: Specified mirror ids to be verified.
7724 * @ids_nr: Number of specified mirror ids.
7726 * This function verifies that specified @mirror_ids are valid
7727 * in the mirrored file @fname.
7729 * Return: 0 on success or a negative error code on failure.
7732 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7734 struct llapi_layout *layout = NULL;
7735 struct verify_mirror_id mirror_id_cbdata = { 0 };
7746 if (stat(fname, &stbuf) < 0) {
7747 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7748 progname, fname, strerror(errno));
7753 if (!S_ISREG(stbuf.st_mode)) {
7754 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7760 fd = open(fname, O_DIRECT | O_RDONLY);
7762 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7763 progname, fname, strerror(errno));
7768 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
7770 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
7771 progname, fname, strerror(errno));
7775 layout = llapi_layout_get_by_fd(fd, 0);
7776 if (layout == NULL) {
7777 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7778 progname, fname, strerror(errno));
7780 llapi_lease_release(fd);
7784 rc = llapi_layout_flags_get(layout, &flr_state);
7786 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7787 progname, fname, strerror(errno));
7792 flr_state &= LCM_FL_FLR_MASK;
7793 switch (flr_state) {
7796 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7797 progname, fname, llapi_layout_flags_string(flr_state));
7804 for (i = 0; i < ids_nr; i++) {
7805 mirror_id_cbdata.mirror_id = mirror_ids[i];
7806 mirror_id_cbdata.is_valid_id = false;
7808 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
7813 "%s: '%s' failed to verify mirror id: %u.\n",
7814 progname, fname, mirror_ids[i]);
7818 if (!mirror_id_cbdata.is_valid_id) {
7821 "%s: '%s' invalid specified mirror id: %u.\n",
7822 progname, fname, mirror_ids[i]);
7828 llapi_layout_free(layout);
7829 llapi_lease_release(fd);
7837 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
7838 __u16 *mirror_ids, int ids_nr)
7840 struct llapi_resync_comp comp_array[1024] = { { 0 } };
7841 struct llapi_layout *layout;
7849 if (stat(fname, &stbuf) < 0) {
7850 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7851 progname, fname, strerror(errno));
7855 if (!S_ISREG(stbuf.st_mode)) {
7856 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7862 fd = open(fname, O_DIRECT | O_RDWR);
7864 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7865 progname, fname, strerror(errno));
7870 layout = llapi_layout_get_by_fd(fd, 0);
7871 if (layout == NULL) {
7872 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7873 progname, fname, strerror(errno));
7878 rc = llapi_layout_flags_get(layout, &flr_state);
7880 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7881 progname, fname, strerror(errno));
7886 flr_state &= LCM_FL_FLR_MASK;
7887 if (flr_state == LCM_FL_NONE) {
7889 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
7894 /* get stale component info */
7895 comp_size = llapi_mirror_find_stale(layout, comp_array,
7896 ARRAY_SIZE(comp_array),
7897 mirror_ids, ids_nr);
7898 if (comp_size <= 0) {
7903 ioc->lil_mode = LL_LEASE_WRLCK;
7904 ioc->lil_flags = LL_LEASE_RESYNC;
7905 rc = llapi_lease_set(fd, ioc);
7907 if (rc == -EALREADY)
7911 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
7912 progname, fname, strerror(errno));
7917 while (idx < comp_size) {
7923 rc = llapi_lease_check(fd);
7924 if (rc != LL_LEASE_WRLCK) {
7925 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7930 mirror_id = comp_array[idx].lrc_mirror_id;
7931 end = comp_array[idx].lrc_end;
7933 /* try to combine adjacent component */
7934 for (i = idx + 1; i < comp_size; i++) {
7935 if (mirror_id != comp_array[i].lrc_mirror_id ||
7936 end != comp_array[i].lrc_start)
7938 end = comp_array[i].lrc_end;
7941 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7942 comp_array[idx].lrc_start,
7945 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7946 "%ld.\n", progname, fname, result);
7949 } else if (result > 0) {
7952 /* mark synced components */
7953 for (j = idx; j < i; j++)
7954 comp_array[j].lrc_synced = true;
7961 /* prepare ioc for lease put */
7962 ioc->lil_mode = LL_LEASE_UNLCK;
7963 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7965 for (idx = 0; idx < comp_size; idx++) {
7966 if (comp_array[idx].lrc_synced) {
7967 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7972 rc = llapi_lease_set(fd, ioc);
7974 if (rc == 0) /* lost lease lock */
7976 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7977 progname, fname, strerror(errno));
7981 * llapi_lease_set returns lease mode when it request to unlock
7987 llapi_layout_free(layout);
7994 static inline int lfs_mirror_resync(int argc, char **argv)
7996 struct ll_ioc_lease *ioc = NULL;
7997 __u16 mirror_ids[128] = { 0 };
8002 struct option long_opts[] = {
8003 { .val = 'o', .name = "only", .has_arg = required_argument },
8006 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8009 rc = parse_mirror_ids(mirror_ids,
8010 sizeof(mirror_ids) / sizeof(__u16),
8014 "%s: bad mirror ids '%s'.\n",
8021 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8022 argv[0], argv[optind - 1]);
8028 if (argc == optind) {
8029 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8034 if (ids_nr > 0 && argc > optind + 1) {
8036 "%s: option '--only' cannot be used upon multiple files.\n",
8044 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8049 /* set the lease on the file */
8050 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8052 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8053 argv[0], strerror(errno));
8058 for (; optind < argc; optind++) {
8059 rc = lfs_mirror_resync_file(argv[optind], ioc,
8060 mirror_ids, ids_nr);
8061 /* ignore previous file's error, continue with next file */
8064 memset(ioc, 0, sizeof(__u32) * 4096);
8073 * struct verify_chunk - Mirror chunk to be verified.
8074 * @chunk: [start, end) of the chunk.
8075 * @mirror_count: Number of mirror ids in @mirror_id array.
8076 * @mirror_id: Array of valid mirror ids that cover the chunk.
8078 struct verify_chunk {
8079 struct lu_extent chunk;
8080 unsigned int mirror_count;
8081 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
8085 * print_chunks() - Print chunk information.
8086 * @fname: Mirrored file name.
8087 * @chunks: Array of chunks.
8088 * @chunk_count: Number of chunks in @chunks array.
8090 * This function prints [start, end) of each chunk in @chunks
8091 * for mirrored file @fname, and also prints the valid mirror ids
8092 * that cover the chunk.
8097 void print_chunks(const char *fname, struct verify_chunk *chunks,
8103 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
8104 for (i = 0; i < chunk_count; i++) {
8105 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
8107 if (chunks[i].mirror_count == 0)
8108 fprintf(stdout, "\t[");
8110 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
8111 for (j = 1; j < chunks[i].mirror_count; j++)
8112 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
8114 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
8116 fprintf(stdout, "\n");
8120 * print_checksums() - Print CRC-32 checksum values.
8121 * @chunk: A chunk and its corresponding valid mirror ids.
8122 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
8124 * This function prints CRC-32 checksum values on @chunk for
8125 * each valid mirror that covers it.
8130 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
8135 "CRC-32 checksum value for chunk "DEXT":\n",
8136 PEXT(&chunk->chunk));
8137 for (i = 0; i < chunk->mirror_count; i++)
8138 fprintf(stdout, "Mirror %u:\t%#lx\n",
8139 chunk->mirror_id[i], crc[i]);
8140 fprintf(stdout, "\n");
8144 * filter_mirror_id() - Filter specified mirror ids.
8145 * @chunks: Array of chunks.
8146 * @chunk_count: Number of chunks in @chunks array.
8147 * @mirror_ids: Specified mirror ids to be verified.
8148 * @ids_nr: Number of specified mirror ids.
8150 * This function scans valid mirror ids that cover each chunk in @chunks
8151 * and filters specified mirror ids.
8156 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
8157 __u16 *mirror_ids, int ids_nr)
8162 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8163 unsigned int valid_count = 0;
8165 for (i = 0; i < chunk_count; i++) {
8166 if (chunks[i].mirror_count == 0)
8170 for (j = 0; j < ids_nr; j++) {
8171 for (k = 0; k < chunks[i].mirror_count; k++) {
8172 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
8173 valid_id[valid_count] = mirror_ids[j];
8180 memcpy(chunks[i].mirror_id, valid_id,
8181 sizeof(__u16) * valid_count);
8182 chunks[i].mirror_count = valid_count;
8187 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
8188 * @layout: Mirror component list.
8189 * @chunks: Array of chunks.
8190 * @chunks_size: Array size of @chunks.
8192 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
8193 * to find out chunk segments and store them in @chunks array.
8195 * The @mirror_id array in each element of @chunks will store the valid
8196 * mirror ids that cover the chunk. If a mirror component covering the
8197 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
8198 * will not be stored into the @mirror_id array, and the chunk for that
8199 * mirror will not be verified.
8201 * The @mirror_count in each element of @chunks will store the number of
8202 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
8203 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
8204 * indicates the chunk is valid in only one mirror. In both cases, the
8205 * chunk will not be verified.
8207 * Here is an example:
8210 * +------+-------------+--------------------+
8212 * +------+------+------+------+-------------+
8213 * | | S | S | | mirror2
8214 * +-------------+------+------+-------------+
8216 * prepared @chunks array will contain 5 elements:
8217 * (([0, 1M), [1, 2], 2),
8218 * ([1M, 2M), [1, 2], 2),
8219 * ([2M, 3M), [1], 1),
8220 * ([3M, 4M], [], 0),
8221 * ([4M, EOF), [2], 1))
8223 * Return: the actual array size of @chunks on success
8224 * or a negative error code on failure.
8227 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
8228 struct verify_chunk *chunks,
8239 memset(chunks, 0, sizeof(*chunks) * chunks_size);
8242 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
8245 "%s: move to the first layout component: %s.\n",
8246 progname, strerror(errno));
8252 chunks[idx].chunk.e_end = LUSTRE_EOF;
8254 rc = llapi_layout_comp_extent_get(layout, &start, &end);
8257 "%s: llapi_layout_comp_extent_get failed: %s.\n",
8258 progname, strerror(errno));
8262 if (start > chunks[idx].chunk.e_start ||
8263 end <= chunks[idx].chunk.e_start)
8266 if (end < chunks[idx].chunk.e_end)
8267 chunks[idx].chunk.e_end = end;
8269 rc = llapi_layout_comp_flags_get(layout, &flags);
8272 "%s: llapi_layout_comp_flags_get failed: %s.\n",
8273 progname, strerror(errno));
8277 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8280 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8283 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8284 progname, strerror(errno));
8288 chunks[idx].mirror_id[i] = mirror_id;
8290 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8292 "%s: mirror_id array is too small.\n",
8299 rc = llapi_layout_comp_use(layout,
8300 LLAPI_LAYOUT_COMP_USE_NEXT);
8303 "%s: move to the next layout component: %s.\n",
8304 progname, strerror(errno));
8307 } /* loop through all components */
8309 chunks[idx].mirror_count = i;
8311 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8315 if (idx >= chunks_size) {
8316 fprintf(stderr, "%s: chunks array is too small.\n",
8322 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
8326 return rc < 0 ? rc : idx + 1;
8330 * lfs_mirror_verify_chunk() - Verify a chunk.
8331 * @fd: File descriptor of the mirrored file.
8332 * @file_size: Size of the mirrored file.
8333 * @chunk: A chunk and its corresponding valid mirror ids.
8334 * @verbose: Verbose mode.
8336 * This function verifies a @chunk contains exactly the same data
8337 * ammong the mirrors that cover it.
8339 * If @verbose is specified, then the function will print where the
8340 * differences are if the data do not match. Otherwise, it will
8341 * just return an error in that case.
8343 * Return: 0 on success or a negative error code on failure.
8346 int lfs_mirror_verify_chunk(int fd, size_t file_size,
8347 struct verify_chunk *chunk, int verbose)
8349 const size_t buflen = 4 * 1024 * 1024; /* 4M */
8351 size_t page_size = sysconf(_SC_PAGESIZE);
8357 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8364 rc = posix_memalign(&buf, page_size, buflen);
8365 if (rc) /* error code is returned directly */
8369 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8370 PEXT(&chunk->chunk));
8371 for (i = 0; i < chunk->mirror_count; i++)
8372 fprintf(stdout, " %u", chunk->mirror_id[i]);
8373 fprintf(stdout, "\n");
8377 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
8378 pos = chunk->chunk.e_start;
8379 while (bytes_done < count) {
8380 /* compute initial CRC-32 checksum */
8381 crc = crc32(0L, Z_NULL, 0);
8382 memset(crc_array, 0, sizeof(crc_array));
8385 for (i = 0; i < chunk->mirror_count; i++) {
8386 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
8388 if (bytes_read < 0) {
8391 "%s: failed to read data from mirror %u: %s.\n",
8392 progname, chunk->mirror_id[i],
8397 /* compute new CRC-32 checksum */
8398 crc_array[i] = crc32(crc, buf, bytes_read);
8402 print_checksums(chunk, crc_array);
8404 /* compare CRC-32 checksum values */
8405 for (i = 1; i < chunk->mirror_count; i++) {
8406 if (crc_array[i] != crc_array[0]) {
8412 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
8413 progname, PEXT(&chunk->chunk),
8414 chunk->mirror_id[0],
8415 chunk->mirror_id[i]);
8420 bytes_done += bytes_read;
8423 if (verbose > 1 && rc == 0) {
8424 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8425 PEXT(&chunk->chunk));
8426 for (i = 0; i < chunk->mirror_count; i++)
8427 fprintf(stdout, " %u", chunk->mirror_id[i]);
8428 fprintf(stdout, " PASS\n\n");
8437 * lfs_mirror_verify_file() - Verify a mirrored file.
8438 * @fname: Mirrored file name.
8439 * @mirror_ids: Specified mirror ids to be verified.
8440 * @ids_nr: Number of specified mirror ids.
8441 * @verbose: Verbose mode.
8443 * This function verifies that each SYNC mirror of a mirrored file
8444 * specified by @fname contains exactly the same data.
8446 * If @mirror_ids is specified, then the function will verify the
8447 * mirrors specified by @mirror_ids contain exactly the same data.
8449 * If @verbose is specified, then the function will print where the
8450 * differences are if the data do not match. Otherwise, it will
8451 * just return an error in that case.
8453 * Return: 0 on success or a negative error code on failure.
8456 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8459 struct verify_chunk chunks_array[1024] = { };
8460 struct llapi_layout *layout = NULL;
8464 int chunk_count = 0;
8470 if (stat(fname, &stbuf) < 0) {
8471 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8472 progname, fname, strerror(errno));
8477 if (!S_ISREG(stbuf.st_mode)) {
8478 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8484 if (stbuf.st_size == 0) {
8486 fprintf(stdout, "%s: '%s' file size is 0.\n",
8492 fd = open(fname, O_DIRECT | O_RDONLY);
8494 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8495 progname, fname, strerror(errno));
8500 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8502 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8503 progname, fname, strerror(errno));
8507 layout = llapi_layout_get_by_fd(fd, 0);
8508 if (layout == NULL) {
8509 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8510 progname, fname, strerror(errno));
8512 llapi_lease_release(fd);
8516 rc = llapi_layout_flags_get(layout, &flr_state);
8518 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8519 progname, fname, strerror(errno));
8524 flr_state &= LCM_FL_FLR_MASK;
8525 switch (flr_state) {
8528 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8529 progname, fname, llapi_layout_flags_string(flr_state));
8535 /* find out mirror chunks to be verified */
8536 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8537 ARRAY_SIZE(chunks_array));
8538 if (chunk_count < 0) {
8544 /* filter specified mirror ids */
8545 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8548 print_chunks(fname, chunks_array, chunk_count);
8550 for (idx = 0; idx < chunk_count; idx++) {
8551 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8554 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8556 PEXT(&chunks_array[idx].chunk),
8557 (unsigned long long)stbuf.st_size);
8561 if (chunks_array[idx].mirror_count == 0) {
8563 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8565 PEXT(&chunks_array[idx].chunk));
8567 fprintf(stderr, "skipped\n");
8571 fprintf(stderr, "failed\n");
8575 if (chunks_array[idx].mirror_count == 1) {
8578 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8580 PEXT(&chunks_array[idx].chunk),
8581 chunks_array[idx].mirror_id[0]);
8585 rc = llapi_lease_check(fd);
8586 if (rc != LL_LEASE_RDLCK) {
8587 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8592 /* verify one chunk */
8593 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8594 &chunks_array[idx], verbose);
8608 llapi_layout_free(layout);
8609 llapi_lease_release(fd);
8617 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
8618 * @argc: The count of lfs mirror verify command line arguments.
8619 * @argv: Array of strings for lfs mirror verify command line arguments.
8621 * This function parses lfs mirror verify command and verifies the
8622 * specified mirrored file(s).
8624 * Return: 0 on success or a negative error code on failure.
8626 static inline int lfs_mirror_verify(int argc, char **argv)
8628 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8636 struct option long_opts[] = {
8637 { .val = 'o', .name = "only", .has_arg = required_argument },
8638 { .val = 'v', .name = "verbose", .has_arg = no_argument },
8641 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8643 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
8646 rc = parse_mirror_ids(mirror_ids,
8647 ARRAY_SIZE(mirror_ids),
8651 "%s: bad mirror ids '%s'.\n",
8658 "%s: at least 2 mirror ids needed with '--only' option.\n",
8668 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8669 progname, argv[optind - 1]);
8675 if (argc == optind) {
8676 fprintf(stderr, "%s: no file name given.\n", progname);
8681 if (ids_nr > 0 && argc > optind + 1) {
8683 "%s: '--only' cannot be used upon multiple files.\n",
8691 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8697 for (; optind < argc; optind++) {
8698 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
8708 * lfs_mirror() - Parse and execute lfs mirror commands.
8709 * @argc: The count of lfs mirror command line arguments.
8710 * @argv: Array of strings for lfs mirror command line arguments.
8712 * This function parses lfs mirror commands and performs the
8713 * corresponding functions specified in mirror_cmdlist[].
8715 * Return: 0 on success or an error code on failure.
8717 static int lfs_mirror(int argc, char **argv)
8724 Parser_init("lfs-mirror > ", mirror_cmdlist);
8726 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8728 program_invocation_short_name = cmd;
8730 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
8732 rc = Parser_commands();
8734 return rc < 0 ? -rc : rc;
8738 * lfs_mirror_list_commands() - List lfs mirror commands.
8739 * @argc: The count of command line arguments.
8740 * @argv: Array of strings for command line arguments.
8742 * This function lists lfs mirror commands defined in mirror_cmdlist[].
8744 * Return: 0 on success.
8746 static int lfs_mirror_list_commands(int argc, char **argv)
8748 char buffer[81] = "";
8750 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
8756 static int lfs_list_commands(int argc, char **argv)
8758 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
8760 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
8765 int main(int argc, char **argv)
8769 /* Ensure that liblustreapi constructor has run */
8770 if (!llapi_liblustreapi_initialized())
8771 fprintf(stderr, "liblustreapi was not properly initialized\n");
8776 Parser_init("lfs > ", cmdlist);
8778 progname = program_invocation_short_name; /* Used in error messages */
8780 llapi_set_command_name(argv[1]);
8781 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
8782 llapi_clear_command_name();
8784 rc = Parser_commands();
8787 return rc < 0 ? -rc : rc;
8790 #ifdef _LUSTRE_IDL_H_
8791 /* Everything we need here should be included by lustreapi.h. */
8792 # error "lfs should not depend on lustre_idl.h"
8793 #endif /* _LUSTRE_IDL_H_ */