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>
66 #include "lfs_project.h"
68 #include <libcfs/util/string.h>
69 #include <libcfs/util/ioctl.h>
70 #include <libcfs/util/parser.h>
71 #include <lustre/lustreapi.h>
72 #include <linux/lustre/lustre_ver.h>
73 #include <linux/lustre/lustre_param.h>
76 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
77 #endif /* !ARRAY_SIZE */
80 static int lfs_find(int argc, char **argv);
81 static int lfs_getstripe(int argc, char **argv);
82 static int lfs_getdirstripe(int argc, char **argv);
83 static int lfs_setdirstripe(int argc, char **argv);
84 static int lfs_rmentry(int argc, char **argv);
85 static int lfs_osts(int argc, char **argv);
86 static int lfs_mdts(int argc, char **argv);
87 static int lfs_df(int argc, char **argv);
88 static int lfs_getname(int argc, char **argv);
89 static int lfs_check(int argc, char **argv);
90 #ifdef HAVE_SYS_QUOTA_H
91 static int lfs_setquota(int argc, char **argv);
92 static int lfs_quota(int argc, char **argv);
93 static int lfs_project(int argc, char **argv);
95 static int lfs_flushctx(int argc, char **argv);
96 static int lfs_cp(int argc, char **argv);
97 static int lfs_ls(int argc, char **argv);
98 static int lfs_poollist(int argc, char **argv);
99 static int lfs_changelog(int argc, char **argv);
100 static int lfs_changelog_clear(int argc, char **argv);
101 static int lfs_fid2path(int argc, char **argv);
102 static int lfs_path2fid(int argc, char **argv);
103 static int lfs_data_version(int argc, char **argv);
104 static int lfs_hsm_state(int argc, char **argv);
105 static int lfs_hsm_set(int argc, char **argv);
106 static int lfs_hsm_clear(int argc, char **argv);
107 static int lfs_hsm_action(int argc, char **argv);
108 static int lfs_hsm_archive(int argc, char **argv);
109 static int lfs_hsm_restore(int argc, char **argv);
110 static int lfs_hsm_release(int argc, char **argv);
111 static int lfs_hsm_remove(int argc, char **argv);
112 static int lfs_hsm_cancel(int argc, char **argv);
113 static int lfs_swap_layouts(int argc, char **argv);
114 static int lfs_mv(int argc, char **argv);
115 static int lfs_ladvise(int argc, char **argv);
116 static int lfs_mirror(int argc, char **argv);
117 static int lfs_mirror_list_commands(int argc, char **argv);
118 static int lfs_list_commands(int argc, char **argv);
119 static inline int lfs_mirror_resync(int argc, char **argv);
120 static inline int lfs_mirror_verify(int argc, char **argv);
122 enum setstripe_origin {
128 static int lfs_setstripe0(int argc, char **argv, enum setstripe_origin opc);
130 static inline int lfs_setstripe(int argc, char **argv)
132 return lfs_setstripe0(argc, argv, SO_SETSTRIPE);
134 static inline int lfs_setstripe_migrate(int argc, char **argv)
136 return lfs_setstripe0(argc, argv, SO_MIGRATE);
138 static inline int lfs_mirror_create(int argc, char **argv)
140 return lfs_setstripe0(argc, argv, SO_MIRROR_CREATE);
142 static inline int lfs_mirror_extend(int argc, char **argv)
144 return lfs_setstripe0(argc, argv, SO_MIRROR_EXTEND);
147 /* Setstripe and migrate share mostly the same parameters */
148 #define SSM_CMD_COMMON(cmd) \
149 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
150 " [--stripe-count|-c <stripe_count>]\n" \
151 " [--stripe-index|-i <start_ost_idx>]\n" \
152 " [--stripe-size|-S <stripe_size>]\n" \
153 " [--layout|-L <pattern>]\n" \
154 " [--pool|-p <pool_name>]\n" \
155 " [--ost|-o <ost_indices>]\n"
157 #define SSM_HELP_COMMON \
158 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
159 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
160 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
161 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
162 "\t respectively)\n" \
163 "\tpool_name: Name of OST pool to use (default none)\n" \
164 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
165 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
166 "\t Indices be specified in a format of:\n" \
167 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
169 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
170 "\t If --pool is set with --ost, then the OSTs\n" \
171 "\t must be the members of the pool.\n" \
172 "\tcomp_end: Extent end of component, start after previous end.\n"\
173 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
174 "\t respectively, -1 for EOF). Must be a multiple of\n"\
177 #define MIRROR_CREATE_HELP \
178 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
179 "\t setstripe layout options\n" \
180 "\t It defaults to 1 if not specified; if specified,\n" \
181 "\t it must follow the option without a space.\n" \
182 "\t The option can also be repeated multiple times to\n" \
183 "\t separate mirrors that have different layouts.\n" \
184 "\tsetstripe options: Mirror layout\n" \
185 "\t It can be a plain layout or a composite layout.\n" \
186 "\t If not specified, the stripe options inherited\n" \
187 "\t from the previous component will be used.\n" \
188 "\tparent: Use default stripe options from parent directory\n" \
189 "\tflags: set flags to the component of the current mirror.\n" \
190 "\t Only \"prefer\" flag is supported so far.\n"
192 #define MIRROR_EXTEND_HELP \
194 "\tvictim_file: The layout of victim_file will be split and used\n" \
195 "\t as a mirror added to the mirrored file.\n" \
196 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
197 "\t from victim file(s) in case the victim file(s)\n" \
198 "\t contains the same data as the original mirrored\n" \
201 #define MIRROR_EXTEND_USAGE \
202 " <--mirror-count|-N[mirror_count]>\n" \
203 " [setstripe options|--parent|-f <victim_file>]\n" \
206 #define SETSTRIPE_USAGE \
207 SSM_CMD_COMMON("setstripe") \
208 MIRROR_EXTEND_USAGE \
209 " <directory|filename>\n" \
213 #define MIGRATE_USAGE \
214 SSM_CMD_COMMON("migrate ") \
216 " [--non-block|-n]\n" \
220 "\tblock: Block file access during data migration (default)\n" \
221 "\tnon-block: Abort migrations if concurrent access is detected\n" \
223 #define SETDIRSTRIPE_USAGE \
224 " [--mdt-count|-c stripe_count>\n" \
225 " [--mdt-index|-i mdt_index]\n" \
226 " [--mdt-hash|-H mdt_hash]\n" \
227 " [--default|-D] [--mode|-m mode] <dir>\n" \
228 "\tstripe_count: stripe count of the striped directory\n" \
229 "\tmdt_index: MDT index of first stripe\n" \
230 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
231 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
232 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
233 "\tdefault_stripe: set default dirstripe of the directory\n" \
234 "\tmode: the mode of the directory\n"
237 * command_t mirror_cmdlist - lfs mirror commands.
239 command_t mirror_cmdlist[] = {
240 { .pc_name = "create", .pc_func = lfs_mirror_create,
241 .pc_help = "Create a mirrored file.\n"
242 "usage: lfs mirror create "
243 "<--mirror-count|-N[mirror_count]> "
244 "[setstripe options|--parent] ... <filename|directory>\n"
245 MIRROR_CREATE_HELP },
246 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
247 .pc_help = "Extend a mirrored file.\n"
248 "usage: lfs mirror extend "
249 "<--mirror-count|-N[mirror_count]> [--no-verify] "
250 "[setstripe options|--parent|-f <victim_file>] ... <filename>\n"
251 MIRROR_EXTEND_HELP },
252 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
253 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
254 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
255 "<mirrored file> [<mirrored file2>...]\n"},
256 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
257 .pc_help = "Verify mirrored file(s).\n"
258 "usage: lfs mirror verify "
259 "[--only <mirror_id,mirror_id2[,...]>] "
260 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
261 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
262 .pc_help = "list commands supported by lfs mirror"},
263 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
264 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
265 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
269 /* all available commands */
270 command_t cmdlist[] = {
271 {"setstripe", lfs_setstripe, 0,
272 "To create a file with specified striping/composite layout, or\n"
273 "create/replace the default layout on an existing directory:\n"
274 SSM_CMD_COMMON("setstripe")
275 " <directory|filename>\n"
277 "To add component(s) to an existing composite file:\n"
278 SSM_CMD_COMMON("setstripe --component-add")
280 "To totally delete the default striping from an existing directory:\n"
281 "usage: setstripe -d <directory>\n"
283 "To delete the last component(s) from an existing composite file\n"
284 "(note that this will also delete any data in those components):\n"
285 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
286 " [--component-flags|-F <comp_flags>]\n"
288 "\tcomp_id: Unique component ID to delete\n"
289 "\tcomp_flags: 'init' indicating all instantiated components\n"
290 "\t '^init' indicating all uninstantiated components\n"
291 "\t-I and -F cannot be specified at the same time\n"},
292 {"getstripe", lfs_getstripe, 0,
293 "To list the striping info for a given file or files in a\n"
294 "directory or recursively for all files in a directory tree.\n"
295 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
296 " [--stripe-count|-c] [--stripe-index|-i]\n"
297 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
298 " [--mdt|-m] [--recursive|-r] [--raw|-R] [--yaml|-y]\n"
299 " [--layout|-L] [--fid|-F] [--generation|-g]\n"
300 " [--component-id[=comp_id]|-I[comp_id]]\n"
301 " [--component-flags[=comp_flags]]\n"
302 " [--component-count]\n"
303 " [--component-start[=[+-]comp_start]]\n"
304 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
305 " <directory|filename> ..."},
306 {"setdirstripe", lfs_setdirstripe, 0,
307 "To create a striped directory on a specified MDT. This can only\n"
308 "be done on MDT0 with the right of administrator.\n"
309 "usage: setdirstripe [OPTION] <directory>\n"
311 {"getdirstripe", lfs_getdirstripe, 0,
312 "To list the striping info for a given directory\n"
313 "or recursively for all directories in a directory tree.\n"
314 "usage: getdirstripe [--obd|-O <uuid>] [--mdt-count|-c]\n"
315 " [--mdt-index|-i] [--mdt-hash|-t]\n"
316 " [--recursive|-r] [--yaml|-y]\n"
317 " [--default|-D] <dir> ..."},
318 {"mkdir", lfs_setdirstripe, 0,
319 "To create a striped directory on a specified MDT. This can only\n"
320 "be done on MDT0 with the right of administrator.\n"
321 "usage: mkdir [OPTION] <directory>\n"
323 {"rm_entry", lfs_rmentry, 0,
324 "To remove the name entry of the remote directory. Note: This\n"
325 "command will only delete the name entry, i.e. the remote directory\n"
326 "will become inaccessable after this command. This can only be done\n"
327 "by the administrator\n"
328 "usage: rm_entry <dir>\n"},
329 {"pool_list", lfs_poollist, 0,
330 "List pools or pool OSTs\n"
331 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
332 {"find", lfs_find, 0,
333 "find files matching given attributes recursively in directory tree.\n"
334 "usage: find <directory|filename> ...\n"
335 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
336 " [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
337 " [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
338 " [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
339 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
340 " [[!] --stripe-count|-c [+-]<stripes>]\n"
341 " [[!] --stripe-index|-i <index,...>]\n"
342 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
343 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
344 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
345 " [[!] --projid <projid>]\n"
346 " [[!] --layout|-L released,raid0,mdt]\n"
347 " [[!] --component-count [+-]<comp_cnt>]\n"
348 " [[!] --component-start [+-]N[kMGTPE]]\n"
349 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
350 " [[!] --component-flags <comp_flags>]\n"
351 " [[!] --mdt-count|-T [+-]<stripes>]\n"
352 " [[!] --mdt-hash|-H <hashtype>\n"
353 "\t !: used before an option indicates 'NOT' requested attribute\n"
354 "\t -: used before a value indicates less than requested value\n"
355 "\t +: used before a value indicates more than requested value\n"
356 "\tmdt-hash: hash type of the striped directory.\n"
357 "\t fnv_1a_64 FNV-1a hash algorithm\n"
358 "\t all_char sum of characters % MDT_COUNT\n"},
359 {"check", lfs_check, 0,
360 "Display the status of MDS or OSTs (as specified in the command)\n"
361 "or all the servers (MDS and OSTs).\n"
362 "usage: check <osts|mds|servers>"},
363 {"osts", lfs_osts, 0, "list OSTs connected to client "
364 "[for specified path only]\n" "usage: osts [path]"},
365 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
366 "[for specified path only]\n" "usage: mdts [path]"},
368 "report filesystem disk space usage or inodes usage"
369 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
370 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
371 {"getname", lfs_getname, 0, "list instances and specified mount points "
372 "[for specified path only]\n"
373 "Usage: getname [-h]|[path ...] "},
374 #ifdef HAVE_SYS_QUOTA_H
375 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
376 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
377 " -b <block-softlimit> -B <block-hardlimit>\n"
378 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
379 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
380 " [--block-softlimit <block-softlimit>]\n"
381 " [--block-hardlimit <block-hardlimit>]\n"
382 " [--inode-softlimit <inode-softlimit>]\n"
383 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
384 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
385 " [--block-grace <block-grace>]\n"
386 " [--inode-grace <inode-grace>] <filesystem>\n"
387 " -b can be used instead of --block-softlimit/--block-grace\n"
388 " -B can be used instead of --block-hardlimit\n"
389 " -i can be used instead of --inode-softlimit/--inode-grace\n"
390 " -I can be used instead of --inode-hardlimit\n\n"
391 "Note: The total quota space will be split into many qunits and\n"
392 " balanced over all server targets, the minimal qunit size is\n"
393 " 1M bytes for block space and 1K inodes for inode space.\n\n"
394 " Quota space rebalancing process will stop when this mininum\n"
395 " value is reached. As a result, quota exceeded can be returned\n"
396 " while many targets still have 1MB or 1K inodes of spare\n"
398 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
399 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
401 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
402 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
403 {"project", lfs_project, 0,
404 "Change or list project attribute for specified file or directory.\n"
405 "usage: project [-d|-r] <file|directory...>\n"
406 " list project ID and flags on file(s) or directories\n"
407 " project [-p id] [-s] [-r] <file|directory...>\n"
408 " set project ID and/or inherit flag for specified file(s) or directories\n"
409 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
410 " check project ID and flags on file(s) or directories, print outliers\n"
411 " project -C [-r] [-k] <file|directory...>\n"
412 " clear the project inherit flag and ID on the file or directory\n"
415 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
416 "usage: flushctx [-k] [mountpoint...]"},
418 "Remote user copy files and directories.\n"
419 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
421 "Remote user list directory contents.\n"
422 "usage: ls [OPTION]... [FILE]..."},
423 {"changelog", lfs_changelog, 0,
424 "Show the metadata changes on an MDT."
425 "\nusage: changelog <mdtname> [startrec [endrec]]"},
426 {"changelog_clear", lfs_changelog_clear, 0,
427 "Indicate that old changelog records up to <endrec> are no longer of "
428 "interest to consumer <id>, allowing the system to free up space.\n"
429 "An <endrec> of 0 means all records.\n"
430 "usage: changelog_clear <mdtname> <id> <endrec>"},
431 {"fid2path", lfs_fid2path, 0,
432 "Resolve the full path(s) for given FID(s). For a specific hardlink "
433 "specify link number <linkno>.\n"
434 /* "For a historical link name, specify changelog record <recno>.\n" */
435 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
436 /* [ --rec <recno> ] */ },
437 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
438 "usage: path2fid [--parents] <path> ..."},
439 {"data_version", lfs_data_version, 0, "Display file data version for "
440 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
441 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
442 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
443 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
444 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
445 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
446 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
448 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
449 "[--archived] [--lost] <file> ..."},
450 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
451 "given files.\n" "usage: hsm_action <file> ..."},
452 {"hsm_archive", lfs_hsm_archive, 0,
453 "Archive file to external storage.\n"
454 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
456 {"hsm_restore", lfs_hsm_restore, 0,
457 "Restore file from external storage.\n"
458 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
459 {"hsm_release", lfs_hsm_release, 0,
460 "Release files from Lustre.\n"
461 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
462 {"hsm_remove", lfs_hsm_remove, 0,
463 "Remove file copy from external storage.\n"
464 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
466 " (FILE [FILE ...] | "
467 "--mntpath MOUNTPATH FID [FID ...])\n"
469 "Note: To remove an archived copy of a file already deleted from a "
471 "--mntpath option and a list of FIDs must be specified"
473 {"hsm_cancel", lfs_hsm_cancel, 0,
474 "Cancel requests related to specified files.\n"
475 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
476 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
477 "usage: swap_layouts <path1> <path2>"},
478 {"migrate", lfs_setstripe_migrate, 0,
479 "migrate a directory between MDTs.\n"
480 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
482 "\tmdt_idx: index of the destination MDT\n"
484 "migrate file objects from one OST "
485 "layout\nto another (may be not safe with concurent writes).\n"
487 "[--stripe-count|-c] <stripe_count>\n"
488 " [--stripe-index|-i] <start_ost_index>\n"
489 " [--stripe-size|-S] <stripe_size>\n"
490 " [--pool|-p] <pool_name>\n"
491 " [--ost-list|-o] <ost_indices>\n"
493 " [--non-block|-n]\n"
494 " <file|directory>\n"
495 "\tstripe_count: number of OSTs to stripe a file over\n"
496 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
497 "\tstripe_size: number of bytes to store before moving to the next OST\n"
498 "\tpool_name: name of the predefined pool of OSTs\n"
499 "\tost_indices: OSTs to stripe over, in order\n"
500 "\tblock: wait for the operation to return before continuing\n"
501 "\tnon-block: do not wait for the operation to return.\n"},
503 "To move directories between MDTs. This command is deprecated, "
504 "use \"migrate\" instead.\n"
505 "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
507 {"ladvise", lfs_ladvise, 0,
508 "Provide servers with advice about access patterns for a file.\n"
509 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
510 " [--background|-b] [--unset|-u]\n\n"
511 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
512 " {[--mode|-m [READ,WRITE]}\n"
514 {"mirror", lfs_mirror, mirror_cmdlist,
515 "lfs commands used to manage files with mirrored components:\n"
516 "lfs mirror create - create a mirrored file or directory\n"
517 "lfs mirror extend - add mirror(s) to an existing file\n"
518 "lfs mirror split - split a mirror from an existing mirrored file\n"
519 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
520 "lfs mirror verify - verify mirrored file(s)\n"},
521 {"help", Parser_help, 0, "help"},
522 {"exit", Parser_quit, 0, "quit"},
523 {"quit", Parser_quit, 0, "quit"},
524 {"--version", Parser_version, 0,
525 "output build version of the utility and exit"},
526 {"--list-commands", lfs_list_commands, 0,
527 "list commands supported by the utility and exit"},
532 static int check_hashtype(const char *hashtype)
536 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
537 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
544 static const char *error_loc = "syserror";
547 MIGRATION_NONBLOCK = 1 << 0,
548 MIGRATION_MIRROR = 1 << 1,
551 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
552 struct llapi_layout *layout);
555 migrate_open_files(const char *name, const struct llapi_stripe_param *param,
556 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
562 char parent[PATH_MAX];
563 char volatile_file[PATH_MAX];
569 if (param == NULL && layout == NULL) {
570 error_loc = "layout information";
574 /* search for file directory pathname */
575 if (strlen(name) > sizeof(parent) - 1) {
576 error_loc = "source file name";
580 strncpy(parent, name, sizeof(parent));
581 ptr = strrchr(parent, '/');
583 if (getcwd(parent, sizeof(parent)) == NULL) {
584 error_loc = "getcwd";
588 if (ptr == parent) /* leading '/' */
593 /* open file, direct io */
594 /* even if the file is only read, WR mode is nedeed to allow
595 * layout swap on fd */
596 fd = open(name, O_RDWR | O_DIRECT);
599 error_loc = "cannot open source file";
603 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
605 error_loc = "cannot get MDT index";
610 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
611 mode_t open_mode = S_IRUSR | S_IWUSR;
613 random_value = random();
614 rc = snprintf(volatile_file, sizeof(volatile_file),
615 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
616 mdt_index, random_value);
617 if (rc >= sizeof(volatile_file)) {
622 /* create, open a volatile file, use caching (ie no directio) */
624 fdv = llapi_file_open_param(volatile_file, open_flags,
627 fdv = lfs_component_create(volatile_file, open_flags,
629 } while (fdv < 0 && (rc = fdv) == -EEXIST);
632 error_loc = "cannot create volatile file";
636 /* In case the MDT does not support creation of volatile files
637 * we should try to unlink it. */
638 (void)unlink(volatile_file);
640 /* Not-owner (root?) special case.
641 * Need to set owner/group of volatile file like original.
642 * This will allow to pass related check during layout_swap.
647 error_loc = "cannot stat source file";
651 rc = fstat(fdv, &stv);
654 error_loc = "cannot stat volatile";
658 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
659 rc = fchown(fdv, st.st_uid, st.st_gid);
662 error_loc = "cannot change ownwership of volatile";
681 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
683 struct llapi_layout *layout;
684 size_t buf_size = 4 * 1024 * 1024;
693 layout = llapi_layout_get_by_fd(fd_src, 0);
694 if (layout != NULL) {
695 uint64_t stripe_size;
697 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
699 buf_size = stripe_size;
701 llapi_layout_free(layout);
704 /* Use a page-aligned buffer for direct I/O */
705 rc = posix_memalign(&buf, getpagesize(), buf_size);
710 /* read new data only if we have written all
711 * previously read data */
714 rc = check_file(fd_src);
719 rsize = read(fd_src, buf, buf_size);
731 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
750 static int migrate_copy_timestamps(int fd, int fdv)
754 if (fstat(fd, &st) == 0) {
755 struct timeval tv[2] = {
756 {.tv_sec = st.st_atime},
757 {.tv_sec = st.st_mtime}
760 return futimes(fdv, tv);
766 static int migrate_block(int fd, int fdv)
773 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
775 error_loc = "cannot get dataversion";
783 /* The grouplock blocks all concurrent accesses to the file.
784 * It has to be taken after llapi_get_data_version as it would
786 rc = llapi_group_lock(fd, gid);
788 error_loc = "cannot get group lock";
792 rc = migrate_copy_data(fd, fdv, NULL);
794 error_loc = "data copy failed";
798 /* Make sure we keep original atime/mtime values */
799 rc = migrate_copy_timestamps(fd, fdv);
801 error_loc = "timestamp copy failed";
806 * for a migration we need to check data version on file did
809 * Pass in gid=0 since we already own grouplock. */
810 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
811 SWAP_LAYOUTS_CHECK_DV1);
813 error_loc = "file changed";
816 error_loc = "cannot swap layout";
821 rc2 = llapi_group_unlock(fd, gid);
822 if (rc2 < 0 && rc == 0) {
823 error_loc = "unlock group lock";
831 * Internal helper for migrate_copy_data(). Check lease and report error if
834 * \param[in] fd File descriptor on which to check the lease.
836 * \retval 0 Migration can keep on going.
837 * \retval -errno Error occurred, abort migration.
839 static int check_lease(int fd)
843 rc = llapi_lease_check(fd);
845 return 0; /* llapi_check_lease returns > 0 on success. */
850 static int migrate_nonblock(int fd, int fdv)
856 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
858 error_loc = "cannot get data version";
862 rc = migrate_copy_data(fd, fdv, check_lease);
864 error_loc = "data copy failed";
868 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
870 error_loc = "cannot get data version";
876 error_loc = "source file changed";
880 /* Make sure we keep original atime/mtime values */
881 rc = migrate_copy_timestamps(fd, fdv);
883 error_loc = "timestamp copy failed";
890 static int lfs_component_set(char *fname, int comp_id,
891 __u32 flags, __u32 neg_flags)
894 __u32 flags_array[2];
899 ids[count] = comp_id;
900 flags_array[count] = flags;
905 ids[count] = comp_id;
906 flags_array[count] = neg_flags | LCME_FL_NEG;
910 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
913 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
914 progname, comp_id, fname, flags, neg_flags);
919 static int lfs_component_del(char *fname, __u32 comp_id,
920 __u32 flags, __u32 neg_flags)
924 if (flags && neg_flags)
927 if (!flags && neg_flags)
928 flags = neg_flags | LCME_FL_NEG;
930 if ((flags && comp_id) || (!flags && !comp_id))
933 /* LCME_FL_INIT is the only supported flag in PFL */
935 if (flags & ~LCME_KNOWN_FLAGS) {
937 "%s setstripe: unknown flags %#x\n",
941 } else if (comp_id > LCME_ID_MAX) {
942 fprintf(stderr, "%s setstripe: invalid component id %u\n",
947 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
950 "%s setstripe: cannot delete component %#x from '%s': %s\n",
951 progname, comp_id, fname, strerror(errno));
955 static int lfs_component_add(char *fname, struct llapi_layout *layout)
962 rc = llapi_layout_file_comp_add(fname, layout);
964 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
965 fname, strerror(errno));
969 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
970 struct llapi_layout *layout)
978 fd = lstat(fname, &st);
979 if (fd == 0 && S_ISDIR(st.st_mode))
980 open_flags = O_DIRECTORY | O_RDONLY;
982 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
984 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
985 S_ISDIR(st.st_mode) ?
986 "set default composite layout for" :
987 "create composite file",
988 fname, strerror(errno));
992 static int lfs_migrate(char *name, __u64 migration_flags,
993 struct llapi_stripe_param *param,
994 struct llapi_layout *layout)
1000 rc = migrate_open_files(name, param, layout, &fd, &fdv);
1004 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1005 /* Blocking mode (forced if servers do not support file lease).
1006 * It is also the default mode, since we cannot distinguish
1007 * between a broken lease and a server that does not support
1008 * atomic swap/close (LU-6785) */
1009 rc = migrate_block(fd, fdv);
1013 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
1015 error_loc = "cannot get lease";
1019 rc = migrate_nonblock(fd, fdv);
1021 llapi_lease_put(fd);
1025 /* Atomically put lease, swap layouts and close.
1026 * for a migration we need to check data version on file did
1028 rc = llapi_fswap_layouts(fd, fdv, 0, 0,
1029 migration_flags & MIGRATION_MIRROR ?
1030 MERGE_LAYOUTS_CLOSE : SWAP_LAYOUTS_CLOSE);
1032 error_loc = "cannot swap layout";
1044 fprintf(stderr, "error: %s: %s: %s: %s\n",
1045 progname, name, error_loc, strerror(-rc));
1049 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1058 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1062 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1063 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1064 const char *comp_name = comp_flags_table[i].cfn_name;
1066 if (strcmp(name, comp_name) == 0) {
1067 *flags |= comp_flag;
1069 } else if (strncmp(name, "^", 1) == 0 &&
1070 strcmp(name + 1, comp_name) == 0) {
1071 *neg_flags |= comp_flag;
1076 llapi_printf(LLAPI_MSG_ERROR,
1077 "%s: component flag '%s' not supported\n",
1083 if (!*flags && !*neg_flags)
1086 /* don't allow to set and exclude the same flag */
1087 if (*flags & *neg_flags)
1094 * struct mirror_args - Command-line arguments for mirror(s).
1095 * @m_count: Number of mirrors to be created with this layout.
1096 * @m_flags: Mirror level flags, only 'prefer' is supported.
1097 * @m_layout: Mirror layout.
1098 * @m_file: A victim file. Its layout will be split and used as a mirror.
1099 * @m_next: Point to the next node of the list.
1101 * Command-line arguments for mirror(s) will be parsed and stored in
1102 * a linked list that consists of this structure.
1104 struct mirror_args {
1107 struct llapi_layout *m_layout;
1109 struct mirror_args *m_next;
1112 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1117 rc = llapi_layout_comp_flags_get(layout, &flags);
1121 if (flags & LCME_FL_NEG) {
1122 fprintf(stderr, "error: %s: negative flags are not supported\n",
1127 if (flags & LCME_FL_STALE) {
1128 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1129 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1133 return LLAPI_LAYOUT_ITER_CONT;
1136 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1138 uint64_t start, end;
1142 /* LU-10112: do not support dom+flr in phase 1 */
1143 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1147 rc = llapi_layout_pattern_get(layout, &pattern);
1151 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1152 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1157 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1161 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1165 if (end != LUSTRE_EOF) {
1166 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1171 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1176 * enum mirror_flags - Flags for extending a mirrored file.
1177 * @NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1178 * in case the victim file(s) contains the same data as the
1179 * original mirrored file.
1181 * Flags for extending a mirrored file.
1188 * mirror_create_sanity_check() - Check mirror list.
1189 * @list: A linked list that stores the mirror arguments.
1191 * This function does a sanity check on @list for creating
1194 * Return: 0 on success or a negative error code on failure.
1196 static int mirror_create_sanity_check(const char *fname,
1197 struct mirror_args *list)
1200 bool has_m_file = false;
1201 bool has_m_layout = false;
1207 struct llapi_layout *layout;
1209 layout = llapi_layout_get_by_path(fname, 0);
1212 "error: %s: file '%s' couldn't get layout\n",
1217 rc = mirror_sanity_check_one(layout);
1218 llapi_layout_free(layout);
1224 while (list != NULL) {
1225 if (list->m_file != NULL) {
1227 llapi_layout_free(list->m_layout);
1230 llapi_layout_get_by_path(list->m_file, 0);
1231 if (list->m_layout == NULL) {
1233 "error: %s: file '%s' has no layout\n",
1234 progname, list->m_file);
1238 has_m_layout = true;
1239 if (list->m_layout == NULL) {
1240 fprintf(stderr, "error: %s: no mirror layout\n",
1246 rc = mirror_sanity_check_one(list->m_layout);
1250 list = list->m_next;
1253 if (has_m_file && has_m_layout) {
1254 fprintf(stderr, "error: %s: -f <victim_file> option should not "
1255 "be specified with setstripe options or "
1256 "--parent option\n", progname);
1263 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1265 __u32 mirror_flags = *(__u32 *)cbdata;
1269 rc = llapi_layout_comp_flags_get(layout, &flags);
1274 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1279 return LLAPI_LAYOUT_ITER_CONT;
1283 * mirror_create() - Create a mirrored file.
1284 * @fname: The file to be created.
1285 * @mirror_list: A linked list that stores the mirror arguments.
1287 * This function creates a mirrored file @fname with the mirror(s)
1288 * from @mirror_list.
1290 * Return: 0 on success or a negative error code on failure.
1292 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1294 struct llapi_layout *layout = NULL;
1295 struct mirror_args *cur_mirror = NULL;
1296 uint16_t mirror_count = 0;
1300 rc = mirror_create_sanity_check(NULL, mirror_list);
1304 cur_mirror = mirror_list;
1305 while (cur_mirror != NULL) {
1306 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1308 &cur_mirror->m_flags);
1311 fprintf(stderr, "%s: failed to set mirror flags\n",
1316 for (i = 0; i < cur_mirror->m_count; i++) {
1317 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1320 fprintf(stderr, "error: %s: "
1321 "merge layout failed: %s\n",
1322 progname, strerror(errno));
1326 mirror_count += cur_mirror->m_count;
1327 cur_mirror = cur_mirror->m_next;
1330 if (layout == NULL) {
1331 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1335 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1338 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1339 progname, strerror(errno));
1343 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0644,
1351 llapi_layout_free(layout);
1356 * Compare files and check lease on @fd.
1358 * \retval bytes number of bytes are the same
1360 static ssize_t mirror_file_compare(int fd, int fdv)
1362 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1364 ssize_t bytes_done = 0;
1365 ssize_t bytes_read = 0;
1367 buf = malloc(buflen * 2);
1372 if (!llapi_lease_check(fd)) {
1373 bytes_done = -EBUSY;
1377 bytes_read = read(fd, buf, buflen);
1378 if (bytes_read <= 0)
1381 if (bytes_read != read(fdv, buf + buflen, buflen))
1384 /* XXX: should compute the checksum on each buffer and then
1385 * compare checksum to avoid cache collision */
1386 if (memcmp(buf, buf + buflen, bytes_read))
1389 bytes_done += bytes_read;
1397 static int mirror_extend_file(const char *fname, const char *victim_file,
1398 enum mirror_flags mirror_flags)
1403 struct stat stbuf_v;
1406 fd = open(fname, O_RDWR);
1408 error_loc = "open source file";
1413 fdv = open(victim_file, O_RDWR);
1415 error_loc = "open target file";
1420 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1421 error_loc = "stat source or target file";
1426 if (stbuf.st_dev != stbuf_v.st_dev) {
1427 error_loc = "stat source and target file";
1432 /* mirrors should be of the same size */
1433 if (stbuf.st_size != stbuf_v.st_size) {
1434 error_loc = "file sizes don't match";
1439 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
1441 error_loc = "cannot get lease";
1445 if (!(mirror_flags & NO_VERIFY)) {
1447 /* mirrors should have the same contents */
1448 ret = mirror_file_compare(fd, fdv);
1449 if (ret != stbuf.st_size) {
1450 error_loc = "file busy or contents don't match";
1451 rc = ret < 0 ? ret : -EINVAL;
1456 /* Get rid of caching pages from clients */
1457 rc = llapi_file_flush(fd);
1459 error_loc = "cannot get data version";
1463 rc = llapi_file_flush(fdv);
1465 error_loc = "cannot get data version";
1470 /* Make sure we keep original atime/mtime values */
1471 rc = migrate_copy_timestamps(fd, fdv);
1473 error_loc = "cannot copy timestamp";
1477 /* Atomically put lease, swap layouts and close.
1478 * for a migration we need to check data version on file did
1480 rc = llapi_fswap_layouts(fd, fdv, 0, 0, MERGE_LAYOUTS_CLOSE);
1482 error_loc = "cannot swap layout";
1494 (void) unlink(victim_file);
1497 fprintf(stderr, "error: %s: %s: %s: %s\n",
1498 progname, fname, error_loc, strerror(-rc));
1502 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1503 enum mirror_flags mirror_flags)
1507 rc = mirror_create_sanity_check(fname, mirror_list);
1511 while (mirror_list) {
1512 if (mirror_list->m_file != NULL) {
1513 rc = mirror_extend_file(fname, mirror_list->m_file,
1516 __u32 mirror_count = mirror_list->m_count;
1518 while (mirror_count > 0) {
1519 rc = lfs_migrate(fname,
1520 MIGRATION_NONBLOCK | MIGRATION_MIRROR,
1521 NULL, mirror_list->m_layout);
1531 mirror_list = mirror_list->m_next;
1538 * Parse a string containing an target index list into an array of integers.
1540 * The input string contains a comma delimited list of individual
1541 * indices and ranges, for example "1,2-4,7". Add the indices into the
1542 * \a tgts array and remove duplicates.
1544 * \param[out] tgts array to store indices in
1545 * \param[in] size size of \a tgts array
1546 * \param[in] offset starting index in \a tgts
1547 * \param[in] arg string containing OST index list
1549 * \retval positive number of indices in \a tgts
1550 * \retval -EINVAL unable to parse \a arg
1552 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1556 int slots = size - offset;
1563 end_of_loop = false;
1564 while (!end_of_loop) {
1568 char *endptr = NULL;
1572 ptr = strchrnul(arg, ',');
1574 end_of_loop = *ptr == '\0';
1577 start_index = strtol(arg, &endptr, 0);
1578 if (endptr == arg) /* no data at all */
1580 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1583 end_index = start_index;
1584 if (*endptr == '-') {
1585 end_index = strtol(endptr + 1, &endptr, 0);
1586 if (*endptr != '\0')
1588 if (end_index < start_index)
1592 for (i = start_index; i <= end_index && slots > 0; i++) {
1595 /* remove duplicate */
1596 for (j = 0; j < offset; j++) {
1600 if (j == offset) { /* no duplicate */
1605 if (slots == 0 && i < end_index)
1613 if (!end_of_loop && ptr != NULL)
1616 return rc < 0 ? rc : nr;
1619 struct lfs_setstripe_args {
1620 unsigned long long lsa_comp_end;
1621 unsigned long long lsa_stripe_size;
1622 long long lsa_stripe_count;
1623 long long lsa_stripe_off;
1624 __u32 lsa_comp_flags;
1625 __u32 lsa_comp_neg_flags;
1626 unsigned long long lsa_pattern;
1629 char *lsa_pool_name;
1632 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1634 memset(lsa, 0, sizeof(*lsa));
1636 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1637 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1638 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1639 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1640 lsa->lsa_pool_name = NULL;
1644 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1645 * @lsa: Stripe options to be initialized and inherited.
1647 * This function initializes stripe options in @lsa and inherit
1648 * stripe_size, stripe_count and OST pool_name options.
1652 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1654 unsigned long long stripe_size;
1655 long long stripe_count;
1656 char *pool_name = NULL;
1658 stripe_size = lsa->lsa_stripe_size;
1659 stripe_count = lsa->lsa_stripe_count;
1660 pool_name = lsa->lsa_pool_name;
1662 setstripe_args_init(lsa);
1664 lsa->lsa_stripe_size = stripe_size;
1665 lsa->lsa_stripe_count = stripe_count;
1666 lsa->lsa_pool_name = pool_name;
1669 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1671 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1672 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1673 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1674 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1675 lsa->lsa_pool_name != NULL ||
1676 lsa->lsa_comp_end != 0);
1680 * comp_args_to_layout() - Create or extend a composite layout.
1681 * @composite: Pointer to the composite layout.
1682 * @lsa: Stripe options for the new component.
1684 * This function creates or extends a composite layout by adding a new
1685 * component with stripe options from @lsa.
1687 * Return: 0 on success or an error code on failure.
1689 static int comp_args_to_layout(struct llapi_layout **composite,
1690 struct lfs_setstripe_args *lsa)
1692 struct llapi_layout *layout = *composite;
1693 uint64_t prev_end = 0;
1696 if (layout == NULL) {
1697 layout = llapi_layout_alloc();
1698 if (layout == NULL) {
1699 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1703 *composite = layout;
1707 /* Get current component extent, current component
1708 * must be the tail component. */
1709 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
1711 fprintf(stderr, "Get comp extent failed. %s\n",
1716 rc = llapi_layout_comp_add(layout);
1718 fprintf(stderr, "Add component failed. %s\n",
1724 rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
1726 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
1727 prev_end, lsa->lsa_comp_end, strerror(errno));
1731 /* Data-on-MDT component setting */
1732 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
1733 /* In case of Data-on-MDT patterns the only extra option
1734 * applicable is stripe size option. */
1735 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
1736 fprintf(stderr, "Option 'stripe-count' can't be "
1737 "specified with Data-on-MDT component: %lld\n",
1738 lsa->lsa_stripe_count);
1741 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
1742 fprintf(stderr, "Option 'stripe-size' can't be "
1743 "specified with Data-on-MDT component: %llu\n",
1744 lsa->lsa_stripe_size);
1747 if (lsa->lsa_nr_tgts != 0) {
1748 fprintf(stderr, "Option 'ost-list' can't be specified "
1749 "with Data-on-MDT component: '%i'\n",
1753 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
1754 fprintf(stderr, "Option 'stripe-offset' can't be "
1755 "specified with Data-on-MDT component: %lld\n",
1756 lsa->lsa_stripe_off);
1759 if (lsa->lsa_pool_name != 0) {
1760 fprintf(stderr, "Option 'pool' can't be specified "
1761 "with Data-on-MDT component: '%s'\n",
1762 lsa->lsa_pool_name);
1766 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
1768 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
1769 lsa->lsa_pattern, strerror(errno));
1772 /* Data-on-MDT component has always single stripe up to end */
1773 lsa->lsa_stripe_size = lsa->lsa_comp_end;
1776 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
1778 fprintf(stderr, "Set stripe size %llu failed: %s\n",
1779 lsa->lsa_stripe_size, strerror(errno));
1783 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
1785 fprintf(stderr, "Set stripe count %lld failed: %s\n",
1786 lsa->lsa_stripe_count, strerror(errno));
1790 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
1792 fprintf(stderr, "Set flags 0x%x failed: %s\n",
1793 lsa->lsa_comp_flags, strerror(errno));
1797 if (lsa->lsa_pool_name != NULL) {
1798 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
1800 fprintf(stderr, "Set pool name: %s failed. %s\n",
1801 lsa->lsa_pool_name, strerror(errno));
1805 rc = llapi_layout_pool_name_set(layout, "");
1807 fprintf(stderr, "Clear pool name failed: %s\n",
1813 if (lsa->lsa_nr_tgts > 0) {
1814 if (lsa->lsa_stripe_count > 0 &&
1815 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
1816 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
1817 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
1818 fprintf(stderr, "stripe_count(%lld) != nr_osts(%d)\n",
1819 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
1822 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
1823 rc = llapi_layout_ost_index_set(layout, i,
1828 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
1829 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
1832 fprintf(stderr, "Set ost index %d failed. %s\n",
1833 i, strerror(errno));
1840 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
1841 * end of the last component in the existing file, and adjust the
1842 * first extent start of the components to be added accordingly. */
1843 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
1845 struct llapi_layout *head;
1846 uint64_t start, end, stripe_size, prev_end = 0;
1849 if (layout == NULL) {
1851 "%s setstripe: layout must be specified\n",
1857 head = llapi_layout_get_by_path(fname, 0);
1860 "%s setstripe: cannot read layout from '%s': %s\n",
1861 progname, fname, strerror(errno));
1863 } else if (errno == ENODATA) {
1864 /* file without LOVEA, this component-add will be turned
1865 * into a component-create. */
1866 llapi_layout_free(head);
1868 } else if (!llapi_layout_is_composite(head)) {
1869 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
1871 llapi_layout_free(head);
1875 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
1877 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
1878 progname, strerror(errno));
1879 llapi_layout_free(head);
1883 llapi_layout_free(head);
1885 /* Make sure we use the first component of the layout to be added. */
1886 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1889 "%s setstripe: cannot move component cursor: %s\n",
1890 progname, strerror(errno));
1894 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1896 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
1897 progname, strerror(errno));
1901 if (start > prev_end || end <= prev_end) {
1903 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
1904 progname, start, end, prev_end);
1908 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
1910 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
1911 progname, strerror(errno));
1915 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
1916 (prev_end & (stripe_size - 1))) {
1918 "%s setstripe: stripe size %lu not aligned with %lu\n",
1919 progname, stripe_size, prev_end);
1923 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
1926 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
1927 progname, prev_end, end, strerror(errno));
1934 static inline bool arg_is_eof(char *arg)
1936 return !strncmp(arg, "-1", strlen("-1")) ||
1937 !strncmp(arg, "EOF", strlen("EOF")) ||
1938 !strncmp(arg, "eof", strlen("eof"));
1942 * lfs_mirror_alloc() - Allocate a mirror argument structure.
1944 * Return: Valid mirror_args pointer on success and
1945 * NULL if memory allocation fails.
1947 static struct mirror_args *lfs_mirror_alloc(void)
1949 struct mirror_args *mirror = NULL;
1952 mirror = calloc(1, sizeof(*mirror));
1963 * lfs_mirror_free() - Free memory allocated for a mirror argument
1965 * @mirror: Previously allocated mirror argument structure by
1966 * lfs_mirror_alloc().
1968 * Free memory allocated for @mirror.
1972 static void lfs_mirror_free(struct mirror_args *mirror)
1974 if (mirror->m_layout != NULL)
1975 llapi_layout_free(mirror->m_layout);
1980 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
1981 * @mirror_list: Previously allocated mirror list.
1983 * Free memory allocated for @mirror_list.
1987 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
1989 struct mirror_args *next_mirror = NULL;
1991 while (mirror_list != NULL) {
1992 next_mirror = mirror_list->m_next;
1993 lfs_mirror_free(mirror_list);
1994 mirror_list = next_mirror;
2006 LFS_COMP_USE_PARENT_OPT,
2007 LFS_COMP_NO_VERIFY_OPT,
2009 LFS_MIRROR_FLAGS_OPT,
2013 static int lfs_setstripe0(int argc, char **argv, enum setstripe_origin opc)
2015 struct lfs_setstripe_args lsa;
2016 struct llapi_stripe_param *param = NULL;
2017 struct find_param migrate_mdt_param = {
2027 char *mdt_idx_arg = NULL;
2028 unsigned long long size_units = 1;
2029 bool migrate_mode = false;
2030 bool migration_block = false;
2031 __u64 migration_flags = 0;
2032 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2033 int comp_del = 0, comp_set = 0;
2036 struct llapi_layout *layout = NULL;
2037 struct llapi_layout **lpp = &layout;
2038 bool mirror_mode = false;
2039 bool has_m_file = false;
2040 __u32 mirror_count = 0;
2041 enum mirror_flags mirror_flags = 0;
2042 struct mirror_args *mirror_list = NULL;
2043 struct mirror_args *new_mirror = NULL;
2044 struct mirror_args *last_mirror = NULL;
2047 struct option long_opts[] = {
2048 /* --block is only valid in migrate mode */
2049 { .val = 'b', .name = "block", .has_arg = no_argument},
2050 { .val = LFS_COMP_ADD_OPT,
2051 .name = "comp-add", .has_arg = no_argument},
2052 { .val = LFS_COMP_ADD_OPT,
2053 .name = "component-add",
2054 .has_arg = no_argument},
2055 { .val = LFS_COMP_DEL_OPT,
2056 .name = "comp-del", .has_arg = no_argument},
2057 { .val = LFS_COMP_DEL_OPT,
2058 .name = "component-del",
2059 .has_arg = no_argument},
2060 { .val = LFS_COMP_FLAGS_OPT,
2061 .name = "comp-flags", .has_arg = required_argument},
2062 { .val = LFS_COMP_FLAGS_OPT,
2063 .name = "component-flags",
2064 .has_arg = required_argument},
2065 { .val = LFS_COMP_SET_OPT,
2066 .name = "comp-set", .has_arg = no_argument},
2067 { .val = LFS_COMP_SET_OPT,
2068 .name = "component-set",
2069 .has_arg = no_argument},
2070 { .val = LFS_COMP_USE_PARENT_OPT,
2071 .name = "parent", .has_arg = no_argument},
2072 { .val = LFS_COMP_NO_VERIFY_OPT,
2073 .name = "no-verify", .has_arg = no_argument},
2074 { .val = LFS_MIRROR_FLAGS_OPT,
2075 .name = "flags", .has_arg = required_argument},
2076 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2077 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2078 { .val = 'd', .name = "delete", .has_arg = no_argument},
2079 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2080 { .val = 'E', .name = "component-end",
2081 .has_arg = required_argument},
2082 { .val = 'f', .name = "file", .has_arg = required_argument },
2083 /* dirstripe {"mdt-hash", required_argument, 0, 'H'}, */
2084 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2085 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2086 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2087 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2088 { .val = 'L', .name = "layout", .has_arg = required_argument },
2089 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2090 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2091 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2092 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2093 /* --non-block is only valid in migrate mode */
2094 { .val = 'n', .name = "non-block", .has_arg = no_argument},
2095 { .val = 'o', .name = "ost", .has_arg = required_argument},
2096 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2097 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2098 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2100 { .val = 'p', .name = "pool", .has_arg = required_argument },
2101 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2102 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2103 /* dirstripe {"mdt-count", required_argument, 0, 'T'}, */
2104 /* --verbose is only valid in migrate mode */
2105 { .val = 'v', .name = "verbose", .has_arg = no_argument },
2108 setstripe_args_init(&lsa);
2110 migrate_mode = (opc == SO_MIGRATE);
2111 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2113 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2115 while ((c = getopt_long(argc, argv, "bc:dE:f:i:I:m:N::no:p:L:s:S:v",
2116 long_opts, NULL)) >= 0) {
2121 case LFS_COMP_ADD_OPT:
2124 case LFS_COMP_DEL_OPT:
2127 case LFS_COMP_FLAGS_OPT:
2128 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2129 &lsa.lsa_comp_neg_flags);
2132 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2133 fprintf(stderr, "%s: inverted flags are not supported\n",
2137 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2139 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2146 case LFS_COMP_SET_OPT:
2149 case LFS_COMP_USE_PARENT_OPT:
2151 fprintf(stderr, "error: %s: --parent must be "
2152 "specified with --mirror-count|-N "
2153 "option\n", progname);
2156 setstripe_args_init(&lsa);
2158 case LFS_COMP_NO_VERIFY_OPT:
2159 mirror_flags |= NO_VERIFY;
2161 case LFS_MIRROR_FLAGS_OPT: {
2164 if (!mirror_mode || !last_mirror) {
2165 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2170 result = comp_str2flags(optarg, &last_mirror->m_flags,
2176 fprintf(stderr, "%s: inverted flags are not supported\n",
2181 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2183 "%s: unsupported mirror flags: %s\n",
2191 if (!migrate_mode) {
2193 "%s %s: -b|--block valid only for migrate command\n",
2197 migration_block = true;
2200 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2203 "%s %s: invalid stripe count '%s'\n",
2204 progname, argv[0], optarg);
2208 if (lsa.lsa_stripe_count == -1)
2209 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2212 /* delete the default striping pattern */
2216 if (lsa.lsa_comp_end != 0) {
2217 result = comp_args_to_layout(lpp, &lsa);
2220 "%s %s: invalid layout\n",
2225 setstripe_args_init_inherit(&lsa);
2228 if (arg_is_eof(optarg)) {
2229 lsa.lsa_comp_end = LUSTRE_EOF;
2231 result = llapi_parse_size(optarg,
2236 "%s %s: invalid component end '%s'\n",
2237 progname, argv[0], optarg);
2243 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2246 "%s %s: invalid stripe offset '%s'\n",
2247 progname, argv[0], optarg);
2250 if (lsa.lsa_stripe_off == -1)
2251 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2254 comp_id = strtoul(optarg, &end, 0);
2255 if (*end != '\0' || comp_id == 0 ||
2256 comp_id > LCME_ID_MAX) {
2258 "%s %s: invalid component ID '%s'\n",
2259 progname, argv[0], optarg);
2264 if (opc != SO_MIRROR_EXTEND) {
2266 "error: %s: invalid option: %s\n",
2267 progname, argv[optopt + 1]);
2270 if (last_mirror == NULL) {
2271 fprintf(stderr, "error: %s: '-N' must exist "
2272 "in front of '%s'\n",
2273 progname, argv[optopt + 1]);
2277 last_mirror->m_file = optarg;
2278 last_mirror->m_count = 1;
2282 if (strcmp(argv[optind - 1], "mdt") == 0) {
2283 /* Can be only the first component */
2284 if (layout != NULL) {
2286 fprintf(stderr, "error: 'mdt' layout "
2287 "can be only the first one\n");
2290 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2292 fprintf(stderr, "error: 'mdt' layout "
2293 "size is too big\n");
2296 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2297 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2299 fprintf(stderr, "error: layout '%s' is "
2300 "unknown, supported layouts are: "
2301 "'mdt', 'raid0'\n", argv[optind]);
2306 if (!migrate_mode) {
2308 "%s %s: -m|--mdt-index valid only for migrate command\n",
2312 mdt_idx_arg = optarg;
2315 if (!migrate_mode) {
2317 "%s %s: -n|--non-block valid only for migrate command\n",
2321 migration_flags |= MIGRATION_NONBLOCK;
2324 if (opc == SO_SETSTRIPE) {
2325 opc = SO_MIRROR_CREATE;
2329 if (optarg != NULL) {
2330 mirror_count = strtoul(optarg, &end, 0);
2331 if (*end != '\0' || mirror_count == 0) {
2333 "error: %s: bad mirror count: %s\n",
2340 new_mirror = lfs_mirror_alloc();
2341 new_mirror->m_count = mirror_count;
2343 if (mirror_list == NULL)
2344 mirror_list = new_mirror;
2346 if (last_mirror != NULL) {
2347 /* wrap up last mirror */
2348 if (lsa.lsa_comp_end == 0)
2349 lsa.lsa_comp_end = LUSTRE_EOF;
2351 result = comp_args_to_layout(lpp, &lsa);
2353 lfs_mirror_free(new_mirror);
2357 setstripe_args_init_inherit(&lsa);
2359 last_mirror->m_next = new_mirror;
2362 last_mirror = new_mirror;
2363 lpp = &last_mirror->m_layout;
2366 lsa.lsa_nr_tgts = parse_targets(osts,
2367 sizeof(osts) / sizeof(__u32),
2368 lsa.lsa_nr_tgts, optarg);
2369 if (lsa.lsa_nr_tgts < 0) {
2371 "%s %s: invalid OST target(s) '%s'\n",
2372 progname, argv[0], optarg);
2376 lsa.lsa_tgts = osts;
2377 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2378 lsa.lsa_stripe_off = osts[0];
2383 lsa.lsa_pool_name = optarg;
2386 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2390 "%s %s: invalid stripe size '%s'\n",
2391 progname, argv[0], optarg);
2396 if (!migrate_mode) {
2398 "%s %s: -v|--verbose valid only for migrate command\n",
2402 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2405 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2406 progname, argv[0], argv[optind - 1]);
2411 fname = argv[optind];
2413 if (optind == argc) {
2414 fprintf(stderr, "%s %s: FILE must be specified\n",
2419 if (mirror_mode && mirror_count == 0) {
2421 "error: %s: --mirror-count|-N option is required\n",
2428 if (lsa.lsa_comp_end == 0)
2429 lsa.lsa_comp_end = LUSTRE_EOF;
2432 if (lsa.lsa_comp_end != 0) {
2433 result = comp_args_to_layout(lpp, &lsa);
2438 if (mirror_flags & NO_VERIFY) {
2439 if (opc != SO_MIRROR_EXTEND) {
2441 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2445 } else if (!has_m_file) {
2447 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2454 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2455 * altered by user space tool, so we don't need to support the
2456 * --component-set for this moment. */
2457 if (comp_set && !comp_id) {
2458 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2463 if ((delete + comp_set + comp_del + comp_add) > 1) {
2465 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2470 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2471 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2473 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2478 if ((comp_set || comp_del) &&
2479 (setstripe_args_specified(&lsa) || layout != NULL)) {
2481 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
2486 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
2488 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
2493 if (comp_add || comp_del) {
2496 result = lstat(fname, &st);
2497 if (result == 0 && S_ISDIR(st.st_mode)) {
2499 "%s setstripe: cannot use --component-add or --component-del for directory\n",
2505 fprintf(stderr, "error: %s: can't use --component-add "
2506 "or --component-del for mirror operation\n",
2513 if (layout == NULL) {
2515 "%s %s: option -E must be specified with --component-add\n",
2520 result = adjust_first_extent(fname, layout);
2521 if (result == -ENODATA)
2523 else if (result != 0)
2527 if (mdt_idx_arg != NULL && optind > 3) {
2529 "%s %s: option -m cannot be used with other options\n",
2534 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
2536 "%s %s: options --non-block and --block are mutually exclusive\n",
2541 if (!comp_del && !comp_set && comp_id != 0) {
2543 "%s %s: option -I can only be used with --component-del\n",
2548 if (mdt_idx_arg != NULL) {
2549 /* initialize migrate mdt parameters */
2550 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
2552 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
2553 progname, argv[0], mdt_idx_arg);
2556 migrate_mdt_param.fp_migrate = 1;
2557 } else if (layout == NULL) {
2558 /* initialize stripe parameters */
2559 param = calloc(1, offsetof(typeof(*param),
2560 lsp_osts[lsa.lsa_nr_tgts]));
2561 if (param == NULL) {
2563 "%s %s: cannot allocate memory for parameters: %s\n",
2564 progname, argv[0], strerror(ENOMEM));
2569 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
2570 param->lsp_stripe_size = lsa.lsa_stripe_size;
2571 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2572 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
2573 param->lsp_stripe_count = -1;
2575 param->lsp_stripe_count = lsa.lsa_stripe_count;
2577 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2578 param->lsp_stripe_offset = -1;
2580 param->lsp_stripe_offset = lsa.lsa_stripe_off;
2581 param->lsp_pool = lsa.lsa_pool_name;
2582 param->lsp_is_specific = false;
2583 if (lsa.lsa_nr_tgts > 0) {
2584 if (lsa.lsa_stripe_count > 0 &&
2585 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2586 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2587 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
2588 fprintf(stderr, "error: %s: stripe count %lld "
2589 "doesn't match the number of OSTs: %d\n"
2590 , argv[0], lsa.lsa_stripe_count,
2596 param->lsp_is_specific = true;
2597 param->lsp_stripe_count = lsa.lsa_nr_tgts;
2598 memcpy(param->lsp_osts, osts,
2599 sizeof(*osts) * lsa.lsa_nr_tgts);
2603 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
2604 if (mdt_idx_arg != NULL) {
2605 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
2606 } else if (migrate_mode) {
2607 result = lfs_migrate(fname, migration_flags, param,
2609 } else if (comp_set != 0) {
2610 result = lfs_component_set(fname, comp_id,
2612 lsa.lsa_comp_neg_flags);
2613 } else if (comp_del != 0) {
2614 result = lfs_component_del(fname, comp_id,
2616 lsa.lsa_comp_neg_flags);
2617 } else if (comp_add != 0) {
2618 result = lfs_component_add(fname, layout);
2619 } else if (opc == SO_MIRROR_CREATE) {
2620 result = mirror_create(fname, mirror_list);
2621 } else if (opc == SO_MIRROR_EXTEND) {
2622 result = mirror_extend(fname, mirror_list,
2624 } else if (layout != NULL) {
2625 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
2632 result = llapi_file_open_param(fname,
2641 /* Save the first error encountered. */
2649 llapi_layout_free(layout);
2650 lfs_mirror_list_free(mirror_list);
2655 llapi_layout_free(layout);
2656 lfs_mirror_list_free(mirror_list);
2660 static int lfs_poollist(int argc, char **argv)
2665 return llapi_poollist(argv[1]);
2668 static int set_time(time_t *time, time_t *set, char *str)
2675 else if (str[0] == '-')
2681 t = strtol(str, NULL, 0);
2682 if (*time < t * 24 * 60 * 60) {
2685 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
2689 *set = *time - t * 24 * 60 * 60;
2692 static int name2uid(unsigned int *id, const char *name)
2694 struct passwd *passwd;
2696 passwd = getpwnam(name);
2699 *id = passwd->pw_uid;
2704 static int name2gid(unsigned int *id, const char *name)
2706 struct group *group;
2708 group = getgrnam(name);
2711 *id = group->gr_gid;
2716 static inline int name2projid(unsigned int *id, const char *name)
2721 static int uid2name(char **name, unsigned int id)
2723 struct passwd *passwd;
2725 passwd = getpwuid(id);
2728 *name = passwd->pw_name;
2733 static inline int gid2name(char **name, unsigned int id)
2735 struct group *group;
2737 group = getgrgid(id);
2740 *name = group->gr_name;
2745 static int name2layout(__u32 *layout, char *name)
2747 char *ptr, *layout_name;
2750 for (ptr = name; ; ptr = NULL) {
2751 layout_name = strtok(ptr, ",");
2752 if (layout_name == NULL)
2754 if (strcmp(layout_name, "released") == 0)
2755 *layout |= LOV_PATTERN_F_RELEASED;
2756 else if (strcmp(layout_name, "raid0") == 0)
2757 *layout |= LOV_PATTERN_RAID0;
2758 else if (strcmp(layout_name, "mdt") == 0)
2759 *layout |= LOV_PATTERN_MDT;
2766 static int lfs_find(int argc, char **argv)
2771 struct find_param param = {
2775 struct option long_opts[] = {
2776 { .val = 'A', .name = "atime", .has_arg = required_argument },
2777 { .val = LFS_COMP_COUNT_OPT,
2778 .name = "comp-count", .has_arg = required_argument },
2779 { .val = LFS_COMP_COUNT_OPT,
2780 .name = "component-count",
2781 .has_arg = required_argument },
2782 { .val = LFS_COMP_FLAGS_OPT,
2783 .name = "comp-flags", .has_arg = required_argument },
2784 { .val = LFS_COMP_FLAGS_OPT,
2785 .name = "component-flags",
2786 .has_arg = required_argument },
2787 { .val = LFS_COMP_START_OPT,
2788 .name = "comp-start", .has_arg = required_argument },
2789 { .val = LFS_COMP_START_OPT,
2790 .name = "component-start",
2791 .has_arg = required_argument },
2792 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
2793 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
2794 { .val = 'C', .name = "ctime", .has_arg = required_argument },
2795 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
2796 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
2797 { .val = 'E', .name = "component-end",
2798 .has_arg = required_argument },
2799 { .val = 'g', .name = "gid", .has_arg = required_argument },
2800 { .val = 'G', .name = "group", .has_arg = required_argument },
2801 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
2802 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
2803 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
2804 /*{"component-id", required_argument, 0, 'I'},*/
2805 { .val = 'L', .name = "layout", .has_arg = required_argument },
2806 { .val = 'm', .name = "mdt", .has_arg = required_argument },
2807 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
2808 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
2809 { .val = 'M', .name = "mtime", .has_arg = required_argument },
2810 { .val = 'n', .name = "name", .has_arg = required_argument },
2811 /* reserve {"or", no_argument, , 0, 'o'}, to match find(1) */
2812 { .val = 'O', .name = "obd", .has_arg = required_argument },
2813 { .val = 'O', .name = "ost", .has_arg = required_argument },
2814 /* no short option for pool, p/P already used */
2815 { .val = LFS_POOL_OPT,
2816 .name = "pool", .has_arg = required_argument },
2817 { .val = 'p', .name = "print0", .has_arg = no_argument },
2818 { .val = 'P', .name = "print", .has_arg = no_argument },
2819 { .val = LFS_PROJID_OPT,
2820 .name = "projid", .has_arg = required_argument },
2821 { .val = 's', .name = "size", .has_arg = required_argument },
2822 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2823 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2824 { .val = 't', .name = "type", .has_arg = required_argument },
2825 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
2826 { .val = 'u', .name = "uid", .has_arg = required_argument },
2827 { .val = 'U', .name = "user", .has_arg = required_argument },
2839 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
2840 while ((c = getopt_long_only(argc, argv,
2841 "-A:c:C:D:E:g:G:H:i:L:m:M:n:O:Ppqrs:S:t:T:u:U:v",
2842 long_opts, NULL)) >= 0) {
2847 /* '!' is part of option */
2848 /* when getopt_long_only() finds a string which is not
2849 * an option nor a known option argument it returns 1
2850 * in that case if we already have found pathstart and pathend
2851 * (i.e. we have the list of pathnames),
2852 * the only supported value is "!"
2854 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
2855 if (!isoption && pathend != -1) {
2856 fprintf(stderr, "err: %s: filename|dirname must either "
2857 "precede options or follow options\n",
2862 if (!isoption && pathstart == -1)
2863 pathstart = optind - 1;
2864 if (isoption && pathstart != -1 && pathend == -1)
2865 pathend = optind - 2;
2871 /* unknown; opt is "!" or path component,
2872 * checking done above.
2874 if (strcmp(optarg, "!") == 0)
2878 xtime = ¶m.fp_atime;
2879 xsign = ¶m.fp_asign;
2880 param.fp_exclude_atime = !!neg_opt;
2881 /* no break, this falls through to 'C' for ctime */
2884 xtime = ¶m.fp_ctime;
2885 xsign = ¶m.fp_csign;
2886 param.fp_exclude_ctime = !!neg_opt;
2888 /* no break, this falls through to 'M' for mtime */
2891 xtime = ¶m.fp_mtime;
2892 xsign = ¶m.fp_msign;
2893 param.fp_exclude_mtime = !!neg_opt;
2895 rc = set_time(&t, xtime, optarg);
2896 if (rc == INT_MAX) {
2903 case LFS_COMP_COUNT_OPT:
2904 if (optarg[0] == '+') {
2905 param.fp_comp_count_sign = -1;
2907 } else if (optarg[0] == '-') {
2908 param.fp_comp_count_sign = 1;
2912 param.fp_comp_count = strtoul(optarg, &endptr, 0);
2913 if (*endptr != '\0') {
2914 fprintf(stderr, "error: bad component count "
2918 param.fp_check_comp_count = 1;
2919 param.fp_exclude_comp_count = !!neg_opt;
2921 case LFS_COMP_FLAGS_OPT:
2922 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
2923 ¶m.fp_comp_neg_flags);
2925 fprintf(stderr, "error: bad component flags "
2929 param.fp_check_comp_flags = 1;
2931 __u32 flags = param.fp_comp_neg_flags;
2932 param.fp_comp_neg_flags = param.fp_comp_flags;
2933 param.fp_comp_flags = flags;
2936 case LFS_COMP_START_OPT:
2937 if (optarg[0] == '+') {
2938 param.fp_comp_start_sign = -1;
2940 } else if (optarg[0] == '-') {
2941 param.fp_comp_start_sign = 1;
2945 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
2946 ¶m.fp_comp_start_units, 0);
2948 fprintf(stderr, "error: bad component start "
2952 param.fp_check_comp_start = 1;
2953 param.fp_exclude_comp_start = !!neg_opt;
2956 if (optarg[0] == '+') {
2957 param.fp_stripe_count_sign = -1;
2959 } else if (optarg[0] == '-') {
2960 param.fp_stripe_count_sign = 1;
2964 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
2965 if (*endptr != '\0') {
2966 fprintf(stderr,"error: bad stripe_count '%s'\n",
2971 param.fp_check_stripe_count = 1;
2972 param.fp_exclude_stripe_count = !!neg_opt;
2975 param.fp_max_depth = strtol(optarg, 0, 0);
2978 if (optarg[0] == '+') {
2979 param.fp_comp_end_sign = -1;
2981 } else if (optarg[0] == '-') {
2982 param.fp_comp_end_sign = 1;
2986 if (arg_is_eof(optarg)) {
2987 param.fp_comp_end = LUSTRE_EOF;
2988 param.fp_comp_end_units = 1;
2991 rc = llapi_parse_size(optarg,
2993 ¶m.fp_comp_end_units, 0);
2996 fprintf(stderr, "error: bad component end "
3000 param.fp_check_comp_end = 1;
3001 param.fp_exclude_comp_end = !!neg_opt;
3005 rc = name2gid(¶m.fp_gid, optarg);
3007 param.fp_gid = strtoul(optarg, &endptr, 10);
3008 if (*endptr != '\0') {
3009 fprintf(stderr, "Group/GID: %s cannot "
3010 "be found.\n", optarg);
3015 param.fp_exclude_gid = !!neg_opt;
3016 param.fp_check_gid = 1;
3019 param.fp_hash_type = check_hashtype(optarg);
3020 if (param.fp_hash_type == 0) {
3021 fprintf(stderr, "error: bad hash_type '%s'\n",
3026 param.fp_check_hash_type = 1;
3027 param.fp_exclude_hash_type = !!neg_opt;
3030 ret = name2layout(¶m.fp_layout, optarg);
3033 param.fp_exclude_layout = !!neg_opt;
3034 param.fp_check_layout = 1;
3038 rc = name2uid(¶m.fp_uid, optarg);
3040 param.fp_uid = strtoul(optarg, &endptr, 10);
3041 if (*endptr != '\0') {
3042 fprintf(stderr, "User/UID: %s cannot "
3043 "be found.\n", optarg);
3048 param.fp_exclude_uid = !!neg_opt;
3049 param.fp_check_uid = 1;
3052 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3054 "Pool name %s is too long"
3055 " (max is %d)\n", optarg,
3060 /* we do check for empty pool because empty pool
3061 * is used to find V1 lov attributes */
3062 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3063 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3064 param.fp_exclude_pool = !!neg_opt;
3065 param.fp_check_pool = 1;
3068 param.fp_pattern = (char *)optarg;
3069 param.fp_exclude_pattern = !!neg_opt;
3074 char *buf, *token, *next, *p;
3078 buf = strdup(optarg);
3084 param.fp_exclude_obd = !!neg_opt;
3087 while (token && *token) {
3088 token = strchr(token, ',');
3095 param.fp_exclude_mdt = !!neg_opt;
3096 param.fp_num_alloc_mdts += len;
3097 tmp = realloc(param.fp_mdt_uuid,
3098 param.fp_num_alloc_mdts *
3099 sizeof(*param.fp_mdt_uuid));
3105 param.fp_mdt_uuid = tmp;
3107 param.fp_exclude_obd = !!neg_opt;
3108 param.fp_num_alloc_obds += len;
3109 tmp = realloc(param.fp_obd_uuid,
3110 param.fp_num_alloc_obds *
3111 sizeof(*param.fp_obd_uuid));
3117 param.fp_obd_uuid = tmp;
3119 for (token = buf; token && *token; token = next) {
3120 struct obd_uuid *puuid;
3123 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3126 ¶m.fp_obd_uuid[param.fp_num_obds++];
3128 p = strchr(token, ',');
3135 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3140 strncpy(puuid->uuid, token,
3141 sizeof(puuid->uuid));
3149 param.fp_zero_end = 1;
3153 case LFS_PROJID_OPT:
3154 rc = name2projid(¶m.fp_projid, optarg);
3156 param.fp_projid = strtoul(optarg, &endptr, 10);
3157 if (*endptr != '\0') {
3159 "Invalid project ID: %s",
3165 param.fp_exclude_projid = !!neg_opt;
3166 param.fp_check_projid = 1;
3169 if (optarg[0] == '+') {
3170 param.fp_size_sign = -1;
3172 } else if (optarg[0] == '-') {
3173 param.fp_size_sign = 1;
3177 ret = llapi_parse_size(optarg, ¶m.fp_size,
3178 ¶m.fp_size_units, 0);
3180 fprintf(stderr, "error: bad file size '%s'\n",
3184 param.fp_check_size = 1;
3185 param.fp_exclude_size = !!neg_opt;
3188 if (optarg[0] == '+') {
3189 param.fp_stripe_size_sign = -1;
3191 } else if (optarg[0] == '-') {
3192 param.fp_stripe_size_sign = 1;
3196 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3197 ¶m.fp_stripe_size_units, 0);
3199 fprintf(stderr, "error: bad stripe_size '%s'\n",
3203 param.fp_check_stripe_size = 1;
3204 param.fp_exclude_stripe_size = !!neg_opt;
3207 param.fp_exclude_type = !!neg_opt;
3208 switch (optarg[0]) {
3210 param.fp_type = S_IFBLK;
3213 param.fp_type = S_IFCHR;
3216 param.fp_type = S_IFDIR;
3219 param.fp_type = S_IFREG;
3222 param.fp_type = S_IFLNK;
3225 param.fp_type = S_IFIFO;
3228 param.fp_type = S_IFSOCK;
3231 fprintf(stderr, "error: %s: bad type '%s'\n",
3238 if (optarg[0] == '+') {
3239 param.fp_mdt_count_sign = -1;
3241 } else if (optarg[0] == '-') {
3242 param.fp_mdt_count_sign = 1;
3246 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3247 if (*endptr != '\0') {
3248 fprintf(stderr, "error: bad mdt_count '%s'\n",
3253 param.fp_check_mdt_count = 1;
3254 param.fp_exclude_mdt_count = !!neg_opt;
3262 if (pathstart == -1) {
3263 fprintf(stderr, "error: %s: no filename|pathname\n",
3267 } else if (pathend == -1) {
3273 rc = llapi_find(argv[pathstart], ¶m);
3274 if (rc != 0 && ret == 0)
3276 } while (++pathstart < pathend);
3279 fprintf(stderr, "error: %s failed for %s.\n",
3280 argv[0], argv[optind - 1]);
3282 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3283 free(param.fp_obd_uuid);
3285 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3286 free(param.fp_mdt_uuid);
3291 static int lfs_getstripe_internal(int argc, char **argv,
3292 struct find_param *param)
3294 struct option long_opts[] = {
3295 { .val = LFS_COMP_COUNT_OPT,
3296 .name = "comp-count", .has_arg = no_argument },
3297 { .val = LFS_COMP_COUNT_OPT,
3298 .name = "component-count", .has_arg = no_argument },
3299 { .val = LFS_COMP_FLAGS_OPT,
3300 .name = "comp-flags", .has_arg = optional_argument },
3301 { .val = LFS_COMP_FLAGS_OPT,
3302 .name = "component-flags", .has_arg = optional_argument },
3303 { .val = LFS_COMP_START_OPT,
3304 .name = "comp-start", .has_arg = optional_argument },
3305 { .val = LFS_COMP_START_OPT,
3306 .name = "component-start", .has_arg = optional_argument },
3307 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3308 /* This formerly implied "stripe-count", but was explicitly
3309 * made "stripe-count" for consistency with other options,
3310 * and to separate it from "mdt-count" when DNE arrives. */
3311 { .val = 'c', .name = "count", .has_arg = no_argument },
3313 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3314 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3315 { .val = 'd', .name = "directory", .has_arg = no_argument },
3316 { .val = 'D', .name = "default", .has_arg = no_argument },
3317 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3318 { .val = 'E', .name = "component-end",
3319 .has_arg = optional_argument },
3320 { .val = 'F', .name = "fid", .has_arg = no_argument },
3321 { .val = 'g', .name = "generation", .has_arg = no_argument },
3322 /* dirstripe { .val = 'H', .name = "mdt-hash",
3323 * .has_arg = required_argument }, */
3324 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3325 /* This formerly implied "stripe-index", but was explicitly
3326 * made "stripe-index" for consistency with other options,
3327 * and to separate it from "mdt-index" when DNE arrives. */
3328 { .val = 'i', .name = "index", .has_arg = no_argument },
3330 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3331 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3332 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3333 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3334 { .val = 'L', .name = "layout", .has_arg = no_argument },
3335 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3336 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3337 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3338 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3339 { .val = 'M', .name = "mdt-index", .has_arg = no_argument },
3340 { .val = 'M', .name = "mdt_index", .has_arg = no_argument },
3342 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3343 /* This formerly implied "stripe-index", but was confusing
3344 * with "file offset" (which will eventually be needed for
3345 * with different layouts by offset), so deprecate it. */
3346 { .val = 'o', .name = "offset", .has_arg = no_argument },
3348 { .val = 'O', .name = "obd", .has_arg = required_argument },
3349 { .val = 'O', .name = "ost", .has_arg = required_argument },
3350 { .val = 'p', .name = "pool", .has_arg = no_argument },
3351 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3352 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3353 { .val = 'R', .name = "raw", .has_arg = no_argument },
3354 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3355 /* This formerly implied "--stripe-size", but was confusing
3356 * with "lfs find --size|-s", which means "file size", so use
3357 * the consistent "--stripe-size|-S" for all commands. */
3358 { .val = 's', .name = "size", .has_arg = no_argument },
3360 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3361 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3362 /* dirstripe { .val = 'T', .name = "mdt-count",
3363 * .has_arg = required_argument }, */
3364 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3365 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3370 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
3371 long_opts, NULL)) != -1) {
3374 if (strcmp(argv[optind - 1], "--count") == 0)
3375 fprintf(stderr, "warning: '--count' deprecated,"
3376 " use '--stripe-count' instead\n");
3377 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3378 param->fp_verbose |= VERBOSE_COUNT;
3379 param->fp_max_depth = 0;
3382 case LFS_COMP_COUNT_OPT:
3383 param->fp_verbose |= VERBOSE_COMP_COUNT;
3384 param->fp_max_depth = 0;
3386 case LFS_COMP_FLAGS_OPT:
3387 if (optarg != NULL) {
3388 rc = comp_str2flags(optarg,
3389 ¶m->fp_comp_flags,
3390 ¶m->fp_comp_neg_flags);
3392 fprintf(stderr, "error: %s bad "
3393 "component flags '%s'.\n",
3397 param->fp_check_comp_flags = 1;
3399 param->fp_verbose |= VERBOSE_COMP_FLAGS;
3400 param->fp_max_depth = 0;
3403 case LFS_COMP_START_OPT:
3404 if (optarg != NULL) {
3406 if (tmp[0] == '+') {
3407 param->fp_comp_start_sign = -1;
3409 } else if (tmp[0] == '-') {
3410 param->fp_comp_start_sign = 1;
3413 rc = llapi_parse_size(tmp,
3414 ¶m->fp_comp_start,
3415 ¶m->fp_comp_start_units, 0);
3417 fprintf(stderr, "error: %s bad "
3418 "component start '%s'.\n",
3422 param->fp_check_comp_start = 1;
3425 param->fp_verbose |= VERBOSE_COMP_START;
3426 param->fp_max_depth = 0;
3430 param->fp_max_depth = 0;
3433 param->fp_get_default_lmv = 1;
3436 if (optarg != NULL) {
3438 if (tmp[0] == '+') {
3439 param->fp_comp_end_sign = -1;
3441 } else if (tmp[0] == '-') {
3442 param->fp_comp_end_sign = 1;
3446 if (arg_is_eof(tmp)) {
3447 param->fp_comp_end = LUSTRE_EOF;
3448 param->fp_comp_end_units = 1;
3451 rc = llapi_parse_size(tmp,
3452 ¶m->fp_comp_end,
3453 ¶m->fp_comp_end_units, 0);
3456 fprintf(stderr, "error: %s bad "
3457 "component end '%s'.\n",
3461 param->fp_check_comp_end = 1;
3463 param->fp_verbose |= VERBOSE_COMP_END;
3464 param->fp_max_depth = 0;
3468 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3469 param->fp_verbose |= VERBOSE_DFID;
3470 param->fp_max_depth = 0;
3474 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3475 param->fp_verbose |= VERBOSE_GENERATION;
3476 param->fp_max_depth = 0;
3479 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3481 fprintf(stderr, "warning: '--offset|-o' deprecated, "
3482 "use '--stripe-index|-i' instead\n");
3485 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
3486 if (strcmp(argv[optind - 1], "--index") == 0)
3487 fprintf(stderr, "warning: '--index' deprecated"
3488 ", use '--stripe-index' instead\n");
3490 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3491 param->fp_verbose |= VERBOSE_OFFSET;
3492 param->fp_max_depth = 0;
3496 if (optarg != NULL) {
3497 param->fp_comp_id = strtoul(optarg, &end, 0);
3498 if (*end != '\0' || param->fp_comp_id == 0 ||
3499 param->fp_comp_id > LCME_ID_MAX) {
3500 fprintf(stderr, "error: %s bad "
3501 "component id '%s'\n",
3505 param->fp_check_comp_id = 1;
3508 param->fp_max_depth = 0;
3509 param->fp_verbose |= VERBOSE_COMP_ID;
3513 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3514 param->fp_verbose |= VERBOSE_LAYOUT;
3515 param->fp_max_depth = 0;
3518 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3520 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3521 fprintf(stderr, "warning: '-M' deprecated"
3522 ", use '-m' instead\n");
3526 if (!(param->fp_verbose & VERBOSE_DETAIL))
3527 param->fp_max_depth = 0;
3528 param->fp_verbose |= VERBOSE_MDTINDEX;
3531 if (param->fp_obd_uuid) {
3533 "error: %s: only one obduuid allowed",
3537 param->fp_obd_uuid = (struct obd_uuid *)optarg;
3540 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3541 param->fp_verbose |= VERBOSE_POOL;
3542 param->fp_max_depth = 0;
3549 param->fp_recursive = 1;
3554 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3556 fprintf(stderr, "warning: '--size|-s' deprecated, "
3557 "use '--stripe-size|-S' instead\n");
3558 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0) */
3560 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3561 param->fp_verbose |= VERBOSE_SIZE;
3562 param->fp_max_depth = 0;
3566 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
3579 if (param->fp_recursive)
3580 param->fp_max_depth = -1;
3581 else if (param->fp_verbose & VERBOSE_DETAIL)
3582 param->fp_max_depth = 1;
3584 if (!param->fp_verbose)
3585 param->fp_verbose = VERBOSE_DEFAULT;
3586 if (param->fp_quiet)
3587 param->fp_verbose = VERBOSE_OBJID;
3590 rc = llapi_getstripe(argv[optind], param);
3591 } while (++optind < argc && !rc);
3594 fprintf(stderr, "error: %s failed for %s.\n",
3595 argv[0], argv[optind - 1]);
3599 static int lfs_tgts(int argc, char **argv)
3601 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3602 struct find_param param;
3603 int index = 0, rc=0;
3608 if (argc == 2 && !realpath(argv[1], path)) {
3610 fprintf(stderr, "error: invalid path '%s': %s\n",
3611 argv[1], strerror(-rc));
3615 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
3616 /* Check if we have a mount point */
3617 if (mntdir[0] == '\0')
3620 memset(¶m, 0, sizeof(param));
3621 if (!strcmp(argv[0], "mdts"))
3622 param.fp_get_lmv = 1;
3624 rc = llapi_ostlist(mntdir, ¶m);
3626 fprintf(stderr, "error: %s: failed on %s\n",
3629 if (path[0] != '\0')
3631 memset(mntdir, 0, PATH_MAX);
3637 static int lfs_getstripe(int argc, char **argv)
3639 struct find_param param = { 0 };
3641 param.fp_max_depth = 1;
3642 return lfs_getstripe_internal(argc, argv, ¶m);
3646 static int lfs_getdirstripe(int argc, char **argv)
3648 struct find_param param = { 0 };
3649 struct option long_opts[] = {
3650 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3651 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
3653 { .val = 'D', .name = "default", .has_arg = no_argument },
3654 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
3655 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
3656 { .val = 'O', .name = "obd", .has_arg = required_argument },
3657 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3658 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3659 { .val = 't', .name = "mdt-hash", .has_arg = no_argument },
3661 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
3662 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3666 param.fp_get_lmv = 1;
3668 while ((c = getopt_long(argc, argv,
3669 "cDHiO:rtTy", long_opts, NULL)) != -1)
3673 if (param.fp_obd_uuid) {
3675 "error: %s: only one obduuid allowed",
3679 param.fp_obd_uuid = (struct obd_uuid *)optarg;
3681 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3683 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 10, 50, 0)
3684 fprintf(stderr, "warning: '-c' deprecated"
3685 ", use '-T' instead\n");
3689 param.fp_verbose |= VERBOSE_COUNT;
3692 param.fp_verbose |= VERBOSE_OFFSET;
3694 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3698 param.fp_verbose |= VERBOSE_HASH_TYPE;
3701 param.fp_get_default_lmv = 1;
3704 param.fp_recursive = 1;
3717 if (param.fp_recursive)
3718 param.fp_max_depth = -1;
3720 if (!param.fp_verbose)
3721 param.fp_verbose = VERBOSE_DEFAULT;
3724 rc = llapi_getstripe(argv[optind], ¶m);
3725 } while (++optind < argc && !rc);
3728 fprintf(stderr, "error: %s failed for %s.\n",
3729 argv[0], argv[optind - 1]);
3734 static int lfs_setdirstripe(int argc, char **argv)
3738 struct lfs_setstripe_args lsa;
3739 struct llapi_stripe_param *param = NULL;
3740 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
3743 char *mode_opt = NULL;
3744 bool default_stripe = false;
3745 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
3746 mode_t previous_mode = 0;
3747 bool delete = false;
3749 struct option long_opts[] = {
3750 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3751 { .val = 'c', .name = "count", .has_arg = required_argument },
3753 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
3754 { .val = 'd', .name = "delete", .has_arg = no_argument },
3755 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3756 { .val = 'i', .name = "index", .has_arg = required_argument },
3758 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
3759 { .val = 'm', .name = "mode", .has_arg = required_argument },
3760 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3761 { .val = 't', .name = "hash-type", .has_arg = required_argument },
3762 { .val = 't', .name = "mdt-hash", .has_arg = required_argument },
3764 {"mdt-hash", required_argument, 0, 'H'},
3765 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3766 { .val = 'D', .name = "default_stripe",
3767 .has_arg = no_argument },
3769 { .val = 'D', .name = "default", .has_arg = no_argument },
3772 setstripe_args_init(&lsa);
3774 while ((c = getopt_long(argc, argv, "c:dDi:H:m:t:", long_opts,
3781 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3782 if (strcmp(argv[optind - 1], "--count") == 0)
3784 "%s %s: warning: '--count' deprecated, use '--mdt-count' instead\n",
3787 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3790 "%s %s: invalid stripe count '%s'\n",
3791 progname, argv[0], optarg);
3797 default_stripe = true;
3800 default_stripe = true;
3803 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3804 if (strcmp(argv[optind - 1], "--index") == 0)
3806 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
3809 lsa.lsa_nr_tgts = parse_targets(mdts,
3810 sizeof(mdts) / sizeof(__u32),
3811 lsa.lsa_nr_tgts, optarg);
3812 if (lsa.lsa_nr_tgts < 0) {
3814 "%s %s: invalid MDT target(s) '%s'\n",
3815 progname, argv[0], optarg);
3819 lsa.lsa_tgts = mdts;
3820 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3821 lsa.lsa_stripe_off = mdts[0];
3826 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3830 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3831 if (strcmp(argv[optind - 1], "--hash-type") == 0)
3833 "%s %s: warning: '--hash-type' deprecated, use '--mdt-hash' instead\n",
3836 lsa.lsa_pattern = check_hashtype(optarg);
3837 if (lsa.lsa_pattern == 0) {
3839 "%s %s: bad stripe hash type '%s'\n",
3840 progname, argv[0], optarg);
3845 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3846 progname, argv[0], argv[optind - 1]);
3851 if (optind == argc) {
3852 fprintf(stderr, "%s %s: DIR must be specified\n",
3857 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
3858 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
3860 "%s %s: stripe offset and count must be specified\n",
3866 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
3867 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
3869 "%s %s: cannot specify -d with -c or -i options\n",
3874 if (mode_opt != NULL) {
3875 mode = strtoul(mode_opt, &end, 8);
3878 "%s %s: bad MODE '%s'\n",
3879 progname, argv[0], mode_opt);
3882 previous_mode = umask(0);
3885 /* initialize stripe parameters */
3886 param = calloc(1, offsetof(typeof(*param), lsp_osts[lsa.lsa_nr_tgts]));
3887 if (param == NULL) {
3889 "%s %s: cannot allocate memory for parameters: %s\n",
3890 progname, argv[0], strerror(ENOMEM));
3894 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3895 param->lsp_stripe_count = lsa.lsa_stripe_count;
3896 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3897 param->lsp_stripe_offset = -1;
3899 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3900 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3901 param->lsp_stripe_pattern = lsa.lsa_pattern;
3903 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
3904 param->lsp_pool = lsa.lsa_pool_name;
3905 param->lsp_is_specific = false;
3906 if (lsa.lsa_nr_tgts > 1) {
3907 if (lsa.lsa_stripe_count > 0 &&
3908 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3909 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3910 fprintf(stderr, "error: %s: stripe count %lld doesn't "
3911 "match the number of MDTs: %d\n",
3912 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
3917 param->lsp_is_specific = true;
3918 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3919 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
3922 dname = argv[optind];
3925 result = llapi_dir_set_default_lmv(dname, param);
3927 result = llapi_dir_create_param(dname, mode, param);
3931 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
3932 progname, dname, strerror(-result));
3935 dname = argv[++optind];
3936 } while (dname != NULL);
3938 if (mode_opt != NULL)
3939 umask(previous_mode);
3946 static int lfs_rmentry(int argc, char **argv)
3953 fprintf(stderr, "error: %s: missing dirname\n",
3959 dname = argv[index];
3960 while (dname != NULL) {
3961 result = llapi_direntry_remove(dname);
3963 fprintf(stderr, "error: %s: remove dir entry '%s' "
3964 "failed\n", argv[0], dname);
3967 dname = argv[++index];
3972 static int lfs_mv(int argc, char **argv)
3974 struct find_param param = {
3981 struct option long_opts[] = {
3982 { .val = 'M', .name = "mdt-index", .has_arg = required_argument },
3983 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3986 while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
3989 param.fp_mdt_index = strtoul(optarg, &end, 0);
3991 fprintf(stderr, "%s: invalid MDT index'%s'\n",
3998 param.fp_verbose = VERBOSE_DETAIL;
4002 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
4003 argv[0], argv[optind - 1]);
4008 if (param.fp_mdt_index == -1) {
4009 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
4013 if (optind >= argc) {
4014 fprintf(stderr, "%s: missing operand path\n", argv[0]);
4018 param.fp_migrate = 1;
4019 rc = llapi_migrate_mdt(argv[optind], ¶m);
4021 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
4022 argv[0], argv[optind], param.fp_mdt_index,
4027 static int lfs_osts(int argc, char **argv)
4029 return lfs_tgts(argc, argv);
4032 static int lfs_mdts(int argc, char **argv)
4034 return lfs_tgts(argc, argv);
4037 #define COOK(value) \
4040 while (value > 1024) { \
4048 #define CDF "%11llu"
4049 #define HDF "%8.1f%c"
4054 MNTDF_INODES = 0x0001,
4055 MNTDF_COOKED = 0x0002,
4056 MNTDF_LAZY = 0x0004,
4057 MNTDF_VERBOSE = 0x0008,
4060 static int showdf(char *mntdir, struct obd_statfs *stat,
4061 char *uuid, enum mntdf_flags flags,
4062 char *type, int index, int rc)
4064 long long avail, used, total;
4066 char *suffix = "KMGTPEZY";
4067 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4068 char tbuf[3 * sizeof(__u64)];
4069 char ubuf[3 * sizeof(__u64)];
4070 char abuf[3 * sizeof(__u64)];
4071 char rbuf[3 * sizeof(__u64)];
4078 if (flags & MNTDF_INODES) {
4079 avail = stat->os_ffree;
4080 used = stat->os_files - stat->os_ffree;
4081 total = stat->os_files;
4083 int shift = flags & MNTDF_COOKED ? 0 : 10;
4085 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4086 used = ((stat->os_blocks - stat->os_bfree) *
4087 stat->os_bsize) >> shift;
4088 total = (stat->os_blocks * stat->os_bsize) >> shift;
4091 if ((used + avail) > 0)
4092 ratio = (double)used / (double)(used + avail);
4094 if (flags & MNTDF_COOKED) {
4098 cook_val = (double)total;
4101 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4104 snprintf(tbuf, sizeof(tbuf), CDF, total);
4106 cook_val = (double)used;
4109 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4112 snprintf(ubuf, sizeof(ubuf), CDF, used);
4114 cook_val = (double)avail;
4117 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4120 snprintf(abuf, sizeof(abuf), CDF, avail);
4122 snprintf(tbuf, sizeof(tbuf), CDF, total);
4123 snprintf(ubuf, sizeof(tbuf), CDF, used);
4124 snprintf(abuf, sizeof(tbuf), CDF, avail);
4127 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
4128 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4129 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4131 printf("[%s:%d]", type, index);
4133 if (stat->os_state) {
4135 * Each character represents the matching
4138 const char state_names[] = "DRSI";
4143 for (i = 0, state = stat->os_state;
4144 state && i < sizeof(state_names); i++) {
4145 if (!(state & (1 << i)))
4147 printf("%c", state_names[i]);
4155 printf(UUF": inactive device\n", uuid);
4158 printf(UUF": %s\n", uuid, strerror(-rc));
4165 struct ll_stat_type {
4170 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags)
4172 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4173 struct obd_uuid uuid_buf;
4174 char *poolname = NULL;
4175 struct ll_stat_type types[] = {
4176 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4177 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4178 { .st_name = NULL } };
4179 struct ll_stat_type *tp;
4180 __u64 ost_ffree = 0;
4188 poolname = strchr(pool, '.');
4189 if (poolname != NULL) {
4190 if (strncmp(fsname, pool, strlen(fsname))) {
4191 fprintf(stderr, "filesystem name incorrect\n");
4199 fd = open(mntdir, O_RDONLY);
4202 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4207 if (flags & MNTDF_INODES)
4208 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4209 "UUID", "Inodes", "IUsed", "IFree",
4210 "IUse%", "Mounted on");
4212 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4213 "UUID", flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4214 "Used", "Available", "Use%", "Mounted on");
4216 for (tp = types; tp->st_name != NULL; tp++) {
4217 for (index = 0; ; index++) {
4218 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4219 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4220 type = flags & MNTDF_LAZY ?
4221 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4222 rc2 = llapi_obd_fstatfs(fd, type, index,
4223 &stat_buf, &uuid_buf);
4228 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4229 if (!(flags & MNTDF_VERBOSE))
4231 } else if (rc2 < 0 && rc == 0) {
4235 if (poolname && tp->st_op == LL_STATFS_LOV &&
4236 llapi_search_ost(fsname, poolname,
4237 obd_uuid2str(&uuid_buf)) != 1)
4240 /* the llapi_obd_statfs() call may have returned with
4241 * an error, but if it filled in uuid_buf we will at
4242 * lease use that to print out a message for that OBD.
4243 * If we didn't get anything in the uuid_buf, then fill
4244 * it in so that we can print an error message. */
4245 if (uuid_buf.uuid[0] == '\0')
4246 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4247 "%s%04x", tp->st_name, index);
4248 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
4249 flags, tp->st_name, index, rc2);
4252 if (tp->st_op == LL_STATFS_LMV) {
4253 sum.os_ffree += stat_buf.os_ffree;
4254 sum.os_files += stat_buf.os_files;
4255 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4256 sum.os_blocks += stat_buf.os_blocks *
4258 sum.os_bfree += stat_buf.os_bfree *
4260 sum.os_bavail += stat_buf.os_bavail *
4262 ost_ffree += stat_buf.os_ffree;
4270 /* If we don't have as many objects free on the OST as inodes
4271 * on the MDS, we reduce the total number of inodes to
4272 * compensate, so that the "inodes in use" number is correct.
4273 * Matches ll_statfs_internal() so the results are consistent. */
4274 if (ost_ffree < sum.os_ffree) {
4275 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4276 sum.os_ffree = ost_ffree;
4279 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4285 static int lfs_df(int argc, char **argv)
4287 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4288 enum mntdf_flags flags = 0;
4289 int c, rc = 0, index = 0;
4290 char fsname[PATH_MAX] = "", *pool_name = NULL;
4291 struct option long_opts[] = {
4292 { .val = 'h', .name = "human-readable",
4293 .has_arg = no_argument },
4294 { .val = 'i', .name = "inodes", .has_arg = no_argument },
4295 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4296 { .val = 'p', .name = "pool", .has_arg = required_argument },
4297 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4300 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
4303 flags |= MNTDF_COOKED;
4306 flags |= MNTDF_INODES;
4309 flags |= MNTDF_LAZY;
4315 flags |= MNTDF_VERBOSE;
4321 if (optind < argc && !realpath(argv[optind], path)) {
4323 fprintf(stderr, "error: invalid path '%s': %s\n",
4324 argv[optind], strerror(-rc));
4328 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4329 /* Check if we have a mount point */
4330 if (mntdir[0] == '\0')
4333 rc = mntdf(mntdir, fsname, pool_name, flags);
4334 if (rc || path[0] != '\0')
4336 fsname[0] = '\0'; /* avoid matching in next loop */
4337 mntdir[0] = '\0'; /* avoid matching in next loop */
4343 static int lfs_getname(int argc, char **argv)
4345 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
4346 int rc = 0, index = 0, c;
4347 char buf[sizeof(struct obd_uuid)];
4349 while ((c = getopt(argc, argv, "h")) != -1)
4352 if (optind == argc) { /* no paths specified, get all paths. */
4353 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4354 rc = llapi_getname(mntdir, buf, sizeof(buf));
4357 "cannot get name for `%s': %s\n",
4358 mntdir, strerror(-rc));
4362 printf("%s %s\n", buf, mntdir);
4364 path[0] = fsname[0] = mntdir[0] = 0;
4366 } else { /* paths specified, only attempt to search these. */
4367 for (; optind < argc; optind++) {
4368 rc = llapi_getname(argv[optind], buf, sizeof(buf));
4371 "cannot get name for `%s': %s\n",
4372 argv[optind], strerror(-rc));
4376 printf("%s %s\n", buf, argv[optind]);
4382 static int lfs_check(int argc, char **argv)
4385 char mntdir[PATH_MAX] = {'\0'};
4392 fprintf(stderr, "%s check: server type must be specified\n",
4397 obd_types[0] = obd_type1;
4398 obd_types[1] = obd_type2;
4400 if (strcmp(argv[1], "osts") == 0) {
4401 strcpy(obd_types[0], "osc");
4402 } else if (strcmp(argv[1], "mds") == 0) {
4403 strcpy(obd_types[0], "mdc");
4404 } else if (strcmp(argv[1], "servers") == 0) {
4406 strcpy(obd_types[0], "osc");
4407 strcpy(obd_types[1], "mdc");
4409 fprintf(stderr, "%s check: unrecognized option '%s'\n",
4414 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
4415 if (rc < 0 || mntdir[0] == '\0') {
4417 "%s check: cannot find mounted Lustre filesystem: %s\n",
4418 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
4422 rc = llapi_target_check(num_types, obd_types, mntdir);
4424 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
4425 progname, argv[1], strerror(-rc));
4431 #ifdef HAVE_SYS_QUOTA_H
4432 #define ARG2INT(nr, str, msg) \
4435 nr = strtol(str, &endp, 0); \
4436 if (*endp != '\0') { \
4437 fprintf(stderr, "%s: bad %s '%s'\n", \
4438 progname, msg, str); \
4443 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
4445 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
4446 * returns the value or ULONG_MAX on integer overflow or incorrect format
4448 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
4449 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
4450 * 3. empty integer value is interpreted as 0
4452 static unsigned long str2sec(const char* timestr)
4454 const char spec[] = "smhdw";
4455 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4456 unsigned long val = 0;
4459 if (strpbrk(timestr, spec) == NULL) {
4460 /* no specifiers inside the time string,
4461 should treat it as an integer value */
4462 val = strtoul(timestr, &tail, 10);
4463 return *tail ? ULONG_MAX : val;
4466 /* format string is XXwXXdXXhXXmXXs */
4472 v = strtoul(timestr, &tail, 10);
4473 if (v == ULONG_MAX || *tail == '\0')
4474 /* value too large (ULONG_MAX or more)
4475 or missing specifier */
4478 ptr = strchr(spec, *tail);
4480 /* unknown specifier */
4485 /* check if product will overflow the type */
4486 if (!(v < ULONG_MAX / mult[ind]))
4489 ADD_OVERFLOW(val, mult[ind] * v);
4490 if (val == ULONG_MAX)
4502 #define ARG2ULL(nr, str, def_units) \
4504 unsigned long long limit, units = def_units; \
4507 rc = llapi_parse_size(str, &limit, &units, 1); \
4509 fprintf(stderr, "%s: invalid limit '%s'\n", \
4516 static inline int has_times_option(int argc, char **argv)
4520 for (i = 1; i < argc; i++)
4521 if (!strcmp(argv[i], "-t"))
4527 int lfs_setquota_times(int argc, char **argv)
4530 struct if_quotactl qctl;
4531 char *mnt, *obd_type = (char *)qctl.obd_type;
4532 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4533 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
4534 struct option long_opts[] = {
4535 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
4536 { .val = 'g', .name = "group", .has_arg = no_argument },
4537 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
4538 { .val = 'p', .name = "projid", .has_arg = no_argument },
4539 { .val = 't', .name = "times", .has_arg = no_argument },
4540 { .val = 'u', .name = "user", .has_arg = no_argument },
4544 memset(&qctl, 0, sizeof(qctl));
4545 qctl.qc_cmd = LUSTRE_Q_SETINFO;
4546 qctl.qc_type = ALLQUOTA;
4548 while ((c = getopt_long(argc, argv, "b:gi:ptu",
4549 long_opts, NULL)) != -1) {
4560 if (qctl.qc_type != ALLQUOTA) {
4561 fprintf(stderr, "error: -u/g/p can't be used "
4562 "more than once\n");
4565 qctl.qc_type = qtype;
4568 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
4569 fprintf(stderr, "error: bad block-grace: %s\n",
4573 dqb->dqb_valid |= QIF_BTIME;
4576 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
4577 fprintf(stderr, "error: bad inode-grace: %s\n",
4581 dqb->dqb_valid |= QIF_ITIME;
4583 case 't': /* Yes, of course! */
4585 default: /* getopt prints error message for us when opterr != 0 */
4590 if (qctl.qc_type == ALLQUOTA) {
4591 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
4595 if (optind != argc - 1) {
4596 fprintf(stderr, "error: unexpected parameters encountered\n");
4601 rc = llapi_quotactl(mnt, &qctl);
4604 fprintf(stderr, "%s %s ", obd_type,
4605 obd_uuid2str(&qctl.obd_uuid));
4606 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
4613 #define BSLIMIT (1 << 0)
4614 #define BHLIMIT (1 << 1)
4615 #define ISLIMIT (1 << 2)
4616 #define IHLIMIT (1 << 3)
4618 int lfs_setquota(int argc, char **argv)
4621 struct if_quotactl qctl;
4622 char *mnt, *obd_type = (char *)qctl.obd_type;
4623 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4624 struct option long_opts[] = {
4625 { .val = 'b', .name = "block-softlimit",
4626 .has_arg = required_argument },
4627 { .val = 'B', .name = "block-hardlimit",
4628 .has_arg = required_argument },
4629 { .val = 'g', .name = "group", .has_arg = required_argument },
4630 { .val = 'i', .name = "inode-softlimit",
4631 .has_arg = required_argument },
4632 { .val = 'I', .name = "inode-hardlimit",
4633 .has_arg = required_argument },
4634 { .val = 'p', .name = "projid", .has_arg = required_argument },
4635 { .val = 'u', .name = "user", .has_arg = required_argument },
4637 unsigned limit_mask = 0;
4641 if (has_times_option(argc, argv))
4642 return lfs_setquota_times(argc, argv);
4644 memset(&qctl, 0, sizeof(qctl));
4645 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
4646 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
4647 * so it can be used as a marker that qc_type
4648 * isn't reinitialized from command line */
4650 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
4651 long_opts, NULL)) != -1) {
4655 rc = name2uid(&qctl.qc_id, optarg);
4659 rc = name2gid(&qctl.qc_id, optarg);
4663 rc = name2projid(&qctl.qc_id, optarg);
4665 if (qctl.qc_type != ALLQUOTA) {
4667 "%s setquota: only one of -u, -g or -p may be specified\n",
4671 qctl.qc_type = qtype;
4673 qctl.qc_id = strtoul(optarg, &endptr, 10);
4674 if (*endptr != '\0') {
4676 "%s setquota: invalid id '%s'\n",
4683 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
4684 dqb->dqb_bsoftlimit >>= 10;
4685 limit_mask |= BSLIMIT;
4686 if (dqb->dqb_bsoftlimit &&
4687 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
4689 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
4690 "See '%s help setquota' or Lustre manual for details\n",
4691 progname, dqb->dqb_bsoftlimit,
4695 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
4696 dqb->dqb_bhardlimit >>= 10;
4697 limit_mask |= BHLIMIT;
4698 if (dqb->dqb_bhardlimit &&
4699 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
4701 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
4702 "See '%s help setquota' or Lustre manual for details\n",
4703 progname, dqb->dqb_bhardlimit,
4707 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
4708 limit_mask |= ISLIMIT;
4709 if (dqb->dqb_isoftlimit &&
4710 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
4712 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
4713 "See '%s help setquota' or Lustre manual for details\n",
4714 progname, dqb->dqb_isoftlimit,
4718 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
4719 limit_mask |= IHLIMIT;
4720 if (dqb->dqb_ihardlimit &&
4721 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
4723 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
4724 "See '%s help setquota' or Lustre manual for details\n",
4725 progname, dqb->dqb_ihardlimit,
4730 "%s setquota: unrecognized option '%s'\n",
4731 progname, argv[optind - 1]);
4736 if (qctl.qc_type == ALLQUOTA) {
4738 "%s setquota: either -u or -g must be specified\n",
4743 if (limit_mask == 0) {
4745 "%s setquota: at least one limit must be specified\n",
4750 if (optind != argc - 1) {
4752 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
4753 progname, argv[optind]);
4759 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
4760 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
4761 /* sigh, we can't just set blimits/ilimits */
4762 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
4763 .qc_type = qctl.qc_type,
4764 .qc_id = qctl.qc_id};
4766 rc = llapi_quotactl(mnt, &tmp_qctl);
4770 if (!(limit_mask & BHLIMIT))
4771 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
4772 if (!(limit_mask & BSLIMIT))
4773 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
4774 if (!(limit_mask & IHLIMIT))
4775 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
4776 if (!(limit_mask & ISLIMIT))
4777 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
4779 /* Keep grace times if we have got no softlimit arguments */
4780 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
4781 dqb->dqb_valid |= QIF_BTIME;
4782 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
4785 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
4786 dqb->dqb_valid |= QIF_ITIME;
4787 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
4791 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
4792 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
4794 rc = llapi_quotactl(mnt, &qctl);
4798 "%s setquota: cannot quotactl '%s' '%s': %s",
4800 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
4807 /* Converts seconds value into format string
4808 * result is returned in buf
4810 * 1. result is in descenting order: 1w2d3h4m5s
4811 * 2. zero fields are not filled (except for p. 3): 5d1s
4812 * 3. zero seconds value is presented as "0s"
4814 static char * __sec2str(time_t seconds, char *buf)
4816 const char spec[] = "smhdw";
4817 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4822 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
4823 c = seconds / mult[i];
4825 if (c > 0 || (i == 0 && buf == tail))
4826 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
4834 static void sec2str(time_t seconds, char *buf, int rc)
4841 tail = __sec2str(seconds, tail);
4843 if (rc && tail - buf < 39) {
4849 static void diff2str(time_t seconds, char *buf, time_t now)
4855 if (seconds <= now) {
4856 strcpy(buf, "none");
4859 __sec2str(seconds - now, buf);
4862 static void print_quota_title(char *name, struct if_quotactl *qctl,
4863 bool human_readable)
4865 printf("Disk quotas for %s %s (%cid %u):\n",
4866 qtype_name(qctl->qc_type), name,
4867 *qtype_name(qctl->qc_type), qctl->qc_id);
4868 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
4869 "Filesystem", human_readable ? "used" : "kbytes",
4870 "quota", "limit", "grace",
4871 "files", "quota", "limit", "grace");
4874 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
4877 snprintf(buf, buflen, "%ju", (uintmax_t)num);
4880 snprintf(buf, buflen, "%5.4gP",
4881 (double)num / ((__u64)1 << 40));
4883 snprintf(buf, buflen, "%5.4gT",
4884 (double)num / (1 << 30));
4886 snprintf(buf, buflen, "%5.4gG",
4887 (double)num / (1 << 20));
4889 snprintf(buf, buflen, "%5.4gM",
4890 (double)num / (1 << 10));
4892 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
4896 #define STRBUF_LEN 32
4897 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
4904 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
4905 int bover = 0, iover = 0;
4906 struct obd_dqblk *dqb = &qctl->qc_dqblk;
4907 char numbuf[3][STRBUF_LEN];
4909 char strbuf[STRBUF_LEN];
4911 if (dqb->dqb_bhardlimit &&
4912 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
4914 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
4915 if (dqb->dqb_btime > now) {
4922 if (dqb->dqb_ihardlimit &&
4923 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
4925 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
4926 if (dqb->dqb_itime > now) {
4934 if (strlen(mnt) > 15)
4935 printf("%s\n%15s", mnt, "");
4937 printf("%15s", mnt);
4940 diff2str(dqb->dqb_btime, timebuf, now);
4942 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
4943 strbuf, sizeof(strbuf), h);
4944 if (rc == -EREMOTEIO)
4945 sprintf(numbuf[0], "%s*", strbuf);
4947 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
4948 "%s" : "[%s]", strbuf);
4950 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
4951 if (type == QC_GENERAL)
4952 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
4953 "%s" : "[%s]", strbuf);
4955 sprintf(numbuf[1], "%s", "-");
4957 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
4958 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
4959 "%s" : "[%s]", strbuf);
4961 printf(" %7s%c %6s %7s %7s",
4962 numbuf[0], bover ? '*' : ' ', numbuf[1],
4963 numbuf[2], bover > 1 ? timebuf : "-");
4966 diff2str(dqb->dqb_itime, timebuf, now);
4968 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
4969 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
4971 if (type == QC_GENERAL)
4972 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
4974 (uintmax_t)dqb->dqb_isoftlimit);
4976 sprintf(numbuf[1], "%s", "-");
4978 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
4979 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
4981 if (type != QC_OSTIDX)
4982 printf(" %7s%c %6s %7s %7s",
4983 numbuf[0], iover ? '*' : ' ', numbuf[1],
4984 numbuf[2], iover > 1 ? timebuf : "-");
4986 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
4989 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
4990 qctl->qc_cmd == Q_GETOINFO) {
4994 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
4995 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
4996 printf("Block grace time: %s; Inode grace time: %s\n",
4997 bgtimebuf, igtimebuf);
5001 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5002 bool h, __u64 *total)
5004 int rc = 0, rc1 = 0, count = 0;
5005 __u32 valid = qctl->qc_valid;
5007 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5009 fprintf(stderr, "can not get %s count: %s\n",
5010 is_mdt ? "mdt": "ost", strerror(-rc));
5014 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5015 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5016 rc = llapi_quotactl(mnt, qctl);
5018 /* It is remote client case. */
5019 if (rc == -EOPNOTSUPP) {
5026 fprintf(stderr, "quotactl %s%d failed.\n",
5027 is_mdt ? "mdt": "ost", qctl->qc_idx);
5031 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5032 qctl->qc_valid, 0, h);
5033 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5034 qctl->qc_dqblk.dqb_bhardlimit;
5037 qctl->qc_valid = valid;
5041 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5042 int verbose, int quiet, bool human_readable)
5044 int rc1 = 0, rc2 = 0, rc3 = 0;
5045 char *obd_type = (char *)qctl->obd_type;
5046 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5047 __u64 total_ialloc = 0, total_balloc = 0;
5050 rc1 = llapi_quotactl(mnt, qctl);
5054 fprintf(stderr, "%s quotas are not enabled.\n",
5055 qtype_name(qctl->qc_type));
5058 fprintf(stderr, "Permission denied.\n");
5061 /* We already got error message. */
5064 fprintf(stderr, "Unexpected quotactl error: %s\n",
5069 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
5070 print_quota_title(name, qctl, human_readable);
5072 if (rc1 && *obd_type)
5073 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5075 if (qctl->qc_valid != QC_GENERAL)
5078 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5079 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5080 (QIF_LIMITS|QIF_USAGE));
5082 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
5084 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
5086 char strbuf[STRBUF_LEN];
5088 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5090 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5092 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5094 printf("Total allocated inode limit: %ju, total "
5095 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5099 if (rc1 || rc2 || rc3 || inacc)
5100 printf("Some errors happened when getting quota info. "
5101 "Some devices may be not working or deactivated. "
5102 "The data in \"[]\" is inaccurate.\n");
5108 static int lfs_project(int argc, char **argv)
5110 int ret = 0, err = 0, c, i;
5111 struct project_handle_control phc = { 0 };
5112 enum lfs_project_ops_t op;
5115 phc.assign_projid = false;
5116 /* default action */
5117 op = LFS_PROJECT_LIST;
5119 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
5122 if (op != LFS_PROJECT_LIST) {
5124 "%s: cannot specify '-c' '-C' '-s' together\n",
5129 op = LFS_PROJECT_CHECK;
5132 if (op != LFS_PROJECT_LIST) {
5134 "%s: cannot specify '-c' '-C' '-s' together\n",
5139 op = LFS_PROJECT_CLEAR;
5142 if (op != LFS_PROJECT_LIST) {
5144 "%s: cannot specify '-c' '-C' '-s' together\n",
5149 phc.set_inherit = true;
5150 op = LFS_PROJECT_SET;
5156 phc.keep_projid = true;
5159 phc.recursive = true;
5162 phc.projid = strtoul(optarg, NULL, 0);
5163 phc.assign_projid = true;
5167 phc.newline = false;
5170 fprintf(stderr, "%s: invalid option '%c'\n",
5176 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
5177 op = LFS_PROJECT_SET;
5178 phc.set_projid = true;
5179 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
5180 phc.set_projid = true;
5184 case LFS_PROJECT_CHECK:
5185 if (phc.keep_projid) {
5187 "%s: '-k' is useless together with '-c'\n",
5192 case LFS_PROJECT_CLEAR:
5195 "%s: '-0' is useless together with '-C'\n",
5199 if (phc.assign_projid) {
5201 "%s: '-p' is useless together with '-C'\n",
5206 case LFS_PROJECT_SET:
5209 "%s: '-0' is useless together with '-s'\n",
5213 if (phc.keep_projid) {
5215 "%s: '-k' is useless together with '-s'\n",
5223 "%s: '-0' is useless for list operations\n",
5233 fprintf(stderr, "%s: missing file or directory target(s)\n",
5238 for (i = 0; i < argc; i++) {
5240 case LFS_PROJECT_CHECK:
5241 err = lfs_project_check(argv[i], &phc);
5243 case LFS_PROJECT_LIST:
5244 err = lfs_project_list(argv[i], &phc);
5246 case LFS_PROJECT_CLEAR:
5247 err = lfs_project_clear(argv[i], &phc);
5249 case LFS_PROJECT_SET:
5250 err = lfs_project_set(argv[i], &phc);
5262 static int lfs_quota(int argc, char **argv)
5265 char *mnt, *name = NULL;
5266 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
5267 .qc_type = ALLQUOTA };
5268 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
5269 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
5271 __u32 valid = QC_GENERAL, idx = 0;
5272 bool human_readable = false;
5275 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
5286 if (qctl.qc_type != ALLQUOTA) {
5288 "%s quota: only one of -u, -g, or -p may be specified\n",
5292 qctl.qc_type = qtype;
5295 qctl.qc_cmd = LUSTRE_Q_GETINFO;
5298 valid = qctl.qc_valid = QC_UUID;
5299 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
5302 valid = qctl.qc_valid = QC_MDTIDX;
5303 idx = qctl.qc_idx = atoi(optarg);
5304 if (idx == 0 && *optarg != '0') {
5306 "%s quota: invalid MDT index '%s'\n",
5312 valid = qctl.qc_valid = QC_OSTIDX;
5313 idx = qctl.qc_idx = atoi(optarg);
5314 if (idx == 0 && *optarg != '0') {
5316 "%s quota: invalid OST index '%s'\n",
5328 human_readable = true;
5331 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
5332 progname, argv[optind - 1]);
5337 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
5338 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
5339 optind == argc - 1) {
5341 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
5342 qctl.qc_valid = valid;
5345 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
5346 qctl.qc_type = qtype;
5347 if (qtype == USRQUOTA) {
5348 qctl.qc_id = geteuid();
5349 rc = uid2name(&name, qctl.qc_id);
5351 qctl.qc_id = getegid();
5352 rc = gid2name(&name, qctl.qc_id);
5357 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
5362 /* lfs quota -u username /path/to/lustre/mount */
5363 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
5364 /* options should be followed by u/g-name and mntpoint */
5365 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
5367 "%s quota: name and mount point must be specified\n",
5372 name = argv[optind++];
5373 switch (qctl.qc_type) {
5375 rc = name2uid(&qctl.qc_id, name);
5378 rc = name2gid(&qctl.qc_id, name);
5381 rc = name2projid(&qctl.qc_id, name);
5388 qctl.qc_id = strtoul(name, &endptr, 10);
5389 if (*endptr != '\0') {
5390 fprintf(stderr, "%s quota: invalid id '%s'\n",
5396 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
5398 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
5399 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
5406 #endif /* HAVE_SYS_QUOTA_H! */
5408 static int flushctx_ioctl(char *mp)
5412 fd = open(mp, O_RDONLY);
5414 fprintf(stderr, "flushctx: error open %s: %s\n",
5415 mp, strerror(errno));
5419 rc = ioctl(fd, LL_IOC_FLUSHCTX);
5421 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
5422 mp, strerror(errno));
5428 static int lfs_flushctx(int argc, char **argv)
5430 int kdestroy = 0, c;
5431 char mntdir[PATH_MAX] = {'\0'};
5435 while ((c = getopt(argc, argv, "k")) != -1) {
5441 fprintf(stderr, "error: %s: option '-%c' "
5442 "unrecognized\n", argv[0], c);
5448 if ((rc = system("kdestroy > /dev/null")) != 0) {
5449 rc = WEXITSTATUS(rc);
5450 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
5454 if (optind >= argc) {
5455 /* flush for all mounted lustre fs. */
5456 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
5457 /* Check if we have a mount point */
5458 if (mntdir[0] == '\0')
5461 if (flushctx_ioctl(mntdir))
5464 mntdir[0] = '\0'; /* avoid matching in next loop */
5467 /* flush fs as specified */
5468 while (optind < argc) {
5469 if (flushctx_ioctl(argv[optind++]))
5476 static int lfs_cp(int argc, char **argv)
5478 fprintf(stderr, "remote client copy file(s).\n"
5479 "obsolete, does not support it anymore.\n");
5483 static int lfs_ls(int argc, char **argv)
5485 fprintf(stderr, "remote client lists directory contents.\n"
5486 "obsolete, does not support it anymore.\n");
5490 static int lfs_changelog(int argc, char **argv)
5492 void *changelog_priv;
5493 struct changelog_rec *rec;
5494 long long startrec = 0, endrec = 0;
5496 struct option long_opts[] = {
5497 { .val = 'f', .name = "follow", .has_arg = no_argument },
5499 char short_opts[] = "f";
5502 while ((rc = getopt_long(argc, argv, short_opts,
5503 long_opts, NULL)) != -1) {
5511 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5512 argv[0], argv[optind - 1]);
5519 mdd = argv[optind++];
5521 startrec = strtoll(argv[optind++], NULL, 10);
5523 endrec = strtoll(argv[optind++], NULL, 10);
5525 rc = llapi_changelog_start(&changelog_priv,
5526 CHANGELOG_FLAG_BLOCK |
5527 CHANGELOG_FLAG_JOBID |
5528 CHANGELOG_FLAG_EXTRA_FLAGS |
5529 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
5532 fprintf(stderr, "Can't start changelog: %s\n",
5533 strerror(errno = -rc));
5537 rc = llapi_changelog_set_xflags(changelog_priv,
5538 CHANGELOG_EXTRA_FLAG_UIDGID);
5540 fprintf(stderr, "Can't set xflags for changelog: %s\n",
5541 strerror(errno = -rc));
5545 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
5549 if (endrec && rec->cr_index > endrec) {
5550 llapi_changelog_free(&rec);
5553 if (rec->cr_index < startrec) {
5554 llapi_changelog_free(&rec);
5558 secs = rec->cr_time >> 30;
5559 gmtime_r(&secs, &ts);
5560 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
5561 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
5562 changelog_type2str(rec->cr_type),
5563 ts.tm_hour, ts.tm_min, ts.tm_sec,
5564 (int)(rec->cr_time & ((1 << 30) - 1)),
5565 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
5566 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
5568 if (rec->cr_flags & CLF_JOBID) {
5569 struct changelog_ext_jobid *jid =
5570 changelog_rec_jobid(rec);
5572 if (jid->cr_jobid[0] != '\0')
5573 printf(" j=%s", jid->cr_jobid);
5576 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
5577 struct changelog_ext_extra_flags *ef =
5578 changelog_rec_extra_flags(rec);
5580 printf(" ef=0x%llx", ef->cr_extra_flags);
5582 if (ef->cr_extra_flags & CLFE_UIDGID) {
5583 struct changelog_ext_uidgid *uidgid =
5584 changelog_rec_uidgid(rec);
5586 printf(" u=%llu:%llu",
5587 uidgid->cr_uid, uidgid->cr_gid);
5591 if (rec->cr_namelen)
5592 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
5593 rec->cr_namelen, changelog_rec_name(rec));
5595 if (rec->cr_flags & CLF_RENAME) {
5596 struct changelog_ext_rename *rnm =
5597 changelog_rec_rename(rec);
5599 if (!fid_is_zero(&rnm->cr_sfid))
5600 printf(" s="DFID" sp="DFID" %.*s",
5601 PFID(&rnm->cr_sfid),
5602 PFID(&rnm->cr_spfid),
5603 (int)changelog_rec_snamelen(rec),
5604 changelog_rec_sname(rec));
5608 llapi_changelog_free(&rec);
5611 llapi_changelog_fini(&changelog_priv);
5614 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
5616 return (rc == 1 ? 0 : rc);
5619 static int lfs_changelog_clear(int argc, char **argv)
5627 endrec = strtoll(argv[3], NULL, 10);
5629 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
5632 fprintf(stderr, "%s: record out of range: %llu\n",
5634 else if (rc == -ENOENT)
5635 fprintf(stderr, "%s: no changelog user: %s\n",
5638 fprintf(stderr, "%s error: %s\n", argv[0],
5647 static int lfs_fid2path(int argc, char **argv)
5649 struct option long_opts[] = {
5650 { .val = 'c', .name = "cur", .has_arg = no_argument },
5651 { .val = 'l', .name = "link", .has_arg = required_argument },
5652 { .val = 'r', .name = "rec", .has_arg = required_argument },
5654 char short_opts[] = "cl:r:";
5655 char *device, *fid, *path;
5656 long long recno = -1;
5662 while ((rc = getopt_long(argc, argv, short_opts,
5663 long_opts, NULL)) != -1) {
5669 linkno = strtol(optarg, NULL, 10);
5672 recno = strtoll(optarg, NULL, 10);
5677 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5678 argv[0], argv[optind - 1]);
5686 device = argv[optind++];
5687 path = calloc(1, PATH_MAX);
5689 fprintf(stderr, "error: Not enough memory\n");
5694 while (optind < argc) {
5695 fid = argv[optind++];
5697 lnktmp = (linkno >= 0) ? linkno : 0;
5699 int oldtmp = lnktmp;
5700 long long rectmp = recno;
5702 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
5705 fprintf(stderr, "%s: error on FID %s: %s\n",
5706 argv[0], fid, strerror(errno = -rc2));
5713 fprintf(stdout, "%lld ", rectmp);
5714 if (device[0] == '/') {
5715 fprintf(stdout, "%s", device);
5716 if (device[strlen(device) - 1] != '/')
5717 fprintf(stdout, "/");
5718 } else if (path[0] == '\0') {
5719 fprintf(stdout, "/");
5721 fprintf(stdout, "%s\n", path);
5724 /* specified linkno */
5726 if (oldtmp == lnktmp)
5736 static int lfs_path2fid(int argc, char **argv)
5738 struct option long_opts[] = {
5739 { .val = 'p', .name = "parents", .has_arg = no_argument },
5742 const char short_opts[] = "p";
5743 const char *sep = "";
5746 bool show_parents = false;
5748 while ((rc = getopt_long(argc, argv, short_opts,
5749 long_opts, NULL)) != -1) {
5752 show_parents = true;
5755 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5756 argv[0], argv[optind - 1]);
5761 if (optind > argc - 1)
5763 else if (optind < argc - 1)
5767 for (path = argv + optind; *path != NULL; path++) {
5769 if (!show_parents) {
5770 err = llapi_path2fid(*path, &fid);
5772 printf("%s%s"DFID"\n",
5773 *sep != '\0' ? *path : "", sep,
5776 char name[NAME_MAX + 1];
5777 unsigned int linkno = 0;
5779 while ((err = llapi_path2parent(*path, linkno, &fid,
5780 name, sizeof(name))) == 0) {
5781 if (*sep != '\0' && linkno == 0)
5782 printf("%s%s", *path, sep);
5784 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
5789 /* err == -ENODATA is end-of-loop */
5790 if (linkno > 0 && err == -ENODATA) {
5797 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
5798 argv[0], show_parents ? "parent " : "", *path,
5810 static int lfs_data_version(int argc, char **argv)
5817 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
5822 while ((c = getopt(argc, argv, "nrw")) != -1) {
5825 data_version_flags = 0;
5828 data_version_flags |= LL_DV_RD_FLUSH;
5831 data_version_flags |= LL_DV_WR_FLUSH;
5840 path = argv[optind];
5841 fd = open(path, O_RDONLY);
5843 err(errno, "cannot open file %s", path);
5845 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
5847 err(errno, "cannot get version for %s", path);
5849 printf("%ju" "\n", (uintmax_t)data_version);
5855 static int lfs_hsm_state(int argc, char **argv)
5860 struct hsm_user_state hus;
5868 rc = llapi_hsm_state_get(path, &hus);
5870 fprintf(stderr, "can't get hsm state for %s: %s\n",
5871 path, strerror(errno = -rc));
5875 /* Display path name and status flags */
5876 printf("%s: (0x%08x)", path, hus.hus_states);
5878 if (hus.hus_states & HS_RELEASED)
5879 printf(" released");
5880 if (hus.hus_states & HS_EXISTS)
5882 if (hus.hus_states & HS_DIRTY)
5884 if (hus.hus_states & HS_ARCHIVED)
5885 printf(" archived");
5886 /* Display user-settable flags */
5887 if (hus.hus_states & HS_NORELEASE)
5888 printf(" never_release");
5889 if (hus.hus_states & HS_NOARCHIVE)
5890 printf(" never_archive");
5891 if (hus.hus_states & HS_LOST)
5892 printf(" lost_from_hsm");
5894 if (hus.hus_archive_id != 0)
5895 printf(", archive_id:%d", hus.hus_archive_id);
5898 } while (++i < argc);
5903 #define LFS_HSM_SET 0
5904 #define LFS_HSM_CLEAR 1
5907 * Generic function to set or clear HSM flags.
5908 * Used by hsm_set and hsm_clear.
5910 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
5912 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
5914 struct option long_opts[] = {
5915 { .val = 'A', .name = "archived", .has_arg = no_argument },
5916 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
5917 { .val = 'd', .name = "dirty", .has_arg = no_argument },
5918 { .val = 'e', .name = "exists", .has_arg = no_argument },
5919 { .val = 'l', .name = "lost", .has_arg = no_argument },
5920 { .val = 'r', .name = "norelease", .has_arg = no_argument },
5921 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
5923 char short_opts[] = "lraAdei:";
5927 __u32 archive_id = 0;
5933 while ((c = getopt_long(argc, argv, short_opts,
5934 long_opts, NULL)) != -1) {
5940 mask |= HS_NOARCHIVE;
5943 mask |= HS_ARCHIVED;
5946 mask |= HS_NORELEASE;
5955 archive_id = strtol(optarg, &end, 10);
5957 fprintf(stderr, "invalid archive_id: '%s'\n",
5965 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5966 argv[0], argv[optind - 1]);
5971 /* User should have specified a flag */
5975 while (optind < argc) {
5977 path = argv[optind];
5979 /* If mode == 0, this means we apply the mask. */
5980 if (mode == LFS_HSM_SET)
5981 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
5983 rc = llapi_hsm_state_set(path, 0, mask, 0);
5986 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
5987 path, strerror(errno = -rc));
5996 static int lfs_hsm_action(int argc, char **argv)
6001 struct hsm_current_action hca;
6002 struct hsm_extent he;
6003 enum hsm_user_action hua;
6004 enum hsm_progress_states hps;
6012 rc = llapi_hsm_current_action(path, &hca);
6014 fprintf(stderr, "can't get hsm action for %s: %s\n",
6015 path, strerror(errno = -rc));
6018 he = hca.hca_location;
6019 hua = hca.hca_action;
6020 hps = hca.hca_state;
6022 printf("%s: %s", path, hsm_user_action2name(hua));
6024 /* Skip file without action */
6025 if (hca.hca_action == HUA_NONE) {
6030 printf(" %s ", hsm_progress_state2name(hps));
6032 if ((hps == HPS_RUNNING) &&
6033 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
6034 printf("(%llu bytes moved)\n",
6035 (unsigned long long)he.length);
6036 else if ((he.offset + he.length) == LUSTRE_EOF)
6037 printf("(from %llu to EOF)\n",
6038 (unsigned long long)he.offset);
6040 printf("(from %llu to %llu)\n",
6041 (unsigned long long)he.offset,
6042 (unsigned long long)(he.offset + he.length));
6044 } while (++i < argc);
6049 static int lfs_hsm_set(int argc, char **argv)
6051 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
6054 static int lfs_hsm_clear(int argc, char **argv)
6056 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
6060 * Check file state and return its fid, to be used by lfs_hsm_request().
6062 * \param[in] file Path to file to check
6063 * \param[in,out] fid Pointer to allocated lu_fid struct.
6064 * \param[in,out] last_dev Pointer to last device id used.
6066 * \return 0 on success.
6068 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
6074 rc = lstat(file, &st);
6076 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
6079 /* Checking for regular file as archiving as posix copytool
6080 * rejects archiving files other than regular files
6082 if (!S_ISREG(st.st_mode)) {
6083 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
6086 /* A request should be ... */
6087 if (*last_dev != st.st_dev && *last_dev != 0) {
6088 fprintf(stderr, "All files should be "
6089 "on the same filesystem: %s\n", file);
6092 *last_dev = st.st_dev;
6094 rc = llapi_path2fid(file, fid);
6096 fprintf(stderr, "Cannot read FID of %s: %s\n",
6097 file, strerror(-rc));
6103 /* Fill an HSM HUR item with a given file name.
6105 * If mntpath is set, then the filename is actually a FID, and no
6106 * lookup on the filesystem will be performed.
6108 * \param[in] hur the user request to fill
6109 * \param[in] idx index of the item inside the HUR to fill
6110 * \param[in] mntpath mountpoint of Lustre
6111 * \param[in] fname filename (if mtnpath is NULL)
6112 * or FID (if mntpath is set)
6113 * \param[in] last_dev pointer to last device id used
6115 * \retval 0 on success
6116 * \retval CMD_HELP or a negative errno on error
6118 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
6119 const char *mntpath, const char *fname,
6122 struct hsm_user_item *hui = &hur->hur_user_item[idx];
6125 hui->hui_extent.length = -1;
6127 if (mntpath != NULL) {
6130 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
6134 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
6139 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
6143 hur->hur_request.hr_itemcount++;
6148 static int lfs_hsm_request(int argc, char **argv, int action)
6150 struct option long_opts[] = {
6151 { .val = 'a', .name = "archive", .has_arg = required_argument },
6152 { .val = 'D', .name = "data", .has_arg = required_argument },
6153 { .val = 'l', .name = "filelist", .has_arg = required_argument },
6154 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
6157 char short_opts[] = "l:D:a:m:";
6158 struct hsm_user_request *hur, *oldhur;
6163 char *filelist = NULL;
6164 char fullpath[PATH_MAX];
6165 char *opaque = NULL;
6169 int nbfile_alloc = 0;
6170 char *some_file = NULL;
6171 char *mntpath = NULL;
6177 while ((c = getopt_long(argc, argv, short_opts,
6178 long_opts, NULL)) != -1) {
6187 if (action != HUA_ARCHIVE &&
6188 action != HUA_REMOVE) {
6190 "error: -a is supported only "
6191 "when archiving or removing\n");
6194 archive_id = atoi(optarg);
6197 if (some_file == NULL) {
6199 some_file = strdup(optarg);
6205 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6206 argv[0], argv[optind - 1]);
6211 /* All remaining args are files, so we have at least nbfile */
6212 nbfile = argc - optind;
6214 if ((nbfile == 0) && (filelist == NULL))
6218 opaque_len = strlen(opaque);
6220 /* Alloc the request structure with enough place to store all files
6221 * from command line. */
6222 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
6224 fprintf(stderr, "Cannot create the request: %s\n",
6228 nbfile_alloc = nbfile;
6230 hur->hur_request.hr_action = action;
6231 hur->hur_request.hr_archive_id = archive_id;
6232 hur->hur_request.hr_flags = 0;
6234 /* All remaining args are files, add them */
6235 if (nbfile != 0 && some_file == NULL)
6236 some_file = strdup(argv[optind]);
6238 for (i = 0; i < nbfile; i++) {
6239 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
6245 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
6247 /* If a filelist was specified, read the filelist from it. */
6248 if (filelist != NULL) {
6249 fp = fopen(filelist, "r");
6251 fprintf(stderr, "Cannot read the file list %s: %s\n",
6252 filelist, strerror(errno));
6257 while ((rc = getline(&line, &len, fp)) != -1) {
6258 /* If allocated buffer was too small, get something
6260 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
6263 nbfile_alloc = nbfile_alloc * 2 + 1;
6265 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
6268 fprintf(stderr, "hsm: cannot allocate "
6269 "the request: %s\n",
6276 size = hur_len(oldhur);
6278 fprintf(stderr, "hsm: cannot allocate "
6279 "%u files + %u bytes data\n",
6280 oldhur->hur_request.hr_itemcount,
6281 oldhur->hur_request.hr_data_len);
6288 memcpy(hur, oldhur, size);
6293 if (line[strlen(line) - 1] == '\n')
6294 line[strlen(line) - 1] = '\0';
6296 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
6297 mntpath, line, &last_dev);
6303 if (some_file == NULL) {
6313 /* If a --data was used, add it to the request */
6314 hur->hur_request.hr_data_len = opaque_len;
6316 memcpy(hur_data(hur), opaque, opaque_len);
6318 /* Send the HSM request */
6319 if (realpath(some_file, fullpath) == NULL) {
6320 fprintf(stderr, "Could not find path '%s': %s\n",
6321 some_file, strerror(errno));
6323 rc = llapi_hsm_request(fullpath, hur);
6325 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
6326 some_file, strerror(-rc));
6336 static int lfs_hsm_archive(int argc, char **argv)
6338 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
6341 static int lfs_hsm_restore(int argc, char **argv)
6343 return lfs_hsm_request(argc, argv, HUA_RESTORE);
6346 static int lfs_hsm_release(int argc, char **argv)
6348 return lfs_hsm_request(argc, argv, HUA_RELEASE);
6351 static int lfs_hsm_remove(int argc, char **argv)
6353 return lfs_hsm_request(argc, argv, HUA_REMOVE);
6356 static int lfs_hsm_cancel(int argc, char **argv)
6358 return lfs_hsm_request(argc, argv, HUA_CANCEL);
6361 static int lfs_swap_layouts(int argc, char **argv)
6366 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
6367 SWAP_LAYOUTS_KEEP_MTIME |
6368 SWAP_LAYOUTS_KEEP_ATIME);
6371 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
6373 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
6375 int lfs_get_mode(const char *string)
6377 enum lock_mode_user mode;
6379 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
6380 if (lock_mode_names[mode] == NULL)
6382 if (strcmp(string, lock_mode_names[mode]) == 0)
6389 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
6391 enum lu_ladvise_type advice;
6394 advice < ARRAY_SIZE(ladvise_names); advice++) {
6395 if (ladvise_names[advice] == NULL)
6397 if (strcmp(string, ladvise_names[advice]) == 0)
6401 return LU_LADVISE_INVALID;
6404 static int lfs_ladvise(int argc, char **argv)
6406 struct option long_opts[] = {
6407 { .val = 'a', .name = "advice", .has_arg = required_argument },
6408 { .val = 'b', .name = "background", .has_arg = no_argument },
6409 { .val = 'e', .name = "end", .has_arg = required_argument },
6410 { .val = 'l', .name = "length", .has_arg = required_argument },
6411 { .val = 'm', .name = "mode", .has_arg = required_argument },
6412 { .val = 's', .name = "start", .has_arg = required_argument },
6413 { .val = 'u', .name = "unset", .has_arg = no_argument },
6415 char short_opts[] = "a:be:l:m:s:u";
6420 struct llapi_lu_ladvise advice;
6421 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
6422 unsigned long long start = 0;
6423 unsigned long long end = LUSTRE_EOF;
6424 unsigned long long length = 0;
6425 unsigned long long size_units;
6426 unsigned long long flags = 0;
6430 while ((c = getopt_long(argc, argv, short_opts,
6431 long_opts, NULL)) != -1) {
6434 advice_type = lfs_get_ladvice(optarg);
6435 if (advice_type == LU_LADVISE_INVALID) {
6436 fprintf(stderr, "%s: invalid advice type "
6437 "'%s'\n", argv[0], optarg);
6438 fprintf(stderr, "Valid types:");
6440 for (advice_type = 0;
6441 advice_type < ARRAY_SIZE(ladvise_names);
6443 if (ladvise_names[advice_type] == NULL)
6445 fprintf(stderr, " %s",
6446 ladvise_names[advice_type]);
6448 fprintf(stderr, "\n");
6461 rc = llapi_parse_size(optarg, &end,
6464 fprintf(stderr, "%s: bad end offset '%s'\n",
6471 rc = llapi_parse_size(optarg, &start,
6474 fprintf(stderr, "%s: bad start offset "
6475 "'%s'\n", argv[0], optarg);
6481 rc = llapi_parse_size(optarg, &length,
6484 fprintf(stderr, "%s: bad length '%s'\n",
6490 mode = lfs_get_mode(optarg);
6492 fprintf(stderr, "%s: bad mode '%s', valid "
6493 "modes are READ or WRITE\n",
6501 fprintf(stderr, "%s: option '%s' unrecognized\n",
6502 argv[0], argv[optind - 1]);
6507 if (advice_type == LU_LADVISE_INVALID) {
6508 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
6509 fprintf(stderr, "Valid types:");
6510 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
6512 if (ladvise_names[advice_type] == NULL)
6514 fprintf(stderr, " %s", ladvise_names[advice_type]);
6516 fprintf(stderr, "\n");
6520 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
6521 fprintf(stderr, "%s: Lock no expand advice is a per file "
6522 "descriptor advice, so when called from lfs, "
6523 "it does nothing.\n", argv[0]);
6527 if (argc <= optind) {
6528 fprintf(stderr, "%s: please give one or more file names\n",
6533 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
6534 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
6539 if (end == LUSTRE_EOF && length != 0)
6540 end = start + length;
6543 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
6544 argv[0], start, end);
6548 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
6549 fprintf(stderr, "%s: mode is only valid with lockahead\n",
6554 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
6555 fprintf(stderr, "%s: mode is required with lockahead\n",
6560 while (optind < argc) {
6563 path = argv[optind++];
6565 fd = open(path, O_RDONLY);
6567 fprintf(stderr, "%s: cannot open file '%s': %s\n",
6568 argv[0], path, strerror(errno));
6573 advice.lla_start = start;
6574 advice.lla_end = end;
6575 advice.lla_advice = advice_type;
6576 advice.lla_value1 = 0;
6577 advice.lla_value2 = 0;
6578 advice.lla_value3 = 0;
6579 advice.lla_value4 = 0;
6580 if (advice_type == LU_LADVISE_LOCKAHEAD) {
6581 advice.lla_lockahead_mode = mode;
6582 advice.lla_peradvice_flags = flags;
6585 rc2 = llapi_ladvise(fd, flags, 1, &advice);
6588 fprintf(stderr, "%s: cannot give advice '%s' to file "
6589 "'%s': %s\n", argv[0],
6590 ladvise_names[advice_type],
6591 path, strerror(errno));
6597 if (rc == 0 && rc2 < 0)
6603 /** The input string contains a comma delimited list of component ids and
6604 * ranges, for example "1,2-4,7".
6606 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
6608 bool end_of_loop = false;
6616 while (!end_of_loop) {
6620 char *endptr = NULL;
6623 ptr = strchrnul(arg, ',');
6624 end_of_loop = *ptr == '\0';
6627 start_index = strtol(arg, &endptr, 0);
6628 if (endptr == arg) /* no data at all */
6630 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
6632 if (start_index < 0)
6635 end_index = start_index;
6636 if (*endptr == '-') {
6637 end_index = strtol(endptr + 1, &endptr, 0);
6638 if (*endptr != '\0')
6640 if (end_index < start_index)
6644 for (i = start_index; i <= end_index && size > 0; i++) {
6647 /* remove duplicate */
6648 for (j = 0; j < nr; j++) {
6652 if (j == nr) { /* no duplicate */
6658 if (size == 0 && i < end_index)
6665 if (!end_of_loop && ptr != NULL)
6668 return rc < 0 ? rc : nr;
6672 * struct verify_mirror_id - Mirror id to be verified.
6673 * @mirror_id: A specified mirror id.
6674 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
6676 struct verify_mirror_id {
6682 * compare_mirror_ids() - Compare mirror ids.
6683 * @layout: Mirror component list.
6684 * @cbdata: Callback data in verify_mirror_id structure.
6686 * This is a callback function called by llapi_layout_comp_iterate()
6687 * to compare the specified mirror id with the one in the current
6688 * component of @layout. If they are the same, then the specified
6689 * mirror id is valid.
6691 * Return: a negative error code on failure or
6692 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
6693 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
6696 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
6698 struct verify_mirror_id *mirror_id_cbdata =
6699 (struct verify_mirror_id *)cbdata;
6703 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
6707 "%s: llapi_layout_mirror_id_get failed: %s.\n",
6708 progname, strerror(errno));
6712 if (mirror_id_cbdata->mirror_id == mirror_id) {
6713 mirror_id_cbdata->is_valid_id = true;
6714 return LLAPI_LAYOUT_ITER_STOP;
6717 return LLAPI_LAYOUT_ITER_CONT;
6721 * verify_mirror_ids() - Verify specified mirror ids.
6722 * @fname: Mirrored file name.
6723 * @mirror_ids: Specified mirror ids to be verified.
6724 * @ids_nr: Number of specified mirror ids.
6726 * This function verifies that specified @mirror_ids are valid
6727 * in the mirrored file @fname.
6729 * Return: 0 on success or a negative error code on failure.
6732 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
6734 struct llapi_layout *layout = NULL;
6735 struct verify_mirror_id mirror_id_cbdata = { 0 };
6746 if (stat(fname, &stbuf) < 0) {
6747 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
6748 progname, fname, strerror(errno));
6753 if (!S_ISREG(stbuf.st_mode)) {
6754 fprintf(stderr, "%s: '%s' is not a regular file.\n",
6760 fd = open(fname, O_DIRECT | O_RDONLY);
6762 fprintf(stderr, "%s: cannot open '%s': %s.\n",
6763 progname, fname, strerror(errno));
6768 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
6770 fprintf(stderr, "%s: '%s' llapi_lease_get failed: %s.\n",
6771 progname, fname, strerror(errno));
6775 layout = llapi_layout_get_by_fd(fd, 0);
6776 if (layout == NULL) {
6777 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
6778 progname, fname, strerror(errno));
6780 llapi_lease_put(fd);
6784 rc = llapi_layout_flags_get(layout, &flr_state);
6786 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
6787 progname, fname, strerror(errno));
6792 flr_state &= LCM_FL_FLR_MASK;
6793 switch (flr_state) {
6794 case LCM_FL_NOT_FLR:
6796 fprintf(stderr, "%s: '%s' file state error: %s.\n",
6797 progname, fname, lcm_flags_string(flr_state));
6804 for (i = 0; i < ids_nr; i++) {
6805 mirror_id_cbdata.mirror_id = mirror_ids[i];
6806 mirror_id_cbdata.is_valid_id = false;
6808 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
6813 "%s: '%s' failed to verify mirror id: %u.\n",
6814 progname, fname, mirror_ids[i]);
6818 if (!mirror_id_cbdata.is_valid_id) {
6821 "%s: '%s' invalid specified mirror id: %u.\n",
6822 progname, fname, mirror_ids[i]);
6828 llapi_layout_free(layout);
6829 llapi_lease_put(fd);
6837 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
6838 __u16 *mirror_ids, int ids_nr)
6840 const char *progname = "lfs mirror resync";
6841 struct llapi_resync_comp comp_array[1024] = { { 0 } };
6842 struct llapi_layout *layout;
6850 if (stat(fname, &stbuf) < 0) {
6851 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
6852 progname, fname, strerror(errno));
6856 if (!S_ISREG(stbuf.st_mode)) {
6857 fprintf(stderr, "%s: '%s' is not a regular file.\n",
6863 fd = open(fname, O_DIRECT | O_RDWR);
6865 fprintf(stderr, "%s: cannot open '%s': %s.\n",
6866 progname, fname, strerror(errno));
6871 ioc->lil_mode = LL_LEASE_WRLCK;
6872 ioc->lil_flags = LL_LEASE_RESYNC;
6873 rc = llapi_lease_get_ext(fd, ioc);
6875 fprintf(stderr, "%s: '%s' llapi_lease_get_ext resync failed: "
6876 "%s.\n", progname, fname, strerror(errno));
6880 layout = llapi_layout_get_by_fd(fd, 0);
6881 if (layout == NULL) {
6882 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
6883 progname, fname, strerror(errno));
6888 rc = llapi_layout_flags_get(layout, &flr_state);
6890 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
6891 progname, fname, strerror(errno));
6896 flr_state &= LCM_FL_FLR_MASK;
6897 switch (flr_state) {
6898 case LCM_FL_NOT_FLR:
6901 fprintf(stderr, "%s: '%s' file state error: %s.\n",
6902 progname, fname, lcm_flags_string(flr_state));
6908 /* get stale component info */
6909 comp_size = llapi_mirror_find_stale(layout, comp_array,
6910 ARRAY_SIZE(comp_array),
6911 mirror_ids, ids_nr);
6912 if (comp_size < 0) {
6918 while (idx < comp_size) {
6924 rc = llapi_lease_check(fd);
6925 if (rc != LL_LEASE_WRLCK) {
6926 fprintf(stderr, "%s: '%s' lost lease lock.\n",
6931 mirror_id = comp_array[idx].lrc_mirror_id;
6932 end = comp_array[idx].lrc_end;
6934 /* try to combine adjacent component */
6935 for (i = idx + 1; i < comp_size; i++) {
6936 if (mirror_id != comp_array[i].lrc_mirror_id ||
6937 end != comp_array[i].lrc_start)
6939 end = comp_array[i].lrc_end;
6942 result = llapi_mirror_resync_one(fd, layout, mirror_id,
6943 comp_array[idx].lrc_start,
6946 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
6947 "%ld.\n", progname, fname, result);
6950 } else if (result > 0) {
6953 /* mark synced components */
6954 for (j = idx; j < i; j++)
6955 comp_array[j].lrc_synced = true;
6961 /* prepare ioc for lease put */
6962 ioc->lil_mode = LL_LEASE_UNLCK;
6963 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
6965 for (idx = 0; idx < comp_size; idx++) {
6966 if (comp_array[idx].lrc_synced) {
6967 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
6972 llapi_layout_free(layout);
6974 rc = llapi_lease_get_ext(fd, ioc);
6976 if (rc == 0) /* lost lease lock */
6978 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
6979 progname, fname, strerror(errno));
6983 * llapi_lease_get_ext returns lease mode when it request to unlock
6994 static inline int lfs_mirror_resync(int argc, char **argv)
6996 struct ll_ioc_lease *ioc = NULL;
6997 __u16 mirror_ids[128] = { 0 };
7002 struct option long_opts[] = {
7003 { .val = 'o', .name = "only", .has_arg = required_argument },
7006 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
7009 rc = parse_mirror_ids(mirror_ids,
7010 sizeof(mirror_ids) / sizeof(__u16),
7014 "%s: bad mirror ids '%s'.\n",
7021 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7022 argv[0], argv[optind - 1]);
7028 if (argc == optind) {
7029 fprintf(stderr, "%s: no file name given.\n", argv[0]);
7034 if (ids_nr > 0 && argc > optind + 1) {
7035 fprintf(stderr, "%s: option '--only' cannot be used upon "
7036 "multiple files.\n", argv[0]);
7043 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7048 /* set the lease on the file */
7049 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
7051 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
7052 argv[0], strerror(errno));
7057 for (; optind < argc; optind++) {
7058 rc = lfs_mirror_resync_file(argv[optind], ioc,
7059 mirror_ids, ids_nr);
7061 fprintf(stderr, "%s: resync file '%s' failed: %d\n",
7062 argv[0], argv[optind], rc);
7063 /* ignore previous file's error, continue with next file */
7066 memset(ioc, 0, sizeof(__u32) * 4096);
7075 * struct verify_chunk - Mirror chunk to be verified.
7076 * @chunk: [start, end) of the chunk.
7077 * @mirror_count: Number of mirror ids in @mirror_id array.
7078 * @mirror_id: Array of valid mirror ids that cover the chunk.
7080 struct verify_chunk {
7081 struct lu_extent chunk;
7082 unsigned int mirror_count;
7083 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
7087 * print_chunks() - Print chunk information.
7088 * @fname: Mirrored file name.
7089 * @chunks: Array of chunks.
7090 * @chunk_count: Number of chunks in @chunks array.
7092 * This function prints [start, end) of each chunk in @chunks
7093 * for mirrored file @fname, and also prints the valid mirror ids
7094 * that cover the chunk.
7099 void print_chunks(const char *fname, struct verify_chunk *chunks,
7105 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
7106 for (i = 0; i < chunk_count; i++) {
7107 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
7109 if (chunks[i].mirror_count == 0)
7110 fprintf(stdout, "\t[");
7112 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
7113 for (j = 1; j < chunks[i].mirror_count; j++)
7114 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
7116 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
7118 fprintf(stdout, "\n");
7122 * print_checksums() - Print CRC-32 checksum values.
7123 * @chunk: A chunk and its corresponding valid mirror ids.
7124 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
7126 * This function prints CRC-32 checksum values on @chunk for
7127 * each valid mirror that covers it.
7132 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
7137 "CRC-32 checksum value for chunk "DEXT":\n",
7138 PEXT(&chunk->chunk));
7139 for (i = 0; i < chunk->mirror_count; i++)
7140 fprintf(stdout, "Mirror %u:\t%#lx\n",
7141 chunk->mirror_id[i], crc[i]);
7142 fprintf(stdout, "\n");
7146 * filter_mirror_id() - Filter specified mirror ids.
7147 * @chunks: Array of chunks.
7148 * @chunk_count: Number of chunks in @chunks array.
7149 * @mirror_ids: Specified mirror ids to be verified.
7150 * @ids_nr: Number of specified mirror ids.
7152 * This function scans valid mirror ids that cover each chunk in @chunks
7153 * and filters specified mirror ids.
7158 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
7159 __u16 *mirror_ids, int ids_nr)
7164 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7165 unsigned int valid_count = 0;
7167 for (i = 0; i < chunk_count; i++) {
7168 if (chunks[i].mirror_count == 0)
7172 for (j = 0; j < ids_nr; j++) {
7173 for (k = 0; k < chunks[i].mirror_count; k++) {
7174 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
7175 valid_id[valid_count] = mirror_ids[j];
7182 memcpy(chunks[i].mirror_id, valid_id,
7183 sizeof(__u16) * valid_count);
7184 chunks[i].mirror_count = valid_count;
7189 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
7190 * @layout: Mirror component list.
7191 * @chunks: Array of chunks.
7192 * @chunks_size: Array size of @chunks.
7194 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
7195 * to find out chunk segments and store them in @chunks array.
7197 * The @mirror_id array in each element of @chunks will store the valid
7198 * mirror ids that cover the chunk. If a mirror component covering the
7199 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
7200 * will not be stored into the @mirror_id array, and the chunk for that
7201 * mirror will not be verified.
7203 * The @mirror_count in each element of @chunks will store the number of
7204 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
7205 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
7206 * indicates the chunk is valid in only one mirror. In both cases, the
7207 * chunk will not be verified.
7209 * Here is an example:
7212 * +------+-------------+--------------------+
7214 * +------+------+------+------+-------------+
7215 * | | S | S | | mirror2
7216 * +-------------+------+------+-------------+
7218 * prepared @chunks array will contain 5 elements:
7219 * (([0, 1M), [1, 2], 2),
7220 * ([1M, 2M), [1, 2], 2),
7221 * ([2M, 3M), [1], 1),
7222 * ([3M, 4M], [], 0),
7223 * ([4M, EOF), [2], 1))
7225 * Return: the actual array size of @chunks on success
7226 * or a negative error code on failure.
7229 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
7230 struct verify_chunk *chunks,
7241 memset(chunks, 0, sizeof(*chunks) * chunks_size);
7244 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
7247 "%s: move to the first layout component: %s.\n",
7248 progname, strerror(errno));
7254 chunks[idx].chunk.e_end = LUSTRE_EOF;
7256 rc = llapi_layout_comp_extent_get(layout, &start, &end);
7259 "%s: llapi_layout_comp_extent_get failed: %s.\n",
7260 progname, strerror(errno));
7264 if (start > chunks[idx].chunk.e_start ||
7265 end <= chunks[idx].chunk.e_start)
7268 if (end < chunks[idx].chunk.e_end)
7269 chunks[idx].chunk.e_end = end;
7271 rc = llapi_layout_comp_flags_get(layout, &flags);
7274 "%s: llapi_layout_comp_flags_get failed: %s.\n",
7275 progname, strerror(errno));
7279 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
7282 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7285 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7286 progname, strerror(errno));
7290 chunks[idx].mirror_id[i] = mirror_id;
7292 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
7294 "%s: mirror_id array is too small.\n",
7301 rc = llapi_layout_comp_use(layout,
7302 LLAPI_LAYOUT_COMP_USE_NEXT);
7305 "%s: move to the next layout component: %s.\n",
7306 progname, strerror(errno));
7309 } /* loop through all components */
7311 chunks[idx].mirror_count = i;
7313 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
7317 if (idx >= chunks_size) {
7318 fprintf(stderr, "%s: chunks array is too small.\n",
7324 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
7328 return rc < 0 ? rc : idx + 1;
7332 * lfs_mirror_verify_chunk() - Verify a chunk.
7333 * @fd: File descriptor of the mirrored file.
7334 * @file_size: Size of the mirrored file.
7335 * @chunk: A chunk and its corresponding valid mirror ids.
7336 * @verbose: Verbose mode.
7338 * This function verifies a @chunk contains exactly the same data
7339 * ammong the mirrors that cover it.
7341 * If @verbose is specified, then the function will print where the
7342 * differences are if the data do not match. Otherwise, it will
7343 * just return an error in that case.
7345 * Return: 0 on success or a negative error code on failure.
7348 int lfs_mirror_verify_chunk(int fd, size_t file_size,
7349 struct verify_chunk *chunk, int verbose)
7351 const size_t buflen = 4 * 1024 * 1024; /* 4M */
7353 size_t page_size = sysconf(_SC_PAGESIZE);
7359 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7366 rc = posix_memalign(&buf, page_size, buflen);
7367 if (rc) /* error code is returned directly */
7371 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7372 PEXT(&chunk->chunk));
7373 for (i = 0; i < chunk->mirror_count; i++)
7374 fprintf(stdout, " %u", chunk->mirror_id[i]);
7375 fprintf(stdout, "\n");
7379 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
7380 pos = chunk->chunk.e_start;
7381 while (bytes_done < count) {
7382 /* compute initial CRC-32 checksum */
7383 crc = crc32(0L, Z_NULL, 0);
7384 memset(crc_array, 0, sizeof(crc_array));
7387 for (i = 0; i < chunk->mirror_count; i++) {
7388 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
7390 if (bytes_read < 0) {
7393 "%s: failed to read data from mirror %u: %s.\n",
7394 progname, chunk->mirror_id[i],
7399 /* compute new CRC-32 checksum */
7400 crc_array[i] = crc32(crc, buf, bytes_read);
7404 print_checksums(chunk, crc_array);
7406 /* compare CRC-32 checksum values */
7407 for (i = 1; i < chunk->mirror_count; i++) {
7408 if (crc_array[i] != crc_array[0]) {
7414 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
7415 progname, PEXT(&chunk->chunk),
7416 chunk->mirror_id[0],
7417 chunk->mirror_id[i]);
7422 bytes_done += bytes_read;
7425 if (verbose > 1 && rc == 0) {
7426 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7427 PEXT(&chunk->chunk));
7428 for (i = 0; i < chunk->mirror_count; i++)
7429 fprintf(stdout, " %u", chunk->mirror_id[i]);
7430 fprintf(stdout, " PASS\n\n");
7439 * lfs_mirror_verify_file() - Verify a mirrored file.
7440 * @fname: Mirrored file name.
7441 * @mirror_ids: Specified mirror ids to be verified.
7442 * @ids_nr: Number of specified mirror ids.
7443 * @verbose: Verbose mode.
7445 * This function verifies that each SYNC mirror of a mirrored file
7446 * specified by @fname contains exactly the same data.
7448 * If @mirror_ids is specified, then the function will verify the
7449 * mirrors specified by @mirror_ids contain exactly the same data.
7451 * If @verbose is specified, then the function will print where the
7452 * differences are if the data do not match. Otherwise, it will
7453 * just return an error in that case.
7455 * Return: 0 on success or a negative error code on failure.
7458 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
7461 struct verify_chunk chunks_array[1024] = { };
7462 struct llapi_layout *layout = NULL;
7466 int chunk_count = 0;
7472 if (stat(fname, &stbuf) < 0) {
7473 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7474 progname, fname, strerror(errno));
7479 if (!S_ISREG(stbuf.st_mode)) {
7480 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7486 if (stbuf.st_size == 0) {
7488 fprintf(stdout, "%s: '%s' file size is 0.\n",
7494 fd = open(fname, O_DIRECT | O_RDONLY);
7496 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7497 progname, fname, strerror(errno));
7502 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
7504 fprintf(stderr, "%s: '%s' llapi_lease_get failed: %s.\n",
7505 progname, fname, strerror(errno));
7509 layout = llapi_layout_get_by_fd(fd, 0);
7510 if (layout == NULL) {
7511 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7512 progname, fname, strerror(errno));
7514 llapi_lease_put(fd);
7518 rc = llapi_layout_flags_get(layout, &flr_state);
7520 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7521 progname, fname, strerror(errno));
7526 flr_state &= LCM_FL_FLR_MASK;
7527 switch (flr_state) {
7528 case LCM_FL_NOT_FLR:
7530 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7531 progname, fname, lcm_flags_string(flr_state));
7537 /* find out mirror chunks to be verified */
7538 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
7539 ARRAY_SIZE(chunks_array));
7540 if (chunk_count < 0) {
7546 /* filter specified mirror ids */
7547 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
7550 print_chunks(fname, chunks_array, chunk_count);
7552 for (idx = 0; idx < chunk_count; idx++) {
7553 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
7556 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
7558 PEXT(&chunks_array[idx].chunk),
7559 (unsigned long long)stbuf.st_size);
7563 if (chunks_array[idx].mirror_count == 0) {
7565 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
7567 PEXT(&chunks_array[idx].chunk));
7569 fprintf(stderr, "skipped\n");
7573 fprintf(stderr, "failed\n");
7577 if (chunks_array[idx].mirror_count == 1) {
7580 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
7582 PEXT(&chunks_array[idx].chunk),
7583 chunks_array[idx].mirror_id[0]);
7587 rc = llapi_lease_check(fd);
7588 if (rc != LL_LEASE_RDLCK) {
7589 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7594 /* verify one chunk */
7595 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
7596 &chunks_array[idx], verbose);
7610 llapi_layout_free(layout);
7611 llapi_lease_put(fd);
7619 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
7620 * @argc: The count of lfs mirror verify command line arguments.
7621 * @argv: Array of strings for lfs mirror verify command line arguments.
7623 * This function parses lfs mirror verify command and verifies the
7624 * specified mirrored file(s).
7626 * Return: 0 on success or a negative error code on failure.
7628 static inline int lfs_mirror_verify(int argc, char **argv)
7630 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7638 struct option long_opts[] = {
7639 { .val = 'o', .name = "only", .has_arg = required_argument },
7640 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7643 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
7645 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
7648 rc = parse_mirror_ids(mirror_ids,
7649 ARRAY_SIZE(mirror_ids),
7653 "%s: bad mirror ids '%s'.\n",
7660 "%s: at least 2 mirror ids needed with '--only' option.\n",
7670 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7671 progname, argv[optind - 1]);
7677 if (argc == optind) {
7678 fprintf(stderr, "%s: no file name given.\n", progname);
7683 if (ids_nr > 0 && argc > optind + 1) {
7685 "%s: '--only' cannot be used upon multiple files.\n",
7693 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7699 for (; optind < argc; optind++) {
7700 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
7710 * lfs_mirror() - Parse and execute lfs mirror commands.
7711 * @argc: The count of lfs mirror command line arguments.
7712 * @argv: Array of strings for lfs mirror command line arguments.
7714 * This function parses lfs mirror commands and performs the
7715 * corresponding functions specified in mirror_cmdlist[].
7717 * Return: 0 on success or an error code on failure.
7719 static int lfs_mirror(int argc, char **argv)
7726 Parser_init("lfs-mirror > ", mirror_cmdlist);
7728 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
7730 program_invocation_short_name = cmd;
7732 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
7734 rc = Parser_commands();
7736 return rc < 0 ? -rc : rc;
7740 * lfs_mirror_list_commands() - List lfs mirror commands.
7741 * @argc: The count of command line arguments.
7742 * @argv: Array of strings for command line arguments.
7744 * This function lists lfs mirror commands defined in mirror_cmdlist[].
7746 * Return: 0 on success.
7748 static int lfs_mirror_list_commands(int argc, char **argv)
7750 char buffer[81] = "";
7752 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
7758 static int lfs_list_commands(int argc, char **argv)
7760 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
7762 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
7767 int main(int argc, char **argv)
7771 /* Ensure that liblustreapi constructor has run */
7772 if (!llapi_liblustreapi_initialized())
7773 fprintf(stderr, "liblustreapi was not properly initialized\n");
7778 Parser_init("lfs > ", cmdlist);
7780 progname = program_invocation_short_name; /* Used in error messages */
7782 llapi_set_command_name(argv[1]);
7783 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
7784 llapi_clear_command_name();
7786 rc = Parser_commands();
7789 return rc < 0 ? -rc : rc;
7792 #ifdef _LUSTRE_IDL_H_
7793 /* Everything we need here should be included by lustreapi.h. */
7794 # error "lfs should not depend on lustre_idl.h"
7795 #endif /* _LUSTRE_IDL_H_ */