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 delete the last component(s) from an existing composite file\n"
312 "(note that this will also delete any data in those components):\n"
313 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
314 " [--component-flags|-F <comp_flags>]\n"
316 "\tcomp_id: Unique component ID to delete\n"
317 "\tcomp_flags: 'init' indicating all instantiated components\n"
318 "\t '^init' indicating all uninstantiated components\n"
319 "\t-I and -F cannot be specified at the same time\n"},
320 {"getstripe", lfs_getstripe, 0,
321 "To list the layout pattern for a given file or files in a\n"
322 "directory or recursively for all files in a directory tree.\n"
323 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
324 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
325 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
326 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
327 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
328 " [--component-id[=comp_id]|-I[comp_id]]\n"
329 " [--component-flags[=comp_flags]]\n"
330 " [--component-count]\n"
331 " [--component-start[=[+-]comp_start]]\n"
332 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
333 " <directory|filename> ..."},
334 {"setdirstripe", lfs_setdirstripe, 0,
335 "To create a striped directory on a specified MDT. This can only\n"
336 "be done on MDT0 with the right of administrator.\n"
337 "usage: setdirstripe [OPTION] <directory>\n"
339 {"getdirstripe", lfs_getdirstripe, 0,
340 "To list the layout pattern info for a given directory\n"
341 "or recursively for all directories in a directory tree.\n"
342 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
343 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
344 " [--recursive|-r] [--yaml|-y]\n"
345 " [--default|-D] <dir> ..."},
346 {"mkdir", lfs_setdirstripe, 0,
347 "To create a striped directory on a specified MDT. This can only\n"
348 "be done on MDT0 with the right of administrator.\n"
349 "usage: mkdir [OPTION] <directory>\n"
351 {"rm_entry", lfs_rmentry, 0,
352 "To remove the name entry of the remote directory. Note: This\n"
353 "command will only delete the name entry, i.e. the remote directory\n"
354 "will become inaccessable after this command. This can only be done\n"
355 "by the administrator\n"
356 "usage: rm_entry <dir>\n"},
357 {"pool_list", lfs_poollist, 0,
358 "List pools or pool OSTs\n"
359 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
360 {"find", lfs_find, 0,
361 "find files matching given attributes recursively in directory tree.\n"
362 "usage: find <directory|filename> ...\n"
363 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
364 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
365 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
366 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
367 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
368 " [[!] --stripe-count|-c [+-]<stripes>]\n"
369 " [[!] --stripe-index|-i <index,...>]\n"
370 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
371 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
372 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
373 " [[!] --projid <projid>]\n"
374 " [[!] --layout|-L released,raid0,mdt]\n"
375 " [[!] --component-count [+-]<comp_cnt>]\n"
376 " [[!] --component-start [+-]N[kMGTPE]]\n"
377 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
378 " [[!] --component-flags <comp_flags>]\n"
379 " [[!] --mirror-count|-N [+-]<n>]\n"
380 " [[!] --mirror-state <[^]state>]\n"
381 " [[!] --mdt-count|-T [+-]<stripes>]\n"
382 " [[!] --mdt-hash|-H <hashtype>\n"
383 "\t !: used before an option indicates 'NOT' requested attribute\n"
384 "\t -: used before a value indicates less than requested value\n"
385 "\t +: used before a value indicates more than requested value\n"
386 "\thashtype: hash type of the striped directory.\n"
387 "\t fnv_1a_64 FNV-1a hash algorithm\n"
388 "\t all_char sum of characters % MDT_COUNT\n"},
389 {"check", lfs_check, 0,
390 "Display the status of MDS or OSTs (as specified in the command)\n"
391 "or all the servers (MDS and OSTs).\n"
392 "usage: check <osts|mds|servers>"},
393 {"osts", lfs_osts, 0, "list OSTs connected to client "
394 "[for specified path only]\n" "usage: osts [path]"},
395 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
396 "[for specified path only]\n" "usage: mdts [path]"},
398 "report filesystem disk space usage or inodes usage"
399 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
400 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
401 {"getname", lfs_getname, 0, "list instances and specified mount points "
402 "[for specified path only]\n"
403 "Usage: getname [-h]|[path ...] "},
404 #ifdef HAVE_SYS_QUOTA_H
405 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
406 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
407 " -b <block-softlimit> -B <block-hardlimit>\n"
408 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
409 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
410 " [--block-softlimit <block-softlimit>]\n"
411 " [--block-hardlimit <block-hardlimit>]\n"
412 " [--inode-softlimit <inode-softlimit>]\n"
413 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
414 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
415 " [--block-grace <block-grace>]\n"
416 " [--inode-grace <inode-grace>] <filesystem>\n"
417 " setquota <-U|-G|-P>\n"
418 " -b <block-softlimit> -B <block-hardlimit>\n"
419 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
420 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
421 " [--block-softlimit <block-softlimit>]\n"
422 " [--block-hardlimit <block-hardlimit>]\n"
423 " [--inode-softlimit <inode-softlimit>]\n"
424 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
425 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
427 " -b can be used instead of --block-softlimit/--block-grace\n"
428 " -B can be used instead of --block-hardlimit\n"
429 " -i can be used instead of --inode-softlimit/--inode-grace\n"
430 " -I can be used instead of --inode-hardlimit\n"
431 " -d can be used instead of --default\n\n"
432 "Note: The total quota space will be split into many qunits and\n"
433 " balanced over all server targets, the minimal qunit size is\n"
434 " 1M bytes for block space and 1K inodes for inode space.\n\n"
435 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
436 " Quota space rebalancing process will stop when this mininum\n"
437 " value is reached. As a result, quota exceeded can be returned\n"
438 " while many targets still have 1MB or 1K inodes of spare\n"
440 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
441 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
443 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
444 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
445 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
446 {"project", lfs_project, 0,
447 "Change or list project attribute for specified file or directory.\n"
448 "usage: project [-d|-r] <file|directory...>\n"
449 " list project ID and flags on file(s) or directories\n"
450 " project [-p id] [-s] [-r] <file|directory...>\n"
451 " set project ID and/or inherit flag for specified file(s) or directories\n"
452 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
453 " check project ID and flags on file(s) or directories, print outliers\n"
454 " project -C [-r] [-k] <file|directory...>\n"
455 " clear the project inherit flag and ID on the file or directory\n"
458 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
459 "usage: flushctx [-k] [mountpoint...]"},
461 "Remote user copy files and directories.\n"
462 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
464 "Remote user list directory contents.\n"
465 "usage: ls [OPTION]... [FILE]..."},
466 {"changelog", lfs_changelog, 0,
467 "Show the metadata changes on an MDT."
468 "\nusage: changelog <mdtname> [startrec [endrec]]"},
469 {"changelog_clear", lfs_changelog_clear, 0,
470 "Indicate that old changelog records up to <endrec> are no longer of "
471 "interest to consumer <id>, allowing the system to free up space.\n"
472 "An <endrec> of 0 means all records.\n"
473 "usage: changelog_clear <mdtname> <id> <endrec>"},
474 {"fid2path", lfs_fid2path, 0,
475 "Resolve the full path(s) for given FID(s). For a specific hardlink "
476 "specify link number <linkno>.\n"
477 /* "For a historical link name, specify changelog record <recno>.\n" */
478 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
479 /* [ --rec <recno> ] */ },
480 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
481 "usage: path2fid [--parents] <path> ..."},
482 {"data_version", lfs_data_version, 0, "Display file data version for "
483 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
484 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
485 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
486 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
487 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
488 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
489 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
491 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
492 "[--archived] [--lost] <file> ..."},
493 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
494 "given files.\n" "usage: hsm_action <file> ..."},
495 {"hsm_archive", lfs_hsm_archive, 0,
496 "Archive file to external storage.\n"
497 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
499 {"hsm_restore", lfs_hsm_restore, 0,
500 "Restore file from external storage.\n"
501 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
502 {"hsm_release", lfs_hsm_release, 0,
503 "Release files from Lustre.\n"
504 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
505 {"hsm_remove", lfs_hsm_remove, 0,
506 "Remove file copy from external storage.\n"
507 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
509 " (FILE [FILE ...] | "
510 "--mntpath MOUNTPATH FID [FID ...])\n"
512 "Note: To remove an archived copy of a file already deleted from a "
514 "--mntpath option and a list of FIDs must be specified"
516 {"hsm_cancel", lfs_hsm_cancel, 0,
517 "Cancel requests related to specified files.\n"
518 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
519 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
520 "usage: swap_layouts <path1> <path2>"},
521 {"migrate", lfs_setstripe_migrate, 0,
522 "migrate a directory between MDTs.\n"
523 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
525 "\tmdt_idx: index of the destination MDT\n"
527 "migrate file objects from one OST "
528 "layout\nto another (may be not safe with concurent writes).\n"
530 "[--stripe-count|-c] <stripe_count>\n"
531 " [--stripe-index|-i] <start_ost_index>\n"
532 " [--stripe-size|-S] <stripe_size>\n"
533 " [--pool|-p] <pool_name>\n"
534 " [--ost|-o] <ost_indices>\n"
536 " [--non-block|-n]\n"
537 " [--non-direct|-D]\n"
538 " <file|directory>\n"
539 "\tstripe_count: number of OSTs to stripe a file over\n"
540 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
541 "\tstripe_size: number of bytes to store before moving to the next OST\n"
542 "\tpool_name: name of the predefined pool of OSTs\n"
543 "\tost_indices: OSTs to stripe over, in order\n"
544 "\tblock: wait for the operation to return before continuing\n"
545 "\tnon-block: do not wait for the operation to return\n"
546 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
548 "To move directories between MDTs. This command is deprecated, "
549 "use \"migrate\" instead.\n"
550 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
552 {"ladvise", lfs_ladvise, 0,
553 "Provide servers with advice about access patterns for a file.\n"
554 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
555 " [--background|-b] [--unset|-u]\n\n"
556 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
557 " {[--mode|-m [READ,WRITE]}\n"
559 {"mirror", lfs_mirror, mirror_cmdlist,
560 "lfs commands used to manage files with mirrored components:\n"
561 "lfs mirror create - create a mirrored file or directory\n"
562 "lfs mirror extend - add mirror(s) to an existing file\n"
563 "lfs mirror split - split a mirror from an existing mirrored file\n"
564 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
565 "lfs mirror verify - verify mirrored file(s)\n"},
566 {"help", Parser_help, 0, "help"},
567 {"exit", Parser_quit, 0, "quit"},
568 {"quit", Parser_quit, 0, "quit"},
569 {"--version", Parser_version, 0,
570 "output build version of the utility and exit"},
571 {"--list-commands", lfs_list_commands, 0,
572 "list commands supported by the utility and exit"},
577 static int check_hashtype(const char *hashtype)
581 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
582 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
589 static const char *error_loc = "syserror";
592 MIGRATION_NONBLOCK = 1 << 0,
593 MIGRATION_MIRROR = 1 << 1,
594 MIGRATION_NONDIRECT = 1 << 2,
597 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
598 struct llapi_layout *layout);
601 migrate_open_files(const char *name, __u64 migration_flags,
602 const struct llapi_stripe_param *param,
603 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
610 char parent[PATH_MAX];
611 char volatile_file[PATH_MAX];
617 if (param == NULL && layout == NULL) {
618 error_loc = "layout information";
622 /* search for file directory pathname */
623 if (strlen(name) > sizeof(parent) - 1) {
624 error_loc = "source file name";
628 strncpy(parent, name, sizeof(parent));
629 ptr = strrchr(parent, '/');
631 if (getcwd(parent, sizeof(parent)) == NULL) {
632 error_loc = "getcwd";
636 if (ptr == parent) /* leading '/' */
641 /* open file, direct io */
642 /* even if the file is only read, WR mode is nedeed to allow
643 * layout swap on fd */
645 if (!(migration_flags & MIGRATION_NONDIRECT))
647 fd = open(name, rflags);
650 error_loc = "cannot open source file";
654 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
656 error_loc = "cannot get MDT index";
661 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
662 mode_t open_mode = S_IRUSR | S_IWUSR;
664 random_value = random();
665 rc = snprintf(volatile_file, sizeof(volatile_file),
666 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
667 mdt_index, random_value);
668 if (rc >= sizeof(volatile_file)) {
673 /* create, open a volatile file, use caching (ie no directio) */
675 fdv = llapi_file_open_param(volatile_file, open_flags,
678 fdv = lfs_component_create(volatile_file, open_flags,
680 } while (fdv < 0 && (rc = fdv) == -EEXIST);
683 error_loc = "cannot create volatile file";
687 /* In case the MDT does not support creation of volatile files
688 * we should try to unlink it. */
689 (void)unlink(volatile_file);
691 /* Not-owner (root?) special case.
692 * Need to set owner/group of volatile file like original.
693 * This will allow to pass related check during layout_swap.
698 error_loc = "cannot stat source file";
702 rc = fstat(fdv, &stv);
705 error_loc = "cannot stat volatile";
709 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
710 rc = fchown(fdv, st.st_uid, st.st_gid);
713 error_loc = "cannot change ownwership of volatile";
732 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
734 struct llapi_layout *layout;
735 size_t buf_size = 4 * 1024 * 1024;
744 layout = llapi_layout_get_by_fd(fd_src, 0);
745 if (layout != NULL) {
746 uint64_t stripe_size;
748 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
750 buf_size = stripe_size;
752 llapi_layout_free(layout);
755 /* Use a page-aligned buffer for direct I/O */
756 rc = posix_memalign(&buf, getpagesize(), buf_size);
761 /* read new data only if we have written all
762 * previously read data */
765 rc = check_file(fd_src);
770 rsize = read(fd_src, buf, buf_size);
782 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
801 static int migrate_copy_timestamps(int fd, int fdv)
805 if (fstat(fd, &st) == 0) {
806 struct timeval tv[2] = {
807 {.tv_sec = st.st_atime},
808 {.tv_sec = st.st_mtime}
811 return futimes(fdv, tv);
817 static int migrate_block(int fd, int fdv)
824 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
826 error_loc = "cannot get dataversion";
834 /* The grouplock blocks all concurrent accesses to the file.
835 * It has to be taken after llapi_get_data_version as it would
837 rc = llapi_group_lock(fd, gid);
839 error_loc = "cannot get group lock";
843 rc = migrate_copy_data(fd, fdv, NULL);
845 error_loc = "data copy failed";
849 /* Make sure we keep original atime/mtime values */
850 rc = migrate_copy_timestamps(fd, fdv);
852 error_loc = "timestamp copy failed";
857 * for a migration we need to check data version on file did
860 * Pass in gid=0 since we already own grouplock. */
861 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
862 SWAP_LAYOUTS_CHECK_DV1);
864 error_loc = "file changed";
867 error_loc = "cannot swap layout";
872 rc2 = llapi_group_unlock(fd, gid);
873 if (rc2 < 0 && rc == 0) {
874 error_loc = "unlock group lock";
882 * Internal helper for migrate_copy_data(). Check lease and report error if
885 * \param[in] fd File descriptor on which to check the lease.
887 * \retval 0 Migration can keep on going.
888 * \retval -errno Error occurred, abort migration.
890 static int check_lease(int fd)
894 rc = llapi_lease_check(fd);
896 return 0; /* llapi_check_lease returns > 0 on success. */
901 static int migrate_nonblock(int fd, int fdv)
907 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
909 error_loc = "cannot get data version";
913 rc = migrate_copy_data(fd, fdv, check_lease);
915 error_loc = "data copy failed";
919 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
921 error_loc = "cannot get data version";
927 error_loc = "source file changed";
931 /* Make sure we keep original atime/mtime values */
932 rc = migrate_copy_timestamps(fd, fdv);
934 error_loc = "timestamp copy failed";
941 static int lfs_component_set(char *fname, int comp_id,
942 __u32 flags, __u32 neg_flags)
945 __u32 flags_array[2];
950 ids[count] = comp_id;
951 flags_array[count] = flags;
956 ids[count] = comp_id;
957 flags_array[count] = neg_flags | LCME_FL_NEG;
961 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
964 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
965 progname, comp_id, fname, flags, neg_flags);
970 static int lfs_component_del(char *fname, __u32 comp_id,
971 __u32 flags, __u32 neg_flags)
975 if (flags && neg_flags)
978 if (!flags && neg_flags)
979 flags = neg_flags | LCME_FL_NEG;
981 if ((flags && comp_id) || (!flags && !comp_id))
984 /* LCME_FL_INIT is the only supported flag in PFL */
986 if (flags & ~LCME_KNOWN_FLAGS) {
988 "%s setstripe: unknown flags %#x\n",
992 } else if (comp_id > LCME_ID_MAX) {
993 fprintf(stderr, "%s setstripe: invalid component id %u\n",
998 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1001 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1002 progname, comp_id, fname, strerror(errno));
1006 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1013 rc = llapi_layout_file_comp_add(fname, layout);
1015 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1016 fname, strerror(errno));
1020 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1021 struct llapi_layout *layout)
1029 fd = lstat(fname, &st);
1030 if (fd == 0 && S_ISDIR(st.st_mode))
1031 open_flags = O_DIRECTORY | O_RDONLY;
1033 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1035 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1036 S_ISDIR(st.st_mode) ?
1037 "set default composite layout for" :
1038 "create composite file",
1039 fname, strerror(errno));
1043 static int lfs_migrate(char *name, __u64 migration_flags,
1044 struct llapi_stripe_param *param,
1045 struct llapi_layout *layout)
1051 rc = migrate_open_files(name, migration_flags, param, layout,
1056 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1057 /* Blocking mode (forced if servers do not support file lease).
1058 * It is also the default mode, since we cannot distinguish
1059 * between a broken lease and a server that does not support
1060 * atomic swap/close (LU-6785) */
1061 rc = migrate_block(fd, fdv);
1065 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1067 error_loc = "cannot get lease";
1071 rc = migrate_nonblock(fd, fdv);
1073 llapi_lease_release(fd);
1077 /* Atomically put lease, swap layouts and close.
1078 * for a migration we need to check data version on file did
1080 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1082 error_loc = "cannot swap layout";
1094 fprintf(stderr, "error: %s: %s: %s: %s\n",
1095 progname, name, error_loc, strerror(-rc));
1099 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1108 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1112 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1113 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1114 const char *comp_name = comp_flags_table[i].cfn_name;
1116 if (strcmp(name, comp_name) == 0) {
1117 *flags |= comp_flag;
1119 } else if (strncmp(name, "^", 1) == 0 &&
1120 strcmp(name + 1, comp_name) == 0) {
1121 *neg_flags |= comp_flag;
1126 llapi_printf(LLAPI_MSG_ERROR,
1127 "%s: component flag '%s' not supported\n",
1133 if (!*flags && !*neg_flags)
1136 /* don't allow to set and exclude the same flag */
1137 if (*flags & *neg_flags)
1143 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1151 if (strncmp(string, "^", 1) == 0) {
1152 *neg_state = llapi_layout_string_flags(string + 1);
1153 if (*neg_state != 0)
1156 *state = llapi_layout_string_flags(string);
1161 llapi_printf(LLAPI_MSG_ERROR,
1162 "%s: mirrored file state '%s' not supported\n",
1168 * struct mirror_args - Command-line arguments for mirror(s).
1169 * @m_count: Number of mirrors to be created with this layout.
1170 * @m_flags: Mirror level flags, only 'prefer' is supported.
1171 * @m_layout: Mirror layout.
1172 * @m_file: A victim file. Its layout will be split and used as a mirror.
1173 * @m_next: Point to the next node of the list.
1175 * Command-line arguments for mirror(s) will be parsed and stored in
1176 * a linked list that consists of this structure.
1178 struct mirror_args {
1181 struct llapi_layout *m_layout;
1183 struct mirror_args *m_next;
1186 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1191 rc = llapi_layout_comp_flags_get(layout, &flags);
1195 if (flags & LCME_FL_NEG) {
1196 fprintf(stderr, "error: %s: negative flags are not supported\n",
1201 if (flags & LCME_FL_STALE) {
1202 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1203 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1207 return LLAPI_LAYOUT_ITER_CONT;
1210 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1212 uint64_t start, end;
1216 /* LU-10112: do not support dom+flr in phase 1 */
1217 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1221 rc = llapi_layout_pattern_get(layout, &pattern);
1225 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1226 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1231 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1235 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1239 if (end != LUSTRE_EOF) {
1240 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1245 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1250 * enum mirror_flags - Flags for extending a mirrored file.
1251 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1252 * in case the victim file(s) contains the same data as the
1253 * original mirrored file.
1254 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1256 * Flags for extending a mirrored file.
1264 * mirror_create_sanity_check() - Check mirror list.
1265 * @list: A linked list that stores the mirror arguments.
1267 * This function does a sanity check on @list for creating
1270 * Return: 0 on success or a negative error code on failure.
1272 static int mirror_create_sanity_check(const char *fname,
1273 struct mirror_args *list)
1276 bool has_m_file = false;
1277 bool has_m_layout = false;
1283 struct llapi_layout *layout;
1285 layout = llapi_layout_get_by_path(fname, 0);
1288 "error: %s: file '%s' couldn't get layout\n",
1293 rc = mirror_sanity_check_one(layout);
1294 llapi_layout_free(layout);
1300 while (list != NULL) {
1301 if (list->m_file != NULL) {
1303 llapi_layout_free(list->m_layout);
1306 llapi_layout_get_by_path(list->m_file, 0);
1307 if (list->m_layout == NULL) {
1309 "error: %s: file '%s' has no layout\n",
1310 progname, list->m_file);
1314 has_m_layout = true;
1315 if (list->m_layout == NULL) {
1316 fprintf(stderr, "error: %s: no mirror layout\n",
1322 rc = mirror_sanity_check_one(list->m_layout);
1326 list = list->m_next;
1329 if (has_m_file && has_m_layout) {
1331 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1339 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1341 __u32 mirror_flags = *(__u32 *)cbdata;
1345 rc = llapi_layout_comp_flags_get(layout, &flags);
1350 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1355 return LLAPI_LAYOUT_ITER_CONT;
1359 * mirror_create() - Create a mirrored file.
1360 * @fname: The file to be created.
1361 * @mirror_list: A linked list that stores the mirror arguments.
1363 * This function creates a mirrored file @fname with the mirror(s)
1364 * from @mirror_list.
1366 * Return: 0 on success or a negative error code on failure.
1368 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1370 struct llapi_layout *layout = NULL;
1371 struct mirror_args *cur_mirror = NULL;
1372 uint16_t mirror_count = 0;
1376 rc = mirror_create_sanity_check(NULL, mirror_list);
1380 cur_mirror = mirror_list;
1381 while (cur_mirror != NULL) {
1382 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1384 &cur_mirror->m_flags);
1387 fprintf(stderr, "%s: failed to set mirror flags\n",
1392 for (i = 0; i < cur_mirror->m_count; i++) {
1393 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1396 fprintf(stderr, "error: %s: "
1397 "merge layout failed: %s\n",
1398 progname, strerror(errno));
1402 mirror_count += cur_mirror->m_count;
1403 cur_mirror = cur_mirror->m_next;
1406 if (layout == NULL) {
1407 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1411 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1414 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1415 progname, strerror(errno));
1419 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1427 llapi_layout_free(layout);
1432 * Compare files and check lease on @fd.
1434 * \retval bytes number of bytes are the same
1436 static ssize_t mirror_file_compare(int fd, int fdv)
1438 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1440 ssize_t bytes_done = 0;
1441 ssize_t bytes_read = 0;
1443 buf = malloc(buflen * 2);
1448 if (!llapi_lease_check(fd)) {
1449 bytes_done = -EBUSY;
1453 bytes_read = read(fd, buf, buflen);
1454 if (bytes_read <= 0)
1457 if (bytes_read != read(fdv, buf + buflen, buflen))
1460 /* XXX: should compute the checksum on each buffer and then
1461 * compare checksum to avoid cache collision */
1462 if (memcmp(buf, buf + buflen, bytes_read))
1465 bytes_done += bytes_read;
1473 static int mirror_extend_file(const char *fname, const char *victim_file,
1474 enum mirror_flags mirror_flags)
1479 struct stat stbuf_v;
1480 struct ll_ioc_lease *data = NULL;
1483 fd = open(fname, O_RDWR);
1485 error_loc = "open source file";
1490 fdv = open(victim_file, O_RDWR);
1492 error_loc = "open target file";
1497 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1498 error_loc = "stat source or target file";
1503 if (stbuf.st_dev != stbuf_v.st_dev) {
1504 error_loc = "stat source and target file";
1509 /* mirrors should be of the same size */
1510 if (stbuf.st_size != stbuf_v.st_size) {
1511 error_loc = "file sizes don't match";
1516 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1518 error_loc = "cannot get lease";
1522 if (!(mirror_flags & MF_NO_VERIFY)) {
1524 /* mirrors should have the same contents */
1525 ret = mirror_file_compare(fd, fdv);
1526 if (ret != stbuf.st_size) {
1527 error_loc = "file busy or contents don't match";
1528 rc = ret < 0 ? ret : -EINVAL;
1533 /* Get rid of caching pages from clients */
1534 rc = llapi_file_flush(fd);
1536 error_loc = "cannot get data version";
1540 rc = llapi_file_flush(fdv);
1542 error_loc = "cannot get data version";
1547 /* Make sure we keep original atime/mtime values */
1548 rc = migrate_copy_timestamps(fd, fdv);
1550 error_loc = "cannot copy timestamp";
1554 /* Atomically put lease, merge layouts and close. */
1555 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1557 error_loc = "memory allocation";
1560 data->lil_mode = LL_LEASE_UNLCK;
1561 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1562 data->lil_count = 1;
1563 data->lil_ids[0] = fdv;
1564 rc = llapi_lease_set(fd, data);
1566 error_loc = "cannot merge layout";
1568 } else if (rc == 0) {
1570 error_loc = "lost lease lock";
1583 (void) unlink(victim_file);
1585 fprintf(stderr, "error: %s: %s: %s: %s\n",
1586 progname, fname, error_loc, strerror(-rc));
1590 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1592 struct ll_ioc_lease *data = NULL;
1597 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1601 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1603 error_loc = "cannot get lease";
1607 rc = migrate_nonblock(fd, fdv);
1609 llapi_lease_release(fd);
1613 /* Atomically put lease, merge layouts and close. */
1614 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1616 error_loc = "memory allocation";
1619 data->lil_mode = LL_LEASE_UNLCK;
1620 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1621 data->lil_count = 1;
1622 data->lil_ids[0] = fdv;
1623 rc = llapi_lease_set(fd, data);
1625 error_loc = "cannot merge layout";
1627 } else if (rc == 0) {
1629 error_loc = "lost lease lock";
1642 fprintf(stderr, "error: %s: %s: %s: %s\n",
1643 progname, name, error_loc, strerror(-rc));
1647 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1648 enum mirror_flags mirror_flags)
1652 rc = mirror_create_sanity_check(fname, mirror_list);
1656 while (mirror_list) {
1657 if (mirror_list->m_file != NULL) {
1658 rc = mirror_extend_file(fname, mirror_list->m_file,
1661 __u32 mirror_count = mirror_list->m_count;
1663 while (mirror_count > 0) {
1664 rc = mirror_extend_layout(fname,
1665 mirror_list->m_layout);
1675 mirror_list = mirror_list->m_next;
1681 static int verify_id(struct llapi_layout *layout, void *cbdata)
1686 rc = llapi_layout_mirror_id_get(layout, &id);
1690 if ((__u16)id == *(__u16 *)cbdata)
1691 return LLAPI_LAYOUT_ITER_STOP;
1693 return LLAPI_LAYOUT_ITER_CONT;
1696 static int mirror_split(const char *fname, __u16 mirror_id,
1697 enum mirror_flags mflags, const char *victim_file)
1699 struct llapi_layout *layout;
1700 char parent[PATH_MAX];
1701 char victim[PATH_MAX];
1702 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1704 struct ll_ioc_lease *data;
1705 uint16_t mirror_count;
1710 /* check fname contains mirror with mirror_id */
1711 layout = llapi_layout_get_by_path(fname, 0);
1714 "error %s: file '%s' couldn't get layout\n",
1719 rc = mirror_sanity_check_one(layout);
1723 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1726 "error %s: file '%s' couldn't get mirror count\n",
1730 if (mirror_count < 2) {
1732 "error %s: file '%s' has %d component, cannot split\n",
1733 progname, fname, mirror_count);
1737 rc = llapi_layout_comp_iterate(layout, verify_id, &mirror_id);
1739 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1742 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1744 "error %s: file '%s' does not contain mirror with id %u\n",
1745 progname, fname, mirror_id);
1749 fd = open(fname, O_RDWR);
1752 "error %s: open file '%s' failed: %s\n",
1753 progname, fname, strerror(errno));
1757 /* get victim file directory pathname */
1758 if (strlen(fname) > sizeof(parent) - 1) {
1759 fprintf(stderr, "error %s: file name of '%s' too long\n",
1764 strncpy(parent, fname, sizeof(parent));
1765 ptr = strrchr(parent, '/');
1767 if (getcwd(parent, sizeof(parent)) == NULL) {
1768 fprintf(stderr, "error %s: getcwd failed: %s\n",
1769 progname, strerror(errno));
1779 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1781 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1786 if (victim_file == NULL) {
1787 /* use a temp file to store the splitted layout */
1788 if (mflags & MF_DESTROY) {
1789 fdv = llapi_create_volatile_idx(parent, mdt_index,
1790 O_LOV_DELAY_CREATE);
1792 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1794 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1797 /* user specified victim file */
1798 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1803 "error %s: create victim file failed: %s\n",
1804 progname, strerror(errno));
1808 /* get lease lock of fname */
1809 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1812 "error %s: cannot get lease of file '%s': %d\n",
1813 progname, fname, rc);
1817 /* Atomatically put lease, split layouts and close. */
1818 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1824 data->lil_mode = LL_LEASE_UNLCK;
1825 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1826 data->lil_count = 2;
1827 data->lil_ids[0] = fdv;
1828 data->lil_ids[1] = mirror_id;
1829 rc = llapi_lease_set(fd, data);
1831 if (rc == 0) /* lost lease lock */
1834 "error %s: cannot split '%s': %s\n",
1835 progname, fname, strerror(-rc));
1846 llapi_layout_free(layout);
1851 * Parse a string containing an target index list into an array of integers.
1853 * The input string contains a comma delimited list of individual
1854 * indices and ranges, for example "1,2-4,7". Add the indices into the
1855 * \a tgts array and remove duplicates.
1857 * \param[out] tgts array to store indices in
1858 * \param[in] size size of \a tgts array
1859 * \param[in] offset starting index in \a tgts
1860 * \param[in] arg string containing OST index list
1862 * \retval positive number of indices in \a tgts
1863 * \retval -EINVAL unable to parse \a arg
1865 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1869 int slots = size - offset;
1876 end_of_loop = false;
1877 while (!end_of_loop) {
1881 char *endptr = NULL;
1885 ptr = strchrnul(arg, ',');
1887 end_of_loop = *ptr == '\0';
1890 start_index = strtol(arg, &endptr, 0);
1891 if (endptr == arg) /* no data at all */
1893 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1896 end_index = start_index;
1897 if (*endptr == '-') {
1898 end_index = strtol(endptr + 1, &endptr, 0);
1899 if (*endptr != '\0')
1901 if (end_index < start_index)
1905 for (i = start_index; i <= end_index && slots > 0; i++) {
1908 /* remove duplicate */
1909 for (j = 0; j < offset; j++) {
1913 if (j == offset) { /* no duplicate */
1918 if (slots == 0 && i < end_index)
1926 if (!end_of_loop && ptr != NULL)
1929 return rc < 0 ? rc : nr;
1932 struct lfs_setstripe_args {
1933 unsigned long long lsa_comp_end;
1934 unsigned long long lsa_stripe_size;
1935 long long lsa_stripe_count;
1936 long long lsa_stripe_off;
1937 __u32 lsa_comp_flags;
1938 __u32 lsa_comp_neg_flags;
1939 unsigned long long lsa_pattern;
1940 unsigned int lsa_mirror_count;
1942 bool lsa_first_comp;
1944 char *lsa_pool_name;
1947 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1949 unsigned int mirror_count = lsa->lsa_mirror_count;
1950 bool first_comp = lsa->lsa_first_comp;
1952 memset(lsa, 0, sizeof(*lsa));
1954 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1955 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1956 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1957 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1958 lsa->lsa_pool_name = NULL;
1960 lsa->lsa_mirror_count = mirror_count;
1961 lsa->lsa_first_comp = first_comp;
1965 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1966 * @lsa: Stripe options to be initialized and inherited.
1968 * This function initializes stripe options in @lsa and inherit
1969 * stripe_size, stripe_count and OST pool_name options.
1973 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1975 unsigned long long stripe_size;
1976 long long stripe_count;
1977 char *pool_name = NULL;
1979 stripe_size = lsa->lsa_stripe_size;
1980 stripe_count = lsa->lsa_stripe_count;
1981 pool_name = lsa->lsa_pool_name;
1983 setstripe_args_init(lsa);
1985 lsa->lsa_stripe_size = stripe_size;
1986 lsa->lsa_stripe_count = stripe_count;
1987 lsa->lsa_pool_name = pool_name;
1990 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1992 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1993 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1994 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1995 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1996 lsa->lsa_pool_name != NULL ||
1997 lsa->lsa_comp_end != 0);
2001 * comp_args_to_layout() - Create or extend a composite layout.
2002 * @composite: Pointer to the composite layout.
2003 * @lsa: Stripe options for the new component.
2005 * This function creates or extends a composite layout by adding a new
2006 * component with stripe options from @lsa.
2008 * Return: 0 on success or an error code on failure.
2010 static int comp_args_to_layout(struct llapi_layout **composite,
2011 struct lfs_setstripe_args *lsa,
2014 struct llapi_layout *layout = *composite;
2015 uint64_t prev_end = 0;
2018 if (layout == NULL) {
2019 layout = llapi_layout_alloc();
2020 if (layout == NULL) {
2021 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2025 *composite = layout;
2029 /* Get current component extent, current component
2030 * must be the tail component. */
2031 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2033 fprintf(stderr, "Get comp extent failed. %s\n",
2038 if (lsa->lsa_first_comp)
2041 if (lsa->lsa_first_comp)
2042 rc = llapi_layout_add_first_comp(layout);
2044 rc = llapi_layout_comp_add(layout);
2046 fprintf(stderr, "Add component failed. %s\n",
2051 /* reset lsa_first_comp */
2052 lsa->lsa_first_comp = false;
2055 rc = llapi_layout_comp_extent_set(layout, prev_end,
2058 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2059 prev_end, lsa->lsa_comp_end, strerror(errno));
2064 /* Data-on-MDT component setting */
2065 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2066 /* In case of Data-on-MDT patterns the only extra option
2067 * applicable is stripe size option. */
2068 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2069 fprintf(stderr, "Option 'stripe-count' can't be "
2070 "specified with Data-on-MDT component: %lld\n",
2071 lsa->lsa_stripe_count);
2074 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2075 fprintf(stderr, "Option 'stripe-size' can't be "
2076 "specified with Data-on-MDT component: %llu\n",
2077 lsa->lsa_stripe_size);
2080 if (lsa->lsa_nr_tgts != 0) {
2081 fprintf(stderr, "Option 'ost-list' can't be specified "
2082 "with Data-on-MDT component: '%i'\n",
2086 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2087 fprintf(stderr, "Option 'stripe-offset' can't be "
2088 "specified with Data-on-MDT component: %lld\n",
2089 lsa->lsa_stripe_off);
2092 if (lsa->lsa_pool_name != 0) {
2093 fprintf(stderr, "Option 'pool' can't be specified "
2094 "with Data-on-MDT component: '%s'\n",
2095 lsa->lsa_pool_name);
2099 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2101 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2102 lsa->lsa_pattern, strerror(errno));
2105 /* Data-on-MDT component has always single stripe up to end */
2106 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2109 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2111 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2112 lsa->lsa_stripe_size, strerror(errno));
2116 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2118 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2119 lsa->lsa_stripe_count, strerror(errno));
2123 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2125 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2126 lsa->lsa_comp_flags, strerror(errno));
2130 if (lsa->lsa_pool_name != NULL) {
2131 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2133 fprintf(stderr, "Set pool name: %s failed. %s\n",
2134 lsa->lsa_pool_name, strerror(errno));
2138 rc = llapi_layout_pool_name_set(layout, "");
2140 fprintf(stderr, "Clear pool name failed: %s\n",
2146 if (lsa->lsa_nr_tgts > 0) {
2147 if (lsa->lsa_stripe_count > 0 &&
2148 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2149 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2150 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2151 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2152 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2155 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2156 rc = llapi_layout_ost_index_set(layout, i,
2161 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2162 lsa->lsa_stripe_off != -1) {
2163 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2166 fprintf(stderr, "Set ost index %d failed. %s\n",
2167 i, strerror(errno));
2174 static int build_component(struct llapi_layout **layout,
2175 struct lfs_setstripe_args *lsa, bool set_extent)
2179 rc = comp_args_to_layout(layout, lsa, set_extent);
2183 if (lsa->lsa_mirror_count > 0) {
2184 rc = llapi_layout_mirror_count_set(*layout,
2185 lsa->lsa_mirror_count);
2189 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2192 lsa->lsa_mirror_count = 0;
2198 static int build_layout_from_yaml_node(struct cYAML *node,
2199 struct llapi_layout **layout,
2200 struct lfs_setstripe_args *lsa,
2207 string = node->cy_string;
2208 /* skip leading lmm_ if present, to simplify parsing */
2209 if (string != NULL && strncmp(string, "lmm_", 4) == 0)
2212 if (node->cy_type == CYAML_TYPE_STRING) {
2213 if (!strcmp(string, "lcme_extent.e_end")) {
2214 if (!strcmp(node->cy_valuestring, "EOF") ||
2215 !strcmp(node->cy_valuestring, "eof"))
2216 lsa->lsa_comp_end = LUSTRE_EOF;
2217 } else if (!strcmp(string, "pool")) {
2218 lsa->lsa_pool_name = node->cy_valuestring;
2219 } else if (!strcmp(string, "pattern")) {
2220 if (!strcmp(node->cy_valuestring, "mdt"))
2221 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2223 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2224 if (!strcmp(string, "lcm_mirror_count")) {
2225 lsa->lsa_mirror_count = node->cy_valueint;
2226 } else if (!strcmp(string, "lcme_extent.e_start")) {
2227 if (node->cy_valueint != 0 || *layout != NULL) {
2228 rc = build_component(layout, lsa, true);
2233 if (node->cy_valueint == 0)
2234 lsa->lsa_first_comp = true;
2236 /* initialize lsa */
2237 setstripe_args_init(lsa);
2238 lsa->lsa_tgts = osts;
2239 } else if (!strcmp(string, "lcme_extent.e_end")) {
2240 if (node->cy_valueint == -1)
2241 lsa->lsa_comp_end = LUSTRE_EOF;
2243 lsa->lsa_comp_end = node->cy_valueint;
2244 } else if (!strcmp(string, "stripe_count")) {
2245 lsa->lsa_stripe_count = node->cy_valueint;
2246 } else if (!strcmp(string, "stripe_size")) {
2247 lsa->lsa_stripe_size = node->cy_valueint;
2248 } else if (!strcmp(string, "stripe_offset")) {
2249 lsa->lsa_stripe_off = node->cy_valueint;
2250 } else if (!strcmp(string, "l_ost_idx")) {
2251 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2254 } else if (node->cy_type == CYAML_TYPE_OBJECT) {
2255 /* go deep to sub blocks */
2256 rc = build_layout_from_yaml_node(node->cy_child, layout,
2261 node = node->cy_next;
2267 static int lfs_comp_create_from_yaml(char *template,
2268 struct llapi_layout **layout,
2269 struct lfs_setstripe_args *lsa,
2272 struct cYAML *tree = NULL, *err_rc = NULL;
2275 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2277 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2278 progname, template);
2279 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2280 "can't parse", &err_rc);
2281 cYAML_print_tree2file(stderr, err_rc);
2282 cYAML_free_tree(err_rc);
2287 /* initialize lsa for plain file */
2288 setstripe_args_init(lsa);
2289 lsa->lsa_tgts = osts;
2291 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2293 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2294 progname, template);
2297 rc = build_component(layout, lsa, *layout != NULL);
2299 /* clean clean lsa */
2300 setstripe_args_init(lsa);
2304 cYAML_free_tree(tree);
2308 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2309 * end of the last component in the existing file, and adjust the
2310 * first extent start of the components to be added accordingly. */
2311 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2313 struct llapi_layout *head;
2314 uint64_t start, end, stripe_size, prev_end = 0;
2317 if (layout == NULL) {
2319 "%s setstripe: layout must be specified\n",
2325 head = llapi_layout_get_by_path(fname, 0);
2328 "%s setstripe: cannot read layout from '%s': %s\n",
2329 progname, fname, strerror(errno));
2331 } else if (errno == ENODATA) {
2332 /* file without LOVEA, this component-add will be turned
2333 * into a component-create. */
2334 llapi_layout_free(head);
2336 } else if (!llapi_layout_is_composite(head)) {
2337 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2339 llapi_layout_free(head);
2343 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2345 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2346 progname, strerror(errno));
2347 llapi_layout_free(head);
2351 llapi_layout_free(head);
2353 /* Make sure we use the first component of the layout to be added. */
2354 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2357 "%s setstripe: cannot move component cursor: %s\n",
2358 progname, strerror(errno));
2362 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2364 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2365 progname, strerror(errno));
2369 if (start > prev_end || end <= prev_end) {
2371 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2372 progname, start, end, prev_end);
2376 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2378 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2379 progname, strerror(errno));
2383 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2384 (prev_end & (stripe_size - 1))) {
2386 "%s setstripe: stripe size %lu not aligned with %lu\n",
2387 progname, stripe_size, prev_end);
2391 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2394 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2395 progname, prev_end, end, strerror(errno));
2402 static inline bool arg_is_eof(char *arg)
2404 return !strncmp(arg, "-1", strlen("-1")) ||
2405 !strncmp(arg, "EOF", strlen("EOF")) ||
2406 !strncmp(arg, "eof", strlen("eof"));
2410 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2412 * Return: Valid mirror_args pointer on success and
2413 * NULL if memory allocation fails.
2415 static struct mirror_args *lfs_mirror_alloc(void)
2417 struct mirror_args *mirror = NULL;
2420 mirror = calloc(1, sizeof(*mirror));
2431 * lfs_mirror_free() - Free memory allocated for a mirror argument
2433 * @mirror: Previously allocated mirror argument structure by
2434 * lfs_mirror_alloc().
2436 * Free memory allocated for @mirror.
2440 static void lfs_mirror_free(struct mirror_args *mirror)
2442 if (mirror->m_layout != NULL)
2443 llapi_layout_free(mirror->m_layout);
2448 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2449 * @mirror_list: Previously allocated mirror list.
2451 * Free memory allocated for @mirror_list.
2455 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2457 struct mirror_args *next_mirror = NULL;
2459 while (mirror_list != NULL) {
2460 next_mirror = mirror_list->m_next;
2461 lfs_mirror_free(mirror_list);
2462 mirror_list = next_mirror;
2474 LFS_COMP_NO_VERIFY_OPT,
2476 LFS_MIRROR_FLAGS_OPT,
2478 LFS_MIRROR_STATE_OPT,
2483 static int lfs_setstripe_internal(int argc, char **argv,
2484 enum setstripe_origin opc)
2486 struct lfs_setstripe_args lsa = { 0 };
2487 struct llapi_stripe_param *param = NULL;
2488 struct find_param migrate_mdt_param = {
2498 char *mdt_idx_arg = NULL;
2499 unsigned long long size_units = 1;
2500 bool migrate_mode = false;
2501 bool migration_block = false;
2502 __u64 migration_flags = 0;
2503 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2504 int comp_del = 0, comp_set = 0;
2507 struct llapi_layout *layout = NULL;
2508 struct llapi_layout **lpp = &layout;
2509 bool mirror_mode = false;
2510 bool has_m_file = false;
2511 __u32 mirror_count = 0;
2512 enum mirror_flags mirror_flags = 0;
2513 struct mirror_args *mirror_list = NULL;
2514 struct mirror_args *new_mirror = NULL;
2515 struct mirror_args *last_mirror = NULL;
2516 __u16 mirror_id = 0;
2518 bool from_yaml = false;
2519 bool from_copy = false;
2520 char *template = NULL;
2522 struct option long_opts[] = {
2523 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2524 /* --block is only valid in migrate mode */
2525 { .val = 'b', .name = "block", .has_arg = no_argument },
2526 { .val = LFS_COMP_ADD_OPT,
2527 .name = "comp-add", .has_arg = no_argument },
2528 { .val = LFS_COMP_ADD_OPT,
2529 .name = "component-add", .has_arg = no_argument },
2530 { .val = LFS_COMP_DEL_OPT,
2531 .name = "comp-del", .has_arg = no_argument },
2532 { .val = LFS_COMP_DEL_OPT,
2533 .name = "component-del", .has_arg = no_argument },
2534 { .val = LFS_COMP_FLAGS_OPT,
2535 .name = "comp-flags", .has_arg = required_argument },
2536 { .val = LFS_COMP_FLAGS_OPT,
2537 .name = "component-flags",
2538 .has_arg = required_argument },
2539 { .val = LFS_COMP_SET_OPT,
2540 .name = "comp-set", .has_arg = no_argument },
2541 { .val = LFS_COMP_SET_OPT,
2542 .name = "component-set",
2543 .has_arg = no_argument},
2544 { .val = LFS_COMP_NO_VERIFY_OPT,
2545 .name = "no-verify", .has_arg = no_argument},
2546 { .val = LFS_MIRROR_FLAGS_OPT,
2547 .name = "flags", .has_arg = required_argument},
2548 { .val = LFS_MIRROR_ID_OPT,
2549 .name = "mirror-id", .has_arg = required_argument},
2550 { .val = LFS_LAYOUT_COPY,
2551 .name = "copy", .has_arg = required_argument},
2552 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2553 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2554 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2555 { .val = 'd', .name = "delete", .has_arg = no_argument},
2556 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2557 /* --non-direct is only valid in migrate mode */
2558 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2559 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2560 { .val = 'E', .name = "component-end",
2561 .has_arg = required_argument},
2562 { .val = 'f', .name = "file", .has_arg = required_argument },
2563 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2564 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2565 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2566 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
2567 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2568 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2569 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2570 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2571 { .val = 'L', .name = "layout", .has_arg = required_argument },
2572 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2573 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2574 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2575 /* --non-block is only valid in migrate mode */
2576 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2577 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2578 { .val = 'o', .name = "ost", .has_arg = required_argument },
2579 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2580 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2581 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2583 { .val = 'p', .name = "pool", .has_arg = required_argument },
2584 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2585 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2586 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2587 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2588 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2589 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2590 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2591 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2592 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2593 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2594 /* --verbose is only valid in migrate mode */
2595 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2596 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2599 setstripe_args_init(&lsa);
2601 migrate_mode = (opc == SO_MIGRATE);
2602 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2604 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2606 while ((c = getopt_long(argc, argv, "bc:dDE:f:i:I:m:N::no:p:L:s:S:vy:",
2607 long_opts, NULL)) >= 0) {
2612 case LFS_COMP_ADD_OPT:
2615 case LFS_COMP_DEL_OPT:
2618 case LFS_COMP_FLAGS_OPT:
2619 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2620 &lsa.lsa_comp_neg_flags);
2623 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2624 fprintf(stderr, "%s: inverted flags are not supported\n",
2628 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2630 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2637 case LFS_COMP_SET_OPT:
2640 case LFS_COMP_NO_VERIFY_OPT:
2641 mirror_flags |= MF_NO_VERIFY;
2643 case LFS_MIRROR_ID_OPT:
2644 mirror_id = strtoul(optarg, &end, 0);
2645 if (*end != '\0' || mirror_id == 0) {
2647 "%s %s: invalid mirror ID '%s'\n",
2648 progname, argv[0], optarg);
2652 case LFS_MIRROR_FLAGS_OPT: {
2655 if (!mirror_mode || !last_mirror) {
2656 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2661 result = comp_str2flags(optarg, &last_mirror->m_flags,
2667 fprintf(stderr, "%s: inverted flags are not supported\n",
2672 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2674 "%s: unsupported mirror flags: %s\n",
2681 case LFS_LAYOUT_COPY:
2686 if (!migrate_mode) {
2688 "%s %s: -b|--block valid only for migrate command\n",
2692 migration_block = true;
2695 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2698 "%s %s: invalid stripe count '%s'\n",
2699 progname, argv[0], optarg);
2703 if (lsa.lsa_stripe_count == -1)
2704 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2707 /* delete the default striping pattern */
2709 if (opc == SO_MIRROR_SPLIT) {
2712 "%s %s: -d cannot used with -f\n",
2716 mirror_flags |= MF_DESTROY;
2720 if (!migrate_mode) {
2722 "%s %s: -D|--non-direct is valid "
2723 "only for migrate command\n",
2727 migration_flags |= MIGRATION_NONDIRECT;
2730 if (lsa.lsa_comp_end != 0) {
2731 result = comp_args_to_layout(lpp, &lsa, true);
2734 "%s %s: invalid layout\n",
2739 setstripe_args_init_inherit(&lsa);
2742 if (arg_is_eof(optarg)) {
2743 lsa.lsa_comp_end = LUSTRE_EOF;
2745 result = llapi_parse_size(optarg,
2750 "%s %s: invalid component end '%s'\n",
2751 progname, argv[0], optarg);
2757 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2760 "%s %s: invalid stripe offset '%s'\n",
2761 progname, argv[0], optarg);
2764 if (lsa.lsa_stripe_off == -1)
2765 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2768 comp_id = strtoul(optarg, &end, 0);
2769 if (*end != '\0' || comp_id == 0 ||
2770 comp_id > LCME_ID_MAX) {
2772 "%s %s: invalid component ID '%s'\n",
2773 progname, argv[0], optarg);
2778 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2780 "error: %s: invalid option: %s\n",
2781 progname, argv[optopt + 1]);
2784 if (opc == SO_MIRROR_EXTEND) {
2785 if (last_mirror == NULL) {
2787 "error: %s: '-N' must exist in front of '%s'\n",
2788 progname, argv[optopt + 1]);
2791 last_mirror->m_file = optarg;
2792 last_mirror->m_count = 1;
2795 if (mirror_list == NULL)
2796 mirror_list = lfs_mirror_alloc();
2797 mirror_list->m_file = optarg;
2802 if (strcmp(argv[optind - 1], "mdt") == 0) {
2803 /* Can be only the first component */
2804 if (layout != NULL) {
2806 fprintf(stderr, "error: 'mdt' layout "
2807 "can be only the first one\n");
2810 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2812 fprintf(stderr, "error: 'mdt' layout "
2813 "size is too big\n");
2816 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2817 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2819 fprintf(stderr, "error: layout '%s' is "
2820 "unknown, supported layouts are: "
2821 "'mdt', 'raid0'\n", argv[optind]);
2826 if (!migrate_mode) {
2828 "%s %s: -m|--mdt-index valid only for migrate command\n",
2832 mdt_idx_arg = optarg;
2835 if (!migrate_mode) {
2837 "%s %s: -n|--non-block valid only for migrate command\n",
2841 migration_flags |= MIGRATION_NONBLOCK;
2844 if (opc == SO_SETSTRIPE) {
2845 opc = SO_MIRROR_CREATE;
2849 if (optarg != NULL) {
2850 mirror_count = strtoul(optarg, &end, 0);
2851 if (*end != '\0' || mirror_count == 0) {
2853 "error: %s: bad mirror count: %s\n",
2860 new_mirror = lfs_mirror_alloc();
2861 new_mirror->m_count = mirror_count;
2863 if (mirror_list == NULL)
2864 mirror_list = new_mirror;
2866 if (last_mirror != NULL) {
2867 /* wrap up last mirror */
2868 if (lsa.lsa_comp_end == 0)
2869 lsa.lsa_comp_end = LUSTRE_EOF;
2871 result = comp_args_to_layout(lpp, &lsa, true);
2873 lfs_mirror_free(new_mirror);
2877 setstripe_args_init_inherit(&lsa);
2879 last_mirror->m_next = new_mirror;
2882 last_mirror = new_mirror;
2883 lpp = &last_mirror->m_layout;
2886 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2887 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2888 fprintf(stderr, "warning: '--ost-list' is "
2889 "deprecated, use '--ost' instead\n");
2891 lsa.lsa_nr_tgts = parse_targets(osts,
2892 sizeof(osts) / sizeof(__u32),
2893 lsa.lsa_nr_tgts, optarg);
2894 if (lsa.lsa_nr_tgts < 0) {
2896 "%s %s: invalid OST target(s) '%s'\n",
2897 progname, argv[0], optarg);
2901 lsa.lsa_tgts = osts;
2902 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2903 lsa.lsa_stripe_off = osts[0];
2908 lsa.lsa_pool_name = optarg;
2910 if (strlen(lsa.lsa_pool_name) == 0 ||
2911 strncmp(lsa.lsa_pool_name, "none",
2912 LOV_MAXPOOLNAME) == 0)
2913 lsa.lsa_pool_name = NULL;
2916 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2920 "%s %s: invalid stripe size '%s'\n",
2921 progname, argv[0], optarg);
2926 if (!migrate_mode) {
2928 "%s %s: -v|--verbose valid only for migrate command\n",
2932 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2939 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2940 progname, argv[0], argv[optind - 1]);
2945 fname = argv[optind];
2947 if (optind == argc) {
2948 fprintf(stderr, "%s %s: FILE must be specified\n",
2953 if (mirror_mode && mirror_count == 0) {
2955 "error: %s: --mirror-count|-N option is required\n",
2962 if (lsa.lsa_comp_end == 0)
2963 lsa.lsa_comp_end = LUSTRE_EOF;
2966 if (lsa.lsa_comp_end != 0) {
2967 result = comp_args_to_layout(lpp, &lsa, true);
2972 if (mirror_flags & MF_NO_VERIFY) {
2973 if (opc != SO_MIRROR_EXTEND) {
2975 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2979 } else if (!has_m_file) {
2981 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2988 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2989 * altered by user space tool, so we don't need to support the
2990 * --component-set for this moment. */
2991 if (comp_set && !comp_id) {
2992 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2997 if ((delete + comp_set + comp_del + comp_add) > 1) {
2999 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3004 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3005 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3007 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3012 if ((comp_set || comp_del) &&
3013 (setstripe_args_specified(&lsa) || layout != NULL)) {
3015 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3020 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3022 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3027 if (comp_add || comp_del) {
3030 result = lstat(fname, &st);
3031 if (result == 0 && S_ISDIR(st.st_mode)) {
3033 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3039 fprintf(stderr, "error: %s: can't use --component-add "
3040 "or --component-del for mirror operation\n",
3047 if (layout == NULL) {
3049 "%s %s: option -E must be specified with --component-add\n",
3054 result = adjust_first_extent(fname, layout);
3055 if (result == -ENODATA)
3057 else if (result != 0)
3061 if (from_yaml && from_copy) {
3063 "%s: can't specify --yaml and --copy together\n",
3068 if ((from_yaml || from_copy) &&
3069 (setstripe_args_specified(&lsa) || layout != NULL)) {
3070 fprintf(stderr, "error: %s: can't specify --yaml with "
3071 "-c, -S, -i, -o, -p or -E options.\n",
3076 if (mdt_idx_arg != NULL && optind > 3) {
3078 "%s %s: option -m cannot be used with other options\n",
3083 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3085 "%s %s: options --non-block and --block are mutually exclusive\n",
3090 if (!comp_del && !comp_set && comp_id != 0) {
3092 "%s %s: option -I can only be used with --component-del\n",
3097 if (mdt_idx_arg != NULL) {
3098 /* initialize migrate mdt parameters */
3099 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
3101 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
3102 progname, argv[0], mdt_idx_arg);
3105 migrate_mdt_param.fp_migrate = 1;
3106 } else if (layout == NULL) {
3107 /* initialize stripe parameters */
3108 param = calloc(1, offsetof(typeof(*param),
3109 lsp_osts[lsa.lsa_nr_tgts]));
3110 if (param == NULL) {
3112 "%s %s: cannot allocate memory for parameters: %s\n",
3113 progname, argv[0], strerror(ENOMEM));
3118 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3119 param->lsp_stripe_size = lsa.lsa_stripe_size;
3120 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3121 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3122 param->lsp_stripe_count = -1;
3124 param->lsp_stripe_count = lsa.lsa_stripe_count;
3126 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3127 param->lsp_stripe_offset = -1;
3129 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3130 param->lsp_pool = lsa.lsa_pool_name;
3131 param->lsp_is_specific = false;
3132 if (lsa.lsa_nr_tgts > 0) {
3133 if (lsa.lsa_stripe_count > 0 &&
3134 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3135 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3136 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3137 fprintf(stderr, "error: %s: stripe count %lld "
3138 "doesn't match the number of OSTs: %d\n"
3139 , argv[0], lsa.lsa_stripe_count,
3145 param->lsp_is_specific = true;
3146 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3147 memcpy(param->lsp_osts, osts,
3148 sizeof(*osts) * lsa.lsa_nr_tgts);
3153 /* generate a layout from a YAML template */
3154 result = lfs_comp_create_from_yaml(template, &layout,
3157 fprintf(stderr, "error: %s: can't create composite "
3158 "layout from template file %s\n",
3162 } else if (from_copy) {
3163 layout = llapi_layout_get_by_path(template, 0);
3164 if (layout == NULL) {
3166 "%s: can't create composite layout from file %s.\n",
3167 progname, template);
3172 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3173 if (mdt_idx_arg != NULL) {
3174 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3175 } else if (migrate_mode) {
3176 result = lfs_migrate(fname, migration_flags, param,
3178 } else if (comp_set != 0) {
3179 result = lfs_component_set(fname, comp_id,
3181 lsa.lsa_comp_neg_flags);
3182 } else if (comp_del != 0) {
3183 result = lfs_component_del(fname, comp_id,
3185 lsa.lsa_comp_neg_flags);
3186 } else if (comp_add != 0) {
3187 result = lfs_component_add(fname, layout);
3188 } else if (opc == SO_MIRROR_CREATE) {
3189 result = mirror_create(fname, mirror_list);
3190 } else if (opc == SO_MIRROR_EXTEND) {
3191 result = mirror_extend(fname, mirror_list,
3193 } else if (opc == SO_MIRROR_SPLIT) {
3194 if (mirror_id == 0) {
3196 "%s %s: no mirror id is specified\n",
3200 result = mirror_split(fname, mirror_id, mirror_flags,
3201 has_m_file ? mirror_list->m_file :
3203 } else if (layout != NULL) {
3204 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3211 result = llapi_file_open_param(fname,
3220 /* Save the first error encountered. */
3228 llapi_layout_free(layout);
3229 lfs_mirror_list_free(mirror_list);
3234 llapi_layout_free(layout);
3235 lfs_mirror_list_free(mirror_list);
3239 static int lfs_poollist(int argc, char **argv)
3244 return llapi_poollist(argv[1]);
3247 static int set_time(time_t *time, time_t *set, char *str)
3255 else if (str[0] == '-')
3261 t = strtol(str, &endptr, 0);
3262 if (*endptr != '\0') {
3264 "%s find: bad time '%s': %s\n",
3265 progname, str, strerror(EINVAL));
3268 if (*time < t * 24 * 60 * 60) {
3272 "%s find: bad time '%s': too large\n",
3277 *set = *time - t * 24 * 60 * 60;
3281 static int name2uid(unsigned int *id, const char *name)
3283 struct passwd *passwd;
3285 passwd = getpwnam(name);
3288 *id = passwd->pw_uid;
3293 static int name2gid(unsigned int *id, const char *name)
3295 struct group *group;
3297 group = getgrnam(name);
3300 *id = group->gr_gid;
3305 static inline int name2projid(unsigned int *id, const char *name)
3310 static int uid2name(char **name, unsigned int id)
3312 struct passwd *passwd;
3314 passwd = getpwuid(id);
3317 *name = passwd->pw_name;
3322 static inline int gid2name(char **name, unsigned int id)
3324 struct group *group;
3326 group = getgrgid(id);
3329 *name = group->gr_name;
3334 static int name2layout(__u32 *layout, char *name)
3336 char *ptr, *layout_name;
3339 for (ptr = name; ; ptr = NULL) {
3340 layout_name = strtok(ptr, ",");
3341 if (layout_name == NULL)
3343 if (strcmp(layout_name, "released") == 0)
3344 *layout |= LOV_PATTERN_F_RELEASED;
3345 else if (strcmp(layout_name, "raid0") == 0)
3346 *layout |= LOV_PATTERN_RAID0;
3347 else if (strcmp(layout_name, "mdt") == 0)
3348 *layout |= LOV_PATTERN_MDT;
3355 static int lfs_find(int argc, char **argv)
3360 struct find_param param = {
3364 struct option long_opts[] = {
3365 { .val = 'A', .name = "atime", .has_arg = required_argument },
3366 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3367 { .val = LFS_COMP_COUNT_OPT,
3368 .name = "comp-count", .has_arg = required_argument },
3369 { .val = LFS_COMP_COUNT_OPT,
3370 .name = "component-count",
3371 .has_arg = required_argument },
3372 { .val = LFS_COMP_FLAGS_OPT,
3373 .name = "comp-flags", .has_arg = required_argument },
3374 { .val = LFS_COMP_FLAGS_OPT,
3375 .name = "component-flags",
3376 .has_arg = required_argument },
3377 { .val = LFS_COMP_START_OPT,
3378 .name = "comp-start", .has_arg = required_argument },
3379 { .val = LFS_COMP_START_OPT,
3380 .name = "component-start",
3381 .has_arg = required_argument },
3382 { .val = LFS_MIRROR_STATE_OPT,
3383 .name = "mirror-state", .has_arg = required_argument },
3384 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3385 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3386 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3387 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3388 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3389 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3390 { .val = 'E', .name = "component-end",
3391 .has_arg = required_argument },
3392 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3393 { .val = 'g', .name = "gid", .has_arg = required_argument },
3394 { .val = 'G', .name = "group", .has_arg = required_argument },
3395 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3396 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3397 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3398 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3399 { .val = 'L', .name = "layout", .has_arg = required_argument },
3400 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3401 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3402 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3403 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3404 { .val = 'n', .name = "name", .has_arg = required_argument },
3405 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3406 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3407 { .val = 'O', .name = "obd", .has_arg = required_argument },
3408 { .val = 'O', .name = "ost", .has_arg = required_argument },
3409 /* no short option for pool yet, can be 'p' after 2.18 */
3410 { .val = LFS_POOL_OPT,
3411 .name = "pool", .has_arg = required_argument },
3412 { .val = '0', .name = "print0", .has_arg = no_argument },
3413 { .val = 'P', .name = "print", .has_arg = no_argument },
3414 { .val = LFS_PROJID_OPT,
3415 .name = "projid", .has_arg = required_argument },
3416 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3417 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3418 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3419 { .val = 's', .name = "size", .has_arg = required_argument },
3420 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3421 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3422 { .val = 't', .name = "type", .has_arg = required_argument },
3423 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3424 { .val = 'u', .name = "uid", .has_arg = required_argument },
3425 { .val = 'U', .name = "user", .has_arg = required_argument },
3426 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3427 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3439 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3440 while ((c = getopt_long_only(argc, argv,
3441 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3442 long_opts, NULL)) >= 0) {
3447 /* '!' is part of option */
3448 /* when getopt_long_only() finds a string which is not
3449 * an option nor a known option argument it returns 1
3450 * in that case if we already have found pathstart and pathend
3451 * (i.e. we have the list of pathnames),
3452 * the only supported value is "!"
3454 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3455 if (!isoption && pathend != -1) {
3456 fprintf(stderr, "err: %s: filename|dirname must either "
3457 "precede options or follow options\n",
3462 if (!isoption && pathstart == -1)
3463 pathstart = optind - 1;
3464 if (isoption && pathstart != -1 && pathend == -1)
3465 pathend = optind - 2;
3471 /* unknown; opt is "!" or path component,
3472 * checking done above.
3474 if (strcmp(optarg, "!") == 0)
3478 xtime = ¶m.fp_atime;
3479 xsign = ¶m.fp_asign;
3480 param.fp_exclude_atime = !!neg_opt;
3481 /* no break, this falls through to 'C' for ctime */
3484 xtime = ¶m.fp_ctime;
3485 xsign = ¶m.fp_csign;
3486 param.fp_exclude_ctime = !!neg_opt;
3488 /* no break, this falls through to 'M' for mtime */
3491 xtime = ¶m.fp_mtime;
3492 xsign = ¶m.fp_msign;
3493 param.fp_exclude_mtime = !!neg_opt;
3495 rc = set_time(&t, xtime, optarg);
3496 if (rc == INT_MAX) {
3504 if (optarg[0] == '+') {
3505 param.fp_blocks_sign = -1;
3507 } else if (optarg[0] == '-') {
3508 param.fp_blocks_sign = 1;
3512 param.fp_blocks_units = 1024;
3513 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3514 ¶m.fp_blocks_units, 0);
3516 fprintf(stderr, "error: bad blocks '%s'\n",
3520 param.fp_check_blocks = 1;
3521 param.fp_exclude_blocks = !!neg_opt;
3523 case LFS_COMP_COUNT_OPT:
3524 if (optarg[0] == '+') {
3525 param.fp_comp_count_sign = -1;
3527 } else if (optarg[0] == '-') {
3528 param.fp_comp_count_sign = 1;
3532 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3533 if (*endptr != '\0') {
3534 fprintf(stderr, "error: bad component count "
3538 param.fp_check_comp_count = 1;
3539 param.fp_exclude_comp_count = !!neg_opt;
3541 case LFS_COMP_FLAGS_OPT:
3542 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3543 ¶m.fp_comp_neg_flags);
3545 fprintf(stderr, "error: bad component flags "
3549 param.fp_check_comp_flags = 1;
3551 __u32 flags = param.fp_comp_neg_flags;
3552 param.fp_comp_neg_flags = param.fp_comp_flags;
3553 param.fp_comp_flags = flags;
3556 case LFS_COMP_START_OPT:
3557 if (optarg[0] == '+') {
3558 param.fp_comp_start_sign = -1;
3560 } else if (optarg[0] == '-') {
3561 param.fp_comp_start_sign = 1;
3565 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3566 ¶m.fp_comp_start_units, 0);
3568 fprintf(stderr, "error: bad component start "
3572 param.fp_check_comp_start = 1;
3573 param.fp_exclude_comp_start = !!neg_opt;
3575 case LFS_MIRROR_STATE_OPT:
3576 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3577 ¶m.fp_mirror_neg_state);
3580 "error: bad mirrored file state '%s'\n",
3584 param.fp_check_mirror_state = 1;
3586 __u16 state = param.fp_mirror_neg_state;
3587 param.fp_mirror_neg_state =
3588 param.fp_mirror_state;
3589 param.fp_mirror_state = state;
3593 if (optarg[0] == '+') {
3594 param.fp_stripe_count_sign = -1;
3596 } else if (optarg[0] == '-') {
3597 param.fp_stripe_count_sign = 1;
3601 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3602 if (*endptr != '\0') {
3603 fprintf(stderr,"error: bad stripe_count '%s'\n",
3608 param.fp_check_stripe_count = 1;
3609 param.fp_exclude_stripe_count = !!neg_opt;
3612 param.fp_max_depth = strtol(optarg, 0, 0);
3615 if (optarg[0] == '+') {
3616 param.fp_comp_end_sign = -1;
3618 } else if (optarg[0] == '-') {
3619 param.fp_comp_end_sign = 1;
3623 if (arg_is_eof(optarg)) {
3624 param.fp_comp_end = LUSTRE_EOF;
3625 param.fp_comp_end_units = 1;
3628 rc = llapi_parse_size(optarg,
3630 ¶m.fp_comp_end_units, 0);
3633 fprintf(stderr, "error: bad component end "
3637 param.fp_check_comp_end = 1;
3638 param.fp_exclude_comp_end = !!neg_opt;
3642 rc = name2gid(¶m.fp_gid, optarg);
3644 param.fp_gid = strtoul(optarg, &endptr, 10);
3645 if (*endptr != '\0') {
3646 fprintf(stderr, "Group/GID: %s cannot "
3647 "be found.\n", optarg);
3652 param.fp_exclude_gid = !!neg_opt;
3653 param.fp_check_gid = 1;
3656 param.fp_hash_type = check_hashtype(optarg);
3657 if (param.fp_hash_type == 0) {
3658 fprintf(stderr, "error: bad hash_type '%s'\n",
3663 param.fp_check_hash_type = 1;
3664 param.fp_exclude_hash_type = !!neg_opt;
3667 ret = name2layout(¶m.fp_layout, optarg);
3670 param.fp_exclude_layout = !!neg_opt;
3671 param.fp_check_layout = 1;
3675 rc = name2uid(¶m.fp_uid, optarg);
3677 param.fp_uid = strtoul(optarg, &endptr, 10);
3678 if (*endptr != '\0') {
3679 fprintf(stderr, "User/UID: %s cannot "
3680 "be found.\n", optarg);
3685 param.fp_exclude_uid = !!neg_opt;
3686 param.fp_check_uid = 1;
3689 param.fp_pattern = (char *)optarg;
3690 param.fp_exclude_pattern = !!neg_opt;
3693 if (optarg[0] == '+') {
3694 param.fp_mirror_count_sign = -1;
3696 } else if (optarg[0] == '-') {
3697 param.fp_mirror_count_sign = 1;
3701 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3702 if (*endptr != '\0') {
3704 "error: bad mirror count '%s'\n",
3708 param.fp_check_mirror_count = 1;
3709 param.fp_exclude_mirror_count = !!neg_opt;
3714 char *buf, *token, *next, *p;
3718 buf = strdup(optarg);
3724 param.fp_exclude_obd = !!neg_opt;
3727 while (token && *token) {
3728 token = strchr(token, ',');
3735 param.fp_exclude_mdt = !!neg_opt;
3736 param.fp_num_alloc_mdts += len;
3737 tmp = realloc(param.fp_mdt_uuid,
3738 param.fp_num_alloc_mdts *
3739 sizeof(*param.fp_mdt_uuid));
3745 param.fp_mdt_uuid = tmp;
3747 param.fp_exclude_obd = !!neg_opt;
3748 param.fp_num_alloc_obds += len;
3749 tmp = realloc(param.fp_obd_uuid,
3750 param.fp_num_alloc_obds *
3751 sizeof(*param.fp_obd_uuid));
3757 param.fp_obd_uuid = tmp;
3759 for (token = buf; token && *token; token = next) {
3760 struct obd_uuid *puuid;
3763 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3766 ¶m.fp_obd_uuid[param.fp_num_obds++];
3768 p = strchr(token, ',');
3775 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3780 strncpy(puuid->uuid, token,
3781 sizeof(puuid->uuid));
3788 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3792 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3794 "Pool name %s is too long (max %d)\n",
3795 optarg, LOV_MAXPOOLNAME);
3800 * We do check for empty pool because empty pool
3801 * is used to find V1 LOV attributes
3803 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3804 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3805 param.fp_exclude_pool = !!neg_opt;
3806 param.fp_check_pool = 1;
3808 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3809 case 'p': /* want this for --pool, to match getstripe/find */
3811 "warning: -p deprecated, use --print0 or -0\n");
3814 param.fp_zero_end = 1;
3816 case 'P': /* we always print, this option is a no-op */
3818 case LFS_PROJID_OPT:
3819 rc = name2projid(¶m.fp_projid, optarg);
3821 param.fp_projid = strtoul(optarg, &endptr, 10);
3822 if (*endptr != '\0') {
3824 "Invalid project ID: %s",
3830 param.fp_exclude_projid = !!neg_opt;
3831 param.fp_check_projid = 1;
3834 if (optarg[0] == '+') {
3835 param.fp_size_sign = -1;
3837 } else if (optarg[0] == '-') {
3838 param.fp_size_sign = 1;
3842 ret = llapi_parse_size(optarg, ¶m.fp_size,
3843 ¶m.fp_size_units, 0);
3845 fprintf(stderr, "error: bad file size '%s'\n",
3849 param.fp_check_size = 1;
3850 param.fp_exclude_size = !!neg_opt;
3853 if (optarg[0] == '+') {
3854 param.fp_stripe_size_sign = -1;
3856 } else if (optarg[0] == '-') {
3857 param.fp_stripe_size_sign = 1;
3861 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3862 ¶m.fp_stripe_size_units, 0);
3864 fprintf(stderr, "error: bad stripe_size '%s'\n",
3868 param.fp_check_stripe_size = 1;
3869 param.fp_exclude_stripe_size = !!neg_opt;
3872 param.fp_exclude_type = !!neg_opt;
3873 switch (optarg[0]) {
3875 param.fp_type = S_IFBLK;
3878 param.fp_type = S_IFCHR;
3881 param.fp_type = S_IFDIR;
3884 param.fp_type = S_IFREG;
3887 param.fp_type = S_IFLNK;
3890 param.fp_type = S_IFIFO;
3893 param.fp_type = S_IFSOCK;
3896 fprintf(stderr, "error: %s: bad type '%s'\n",
3903 if (optarg[0] == '+') {
3904 param.fp_mdt_count_sign = -1;
3906 } else if (optarg[0] == '-') {
3907 param.fp_mdt_count_sign = 1;
3911 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3912 if (*endptr != '\0') {
3913 fprintf(stderr, "error: bad mdt_count '%s'\n",
3918 param.fp_check_mdt_count = 1;
3919 param.fp_exclude_mdt_count = !!neg_opt;
3927 if (pathstart == -1) {
3928 fprintf(stderr, "error: %s: no filename|pathname\n",
3932 } else if (pathend == -1) {
3938 rc = llapi_find(argv[pathstart], ¶m);
3939 if (rc != 0 && ret == 0)
3941 } while (++pathstart < pathend);
3944 fprintf(stderr, "error: %s failed for %s.\n",
3945 argv[0], argv[optind - 1]);
3947 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3948 free(param.fp_obd_uuid);
3950 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3951 free(param.fp_mdt_uuid);
3956 static int lfs_getstripe_internal(int argc, char **argv,
3957 struct find_param *param)
3959 struct option long_opts[] = {
3960 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3961 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
3962 { .val = LFS_COMP_COUNT_OPT,
3963 .name = "comp-count", .has_arg = no_argument },
3964 { .val = LFS_COMP_COUNT_OPT,
3965 .name = "component-count", .has_arg = no_argument },
3966 { .val = LFS_COMP_FLAGS_OPT,
3967 .name = "comp-flags", .has_arg = optional_argument },
3968 { .val = LFS_COMP_FLAGS_OPT,
3969 .name = "component-flags", .has_arg = optional_argument },
3970 { .val = LFS_COMP_START_OPT,
3971 .name = "comp-start", .has_arg = optional_argument },
3972 { .val = LFS_COMP_START_OPT,
3973 .name = "component-start", .has_arg = optional_argument },
3974 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3975 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3976 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
3977 { .val = 'd', .name = "directory", .has_arg = no_argument },
3978 { .val = 'D', .name = "default", .has_arg = no_argument },
3979 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3980 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
3981 { .val = 'F', .name = "fid", .has_arg = no_argument },
3982 { .val = 'g', .name = "generation", .has_arg = no_argument },
3983 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3984 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
3985 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3986 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3987 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3988 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3989 { .val = 'L', .name = "layout", .has_arg = no_argument },
3990 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3991 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3992 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3993 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
3994 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
3995 { .val = 'O', .name = "obd", .has_arg = required_argument },
3996 { .val = 'O', .name = "ost", .has_arg = required_argument },
3997 { .val = 'p', .name = "pool", .has_arg = no_argument },
3998 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3999 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4000 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4001 { .val = 'R', .name = "raw", .has_arg = no_argument },
4002 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4003 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4004 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4005 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4006 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4007 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4008 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4009 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4014 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
4015 long_opts, NULL)) != -1) {
4018 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4019 param->fp_verbose |= VERBOSE_COUNT;
4020 param->fp_max_depth = 0;
4023 case LFS_COMP_COUNT_OPT:
4024 param->fp_verbose |= VERBOSE_COMP_COUNT;
4025 param->fp_max_depth = 0;
4027 case LFS_COMP_FLAGS_OPT:
4028 if (optarg != NULL) {
4029 rc = comp_str2flags(optarg,
4030 ¶m->fp_comp_flags,
4031 ¶m->fp_comp_neg_flags);
4033 fprintf(stderr, "error: %s bad "
4034 "component flags '%s'.\n",
4038 param->fp_check_comp_flags = 1;
4040 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4041 param->fp_max_depth = 0;
4044 case LFS_COMP_START_OPT:
4045 if (optarg != NULL) {
4047 if (tmp[0] == '+') {
4048 param->fp_comp_start_sign = -1;
4050 } else if (tmp[0] == '-') {
4051 param->fp_comp_start_sign = 1;
4054 rc = llapi_parse_size(tmp,
4055 ¶m->fp_comp_start,
4056 ¶m->fp_comp_start_units, 0);
4058 fprintf(stderr, "error: %s bad "
4059 "component start '%s'.\n",
4063 param->fp_check_comp_start = 1;
4066 param->fp_verbose |= VERBOSE_COMP_START;
4067 param->fp_max_depth = 0;
4071 param->fp_max_depth = 0;
4074 param->fp_get_default_lmv = 1;
4077 if (optarg != NULL) {
4079 if (tmp[0] == '+') {
4080 param->fp_comp_end_sign = -1;
4082 } else if (tmp[0] == '-') {
4083 param->fp_comp_end_sign = 1;
4087 if (arg_is_eof(tmp)) {
4088 param->fp_comp_end = LUSTRE_EOF;
4089 param->fp_comp_end_units = 1;
4092 rc = llapi_parse_size(tmp,
4093 ¶m->fp_comp_end,
4094 ¶m->fp_comp_end_units, 0);
4097 fprintf(stderr, "error: %s bad "
4098 "component end '%s'.\n",
4102 param->fp_check_comp_end = 1;
4104 param->fp_verbose |= VERBOSE_COMP_END;
4105 param->fp_max_depth = 0;
4109 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4110 param->fp_verbose |= VERBOSE_DFID;
4111 param->fp_max_depth = 0;
4115 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4116 param->fp_verbose |= VERBOSE_GENERATION;
4117 param->fp_max_depth = 0;
4121 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4122 param->fp_verbose |= VERBOSE_OFFSET;
4123 param->fp_max_depth = 0;
4127 if (optarg != NULL) {
4128 param->fp_comp_id = strtoul(optarg, &end, 0);
4129 if (*end != '\0' || param->fp_comp_id == 0 ||
4130 param->fp_comp_id > LCME_ID_MAX) {
4131 fprintf(stderr, "error: %s bad "
4132 "component id '%s'\n",
4136 param->fp_check_comp_id = 1;
4139 param->fp_max_depth = 0;
4140 param->fp_verbose |= VERBOSE_COMP_ID;
4144 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4145 param->fp_verbose |= VERBOSE_LAYOUT;
4146 param->fp_max_depth = 0;
4149 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4151 fprintf(stderr, "warning: '-M' deprecated"
4152 ", use '--mdt-index' or '-m' instead\n");
4155 if (!(param->fp_verbose & VERBOSE_DETAIL))
4156 param->fp_max_depth = 0;
4157 param->fp_verbose |= VERBOSE_MDTINDEX;
4160 if (param->fp_obd_uuid) {
4162 "error: %s: only one obduuid allowed",
4166 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4169 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4170 param->fp_verbose |= VERBOSE_POOL;
4171 param->fp_max_depth = 0;
4178 param->fp_recursive = 1;
4184 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4185 param->fp_verbose |= VERBOSE_SIZE;
4186 param->fp_max_depth = 0;
4190 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4203 if (param->fp_recursive)
4204 param->fp_max_depth = -1;
4205 else if (param->fp_verbose & VERBOSE_DETAIL)
4206 param->fp_max_depth = 1;
4208 if (!param->fp_verbose)
4209 param->fp_verbose = VERBOSE_DEFAULT;
4210 if (param->fp_quiet)
4211 param->fp_verbose = VERBOSE_OBJID;
4214 rc = llapi_getstripe(argv[optind], param);
4215 } while (++optind < argc && !rc);
4218 fprintf(stderr, "error: %s failed for %s.\n",
4219 argv[0], argv[optind - 1]);
4223 static int lfs_tgts(int argc, char **argv)
4225 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4226 struct find_param param;
4227 int index = 0, rc=0;
4232 if (argc == 2 && !realpath(argv[1], path)) {
4234 fprintf(stderr, "error: invalid path '%s': %s\n",
4235 argv[1], strerror(-rc));
4239 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4240 /* Check if we have a mount point */
4241 if (mntdir[0] == '\0')
4244 memset(¶m, 0, sizeof(param));
4245 if (!strcmp(argv[0], "mdts"))
4246 param.fp_get_lmv = 1;
4248 rc = llapi_ostlist(mntdir, ¶m);
4250 fprintf(stderr, "error: %s: failed on %s\n",
4253 if (path[0] != '\0')
4255 memset(mntdir, 0, PATH_MAX);
4261 static int lfs_getstripe(int argc, char **argv)
4263 struct find_param param = { 0 };
4265 param.fp_max_depth = 1;
4266 return lfs_getstripe_internal(argc, argv, ¶m);
4270 static int lfs_getdirstripe(int argc, char **argv)
4272 struct find_param param = { 0 };
4273 struct option long_opts[] = {
4274 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4275 { .val = 'D', .name = "default", .has_arg = no_argument },
4276 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4277 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4278 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4279 { .val = 'O', .name = "obd", .has_arg = required_argument },
4280 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4281 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4282 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4286 param.fp_get_lmv = 1;
4288 while ((c = getopt_long(argc, argv,
4289 "cDHimO:rtTy", long_opts, NULL)) != -1)
4294 param.fp_verbose |= VERBOSE_COUNT;
4297 param.fp_get_default_lmv = 1;
4301 param.fp_verbose |= VERBOSE_OFFSET;
4303 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4305 fprintf(stderr, "warning: '-t' deprecated, "
4306 "use '--mdt-hash' or '-H' instead\n");
4309 param.fp_verbose |= VERBOSE_HASH_TYPE;
4312 if (param.fp_obd_uuid) {
4314 "error: %s: only one obduuid allowed",
4318 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4321 param.fp_recursive = 1;
4334 if (param.fp_recursive)
4335 param.fp_max_depth = -1;
4337 if (!param.fp_verbose)
4338 param.fp_verbose = VERBOSE_DEFAULT;
4341 rc = llapi_getstripe(argv[optind], ¶m);
4342 } while (++optind < argc && !rc);
4345 fprintf(stderr, "error: %s failed for %s.\n",
4346 argv[0], argv[optind - 1]);
4351 MNTDF_INODES = 0x0001,
4352 MNTDF_COOKED = 0x0002,
4353 MNTDF_LAZY = 0x0004,
4354 MNTDF_VERBOSE = 0x0008,
4355 MNTDF_SHOW = 0x0010,
4358 #define COOK(value) \
4361 while (value > 1024) { \
4369 #define CDF "%11llu"
4370 #define HDF "%8.1f%c"
4374 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4376 double avail, used, ratio = 0;
4378 avail = st->os_bavail;
4379 used = st->os_blocks - st->os_bfree;
4380 if (avail + used > 0)
4381 ratio = used / (used + avail) * 100 + 0.5;
4386 static int showdf(char *mntdir, struct obd_statfs *stat,
4387 char *uuid, enum mntdf_flags flags,
4388 char *type, int index, int rc)
4390 long long avail, used, total;
4392 char *suffix = "KMGTPEZY";
4393 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4394 char tbuf[3 * sizeof(__u64)];
4395 char ubuf[3 * sizeof(__u64)];
4396 char abuf[3 * sizeof(__u64)];
4397 char rbuf[3 * sizeof(__u64)];
4404 if (flags & MNTDF_INODES) {
4405 avail = stat->os_ffree;
4406 used = stat->os_files - stat->os_ffree;
4407 total = stat->os_files;
4409 int shift = flags & MNTDF_COOKED ? 0 : 10;
4411 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4412 used = ((stat->os_blocks - stat->os_bfree) *
4413 stat->os_bsize) >> shift;
4414 total = (stat->os_blocks * stat->os_bsize) >> shift;
4417 ratio = obd_statfs_ratio(stat);
4419 if (flags & MNTDF_COOKED) {
4423 cook_val = (double)total;
4426 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4429 snprintf(tbuf, sizeof(tbuf), CDF, total);
4431 cook_val = (double)used;
4434 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4437 snprintf(ubuf, sizeof(ubuf), CDF, used);
4439 cook_val = (double)avail;
4442 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4445 snprintf(abuf, sizeof(abuf), CDF, avail);
4447 snprintf(tbuf, sizeof(tbuf), CDF, total);
4448 snprintf(ubuf, sizeof(tbuf), CDF, used);
4449 snprintf(abuf, sizeof(tbuf), CDF, avail);
4452 sprintf(rbuf, RDF, ratio);
4453 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4454 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4456 printf("[%s:%d]", type, index);
4458 if (stat->os_state) {
4460 * Each character represents the matching
4463 const char state_names[] = "DRSI";
4468 for (i = 0, state = stat->os_state;
4469 state && i < sizeof(state_names); i++) {
4470 if (!(state & (1 << i)))
4472 printf("%c", state_names[i]);
4480 printf(UUF": inactive device\n", uuid);
4483 printf(UUF": %s\n", uuid, strerror(-rc));
4490 struct ll_stat_type {
4495 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4497 struct ll_statfs_data {
4499 struct obd_statfs sd_st;
4502 struct ll_statfs_buf {
4504 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4507 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4508 int ops, struct ll_statfs_buf *lsb)
4510 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4511 struct obd_uuid uuid_buf;
4512 char *poolname = NULL;
4513 struct ll_stat_type types[] = {
4514 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4515 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4516 { .st_name = NULL } };
4517 struct ll_stat_type *tp;
4518 __u64 ost_ffree = 0;
4526 poolname = strchr(pool, '.');
4527 if (poolname != NULL) {
4528 if (strncmp(fsname, pool, strlen(fsname))) {
4529 fprintf(stderr, "filesystem name incorrect\n");
4537 fd = open(mntdir, O_RDONLY);
4540 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4545 if (flags & MNTDF_SHOW) {
4546 if (flags & MNTDF_INODES)
4547 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4548 "UUID", "Inodes", "IUsed", "IFree",
4549 "IUse%", "Mounted on");
4551 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4553 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4554 "Used", "Available", "Use%", "Mounted on");
4557 for (tp = types; tp->st_name != NULL; tp++) {
4558 if (!(tp->st_op & ops))
4561 for (index = 0; ; index++) {
4562 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4563 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4564 type = flags & MNTDF_LAZY ?
4565 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4566 rc2 = llapi_obd_fstatfs(fd, type, index,
4567 &stat_buf, &uuid_buf);
4572 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4573 if (!(flags & MNTDF_VERBOSE))
4575 } else if (rc2 < 0 && rc == 0) {
4579 if (poolname && tp->st_op == LL_STATFS_LOV &&
4580 llapi_search_ost(fsname, poolname,
4581 obd_uuid2str(&uuid_buf)) != 1)
4584 /* the llapi_obd_statfs() call may have returned with
4585 * an error, but if it filled in uuid_buf we will at
4586 * lease use that to print out a message for that OBD.
4587 * If we didn't get anything in the uuid_buf, then fill
4588 * it in so that we can print an error message. */
4589 if (uuid_buf.uuid[0] == '\0')
4590 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4591 "%s%04x", tp->st_name, index);
4593 lsb->sb_buf[lsb->sb_count].sd_index = index;
4594 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4597 if (flags & MNTDF_SHOW)
4598 showdf(mntdir, &stat_buf,
4599 obd_uuid2str(&uuid_buf), flags,
4600 tp->st_name, index, rc2);
4603 if (tp->st_op == LL_STATFS_LMV) {
4604 sum.os_ffree += stat_buf.os_ffree;
4605 sum.os_files += stat_buf.os_files;
4606 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4607 sum.os_blocks += stat_buf.os_blocks *
4609 sum.os_bfree += stat_buf.os_bfree *
4611 sum.os_bavail += stat_buf.os_bavail *
4613 ost_ffree += stat_buf.os_ffree;
4621 /* If we don't have as many objects free on the OST as inodes
4622 * on the MDS, we reduce the total number of inodes to
4623 * compensate, so that the "inodes in use" number is correct.
4624 * Matches ll_statfs_internal() so the results are consistent. */
4625 if (ost_ffree < sum.os_ffree) {
4626 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4627 sum.os_ffree = ost_ffree;
4629 if (flags & MNTDF_SHOW) {
4631 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4638 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4640 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4642 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4644 int r1 = obd_statfs_ratio(st1);
4645 int r2 = obd_statfs_ratio(st2);
4646 int64_t result = r1 - r2;
4648 /* if both space usage are above 90, compare free inodes */
4649 if (r1 > 90 && r2 > 90)
4650 result = st2->os_ffree - st1->os_ffree;
4654 else if (result == 0)
4661 static int lfs_setdirstripe(int argc, char **argv)
4665 struct lfs_setstripe_args lsa = { 0 };
4666 struct llapi_stripe_param *param = NULL;
4667 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4670 char *mode_opt = NULL;
4671 bool default_stripe = false;
4672 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4673 mode_t previous_mode = 0;
4674 bool delete = false;
4675 struct ll_statfs_buf *lsb = NULL;
4676 char mntdir[PATH_MAX] = "";
4677 bool auto_distributed = false;
4679 struct option long_opts[] = {
4680 { .val = 'c', .name = "count", .has_arg = required_argument },
4681 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4682 { .val = 'd', .name = "delete", .has_arg = no_argument },
4683 { .val = 'D', .name = "default", .has_arg = no_argument },
4684 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4685 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4686 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4687 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4688 { .val = 'i', .name = "index", .has_arg = required_argument },
4690 { .val = 'o', .name = "mode", .has_arg = required_argument },
4691 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4692 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4694 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4695 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4698 setstripe_args_init(&lsa);
4700 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4708 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4711 "%s %s: invalid stripe count '%s'\n",
4712 progname, argv[0], optarg);
4718 default_stripe = true;
4721 default_stripe = true;
4723 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4725 fprintf(stderr, "warning: '--hash-type' and '-t' "
4726 "deprecated, use '--mdt-hash' or '-H' instead\n");
4729 lsa.lsa_pattern = check_hashtype(optarg);
4730 if (lsa.lsa_pattern == 0) {
4732 "%s %s: bad stripe hash type '%s'\n",
4733 progname, argv[0], optarg);
4738 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4739 if (strcmp(argv[optind - 1], "--index") == 0)
4741 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4744 lsa.lsa_nr_tgts = parse_targets(mdts,
4745 sizeof(mdts) / sizeof(__u32),
4746 lsa.lsa_nr_tgts, optarg);
4747 if (lsa.lsa_nr_tgts < 0) {
4749 "%s %s: invalid MDT target(s) '%s'\n",
4750 progname, argv[0], optarg);
4754 lsa.lsa_tgts = mdts;
4755 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4756 lsa.lsa_stripe_off = mdts[0];
4758 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4760 fprintf(stderr, "warning: '-m' is deprecated, "
4761 "use '--mode' or '-o' instead\n");
4767 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4768 progname, argv[0], argv[optind - 1]);
4773 if (optind == argc) {
4774 fprintf(stderr, "%s %s: DIR must be specified\n",
4779 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
4780 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
4782 "%s %s: stripe offset and count must be specified\n",
4788 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
4789 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
4791 "%s %s: cannot specify -d with -c or -i options\n",
4796 if (mode_opt != NULL) {
4797 mode = strtoul(mode_opt, &end, 8);
4800 "%s %s: bad MODE '%s'\n",
4801 progname, argv[0], mode_opt);
4804 previous_mode = umask(0);
4808 * initialize stripe parameters, in case param is converted to specific,
4809 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
4811 param = calloc(1, offsetof(typeof(*param),
4812 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
4813 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
4814 if (param == NULL) {
4816 "%s %s: cannot allocate memory for parameters: %s\n",
4817 progname, argv[0], strerror(ENOMEM));
4821 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4822 param->lsp_stripe_count = lsa.lsa_stripe_count;
4823 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4824 param->lsp_stripe_offset = -1;
4826 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4827 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4828 param->lsp_stripe_pattern = lsa.lsa_pattern;
4830 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
4831 param->lsp_pool = lsa.lsa_pool_name;
4832 param->lsp_is_specific = false;
4833 if (lsa.lsa_nr_tgts > 1) {
4834 if (lsa.lsa_stripe_count > 0 &&
4835 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4836 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4837 fprintf(stderr, "error: %s: stripe count %lld doesn't "
4838 "match the number of MDTs: %d\n",
4839 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
4844 param->lsp_is_specific = true;
4845 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4846 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
4849 dname = argv[optind];
4851 if (default_stripe) {
4852 result = llapi_dir_set_default_lmv(dname, param);
4854 /* if current \a dname isn't under the same \a mntdir
4855 * as the last one, and the last one was
4856 * auto-distributed, restore \a param.
4858 if (mntdir[0] != '\0' &&
4859 strncmp(dname, mntdir, strlen(mntdir)) &&
4861 param->lsp_is_specific = false;
4862 param->lsp_stripe_offset = -1;
4863 auto_distributed = false;
4866 if (!param->lsp_is_specific &&
4867 param->lsp_stripe_offset == -1) {
4868 char path[PATH_MAX] = "";
4871 lsb = malloc(sizeof(*lsb));
4879 /* use mntdir for dirname() temporarily */
4880 strncpy(mntdir, dname, sizeof(mntdir));
4881 if (!realpath(dirname(mntdir), path)) {
4884 "error: invalid path '%s': %s\n",
4885 argv[optind], strerror(errno));
4890 result = llapi_search_mounts(path, 0, mntdir,
4892 if (result < 0 || mntdir[0] == '\0') {
4894 "No suitable Lustre mount found\n");
4898 result = mntdf(mntdir, NULL, NULL, 0,
4899 LL_STATFS_LMV, lsb);
4903 if (param->lsp_stripe_count > lsb->sb_count) {
4905 "error: stripe count %d is too big\n",
4906 param->lsp_stripe_count);
4911 qsort(lsb->sb_buf, lsb->sb_count,
4912 sizeof(struct ll_statfs_data),
4913 ll_statfs_data_comp);
4915 auto_distributed = true;
4918 if (auto_distributed) {
4920 int nr = MAX(param->lsp_stripe_count,
4923 /* don't use server whose usage is above 90% */
4924 while (nr != param->lsp_stripe_count &&
4925 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
4927 nr = MAX(param->lsp_stripe_count,
4930 /* get \a r between [0, nr) */
4933 param->lsp_stripe_offset =
4934 lsb->sb_buf[r].sd_index;
4935 if (param->lsp_stripe_count > 1) {
4938 param->lsp_is_specific = true;
4939 for (; i < param->lsp_stripe_count; i++)
4940 param->lsp_tgts[(i + r) % nr] =
4941 lsb->sb_buf[i].sd_index;
4945 result = llapi_dir_create(dname, mode, param);
4950 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
4951 progname, dname, strerror(-result));
4954 dname = argv[++optind];
4955 } while (dname != NULL);
4957 if (mode_opt != NULL)
4958 umask(previous_mode);
4966 static int lfs_rmentry(int argc, char **argv)
4973 fprintf(stderr, "error: %s: missing dirname\n",
4979 dname = argv[index];
4980 while (dname != NULL) {
4981 result = llapi_direntry_remove(dname);
4983 fprintf(stderr, "error: %s: remove dir entry '%s' "
4984 "failed\n", argv[0], dname);
4987 dname = argv[++index];
4992 static int lfs_mv(int argc, char **argv)
4994 struct find_param param = {
5001 struct option long_opts[] = {
5002 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5003 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5006 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5008 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5010 fprintf(stderr, "warning: '-M' deprecated"
5011 ", use '--mdt-index' or '-m' instead\n");
5014 param.fp_mdt_index = strtoul(optarg, &end, 0);
5016 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5022 param.fp_verbose = VERBOSE_DETAIL;
5025 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5026 progname, argv[optind - 1]);
5031 if (param.fp_mdt_index == -1) {
5032 fprintf(stderr, "%s mv: MDT index must be specified\n",
5037 if (optind >= argc) {
5038 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5042 param.fp_migrate = 1;
5043 rc = llapi_migrate_mdt(argv[optind], ¶m);
5045 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5046 progname, argv[optind], param.fp_mdt_index,
5051 static int lfs_osts(int argc, char **argv)
5053 return lfs_tgts(argc, argv);
5056 static int lfs_mdts(int argc, char **argv)
5058 return lfs_tgts(argc, argv);
5061 static int lfs_df(int argc, char **argv)
5063 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5064 enum mntdf_flags flags = MNTDF_SHOW;
5065 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5066 int c, rc = 0, index = 0;
5067 char fsname[PATH_MAX] = "", *pool_name = NULL;
5068 struct option long_opts[] = {
5069 { .val = 'h', .name = "human-readable",
5070 .has_arg = no_argument },
5071 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5072 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5073 { .val = 'p', .name = "pool", .has_arg = required_argument },
5074 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5077 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5080 flags |= MNTDF_COOKED;
5083 flags |= MNTDF_INODES;
5086 flags |= MNTDF_LAZY;
5092 flags |= MNTDF_VERBOSE;
5098 if (optind < argc && !realpath(argv[optind], path)) {
5100 fprintf(stderr, "error: invalid path '%s': %s\n",
5101 argv[optind], strerror(-rc));
5105 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5106 /* Check if we have a mount point */
5107 if (mntdir[0] == '\0')
5110 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5111 if (rc || path[0] != '\0')
5113 fsname[0] = '\0'; /* avoid matching in next loop */
5114 mntdir[0] = '\0'; /* avoid matching in next loop */
5120 static int lfs_getname(int argc, char **argv)
5122 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5123 int rc = 0, index = 0, c;
5124 char buf[sizeof(struct obd_uuid)];
5126 while ((c = getopt(argc, argv, "h")) != -1)
5129 if (optind == argc) { /* no paths specified, get all paths. */
5130 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5131 rc = llapi_getname(mntdir, buf, sizeof(buf));
5134 "cannot get name for `%s': %s\n",
5135 mntdir, strerror(-rc));
5139 printf("%s %s\n", buf, mntdir);
5141 path[0] = fsname[0] = mntdir[0] = 0;
5143 } else { /* paths specified, only attempt to search these. */
5144 for (; optind < argc; optind++) {
5145 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5148 "cannot get name for `%s': %s\n",
5149 argv[optind], strerror(-rc));
5153 printf("%s %s\n", buf, argv[optind]);
5159 static int lfs_check(int argc, char **argv)
5162 char mntdir[PATH_MAX] = {'\0'};
5169 fprintf(stderr, "%s check: server type must be specified\n",
5174 obd_types[0] = obd_type1;
5175 obd_types[1] = obd_type2;
5177 if (strcmp(argv[1], "osts") == 0) {
5178 strcpy(obd_types[0], "osc");
5179 } else if (strcmp(argv[1], "mds") == 0) {
5180 strcpy(obd_types[0], "mdc");
5181 } else if (strcmp(argv[1], "servers") == 0) {
5183 strcpy(obd_types[0], "osc");
5184 strcpy(obd_types[1], "mdc");
5186 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5191 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5192 if (rc < 0 || mntdir[0] == '\0') {
5194 "%s check: cannot find mounted Lustre filesystem: %s\n",
5195 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5199 rc = llapi_target_check(num_types, obd_types, mntdir);
5201 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5202 progname, argv[1], strerror(-rc));
5208 #ifdef HAVE_SYS_QUOTA_H
5209 #define ARG2INT(nr, str, msg) \
5212 nr = strtol(str, &endp, 0); \
5213 if (*endp != '\0') { \
5214 fprintf(stderr, "%s: bad %s '%s'\n", \
5215 progname, msg, str); \
5220 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5222 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5223 * returns the value or ULONG_MAX on integer overflow or incorrect format
5225 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5226 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5227 * 3. empty integer value is interpreted as 0
5229 static unsigned long str2sec(const char* timestr)
5231 const char spec[] = "smhdw";
5232 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5233 unsigned long val = 0;
5236 if (strpbrk(timestr, spec) == NULL) {
5237 /* no specifiers inside the time string,
5238 should treat it as an integer value */
5239 val = strtoul(timestr, &tail, 10);
5240 return *tail ? ULONG_MAX : val;
5243 /* format string is XXwXXdXXhXXmXXs */
5249 v = strtoul(timestr, &tail, 10);
5250 if (v == ULONG_MAX || *tail == '\0')
5251 /* value too large (ULONG_MAX or more)
5252 or missing specifier */
5255 ptr = strchr(spec, *tail);
5257 /* unknown specifier */
5262 /* check if product will overflow the type */
5263 if (!(v < ULONG_MAX / mult[ind]))
5266 ADD_OVERFLOW(val, mult[ind] * v);
5267 if (val == ULONG_MAX)
5279 #define ARG2ULL(nr, str, def_units) \
5281 unsigned long long limit, units = def_units; \
5284 rc = llapi_parse_size(str, &limit, &units, 1); \
5286 fprintf(stderr, "%s: invalid limit '%s'\n", \
5293 static inline int has_times_option(int argc, char **argv)
5297 for (i = 1; i < argc; i++)
5298 if (!strcmp(argv[i], "-t"))
5304 int lfs_setquota_times(int argc, char **argv)
5307 struct if_quotactl qctl;
5308 char *mnt, *obd_type = (char *)qctl.obd_type;
5309 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5310 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5311 struct option long_opts[] = {
5312 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5313 { .val = 'g', .name = "group", .has_arg = no_argument },
5314 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5315 { .val = 'p', .name = "projid", .has_arg = no_argument },
5316 { .val = 't', .name = "times", .has_arg = no_argument },
5317 { .val = 'u', .name = "user", .has_arg = no_argument },
5321 memset(&qctl, 0, sizeof(qctl));
5322 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5323 qctl.qc_type = ALLQUOTA;
5325 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5326 long_opts, NULL)) != -1) {
5337 if (qctl.qc_type != ALLQUOTA) {
5338 fprintf(stderr, "error: -u/g/p can't be used "
5339 "more than once\n");
5342 qctl.qc_type = qtype;
5345 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5346 fprintf(stderr, "error: bad block-grace: %s\n",
5350 dqb->dqb_valid |= QIF_BTIME;
5353 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5354 fprintf(stderr, "error: bad inode-grace: %s\n",
5358 dqb->dqb_valid |= QIF_ITIME;
5360 case 't': /* Yes, of course! */
5362 default: /* getopt prints error message for us when opterr != 0 */
5367 if (qctl.qc_type == ALLQUOTA) {
5368 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5372 if (optind != argc - 1) {
5373 fprintf(stderr, "error: unexpected parameters encountered\n");
5377 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5378 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5379 fprintf(stderr, "error: grace time is too large\n");
5384 rc = llapi_quotactl(mnt, &qctl);
5387 fprintf(stderr, "%s %s ", obd_type,
5388 obd_uuid2str(&qctl.obd_uuid));
5389 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5396 #define BSLIMIT (1 << 0)
5397 #define BHLIMIT (1 << 1)
5398 #define ISLIMIT (1 << 2)
5399 #define IHLIMIT (1 << 3)
5401 int lfs_setquota(int argc, char **argv)
5404 struct if_quotactl qctl;
5405 char *mnt, *obd_type = (char *)qctl.obd_type;
5406 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5407 struct option long_opts[] = {
5408 { .val = 'b', .name = "block-softlimit",
5409 .has_arg = required_argument },
5410 { .val = 'B', .name = "block-hardlimit",
5411 .has_arg = required_argument },
5412 { .val = 'd', .name = "default", .has_arg = no_argument },
5413 { .val = 'g', .name = "group", .has_arg = required_argument },
5414 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5415 { .val = 'i', .name = "inode-softlimit",
5416 .has_arg = required_argument },
5417 { .val = 'I', .name = "inode-hardlimit",
5418 .has_arg = required_argument },
5419 { .val = 'p', .name = "projid", .has_arg = required_argument },
5420 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5421 { .val = 'u', .name = "user", .has_arg = required_argument },
5422 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5424 unsigned limit_mask = 0;
5426 bool use_default = false;
5429 if (has_times_option(argc, argv))
5430 return lfs_setquota_times(argc, argv);
5432 memset(&qctl, 0, sizeof(qctl));
5433 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5434 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5435 * so it can be used as a marker that qc_type
5436 * isn't reinitialized from command line */
5438 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5439 long_opts, NULL)) != -1) {
5442 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5445 goto quota_type_def;
5448 rc = name2uid(&qctl.qc_id, optarg);
5451 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5454 goto quota_type_def;
5457 rc = name2gid(&qctl.qc_id, optarg);
5460 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5463 goto quota_type_def;
5466 rc = name2projid(&qctl.qc_id, optarg);
5469 qctl.qc_id = strtoul(optarg, &endptr, 10);
5470 if (*endptr != '\0') {
5471 fprintf(stderr, "%s setquota: invalid"
5472 " id '%s'\n", progname, optarg);
5477 if (qctl.qc_id == 0) {
5478 fprintf(stderr, "%s setquota: can't set quota"
5479 " for root usr/group/project.\n",
5485 if (qctl.qc_type != ALLQUOTA) {
5487 "%s setquota: only one of -u, -U, -g,"
5488 " -G, -p or -P may be specified\n",
5492 qctl.qc_type = qtype;
5495 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5499 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5500 dqb->dqb_bsoftlimit >>= 10;
5501 limit_mask |= BSLIMIT;
5502 if (dqb->dqb_bsoftlimit &&
5503 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5505 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5506 "See '%s help setquota' or Lustre manual for details\n",
5507 progname, dqb->dqb_bsoftlimit,
5511 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5512 dqb->dqb_bhardlimit >>= 10;
5513 limit_mask |= BHLIMIT;
5514 if (dqb->dqb_bhardlimit &&
5515 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5517 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5518 "See '%s help setquota' or Lustre manual for details\n",
5519 progname, dqb->dqb_bhardlimit,
5523 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5524 limit_mask |= ISLIMIT;
5525 if (dqb->dqb_isoftlimit &&
5526 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5528 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5529 "See '%s help setquota' or Lustre manual for details\n",
5530 progname, dqb->dqb_isoftlimit,
5534 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5535 limit_mask |= IHLIMIT;
5536 if (dqb->dqb_ihardlimit &&
5537 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5539 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5540 "See '%s help setquota' or Lustre manual for details\n",
5541 progname, dqb->dqb_ihardlimit,
5546 "%s setquota: unrecognized option '%s'\n",
5547 progname, argv[optind - 1]);
5552 if (qctl.qc_type == ALLQUOTA) {
5554 "%s setquota: either -u or -g must be specified\n",
5559 if (!use_default && limit_mask == 0) {
5561 "%s setquota: at least one limit must be specified\n",
5566 if (use_default && limit_mask != 0) {
5568 "%s setquota: limits should not be specified when"
5569 " using default quota\n",
5574 if (use_default && qctl.qc_id == 0) {
5576 "%s setquota: can not set default quota for root"
5577 " user/group/project\n",
5582 if (optind != argc - 1) {
5584 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5585 progname, argv[optind]);
5592 dqb->dqb_bhardlimit = 0;
5593 dqb->dqb_bsoftlimit = 0;
5594 dqb->dqb_ihardlimit = 0;
5595 dqb->dqb_isoftlimit = 0;
5598 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5599 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5600 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5601 /* sigh, we can't just set blimits/ilimits */
5602 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5603 .qc_type = qctl.qc_type,
5604 .qc_id = qctl.qc_id};
5606 rc = llapi_quotactl(mnt, &tmp_qctl);
5610 if (!(limit_mask & BHLIMIT))
5611 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5612 if (!(limit_mask & BSLIMIT))
5613 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5614 if (!(limit_mask & IHLIMIT))
5615 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5616 if (!(limit_mask & ISLIMIT))
5617 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5619 /* Keep grace times if we have got no softlimit arguments */
5620 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5621 dqb->dqb_valid |= QIF_BTIME;
5622 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5625 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5626 dqb->dqb_valid |= QIF_ITIME;
5627 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5631 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5632 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5634 rc = llapi_quotactl(mnt, &qctl);
5638 "%s setquota: cannot quotactl '%s' '%s': %s",
5640 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5647 /* Converts seconds value into format string
5648 * result is returned in buf
5650 * 1. result is in descenting order: 1w2d3h4m5s
5651 * 2. zero fields are not filled (except for p. 3): 5d1s
5652 * 3. zero seconds value is presented as "0s"
5654 static char * __sec2str(time_t seconds, char *buf)
5656 const char spec[] = "smhdw";
5657 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5662 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5663 c = seconds / mult[i];
5665 if (c > 0 || (i == 0 && buf == tail))
5666 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5674 static void sec2str(time_t seconds, char *buf, int rc)
5681 tail = __sec2str(seconds, tail);
5683 if (rc && tail - buf < 39) {
5689 static void diff2str(time_t seconds, char *buf, time_t now)
5695 if (seconds <= now) {
5696 strcpy(buf, "none");
5699 __sec2str(seconds - now, buf);
5702 static void print_quota_title(char *name, struct if_quotactl *qctl,
5703 bool human_readable, bool show_default)
5706 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
5707 printf("%15s %8s%8s%8s %8s%8s%8s\n",
5708 "Filesystem", "bquota", "blimit", "bgrace",
5709 "iquota", "ilimit", "igrace");
5711 printf("Disk quotas for %s %s (%cid %u):\n",
5712 qtype_name(qctl->qc_type), name,
5713 *qtype_name(qctl->qc_type), qctl->qc_id);
5714 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5715 "Filesystem", human_readable ? "used" : "kbytes",
5716 "quota", "limit", "grace",
5717 "files", "quota", "limit", "grace");
5721 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5724 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5727 snprintf(buf, buflen, "%5.4gP",
5728 (double)num / ((__u64)1 << 40));
5730 snprintf(buf, buflen, "%5.4gT",
5731 (double)num / (1 << 30));
5733 snprintf(buf, buflen, "%5.4gG",
5734 (double)num / (1 << 20));
5736 snprintf(buf, buflen, "%5.4gM",
5737 (double)num / (1 << 10));
5739 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5743 #define STRBUF_LEN 32
5744 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5745 int rc, bool h, bool show_default)
5751 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
5752 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
5753 int bover = 0, iover = 0;
5754 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5755 char numbuf[3][STRBUF_LEN];
5757 char strbuf[STRBUF_LEN];
5759 if (dqb->dqb_bhardlimit &&
5760 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5762 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5763 if (dqb->dqb_btime > now) {
5770 if (dqb->dqb_ihardlimit &&
5771 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5773 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5774 if (dqb->dqb_itime > now) {
5782 if (strlen(mnt) > 15)
5783 printf("%s\n%15s", mnt, "");
5785 printf("%15s", mnt);
5788 diff2str(dqb->dqb_btime, timebuf, now);
5789 else if (show_default)
5790 snprintf(timebuf, sizeof(timebuf), "%llu",
5793 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5794 strbuf, sizeof(strbuf), h);
5795 if (rc == -EREMOTEIO)
5796 sprintf(numbuf[0], "%s*", strbuf);
5798 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5799 "%s" : "[%s]", strbuf);
5801 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5802 if (type == QC_GENERAL)
5803 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5804 "%s" : "[%s]", strbuf);
5806 sprintf(numbuf[1], "%s", "-");
5808 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5809 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5810 "%s" : "[%s]", strbuf);
5813 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
5815 printf(" %7s%c %6s %7s %7s",
5816 numbuf[0], bover ? '*' : ' ', numbuf[1],
5817 numbuf[2], bover > 1 ? timebuf : "-");
5821 diff2str(dqb->dqb_itime, timebuf, now);
5822 else if (show_default)
5823 snprintf(timebuf, sizeof(timebuf), "%llu",
5826 snprintf(numbuf[0], sizeof(numbuf),
5827 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
5828 (uintmax_t)dqb->dqb_curinodes);
5830 if (type == QC_GENERAL)
5831 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5833 (uintmax_t)dqb->dqb_isoftlimit);
5835 sprintf(numbuf[1], "%s", "-");
5837 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5838 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5841 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
5842 else if (type != QC_OSTIDX)
5843 printf(" %7s%c %6s %7s %7s",
5844 numbuf[0], iover ? '*' : ' ', numbuf[1],
5845 numbuf[2], iover > 1 ? timebuf : "-");
5847 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5850 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5851 qctl->qc_cmd == Q_GETOINFO) {
5855 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5856 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5857 printf("Block grace time: %s; Inode grace time: %s\n",
5858 bgtimebuf, igtimebuf);
5862 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5863 bool h, __u64 *total)
5865 int rc = 0, rc1 = 0, count = 0;
5866 __u32 valid = qctl->qc_valid;
5868 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5870 fprintf(stderr, "can not get %s count: %s\n",
5871 is_mdt ? "mdt": "ost", strerror(-rc));
5875 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5876 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5877 rc = llapi_quotactl(mnt, qctl);
5879 /* It is remote client case. */
5880 if (rc == -EOPNOTSUPP) {
5887 fprintf(stderr, "quotactl %s%d failed.\n",
5888 is_mdt ? "mdt": "ost", qctl->qc_idx);
5892 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5893 qctl->qc_valid, 0, h, false);
5894 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5895 qctl->qc_dqblk.dqb_bhardlimit;
5898 qctl->qc_valid = valid;
5902 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5903 int verbose, int quiet, bool human_readable,
5906 int rc1 = 0, rc2 = 0, rc3 = 0;
5907 char *obd_type = (char *)qctl->obd_type;
5908 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5909 __u64 total_ialloc = 0, total_balloc = 0;
5910 bool use_default_for_blk = false;
5911 bool use_default_for_file = false;
5914 rc1 = llapi_quotactl(mnt, qctl);
5918 fprintf(stderr, "%s quotas are not enabled.\n",
5919 qtype_name(qctl->qc_type));
5922 fprintf(stderr, "Permission denied.\n");
5925 /* We already got error message. */
5928 fprintf(stderr, "Unexpected quotactl error: %s\n",
5933 if (!show_default && qctl->qc_id == 0) {
5934 qctl->qc_dqblk.dqb_bhardlimit = 0;
5935 qctl->qc_dqblk.dqb_bsoftlimit = 0;
5936 qctl->qc_dqblk.dqb_ihardlimit = 0;
5937 qctl->qc_dqblk.dqb_isoftlimit = 0;
5938 qctl->qc_dqblk.dqb_btime = 0;
5939 qctl->qc_dqblk.dqb_itime = 0;
5940 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
5943 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
5944 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
5945 use_default_for_blk = true;
5946 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
5949 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
5950 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
5951 use_default_for_file = true;
5952 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
5955 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
5956 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
5957 print_quota_title(name, qctl, human_readable, show_default);
5959 if (rc1 && *obd_type)
5960 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5962 if (qctl->qc_valid != QC_GENERAL)
5965 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5966 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5967 (QIF_LIMITS|QIF_USAGE));
5969 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
5971 if (!show_default && verbose &&
5972 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
5973 char strbuf[STRBUF_LEN];
5975 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5977 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5979 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5981 printf("Total allocated inode limit: %ju, total "
5982 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5986 if (use_default_for_blk)
5987 printf("%cid %u is using default block quota setting\n",
5988 *qtype_name(qctl->qc_type), qctl->qc_id);
5990 if (use_default_for_file)
5991 printf("%cid %u is using default file quota setting\n",
5992 *qtype_name(qctl->qc_type), qctl->qc_id);
5994 if (rc1 || rc2 || rc3 || inacc)
5995 printf("Some errors happened when getting quota info. "
5996 "Some devices may be not working or deactivated. "
5997 "The data in \"[]\" is inaccurate.\n");
6003 static int lfs_project(int argc, char **argv)
6005 int ret = 0, err = 0, c, i;
6006 struct project_handle_control phc = { 0 };
6007 enum lfs_project_ops_t op;
6010 phc.assign_projid = false;
6011 /* default action */
6012 op = LFS_PROJECT_LIST;
6014 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6017 if (op != LFS_PROJECT_LIST) {
6019 "%s: cannot specify '-c' '-C' '-s' together\n",
6024 op = LFS_PROJECT_CHECK;
6027 if (op != LFS_PROJECT_LIST) {
6029 "%s: cannot specify '-c' '-C' '-s' together\n",
6034 op = LFS_PROJECT_CLEAR;
6037 if (op != LFS_PROJECT_LIST) {
6039 "%s: cannot specify '-c' '-C' '-s' together\n",
6044 phc.set_inherit = true;
6045 op = LFS_PROJECT_SET;
6051 phc.keep_projid = true;
6054 phc.recursive = true;
6057 phc.projid = strtoul(optarg, NULL, 0);
6058 phc.assign_projid = true;
6062 phc.newline = false;
6065 fprintf(stderr, "%s: invalid option '%c'\n",
6071 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6072 op = LFS_PROJECT_SET;
6073 phc.set_projid = true;
6074 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6075 phc.set_projid = true;
6079 case LFS_PROJECT_CHECK:
6080 if (phc.keep_projid) {
6082 "%s: '-k' is useless together with '-c'\n",
6087 case LFS_PROJECT_CLEAR:
6090 "%s: '-0' is useless together with '-C'\n",
6094 if (phc.assign_projid) {
6096 "%s: '-p' is useless together with '-C'\n",
6101 case LFS_PROJECT_SET:
6104 "%s: '-0' is useless together with '-s'\n",
6108 if (phc.keep_projid) {
6110 "%s: '-k' is useless together with '-s'\n",
6118 "%s: '-0' is useless for list operations\n",
6128 fprintf(stderr, "%s: missing file or directory target(s)\n",
6133 for (i = 0; i < argc; i++) {
6135 case LFS_PROJECT_CHECK:
6136 err = lfs_project_check(argv[i], &phc);
6138 case LFS_PROJECT_LIST:
6139 err = lfs_project_list(argv[i], &phc);
6141 case LFS_PROJECT_CLEAR:
6142 err = lfs_project_clear(argv[i], &phc);
6144 case LFS_PROJECT_SET:
6145 err = lfs_project_set(argv[i], &phc);
6157 static int lfs_quota(int argc, char **argv)
6160 char *mnt, *name = NULL;
6161 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6162 .qc_type = ALLQUOTA };
6163 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6164 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6166 __u32 valid = QC_GENERAL, idx = 0;
6167 bool human_readable = false;
6168 bool show_default = false;
6171 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6174 show_default = true;
6179 show_default = true;
6184 show_default = true;
6188 if (qctl.qc_type != ALLQUOTA) {
6190 "%s quota: only one of -u, -g, or -p may be specified\n",
6194 qctl.qc_type = qtype;
6197 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6200 valid = qctl.qc_valid = QC_UUID;
6201 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6204 valid = qctl.qc_valid = QC_MDTIDX;
6205 idx = qctl.qc_idx = atoi(optarg);
6206 if (idx == 0 && *optarg != '0') {
6208 "%s quota: invalid MDT index '%s'\n",
6214 valid = qctl.qc_valid = QC_OSTIDX;
6215 idx = qctl.qc_idx = atoi(optarg);
6216 if (idx == 0 && *optarg != '0') {
6218 "%s quota: invalid OST index '%s'\n",
6230 human_readable = true;
6233 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6234 progname, argv[optind - 1]);
6239 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6240 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6241 optind == argc - 1 && !show_default) {
6243 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6244 qctl.qc_valid = valid;
6247 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6248 qctl.qc_type = qtype;
6249 if (qtype == USRQUOTA) {
6250 qctl.qc_id = geteuid();
6251 rc = uid2name(&name, qctl.qc_id);
6253 qctl.qc_id = getegid();
6254 rc = gid2name(&name, qctl.qc_id);
6259 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6260 human_readable, show_default);
6265 /* lfs quota -u username /path/to/lustre/mount */
6266 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6267 /* options should be followed by u/g-name and mntpoint */
6268 if ((!show_default && optind + 2 != argc) ||
6269 (show_default && optind + 1 != argc) ||
6270 qctl.qc_type == ALLQUOTA) {
6272 "%s quota: name and mount point must be specified\n",
6277 if (!show_default) {
6278 name = argv[optind++];
6279 switch (qctl.qc_type) {
6281 rc = name2uid(&qctl.qc_id, name);
6284 rc = name2gid(&qctl.qc_id, name);
6287 rc = name2projid(&qctl.qc_id, name);
6294 qctl.qc_valid = QC_GENERAL;
6295 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6300 qctl.qc_id = strtoul(name, &endptr, 10);
6301 if (*endptr != '\0') {
6302 fprintf(stderr, "%s quota: invalid id '%s'\n",
6307 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6308 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6314 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6315 human_readable, show_default);
6318 #endif /* HAVE_SYS_QUOTA_H! */
6320 static int flushctx_ioctl(char *mp)
6324 fd = open(mp, O_RDONLY);
6326 fprintf(stderr, "flushctx: error open %s: %s\n",
6327 mp, strerror(errno));
6331 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6333 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6334 mp, strerror(errno));
6340 static int lfs_flushctx(int argc, char **argv)
6342 int kdestroy = 0, c;
6343 char mntdir[PATH_MAX] = {'\0'};
6347 while ((c = getopt(argc, argv, "k")) != -1) {
6353 fprintf(stderr, "error: %s: option '-%c' "
6354 "unrecognized\n", argv[0], c);
6360 if ((rc = system("kdestroy > /dev/null")) != 0) {
6361 rc = WEXITSTATUS(rc);
6362 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6366 if (optind >= argc) {
6367 /* flush for all mounted lustre fs. */
6368 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6369 /* Check if we have a mount point */
6370 if (mntdir[0] == '\0')
6373 if (flushctx_ioctl(mntdir))
6376 mntdir[0] = '\0'; /* avoid matching in next loop */
6379 /* flush fs as specified */
6380 while (optind < argc) {
6381 if (flushctx_ioctl(argv[optind++]))
6388 static int lfs_cp(int argc, char **argv)
6390 fprintf(stderr, "remote client copy file(s).\n"
6391 "obsolete, does not support it anymore.\n");
6395 static int lfs_ls(int argc, char **argv)
6397 fprintf(stderr, "remote client lists directory contents.\n"
6398 "obsolete, does not support it anymore.\n");
6402 static int lfs_changelog(int argc, char **argv)
6404 void *changelog_priv;
6405 struct changelog_rec *rec;
6406 long long startrec = 0, endrec = 0;
6408 struct option long_opts[] = {
6409 { .val = 'f', .name = "follow", .has_arg = no_argument },
6411 char short_opts[] = "f";
6414 while ((rc = getopt_long(argc, argv, short_opts,
6415 long_opts, NULL)) != -1) {
6422 "%s changelog: unrecognized option '%s'\n",
6423 progname, argv[optind - 1]);
6427 if (optind >= argc) {
6428 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6433 mdd = argv[optind++];
6435 startrec = strtoll(argv[optind++], NULL, 10);
6437 endrec = strtoll(argv[optind++], NULL, 10);
6439 rc = llapi_changelog_start(&changelog_priv,
6440 CHANGELOG_FLAG_BLOCK |
6441 CHANGELOG_FLAG_JOBID |
6442 CHANGELOG_FLAG_EXTRA_FLAGS |
6443 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6446 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6447 progname, strerror(errno = -rc));
6451 rc = llapi_changelog_set_xflags(changelog_priv,
6452 CHANGELOG_EXTRA_FLAG_UIDGID |
6453 CHANGELOG_EXTRA_FLAG_NID |
6454 CHANGELOG_EXTRA_FLAG_OMODE |
6455 CHANGELOG_EXTRA_FLAG_XATTR);
6458 "%s changelog: cannot set xflags for changelog: %s\n",
6459 progname, strerror(errno = -rc));
6463 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6467 if (endrec && rec->cr_index > endrec) {
6468 llapi_changelog_free(&rec);
6471 if (rec->cr_index < startrec) {
6472 llapi_changelog_free(&rec);
6476 secs = rec->cr_time >> 30;
6477 gmtime_r(&secs, &ts);
6478 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6479 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6480 changelog_type2str(rec->cr_type),
6481 ts.tm_hour, ts.tm_min, ts.tm_sec,
6482 (int)(rec->cr_time & ((1 << 30) - 1)),
6483 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6484 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6486 if (rec->cr_flags & CLF_JOBID) {
6487 struct changelog_ext_jobid *jid =
6488 changelog_rec_jobid(rec);
6490 if (jid->cr_jobid[0] != '\0')
6491 printf(" j=%s", jid->cr_jobid);
6494 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6495 struct changelog_ext_extra_flags *ef =
6496 changelog_rec_extra_flags(rec);
6498 printf(" ef=0x%llx", ef->cr_extra_flags);
6500 if (ef->cr_extra_flags & CLFE_UIDGID) {
6501 struct changelog_ext_uidgid *uidgid =
6502 changelog_rec_uidgid(rec);
6504 printf(" u=%llu:%llu",
6505 uidgid->cr_uid, uidgid->cr_gid);
6507 if (ef->cr_extra_flags & CLFE_NID) {
6508 struct changelog_ext_nid *nid =
6509 changelog_rec_nid(rec);
6512 libcfs_nid2str(nid->cr_nid));
6515 if (ef->cr_extra_flags & CLFE_OPEN) {
6516 struct changelog_ext_openmode *omd =
6517 changelog_rec_openmode(rec);
6518 char mode[] = "---";
6520 /* exec mode must be exclusive */
6521 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6524 if (omd->cr_openflags & MDS_FMODE_READ)
6526 if (omd->cr_openflags &
6533 if (strcmp(mode, "---") != 0)
6534 printf(" m=%s", mode);
6538 if (ef->cr_extra_flags & CLFE_XATTR) {
6539 struct changelog_ext_xattr *xattr =
6540 changelog_rec_xattr(rec);
6542 if (xattr->cr_xattr[0] != '\0')
6543 printf(" x=%s", xattr->cr_xattr);
6547 if (rec->cr_namelen)
6548 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6549 rec->cr_namelen, changelog_rec_name(rec));
6551 if (rec->cr_flags & CLF_RENAME) {
6552 struct changelog_ext_rename *rnm =
6553 changelog_rec_rename(rec);
6555 if (!fid_is_zero(&rnm->cr_sfid))
6556 printf(" s="DFID" sp="DFID" %.*s",
6557 PFID(&rnm->cr_sfid),
6558 PFID(&rnm->cr_spfid),
6559 (int)changelog_rec_snamelen(rec),
6560 changelog_rec_sname(rec));
6564 llapi_changelog_free(&rec);
6567 llapi_changelog_fini(&changelog_priv);
6570 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6571 progname, strerror(errno = -rc));
6573 return (rc == 1 ? 0 : rc);
6576 static int lfs_changelog_clear(int argc, char **argv)
6584 endrec = strtoll(argv[3], NULL, 10);
6586 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6589 fprintf(stderr, "%s: record out of range: %llu\n",
6591 else if (rc == -ENOENT)
6592 fprintf(stderr, "%s: no changelog user: %s\n",
6595 fprintf(stderr, "%s error: %s\n", argv[0],
6604 static int lfs_fid2path(int argc, char **argv)
6606 struct option long_opts[] = {
6607 { .val = 'c', .name = "cur", .has_arg = no_argument },
6608 { .val = 'l', .name = "link", .has_arg = required_argument },
6609 { .val = 'r', .name = "rec", .has_arg = required_argument },
6611 char short_opts[] = "cl:r:";
6612 char *device, *fid, *path;
6613 long long recno = -1;
6618 char *endptr = NULL;
6620 while ((rc = getopt_long(argc, argv, short_opts,
6621 long_opts, NULL)) != -1) {
6627 linkno = strtol(optarg, &endptr, 10);
6628 if (*endptr != '\0') {
6630 "%s fid2path: invalid linkno '%s'\n",
6636 recno = strtoll(optarg, &endptr, 10);
6637 if (*endptr != '\0') {
6639 "%s fid2path: invalid recno '%s'\n",
6646 "%s fid2path: unrecognized option '%s'\n",
6647 progname, argv[optind - 1]);
6654 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6659 device = argv[optind++];
6660 path = calloc(1, PATH_MAX);
6664 "%s fid2path: cannot allocate memory for path: %s\n",
6665 progname, strerror(-rc));
6670 while (optind < argc) {
6671 fid = argv[optind++];
6673 lnktmp = (linkno >= 0) ? linkno : 0;
6675 int oldtmp = lnktmp;
6676 long long rectmp = recno;
6678 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6682 "%s fid2path: cannot find '%s': %s\n",
6683 progname, fid, strerror(errno = -rc2));
6690 fprintf(stdout, "%lld ", rectmp);
6691 if (device[0] == '/') {
6692 fprintf(stdout, "%s", device);
6693 if (device[strlen(device) - 1] != '/')
6694 fprintf(stdout, "/");
6695 } else if (path[0] == '\0') {
6696 fprintf(stdout, "/");
6698 fprintf(stdout, "%s\n", path);
6701 /* specified linkno */
6703 if (oldtmp == lnktmp)
6713 static int lfs_path2fid(int argc, char **argv)
6715 struct option long_opts[] = {
6716 { .val = 'p', .name = "parents", .has_arg = no_argument },
6719 const char short_opts[] = "p";
6720 const char *sep = "";
6723 bool show_parents = false;
6725 while ((rc = getopt_long(argc, argv, short_opts,
6726 long_opts, NULL)) != -1) {
6729 show_parents = true;
6733 "%s path2fid: unrecognized option '%s'\n",
6734 progname, argv[optind - 1]);
6739 if (optind > argc - 1) {
6740 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
6744 else if (optind < argc - 1)
6748 for (path = argv + optind; *path != NULL; path++) {
6750 if (!show_parents) {
6751 err = llapi_path2fid(*path, &fid);
6753 printf("%s%s"DFID"\n",
6754 *sep != '\0' ? *path : "", sep,
6757 char name[NAME_MAX + 1];
6758 unsigned int linkno = 0;
6760 while ((err = llapi_path2parent(*path, linkno, &fid,
6761 name, sizeof(name))) == 0) {
6762 if (*sep != '\0' && linkno == 0)
6763 printf("%s%s", *path, sep);
6765 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6770 /* err == -ENODATA is end-of-loop */
6771 if (linkno > 0 && err == -ENODATA) {
6779 "%s path2fid: cannot get %sfid for '%s': %s\n",
6780 progname, show_parents ? "parent " : "", *path,
6792 static int lfs_data_version(int argc, char **argv)
6799 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
6802 fprintf(stderr, "%s data_version: FILE must be specified\n",
6807 while ((c = getopt(argc, argv, "nrw")) != -1) {
6810 data_version_flags = 0;
6813 data_version_flags |= LL_DV_RD_FLUSH;
6816 data_version_flags |= LL_DV_WR_FLUSH;
6820 "%s data_version: unrecognized option '%s'\n",
6821 progname, argv[optind - 1]);
6825 if (optind == argc) {
6826 fprintf(stderr, "%s data_version: FILE must be specified\n",
6831 path = argv[optind];
6832 fd = open(path, O_RDONLY);
6835 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
6836 progname, path, strerror(-rc));
6840 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
6843 "%s data_version: cannot get version for '%s': %s\n",
6844 progname, path, strerror(-rc));
6846 printf("%ju" "\n", (uintmax_t)data_version);
6852 static int lfs_hsm_state(int argc, char **argv)
6857 struct hsm_user_state hus;
6865 rc = llapi_hsm_state_get(path, &hus);
6867 fprintf(stderr, "can't get hsm state for %s: %s\n",
6868 path, strerror(errno = -rc));
6872 /* Display path name and status flags */
6873 printf("%s: (0x%08x)", path, hus.hus_states);
6875 if (hus.hus_states & HS_RELEASED)
6876 printf(" released");
6877 if (hus.hus_states & HS_EXISTS)
6879 if (hus.hus_states & HS_DIRTY)
6881 if (hus.hus_states & HS_ARCHIVED)
6882 printf(" archived");
6883 /* Display user-settable flags */
6884 if (hus.hus_states & HS_NORELEASE)
6885 printf(" never_release");
6886 if (hus.hus_states & HS_NOARCHIVE)
6887 printf(" never_archive");
6888 if (hus.hus_states & HS_LOST)
6889 printf(" lost_from_hsm");
6891 if (hus.hus_archive_id != 0)
6892 printf(", archive_id:%d", hus.hus_archive_id);
6895 } while (++i < argc);
6900 #define LFS_HSM_SET 0
6901 #define LFS_HSM_CLEAR 1
6904 * Generic function to set or clear HSM flags.
6905 * Used by hsm_set and hsm_clear.
6907 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
6909 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
6911 struct option long_opts[] = {
6912 { .val = 'A', .name = "archived", .has_arg = no_argument },
6913 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
6914 { .val = 'd', .name = "dirty", .has_arg = no_argument },
6915 { .val = 'e', .name = "exists", .has_arg = no_argument },
6916 { .val = 'l', .name = "lost", .has_arg = no_argument },
6917 { .val = 'r', .name = "norelease", .has_arg = no_argument },
6918 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
6920 char short_opts[] = "lraAdei:";
6924 __u32 archive_id = 0;
6930 while ((c = getopt_long(argc, argv, short_opts,
6931 long_opts, NULL)) != -1) {
6937 mask |= HS_NOARCHIVE;
6940 mask |= HS_ARCHIVED;
6943 mask |= HS_NORELEASE;
6952 archive_id = strtol(optarg, &end, 10);
6954 fprintf(stderr, "invalid archive_id: '%s'\n",
6962 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6963 argv[0], argv[optind - 1]);
6968 /* User should have specified a flag */
6972 while (optind < argc) {
6974 path = argv[optind];
6976 /* If mode == 0, this means we apply the mask. */
6977 if (mode == LFS_HSM_SET)
6978 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6980 rc = llapi_hsm_state_set(path, 0, mask, 0);
6983 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6984 path, strerror(errno = -rc));
6993 static int lfs_hsm_action(int argc, char **argv)
6998 struct hsm_current_action hca;
6999 struct hsm_extent he;
7000 enum hsm_user_action hua;
7001 enum hsm_progress_states hps;
7009 rc = llapi_hsm_current_action(path, &hca);
7011 fprintf(stderr, "can't get hsm action for %s: %s\n",
7012 path, strerror(errno = -rc));
7015 he = hca.hca_location;
7016 hua = hca.hca_action;
7017 hps = hca.hca_state;
7019 printf("%s: %s", path, hsm_user_action2name(hua));
7021 /* Skip file without action */
7022 if (hca.hca_action == HUA_NONE) {
7027 printf(" %s ", hsm_progress_state2name(hps));
7029 if ((hps == HPS_RUNNING) &&
7030 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7031 printf("(%llu bytes moved)\n",
7032 (unsigned long long)he.length);
7033 else if ((he.offset + he.length) == LUSTRE_EOF)
7034 printf("(from %llu to EOF)\n",
7035 (unsigned long long)he.offset);
7037 printf("(from %llu to %llu)\n",
7038 (unsigned long long)he.offset,
7039 (unsigned long long)(he.offset + he.length));
7041 } while (++i < argc);
7046 static int lfs_hsm_set(int argc, char **argv)
7048 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7051 static int lfs_hsm_clear(int argc, char **argv)
7053 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7057 * Check file state and return its fid, to be used by lfs_hsm_request().
7059 * \param[in] file Path to file to check
7060 * \param[in,out] fid Pointer to allocated lu_fid struct.
7061 * \param[in,out] last_dev Pointer to last device id used.
7063 * \return 0 on success.
7065 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7071 rc = lstat(file, &st);
7073 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7076 /* Checking for regular file as archiving as posix copytool
7077 * rejects archiving files other than regular files
7079 if (!S_ISREG(st.st_mode)) {
7080 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7083 /* A request should be ... */
7084 if (*last_dev != st.st_dev && *last_dev != 0) {
7085 fprintf(stderr, "All files should be "
7086 "on the same filesystem: %s\n", file);
7089 *last_dev = st.st_dev;
7091 rc = llapi_path2fid(file, fid);
7093 fprintf(stderr, "Cannot read FID of %s: %s\n",
7094 file, strerror(-rc));
7100 /* Fill an HSM HUR item with a given file name.
7102 * If mntpath is set, then the filename is actually a FID, and no
7103 * lookup on the filesystem will be performed.
7105 * \param[in] hur the user request to fill
7106 * \param[in] idx index of the item inside the HUR to fill
7107 * \param[in] mntpath mountpoint of Lustre
7108 * \param[in] fname filename (if mtnpath is NULL)
7109 * or FID (if mntpath is set)
7110 * \param[in] last_dev pointer to last device id used
7112 * \retval 0 on success
7113 * \retval CMD_HELP or a negative errno on error
7115 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7116 const char *mntpath, const char *fname,
7119 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7122 hui->hui_extent.length = -1;
7124 if (mntpath != NULL) {
7127 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7131 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7136 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7140 hur->hur_request.hr_itemcount++;
7145 static int lfs_hsm_request(int argc, char **argv, int action)
7147 struct option long_opts[] = {
7148 { .val = 'a', .name = "archive", .has_arg = required_argument },
7149 { .val = 'D', .name = "data", .has_arg = required_argument },
7150 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7151 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7154 char short_opts[] = "l:D:a:m:";
7155 struct hsm_user_request *hur, *oldhur;
7160 char *filelist = NULL;
7161 char fullpath[PATH_MAX];
7162 char *opaque = NULL;
7166 int nbfile_alloc = 0;
7167 char *some_file = NULL;
7168 char *mntpath = NULL;
7174 while ((c = getopt_long(argc, argv, short_opts,
7175 long_opts, NULL)) != -1) {
7184 if (action != HUA_ARCHIVE &&
7185 action != HUA_REMOVE) {
7187 "error: -a is supported only "
7188 "when archiving or removing\n");
7191 archive_id = atoi(optarg);
7194 if (some_file == NULL) {
7196 some_file = strdup(optarg);
7202 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7203 argv[0], argv[optind - 1]);
7208 /* All remaining args are files, so we have at least nbfile */
7209 nbfile = argc - optind;
7211 if ((nbfile == 0) && (filelist == NULL))
7215 opaque_len = strlen(opaque);
7217 /* Alloc the request structure with enough place to store all files
7218 * from command line. */
7219 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7221 fprintf(stderr, "Cannot create the request: %s\n",
7225 nbfile_alloc = nbfile;
7227 hur->hur_request.hr_action = action;
7228 hur->hur_request.hr_archive_id = archive_id;
7229 hur->hur_request.hr_flags = 0;
7231 /* All remaining args are files, add them */
7232 if (nbfile != 0 && some_file == NULL)
7233 some_file = strdup(argv[optind]);
7235 for (i = 0; i < nbfile; i++) {
7236 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7242 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7244 /* If a filelist was specified, read the filelist from it. */
7245 if (filelist != NULL) {
7246 fp = fopen(filelist, "r");
7248 fprintf(stderr, "Cannot read the file list %s: %s\n",
7249 filelist, strerror(errno));
7254 while ((rc = getline(&line, &len, fp)) != -1) {
7255 /* If allocated buffer was too small, get something
7257 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7260 nbfile_alloc = nbfile_alloc * 2 + 1;
7262 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7265 fprintf(stderr, "hsm: cannot allocate "
7266 "the request: %s\n",
7273 size = hur_len(oldhur);
7275 fprintf(stderr, "hsm: cannot allocate "
7276 "%u files + %u bytes data\n",
7277 oldhur->hur_request.hr_itemcount,
7278 oldhur->hur_request.hr_data_len);
7285 memcpy(hur, oldhur, size);
7290 if (line[strlen(line) - 1] == '\n')
7291 line[strlen(line) - 1] = '\0';
7293 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7294 mntpath, line, &last_dev);
7300 if (some_file == NULL) {
7310 /* If a --data was used, add it to the request */
7311 hur->hur_request.hr_data_len = opaque_len;
7313 memcpy(hur_data(hur), opaque, opaque_len);
7315 /* Send the HSM request */
7316 if (realpath(some_file, fullpath) == NULL) {
7317 fprintf(stderr, "Could not find path '%s': %s\n",
7318 some_file, strerror(errno));
7320 rc = llapi_hsm_request(fullpath, hur);
7322 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7323 some_file, strerror(-rc));
7333 static int lfs_hsm_archive(int argc, char **argv)
7335 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7338 static int lfs_hsm_restore(int argc, char **argv)
7340 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7343 static int lfs_hsm_release(int argc, char **argv)
7345 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7348 static int lfs_hsm_remove(int argc, char **argv)
7350 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7353 static int lfs_hsm_cancel(int argc, char **argv)
7355 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7358 static int lfs_swap_layouts(int argc, char **argv)
7363 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7364 SWAP_LAYOUTS_KEEP_MTIME |
7365 SWAP_LAYOUTS_KEEP_ATIME);
7368 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7370 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7372 int lfs_get_mode(const char *string)
7374 enum lock_mode_user mode;
7376 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7377 if (lock_mode_names[mode] == NULL)
7379 if (strcmp(string, lock_mode_names[mode]) == 0)
7386 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7388 enum lu_ladvise_type advice;
7391 advice < ARRAY_SIZE(ladvise_names); advice++) {
7392 if (ladvise_names[advice] == NULL)
7394 if (strcmp(string, ladvise_names[advice]) == 0)
7398 return LU_LADVISE_INVALID;
7401 static int lfs_ladvise(int argc, char **argv)
7403 struct option long_opts[] = {
7404 { .val = 'a', .name = "advice", .has_arg = required_argument },
7405 { .val = 'b', .name = "background", .has_arg = no_argument },
7406 { .val = 'e', .name = "end", .has_arg = required_argument },
7407 { .val = 'l', .name = "length", .has_arg = required_argument },
7408 { .val = 'm', .name = "mode", .has_arg = required_argument },
7409 { .val = 's', .name = "start", .has_arg = required_argument },
7410 { .val = 'u', .name = "unset", .has_arg = no_argument },
7412 char short_opts[] = "a:be:l:m:s:u";
7417 struct llapi_lu_ladvise advice;
7418 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7419 unsigned long long start = 0;
7420 unsigned long long end = LUSTRE_EOF;
7421 unsigned long long length = 0;
7422 unsigned long long size_units;
7423 unsigned long long flags = 0;
7427 while ((c = getopt_long(argc, argv, short_opts,
7428 long_opts, NULL)) != -1) {
7431 advice_type = lfs_get_ladvice(optarg);
7432 if (advice_type == LU_LADVISE_INVALID) {
7433 fprintf(stderr, "%s: invalid advice type "
7434 "'%s'\n", argv[0], optarg);
7435 fprintf(stderr, "Valid types:");
7437 for (advice_type = 0;
7438 advice_type < ARRAY_SIZE(ladvise_names);
7440 if (ladvise_names[advice_type] == NULL)
7442 fprintf(stderr, " %s",
7443 ladvise_names[advice_type]);
7445 fprintf(stderr, "\n");
7458 rc = llapi_parse_size(optarg, &end,
7461 fprintf(stderr, "%s: bad end offset '%s'\n",
7468 rc = llapi_parse_size(optarg, &start,
7471 fprintf(stderr, "%s: bad start offset "
7472 "'%s'\n", argv[0], optarg);
7478 rc = llapi_parse_size(optarg, &length,
7481 fprintf(stderr, "%s: bad length '%s'\n",
7487 mode = lfs_get_mode(optarg);
7489 fprintf(stderr, "%s: bad mode '%s', valid "
7490 "modes are READ or WRITE\n",
7498 fprintf(stderr, "%s: option '%s' unrecognized\n",
7499 argv[0], argv[optind - 1]);
7504 if (advice_type == LU_LADVISE_INVALID) {
7505 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7506 fprintf(stderr, "Valid types:");
7507 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7509 if (ladvise_names[advice_type] == NULL)
7511 fprintf(stderr, " %s", ladvise_names[advice_type]);
7513 fprintf(stderr, "\n");
7517 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7518 fprintf(stderr, "%s: Lock no expand advice is a per file "
7519 "descriptor advice, so when called from lfs, "
7520 "it does nothing.\n", argv[0]);
7524 if (argc <= optind) {
7525 fprintf(stderr, "%s: please give one or more file names\n",
7530 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7531 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7536 if (end == LUSTRE_EOF && length != 0)
7537 end = start + length;
7540 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7541 argv[0], start, end);
7545 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7546 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7551 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7552 fprintf(stderr, "%s: mode is required with lockahead\n",
7557 while (optind < argc) {
7560 path = argv[optind++];
7562 fd = open(path, O_RDONLY);
7564 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7565 argv[0], path, strerror(errno));
7570 advice.lla_start = start;
7571 advice.lla_end = end;
7572 advice.lla_advice = advice_type;
7573 advice.lla_value1 = 0;
7574 advice.lla_value2 = 0;
7575 advice.lla_value3 = 0;
7576 advice.lla_value4 = 0;
7577 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7578 advice.lla_lockahead_mode = mode;
7579 advice.lla_peradvice_flags = flags;
7582 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7585 fprintf(stderr, "%s: cannot give advice '%s' to file "
7586 "'%s': %s\n", argv[0],
7587 ladvise_names[advice_type],
7588 path, strerror(errno));
7594 if (rc == 0 && rc2 < 0)
7600 /** The input string contains a comma delimited list of component ids and
7601 * ranges, for example "1,2-4,7".
7603 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7605 bool end_of_loop = false;
7613 while (!end_of_loop) {
7617 char *endptr = NULL;
7620 ptr = strchrnul(arg, ',');
7621 end_of_loop = *ptr == '\0';
7624 start_index = strtol(arg, &endptr, 0);
7625 if (endptr == arg) /* no data at all */
7627 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7629 if (start_index < 0)
7632 end_index = start_index;
7633 if (*endptr == '-') {
7634 end_index = strtol(endptr + 1, &endptr, 0);
7635 if (*endptr != '\0')
7637 if (end_index < start_index)
7641 for (i = start_index; i <= end_index && size > 0; i++) {
7644 /* remove duplicate */
7645 for (j = 0; j < nr; j++) {
7649 if (j == nr) { /* no duplicate */
7655 if (size == 0 && i < end_index)
7662 if (!end_of_loop && ptr != NULL)
7665 return rc < 0 ? rc : nr;
7669 * struct verify_mirror_id - Mirror id to be verified.
7670 * @mirror_id: A specified mirror id.
7671 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7673 struct verify_mirror_id {
7679 * compare_mirror_ids() - Compare mirror ids.
7680 * @layout: Mirror component list.
7681 * @cbdata: Callback data in verify_mirror_id structure.
7683 * This is a callback function called by llapi_layout_comp_iterate()
7684 * to compare the specified mirror id with the one in the current
7685 * component of @layout. If they are the same, then the specified
7686 * mirror id is valid.
7688 * Return: a negative error code on failure or
7689 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7690 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7693 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7695 struct verify_mirror_id *mirror_id_cbdata =
7696 (struct verify_mirror_id *)cbdata;
7700 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7704 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7705 progname, strerror(errno));
7709 if (mirror_id_cbdata->mirror_id == mirror_id) {
7710 mirror_id_cbdata->is_valid_id = true;
7711 return LLAPI_LAYOUT_ITER_STOP;
7714 return LLAPI_LAYOUT_ITER_CONT;
7718 * verify_mirror_ids() - Verify specified mirror ids.
7719 * @fname: Mirrored file name.
7720 * @mirror_ids: Specified mirror ids to be verified.
7721 * @ids_nr: Number of specified mirror ids.
7723 * This function verifies that specified @mirror_ids are valid
7724 * in the mirrored file @fname.
7726 * Return: 0 on success or a negative error code on failure.
7729 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7731 struct llapi_layout *layout = NULL;
7732 struct verify_mirror_id mirror_id_cbdata = { 0 };
7743 if (stat(fname, &stbuf) < 0) {
7744 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7745 progname, fname, strerror(errno));
7750 if (!S_ISREG(stbuf.st_mode)) {
7751 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7757 fd = open(fname, O_DIRECT | O_RDONLY);
7759 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7760 progname, fname, strerror(errno));
7765 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
7767 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
7768 progname, fname, strerror(errno));
7772 layout = llapi_layout_get_by_fd(fd, 0);
7773 if (layout == NULL) {
7774 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7775 progname, fname, strerror(errno));
7777 llapi_lease_release(fd);
7781 rc = llapi_layout_flags_get(layout, &flr_state);
7783 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7784 progname, fname, strerror(errno));
7789 flr_state &= LCM_FL_FLR_MASK;
7790 switch (flr_state) {
7793 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7794 progname, fname, llapi_layout_flags_string(flr_state));
7801 for (i = 0; i < ids_nr; i++) {
7802 mirror_id_cbdata.mirror_id = mirror_ids[i];
7803 mirror_id_cbdata.is_valid_id = false;
7805 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
7810 "%s: '%s' failed to verify mirror id: %u.\n",
7811 progname, fname, mirror_ids[i]);
7815 if (!mirror_id_cbdata.is_valid_id) {
7818 "%s: '%s' invalid specified mirror id: %u.\n",
7819 progname, fname, mirror_ids[i]);
7825 llapi_layout_free(layout);
7826 llapi_lease_release(fd);
7834 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
7835 __u16 *mirror_ids, int ids_nr)
7837 struct llapi_resync_comp comp_array[1024] = { { 0 } };
7838 struct llapi_layout *layout;
7846 if (stat(fname, &stbuf) < 0) {
7847 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7848 progname, fname, strerror(errno));
7852 if (!S_ISREG(stbuf.st_mode)) {
7853 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7859 fd = open(fname, O_DIRECT | O_RDWR);
7861 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7862 progname, fname, strerror(errno));
7867 layout = llapi_layout_get_by_fd(fd, 0);
7868 if (layout == NULL) {
7869 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7870 progname, fname, strerror(errno));
7875 rc = llapi_layout_flags_get(layout, &flr_state);
7877 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7878 progname, fname, strerror(errno));
7883 flr_state &= LCM_FL_FLR_MASK;
7884 if (flr_state == LCM_FL_NONE) {
7886 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
7891 /* get stale component info */
7892 comp_size = llapi_mirror_find_stale(layout, comp_array,
7893 ARRAY_SIZE(comp_array),
7894 mirror_ids, ids_nr);
7895 if (comp_size <= 0) {
7900 ioc->lil_mode = LL_LEASE_WRLCK;
7901 ioc->lil_flags = LL_LEASE_RESYNC;
7902 rc = llapi_lease_set(fd, ioc);
7904 if (rc == -EALREADY)
7908 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
7909 progname, fname, strerror(errno));
7914 while (idx < comp_size) {
7920 rc = llapi_lease_check(fd);
7921 if (rc != LL_LEASE_WRLCK) {
7922 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7927 mirror_id = comp_array[idx].lrc_mirror_id;
7928 end = comp_array[idx].lrc_end;
7930 /* try to combine adjacent component */
7931 for (i = idx + 1; i < comp_size; i++) {
7932 if (mirror_id != comp_array[i].lrc_mirror_id ||
7933 end != comp_array[i].lrc_start)
7935 end = comp_array[i].lrc_end;
7938 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7939 comp_array[idx].lrc_start,
7942 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7943 "%ld.\n", progname, fname, result);
7946 } else if (result > 0) {
7949 /* mark synced components */
7950 for (j = idx; j < i; j++)
7951 comp_array[j].lrc_synced = true;
7958 /* prepare ioc for lease put */
7959 ioc->lil_mode = LL_LEASE_UNLCK;
7960 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7962 for (idx = 0; idx < comp_size; idx++) {
7963 if (comp_array[idx].lrc_synced) {
7964 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7969 rc = llapi_lease_set(fd, ioc);
7971 if (rc == 0) /* lost lease lock */
7973 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7974 progname, fname, strerror(errno));
7978 * llapi_lease_set returns lease mode when it request to unlock
7984 llapi_layout_free(layout);
7991 static inline int lfs_mirror_resync(int argc, char **argv)
7993 struct ll_ioc_lease *ioc = NULL;
7994 __u16 mirror_ids[128] = { 0 };
7999 struct option long_opts[] = {
8000 { .val = 'o', .name = "only", .has_arg = required_argument },
8003 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8006 rc = parse_mirror_ids(mirror_ids,
8007 sizeof(mirror_ids) / sizeof(__u16),
8011 "%s: bad mirror ids '%s'.\n",
8018 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8019 argv[0], argv[optind - 1]);
8025 if (argc == optind) {
8026 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8031 if (ids_nr > 0 && argc > optind + 1) {
8033 "%s: option '--only' cannot be used upon multiple files.\n",
8041 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8046 /* set the lease on the file */
8047 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8049 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8050 argv[0], strerror(errno));
8055 for (; optind < argc; optind++) {
8056 rc = lfs_mirror_resync_file(argv[optind], ioc,
8057 mirror_ids, ids_nr);
8058 /* ignore previous file's error, continue with next file */
8061 memset(ioc, 0, sizeof(__u32) * 4096);
8070 * struct verify_chunk - Mirror chunk to be verified.
8071 * @chunk: [start, end) of the chunk.
8072 * @mirror_count: Number of mirror ids in @mirror_id array.
8073 * @mirror_id: Array of valid mirror ids that cover the chunk.
8075 struct verify_chunk {
8076 struct lu_extent chunk;
8077 unsigned int mirror_count;
8078 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
8082 * print_chunks() - Print chunk information.
8083 * @fname: Mirrored file name.
8084 * @chunks: Array of chunks.
8085 * @chunk_count: Number of chunks in @chunks array.
8087 * This function prints [start, end) of each chunk in @chunks
8088 * for mirrored file @fname, and also prints the valid mirror ids
8089 * that cover the chunk.
8094 void print_chunks(const char *fname, struct verify_chunk *chunks,
8100 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
8101 for (i = 0; i < chunk_count; i++) {
8102 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
8104 if (chunks[i].mirror_count == 0)
8105 fprintf(stdout, "\t[");
8107 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
8108 for (j = 1; j < chunks[i].mirror_count; j++)
8109 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
8111 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
8113 fprintf(stdout, "\n");
8117 * print_checksums() - Print CRC-32 checksum values.
8118 * @chunk: A chunk and its corresponding valid mirror ids.
8119 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
8121 * This function prints CRC-32 checksum values on @chunk for
8122 * each valid mirror that covers it.
8127 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
8132 "CRC-32 checksum value for chunk "DEXT":\n",
8133 PEXT(&chunk->chunk));
8134 for (i = 0; i < chunk->mirror_count; i++)
8135 fprintf(stdout, "Mirror %u:\t%#lx\n",
8136 chunk->mirror_id[i], crc[i]);
8137 fprintf(stdout, "\n");
8141 * filter_mirror_id() - Filter specified mirror ids.
8142 * @chunks: Array of chunks.
8143 * @chunk_count: Number of chunks in @chunks array.
8144 * @mirror_ids: Specified mirror ids to be verified.
8145 * @ids_nr: Number of specified mirror ids.
8147 * This function scans valid mirror ids that cover each chunk in @chunks
8148 * and filters specified mirror ids.
8153 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
8154 __u16 *mirror_ids, int ids_nr)
8159 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8160 unsigned int valid_count = 0;
8162 for (i = 0; i < chunk_count; i++) {
8163 if (chunks[i].mirror_count == 0)
8167 for (j = 0; j < ids_nr; j++) {
8168 for (k = 0; k < chunks[i].mirror_count; k++) {
8169 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
8170 valid_id[valid_count] = mirror_ids[j];
8177 memcpy(chunks[i].mirror_id, valid_id,
8178 sizeof(__u16) * valid_count);
8179 chunks[i].mirror_count = valid_count;
8184 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
8185 * @layout: Mirror component list.
8186 * @chunks: Array of chunks.
8187 * @chunks_size: Array size of @chunks.
8189 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
8190 * to find out chunk segments and store them in @chunks array.
8192 * The @mirror_id array in each element of @chunks will store the valid
8193 * mirror ids that cover the chunk. If a mirror component covering the
8194 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
8195 * will not be stored into the @mirror_id array, and the chunk for that
8196 * mirror will not be verified.
8198 * The @mirror_count in each element of @chunks will store the number of
8199 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
8200 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
8201 * indicates the chunk is valid in only one mirror. In both cases, the
8202 * chunk will not be verified.
8204 * Here is an example:
8207 * +------+-------------+--------------------+
8209 * +------+------+------+------+-------------+
8210 * | | S | S | | mirror2
8211 * +-------------+------+------+-------------+
8213 * prepared @chunks array will contain 5 elements:
8214 * (([0, 1M), [1, 2], 2),
8215 * ([1M, 2M), [1, 2], 2),
8216 * ([2M, 3M), [1], 1),
8217 * ([3M, 4M], [], 0),
8218 * ([4M, EOF), [2], 1))
8220 * Return: the actual array size of @chunks on success
8221 * or a negative error code on failure.
8224 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
8225 struct verify_chunk *chunks,
8236 memset(chunks, 0, sizeof(*chunks) * chunks_size);
8239 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
8242 "%s: move to the first layout component: %s.\n",
8243 progname, strerror(errno));
8249 chunks[idx].chunk.e_end = LUSTRE_EOF;
8251 rc = llapi_layout_comp_extent_get(layout, &start, &end);
8254 "%s: llapi_layout_comp_extent_get failed: %s.\n",
8255 progname, strerror(errno));
8259 if (start > chunks[idx].chunk.e_start ||
8260 end <= chunks[idx].chunk.e_start)
8263 if (end < chunks[idx].chunk.e_end)
8264 chunks[idx].chunk.e_end = end;
8266 rc = llapi_layout_comp_flags_get(layout, &flags);
8269 "%s: llapi_layout_comp_flags_get failed: %s.\n",
8270 progname, strerror(errno));
8274 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8277 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8280 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8281 progname, strerror(errno));
8285 chunks[idx].mirror_id[i] = mirror_id;
8287 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8289 "%s: mirror_id array is too small.\n",
8296 rc = llapi_layout_comp_use(layout,
8297 LLAPI_LAYOUT_COMP_USE_NEXT);
8300 "%s: move to the next layout component: %s.\n",
8301 progname, strerror(errno));
8304 } /* loop through all components */
8306 chunks[idx].mirror_count = i;
8308 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8312 if (idx >= chunks_size) {
8313 fprintf(stderr, "%s: chunks array is too small.\n",
8319 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
8323 return rc < 0 ? rc : idx + 1;
8327 * lfs_mirror_verify_chunk() - Verify a chunk.
8328 * @fd: File descriptor of the mirrored file.
8329 * @file_size: Size of the mirrored file.
8330 * @chunk: A chunk and its corresponding valid mirror ids.
8331 * @verbose: Verbose mode.
8333 * This function verifies a @chunk contains exactly the same data
8334 * ammong the mirrors that cover it.
8336 * If @verbose is specified, then the function will print where the
8337 * differences are if the data do not match. Otherwise, it will
8338 * just return an error in that case.
8340 * Return: 0 on success or a negative error code on failure.
8343 int lfs_mirror_verify_chunk(int fd, size_t file_size,
8344 struct verify_chunk *chunk, int verbose)
8346 const size_t buflen = 4 * 1024 * 1024; /* 4M */
8348 size_t page_size = sysconf(_SC_PAGESIZE);
8354 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8361 rc = posix_memalign(&buf, page_size, buflen);
8362 if (rc) /* error code is returned directly */
8366 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8367 PEXT(&chunk->chunk));
8368 for (i = 0; i < chunk->mirror_count; i++)
8369 fprintf(stdout, " %u", chunk->mirror_id[i]);
8370 fprintf(stdout, "\n");
8374 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
8375 pos = chunk->chunk.e_start;
8376 while (bytes_done < count) {
8377 /* compute initial CRC-32 checksum */
8378 crc = crc32(0L, Z_NULL, 0);
8379 memset(crc_array, 0, sizeof(crc_array));
8382 for (i = 0; i < chunk->mirror_count; i++) {
8383 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
8385 if (bytes_read < 0) {
8388 "%s: failed to read data from mirror %u: %s.\n",
8389 progname, chunk->mirror_id[i],
8394 /* compute new CRC-32 checksum */
8395 crc_array[i] = crc32(crc, buf, bytes_read);
8399 print_checksums(chunk, crc_array);
8401 /* compare CRC-32 checksum values */
8402 for (i = 1; i < chunk->mirror_count; i++) {
8403 if (crc_array[i] != crc_array[0]) {
8409 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
8410 progname, PEXT(&chunk->chunk),
8411 chunk->mirror_id[0],
8412 chunk->mirror_id[i]);
8417 bytes_done += bytes_read;
8420 if (verbose > 1 && rc == 0) {
8421 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8422 PEXT(&chunk->chunk));
8423 for (i = 0; i < chunk->mirror_count; i++)
8424 fprintf(stdout, " %u", chunk->mirror_id[i]);
8425 fprintf(stdout, " PASS\n\n");
8434 * lfs_mirror_verify_file() - Verify a mirrored file.
8435 * @fname: Mirrored file name.
8436 * @mirror_ids: Specified mirror ids to be verified.
8437 * @ids_nr: Number of specified mirror ids.
8438 * @verbose: Verbose mode.
8440 * This function verifies that each SYNC mirror of a mirrored file
8441 * specified by @fname contains exactly the same data.
8443 * If @mirror_ids is specified, then the function will verify the
8444 * mirrors specified by @mirror_ids contain exactly the same data.
8446 * If @verbose is specified, then the function will print where the
8447 * differences are if the data do not match. Otherwise, it will
8448 * just return an error in that case.
8450 * Return: 0 on success or a negative error code on failure.
8453 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8456 struct verify_chunk chunks_array[1024] = { };
8457 struct llapi_layout *layout = NULL;
8461 int chunk_count = 0;
8467 if (stat(fname, &stbuf) < 0) {
8468 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8469 progname, fname, strerror(errno));
8474 if (!S_ISREG(stbuf.st_mode)) {
8475 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8481 if (stbuf.st_size == 0) {
8483 fprintf(stdout, "%s: '%s' file size is 0.\n",
8489 fd = open(fname, O_DIRECT | O_RDONLY);
8491 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8492 progname, fname, strerror(errno));
8497 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8499 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8500 progname, fname, strerror(errno));
8504 layout = llapi_layout_get_by_fd(fd, 0);
8505 if (layout == NULL) {
8506 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8507 progname, fname, strerror(errno));
8509 llapi_lease_release(fd);
8513 rc = llapi_layout_flags_get(layout, &flr_state);
8515 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8516 progname, fname, strerror(errno));
8521 flr_state &= LCM_FL_FLR_MASK;
8522 switch (flr_state) {
8525 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8526 progname, fname, llapi_layout_flags_string(flr_state));
8532 /* find out mirror chunks to be verified */
8533 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8534 ARRAY_SIZE(chunks_array));
8535 if (chunk_count < 0) {
8541 /* filter specified mirror ids */
8542 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8545 print_chunks(fname, chunks_array, chunk_count);
8547 for (idx = 0; idx < chunk_count; idx++) {
8548 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8551 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8553 PEXT(&chunks_array[idx].chunk),
8554 (unsigned long long)stbuf.st_size);
8558 if (chunks_array[idx].mirror_count == 0) {
8560 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8562 PEXT(&chunks_array[idx].chunk));
8564 fprintf(stderr, "skipped\n");
8568 fprintf(stderr, "failed\n");
8572 if (chunks_array[idx].mirror_count == 1) {
8575 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8577 PEXT(&chunks_array[idx].chunk),
8578 chunks_array[idx].mirror_id[0]);
8582 rc = llapi_lease_check(fd);
8583 if (rc != LL_LEASE_RDLCK) {
8584 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8589 /* verify one chunk */
8590 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8591 &chunks_array[idx], verbose);
8605 llapi_layout_free(layout);
8606 llapi_lease_release(fd);
8614 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
8615 * @argc: The count of lfs mirror verify command line arguments.
8616 * @argv: Array of strings for lfs mirror verify command line arguments.
8618 * This function parses lfs mirror verify command and verifies the
8619 * specified mirrored file(s).
8621 * Return: 0 on success or a negative error code on failure.
8623 static inline int lfs_mirror_verify(int argc, char **argv)
8625 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8633 struct option long_opts[] = {
8634 { .val = 'o', .name = "only", .has_arg = required_argument },
8635 { .val = 'v', .name = "verbose", .has_arg = no_argument },
8638 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8640 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
8643 rc = parse_mirror_ids(mirror_ids,
8644 ARRAY_SIZE(mirror_ids),
8648 "%s: bad mirror ids '%s'.\n",
8655 "%s: at least 2 mirror ids needed with '--only' option.\n",
8665 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8666 progname, argv[optind - 1]);
8672 if (argc == optind) {
8673 fprintf(stderr, "%s: no file name given.\n", progname);
8678 if (ids_nr > 0 && argc > optind + 1) {
8680 "%s: '--only' cannot be used upon multiple files.\n",
8688 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8694 for (; optind < argc; optind++) {
8695 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
8705 * lfs_mirror() - Parse and execute lfs mirror commands.
8706 * @argc: The count of lfs mirror command line arguments.
8707 * @argv: Array of strings for lfs mirror command line arguments.
8709 * This function parses lfs mirror commands and performs the
8710 * corresponding functions specified in mirror_cmdlist[].
8712 * Return: 0 on success or an error code on failure.
8714 static int lfs_mirror(int argc, char **argv)
8721 Parser_init("lfs-mirror > ", mirror_cmdlist);
8723 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8725 program_invocation_short_name = cmd;
8727 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
8729 rc = Parser_commands();
8731 return rc < 0 ? -rc : rc;
8735 * lfs_mirror_list_commands() - List lfs mirror commands.
8736 * @argc: The count of command line arguments.
8737 * @argv: Array of strings for command line arguments.
8739 * This function lists lfs mirror commands defined in mirror_cmdlist[].
8741 * Return: 0 on success.
8743 static int lfs_mirror_list_commands(int argc, char **argv)
8745 char buffer[81] = "";
8747 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
8753 static int lfs_list_commands(int argc, char **argv)
8755 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
8757 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
8762 int main(int argc, char **argv)
8766 /* Ensure that liblustreapi constructor has run */
8767 if (!llapi_liblustreapi_initialized())
8768 fprintf(stderr, "liblustreapi was not properly initialized\n");
8773 Parser_init("lfs > ", cmdlist);
8775 progname = program_invocation_short_name; /* Used in error messages */
8777 llapi_set_command_name(argv[1]);
8778 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
8779 llapi_clear_command_name();
8781 rc = Parser_commands();
8784 return rc < 0 ? -rc : rc;
8787 #ifdef _LUSTRE_IDL_H_
8788 /* Everything we need here should be included by lustreapi.h. */
8789 # error "lfs should not depend on lustre_idl.h"
8790 #endif /* _LUSTRE_IDL_H_ */