4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
66 #include "lfs_project.h"
68 #include <libcfs/util/string.h>
69 #include <libcfs/util/ioctl.h>
70 #include <libcfs/util/parser.h>
71 #include <lustre/lustreapi.h>
72 #include <linux/lustre/lustre_ver.h>
73 #include <linux/lustre/lustre_param.h>
76 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
77 #endif /* !ARRAY_SIZE */
80 static int lfs_find(int argc, char **argv);
81 static int lfs_getstripe(int argc, char **argv);
82 static int lfs_getdirstripe(int argc, char **argv);
83 static int lfs_setdirstripe(int argc, char **argv);
84 static int lfs_rmentry(int argc, char **argv);
85 static int lfs_osts(int argc, char **argv);
86 static int lfs_mdts(int argc, char **argv);
87 static int lfs_df(int argc, char **argv);
88 static int lfs_getname(int argc, char **argv);
89 static int lfs_check(int argc, char **argv);
90 #ifdef HAVE_SYS_QUOTA_H
91 static int lfs_setquota(int argc, char **argv);
92 static int lfs_quota(int argc, char **argv);
93 static int lfs_project(int argc, char **argv);
95 static int lfs_flushctx(int argc, char **argv);
96 static int lfs_cp(int argc, char **argv);
97 static int lfs_ls(int argc, char **argv);
98 static int lfs_poollist(int argc, char **argv);
99 static int lfs_changelog(int argc, char **argv);
100 static int lfs_changelog_clear(int argc, char **argv);
101 static int lfs_fid2path(int argc, char **argv);
102 static int lfs_path2fid(int argc, char **argv);
103 static int lfs_data_version(int argc, char **argv);
104 static int lfs_hsm_state(int argc, char **argv);
105 static int lfs_hsm_set(int argc, char **argv);
106 static int lfs_hsm_clear(int argc, char **argv);
107 static int lfs_hsm_action(int argc, char **argv);
108 static int lfs_hsm_archive(int argc, char **argv);
109 static int lfs_hsm_restore(int argc, char **argv);
110 static int lfs_hsm_release(int argc, char **argv);
111 static int lfs_hsm_remove(int argc, char **argv);
112 static int lfs_hsm_cancel(int argc, char **argv);
113 static int lfs_swap_layouts(int argc, char **argv);
114 static int lfs_mv(int argc, char **argv);
115 static int lfs_ladvise(int argc, char **argv);
116 static int lfs_mirror(int argc, char **argv);
117 static int lfs_mirror_list_commands(int argc, char **argv);
118 static int lfs_list_commands(int argc, char **argv);
119 static inline int lfs_mirror_resync(int argc, char **argv);
120 static inline int lfs_mirror_verify(int argc, char **argv);
122 enum setstripe_origin {
128 static int lfs_setstripe0(int argc, char **argv, enum setstripe_origin opc);
130 static inline int lfs_setstripe(int argc, char **argv)
132 return lfs_setstripe0(argc, argv, SO_SETSTRIPE);
134 static inline int lfs_setstripe_migrate(int argc, char **argv)
136 return lfs_setstripe0(argc, argv, SO_MIGRATE);
138 static inline int lfs_mirror_create(int argc, char **argv)
140 return lfs_setstripe0(argc, argv, SO_MIRROR_CREATE);
142 static inline int lfs_mirror_extend(int argc, char **argv)
144 return lfs_setstripe0(argc, argv, SO_MIRROR_EXTEND);
147 /* Setstripe and migrate share mostly the same parameters */
148 #define SSM_CMD_COMMON(cmd) \
149 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
150 " [--stripe-count|-c <stripe_count>]\n" \
151 " [--stripe-index|-i <start_ost_idx>]\n" \
152 " [--stripe-size|-S <stripe_size>]\n" \
153 " [--layout|-L <pattern>]\n" \
154 " [--pool|-p <pool_name>]\n" \
155 " [--ost|-o <ost_indices>]\n"
157 #define SSM_HELP_COMMON \
158 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
159 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
160 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
161 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
162 "\t respectively)\n" \
163 "\tpool_name: Name of OST pool to use (default none)\n" \
164 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
165 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
166 "\t Indices be specified in a format of:\n" \
167 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
169 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
170 "\t If --pool is set with --ost, then the OSTs\n" \
171 "\t must be the members of the pool.\n" \
172 "\tcomp_end: Extent end of component, start after previous end.\n"\
173 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
174 "\t respectively, -1 for EOF). Must be a multiple of\n"\
177 #define MIRROR_CREATE_HELP \
178 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
179 "\t setstripe layout options\n" \
180 "\t It defaults to 1 if not specified; if specified,\n" \
181 "\t it must follow the option without a space.\n" \
182 "\t The option can also be repeated multiple times to\n" \
183 "\t separate mirrors that have different layouts.\n" \
184 "\tsetstripe options: Mirror layout\n" \
185 "\t It can be a plain layout or a composite layout.\n" \
186 "\t If not specified, the stripe options inherited\n" \
187 "\t from the previous component will be used.\n" \
188 "\tparent: Use default stripe options from parent directory\n" \
189 "\tflags: set flags to the component of the current mirror.\n" \
190 "\t Only \"prefer\" flag is supported so far.\n"
192 #define MIRROR_EXTEND_HELP \
194 "\tvictim_file: The layout of victim_file will be split and used\n" \
195 "\t as a mirror added to the mirrored file.\n" \
196 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
197 "\t from victim file(s) in case the victim file(s)\n" \
198 "\t contains the same data as the original mirrored\n" \
201 #define MIRROR_EXTEND_USAGE \
202 " <--mirror-count|-N[mirror_count]>\n" \
203 " [setstripe options|--parent|-f <victim_file>]\n" \
206 #define SETSTRIPE_USAGE \
207 SSM_CMD_COMMON("setstripe") \
208 MIRROR_EXTEND_USAGE \
209 " <directory|filename>\n" \
213 #define MIGRATE_USAGE \
214 SSM_CMD_COMMON("migrate ") \
216 " [--non-block|-n]\n" \
220 "\tblock: Block file access during data migration (default)\n" \
221 "\tnon-block: Abort migrations if concurrent access is detected\n" \
223 #define SETDIRSTRIPE_USAGE \
224 " [--mdt-count|-c stripe_count>\n" \
225 " [--mdt-index|-i mdt_index]\n" \
226 " [--mdt-hash|-H mdt_hash]\n" \
227 " [--default|-D] [--mode|-m mode] <dir>\n" \
228 "\tstripe_count: stripe count of the striped directory\n" \
229 "\tmdt_index: MDT index of first stripe\n" \
230 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
231 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
232 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
233 "\tdefault_stripe: set default dirstripe of the directory\n" \
234 "\tmode: the mode of the directory\n"
237 * command_t mirror_cmdlist - lfs mirror commands.
239 command_t mirror_cmdlist[] = {
240 { .pc_name = "create", .pc_func = lfs_mirror_create,
241 .pc_help = "Create a mirrored file.\n"
242 "usage: lfs mirror create "
243 "<--mirror-count|-N[mirror_count]> "
244 "[setstripe options|--parent] ... <filename|directory>\n"
245 MIRROR_CREATE_HELP },
246 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
247 .pc_help = "Extend a mirrored file.\n"
248 "usage: lfs mirror extend "
249 "<--mirror-count|-N[mirror_count]> [--no-verify] "
250 "[setstripe options|--parent|-f <victim_file>] ... <filename>\n"
251 MIRROR_EXTEND_HELP },
252 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
253 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
254 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
255 "<mirrored file> [<mirrored file2>...]\n"},
256 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
257 .pc_help = "Verify mirrored file(s).\n"
258 "usage: lfs mirror verify "
259 "[--only <mirror_id,mirror_id2[,...]>] "
260 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
261 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
262 .pc_help = "list commands supported by lfs mirror"},
263 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
264 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
265 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
269 /* all available commands */
270 command_t cmdlist[] = {
271 {"setstripe", lfs_setstripe, 0,
272 "To create a file with specified striping/composite layout, or\n"
273 "create/replace the default layout on an existing directory:\n"
274 SSM_CMD_COMMON("setstripe")
275 " <directory|filename>\n"
277 "To add component(s) to an existing composite file:\n"
278 SSM_CMD_COMMON("setstripe --component-add")
280 "To totally delete the default striping from an existing directory:\n"
281 "usage: setstripe -d <directory>\n"
283 "To delete the last component(s) from an existing composite file\n"
284 "(note that this will also delete any data in those components):\n"
285 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
286 " [--component-flags|-F <comp_flags>]\n"
288 "\tcomp_id: Unique component ID to delete\n"
289 "\tcomp_flags: 'init' indicating all instantiated components\n"
290 "\t '^init' indicating all uninstantiated components\n"
291 "\t-I and -F cannot be specified at the same time\n"},
292 {"getstripe", lfs_getstripe, 0,
293 "To list the striping info for a given file or files in a\n"
294 "directory or recursively for all files in a directory tree.\n"
295 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
296 " [--stripe-count|-c] [--stripe-index|-i]\n"
297 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
298 " [--mdt|-m] [--recursive|-r] [--raw|-R] [--yaml|-y]\n"
299 " [--layout|-L] [--fid|-F] [--generation|-g]\n"
300 " [--component-id[=comp_id]|-I[comp_id]]\n"
301 " [--component-flags[=comp_flags]]\n"
302 " [--component-count]\n"
303 " [--component-start[=[+-]comp_start]]\n"
304 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
305 " <directory|filename> ..."},
306 {"setdirstripe", lfs_setdirstripe, 0,
307 "To create a striped directory on a specified MDT. This can only\n"
308 "be done on MDT0 with the right of administrator.\n"
309 "usage: setdirstripe [OPTION] <directory>\n"
311 {"getdirstripe", lfs_getdirstripe, 0,
312 "To list the striping info for a given directory\n"
313 "or recursively for all directories in a directory tree.\n"
314 "usage: getdirstripe [--obd|-O <uuid>] [--mdt-count|-c]\n"
315 " [--mdt-index|-i] [--mdt-hash|-t]\n"
316 " [--recursive|-r] [--yaml|-y]\n"
317 " [--default|-D] <dir> ..."},
318 {"mkdir", lfs_setdirstripe, 0,
319 "To create a striped directory on a specified MDT. This can only\n"
320 "be done on MDT0 with the right of administrator.\n"
321 "usage: mkdir [OPTION] <directory>\n"
323 {"rm_entry", lfs_rmentry, 0,
324 "To remove the name entry of the remote directory. Note: This\n"
325 "command will only delete the name entry, i.e. the remote directory\n"
326 "will become inaccessable after this command. This can only be done\n"
327 "by the administrator\n"
328 "usage: rm_entry <dir>\n"},
329 {"pool_list", lfs_poollist, 0,
330 "List pools or pool OSTs\n"
331 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
332 {"find", lfs_find, 0,
333 "find files matching given attributes recursively in directory tree.\n"
334 "usage: find <directory|filename> ...\n"
335 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
336 " [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
337 " [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
338 " [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
339 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
340 " [[!] --stripe-count|-c [+-]<stripes>]\n"
341 " [[!] --stripe-index|-i <index,...>]\n"
342 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
343 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
344 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
345 " [[!] --projid <projid>]\n"
346 " [[!] --layout|-L released,raid0,mdt]\n"
347 " [[!] --component-count [+-]<comp_cnt>]\n"
348 " [[!] --component-start [+-]N[kMGTPE]]\n"
349 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
350 " [[!] --component-flags <comp_flags>]\n"
351 " [[!] --mdt-count|-T [+-]<stripes>]\n"
352 " [[!] --mdt-hash|-H <hashtype>\n"
353 "\t !: used before an option indicates 'NOT' requested attribute\n"
354 "\t -: used before a value indicates less than requested value\n"
355 "\t +: used before a value indicates more than requested value\n"
356 "\tmdt-hash: hash type of the striped directory.\n"
357 "\t fnv_1a_64 FNV-1a hash algorithm\n"
358 "\t all_char sum of characters % MDT_COUNT\n"},
359 {"check", lfs_check, 0,
360 "Display the status of MDS or OSTs (as specified in the command)\n"
361 "or all the servers (MDS and OSTs).\n"
362 "usage: check <osts|mds|servers>"},
363 {"osts", lfs_osts, 0, "list OSTs connected to client "
364 "[for specified path only]\n" "usage: osts [path]"},
365 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
366 "[for specified path only]\n" "usage: mdts [path]"},
368 "report filesystem disk space usage or inodes usage"
369 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
370 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
371 {"getname", lfs_getname, 0, "list instances and specified mount points "
372 "[for specified path only]\n"
373 "Usage: getname [-h]|[path ...] "},
374 #ifdef HAVE_SYS_QUOTA_H
375 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
376 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
377 " -b <block-softlimit> -B <block-hardlimit>\n"
378 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
379 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
380 " [--block-softlimit <block-softlimit>]\n"
381 " [--block-hardlimit <block-hardlimit>]\n"
382 " [--inode-softlimit <inode-softlimit>]\n"
383 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
384 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
385 " [--block-grace <block-grace>]\n"
386 " [--inode-grace <inode-grace>] <filesystem>\n"
387 " -b can be used instead of --block-softlimit/--block-grace\n"
388 " -B can be used instead of --block-hardlimit\n"
389 " -i can be used instead of --inode-softlimit/--inode-grace\n"
390 " -I can be used instead of --inode-hardlimit\n\n"
391 "Note: The total quota space will be split into many qunits and\n"
392 " balanced over all server targets, the minimal qunit size is\n"
393 " 1M bytes for block space and 1K inodes for inode space.\n\n"
394 " Quota space rebalancing process will stop when this mininum\n"
395 " value is reached. As a result, quota exceeded can be returned\n"
396 " while many targets still have 1MB or 1K inodes of spare\n"
398 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
399 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
401 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
402 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
403 {"project", lfs_project, 0,
404 "Change or list project attribute for specified file or directory.\n"
405 "usage: project [-d|-r] <file|directory...>\n"
406 " list project ID and flags on file(s) or directories\n"
407 " project [-p id] [-s] [-r] <file|directory...>\n"
408 " set project ID and/or inherit flag for specified file(s) or directories\n"
409 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
410 " check project ID and flags on file(s) or directories, print outliers\n"
411 " project -C [-r] [-k] <file|directory...>\n"
412 " clear the project inherit flag and ID on the file or directory\n"
415 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
416 "usage: flushctx [-k] [mountpoint...]"},
418 "Remote user copy files and directories.\n"
419 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
421 "Remote user list directory contents.\n"
422 "usage: ls [OPTION]... [FILE]..."},
423 {"changelog", lfs_changelog, 0,
424 "Show the metadata changes on an MDT."
425 "\nusage: changelog <mdtname> [startrec [endrec]]"},
426 {"changelog_clear", lfs_changelog_clear, 0,
427 "Indicate that old changelog records up to <endrec> are no longer of "
428 "interest to consumer <id>, allowing the system to free up space.\n"
429 "An <endrec> of 0 means all records.\n"
430 "usage: changelog_clear <mdtname> <id> <endrec>"},
431 {"fid2path", lfs_fid2path, 0,
432 "Resolve the full path(s) for given FID(s). For a specific hardlink "
433 "specify link number <linkno>.\n"
434 /* "For a historical link name, specify changelog record <recno>.\n" */
435 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
436 /* [ --rec <recno> ] */ },
437 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
438 "usage: path2fid [--parents] <path> ..."},
439 {"data_version", lfs_data_version, 0, "Display file data version for "
440 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
441 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
442 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
443 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
444 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
445 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
446 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
448 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
449 "[--archived] [--lost] <file> ..."},
450 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
451 "given files.\n" "usage: hsm_action <file> ..."},
452 {"hsm_archive", lfs_hsm_archive, 0,
453 "Archive file to external storage.\n"
454 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
456 {"hsm_restore", lfs_hsm_restore, 0,
457 "Restore file from external storage.\n"
458 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
459 {"hsm_release", lfs_hsm_release, 0,
460 "Release files from Lustre.\n"
461 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
462 {"hsm_remove", lfs_hsm_remove, 0,
463 "Remove file copy from external storage.\n"
464 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
466 " (FILE [FILE ...] | "
467 "--mntpath MOUNTPATH FID [FID ...])\n"
469 "Note: To remove an archived copy of a file already deleted from a "
471 "--mntpath option and a list of FIDs must be specified"
473 {"hsm_cancel", lfs_hsm_cancel, 0,
474 "Cancel requests related to specified files.\n"
475 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
476 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
477 "usage: swap_layouts <path1> <path2>"},
478 {"migrate", lfs_setstripe_migrate, 0,
479 "migrate a directory between MDTs.\n"
480 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
482 "\tmdt_idx: index of the destination MDT\n"
484 "migrate file objects from one OST "
485 "layout\nto another (may be not safe with concurent writes).\n"
487 "[--stripe-count|-c] <stripe_count>\n"
488 " [--stripe-index|-i] <start_ost_index>\n"
489 " [--stripe-size|-S] <stripe_size>\n"
490 " [--pool|-p] <pool_name>\n"
491 " [--ost-list|-o] <ost_indices>\n"
493 " [--non-block|-n]\n"
494 " <file|directory>\n"
495 "\tstripe_count: number of OSTs to stripe a file over\n"
496 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
497 "\tstripe_size: number of bytes to store before moving to the next OST\n"
498 "\tpool_name: name of the predefined pool of OSTs\n"
499 "\tost_indices: OSTs to stripe over, in order\n"
500 "\tblock: wait for the operation to return before continuing\n"
501 "\tnon-block: do not wait for the operation to return.\n"},
503 "To move directories between MDTs. This command is deprecated, "
504 "use \"migrate\" instead.\n"
505 "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
507 {"ladvise", lfs_ladvise, 0,
508 "Provide servers with advice about access patterns for a file.\n"
509 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
510 " [--background|-b] [--unset|-u]\n\n"
511 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
512 " {[--mode|-m [READ,WRITE]}\n"
514 {"mirror", lfs_mirror, mirror_cmdlist,
515 "lfs commands used to manage files with mirrored components:\n"
516 "lfs mirror create - create a mirrored file or directory\n"
517 "lfs mirror extend - add mirror(s) to an existing file\n"
518 "lfs mirror split - split a mirror from an existing mirrored file\n"
519 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
520 "lfs mirror verify - verify mirrored file(s)\n"},
521 {"help", Parser_help, 0, "help"},
522 {"exit", Parser_quit, 0, "quit"},
523 {"quit", Parser_quit, 0, "quit"},
524 {"--version", Parser_version, 0,
525 "output build version of the utility and exit"},
526 {"--list-commands", lfs_list_commands, 0,
527 "list commands supported by the utility and exit"},
532 static int check_hashtype(const char *hashtype)
536 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
537 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
544 static const char *error_loc = "syserror";
547 MIGRATION_NONBLOCK = 1 << 0,
548 MIGRATION_MIRROR = 1 << 1,
551 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
552 struct llapi_layout *layout);
555 migrate_open_files(const char *name, const struct llapi_stripe_param *param,
556 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
562 char parent[PATH_MAX];
563 char volatile_file[PATH_MAX];
569 if (param == NULL && layout == NULL) {
570 error_loc = "layout information";
574 /* search for file directory pathname */
575 if (strlen(name) > sizeof(parent) - 1) {
576 error_loc = "source file name";
580 strncpy(parent, name, sizeof(parent));
581 ptr = strrchr(parent, '/');
583 if (getcwd(parent, sizeof(parent)) == NULL) {
584 error_loc = "getcwd";
588 if (ptr == parent) /* leading '/' */
593 /* open file, direct io */
594 /* even if the file is only read, WR mode is nedeed to allow
595 * layout swap on fd */
596 fd = open(name, O_RDWR | O_DIRECT);
599 error_loc = "cannot open source file";
603 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
605 error_loc = "cannot get MDT index";
610 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
611 mode_t open_mode = S_IRUSR | S_IWUSR;
613 random_value = random();
614 rc = snprintf(volatile_file, sizeof(volatile_file),
615 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
616 mdt_index, random_value);
617 if (rc >= sizeof(volatile_file)) {
622 /* create, open a volatile file, use caching (ie no directio) */
624 fdv = llapi_file_open_param(volatile_file, open_flags,
627 fdv = lfs_component_create(volatile_file, open_flags,
629 } while (fdv < 0 && (rc = fdv) == -EEXIST);
632 error_loc = "cannot create volatile file";
636 /* In case the MDT does not support creation of volatile files
637 * we should try to unlink it. */
638 (void)unlink(volatile_file);
640 /* Not-owner (root?) special case.
641 * Need to set owner/group of volatile file like original.
642 * This will allow to pass related check during layout_swap.
647 error_loc = "cannot stat source file";
651 rc = fstat(fdv, &stv);
654 error_loc = "cannot stat volatile";
658 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
659 rc = fchown(fdv, st.st_uid, st.st_gid);
662 error_loc = "cannot change ownwership of volatile";
681 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
683 struct llapi_layout *layout;
684 size_t buf_size = 4 * 1024 * 1024;
693 layout = llapi_layout_get_by_fd(fd_src, 0);
694 if (layout != NULL) {
695 uint64_t stripe_size;
697 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
699 buf_size = stripe_size;
701 llapi_layout_free(layout);
704 /* Use a page-aligned buffer for direct I/O */
705 rc = posix_memalign(&buf, getpagesize(), buf_size);
710 /* read new data only if we have written all
711 * previously read data */
714 rc = check_file(fd_src);
719 rsize = read(fd_src, buf, buf_size);
731 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
750 static int migrate_copy_timestamps(int fd, int fdv)
754 if (fstat(fd, &st) == 0) {
755 struct timeval tv[2] = {
756 {.tv_sec = st.st_atime},
757 {.tv_sec = st.st_mtime}
760 return futimes(fdv, tv);
766 static int migrate_block(int fd, int fdv)
773 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
775 error_loc = "cannot get dataversion";
783 /* The grouplock blocks all concurrent accesses to the file.
784 * It has to be taken after llapi_get_data_version as it would
786 rc = llapi_group_lock(fd, gid);
788 error_loc = "cannot get group lock";
792 rc = migrate_copy_data(fd, fdv, NULL);
794 error_loc = "data copy failed";
798 /* Make sure we keep original atime/mtime values */
799 rc = migrate_copy_timestamps(fd, fdv);
801 error_loc = "timestamp copy failed";
806 * for a migration we need to check data version on file did
809 * Pass in gid=0 since we already own grouplock. */
810 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
811 SWAP_LAYOUTS_CHECK_DV1);
813 error_loc = "file changed";
816 error_loc = "cannot swap layout";
821 rc2 = llapi_group_unlock(fd, gid);
822 if (rc2 < 0 && rc == 0) {
823 error_loc = "unlock group lock";
831 * Internal helper for migrate_copy_data(). Check lease and report error if
834 * \param[in] fd File descriptor on which to check the lease.
836 * \retval 0 Migration can keep on going.
837 * \retval -errno Error occurred, abort migration.
839 static int check_lease(int fd)
843 rc = llapi_lease_check(fd);
845 return 0; /* llapi_check_lease returns > 0 on success. */
850 static int migrate_nonblock(int fd, int fdv)
856 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
858 error_loc = "cannot get data version";
862 rc = migrate_copy_data(fd, fdv, check_lease);
864 error_loc = "data copy failed";
868 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
870 error_loc = "cannot get data version";
876 error_loc = "source file changed";
880 /* Make sure we keep original atime/mtime values */
881 rc = migrate_copy_timestamps(fd, fdv);
883 error_loc = "timestamp copy failed";
890 static int lfs_component_set(char *fname, int comp_id,
891 __u32 flags, __u32 neg_flags)
894 __u32 flags_array[2];
899 ids[count] = comp_id;
900 flags_array[count] = flags;
905 ids[count] = comp_id;
906 flags_array[count] = neg_flags | LCME_FL_NEG;
910 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
913 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
914 progname, comp_id, fname, flags, neg_flags);
919 static int lfs_component_del(char *fname, __u32 comp_id,
920 __u32 flags, __u32 neg_flags)
924 if (flags && neg_flags)
927 if (!flags && neg_flags)
928 flags = neg_flags | LCME_FL_NEG;
930 if ((flags && comp_id) || (!flags && !comp_id))
933 /* LCME_FL_INIT is the only supported flag in PFL */
935 if (flags & ~LCME_KNOWN_FLAGS) {
937 "%s setstripe: unknown flags %#x\n",
941 } else if (comp_id > LCME_ID_MAX) {
942 fprintf(stderr, "%s setstripe: invalid component id %u\n",
947 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
950 "%s setstripe: cannot delete component %#x from '%s': %s\n",
951 progname, comp_id, fname, strerror(errno));
955 static int lfs_component_add(char *fname, struct llapi_layout *layout)
962 rc = llapi_layout_file_comp_add(fname, layout);
964 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
965 fname, strerror(errno));
969 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
970 struct llapi_layout *layout)
978 fd = lstat(fname, &st);
979 if (fd == 0 && S_ISDIR(st.st_mode))
980 open_flags = O_DIRECTORY | O_RDONLY;
982 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
984 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
985 S_ISDIR(st.st_mode) ?
986 "set default composite layout for" :
987 "create composite file",
988 fname, strerror(errno));
992 static int lfs_migrate(char *name, __u64 migration_flags,
993 struct llapi_stripe_param *param,
994 struct llapi_layout *layout)
1000 rc = migrate_open_files(name, param, layout, &fd, &fdv);
1004 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1005 /* Blocking mode (forced if servers do not support file lease).
1006 * It is also the default mode, since we cannot distinguish
1007 * between a broken lease and a server that does not support
1008 * atomic swap/close (LU-6785) */
1009 rc = migrate_block(fd, fdv);
1013 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1015 error_loc = "cannot get lease";
1019 rc = migrate_nonblock(fd, fdv);
1021 llapi_lease_release(fd);
1025 /* Atomically put lease, swap layouts and close.
1026 * for a migration we need to check data version on file did
1028 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1030 error_loc = "cannot swap layout";
1042 fprintf(stderr, "error: %s: %s: %s: %s\n",
1043 progname, name, error_loc, strerror(-rc));
1047 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1056 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1060 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1061 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1062 const char *comp_name = comp_flags_table[i].cfn_name;
1064 if (strcmp(name, comp_name) == 0) {
1065 *flags |= comp_flag;
1067 } else if (strncmp(name, "^", 1) == 0 &&
1068 strcmp(name + 1, comp_name) == 0) {
1069 *neg_flags |= comp_flag;
1074 llapi_printf(LLAPI_MSG_ERROR,
1075 "%s: component flag '%s' not supported\n",
1081 if (!*flags && !*neg_flags)
1084 /* don't allow to set and exclude the same flag */
1085 if (*flags & *neg_flags)
1092 * struct mirror_args - Command-line arguments for mirror(s).
1093 * @m_count: Number of mirrors to be created with this layout.
1094 * @m_flags: Mirror level flags, only 'prefer' is supported.
1095 * @m_layout: Mirror layout.
1096 * @m_file: A victim file. Its layout will be split and used as a mirror.
1097 * @m_next: Point to the next node of the list.
1099 * Command-line arguments for mirror(s) will be parsed and stored in
1100 * a linked list that consists of this structure.
1102 struct mirror_args {
1105 struct llapi_layout *m_layout;
1107 struct mirror_args *m_next;
1110 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1115 rc = llapi_layout_comp_flags_get(layout, &flags);
1119 if (flags & LCME_FL_NEG) {
1120 fprintf(stderr, "error: %s: negative flags are not supported\n",
1125 if (flags & LCME_FL_STALE) {
1126 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1127 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1131 return LLAPI_LAYOUT_ITER_CONT;
1134 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1136 uint64_t start, end;
1140 /* LU-10112: do not support dom+flr in phase 1 */
1141 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1145 rc = llapi_layout_pattern_get(layout, &pattern);
1149 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1150 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1155 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1159 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1163 if (end != LUSTRE_EOF) {
1164 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1169 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1174 * enum mirror_flags - Flags for extending a mirrored file.
1175 * @NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1176 * in case the victim file(s) contains the same data as the
1177 * original mirrored file.
1179 * Flags for extending a mirrored file.
1186 * mirror_create_sanity_check() - Check mirror list.
1187 * @list: A linked list that stores the mirror arguments.
1189 * This function does a sanity check on @list for creating
1192 * Return: 0 on success or a negative error code on failure.
1194 static int mirror_create_sanity_check(const char *fname,
1195 struct mirror_args *list)
1198 bool has_m_file = false;
1199 bool has_m_layout = false;
1205 struct llapi_layout *layout;
1207 layout = llapi_layout_get_by_path(fname, 0);
1210 "error: %s: file '%s' couldn't get layout\n",
1215 rc = mirror_sanity_check_one(layout);
1216 llapi_layout_free(layout);
1222 while (list != NULL) {
1223 if (list->m_file != NULL) {
1225 llapi_layout_free(list->m_layout);
1228 llapi_layout_get_by_path(list->m_file, 0);
1229 if (list->m_layout == NULL) {
1231 "error: %s: file '%s' has no layout\n",
1232 progname, list->m_file);
1236 has_m_layout = true;
1237 if (list->m_layout == NULL) {
1238 fprintf(stderr, "error: %s: no mirror layout\n",
1244 rc = mirror_sanity_check_one(list->m_layout);
1248 list = list->m_next;
1251 if (has_m_file && has_m_layout) {
1252 fprintf(stderr, "error: %s: -f <victim_file> option should not "
1253 "be specified with setstripe options or "
1254 "--parent option\n", progname);
1261 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1263 __u32 mirror_flags = *(__u32 *)cbdata;
1267 rc = llapi_layout_comp_flags_get(layout, &flags);
1272 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1277 return LLAPI_LAYOUT_ITER_CONT;
1281 * mirror_create() - Create a mirrored file.
1282 * @fname: The file to be created.
1283 * @mirror_list: A linked list that stores the mirror arguments.
1285 * This function creates a mirrored file @fname with the mirror(s)
1286 * from @mirror_list.
1288 * Return: 0 on success or a negative error code on failure.
1290 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1292 struct llapi_layout *layout = NULL;
1293 struct mirror_args *cur_mirror = NULL;
1294 uint16_t mirror_count = 0;
1298 rc = mirror_create_sanity_check(NULL, mirror_list);
1302 cur_mirror = mirror_list;
1303 while (cur_mirror != NULL) {
1304 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1306 &cur_mirror->m_flags);
1309 fprintf(stderr, "%s: failed to set mirror flags\n",
1314 for (i = 0; i < cur_mirror->m_count; i++) {
1315 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1318 fprintf(stderr, "error: %s: "
1319 "merge layout failed: %s\n",
1320 progname, strerror(errno));
1324 mirror_count += cur_mirror->m_count;
1325 cur_mirror = cur_mirror->m_next;
1328 if (layout == NULL) {
1329 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1333 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1336 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1337 progname, strerror(errno));
1341 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0644,
1349 llapi_layout_free(layout);
1354 * Compare files and check lease on @fd.
1356 * \retval bytes number of bytes are the same
1358 static ssize_t mirror_file_compare(int fd, int fdv)
1360 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1362 ssize_t bytes_done = 0;
1363 ssize_t bytes_read = 0;
1365 buf = malloc(buflen * 2);
1370 if (!llapi_lease_check(fd)) {
1371 bytes_done = -EBUSY;
1375 bytes_read = read(fd, buf, buflen);
1376 if (bytes_read <= 0)
1379 if (bytes_read != read(fdv, buf + buflen, buflen))
1382 /* XXX: should compute the checksum on each buffer and then
1383 * compare checksum to avoid cache collision */
1384 if (memcmp(buf, buf + buflen, bytes_read))
1387 bytes_done += bytes_read;
1395 static int mirror_extend_file(const char *fname, const char *victim_file,
1396 enum mirror_flags mirror_flags)
1401 struct stat stbuf_v;
1402 struct ll_ioc_lease *data = NULL;
1405 fd = open(fname, O_RDWR);
1407 error_loc = "open source file";
1412 fdv = open(victim_file, O_RDWR);
1414 error_loc = "open target file";
1419 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1420 error_loc = "stat source or target file";
1425 if (stbuf.st_dev != stbuf_v.st_dev) {
1426 error_loc = "stat source and target file";
1431 /* mirrors should be of the same size */
1432 if (stbuf.st_size != stbuf_v.st_size) {
1433 error_loc = "file sizes don't match";
1438 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1440 error_loc = "cannot get lease";
1444 if (!(mirror_flags & NO_VERIFY)) {
1446 /* mirrors should have the same contents */
1447 ret = mirror_file_compare(fd, fdv);
1448 if (ret != stbuf.st_size) {
1449 error_loc = "file busy or contents don't match";
1450 rc = ret < 0 ? ret : -EINVAL;
1455 /* Get rid of caching pages from clients */
1456 rc = llapi_file_flush(fd);
1458 error_loc = "cannot get data version";
1462 rc = llapi_file_flush(fdv);
1464 error_loc = "cannot get data version";
1469 /* Make sure we keep original atime/mtime values */
1470 rc = migrate_copy_timestamps(fd, fdv);
1472 error_loc = "cannot copy timestamp";
1476 /* Atomically put lease, merge layouts and close. */
1477 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1479 error_loc = "memory allocation";
1482 data->lil_mode = LL_LEASE_UNLCK;
1483 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1484 data->lil_count = 1;
1485 data->lil_ids[0] = fdv;
1486 rc = llapi_lease_set(fd, data);
1488 error_loc = "cannot merge layout";
1490 } else if (rc == 0) {
1492 error_loc = "lost lease lock";
1505 (void) unlink(victim_file);
1507 fprintf(stderr, "error: %s: %s: %s: %s\n",
1508 progname, fname, error_loc, strerror(-rc));
1512 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1514 struct ll_ioc_lease *data = NULL;
1519 rc = migrate_open_files(name, NULL, layout, &fd, &fdv);
1523 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1525 error_loc = "cannot get lease";
1529 rc = migrate_nonblock(fd, fdv);
1531 llapi_lease_release(fd);
1535 /* Atomically put lease, merge layouts and close. */
1536 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1538 error_loc = "memory allocation";
1541 data->lil_mode = LL_LEASE_UNLCK;
1542 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1543 data->lil_count = 1;
1544 data->lil_ids[0] = fdv;
1545 rc = llapi_lease_set(fd, data);
1547 error_loc = "cannot merge layout";
1549 } else if (rc == 0) {
1551 error_loc = "lost lease lock";
1564 fprintf(stderr, "error: %s: %s: %s: %s\n",
1565 progname, name, error_loc, strerror(-rc));
1569 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1570 enum mirror_flags mirror_flags)
1574 rc = mirror_create_sanity_check(fname, mirror_list);
1578 while (mirror_list) {
1579 if (mirror_list->m_file != NULL) {
1580 rc = mirror_extend_file(fname, mirror_list->m_file,
1583 __u32 mirror_count = mirror_list->m_count;
1585 while (mirror_count > 0) {
1586 rc = mirror_extend_layout(fname,
1587 mirror_list->m_layout);
1597 mirror_list = mirror_list->m_next;
1604 * Parse a string containing an target index list into an array of integers.
1606 * The input string contains a comma delimited list of individual
1607 * indices and ranges, for example "1,2-4,7". Add the indices into the
1608 * \a tgts array and remove duplicates.
1610 * \param[out] tgts array to store indices in
1611 * \param[in] size size of \a tgts array
1612 * \param[in] offset starting index in \a tgts
1613 * \param[in] arg string containing OST index list
1615 * \retval positive number of indices in \a tgts
1616 * \retval -EINVAL unable to parse \a arg
1618 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1622 int slots = size - offset;
1629 end_of_loop = false;
1630 while (!end_of_loop) {
1634 char *endptr = NULL;
1638 ptr = strchrnul(arg, ',');
1640 end_of_loop = *ptr == '\0';
1643 start_index = strtol(arg, &endptr, 0);
1644 if (endptr == arg) /* no data at all */
1646 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1649 end_index = start_index;
1650 if (*endptr == '-') {
1651 end_index = strtol(endptr + 1, &endptr, 0);
1652 if (*endptr != '\0')
1654 if (end_index < start_index)
1658 for (i = start_index; i <= end_index && slots > 0; i++) {
1661 /* remove duplicate */
1662 for (j = 0; j < offset; j++) {
1666 if (j == offset) { /* no duplicate */
1671 if (slots == 0 && i < end_index)
1679 if (!end_of_loop && ptr != NULL)
1682 return rc < 0 ? rc : nr;
1685 struct lfs_setstripe_args {
1686 unsigned long long lsa_comp_end;
1687 unsigned long long lsa_stripe_size;
1688 long long lsa_stripe_count;
1689 long long lsa_stripe_off;
1690 __u32 lsa_comp_flags;
1691 __u32 lsa_comp_neg_flags;
1692 unsigned long long lsa_pattern;
1695 char *lsa_pool_name;
1698 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1700 memset(lsa, 0, sizeof(*lsa));
1702 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1703 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1704 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1705 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1706 lsa->lsa_pool_name = NULL;
1710 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1711 * @lsa: Stripe options to be initialized and inherited.
1713 * This function initializes stripe options in @lsa and inherit
1714 * stripe_size, stripe_count and OST pool_name options.
1718 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1720 unsigned long long stripe_size;
1721 long long stripe_count;
1722 char *pool_name = NULL;
1724 stripe_size = lsa->lsa_stripe_size;
1725 stripe_count = lsa->lsa_stripe_count;
1726 pool_name = lsa->lsa_pool_name;
1728 setstripe_args_init(lsa);
1730 lsa->lsa_stripe_size = stripe_size;
1731 lsa->lsa_stripe_count = stripe_count;
1732 lsa->lsa_pool_name = pool_name;
1735 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1737 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1738 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1739 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1740 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1741 lsa->lsa_pool_name != NULL ||
1742 lsa->lsa_comp_end != 0);
1746 * comp_args_to_layout() - Create or extend a composite layout.
1747 * @composite: Pointer to the composite layout.
1748 * @lsa: Stripe options for the new component.
1750 * This function creates or extends a composite layout by adding a new
1751 * component with stripe options from @lsa.
1753 * Return: 0 on success or an error code on failure.
1755 static int comp_args_to_layout(struct llapi_layout **composite,
1756 struct lfs_setstripe_args *lsa)
1758 struct llapi_layout *layout = *composite;
1759 uint64_t prev_end = 0;
1762 if (layout == NULL) {
1763 layout = llapi_layout_alloc();
1764 if (layout == NULL) {
1765 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1769 *composite = layout;
1773 /* Get current component extent, current component
1774 * must be the tail component. */
1775 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
1777 fprintf(stderr, "Get comp extent failed. %s\n",
1782 rc = llapi_layout_comp_add(layout);
1784 fprintf(stderr, "Add component failed. %s\n",
1790 rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
1792 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
1793 prev_end, lsa->lsa_comp_end, strerror(errno));
1797 /* Data-on-MDT component setting */
1798 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
1799 /* In case of Data-on-MDT patterns the only extra option
1800 * applicable is stripe size option. */
1801 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
1802 fprintf(stderr, "Option 'stripe-count' can't be "
1803 "specified with Data-on-MDT component: %lld\n",
1804 lsa->lsa_stripe_count);
1807 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
1808 fprintf(stderr, "Option 'stripe-size' can't be "
1809 "specified with Data-on-MDT component: %llu\n",
1810 lsa->lsa_stripe_size);
1813 if (lsa->lsa_nr_tgts != 0) {
1814 fprintf(stderr, "Option 'ost-list' can't be specified "
1815 "with Data-on-MDT component: '%i'\n",
1819 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
1820 fprintf(stderr, "Option 'stripe-offset' can't be "
1821 "specified with Data-on-MDT component: %lld\n",
1822 lsa->lsa_stripe_off);
1825 if (lsa->lsa_pool_name != 0) {
1826 fprintf(stderr, "Option 'pool' can't be specified "
1827 "with Data-on-MDT component: '%s'\n",
1828 lsa->lsa_pool_name);
1832 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
1834 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
1835 lsa->lsa_pattern, strerror(errno));
1838 /* Data-on-MDT component has always single stripe up to end */
1839 lsa->lsa_stripe_size = lsa->lsa_comp_end;
1842 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
1844 fprintf(stderr, "Set stripe size %llu failed: %s\n",
1845 lsa->lsa_stripe_size, strerror(errno));
1849 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
1851 fprintf(stderr, "Set stripe count %lld failed: %s\n",
1852 lsa->lsa_stripe_count, strerror(errno));
1856 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
1858 fprintf(stderr, "Set flags 0x%x failed: %s\n",
1859 lsa->lsa_comp_flags, strerror(errno));
1863 if (lsa->lsa_pool_name != NULL) {
1864 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
1866 fprintf(stderr, "Set pool name: %s failed. %s\n",
1867 lsa->lsa_pool_name, strerror(errno));
1871 rc = llapi_layout_pool_name_set(layout, "");
1873 fprintf(stderr, "Clear pool name failed: %s\n",
1879 if (lsa->lsa_nr_tgts > 0) {
1880 if (lsa->lsa_stripe_count > 0 &&
1881 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
1882 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
1883 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
1884 fprintf(stderr, "stripe_count(%lld) != nr_osts(%d)\n",
1885 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
1888 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
1889 rc = llapi_layout_ost_index_set(layout, i,
1894 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
1895 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
1898 fprintf(stderr, "Set ost index %d failed. %s\n",
1899 i, strerror(errno));
1906 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
1907 * end of the last component in the existing file, and adjust the
1908 * first extent start of the components to be added accordingly. */
1909 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
1911 struct llapi_layout *head;
1912 uint64_t start, end, stripe_size, prev_end = 0;
1915 if (layout == NULL) {
1917 "%s setstripe: layout must be specified\n",
1923 head = llapi_layout_get_by_path(fname, 0);
1926 "%s setstripe: cannot read layout from '%s': %s\n",
1927 progname, fname, strerror(errno));
1929 } else if (errno == ENODATA) {
1930 /* file without LOVEA, this component-add will be turned
1931 * into a component-create. */
1932 llapi_layout_free(head);
1934 } else if (!llapi_layout_is_composite(head)) {
1935 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
1937 llapi_layout_free(head);
1941 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
1943 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
1944 progname, strerror(errno));
1945 llapi_layout_free(head);
1949 llapi_layout_free(head);
1951 /* Make sure we use the first component of the layout to be added. */
1952 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1955 "%s setstripe: cannot move component cursor: %s\n",
1956 progname, strerror(errno));
1960 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1962 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
1963 progname, strerror(errno));
1967 if (start > prev_end || end <= prev_end) {
1969 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
1970 progname, start, end, prev_end);
1974 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
1976 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
1977 progname, strerror(errno));
1981 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
1982 (prev_end & (stripe_size - 1))) {
1984 "%s setstripe: stripe size %lu not aligned with %lu\n",
1985 progname, stripe_size, prev_end);
1989 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
1992 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
1993 progname, prev_end, end, strerror(errno));
2000 static inline bool arg_is_eof(char *arg)
2002 return !strncmp(arg, "-1", strlen("-1")) ||
2003 !strncmp(arg, "EOF", strlen("EOF")) ||
2004 !strncmp(arg, "eof", strlen("eof"));
2008 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2010 * Return: Valid mirror_args pointer on success and
2011 * NULL if memory allocation fails.
2013 static struct mirror_args *lfs_mirror_alloc(void)
2015 struct mirror_args *mirror = NULL;
2018 mirror = calloc(1, sizeof(*mirror));
2029 * lfs_mirror_free() - Free memory allocated for a mirror argument
2031 * @mirror: Previously allocated mirror argument structure by
2032 * lfs_mirror_alloc().
2034 * Free memory allocated for @mirror.
2038 static void lfs_mirror_free(struct mirror_args *mirror)
2040 if (mirror->m_layout != NULL)
2041 llapi_layout_free(mirror->m_layout);
2046 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2047 * @mirror_list: Previously allocated mirror list.
2049 * Free memory allocated for @mirror_list.
2053 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2055 struct mirror_args *next_mirror = NULL;
2057 while (mirror_list != NULL) {
2058 next_mirror = mirror_list->m_next;
2059 lfs_mirror_free(mirror_list);
2060 mirror_list = next_mirror;
2072 LFS_COMP_USE_PARENT_OPT,
2073 LFS_COMP_NO_VERIFY_OPT,
2075 LFS_MIRROR_FLAGS_OPT,
2079 static int lfs_setstripe0(int argc, char **argv, enum setstripe_origin opc)
2081 struct lfs_setstripe_args lsa;
2082 struct llapi_stripe_param *param = NULL;
2083 struct find_param migrate_mdt_param = {
2093 char *mdt_idx_arg = NULL;
2094 unsigned long long size_units = 1;
2095 bool migrate_mode = false;
2096 bool migration_block = false;
2097 __u64 migration_flags = 0;
2098 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2099 int comp_del = 0, comp_set = 0;
2102 struct llapi_layout *layout = NULL;
2103 struct llapi_layout **lpp = &layout;
2104 bool mirror_mode = false;
2105 bool has_m_file = false;
2106 __u32 mirror_count = 0;
2107 enum mirror_flags mirror_flags = 0;
2108 struct mirror_args *mirror_list = NULL;
2109 struct mirror_args *new_mirror = NULL;
2110 struct mirror_args *last_mirror = NULL;
2113 struct option long_opts[] = {
2114 /* --block is only valid in migrate mode */
2115 { .val = 'b', .name = "block", .has_arg = no_argument},
2116 { .val = LFS_COMP_ADD_OPT,
2117 .name = "comp-add", .has_arg = no_argument},
2118 { .val = LFS_COMP_ADD_OPT,
2119 .name = "component-add",
2120 .has_arg = no_argument},
2121 { .val = LFS_COMP_DEL_OPT,
2122 .name = "comp-del", .has_arg = no_argument},
2123 { .val = LFS_COMP_DEL_OPT,
2124 .name = "component-del",
2125 .has_arg = no_argument},
2126 { .val = LFS_COMP_FLAGS_OPT,
2127 .name = "comp-flags", .has_arg = required_argument},
2128 { .val = LFS_COMP_FLAGS_OPT,
2129 .name = "component-flags",
2130 .has_arg = required_argument},
2131 { .val = LFS_COMP_SET_OPT,
2132 .name = "comp-set", .has_arg = no_argument},
2133 { .val = LFS_COMP_SET_OPT,
2134 .name = "component-set",
2135 .has_arg = no_argument},
2136 { .val = LFS_COMP_USE_PARENT_OPT,
2137 .name = "parent", .has_arg = no_argument},
2138 { .val = LFS_COMP_NO_VERIFY_OPT,
2139 .name = "no-verify", .has_arg = no_argument},
2140 { .val = LFS_MIRROR_FLAGS_OPT,
2141 .name = "flags", .has_arg = required_argument},
2142 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2143 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2144 { .val = 'd', .name = "delete", .has_arg = no_argument},
2145 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2146 { .val = 'E', .name = "component-end",
2147 .has_arg = required_argument},
2148 { .val = 'f', .name = "file", .has_arg = required_argument },
2149 /* dirstripe {"mdt-hash", required_argument, 0, 'H'}, */
2150 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2151 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2152 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2153 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2154 { .val = 'L', .name = "layout", .has_arg = required_argument },
2155 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2156 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2157 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2158 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2159 /* --non-block is only valid in migrate mode */
2160 { .val = 'n', .name = "non-block", .has_arg = no_argument},
2161 { .val = 'o', .name = "ost", .has_arg = required_argument},
2162 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2163 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2164 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2166 { .val = 'p', .name = "pool", .has_arg = required_argument },
2167 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2168 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2169 /* dirstripe {"mdt-count", required_argument, 0, 'T'}, */
2170 /* --verbose is only valid in migrate mode */
2171 { .val = 'v', .name = "verbose", .has_arg = no_argument },
2174 setstripe_args_init(&lsa);
2176 migrate_mode = (opc == SO_MIGRATE);
2177 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2179 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2181 while ((c = getopt_long(argc, argv, "bc:dE:f:i:I:m:N::no:p:L:s:S:v",
2182 long_opts, NULL)) >= 0) {
2187 case LFS_COMP_ADD_OPT:
2190 case LFS_COMP_DEL_OPT:
2193 case LFS_COMP_FLAGS_OPT:
2194 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2195 &lsa.lsa_comp_neg_flags);
2198 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2199 fprintf(stderr, "%s: inverted flags are not supported\n",
2203 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2205 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2212 case LFS_COMP_SET_OPT:
2215 case LFS_COMP_USE_PARENT_OPT:
2217 fprintf(stderr, "error: %s: --parent must be "
2218 "specified with --mirror-count|-N "
2219 "option\n", progname);
2222 setstripe_args_init(&lsa);
2224 case LFS_COMP_NO_VERIFY_OPT:
2225 mirror_flags |= NO_VERIFY;
2227 case LFS_MIRROR_FLAGS_OPT: {
2230 if (!mirror_mode || !last_mirror) {
2231 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2236 result = comp_str2flags(optarg, &last_mirror->m_flags,
2242 fprintf(stderr, "%s: inverted flags are not supported\n",
2247 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2249 "%s: unsupported mirror flags: %s\n",
2257 if (!migrate_mode) {
2259 "%s %s: -b|--block valid only for migrate command\n",
2263 migration_block = true;
2266 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2269 "%s %s: invalid stripe count '%s'\n",
2270 progname, argv[0], optarg);
2274 if (lsa.lsa_stripe_count == -1)
2275 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2278 /* delete the default striping pattern */
2282 if (lsa.lsa_comp_end != 0) {
2283 result = comp_args_to_layout(lpp, &lsa);
2286 "%s %s: invalid layout\n",
2291 setstripe_args_init_inherit(&lsa);
2294 if (arg_is_eof(optarg)) {
2295 lsa.lsa_comp_end = LUSTRE_EOF;
2297 result = llapi_parse_size(optarg,
2302 "%s %s: invalid component end '%s'\n",
2303 progname, argv[0], optarg);
2309 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2312 "%s %s: invalid stripe offset '%s'\n",
2313 progname, argv[0], optarg);
2316 if (lsa.lsa_stripe_off == -1)
2317 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2320 comp_id = strtoul(optarg, &end, 0);
2321 if (*end != '\0' || comp_id == 0 ||
2322 comp_id > LCME_ID_MAX) {
2324 "%s %s: invalid component ID '%s'\n",
2325 progname, argv[0], optarg);
2330 if (opc != SO_MIRROR_EXTEND) {
2332 "error: %s: invalid option: %s\n",
2333 progname, argv[optopt + 1]);
2336 if (last_mirror == NULL) {
2337 fprintf(stderr, "error: %s: '-N' must exist "
2338 "in front of '%s'\n",
2339 progname, argv[optopt + 1]);
2343 last_mirror->m_file = optarg;
2344 last_mirror->m_count = 1;
2348 if (strcmp(argv[optind - 1], "mdt") == 0) {
2349 /* Can be only the first component */
2350 if (layout != NULL) {
2352 fprintf(stderr, "error: 'mdt' layout "
2353 "can be only the first one\n");
2356 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2358 fprintf(stderr, "error: 'mdt' layout "
2359 "size is too big\n");
2362 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2363 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2365 fprintf(stderr, "error: layout '%s' is "
2366 "unknown, supported layouts are: "
2367 "'mdt', 'raid0'\n", argv[optind]);
2372 if (!migrate_mode) {
2374 "%s %s: -m|--mdt-index valid only for migrate command\n",
2378 mdt_idx_arg = optarg;
2381 if (!migrate_mode) {
2383 "%s %s: -n|--non-block valid only for migrate command\n",
2387 migration_flags |= MIGRATION_NONBLOCK;
2390 if (opc == SO_SETSTRIPE) {
2391 opc = SO_MIRROR_CREATE;
2395 if (optarg != NULL) {
2396 mirror_count = strtoul(optarg, &end, 0);
2397 if (*end != '\0' || mirror_count == 0) {
2399 "error: %s: bad mirror count: %s\n",
2406 new_mirror = lfs_mirror_alloc();
2407 new_mirror->m_count = mirror_count;
2409 if (mirror_list == NULL)
2410 mirror_list = new_mirror;
2412 if (last_mirror != NULL) {
2413 /* wrap up last mirror */
2414 if (lsa.lsa_comp_end == 0)
2415 lsa.lsa_comp_end = LUSTRE_EOF;
2417 result = comp_args_to_layout(lpp, &lsa);
2419 lfs_mirror_free(new_mirror);
2423 setstripe_args_init_inherit(&lsa);
2425 last_mirror->m_next = new_mirror;
2428 last_mirror = new_mirror;
2429 lpp = &last_mirror->m_layout;
2432 lsa.lsa_nr_tgts = parse_targets(osts,
2433 sizeof(osts) / sizeof(__u32),
2434 lsa.lsa_nr_tgts, optarg);
2435 if (lsa.lsa_nr_tgts < 0) {
2437 "%s %s: invalid OST target(s) '%s'\n",
2438 progname, argv[0], optarg);
2442 lsa.lsa_tgts = osts;
2443 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2444 lsa.lsa_stripe_off = osts[0];
2449 lsa.lsa_pool_name = optarg;
2452 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2456 "%s %s: invalid stripe size '%s'\n",
2457 progname, argv[0], optarg);
2462 if (!migrate_mode) {
2464 "%s %s: -v|--verbose valid only for migrate command\n",
2468 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2471 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2472 progname, argv[0], argv[optind - 1]);
2477 fname = argv[optind];
2479 if (optind == argc) {
2480 fprintf(stderr, "%s %s: FILE must be specified\n",
2485 if (mirror_mode && mirror_count == 0) {
2487 "error: %s: --mirror-count|-N option is required\n",
2494 if (lsa.lsa_comp_end == 0)
2495 lsa.lsa_comp_end = LUSTRE_EOF;
2498 if (lsa.lsa_comp_end != 0) {
2499 result = comp_args_to_layout(lpp, &lsa);
2504 if (mirror_flags & NO_VERIFY) {
2505 if (opc != SO_MIRROR_EXTEND) {
2507 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2511 } else if (!has_m_file) {
2513 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2520 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2521 * altered by user space tool, so we don't need to support the
2522 * --component-set for this moment. */
2523 if (comp_set && !comp_id) {
2524 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2529 if ((delete + comp_set + comp_del + comp_add) > 1) {
2531 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2536 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2537 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2539 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2544 if ((comp_set || comp_del) &&
2545 (setstripe_args_specified(&lsa) || layout != NULL)) {
2547 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
2552 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
2554 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
2559 if (comp_add || comp_del) {
2562 result = lstat(fname, &st);
2563 if (result == 0 && S_ISDIR(st.st_mode)) {
2565 "%s setstripe: cannot use --component-add or --component-del for directory\n",
2571 fprintf(stderr, "error: %s: can't use --component-add "
2572 "or --component-del for mirror operation\n",
2579 if (layout == NULL) {
2581 "%s %s: option -E must be specified with --component-add\n",
2586 result = adjust_first_extent(fname, layout);
2587 if (result == -ENODATA)
2589 else if (result != 0)
2593 if (mdt_idx_arg != NULL && optind > 3) {
2595 "%s %s: option -m cannot be used with other options\n",
2600 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
2602 "%s %s: options --non-block and --block are mutually exclusive\n",
2607 if (!comp_del && !comp_set && comp_id != 0) {
2609 "%s %s: option -I can only be used with --component-del\n",
2614 if (mdt_idx_arg != NULL) {
2615 /* initialize migrate mdt parameters */
2616 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
2618 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
2619 progname, argv[0], mdt_idx_arg);
2622 migrate_mdt_param.fp_migrate = 1;
2623 } else if (layout == NULL) {
2624 /* initialize stripe parameters */
2625 param = calloc(1, offsetof(typeof(*param),
2626 lsp_osts[lsa.lsa_nr_tgts]));
2627 if (param == NULL) {
2629 "%s %s: cannot allocate memory for parameters: %s\n",
2630 progname, argv[0], strerror(ENOMEM));
2635 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
2636 param->lsp_stripe_size = lsa.lsa_stripe_size;
2637 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2638 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
2639 param->lsp_stripe_count = -1;
2641 param->lsp_stripe_count = lsa.lsa_stripe_count;
2643 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2644 param->lsp_stripe_offset = -1;
2646 param->lsp_stripe_offset = lsa.lsa_stripe_off;
2647 param->lsp_pool = lsa.lsa_pool_name;
2648 param->lsp_is_specific = false;
2649 if (lsa.lsa_nr_tgts > 0) {
2650 if (lsa.lsa_stripe_count > 0 &&
2651 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2652 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2653 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
2654 fprintf(stderr, "error: %s: stripe count %lld "
2655 "doesn't match the number of OSTs: %d\n"
2656 , argv[0], lsa.lsa_stripe_count,
2662 param->lsp_is_specific = true;
2663 param->lsp_stripe_count = lsa.lsa_nr_tgts;
2664 memcpy(param->lsp_osts, osts,
2665 sizeof(*osts) * lsa.lsa_nr_tgts);
2669 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
2670 if (mdt_idx_arg != NULL) {
2671 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
2672 } else if (migrate_mode) {
2673 result = lfs_migrate(fname, migration_flags, param,
2675 } else if (comp_set != 0) {
2676 result = lfs_component_set(fname, comp_id,
2678 lsa.lsa_comp_neg_flags);
2679 } else if (comp_del != 0) {
2680 result = lfs_component_del(fname, comp_id,
2682 lsa.lsa_comp_neg_flags);
2683 } else if (comp_add != 0) {
2684 result = lfs_component_add(fname, layout);
2685 } else if (opc == SO_MIRROR_CREATE) {
2686 result = mirror_create(fname, mirror_list);
2687 } else if (opc == SO_MIRROR_EXTEND) {
2688 result = mirror_extend(fname, mirror_list,
2690 } else if (layout != NULL) {
2691 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
2698 result = llapi_file_open_param(fname,
2707 /* Save the first error encountered. */
2715 llapi_layout_free(layout);
2716 lfs_mirror_list_free(mirror_list);
2721 llapi_layout_free(layout);
2722 lfs_mirror_list_free(mirror_list);
2726 static int lfs_poollist(int argc, char **argv)
2731 return llapi_poollist(argv[1]);
2734 static int set_time(time_t *time, time_t *set, char *str)
2741 else if (str[0] == '-')
2747 t = strtol(str, NULL, 0);
2748 if (*time < t * 24 * 60 * 60) {
2751 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
2755 *set = *time - t * 24 * 60 * 60;
2758 static int name2uid(unsigned int *id, const char *name)
2760 struct passwd *passwd;
2762 passwd = getpwnam(name);
2765 *id = passwd->pw_uid;
2770 static int name2gid(unsigned int *id, const char *name)
2772 struct group *group;
2774 group = getgrnam(name);
2777 *id = group->gr_gid;
2782 static inline int name2projid(unsigned int *id, const char *name)
2787 static int uid2name(char **name, unsigned int id)
2789 struct passwd *passwd;
2791 passwd = getpwuid(id);
2794 *name = passwd->pw_name;
2799 static inline int gid2name(char **name, unsigned int id)
2801 struct group *group;
2803 group = getgrgid(id);
2806 *name = group->gr_name;
2811 static int name2layout(__u32 *layout, char *name)
2813 char *ptr, *layout_name;
2816 for (ptr = name; ; ptr = NULL) {
2817 layout_name = strtok(ptr, ",");
2818 if (layout_name == NULL)
2820 if (strcmp(layout_name, "released") == 0)
2821 *layout |= LOV_PATTERN_F_RELEASED;
2822 else if (strcmp(layout_name, "raid0") == 0)
2823 *layout |= LOV_PATTERN_RAID0;
2824 else if (strcmp(layout_name, "mdt") == 0)
2825 *layout |= LOV_PATTERN_MDT;
2832 static int lfs_find(int argc, char **argv)
2837 struct find_param param = {
2841 struct option long_opts[] = {
2842 { .val = 'A', .name = "atime", .has_arg = required_argument },
2843 { .val = LFS_COMP_COUNT_OPT,
2844 .name = "comp-count", .has_arg = required_argument },
2845 { .val = LFS_COMP_COUNT_OPT,
2846 .name = "component-count",
2847 .has_arg = required_argument },
2848 { .val = LFS_COMP_FLAGS_OPT,
2849 .name = "comp-flags", .has_arg = required_argument },
2850 { .val = LFS_COMP_FLAGS_OPT,
2851 .name = "component-flags",
2852 .has_arg = required_argument },
2853 { .val = LFS_COMP_START_OPT,
2854 .name = "comp-start", .has_arg = required_argument },
2855 { .val = LFS_COMP_START_OPT,
2856 .name = "component-start",
2857 .has_arg = required_argument },
2858 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
2859 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
2860 { .val = 'C', .name = "ctime", .has_arg = required_argument },
2861 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
2862 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
2863 { .val = 'E', .name = "component-end",
2864 .has_arg = required_argument },
2865 { .val = 'g', .name = "gid", .has_arg = required_argument },
2866 { .val = 'G', .name = "group", .has_arg = required_argument },
2867 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
2868 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
2869 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
2870 /*{"component-id", required_argument, 0, 'I'},*/
2871 { .val = 'L', .name = "layout", .has_arg = required_argument },
2872 { .val = 'm', .name = "mdt", .has_arg = required_argument },
2873 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
2874 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
2875 { .val = 'M', .name = "mtime", .has_arg = required_argument },
2876 { .val = 'n', .name = "name", .has_arg = required_argument },
2877 /* reserve {"or", no_argument, , 0, 'o'}, to match find(1) */
2878 { .val = 'O', .name = "obd", .has_arg = required_argument },
2879 { .val = 'O', .name = "ost", .has_arg = required_argument },
2880 /* no short option for pool, p/P already used */
2881 { .val = LFS_POOL_OPT,
2882 .name = "pool", .has_arg = required_argument },
2883 { .val = 'p', .name = "print0", .has_arg = no_argument },
2884 { .val = 'P', .name = "print", .has_arg = no_argument },
2885 { .val = LFS_PROJID_OPT,
2886 .name = "projid", .has_arg = required_argument },
2887 { .val = 's', .name = "size", .has_arg = required_argument },
2888 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2889 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2890 { .val = 't', .name = "type", .has_arg = required_argument },
2891 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
2892 { .val = 'u', .name = "uid", .has_arg = required_argument },
2893 { .val = 'U', .name = "user", .has_arg = required_argument },
2905 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
2906 while ((c = getopt_long_only(argc, argv,
2907 "-A:c:C:D:E:g:G:H:i:L:m:M:n:O:Ppqrs:S:t:T:u:U:v",
2908 long_opts, NULL)) >= 0) {
2913 /* '!' is part of option */
2914 /* when getopt_long_only() finds a string which is not
2915 * an option nor a known option argument it returns 1
2916 * in that case if we already have found pathstart and pathend
2917 * (i.e. we have the list of pathnames),
2918 * the only supported value is "!"
2920 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
2921 if (!isoption && pathend != -1) {
2922 fprintf(stderr, "err: %s: filename|dirname must either "
2923 "precede options or follow options\n",
2928 if (!isoption && pathstart == -1)
2929 pathstart = optind - 1;
2930 if (isoption && pathstart != -1 && pathend == -1)
2931 pathend = optind - 2;
2937 /* unknown; opt is "!" or path component,
2938 * checking done above.
2940 if (strcmp(optarg, "!") == 0)
2944 xtime = ¶m.fp_atime;
2945 xsign = ¶m.fp_asign;
2946 param.fp_exclude_atime = !!neg_opt;
2947 /* no break, this falls through to 'C' for ctime */
2950 xtime = ¶m.fp_ctime;
2951 xsign = ¶m.fp_csign;
2952 param.fp_exclude_ctime = !!neg_opt;
2954 /* no break, this falls through to 'M' for mtime */
2957 xtime = ¶m.fp_mtime;
2958 xsign = ¶m.fp_msign;
2959 param.fp_exclude_mtime = !!neg_opt;
2961 rc = set_time(&t, xtime, optarg);
2962 if (rc == INT_MAX) {
2969 case LFS_COMP_COUNT_OPT:
2970 if (optarg[0] == '+') {
2971 param.fp_comp_count_sign = -1;
2973 } else if (optarg[0] == '-') {
2974 param.fp_comp_count_sign = 1;
2978 param.fp_comp_count = strtoul(optarg, &endptr, 0);
2979 if (*endptr != '\0') {
2980 fprintf(stderr, "error: bad component count "
2984 param.fp_check_comp_count = 1;
2985 param.fp_exclude_comp_count = !!neg_opt;
2987 case LFS_COMP_FLAGS_OPT:
2988 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
2989 ¶m.fp_comp_neg_flags);
2991 fprintf(stderr, "error: bad component flags "
2995 param.fp_check_comp_flags = 1;
2997 __u32 flags = param.fp_comp_neg_flags;
2998 param.fp_comp_neg_flags = param.fp_comp_flags;
2999 param.fp_comp_flags = flags;
3002 case LFS_COMP_START_OPT:
3003 if (optarg[0] == '+') {
3004 param.fp_comp_start_sign = -1;
3006 } else if (optarg[0] == '-') {
3007 param.fp_comp_start_sign = 1;
3011 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3012 ¶m.fp_comp_start_units, 0);
3014 fprintf(stderr, "error: bad component start "
3018 param.fp_check_comp_start = 1;
3019 param.fp_exclude_comp_start = !!neg_opt;
3022 if (optarg[0] == '+') {
3023 param.fp_stripe_count_sign = -1;
3025 } else if (optarg[0] == '-') {
3026 param.fp_stripe_count_sign = 1;
3030 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3031 if (*endptr != '\0') {
3032 fprintf(stderr,"error: bad stripe_count '%s'\n",
3037 param.fp_check_stripe_count = 1;
3038 param.fp_exclude_stripe_count = !!neg_opt;
3041 param.fp_max_depth = strtol(optarg, 0, 0);
3044 if (optarg[0] == '+') {
3045 param.fp_comp_end_sign = -1;
3047 } else if (optarg[0] == '-') {
3048 param.fp_comp_end_sign = 1;
3052 if (arg_is_eof(optarg)) {
3053 param.fp_comp_end = LUSTRE_EOF;
3054 param.fp_comp_end_units = 1;
3057 rc = llapi_parse_size(optarg,
3059 ¶m.fp_comp_end_units, 0);
3062 fprintf(stderr, "error: bad component end "
3066 param.fp_check_comp_end = 1;
3067 param.fp_exclude_comp_end = !!neg_opt;
3071 rc = name2gid(¶m.fp_gid, optarg);
3073 param.fp_gid = strtoul(optarg, &endptr, 10);
3074 if (*endptr != '\0') {
3075 fprintf(stderr, "Group/GID: %s cannot "
3076 "be found.\n", optarg);
3081 param.fp_exclude_gid = !!neg_opt;
3082 param.fp_check_gid = 1;
3085 param.fp_hash_type = check_hashtype(optarg);
3086 if (param.fp_hash_type == 0) {
3087 fprintf(stderr, "error: bad hash_type '%s'\n",
3092 param.fp_check_hash_type = 1;
3093 param.fp_exclude_hash_type = !!neg_opt;
3096 ret = name2layout(¶m.fp_layout, optarg);
3099 param.fp_exclude_layout = !!neg_opt;
3100 param.fp_check_layout = 1;
3104 rc = name2uid(¶m.fp_uid, optarg);
3106 param.fp_uid = strtoul(optarg, &endptr, 10);
3107 if (*endptr != '\0') {
3108 fprintf(stderr, "User/UID: %s cannot "
3109 "be found.\n", optarg);
3114 param.fp_exclude_uid = !!neg_opt;
3115 param.fp_check_uid = 1;
3118 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3120 "Pool name %s is too long"
3121 " (max is %d)\n", optarg,
3126 /* we do check for empty pool because empty pool
3127 * is used to find V1 lov attributes */
3128 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3129 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3130 param.fp_exclude_pool = !!neg_opt;
3131 param.fp_check_pool = 1;
3134 param.fp_pattern = (char *)optarg;
3135 param.fp_exclude_pattern = !!neg_opt;
3140 char *buf, *token, *next, *p;
3144 buf = strdup(optarg);
3150 param.fp_exclude_obd = !!neg_opt;
3153 while (token && *token) {
3154 token = strchr(token, ',');
3161 param.fp_exclude_mdt = !!neg_opt;
3162 param.fp_num_alloc_mdts += len;
3163 tmp = realloc(param.fp_mdt_uuid,
3164 param.fp_num_alloc_mdts *
3165 sizeof(*param.fp_mdt_uuid));
3171 param.fp_mdt_uuid = tmp;
3173 param.fp_exclude_obd = !!neg_opt;
3174 param.fp_num_alloc_obds += len;
3175 tmp = realloc(param.fp_obd_uuid,
3176 param.fp_num_alloc_obds *
3177 sizeof(*param.fp_obd_uuid));
3183 param.fp_obd_uuid = tmp;
3185 for (token = buf; token && *token; token = next) {
3186 struct obd_uuid *puuid;
3189 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3192 ¶m.fp_obd_uuid[param.fp_num_obds++];
3194 p = strchr(token, ',');
3201 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3206 strncpy(puuid->uuid, token,
3207 sizeof(puuid->uuid));
3215 param.fp_zero_end = 1;
3219 case LFS_PROJID_OPT:
3220 rc = name2projid(¶m.fp_projid, optarg);
3222 param.fp_projid = strtoul(optarg, &endptr, 10);
3223 if (*endptr != '\0') {
3225 "Invalid project ID: %s",
3231 param.fp_exclude_projid = !!neg_opt;
3232 param.fp_check_projid = 1;
3235 if (optarg[0] == '+') {
3236 param.fp_size_sign = -1;
3238 } else if (optarg[0] == '-') {
3239 param.fp_size_sign = 1;
3243 ret = llapi_parse_size(optarg, ¶m.fp_size,
3244 ¶m.fp_size_units, 0);
3246 fprintf(stderr, "error: bad file size '%s'\n",
3250 param.fp_check_size = 1;
3251 param.fp_exclude_size = !!neg_opt;
3254 if (optarg[0] == '+') {
3255 param.fp_stripe_size_sign = -1;
3257 } else if (optarg[0] == '-') {
3258 param.fp_stripe_size_sign = 1;
3262 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3263 ¶m.fp_stripe_size_units, 0);
3265 fprintf(stderr, "error: bad stripe_size '%s'\n",
3269 param.fp_check_stripe_size = 1;
3270 param.fp_exclude_stripe_size = !!neg_opt;
3273 param.fp_exclude_type = !!neg_opt;
3274 switch (optarg[0]) {
3276 param.fp_type = S_IFBLK;
3279 param.fp_type = S_IFCHR;
3282 param.fp_type = S_IFDIR;
3285 param.fp_type = S_IFREG;
3288 param.fp_type = S_IFLNK;
3291 param.fp_type = S_IFIFO;
3294 param.fp_type = S_IFSOCK;
3297 fprintf(stderr, "error: %s: bad type '%s'\n",
3304 if (optarg[0] == '+') {
3305 param.fp_mdt_count_sign = -1;
3307 } else if (optarg[0] == '-') {
3308 param.fp_mdt_count_sign = 1;
3312 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3313 if (*endptr != '\0') {
3314 fprintf(stderr, "error: bad mdt_count '%s'\n",
3319 param.fp_check_mdt_count = 1;
3320 param.fp_exclude_mdt_count = !!neg_opt;
3328 if (pathstart == -1) {
3329 fprintf(stderr, "error: %s: no filename|pathname\n",
3333 } else if (pathend == -1) {
3339 rc = llapi_find(argv[pathstart], ¶m);
3340 if (rc != 0 && ret == 0)
3342 } while (++pathstart < pathend);
3345 fprintf(stderr, "error: %s failed for %s.\n",
3346 argv[0], argv[optind - 1]);
3348 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3349 free(param.fp_obd_uuid);
3351 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3352 free(param.fp_mdt_uuid);
3357 static int lfs_getstripe_internal(int argc, char **argv,
3358 struct find_param *param)
3360 struct option long_opts[] = {
3361 { .val = LFS_COMP_COUNT_OPT,
3362 .name = "comp-count", .has_arg = no_argument },
3363 { .val = LFS_COMP_COUNT_OPT,
3364 .name = "component-count", .has_arg = no_argument },
3365 { .val = LFS_COMP_FLAGS_OPT,
3366 .name = "comp-flags", .has_arg = optional_argument },
3367 { .val = LFS_COMP_FLAGS_OPT,
3368 .name = "component-flags", .has_arg = optional_argument },
3369 { .val = LFS_COMP_START_OPT,
3370 .name = "comp-start", .has_arg = optional_argument },
3371 { .val = LFS_COMP_START_OPT,
3372 .name = "component-start", .has_arg = optional_argument },
3373 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3374 /* This formerly implied "stripe-count", but was explicitly
3375 * made "stripe-count" for consistency with other options,
3376 * and to separate it from "mdt-count" when DNE arrives. */
3377 { .val = 'c', .name = "count", .has_arg = no_argument },
3379 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3380 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3381 { .val = 'd', .name = "directory", .has_arg = no_argument },
3382 { .val = 'D', .name = "default", .has_arg = no_argument },
3383 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3384 { .val = 'E', .name = "component-end",
3385 .has_arg = optional_argument },
3386 { .val = 'F', .name = "fid", .has_arg = no_argument },
3387 { .val = 'g', .name = "generation", .has_arg = no_argument },
3388 /* dirstripe { .val = 'H', .name = "mdt-hash",
3389 * .has_arg = required_argument }, */
3390 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3391 /* This formerly implied "stripe-index", but was explicitly
3392 * made "stripe-index" for consistency with other options,
3393 * and to separate it from "mdt-index" when DNE arrives. */
3394 { .val = 'i', .name = "index", .has_arg = no_argument },
3396 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3397 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3398 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3399 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3400 { .val = 'L', .name = "layout", .has_arg = no_argument },
3401 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3402 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3403 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3404 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3405 { .val = 'M', .name = "mdt-index", .has_arg = no_argument },
3406 { .val = 'M', .name = "mdt_index", .has_arg = no_argument },
3408 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3409 /* This formerly implied "stripe-index", but was confusing
3410 * with "file offset" (which will eventually be needed for
3411 * with different layouts by offset), so deprecate it. */
3412 { .val = 'o', .name = "offset", .has_arg = no_argument },
3414 { .val = 'O', .name = "obd", .has_arg = required_argument },
3415 { .val = 'O', .name = "ost", .has_arg = required_argument },
3416 { .val = 'p', .name = "pool", .has_arg = no_argument },
3417 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3418 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3419 { .val = 'R', .name = "raw", .has_arg = no_argument },
3420 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3421 /* This formerly implied "--stripe-size", but was confusing
3422 * with "lfs find --size|-s", which means "file size", so use
3423 * the consistent "--stripe-size|-S" for all commands. */
3424 { .val = 's', .name = "size", .has_arg = no_argument },
3426 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3427 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3428 /* dirstripe { .val = 'T', .name = "mdt-count",
3429 * .has_arg = required_argument }, */
3430 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3431 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3436 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
3437 long_opts, NULL)) != -1) {
3440 if (strcmp(argv[optind - 1], "--count") == 0)
3441 fprintf(stderr, "warning: '--count' deprecated,"
3442 " use '--stripe-count' instead\n");
3443 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3444 param->fp_verbose |= VERBOSE_COUNT;
3445 param->fp_max_depth = 0;
3448 case LFS_COMP_COUNT_OPT:
3449 param->fp_verbose |= VERBOSE_COMP_COUNT;
3450 param->fp_max_depth = 0;
3452 case LFS_COMP_FLAGS_OPT:
3453 if (optarg != NULL) {
3454 rc = comp_str2flags(optarg,
3455 ¶m->fp_comp_flags,
3456 ¶m->fp_comp_neg_flags);
3458 fprintf(stderr, "error: %s bad "
3459 "component flags '%s'.\n",
3463 param->fp_check_comp_flags = 1;
3465 param->fp_verbose |= VERBOSE_COMP_FLAGS;
3466 param->fp_max_depth = 0;
3469 case LFS_COMP_START_OPT:
3470 if (optarg != NULL) {
3472 if (tmp[0] == '+') {
3473 param->fp_comp_start_sign = -1;
3475 } else if (tmp[0] == '-') {
3476 param->fp_comp_start_sign = 1;
3479 rc = llapi_parse_size(tmp,
3480 ¶m->fp_comp_start,
3481 ¶m->fp_comp_start_units, 0);
3483 fprintf(stderr, "error: %s bad "
3484 "component start '%s'.\n",
3488 param->fp_check_comp_start = 1;
3491 param->fp_verbose |= VERBOSE_COMP_START;
3492 param->fp_max_depth = 0;
3496 param->fp_max_depth = 0;
3499 param->fp_get_default_lmv = 1;
3502 if (optarg != NULL) {
3504 if (tmp[0] == '+') {
3505 param->fp_comp_end_sign = -1;
3507 } else if (tmp[0] == '-') {
3508 param->fp_comp_end_sign = 1;
3512 if (arg_is_eof(tmp)) {
3513 param->fp_comp_end = LUSTRE_EOF;
3514 param->fp_comp_end_units = 1;
3517 rc = llapi_parse_size(tmp,
3518 ¶m->fp_comp_end,
3519 ¶m->fp_comp_end_units, 0);
3522 fprintf(stderr, "error: %s bad "
3523 "component end '%s'.\n",
3527 param->fp_check_comp_end = 1;
3529 param->fp_verbose |= VERBOSE_COMP_END;
3530 param->fp_max_depth = 0;
3534 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3535 param->fp_verbose |= VERBOSE_DFID;
3536 param->fp_max_depth = 0;
3540 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3541 param->fp_verbose |= VERBOSE_GENERATION;
3542 param->fp_max_depth = 0;
3545 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3547 fprintf(stderr, "warning: '--offset|-o' deprecated, "
3548 "use '--stripe-index|-i' instead\n");
3551 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
3552 if (strcmp(argv[optind - 1], "--index") == 0)
3553 fprintf(stderr, "warning: '--index' deprecated"
3554 ", use '--stripe-index' instead\n");
3556 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3557 param->fp_verbose |= VERBOSE_OFFSET;
3558 param->fp_max_depth = 0;
3562 if (optarg != NULL) {
3563 param->fp_comp_id = strtoul(optarg, &end, 0);
3564 if (*end != '\0' || param->fp_comp_id == 0 ||
3565 param->fp_comp_id > LCME_ID_MAX) {
3566 fprintf(stderr, "error: %s bad "
3567 "component id '%s'\n",
3571 param->fp_check_comp_id = 1;
3574 param->fp_max_depth = 0;
3575 param->fp_verbose |= VERBOSE_COMP_ID;
3579 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3580 param->fp_verbose |= VERBOSE_LAYOUT;
3581 param->fp_max_depth = 0;
3584 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3586 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3587 fprintf(stderr, "warning: '-M' deprecated"
3588 ", use '-m' instead\n");
3592 if (!(param->fp_verbose & VERBOSE_DETAIL))
3593 param->fp_max_depth = 0;
3594 param->fp_verbose |= VERBOSE_MDTINDEX;
3597 if (param->fp_obd_uuid) {
3599 "error: %s: only one obduuid allowed",
3603 param->fp_obd_uuid = (struct obd_uuid *)optarg;
3606 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3607 param->fp_verbose |= VERBOSE_POOL;
3608 param->fp_max_depth = 0;
3615 param->fp_recursive = 1;
3620 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3622 fprintf(stderr, "warning: '--size|-s' deprecated, "
3623 "use '--stripe-size|-S' instead\n");
3624 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0) */
3626 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3627 param->fp_verbose |= VERBOSE_SIZE;
3628 param->fp_max_depth = 0;
3632 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
3645 if (param->fp_recursive)
3646 param->fp_max_depth = -1;
3647 else if (param->fp_verbose & VERBOSE_DETAIL)
3648 param->fp_max_depth = 1;
3650 if (!param->fp_verbose)
3651 param->fp_verbose = VERBOSE_DEFAULT;
3652 if (param->fp_quiet)
3653 param->fp_verbose = VERBOSE_OBJID;
3656 rc = llapi_getstripe(argv[optind], param);
3657 } while (++optind < argc && !rc);
3660 fprintf(stderr, "error: %s failed for %s.\n",
3661 argv[0], argv[optind - 1]);
3665 static int lfs_tgts(int argc, char **argv)
3667 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3668 struct find_param param;
3669 int index = 0, rc=0;
3674 if (argc == 2 && !realpath(argv[1], path)) {
3676 fprintf(stderr, "error: invalid path '%s': %s\n",
3677 argv[1], strerror(-rc));
3681 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
3682 /* Check if we have a mount point */
3683 if (mntdir[0] == '\0')
3686 memset(¶m, 0, sizeof(param));
3687 if (!strcmp(argv[0], "mdts"))
3688 param.fp_get_lmv = 1;
3690 rc = llapi_ostlist(mntdir, ¶m);
3692 fprintf(stderr, "error: %s: failed on %s\n",
3695 if (path[0] != '\0')
3697 memset(mntdir, 0, PATH_MAX);
3703 static int lfs_getstripe(int argc, char **argv)
3705 struct find_param param = { 0 };
3707 param.fp_max_depth = 1;
3708 return lfs_getstripe_internal(argc, argv, ¶m);
3712 static int lfs_getdirstripe(int argc, char **argv)
3714 struct find_param param = { 0 };
3715 struct option long_opts[] = {
3716 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3717 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
3719 { .val = 'D', .name = "default", .has_arg = no_argument },
3720 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
3721 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
3722 { .val = 'O', .name = "obd", .has_arg = required_argument },
3723 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3724 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3725 { .val = 't', .name = "mdt-hash", .has_arg = no_argument },
3727 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
3728 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3732 param.fp_get_lmv = 1;
3734 while ((c = getopt_long(argc, argv,
3735 "cDHiO:rtTy", long_opts, NULL)) != -1)
3739 if (param.fp_obd_uuid) {
3741 "error: %s: only one obduuid allowed",
3745 param.fp_obd_uuid = (struct obd_uuid *)optarg;
3747 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3749 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 10, 50, 0)
3750 fprintf(stderr, "warning: '-c' deprecated"
3751 ", use '-T' instead\n");
3755 param.fp_verbose |= VERBOSE_COUNT;
3758 param.fp_verbose |= VERBOSE_OFFSET;
3760 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3764 param.fp_verbose |= VERBOSE_HASH_TYPE;
3767 param.fp_get_default_lmv = 1;
3770 param.fp_recursive = 1;
3783 if (param.fp_recursive)
3784 param.fp_max_depth = -1;
3786 if (!param.fp_verbose)
3787 param.fp_verbose = VERBOSE_DEFAULT;
3790 rc = llapi_getstripe(argv[optind], ¶m);
3791 } while (++optind < argc && !rc);
3794 fprintf(stderr, "error: %s failed for %s.\n",
3795 argv[0], argv[optind - 1]);
3800 static int lfs_setdirstripe(int argc, char **argv)
3804 struct lfs_setstripe_args lsa;
3805 struct llapi_stripe_param *param = NULL;
3806 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
3809 char *mode_opt = NULL;
3810 bool default_stripe = false;
3811 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
3812 mode_t previous_mode = 0;
3813 bool delete = false;
3815 struct option long_opts[] = {
3816 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3817 { .val = 'c', .name = "count", .has_arg = required_argument },
3819 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
3820 { .val = 'd', .name = "delete", .has_arg = no_argument },
3821 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3822 { .val = 'i', .name = "index", .has_arg = required_argument },
3824 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
3825 { .val = 'm', .name = "mode", .has_arg = required_argument },
3826 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3827 { .val = 't', .name = "hash-type", .has_arg = required_argument },
3828 { .val = 't', .name = "mdt-hash", .has_arg = required_argument },
3830 {"mdt-hash", required_argument, 0, 'H'},
3831 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3832 { .val = 'D', .name = "default_stripe",
3833 .has_arg = no_argument },
3835 { .val = 'D', .name = "default", .has_arg = no_argument },
3838 setstripe_args_init(&lsa);
3840 while ((c = getopt_long(argc, argv, "c:dDi:H:m:t:", long_opts,
3847 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3848 if (strcmp(argv[optind - 1], "--count") == 0)
3850 "%s %s: warning: '--count' deprecated, use '--mdt-count' instead\n",
3853 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3856 "%s %s: invalid stripe count '%s'\n",
3857 progname, argv[0], optarg);
3863 default_stripe = true;
3866 default_stripe = true;
3869 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3870 if (strcmp(argv[optind - 1], "--index") == 0)
3872 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
3875 lsa.lsa_nr_tgts = parse_targets(mdts,
3876 sizeof(mdts) / sizeof(__u32),
3877 lsa.lsa_nr_tgts, optarg);
3878 if (lsa.lsa_nr_tgts < 0) {
3880 "%s %s: invalid MDT target(s) '%s'\n",
3881 progname, argv[0], optarg);
3885 lsa.lsa_tgts = mdts;
3886 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3887 lsa.lsa_stripe_off = mdts[0];
3892 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3896 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3897 if (strcmp(argv[optind - 1], "--hash-type") == 0)
3899 "%s %s: warning: '--hash-type' deprecated, use '--mdt-hash' instead\n",
3902 lsa.lsa_pattern = check_hashtype(optarg);
3903 if (lsa.lsa_pattern == 0) {
3905 "%s %s: bad stripe hash type '%s'\n",
3906 progname, argv[0], optarg);
3911 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3912 progname, argv[0], argv[optind - 1]);
3917 if (optind == argc) {
3918 fprintf(stderr, "%s %s: DIR must be specified\n",
3923 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
3924 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
3926 "%s %s: stripe offset and count must be specified\n",
3932 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
3933 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
3935 "%s %s: cannot specify -d with -c or -i options\n",
3940 if (mode_opt != NULL) {
3941 mode = strtoul(mode_opt, &end, 8);
3944 "%s %s: bad MODE '%s'\n",
3945 progname, argv[0], mode_opt);
3948 previous_mode = umask(0);
3951 /* initialize stripe parameters */
3952 param = calloc(1, offsetof(typeof(*param), lsp_osts[lsa.lsa_nr_tgts]));
3953 if (param == NULL) {
3955 "%s %s: cannot allocate memory for parameters: %s\n",
3956 progname, argv[0], strerror(ENOMEM));
3960 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3961 param->lsp_stripe_count = lsa.lsa_stripe_count;
3962 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3963 param->lsp_stripe_offset = -1;
3965 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3966 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3967 param->lsp_stripe_pattern = lsa.lsa_pattern;
3969 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
3970 param->lsp_pool = lsa.lsa_pool_name;
3971 param->lsp_is_specific = false;
3972 if (lsa.lsa_nr_tgts > 1) {
3973 if (lsa.lsa_stripe_count > 0 &&
3974 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3975 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3976 fprintf(stderr, "error: %s: stripe count %lld doesn't "
3977 "match the number of MDTs: %d\n",
3978 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
3983 param->lsp_is_specific = true;
3984 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3985 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
3988 dname = argv[optind];
3991 result = llapi_dir_set_default_lmv(dname, param);
3993 result = llapi_dir_create_param(dname, mode, param);
3997 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
3998 progname, dname, strerror(-result));
4001 dname = argv[++optind];
4002 } while (dname != NULL);
4004 if (mode_opt != NULL)
4005 umask(previous_mode);
4012 static int lfs_rmentry(int argc, char **argv)
4019 fprintf(stderr, "error: %s: missing dirname\n",
4025 dname = argv[index];
4026 while (dname != NULL) {
4027 result = llapi_direntry_remove(dname);
4029 fprintf(stderr, "error: %s: remove dir entry '%s' "
4030 "failed\n", argv[0], dname);
4033 dname = argv[++index];
4038 static int lfs_mv(int argc, char **argv)
4040 struct find_param param = {
4047 struct option long_opts[] = {
4048 { .val = 'M', .name = "mdt-index", .has_arg = required_argument },
4049 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4052 while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
4055 param.fp_mdt_index = strtoul(optarg, &end, 0);
4057 fprintf(stderr, "%s: invalid MDT index'%s'\n",
4064 param.fp_verbose = VERBOSE_DETAIL;
4068 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
4069 argv[0], argv[optind - 1]);
4074 if (param.fp_mdt_index == -1) {
4075 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
4079 if (optind >= argc) {
4080 fprintf(stderr, "%s: missing operand path\n", argv[0]);
4084 param.fp_migrate = 1;
4085 rc = llapi_migrate_mdt(argv[optind], ¶m);
4087 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
4088 argv[0], argv[optind], param.fp_mdt_index,
4093 static int lfs_osts(int argc, char **argv)
4095 return lfs_tgts(argc, argv);
4098 static int lfs_mdts(int argc, char **argv)
4100 return lfs_tgts(argc, argv);
4103 #define COOK(value) \
4106 while (value > 1024) { \
4114 #define CDF "%11llu"
4115 #define HDF "%8.1f%c"
4120 MNTDF_INODES = 0x0001,
4121 MNTDF_COOKED = 0x0002,
4122 MNTDF_LAZY = 0x0004,
4123 MNTDF_VERBOSE = 0x0008,
4126 static int showdf(char *mntdir, struct obd_statfs *stat,
4127 char *uuid, enum mntdf_flags flags,
4128 char *type, int index, int rc)
4130 long long avail, used, total;
4132 char *suffix = "KMGTPEZY";
4133 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4134 char tbuf[3 * sizeof(__u64)];
4135 char ubuf[3 * sizeof(__u64)];
4136 char abuf[3 * sizeof(__u64)];
4137 char rbuf[3 * sizeof(__u64)];
4144 if (flags & MNTDF_INODES) {
4145 avail = stat->os_ffree;
4146 used = stat->os_files - stat->os_ffree;
4147 total = stat->os_files;
4149 int shift = flags & MNTDF_COOKED ? 0 : 10;
4151 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4152 used = ((stat->os_blocks - stat->os_bfree) *
4153 stat->os_bsize) >> shift;
4154 total = (stat->os_blocks * stat->os_bsize) >> shift;
4157 if ((used + avail) > 0)
4158 ratio = (double)used / (double)(used + avail);
4160 if (flags & MNTDF_COOKED) {
4164 cook_val = (double)total;
4167 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4170 snprintf(tbuf, sizeof(tbuf), CDF, total);
4172 cook_val = (double)used;
4175 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4178 snprintf(ubuf, sizeof(ubuf), CDF, used);
4180 cook_val = (double)avail;
4183 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4186 snprintf(abuf, sizeof(abuf), CDF, avail);
4188 snprintf(tbuf, sizeof(tbuf), CDF, total);
4189 snprintf(ubuf, sizeof(tbuf), CDF, used);
4190 snprintf(abuf, sizeof(tbuf), CDF, avail);
4193 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
4194 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4195 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4197 printf("[%s:%d]", type, index);
4199 if (stat->os_state) {
4201 * Each character represents the matching
4204 const char state_names[] = "DRSI";
4209 for (i = 0, state = stat->os_state;
4210 state && i < sizeof(state_names); i++) {
4211 if (!(state & (1 << i)))
4213 printf("%c", state_names[i]);
4221 printf(UUF": inactive device\n", uuid);
4224 printf(UUF": %s\n", uuid, strerror(-rc));
4231 struct ll_stat_type {
4236 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags)
4238 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4239 struct obd_uuid uuid_buf;
4240 char *poolname = NULL;
4241 struct ll_stat_type types[] = {
4242 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4243 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4244 { .st_name = NULL } };
4245 struct ll_stat_type *tp;
4246 __u64 ost_ffree = 0;
4254 poolname = strchr(pool, '.');
4255 if (poolname != NULL) {
4256 if (strncmp(fsname, pool, strlen(fsname))) {
4257 fprintf(stderr, "filesystem name incorrect\n");
4265 fd = open(mntdir, O_RDONLY);
4268 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4273 if (flags & MNTDF_INODES)
4274 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4275 "UUID", "Inodes", "IUsed", "IFree",
4276 "IUse%", "Mounted on");
4278 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4279 "UUID", flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4280 "Used", "Available", "Use%", "Mounted on");
4282 for (tp = types; tp->st_name != NULL; tp++) {
4283 for (index = 0; ; index++) {
4284 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4285 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4286 type = flags & MNTDF_LAZY ?
4287 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4288 rc2 = llapi_obd_fstatfs(fd, type, index,
4289 &stat_buf, &uuid_buf);
4294 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4295 if (!(flags & MNTDF_VERBOSE))
4297 } else if (rc2 < 0 && rc == 0) {
4301 if (poolname && tp->st_op == LL_STATFS_LOV &&
4302 llapi_search_ost(fsname, poolname,
4303 obd_uuid2str(&uuid_buf)) != 1)
4306 /* the llapi_obd_statfs() call may have returned with
4307 * an error, but if it filled in uuid_buf we will at
4308 * lease use that to print out a message for that OBD.
4309 * If we didn't get anything in the uuid_buf, then fill
4310 * it in so that we can print an error message. */
4311 if (uuid_buf.uuid[0] == '\0')
4312 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4313 "%s%04x", tp->st_name, index);
4314 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
4315 flags, tp->st_name, index, rc2);
4318 if (tp->st_op == LL_STATFS_LMV) {
4319 sum.os_ffree += stat_buf.os_ffree;
4320 sum.os_files += stat_buf.os_files;
4321 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4322 sum.os_blocks += stat_buf.os_blocks *
4324 sum.os_bfree += stat_buf.os_bfree *
4326 sum.os_bavail += stat_buf.os_bavail *
4328 ost_ffree += stat_buf.os_ffree;
4336 /* If we don't have as many objects free on the OST as inodes
4337 * on the MDS, we reduce the total number of inodes to
4338 * compensate, so that the "inodes in use" number is correct.
4339 * Matches ll_statfs_internal() so the results are consistent. */
4340 if (ost_ffree < sum.os_ffree) {
4341 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4342 sum.os_ffree = ost_ffree;
4345 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4351 static int lfs_df(int argc, char **argv)
4353 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4354 enum mntdf_flags flags = 0;
4355 int c, rc = 0, index = 0;
4356 char fsname[PATH_MAX] = "", *pool_name = NULL;
4357 struct option long_opts[] = {
4358 { .val = 'h', .name = "human-readable",
4359 .has_arg = no_argument },
4360 { .val = 'i', .name = "inodes", .has_arg = no_argument },
4361 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4362 { .val = 'p', .name = "pool", .has_arg = required_argument },
4363 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4366 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
4369 flags |= MNTDF_COOKED;
4372 flags |= MNTDF_INODES;
4375 flags |= MNTDF_LAZY;
4381 flags |= MNTDF_VERBOSE;
4387 if (optind < argc && !realpath(argv[optind], path)) {
4389 fprintf(stderr, "error: invalid path '%s': %s\n",
4390 argv[optind], strerror(-rc));
4394 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4395 /* Check if we have a mount point */
4396 if (mntdir[0] == '\0')
4399 rc = mntdf(mntdir, fsname, pool_name, flags);
4400 if (rc || path[0] != '\0')
4402 fsname[0] = '\0'; /* avoid matching in next loop */
4403 mntdir[0] = '\0'; /* avoid matching in next loop */
4409 static int lfs_getname(int argc, char **argv)
4411 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
4412 int rc = 0, index = 0, c;
4413 char buf[sizeof(struct obd_uuid)];
4415 while ((c = getopt(argc, argv, "h")) != -1)
4418 if (optind == argc) { /* no paths specified, get all paths. */
4419 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4420 rc = llapi_getname(mntdir, buf, sizeof(buf));
4423 "cannot get name for `%s': %s\n",
4424 mntdir, strerror(-rc));
4428 printf("%s %s\n", buf, mntdir);
4430 path[0] = fsname[0] = mntdir[0] = 0;
4432 } else { /* paths specified, only attempt to search these. */
4433 for (; optind < argc; optind++) {
4434 rc = llapi_getname(argv[optind], buf, sizeof(buf));
4437 "cannot get name for `%s': %s\n",
4438 argv[optind], strerror(-rc));
4442 printf("%s %s\n", buf, argv[optind]);
4448 static int lfs_check(int argc, char **argv)
4451 char mntdir[PATH_MAX] = {'\0'};
4458 fprintf(stderr, "%s check: server type must be specified\n",
4463 obd_types[0] = obd_type1;
4464 obd_types[1] = obd_type2;
4466 if (strcmp(argv[1], "osts") == 0) {
4467 strcpy(obd_types[0], "osc");
4468 } else if (strcmp(argv[1], "mds") == 0) {
4469 strcpy(obd_types[0], "mdc");
4470 } else if (strcmp(argv[1], "servers") == 0) {
4472 strcpy(obd_types[0], "osc");
4473 strcpy(obd_types[1], "mdc");
4475 fprintf(stderr, "%s check: unrecognized option '%s'\n",
4480 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
4481 if (rc < 0 || mntdir[0] == '\0') {
4483 "%s check: cannot find mounted Lustre filesystem: %s\n",
4484 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
4488 rc = llapi_target_check(num_types, obd_types, mntdir);
4490 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
4491 progname, argv[1], strerror(-rc));
4497 #ifdef HAVE_SYS_QUOTA_H
4498 #define ARG2INT(nr, str, msg) \
4501 nr = strtol(str, &endp, 0); \
4502 if (*endp != '\0') { \
4503 fprintf(stderr, "%s: bad %s '%s'\n", \
4504 progname, msg, str); \
4509 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
4511 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
4512 * returns the value or ULONG_MAX on integer overflow or incorrect format
4514 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
4515 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
4516 * 3. empty integer value is interpreted as 0
4518 static unsigned long str2sec(const char* timestr)
4520 const char spec[] = "smhdw";
4521 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4522 unsigned long val = 0;
4525 if (strpbrk(timestr, spec) == NULL) {
4526 /* no specifiers inside the time string,
4527 should treat it as an integer value */
4528 val = strtoul(timestr, &tail, 10);
4529 return *tail ? ULONG_MAX : val;
4532 /* format string is XXwXXdXXhXXmXXs */
4538 v = strtoul(timestr, &tail, 10);
4539 if (v == ULONG_MAX || *tail == '\0')
4540 /* value too large (ULONG_MAX or more)
4541 or missing specifier */
4544 ptr = strchr(spec, *tail);
4546 /* unknown specifier */
4551 /* check if product will overflow the type */
4552 if (!(v < ULONG_MAX / mult[ind]))
4555 ADD_OVERFLOW(val, mult[ind] * v);
4556 if (val == ULONG_MAX)
4568 #define ARG2ULL(nr, str, def_units) \
4570 unsigned long long limit, units = def_units; \
4573 rc = llapi_parse_size(str, &limit, &units, 1); \
4575 fprintf(stderr, "%s: invalid limit '%s'\n", \
4582 static inline int has_times_option(int argc, char **argv)
4586 for (i = 1; i < argc; i++)
4587 if (!strcmp(argv[i], "-t"))
4593 int lfs_setquota_times(int argc, char **argv)
4596 struct if_quotactl qctl;
4597 char *mnt, *obd_type = (char *)qctl.obd_type;
4598 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4599 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
4600 struct option long_opts[] = {
4601 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
4602 { .val = 'g', .name = "group", .has_arg = no_argument },
4603 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
4604 { .val = 'p', .name = "projid", .has_arg = no_argument },
4605 { .val = 't', .name = "times", .has_arg = no_argument },
4606 { .val = 'u', .name = "user", .has_arg = no_argument },
4610 memset(&qctl, 0, sizeof(qctl));
4611 qctl.qc_cmd = LUSTRE_Q_SETINFO;
4612 qctl.qc_type = ALLQUOTA;
4614 while ((c = getopt_long(argc, argv, "b:gi:ptu",
4615 long_opts, NULL)) != -1) {
4626 if (qctl.qc_type != ALLQUOTA) {
4627 fprintf(stderr, "error: -u/g/p can't be used "
4628 "more than once\n");
4631 qctl.qc_type = qtype;
4634 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
4635 fprintf(stderr, "error: bad block-grace: %s\n",
4639 dqb->dqb_valid |= QIF_BTIME;
4642 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
4643 fprintf(stderr, "error: bad inode-grace: %s\n",
4647 dqb->dqb_valid |= QIF_ITIME;
4649 case 't': /* Yes, of course! */
4651 default: /* getopt prints error message for us when opterr != 0 */
4656 if (qctl.qc_type == ALLQUOTA) {
4657 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
4661 if (optind != argc - 1) {
4662 fprintf(stderr, "error: unexpected parameters encountered\n");
4667 rc = llapi_quotactl(mnt, &qctl);
4670 fprintf(stderr, "%s %s ", obd_type,
4671 obd_uuid2str(&qctl.obd_uuid));
4672 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
4679 #define BSLIMIT (1 << 0)
4680 #define BHLIMIT (1 << 1)
4681 #define ISLIMIT (1 << 2)
4682 #define IHLIMIT (1 << 3)
4684 int lfs_setquota(int argc, char **argv)
4687 struct if_quotactl qctl;
4688 char *mnt, *obd_type = (char *)qctl.obd_type;
4689 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4690 struct option long_opts[] = {
4691 { .val = 'b', .name = "block-softlimit",
4692 .has_arg = required_argument },
4693 { .val = 'B', .name = "block-hardlimit",
4694 .has_arg = required_argument },
4695 { .val = 'g', .name = "group", .has_arg = required_argument },
4696 { .val = 'i', .name = "inode-softlimit",
4697 .has_arg = required_argument },
4698 { .val = 'I', .name = "inode-hardlimit",
4699 .has_arg = required_argument },
4700 { .val = 'p', .name = "projid", .has_arg = required_argument },
4701 { .val = 'u', .name = "user", .has_arg = required_argument },
4703 unsigned limit_mask = 0;
4707 if (has_times_option(argc, argv))
4708 return lfs_setquota_times(argc, argv);
4710 memset(&qctl, 0, sizeof(qctl));
4711 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
4712 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
4713 * so it can be used as a marker that qc_type
4714 * isn't reinitialized from command line */
4716 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
4717 long_opts, NULL)) != -1) {
4721 rc = name2uid(&qctl.qc_id, optarg);
4725 rc = name2gid(&qctl.qc_id, optarg);
4729 rc = name2projid(&qctl.qc_id, optarg);
4731 if (qctl.qc_type != ALLQUOTA) {
4733 "%s setquota: only one of -u, -g or -p may be specified\n",
4737 qctl.qc_type = qtype;
4739 qctl.qc_id = strtoul(optarg, &endptr, 10);
4740 if (*endptr != '\0') {
4742 "%s setquota: invalid id '%s'\n",
4749 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
4750 dqb->dqb_bsoftlimit >>= 10;
4751 limit_mask |= BSLIMIT;
4752 if (dqb->dqb_bsoftlimit &&
4753 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
4755 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
4756 "See '%s help setquota' or Lustre manual for details\n",
4757 progname, dqb->dqb_bsoftlimit,
4761 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
4762 dqb->dqb_bhardlimit >>= 10;
4763 limit_mask |= BHLIMIT;
4764 if (dqb->dqb_bhardlimit &&
4765 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
4767 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
4768 "See '%s help setquota' or Lustre manual for details\n",
4769 progname, dqb->dqb_bhardlimit,
4773 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
4774 limit_mask |= ISLIMIT;
4775 if (dqb->dqb_isoftlimit &&
4776 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
4778 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
4779 "See '%s help setquota' or Lustre manual for details\n",
4780 progname, dqb->dqb_isoftlimit,
4784 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
4785 limit_mask |= IHLIMIT;
4786 if (dqb->dqb_ihardlimit &&
4787 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
4789 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
4790 "See '%s help setquota' or Lustre manual for details\n",
4791 progname, dqb->dqb_ihardlimit,
4796 "%s setquota: unrecognized option '%s'\n",
4797 progname, argv[optind - 1]);
4802 if (qctl.qc_type == ALLQUOTA) {
4804 "%s setquota: either -u or -g must be specified\n",
4809 if (limit_mask == 0) {
4811 "%s setquota: at least one limit must be specified\n",
4816 if (optind != argc - 1) {
4818 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
4819 progname, argv[optind]);
4825 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
4826 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
4827 /* sigh, we can't just set blimits/ilimits */
4828 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
4829 .qc_type = qctl.qc_type,
4830 .qc_id = qctl.qc_id};
4832 rc = llapi_quotactl(mnt, &tmp_qctl);
4836 if (!(limit_mask & BHLIMIT))
4837 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
4838 if (!(limit_mask & BSLIMIT))
4839 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
4840 if (!(limit_mask & IHLIMIT))
4841 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
4842 if (!(limit_mask & ISLIMIT))
4843 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
4845 /* Keep grace times if we have got no softlimit arguments */
4846 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
4847 dqb->dqb_valid |= QIF_BTIME;
4848 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
4851 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
4852 dqb->dqb_valid |= QIF_ITIME;
4853 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
4857 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
4858 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
4860 rc = llapi_quotactl(mnt, &qctl);
4864 "%s setquota: cannot quotactl '%s' '%s': %s",
4866 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
4873 /* Converts seconds value into format string
4874 * result is returned in buf
4876 * 1. result is in descenting order: 1w2d3h4m5s
4877 * 2. zero fields are not filled (except for p. 3): 5d1s
4878 * 3. zero seconds value is presented as "0s"
4880 static char * __sec2str(time_t seconds, char *buf)
4882 const char spec[] = "smhdw";
4883 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4888 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
4889 c = seconds / mult[i];
4891 if (c > 0 || (i == 0 && buf == tail))
4892 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
4900 static void sec2str(time_t seconds, char *buf, int rc)
4907 tail = __sec2str(seconds, tail);
4909 if (rc && tail - buf < 39) {
4915 static void diff2str(time_t seconds, char *buf, time_t now)
4921 if (seconds <= now) {
4922 strcpy(buf, "none");
4925 __sec2str(seconds - now, buf);
4928 static void print_quota_title(char *name, struct if_quotactl *qctl,
4929 bool human_readable)
4931 printf("Disk quotas for %s %s (%cid %u):\n",
4932 qtype_name(qctl->qc_type), name,
4933 *qtype_name(qctl->qc_type), qctl->qc_id);
4934 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
4935 "Filesystem", human_readable ? "used" : "kbytes",
4936 "quota", "limit", "grace",
4937 "files", "quota", "limit", "grace");
4940 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
4943 snprintf(buf, buflen, "%ju", (uintmax_t)num);
4946 snprintf(buf, buflen, "%5.4gP",
4947 (double)num / ((__u64)1 << 40));
4949 snprintf(buf, buflen, "%5.4gT",
4950 (double)num / (1 << 30));
4952 snprintf(buf, buflen, "%5.4gG",
4953 (double)num / (1 << 20));
4955 snprintf(buf, buflen, "%5.4gM",
4956 (double)num / (1 << 10));
4958 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
4962 #define STRBUF_LEN 32
4963 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
4970 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
4971 int bover = 0, iover = 0;
4972 struct obd_dqblk *dqb = &qctl->qc_dqblk;
4973 char numbuf[3][STRBUF_LEN];
4975 char strbuf[STRBUF_LEN];
4977 if (dqb->dqb_bhardlimit &&
4978 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
4980 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
4981 if (dqb->dqb_btime > now) {
4988 if (dqb->dqb_ihardlimit &&
4989 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
4991 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
4992 if (dqb->dqb_itime > now) {
5000 if (strlen(mnt) > 15)
5001 printf("%s\n%15s", mnt, "");
5003 printf("%15s", mnt);
5006 diff2str(dqb->dqb_btime, timebuf, now);
5008 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5009 strbuf, sizeof(strbuf), h);
5010 if (rc == -EREMOTEIO)
5011 sprintf(numbuf[0], "%s*", strbuf);
5013 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5014 "%s" : "[%s]", strbuf);
5016 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5017 if (type == QC_GENERAL)
5018 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5019 "%s" : "[%s]", strbuf);
5021 sprintf(numbuf[1], "%s", "-");
5023 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5024 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5025 "%s" : "[%s]", strbuf);
5027 printf(" %7s%c %6s %7s %7s",
5028 numbuf[0], bover ? '*' : ' ', numbuf[1],
5029 numbuf[2], bover > 1 ? timebuf : "-");
5032 diff2str(dqb->dqb_itime, timebuf, now);
5034 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
5035 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
5037 if (type == QC_GENERAL)
5038 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5040 (uintmax_t)dqb->dqb_isoftlimit);
5042 sprintf(numbuf[1], "%s", "-");
5044 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5045 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5047 if (type != QC_OSTIDX)
5048 printf(" %7s%c %6s %7s %7s",
5049 numbuf[0], iover ? '*' : ' ', numbuf[1],
5050 numbuf[2], iover > 1 ? timebuf : "-");
5052 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5055 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5056 qctl->qc_cmd == Q_GETOINFO) {
5060 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5061 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5062 printf("Block grace time: %s; Inode grace time: %s\n",
5063 bgtimebuf, igtimebuf);
5067 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5068 bool h, __u64 *total)
5070 int rc = 0, rc1 = 0, count = 0;
5071 __u32 valid = qctl->qc_valid;
5073 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5075 fprintf(stderr, "can not get %s count: %s\n",
5076 is_mdt ? "mdt": "ost", strerror(-rc));
5080 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5081 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5082 rc = llapi_quotactl(mnt, qctl);
5084 /* It is remote client case. */
5085 if (rc == -EOPNOTSUPP) {
5092 fprintf(stderr, "quotactl %s%d failed.\n",
5093 is_mdt ? "mdt": "ost", qctl->qc_idx);
5097 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5098 qctl->qc_valid, 0, h);
5099 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5100 qctl->qc_dqblk.dqb_bhardlimit;
5103 qctl->qc_valid = valid;
5107 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5108 int verbose, int quiet, bool human_readable)
5110 int rc1 = 0, rc2 = 0, rc3 = 0;
5111 char *obd_type = (char *)qctl->obd_type;
5112 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5113 __u64 total_ialloc = 0, total_balloc = 0;
5116 rc1 = llapi_quotactl(mnt, qctl);
5120 fprintf(stderr, "%s quotas are not enabled.\n",
5121 qtype_name(qctl->qc_type));
5124 fprintf(stderr, "Permission denied.\n");
5127 /* We already got error message. */
5130 fprintf(stderr, "Unexpected quotactl error: %s\n",
5135 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
5136 print_quota_title(name, qctl, human_readable);
5138 if (rc1 && *obd_type)
5139 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5141 if (qctl->qc_valid != QC_GENERAL)
5144 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5145 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5146 (QIF_LIMITS|QIF_USAGE));
5148 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
5150 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
5152 char strbuf[STRBUF_LEN];
5154 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5156 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5158 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5160 printf("Total allocated inode limit: %ju, total "
5161 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5165 if (rc1 || rc2 || rc3 || inacc)
5166 printf("Some errors happened when getting quota info. "
5167 "Some devices may be not working or deactivated. "
5168 "The data in \"[]\" is inaccurate.\n");
5174 static int lfs_project(int argc, char **argv)
5176 int ret = 0, err = 0, c, i;
5177 struct project_handle_control phc = { 0 };
5178 enum lfs_project_ops_t op;
5181 phc.assign_projid = false;
5182 /* default action */
5183 op = LFS_PROJECT_LIST;
5185 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
5188 if (op != LFS_PROJECT_LIST) {
5190 "%s: cannot specify '-c' '-C' '-s' together\n",
5195 op = LFS_PROJECT_CHECK;
5198 if (op != LFS_PROJECT_LIST) {
5200 "%s: cannot specify '-c' '-C' '-s' together\n",
5205 op = LFS_PROJECT_CLEAR;
5208 if (op != LFS_PROJECT_LIST) {
5210 "%s: cannot specify '-c' '-C' '-s' together\n",
5215 phc.set_inherit = true;
5216 op = LFS_PROJECT_SET;
5222 phc.keep_projid = true;
5225 phc.recursive = true;
5228 phc.projid = strtoul(optarg, NULL, 0);
5229 phc.assign_projid = true;
5233 phc.newline = false;
5236 fprintf(stderr, "%s: invalid option '%c'\n",
5242 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
5243 op = LFS_PROJECT_SET;
5244 phc.set_projid = true;
5245 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
5246 phc.set_projid = true;
5250 case LFS_PROJECT_CHECK:
5251 if (phc.keep_projid) {
5253 "%s: '-k' is useless together with '-c'\n",
5258 case LFS_PROJECT_CLEAR:
5261 "%s: '-0' is useless together with '-C'\n",
5265 if (phc.assign_projid) {
5267 "%s: '-p' is useless together with '-C'\n",
5272 case LFS_PROJECT_SET:
5275 "%s: '-0' is useless together with '-s'\n",
5279 if (phc.keep_projid) {
5281 "%s: '-k' is useless together with '-s'\n",
5289 "%s: '-0' is useless for list operations\n",
5299 fprintf(stderr, "%s: missing file or directory target(s)\n",
5304 for (i = 0; i < argc; i++) {
5306 case LFS_PROJECT_CHECK:
5307 err = lfs_project_check(argv[i], &phc);
5309 case LFS_PROJECT_LIST:
5310 err = lfs_project_list(argv[i], &phc);
5312 case LFS_PROJECT_CLEAR:
5313 err = lfs_project_clear(argv[i], &phc);
5315 case LFS_PROJECT_SET:
5316 err = lfs_project_set(argv[i], &phc);
5328 static int lfs_quota(int argc, char **argv)
5331 char *mnt, *name = NULL;
5332 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
5333 .qc_type = ALLQUOTA };
5334 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
5335 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
5337 __u32 valid = QC_GENERAL, idx = 0;
5338 bool human_readable = false;
5341 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
5352 if (qctl.qc_type != ALLQUOTA) {
5354 "%s quota: only one of -u, -g, or -p may be specified\n",
5358 qctl.qc_type = qtype;
5361 qctl.qc_cmd = LUSTRE_Q_GETINFO;
5364 valid = qctl.qc_valid = QC_UUID;
5365 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
5368 valid = qctl.qc_valid = QC_MDTIDX;
5369 idx = qctl.qc_idx = atoi(optarg);
5370 if (idx == 0 && *optarg != '0') {
5372 "%s quota: invalid MDT index '%s'\n",
5378 valid = qctl.qc_valid = QC_OSTIDX;
5379 idx = qctl.qc_idx = atoi(optarg);
5380 if (idx == 0 && *optarg != '0') {
5382 "%s quota: invalid OST index '%s'\n",
5394 human_readable = true;
5397 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
5398 progname, argv[optind - 1]);
5403 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
5404 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
5405 optind == argc - 1) {
5407 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
5408 qctl.qc_valid = valid;
5411 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
5412 qctl.qc_type = qtype;
5413 if (qtype == USRQUOTA) {
5414 qctl.qc_id = geteuid();
5415 rc = uid2name(&name, qctl.qc_id);
5417 qctl.qc_id = getegid();
5418 rc = gid2name(&name, qctl.qc_id);
5423 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
5428 /* lfs quota -u username /path/to/lustre/mount */
5429 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
5430 /* options should be followed by u/g-name and mntpoint */
5431 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
5433 "%s quota: name and mount point must be specified\n",
5438 name = argv[optind++];
5439 switch (qctl.qc_type) {
5441 rc = name2uid(&qctl.qc_id, name);
5444 rc = name2gid(&qctl.qc_id, name);
5447 rc = name2projid(&qctl.qc_id, name);
5454 qctl.qc_id = strtoul(name, &endptr, 10);
5455 if (*endptr != '\0') {
5456 fprintf(stderr, "%s quota: invalid id '%s'\n",
5462 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
5464 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
5465 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
5472 #endif /* HAVE_SYS_QUOTA_H! */
5474 static int flushctx_ioctl(char *mp)
5478 fd = open(mp, O_RDONLY);
5480 fprintf(stderr, "flushctx: error open %s: %s\n",
5481 mp, strerror(errno));
5485 rc = ioctl(fd, LL_IOC_FLUSHCTX);
5487 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
5488 mp, strerror(errno));
5494 static int lfs_flushctx(int argc, char **argv)
5496 int kdestroy = 0, c;
5497 char mntdir[PATH_MAX] = {'\0'};
5501 while ((c = getopt(argc, argv, "k")) != -1) {
5507 fprintf(stderr, "error: %s: option '-%c' "
5508 "unrecognized\n", argv[0], c);
5514 if ((rc = system("kdestroy > /dev/null")) != 0) {
5515 rc = WEXITSTATUS(rc);
5516 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
5520 if (optind >= argc) {
5521 /* flush for all mounted lustre fs. */
5522 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
5523 /* Check if we have a mount point */
5524 if (mntdir[0] == '\0')
5527 if (flushctx_ioctl(mntdir))
5530 mntdir[0] = '\0'; /* avoid matching in next loop */
5533 /* flush fs as specified */
5534 while (optind < argc) {
5535 if (flushctx_ioctl(argv[optind++]))
5542 static int lfs_cp(int argc, char **argv)
5544 fprintf(stderr, "remote client copy file(s).\n"
5545 "obsolete, does not support it anymore.\n");
5549 static int lfs_ls(int argc, char **argv)
5551 fprintf(stderr, "remote client lists directory contents.\n"
5552 "obsolete, does not support it anymore.\n");
5556 static int lfs_changelog(int argc, char **argv)
5558 void *changelog_priv;
5559 struct changelog_rec *rec;
5560 long long startrec = 0, endrec = 0;
5562 struct option long_opts[] = {
5563 { .val = 'f', .name = "follow", .has_arg = no_argument },
5565 char short_opts[] = "f";
5568 while ((rc = getopt_long(argc, argv, short_opts,
5569 long_opts, NULL)) != -1) {
5577 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5578 argv[0], argv[optind - 1]);
5585 mdd = argv[optind++];
5587 startrec = strtoll(argv[optind++], NULL, 10);
5589 endrec = strtoll(argv[optind++], NULL, 10);
5591 rc = llapi_changelog_start(&changelog_priv,
5592 CHANGELOG_FLAG_BLOCK |
5593 CHANGELOG_FLAG_JOBID |
5594 CHANGELOG_FLAG_EXTRA_FLAGS |
5595 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
5598 fprintf(stderr, "Can't start changelog: %s\n",
5599 strerror(errno = -rc));
5603 rc = llapi_changelog_set_xflags(changelog_priv,
5604 CHANGELOG_EXTRA_FLAG_UIDGID);
5606 fprintf(stderr, "Can't set xflags for changelog: %s\n",
5607 strerror(errno = -rc));
5611 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
5615 if (endrec && rec->cr_index > endrec) {
5616 llapi_changelog_free(&rec);
5619 if (rec->cr_index < startrec) {
5620 llapi_changelog_free(&rec);
5624 secs = rec->cr_time >> 30;
5625 gmtime_r(&secs, &ts);
5626 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
5627 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
5628 changelog_type2str(rec->cr_type),
5629 ts.tm_hour, ts.tm_min, ts.tm_sec,
5630 (int)(rec->cr_time & ((1 << 30) - 1)),
5631 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
5632 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
5634 if (rec->cr_flags & CLF_JOBID) {
5635 struct changelog_ext_jobid *jid =
5636 changelog_rec_jobid(rec);
5638 if (jid->cr_jobid[0] != '\0')
5639 printf(" j=%s", jid->cr_jobid);
5642 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
5643 struct changelog_ext_extra_flags *ef =
5644 changelog_rec_extra_flags(rec);
5646 printf(" ef=0x%llx", ef->cr_extra_flags);
5648 if (ef->cr_extra_flags & CLFE_UIDGID) {
5649 struct changelog_ext_uidgid *uidgid =
5650 changelog_rec_uidgid(rec);
5652 printf(" u=%llu:%llu",
5653 uidgid->cr_uid, uidgid->cr_gid);
5657 if (rec->cr_namelen)
5658 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
5659 rec->cr_namelen, changelog_rec_name(rec));
5661 if (rec->cr_flags & CLF_RENAME) {
5662 struct changelog_ext_rename *rnm =
5663 changelog_rec_rename(rec);
5665 if (!fid_is_zero(&rnm->cr_sfid))
5666 printf(" s="DFID" sp="DFID" %.*s",
5667 PFID(&rnm->cr_sfid),
5668 PFID(&rnm->cr_spfid),
5669 (int)changelog_rec_snamelen(rec),
5670 changelog_rec_sname(rec));
5674 llapi_changelog_free(&rec);
5677 llapi_changelog_fini(&changelog_priv);
5680 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
5682 return (rc == 1 ? 0 : rc);
5685 static int lfs_changelog_clear(int argc, char **argv)
5693 endrec = strtoll(argv[3], NULL, 10);
5695 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
5698 fprintf(stderr, "%s: record out of range: %llu\n",
5700 else if (rc == -ENOENT)
5701 fprintf(stderr, "%s: no changelog user: %s\n",
5704 fprintf(stderr, "%s error: %s\n", argv[0],
5713 static int lfs_fid2path(int argc, char **argv)
5715 struct option long_opts[] = {
5716 { .val = 'c', .name = "cur", .has_arg = no_argument },
5717 { .val = 'l', .name = "link", .has_arg = required_argument },
5718 { .val = 'r', .name = "rec", .has_arg = required_argument },
5720 char short_opts[] = "cl:r:";
5721 char *device, *fid, *path;
5722 long long recno = -1;
5728 while ((rc = getopt_long(argc, argv, short_opts,
5729 long_opts, NULL)) != -1) {
5735 linkno = strtol(optarg, NULL, 10);
5738 recno = strtoll(optarg, NULL, 10);
5743 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5744 argv[0], argv[optind - 1]);
5752 device = argv[optind++];
5753 path = calloc(1, PATH_MAX);
5755 fprintf(stderr, "error: Not enough memory\n");
5760 while (optind < argc) {
5761 fid = argv[optind++];
5763 lnktmp = (linkno >= 0) ? linkno : 0;
5765 int oldtmp = lnktmp;
5766 long long rectmp = recno;
5768 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
5771 fprintf(stderr, "%s: error on FID %s: %s\n",
5772 argv[0], fid, strerror(errno = -rc2));
5779 fprintf(stdout, "%lld ", rectmp);
5780 if (device[0] == '/') {
5781 fprintf(stdout, "%s", device);
5782 if (device[strlen(device) - 1] != '/')
5783 fprintf(stdout, "/");
5784 } else if (path[0] == '\0') {
5785 fprintf(stdout, "/");
5787 fprintf(stdout, "%s\n", path);
5790 /* specified linkno */
5792 if (oldtmp == lnktmp)
5802 static int lfs_path2fid(int argc, char **argv)
5804 struct option long_opts[] = {
5805 { .val = 'p', .name = "parents", .has_arg = no_argument },
5808 const char short_opts[] = "p";
5809 const char *sep = "";
5812 bool show_parents = false;
5814 while ((rc = getopt_long(argc, argv, short_opts,
5815 long_opts, NULL)) != -1) {
5818 show_parents = true;
5821 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5822 argv[0], argv[optind - 1]);
5827 if (optind > argc - 1)
5829 else if (optind < argc - 1)
5833 for (path = argv + optind; *path != NULL; path++) {
5835 if (!show_parents) {
5836 err = llapi_path2fid(*path, &fid);
5838 printf("%s%s"DFID"\n",
5839 *sep != '\0' ? *path : "", sep,
5842 char name[NAME_MAX + 1];
5843 unsigned int linkno = 0;
5845 while ((err = llapi_path2parent(*path, linkno, &fid,
5846 name, sizeof(name))) == 0) {
5847 if (*sep != '\0' && linkno == 0)
5848 printf("%s%s", *path, sep);
5850 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
5855 /* err == -ENODATA is end-of-loop */
5856 if (linkno > 0 && err == -ENODATA) {
5863 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
5864 argv[0], show_parents ? "parent " : "", *path,
5876 static int lfs_data_version(int argc, char **argv)
5883 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
5888 while ((c = getopt(argc, argv, "nrw")) != -1) {
5891 data_version_flags = 0;
5894 data_version_flags |= LL_DV_RD_FLUSH;
5897 data_version_flags |= LL_DV_WR_FLUSH;
5906 path = argv[optind];
5907 fd = open(path, O_RDONLY);
5909 err(errno, "cannot open file %s", path);
5911 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
5913 err(errno, "cannot get version for %s", path);
5915 printf("%ju" "\n", (uintmax_t)data_version);
5921 static int lfs_hsm_state(int argc, char **argv)
5926 struct hsm_user_state hus;
5934 rc = llapi_hsm_state_get(path, &hus);
5936 fprintf(stderr, "can't get hsm state for %s: %s\n",
5937 path, strerror(errno = -rc));
5941 /* Display path name and status flags */
5942 printf("%s: (0x%08x)", path, hus.hus_states);
5944 if (hus.hus_states & HS_RELEASED)
5945 printf(" released");
5946 if (hus.hus_states & HS_EXISTS)
5948 if (hus.hus_states & HS_DIRTY)
5950 if (hus.hus_states & HS_ARCHIVED)
5951 printf(" archived");
5952 /* Display user-settable flags */
5953 if (hus.hus_states & HS_NORELEASE)
5954 printf(" never_release");
5955 if (hus.hus_states & HS_NOARCHIVE)
5956 printf(" never_archive");
5957 if (hus.hus_states & HS_LOST)
5958 printf(" lost_from_hsm");
5960 if (hus.hus_archive_id != 0)
5961 printf(", archive_id:%d", hus.hus_archive_id);
5964 } while (++i < argc);
5969 #define LFS_HSM_SET 0
5970 #define LFS_HSM_CLEAR 1
5973 * Generic function to set or clear HSM flags.
5974 * Used by hsm_set and hsm_clear.
5976 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
5978 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
5980 struct option long_opts[] = {
5981 { .val = 'A', .name = "archived", .has_arg = no_argument },
5982 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
5983 { .val = 'd', .name = "dirty", .has_arg = no_argument },
5984 { .val = 'e', .name = "exists", .has_arg = no_argument },
5985 { .val = 'l', .name = "lost", .has_arg = no_argument },
5986 { .val = 'r', .name = "norelease", .has_arg = no_argument },
5987 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
5989 char short_opts[] = "lraAdei:";
5993 __u32 archive_id = 0;
5999 while ((c = getopt_long(argc, argv, short_opts,
6000 long_opts, NULL)) != -1) {
6006 mask |= HS_NOARCHIVE;
6009 mask |= HS_ARCHIVED;
6012 mask |= HS_NORELEASE;
6021 archive_id = strtol(optarg, &end, 10);
6023 fprintf(stderr, "invalid archive_id: '%s'\n",
6031 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6032 argv[0], argv[optind - 1]);
6037 /* User should have specified a flag */
6041 while (optind < argc) {
6043 path = argv[optind];
6045 /* If mode == 0, this means we apply the mask. */
6046 if (mode == LFS_HSM_SET)
6047 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6049 rc = llapi_hsm_state_set(path, 0, mask, 0);
6052 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6053 path, strerror(errno = -rc));
6062 static int lfs_hsm_action(int argc, char **argv)
6067 struct hsm_current_action hca;
6068 struct hsm_extent he;
6069 enum hsm_user_action hua;
6070 enum hsm_progress_states hps;
6078 rc = llapi_hsm_current_action(path, &hca);
6080 fprintf(stderr, "can't get hsm action for %s: %s\n",
6081 path, strerror(errno = -rc));
6084 he = hca.hca_location;
6085 hua = hca.hca_action;
6086 hps = hca.hca_state;
6088 printf("%s: %s", path, hsm_user_action2name(hua));
6090 /* Skip file without action */
6091 if (hca.hca_action == HUA_NONE) {
6096 printf(" %s ", hsm_progress_state2name(hps));
6098 if ((hps == HPS_RUNNING) &&
6099 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
6100 printf("(%llu bytes moved)\n",
6101 (unsigned long long)he.length);
6102 else if ((he.offset + he.length) == LUSTRE_EOF)
6103 printf("(from %llu to EOF)\n",
6104 (unsigned long long)he.offset);
6106 printf("(from %llu to %llu)\n",
6107 (unsigned long long)he.offset,
6108 (unsigned long long)(he.offset + he.length));
6110 } while (++i < argc);
6115 static int lfs_hsm_set(int argc, char **argv)
6117 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
6120 static int lfs_hsm_clear(int argc, char **argv)
6122 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
6126 * Check file state and return its fid, to be used by lfs_hsm_request().
6128 * \param[in] file Path to file to check
6129 * \param[in,out] fid Pointer to allocated lu_fid struct.
6130 * \param[in,out] last_dev Pointer to last device id used.
6132 * \return 0 on success.
6134 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
6140 rc = lstat(file, &st);
6142 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
6145 /* Checking for regular file as archiving as posix copytool
6146 * rejects archiving files other than regular files
6148 if (!S_ISREG(st.st_mode)) {
6149 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
6152 /* A request should be ... */
6153 if (*last_dev != st.st_dev && *last_dev != 0) {
6154 fprintf(stderr, "All files should be "
6155 "on the same filesystem: %s\n", file);
6158 *last_dev = st.st_dev;
6160 rc = llapi_path2fid(file, fid);
6162 fprintf(stderr, "Cannot read FID of %s: %s\n",
6163 file, strerror(-rc));
6169 /* Fill an HSM HUR item with a given file name.
6171 * If mntpath is set, then the filename is actually a FID, and no
6172 * lookup on the filesystem will be performed.
6174 * \param[in] hur the user request to fill
6175 * \param[in] idx index of the item inside the HUR to fill
6176 * \param[in] mntpath mountpoint of Lustre
6177 * \param[in] fname filename (if mtnpath is NULL)
6178 * or FID (if mntpath is set)
6179 * \param[in] last_dev pointer to last device id used
6181 * \retval 0 on success
6182 * \retval CMD_HELP or a negative errno on error
6184 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
6185 const char *mntpath, const char *fname,
6188 struct hsm_user_item *hui = &hur->hur_user_item[idx];
6191 hui->hui_extent.length = -1;
6193 if (mntpath != NULL) {
6196 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
6200 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
6205 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
6209 hur->hur_request.hr_itemcount++;
6214 static int lfs_hsm_request(int argc, char **argv, int action)
6216 struct option long_opts[] = {
6217 { .val = 'a', .name = "archive", .has_arg = required_argument },
6218 { .val = 'D', .name = "data", .has_arg = required_argument },
6219 { .val = 'l', .name = "filelist", .has_arg = required_argument },
6220 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
6223 char short_opts[] = "l:D:a:m:";
6224 struct hsm_user_request *hur, *oldhur;
6229 char *filelist = NULL;
6230 char fullpath[PATH_MAX];
6231 char *opaque = NULL;
6235 int nbfile_alloc = 0;
6236 char *some_file = NULL;
6237 char *mntpath = NULL;
6243 while ((c = getopt_long(argc, argv, short_opts,
6244 long_opts, NULL)) != -1) {
6253 if (action != HUA_ARCHIVE &&
6254 action != HUA_REMOVE) {
6256 "error: -a is supported only "
6257 "when archiving or removing\n");
6260 archive_id = atoi(optarg);
6263 if (some_file == NULL) {
6265 some_file = strdup(optarg);
6271 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6272 argv[0], argv[optind - 1]);
6277 /* All remaining args are files, so we have at least nbfile */
6278 nbfile = argc - optind;
6280 if ((nbfile == 0) && (filelist == NULL))
6284 opaque_len = strlen(opaque);
6286 /* Alloc the request structure with enough place to store all files
6287 * from command line. */
6288 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
6290 fprintf(stderr, "Cannot create the request: %s\n",
6294 nbfile_alloc = nbfile;
6296 hur->hur_request.hr_action = action;
6297 hur->hur_request.hr_archive_id = archive_id;
6298 hur->hur_request.hr_flags = 0;
6300 /* All remaining args are files, add them */
6301 if (nbfile != 0 && some_file == NULL)
6302 some_file = strdup(argv[optind]);
6304 for (i = 0; i < nbfile; i++) {
6305 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
6311 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
6313 /* If a filelist was specified, read the filelist from it. */
6314 if (filelist != NULL) {
6315 fp = fopen(filelist, "r");
6317 fprintf(stderr, "Cannot read the file list %s: %s\n",
6318 filelist, strerror(errno));
6323 while ((rc = getline(&line, &len, fp)) != -1) {
6324 /* If allocated buffer was too small, get something
6326 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
6329 nbfile_alloc = nbfile_alloc * 2 + 1;
6331 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
6334 fprintf(stderr, "hsm: cannot allocate "
6335 "the request: %s\n",
6342 size = hur_len(oldhur);
6344 fprintf(stderr, "hsm: cannot allocate "
6345 "%u files + %u bytes data\n",
6346 oldhur->hur_request.hr_itemcount,
6347 oldhur->hur_request.hr_data_len);
6354 memcpy(hur, oldhur, size);
6359 if (line[strlen(line) - 1] == '\n')
6360 line[strlen(line) - 1] = '\0';
6362 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
6363 mntpath, line, &last_dev);
6369 if (some_file == NULL) {
6379 /* If a --data was used, add it to the request */
6380 hur->hur_request.hr_data_len = opaque_len;
6382 memcpy(hur_data(hur), opaque, opaque_len);
6384 /* Send the HSM request */
6385 if (realpath(some_file, fullpath) == NULL) {
6386 fprintf(stderr, "Could not find path '%s': %s\n",
6387 some_file, strerror(errno));
6389 rc = llapi_hsm_request(fullpath, hur);
6391 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
6392 some_file, strerror(-rc));
6402 static int lfs_hsm_archive(int argc, char **argv)
6404 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
6407 static int lfs_hsm_restore(int argc, char **argv)
6409 return lfs_hsm_request(argc, argv, HUA_RESTORE);
6412 static int lfs_hsm_release(int argc, char **argv)
6414 return lfs_hsm_request(argc, argv, HUA_RELEASE);
6417 static int lfs_hsm_remove(int argc, char **argv)
6419 return lfs_hsm_request(argc, argv, HUA_REMOVE);
6422 static int lfs_hsm_cancel(int argc, char **argv)
6424 return lfs_hsm_request(argc, argv, HUA_CANCEL);
6427 static int lfs_swap_layouts(int argc, char **argv)
6432 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
6433 SWAP_LAYOUTS_KEEP_MTIME |
6434 SWAP_LAYOUTS_KEEP_ATIME);
6437 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
6439 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
6441 int lfs_get_mode(const char *string)
6443 enum lock_mode_user mode;
6445 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
6446 if (lock_mode_names[mode] == NULL)
6448 if (strcmp(string, lock_mode_names[mode]) == 0)
6455 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
6457 enum lu_ladvise_type advice;
6460 advice < ARRAY_SIZE(ladvise_names); advice++) {
6461 if (ladvise_names[advice] == NULL)
6463 if (strcmp(string, ladvise_names[advice]) == 0)
6467 return LU_LADVISE_INVALID;
6470 static int lfs_ladvise(int argc, char **argv)
6472 struct option long_opts[] = {
6473 { .val = 'a', .name = "advice", .has_arg = required_argument },
6474 { .val = 'b', .name = "background", .has_arg = no_argument },
6475 { .val = 'e', .name = "end", .has_arg = required_argument },
6476 { .val = 'l', .name = "length", .has_arg = required_argument },
6477 { .val = 'm', .name = "mode", .has_arg = required_argument },
6478 { .val = 's', .name = "start", .has_arg = required_argument },
6479 { .val = 'u', .name = "unset", .has_arg = no_argument },
6481 char short_opts[] = "a:be:l:m:s:u";
6486 struct llapi_lu_ladvise advice;
6487 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
6488 unsigned long long start = 0;
6489 unsigned long long end = LUSTRE_EOF;
6490 unsigned long long length = 0;
6491 unsigned long long size_units;
6492 unsigned long long flags = 0;
6496 while ((c = getopt_long(argc, argv, short_opts,
6497 long_opts, NULL)) != -1) {
6500 advice_type = lfs_get_ladvice(optarg);
6501 if (advice_type == LU_LADVISE_INVALID) {
6502 fprintf(stderr, "%s: invalid advice type "
6503 "'%s'\n", argv[0], optarg);
6504 fprintf(stderr, "Valid types:");
6506 for (advice_type = 0;
6507 advice_type < ARRAY_SIZE(ladvise_names);
6509 if (ladvise_names[advice_type] == NULL)
6511 fprintf(stderr, " %s",
6512 ladvise_names[advice_type]);
6514 fprintf(stderr, "\n");
6527 rc = llapi_parse_size(optarg, &end,
6530 fprintf(stderr, "%s: bad end offset '%s'\n",
6537 rc = llapi_parse_size(optarg, &start,
6540 fprintf(stderr, "%s: bad start offset "
6541 "'%s'\n", argv[0], optarg);
6547 rc = llapi_parse_size(optarg, &length,
6550 fprintf(stderr, "%s: bad length '%s'\n",
6556 mode = lfs_get_mode(optarg);
6558 fprintf(stderr, "%s: bad mode '%s', valid "
6559 "modes are READ or WRITE\n",
6567 fprintf(stderr, "%s: option '%s' unrecognized\n",
6568 argv[0], argv[optind - 1]);
6573 if (advice_type == LU_LADVISE_INVALID) {
6574 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
6575 fprintf(stderr, "Valid types:");
6576 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
6578 if (ladvise_names[advice_type] == NULL)
6580 fprintf(stderr, " %s", ladvise_names[advice_type]);
6582 fprintf(stderr, "\n");
6586 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
6587 fprintf(stderr, "%s: Lock no expand advice is a per file "
6588 "descriptor advice, so when called from lfs, "
6589 "it does nothing.\n", argv[0]);
6593 if (argc <= optind) {
6594 fprintf(stderr, "%s: please give one or more file names\n",
6599 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
6600 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
6605 if (end == LUSTRE_EOF && length != 0)
6606 end = start + length;
6609 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
6610 argv[0], start, end);
6614 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
6615 fprintf(stderr, "%s: mode is only valid with lockahead\n",
6620 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
6621 fprintf(stderr, "%s: mode is required with lockahead\n",
6626 while (optind < argc) {
6629 path = argv[optind++];
6631 fd = open(path, O_RDONLY);
6633 fprintf(stderr, "%s: cannot open file '%s': %s\n",
6634 argv[0], path, strerror(errno));
6639 advice.lla_start = start;
6640 advice.lla_end = end;
6641 advice.lla_advice = advice_type;
6642 advice.lla_value1 = 0;
6643 advice.lla_value2 = 0;
6644 advice.lla_value3 = 0;
6645 advice.lla_value4 = 0;
6646 if (advice_type == LU_LADVISE_LOCKAHEAD) {
6647 advice.lla_lockahead_mode = mode;
6648 advice.lla_peradvice_flags = flags;
6651 rc2 = llapi_ladvise(fd, flags, 1, &advice);
6654 fprintf(stderr, "%s: cannot give advice '%s' to file "
6655 "'%s': %s\n", argv[0],
6656 ladvise_names[advice_type],
6657 path, strerror(errno));
6663 if (rc == 0 && rc2 < 0)
6669 /** The input string contains a comma delimited list of component ids and
6670 * ranges, for example "1,2-4,7".
6672 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
6674 bool end_of_loop = false;
6682 while (!end_of_loop) {
6686 char *endptr = NULL;
6689 ptr = strchrnul(arg, ',');
6690 end_of_loop = *ptr == '\0';
6693 start_index = strtol(arg, &endptr, 0);
6694 if (endptr == arg) /* no data at all */
6696 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
6698 if (start_index < 0)
6701 end_index = start_index;
6702 if (*endptr == '-') {
6703 end_index = strtol(endptr + 1, &endptr, 0);
6704 if (*endptr != '\0')
6706 if (end_index < start_index)
6710 for (i = start_index; i <= end_index && size > 0; i++) {
6713 /* remove duplicate */
6714 for (j = 0; j < nr; j++) {
6718 if (j == nr) { /* no duplicate */
6724 if (size == 0 && i < end_index)
6731 if (!end_of_loop && ptr != NULL)
6734 return rc < 0 ? rc : nr;
6738 * struct verify_mirror_id - Mirror id to be verified.
6739 * @mirror_id: A specified mirror id.
6740 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
6742 struct verify_mirror_id {
6748 * compare_mirror_ids() - Compare mirror ids.
6749 * @layout: Mirror component list.
6750 * @cbdata: Callback data in verify_mirror_id structure.
6752 * This is a callback function called by llapi_layout_comp_iterate()
6753 * to compare the specified mirror id with the one in the current
6754 * component of @layout. If they are the same, then the specified
6755 * mirror id is valid.
6757 * Return: a negative error code on failure or
6758 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
6759 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
6762 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
6764 struct verify_mirror_id *mirror_id_cbdata =
6765 (struct verify_mirror_id *)cbdata;
6769 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
6773 "%s: llapi_layout_mirror_id_get failed: %s.\n",
6774 progname, strerror(errno));
6778 if (mirror_id_cbdata->mirror_id == mirror_id) {
6779 mirror_id_cbdata->is_valid_id = true;
6780 return LLAPI_LAYOUT_ITER_STOP;
6783 return LLAPI_LAYOUT_ITER_CONT;
6787 * verify_mirror_ids() - Verify specified mirror ids.
6788 * @fname: Mirrored file name.
6789 * @mirror_ids: Specified mirror ids to be verified.
6790 * @ids_nr: Number of specified mirror ids.
6792 * This function verifies that specified @mirror_ids are valid
6793 * in the mirrored file @fname.
6795 * Return: 0 on success or a negative error code on failure.
6798 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
6800 struct llapi_layout *layout = NULL;
6801 struct verify_mirror_id mirror_id_cbdata = { 0 };
6812 if (stat(fname, &stbuf) < 0) {
6813 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
6814 progname, fname, strerror(errno));
6819 if (!S_ISREG(stbuf.st_mode)) {
6820 fprintf(stderr, "%s: '%s' is not a regular file.\n",
6826 fd = open(fname, O_DIRECT | O_RDONLY);
6828 fprintf(stderr, "%s: cannot open '%s': %s.\n",
6829 progname, fname, strerror(errno));
6834 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
6836 fprintf(stderr, "%s: '%s' llapi_lease_get failed: %s.\n",
6837 progname, fname, strerror(errno));
6841 layout = llapi_layout_get_by_fd(fd, 0);
6842 if (layout == NULL) {
6843 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
6844 progname, fname, strerror(errno));
6846 llapi_lease_put(fd);
6850 rc = llapi_layout_flags_get(layout, &flr_state);
6852 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
6853 progname, fname, strerror(errno));
6858 flr_state &= LCM_FL_FLR_MASK;
6859 switch (flr_state) {
6860 case LCM_FL_NOT_FLR:
6862 fprintf(stderr, "%s: '%s' file state error: %s.\n",
6863 progname, fname, lcm_flags_string(flr_state));
6870 for (i = 0; i < ids_nr; i++) {
6871 mirror_id_cbdata.mirror_id = mirror_ids[i];
6872 mirror_id_cbdata.is_valid_id = false;
6874 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
6879 "%s: '%s' failed to verify mirror id: %u.\n",
6880 progname, fname, mirror_ids[i]);
6884 if (!mirror_id_cbdata.is_valid_id) {
6887 "%s: '%s' invalid specified mirror id: %u.\n",
6888 progname, fname, mirror_ids[i]);
6894 llapi_layout_free(layout);
6895 llapi_lease_put(fd);
6903 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
6904 __u16 *mirror_ids, int ids_nr)
6906 const char *progname = "lfs mirror resync";
6907 struct llapi_resync_comp comp_array[1024] = { { 0 } };
6908 struct llapi_layout *layout;
6916 if (stat(fname, &stbuf) < 0) {
6917 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
6918 progname, fname, strerror(errno));
6922 if (!S_ISREG(stbuf.st_mode)) {
6923 fprintf(stderr, "%s: '%s' is not a regular file.\n",
6929 fd = open(fname, O_DIRECT | O_RDWR);
6931 fprintf(stderr, "%s: cannot open '%s': %s.\n",
6932 progname, fname, strerror(errno));
6937 ioc->lil_mode = LL_LEASE_WRLCK;
6938 ioc->lil_flags = LL_LEASE_RESYNC;
6939 rc = llapi_lease_set(fd, ioc);
6941 fprintf(stderr, "%s: '%s' llapi_lease_set resync failed: "
6942 "%s.\n", progname, fname, strerror(errno));
6946 layout = llapi_layout_get_by_fd(fd, 0);
6947 if (layout == NULL) {
6948 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
6949 progname, fname, strerror(errno));
6954 rc = llapi_layout_flags_get(layout, &flr_state);
6956 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
6957 progname, fname, strerror(errno));
6962 flr_state &= LCM_FL_FLR_MASK;
6963 switch (flr_state) {
6964 case LCM_FL_NOT_FLR:
6967 fprintf(stderr, "%s: '%s' file state error: %s.\n",
6968 progname, fname, lcm_flags_string(flr_state));
6974 /* get stale component info */
6975 comp_size = llapi_mirror_find_stale(layout, comp_array,
6976 ARRAY_SIZE(comp_array),
6977 mirror_ids, ids_nr);
6978 if (comp_size < 0) {
6984 while (idx < comp_size) {
6990 rc = llapi_lease_check(fd);
6991 if (rc != LL_LEASE_WRLCK) {
6992 fprintf(stderr, "%s: '%s' lost lease lock.\n",
6997 mirror_id = comp_array[idx].lrc_mirror_id;
6998 end = comp_array[idx].lrc_end;
7000 /* try to combine adjacent component */
7001 for (i = idx + 1; i < comp_size; i++) {
7002 if (mirror_id != comp_array[i].lrc_mirror_id ||
7003 end != comp_array[i].lrc_start)
7005 end = comp_array[i].lrc_end;
7008 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7009 comp_array[idx].lrc_start,
7012 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7013 "%ld.\n", progname, fname, result);
7016 } else if (result > 0) {
7019 /* mark synced components */
7020 for (j = idx; j < i; j++)
7021 comp_array[j].lrc_synced = true;
7027 /* prepare ioc for lease put */
7028 ioc->lil_mode = LL_LEASE_UNLCK;
7029 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7031 for (idx = 0; idx < comp_size; idx++) {
7032 if (comp_array[idx].lrc_synced) {
7033 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7038 llapi_layout_free(layout);
7040 rc = llapi_lease_set(fd, ioc);
7042 if (rc == 0) /* lost lease lock */
7044 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7045 progname, fname, strerror(errno));
7049 * llapi_lease_set returns lease mode when it request to unlock
7060 static inline int lfs_mirror_resync(int argc, char **argv)
7062 struct ll_ioc_lease *ioc = NULL;
7063 __u16 mirror_ids[128] = { 0 };
7068 struct option long_opts[] = {
7069 { .val = 'o', .name = "only", .has_arg = required_argument },
7072 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
7075 rc = parse_mirror_ids(mirror_ids,
7076 sizeof(mirror_ids) / sizeof(__u16),
7080 "%s: bad mirror ids '%s'.\n",
7087 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7088 argv[0], argv[optind - 1]);
7094 if (argc == optind) {
7095 fprintf(stderr, "%s: no file name given.\n", argv[0]);
7100 if (ids_nr > 0 && argc > optind + 1) {
7101 fprintf(stderr, "%s: option '--only' cannot be used upon "
7102 "multiple files.\n", argv[0]);
7109 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7114 /* set the lease on the file */
7115 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
7117 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
7118 argv[0], strerror(errno));
7123 for (; optind < argc; optind++) {
7124 rc = lfs_mirror_resync_file(argv[optind], ioc,
7125 mirror_ids, ids_nr);
7127 fprintf(stderr, "%s: resync file '%s' failed: %d\n",
7128 argv[0], argv[optind], rc);
7129 /* ignore previous file's error, continue with next file */
7132 memset(ioc, 0, sizeof(__u32) * 4096);
7141 * struct verify_chunk - Mirror chunk to be verified.
7142 * @chunk: [start, end) of the chunk.
7143 * @mirror_count: Number of mirror ids in @mirror_id array.
7144 * @mirror_id: Array of valid mirror ids that cover the chunk.
7146 struct verify_chunk {
7147 struct lu_extent chunk;
7148 unsigned int mirror_count;
7149 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
7153 * print_chunks() - Print chunk information.
7154 * @fname: Mirrored file name.
7155 * @chunks: Array of chunks.
7156 * @chunk_count: Number of chunks in @chunks array.
7158 * This function prints [start, end) of each chunk in @chunks
7159 * for mirrored file @fname, and also prints the valid mirror ids
7160 * that cover the chunk.
7165 void print_chunks(const char *fname, struct verify_chunk *chunks,
7171 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
7172 for (i = 0; i < chunk_count; i++) {
7173 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
7175 if (chunks[i].mirror_count == 0)
7176 fprintf(stdout, "\t[");
7178 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
7179 for (j = 1; j < chunks[i].mirror_count; j++)
7180 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
7182 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
7184 fprintf(stdout, "\n");
7188 * print_checksums() - Print CRC-32 checksum values.
7189 * @chunk: A chunk and its corresponding valid mirror ids.
7190 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
7192 * This function prints CRC-32 checksum values on @chunk for
7193 * each valid mirror that covers it.
7198 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
7203 "CRC-32 checksum value for chunk "DEXT":\n",
7204 PEXT(&chunk->chunk));
7205 for (i = 0; i < chunk->mirror_count; i++)
7206 fprintf(stdout, "Mirror %u:\t%#lx\n",
7207 chunk->mirror_id[i], crc[i]);
7208 fprintf(stdout, "\n");
7212 * filter_mirror_id() - Filter specified mirror ids.
7213 * @chunks: Array of chunks.
7214 * @chunk_count: Number of chunks in @chunks array.
7215 * @mirror_ids: Specified mirror ids to be verified.
7216 * @ids_nr: Number of specified mirror ids.
7218 * This function scans valid mirror ids that cover each chunk in @chunks
7219 * and filters specified mirror ids.
7224 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
7225 __u16 *mirror_ids, int ids_nr)
7230 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7231 unsigned int valid_count = 0;
7233 for (i = 0; i < chunk_count; i++) {
7234 if (chunks[i].mirror_count == 0)
7238 for (j = 0; j < ids_nr; j++) {
7239 for (k = 0; k < chunks[i].mirror_count; k++) {
7240 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
7241 valid_id[valid_count] = mirror_ids[j];
7248 memcpy(chunks[i].mirror_id, valid_id,
7249 sizeof(__u16) * valid_count);
7250 chunks[i].mirror_count = valid_count;
7255 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
7256 * @layout: Mirror component list.
7257 * @chunks: Array of chunks.
7258 * @chunks_size: Array size of @chunks.
7260 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
7261 * to find out chunk segments and store them in @chunks array.
7263 * The @mirror_id array in each element of @chunks will store the valid
7264 * mirror ids that cover the chunk. If a mirror component covering the
7265 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
7266 * will not be stored into the @mirror_id array, and the chunk for that
7267 * mirror will not be verified.
7269 * The @mirror_count in each element of @chunks will store the number of
7270 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
7271 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
7272 * indicates the chunk is valid in only one mirror. In both cases, the
7273 * chunk will not be verified.
7275 * Here is an example:
7278 * +------+-------------+--------------------+
7280 * +------+------+------+------+-------------+
7281 * | | S | S | | mirror2
7282 * +-------------+------+------+-------------+
7284 * prepared @chunks array will contain 5 elements:
7285 * (([0, 1M), [1, 2], 2),
7286 * ([1M, 2M), [1, 2], 2),
7287 * ([2M, 3M), [1], 1),
7288 * ([3M, 4M], [], 0),
7289 * ([4M, EOF), [2], 1))
7291 * Return: the actual array size of @chunks on success
7292 * or a negative error code on failure.
7295 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
7296 struct verify_chunk *chunks,
7307 memset(chunks, 0, sizeof(*chunks) * chunks_size);
7310 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
7313 "%s: move to the first layout component: %s.\n",
7314 progname, strerror(errno));
7320 chunks[idx].chunk.e_end = LUSTRE_EOF;
7322 rc = llapi_layout_comp_extent_get(layout, &start, &end);
7325 "%s: llapi_layout_comp_extent_get failed: %s.\n",
7326 progname, strerror(errno));
7330 if (start > chunks[idx].chunk.e_start ||
7331 end <= chunks[idx].chunk.e_start)
7334 if (end < chunks[idx].chunk.e_end)
7335 chunks[idx].chunk.e_end = end;
7337 rc = llapi_layout_comp_flags_get(layout, &flags);
7340 "%s: llapi_layout_comp_flags_get failed: %s.\n",
7341 progname, strerror(errno));
7345 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
7348 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7351 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7352 progname, strerror(errno));
7356 chunks[idx].mirror_id[i] = mirror_id;
7358 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
7360 "%s: mirror_id array is too small.\n",
7367 rc = llapi_layout_comp_use(layout,
7368 LLAPI_LAYOUT_COMP_USE_NEXT);
7371 "%s: move to the next layout component: %s.\n",
7372 progname, strerror(errno));
7375 } /* loop through all components */
7377 chunks[idx].mirror_count = i;
7379 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
7383 if (idx >= chunks_size) {
7384 fprintf(stderr, "%s: chunks array is too small.\n",
7390 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
7394 return rc < 0 ? rc : idx + 1;
7398 * lfs_mirror_verify_chunk() - Verify a chunk.
7399 * @fd: File descriptor of the mirrored file.
7400 * @file_size: Size of the mirrored file.
7401 * @chunk: A chunk and its corresponding valid mirror ids.
7402 * @verbose: Verbose mode.
7404 * This function verifies a @chunk contains exactly the same data
7405 * ammong the mirrors that cover it.
7407 * If @verbose is specified, then the function will print where the
7408 * differences are if the data do not match. Otherwise, it will
7409 * just return an error in that case.
7411 * Return: 0 on success or a negative error code on failure.
7414 int lfs_mirror_verify_chunk(int fd, size_t file_size,
7415 struct verify_chunk *chunk, int verbose)
7417 const size_t buflen = 4 * 1024 * 1024; /* 4M */
7419 size_t page_size = sysconf(_SC_PAGESIZE);
7425 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7432 rc = posix_memalign(&buf, page_size, buflen);
7433 if (rc) /* error code is returned directly */
7437 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7438 PEXT(&chunk->chunk));
7439 for (i = 0; i < chunk->mirror_count; i++)
7440 fprintf(stdout, " %u", chunk->mirror_id[i]);
7441 fprintf(stdout, "\n");
7445 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
7446 pos = chunk->chunk.e_start;
7447 while (bytes_done < count) {
7448 /* compute initial CRC-32 checksum */
7449 crc = crc32(0L, Z_NULL, 0);
7450 memset(crc_array, 0, sizeof(crc_array));
7453 for (i = 0; i < chunk->mirror_count; i++) {
7454 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
7456 if (bytes_read < 0) {
7459 "%s: failed to read data from mirror %u: %s.\n",
7460 progname, chunk->mirror_id[i],
7465 /* compute new CRC-32 checksum */
7466 crc_array[i] = crc32(crc, buf, bytes_read);
7470 print_checksums(chunk, crc_array);
7472 /* compare CRC-32 checksum values */
7473 for (i = 1; i < chunk->mirror_count; i++) {
7474 if (crc_array[i] != crc_array[0]) {
7480 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
7481 progname, PEXT(&chunk->chunk),
7482 chunk->mirror_id[0],
7483 chunk->mirror_id[i]);
7488 bytes_done += bytes_read;
7491 if (verbose > 1 && rc == 0) {
7492 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7493 PEXT(&chunk->chunk));
7494 for (i = 0; i < chunk->mirror_count; i++)
7495 fprintf(stdout, " %u", chunk->mirror_id[i]);
7496 fprintf(stdout, " PASS\n\n");
7505 * lfs_mirror_verify_file() - Verify a mirrored file.
7506 * @fname: Mirrored file name.
7507 * @mirror_ids: Specified mirror ids to be verified.
7508 * @ids_nr: Number of specified mirror ids.
7509 * @verbose: Verbose mode.
7511 * This function verifies that each SYNC mirror of a mirrored file
7512 * specified by @fname contains exactly the same data.
7514 * If @mirror_ids is specified, then the function will verify the
7515 * mirrors specified by @mirror_ids contain exactly the same data.
7517 * If @verbose is specified, then the function will print where the
7518 * differences are if the data do not match. Otherwise, it will
7519 * just return an error in that case.
7521 * Return: 0 on success or a negative error code on failure.
7524 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
7527 struct verify_chunk chunks_array[1024] = { };
7528 struct llapi_layout *layout = NULL;
7532 int chunk_count = 0;
7538 if (stat(fname, &stbuf) < 0) {
7539 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7540 progname, fname, strerror(errno));
7545 if (!S_ISREG(stbuf.st_mode)) {
7546 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7552 if (stbuf.st_size == 0) {
7554 fprintf(stdout, "%s: '%s' file size is 0.\n",
7560 fd = open(fname, O_DIRECT | O_RDONLY);
7562 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7563 progname, fname, strerror(errno));
7568 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
7570 fprintf(stderr, "%s: '%s' llapi_lease_get failed: %s.\n",
7571 progname, fname, strerror(errno));
7575 layout = llapi_layout_get_by_fd(fd, 0);
7576 if (layout == NULL) {
7577 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7578 progname, fname, strerror(errno));
7580 llapi_lease_put(fd);
7584 rc = llapi_layout_flags_get(layout, &flr_state);
7586 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7587 progname, fname, strerror(errno));
7592 flr_state &= LCM_FL_FLR_MASK;
7593 switch (flr_state) {
7594 case LCM_FL_NOT_FLR:
7596 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7597 progname, fname, lcm_flags_string(flr_state));
7603 /* find out mirror chunks to be verified */
7604 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
7605 ARRAY_SIZE(chunks_array));
7606 if (chunk_count < 0) {
7612 /* filter specified mirror ids */
7613 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
7616 print_chunks(fname, chunks_array, chunk_count);
7618 for (idx = 0; idx < chunk_count; idx++) {
7619 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
7622 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
7624 PEXT(&chunks_array[idx].chunk),
7625 (unsigned long long)stbuf.st_size);
7629 if (chunks_array[idx].mirror_count == 0) {
7631 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
7633 PEXT(&chunks_array[idx].chunk));
7635 fprintf(stderr, "skipped\n");
7639 fprintf(stderr, "failed\n");
7643 if (chunks_array[idx].mirror_count == 1) {
7646 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
7648 PEXT(&chunks_array[idx].chunk),
7649 chunks_array[idx].mirror_id[0]);
7653 rc = llapi_lease_check(fd);
7654 if (rc != LL_LEASE_RDLCK) {
7655 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7660 /* verify one chunk */
7661 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
7662 &chunks_array[idx], verbose);
7676 llapi_layout_free(layout);
7677 llapi_lease_put(fd);
7685 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
7686 * @argc: The count of lfs mirror verify command line arguments.
7687 * @argv: Array of strings for lfs mirror verify command line arguments.
7689 * This function parses lfs mirror verify command and verifies the
7690 * specified mirrored file(s).
7692 * Return: 0 on success or a negative error code on failure.
7694 static inline int lfs_mirror_verify(int argc, char **argv)
7696 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7704 struct option long_opts[] = {
7705 { .val = 'o', .name = "only", .has_arg = required_argument },
7706 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7709 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
7711 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
7714 rc = parse_mirror_ids(mirror_ids,
7715 ARRAY_SIZE(mirror_ids),
7719 "%s: bad mirror ids '%s'.\n",
7726 "%s: at least 2 mirror ids needed with '--only' option.\n",
7736 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7737 progname, argv[optind - 1]);
7743 if (argc == optind) {
7744 fprintf(stderr, "%s: no file name given.\n", progname);
7749 if (ids_nr > 0 && argc > optind + 1) {
7751 "%s: '--only' cannot be used upon multiple files.\n",
7759 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7765 for (; optind < argc; optind++) {
7766 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
7776 * lfs_mirror() - Parse and execute lfs mirror commands.
7777 * @argc: The count of lfs mirror command line arguments.
7778 * @argv: Array of strings for lfs mirror command line arguments.
7780 * This function parses lfs mirror commands and performs the
7781 * corresponding functions specified in mirror_cmdlist[].
7783 * Return: 0 on success or an error code on failure.
7785 static int lfs_mirror(int argc, char **argv)
7792 Parser_init("lfs-mirror > ", mirror_cmdlist);
7794 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
7796 program_invocation_short_name = cmd;
7798 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
7800 rc = Parser_commands();
7802 return rc < 0 ? -rc : rc;
7806 * lfs_mirror_list_commands() - List lfs mirror commands.
7807 * @argc: The count of command line arguments.
7808 * @argv: Array of strings for command line arguments.
7810 * This function lists lfs mirror commands defined in mirror_cmdlist[].
7812 * Return: 0 on success.
7814 static int lfs_mirror_list_commands(int argc, char **argv)
7816 char buffer[81] = "";
7818 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
7824 static int lfs_list_commands(int argc, char **argv)
7826 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
7828 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
7833 int main(int argc, char **argv)
7837 /* Ensure that liblustreapi constructor has run */
7838 if (!llapi_liblustreapi_initialized())
7839 fprintf(stderr, "liblustreapi was not properly initialized\n");
7844 Parser_init("lfs > ", cmdlist);
7846 progname = program_invocation_short_name; /* Used in error messages */
7848 llapi_set_command_name(argv[1]);
7849 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
7850 llapi_clear_command_name();
7852 rc = Parser_commands();
7855 return rc < 0 ? -rc : rc;
7858 #ifdef _LUSTRE_IDL_H_
7859 /* Everything we need here should be included by lustreapi.h. */
7860 # error "lfs should not depend on lustre_idl.h"
7861 #endif /* _LUSTRE_IDL_H_ */