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>
74 #include <linux/lnet/nidstr.h>
77 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
78 #endif /* !ARRAY_SIZE */
81 static int lfs_find(int argc, char **argv);
82 static int lfs_getstripe(int argc, char **argv);
83 static int lfs_getdirstripe(int argc, char **argv);
84 static int lfs_setdirstripe(int argc, char **argv);
85 static int lfs_rmentry(int argc, char **argv);
86 static int lfs_osts(int argc, char **argv);
87 static int lfs_mdts(int argc, char **argv);
88 static int lfs_df(int argc, char **argv);
89 static int lfs_getname(int argc, char **argv);
90 static int lfs_check(int argc, char **argv);
91 #ifdef HAVE_SYS_QUOTA_H
92 static int lfs_setquota(int argc, char **argv);
93 static int lfs_quota(int argc, char **argv);
94 static int lfs_project(int argc, char **argv);
96 static int lfs_flushctx(int argc, char **argv);
97 static int lfs_cp(int argc, char **argv);
98 static int lfs_ls(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_data_version(int argc, char **argv);
105 static int lfs_hsm_state(int argc, char **argv);
106 static int lfs_hsm_set(int argc, char **argv);
107 static int lfs_hsm_clear(int argc, char **argv);
108 static int lfs_hsm_action(int argc, char **argv);
109 static int lfs_hsm_archive(int argc, char **argv);
110 static int lfs_hsm_restore(int argc, char **argv);
111 static int lfs_hsm_release(int argc, char **argv);
112 static int lfs_hsm_remove(int argc, char **argv);
113 static int lfs_hsm_cancel(int argc, char **argv);
114 static int lfs_swap_layouts(int argc, char **argv);
115 static int lfs_mv(int argc, char **argv);
116 static int lfs_ladvise(int argc, char **argv);
117 static int lfs_mirror(int argc, char **argv);
118 static int lfs_mirror_list_commands(int argc, char **argv);
119 static int lfs_list_commands(int argc, char **argv);
120 static inline int lfs_mirror_resync(int argc, char **argv);
121 static inline int lfs_mirror_verify(int argc, char **argv);
123 enum setstripe_origin {
129 static int lfs_setstripe0(int argc, char **argv, enum setstripe_origin opc);
131 static inline int lfs_setstripe(int argc, char **argv)
133 return lfs_setstripe0(argc, argv, SO_SETSTRIPE);
135 static inline int lfs_setstripe_migrate(int argc, char **argv)
137 return lfs_setstripe0(argc, argv, SO_MIGRATE);
139 static inline int lfs_mirror_create(int argc, char **argv)
141 return lfs_setstripe0(argc, argv, SO_MIRROR_CREATE);
143 static inline int lfs_mirror_extend(int argc, char **argv)
145 return lfs_setstripe0(argc, argv, SO_MIRROR_EXTEND);
148 /* Setstripe and migrate share mostly the same parameters */
149 #define SSM_CMD_COMMON(cmd) \
150 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
151 " [--stripe-count|-c <stripe_count>]\n" \
152 " [--stripe-index|-i <start_ost_idx>]\n" \
153 " [--stripe-size|-S <stripe_size>]\n" \
154 " [--layout|-L <pattern>]\n" \
155 " [--pool|-p <pool_name>]\n" \
156 " [--ost|-o <ost_indices>]\n"
158 #define SSM_HELP_COMMON \
159 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
160 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
161 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
162 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
163 "\t respectively)\n" \
164 "\tpool_name: Name of OST pool to use (default none)\n" \
165 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
166 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
167 "\t Indices be specified in a format of:\n" \
168 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
170 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
171 "\t If --pool is set with --ost, then the OSTs\n" \
172 "\t must be the members of the pool.\n" \
173 "\tcomp_end: Extent end of component, start after previous end.\n"\
174 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
175 "\t respectively, -1 for EOF). Must be a multiple of\n"\
178 #define MIRROR_CREATE_HELP \
179 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
180 "\t setstripe layout options\n" \
181 "\t It defaults to 1 if not specified; if specified,\n" \
182 "\t it must follow the option without a space.\n" \
183 "\t The option can also be repeated multiple times to\n" \
184 "\t separate mirrors that have different layouts.\n" \
185 "\tsetstripe options: Mirror layout\n" \
186 "\t It can be a plain layout or a composite layout.\n" \
187 "\t If not specified, the stripe options inherited\n" \
188 "\t from the previous component will be used.\n" \
189 "\tparent: Use default stripe options from parent directory\n" \
190 "\tflags: set flags to the component of the current mirror.\n" \
191 "\t Only \"prefer\" flag is supported so far.\n"
193 #define MIRROR_EXTEND_HELP \
195 "\tvictim_file: The layout of victim_file will be split and used\n" \
196 "\t as a mirror added to the mirrored file.\n" \
197 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
198 "\t from victim file(s) in case the victim file(s)\n" \
199 "\t contains the same data as the original mirrored\n" \
202 #define MIRROR_EXTEND_USAGE \
203 " <--mirror-count|-N[mirror_count]>\n" \
204 " [setstripe options|--parent|-f <victim_file>]\n" \
207 #define SETSTRIPE_USAGE \
208 SSM_CMD_COMMON("setstripe") \
209 MIRROR_EXTEND_USAGE \
210 " <directory|filename>\n" \
214 #define MIGRATE_USAGE \
215 SSM_CMD_COMMON("migrate ") \
217 " [--non-block|-n]\n" \
221 "\tblock: Block file access during data migration (default)\n" \
222 "\tnon-block: Abort migrations if concurrent access is detected\n" \
224 #define SETDIRSTRIPE_USAGE \
225 " [--mdt-count|-c stripe_count>\n" \
226 " [--mdt-index|-i mdt_index]\n" \
227 " [--mdt-hash|-H mdt_hash]\n" \
228 " [--default|-D] [--mode|-m mode] <dir>\n" \
229 "\tstripe_count: stripe count of the striped directory\n" \
230 "\tmdt_index: MDT index of first stripe\n" \
231 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
232 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
233 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
234 "\tdefault_stripe: set default dirstripe of the directory\n" \
235 "\tmode: the mode of the directory\n"
238 * command_t mirror_cmdlist - lfs mirror commands.
240 command_t mirror_cmdlist[] = {
241 { .pc_name = "create", .pc_func = lfs_mirror_create,
242 .pc_help = "Create a mirrored file.\n"
243 "usage: lfs mirror create "
244 "<--mirror-count|-N[mirror_count]> "
245 "[setstripe options|--parent] ... <filename|directory>\n"
246 MIRROR_CREATE_HELP },
247 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
248 .pc_help = "Extend a mirrored file.\n"
249 "usage: lfs mirror extend "
250 "<--mirror-count|-N[mirror_count]> [--no-verify] "
251 "[setstripe options|--parent|-f <victim_file>] ... <filename>\n"
252 MIRROR_EXTEND_HELP },
253 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
254 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
255 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
256 "<mirrored file> [<mirrored file2>...]\n"},
257 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
258 .pc_help = "Verify mirrored file(s).\n"
259 "usage: lfs mirror verify "
260 "[--only <mirror_id,mirror_id2[,...]>] "
261 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
262 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
263 .pc_help = "list commands supported by lfs mirror"},
264 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
265 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
266 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
270 /* all available commands */
271 command_t cmdlist[] = {
272 {"setstripe", lfs_setstripe, 0,
273 "To create a file with specified striping/composite layout, or\n"
274 "create/replace the default layout on an existing directory:\n"
275 SSM_CMD_COMMON("setstripe")
276 " <directory|filename>\n"
278 "To add component(s) to an existing composite file:\n"
279 SSM_CMD_COMMON("setstripe --component-add")
281 "To totally delete the default striping from an existing directory:\n"
282 "usage: setstripe -d <directory>\n"
284 "To delete the last component(s) from an existing composite file\n"
285 "(note that this will also delete any data in those components):\n"
286 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
287 " [--component-flags|-F <comp_flags>]\n"
289 "\tcomp_id: Unique component ID to delete\n"
290 "\tcomp_flags: 'init' indicating all instantiated components\n"
291 "\t '^init' indicating all uninstantiated components\n"
292 "\t-I and -F cannot be specified at the same time\n"},
293 {"getstripe", lfs_getstripe, 0,
294 "To list the striping info for a given file or files in a\n"
295 "directory or recursively for all files in a directory tree.\n"
296 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
297 " [--stripe-count|-c] [--stripe-index|-i]\n"
298 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
299 " [--mdt|-m] [--recursive|-r] [--raw|-R] [--yaml|-y]\n"
300 " [--layout|-L] [--fid|-F] [--generation|-g]\n"
301 " [--component-id[=comp_id]|-I[comp_id]]\n"
302 " [--component-flags[=comp_flags]]\n"
303 " [--component-count]\n"
304 " [--component-start[=[+-]comp_start]]\n"
305 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
306 " <directory|filename> ..."},
307 {"setdirstripe", lfs_setdirstripe, 0,
308 "To create a striped directory on a specified MDT. This can only\n"
309 "be done on MDT0 with the right of administrator.\n"
310 "usage: setdirstripe [OPTION] <directory>\n"
312 {"getdirstripe", lfs_getdirstripe, 0,
313 "To list the striping info for a given directory\n"
314 "or recursively for all directories in a directory tree.\n"
315 "usage: getdirstripe [--obd|-O <uuid>] [--mdt-count|-c]\n"
316 " [--mdt-index|-i] [--mdt-hash|-t]\n"
317 " [--recursive|-r] [--yaml|-y]\n"
318 " [--default|-D] <dir> ..."},
319 {"mkdir", lfs_setdirstripe, 0,
320 "To create a striped directory on a specified MDT. This can only\n"
321 "be done on MDT0 with the right of administrator.\n"
322 "usage: mkdir [OPTION] <directory>\n"
324 {"rm_entry", lfs_rmentry, 0,
325 "To remove the name entry of the remote directory. Note: This\n"
326 "command will only delete the name entry, i.e. the remote directory\n"
327 "will become inaccessable after this command. This can only be done\n"
328 "by the administrator\n"
329 "usage: rm_entry <dir>\n"},
330 {"pool_list", lfs_poollist, 0,
331 "List pools or pool OSTs\n"
332 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
333 {"find", lfs_find, 0,
334 "find files matching given attributes recursively in directory tree.\n"
335 "usage: find <directory|filename> ...\n"
336 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
337 " [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
338 " [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
339 " [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
340 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
341 " [[!] --stripe-count|-c [+-]<stripes>]\n"
342 " [[!] --stripe-index|-i <index,...>]\n"
343 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
344 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
345 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
346 " [[!] --projid <projid>]\n"
347 " [[!] --layout|-L released,raid0,mdt]\n"
348 " [[!] --component-count [+-]<comp_cnt>]\n"
349 " [[!] --component-start [+-]N[kMGTPE]]\n"
350 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
351 " [[!] --component-flags <comp_flags>]\n"
352 " [[!] --mdt-count|-T [+-]<stripes>]\n"
353 " [[!] --mdt-hash|-H <hashtype>\n"
354 "\t !: used before an option indicates 'NOT' requested attribute\n"
355 "\t -: used before a value indicates less than requested value\n"
356 "\t +: used before a value indicates more than requested value\n"
357 "\tmdt-hash: hash type of the striped directory.\n"
358 "\t fnv_1a_64 FNV-1a hash algorithm\n"
359 "\t all_char sum of characters % MDT_COUNT\n"},
360 {"check", lfs_check, 0,
361 "Display the status of MDS or OSTs (as specified in the command)\n"
362 "or all the servers (MDS and OSTs).\n"
363 "usage: check <osts|mds|servers>"},
364 {"osts", lfs_osts, 0, "list OSTs connected to client "
365 "[for specified path only]\n" "usage: osts [path]"},
366 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
367 "[for specified path only]\n" "usage: mdts [path]"},
369 "report filesystem disk space usage or inodes usage"
370 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
371 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
372 {"getname", lfs_getname, 0, "list instances and specified mount points "
373 "[for specified path only]\n"
374 "Usage: getname [-h]|[path ...] "},
375 #ifdef HAVE_SYS_QUOTA_H
376 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
377 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
378 " -b <block-softlimit> -B <block-hardlimit>\n"
379 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
380 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
381 " [--block-softlimit <block-softlimit>]\n"
382 " [--block-hardlimit <block-hardlimit>]\n"
383 " [--inode-softlimit <inode-softlimit>]\n"
384 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
385 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
386 " [--block-grace <block-grace>]\n"
387 " [--inode-grace <inode-grace>] <filesystem>\n"
388 " -b can be used instead of --block-softlimit/--block-grace\n"
389 " -B can be used instead of --block-hardlimit\n"
390 " -i can be used instead of --inode-softlimit/--inode-grace\n"
391 " -I can be used instead of --inode-hardlimit\n\n"
392 "Note: The total quota space will be split into many qunits and\n"
393 " balanced over all server targets, the minimal qunit size is\n"
394 " 1M bytes for block space and 1K inodes for inode space.\n\n"
395 " Quota space rebalancing process will stop when this mininum\n"
396 " value is reached. As a result, quota exceeded can be returned\n"
397 " while many targets still have 1MB or 1K inodes of spare\n"
399 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
400 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
402 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
403 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
404 {"project", lfs_project, 0,
405 "Change or list project attribute for specified file or directory.\n"
406 "usage: project [-d|-r] <file|directory...>\n"
407 " list project ID and flags on file(s) or directories\n"
408 " project [-p id] [-s] [-r] <file|directory...>\n"
409 " set project ID and/or inherit flag for specified file(s) or directories\n"
410 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
411 " check project ID and flags on file(s) or directories, print outliers\n"
412 " project -C [-r] [-k] <file|directory...>\n"
413 " clear the project inherit flag and ID on the file or directory\n"
416 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
417 "usage: flushctx [-k] [mountpoint...]"},
419 "Remote user copy files and directories.\n"
420 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
422 "Remote user list directory contents.\n"
423 "usage: ls [OPTION]... [FILE]..."},
424 {"changelog", lfs_changelog, 0,
425 "Show the metadata changes on an MDT."
426 "\nusage: changelog <mdtname> [startrec [endrec]]"},
427 {"changelog_clear", lfs_changelog_clear, 0,
428 "Indicate that old changelog records up to <endrec> are no longer of "
429 "interest to consumer <id>, allowing the system to free up space.\n"
430 "An <endrec> of 0 means all records.\n"
431 "usage: changelog_clear <mdtname> <id> <endrec>"},
432 {"fid2path", lfs_fid2path, 0,
433 "Resolve the full path(s) for given FID(s). For a specific hardlink "
434 "specify link number <linkno>.\n"
435 /* "For a historical link name, specify changelog record <recno>.\n" */
436 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
437 /* [ --rec <recno> ] */ },
438 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
439 "usage: path2fid [--parents] <path> ..."},
440 {"data_version", lfs_data_version, 0, "Display file data version for "
441 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
442 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
443 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
444 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
445 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
446 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
447 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
449 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
450 "[--archived] [--lost] <file> ..."},
451 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
452 "given files.\n" "usage: hsm_action <file> ..."},
453 {"hsm_archive", lfs_hsm_archive, 0,
454 "Archive file to external storage.\n"
455 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
457 {"hsm_restore", lfs_hsm_restore, 0,
458 "Restore file from external storage.\n"
459 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
460 {"hsm_release", lfs_hsm_release, 0,
461 "Release files from Lustre.\n"
462 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
463 {"hsm_remove", lfs_hsm_remove, 0,
464 "Remove file copy from external storage.\n"
465 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
467 " (FILE [FILE ...] | "
468 "--mntpath MOUNTPATH FID [FID ...])\n"
470 "Note: To remove an archived copy of a file already deleted from a "
472 "--mntpath option and a list of FIDs must be specified"
474 {"hsm_cancel", lfs_hsm_cancel, 0,
475 "Cancel requests related to specified files.\n"
476 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
477 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
478 "usage: swap_layouts <path1> <path2>"},
479 {"migrate", lfs_setstripe_migrate, 0,
480 "migrate a directory between MDTs.\n"
481 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
483 "\tmdt_idx: index of the destination MDT\n"
485 "migrate file objects from one OST "
486 "layout\nto another (may be not safe with concurent writes).\n"
488 "[--stripe-count|-c] <stripe_count>\n"
489 " [--stripe-index|-i] <start_ost_index>\n"
490 " [--stripe-size|-S] <stripe_size>\n"
491 " [--pool|-p] <pool_name>\n"
492 " [--ost-list|-o] <ost_indices>\n"
494 " [--non-block|-n]\n"
495 " <file|directory>\n"
496 "\tstripe_count: number of OSTs to stripe a file over\n"
497 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
498 "\tstripe_size: number of bytes to store before moving to the next OST\n"
499 "\tpool_name: name of the predefined pool of OSTs\n"
500 "\tost_indices: OSTs to stripe over, in order\n"
501 "\tblock: wait for the operation to return before continuing\n"
502 "\tnon-block: do not wait for the operation to return.\n"},
504 "To move directories between MDTs. This command is deprecated, "
505 "use \"migrate\" instead.\n"
506 "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
508 {"ladvise", lfs_ladvise, 0,
509 "Provide servers with advice about access patterns for a file.\n"
510 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
511 " [--background|-b] [--unset|-u]\n\n"
512 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
513 " {[--mode|-m [READ,WRITE]}\n"
515 {"mirror", lfs_mirror, mirror_cmdlist,
516 "lfs commands used to manage files with mirrored components:\n"
517 "lfs mirror create - create a mirrored file or directory\n"
518 "lfs mirror extend - add mirror(s) to an existing file\n"
519 "lfs mirror split - split a mirror from an existing mirrored file\n"
520 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
521 "lfs mirror verify - verify mirrored file(s)\n"},
522 {"help", Parser_help, 0, "help"},
523 {"exit", Parser_quit, 0, "quit"},
524 {"quit", Parser_quit, 0, "quit"},
525 {"--version", Parser_version, 0,
526 "output build version of the utility and exit"},
527 {"--list-commands", lfs_list_commands, 0,
528 "list commands supported by the utility and exit"},
533 static int check_hashtype(const char *hashtype)
537 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
538 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
545 static const char *error_loc = "syserror";
548 MIGRATION_NONBLOCK = 1 << 0,
549 MIGRATION_MIRROR = 1 << 1,
552 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
553 struct llapi_layout *layout);
556 migrate_open_files(const char *name, const struct llapi_stripe_param *param,
557 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
563 char parent[PATH_MAX];
564 char volatile_file[PATH_MAX];
570 if (param == NULL && layout == NULL) {
571 error_loc = "layout information";
575 /* search for file directory pathname */
576 if (strlen(name) > sizeof(parent) - 1) {
577 error_loc = "source file name";
581 strncpy(parent, name, sizeof(parent));
582 ptr = strrchr(parent, '/');
584 if (getcwd(parent, sizeof(parent)) == NULL) {
585 error_loc = "getcwd";
589 if (ptr == parent) /* leading '/' */
594 /* open file, direct io */
595 /* even if the file is only read, WR mode is nedeed to allow
596 * layout swap on fd */
597 fd = open(name, O_RDWR | O_DIRECT);
600 error_loc = "cannot open source file";
604 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
606 error_loc = "cannot get MDT index";
611 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
612 mode_t open_mode = S_IRUSR | S_IWUSR;
614 random_value = random();
615 rc = snprintf(volatile_file, sizeof(volatile_file),
616 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
617 mdt_index, random_value);
618 if (rc >= sizeof(volatile_file)) {
623 /* create, open a volatile file, use caching (ie no directio) */
625 fdv = llapi_file_open_param(volatile_file, open_flags,
628 fdv = lfs_component_create(volatile_file, open_flags,
630 } while (fdv < 0 && (rc = fdv) == -EEXIST);
633 error_loc = "cannot create volatile file";
637 /* In case the MDT does not support creation of volatile files
638 * we should try to unlink it. */
639 (void)unlink(volatile_file);
641 /* Not-owner (root?) special case.
642 * Need to set owner/group of volatile file like original.
643 * This will allow to pass related check during layout_swap.
648 error_loc = "cannot stat source file";
652 rc = fstat(fdv, &stv);
655 error_loc = "cannot stat volatile";
659 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
660 rc = fchown(fdv, st.st_uid, st.st_gid);
663 error_loc = "cannot change ownwership of volatile";
682 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
684 struct llapi_layout *layout;
685 size_t buf_size = 4 * 1024 * 1024;
694 layout = llapi_layout_get_by_fd(fd_src, 0);
695 if (layout != NULL) {
696 uint64_t stripe_size;
698 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
700 buf_size = stripe_size;
702 llapi_layout_free(layout);
705 /* Use a page-aligned buffer for direct I/O */
706 rc = posix_memalign(&buf, getpagesize(), buf_size);
711 /* read new data only if we have written all
712 * previously read data */
715 rc = check_file(fd_src);
720 rsize = read(fd_src, buf, buf_size);
732 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
751 static int migrate_copy_timestamps(int fd, int fdv)
755 if (fstat(fd, &st) == 0) {
756 struct timeval tv[2] = {
757 {.tv_sec = st.st_atime},
758 {.tv_sec = st.st_mtime}
761 return futimes(fdv, tv);
767 static int migrate_block(int fd, int fdv)
774 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
776 error_loc = "cannot get dataversion";
784 /* The grouplock blocks all concurrent accesses to the file.
785 * It has to be taken after llapi_get_data_version as it would
787 rc = llapi_group_lock(fd, gid);
789 error_loc = "cannot get group lock";
793 rc = migrate_copy_data(fd, fdv, NULL);
795 error_loc = "data copy failed";
799 /* Make sure we keep original atime/mtime values */
800 rc = migrate_copy_timestamps(fd, fdv);
802 error_loc = "timestamp copy failed";
807 * for a migration we need to check data version on file did
810 * Pass in gid=0 since we already own grouplock. */
811 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
812 SWAP_LAYOUTS_CHECK_DV1);
814 error_loc = "file changed";
817 error_loc = "cannot swap layout";
822 rc2 = llapi_group_unlock(fd, gid);
823 if (rc2 < 0 && rc == 0) {
824 error_loc = "unlock group lock";
832 * Internal helper for migrate_copy_data(). Check lease and report error if
835 * \param[in] fd File descriptor on which to check the lease.
837 * \retval 0 Migration can keep on going.
838 * \retval -errno Error occurred, abort migration.
840 static int check_lease(int fd)
844 rc = llapi_lease_check(fd);
846 return 0; /* llapi_check_lease returns > 0 on success. */
851 static int migrate_nonblock(int fd, int fdv)
857 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
859 error_loc = "cannot get data version";
863 rc = migrate_copy_data(fd, fdv, check_lease);
865 error_loc = "data copy failed";
869 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
871 error_loc = "cannot get data version";
877 error_loc = "source file changed";
881 /* Make sure we keep original atime/mtime values */
882 rc = migrate_copy_timestamps(fd, fdv);
884 error_loc = "timestamp copy failed";
891 static int lfs_component_set(char *fname, int comp_id,
892 __u32 flags, __u32 neg_flags)
895 __u32 flags_array[2];
900 ids[count] = comp_id;
901 flags_array[count] = flags;
906 ids[count] = comp_id;
907 flags_array[count] = neg_flags | LCME_FL_NEG;
911 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
914 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
915 progname, comp_id, fname, flags, neg_flags);
920 static int lfs_component_del(char *fname, __u32 comp_id,
921 __u32 flags, __u32 neg_flags)
925 if (flags && neg_flags)
928 if (!flags && neg_flags)
929 flags = neg_flags | LCME_FL_NEG;
931 if ((flags && comp_id) || (!flags && !comp_id))
934 /* LCME_FL_INIT is the only supported flag in PFL */
936 if (flags & ~LCME_KNOWN_FLAGS) {
938 "%s setstripe: unknown flags %#x\n",
942 } else if (comp_id > LCME_ID_MAX) {
943 fprintf(stderr, "%s setstripe: invalid component id %u\n",
948 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
951 "%s setstripe: cannot delete component %#x from '%s': %s\n",
952 progname, comp_id, fname, strerror(errno));
956 static int lfs_component_add(char *fname, struct llapi_layout *layout)
963 rc = llapi_layout_file_comp_add(fname, layout);
965 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
966 fname, strerror(errno));
970 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
971 struct llapi_layout *layout)
979 fd = lstat(fname, &st);
980 if (fd == 0 && S_ISDIR(st.st_mode))
981 open_flags = O_DIRECTORY | O_RDONLY;
983 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
985 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
986 S_ISDIR(st.st_mode) ?
987 "set default composite layout for" :
988 "create composite file",
989 fname, strerror(errno));
993 static int lfs_migrate(char *name, __u64 migration_flags,
994 struct llapi_stripe_param *param,
995 struct llapi_layout *layout)
1001 rc = migrate_open_files(name, param, layout, &fd, &fdv);
1005 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1006 /* Blocking mode (forced if servers do not support file lease).
1007 * It is also the default mode, since we cannot distinguish
1008 * between a broken lease and a server that does not support
1009 * atomic swap/close (LU-6785) */
1010 rc = migrate_block(fd, fdv);
1014 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1016 error_loc = "cannot get lease";
1020 rc = migrate_nonblock(fd, fdv);
1022 llapi_lease_release(fd);
1026 /* Atomically put lease, swap layouts and close.
1027 * for a migration we need to check data version on file did
1029 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1031 error_loc = "cannot swap layout";
1043 fprintf(stderr, "error: %s: %s: %s: %s\n",
1044 progname, name, error_loc, strerror(-rc));
1048 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1057 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1061 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1062 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1063 const char *comp_name = comp_flags_table[i].cfn_name;
1065 if (strcmp(name, comp_name) == 0) {
1066 *flags |= comp_flag;
1068 } else if (strncmp(name, "^", 1) == 0 &&
1069 strcmp(name + 1, comp_name) == 0) {
1070 *neg_flags |= comp_flag;
1075 llapi_printf(LLAPI_MSG_ERROR,
1076 "%s: component flag '%s' not supported\n",
1082 if (!*flags && !*neg_flags)
1085 /* don't allow to set and exclude the same flag */
1086 if (*flags & *neg_flags)
1093 * struct mirror_args - Command-line arguments for mirror(s).
1094 * @m_count: Number of mirrors to be created with this layout.
1095 * @m_flags: Mirror level flags, only 'prefer' is supported.
1096 * @m_layout: Mirror layout.
1097 * @m_file: A victim file. Its layout will be split and used as a mirror.
1098 * @m_next: Point to the next node of the list.
1100 * Command-line arguments for mirror(s) will be parsed and stored in
1101 * a linked list that consists of this structure.
1103 struct mirror_args {
1106 struct llapi_layout *m_layout;
1108 struct mirror_args *m_next;
1111 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1116 rc = llapi_layout_comp_flags_get(layout, &flags);
1120 if (flags & LCME_FL_NEG) {
1121 fprintf(stderr, "error: %s: negative flags are not supported\n",
1126 if (flags & LCME_FL_STALE) {
1127 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1128 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1132 return LLAPI_LAYOUT_ITER_CONT;
1135 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1137 uint64_t start, end;
1141 /* LU-10112: do not support dom+flr in phase 1 */
1142 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1146 rc = llapi_layout_pattern_get(layout, &pattern);
1150 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1151 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1156 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1160 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1164 if (end != LUSTRE_EOF) {
1165 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1170 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1175 * enum mirror_flags - Flags for extending a mirrored file.
1176 * @NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1177 * in case the victim file(s) contains the same data as the
1178 * original mirrored file.
1180 * Flags for extending a mirrored file.
1187 * mirror_create_sanity_check() - Check mirror list.
1188 * @list: A linked list that stores the mirror arguments.
1190 * This function does a sanity check on @list for creating
1193 * Return: 0 on success or a negative error code on failure.
1195 static int mirror_create_sanity_check(const char *fname,
1196 struct mirror_args *list)
1199 bool has_m_file = false;
1200 bool has_m_layout = false;
1206 struct llapi_layout *layout;
1208 layout = llapi_layout_get_by_path(fname, 0);
1211 "error: %s: file '%s' couldn't get layout\n",
1216 rc = mirror_sanity_check_one(layout);
1217 llapi_layout_free(layout);
1223 while (list != NULL) {
1224 if (list->m_file != NULL) {
1226 llapi_layout_free(list->m_layout);
1229 llapi_layout_get_by_path(list->m_file, 0);
1230 if (list->m_layout == NULL) {
1232 "error: %s: file '%s' has no layout\n",
1233 progname, list->m_file);
1237 has_m_layout = true;
1238 if (list->m_layout == NULL) {
1239 fprintf(stderr, "error: %s: no mirror layout\n",
1245 rc = mirror_sanity_check_one(list->m_layout);
1249 list = list->m_next;
1252 if (has_m_file && has_m_layout) {
1253 fprintf(stderr, "error: %s: -f <victim_file> option should not "
1254 "be specified with setstripe options or "
1255 "--parent option\n", progname);
1262 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1264 __u32 mirror_flags = *(__u32 *)cbdata;
1268 rc = llapi_layout_comp_flags_get(layout, &flags);
1273 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1278 return LLAPI_LAYOUT_ITER_CONT;
1282 * mirror_create() - Create a mirrored file.
1283 * @fname: The file to be created.
1284 * @mirror_list: A linked list that stores the mirror arguments.
1286 * This function creates a mirrored file @fname with the mirror(s)
1287 * from @mirror_list.
1289 * Return: 0 on success or a negative error code on failure.
1291 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1293 struct llapi_layout *layout = NULL;
1294 struct mirror_args *cur_mirror = NULL;
1295 uint16_t mirror_count = 0;
1299 rc = mirror_create_sanity_check(NULL, mirror_list);
1303 cur_mirror = mirror_list;
1304 while (cur_mirror != NULL) {
1305 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1307 &cur_mirror->m_flags);
1310 fprintf(stderr, "%s: failed to set mirror flags\n",
1315 for (i = 0; i < cur_mirror->m_count; i++) {
1316 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1319 fprintf(stderr, "error: %s: "
1320 "merge layout failed: %s\n",
1321 progname, strerror(errno));
1325 mirror_count += cur_mirror->m_count;
1326 cur_mirror = cur_mirror->m_next;
1329 if (layout == NULL) {
1330 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1334 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1337 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1338 progname, strerror(errno));
1342 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0644,
1350 llapi_layout_free(layout);
1355 * Compare files and check lease on @fd.
1357 * \retval bytes number of bytes are the same
1359 static ssize_t mirror_file_compare(int fd, int fdv)
1361 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1363 ssize_t bytes_done = 0;
1364 ssize_t bytes_read = 0;
1366 buf = malloc(buflen * 2);
1371 if (!llapi_lease_check(fd)) {
1372 bytes_done = -EBUSY;
1376 bytes_read = read(fd, buf, buflen);
1377 if (bytes_read <= 0)
1380 if (bytes_read != read(fdv, buf + buflen, buflen))
1383 /* XXX: should compute the checksum on each buffer and then
1384 * compare checksum to avoid cache collision */
1385 if (memcmp(buf, buf + buflen, bytes_read))
1388 bytes_done += bytes_read;
1396 static int mirror_extend_file(const char *fname, const char *victim_file,
1397 enum mirror_flags mirror_flags)
1402 struct stat stbuf_v;
1403 struct ll_ioc_lease *data = NULL;
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_acquire(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, merge layouts and close. */
1478 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1480 error_loc = "memory allocation";
1483 data->lil_mode = LL_LEASE_UNLCK;
1484 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1485 data->lil_count = 1;
1486 data->lil_ids[0] = fdv;
1487 rc = llapi_lease_set(fd, data);
1489 error_loc = "cannot merge layout";
1491 } else if (rc == 0) {
1493 error_loc = "lost lease lock";
1506 (void) unlink(victim_file);
1508 fprintf(stderr, "error: %s: %s: %s: %s\n",
1509 progname, fname, error_loc, strerror(-rc));
1513 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1515 struct ll_ioc_lease *data = NULL;
1520 rc = migrate_open_files(name, NULL, layout, &fd, &fdv);
1524 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1526 error_loc = "cannot get lease";
1530 rc = migrate_nonblock(fd, fdv);
1532 llapi_lease_release(fd);
1536 /* Atomically put lease, merge layouts and close. */
1537 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1539 error_loc = "memory allocation";
1542 data->lil_mode = LL_LEASE_UNLCK;
1543 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1544 data->lil_count = 1;
1545 data->lil_ids[0] = fdv;
1546 rc = llapi_lease_set(fd, data);
1548 error_loc = "cannot merge layout";
1550 } else if (rc == 0) {
1552 error_loc = "lost lease lock";
1565 fprintf(stderr, "error: %s: %s: %s: %s\n",
1566 progname, name, error_loc, strerror(-rc));
1570 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1571 enum mirror_flags mirror_flags)
1575 rc = mirror_create_sanity_check(fname, mirror_list);
1579 while (mirror_list) {
1580 if (mirror_list->m_file != NULL) {
1581 rc = mirror_extend_file(fname, mirror_list->m_file,
1584 __u32 mirror_count = mirror_list->m_count;
1586 while (mirror_count > 0) {
1587 rc = mirror_extend_layout(fname,
1588 mirror_list->m_layout);
1598 mirror_list = mirror_list->m_next;
1605 * Parse a string containing an target index list into an array of integers.
1607 * The input string contains a comma delimited list of individual
1608 * indices and ranges, for example "1,2-4,7". Add the indices into the
1609 * \a tgts array and remove duplicates.
1611 * \param[out] tgts array to store indices in
1612 * \param[in] size size of \a tgts array
1613 * \param[in] offset starting index in \a tgts
1614 * \param[in] arg string containing OST index list
1616 * \retval positive number of indices in \a tgts
1617 * \retval -EINVAL unable to parse \a arg
1619 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1623 int slots = size - offset;
1630 end_of_loop = false;
1631 while (!end_of_loop) {
1635 char *endptr = NULL;
1639 ptr = strchrnul(arg, ',');
1641 end_of_loop = *ptr == '\0';
1644 start_index = strtol(arg, &endptr, 0);
1645 if (endptr == arg) /* no data at all */
1647 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1650 end_index = start_index;
1651 if (*endptr == '-') {
1652 end_index = strtol(endptr + 1, &endptr, 0);
1653 if (*endptr != '\0')
1655 if (end_index < start_index)
1659 for (i = start_index; i <= end_index && slots > 0; i++) {
1662 /* remove duplicate */
1663 for (j = 0; j < offset; j++) {
1667 if (j == offset) { /* no duplicate */
1672 if (slots == 0 && i < end_index)
1680 if (!end_of_loop && ptr != NULL)
1683 return rc < 0 ? rc : nr;
1686 struct lfs_setstripe_args {
1687 unsigned long long lsa_comp_end;
1688 unsigned long long lsa_stripe_size;
1689 long long lsa_stripe_count;
1690 long long lsa_stripe_off;
1691 __u32 lsa_comp_flags;
1692 __u32 lsa_comp_neg_flags;
1693 unsigned long long lsa_pattern;
1696 char *lsa_pool_name;
1699 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1701 memset(lsa, 0, sizeof(*lsa));
1703 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1704 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1705 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1706 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1707 lsa->lsa_pool_name = NULL;
1711 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1712 * @lsa: Stripe options to be initialized and inherited.
1714 * This function initializes stripe options in @lsa and inherit
1715 * stripe_size, stripe_count and OST pool_name options.
1719 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1721 unsigned long long stripe_size;
1722 long long stripe_count;
1723 char *pool_name = NULL;
1725 stripe_size = lsa->lsa_stripe_size;
1726 stripe_count = lsa->lsa_stripe_count;
1727 pool_name = lsa->lsa_pool_name;
1729 setstripe_args_init(lsa);
1731 lsa->lsa_stripe_size = stripe_size;
1732 lsa->lsa_stripe_count = stripe_count;
1733 lsa->lsa_pool_name = pool_name;
1736 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1738 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1739 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1740 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1741 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1742 lsa->lsa_pool_name != NULL ||
1743 lsa->lsa_comp_end != 0);
1747 * comp_args_to_layout() - Create or extend a composite layout.
1748 * @composite: Pointer to the composite layout.
1749 * @lsa: Stripe options for the new component.
1751 * This function creates or extends a composite layout by adding a new
1752 * component with stripe options from @lsa.
1754 * Return: 0 on success or an error code on failure.
1756 static int comp_args_to_layout(struct llapi_layout **composite,
1757 struct lfs_setstripe_args *lsa)
1759 struct llapi_layout *layout = *composite;
1760 uint64_t prev_end = 0;
1763 if (layout == NULL) {
1764 layout = llapi_layout_alloc();
1765 if (layout == NULL) {
1766 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1770 *composite = layout;
1774 /* Get current component extent, current component
1775 * must be the tail component. */
1776 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
1778 fprintf(stderr, "Get comp extent failed. %s\n",
1783 rc = llapi_layout_comp_add(layout);
1785 fprintf(stderr, "Add component failed. %s\n",
1791 rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
1793 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
1794 prev_end, lsa->lsa_comp_end, strerror(errno));
1798 /* Data-on-MDT component setting */
1799 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
1800 /* In case of Data-on-MDT patterns the only extra option
1801 * applicable is stripe size option. */
1802 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
1803 fprintf(stderr, "Option 'stripe-count' can't be "
1804 "specified with Data-on-MDT component: %lld\n",
1805 lsa->lsa_stripe_count);
1808 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
1809 fprintf(stderr, "Option 'stripe-size' can't be "
1810 "specified with Data-on-MDT component: %llu\n",
1811 lsa->lsa_stripe_size);
1814 if (lsa->lsa_nr_tgts != 0) {
1815 fprintf(stderr, "Option 'ost-list' can't be specified "
1816 "with Data-on-MDT component: '%i'\n",
1820 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
1821 fprintf(stderr, "Option 'stripe-offset' can't be "
1822 "specified with Data-on-MDT component: %lld\n",
1823 lsa->lsa_stripe_off);
1826 if (lsa->lsa_pool_name != 0) {
1827 fprintf(stderr, "Option 'pool' can't be specified "
1828 "with Data-on-MDT component: '%s'\n",
1829 lsa->lsa_pool_name);
1833 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
1835 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
1836 lsa->lsa_pattern, strerror(errno));
1839 /* Data-on-MDT component has always single stripe up to end */
1840 lsa->lsa_stripe_size = lsa->lsa_comp_end;
1843 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
1845 fprintf(stderr, "Set stripe size %llu failed: %s\n",
1846 lsa->lsa_stripe_size, strerror(errno));
1850 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
1852 fprintf(stderr, "Set stripe count %lld failed: %s\n",
1853 lsa->lsa_stripe_count, strerror(errno));
1857 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
1859 fprintf(stderr, "Set flags 0x%x failed: %s\n",
1860 lsa->lsa_comp_flags, strerror(errno));
1864 if (lsa->lsa_pool_name != NULL) {
1865 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
1867 fprintf(stderr, "Set pool name: %s failed. %s\n",
1868 lsa->lsa_pool_name, strerror(errno));
1872 rc = llapi_layout_pool_name_set(layout, "");
1874 fprintf(stderr, "Clear pool name failed: %s\n",
1880 if (lsa->lsa_nr_tgts > 0) {
1881 if (lsa->lsa_stripe_count > 0 &&
1882 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
1883 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
1884 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
1885 fprintf(stderr, "stripe_count(%lld) != nr_osts(%d)\n",
1886 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
1889 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
1890 rc = llapi_layout_ost_index_set(layout, i,
1895 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
1896 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
1899 fprintf(stderr, "Set ost index %d failed. %s\n",
1900 i, strerror(errno));
1907 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
1908 * end of the last component in the existing file, and adjust the
1909 * first extent start of the components to be added accordingly. */
1910 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
1912 struct llapi_layout *head;
1913 uint64_t start, end, stripe_size, prev_end = 0;
1916 if (layout == NULL) {
1918 "%s setstripe: layout must be specified\n",
1924 head = llapi_layout_get_by_path(fname, 0);
1927 "%s setstripe: cannot read layout from '%s': %s\n",
1928 progname, fname, strerror(errno));
1930 } else if (errno == ENODATA) {
1931 /* file without LOVEA, this component-add will be turned
1932 * into a component-create. */
1933 llapi_layout_free(head);
1935 } else if (!llapi_layout_is_composite(head)) {
1936 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
1938 llapi_layout_free(head);
1942 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
1944 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
1945 progname, strerror(errno));
1946 llapi_layout_free(head);
1950 llapi_layout_free(head);
1952 /* Make sure we use the first component of the layout to be added. */
1953 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1956 "%s setstripe: cannot move component cursor: %s\n",
1957 progname, strerror(errno));
1961 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1963 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
1964 progname, strerror(errno));
1968 if (start > prev_end || end <= prev_end) {
1970 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
1971 progname, start, end, prev_end);
1975 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
1977 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
1978 progname, strerror(errno));
1982 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
1983 (prev_end & (stripe_size - 1))) {
1985 "%s setstripe: stripe size %lu not aligned with %lu\n",
1986 progname, stripe_size, prev_end);
1990 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
1993 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
1994 progname, prev_end, end, strerror(errno));
2001 static inline bool arg_is_eof(char *arg)
2003 return !strncmp(arg, "-1", strlen("-1")) ||
2004 !strncmp(arg, "EOF", strlen("EOF")) ||
2005 !strncmp(arg, "eof", strlen("eof"));
2009 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2011 * Return: Valid mirror_args pointer on success and
2012 * NULL if memory allocation fails.
2014 static struct mirror_args *lfs_mirror_alloc(void)
2016 struct mirror_args *mirror = NULL;
2019 mirror = calloc(1, sizeof(*mirror));
2030 * lfs_mirror_free() - Free memory allocated for a mirror argument
2032 * @mirror: Previously allocated mirror argument structure by
2033 * lfs_mirror_alloc().
2035 * Free memory allocated for @mirror.
2039 static void lfs_mirror_free(struct mirror_args *mirror)
2041 if (mirror->m_layout != NULL)
2042 llapi_layout_free(mirror->m_layout);
2047 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2048 * @mirror_list: Previously allocated mirror list.
2050 * Free memory allocated for @mirror_list.
2054 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2056 struct mirror_args *next_mirror = NULL;
2058 while (mirror_list != NULL) {
2059 next_mirror = mirror_list->m_next;
2060 lfs_mirror_free(mirror_list);
2061 mirror_list = next_mirror;
2073 LFS_COMP_USE_PARENT_OPT,
2074 LFS_COMP_NO_VERIFY_OPT,
2076 LFS_MIRROR_FLAGS_OPT,
2080 static int lfs_setstripe0(int argc, char **argv, enum setstripe_origin opc)
2082 struct lfs_setstripe_args lsa;
2083 struct llapi_stripe_param *param = NULL;
2084 struct find_param migrate_mdt_param = {
2094 char *mdt_idx_arg = NULL;
2095 unsigned long long size_units = 1;
2096 bool migrate_mode = false;
2097 bool migration_block = false;
2098 __u64 migration_flags = 0;
2099 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2100 int comp_del = 0, comp_set = 0;
2103 struct llapi_layout *layout = NULL;
2104 struct llapi_layout **lpp = &layout;
2105 bool mirror_mode = false;
2106 bool has_m_file = false;
2107 __u32 mirror_count = 0;
2108 enum mirror_flags mirror_flags = 0;
2109 struct mirror_args *mirror_list = NULL;
2110 struct mirror_args *new_mirror = NULL;
2111 struct mirror_args *last_mirror = NULL;
2114 struct option long_opts[] = {
2115 /* --block is only valid in migrate mode */
2116 { .val = 'b', .name = "block", .has_arg = no_argument},
2117 { .val = LFS_COMP_ADD_OPT,
2118 .name = "comp-add", .has_arg = no_argument},
2119 { .val = LFS_COMP_ADD_OPT,
2120 .name = "component-add",
2121 .has_arg = no_argument},
2122 { .val = LFS_COMP_DEL_OPT,
2123 .name = "comp-del", .has_arg = no_argument},
2124 { .val = LFS_COMP_DEL_OPT,
2125 .name = "component-del",
2126 .has_arg = no_argument},
2127 { .val = LFS_COMP_FLAGS_OPT,
2128 .name = "comp-flags", .has_arg = required_argument},
2129 { .val = LFS_COMP_FLAGS_OPT,
2130 .name = "component-flags",
2131 .has_arg = required_argument},
2132 { .val = LFS_COMP_SET_OPT,
2133 .name = "comp-set", .has_arg = no_argument},
2134 { .val = LFS_COMP_SET_OPT,
2135 .name = "component-set",
2136 .has_arg = no_argument},
2137 { .val = LFS_COMP_USE_PARENT_OPT,
2138 .name = "parent", .has_arg = no_argument},
2139 { .val = LFS_COMP_NO_VERIFY_OPT,
2140 .name = "no-verify", .has_arg = no_argument},
2141 { .val = LFS_MIRROR_FLAGS_OPT,
2142 .name = "flags", .has_arg = required_argument},
2143 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2144 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2145 { .val = 'd', .name = "delete", .has_arg = no_argument},
2146 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2147 { .val = 'E', .name = "component-end",
2148 .has_arg = required_argument},
2149 { .val = 'f', .name = "file", .has_arg = required_argument },
2150 /* dirstripe {"mdt-hash", required_argument, 0, 'H'}, */
2151 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2152 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2153 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2154 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2155 { .val = 'L', .name = "layout", .has_arg = required_argument },
2156 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2157 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2158 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2159 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2160 /* --non-block is only valid in migrate mode */
2161 { .val = 'n', .name = "non-block", .has_arg = no_argument},
2162 { .val = 'o', .name = "ost", .has_arg = required_argument},
2163 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2164 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2165 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2167 { .val = 'p', .name = "pool", .has_arg = required_argument },
2168 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2169 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2170 /* dirstripe {"mdt-count", required_argument, 0, 'T'}, */
2171 /* --verbose is only valid in migrate mode */
2172 { .val = 'v', .name = "verbose", .has_arg = no_argument },
2175 setstripe_args_init(&lsa);
2177 migrate_mode = (opc == SO_MIGRATE);
2178 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2180 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2182 while ((c = getopt_long(argc, argv, "bc:dE:f:i:I:m:N::no:p:L:s:S:v",
2183 long_opts, NULL)) >= 0) {
2188 case LFS_COMP_ADD_OPT:
2191 case LFS_COMP_DEL_OPT:
2194 case LFS_COMP_FLAGS_OPT:
2195 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2196 &lsa.lsa_comp_neg_flags);
2199 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2200 fprintf(stderr, "%s: inverted flags are not supported\n",
2204 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2206 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2213 case LFS_COMP_SET_OPT:
2216 case LFS_COMP_USE_PARENT_OPT:
2218 fprintf(stderr, "error: %s: --parent must be "
2219 "specified with --mirror-count|-N "
2220 "option\n", progname);
2223 setstripe_args_init(&lsa);
2225 case LFS_COMP_NO_VERIFY_OPT:
2226 mirror_flags |= NO_VERIFY;
2228 case LFS_MIRROR_FLAGS_OPT: {
2231 if (!mirror_mode || !last_mirror) {
2232 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2237 result = comp_str2flags(optarg, &last_mirror->m_flags,
2243 fprintf(stderr, "%s: inverted flags are not supported\n",
2248 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2250 "%s: unsupported mirror flags: %s\n",
2258 if (!migrate_mode) {
2260 "%s %s: -b|--block valid only for migrate command\n",
2264 migration_block = true;
2267 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2270 "%s %s: invalid stripe count '%s'\n",
2271 progname, argv[0], optarg);
2275 if (lsa.lsa_stripe_count == -1)
2276 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2279 /* delete the default striping pattern */
2283 if (lsa.lsa_comp_end != 0) {
2284 result = comp_args_to_layout(lpp, &lsa);
2287 "%s %s: invalid layout\n",
2292 setstripe_args_init_inherit(&lsa);
2295 if (arg_is_eof(optarg)) {
2296 lsa.lsa_comp_end = LUSTRE_EOF;
2298 result = llapi_parse_size(optarg,
2303 "%s %s: invalid component end '%s'\n",
2304 progname, argv[0], optarg);
2310 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2313 "%s %s: invalid stripe offset '%s'\n",
2314 progname, argv[0], optarg);
2317 if (lsa.lsa_stripe_off == -1)
2318 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2321 comp_id = strtoul(optarg, &end, 0);
2322 if (*end != '\0' || comp_id == 0 ||
2323 comp_id > LCME_ID_MAX) {
2325 "%s %s: invalid component ID '%s'\n",
2326 progname, argv[0], optarg);
2331 if (opc != SO_MIRROR_EXTEND) {
2333 "error: %s: invalid option: %s\n",
2334 progname, argv[optopt + 1]);
2337 if (last_mirror == NULL) {
2338 fprintf(stderr, "error: %s: '-N' must exist "
2339 "in front of '%s'\n",
2340 progname, argv[optopt + 1]);
2344 last_mirror->m_file = optarg;
2345 last_mirror->m_count = 1;
2349 if (strcmp(argv[optind - 1], "mdt") == 0) {
2350 /* Can be only the first component */
2351 if (layout != NULL) {
2353 fprintf(stderr, "error: 'mdt' layout "
2354 "can be only the first one\n");
2357 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2359 fprintf(stderr, "error: 'mdt' layout "
2360 "size is too big\n");
2363 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2364 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2366 fprintf(stderr, "error: layout '%s' is "
2367 "unknown, supported layouts are: "
2368 "'mdt', 'raid0'\n", argv[optind]);
2373 if (!migrate_mode) {
2375 "%s %s: -m|--mdt-index valid only for migrate command\n",
2379 mdt_idx_arg = optarg;
2382 if (!migrate_mode) {
2384 "%s %s: -n|--non-block valid only for migrate command\n",
2388 migration_flags |= MIGRATION_NONBLOCK;
2391 if (opc == SO_SETSTRIPE) {
2392 opc = SO_MIRROR_CREATE;
2396 if (optarg != NULL) {
2397 mirror_count = strtoul(optarg, &end, 0);
2398 if (*end != '\0' || mirror_count == 0) {
2400 "error: %s: bad mirror count: %s\n",
2407 new_mirror = lfs_mirror_alloc();
2408 new_mirror->m_count = mirror_count;
2410 if (mirror_list == NULL)
2411 mirror_list = new_mirror;
2413 if (last_mirror != NULL) {
2414 /* wrap up last mirror */
2415 if (lsa.lsa_comp_end == 0)
2416 lsa.lsa_comp_end = LUSTRE_EOF;
2418 result = comp_args_to_layout(lpp, &lsa);
2420 lfs_mirror_free(new_mirror);
2424 setstripe_args_init_inherit(&lsa);
2426 last_mirror->m_next = new_mirror;
2429 last_mirror = new_mirror;
2430 lpp = &last_mirror->m_layout;
2433 lsa.lsa_nr_tgts = parse_targets(osts,
2434 sizeof(osts) / sizeof(__u32),
2435 lsa.lsa_nr_tgts, optarg);
2436 if (lsa.lsa_nr_tgts < 0) {
2438 "%s %s: invalid OST target(s) '%s'\n",
2439 progname, argv[0], optarg);
2443 lsa.lsa_tgts = osts;
2444 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2445 lsa.lsa_stripe_off = osts[0];
2450 lsa.lsa_pool_name = optarg;
2453 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2457 "%s %s: invalid stripe size '%s'\n",
2458 progname, argv[0], optarg);
2463 if (!migrate_mode) {
2465 "%s %s: -v|--verbose valid only for migrate command\n",
2469 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2472 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2473 progname, argv[0], argv[optind - 1]);
2478 fname = argv[optind];
2480 if (optind == argc) {
2481 fprintf(stderr, "%s %s: FILE must be specified\n",
2486 if (mirror_mode && mirror_count == 0) {
2488 "error: %s: --mirror-count|-N option is required\n",
2495 if (lsa.lsa_comp_end == 0)
2496 lsa.lsa_comp_end = LUSTRE_EOF;
2499 if (lsa.lsa_comp_end != 0) {
2500 result = comp_args_to_layout(lpp, &lsa);
2505 if (mirror_flags & NO_VERIFY) {
2506 if (opc != SO_MIRROR_EXTEND) {
2508 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2512 } else if (!has_m_file) {
2514 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2521 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2522 * altered by user space tool, so we don't need to support the
2523 * --component-set for this moment. */
2524 if (comp_set && !comp_id) {
2525 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2530 if ((delete + comp_set + comp_del + comp_add) > 1) {
2532 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2537 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2538 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2540 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2545 if ((comp_set || comp_del) &&
2546 (setstripe_args_specified(&lsa) || layout != NULL)) {
2548 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
2553 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
2555 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
2560 if (comp_add || comp_del) {
2563 result = lstat(fname, &st);
2564 if (result == 0 && S_ISDIR(st.st_mode)) {
2566 "%s setstripe: cannot use --component-add or --component-del for directory\n",
2572 fprintf(stderr, "error: %s: can't use --component-add "
2573 "or --component-del for mirror operation\n",
2580 if (layout == NULL) {
2582 "%s %s: option -E must be specified with --component-add\n",
2587 result = adjust_first_extent(fname, layout);
2588 if (result == -ENODATA)
2590 else if (result != 0)
2594 if (mdt_idx_arg != NULL && optind > 3) {
2596 "%s %s: option -m cannot be used with other options\n",
2601 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
2603 "%s %s: options --non-block and --block are mutually exclusive\n",
2608 if (!comp_del && !comp_set && comp_id != 0) {
2610 "%s %s: option -I can only be used with --component-del\n",
2615 if (mdt_idx_arg != NULL) {
2616 /* initialize migrate mdt parameters */
2617 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
2619 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
2620 progname, argv[0], mdt_idx_arg);
2623 migrate_mdt_param.fp_migrate = 1;
2624 } else if (layout == NULL) {
2625 /* initialize stripe parameters */
2626 param = calloc(1, offsetof(typeof(*param),
2627 lsp_osts[lsa.lsa_nr_tgts]));
2628 if (param == NULL) {
2630 "%s %s: cannot allocate memory for parameters: %s\n",
2631 progname, argv[0], strerror(ENOMEM));
2636 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
2637 param->lsp_stripe_size = lsa.lsa_stripe_size;
2638 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2639 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
2640 param->lsp_stripe_count = -1;
2642 param->lsp_stripe_count = lsa.lsa_stripe_count;
2644 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2645 param->lsp_stripe_offset = -1;
2647 param->lsp_stripe_offset = lsa.lsa_stripe_off;
2648 param->lsp_pool = lsa.lsa_pool_name;
2649 param->lsp_is_specific = false;
2650 if (lsa.lsa_nr_tgts > 0) {
2651 if (lsa.lsa_stripe_count > 0 &&
2652 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2653 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2654 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
2655 fprintf(stderr, "error: %s: stripe count %lld "
2656 "doesn't match the number of OSTs: %d\n"
2657 , argv[0], lsa.lsa_stripe_count,
2663 param->lsp_is_specific = true;
2664 param->lsp_stripe_count = lsa.lsa_nr_tgts;
2665 memcpy(param->lsp_osts, osts,
2666 sizeof(*osts) * lsa.lsa_nr_tgts);
2670 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
2671 if (mdt_idx_arg != NULL) {
2672 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
2673 } else if (migrate_mode) {
2674 result = lfs_migrate(fname, migration_flags, param,
2676 } else if (comp_set != 0) {
2677 result = lfs_component_set(fname, comp_id,
2679 lsa.lsa_comp_neg_flags);
2680 } else if (comp_del != 0) {
2681 result = lfs_component_del(fname, comp_id,
2683 lsa.lsa_comp_neg_flags);
2684 } else if (comp_add != 0) {
2685 result = lfs_component_add(fname, layout);
2686 } else if (opc == SO_MIRROR_CREATE) {
2687 result = mirror_create(fname, mirror_list);
2688 } else if (opc == SO_MIRROR_EXTEND) {
2689 result = mirror_extend(fname, mirror_list,
2691 } else if (layout != NULL) {
2692 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
2699 result = llapi_file_open_param(fname,
2708 /* Save the first error encountered. */
2716 llapi_layout_free(layout);
2717 lfs_mirror_list_free(mirror_list);
2722 llapi_layout_free(layout);
2723 lfs_mirror_list_free(mirror_list);
2727 static int lfs_poollist(int argc, char **argv)
2732 return llapi_poollist(argv[1]);
2735 static int set_time(time_t *time, time_t *set, char *str)
2742 else if (str[0] == '-')
2748 t = strtol(str, NULL, 0);
2749 if (*time < t * 24 * 60 * 60) {
2752 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
2756 *set = *time - t * 24 * 60 * 60;
2759 static int name2uid(unsigned int *id, const char *name)
2761 struct passwd *passwd;
2763 passwd = getpwnam(name);
2766 *id = passwd->pw_uid;
2771 static int name2gid(unsigned int *id, const char *name)
2773 struct group *group;
2775 group = getgrnam(name);
2778 *id = group->gr_gid;
2783 static inline int name2projid(unsigned int *id, const char *name)
2788 static int uid2name(char **name, unsigned int id)
2790 struct passwd *passwd;
2792 passwd = getpwuid(id);
2795 *name = passwd->pw_name;
2800 static inline int gid2name(char **name, unsigned int id)
2802 struct group *group;
2804 group = getgrgid(id);
2807 *name = group->gr_name;
2812 static int name2layout(__u32 *layout, char *name)
2814 char *ptr, *layout_name;
2817 for (ptr = name; ; ptr = NULL) {
2818 layout_name = strtok(ptr, ",");
2819 if (layout_name == NULL)
2821 if (strcmp(layout_name, "released") == 0)
2822 *layout |= LOV_PATTERN_F_RELEASED;
2823 else if (strcmp(layout_name, "raid0") == 0)
2824 *layout |= LOV_PATTERN_RAID0;
2825 else if (strcmp(layout_name, "mdt") == 0)
2826 *layout |= LOV_PATTERN_MDT;
2833 static int lfs_find(int argc, char **argv)
2838 struct find_param param = {
2842 struct option long_opts[] = {
2843 { .val = 'A', .name = "atime", .has_arg = required_argument },
2844 { .val = LFS_COMP_COUNT_OPT,
2845 .name = "comp-count", .has_arg = required_argument },
2846 { .val = LFS_COMP_COUNT_OPT,
2847 .name = "component-count",
2848 .has_arg = required_argument },
2849 { .val = LFS_COMP_FLAGS_OPT,
2850 .name = "comp-flags", .has_arg = required_argument },
2851 { .val = LFS_COMP_FLAGS_OPT,
2852 .name = "component-flags",
2853 .has_arg = required_argument },
2854 { .val = LFS_COMP_START_OPT,
2855 .name = "comp-start", .has_arg = required_argument },
2856 { .val = LFS_COMP_START_OPT,
2857 .name = "component-start",
2858 .has_arg = required_argument },
2859 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
2860 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
2861 { .val = 'C', .name = "ctime", .has_arg = required_argument },
2862 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
2863 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
2864 { .val = 'E', .name = "component-end",
2865 .has_arg = required_argument },
2866 { .val = 'g', .name = "gid", .has_arg = required_argument },
2867 { .val = 'G', .name = "group", .has_arg = required_argument },
2868 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
2869 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
2870 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
2871 /*{"component-id", required_argument, 0, 'I'},*/
2872 { .val = 'L', .name = "layout", .has_arg = required_argument },
2873 { .val = 'm', .name = "mdt", .has_arg = required_argument },
2874 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
2875 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
2876 { .val = 'M', .name = "mtime", .has_arg = required_argument },
2877 { .val = 'n', .name = "name", .has_arg = required_argument },
2878 /* reserve {"or", no_argument, , 0, 'o'}, to match find(1) */
2879 { .val = 'O', .name = "obd", .has_arg = required_argument },
2880 { .val = 'O', .name = "ost", .has_arg = required_argument },
2881 /* no short option for pool, p/P already used */
2882 { .val = LFS_POOL_OPT,
2883 .name = "pool", .has_arg = required_argument },
2884 { .val = 'p', .name = "print0", .has_arg = no_argument },
2885 { .val = 'P', .name = "print", .has_arg = no_argument },
2886 { .val = LFS_PROJID_OPT,
2887 .name = "projid", .has_arg = required_argument },
2888 { .val = 's', .name = "size", .has_arg = required_argument },
2889 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2890 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2891 { .val = 't', .name = "type", .has_arg = required_argument },
2892 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
2893 { .val = 'u', .name = "uid", .has_arg = required_argument },
2894 { .val = 'U', .name = "user", .has_arg = required_argument },
2906 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
2907 while ((c = getopt_long_only(argc, argv,
2908 "-A:c:C:D:E:g:G:H:i:L:m:M:n:O:Ppqrs:S:t:T:u:U:v",
2909 long_opts, NULL)) >= 0) {
2914 /* '!' is part of option */
2915 /* when getopt_long_only() finds a string which is not
2916 * an option nor a known option argument it returns 1
2917 * in that case if we already have found pathstart and pathend
2918 * (i.e. we have the list of pathnames),
2919 * the only supported value is "!"
2921 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
2922 if (!isoption && pathend != -1) {
2923 fprintf(stderr, "err: %s: filename|dirname must either "
2924 "precede options or follow options\n",
2929 if (!isoption && pathstart == -1)
2930 pathstart = optind - 1;
2931 if (isoption && pathstart != -1 && pathend == -1)
2932 pathend = optind - 2;
2938 /* unknown; opt is "!" or path component,
2939 * checking done above.
2941 if (strcmp(optarg, "!") == 0)
2945 xtime = ¶m.fp_atime;
2946 xsign = ¶m.fp_asign;
2947 param.fp_exclude_atime = !!neg_opt;
2948 /* no break, this falls through to 'C' for ctime */
2951 xtime = ¶m.fp_ctime;
2952 xsign = ¶m.fp_csign;
2953 param.fp_exclude_ctime = !!neg_opt;
2955 /* no break, this falls through to 'M' for mtime */
2958 xtime = ¶m.fp_mtime;
2959 xsign = ¶m.fp_msign;
2960 param.fp_exclude_mtime = !!neg_opt;
2962 rc = set_time(&t, xtime, optarg);
2963 if (rc == INT_MAX) {
2970 case LFS_COMP_COUNT_OPT:
2971 if (optarg[0] == '+') {
2972 param.fp_comp_count_sign = -1;
2974 } else if (optarg[0] == '-') {
2975 param.fp_comp_count_sign = 1;
2979 param.fp_comp_count = strtoul(optarg, &endptr, 0);
2980 if (*endptr != '\0') {
2981 fprintf(stderr, "error: bad component count "
2985 param.fp_check_comp_count = 1;
2986 param.fp_exclude_comp_count = !!neg_opt;
2988 case LFS_COMP_FLAGS_OPT:
2989 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
2990 ¶m.fp_comp_neg_flags);
2992 fprintf(stderr, "error: bad component flags "
2996 param.fp_check_comp_flags = 1;
2998 __u32 flags = param.fp_comp_neg_flags;
2999 param.fp_comp_neg_flags = param.fp_comp_flags;
3000 param.fp_comp_flags = flags;
3003 case LFS_COMP_START_OPT:
3004 if (optarg[0] == '+') {
3005 param.fp_comp_start_sign = -1;
3007 } else if (optarg[0] == '-') {
3008 param.fp_comp_start_sign = 1;
3012 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3013 ¶m.fp_comp_start_units, 0);
3015 fprintf(stderr, "error: bad component start "
3019 param.fp_check_comp_start = 1;
3020 param.fp_exclude_comp_start = !!neg_opt;
3023 if (optarg[0] == '+') {
3024 param.fp_stripe_count_sign = -1;
3026 } else if (optarg[0] == '-') {
3027 param.fp_stripe_count_sign = 1;
3031 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3032 if (*endptr != '\0') {
3033 fprintf(stderr,"error: bad stripe_count '%s'\n",
3038 param.fp_check_stripe_count = 1;
3039 param.fp_exclude_stripe_count = !!neg_opt;
3042 param.fp_max_depth = strtol(optarg, 0, 0);
3045 if (optarg[0] == '+') {
3046 param.fp_comp_end_sign = -1;
3048 } else if (optarg[0] == '-') {
3049 param.fp_comp_end_sign = 1;
3053 if (arg_is_eof(optarg)) {
3054 param.fp_comp_end = LUSTRE_EOF;
3055 param.fp_comp_end_units = 1;
3058 rc = llapi_parse_size(optarg,
3060 ¶m.fp_comp_end_units, 0);
3063 fprintf(stderr, "error: bad component end "
3067 param.fp_check_comp_end = 1;
3068 param.fp_exclude_comp_end = !!neg_opt;
3072 rc = name2gid(¶m.fp_gid, optarg);
3074 param.fp_gid = strtoul(optarg, &endptr, 10);
3075 if (*endptr != '\0') {
3076 fprintf(stderr, "Group/GID: %s cannot "
3077 "be found.\n", optarg);
3082 param.fp_exclude_gid = !!neg_opt;
3083 param.fp_check_gid = 1;
3086 param.fp_hash_type = check_hashtype(optarg);
3087 if (param.fp_hash_type == 0) {
3088 fprintf(stderr, "error: bad hash_type '%s'\n",
3093 param.fp_check_hash_type = 1;
3094 param.fp_exclude_hash_type = !!neg_opt;
3097 ret = name2layout(¶m.fp_layout, optarg);
3100 param.fp_exclude_layout = !!neg_opt;
3101 param.fp_check_layout = 1;
3105 rc = name2uid(¶m.fp_uid, optarg);
3107 param.fp_uid = strtoul(optarg, &endptr, 10);
3108 if (*endptr != '\0') {
3109 fprintf(stderr, "User/UID: %s cannot "
3110 "be found.\n", optarg);
3115 param.fp_exclude_uid = !!neg_opt;
3116 param.fp_check_uid = 1;
3119 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3121 "Pool name %s is too long"
3122 " (max is %d)\n", optarg,
3127 /* we do check for empty pool because empty pool
3128 * is used to find V1 lov attributes */
3129 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3130 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3131 param.fp_exclude_pool = !!neg_opt;
3132 param.fp_check_pool = 1;
3135 param.fp_pattern = (char *)optarg;
3136 param.fp_exclude_pattern = !!neg_opt;
3141 char *buf, *token, *next, *p;
3145 buf = strdup(optarg);
3151 param.fp_exclude_obd = !!neg_opt;
3154 while (token && *token) {
3155 token = strchr(token, ',');
3162 param.fp_exclude_mdt = !!neg_opt;
3163 param.fp_num_alloc_mdts += len;
3164 tmp = realloc(param.fp_mdt_uuid,
3165 param.fp_num_alloc_mdts *
3166 sizeof(*param.fp_mdt_uuid));
3172 param.fp_mdt_uuid = tmp;
3174 param.fp_exclude_obd = !!neg_opt;
3175 param.fp_num_alloc_obds += len;
3176 tmp = realloc(param.fp_obd_uuid,
3177 param.fp_num_alloc_obds *
3178 sizeof(*param.fp_obd_uuid));
3184 param.fp_obd_uuid = tmp;
3186 for (token = buf; token && *token; token = next) {
3187 struct obd_uuid *puuid;
3190 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3193 ¶m.fp_obd_uuid[param.fp_num_obds++];
3195 p = strchr(token, ',');
3202 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3207 strncpy(puuid->uuid, token,
3208 sizeof(puuid->uuid));
3216 param.fp_zero_end = 1;
3220 case LFS_PROJID_OPT:
3221 rc = name2projid(¶m.fp_projid, optarg);
3223 param.fp_projid = strtoul(optarg, &endptr, 10);
3224 if (*endptr != '\0') {
3226 "Invalid project ID: %s",
3232 param.fp_exclude_projid = !!neg_opt;
3233 param.fp_check_projid = 1;
3236 if (optarg[0] == '+') {
3237 param.fp_size_sign = -1;
3239 } else if (optarg[0] == '-') {
3240 param.fp_size_sign = 1;
3244 ret = llapi_parse_size(optarg, ¶m.fp_size,
3245 ¶m.fp_size_units, 0);
3247 fprintf(stderr, "error: bad file size '%s'\n",
3251 param.fp_check_size = 1;
3252 param.fp_exclude_size = !!neg_opt;
3255 if (optarg[0] == '+') {
3256 param.fp_stripe_size_sign = -1;
3258 } else if (optarg[0] == '-') {
3259 param.fp_stripe_size_sign = 1;
3263 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3264 ¶m.fp_stripe_size_units, 0);
3266 fprintf(stderr, "error: bad stripe_size '%s'\n",
3270 param.fp_check_stripe_size = 1;
3271 param.fp_exclude_stripe_size = !!neg_opt;
3274 param.fp_exclude_type = !!neg_opt;
3275 switch (optarg[0]) {
3277 param.fp_type = S_IFBLK;
3280 param.fp_type = S_IFCHR;
3283 param.fp_type = S_IFDIR;
3286 param.fp_type = S_IFREG;
3289 param.fp_type = S_IFLNK;
3292 param.fp_type = S_IFIFO;
3295 param.fp_type = S_IFSOCK;
3298 fprintf(stderr, "error: %s: bad type '%s'\n",
3305 if (optarg[0] == '+') {
3306 param.fp_mdt_count_sign = -1;
3308 } else if (optarg[0] == '-') {
3309 param.fp_mdt_count_sign = 1;
3313 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3314 if (*endptr != '\0') {
3315 fprintf(stderr, "error: bad mdt_count '%s'\n",
3320 param.fp_check_mdt_count = 1;
3321 param.fp_exclude_mdt_count = !!neg_opt;
3329 if (pathstart == -1) {
3330 fprintf(stderr, "error: %s: no filename|pathname\n",
3334 } else if (pathend == -1) {
3340 rc = llapi_find(argv[pathstart], ¶m);
3341 if (rc != 0 && ret == 0)
3343 } while (++pathstart < pathend);
3346 fprintf(stderr, "error: %s failed for %s.\n",
3347 argv[0], argv[optind - 1]);
3349 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3350 free(param.fp_obd_uuid);
3352 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3353 free(param.fp_mdt_uuid);
3358 static int lfs_getstripe_internal(int argc, char **argv,
3359 struct find_param *param)
3361 struct option long_opts[] = {
3362 { .val = LFS_COMP_COUNT_OPT,
3363 .name = "comp-count", .has_arg = no_argument },
3364 { .val = LFS_COMP_COUNT_OPT,
3365 .name = "component-count", .has_arg = no_argument },
3366 { .val = LFS_COMP_FLAGS_OPT,
3367 .name = "comp-flags", .has_arg = optional_argument },
3368 { .val = LFS_COMP_FLAGS_OPT,
3369 .name = "component-flags", .has_arg = optional_argument },
3370 { .val = LFS_COMP_START_OPT,
3371 .name = "comp-start", .has_arg = optional_argument },
3372 { .val = LFS_COMP_START_OPT,
3373 .name = "component-start", .has_arg = optional_argument },
3374 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3375 /* This formerly implied "stripe-count", but was explicitly
3376 * made "stripe-count" for consistency with other options,
3377 * and to separate it from "mdt-count" when DNE arrives. */
3378 { .val = 'c', .name = "count", .has_arg = no_argument },
3380 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3381 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3382 { .val = 'd', .name = "directory", .has_arg = no_argument },
3383 { .val = 'D', .name = "default", .has_arg = no_argument },
3384 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3385 { .val = 'E', .name = "component-end",
3386 .has_arg = optional_argument },
3387 { .val = 'F', .name = "fid", .has_arg = no_argument },
3388 { .val = 'g', .name = "generation", .has_arg = no_argument },
3389 /* dirstripe { .val = 'H', .name = "mdt-hash",
3390 * .has_arg = required_argument }, */
3391 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3392 /* This formerly implied "stripe-index", but was explicitly
3393 * made "stripe-index" for consistency with other options,
3394 * and to separate it from "mdt-index" when DNE arrives. */
3395 { .val = 'i', .name = "index", .has_arg = no_argument },
3397 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3398 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3399 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3400 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3401 { .val = 'L', .name = "layout", .has_arg = no_argument },
3402 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3403 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3404 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3405 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3406 { .val = 'M', .name = "mdt-index", .has_arg = no_argument },
3407 { .val = 'M', .name = "mdt_index", .has_arg = no_argument },
3409 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3410 /* This formerly implied "stripe-index", but was confusing
3411 * with "file offset" (which will eventually be needed for
3412 * with different layouts by offset), so deprecate it. */
3413 { .val = 'o', .name = "offset", .has_arg = no_argument },
3415 { .val = 'O', .name = "obd", .has_arg = required_argument },
3416 { .val = 'O', .name = "ost", .has_arg = required_argument },
3417 { .val = 'p', .name = "pool", .has_arg = no_argument },
3418 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3419 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3420 { .val = 'R', .name = "raw", .has_arg = no_argument },
3421 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3422 /* This formerly implied "--stripe-size", but was confusing
3423 * with "lfs find --size|-s", which means "file size", so use
3424 * the consistent "--stripe-size|-S" for all commands. */
3425 { .val = 's', .name = "size", .has_arg = no_argument },
3427 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3428 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3429 /* dirstripe { .val = 'T', .name = "mdt-count",
3430 * .has_arg = required_argument }, */
3431 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3432 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3437 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
3438 long_opts, NULL)) != -1) {
3441 if (strcmp(argv[optind - 1], "--count") == 0)
3442 fprintf(stderr, "warning: '--count' deprecated,"
3443 " use '--stripe-count' instead\n");
3444 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3445 param->fp_verbose |= VERBOSE_COUNT;
3446 param->fp_max_depth = 0;
3449 case LFS_COMP_COUNT_OPT:
3450 param->fp_verbose |= VERBOSE_COMP_COUNT;
3451 param->fp_max_depth = 0;
3453 case LFS_COMP_FLAGS_OPT:
3454 if (optarg != NULL) {
3455 rc = comp_str2flags(optarg,
3456 ¶m->fp_comp_flags,
3457 ¶m->fp_comp_neg_flags);
3459 fprintf(stderr, "error: %s bad "
3460 "component flags '%s'.\n",
3464 param->fp_check_comp_flags = 1;
3466 param->fp_verbose |= VERBOSE_COMP_FLAGS;
3467 param->fp_max_depth = 0;
3470 case LFS_COMP_START_OPT:
3471 if (optarg != NULL) {
3473 if (tmp[0] == '+') {
3474 param->fp_comp_start_sign = -1;
3476 } else if (tmp[0] == '-') {
3477 param->fp_comp_start_sign = 1;
3480 rc = llapi_parse_size(tmp,
3481 ¶m->fp_comp_start,
3482 ¶m->fp_comp_start_units, 0);
3484 fprintf(stderr, "error: %s bad "
3485 "component start '%s'.\n",
3489 param->fp_check_comp_start = 1;
3492 param->fp_verbose |= VERBOSE_COMP_START;
3493 param->fp_max_depth = 0;
3497 param->fp_max_depth = 0;
3500 param->fp_get_default_lmv = 1;
3503 if (optarg != NULL) {
3505 if (tmp[0] == '+') {
3506 param->fp_comp_end_sign = -1;
3508 } else if (tmp[0] == '-') {
3509 param->fp_comp_end_sign = 1;
3513 if (arg_is_eof(tmp)) {
3514 param->fp_comp_end = LUSTRE_EOF;
3515 param->fp_comp_end_units = 1;
3518 rc = llapi_parse_size(tmp,
3519 ¶m->fp_comp_end,
3520 ¶m->fp_comp_end_units, 0);
3523 fprintf(stderr, "error: %s bad "
3524 "component end '%s'.\n",
3528 param->fp_check_comp_end = 1;
3530 param->fp_verbose |= VERBOSE_COMP_END;
3531 param->fp_max_depth = 0;
3535 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3536 param->fp_verbose |= VERBOSE_DFID;
3537 param->fp_max_depth = 0;
3541 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3542 param->fp_verbose |= VERBOSE_GENERATION;
3543 param->fp_max_depth = 0;
3546 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3548 fprintf(stderr, "warning: '--offset|-o' deprecated, "
3549 "use '--stripe-index|-i' instead\n");
3552 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
3553 if (strcmp(argv[optind - 1], "--index") == 0)
3554 fprintf(stderr, "warning: '--index' deprecated"
3555 ", use '--stripe-index' instead\n");
3557 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3558 param->fp_verbose |= VERBOSE_OFFSET;
3559 param->fp_max_depth = 0;
3563 if (optarg != NULL) {
3564 param->fp_comp_id = strtoul(optarg, &end, 0);
3565 if (*end != '\0' || param->fp_comp_id == 0 ||
3566 param->fp_comp_id > LCME_ID_MAX) {
3567 fprintf(stderr, "error: %s bad "
3568 "component id '%s'\n",
3572 param->fp_check_comp_id = 1;
3575 param->fp_max_depth = 0;
3576 param->fp_verbose |= VERBOSE_COMP_ID;
3580 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3581 param->fp_verbose |= VERBOSE_LAYOUT;
3582 param->fp_max_depth = 0;
3585 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3587 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3588 fprintf(stderr, "warning: '-M' deprecated"
3589 ", use '-m' instead\n");
3593 if (!(param->fp_verbose & VERBOSE_DETAIL))
3594 param->fp_max_depth = 0;
3595 param->fp_verbose |= VERBOSE_MDTINDEX;
3598 if (param->fp_obd_uuid) {
3600 "error: %s: only one obduuid allowed",
3604 param->fp_obd_uuid = (struct obd_uuid *)optarg;
3607 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3608 param->fp_verbose |= VERBOSE_POOL;
3609 param->fp_max_depth = 0;
3616 param->fp_recursive = 1;
3621 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3623 fprintf(stderr, "warning: '--size|-s' deprecated, "
3624 "use '--stripe-size|-S' instead\n");
3625 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0) */
3627 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3628 param->fp_verbose |= VERBOSE_SIZE;
3629 param->fp_max_depth = 0;
3633 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
3646 if (param->fp_recursive)
3647 param->fp_max_depth = -1;
3648 else if (param->fp_verbose & VERBOSE_DETAIL)
3649 param->fp_max_depth = 1;
3651 if (!param->fp_verbose)
3652 param->fp_verbose = VERBOSE_DEFAULT;
3653 if (param->fp_quiet)
3654 param->fp_verbose = VERBOSE_OBJID;
3657 rc = llapi_getstripe(argv[optind], param);
3658 } while (++optind < argc && !rc);
3661 fprintf(stderr, "error: %s failed for %s.\n",
3662 argv[0], argv[optind - 1]);
3666 static int lfs_tgts(int argc, char **argv)
3668 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3669 struct find_param param;
3670 int index = 0, rc=0;
3675 if (argc == 2 && !realpath(argv[1], path)) {
3677 fprintf(stderr, "error: invalid path '%s': %s\n",
3678 argv[1], strerror(-rc));
3682 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
3683 /* Check if we have a mount point */
3684 if (mntdir[0] == '\0')
3687 memset(¶m, 0, sizeof(param));
3688 if (!strcmp(argv[0], "mdts"))
3689 param.fp_get_lmv = 1;
3691 rc = llapi_ostlist(mntdir, ¶m);
3693 fprintf(stderr, "error: %s: failed on %s\n",
3696 if (path[0] != '\0')
3698 memset(mntdir, 0, PATH_MAX);
3704 static int lfs_getstripe(int argc, char **argv)
3706 struct find_param param = { 0 };
3708 param.fp_max_depth = 1;
3709 return lfs_getstripe_internal(argc, argv, ¶m);
3713 static int lfs_getdirstripe(int argc, char **argv)
3715 struct find_param param = { 0 };
3716 struct option long_opts[] = {
3717 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3718 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
3720 { .val = 'D', .name = "default", .has_arg = no_argument },
3721 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
3722 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
3723 { .val = 'O', .name = "obd", .has_arg = required_argument },
3724 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3725 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3726 { .val = 't', .name = "mdt-hash", .has_arg = no_argument },
3728 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
3729 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3733 param.fp_get_lmv = 1;
3735 while ((c = getopt_long(argc, argv,
3736 "cDHiO:rtTy", long_opts, NULL)) != -1)
3740 if (param.fp_obd_uuid) {
3742 "error: %s: only one obduuid allowed",
3746 param.fp_obd_uuid = (struct obd_uuid *)optarg;
3748 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3750 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 10, 50, 0)
3751 fprintf(stderr, "warning: '-c' deprecated"
3752 ", use '-T' instead\n");
3756 param.fp_verbose |= VERBOSE_COUNT;
3759 param.fp_verbose |= VERBOSE_OFFSET;
3761 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3765 param.fp_verbose |= VERBOSE_HASH_TYPE;
3768 param.fp_get_default_lmv = 1;
3771 param.fp_recursive = 1;
3784 if (param.fp_recursive)
3785 param.fp_max_depth = -1;
3787 if (!param.fp_verbose)
3788 param.fp_verbose = VERBOSE_DEFAULT;
3791 rc = llapi_getstripe(argv[optind], ¶m);
3792 } while (++optind < argc && !rc);
3795 fprintf(stderr, "error: %s failed for %s.\n",
3796 argv[0], argv[optind - 1]);
3801 static int lfs_setdirstripe(int argc, char **argv)
3805 struct lfs_setstripe_args lsa;
3806 struct llapi_stripe_param *param = NULL;
3807 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
3810 char *mode_opt = NULL;
3811 bool default_stripe = false;
3812 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
3813 mode_t previous_mode = 0;
3814 bool delete = false;
3816 struct option long_opts[] = {
3817 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3818 { .val = 'c', .name = "count", .has_arg = required_argument },
3820 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
3821 { .val = 'd', .name = "delete", .has_arg = no_argument },
3822 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3823 { .val = 'i', .name = "index", .has_arg = required_argument },
3825 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
3826 { .val = 'm', .name = "mode", .has_arg = required_argument },
3827 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3828 { .val = 't', .name = "hash-type", .has_arg = required_argument },
3829 { .val = 't', .name = "mdt-hash", .has_arg = required_argument },
3831 {"mdt-hash", required_argument, 0, 'H'},
3832 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3833 { .val = 'D', .name = "default_stripe",
3834 .has_arg = no_argument },
3836 { .val = 'D', .name = "default", .has_arg = no_argument },
3839 setstripe_args_init(&lsa);
3841 while ((c = getopt_long(argc, argv, "c:dDi:H:m:t:", long_opts,
3848 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3849 if (strcmp(argv[optind - 1], "--count") == 0)
3851 "%s %s: warning: '--count' deprecated, use '--mdt-count' instead\n",
3854 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3857 "%s %s: invalid stripe count '%s'\n",
3858 progname, argv[0], optarg);
3864 default_stripe = true;
3867 default_stripe = true;
3870 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3871 if (strcmp(argv[optind - 1], "--index") == 0)
3873 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
3876 lsa.lsa_nr_tgts = parse_targets(mdts,
3877 sizeof(mdts) / sizeof(__u32),
3878 lsa.lsa_nr_tgts, optarg);
3879 if (lsa.lsa_nr_tgts < 0) {
3881 "%s %s: invalid MDT target(s) '%s'\n",
3882 progname, argv[0], optarg);
3886 lsa.lsa_tgts = mdts;
3887 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3888 lsa.lsa_stripe_off = mdts[0];
3893 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3897 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3898 if (strcmp(argv[optind - 1], "--hash-type") == 0)
3900 "%s %s: warning: '--hash-type' deprecated, use '--mdt-hash' instead\n",
3903 lsa.lsa_pattern = check_hashtype(optarg);
3904 if (lsa.lsa_pattern == 0) {
3906 "%s %s: bad stripe hash type '%s'\n",
3907 progname, argv[0], optarg);
3912 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3913 progname, argv[0], argv[optind - 1]);
3918 if (optind == argc) {
3919 fprintf(stderr, "%s %s: DIR must be specified\n",
3924 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
3925 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
3927 "%s %s: stripe offset and count must be specified\n",
3933 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
3934 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
3936 "%s %s: cannot specify -d with -c or -i options\n",
3941 if (mode_opt != NULL) {
3942 mode = strtoul(mode_opt, &end, 8);
3945 "%s %s: bad MODE '%s'\n",
3946 progname, argv[0], mode_opt);
3949 previous_mode = umask(0);
3952 /* initialize stripe parameters */
3953 param = calloc(1, offsetof(typeof(*param), lsp_osts[lsa.lsa_nr_tgts]));
3954 if (param == NULL) {
3956 "%s %s: cannot allocate memory for parameters: %s\n",
3957 progname, argv[0], strerror(ENOMEM));
3961 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3962 param->lsp_stripe_count = lsa.lsa_stripe_count;
3963 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3964 param->lsp_stripe_offset = -1;
3966 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3967 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3968 param->lsp_stripe_pattern = lsa.lsa_pattern;
3970 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
3971 param->lsp_pool = lsa.lsa_pool_name;
3972 param->lsp_is_specific = false;
3973 if (lsa.lsa_nr_tgts > 1) {
3974 if (lsa.lsa_stripe_count > 0 &&
3975 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3976 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3977 fprintf(stderr, "error: %s: stripe count %lld doesn't "
3978 "match the number of MDTs: %d\n",
3979 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
3984 param->lsp_is_specific = true;
3985 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3986 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
3989 dname = argv[optind];
3992 result = llapi_dir_set_default_lmv(dname, param);
3994 result = llapi_dir_create_param(dname, mode, param);
3998 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
3999 progname, dname, strerror(-result));
4002 dname = argv[++optind];
4003 } while (dname != NULL);
4005 if (mode_opt != NULL)
4006 umask(previous_mode);
4013 static int lfs_rmentry(int argc, char **argv)
4020 fprintf(stderr, "error: %s: missing dirname\n",
4026 dname = argv[index];
4027 while (dname != NULL) {
4028 result = llapi_direntry_remove(dname);
4030 fprintf(stderr, "error: %s: remove dir entry '%s' "
4031 "failed\n", argv[0], dname);
4034 dname = argv[++index];
4039 static int lfs_mv(int argc, char **argv)
4041 struct find_param param = {
4048 struct option long_opts[] = {
4049 { .val = 'M', .name = "mdt-index", .has_arg = required_argument },
4050 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4053 while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
4056 param.fp_mdt_index = strtoul(optarg, &end, 0);
4058 fprintf(stderr, "%s: invalid MDT index'%s'\n",
4065 param.fp_verbose = VERBOSE_DETAIL;
4069 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
4070 argv[0], argv[optind - 1]);
4075 if (param.fp_mdt_index == -1) {
4076 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
4080 if (optind >= argc) {
4081 fprintf(stderr, "%s: missing operand path\n", argv[0]);
4085 param.fp_migrate = 1;
4086 rc = llapi_migrate_mdt(argv[optind], ¶m);
4088 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
4089 argv[0], argv[optind], param.fp_mdt_index,
4094 static int lfs_osts(int argc, char **argv)
4096 return lfs_tgts(argc, argv);
4099 static int lfs_mdts(int argc, char **argv)
4101 return lfs_tgts(argc, argv);
4104 #define COOK(value) \
4107 while (value > 1024) { \
4115 #define CDF "%11llu"
4116 #define HDF "%8.1f%c"
4121 MNTDF_INODES = 0x0001,
4122 MNTDF_COOKED = 0x0002,
4123 MNTDF_LAZY = 0x0004,
4124 MNTDF_VERBOSE = 0x0008,
4127 static int showdf(char *mntdir, struct obd_statfs *stat,
4128 char *uuid, enum mntdf_flags flags,
4129 char *type, int index, int rc)
4131 long long avail, used, total;
4133 char *suffix = "KMGTPEZY";
4134 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4135 char tbuf[3 * sizeof(__u64)];
4136 char ubuf[3 * sizeof(__u64)];
4137 char abuf[3 * sizeof(__u64)];
4138 char rbuf[3 * sizeof(__u64)];
4145 if (flags & MNTDF_INODES) {
4146 avail = stat->os_ffree;
4147 used = stat->os_files - stat->os_ffree;
4148 total = stat->os_files;
4150 int shift = flags & MNTDF_COOKED ? 0 : 10;
4152 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4153 used = ((stat->os_blocks - stat->os_bfree) *
4154 stat->os_bsize) >> shift;
4155 total = (stat->os_blocks * stat->os_bsize) >> shift;
4158 if ((used + avail) > 0)
4159 ratio = (double)used / (double)(used + avail);
4161 if (flags & MNTDF_COOKED) {
4165 cook_val = (double)total;
4168 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4171 snprintf(tbuf, sizeof(tbuf), CDF, total);
4173 cook_val = (double)used;
4176 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4179 snprintf(ubuf, sizeof(ubuf), CDF, used);
4181 cook_val = (double)avail;
4184 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4187 snprintf(abuf, sizeof(abuf), CDF, avail);
4189 snprintf(tbuf, sizeof(tbuf), CDF, total);
4190 snprintf(ubuf, sizeof(tbuf), CDF, used);
4191 snprintf(abuf, sizeof(tbuf), CDF, avail);
4194 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
4195 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4196 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4198 printf("[%s:%d]", type, index);
4200 if (stat->os_state) {
4202 * Each character represents the matching
4205 const char state_names[] = "DRSI";
4210 for (i = 0, state = stat->os_state;
4211 state && i < sizeof(state_names); i++) {
4212 if (!(state & (1 << i)))
4214 printf("%c", state_names[i]);
4222 printf(UUF": inactive device\n", uuid);
4225 printf(UUF": %s\n", uuid, strerror(-rc));
4232 struct ll_stat_type {
4237 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags)
4239 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4240 struct obd_uuid uuid_buf;
4241 char *poolname = NULL;
4242 struct ll_stat_type types[] = {
4243 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4244 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4245 { .st_name = NULL } };
4246 struct ll_stat_type *tp;
4247 __u64 ost_ffree = 0;
4255 poolname = strchr(pool, '.');
4256 if (poolname != NULL) {
4257 if (strncmp(fsname, pool, strlen(fsname))) {
4258 fprintf(stderr, "filesystem name incorrect\n");
4266 fd = open(mntdir, O_RDONLY);
4269 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4274 if (flags & MNTDF_INODES)
4275 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4276 "UUID", "Inodes", "IUsed", "IFree",
4277 "IUse%", "Mounted on");
4279 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4280 "UUID", flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4281 "Used", "Available", "Use%", "Mounted on");
4283 for (tp = types; tp->st_name != NULL; tp++) {
4284 for (index = 0; ; index++) {
4285 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4286 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4287 type = flags & MNTDF_LAZY ?
4288 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4289 rc2 = llapi_obd_fstatfs(fd, type, index,
4290 &stat_buf, &uuid_buf);
4295 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4296 if (!(flags & MNTDF_VERBOSE))
4298 } else if (rc2 < 0 && rc == 0) {
4302 if (poolname && tp->st_op == LL_STATFS_LOV &&
4303 llapi_search_ost(fsname, poolname,
4304 obd_uuid2str(&uuid_buf)) != 1)
4307 /* the llapi_obd_statfs() call may have returned with
4308 * an error, but if it filled in uuid_buf we will at
4309 * lease use that to print out a message for that OBD.
4310 * If we didn't get anything in the uuid_buf, then fill
4311 * it in so that we can print an error message. */
4312 if (uuid_buf.uuid[0] == '\0')
4313 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4314 "%s%04x", tp->st_name, index);
4315 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
4316 flags, tp->st_name, index, rc2);
4319 if (tp->st_op == LL_STATFS_LMV) {
4320 sum.os_ffree += stat_buf.os_ffree;
4321 sum.os_files += stat_buf.os_files;
4322 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4323 sum.os_blocks += stat_buf.os_blocks *
4325 sum.os_bfree += stat_buf.os_bfree *
4327 sum.os_bavail += stat_buf.os_bavail *
4329 ost_ffree += stat_buf.os_ffree;
4337 /* If we don't have as many objects free on the OST as inodes
4338 * on the MDS, we reduce the total number of inodes to
4339 * compensate, so that the "inodes in use" number is correct.
4340 * Matches ll_statfs_internal() so the results are consistent. */
4341 if (ost_ffree < sum.os_ffree) {
4342 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4343 sum.os_ffree = ost_ffree;
4346 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4352 static int lfs_df(int argc, char **argv)
4354 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4355 enum mntdf_flags flags = 0;
4356 int c, rc = 0, index = 0;
4357 char fsname[PATH_MAX] = "", *pool_name = NULL;
4358 struct option long_opts[] = {
4359 { .val = 'h', .name = "human-readable",
4360 .has_arg = no_argument },
4361 { .val = 'i', .name = "inodes", .has_arg = no_argument },
4362 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4363 { .val = 'p', .name = "pool", .has_arg = required_argument },
4364 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4367 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
4370 flags |= MNTDF_COOKED;
4373 flags |= MNTDF_INODES;
4376 flags |= MNTDF_LAZY;
4382 flags |= MNTDF_VERBOSE;
4388 if (optind < argc && !realpath(argv[optind], path)) {
4390 fprintf(stderr, "error: invalid path '%s': %s\n",
4391 argv[optind], strerror(-rc));
4395 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4396 /* Check if we have a mount point */
4397 if (mntdir[0] == '\0')
4400 rc = mntdf(mntdir, fsname, pool_name, flags);
4401 if (rc || path[0] != '\0')
4403 fsname[0] = '\0'; /* avoid matching in next loop */
4404 mntdir[0] = '\0'; /* avoid matching in next loop */
4410 static int lfs_getname(int argc, char **argv)
4412 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
4413 int rc = 0, index = 0, c;
4414 char buf[sizeof(struct obd_uuid)];
4416 while ((c = getopt(argc, argv, "h")) != -1)
4419 if (optind == argc) { /* no paths specified, get all paths. */
4420 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4421 rc = llapi_getname(mntdir, buf, sizeof(buf));
4424 "cannot get name for `%s': %s\n",
4425 mntdir, strerror(-rc));
4429 printf("%s %s\n", buf, mntdir);
4431 path[0] = fsname[0] = mntdir[0] = 0;
4433 } else { /* paths specified, only attempt to search these. */
4434 for (; optind < argc; optind++) {
4435 rc = llapi_getname(argv[optind], buf, sizeof(buf));
4438 "cannot get name for `%s': %s\n",
4439 argv[optind], strerror(-rc));
4443 printf("%s %s\n", buf, argv[optind]);
4449 static int lfs_check(int argc, char **argv)
4452 char mntdir[PATH_MAX] = {'\0'};
4459 fprintf(stderr, "%s check: server type must be specified\n",
4464 obd_types[0] = obd_type1;
4465 obd_types[1] = obd_type2;
4467 if (strcmp(argv[1], "osts") == 0) {
4468 strcpy(obd_types[0], "osc");
4469 } else if (strcmp(argv[1], "mds") == 0) {
4470 strcpy(obd_types[0], "mdc");
4471 } else if (strcmp(argv[1], "servers") == 0) {
4473 strcpy(obd_types[0], "osc");
4474 strcpy(obd_types[1], "mdc");
4476 fprintf(stderr, "%s check: unrecognized option '%s'\n",
4481 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
4482 if (rc < 0 || mntdir[0] == '\0') {
4484 "%s check: cannot find mounted Lustre filesystem: %s\n",
4485 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
4489 rc = llapi_target_check(num_types, obd_types, mntdir);
4491 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
4492 progname, argv[1], strerror(-rc));
4498 #ifdef HAVE_SYS_QUOTA_H
4499 #define ARG2INT(nr, str, msg) \
4502 nr = strtol(str, &endp, 0); \
4503 if (*endp != '\0') { \
4504 fprintf(stderr, "%s: bad %s '%s'\n", \
4505 progname, msg, str); \
4510 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
4512 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
4513 * returns the value or ULONG_MAX on integer overflow or incorrect format
4515 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
4516 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
4517 * 3. empty integer value is interpreted as 0
4519 static unsigned long str2sec(const char* timestr)
4521 const char spec[] = "smhdw";
4522 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4523 unsigned long val = 0;
4526 if (strpbrk(timestr, spec) == NULL) {
4527 /* no specifiers inside the time string,
4528 should treat it as an integer value */
4529 val = strtoul(timestr, &tail, 10);
4530 return *tail ? ULONG_MAX : val;
4533 /* format string is XXwXXdXXhXXmXXs */
4539 v = strtoul(timestr, &tail, 10);
4540 if (v == ULONG_MAX || *tail == '\0')
4541 /* value too large (ULONG_MAX or more)
4542 or missing specifier */
4545 ptr = strchr(spec, *tail);
4547 /* unknown specifier */
4552 /* check if product will overflow the type */
4553 if (!(v < ULONG_MAX / mult[ind]))
4556 ADD_OVERFLOW(val, mult[ind] * v);
4557 if (val == ULONG_MAX)
4569 #define ARG2ULL(nr, str, def_units) \
4571 unsigned long long limit, units = def_units; \
4574 rc = llapi_parse_size(str, &limit, &units, 1); \
4576 fprintf(stderr, "%s: invalid limit '%s'\n", \
4583 static inline int has_times_option(int argc, char **argv)
4587 for (i = 1; i < argc; i++)
4588 if (!strcmp(argv[i], "-t"))
4594 int lfs_setquota_times(int argc, char **argv)
4597 struct if_quotactl qctl;
4598 char *mnt, *obd_type = (char *)qctl.obd_type;
4599 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4600 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
4601 struct option long_opts[] = {
4602 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
4603 { .val = 'g', .name = "group", .has_arg = no_argument },
4604 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
4605 { .val = 'p', .name = "projid", .has_arg = no_argument },
4606 { .val = 't', .name = "times", .has_arg = no_argument },
4607 { .val = 'u', .name = "user", .has_arg = no_argument },
4611 memset(&qctl, 0, sizeof(qctl));
4612 qctl.qc_cmd = LUSTRE_Q_SETINFO;
4613 qctl.qc_type = ALLQUOTA;
4615 while ((c = getopt_long(argc, argv, "b:gi:ptu",
4616 long_opts, NULL)) != -1) {
4627 if (qctl.qc_type != ALLQUOTA) {
4628 fprintf(stderr, "error: -u/g/p can't be used "
4629 "more than once\n");
4632 qctl.qc_type = qtype;
4635 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
4636 fprintf(stderr, "error: bad block-grace: %s\n",
4640 dqb->dqb_valid |= QIF_BTIME;
4643 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
4644 fprintf(stderr, "error: bad inode-grace: %s\n",
4648 dqb->dqb_valid |= QIF_ITIME;
4650 case 't': /* Yes, of course! */
4652 default: /* getopt prints error message for us when opterr != 0 */
4657 if (qctl.qc_type == ALLQUOTA) {
4658 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
4662 if (optind != argc - 1) {
4663 fprintf(stderr, "error: unexpected parameters encountered\n");
4668 rc = llapi_quotactl(mnt, &qctl);
4671 fprintf(stderr, "%s %s ", obd_type,
4672 obd_uuid2str(&qctl.obd_uuid));
4673 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
4680 #define BSLIMIT (1 << 0)
4681 #define BHLIMIT (1 << 1)
4682 #define ISLIMIT (1 << 2)
4683 #define IHLIMIT (1 << 3)
4685 int lfs_setquota(int argc, char **argv)
4688 struct if_quotactl qctl;
4689 char *mnt, *obd_type = (char *)qctl.obd_type;
4690 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4691 struct option long_opts[] = {
4692 { .val = 'b', .name = "block-softlimit",
4693 .has_arg = required_argument },
4694 { .val = 'B', .name = "block-hardlimit",
4695 .has_arg = required_argument },
4696 { .val = 'g', .name = "group", .has_arg = required_argument },
4697 { .val = 'i', .name = "inode-softlimit",
4698 .has_arg = required_argument },
4699 { .val = 'I', .name = "inode-hardlimit",
4700 .has_arg = required_argument },
4701 { .val = 'p', .name = "projid", .has_arg = required_argument },
4702 { .val = 'u', .name = "user", .has_arg = required_argument },
4704 unsigned limit_mask = 0;
4708 if (has_times_option(argc, argv))
4709 return lfs_setquota_times(argc, argv);
4711 memset(&qctl, 0, sizeof(qctl));
4712 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
4713 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
4714 * so it can be used as a marker that qc_type
4715 * isn't reinitialized from command line */
4717 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
4718 long_opts, NULL)) != -1) {
4722 rc = name2uid(&qctl.qc_id, optarg);
4726 rc = name2gid(&qctl.qc_id, optarg);
4730 rc = name2projid(&qctl.qc_id, optarg);
4732 if (qctl.qc_type != ALLQUOTA) {
4734 "%s setquota: only one of -u, -g or -p may be specified\n",
4738 qctl.qc_type = qtype;
4740 qctl.qc_id = strtoul(optarg, &endptr, 10);
4741 if (*endptr != '\0') {
4743 "%s setquota: invalid id '%s'\n",
4750 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
4751 dqb->dqb_bsoftlimit >>= 10;
4752 limit_mask |= BSLIMIT;
4753 if (dqb->dqb_bsoftlimit &&
4754 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
4756 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
4757 "See '%s help setquota' or Lustre manual for details\n",
4758 progname, dqb->dqb_bsoftlimit,
4762 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
4763 dqb->dqb_bhardlimit >>= 10;
4764 limit_mask |= BHLIMIT;
4765 if (dqb->dqb_bhardlimit &&
4766 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
4768 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
4769 "See '%s help setquota' or Lustre manual for details\n",
4770 progname, dqb->dqb_bhardlimit,
4774 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
4775 limit_mask |= ISLIMIT;
4776 if (dqb->dqb_isoftlimit &&
4777 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
4779 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
4780 "See '%s help setquota' or Lustre manual for details\n",
4781 progname, dqb->dqb_isoftlimit,
4785 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
4786 limit_mask |= IHLIMIT;
4787 if (dqb->dqb_ihardlimit &&
4788 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
4790 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
4791 "See '%s help setquota' or Lustre manual for details\n",
4792 progname, dqb->dqb_ihardlimit,
4797 "%s setquota: unrecognized option '%s'\n",
4798 progname, argv[optind - 1]);
4803 if (qctl.qc_type == ALLQUOTA) {
4805 "%s setquota: either -u or -g must be specified\n",
4810 if (limit_mask == 0) {
4812 "%s setquota: at least one limit must be specified\n",
4817 if (optind != argc - 1) {
4819 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
4820 progname, argv[optind]);
4826 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
4827 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
4828 /* sigh, we can't just set blimits/ilimits */
4829 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
4830 .qc_type = qctl.qc_type,
4831 .qc_id = qctl.qc_id};
4833 rc = llapi_quotactl(mnt, &tmp_qctl);
4837 if (!(limit_mask & BHLIMIT))
4838 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
4839 if (!(limit_mask & BSLIMIT))
4840 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
4841 if (!(limit_mask & IHLIMIT))
4842 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
4843 if (!(limit_mask & ISLIMIT))
4844 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
4846 /* Keep grace times if we have got no softlimit arguments */
4847 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
4848 dqb->dqb_valid |= QIF_BTIME;
4849 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
4852 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
4853 dqb->dqb_valid |= QIF_ITIME;
4854 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
4858 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
4859 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
4861 rc = llapi_quotactl(mnt, &qctl);
4865 "%s setquota: cannot quotactl '%s' '%s': %s",
4867 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
4874 /* Converts seconds value into format string
4875 * result is returned in buf
4877 * 1. result is in descenting order: 1w2d3h4m5s
4878 * 2. zero fields are not filled (except for p. 3): 5d1s
4879 * 3. zero seconds value is presented as "0s"
4881 static char * __sec2str(time_t seconds, char *buf)
4883 const char spec[] = "smhdw";
4884 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4889 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
4890 c = seconds / mult[i];
4892 if (c > 0 || (i == 0 && buf == tail))
4893 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
4901 static void sec2str(time_t seconds, char *buf, int rc)
4908 tail = __sec2str(seconds, tail);
4910 if (rc && tail - buf < 39) {
4916 static void diff2str(time_t seconds, char *buf, time_t now)
4922 if (seconds <= now) {
4923 strcpy(buf, "none");
4926 __sec2str(seconds - now, buf);
4929 static void print_quota_title(char *name, struct if_quotactl *qctl,
4930 bool human_readable)
4932 printf("Disk quotas for %s %s (%cid %u):\n",
4933 qtype_name(qctl->qc_type), name,
4934 *qtype_name(qctl->qc_type), qctl->qc_id);
4935 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
4936 "Filesystem", human_readable ? "used" : "kbytes",
4937 "quota", "limit", "grace",
4938 "files", "quota", "limit", "grace");
4941 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
4944 snprintf(buf, buflen, "%ju", (uintmax_t)num);
4947 snprintf(buf, buflen, "%5.4gP",
4948 (double)num / ((__u64)1 << 40));
4950 snprintf(buf, buflen, "%5.4gT",
4951 (double)num / (1 << 30));
4953 snprintf(buf, buflen, "%5.4gG",
4954 (double)num / (1 << 20));
4956 snprintf(buf, buflen, "%5.4gM",
4957 (double)num / (1 << 10));
4959 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
4963 #define STRBUF_LEN 32
4964 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
4971 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
4972 int bover = 0, iover = 0;
4973 struct obd_dqblk *dqb = &qctl->qc_dqblk;
4974 char numbuf[3][STRBUF_LEN];
4976 char strbuf[STRBUF_LEN];
4978 if (dqb->dqb_bhardlimit &&
4979 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
4981 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
4982 if (dqb->dqb_btime > now) {
4989 if (dqb->dqb_ihardlimit &&
4990 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
4992 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
4993 if (dqb->dqb_itime > now) {
5001 if (strlen(mnt) > 15)
5002 printf("%s\n%15s", mnt, "");
5004 printf("%15s", mnt);
5007 diff2str(dqb->dqb_btime, timebuf, now);
5009 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5010 strbuf, sizeof(strbuf), h);
5011 if (rc == -EREMOTEIO)
5012 sprintf(numbuf[0], "%s*", strbuf);
5014 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5015 "%s" : "[%s]", strbuf);
5017 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5018 if (type == QC_GENERAL)
5019 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5020 "%s" : "[%s]", strbuf);
5022 sprintf(numbuf[1], "%s", "-");
5024 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5025 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5026 "%s" : "[%s]", strbuf);
5028 printf(" %7s%c %6s %7s %7s",
5029 numbuf[0], bover ? '*' : ' ', numbuf[1],
5030 numbuf[2], bover > 1 ? timebuf : "-");
5033 diff2str(dqb->dqb_itime, timebuf, now);
5035 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
5036 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
5038 if (type == QC_GENERAL)
5039 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5041 (uintmax_t)dqb->dqb_isoftlimit);
5043 sprintf(numbuf[1], "%s", "-");
5045 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5046 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5048 if (type != QC_OSTIDX)
5049 printf(" %7s%c %6s %7s %7s",
5050 numbuf[0], iover ? '*' : ' ', numbuf[1],
5051 numbuf[2], iover > 1 ? timebuf : "-");
5053 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5056 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5057 qctl->qc_cmd == Q_GETOINFO) {
5061 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5062 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5063 printf("Block grace time: %s; Inode grace time: %s\n",
5064 bgtimebuf, igtimebuf);
5068 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5069 bool h, __u64 *total)
5071 int rc = 0, rc1 = 0, count = 0;
5072 __u32 valid = qctl->qc_valid;
5074 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5076 fprintf(stderr, "can not get %s count: %s\n",
5077 is_mdt ? "mdt": "ost", strerror(-rc));
5081 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5082 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5083 rc = llapi_quotactl(mnt, qctl);
5085 /* It is remote client case. */
5086 if (rc == -EOPNOTSUPP) {
5093 fprintf(stderr, "quotactl %s%d failed.\n",
5094 is_mdt ? "mdt": "ost", qctl->qc_idx);
5098 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5099 qctl->qc_valid, 0, h);
5100 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5101 qctl->qc_dqblk.dqb_bhardlimit;
5104 qctl->qc_valid = valid;
5108 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5109 int verbose, int quiet, bool human_readable)
5111 int rc1 = 0, rc2 = 0, rc3 = 0;
5112 char *obd_type = (char *)qctl->obd_type;
5113 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5114 __u64 total_ialloc = 0, total_balloc = 0;
5117 rc1 = llapi_quotactl(mnt, qctl);
5121 fprintf(stderr, "%s quotas are not enabled.\n",
5122 qtype_name(qctl->qc_type));
5125 fprintf(stderr, "Permission denied.\n");
5128 /* We already got error message. */
5131 fprintf(stderr, "Unexpected quotactl error: %s\n",
5136 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
5137 print_quota_title(name, qctl, human_readable);
5139 if (rc1 && *obd_type)
5140 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5142 if (qctl->qc_valid != QC_GENERAL)
5145 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5146 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5147 (QIF_LIMITS|QIF_USAGE));
5149 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
5151 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
5153 char strbuf[STRBUF_LEN];
5155 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5157 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5159 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5161 printf("Total allocated inode limit: %ju, total "
5162 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5166 if (rc1 || rc2 || rc3 || inacc)
5167 printf("Some errors happened when getting quota info. "
5168 "Some devices may be not working or deactivated. "
5169 "The data in \"[]\" is inaccurate.\n");
5175 static int lfs_project(int argc, char **argv)
5177 int ret = 0, err = 0, c, i;
5178 struct project_handle_control phc = { 0 };
5179 enum lfs_project_ops_t op;
5182 phc.assign_projid = false;
5183 /* default action */
5184 op = LFS_PROJECT_LIST;
5186 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
5189 if (op != LFS_PROJECT_LIST) {
5191 "%s: cannot specify '-c' '-C' '-s' together\n",
5196 op = LFS_PROJECT_CHECK;
5199 if (op != LFS_PROJECT_LIST) {
5201 "%s: cannot specify '-c' '-C' '-s' together\n",
5206 op = LFS_PROJECT_CLEAR;
5209 if (op != LFS_PROJECT_LIST) {
5211 "%s: cannot specify '-c' '-C' '-s' together\n",
5216 phc.set_inherit = true;
5217 op = LFS_PROJECT_SET;
5223 phc.keep_projid = true;
5226 phc.recursive = true;
5229 phc.projid = strtoul(optarg, NULL, 0);
5230 phc.assign_projid = true;
5234 phc.newline = false;
5237 fprintf(stderr, "%s: invalid option '%c'\n",
5243 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
5244 op = LFS_PROJECT_SET;
5245 phc.set_projid = true;
5246 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
5247 phc.set_projid = true;
5251 case LFS_PROJECT_CHECK:
5252 if (phc.keep_projid) {
5254 "%s: '-k' is useless together with '-c'\n",
5259 case LFS_PROJECT_CLEAR:
5262 "%s: '-0' is useless together with '-C'\n",
5266 if (phc.assign_projid) {
5268 "%s: '-p' is useless together with '-C'\n",
5273 case LFS_PROJECT_SET:
5276 "%s: '-0' is useless together with '-s'\n",
5280 if (phc.keep_projid) {
5282 "%s: '-k' is useless together with '-s'\n",
5290 "%s: '-0' is useless for list operations\n",
5300 fprintf(stderr, "%s: missing file or directory target(s)\n",
5305 for (i = 0; i < argc; i++) {
5307 case LFS_PROJECT_CHECK:
5308 err = lfs_project_check(argv[i], &phc);
5310 case LFS_PROJECT_LIST:
5311 err = lfs_project_list(argv[i], &phc);
5313 case LFS_PROJECT_CLEAR:
5314 err = lfs_project_clear(argv[i], &phc);
5316 case LFS_PROJECT_SET:
5317 err = lfs_project_set(argv[i], &phc);
5329 static int lfs_quota(int argc, char **argv)
5332 char *mnt, *name = NULL;
5333 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
5334 .qc_type = ALLQUOTA };
5335 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
5336 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
5338 __u32 valid = QC_GENERAL, idx = 0;
5339 bool human_readable = false;
5342 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
5353 if (qctl.qc_type != ALLQUOTA) {
5355 "%s quota: only one of -u, -g, or -p may be specified\n",
5359 qctl.qc_type = qtype;
5362 qctl.qc_cmd = LUSTRE_Q_GETINFO;
5365 valid = qctl.qc_valid = QC_UUID;
5366 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
5369 valid = qctl.qc_valid = QC_MDTIDX;
5370 idx = qctl.qc_idx = atoi(optarg);
5371 if (idx == 0 && *optarg != '0') {
5373 "%s quota: invalid MDT index '%s'\n",
5379 valid = qctl.qc_valid = QC_OSTIDX;
5380 idx = qctl.qc_idx = atoi(optarg);
5381 if (idx == 0 && *optarg != '0') {
5383 "%s quota: invalid OST index '%s'\n",
5395 human_readable = true;
5398 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
5399 progname, argv[optind - 1]);
5404 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
5405 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
5406 optind == argc - 1) {
5408 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
5409 qctl.qc_valid = valid;
5412 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
5413 qctl.qc_type = qtype;
5414 if (qtype == USRQUOTA) {
5415 qctl.qc_id = geteuid();
5416 rc = uid2name(&name, qctl.qc_id);
5418 qctl.qc_id = getegid();
5419 rc = gid2name(&name, qctl.qc_id);
5424 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
5429 /* lfs quota -u username /path/to/lustre/mount */
5430 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
5431 /* options should be followed by u/g-name and mntpoint */
5432 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
5434 "%s quota: name and mount point must be specified\n",
5439 name = argv[optind++];
5440 switch (qctl.qc_type) {
5442 rc = name2uid(&qctl.qc_id, name);
5445 rc = name2gid(&qctl.qc_id, name);
5448 rc = name2projid(&qctl.qc_id, name);
5455 qctl.qc_id = strtoul(name, &endptr, 10);
5456 if (*endptr != '\0') {
5457 fprintf(stderr, "%s quota: invalid id '%s'\n",
5463 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
5465 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
5466 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
5473 #endif /* HAVE_SYS_QUOTA_H! */
5475 static int flushctx_ioctl(char *mp)
5479 fd = open(mp, O_RDONLY);
5481 fprintf(stderr, "flushctx: error open %s: %s\n",
5482 mp, strerror(errno));
5486 rc = ioctl(fd, LL_IOC_FLUSHCTX);
5488 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
5489 mp, strerror(errno));
5495 static int lfs_flushctx(int argc, char **argv)
5497 int kdestroy = 0, c;
5498 char mntdir[PATH_MAX] = {'\0'};
5502 while ((c = getopt(argc, argv, "k")) != -1) {
5508 fprintf(stderr, "error: %s: option '-%c' "
5509 "unrecognized\n", argv[0], c);
5515 if ((rc = system("kdestroy > /dev/null")) != 0) {
5516 rc = WEXITSTATUS(rc);
5517 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
5521 if (optind >= argc) {
5522 /* flush for all mounted lustre fs. */
5523 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
5524 /* Check if we have a mount point */
5525 if (mntdir[0] == '\0')
5528 if (flushctx_ioctl(mntdir))
5531 mntdir[0] = '\0'; /* avoid matching in next loop */
5534 /* flush fs as specified */
5535 while (optind < argc) {
5536 if (flushctx_ioctl(argv[optind++]))
5543 static int lfs_cp(int argc, char **argv)
5545 fprintf(stderr, "remote client copy file(s).\n"
5546 "obsolete, does not support it anymore.\n");
5550 static int lfs_ls(int argc, char **argv)
5552 fprintf(stderr, "remote client lists directory contents.\n"
5553 "obsolete, does not support it anymore.\n");
5557 static int lfs_changelog(int argc, char **argv)
5559 void *changelog_priv;
5560 struct changelog_rec *rec;
5561 long long startrec = 0, endrec = 0;
5563 struct option long_opts[] = {
5564 { .val = 'f', .name = "follow", .has_arg = no_argument },
5566 char short_opts[] = "f";
5569 while ((rc = getopt_long(argc, argv, short_opts,
5570 long_opts, NULL)) != -1) {
5578 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5579 argv[0], argv[optind - 1]);
5586 mdd = argv[optind++];
5588 startrec = strtoll(argv[optind++], NULL, 10);
5590 endrec = strtoll(argv[optind++], NULL, 10);
5592 rc = llapi_changelog_start(&changelog_priv,
5593 CHANGELOG_FLAG_BLOCK |
5594 CHANGELOG_FLAG_JOBID |
5595 CHANGELOG_FLAG_EXTRA_FLAGS |
5596 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
5599 fprintf(stderr, "Can't start changelog: %s\n",
5600 strerror(errno = -rc));
5604 rc = llapi_changelog_set_xflags(changelog_priv,
5605 CHANGELOG_EXTRA_FLAG_UIDGID |
5606 CHANGELOG_EXTRA_FLAG_NID);
5608 fprintf(stderr, "Can't set xflags for changelog: %s\n",
5609 strerror(errno = -rc));
5613 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
5617 if (endrec && rec->cr_index > endrec) {
5618 llapi_changelog_free(&rec);
5621 if (rec->cr_index < startrec) {
5622 llapi_changelog_free(&rec);
5626 secs = rec->cr_time >> 30;
5627 gmtime_r(&secs, &ts);
5628 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
5629 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
5630 changelog_type2str(rec->cr_type),
5631 ts.tm_hour, ts.tm_min, ts.tm_sec,
5632 (int)(rec->cr_time & ((1 << 30) - 1)),
5633 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
5634 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
5636 if (rec->cr_flags & CLF_JOBID) {
5637 struct changelog_ext_jobid *jid =
5638 changelog_rec_jobid(rec);
5640 if (jid->cr_jobid[0] != '\0')
5641 printf(" j=%s", jid->cr_jobid);
5644 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
5645 struct changelog_ext_extra_flags *ef =
5646 changelog_rec_extra_flags(rec);
5648 printf(" ef=0x%llx", ef->cr_extra_flags);
5650 if (ef->cr_extra_flags & CLFE_UIDGID) {
5651 struct changelog_ext_uidgid *uidgid =
5652 changelog_rec_uidgid(rec);
5654 printf(" u=%llu:%llu",
5655 uidgid->cr_uid, uidgid->cr_gid);
5657 if (ef->cr_extra_flags & CLFE_NID) {
5658 struct changelog_ext_nid *nid =
5659 changelog_rec_nid(rec);
5662 libcfs_nid2str(nid->cr_nid));
5666 if (rec->cr_namelen)
5667 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
5668 rec->cr_namelen, changelog_rec_name(rec));
5670 if (rec->cr_flags & CLF_RENAME) {
5671 struct changelog_ext_rename *rnm =
5672 changelog_rec_rename(rec);
5674 if (!fid_is_zero(&rnm->cr_sfid))
5675 printf(" s="DFID" sp="DFID" %.*s",
5676 PFID(&rnm->cr_sfid),
5677 PFID(&rnm->cr_spfid),
5678 (int)changelog_rec_snamelen(rec),
5679 changelog_rec_sname(rec));
5683 llapi_changelog_free(&rec);
5686 llapi_changelog_fini(&changelog_priv);
5689 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
5691 return (rc == 1 ? 0 : rc);
5694 static int lfs_changelog_clear(int argc, char **argv)
5702 endrec = strtoll(argv[3], NULL, 10);
5704 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
5707 fprintf(stderr, "%s: record out of range: %llu\n",
5709 else if (rc == -ENOENT)
5710 fprintf(stderr, "%s: no changelog user: %s\n",
5713 fprintf(stderr, "%s error: %s\n", argv[0],
5722 static int lfs_fid2path(int argc, char **argv)
5724 struct option long_opts[] = {
5725 { .val = 'c', .name = "cur", .has_arg = no_argument },
5726 { .val = 'l', .name = "link", .has_arg = required_argument },
5727 { .val = 'r', .name = "rec", .has_arg = required_argument },
5729 char short_opts[] = "cl:r:";
5730 char *device, *fid, *path;
5731 long long recno = -1;
5737 while ((rc = getopt_long(argc, argv, short_opts,
5738 long_opts, NULL)) != -1) {
5744 linkno = strtol(optarg, NULL, 10);
5747 recno = strtoll(optarg, NULL, 10);
5752 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5753 argv[0], argv[optind - 1]);
5761 device = argv[optind++];
5762 path = calloc(1, PATH_MAX);
5764 fprintf(stderr, "error: Not enough memory\n");
5769 while (optind < argc) {
5770 fid = argv[optind++];
5772 lnktmp = (linkno >= 0) ? linkno : 0;
5774 int oldtmp = lnktmp;
5775 long long rectmp = recno;
5777 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
5780 fprintf(stderr, "%s: error on FID %s: %s\n",
5781 argv[0], fid, strerror(errno = -rc2));
5788 fprintf(stdout, "%lld ", rectmp);
5789 if (device[0] == '/') {
5790 fprintf(stdout, "%s", device);
5791 if (device[strlen(device) - 1] != '/')
5792 fprintf(stdout, "/");
5793 } else if (path[0] == '\0') {
5794 fprintf(stdout, "/");
5796 fprintf(stdout, "%s\n", path);
5799 /* specified linkno */
5801 if (oldtmp == lnktmp)
5811 static int lfs_path2fid(int argc, char **argv)
5813 struct option long_opts[] = {
5814 { .val = 'p', .name = "parents", .has_arg = no_argument },
5817 const char short_opts[] = "p";
5818 const char *sep = "";
5821 bool show_parents = false;
5823 while ((rc = getopt_long(argc, argv, short_opts,
5824 long_opts, NULL)) != -1) {
5827 show_parents = true;
5830 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5831 argv[0], argv[optind - 1]);
5836 if (optind > argc - 1)
5838 else if (optind < argc - 1)
5842 for (path = argv + optind; *path != NULL; path++) {
5844 if (!show_parents) {
5845 err = llapi_path2fid(*path, &fid);
5847 printf("%s%s"DFID"\n",
5848 *sep != '\0' ? *path : "", sep,
5851 char name[NAME_MAX + 1];
5852 unsigned int linkno = 0;
5854 while ((err = llapi_path2parent(*path, linkno, &fid,
5855 name, sizeof(name))) == 0) {
5856 if (*sep != '\0' && linkno == 0)
5857 printf("%s%s", *path, sep);
5859 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
5864 /* err == -ENODATA is end-of-loop */
5865 if (linkno > 0 && err == -ENODATA) {
5872 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
5873 argv[0], show_parents ? "parent " : "", *path,
5885 static int lfs_data_version(int argc, char **argv)
5892 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
5897 while ((c = getopt(argc, argv, "nrw")) != -1) {
5900 data_version_flags = 0;
5903 data_version_flags |= LL_DV_RD_FLUSH;
5906 data_version_flags |= LL_DV_WR_FLUSH;
5915 path = argv[optind];
5916 fd = open(path, O_RDONLY);
5918 err(errno, "cannot open file %s", path);
5920 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
5922 err(errno, "cannot get version for %s", path);
5924 printf("%ju" "\n", (uintmax_t)data_version);
5930 static int lfs_hsm_state(int argc, char **argv)
5935 struct hsm_user_state hus;
5943 rc = llapi_hsm_state_get(path, &hus);
5945 fprintf(stderr, "can't get hsm state for %s: %s\n",
5946 path, strerror(errno = -rc));
5950 /* Display path name and status flags */
5951 printf("%s: (0x%08x)", path, hus.hus_states);
5953 if (hus.hus_states & HS_RELEASED)
5954 printf(" released");
5955 if (hus.hus_states & HS_EXISTS)
5957 if (hus.hus_states & HS_DIRTY)
5959 if (hus.hus_states & HS_ARCHIVED)
5960 printf(" archived");
5961 /* Display user-settable flags */
5962 if (hus.hus_states & HS_NORELEASE)
5963 printf(" never_release");
5964 if (hus.hus_states & HS_NOARCHIVE)
5965 printf(" never_archive");
5966 if (hus.hus_states & HS_LOST)
5967 printf(" lost_from_hsm");
5969 if (hus.hus_archive_id != 0)
5970 printf(", archive_id:%d", hus.hus_archive_id);
5973 } while (++i < argc);
5978 #define LFS_HSM_SET 0
5979 #define LFS_HSM_CLEAR 1
5982 * Generic function to set or clear HSM flags.
5983 * Used by hsm_set and hsm_clear.
5985 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
5987 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
5989 struct option long_opts[] = {
5990 { .val = 'A', .name = "archived", .has_arg = no_argument },
5991 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
5992 { .val = 'd', .name = "dirty", .has_arg = no_argument },
5993 { .val = 'e', .name = "exists", .has_arg = no_argument },
5994 { .val = 'l', .name = "lost", .has_arg = no_argument },
5995 { .val = 'r', .name = "norelease", .has_arg = no_argument },
5996 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
5998 char short_opts[] = "lraAdei:";
6002 __u32 archive_id = 0;
6008 while ((c = getopt_long(argc, argv, short_opts,
6009 long_opts, NULL)) != -1) {
6015 mask |= HS_NOARCHIVE;
6018 mask |= HS_ARCHIVED;
6021 mask |= HS_NORELEASE;
6030 archive_id = strtol(optarg, &end, 10);
6032 fprintf(stderr, "invalid archive_id: '%s'\n",
6040 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6041 argv[0], argv[optind - 1]);
6046 /* User should have specified a flag */
6050 while (optind < argc) {
6052 path = argv[optind];
6054 /* If mode == 0, this means we apply the mask. */
6055 if (mode == LFS_HSM_SET)
6056 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6058 rc = llapi_hsm_state_set(path, 0, mask, 0);
6061 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6062 path, strerror(errno = -rc));
6071 static int lfs_hsm_action(int argc, char **argv)
6076 struct hsm_current_action hca;
6077 struct hsm_extent he;
6078 enum hsm_user_action hua;
6079 enum hsm_progress_states hps;
6087 rc = llapi_hsm_current_action(path, &hca);
6089 fprintf(stderr, "can't get hsm action for %s: %s\n",
6090 path, strerror(errno = -rc));
6093 he = hca.hca_location;
6094 hua = hca.hca_action;
6095 hps = hca.hca_state;
6097 printf("%s: %s", path, hsm_user_action2name(hua));
6099 /* Skip file without action */
6100 if (hca.hca_action == HUA_NONE) {
6105 printf(" %s ", hsm_progress_state2name(hps));
6107 if ((hps == HPS_RUNNING) &&
6108 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
6109 printf("(%llu bytes moved)\n",
6110 (unsigned long long)he.length);
6111 else if ((he.offset + he.length) == LUSTRE_EOF)
6112 printf("(from %llu to EOF)\n",
6113 (unsigned long long)he.offset);
6115 printf("(from %llu to %llu)\n",
6116 (unsigned long long)he.offset,
6117 (unsigned long long)(he.offset + he.length));
6119 } while (++i < argc);
6124 static int lfs_hsm_set(int argc, char **argv)
6126 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
6129 static int lfs_hsm_clear(int argc, char **argv)
6131 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
6135 * Check file state and return its fid, to be used by lfs_hsm_request().
6137 * \param[in] file Path to file to check
6138 * \param[in,out] fid Pointer to allocated lu_fid struct.
6139 * \param[in,out] last_dev Pointer to last device id used.
6141 * \return 0 on success.
6143 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
6149 rc = lstat(file, &st);
6151 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
6154 /* Checking for regular file as archiving as posix copytool
6155 * rejects archiving files other than regular files
6157 if (!S_ISREG(st.st_mode)) {
6158 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
6161 /* A request should be ... */
6162 if (*last_dev != st.st_dev && *last_dev != 0) {
6163 fprintf(stderr, "All files should be "
6164 "on the same filesystem: %s\n", file);
6167 *last_dev = st.st_dev;
6169 rc = llapi_path2fid(file, fid);
6171 fprintf(stderr, "Cannot read FID of %s: %s\n",
6172 file, strerror(-rc));
6178 /* Fill an HSM HUR item with a given file name.
6180 * If mntpath is set, then the filename is actually a FID, and no
6181 * lookup on the filesystem will be performed.
6183 * \param[in] hur the user request to fill
6184 * \param[in] idx index of the item inside the HUR to fill
6185 * \param[in] mntpath mountpoint of Lustre
6186 * \param[in] fname filename (if mtnpath is NULL)
6187 * or FID (if mntpath is set)
6188 * \param[in] last_dev pointer to last device id used
6190 * \retval 0 on success
6191 * \retval CMD_HELP or a negative errno on error
6193 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
6194 const char *mntpath, const char *fname,
6197 struct hsm_user_item *hui = &hur->hur_user_item[idx];
6200 hui->hui_extent.length = -1;
6202 if (mntpath != NULL) {
6205 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
6209 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
6214 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
6218 hur->hur_request.hr_itemcount++;
6223 static int lfs_hsm_request(int argc, char **argv, int action)
6225 struct option long_opts[] = {
6226 { .val = 'a', .name = "archive", .has_arg = required_argument },
6227 { .val = 'D', .name = "data", .has_arg = required_argument },
6228 { .val = 'l', .name = "filelist", .has_arg = required_argument },
6229 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
6232 char short_opts[] = "l:D:a:m:";
6233 struct hsm_user_request *hur, *oldhur;
6238 char *filelist = NULL;
6239 char fullpath[PATH_MAX];
6240 char *opaque = NULL;
6244 int nbfile_alloc = 0;
6245 char *some_file = NULL;
6246 char *mntpath = NULL;
6252 while ((c = getopt_long(argc, argv, short_opts,
6253 long_opts, NULL)) != -1) {
6262 if (action != HUA_ARCHIVE &&
6263 action != HUA_REMOVE) {
6265 "error: -a is supported only "
6266 "when archiving or removing\n");
6269 archive_id = atoi(optarg);
6272 if (some_file == NULL) {
6274 some_file = strdup(optarg);
6280 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6281 argv[0], argv[optind - 1]);
6286 /* All remaining args are files, so we have at least nbfile */
6287 nbfile = argc - optind;
6289 if ((nbfile == 0) && (filelist == NULL))
6293 opaque_len = strlen(opaque);
6295 /* Alloc the request structure with enough place to store all files
6296 * from command line. */
6297 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
6299 fprintf(stderr, "Cannot create the request: %s\n",
6303 nbfile_alloc = nbfile;
6305 hur->hur_request.hr_action = action;
6306 hur->hur_request.hr_archive_id = archive_id;
6307 hur->hur_request.hr_flags = 0;
6309 /* All remaining args are files, add them */
6310 if (nbfile != 0 && some_file == NULL)
6311 some_file = strdup(argv[optind]);
6313 for (i = 0; i < nbfile; i++) {
6314 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
6320 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
6322 /* If a filelist was specified, read the filelist from it. */
6323 if (filelist != NULL) {
6324 fp = fopen(filelist, "r");
6326 fprintf(stderr, "Cannot read the file list %s: %s\n",
6327 filelist, strerror(errno));
6332 while ((rc = getline(&line, &len, fp)) != -1) {
6333 /* If allocated buffer was too small, get something
6335 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
6338 nbfile_alloc = nbfile_alloc * 2 + 1;
6340 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
6343 fprintf(stderr, "hsm: cannot allocate "
6344 "the request: %s\n",
6351 size = hur_len(oldhur);
6353 fprintf(stderr, "hsm: cannot allocate "
6354 "%u files + %u bytes data\n",
6355 oldhur->hur_request.hr_itemcount,
6356 oldhur->hur_request.hr_data_len);
6363 memcpy(hur, oldhur, size);
6368 if (line[strlen(line) - 1] == '\n')
6369 line[strlen(line) - 1] = '\0';
6371 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
6372 mntpath, line, &last_dev);
6378 if (some_file == NULL) {
6388 /* If a --data was used, add it to the request */
6389 hur->hur_request.hr_data_len = opaque_len;
6391 memcpy(hur_data(hur), opaque, opaque_len);
6393 /* Send the HSM request */
6394 if (realpath(some_file, fullpath) == NULL) {
6395 fprintf(stderr, "Could not find path '%s': %s\n",
6396 some_file, strerror(errno));
6398 rc = llapi_hsm_request(fullpath, hur);
6400 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
6401 some_file, strerror(-rc));
6411 static int lfs_hsm_archive(int argc, char **argv)
6413 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
6416 static int lfs_hsm_restore(int argc, char **argv)
6418 return lfs_hsm_request(argc, argv, HUA_RESTORE);
6421 static int lfs_hsm_release(int argc, char **argv)
6423 return lfs_hsm_request(argc, argv, HUA_RELEASE);
6426 static int lfs_hsm_remove(int argc, char **argv)
6428 return lfs_hsm_request(argc, argv, HUA_REMOVE);
6431 static int lfs_hsm_cancel(int argc, char **argv)
6433 return lfs_hsm_request(argc, argv, HUA_CANCEL);
6436 static int lfs_swap_layouts(int argc, char **argv)
6441 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
6442 SWAP_LAYOUTS_KEEP_MTIME |
6443 SWAP_LAYOUTS_KEEP_ATIME);
6446 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
6448 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
6450 int lfs_get_mode(const char *string)
6452 enum lock_mode_user mode;
6454 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
6455 if (lock_mode_names[mode] == NULL)
6457 if (strcmp(string, lock_mode_names[mode]) == 0)
6464 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
6466 enum lu_ladvise_type advice;
6469 advice < ARRAY_SIZE(ladvise_names); advice++) {
6470 if (ladvise_names[advice] == NULL)
6472 if (strcmp(string, ladvise_names[advice]) == 0)
6476 return LU_LADVISE_INVALID;
6479 static int lfs_ladvise(int argc, char **argv)
6481 struct option long_opts[] = {
6482 { .val = 'a', .name = "advice", .has_arg = required_argument },
6483 { .val = 'b', .name = "background", .has_arg = no_argument },
6484 { .val = 'e', .name = "end", .has_arg = required_argument },
6485 { .val = 'l', .name = "length", .has_arg = required_argument },
6486 { .val = 'm', .name = "mode", .has_arg = required_argument },
6487 { .val = 's', .name = "start", .has_arg = required_argument },
6488 { .val = 'u', .name = "unset", .has_arg = no_argument },
6490 char short_opts[] = "a:be:l:m:s:u";
6495 struct llapi_lu_ladvise advice;
6496 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
6497 unsigned long long start = 0;
6498 unsigned long long end = LUSTRE_EOF;
6499 unsigned long long length = 0;
6500 unsigned long long size_units;
6501 unsigned long long flags = 0;
6505 while ((c = getopt_long(argc, argv, short_opts,
6506 long_opts, NULL)) != -1) {
6509 advice_type = lfs_get_ladvice(optarg);
6510 if (advice_type == LU_LADVISE_INVALID) {
6511 fprintf(stderr, "%s: invalid advice type "
6512 "'%s'\n", argv[0], optarg);
6513 fprintf(stderr, "Valid types:");
6515 for (advice_type = 0;
6516 advice_type < ARRAY_SIZE(ladvise_names);
6518 if (ladvise_names[advice_type] == NULL)
6520 fprintf(stderr, " %s",
6521 ladvise_names[advice_type]);
6523 fprintf(stderr, "\n");
6536 rc = llapi_parse_size(optarg, &end,
6539 fprintf(stderr, "%s: bad end offset '%s'\n",
6546 rc = llapi_parse_size(optarg, &start,
6549 fprintf(stderr, "%s: bad start offset "
6550 "'%s'\n", argv[0], optarg);
6556 rc = llapi_parse_size(optarg, &length,
6559 fprintf(stderr, "%s: bad length '%s'\n",
6565 mode = lfs_get_mode(optarg);
6567 fprintf(stderr, "%s: bad mode '%s', valid "
6568 "modes are READ or WRITE\n",
6576 fprintf(stderr, "%s: option '%s' unrecognized\n",
6577 argv[0], argv[optind - 1]);
6582 if (advice_type == LU_LADVISE_INVALID) {
6583 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
6584 fprintf(stderr, "Valid types:");
6585 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
6587 if (ladvise_names[advice_type] == NULL)
6589 fprintf(stderr, " %s", ladvise_names[advice_type]);
6591 fprintf(stderr, "\n");
6595 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
6596 fprintf(stderr, "%s: Lock no expand advice is a per file "
6597 "descriptor advice, so when called from lfs, "
6598 "it does nothing.\n", argv[0]);
6602 if (argc <= optind) {
6603 fprintf(stderr, "%s: please give one or more file names\n",
6608 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
6609 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
6614 if (end == LUSTRE_EOF && length != 0)
6615 end = start + length;
6618 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
6619 argv[0], start, end);
6623 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
6624 fprintf(stderr, "%s: mode is only valid with lockahead\n",
6629 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
6630 fprintf(stderr, "%s: mode is required with lockahead\n",
6635 while (optind < argc) {
6638 path = argv[optind++];
6640 fd = open(path, O_RDONLY);
6642 fprintf(stderr, "%s: cannot open file '%s': %s\n",
6643 argv[0], path, strerror(errno));
6648 advice.lla_start = start;
6649 advice.lla_end = end;
6650 advice.lla_advice = advice_type;
6651 advice.lla_value1 = 0;
6652 advice.lla_value2 = 0;
6653 advice.lla_value3 = 0;
6654 advice.lla_value4 = 0;
6655 if (advice_type == LU_LADVISE_LOCKAHEAD) {
6656 advice.lla_lockahead_mode = mode;
6657 advice.lla_peradvice_flags = flags;
6660 rc2 = llapi_ladvise(fd, flags, 1, &advice);
6663 fprintf(stderr, "%s: cannot give advice '%s' to file "
6664 "'%s': %s\n", argv[0],
6665 ladvise_names[advice_type],
6666 path, strerror(errno));
6672 if (rc == 0 && rc2 < 0)
6678 /** The input string contains a comma delimited list of component ids and
6679 * ranges, for example "1,2-4,7".
6681 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
6683 bool end_of_loop = false;
6691 while (!end_of_loop) {
6695 char *endptr = NULL;
6698 ptr = strchrnul(arg, ',');
6699 end_of_loop = *ptr == '\0';
6702 start_index = strtol(arg, &endptr, 0);
6703 if (endptr == arg) /* no data at all */
6705 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
6707 if (start_index < 0)
6710 end_index = start_index;
6711 if (*endptr == '-') {
6712 end_index = strtol(endptr + 1, &endptr, 0);
6713 if (*endptr != '\0')
6715 if (end_index < start_index)
6719 for (i = start_index; i <= end_index && size > 0; i++) {
6722 /* remove duplicate */
6723 for (j = 0; j < nr; j++) {
6727 if (j == nr) { /* no duplicate */
6733 if (size == 0 && i < end_index)
6740 if (!end_of_loop && ptr != NULL)
6743 return rc < 0 ? rc : nr;
6747 * struct verify_mirror_id - Mirror id to be verified.
6748 * @mirror_id: A specified mirror id.
6749 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
6751 struct verify_mirror_id {
6757 * compare_mirror_ids() - Compare mirror ids.
6758 * @layout: Mirror component list.
6759 * @cbdata: Callback data in verify_mirror_id structure.
6761 * This is a callback function called by llapi_layout_comp_iterate()
6762 * to compare the specified mirror id with the one in the current
6763 * component of @layout. If they are the same, then the specified
6764 * mirror id is valid.
6766 * Return: a negative error code on failure or
6767 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
6768 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
6771 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
6773 struct verify_mirror_id *mirror_id_cbdata =
6774 (struct verify_mirror_id *)cbdata;
6778 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
6782 "%s: llapi_layout_mirror_id_get failed: %s.\n",
6783 progname, strerror(errno));
6787 if (mirror_id_cbdata->mirror_id == mirror_id) {
6788 mirror_id_cbdata->is_valid_id = true;
6789 return LLAPI_LAYOUT_ITER_STOP;
6792 return LLAPI_LAYOUT_ITER_CONT;
6796 * verify_mirror_ids() - Verify specified mirror ids.
6797 * @fname: Mirrored file name.
6798 * @mirror_ids: Specified mirror ids to be verified.
6799 * @ids_nr: Number of specified mirror ids.
6801 * This function verifies that specified @mirror_ids are valid
6802 * in the mirrored file @fname.
6804 * Return: 0 on success or a negative error code on failure.
6807 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
6809 struct llapi_layout *layout = NULL;
6810 struct verify_mirror_id mirror_id_cbdata = { 0 };
6821 if (stat(fname, &stbuf) < 0) {
6822 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
6823 progname, fname, strerror(errno));
6828 if (!S_ISREG(stbuf.st_mode)) {
6829 fprintf(stderr, "%s: '%s' is not a regular file.\n",
6835 fd = open(fname, O_DIRECT | O_RDONLY);
6837 fprintf(stderr, "%s: cannot open '%s': %s.\n",
6838 progname, fname, strerror(errno));
6843 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
6845 fprintf(stderr, "%s: '%s' llapi_lease_get failed: %s.\n",
6846 progname, fname, strerror(errno));
6850 layout = llapi_layout_get_by_fd(fd, 0);
6851 if (layout == NULL) {
6852 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
6853 progname, fname, strerror(errno));
6855 llapi_lease_put(fd);
6859 rc = llapi_layout_flags_get(layout, &flr_state);
6861 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
6862 progname, fname, strerror(errno));
6867 flr_state &= LCM_FL_FLR_MASK;
6868 switch (flr_state) {
6869 case LCM_FL_NOT_FLR:
6871 fprintf(stderr, "%s: '%s' file state error: %s.\n",
6872 progname, fname, lcm_flags_string(flr_state));
6879 for (i = 0; i < ids_nr; i++) {
6880 mirror_id_cbdata.mirror_id = mirror_ids[i];
6881 mirror_id_cbdata.is_valid_id = false;
6883 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
6888 "%s: '%s' failed to verify mirror id: %u.\n",
6889 progname, fname, mirror_ids[i]);
6893 if (!mirror_id_cbdata.is_valid_id) {
6896 "%s: '%s' invalid specified mirror id: %u.\n",
6897 progname, fname, mirror_ids[i]);
6903 llapi_layout_free(layout);
6904 llapi_lease_put(fd);
6912 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
6913 __u16 *mirror_ids, int ids_nr)
6915 const char *progname = "lfs mirror resync";
6916 struct llapi_resync_comp comp_array[1024] = { { 0 } };
6917 struct llapi_layout *layout;
6925 if (stat(fname, &stbuf) < 0) {
6926 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
6927 progname, fname, strerror(errno));
6931 if (!S_ISREG(stbuf.st_mode)) {
6932 fprintf(stderr, "%s: '%s' is not a regular file.\n",
6938 fd = open(fname, O_DIRECT | O_RDWR);
6940 fprintf(stderr, "%s: cannot open '%s': %s.\n",
6941 progname, fname, strerror(errno));
6946 ioc->lil_mode = LL_LEASE_WRLCK;
6947 ioc->lil_flags = LL_LEASE_RESYNC;
6948 rc = llapi_lease_set(fd, ioc);
6950 fprintf(stderr, "%s: '%s' llapi_lease_set resync failed: "
6951 "%s.\n", progname, fname, strerror(errno));
6955 layout = llapi_layout_get_by_fd(fd, 0);
6956 if (layout == NULL) {
6957 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
6958 progname, fname, strerror(errno));
6963 rc = llapi_layout_flags_get(layout, &flr_state);
6965 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
6966 progname, fname, strerror(errno));
6971 flr_state &= LCM_FL_FLR_MASK;
6972 switch (flr_state) {
6973 case LCM_FL_NOT_FLR:
6976 fprintf(stderr, "%s: '%s' file state error: %s.\n",
6977 progname, fname, lcm_flags_string(flr_state));
6983 /* get stale component info */
6984 comp_size = llapi_mirror_find_stale(layout, comp_array,
6985 ARRAY_SIZE(comp_array),
6986 mirror_ids, ids_nr);
6987 if (comp_size < 0) {
6993 while (idx < comp_size) {
6999 rc = llapi_lease_check(fd);
7000 if (rc != LL_LEASE_WRLCK) {
7001 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7006 mirror_id = comp_array[idx].lrc_mirror_id;
7007 end = comp_array[idx].lrc_end;
7009 /* try to combine adjacent component */
7010 for (i = idx + 1; i < comp_size; i++) {
7011 if (mirror_id != comp_array[i].lrc_mirror_id ||
7012 end != comp_array[i].lrc_start)
7014 end = comp_array[i].lrc_end;
7017 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7018 comp_array[idx].lrc_start,
7021 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7022 "%ld.\n", progname, fname, result);
7025 } else if (result > 0) {
7028 /* mark synced components */
7029 for (j = idx; j < i; j++)
7030 comp_array[j].lrc_synced = true;
7036 /* prepare ioc for lease put */
7037 ioc->lil_mode = LL_LEASE_UNLCK;
7038 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7040 for (idx = 0; idx < comp_size; idx++) {
7041 if (comp_array[idx].lrc_synced) {
7042 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7047 llapi_layout_free(layout);
7049 rc = llapi_lease_set(fd, ioc);
7051 if (rc == 0) /* lost lease lock */
7053 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7054 progname, fname, strerror(errno));
7058 * llapi_lease_set returns lease mode when it request to unlock
7069 static inline int lfs_mirror_resync(int argc, char **argv)
7071 struct ll_ioc_lease *ioc = NULL;
7072 __u16 mirror_ids[128] = { 0 };
7077 struct option long_opts[] = {
7078 { .val = 'o', .name = "only", .has_arg = required_argument },
7081 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
7084 rc = parse_mirror_ids(mirror_ids,
7085 sizeof(mirror_ids) / sizeof(__u16),
7089 "%s: bad mirror ids '%s'.\n",
7096 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7097 argv[0], argv[optind - 1]);
7103 if (argc == optind) {
7104 fprintf(stderr, "%s: no file name given.\n", argv[0]);
7109 if (ids_nr > 0 && argc > optind + 1) {
7110 fprintf(stderr, "%s: option '--only' cannot be used upon "
7111 "multiple files.\n", argv[0]);
7118 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7123 /* set the lease on the file */
7124 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
7126 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
7127 argv[0], strerror(errno));
7132 for (; optind < argc; optind++) {
7133 rc = lfs_mirror_resync_file(argv[optind], ioc,
7134 mirror_ids, ids_nr);
7136 fprintf(stderr, "%s: resync file '%s' failed: %d\n",
7137 argv[0], argv[optind], rc);
7138 /* ignore previous file's error, continue with next file */
7141 memset(ioc, 0, sizeof(__u32) * 4096);
7150 * struct verify_chunk - Mirror chunk to be verified.
7151 * @chunk: [start, end) of the chunk.
7152 * @mirror_count: Number of mirror ids in @mirror_id array.
7153 * @mirror_id: Array of valid mirror ids that cover the chunk.
7155 struct verify_chunk {
7156 struct lu_extent chunk;
7157 unsigned int mirror_count;
7158 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
7162 * print_chunks() - Print chunk information.
7163 * @fname: Mirrored file name.
7164 * @chunks: Array of chunks.
7165 * @chunk_count: Number of chunks in @chunks array.
7167 * This function prints [start, end) of each chunk in @chunks
7168 * for mirrored file @fname, and also prints the valid mirror ids
7169 * that cover the chunk.
7174 void print_chunks(const char *fname, struct verify_chunk *chunks,
7180 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
7181 for (i = 0; i < chunk_count; i++) {
7182 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
7184 if (chunks[i].mirror_count == 0)
7185 fprintf(stdout, "\t[");
7187 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
7188 for (j = 1; j < chunks[i].mirror_count; j++)
7189 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
7191 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
7193 fprintf(stdout, "\n");
7197 * print_checksums() - Print CRC-32 checksum values.
7198 * @chunk: A chunk and its corresponding valid mirror ids.
7199 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
7201 * This function prints CRC-32 checksum values on @chunk for
7202 * each valid mirror that covers it.
7207 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
7212 "CRC-32 checksum value for chunk "DEXT":\n",
7213 PEXT(&chunk->chunk));
7214 for (i = 0; i < chunk->mirror_count; i++)
7215 fprintf(stdout, "Mirror %u:\t%#lx\n",
7216 chunk->mirror_id[i], crc[i]);
7217 fprintf(stdout, "\n");
7221 * filter_mirror_id() - Filter specified mirror ids.
7222 * @chunks: Array of chunks.
7223 * @chunk_count: Number of chunks in @chunks array.
7224 * @mirror_ids: Specified mirror ids to be verified.
7225 * @ids_nr: Number of specified mirror ids.
7227 * This function scans valid mirror ids that cover each chunk in @chunks
7228 * and filters specified mirror ids.
7233 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
7234 __u16 *mirror_ids, int ids_nr)
7239 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7240 unsigned int valid_count = 0;
7242 for (i = 0; i < chunk_count; i++) {
7243 if (chunks[i].mirror_count == 0)
7247 for (j = 0; j < ids_nr; j++) {
7248 for (k = 0; k < chunks[i].mirror_count; k++) {
7249 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
7250 valid_id[valid_count] = mirror_ids[j];
7257 memcpy(chunks[i].mirror_id, valid_id,
7258 sizeof(__u16) * valid_count);
7259 chunks[i].mirror_count = valid_count;
7264 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
7265 * @layout: Mirror component list.
7266 * @chunks: Array of chunks.
7267 * @chunks_size: Array size of @chunks.
7269 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
7270 * to find out chunk segments and store them in @chunks array.
7272 * The @mirror_id array in each element of @chunks will store the valid
7273 * mirror ids that cover the chunk. If a mirror component covering the
7274 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
7275 * will not be stored into the @mirror_id array, and the chunk for that
7276 * mirror will not be verified.
7278 * The @mirror_count in each element of @chunks will store the number of
7279 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
7280 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
7281 * indicates the chunk is valid in only one mirror. In both cases, the
7282 * chunk will not be verified.
7284 * Here is an example:
7287 * +------+-------------+--------------------+
7289 * +------+------+------+------+-------------+
7290 * | | S | S | | mirror2
7291 * +-------------+------+------+-------------+
7293 * prepared @chunks array will contain 5 elements:
7294 * (([0, 1M), [1, 2], 2),
7295 * ([1M, 2M), [1, 2], 2),
7296 * ([2M, 3M), [1], 1),
7297 * ([3M, 4M], [], 0),
7298 * ([4M, EOF), [2], 1))
7300 * Return: the actual array size of @chunks on success
7301 * or a negative error code on failure.
7304 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
7305 struct verify_chunk *chunks,
7316 memset(chunks, 0, sizeof(*chunks) * chunks_size);
7319 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
7322 "%s: move to the first layout component: %s.\n",
7323 progname, strerror(errno));
7329 chunks[idx].chunk.e_end = LUSTRE_EOF;
7331 rc = llapi_layout_comp_extent_get(layout, &start, &end);
7334 "%s: llapi_layout_comp_extent_get failed: %s.\n",
7335 progname, strerror(errno));
7339 if (start > chunks[idx].chunk.e_start ||
7340 end <= chunks[idx].chunk.e_start)
7343 if (end < chunks[idx].chunk.e_end)
7344 chunks[idx].chunk.e_end = end;
7346 rc = llapi_layout_comp_flags_get(layout, &flags);
7349 "%s: llapi_layout_comp_flags_get failed: %s.\n",
7350 progname, strerror(errno));
7354 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
7357 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7360 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7361 progname, strerror(errno));
7365 chunks[idx].mirror_id[i] = mirror_id;
7367 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
7369 "%s: mirror_id array is too small.\n",
7376 rc = llapi_layout_comp_use(layout,
7377 LLAPI_LAYOUT_COMP_USE_NEXT);
7380 "%s: move to the next layout component: %s.\n",
7381 progname, strerror(errno));
7384 } /* loop through all components */
7386 chunks[idx].mirror_count = i;
7388 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
7392 if (idx >= chunks_size) {
7393 fprintf(stderr, "%s: chunks array is too small.\n",
7399 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
7403 return rc < 0 ? rc : idx + 1;
7407 * lfs_mirror_verify_chunk() - Verify a chunk.
7408 * @fd: File descriptor of the mirrored file.
7409 * @file_size: Size of the mirrored file.
7410 * @chunk: A chunk and its corresponding valid mirror ids.
7411 * @verbose: Verbose mode.
7413 * This function verifies a @chunk contains exactly the same data
7414 * ammong the mirrors that cover it.
7416 * If @verbose is specified, then the function will print where the
7417 * differences are if the data do not match. Otherwise, it will
7418 * just return an error in that case.
7420 * Return: 0 on success or a negative error code on failure.
7423 int lfs_mirror_verify_chunk(int fd, size_t file_size,
7424 struct verify_chunk *chunk, int verbose)
7426 const size_t buflen = 4 * 1024 * 1024; /* 4M */
7428 size_t page_size = sysconf(_SC_PAGESIZE);
7434 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7441 rc = posix_memalign(&buf, page_size, buflen);
7442 if (rc) /* error code is returned directly */
7446 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7447 PEXT(&chunk->chunk));
7448 for (i = 0; i < chunk->mirror_count; i++)
7449 fprintf(stdout, " %u", chunk->mirror_id[i]);
7450 fprintf(stdout, "\n");
7454 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
7455 pos = chunk->chunk.e_start;
7456 while (bytes_done < count) {
7457 /* compute initial CRC-32 checksum */
7458 crc = crc32(0L, Z_NULL, 0);
7459 memset(crc_array, 0, sizeof(crc_array));
7462 for (i = 0; i < chunk->mirror_count; i++) {
7463 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
7465 if (bytes_read < 0) {
7468 "%s: failed to read data from mirror %u: %s.\n",
7469 progname, chunk->mirror_id[i],
7474 /* compute new CRC-32 checksum */
7475 crc_array[i] = crc32(crc, buf, bytes_read);
7479 print_checksums(chunk, crc_array);
7481 /* compare CRC-32 checksum values */
7482 for (i = 1; i < chunk->mirror_count; i++) {
7483 if (crc_array[i] != crc_array[0]) {
7489 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
7490 progname, PEXT(&chunk->chunk),
7491 chunk->mirror_id[0],
7492 chunk->mirror_id[i]);
7497 bytes_done += bytes_read;
7500 if (verbose > 1 && rc == 0) {
7501 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7502 PEXT(&chunk->chunk));
7503 for (i = 0; i < chunk->mirror_count; i++)
7504 fprintf(stdout, " %u", chunk->mirror_id[i]);
7505 fprintf(stdout, " PASS\n\n");
7514 * lfs_mirror_verify_file() - Verify a mirrored file.
7515 * @fname: Mirrored file name.
7516 * @mirror_ids: Specified mirror ids to be verified.
7517 * @ids_nr: Number of specified mirror ids.
7518 * @verbose: Verbose mode.
7520 * This function verifies that each SYNC mirror of a mirrored file
7521 * specified by @fname contains exactly the same data.
7523 * If @mirror_ids is specified, then the function will verify the
7524 * mirrors specified by @mirror_ids contain exactly the same data.
7526 * If @verbose is specified, then the function will print where the
7527 * differences are if the data do not match. Otherwise, it will
7528 * just return an error in that case.
7530 * Return: 0 on success or a negative error code on failure.
7533 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
7536 struct verify_chunk chunks_array[1024] = { };
7537 struct llapi_layout *layout = NULL;
7541 int chunk_count = 0;
7547 if (stat(fname, &stbuf) < 0) {
7548 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7549 progname, fname, strerror(errno));
7554 if (!S_ISREG(stbuf.st_mode)) {
7555 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7561 if (stbuf.st_size == 0) {
7563 fprintf(stdout, "%s: '%s' file size is 0.\n",
7569 fd = open(fname, O_DIRECT | O_RDONLY);
7571 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7572 progname, fname, strerror(errno));
7577 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
7579 fprintf(stderr, "%s: '%s' llapi_lease_get failed: %s.\n",
7580 progname, fname, strerror(errno));
7584 layout = llapi_layout_get_by_fd(fd, 0);
7585 if (layout == NULL) {
7586 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7587 progname, fname, strerror(errno));
7589 llapi_lease_put(fd);
7593 rc = llapi_layout_flags_get(layout, &flr_state);
7595 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7596 progname, fname, strerror(errno));
7601 flr_state &= LCM_FL_FLR_MASK;
7602 switch (flr_state) {
7603 case LCM_FL_NOT_FLR:
7605 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7606 progname, fname, lcm_flags_string(flr_state));
7612 /* find out mirror chunks to be verified */
7613 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
7614 ARRAY_SIZE(chunks_array));
7615 if (chunk_count < 0) {
7621 /* filter specified mirror ids */
7622 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
7625 print_chunks(fname, chunks_array, chunk_count);
7627 for (idx = 0; idx < chunk_count; idx++) {
7628 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
7631 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
7633 PEXT(&chunks_array[idx].chunk),
7634 (unsigned long long)stbuf.st_size);
7638 if (chunks_array[idx].mirror_count == 0) {
7640 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
7642 PEXT(&chunks_array[idx].chunk));
7644 fprintf(stderr, "skipped\n");
7648 fprintf(stderr, "failed\n");
7652 if (chunks_array[idx].mirror_count == 1) {
7655 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
7657 PEXT(&chunks_array[idx].chunk),
7658 chunks_array[idx].mirror_id[0]);
7662 rc = llapi_lease_check(fd);
7663 if (rc != LL_LEASE_RDLCK) {
7664 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7669 /* verify one chunk */
7670 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
7671 &chunks_array[idx], verbose);
7685 llapi_layout_free(layout);
7686 llapi_lease_put(fd);
7694 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
7695 * @argc: The count of lfs mirror verify command line arguments.
7696 * @argv: Array of strings for lfs mirror verify command line arguments.
7698 * This function parses lfs mirror verify command and verifies the
7699 * specified mirrored file(s).
7701 * Return: 0 on success or a negative error code on failure.
7703 static inline int lfs_mirror_verify(int argc, char **argv)
7705 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7713 struct option long_opts[] = {
7714 { .val = 'o', .name = "only", .has_arg = required_argument },
7715 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7718 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
7720 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
7723 rc = parse_mirror_ids(mirror_ids,
7724 ARRAY_SIZE(mirror_ids),
7728 "%s: bad mirror ids '%s'.\n",
7735 "%s: at least 2 mirror ids needed with '--only' option.\n",
7745 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7746 progname, argv[optind - 1]);
7752 if (argc == optind) {
7753 fprintf(stderr, "%s: no file name given.\n", progname);
7758 if (ids_nr > 0 && argc > optind + 1) {
7760 "%s: '--only' cannot be used upon multiple files.\n",
7768 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7774 for (; optind < argc; optind++) {
7775 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
7785 * lfs_mirror() - Parse and execute lfs mirror commands.
7786 * @argc: The count of lfs mirror command line arguments.
7787 * @argv: Array of strings for lfs mirror command line arguments.
7789 * This function parses lfs mirror commands and performs the
7790 * corresponding functions specified in mirror_cmdlist[].
7792 * Return: 0 on success or an error code on failure.
7794 static int lfs_mirror(int argc, char **argv)
7801 Parser_init("lfs-mirror > ", mirror_cmdlist);
7803 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
7805 program_invocation_short_name = cmd;
7807 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
7809 rc = Parser_commands();
7811 return rc < 0 ? -rc : rc;
7815 * lfs_mirror_list_commands() - List lfs mirror commands.
7816 * @argc: The count of command line arguments.
7817 * @argv: Array of strings for command line arguments.
7819 * This function lists lfs mirror commands defined in mirror_cmdlist[].
7821 * Return: 0 on success.
7823 static int lfs_mirror_list_commands(int argc, char **argv)
7825 char buffer[81] = "";
7827 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
7833 static int lfs_list_commands(int argc, char **argv)
7835 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
7837 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
7842 int main(int argc, char **argv)
7846 /* Ensure that liblustreapi constructor has run */
7847 if (!llapi_liblustreapi_initialized())
7848 fprintf(stderr, "liblustreapi was not properly initialized\n");
7853 Parser_init("lfs > ", cmdlist);
7855 progname = program_invocation_short_name; /* Used in error messages */
7857 llapi_set_command_name(argv[1]);
7858 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
7859 llapi_clear_command_name();
7861 rc = Parser_commands();
7864 return rc < 0 ? -rc : rc;
7867 #ifdef _LUSTRE_IDL_H_
7868 /* Everything we need here should be included by lustreapi.h. */
7869 # error "lfs should not depend on lustre_idl.h"
7870 #endif /* _LUSTRE_IDL_H_ */