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>
61 #include <sys/xattr.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
81 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
82 #endif /* !ARRAY_SIZE */
85 static int lfs_find(int argc, char **argv);
86 static int lfs_getstripe(int argc, char **argv);
87 static int lfs_getdirstripe(int argc, char **argv);
88 static int lfs_setdirstripe(int argc, char **argv);
89 static int lfs_rmentry(int argc, char **argv);
90 static int lfs_osts(int argc, char **argv);
91 static int lfs_mdts(int argc, char **argv);
92 static int lfs_df(int argc, char **argv);
93 static int lfs_getname(int argc, char **argv);
94 static int lfs_check(int argc, char **argv);
95 #ifdef HAVE_SYS_QUOTA_H
96 static int lfs_setquota(int argc, char **argv);
97 static int lfs_quota(int argc, char **argv);
98 static int lfs_project(int argc, char **argv);
100 static int lfs_flushctx(int argc, char **argv);
101 static int lfs_cp(int argc, char **argv);
102 static int lfs_ls(int argc, char **argv);
103 static int lfs_poollist(int argc, char **argv);
104 static int lfs_changelog(int argc, char **argv);
105 static int lfs_changelog_clear(int argc, char **argv);
106 static int lfs_fid2path(int argc, char **argv);
107 static int lfs_path2fid(int argc, char **argv);
108 static int lfs_data_version(int argc, char **argv);
109 static int lfs_hsm_state(int argc, char **argv);
110 static int lfs_hsm_set(int argc, char **argv);
111 static int lfs_hsm_clear(int argc, char **argv);
112 static int lfs_hsm_action(int argc, char **argv);
113 static int lfs_hsm_archive(int argc, char **argv);
114 static int lfs_hsm_restore(int argc, char **argv);
115 static int lfs_hsm_release(int argc, char **argv);
116 static int lfs_hsm_remove(int argc, char **argv);
117 static int lfs_hsm_cancel(int argc, char **argv);
118 static int lfs_swap_layouts(int argc, char **argv);
119 static int lfs_mv(int argc, char **argv);
120 static int lfs_ladvise(int argc, char **argv);
121 static int lfs_getsom(int argc, char **argv);
122 static int lfs_heat_get(int argc, char **argv);
123 static int lfs_heat_set(int argc, char **argv);
124 static int lfs_mirror(int argc, char **argv);
125 static int lfs_mirror_list_commands(int argc, char **argv);
126 static int lfs_list_commands(int argc, char **argv);
127 static inline int lfs_mirror_resync(int argc, char **argv);
128 static inline int lfs_mirror_verify(int argc, char **argv);
129 static inline int lfs_mirror_read(int argc, char **argv);
130 static inline int lfs_mirror_write(int argc, char **argv);
131 static inline int lfs_mirror_copy(int argc, char **argv);
133 enum setstripe_origin {
141 static int lfs_setstripe_internal(int argc, char **argv,
142 enum setstripe_origin opc);
144 static inline int lfs_setstripe(int argc, char **argv)
146 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
148 static inline int lfs_setstripe_migrate(int argc, char **argv)
150 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
152 static inline int lfs_mirror_create(int argc, char **argv)
154 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
156 static inline int lfs_mirror_extend(int argc, char **argv)
158 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
160 static inline int lfs_mirror_split(int argc, char **argv)
162 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
165 /* Setstripe and migrate share mostly the same parameters */
166 #define SSM_CMD_COMMON(cmd) \
167 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
168 " [--stripe-count|-c <stripe_count>]\n" \
169 " [--stripe-index|-i <start_ost_idx>]\n" \
170 " [--stripe-size|-S <stripe_size>]\n" \
171 " [--layout|-L <pattern>]\n" \
172 " [--pool|-p <pool_name>]\n" \
173 " [--ost|-o <ost_indices>]\n" \
174 " [--yaml|-y <yaml_template_file>]\n" \
175 " [--copy=<lustre_src>]\n"
177 #define SSM_HELP_COMMON \
178 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
179 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
180 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
181 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
182 "\t respectively)\n" \
183 "\tpool_name: Name of OST pool to use (default none)\n" \
184 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
185 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
186 "\t Indices be specified in a format of:\n" \
187 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
189 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
190 "\t If --pool is set with --ost, then the OSTs\n" \
191 "\t must be the members of the pool.\n" \
192 "\tcomp_end: Extent end of component, start after previous end.\n"\
193 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
194 "\t respectively, -1 for EOF). Must be a multiple of\n"\
195 "\t stripe_size.\n" \
196 "\tyaml_template_file:\n" \
197 "\t YAML layout template file, can't be used with -c,\n" \
198 "\t -i, -S, -p, -o, or -E arguments.\n" \
199 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
200 "\t another lustre file or directory, can't used with\n" \
201 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
203 #define MIRROR_CREATE_HELP \
204 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
205 "\t setstripe layout options\n" \
206 "\t It defaults to 1 if not specified; if specified,\n" \
207 "\t it must follow the option without a space.\n" \
208 "\t The option can also be repeated multiple times to\n" \
209 "\t separate mirrors that have different layouts.\n" \
210 "\tsetstripe options: Mirror layout\n" \
211 "\t It can be a plain layout or a composite layout.\n" \
212 "\t If not specified, the stripe options inherited\n" \
213 "\t from the previous component will be used.\n" \
214 "\tflags: set flags to the component of the current mirror.\n" \
215 "\t Only \"prefer\" flag is supported so far.\n"
217 #define MIRROR_EXTEND_HELP \
219 "\tvictim_file: The layout of victim_file will be split and used\n" \
220 "\t as a mirror added to the mirrored file.\n" \
221 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
222 "\t from victim file(s) in case the victim file(s)\n" \
223 "\t contains the same data as the original mirrored\n" \
226 #define MIRROR_EXTEND_USAGE \
227 " <--mirror-count|-N[mirror_count]>\n" \
228 " [setstripe options|-f <victim_file>]\n" \
231 #define SETSTRIPE_USAGE \
232 SSM_CMD_COMMON("setstripe") \
233 MIRROR_EXTEND_USAGE \
234 " <directory|filename>\n" \
238 #define MIGRATE_USAGE \
239 SSM_CMD_COMMON("migrate ") \
240 " [--block|-b] [--non-block|-n]\n" \
241 " [--non-direct|-D] [--verbose|-v]\n" \
245 "\tblock: Block file access during data migration (default)\n" \
246 "\tnon-block: Abort migrations if concurrent access is detected\n" \
247 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
248 "\tverbose: Print each filename as it is migrated\n" \
250 #define SETDIRSTRIPE_USAGE \
251 " [--mdt-count|-c stripe_count>\n" \
252 " [--mdt-hash|-H mdt_hash]\n" \
253 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
254 " [--default|-D] [--mode|-o mode] <dir>\n" \
255 "\tstripe_count: stripe count of the striped directory\n" \
256 "\tmdt_index: MDT index of first stripe\n" \
257 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
258 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
259 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
260 "\tdefault_stripe: set default dirstripe of the directory\n" \
261 "\tmode: the file access permission of the directory (octal)\n"
264 * command_t mirror_cmdlist - lfs mirror commands.
266 command_t mirror_cmdlist[] = {
267 { .pc_name = "create", .pc_func = lfs_mirror_create,
268 .pc_help = "Create a mirrored file.\n"
269 "usage: lfs mirror create "
270 "<--mirror-count|-N[mirror_count]> "
271 "[setstripe options] ... <filename|directory>\n"
272 MIRROR_CREATE_HELP },
273 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
274 .pc_help = "Extend a mirrored file.\n"
275 "usage: lfs mirror extend "
276 "<--mirror-count|-N[mirror_count]> [--no-verify] "
277 "[setstripe options|-f <victim_file>] ... <filename>\n"
278 MIRROR_EXTEND_HELP },
279 { .pc_name = "split", .pc_func = lfs_mirror_split,
280 .pc_help = "Split a mirrored file.\n"
281 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
282 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
283 "\t [-f <new_file>] <mirrored file>\n"
284 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
285 "\t can be fetched by lfs getstripe command.\n"
286 "\tcomp_id: Unique component ID within a mirror.\n"
287 "\tnew_file: This option indicates the layout of the split\n"
288 "\t mirror will be stored into. If not specified,\n"
289 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
290 "\t will be used.\n" },
291 { .pc_name = "read", .pc_func = lfs_mirror_read,
292 .pc_help = "Read the content of a specified mirror of a file.\n"
293 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
294 "[--outfile|-o <output_file>] <mirrored_file>\n" },
295 { .pc_name = "write", .pc_func = lfs_mirror_write,
296 .pc_help = "Write to a specified mirror of a file.\n"
297 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
298 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
299 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
300 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
301 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
302 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
303 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
304 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
305 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
306 "<mirrored file> [<mirrored file2>...]\n"},
307 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
308 .pc_help = "Verify mirrored file(s).\n"
309 "usage: lfs mirror verify "
310 "[--only <mirror_id,mirror_id2[,...]>] "
311 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
312 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
313 .pc_help = "list commands supported by lfs mirror"},
314 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
315 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
316 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
320 /* all available commands */
321 command_t cmdlist[] = {
322 {"setstripe", lfs_setstripe, 0,
323 "To create a file with specified striping/composite layout, or\n"
324 "create/replace the default layout on an existing directory:\n"
325 SSM_CMD_COMMON("setstripe")
326 " <directory|filename>\n"
328 "To add component(s) to an existing composite file:\n"
329 SSM_CMD_COMMON("setstripe --component-add")
331 "To totally delete the default striping from an existing directory:\n"
332 "usage: setstripe -d <directory>\n"
334 "To create a mirrored file or set s default mirror layout on a directory:\n"
335 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
337 "To delete the last component(s) from an existing composite file\n"
338 "(note that this will also delete any data in those components):\n"
339 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
340 " [--component-flags|-F <comp_flags>]\n"
342 "\tcomp_id: Unique component ID to delete\n"
343 "\tcomp_flags: 'init' indicating all instantiated components\n"
344 "\t '^init' indicating all uninstantiated components\n"
345 "\t-I and -F cannot be specified at the same time\n"},
346 {"getstripe", lfs_getstripe, 0,
347 "To list the layout pattern for a given file or files in a\n"
348 "directory or recursively for all files in a directory tree.\n"
349 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
350 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
351 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
352 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
353 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
354 " [--component-id[=comp_id]|-I[comp_id]]\n"
355 " [--component-flags[=comp_flags]]\n"
356 " [--component-count]\n"
357 " [--component-start[=[+-]comp_start]]\n"
358 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
359 " [[!] --mirror-index=[+-]<index> |\n"
360 " [!] --mirror-id=[+-]<id>]\n"
361 " <directory|filename> ..."},
362 {"setdirstripe", lfs_setdirstripe, 0,
363 "To create a striped directory on a specified MDT. This can only\n"
364 "be done on MDT0 with the right of administrator.\n"
365 "usage: setdirstripe [OPTION] <directory>\n"
367 {"getdirstripe", lfs_getdirstripe, 0,
368 "To list the layout pattern info for a given directory\n"
369 "or recursively for all directories in a directory tree.\n"
370 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
371 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
372 " [--recursive|-r] [--yaml|-y]\n"
373 " [--default|-D] <dir> ..."},
374 {"mkdir", lfs_setdirstripe, 0,
375 "To create a striped directory on a specified MDT. This can only\n"
376 "be done on MDT0 with the right of administrator.\n"
377 "usage: mkdir [OPTION] <directory>\n"
379 {"rm_entry", lfs_rmentry, 0,
380 "To remove the name entry of the remote directory. Note: This\n"
381 "command will only delete the name entry, i.e. the remote directory\n"
382 "will become inaccessable after this command. This can only be done\n"
383 "by the administrator\n"
384 "usage: rm_entry <dir>\n"},
385 {"pool_list", lfs_poollist, 0,
386 "List pools or pool OSTs\n"
387 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
388 {"find", lfs_find, 0,
389 "find files matching given attributes recursively in directory tree.\n"
390 "usage: find <directory|filename> ...\n"
391 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
392 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
393 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
394 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
395 " [[!] --stripe-count|-c [+-]<stripes>]\n"
396 " [[!] --stripe-index|-i <index,...>]\n"
397 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
398 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
399 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
400 " [[!] --projid <projid>]\n"
401 " [[!] --layout|-L released,raid0,mdt]\n"
402 " [[!] --component-count [+-]<comp_cnt>]\n"
403 " [[!] --component-start [+-]N[kMGTPE]]\n"
404 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
405 " [[!] --component-flags <comp_flags>]\n"
406 " [[!] --mirror-count|-N [+-]<n>]\n"
407 " [[!] --mirror-state <[^]state>]\n"
408 " [[!] --mdt-count|-T [+-]<stripes>]\n"
409 " [[!] --mdt-hash|-H <hashtype>\n"
410 " [[!] --mdt-index|-m <uuid|index,...>]\n"
411 "\t !: used before an option indicates 'NOT' requested attribute\n"
412 "\t -: used before a value indicates less than requested value\n"
413 "\t +: used before a value indicates more than requested value\n"
414 "\thashtype: hash type of the striped directory.\n"
415 "\t fnv_1a_64 FNV-1a hash algorithm\n"
416 "\t all_char sum of characters % MDT_COUNT\n"},
417 {"check", lfs_check, 0,
418 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
419 "or all the servers (MGTs, MDTs and OSTs).\n"
420 "usage: check <mgts|osts|mdts|all>"},
421 {"osts", lfs_osts, 0, "list OSTs connected to client "
422 "[for specified path only]\n" "usage: osts [path]"},
423 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
424 "[for specified path only]\n" "usage: mdts [path]"},
426 "report filesystem disk space usage or inodes usage"
427 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
428 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
429 {"getname", lfs_getname, 0, "list instances and specified mount points "
430 "[for specified path only]\n"
431 "Usage: getname [-h]|[path ...] "},
432 #ifdef HAVE_SYS_QUOTA_H
433 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
434 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
435 " -b <block-softlimit> -B <block-hardlimit>\n"
436 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
437 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
438 " [--block-softlimit <block-softlimit>]\n"
439 " [--block-hardlimit <block-hardlimit>]\n"
440 " [--inode-softlimit <inode-softlimit>]\n"
441 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
442 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
443 " [--block-grace <block-grace>]\n"
444 " [--inode-grace <inode-grace>] <filesystem>\n"
445 " setquota <-U|-G|-P>\n"
446 " -b <block-softlimit> -B <block-hardlimit>\n"
447 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
448 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
449 " [--block-softlimit <block-softlimit>]\n"
450 " [--block-hardlimit <block-hardlimit>]\n"
451 " [--inode-softlimit <inode-softlimit>]\n"
452 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
453 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
455 " -b can be used instead of --block-softlimit/--block-grace\n"
456 " -B can be used instead of --block-hardlimit\n"
457 " -i can be used instead of --inode-softlimit/--inode-grace\n"
458 " -I can be used instead of --inode-hardlimit\n"
459 " -d can be used instead of --default\n\n"
460 "Note: The total quota space will be split into many qunits and\n"
461 " balanced over all server targets, the minimal qunit size is\n"
462 " 1M bytes for block space and 1K inodes for inode space.\n\n"
463 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
464 " Quota space rebalancing process will stop when this mininum\n"
465 " value is reached. As a result, quota exceeded can be returned\n"
466 " while many targets still have 1MB or 1K inodes of spare\n"
468 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
469 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
471 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
472 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
473 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
474 {"project", lfs_project, 0,
475 "Change or list project attribute for specified file or directory.\n"
476 "usage: project [-d|-r] <file|directory...>\n"
477 " list project ID and flags on file(s) or directories\n"
478 " project [-p id] [-s] [-r] <file|directory...>\n"
479 " set project ID and/or inherit flag for specified file(s) or directories\n"
480 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
481 " check project ID and flags on file(s) or directories, print outliers\n"
482 " project -C [-r] [-k] <file|directory...>\n"
483 " clear the project inherit flag and ID on the file or directory\n"
486 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
487 "usage: flushctx [-k] [mountpoint...]"},
489 "Remote user copy files and directories.\n"
490 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
492 "Remote user list directory contents.\n"
493 "usage: ls [OPTION]... [FILE]..."},
494 {"changelog", lfs_changelog, 0,
495 "Show the metadata changes on an MDT."
496 "\nusage: changelog <mdtname> [startrec [endrec]]"},
497 {"changelog_clear", lfs_changelog_clear, 0,
498 "Indicate that old changelog records up to <endrec> are no longer of "
499 "interest to consumer <id>, allowing the system to free up space.\n"
500 "An <endrec> of 0 means all records.\n"
501 "usage: changelog_clear <mdtname> <id> <endrec>"},
502 {"fid2path", lfs_fid2path, 0,
503 "Resolve the full path(s) for given FID(s). For a specific hardlink "
504 "specify link number <linkno>.\n"
505 /* "For a historical link name, specify changelog record <recno>.\n" */
506 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
507 /* [ --rec <recno> ] */ },
508 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
509 "usage: path2fid [--parents] <path> ..."},
510 {"data_version", lfs_data_version, 0, "Display file data version for "
511 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
512 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
513 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
514 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
515 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
516 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
517 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
519 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
520 "[--archived] [--lost] <file> ..."},
521 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
522 "given files.\n" "usage: hsm_action <file> ..."},
523 {"hsm_archive", lfs_hsm_archive, 0,
524 "Archive file to external storage.\n"
525 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
527 {"hsm_restore", lfs_hsm_restore, 0,
528 "Restore file from external storage.\n"
529 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
530 {"hsm_release", lfs_hsm_release, 0,
531 "Release files from Lustre.\n"
532 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
533 {"hsm_remove", lfs_hsm_remove, 0,
534 "Remove file copy from external storage.\n"
535 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
537 " (FILE [FILE ...] | "
538 "--mntpath MOUNTPATH FID [FID ...])\n"
540 "Note: To remove an archived copy of a file already deleted from a "
542 "--mntpath option and a list of FIDs must be specified"
544 {"hsm_cancel", lfs_hsm_cancel, 0,
545 "Cancel requests related to specified files.\n"
546 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
547 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
548 "usage: swap_layouts <path1> <path2>"},
549 {"migrate", lfs_setstripe_migrate, 0,
550 "migrate a directory between MDTs.\n"
551 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
552 " [--mdt-hash|-H] <hash_type>\n"
553 " [--mdt-index|-m] <start_mdt_index>\n"
556 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
557 " it's the MDT index of first stripe\n"
558 "\tmdt_count: number of MDTs to stripe a directory over\n"
559 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
560 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
561 " all_char sum of characters % MDT_COUNT\n"
563 "migrate file objects from one OST "
564 "layout\nto another (may be not safe with concurent writes).\n"
566 "[--stripe-count|-c] <stripe_count>\n"
567 " [--stripe-index|-i] <start_ost_index>\n"
568 " [--stripe-size|-S] <stripe_size>\n"
569 " [--pool|-p] <pool_name>\n"
570 " [--ost|-o] <ost_indices>\n"
572 " [--non-block|-n]\n"
573 " [--non-direct|-D]\n"
574 " <file|directory>\n"
575 "\tstripe_count: number of OSTs to stripe a file over\n"
576 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
577 "\tstripe_size: number of bytes to store before moving to the next OST\n"
578 "\tpool_name: name of the predefined pool of OSTs\n"
579 "\tost_indices: OSTs to stripe over, in order\n"
580 "\tblock: Block file access during data migration (default)\n"
581 "\tnon-block: Abort migrations if concurrent access is detected\n"
582 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
584 "To move directories between MDTs. This command is deprecated, "
585 "use \"migrate\" instead.\n"
586 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
588 {"ladvise", lfs_ladvise, 0,
589 "Provide servers with advice about access patterns for a file.\n"
590 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
591 " [--background|-b] [--unset|-u]\n\n"
592 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
593 " {[--mode|-m [READ,WRITE]}\n"
595 {"mirror", lfs_mirror, mirror_cmdlist,
596 "lfs commands used to manage files with mirrored components:\n"
597 "lfs mirror create - create a mirrored file or directory\n"
598 "lfs mirror extend - add mirror(s) to an existing file\n"
599 "lfs mirror split - split a mirror from an existing mirrored file\n"
600 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
601 "lfs mirror read - read a mirror content of a mirrored file\n"
602 "lfs mirror write - write to a mirror of a mirrored file\n"
603 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
604 "lfs mirror verify - verify mirrored file(s)\n"},
605 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
606 "usage: getsom [-s] [-b] [-f] <path>\n"
607 "\t-s: Only show the size value of the SOM data for a given file\n"
608 "\t-b: Only show the blocks value of the SOM data for a given file\n"
609 "\t-f: Only show the flags value of the SOM data for a given file\n"},
610 {"heat_get", lfs_heat_get, 0,
611 "To get heat of files.\n"
612 "usage: heat_get <file> ...\n"},
613 {"heat_set", lfs_heat_set, 0,
614 "To set heat flags of files.\n"
615 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
616 "\t--clear|-c: Clear file heat for given files\n"
617 "\t--off|-o: Turn off file heat for given files\n"
618 "\t--on|-O: Turn on file heat for given files\n"},
619 {"help", Parser_help, 0, "help"},
620 {"exit", Parser_quit, 0, "quit"},
621 {"quit", Parser_quit, 0, "quit"},
622 {"--version", Parser_version, 0,
623 "output build version of the utility and exit"},
624 {"--list-commands", lfs_list_commands, 0,
625 "list commands supported by the utility and exit"},
630 static int check_hashtype(const char *hashtype)
634 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
635 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
642 static const char *error_loc = "syserror";
645 MIGRATION_NONBLOCK = 0x0001,
646 MIGRATION_MIRROR = 0x0002,
647 MIGRATION_NONDIRECT = 0x0004,
648 MIGRATION_VERBOSE = 0x0008,
651 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
652 struct llapi_layout *layout);
655 migrate_open_files(const char *name, __u64 migration_flags,
656 const struct llapi_stripe_param *param,
657 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
664 char parent[PATH_MAX];
665 char volatile_file[PATH_MAX];
671 if (param == NULL && layout == NULL) {
672 error_loc = "layout information";
676 /* search for file directory pathname */
677 if (strlen(name) > sizeof(parent) - 1) {
678 error_loc = "source file name";
682 strncpy(parent, name, sizeof(parent));
683 ptr = strrchr(parent, '/');
685 if (getcwd(parent, sizeof(parent)) == NULL) {
686 error_loc = "getcwd";
690 if (ptr == parent) /* leading '/' */
695 /* open file, direct io */
696 /* even if the file is only read, WR mode is nedeed to allow
697 * layout swap on fd */
699 if (!(migration_flags & MIGRATION_NONDIRECT))
701 fd = open(name, rflags);
704 error_loc = "cannot open source file";
708 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
710 error_loc = "cannot get MDT index";
715 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
716 mode_t open_mode = S_IRUSR | S_IWUSR;
718 random_value = random();
719 rc = snprintf(volatile_file, sizeof(volatile_file),
720 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
721 mdt_index, random_value);
722 if (rc >= sizeof(volatile_file)) {
727 /* create, open a volatile file, use caching (ie no directio) */
729 fdv = llapi_file_open_param(volatile_file, open_flags,
732 fdv = lfs_component_create(volatile_file, open_flags,
734 } while (fdv < 0 && (rc = fdv) == -EEXIST);
737 error_loc = "cannot create volatile file";
741 /* In case the MDT does not support creation of volatile files
742 * we should try to unlink it. */
743 (void)unlink(volatile_file);
745 /* Not-owner (root?) special case.
746 * Need to set owner/group of volatile file like original.
747 * This will allow to pass related check during layout_swap.
752 error_loc = "cannot stat source file";
756 rc = fstat(fdv, &stv);
759 error_loc = "cannot stat volatile";
763 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
764 rc = fchown(fdv, st.st_uid, st.st_gid);
767 error_loc = "cannot change ownwership of volatile";
786 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
788 struct llapi_layout *layout;
789 size_t buf_size = 4 * 1024 * 1024;
798 layout = llapi_layout_get_by_fd(fd_src, 0);
799 if (layout != NULL) {
800 uint64_t stripe_size;
802 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
804 buf_size = stripe_size;
806 llapi_layout_free(layout);
809 /* Use a page-aligned buffer for direct I/O */
810 rc = posix_memalign(&buf, getpagesize(), buf_size);
815 /* read new data only if we have written all
816 * previously read data */
819 rc = check_file(fd_src);
824 rsize = read(fd_src, buf, buf_size);
836 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
855 static int migrate_copy_timestamps(int fd, int fdv)
859 if (fstat(fd, &st) == 0) {
860 struct timeval tv[2] = {
861 {.tv_sec = st.st_atime},
862 {.tv_sec = st.st_mtime}
865 return futimes(fdv, tv);
871 static int migrate_block(int fd, int fdv)
878 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
880 error_loc = "cannot get dataversion";
888 /* The grouplock blocks all concurrent accesses to the file.
889 * It has to be taken after llapi_get_data_version as it would
891 rc = llapi_group_lock(fd, gid);
893 error_loc = "cannot get group lock";
897 rc = migrate_copy_data(fd, fdv, NULL);
899 error_loc = "data copy failed";
903 /* Make sure we keep original atime/mtime values */
904 rc = migrate_copy_timestamps(fd, fdv);
906 error_loc = "timestamp copy failed";
911 * for a migration we need to check data version on file did
914 * Pass in gid=0 since we already own grouplock. */
915 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
916 SWAP_LAYOUTS_CHECK_DV1);
918 error_loc = "file changed";
921 error_loc = "cannot swap layout";
926 rc2 = llapi_group_unlock(fd, gid);
927 if (rc2 < 0 && rc == 0) {
928 error_loc = "unlock group lock";
936 * Internal helper for migrate_copy_data(). Check lease and report error if
939 * \param[in] fd File descriptor on which to check the lease.
941 * \retval 0 Migration can keep on going.
942 * \retval -errno Error occurred, abort migration.
944 static int check_lease(int fd)
948 rc = llapi_lease_check(fd);
950 return 0; /* llapi_check_lease returns > 0 on success. */
955 static int migrate_nonblock(int fd, int fdv)
961 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
963 error_loc = "cannot get data version";
967 rc = migrate_copy_data(fd, fdv, check_lease);
969 error_loc = "data copy failed";
973 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
975 error_loc = "cannot get data version";
981 error_loc = "source file changed";
985 /* Make sure we keep original atime/mtime values */
986 rc = migrate_copy_timestamps(fd, fdv);
988 error_loc = "timestamp copy failed";
995 static int lfs_component_set(char *fname, int comp_id,
996 __u32 flags, __u32 neg_flags)
999 __u32 flags_array[2];
1004 ids[count] = comp_id;
1005 flags_array[count] = flags;
1010 ids[count] = comp_id;
1011 flags_array[count] = neg_flags | LCME_FL_NEG;
1015 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1018 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1019 progname, comp_id, fname, flags, neg_flags);
1024 static int lfs_component_del(char *fname, __u32 comp_id,
1025 __u32 flags, __u32 neg_flags)
1029 if (flags && neg_flags)
1032 if (!flags && neg_flags)
1033 flags = neg_flags | LCME_FL_NEG;
1035 if ((flags && comp_id) || (!flags && !comp_id))
1038 /* LCME_FL_INIT is the only supported flag in PFL */
1040 if (flags & ~LCME_KNOWN_FLAGS) {
1042 "%s setstripe: unknown flags %#x\n",
1046 } else if (comp_id > LCME_ID_MAX) {
1047 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1052 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1055 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1056 progname, comp_id, fname, strerror(errno));
1060 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1067 rc = llapi_layout_file_comp_add(fname, layout);
1069 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1070 fname, strerror(errno));
1074 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1075 struct llapi_layout *layout)
1083 fd = lstat(fname, &st);
1084 if (fd == 0 && S_ISDIR(st.st_mode))
1085 open_flags = O_DIRECTORY | O_RDONLY;
1087 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1089 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1090 S_ISDIR(st.st_mode) ?
1091 "set default composite layout for" :
1092 "create composite file",
1093 fname, strerror(errno));
1097 static int lfs_migrate(char *name, __u64 migration_flags,
1098 struct llapi_stripe_param *param,
1099 struct llapi_layout *layout)
1105 rc = migrate_open_files(name, migration_flags, param, layout,
1110 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1111 /* Blocking mode (forced if servers do not support file lease).
1112 * It is also the default mode, since we cannot distinguish
1113 * between a broken lease and a server that does not support
1114 * atomic swap/close (LU-6785) */
1115 rc = migrate_block(fd, fdv);
1119 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1121 error_loc = "cannot get lease";
1125 rc = migrate_nonblock(fd, fdv);
1127 llapi_lease_release(fd);
1131 /* Atomically put lease, swap layouts and close.
1132 * for a migration we need to check data version on file did
1134 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1136 error_loc = "cannot swap layout";
1148 fprintf(stderr, "error: %s: %s: %s: %s\n",
1149 progname, name, error_loc, strerror(-rc));
1150 else if (migration_flags & MIGRATION_VERBOSE)
1151 printf("%s\n", name);
1156 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1165 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1169 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1170 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1171 const char *comp_name = comp_flags_table[i].cfn_name;
1173 if (strcmp(name, comp_name) == 0) {
1174 *flags |= comp_flag;
1176 } else if (strncmp(name, "^", 1) == 0 &&
1177 strcmp(name + 1, comp_name) == 0) {
1178 *neg_flags |= comp_flag;
1183 llapi_printf(LLAPI_MSG_ERROR,
1184 "%s: component flag '%s' not supported\n",
1190 if (!*flags && !*neg_flags)
1193 /* don't allow to set and exclude the same flag */
1194 if (*flags & *neg_flags)
1200 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1208 if (strncmp(string, "^", 1) == 0) {
1209 *neg_state = llapi_layout_string_flags(string + 1);
1210 if (*neg_state != 0)
1213 *state = llapi_layout_string_flags(string);
1218 llapi_printf(LLAPI_MSG_ERROR,
1219 "%s: mirrored file state '%s' not supported\n",
1225 * struct mirror_args - Command-line arguments for mirror(s).
1226 * @m_count: Number of mirrors to be created with this layout.
1227 * @m_flags: Mirror level flags, only 'prefer' is supported.
1228 * @m_layout: Mirror layout.
1229 * @m_file: A victim file. Its layout will be split and used as a mirror.
1230 * @m_next: Point to the next node of the list.
1232 * Command-line arguments for mirror(s) will be parsed and stored in
1233 * a linked list that consists of this structure.
1235 struct mirror_args {
1238 struct llapi_layout *m_layout;
1240 struct mirror_args *m_next;
1243 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1248 rc = llapi_layout_comp_flags_get(layout, &flags);
1252 if (flags & LCME_FL_NEG) {
1253 fprintf(stderr, "error: %s: negative flags are not supported\n",
1258 if (flags & LCME_FL_STALE) {
1259 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1260 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1264 return LLAPI_LAYOUT_ITER_CONT;
1267 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1269 uint64_t start, end;
1273 /* LU-10112: do not support dom+flr in phase 1 */
1274 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1278 rc = llapi_layout_pattern_get(layout, &pattern);
1282 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1283 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1288 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1292 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1296 if (end != LUSTRE_EOF) {
1297 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1302 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1307 * enum mirror_flags - Flags for extending a mirrored file.
1308 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1309 * in case the victim file(s) contains the same data as the
1310 * original mirrored file.
1311 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1312 * @MF_COMP_ID: specified component id instead of mirror id
1314 * Flags for extending a mirrored file.
1323 * mirror_create_sanity_check() - Check mirror list.
1324 * @list: A linked list that stores the mirror arguments.
1326 * This function does a sanity check on @list for creating
1329 * Return: 0 on success or a negative error code on failure.
1331 static int mirror_create_sanity_check(const char *fname,
1332 struct mirror_args *list)
1335 bool has_m_file = false;
1336 bool has_m_layout = false;
1342 struct llapi_layout *layout;
1344 layout = llapi_layout_get_by_path(fname, 0);
1347 "error: %s: file '%s' couldn't get layout\n",
1352 rc = mirror_sanity_check_one(layout);
1353 llapi_layout_free(layout);
1359 while (list != NULL) {
1360 if (list->m_file != NULL) {
1362 llapi_layout_free(list->m_layout);
1365 llapi_layout_get_by_path(list->m_file, 0);
1366 if (list->m_layout == NULL) {
1368 "error: %s: file '%s' has no layout\n",
1369 progname, list->m_file);
1373 has_m_layout = true;
1374 if (list->m_layout == NULL) {
1375 fprintf(stderr, "error: %s: no mirror layout\n",
1381 rc = mirror_sanity_check_one(list->m_layout);
1385 list = list->m_next;
1388 if (has_m_file && has_m_layout) {
1390 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1398 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1400 __u32 mirror_flags = *(__u32 *)cbdata;
1404 rc = llapi_layout_comp_flags_get(layout, &flags);
1409 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1414 return LLAPI_LAYOUT_ITER_CONT;
1418 * mirror_create() - Create a mirrored file.
1419 * @fname: The file to be created.
1420 * @mirror_list: A linked list that stores the mirror arguments.
1422 * This function creates a mirrored file @fname with the mirror(s)
1423 * from @mirror_list.
1425 * Return: 0 on success or a negative error code on failure.
1427 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1429 struct llapi_layout *layout = NULL;
1430 struct mirror_args *cur_mirror = NULL;
1431 uint16_t mirror_count = 0;
1435 rc = mirror_create_sanity_check(NULL, mirror_list);
1439 cur_mirror = mirror_list;
1440 while (cur_mirror != NULL) {
1441 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1443 &cur_mirror->m_flags);
1446 fprintf(stderr, "%s: failed to set mirror flags\n",
1451 for (i = 0; i < cur_mirror->m_count; i++) {
1452 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1455 fprintf(stderr, "error: %s: "
1456 "merge layout failed: %s\n",
1457 progname, strerror(errno));
1461 mirror_count += cur_mirror->m_count;
1462 cur_mirror = cur_mirror->m_next;
1465 if (layout == NULL) {
1466 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1470 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1473 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1474 progname, strerror(errno));
1478 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1486 llapi_layout_free(layout);
1491 * Compare files and check lease on @fd.
1493 * \retval bytes number of bytes are the same
1495 static ssize_t mirror_file_compare(int fd, int fdv)
1497 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1499 ssize_t bytes_done = 0;
1500 ssize_t bytes_read = 0;
1502 buf = malloc(buflen * 2);
1507 if (!llapi_lease_check(fd)) {
1508 bytes_done = -EBUSY;
1512 bytes_read = read(fd, buf, buflen);
1513 if (bytes_read <= 0)
1516 if (bytes_read != read(fdv, buf + buflen, buflen))
1519 /* XXX: should compute the checksum on each buffer and then
1520 * compare checksum to avoid cache collision */
1521 if (memcmp(buf, buf + buflen, bytes_read))
1524 bytes_done += bytes_read;
1532 static int mirror_extend_file(const char *fname, const char *victim_file,
1533 enum mirror_flags mirror_flags)
1538 struct stat stbuf_v;
1539 struct ll_ioc_lease *data = NULL;
1542 fd = open(fname, O_RDWR);
1544 error_loc = "open source file";
1549 fdv = open(victim_file, O_RDWR);
1551 error_loc = "open target file";
1556 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1557 error_loc = "stat source or target file";
1562 if (stbuf.st_dev != stbuf_v.st_dev) {
1563 error_loc = "stat source and target file";
1568 /* mirrors should be of the same size */
1569 if (stbuf.st_size != stbuf_v.st_size) {
1570 error_loc = "file sizes don't match";
1575 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1577 error_loc = "cannot get lease";
1581 if (!(mirror_flags & MF_NO_VERIFY)) {
1583 /* mirrors should have the same contents */
1584 ret = mirror_file_compare(fd, fdv);
1585 if (ret != stbuf.st_size) {
1586 error_loc = "file busy or contents don't match";
1587 rc = ret < 0 ? ret : -EINVAL;
1592 /* Get rid of caching pages from clients */
1593 rc = llapi_file_flush(fd);
1595 error_loc = "cannot get data version";
1599 rc = llapi_file_flush(fdv);
1601 error_loc = "cannot get data version";
1606 /* Make sure we keep original atime/mtime values */
1607 rc = migrate_copy_timestamps(fd, fdv);
1609 error_loc = "cannot copy timestamp";
1613 /* Atomically put lease, merge layouts and close. */
1614 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1616 error_loc = "memory allocation";
1619 data->lil_mode = LL_LEASE_UNLCK;
1620 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1621 data->lil_count = 1;
1622 data->lil_ids[0] = fdv;
1623 rc = llapi_lease_set(fd, data);
1625 error_loc = "cannot merge layout";
1627 } else if (rc == 0) {
1629 error_loc = "lost lease lock";
1642 (void) unlink(victim_file);
1644 fprintf(stderr, "error: %s: %s: %s: %s\n",
1645 progname, fname, error_loc, strerror(-rc));
1649 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1651 struct ll_ioc_lease *data = NULL;
1656 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1660 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1662 error_loc = "cannot get lease";
1666 rc = migrate_nonblock(fd, fdv);
1668 llapi_lease_release(fd);
1672 /* Atomically put lease, merge layouts and close. */
1673 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1675 error_loc = "memory allocation";
1678 data->lil_mode = LL_LEASE_UNLCK;
1679 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1680 data->lil_count = 1;
1681 data->lil_ids[0] = fdv;
1682 rc = llapi_lease_set(fd, data);
1684 error_loc = "cannot merge layout";
1686 } else if (rc == 0) {
1688 error_loc = "lost lease lock";
1701 fprintf(stderr, "error: %s: %s: %s: %s\n",
1702 progname, name, error_loc, strerror(-rc));
1706 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1707 enum mirror_flags mirror_flags)
1711 rc = mirror_create_sanity_check(fname, mirror_list);
1715 while (mirror_list) {
1716 if (mirror_list->m_file != NULL) {
1717 rc = mirror_extend_file(fname, mirror_list->m_file,
1720 __u32 mirror_count = mirror_list->m_count;
1722 while (mirror_count > 0) {
1723 rc = mirror_extend_layout(fname,
1724 mirror_list->m_layout);
1734 mirror_list = mirror_list->m_next;
1740 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1745 rc = llapi_layout_mirror_id_get(layout, &id);
1749 if ((__u16)id == *(__u16 *)cbdata)
1750 return LLAPI_LAYOUT_ITER_STOP;
1752 return LLAPI_LAYOUT_ITER_CONT;
1755 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1760 rc = llapi_layout_comp_id_get(layout, &id);
1764 if (id == *(__u32 *)cbdata)
1765 return LLAPI_LAYOUT_ITER_STOP;
1767 return LLAPI_LAYOUT_ITER_CONT;
1769 static int mirror_split(const char *fname, __u32 id,
1770 enum mirror_flags mflags, const char *victim_file)
1772 struct llapi_layout *layout;
1773 char parent[PATH_MAX];
1774 char victim[PATH_MAX];
1775 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1777 struct ll_ioc_lease *data;
1778 uint16_t mirror_count;
1783 /* check fname contains mirror with mirror_id/comp_id */
1784 layout = llapi_layout_get_by_path(fname, 0);
1787 "error %s: file '%s' couldn't get layout\n",
1792 rc = mirror_sanity_check_one(layout);
1796 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1799 "error %s: file '%s' couldn't get mirror count\n",
1803 if (mirror_count < 2) {
1805 "error %s: file '%s' has %d component, cannot split\n",
1806 progname, fname, mirror_count);
1810 if (mflags & MF_COMP_ID) {
1811 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1812 id = mirror_id_of(id);
1814 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1817 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1820 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1822 "error %s: file '%s' does not contain mirror with id %u\n",
1823 progname, fname, id);
1827 fd = open(fname, O_RDWR);
1830 "error %s: open file '%s' failed: %s\n",
1831 progname, fname, strerror(errno));
1835 /* get victim file directory pathname */
1836 if (strlen(fname) > sizeof(parent) - 1) {
1837 fprintf(stderr, "error %s: file name of '%s' too long\n",
1842 strncpy(parent, fname, sizeof(parent));
1843 ptr = strrchr(parent, '/');
1845 if (getcwd(parent, sizeof(parent)) == NULL) {
1846 fprintf(stderr, "error %s: getcwd failed: %s\n",
1847 progname, strerror(errno));
1857 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1859 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1864 if (victim_file == NULL) {
1865 /* use a temp file to store the splitted layout */
1866 if (mflags & MF_DESTROY) {
1867 fdv = llapi_create_volatile_idx(parent, mdt_index,
1868 O_LOV_DELAY_CREATE);
1870 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1872 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1875 /* user specified victim file */
1876 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1881 "error %s: create victim file failed: %s\n",
1882 progname, strerror(errno));
1886 /* get lease lock of fname */
1887 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1890 "error %s: cannot get lease of file '%s': %d\n",
1891 progname, fname, rc);
1895 /* Atomatically put lease, split layouts and close. */
1896 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1902 data->lil_mode = LL_LEASE_UNLCK;
1903 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1904 data->lil_count = 2;
1905 data->lil_ids[0] = fdv;
1906 data->lil_ids[1] = id;
1907 rc = llapi_lease_set(fd, data);
1909 if (rc == 0) /* lost lease lock */
1912 "error %s: cannot split '%s': %s\n",
1913 progname, fname, strerror(-rc));
1924 llapi_layout_free(layout);
1929 * Parse a string containing an target index list into an array of integers.
1931 * The input string contains a comma delimited list of individual
1932 * indices and ranges, for example "1,2-4,7". Add the indices into the
1933 * \a tgts array and remove duplicates.
1935 * \param[out] tgts array to store indices in
1936 * \param[in] size size of \a tgts array
1937 * \param[in] offset starting index in \a tgts
1938 * \param[in] arg string containing OST index list
1940 * \retval positive number of indices in \a tgts
1941 * \retval -EINVAL unable to parse \a arg
1943 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1947 int slots = size - offset;
1954 end_of_loop = false;
1955 while (!end_of_loop) {
1959 char *endptr = NULL;
1963 ptr = strchrnul(arg, ',');
1965 end_of_loop = *ptr == '\0';
1968 start_index = strtol(arg, &endptr, 0);
1969 if (endptr == arg) /* no data at all */
1971 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1974 end_index = start_index;
1975 if (*endptr == '-') {
1976 end_index = strtol(endptr + 1, &endptr, 0);
1977 if (*endptr != '\0')
1979 if (end_index < start_index)
1983 for (i = start_index; i <= end_index && slots > 0; i++) {
1986 /* remove duplicate */
1987 for (j = 0; j < offset; j++) {
1991 if (j == offset) { /* no duplicate */
1996 if (slots == 0 && i < end_index)
2004 if (!end_of_loop && ptr != NULL)
2007 return rc < 0 ? rc : nr;
2010 struct lfs_setstripe_args {
2011 unsigned long long lsa_comp_end;
2012 unsigned long long lsa_stripe_size;
2013 long long lsa_stripe_count;
2014 long long lsa_stripe_off;
2015 __u32 lsa_comp_flags;
2016 __u32 lsa_comp_neg_flags;
2017 unsigned long long lsa_pattern;
2018 unsigned int lsa_mirror_count;
2020 bool lsa_first_comp;
2022 char *lsa_pool_name;
2025 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2027 unsigned int mirror_count = lsa->lsa_mirror_count;
2028 bool first_comp = lsa->lsa_first_comp;
2030 memset(lsa, 0, sizeof(*lsa));
2032 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2033 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2034 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2035 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2036 lsa->lsa_pool_name = NULL;
2038 lsa->lsa_mirror_count = mirror_count;
2039 lsa->lsa_first_comp = first_comp;
2043 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2044 * @lsa: Stripe options to be initialized and inherited.
2046 * This function initializes stripe options in @lsa and inherit
2047 * stripe_size, stripe_count and OST pool_name options.
2051 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2053 unsigned long long stripe_size;
2054 long long stripe_count;
2055 char *pool_name = NULL;
2057 stripe_size = lsa->lsa_stripe_size;
2058 stripe_count = lsa->lsa_stripe_count;
2059 pool_name = lsa->lsa_pool_name;
2061 setstripe_args_init(lsa);
2063 lsa->lsa_stripe_size = stripe_size;
2064 lsa->lsa_stripe_count = stripe_count;
2065 lsa->lsa_pool_name = pool_name;
2068 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2070 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2071 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2072 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2073 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2074 lsa->lsa_pool_name != NULL ||
2075 lsa->lsa_comp_end != 0);
2079 * comp_args_to_layout() - Create or extend a composite layout.
2080 * @composite: Pointer to the composite layout.
2081 * @lsa: Stripe options for the new component.
2083 * This function creates or extends a composite layout by adding a new
2084 * component with stripe options from @lsa.
2086 * Return: 0 on success or an error code on failure.
2088 static int comp_args_to_layout(struct llapi_layout **composite,
2089 struct lfs_setstripe_args *lsa,
2092 struct llapi_layout *layout = *composite;
2093 uint64_t prev_end = 0;
2096 if (layout == NULL) {
2097 layout = llapi_layout_alloc();
2098 if (layout == NULL) {
2099 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2103 *composite = layout;
2107 /* Get current component extent, current component
2108 * must be the tail component. */
2109 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2111 fprintf(stderr, "Get comp extent failed. %s\n",
2116 if (lsa->lsa_first_comp)
2119 if (lsa->lsa_first_comp)
2120 rc = llapi_layout_add_first_comp(layout);
2122 rc = llapi_layout_comp_add(layout);
2124 fprintf(stderr, "Add component failed. %s\n",
2129 /* reset lsa_first_comp */
2130 lsa->lsa_first_comp = false;
2133 rc = llapi_layout_comp_extent_set(layout, prev_end,
2136 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2137 prev_end, lsa->lsa_comp_end, strerror(errno));
2142 /* Data-on-MDT component setting */
2143 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2144 /* In case of Data-on-MDT patterns the only extra option
2145 * applicable is stripe size option. */
2146 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2147 fprintf(stderr, "Option 'stripe-count' can't be "
2148 "specified with Data-on-MDT component: %lld\n",
2149 lsa->lsa_stripe_count);
2152 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2153 fprintf(stderr, "Option 'stripe-size' can't be "
2154 "specified with Data-on-MDT component: %llu\n",
2155 lsa->lsa_stripe_size);
2158 if (lsa->lsa_nr_tgts != 0) {
2159 fprintf(stderr, "Option 'ost-list' can't be specified "
2160 "with Data-on-MDT component: '%i'\n",
2164 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2165 fprintf(stderr, "Option 'stripe-offset' can't be "
2166 "specified with Data-on-MDT component: %lld\n",
2167 lsa->lsa_stripe_off);
2170 if (lsa->lsa_pool_name != 0) {
2171 fprintf(stderr, "Option 'pool' can't be specified "
2172 "with Data-on-MDT component: '%s'\n",
2173 lsa->lsa_pool_name);
2177 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2179 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2180 lsa->lsa_pattern, strerror(errno));
2183 /* Data-on-MDT component has always single stripe up to end */
2184 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2187 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2189 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2190 lsa->lsa_stripe_size, strerror(errno));
2194 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2196 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2197 lsa->lsa_stripe_count, strerror(errno));
2201 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2203 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2204 lsa->lsa_comp_flags, strerror(errno));
2208 if (lsa->lsa_pool_name != NULL) {
2209 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2211 fprintf(stderr, "Set pool name: %s failed. %s\n",
2212 lsa->lsa_pool_name, strerror(errno));
2216 rc = llapi_layout_pool_name_set(layout, "");
2218 fprintf(stderr, "Clear pool name failed: %s\n",
2224 if (lsa->lsa_nr_tgts > 0) {
2225 if (lsa->lsa_stripe_count > 0 &&
2226 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2227 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2228 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2229 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2230 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2233 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2234 rc = llapi_layout_ost_index_set(layout, i,
2239 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2240 lsa->lsa_stripe_off != -1) {
2241 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2244 fprintf(stderr, "Set ost index %d failed. %s\n",
2245 i, strerror(errno));
2252 static int build_component(struct llapi_layout **layout,
2253 struct lfs_setstripe_args *lsa, bool set_extent)
2257 rc = comp_args_to_layout(layout, lsa, set_extent);
2261 if (lsa->lsa_mirror_count > 0) {
2262 rc = llapi_layout_mirror_count_set(*layout,
2263 lsa->lsa_mirror_count);
2267 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2270 lsa->lsa_mirror_count = 0;
2276 static int build_layout_from_yaml_node(struct cYAML *node,
2277 struct llapi_layout **layout,
2278 struct lfs_setstripe_args *lsa,
2285 if (node->cy_type == CYAML_TYPE_OBJECT) {
2286 /* go deep to sub blocks */
2287 rc = build_layout_from_yaml_node(node->cy_child, layout,
2292 if (node->cy_string == NULL)
2295 string = node->cy_string;
2296 /* skip leading lmm_ if present, to simplify parsing */
2297 if (strncmp(string, "lmm_", 4) == 0)
2300 if (node->cy_type == CYAML_TYPE_STRING) {
2301 if (!strcmp(string, "lcme_extent.e_end")) {
2302 if (!strcmp(node->cy_valuestring, "EOF") ||
2303 !strcmp(node->cy_valuestring, "eof"))
2304 lsa->lsa_comp_end = LUSTRE_EOF;
2305 } else if (!strcmp(string, "pool")) {
2306 lsa->lsa_pool_name = node->cy_valuestring;
2307 } else if (!strcmp(string, "pattern")) {
2308 if (!strcmp(node->cy_valuestring, "mdt"))
2309 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2310 } else if (!strcmp(string, "lcme_flags")) {
2311 rc = comp_str2flags(node->cy_valuestring,
2312 &lsa->lsa_comp_flags,
2313 &lsa->lsa_comp_neg_flags);
2316 /* Only template flags have meaning in
2317 * the layout for a new file
2319 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2321 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2322 if (!strcmp(string, "lcm_mirror_count")) {
2323 lsa->lsa_mirror_count = node->cy_valueint;
2324 } else if (!strcmp(string, "lcme_extent.e_start")) {
2325 if (node->cy_valueint != 0 || *layout != NULL) {
2326 rc = build_component(layout, lsa, true);
2331 if (node->cy_valueint == 0)
2332 lsa->lsa_first_comp = true;
2334 /* initialize lsa */
2335 setstripe_args_init(lsa);
2336 lsa->lsa_tgts = osts;
2337 } else if (!strcmp(string, "lcme_extent.e_end")) {
2338 if (node->cy_valueint == -1)
2339 lsa->lsa_comp_end = LUSTRE_EOF;
2341 lsa->lsa_comp_end = node->cy_valueint;
2342 } else if (!strcmp(string, "stripe_count")) {
2343 lsa->lsa_stripe_count = node->cy_valueint;
2344 } else if (!strcmp(string, "stripe_size")) {
2345 lsa->lsa_stripe_size = node->cy_valueint;
2346 } else if (!strcmp(string, "stripe_offset")) {
2347 lsa->lsa_stripe_off = node->cy_valueint;
2348 } else if (!strcmp(string, "l_ost_idx")) {
2349 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2354 node = node->cy_next;
2360 static int lfs_comp_create_from_yaml(char *template,
2361 struct llapi_layout **layout,
2362 struct lfs_setstripe_args *lsa,
2365 struct cYAML *tree = NULL, *err_rc = NULL;
2368 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2370 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2371 progname, template);
2372 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2373 "can't parse", &err_rc);
2374 cYAML_print_tree2file(stderr, err_rc);
2375 cYAML_free_tree(err_rc);
2380 /* initialize lsa for plain file */
2381 setstripe_args_init(lsa);
2382 lsa->lsa_tgts = osts;
2384 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2386 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2387 progname, template);
2390 rc = build_component(layout, lsa, *layout != NULL);
2392 /* clean clean lsa */
2393 setstripe_args_init(lsa);
2397 cYAML_free_tree(tree);
2401 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2402 * end of the last component in the existing file, and adjust the
2403 * first extent start of the components to be added accordingly. */
2404 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2406 struct llapi_layout *head;
2407 uint64_t start, end, stripe_size, prev_end = 0;
2410 if (layout == NULL) {
2412 "%s setstripe: layout must be specified\n",
2418 head = llapi_layout_get_by_path(fname, 0);
2421 "%s setstripe: cannot read layout from '%s': %s\n",
2422 progname, fname, strerror(errno));
2424 } else if (errno == ENODATA) {
2425 /* file without LOVEA, this component-add will be turned
2426 * into a component-create. */
2427 llapi_layout_free(head);
2429 } else if (!llapi_layout_is_composite(head)) {
2430 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2432 llapi_layout_free(head);
2436 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2438 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2439 progname, strerror(errno));
2440 llapi_layout_free(head);
2444 llapi_layout_free(head);
2446 /* Make sure we use the first component of the layout to be added. */
2447 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2450 "%s setstripe: cannot move component cursor: %s\n",
2451 progname, strerror(errno));
2455 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2457 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2458 progname, strerror(errno));
2462 if (start > prev_end || end <= prev_end) {
2464 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2465 progname, start, end, prev_end);
2469 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2471 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2472 progname, strerror(errno));
2476 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2477 (prev_end & (stripe_size - 1))) {
2479 "%s setstripe: stripe size %lu not aligned with %lu\n",
2480 progname, stripe_size, prev_end);
2484 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2487 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2488 progname, prev_end, end, strerror(errno));
2495 static inline bool arg_is_eof(char *arg)
2497 return !strncmp(arg, "-1", strlen("-1")) ||
2498 !strncmp(arg, "EOF", strlen("EOF")) ||
2499 !strncmp(arg, "eof", strlen("eof"));
2503 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2505 * Return: Valid mirror_args pointer on success and
2506 * NULL if memory allocation fails.
2508 static struct mirror_args *lfs_mirror_alloc(void)
2510 struct mirror_args *mirror = NULL;
2513 mirror = calloc(1, sizeof(*mirror));
2524 * lfs_mirror_free() - Free memory allocated for a mirror argument
2526 * @mirror: Previously allocated mirror argument structure by
2527 * lfs_mirror_alloc().
2529 * Free memory allocated for @mirror.
2533 static void lfs_mirror_free(struct mirror_args *mirror)
2535 if (mirror->m_layout != NULL)
2536 llapi_layout_free(mirror->m_layout);
2541 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2542 * @mirror_list: Previously allocated mirror list.
2544 * Free memory allocated for @mirror_list.
2548 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2550 struct mirror_args *next_mirror = NULL;
2552 while (mirror_list != NULL) {
2553 next_mirror = mirror_list->m_next;
2554 lfs_mirror_free(mirror_list);
2555 mirror_list = next_mirror;
2567 LFS_COMP_NO_VERIFY_OPT,
2569 LFS_MIRROR_FLAGS_OPT,
2571 LFS_MIRROR_STATE_OPT,
2573 LFS_MIRROR_INDEX_OPT,
2577 static int lfs_setstripe_internal(int argc, char **argv,
2578 enum setstripe_origin opc)
2580 struct lfs_setstripe_args lsa = { 0 };
2581 struct llapi_stripe_param *param = NULL;
2582 struct find_param migrate_mdt_param = {
2592 unsigned long long size_units = 1;
2593 bool migrate_mode = false;
2594 bool migrate_mdt_mode = false;
2595 bool migration_block = false;
2596 __u64 migration_flags = 0;
2597 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2598 int comp_del = 0, comp_set = 0;
2601 struct llapi_layout *layout = NULL;
2602 struct llapi_layout **lpp = &layout;
2603 bool mirror_mode = false;
2604 bool has_m_file = false;
2605 __u32 mirror_count = 0;
2606 enum mirror_flags mirror_flags = 0;
2607 struct mirror_args *mirror_list = NULL;
2608 struct mirror_args *new_mirror = NULL;
2609 struct mirror_args *last_mirror = NULL;
2610 __u16 mirror_id = 0;
2612 bool from_yaml = false;
2613 bool from_copy = false;
2614 char *template = NULL;
2616 struct option long_opts[] = {
2617 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2618 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2619 /* --block is only valid in migrate mode */
2620 { .val = 'b', .name = "block", .has_arg = no_argument },
2621 { .val = LFS_COMP_ADD_OPT,
2622 .name = "comp-add", .has_arg = no_argument },
2623 { .val = LFS_COMP_ADD_OPT,
2624 .name = "component-add", .has_arg = no_argument },
2625 { .val = LFS_COMP_DEL_OPT,
2626 .name = "comp-del", .has_arg = no_argument },
2627 { .val = LFS_COMP_DEL_OPT,
2628 .name = "component-del", .has_arg = no_argument },
2629 { .val = LFS_COMP_FLAGS_OPT,
2630 .name = "comp-flags", .has_arg = required_argument },
2631 { .val = LFS_COMP_FLAGS_OPT,
2632 .name = "component-flags",
2633 .has_arg = required_argument },
2634 { .val = LFS_COMP_SET_OPT,
2635 .name = "comp-set", .has_arg = no_argument },
2636 { .val = LFS_COMP_SET_OPT,
2637 .name = "component-set",
2638 .has_arg = no_argument},
2639 { .val = LFS_COMP_NO_VERIFY_OPT,
2640 .name = "no-verify", .has_arg = no_argument},
2641 { .val = LFS_MIRROR_FLAGS_OPT,
2642 .name = "flags", .has_arg = required_argument},
2643 { .val = LFS_MIRROR_ID_OPT,
2644 .name = "mirror-id", .has_arg = required_argument},
2645 { .val = LFS_LAYOUT_COPY,
2646 .name = "copy", .has_arg = required_argument},
2647 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2648 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2649 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2650 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2651 { .val = 'd', .name = "delete", .has_arg = no_argument},
2652 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2653 /* --non-direct is only valid in migrate mode */
2654 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2655 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2656 { .val = 'E', .name = "component-end",
2657 .has_arg = required_argument},
2658 { .val = 'f', .name = "file", .has_arg = required_argument },
2659 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2660 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2661 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2662 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2663 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2664 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2665 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2666 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2667 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2668 { .val = 'L', .name = "layout", .has_arg = required_argument },
2669 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2670 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2671 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2672 /* --non-block is only valid in migrate mode */
2673 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2674 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2675 { .val = 'o', .name = "ost", .has_arg = required_argument },
2676 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2677 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2678 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2680 { .val = 'p', .name = "pool", .has_arg = required_argument },
2681 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2682 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2683 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2684 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2685 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2686 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2687 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2688 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2689 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2690 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2691 /* --verbose is only valid in migrate mode */
2692 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2693 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2696 setstripe_args_init(&lsa);
2698 migrate_mode = (opc == SO_MIGRATE);
2699 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2701 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2703 while ((c = getopt_long(argc, argv,
2704 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2710 case LFS_COMP_ADD_OPT:
2713 case LFS_COMP_DEL_OPT:
2716 case LFS_COMP_FLAGS_OPT:
2717 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2718 &lsa.lsa_comp_neg_flags);
2721 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2722 fprintf(stderr, "%s: inverted flags are not supported\n",
2726 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2728 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2735 case LFS_COMP_SET_OPT:
2738 case LFS_COMP_NO_VERIFY_OPT:
2739 mirror_flags |= MF_NO_VERIFY;
2741 case LFS_MIRROR_ID_OPT:
2742 mirror_id = strtoul(optarg, &end, 0);
2743 if (*end != '\0' || mirror_id == 0) {
2745 "%s %s: invalid mirror ID '%s'\n",
2746 progname, argv[0], optarg);
2750 case LFS_MIRROR_FLAGS_OPT: {
2753 if (!mirror_mode || !last_mirror) {
2754 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2759 result = comp_str2flags(optarg, &last_mirror->m_flags,
2765 fprintf(stderr, "%s: inverted flags are not supported\n",
2770 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2772 "%s: unsupported mirror flags: %s\n",
2779 case LFS_LAYOUT_COPY:
2784 if (!migrate_mode) {
2786 "%s %s: -b|--block valid only for migrate command\n",
2790 migration_block = true;
2793 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2796 "%s %s: invalid stripe count '%s'\n",
2797 progname, argv[0], optarg);
2801 if (lsa.lsa_stripe_count == -1)
2802 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2805 /* delete the default striping pattern */
2807 if (opc == SO_MIRROR_SPLIT) {
2810 "%s %s: -d cannot used with -f\n",
2814 mirror_flags |= MF_DESTROY;
2818 if (!migrate_mode) {
2820 "%s %s: -D|--non-direct is valid "
2821 "only for migrate command\n",
2825 migration_flags |= MIGRATION_NONDIRECT;
2828 if (lsa.lsa_comp_end != 0) {
2829 result = comp_args_to_layout(lpp, &lsa, true);
2832 "%s %s: invalid layout\n",
2837 setstripe_args_init_inherit(&lsa);
2840 if (arg_is_eof(optarg)) {
2841 lsa.lsa_comp_end = LUSTRE_EOF;
2843 result = llapi_parse_size(optarg,
2848 "%s %s: invalid component end '%s'\n",
2849 progname, argv[0], optarg);
2855 if (!migrate_mode) {
2856 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2860 lsa.lsa_pattern = check_hashtype(optarg);
2861 if (lsa.lsa_pattern == 0) {
2863 "%s %s: bad stripe hash type '%s'\n",
2864 progname, argv[0], optarg);
2869 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2872 "%s %s: invalid stripe offset '%s'\n",
2873 progname, argv[0], optarg);
2876 if (lsa.lsa_stripe_off == -1)
2877 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2880 comp_id = strtoul(optarg, &end, 0);
2881 if (*end != '\0' || comp_id == 0 ||
2882 comp_id > LCME_ID_MAX) {
2884 "%s %s: invalid component ID '%s'\n",
2885 progname, argv[0], optarg);
2890 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2892 "error: %s: invalid option: %s\n",
2893 progname, argv[optopt + 1]);
2896 if (opc == SO_MIRROR_EXTEND) {
2897 if (last_mirror == NULL) {
2899 "error: %s: '-N' must exist in front of '%s'\n",
2900 progname, argv[optopt + 1]);
2903 last_mirror->m_file = optarg;
2904 last_mirror->m_count = 1;
2907 if (mirror_list == NULL)
2908 mirror_list = lfs_mirror_alloc();
2909 mirror_list->m_file = optarg;
2914 if (strcmp(argv[optind - 1], "mdt") == 0) {
2915 /* Can be only the first component */
2916 if (layout != NULL) {
2918 fprintf(stderr, "error: 'mdt' layout "
2919 "can be only the first one\n");
2922 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2924 fprintf(stderr, "error: 'mdt' layout "
2925 "size is too big\n");
2928 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2929 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2931 fprintf(stderr, "error: layout '%s' is "
2932 "unknown, supported layouts are: "
2933 "'mdt', 'raid0'\n", argv[optind]);
2938 if (!migrate_mode) {
2940 "%s %s: -m|--mdt-index is valid only for migrate command\n",
2944 migrate_mdt_mode = true;
2945 lsa.lsa_nr_tgts = parse_targets(tgts,
2946 sizeof(tgts) / sizeof(__u32),
2947 lsa.lsa_nr_tgts, optarg);
2948 if (lsa.lsa_nr_tgts < 0) {
2950 "%s %s: invalid MDT target(s) '%s'\n",
2951 progname, argv[0], optarg);
2955 lsa.lsa_tgts = tgts;
2956 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2957 lsa.lsa_stripe_off = tgts[0];
2960 if (!migrate_mode) {
2962 "%s %s: -n|--non-block valid only for migrate command\n",
2966 migration_flags |= MIGRATION_NONBLOCK;
2969 if (opc == SO_SETSTRIPE) {
2970 opc = SO_MIRROR_CREATE;
2974 if (optarg != NULL) {
2975 mirror_count = strtoul(optarg, &end, 0);
2976 if (*end != '\0' || mirror_count == 0) {
2978 "error: %s: bad mirror count: %s\n",
2985 new_mirror = lfs_mirror_alloc();
2986 new_mirror->m_count = mirror_count;
2988 if (mirror_list == NULL)
2989 mirror_list = new_mirror;
2991 if (last_mirror != NULL) {
2992 /* wrap up last mirror */
2993 if (lsa.lsa_comp_end == 0)
2994 lsa.lsa_comp_end = LUSTRE_EOF;
2996 result = comp_args_to_layout(lpp, &lsa, true);
2998 lfs_mirror_free(new_mirror);
3002 setstripe_args_init_inherit(&lsa);
3004 last_mirror->m_next = new_mirror;
3007 last_mirror = new_mirror;
3008 lpp = &last_mirror->m_layout;
3011 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3012 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3013 fprintf(stderr, "warning: '--ost-list' is "
3014 "deprecated, use '--ost' instead\n");
3016 lsa.lsa_nr_tgts = parse_targets(tgts,
3017 sizeof(tgts) / sizeof(__u32),
3018 lsa.lsa_nr_tgts, optarg);
3019 if (lsa.lsa_nr_tgts < 0) {
3021 "%s %s: invalid OST target(s) '%s'\n",
3022 progname, argv[0], optarg);
3026 lsa.lsa_tgts = tgts;
3027 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3028 lsa.lsa_stripe_off = tgts[0];
3033 lsa.lsa_pool_name = optarg;
3035 if (strlen(lsa.lsa_pool_name) == 0 ||
3036 strncmp(lsa.lsa_pool_name, "none",
3037 LOV_MAXPOOLNAME) == 0)
3038 lsa.lsa_pool_name = NULL;
3041 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3045 "%s %s: invalid stripe size '%s'\n",
3046 progname, argv[0], optarg);
3051 if (!migrate_mode) {
3053 "%s %s: -v|--verbose valid only for migrate command\n",
3057 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3058 migration_flags = MIGRATION_VERBOSE;
3065 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3066 progname, argv[0], argv[optind - 1]);
3071 fname = argv[optind];
3073 if (optind == argc) {
3074 fprintf(stderr, "%s %s: FILE must be specified\n",
3079 if (mirror_mode && mirror_count == 0) {
3081 "error: %s: --mirror-count|-N option is required\n",
3088 if (lsa.lsa_comp_end == 0)
3089 lsa.lsa_comp_end = LUSTRE_EOF;
3092 if (lsa.lsa_comp_end != 0) {
3093 result = comp_args_to_layout(lpp, &lsa, true);
3098 if (mirror_flags & MF_NO_VERIFY) {
3099 if (opc != SO_MIRROR_EXTEND) {
3101 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3105 } else if (!has_m_file) {
3107 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3114 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3115 * altered by user space tool, so we don't need to support the
3116 * --component-set for this moment. */
3117 if (comp_set && !comp_id) {
3118 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3123 if ((delete + comp_set + comp_del + comp_add) > 1) {
3125 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3130 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3131 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3133 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3138 if ((comp_set || comp_del) &&
3139 (setstripe_args_specified(&lsa) || layout != NULL)) {
3141 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3146 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3148 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3153 if (comp_add || comp_del) {
3156 result = lstat(fname, &st);
3157 if (result == 0 && S_ISDIR(st.st_mode)) {
3159 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3165 fprintf(stderr, "error: %s: can't use --component-add "
3166 "or --component-del for mirror operation\n",
3173 if (layout == NULL) {
3175 "%s %s: option -E must be specified with --component-add\n",
3180 result = adjust_first_extent(fname, layout);
3181 if (result == -ENODATA)
3183 else if (result != 0)
3187 if (from_yaml && from_copy) {
3189 "%s: can't specify --yaml and --copy together\n",
3194 if ((from_yaml || from_copy) &&
3195 (setstripe_args_specified(&lsa) || layout != NULL)) {
3196 fprintf(stderr, "error: %s: can't specify --yaml with "
3197 "-c, -S, -i, -o, -p or -E options.\n",
3202 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3204 "%s %s: options --non-block and --block are mutually exclusive\n",
3209 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3212 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3217 if (migrate_mdt_mode) {
3218 struct lmv_user_md *lmu;
3220 /* initialize migrate mdt parameters */
3221 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3222 LMV_USER_MAGIC_SPECIFIC));
3225 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3226 progname, argv[0], strerror(ENOMEM));
3230 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3231 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3232 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3234 "%s %s: migrate should specify MDT index\n",
3239 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3240 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3241 lmu->lum_hash_type = lsa.lsa_pattern;
3243 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3244 if (lsa.lsa_pool_name)
3245 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3246 sizeof(lmu->lum_pool_name));
3247 if (lsa.lsa_nr_tgts > 1) {
3250 if (lsa.lsa_stripe_count > 0 &&
3251 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3252 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3254 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3255 progname, lsa.lsa_stripe_count,
3261 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3262 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3263 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3264 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3266 lmu->lum_magic = LMV_USER_MAGIC;
3269 migrate_mdt_param.fp_lmv_md = lmu;
3270 migrate_mdt_param.fp_migrate = 1;
3271 } else if (layout == NULL) {
3272 /* initialize stripe parameters */
3273 param = calloc(1, offsetof(typeof(*param),
3274 lsp_osts[lsa.lsa_nr_tgts]));
3275 if (param == NULL) {
3277 "%s %s: cannot allocate memory for parameters: %s\n",
3278 progname, argv[0], strerror(ENOMEM));
3283 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3284 param->lsp_stripe_size = lsa.lsa_stripe_size;
3285 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3286 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3287 param->lsp_stripe_count = -1;
3289 param->lsp_stripe_count = lsa.lsa_stripe_count;
3291 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3292 param->lsp_stripe_offset = -1;
3294 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3295 param->lsp_pool = lsa.lsa_pool_name;
3296 param->lsp_is_specific = false;
3297 if (lsa.lsa_nr_tgts > 0) {
3298 if (lsa.lsa_stripe_count > 0 &&
3299 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3300 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3301 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3302 fprintf(stderr, "error: %s: stripe count %lld "
3303 "doesn't match the number of OSTs: %d\n"
3304 , argv[0], lsa.lsa_stripe_count,
3310 param->lsp_is_specific = true;
3311 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3312 memcpy(param->lsp_osts, tgts,
3313 sizeof(*tgts) * lsa.lsa_nr_tgts);
3318 /* generate a layout from a YAML template */
3319 result = lfs_comp_create_from_yaml(template, &layout,
3322 fprintf(stderr, "error: %s: can't create composite "
3323 "layout from template file %s\n",
3327 } else if (from_copy) {
3328 layout = llapi_layout_get_by_path(template, 0);
3329 if (layout == NULL) {
3331 "%s: can't create composite layout from file %s.\n",
3332 progname, template);
3337 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3338 if (migrate_mdt_mode) {
3339 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3340 } else if (migrate_mode) {
3341 result = lfs_migrate(fname, migration_flags, param,
3343 } else if (comp_set != 0) {
3344 result = lfs_component_set(fname, comp_id,
3346 lsa.lsa_comp_neg_flags);
3347 } else if (comp_del != 0) {
3348 result = lfs_component_del(fname, comp_id,
3350 lsa.lsa_comp_neg_flags);
3351 } else if (comp_add != 0) {
3352 result = lfs_component_add(fname, layout);
3353 } else if (opc == SO_MIRROR_CREATE) {
3354 result = mirror_create(fname, mirror_list);
3355 } else if (opc == SO_MIRROR_EXTEND) {
3356 result = mirror_extend(fname, mirror_list,
3358 } else if (opc == SO_MIRROR_SPLIT) {
3359 if (mirror_id == 0 && comp_id == 0) {
3361 "%s %s: no mirror id or component id is specified\n",
3366 comp_id = mirror_id;
3368 mirror_flags |= MF_COMP_ID;
3369 result = mirror_split(fname, comp_id, mirror_flags,
3370 has_m_file ? mirror_list->m_file :
3372 } else if (layout != NULL) {
3373 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3380 result = llapi_file_open_param(fname,
3389 /* Save the first error encountered. */
3397 free(migrate_mdt_param.fp_lmv_md);
3398 llapi_layout_free(layout);
3399 lfs_mirror_list_free(mirror_list);
3404 llapi_layout_free(layout);
3405 lfs_mirror_list_free(mirror_list);
3409 static int lfs_poollist(int argc, char **argv)
3414 return llapi_poollist(argv[1]);
3417 static int set_time(time_t *time, time_t *set, char *str)
3425 else if (str[0] == '-')
3431 t = strtol(str, &endptr, 0);
3432 if (*endptr != '\0') {
3434 "%s find: bad time '%s': %s\n",
3435 progname, str, strerror(EINVAL));
3438 if (*time < t * 24 * 60 * 60) {
3442 "%s find: bad time '%s': too large\n",
3447 *set = *time - t * 24 * 60 * 60;
3451 static int name2uid(unsigned int *id, const char *name)
3453 struct passwd *passwd;
3455 passwd = getpwnam(name);
3458 *id = passwd->pw_uid;
3463 static int name2gid(unsigned int *id, const char *name)
3465 struct group *group;
3467 group = getgrnam(name);
3470 *id = group->gr_gid;
3475 static inline int name2projid(unsigned int *id, const char *name)
3480 static int uid2name(char **name, unsigned int id)
3482 struct passwd *passwd;
3484 passwd = getpwuid(id);
3487 *name = passwd->pw_name;
3492 static inline int gid2name(char **name, unsigned int id)
3494 struct group *group;
3496 group = getgrgid(id);
3499 *name = group->gr_name;
3504 static int name2layout(__u32 *layout, char *name)
3506 char *ptr, *layout_name;
3509 for (ptr = name; ; ptr = NULL) {
3510 layout_name = strtok(ptr, ",");
3511 if (layout_name == NULL)
3513 if (strcmp(layout_name, "released") == 0)
3514 *layout |= LOV_PATTERN_F_RELEASED;
3515 else if (strcmp(layout_name, "raid0") == 0)
3516 *layout |= LOV_PATTERN_RAID0;
3517 else if (strcmp(layout_name, "mdt") == 0)
3518 *layout |= LOV_PATTERN_MDT;
3525 static int lfs_find(int argc, char **argv)
3530 struct find_param param = {
3534 struct option long_opts[] = {
3535 { .val = 'A', .name = "atime", .has_arg = required_argument },
3536 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3537 { .val = LFS_COMP_COUNT_OPT,
3538 .name = "comp-count", .has_arg = required_argument },
3539 { .val = LFS_COMP_COUNT_OPT,
3540 .name = "component-count",
3541 .has_arg = required_argument },
3542 { .val = LFS_COMP_FLAGS_OPT,
3543 .name = "comp-flags", .has_arg = required_argument },
3544 { .val = LFS_COMP_FLAGS_OPT,
3545 .name = "component-flags",
3546 .has_arg = required_argument },
3547 { .val = LFS_COMP_START_OPT,
3548 .name = "comp-start", .has_arg = required_argument },
3549 { .val = LFS_COMP_START_OPT,
3550 .name = "component-start",
3551 .has_arg = required_argument },
3552 { .val = LFS_MIRROR_STATE_OPT,
3553 .name = "mirror-state", .has_arg = required_argument },
3554 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3555 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3556 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3557 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3558 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3559 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3560 { .val = 'E', .name = "component-end",
3561 .has_arg = required_argument },
3562 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3563 { .val = 'g', .name = "gid", .has_arg = required_argument },
3564 { .val = 'G', .name = "group", .has_arg = required_argument },
3565 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3566 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3567 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3568 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3569 { .val = 'L', .name = "layout", .has_arg = required_argument },
3570 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3571 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3572 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3573 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3574 { .val = 'n', .name = "name", .has_arg = required_argument },
3575 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3576 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3577 { .val = 'O', .name = "obd", .has_arg = required_argument },
3578 { .val = 'O', .name = "ost", .has_arg = required_argument },
3579 /* no short option for pool yet, can be 'p' after 2.18 */
3580 { .val = LFS_POOL_OPT,
3581 .name = "pool", .has_arg = required_argument },
3582 { .val = '0', .name = "print0", .has_arg = no_argument },
3583 { .val = 'P', .name = "print", .has_arg = no_argument },
3584 { .val = LFS_PROJID_OPT,
3585 .name = "projid", .has_arg = required_argument },
3586 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3587 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3588 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3589 { .val = 's', .name = "size", .has_arg = required_argument },
3590 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3591 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3592 { .val = 't', .name = "type", .has_arg = required_argument },
3593 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3594 { .val = 'u', .name = "uid", .has_arg = required_argument },
3595 { .val = 'U', .name = "user", .has_arg = required_argument },
3596 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3597 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3609 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3610 while ((c = getopt_long_only(argc, argv,
3611 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3612 long_opts, NULL)) >= 0) {
3617 /* '!' is part of option */
3618 /* when getopt_long_only() finds a string which is not
3619 * an option nor a known option argument it returns 1
3620 * in that case if we already have found pathstart and pathend
3621 * (i.e. we have the list of pathnames),
3622 * the only supported value is "!"
3624 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3625 if (!isoption && pathend != -1) {
3626 fprintf(stderr, "err: %s: filename|dirname must either "
3627 "precede options or follow options\n",
3632 if (!isoption && pathstart == -1)
3633 pathstart = optind - 1;
3634 if (isoption && pathstart != -1 && pathend == -1)
3635 pathend = optind - 2;
3641 /* unknown; opt is "!" or path component,
3642 * checking done above.
3644 if (strcmp(optarg, "!") == 0)
3648 xtime = ¶m.fp_atime;
3649 xsign = ¶m.fp_asign;
3650 param.fp_exclude_atime = !!neg_opt;
3651 /* no break, this falls through to 'C' for ctime */
3654 xtime = ¶m.fp_ctime;
3655 xsign = ¶m.fp_csign;
3656 param.fp_exclude_ctime = !!neg_opt;
3658 /* no break, this falls through to 'M' for mtime */
3661 xtime = ¶m.fp_mtime;
3662 xsign = ¶m.fp_msign;
3663 param.fp_exclude_mtime = !!neg_opt;
3665 rc = set_time(&t, xtime, optarg);
3666 if (rc == INT_MAX) {
3674 if (optarg[0] == '+') {
3675 param.fp_blocks_sign = -1;
3677 } else if (optarg[0] == '-') {
3678 param.fp_blocks_sign = 1;
3682 param.fp_blocks_units = 1024;
3683 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3684 ¶m.fp_blocks_units, 0);
3686 fprintf(stderr, "error: bad blocks '%s'\n",
3690 param.fp_check_blocks = 1;
3691 param.fp_exclude_blocks = !!neg_opt;
3693 case LFS_COMP_COUNT_OPT:
3694 if (optarg[0] == '+') {
3695 param.fp_comp_count_sign = -1;
3697 } else if (optarg[0] == '-') {
3698 param.fp_comp_count_sign = 1;
3702 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3703 if (*endptr != '\0') {
3704 fprintf(stderr, "error: bad component count "
3708 param.fp_check_comp_count = 1;
3709 param.fp_exclude_comp_count = !!neg_opt;
3711 case LFS_COMP_FLAGS_OPT:
3712 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3713 ¶m.fp_comp_neg_flags);
3715 fprintf(stderr, "error: bad component flags "
3719 param.fp_check_comp_flags = 1;
3721 __u32 flags = param.fp_comp_neg_flags;
3722 param.fp_comp_neg_flags = param.fp_comp_flags;
3723 param.fp_comp_flags = flags;
3726 case LFS_COMP_START_OPT:
3727 if (optarg[0] == '+') {
3728 param.fp_comp_start_sign = -1;
3730 } else if (optarg[0] == '-') {
3731 param.fp_comp_start_sign = 1;
3735 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3736 ¶m.fp_comp_start_units, 0);
3738 fprintf(stderr, "error: bad component start "
3742 param.fp_check_comp_start = 1;
3743 param.fp_exclude_comp_start = !!neg_opt;
3745 case LFS_MIRROR_STATE_OPT:
3746 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3747 ¶m.fp_mirror_neg_state);
3750 "error: bad mirrored file state '%s'\n",
3754 param.fp_check_mirror_state = 1;
3756 __u16 state = param.fp_mirror_neg_state;
3757 param.fp_mirror_neg_state =
3758 param.fp_mirror_state;
3759 param.fp_mirror_state = state;
3763 if (optarg[0] == '+') {
3764 param.fp_stripe_count_sign = -1;
3766 } else if (optarg[0] == '-') {
3767 param.fp_stripe_count_sign = 1;
3771 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3772 if (*endptr != '\0') {
3773 fprintf(stderr,"error: bad stripe_count '%s'\n",
3778 param.fp_check_stripe_count = 1;
3779 param.fp_exclude_stripe_count = !!neg_opt;
3782 param.fp_max_depth = strtol(optarg, 0, 0);
3785 if (optarg[0] == '+') {
3786 param.fp_comp_end_sign = -1;
3788 } else if (optarg[0] == '-') {
3789 param.fp_comp_end_sign = 1;
3793 if (arg_is_eof(optarg)) {
3794 param.fp_comp_end = LUSTRE_EOF;
3795 param.fp_comp_end_units = 1;
3798 rc = llapi_parse_size(optarg,
3800 ¶m.fp_comp_end_units, 0);
3803 fprintf(stderr, "error: bad component end "
3807 param.fp_check_comp_end = 1;
3808 param.fp_exclude_comp_end = !!neg_opt;
3812 rc = name2gid(¶m.fp_gid, optarg);
3814 param.fp_gid = strtoul(optarg, &endptr, 10);
3815 if (*endptr != '\0') {
3816 fprintf(stderr, "Group/GID: %s cannot "
3817 "be found.\n", optarg);
3822 param.fp_exclude_gid = !!neg_opt;
3823 param.fp_check_gid = 1;
3826 param.fp_hash_type = check_hashtype(optarg);
3827 if (param.fp_hash_type == 0) {
3828 fprintf(stderr, "error: bad hash_type '%s'\n",
3833 param.fp_check_hash_type = 1;
3834 param.fp_exclude_hash_type = !!neg_opt;
3837 ret = name2layout(¶m.fp_layout, optarg);
3840 param.fp_exclude_layout = !!neg_opt;
3841 param.fp_check_layout = 1;
3845 rc = name2uid(¶m.fp_uid, optarg);
3847 param.fp_uid = strtoul(optarg, &endptr, 10);
3848 if (*endptr != '\0') {
3849 fprintf(stderr, "User/UID: %s cannot "
3850 "be found.\n", optarg);
3855 param.fp_exclude_uid = !!neg_opt;
3856 param.fp_check_uid = 1;
3859 param.fp_pattern = (char *)optarg;
3860 param.fp_exclude_pattern = !!neg_opt;
3863 if (optarg[0] == '+') {
3864 param.fp_mirror_count_sign = -1;
3866 } else if (optarg[0] == '-') {
3867 param.fp_mirror_count_sign = 1;
3871 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3872 if (*endptr != '\0') {
3874 "error: bad mirror count '%s'\n",
3878 param.fp_check_mirror_count = 1;
3879 param.fp_exclude_mirror_count = !!neg_opt;
3884 char *buf, *token, *next, *p;
3888 buf = strdup(optarg);
3894 param.fp_exclude_obd = !!neg_opt;
3897 while (token && *token) {
3898 token = strchr(token, ',');
3905 param.fp_exclude_mdt = !!neg_opt;
3906 param.fp_num_alloc_mdts += len;
3907 tmp = realloc(param.fp_mdt_uuid,
3908 param.fp_num_alloc_mdts *
3909 sizeof(*param.fp_mdt_uuid));
3915 param.fp_mdt_uuid = tmp;
3917 param.fp_exclude_obd = !!neg_opt;
3918 param.fp_num_alloc_obds += len;
3919 tmp = realloc(param.fp_obd_uuid,
3920 param.fp_num_alloc_obds *
3921 sizeof(*param.fp_obd_uuid));
3927 param.fp_obd_uuid = tmp;
3929 for (token = buf; token && *token; token = next) {
3930 struct obd_uuid *puuid;
3933 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3936 ¶m.fp_obd_uuid[param.fp_num_obds++];
3938 p = strchr(token, ',');
3945 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3950 strncpy(puuid->uuid, token,
3951 sizeof(puuid->uuid));
3958 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3962 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3964 "Pool name %s is too long (max %d)\n",
3965 optarg, LOV_MAXPOOLNAME);
3970 * We do check for empty pool because empty pool
3971 * is used to find V1 LOV attributes
3973 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3974 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3975 param.fp_exclude_pool = !!neg_opt;
3976 param.fp_check_pool = 1;
3978 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3979 case 'p': /* want this for --pool, to match getstripe/find */
3981 "warning: -p deprecated, use --print0 or -0\n");
3984 param.fp_zero_end = 1;
3986 case 'P': /* we always print, this option is a no-op */
3988 case LFS_PROJID_OPT:
3989 rc = name2projid(¶m.fp_projid, optarg);
3991 param.fp_projid = strtoul(optarg, &endptr, 10);
3992 if (*endptr != '\0') {
3994 "Invalid project ID: %s",
4000 param.fp_exclude_projid = !!neg_opt;
4001 param.fp_check_projid = 1;
4004 if (optarg[0] == '+') {
4005 param.fp_size_sign = -1;
4007 } else if (optarg[0] == '-') {
4008 param.fp_size_sign = 1;
4012 ret = llapi_parse_size(optarg, ¶m.fp_size,
4013 ¶m.fp_size_units, 0);
4015 fprintf(stderr, "error: bad file size '%s'\n",
4019 param.fp_check_size = 1;
4020 param.fp_exclude_size = !!neg_opt;
4023 if (optarg[0] == '+') {
4024 param.fp_stripe_size_sign = -1;
4026 } else if (optarg[0] == '-') {
4027 param.fp_stripe_size_sign = 1;
4031 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4032 ¶m.fp_stripe_size_units, 0);
4034 fprintf(stderr, "error: bad stripe_size '%s'\n",
4038 param.fp_check_stripe_size = 1;
4039 param.fp_exclude_stripe_size = !!neg_opt;
4042 param.fp_exclude_type = !!neg_opt;
4043 switch (optarg[0]) {
4045 param.fp_type = S_IFBLK;
4048 param.fp_type = S_IFCHR;
4051 param.fp_type = S_IFDIR;
4054 param.fp_type = S_IFREG;
4057 param.fp_type = S_IFLNK;
4060 param.fp_type = S_IFIFO;
4063 param.fp_type = S_IFSOCK;
4066 fprintf(stderr, "error: %s: bad type '%s'\n",
4073 if (optarg[0] == '+') {
4074 param.fp_mdt_count_sign = -1;
4076 } else if (optarg[0] == '-') {
4077 param.fp_mdt_count_sign = 1;
4081 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4082 if (*endptr != '\0') {
4083 fprintf(stderr, "error: bad mdt_count '%s'\n",
4088 param.fp_check_mdt_count = 1;
4089 param.fp_exclude_mdt_count = !!neg_opt;
4097 if (pathstart == -1) {
4098 fprintf(stderr, "error: %s: no filename|pathname\n",
4102 } else if (pathend == -1) {
4108 rc = llapi_find(argv[pathstart], ¶m);
4109 if (rc != 0 && ret == 0)
4111 } while (++pathstart < pathend);
4114 fprintf(stderr, "error: %s failed for %s.\n",
4115 argv[0], argv[optind - 1]);
4117 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4118 free(param.fp_obd_uuid);
4120 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4121 free(param.fp_mdt_uuid);
4126 static int lfs_getstripe_internal(int argc, char **argv,
4127 struct find_param *param)
4129 struct option long_opts[] = {
4130 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4131 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4132 { .val = LFS_COMP_COUNT_OPT,
4133 .name = "comp-count", .has_arg = no_argument },
4134 { .val = LFS_COMP_COUNT_OPT,
4135 .name = "component-count", .has_arg = no_argument },
4136 { .val = LFS_COMP_FLAGS_OPT,
4137 .name = "comp-flags", .has_arg = optional_argument },
4138 { .val = LFS_COMP_FLAGS_OPT,
4139 .name = "component-flags", .has_arg = optional_argument },
4140 { .val = LFS_COMP_START_OPT,
4141 .name = "comp-start", .has_arg = optional_argument },
4142 { .val = LFS_COMP_START_OPT,
4143 .name = "component-start", .has_arg = optional_argument },
4144 { .val = LFS_MIRROR_INDEX_OPT,
4145 .name = "mirror-index", .has_arg = required_argument },
4146 { .val = LFS_MIRROR_ID_OPT,
4147 .name = "mirror-id", .has_arg = required_argument },
4148 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4149 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4150 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4151 { .val = 'd', .name = "directory", .has_arg = no_argument },
4152 { .val = 'D', .name = "default", .has_arg = no_argument },
4153 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4154 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4155 { .val = 'F', .name = "fid", .has_arg = no_argument },
4156 { .val = 'g', .name = "generation", .has_arg = no_argument },
4157 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4158 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4159 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4160 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4161 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4162 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4163 { .val = 'L', .name = "layout", .has_arg = no_argument },
4164 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4165 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4166 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4167 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4168 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4169 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4170 { .val = 'O', .name = "obd", .has_arg = required_argument },
4171 { .val = 'O', .name = "ost", .has_arg = required_argument },
4172 { .val = 'p', .name = "pool", .has_arg = no_argument },
4173 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4174 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4175 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4176 { .val = 'R', .name = "raw", .has_arg = no_argument },
4177 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4178 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4179 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4180 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4181 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4182 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4183 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4184 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4188 int pathstart = -1, pathend = -1;
4192 while ((c = getopt_long(argc, argv,
4193 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4194 long_opts, NULL)) != -1) {
4198 /* '!' is part of option */
4199 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4200 if (!isoption && pathend != -1) {
4202 "error: %s: filename|dirname must either precede options or follow options\n",
4206 if (!isoption && pathstart == -1)
4207 pathstart = optind - 1;
4208 if (isoption && pathstart != -1 && pathend == -1)
4209 pathend = optind - 2;
4213 /* unknown: opt is "!" */
4214 if (strcmp(optarg, "!") == 0)
4218 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4219 param->fp_verbose |= VERBOSE_COUNT;
4220 param->fp_max_depth = 0;
4223 case LFS_COMP_COUNT_OPT:
4224 param->fp_verbose |= VERBOSE_COMP_COUNT;
4225 param->fp_max_depth = 0;
4227 case LFS_COMP_FLAGS_OPT:
4228 if (optarg != NULL) {
4229 rc = comp_str2flags(optarg,
4230 ¶m->fp_comp_flags,
4231 ¶m->fp_comp_neg_flags);
4233 fprintf(stderr, "error: %s bad "
4234 "component flags '%s'.\n",
4238 param->fp_check_comp_flags = 1;
4240 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4241 param->fp_max_depth = 0;
4244 case LFS_COMP_START_OPT:
4245 if (optarg != NULL) {
4247 if (tmp[0] == '+') {
4248 param->fp_comp_start_sign = -1;
4250 } else if (tmp[0] == '-') {
4251 param->fp_comp_start_sign = 1;
4254 rc = llapi_parse_size(tmp,
4255 ¶m->fp_comp_start,
4256 ¶m->fp_comp_start_units, 0);
4258 fprintf(stderr, "error: %s bad "
4259 "component start '%s'.\n",
4263 param->fp_check_comp_start = 1;
4266 param->fp_verbose |= VERBOSE_COMP_START;
4267 param->fp_max_depth = 0;
4270 case LFS_MIRROR_INDEX_OPT:
4271 if (optarg[0] == '+') {
4272 param->fp_mirror_index_sign = -1;
4274 } else if (optarg[0] == '-') {
4275 param->fp_mirror_index_sign = 1;
4279 param->fp_mirror_index = strtoul(optarg, &end, 0);
4280 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4281 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4283 "%s %s: invalid mirror index '%s'\n",
4284 progname, argv[0], optarg);
4287 if (param->fp_mirror_id != 0) {
4289 "%s %s: can't specify both mirror index and mirror ID\n",
4293 param->fp_check_mirror_index = 1;
4294 param->fp_exclude_mirror_index = !!neg_opt;
4296 case LFS_MIRROR_ID_OPT:
4297 if (optarg[0] == '+') {
4298 param->fp_mirror_id_sign = -1;
4300 } else if (optarg[0] == '-') {
4301 param->fp_mirror_id_sign = 1;
4305 param->fp_mirror_id = strtoul(optarg, &end, 0);
4306 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4307 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4309 "%s %s: invalid mirror ID '%s'\n",
4310 progname, argv[0], optarg);
4313 if (param->fp_mirror_index != 0) {
4315 "%s %s: can't specify both mirror index and mirror ID\n",
4319 param->fp_check_mirror_id = 1;
4320 param->fp_exclude_mirror_id = !!neg_opt;
4323 param->fp_max_depth = 0;
4326 param->fp_get_default_lmv = 1;
4329 if (optarg != NULL) {
4331 if (tmp[0] == '+') {
4332 param->fp_comp_end_sign = -1;
4334 } else if (tmp[0] == '-') {
4335 param->fp_comp_end_sign = 1;
4339 if (arg_is_eof(tmp)) {
4340 param->fp_comp_end = LUSTRE_EOF;
4341 param->fp_comp_end_units = 1;
4344 rc = llapi_parse_size(tmp,
4345 ¶m->fp_comp_end,
4346 ¶m->fp_comp_end_units, 0);
4349 fprintf(stderr, "error: %s bad "
4350 "component end '%s'.\n",
4354 param->fp_check_comp_end = 1;
4356 param->fp_verbose |= VERBOSE_COMP_END;
4357 param->fp_max_depth = 0;
4361 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4362 param->fp_verbose |= VERBOSE_DFID;
4363 param->fp_max_depth = 0;
4367 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4368 param->fp_verbose |= VERBOSE_GENERATION;
4369 param->fp_max_depth = 0;
4373 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4374 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4375 param->fp_max_depth = 0;
4379 if (optarg != NULL) {
4380 param->fp_comp_id = strtoul(optarg, &end, 0);
4381 if (*end != '\0' || param->fp_comp_id == 0 ||
4382 param->fp_comp_id > LCME_ID_MAX) {
4383 fprintf(stderr, "error: %s bad "
4384 "component id '%s'\n",
4388 param->fp_check_comp_id = 1;
4391 param->fp_max_depth = 0;
4392 param->fp_verbose |= VERBOSE_COMP_ID;
4396 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4397 param->fp_verbose |= VERBOSE_PATTERN;
4398 param->fp_max_depth = 0;
4401 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4403 fprintf(stderr, "warning: '-M' deprecated"
4404 ", use '--mdt-index' or '-m' instead\n");
4407 if (!(param->fp_verbose & VERBOSE_DETAIL))
4408 param->fp_max_depth = 0;
4409 param->fp_verbose |= VERBOSE_MDTINDEX;
4412 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4413 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4414 param->fp_max_depth = 0;
4418 if (param->fp_obd_uuid) {
4420 "error: %s: only one obduuid allowed",
4424 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4427 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4428 param->fp_verbose |= VERBOSE_POOL;
4429 param->fp_max_depth = 0;
4436 param->fp_recursive = 1;
4442 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4443 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4444 param->fp_max_depth = 0;
4448 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4458 if (pathstart == -1) {
4459 fprintf(stderr, "error: %s: no filename|pathname\n",
4462 } else if (pathend == -1) {
4470 if (param->fp_recursive)
4471 param->fp_max_depth = -1;
4472 else if (param->fp_verbose & VERBOSE_DETAIL)
4473 param->fp_max_depth = 1;
4475 if (!param->fp_verbose)
4476 param->fp_verbose = VERBOSE_DEFAULT;
4477 if (param->fp_quiet)
4478 param->fp_verbose = VERBOSE_OBJID;
4481 rc = llapi_getstripe(argv[pathstart], param);
4482 } while (++pathstart < pathend && !rc);
4485 fprintf(stderr, "error: %s failed for %s.\n",
4486 argv[0], argv[optind - 1]);
4490 static int lfs_tgts(int argc, char **argv)
4492 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4493 struct find_param param;
4494 int index = 0, rc=0;
4499 if (argc == 2 && !realpath(argv[1], path)) {
4501 fprintf(stderr, "error: invalid path '%s': %s\n",
4502 argv[1], strerror(-rc));
4506 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4507 /* Check if we have a mount point */
4508 if (mntdir[0] == '\0')
4511 memset(¶m, 0, sizeof(param));
4512 if (!strcmp(argv[0], "mdts"))
4513 param.fp_get_lmv = 1;
4515 rc = llapi_ostlist(mntdir, ¶m);
4517 fprintf(stderr, "error: %s: failed on %s\n",
4520 if (path[0] != '\0')
4522 memset(mntdir, 0, PATH_MAX);
4528 static int lfs_getstripe(int argc, char **argv)
4530 struct find_param param = { 0 };
4532 param.fp_max_depth = 1;
4533 return lfs_getstripe_internal(argc, argv, ¶m);
4537 static int lfs_getdirstripe(int argc, char **argv)
4539 struct find_param param = { 0 };
4540 struct option long_opts[] = {
4541 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4542 { .val = 'D', .name = "default", .has_arg = no_argument },
4543 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4544 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4545 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4546 { .val = 'O', .name = "obd", .has_arg = required_argument },
4547 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4548 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4549 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4553 param.fp_get_lmv = 1;
4555 while ((c = getopt_long(argc, argv,
4556 "cDHimO:rtTy", long_opts, NULL)) != -1)
4561 param.fp_verbose |= VERBOSE_COUNT;
4564 param.fp_get_default_lmv = 1;
4568 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4570 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4572 fprintf(stderr, "warning: '-t' deprecated, "
4573 "use '--mdt-hash' or '-H' instead\n");
4576 param.fp_verbose |= VERBOSE_HASH_TYPE;
4579 if (param.fp_obd_uuid) {
4581 "error: %s: only one obduuid allowed",
4585 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4588 param.fp_recursive = 1;
4601 if (param.fp_recursive)
4602 param.fp_max_depth = -1;
4604 if (!param.fp_verbose)
4605 param.fp_verbose = VERBOSE_DEFAULT;
4608 rc = llapi_getstripe(argv[optind], ¶m);
4609 } while (++optind < argc && !rc);
4612 fprintf(stderr, "error: %s failed for %s.\n",
4613 argv[0], argv[optind - 1]);
4618 MNTDF_INODES = 0x0001,
4619 MNTDF_COOKED = 0x0002,
4620 MNTDF_LAZY = 0x0004,
4621 MNTDF_VERBOSE = 0x0008,
4622 MNTDF_SHOW = 0x0010,
4625 #define COOK(value) \
4628 while (value > 1024) { \
4636 #define CDF "%11llu"
4637 #define HDF "%8.1f%c"
4641 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4643 double avail, used, ratio = 0;
4646 avail = st->os_ffree;
4647 used = st->os_files - st->os_ffree;
4649 avail = st->os_bavail;
4650 used = st->os_blocks - st->os_bfree;
4652 if (avail + used > 0)
4653 ratio = used / (used + avail) * 100;
4655 /* Round up to match df(1) usage percentage */
4656 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4659 static int showdf(char *mntdir, struct obd_statfs *stat,
4660 char *uuid, enum mntdf_flags flags,
4661 char *type, int index, int rc)
4663 long long avail, used, total;
4665 char *suffix = "KMGTPEZY";
4666 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4667 char tbuf[3 * sizeof(__u64)];
4668 char ubuf[3 * sizeof(__u64)];
4669 char abuf[3 * sizeof(__u64)];
4670 char rbuf[3 * sizeof(__u64)];
4677 if (flags & MNTDF_INODES) {
4678 avail = stat->os_ffree;
4679 used = stat->os_files - stat->os_ffree;
4680 total = stat->os_files;
4682 int shift = flags & MNTDF_COOKED ? 0 : 10;
4684 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4685 used = ((stat->os_blocks - stat->os_bfree) *
4686 stat->os_bsize) >> shift;
4687 total = (stat->os_blocks * stat->os_bsize) >> shift;
4690 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4692 if (flags & MNTDF_COOKED) {
4696 cook_val = (double)total;
4699 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4702 snprintf(tbuf, sizeof(tbuf), CDF, total);
4704 cook_val = (double)used;
4707 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4710 snprintf(ubuf, sizeof(ubuf), CDF, used);
4712 cook_val = (double)avail;
4715 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4718 snprintf(abuf, sizeof(abuf), CDF, avail);
4720 snprintf(tbuf, sizeof(tbuf), CDF, total);
4721 snprintf(ubuf, sizeof(tbuf), CDF, used);
4722 snprintf(abuf, sizeof(tbuf), CDF, avail);
4725 sprintf(rbuf, RDF, ratio);
4726 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4727 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4729 printf("[%s:%d]", type, index);
4731 if (stat->os_state) {
4733 * Each character represents the matching
4736 const char state_names[] = "DRSI";
4741 for (i = 0, state = stat->os_state;
4742 state && i < sizeof(state_names); i++) {
4743 if (!(state & (1 << i)))
4745 printf("%c", state_names[i]);
4753 printf(UUF": inactive device\n", uuid);
4756 printf(UUF": %s\n", uuid, strerror(-rc));
4763 struct ll_stat_type {
4768 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4770 struct ll_statfs_data {
4772 struct obd_statfs sd_st;
4775 struct ll_statfs_buf {
4777 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4780 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4781 int ops, struct ll_statfs_buf *lsb)
4783 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4784 struct obd_uuid uuid_buf;
4785 char *poolname = NULL;
4786 struct ll_stat_type types[] = {
4787 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4788 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4789 { .st_name = NULL } };
4790 struct ll_stat_type *tp;
4791 __u64 ost_ffree = 0;
4799 poolname = strchr(pool, '.');
4800 if (poolname != NULL) {
4801 if (strncmp(fsname, pool, strlen(fsname))) {
4802 fprintf(stderr, "filesystem name incorrect\n");
4810 fd = open(mntdir, O_RDONLY);
4813 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4818 if (flags & MNTDF_SHOW) {
4819 if (flags & MNTDF_INODES)
4820 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4821 "UUID", "Inodes", "IUsed", "IFree",
4822 "IUse%", "Mounted on");
4824 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4826 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4827 "Used", "Available", "Use%", "Mounted on");
4830 for (tp = types; tp->st_name != NULL; tp++) {
4831 bool have_ost = false;
4833 if (!(tp->st_op & ops))
4836 for (index = 0; ; index++) {
4837 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4838 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4839 type = flags & MNTDF_LAZY ?
4840 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4841 rc2 = llapi_obd_fstatfs(fd, type, index,
4842 &stat_buf, &uuid_buf);
4847 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4848 if (!(flags & MNTDF_VERBOSE))
4850 } else if (rc2 < 0 && rc == 0) {
4854 /* If we have OSTs then don't report MDT block counts.
4855 * For MDT-only filesystems the expectation is that all
4856 * layouts have a DoM component. For filesystems with
4857 * OSTs, files are not necessarily going to store data
4858 * on MDTs, and MDT space is limited to a fraction of
4859 * OST space, so don't include it in the summary.
4861 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
4868 if (poolname && tp->st_op == LL_STATFS_LOV &&
4869 llapi_search_ost(fsname, poolname,
4870 obd_uuid2str(&uuid_buf)) != 1)
4873 /* the llapi_obd_statfs() call may have returned with
4874 * an error, but if it filled in uuid_buf we will at
4875 * lease use that to print out a message for that OBD.
4876 * If we didn't get anything in the uuid_buf, then fill
4877 * it in so that we can print an error message. */
4878 if (uuid_buf.uuid[0] == '\0')
4879 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4880 "%s%04x", tp->st_name, index);
4882 lsb->sb_buf[lsb->sb_count].sd_index = index;
4883 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4886 if (flags & MNTDF_SHOW)
4887 showdf(mntdir, &stat_buf,
4888 obd_uuid2str(&uuid_buf), flags,
4889 tp->st_name, index, rc2);
4894 if (tp->st_op == LL_STATFS_LMV) {
4895 sum.os_ffree += stat_buf.os_ffree;
4896 sum.os_files += stat_buf.os_files;
4897 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4898 ost_ffree += stat_buf.os_ffree;
4900 sum.os_blocks += stat_buf.os_blocks *
4902 sum.os_bfree += stat_buf.os_bfree *
4904 sum.os_bavail += stat_buf.os_bavail *
4911 /* If we don't have as many objects free on the OST as inodes
4912 * on the MDS, we reduce the total number of inodes to
4913 * compensate, so that the "inodes in use" number is correct.
4914 * Matches ll_statfs_internal() so the results are consistent. */
4915 if (ost_ffree < sum.os_ffree) {
4916 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4917 sum.os_ffree = ost_ffree;
4919 if (flags & MNTDF_SHOW) {
4921 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4928 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4930 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4932 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4934 int r1 = obd_statfs_ratio(st1, false);
4935 int r2 = obd_statfs_ratio(st2, false);
4936 int64_t result = r1 - r2;
4938 /* if both space usage are above 90, compare free inodes */
4939 if (r1 > 90 && r2 > 90)
4940 result = st2->os_ffree - st1->os_ffree;
4944 else if (result == 0)
4951 static int lfs_setdirstripe(int argc, char **argv)
4955 struct lfs_setstripe_args lsa = { 0 };
4956 struct llapi_stripe_param *param = NULL;
4957 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4960 char *mode_opt = NULL;
4961 bool default_stripe = false;
4962 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4963 mode_t previous_mode = 0;
4964 bool delete = false;
4965 struct ll_statfs_buf *lsb = NULL;
4966 char mntdir[PATH_MAX] = "";
4967 bool auto_distributed = false;
4969 struct option long_opts[] = {
4970 { .val = 'c', .name = "count", .has_arg = required_argument },
4971 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4972 { .val = 'd', .name = "delete", .has_arg = no_argument },
4973 { .val = 'D', .name = "default", .has_arg = no_argument },
4974 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4975 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4976 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
4977 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4978 { .val = 'i', .name = "mdt", .has_arg = required_argument },
4980 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4981 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4983 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4984 { .val = 'i', .name = "index", .has_arg = required_argument },
4986 { .val = 'o', .name = "mode", .has_arg = required_argument },
4987 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4988 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4990 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4991 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4994 setstripe_args_init(&lsa);
4996 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
5004 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5007 "%s %s: invalid stripe count '%s'\n",
5008 progname, argv[0], optarg);
5014 default_stripe = true;
5017 default_stripe = true;
5019 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5021 fprintf(stderr, "warning: '--hash-type' and '-t' "
5022 "deprecated, use '--mdt-hash' or '-H' instead\n");
5025 lsa.lsa_pattern = check_hashtype(optarg);
5026 if (lsa.lsa_pattern == 0) {
5028 "%s %s: bad stripe hash type '%s'\n",
5029 progname, argv[0], optarg);
5034 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5037 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5038 if (strcmp(argv[optind - 1], "--index") == 0)
5040 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5043 lsa.lsa_nr_tgts = parse_targets(mdts,
5044 sizeof(mdts) / sizeof(__u32),
5045 lsa.lsa_nr_tgts, optarg);
5046 if (lsa.lsa_nr_tgts < 0) {
5048 "%s %s: invalid MDT target(s) '%s'\n",
5049 progname, argv[0], optarg);
5053 lsa.lsa_tgts = mdts;
5054 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5055 lsa.lsa_stripe_off = mdts[0];
5057 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5059 fprintf(stderr, "warning: '-m' is deprecated, "
5060 "use '--mode' or '-o' instead\n");
5066 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5067 progname, argv[0], argv[optind - 1]);
5072 if (optind == argc) {
5073 fprintf(stderr, "%s %s: DIR must be specified\n",
5078 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5079 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5081 "%s %s: stripe offset and count must be specified\n",
5087 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5088 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5090 "%s %s: cannot specify -d with -c or -i options\n",
5095 if (mode_opt != NULL) {
5096 mode = strtoul(mode_opt, &end, 8);
5099 "%s %s: bad MODE '%s'\n",
5100 progname, argv[0], mode_opt);
5103 previous_mode = umask(0);
5107 * initialize stripe parameters, in case param is converted to specific,
5108 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5110 param = calloc(1, offsetof(typeof(*param),
5111 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5112 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5113 if (param == NULL) {
5115 "%s %s: cannot allocate memory for parameters: %s\n",
5116 progname, argv[0], strerror(ENOMEM));
5120 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5121 param->lsp_stripe_count = lsa.lsa_stripe_count;
5122 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5123 param->lsp_stripe_offset = -1;
5125 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5126 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5127 param->lsp_stripe_pattern = lsa.lsa_pattern;
5129 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5130 param->lsp_pool = lsa.lsa_pool_name;
5131 param->lsp_is_specific = false;
5132 if (lsa.lsa_nr_tgts > 1) {
5133 if (lsa.lsa_stripe_count > 0 &&
5134 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5135 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5136 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5137 "match the number of MDTs: %d\n",
5138 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5143 param->lsp_is_specific = true;
5144 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5145 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5148 dname = argv[optind];
5150 if (default_stripe) {
5151 result = llapi_dir_set_default_lmv(dname, param);
5153 /* if current \a dname isn't under the same \a mntdir
5154 * as the last one, and the last one was
5155 * auto-distributed, restore \a param.
5157 if (mntdir[0] != '\0' &&
5158 strncmp(dname, mntdir, strlen(mntdir)) &&
5160 param->lsp_is_specific = false;
5161 param->lsp_stripe_offset = -1;
5162 auto_distributed = false;
5165 if (!param->lsp_is_specific &&
5166 param->lsp_stripe_offset == -1) {
5167 char path[PATH_MAX] = "";
5170 lsb = malloc(sizeof(*lsb));
5178 /* use mntdir for dirname() temporarily */
5179 strncpy(mntdir, dname, sizeof(mntdir));
5180 if (!realpath(dirname(mntdir), path)) {
5183 "error: invalid path '%s': %s\n",
5184 argv[optind], strerror(errno));
5189 result = llapi_search_mounts(path, 0, mntdir,
5191 if (result < 0 || mntdir[0] == '\0') {
5193 "No suitable Lustre mount found\n");
5197 result = mntdf(mntdir, NULL, NULL, 0,
5198 LL_STATFS_LMV, lsb);
5202 if (param->lsp_stripe_count > lsb->sb_count) {
5204 "error: stripe count %d is too big\n",
5205 param->lsp_stripe_count);
5210 qsort(lsb->sb_buf, lsb->sb_count,
5211 sizeof(struct ll_statfs_data),
5212 ll_statfs_data_comp);
5214 auto_distributed = true;
5217 if (auto_distributed) {
5219 int nr = MAX(param->lsp_stripe_count,
5222 /* don't use server whose usage is above 90% */
5223 while (nr != param->lsp_stripe_count &&
5224 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5226 nr = MAX(param->lsp_stripe_count,
5229 /* get \a r between [0, nr) */
5232 param->lsp_stripe_offset =
5233 lsb->sb_buf[r].sd_index;
5234 if (param->lsp_stripe_count > 1) {
5237 param->lsp_is_specific = true;
5238 for (; i < param->lsp_stripe_count; i++)
5239 param->lsp_tgts[(i + r) % nr] =
5240 lsb->sb_buf[i].sd_index;
5244 result = llapi_dir_create(dname, mode, param);
5249 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5250 progname, dname, strerror(-result));
5253 dname = argv[++optind];
5254 } while (dname != NULL);
5256 if (mode_opt != NULL)
5257 umask(previous_mode);
5265 static int lfs_rmentry(int argc, char **argv)
5272 fprintf(stderr, "error: %s: missing dirname\n",
5278 dname = argv[index];
5279 while (dname != NULL) {
5280 result = llapi_direntry_remove(dname);
5282 fprintf(stderr, "error: %s: remove dir entry '%s' "
5283 "failed\n", argv[0], dname);
5286 dname = argv[++index];
5291 static int lfs_mv(int argc, char **argv)
5293 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5294 struct find_param param = {
5301 struct option long_opts[] = {
5302 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5303 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5304 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5307 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5309 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5311 fprintf(stderr, "warning: '-M' deprecated"
5312 ", use '--mdt-index' or '-m' instead\n");
5315 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5317 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5323 param.fp_verbose = VERBOSE_DETAIL;
5326 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5327 progname, argv[optind - 1]);
5332 if (lmu.lum_stripe_offset == -1) {
5333 fprintf(stderr, "%s mv: MDT index must be specified\n",
5338 if (optind >= argc) {
5339 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5344 /* initialize migrate mdt parameters */
5345 param.fp_lmv_md = &lmu;
5346 param.fp_migrate = 1;
5347 rc = llapi_migrate_mdt(argv[optind], ¶m);
5349 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5350 progname, argv[optind], param.fp_mdt_index,
5355 static int lfs_osts(int argc, char **argv)
5357 return lfs_tgts(argc, argv);
5360 static int lfs_mdts(int argc, char **argv)
5362 return lfs_tgts(argc, argv);
5365 static int lfs_df(int argc, char **argv)
5367 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5368 enum mntdf_flags flags = MNTDF_SHOW;
5369 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5370 int c, rc = 0, index = 0;
5371 char fsname[PATH_MAX] = "", *pool_name = NULL;
5372 struct option long_opts[] = {
5373 { .val = 'h', .name = "human-readable",
5374 .has_arg = no_argument },
5375 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5376 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5377 { .val = 'p', .name = "pool", .has_arg = required_argument },
5378 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5381 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5384 flags |= MNTDF_COOKED;
5387 flags |= MNTDF_INODES;
5390 flags |= MNTDF_LAZY;
5396 flags |= MNTDF_VERBOSE;
5402 if (optind < argc && !realpath(argv[optind], path)) {
5404 fprintf(stderr, "error: invalid path '%s': %s\n",
5405 argv[optind], strerror(-rc));
5409 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5410 /* Check if we have a mount point */
5411 if (mntdir[0] == '\0')
5414 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5415 if (rc || path[0] != '\0')
5417 fsname[0] = '\0'; /* avoid matching in next loop */
5418 mntdir[0] = '\0'; /* avoid matching in next loop */
5424 static int lfs_getname(int argc, char **argv)
5426 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5427 int rc = 0, index = 0, c;
5428 char buf[sizeof(struct obd_uuid)];
5430 while ((c = getopt(argc, argv, "h")) != -1)
5433 if (optind == argc) { /* no paths specified, get all paths. */
5434 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5435 rc = llapi_getname(mntdir, buf, sizeof(buf));
5438 "cannot get name for `%s': %s\n",
5439 mntdir, strerror(-rc));
5443 printf("%s %s\n", buf, mntdir);
5445 path[0] = fsname[0] = mntdir[0] = 0;
5447 } else { /* paths specified, only attempt to search these. */
5448 for (; optind < argc; optind++) {
5449 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5452 "cannot get name for `%s': %s\n",
5453 argv[optind], strerror(-rc));
5457 printf("%s %s\n", buf, argv[optind]);
5463 static int lfs_check(int argc, char **argv)
5465 char mntdir[PATH_MAX] = {'\0'};
5474 fprintf(stderr, "%s check: server type must be specified\n",
5479 obd_types[0] = obd_type1;
5480 obd_types[1] = obd_type2;
5481 obd_types[2] = obd_type3;
5483 if (strcmp(argv[1], "osts") == 0) {
5484 strcpy(obd_types[0], "osc");
5485 } else if (strcmp(argv[1], "mdts") == 0 ||
5486 strcmp(argv[1], "mds") == 0) {
5487 strcpy(obd_types[0], "mdc");
5488 } else if (strcmp(argv[1], "mgts") == 0) {
5489 strcpy(obd_types[0], "mgc");
5490 } else if (strcmp(argv[1], "all") == 0 ||
5491 strcmp(argv[1], "servers") == 0) {
5493 strcpy(obd_types[0], "osc");
5494 strcpy(obd_types[1], "mdc");
5495 strcpy(obd_types[2], "mgc");
5497 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5502 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5503 if (rc < 0 || mntdir[0] == '\0') {
5505 "%s check: cannot find mounted Lustre filesystem: %s\n",
5506 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5510 rc = llapi_target_check(num_types, obd_types, mntdir);
5512 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5513 progname, argv[1], strerror(-rc));
5519 #ifdef HAVE_SYS_QUOTA_H
5520 #define ARG2INT(nr, str, msg) \
5523 nr = strtol(str, &endp, 0); \
5524 if (*endp != '\0') { \
5525 fprintf(stderr, "%s: bad %s '%s'\n", \
5526 progname, msg, str); \
5531 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5533 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5534 * returns the value or ULONG_MAX on integer overflow or incorrect format
5536 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5537 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5538 * 3. empty integer value is interpreted as 0
5540 static unsigned long str2sec(const char* timestr)
5542 const char spec[] = "smhdw";
5543 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5544 unsigned long val = 0;
5547 if (strpbrk(timestr, spec) == NULL) {
5548 /* no specifiers inside the time string,
5549 should treat it as an integer value */
5550 val = strtoul(timestr, &tail, 10);
5551 return *tail ? ULONG_MAX : val;
5554 /* format string is XXwXXdXXhXXmXXs */
5560 v = strtoul(timestr, &tail, 10);
5561 if (v == ULONG_MAX || *tail == '\0')
5562 /* value too large (ULONG_MAX or more)
5563 or missing specifier */
5566 ptr = strchr(spec, *tail);
5568 /* unknown specifier */
5573 /* check if product will overflow the type */
5574 if (!(v < ULONG_MAX / mult[ind]))
5577 ADD_OVERFLOW(val, mult[ind] * v);
5578 if (val == ULONG_MAX)
5590 #define ARG2ULL(nr, str, def_units) \
5592 unsigned long long limit, units = def_units; \
5595 rc = llapi_parse_size(str, &limit, &units, 1); \
5597 fprintf(stderr, "%s: invalid limit '%s'\n", \
5604 static inline int has_times_option(int argc, char **argv)
5608 for (i = 1; i < argc; i++)
5609 if (!strcmp(argv[i], "-t"))
5615 int lfs_setquota_times(int argc, char **argv)
5618 struct if_quotactl qctl;
5619 char *mnt, *obd_type = (char *)qctl.obd_type;
5620 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5621 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5622 struct option long_opts[] = {
5623 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5624 { .val = 'g', .name = "group", .has_arg = no_argument },
5625 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5626 { .val = 'p', .name = "projid", .has_arg = no_argument },
5627 { .val = 't', .name = "times", .has_arg = no_argument },
5628 { .val = 'u', .name = "user", .has_arg = no_argument },
5632 memset(&qctl, 0, sizeof(qctl));
5633 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5634 qctl.qc_type = ALLQUOTA;
5636 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5637 long_opts, NULL)) != -1) {
5648 if (qctl.qc_type != ALLQUOTA) {
5649 fprintf(stderr, "error: -u/g/p can't be used "
5650 "more than once\n");
5653 qctl.qc_type = qtype;
5656 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5657 fprintf(stderr, "error: bad block-grace: %s\n",
5661 dqb->dqb_valid |= QIF_BTIME;
5664 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5665 fprintf(stderr, "error: bad inode-grace: %s\n",
5669 dqb->dqb_valid |= QIF_ITIME;
5671 case 't': /* Yes, of course! */
5673 default: /* getopt prints error message for us when opterr != 0 */
5678 if (qctl.qc_type == ALLQUOTA) {
5679 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5683 if (optind != argc - 1) {
5684 fprintf(stderr, "error: unexpected parameters encountered\n");
5688 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5689 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5690 fprintf(stderr, "error: grace time is too large\n");
5695 rc = llapi_quotactl(mnt, &qctl);
5698 fprintf(stderr, "%s %s ", obd_type,
5699 obd_uuid2str(&qctl.obd_uuid));
5700 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5707 #define BSLIMIT (1 << 0)
5708 #define BHLIMIT (1 << 1)
5709 #define ISLIMIT (1 << 2)
5710 #define IHLIMIT (1 << 3)
5712 int lfs_setquota(int argc, char **argv)
5715 struct if_quotactl qctl;
5716 char *mnt, *obd_type = (char *)qctl.obd_type;
5717 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5718 struct option long_opts[] = {
5719 { .val = 'b', .name = "block-softlimit",
5720 .has_arg = required_argument },
5721 { .val = 'B', .name = "block-hardlimit",
5722 .has_arg = required_argument },
5723 { .val = 'd', .name = "default", .has_arg = no_argument },
5724 { .val = 'g', .name = "group", .has_arg = required_argument },
5725 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5726 { .val = 'i', .name = "inode-softlimit",
5727 .has_arg = required_argument },
5728 { .val = 'I', .name = "inode-hardlimit",
5729 .has_arg = required_argument },
5730 { .val = 'p', .name = "projid", .has_arg = required_argument },
5731 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5732 { .val = 'u', .name = "user", .has_arg = required_argument },
5733 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5735 unsigned limit_mask = 0;
5737 bool use_default = false;
5740 if (has_times_option(argc, argv))
5741 return lfs_setquota_times(argc, argv);
5743 memset(&qctl, 0, sizeof(qctl));
5744 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5745 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5746 * so it can be used as a marker that qc_type
5747 * isn't reinitialized from command line */
5749 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5750 long_opts, NULL)) != -1) {
5753 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5756 goto quota_type_def;
5759 rc = name2uid(&qctl.qc_id, optarg);
5762 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5765 goto quota_type_def;
5768 rc = name2gid(&qctl.qc_id, optarg);
5771 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5774 goto quota_type_def;
5777 rc = name2projid(&qctl.qc_id, optarg);
5780 qctl.qc_id = strtoul(optarg, &endptr, 10);
5781 if (*endptr != '\0') {
5782 fprintf(stderr, "%s setquota: invalid"
5783 " id '%s'\n", progname, optarg);
5788 if (qctl.qc_id == 0) {
5789 fprintf(stderr, "%s setquota: can't set quota"
5790 " for root usr/group/project.\n",
5796 if (qctl.qc_type != ALLQUOTA) {
5798 "%s setquota: only one of -u, -U, -g,"
5799 " -G, -p or -P may be specified\n",
5803 qctl.qc_type = qtype;
5806 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5810 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5811 dqb->dqb_bsoftlimit >>= 10;
5812 limit_mask |= BSLIMIT;
5813 if (dqb->dqb_bsoftlimit &&
5814 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5816 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5817 "See '%s help setquota' or Lustre manual for details\n",
5818 progname, dqb->dqb_bsoftlimit,
5822 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5823 dqb->dqb_bhardlimit >>= 10;
5824 limit_mask |= BHLIMIT;
5825 if (dqb->dqb_bhardlimit &&
5826 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5828 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5829 "See '%s help setquota' or Lustre manual for details\n",
5830 progname, dqb->dqb_bhardlimit,
5834 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5835 limit_mask |= ISLIMIT;
5836 if (dqb->dqb_isoftlimit &&
5837 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5839 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5840 "See '%s help setquota' or Lustre manual for details\n",
5841 progname, dqb->dqb_isoftlimit,
5845 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5846 limit_mask |= IHLIMIT;
5847 if (dqb->dqb_ihardlimit &&
5848 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5850 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5851 "See '%s help setquota' or Lustre manual for details\n",
5852 progname, dqb->dqb_ihardlimit,
5857 "%s setquota: unrecognized option '%s'\n",
5858 progname, argv[optind - 1]);
5863 if (qctl.qc_type == ALLQUOTA) {
5865 "%s setquota: either -u or -g must be specified\n",
5870 if (!use_default && limit_mask == 0) {
5872 "%s setquota: at least one limit must be specified\n",
5877 if (use_default && limit_mask != 0) {
5879 "%s setquota: limits should not be specified when"
5880 " using default quota\n",
5885 if (use_default && qctl.qc_id == 0) {
5887 "%s setquota: can not set default quota for root"
5888 " user/group/project\n",
5893 if (optind != argc - 1) {
5895 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5896 progname, argv[optind]);
5903 dqb->dqb_bhardlimit = 0;
5904 dqb->dqb_bsoftlimit = 0;
5905 dqb->dqb_ihardlimit = 0;
5906 dqb->dqb_isoftlimit = 0;
5909 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5910 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5911 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5912 /* sigh, we can't just set blimits/ilimits */
5913 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5914 .qc_type = qctl.qc_type,
5915 .qc_id = qctl.qc_id};
5917 rc = llapi_quotactl(mnt, &tmp_qctl);
5921 if (!(limit_mask & BHLIMIT))
5922 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5923 if (!(limit_mask & BSLIMIT))
5924 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5925 if (!(limit_mask & IHLIMIT))
5926 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5927 if (!(limit_mask & ISLIMIT))
5928 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5930 /* Keep grace times if we have got no softlimit arguments */
5931 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5932 dqb->dqb_valid |= QIF_BTIME;
5933 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5936 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5937 dqb->dqb_valid |= QIF_ITIME;
5938 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5942 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5943 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5945 rc = llapi_quotactl(mnt, &qctl);
5949 "%s setquota: cannot quotactl '%s' '%s': %s",
5951 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5958 /* Converts seconds value into format string
5959 * result is returned in buf
5961 * 1. result is in descenting order: 1w2d3h4m5s
5962 * 2. zero fields are not filled (except for p. 3): 5d1s
5963 * 3. zero seconds value is presented as "0s"
5965 static char * __sec2str(time_t seconds, char *buf)
5967 const char spec[] = "smhdw";
5968 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5973 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5974 c = seconds / mult[i];
5976 if (c > 0 || (i == 0 && buf == tail))
5977 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5985 static void sec2str(time_t seconds, char *buf, int rc)
5992 tail = __sec2str(seconds, tail);
5994 if (rc && tail - buf < 39) {
6000 static void diff2str(time_t seconds, char *buf, time_t now)
6006 if (seconds <= now) {
6007 strcpy(buf, "none");
6010 __sec2str(seconds - now, buf);
6013 static void print_quota_title(char *name, struct if_quotactl *qctl,
6014 bool human_readable, bool show_default)
6017 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6018 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6019 "Filesystem", "bquota", "blimit", "bgrace",
6020 "iquota", "ilimit", "igrace");
6022 printf("Disk quotas for %s %s (%cid %u):\n",
6023 qtype_name(qctl->qc_type), name,
6024 *qtype_name(qctl->qc_type), qctl->qc_id);
6025 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6026 "Filesystem", human_readable ? "used" : "kbytes",
6027 "quota", "limit", "grace",
6028 "files", "quota", "limit", "grace");
6032 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6035 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6038 snprintf(buf, buflen, "%5.4gP",
6039 (double)num / ((__u64)1 << 40));
6041 snprintf(buf, buflen, "%5.4gT",
6042 (double)num / (1 << 30));
6044 snprintf(buf, buflen, "%5.4gG",
6045 (double)num / (1 << 20));
6047 snprintf(buf, buflen, "%5.4gM",
6048 (double)num / (1 << 10));
6050 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6054 #define STRBUF_LEN 32
6055 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6056 int rc, bool h, bool show_default)
6062 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6063 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6064 int bover = 0, iover = 0;
6065 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6066 char numbuf[3][STRBUF_LEN];
6068 char strbuf[STRBUF_LEN];
6070 if (dqb->dqb_bhardlimit &&
6071 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6073 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6074 if (dqb->dqb_btime > now) {
6081 if (dqb->dqb_ihardlimit &&
6082 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6084 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6085 if (dqb->dqb_itime > now) {
6093 if (strlen(mnt) > 15)
6094 printf("%s\n%15s", mnt, "");
6096 printf("%15s", mnt);
6099 diff2str(dqb->dqb_btime, timebuf, now);
6100 else if (show_default)
6101 snprintf(timebuf, sizeof(timebuf), "%llu",
6104 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6105 strbuf, sizeof(strbuf), h);
6106 if (rc == -EREMOTEIO)
6107 sprintf(numbuf[0], "%s*", strbuf);
6109 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6110 "%s" : "[%s]", strbuf);
6112 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6113 if (type == QC_GENERAL)
6114 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6115 "%s" : "[%s]", strbuf);
6117 sprintf(numbuf[1], "%s", "-");
6119 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6120 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6121 "%s" : "[%s]", strbuf);
6124 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6126 printf(" %7s%c %6s %7s %7s",
6127 numbuf[0], bover ? '*' : ' ', numbuf[1],
6128 numbuf[2], bover > 1 ? timebuf : "-");
6132 diff2str(dqb->dqb_itime, timebuf, now);
6133 else if (show_default)
6134 snprintf(timebuf, sizeof(timebuf), "%llu",
6137 snprintf(numbuf[0], sizeof(numbuf),
6138 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6139 (uintmax_t)dqb->dqb_curinodes);
6141 if (type == QC_GENERAL)
6142 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6144 (uintmax_t)dqb->dqb_isoftlimit);
6146 sprintf(numbuf[1], "%s", "-");
6148 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6149 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6152 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6153 else if (type != QC_OSTIDX)
6154 printf(" %7s%c %6s %7s %7s",
6155 numbuf[0], iover ? '*' : ' ', numbuf[1],
6156 numbuf[2], iover > 1 ? timebuf : "-");
6158 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6161 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6162 qctl->qc_cmd == Q_GETOINFO) {
6166 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6167 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6168 printf("Block grace time: %s; Inode grace time: %s\n",
6169 bgtimebuf, igtimebuf);
6173 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6174 bool h, __u64 *total)
6176 int rc = 0, rc1 = 0, count = 0;
6177 __u32 valid = qctl->qc_valid;
6179 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6181 fprintf(stderr, "can not get %s count: %s\n",
6182 is_mdt ? "mdt": "ost", strerror(-rc));
6186 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6187 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6188 rc = llapi_quotactl(mnt, qctl);
6190 /* It is remote client case. */
6191 if (rc == -EOPNOTSUPP) {
6198 fprintf(stderr, "quotactl %s%d failed.\n",
6199 is_mdt ? "mdt": "ost", qctl->qc_idx);
6203 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6204 qctl->qc_valid, 0, h, false);
6205 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6206 qctl->qc_dqblk.dqb_bhardlimit;
6209 qctl->qc_valid = valid;
6213 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6214 int verbose, int quiet, bool human_readable,
6217 int rc1 = 0, rc2 = 0, rc3 = 0;
6218 char *obd_type = (char *)qctl->obd_type;
6219 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6220 __u64 total_ialloc = 0, total_balloc = 0;
6221 bool use_default_for_blk = false;
6222 bool use_default_for_file = false;
6225 rc1 = llapi_quotactl(mnt, qctl);
6229 fprintf(stderr, "%s quotas are not enabled.\n",
6230 qtype_name(qctl->qc_type));
6233 fprintf(stderr, "Permission denied.\n");
6236 /* We already got error message. */
6239 fprintf(stderr, "Unexpected quotactl error: %s\n",
6244 if (!show_default && qctl->qc_id == 0) {
6245 qctl->qc_dqblk.dqb_bhardlimit = 0;
6246 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6247 qctl->qc_dqblk.dqb_ihardlimit = 0;
6248 qctl->qc_dqblk.dqb_isoftlimit = 0;
6249 qctl->qc_dqblk.dqb_btime = 0;
6250 qctl->qc_dqblk.dqb_itime = 0;
6251 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6254 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6255 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6256 use_default_for_blk = true;
6257 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6260 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6261 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6262 use_default_for_file = true;
6263 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6266 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6267 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6268 print_quota_title(name, qctl, human_readable, show_default);
6270 if (rc1 && *obd_type)
6271 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6273 if (qctl->qc_valid != QC_GENERAL)
6276 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6277 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6278 (QIF_LIMITS|QIF_USAGE));
6280 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6282 if (!show_default && verbose &&
6283 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6284 char strbuf[STRBUF_LEN];
6286 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6288 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6290 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6292 printf("Total allocated inode limit: %ju, total "
6293 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6297 if (use_default_for_blk)
6298 printf("%cid %u is using default block quota setting\n",
6299 *qtype_name(qctl->qc_type), qctl->qc_id);
6301 if (use_default_for_file)
6302 printf("%cid %u is using default file quota setting\n",
6303 *qtype_name(qctl->qc_type), qctl->qc_id);
6305 if (rc1 || rc2 || rc3 || inacc)
6306 printf("Some errors happened when getting quota info. "
6307 "Some devices may be not working or deactivated. "
6308 "The data in \"[]\" is inaccurate.\n");
6314 static int lfs_project(int argc, char **argv)
6316 int ret = 0, err = 0, c, i;
6317 struct project_handle_control phc = { 0 };
6318 enum lfs_project_ops_t op;
6321 phc.assign_projid = false;
6322 /* default action */
6323 op = LFS_PROJECT_LIST;
6325 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6328 if (op != LFS_PROJECT_LIST) {
6330 "%s: cannot specify '-c' '-C' '-s' together\n",
6335 op = LFS_PROJECT_CHECK;
6338 if (op != LFS_PROJECT_LIST) {
6340 "%s: cannot specify '-c' '-C' '-s' together\n",
6345 op = LFS_PROJECT_CLEAR;
6348 if (op != LFS_PROJECT_LIST) {
6350 "%s: cannot specify '-c' '-C' '-s' together\n",
6355 phc.set_inherit = true;
6356 op = LFS_PROJECT_SET;
6362 phc.keep_projid = true;
6365 phc.recursive = true;
6368 phc.projid = strtoul(optarg, NULL, 0);
6369 phc.assign_projid = true;
6373 phc.newline = false;
6376 fprintf(stderr, "%s: invalid option '%c'\n",
6382 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6383 op = LFS_PROJECT_SET;
6384 phc.set_projid = true;
6385 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6386 phc.set_projid = true;
6390 case LFS_PROJECT_CHECK:
6391 if (phc.keep_projid) {
6393 "%s: '-k' is useless together with '-c'\n",
6398 case LFS_PROJECT_CLEAR:
6401 "%s: '-0' is useless together with '-C'\n",
6405 if (phc.assign_projid) {
6407 "%s: '-p' is useless together with '-C'\n",
6412 case LFS_PROJECT_SET:
6415 "%s: '-0' is useless together with '-s'\n",
6419 if (phc.keep_projid) {
6421 "%s: '-k' is useless together with '-s'\n",
6429 "%s: '-0' is useless for list operations\n",
6439 fprintf(stderr, "%s: missing file or directory target(s)\n",
6444 for (i = 0; i < argc; i++) {
6446 case LFS_PROJECT_CHECK:
6447 err = lfs_project_check(argv[i], &phc);
6449 case LFS_PROJECT_LIST:
6450 err = lfs_project_list(argv[i], &phc);
6452 case LFS_PROJECT_CLEAR:
6453 err = lfs_project_clear(argv[i], &phc);
6455 case LFS_PROJECT_SET:
6456 err = lfs_project_set(argv[i], &phc);
6468 static int lfs_quota(int argc, char **argv)
6471 char *mnt, *name = NULL;
6472 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6473 .qc_type = ALLQUOTA };
6474 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6475 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6477 __u32 valid = QC_GENERAL, idx = 0;
6478 bool human_readable = false;
6479 bool show_default = false;
6482 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6485 show_default = true;
6490 show_default = true;
6495 show_default = true;
6499 if (qctl.qc_type != ALLQUOTA) {
6501 "%s quota: only one of -u, -g, or -p may be specified\n",
6505 qctl.qc_type = qtype;
6508 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6511 valid = qctl.qc_valid = QC_UUID;
6512 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6515 valid = qctl.qc_valid = QC_MDTIDX;
6516 idx = qctl.qc_idx = atoi(optarg);
6517 if (idx == 0 && *optarg != '0') {
6519 "%s quota: invalid MDT index '%s'\n",
6525 valid = qctl.qc_valid = QC_OSTIDX;
6526 idx = qctl.qc_idx = atoi(optarg);
6527 if (idx == 0 && *optarg != '0') {
6529 "%s quota: invalid OST index '%s'\n",
6541 human_readable = true;
6544 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6545 progname, argv[optind - 1]);
6550 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6551 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6552 optind == argc - 1 && !show_default) {
6554 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6555 qctl.qc_valid = valid;
6558 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6559 qctl.qc_type = qtype;
6560 if (qtype == USRQUOTA) {
6561 qctl.qc_id = geteuid();
6562 rc = uid2name(&name, qctl.qc_id);
6564 qctl.qc_id = getegid();
6565 rc = gid2name(&name, qctl.qc_id);
6570 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6571 human_readable, show_default);
6576 /* lfs quota -u username /path/to/lustre/mount */
6577 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6578 /* options should be followed by u/g-name and mntpoint */
6579 if ((!show_default && optind + 2 != argc) ||
6580 (show_default && optind + 1 != argc) ||
6581 qctl.qc_type == ALLQUOTA) {
6583 "%s quota: name and mount point must be specified\n",
6588 if (!show_default) {
6589 name = argv[optind++];
6590 switch (qctl.qc_type) {
6592 rc = name2uid(&qctl.qc_id, name);
6595 rc = name2gid(&qctl.qc_id, name);
6598 rc = name2projid(&qctl.qc_id, name);
6605 qctl.qc_valid = QC_GENERAL;
6606 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6611 qctl.qc_id = strtoul(name, &endptr, 10);
6612 if (*endptr != '\0') {
6613 fprintf(stderr, "%s quota: invalid id '%s'\n",
6618 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6619 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6625 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6626 human_readable, show_default);
6629 #endif /* HAVE_SYS_QUOTA_H! */
6631 static int flushctx_ioctl(char *mp)
6635 fd = open(mp, O_RDONLY);
6637 fprintf(stderr, "flushctx: error open %s: %s\n",
6638 mp, strerror(errno));
6642 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6644 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6645 mp, strerror(errno));
6651 static int lfs_flushctx(int argc, char **argv)
6653 int kdestroy = 0, c;
6654 char mntdir[PATH_MAX] = {'\0'};
6658 while ((c = getopt(argc, argv, "k")) != -1) {
6664 fprintf(stderr, "error: %s: option '-%c' "
6665 "unrecognized\n", argv[0], c);
6671 if ((rc = system("kdestroy > /dev/null")) != 0) {
6672 rc = WEXITSTATUS(rc);
6673 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6677 if (optind >= argc) {
6678 /* flush for all mounted lustre fs. */
6679 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6680 /* Check if we have a mount point */
6681 if (mntdir[0] == '\0')
6684 if (flushctx_ioctl(mntdir))
6687 mntdir[0] = '\0'; /* avoid matching in next loop */
6690 /* flush fs as specified */
6691 while (optind < argc) {
6692 if (flushctx_ioctl(argv[optind++]))
6699 static int lfs_cp(int argc, char **argv)
6701 fprintf(stderr, "remote client copy file(s).\n"
6702 "obsolete, does not support it anymore.\n");
6706 static int lfs_ls(int argc, char **argv)
6708 fprintf(stderr, "remote client lists directory contents.\n"
6709 "obsolete, does not support it anymore.\n");
6713 static int lfs_changelog(int argc, char **argv)
6715 void *changelog_priv;
6716 struct changelog_rec *rec;
6717 long long startrec = 0, endrec = 0;
6719 struct option long_opts[] = {
6720 { .val = 'f', .name = "follow", .has_arg = no_argument },
6722 char short_opts[] = "f";
6725 while ((rc = getopt_long(argc, argv, short_opts,
6726 long_opts, NULL)) != -1) {
6733 "%s changelog: unrecognized option '%s'\n",
6734 progname, argv[optind - 1]);
6738 if (optind >= argc) {
6739 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6744 mdd = argv[optind++];
6746 startrec = strtoll(argv[optind++], NULL, 10);
6748 endrec = strtoll(argv[optind++], NULL, 10);
6750 rc = llapi_changelog_start(&changelog_priv,
6751 CHANGELOG_FLAG_BLOCK |
6752 CHANGELOG_FLAG_JOBID |
6753 CHANGELOG_FLAG_EXTRA_FLAGS |
6754 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6757 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6758 progname, strerror(errno = -rc));
6762 rc = llapi_changelog_set_xflags(changelog_priv,
6763 CHANGELOG_EXTRA_FLAG_UIDGID |
6764 CHANGELOG_EXTRA_FLAG_NID |
6765 CHANGELOG_EXTRA_FLAG_OMODE |
6766 CHANGELOG_EXTRA_FLAG_XATTR);
6769 "%s changelog: cannot set xflags for changelog: %s\n",
6770 progname, strerror(errno = -rc));
6774 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6778 if (endrec && rec->cr_index > endrec) {
6779 llapi_changelog_free(&rec);
6782 if (rec->cr_index < startrec) {
6783 llapi_changelog_free(&rec);
6787 secs = rec->cr_time >> 30;
6788 gmtime_r(&secs, &ts);
6789 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6790 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6791 changelog_type2str(rec->cr_type),
6792 ts.tm_hour, ts.tm_min, ts.tm_sec,
6793 (int)(rec->cr_time & ((1 << 30) - 1)),
6794 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6795 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6797 if (rec->cr_flags & CLF_JOBID) {
6798 struct changelog_ext_jobid *jid =
6799 changelog_rec_jobid(rec);
6801 if (jid->cr_jobid[0] != '\0')
6802 printf(" j=%s", jid->cr_jobid);
6805 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6806 struct changelog_ext_extra_flags *ef =
6807 changelog_rec_extra_flags(rec);
6809 printf(" ef=0x%llx", ef->cr_extra_flags);
6811 if (ef->cr_extra_flags & CLFE_UIDGID) {
6812 struct changelog_ext_uidgid *uidgid =
6813 changelog_rec_uidgid(rec);
6815 printf(" u=%llu:%llu",
6816 uidgid->cr_uid, uidgid->cr_gid);
6818 if (ef->cr_extra_flags & CLFE_NID) {
6819 struct changelog_ext_nid *nid =
6820 changelog_rec_nid(rec);
6823 libcfs_nid2str(nid->cr_nid));
6826 if (ef->cr_extra_flags & CLFE_OPEN) {
6827 struct changelog_ext_openmode *omd =
6828 changelog_rec_openmode(rec);
6829 char mode[] = "---";
6831 /* exec mode must be exclusive */
6832 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6835 if (omd->cr_openflags & MDS_FMODE_READ)
6837 if (omd->cr_openflags &
6844 if (strcmp(mode, "---") != 0)
6845 printf(" m=%s", mode);
6849 if (ef->cr_extra_flags & CLFE_XATTR) {
6850 struct changelog_ext_xattr *xattr =
6851 changelog_rec_xattr(rec);
6853 if (xattr->cr_xattr[0] != '\0')
6854 printf(" x=%s", xattr->cr_xattr);
6858 if (rec->cr_namelen)
6859 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6860 rec->cr_namelen, changelog_rec_name(rec));
6862 if (rec->cr_flags & CLF_RENAME) {
6863 struct changelog_ext_rename *rnm =
6864 changelog_rec_rename(rec);
6866 if (!fid_is_zero(&rnm->cr_sfid))
6867 printf(" s="DFID" sp="DFID" %.*s",
6868 PFID(&rnm->cr_sfid),
6869 PFID(&rnm->cr_spfid),
6870 (int)changelog_rec_snamelen(rec),
6871 changelog_rec_sname(rec));
6875 llapi_changelog_free(&rec);
6878 llapi_changelog_fini(&changelog_priv);
6881 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6882 progname, strerror(errno = -rc));
6884 return (rc == 1 ? 0 : rc);
6887 static int lfs_changelog_clear(int argc, char **argv)
6895 endrec = strtoll(argv[3], NULL, 10);
6897 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6900 fprintf(stderr, "%s: record out of range: %llu\n",
6902 else if (rc == -ENOENT)
6903 fprintf(stderr, "%s: no changelog user: %s\n",
6906 fprintf(stderr, "%s error: %s\n", argv[0],
6915 static int lfs_fid2path(int argc, char **argv)
6917 struct option long_opts[] = {
6918 { .val = 'c', .name = "cur", .has_arg = no_argument },
6919 { .val = 'l', .name = "link", .has_arg = required_argument },
6920 { .val = 'r', .name = "rec", .has_arg = required_argument },
6922 char short_opts[] = "cl:r:";
6923 char *device, *fid, *path;
6924 long long recno = -1;
6929 char *endptr = NULL;
6931 while ((rc = getopt_long(argc, argv, short_opts,
6932 long_opts, NULL)) != -1) {
6938 linkno = strtol(optarg, &endptr, 10);
6939 if (*endptr != '\0') {
6941 "%s fid2path: invalid linkno '%s'\n",
6947 recno = strtoll(optarg, &endptr, 10);
6948 if (*endptr != '\0') {
6950 "%s fid2path: invalid recno '%s'\n",
6957 "%s fid2path: unrecognized option '%s'\n",
6958 progname, argv[optind - 1]);
6965 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6970 device = argv[optind++];
6971 path = calloc(1, PATH_MAX);
6975 "%s fid2path: cannot allocate memory for path: %s\n",
6976 progname, strerror(-rc));
6981 while (optind < argc) {
6982 fid = argv[optind++];
6984 lnktmp = (linkno >= 0) ? linkno : 0;
6986 int oldtmp = lnktmp;
6987 long long rectmp = recno;
6989 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6993 "%s fid2path: cannot find '%s': %s\n",
6994 progname, fid, strerror(errno = -rc2));
7001 fprintf(stdout, "%lld ", rectmp);
7002 if (device[0] == '/') {
7003 fprintf(stdout, "%s", device);
7004 if (device[strlen(device) - 1] != '/')
7005 fprintf(stdout, "/");
7006 } else if (path[0] == '\0') {
7007 fprintf(stdout, "/");
7009 fprintf(stdout, "%s\n", path);
7012 /* specified linkno */
7014 if (oldtmp == lnktmp)
7024 static int lfs_path2fid(int argc, char **argv)
7026 struct option long_opts[] = {
7027 { .val = 'p', .name = "parents", .has_arg = no_argument },
7030 const char short_opts[] = "p";
7031 const char *sep = "";
7034 bool show_parents = false;
7036 while ((rc = getopt_long(argc, argv, short_opts,
7037 long_opts, NULL)) != -1) {
7040 show_parents = true;
7044 "%s path2fid: unrecognized option '%s'\n",
7045 progname, argv[optind - 1]);
7050 if (optind > argc - 1) {
7051 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7055 else if (optind < argc - 1)
7059 for (path = argv + optind; *path != NULL; path++) {
7061 if (!show_parents) {
7062 err = llapi_path2fid(*path, &fid);
7064 printf("%s%s"DFID"\n",
7065 *sep != '\0' ? *path : "", sep,
7068 char name[NAME_MAX + 1];
7069 unsigned int linkno = 0;
7071 while ((err = llapi_path2parent(*path, linkno, &fid,
7072 name, sizeof(name))) == 0) {
7073 if (*sep != '\0' && linkno == 0)
7074 printf("%s%s", *path, sep);
7076 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7081 /* err == -ENODATA is end-of-loop */
7082 if (linkno > 0 && err == -ENODATA) {
7090 "%s path2fid: cannot get %sfid for '%s': %s\n",
7091 progname, show_parents ? "parent " : "", *path,
7103 static int lfs_data_version(int argc, char **argv)
7110 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7113 fprintf(stderr, "%s data_version: FILE must be specified\n",
7118 while ((c = getopt(argc, argv, "nrw")) != -1) {
7121 data_version_flags = 0;
7124 data_version_flags |= LL_DV_RD_FLUSH;
7127 data_version_flags |= LL_DV_WR_FLUSH;
7131 "%s data_version: unrecognized option '%s'\n",
7132 progname, argv[optind - 1]);
7136 if (optind == argc) {
7137 fprintf(stderr, "%s data_version: FILE must be specified\n",
7142 path = argv[optind];
7143 fd = open(path, O_RDONLY);
7146 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7147 progname, path, strerror(-rc));
7151 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7154 "%s data_version: cannot get version for '%s': %s\n",
7155 progname, path, strerror(-rc));
7157 printf("%ju" "\n", (uintmax_t)data_version);
7163 static int lfs_hsm_state(int argc, char **argv)
7168 struct hsm_user_state hus;
7176 rc = llapi_hsm_state_get(path, &hus);
7178 fprintf(stderr, "can't get hsm state for %s: %s\n",
7179 path, strerror(errno = -rc));
7183 /* Display path name and status flags */
7184 printf("%s: (0x%08x)", path, hus.hus_states);
7186 if (hus.hus_states & HS_RELEASED)
7187 printf(" released");
7188 if (hus.hus_states & HS_EXISTS)
7190 if (hus.hus_states & HS_DIRTY)
7192 if (hus.hus_states & HS_ARCHIVED)
7193 printf(" archived");
7194 /* Display user-settable flags */
7195 if (hus.hus_states & HS_NORELEASE)
7196 printf(" never_release");
7197 if (hus.hus_states & HS_NOARCHIVE)
7198 printf(" never_archive");
7199 if (hus.hus_states & HS_LOST)
7200 printf(" lost_from_hsm");
7202 if (hus.hus_archive_id != 0)
7203 printf(", archive_id:%d", hus.hus_archive_id);
7206 } while (++i < argc);
7211 #define LFS_HSM_SET 0
7212 #define LFS_HSM_CLEAR 1
7215 * Generic function to set or clear HSM flags.
7216 * Used by hsm_set and hsm_clear.
7218 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7220 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7222 struct option long_opts[] = {
7223 { .val = 'A', .name = "archived", .has_arg = no_argument },
7224 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7225 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7226 { .val = 'e', .name = "exists", .has_arg = no_argument },
7227 { .val = 'l', .name = "lost", .has_arg = no_argument },
7228 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7229 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7231 char short_opts[] = "lraAdei:";
7235 __u32 archive_id = 0;
7241 while ((c = getopt_long(argc, argv, short_opts,
7242 long_opts, NULL)) != -1) {
7248 mask |= HS_NOARCHIVE;
7251 mask |= HS_ARCHIVED;
7254 mask |= HS_NORELEASE;
7263 archive_id = strtol(optarg, &end, 10);
7265 fprintf(stderr, "invalid archive_id: '%s'\n",
7273 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7274 argv[0], argv[optind - 1]);
7279 /* User should have specified a flag */
7283 while (optind < argc) {
7285 path = argv[optind];
7287 /* If mode == 0, this means we apply the mask. */
7288 if (mode == LFS_HSM_SET)
7289 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7291 rc = llapi_hsm_state_set(path, 0, mask, 0);
7294 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7295 path, strerror(errno = -rc));
7304 static int lfs_hsm_action(int argc, char **argv)
7309 struct hsm_current_action hca;
7310 struct hsm_extent he;
7311 enum hsm_user_action hua;
7312 enum hsm_progress_states hps;
7320 rc = llapi_hsm_current_action(path, &hca);
7322 fprintf(stderr, "can't get hsm action for %s: %s\n",
7323 path, strerror(errno = -rc));
7326 he = hca.hca_location;
7327 hua = hca.hca_action;
7328 hps = hca.hca_state;
7330 printf("%s: %s", path, hsm_user_action2name(hua));
7332 /* Skip file without action */
7333 if (hca.hca_action == HUA_NONE) {
7338 printf(" %s ", hsm_progress_state2name(hps));
7340 if ((hps == HPS_RUNNING) &&
7341 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7342 printf("(%llu bytes moved)\n",
7343 (unsigned long long)he.length);
7344 else if ((he.offset + he.length) == LUSTRE_EOF)
7345 printf("(from %llu to EOF)\n",
7346 (unsigned long long)he.offset);
7348 printf("(from %llu to %llu)\n",
7349 (unsigned long long)he.offset,
7350 (unsigned long long)(he.offset + he.length));
7352 } while (++i < argc);
7357 static int lfs_hsm_set(int argc, char **argv)
7359 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7362 static int lfs_hsm_clear(int argc, char **argv)
7364 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7368 * Check file state and return its fid, to be used by lfs_hsm_request().
7370 * \param[in] file Path to file to check
7371 * \param[in,out] fid Pointer to allocated lu_fid struct.
7372 * \param[in,out] last_dev Pointer to last device id used.
7374 * \return 0 on success.
7376 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7382 rc = lstat(file, &st);
7384 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7387 /* Checking for regular file as archiving as posix copytool
7388 * rejects archiving files other than regular files
7390 if (!S_ISREG(st.st_mode)) {
7391 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7394 /* A request should be ... */
7395 if (*last_dev != st.st_dev && *last_dev != 0) {
7396 fprintf(stderr, "All files should be "
7397 "on the same filesystem: %s\n", file);
7400 *last_dev = st.st_dev;
7402 rc = llapi_path2fid(file, fid);
7404 fprintf(stderr, "Cannot read FID of %s: %s\n",
7405 file, strerror(-rc));
7411 /* Fill an HSM HUR item with a given file name.
7413 * If mntpath is set, then the filename is actually a FID, and no
7414 * lookup on the filesystem will be performed.
7416 * \param[in] hur the user request to fill
7417 * \param[in] idx index of the item inside the HUR to fill
7418 * \param[in] mntpath mountpoint of Lustre
7419 * \param[in] fname filename (if mtnpath is NULL)
7420 * or FID (if mntpath is set)
7421 * \param[in] last_dev pointer to last device id used
7423 * \retval 0 on success
7424 * \retval CMD_HELP or a negative errno on error
7426 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7427 const char *mntpath, const char *fname,
7430 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7433 hui->hui_extent.length = -1;
7435 if (mntpath != NULL) {
7438 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7442 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7447 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7451 hur->hur_request.hr_itemcount++;
7456 static int lfs_hsm_request(int argc, char **argv, int action)
7458 struct option long_opts[] = {
7459 { .val = 'a', .name = "archive", .has_arg = required_argument },
7460 { .val = 'D', .name = "data", .has_arg = required_argument },
7461 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7462 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7465 char short_opts[] = "l:D:a:m:";
7466 struct hsm_user_request *hur, *oldhur;
7471 char *filelist = NULL;
7472 char fullpath[PATH_MAX];
7473 char *opaque = NULL;
7477 int nbfile_alloc = 0;
7478 char *some_file = NULL;
7479 char *mntpath = NULL;
7485 while ((c = getopt_long(argc, argv, short_opts,
7486 long_opts, NULL)) != -1) {
7495 if (action != HUA_ARCHIVE &&
7496 action != HUA_REMOVE) {
7498 "error: -a is supported only "
7499 "when archiving or removing\n");
7502 archive_id = atoi(optarg);
7505 if (some_file == NULL) {
7507 some_file = strdup(optarg);
7513 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7514 argv[0], argv[optind - 1]);
7519 /* All remaining args are files, so we have at least nbfile */
7520 nbfile = argc - optind;
7522 if ((nbfile == 0) && (filelist == NULL))
7526 opaque_len = strlen(opaque);
7528 /* Alloc the request structure with enough place to store all files
7529 * from command line. */
7530 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7532 fprintf(stderr, "Cannot create the request: %s\n",
7536 nbfile_alloc = nbfile;
7538 hur->hur_request.hr_action = action;
7539 hur->hur_request.hr_archive_id = archive_id;
7540 hur->hur_request.hr_flags = 0;
7542 /* All remaining args are files, add them */
7543 if (nbfile != 0 && some_file == NULL)
7544 some_file = strdup(argv[optind]);
7546 for (i = 0; i < nbfile; i++) {
7547 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7553 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7555 /* If a filelist was specified, read the filelist from it. */
7556 if (filelist != NULL) {
7557 fp = fopen(filelist, "r");
7559 fprintf(stderr, "Cannot read the file list %s: %s\n",
7560 filelist, strerror(errno));
7565 while ((rc = getline(&line, &len, fp)) != -1) {
7566 /* If allocated buffer was too small, get something
7568 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7571 nbfile_alloc = nbfile_alloc * 2 + 1;
7573 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7576 fprintf(stderr, "hsm: cannot allocate "
7577 "the request: %s\n",
7584 size = hur_len(oldhur);
7586 fprintf(stderr, "hsm: cannot allocate "
7587 "%u files + %u bytes data\n",
7588 oldhur->hur_request.hr_itemcount,
7589 oldhur->hur_request.hr_data_len);
7596 memcpy(hur, oldhur, size);
7601 if (line[strlen(line) - 1] == '\n')
7602 line[strlen(line) - 1] = '\0';
7604 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7605 mntpath, line, &last_dev);
7611 if (some_file == NULL) {
7621 /* If a --data was used, add it to the request */
7622 hur->hur_request.hr_data_len = opaque_len;
7624 memcpy(hur_data(hur), opaque, opaque_len);
7626 /* Send the HSM request */
7627 if (realpath(some_file, fullpath) == NULL) {
7628 fprintf(stderr, "Could not find path '%s': %s\n",
7629 some_file, strerror(errno));
7631 rc = llapi_hsm_request(fullpath, hur);
7633 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7634 some_file, strerror(-rc));
7644 static int lfs_hsm_archive(int argc, char **argv)
7646 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7649 static int lfs_hsm_restore(int argc, char **argv)
7651 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7654 static int lfs_hsm_release(int argc, char **argv)
7656 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7659 static int lfs_hsm_remove(int argc, char **argv)
7661 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7664 static int lfs_hsm_cancel(int argc, char **argv)
7666 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7669 static int lfs_swap_layouts(int argc, char **argv)
7674 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7675 SWAP_LAYOUTS_KEEP_MTIME |
7676 SWAP_LAYOUTS_KEEP_ATIME);
7679 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7681 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7683 int lfs_get_mode(const char *string)
7685 enum lock_mode_user mode;
7687 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7688 if (lock_mode_names[mode] == NULL)
7690 if (strcmp(string, lock_mode_names[mode]) == 0)
7697 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7699 enum lu_ladvise_type advice;
7702 advice < ARRAY_SIZE(ladvise_names); advice++) {
7703 if (ladvise_names[advice] == NULL)
7705 if (strcmp(string, ladvise_names[advice]) == 0)
7709 return LU_LADVISE_INVALID;
7712 static int lfs_ladvise(int argc, char **argv)
7714 struct option long_opts[] = {
7715 { .val = 'a', .name = "advice", .has_arg = required_argument },
7716 { .val = 'b', .name = "background", .has_arg = no_argument },
7717 { .val = 'e', .name = "end", .has_arg = required_argument },
7718 { .val = 'l', .name = "length", .has_arg = required_argument },
7719 { .val = 'm', .name = "mode", .has_arg = required_argument },
7720 { .val = 's', .name = "start", .has_arg = required_argument },
7721 { .val = 'u', .name = "unset", .has_arg = no_argument },
7723 char short_opts[] = "a:be:l:m:s:u";
7728 struct llapi_lu_ladvise advice;
7729 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7730 unsigned long long start = 0;
7731 unsigned long long end = LUSTRE_EOF;
7732 unsigned long long length = 0;
7733 unsigned long long size_units;
7734 unsigned long long flags = 0;
7738 while ((c = getopt_long(argc, argv, short_opts,
7739 long_opts, NULL)) != -1) {
7742 advice_type = lfs_get_ladvice(optarg);
7743 if (advice_type == LU_LADVISE_INVALID) {
7744 fprintf(stderr, "%s: invalid advice type "
7745 "'%s'\n", argv[0], optarg);
7746 fprintf(stderr, "Valid types:");
7748 for (advice_type = 0;
7749 advice_type < ARRAY_SIZE(ladvise_names);
7751 if (ladvise_names[advice_type] == NULL)
7753 fprintf(stderr, " %s",
7754 ladvise_names[advice_type]);
7756 fprintf(stderr, "\n");
7769 rc = llapi_parse_size(optarg, &end,
7772 fprintf(stderr, "%s: bad end offset '%s'\n",
7779 rc = llapi_parse_size(optarg, &start,
7782 fprintf(stderr, "%s: bad start offset "
7783 "'%s'\n", argv[0], optarg);
7789 rc = llapi_parse_size(optarg, &length,
7792 fprintf(stderr, "%s: bad length '%s'\n",
7798 mode = lfs_get_mode(optarg);
7800 fprintf(stderr, "%s: bad mode '%s', valid "
7801 "modes are READ or WRITE\n",
7809 fprintf(stderr, "%s: option '%s' unrecognized\n",
7810 argv[0], argv[optind - 1]);
7815 if (advice_type == LU_LADVISE_INVALID) {
7816 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7817 fprintf(stderr, "Valid types:");
7818 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7820 if (ladvise_names[advice_type] == NULL)
7822 fprintf(stderr, " %s", ladvise_names[advice_type]);
7824 fprintf(stderr, "\n");
7828 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7829 fprintf(stderr, "%s: Lock no expand advice is a per file "
7830 "descriptor advice, so when called from lfs, "
7831 "it does nothing.\n", argv[0]);
7835 if (argc <= optind) {
7836 fprintf(stderr, "%s: please give one or more file names\n",
7841 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7842 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7847 if (end == LUSTRE_EOF && length != 0)
7848 end = start + length;
7851 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7852 argv[0], start, end);
7856 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7857 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7862 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7863 fprintf(stderr, "%s: mode is required with lockahead\n",
7868 while (optind < argc) {
7871 path = argv[optind++];
7873 fd = open(path, O_RDONLY);
7875 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7876 argv[0], path, strerror(errno));
7881 advice.lla_start = start;
7882 advice.lla_end = end;
7883 advice.lla_advice = advice_type;
7884 advice.lla_value1 = 0;
7885 advice.lla_value2 = 0;
7886 advice.lla_value3 = 0;
7887 advice.lla_value4 = 0;
7888 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7889 advice.lla_lockahead_mode = mode;
7890 advice.lla_peradvice_flags = flags;
7893 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7896 fprintf(stderr, "%s: cannot give advice '%s' to file "
7897 "'%s': %s\n", argv[0],
7898 ladvise_names[advice_type],
7899 path, strerror(errno));
7905 if (rc == 0 && rc2 < 0)
7912 static const char *const heat_names[] = LU_HEAT_NAMES;
7914 static int lfs_heat_get(int argc, char **argv)
7916 struct lu_heat *heat;
7925 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
7927 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
7932 while (optind < argc) {
7933 path = argv[optind++];
7935 fd = open(path, O_RDONLY);
7937 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7938 argv[0], path, strerror(errno));
7943 heat->lh_count = OBD_HEAT_COUNT;
7944 rc2 = llapi_heat_get(fd, heat);
7947 fprintf(stderr, "%s: cannot get heat of file '%s'"
7948 ": %s\n", argv[0], path, strerror(errno));
7952 printf("flags: %x\n", heat->lh_flags);
7953 for (i = 0; i < heat->lh_count; i++)
7954 printf("%s: %llu\n", heat_names[i], heat->lh_heat[i]);
7956 if (rc == 0 && rc2 < 0)
7964 static int lfs_heat_set(int argc, char **argv)
7966 struct option long_opts[] = {
7967 {"clear", no_argument, 0, 'c'},
7968 {"off", no_argument, 0, 'o'},
7969 {"on", no_argument, 0, 'O'},
7972 char short_opts[] = "coO";
7983 while ((c = getopt_long(argc, argv, short_opts,
7984 long_opts, NULL)) != -1) {
7987 flags |= LU_HEAT_FLAG_CLEAR;
7990 flags |= LU_HEAT_FLAG_CLEAR;
7991 flags |= LU_HEAT_FLAG_OFF;
7994 flags &= ~LU_HEAT_FLAG_OFF;
7999 fprintf(stderr, "%s: option '%s' unrecognized\n",
8000 argv[0], argv[optind - 1]);
8005 if (argc <= optind) {
8006 fprintf(stderr, "%s: please give one or more file names\n",
8011 while (optind < argc) {
8012 path = argv[optind++];
8014 fd = open(path, O_RDONLY);
8016 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8017 argv[0], path, strerror(errno));
8022 rc2 = llapi_heat_set(fd, flags);
8025 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8026 ": %s\n", argv[0], path, strerror(errno));
8030 if (rc == 0 && rc2 < 0)
8036 /** The input string contains a comma delimited list of component ids and
8037 * ranges, for example "1,2-4,7".
8039 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8041 bool end_of_loop = false;
8049 while (!end_of_loop) {
8053 char *endptr = NULL;
8056 ptr = strchrnul(arg, ',');
8057 end_of_loop = *ptr == '\0';
8060 start_index = strtol(arg, &endptr, 0);
8061 if (endptr == arg) /* no data at all */
8063 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8065 if (start_index < 0)
8068 end_index = start_index;
8069 if (*endptr == '-') {
8070 end_index = strtol(endptr + 1, &endptr, 0);
8071 if (*endptr != '\0')
8073 if (end_index < start_index)
8077 for (i = start_index; i <= end_index && size > 0; i++) {
8080 /* remove duplicate */
8081 for (j = 0; j < nr; j++) {
8085 if (j == nr) { /* no duplicate */
8091 if (size == 0 && i < end_index)
8098 if (!end_of_loop && ptr != NULL)
8101 return rc < 0 ? rc : nr;
8105 * struct verify_mirror_id - Mirror id to be verified.
8106 * @mirror_id: A specified mirror id.
8107 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
8109 struct verify_mirror_id {
8115 * compare_mirror_ids() - Compare mirror ids.
8116 * @layout: Mirror component list.
8117 * @cbdata: Callback data in verify_mirror_id structure.
8119 * This is a callback function called by llapi_layout_comp_iterate()
8120 * to compare the specified mirror id with the one in the current
8121 * component of @layout. If they are the same, then the specified
8122 * mirror id is valid.
8124 * Return: a negative error code on failure or
8125 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
8126 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
8129 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
8131 struct verify_mirror_id *mirror_id_cbdata =
8132 (struct verify_mirror_id *)cbdata;
8136 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8140 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8141 progname, strerror(errno));
8145 if (mirror_id_cbdata->mirror_id == mirror_id) {
8146 mirror_id_cbdata->is_valid_id = true;
8147 return LLAPI_LAYOUT_ITER_STOP;
8150 return LLAPI_LAYOUT_ITER_CONT;
8154 * verify_mirror_ids() - Verify specified mirror ids.
8155 * @fname: Mirrored file name.
8156 * @mirror_ids: Specified mirror ids to be verified.
8157 * @ids_nr: Number of specified mirror ids.
8159 * This function verifies that specified @mirror_ids are valid
8160 * in the mirrored file @fname.
8162 * Return: 0 on success or a negative error code on failure.
8165 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8167 struct llapi_layout *layout = NULL;
8168 struct verify_mirror_id mirror_id_cbdata = { 0 };
8179 if (stat(fname, &stbuf) < 0) {
8180 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8181 progname, fname, strerror(errno));
8186 if (!S_ISREG(stbuf.st_mode)) {
8187 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8193 fd = open(fname, O_DIRECT | O_RDONLY);
8195 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8196 progname, fname, strerror(errno));
8201 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8203 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8204 progname, fname, strerror(errno));
8208 layout = llapi_layout_get_by_fd(fd, 0);
8209 if (layout == NULL) {
8210 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8211 progname, fname, strerror(errno));
8213 llapi_lease_release(fd);
8217 rc = llapi_layout_flags_get(layout, &flr_state);
8219 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8220 progname, fname, strerror(errno));
8225 flr_state &= LCM_FL_FLR_MASK;
8226 switch (flr_state) {
8229 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8230 progname, fname, llapi_layout_flags_string(flr_state));
8237 for (i = 0; i < ids_nr; i++) {
8238 mirror_id_cbdata.mirror_id = mirror_ids[i];
8239 mirror_id_cbdata.is_valid_id = false;
8241 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8246 "%s: '%s' failed to verify mirror id: %u.\n",
8247 progname, fname, mirror_ids[i]);
8251 if (!mirror_id_cbdata.is_valid_id) {
8254 "%s: '%s' invalid specified mirror id: %u.\n",
8255 progname, fname, mirror_ids[i]);
8261 llapi_layout_free(layout);
8262 llapi_lease_release(fd);
8270 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8271 __u16 *mirror_ids, int ids_nr)
8273 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8274 struct llapi_layout *layout;
8284 if (stat(fname, &stbuf) < 0) {
8285 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8286 progname, fname, strerror(errno));
8290 if (!S_ISREG(stbuf.st_mode)) {
8291 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8297 fd = open(fname, O_DIRECT | O_RDWR);
8299 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8300 progname, fname, strerror(errno));
8305 layout = llapi_layout_get_by_fd(fd, 0);
8306 if (layout == NULL) {
8307 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8308 progname, fname, strerror(errno));
8313 rc = llapi_layout_flags_get(layout, &flr_state);
8315 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8316 progname, fname, strerror(errno));
8321 flr_state &= LCM_FL_FLR_MASK;
8322 if (flr_state == LCM_FL_NONE) {
8324 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8329 /* get stale component info */
8330 comp_size = llapi_mirror_find_stale(layout, comp_array,
8331 ARRAY_SIZE(comp_array),
8332 mirror_ids, ids_nr);
8333 if (comp_size <= 0) {
8338 ioc->lil_mode = LL_LEASE_WRLCK;
8339 ioc->lil_flags = LL_LEASE_RESYNC;
8340 rc = llapi_lease_set(fd, ioc);
8342 if (rc == -EALREADY)
8346 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8347 progname, fname, strerror(errno));
8351 /* get the read range [start, end) */
8352 start = comp_array[0].lrc_start;
8353 end = comp_array[0].lrc_end;
8354 for (idx = 1; idx < comp_size; idx++) {
8355 if (comp_array[idx].lrc_start < start)
8356 start = comp_array[idx].lrc_start;
8357 if (end < comp_array[idx].lrc_end)
8358 end = comp_array[idx].lrc_end;
8361 rc = llapi_lease_check(fd);
8362 if (rc != LL_LEASE_WRLCK) {
8363 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8368 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8371 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8372 progname, fname, rc);
8374 /* prepare ioc for lease put */
8375 ioc->lil_mode = LL_LEASE_UNLCK;
8376 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8378 for (idx = 0; idx < comp_size; idx++) {
8379 if (comp_array[idx].lrc_synced) {
8380 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8385 rc = llapi_lease_set(fd, ioc);
8387 if (rc == 0) /* lost lease lock */
8389 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8390 progname, fname, strerror(errno));
8394 * llapi_lease_set returns lease mode when it request to unlock
8400 llapi_layout_free(layout);
8407 static inline int lfs_mirror_resync(int argc, char **argv)
8409 struct ll_ioc_lease *ioc = NULL;
8410 __u16 mirror_ids[128] = { 0 };
8415 struct option long_opts[] = {
8416 { .val = 'o', .name = "only", .has_arg = required_argument },
8419 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8422 rc = parse_mirror_ids(mirror_ids,
8423 sizeof(mirror_ids) / sizeof(__u16),
8427 "%s: bad mirror ids '%s'.\n",
8434 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8435 argv[0], argv[optind - 1]);
8441 if (argc == optind) {
8442 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8447 if (ids_nr > 0 && argc > optind + 1) {
8449 "%s: option '--only' cannot be used upon multiple files.\n",
8457 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8462 /* set the lease on the file */
8463 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8465 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8466 argv[0], strerror(errno));
8471 for (; optind < argc; optind++) {
8472 rc = lfs_mirror_resync_file(argv[optind], ioc,
8473 mirror_ids, ids_nr);
8474 /* ignore previous file's error, continue with next file */
8477 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8485 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8487 struct llapi_layout *layout;
8490 layout = llapi_layout_get_by_fd(fd, 0);
8491 if (layout == NULL) {
8492 fprintf(stderr, "could not get layout.\n");
8496 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8498 fprintf(stderr, "failed to iterate layout\n");
8499 llapi_layout_free(layout);
8502 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8503 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8504 llapi_layout_free(layout);
8508 llapi_layout_free(layout);
8514 * Check whether two files are the same file
8515 * \retval 0 same file
8516 * \retval 1 not the same file
8517 * \retval <0 error code
8519 static inline int check_same_file(const char *f1, const char *f2)
8524 if (stat(f1, &stbuf1) < 0) {
8525 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8526 progname, f1, strerror(errno));
8530 if (stat(f2, &stbuf2) < 0) {
8531 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8532 progname, f2, strerror(errno));
8536 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8537 stbuf1.st_ino == stbuf2.st_ino)
8543 static inline int lfs_mirror_read(int argc, char **argv)
8546 __u16 mirror_id = 0;
8547 const char *outfile = NULL;
8553 const size_t buflen = 4 << 20;
8555 struct option long_opts[] = {
8556 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8557 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8560 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8565 mirror_id = strtoul(optarg, &end, 0);
8566 if (*end != '\0' || mirror_id == 0) {
8568 "%s %s: invalid mirror ID '%s'\n",
8569 progname, argv[0], optarg);
8577 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8578 progname, argv[optind - 1]);
8583 if (argc == optind) {
8584 fprintf(stderr, "%s %s: no mirrored file provided\n",
8587 } else if (argc > optind + 1) {
8588 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8592 if (mirror_id == 0) {
8593 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8598 /* open mirror file */
8599 fname = argv[optind];
8602 rc = check_same_file(fname, outfile);
8605 "%s %s: output file cannot be the mirrored file\n",
8613 fd = open(fname, O_DIRECT | O_RDONLY);
8615 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8616 progname, argv[0], fname, strerror(errno));
8620 /* verify mirror id */
8621 rc = verify_mirror_id_by_fd(fd, mirror_id);
8624 "%s %s: cannot find mirror with ID %u in '%s'\n",
8625 progname, argv[0], mirror_id, fname);
8629 /* open output file */
8631 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8633 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8634 progname, argv[0], outfile, strerror(errno));
8639 outfd = STDOUT_FILENO;
8642 /* allocate buffer */
8643 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8645 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8646 progname, argv[0], rc);
8653 ssize_t written = 0;
8655 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8656 if (bytes_read < 0) {
8659 "%s %s: fail to read data from mirror %u: %s\n",
8660 progname, argv[0], mirror_id, strerror(-rc));
8665 if (bytes_read == 0)
8668 while (written < bytes_read) {
8671 written2 = write(outfd, buf + written,
8672 bytes_read - written);
8675 "%s %s: fail to write %s: %s\n",
8676 progname, argv[0], outfile ? : "STDOUT",
8681 written += written2;
8684 if (written != bytes_read) {
8686 "%s %s: written %ld bytes does not match with %ld read.\n",
8687 progname, argv[0], written, bytes_read);
8709 static inline int lfs_mirror_write(int argc, char **argv)
8712 __u16 mirror_id = 0;
8713 const char *inputfile = NULL;
8719 const size_t buflen = 4 << 20;
8721 size_t page_size = sysconf(_SC_PAGESIZE);
8722 struct ll_ioc_lease_id ioc;
8724 struct option long_opts[] = {
8725 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8726 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8729 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8734 mirror_id = strtoul(optarg, &end, 0);
8735 if (*end != '\0' || mirror_id == 0) {
8737 "%s %s: invalid mirror ID '%s'\n",
8738 progname, argv[0], optarg);
8746 fprintf(stderr, "%s: option '%s' unrecognized\n",
8747 progname, argv[optind - 1]);
8752 if (argc == optind) {
8753 fprintf(stderr, "%s %s: no mirrored file provided\n",
8756 } else if (argc > optind + 1) {
8757 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8761 if (mirror_id == 0) {
8762 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8767 /* open mirror file */
8768 fname = argv[optind];
8771 rc = check_same_file(fname, inputfile);
8774 "%s %s: input file cannot be the mirrored file\n",
8782 fd = open(fname, O_DIRECT | O_WRONLY);
8784 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8785 progname, argv[0], fname, strerror(errno));
8789 /* verify mirror id */
8790 rc = verify_mirror_id_by_fd(fd, mirror_id);
8793 "%s %s: cannot find mirror with ID %u in '%s'\n",
8794 progname, argv[0], mirror_id, fname);
8798 /* open input file */
8800 inputfd = open(inputfile, O_RDONLY, 0644);
8802 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
8803 progname, argv[0], inputfile, strerror(errno));
8808 inputfd = STDIN_FILENO;
8811 /* allocate buffer */
8812 rc = posix_memalign(&buf, page_size, buflen);
8814 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8815 progname, argv[0], rc);
8819 /* prepare target mirror components instantiation */
8820 ioc.lil_mode = LL_LEASE_WRLCK;
8821 ioc.lil_flags = LL_LEASE_RESYNC;
8822 ioc.lil_mirror_id = mirror_id;
8823 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8826 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8827 progname, argv[0], fname, strerror(errno));
8837 rc = llapi_lease_check(fd);
8838 if (rc != LL_LEASE_WRLCK) {
8839 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
8840 progname, argv[0], fname);
8844 bytes_read = read(inputfd, buf, buflen);
8845 if (bytes_read < 0) {
8848 "%s %s: fail to read data from '%s': %s\n",
8849 progname, argv[0], inputfile ? : "STDIN",
8856 if (bytes_read == 0)
8859 /* round up to page align to make direct IO happy. */
8860 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
8862 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
8867 "%s %s: fail to write to mirror %u: %s\n",
8868 progname, argv[0], mirror_id,
8876 if (pos & (page_size - 1)) {
8877 rc = llapi_mirror_truncate(fd, mirror_id, pos);
8882 ioc.lil_mode = LL_LEASE_UNLCK;
8883 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
8885 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8890 "%s %s: release lease lock of '%s' failed: %s\n",
8891 progname, argv[0], fname, strerror(errno));
8908 struct collect_ids_data {
8914 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
8916 struct collect_ids_data *cid = cbdata;
8920 rc = llapi_layout_mirror_id_get(layout, &id);
8924 if ((__u16)id != cid->cid_exclude) {
8927 for (i = 0; i < cid->cid_count; i++) {
8928 /* already collected the mirror id */
8929 if (id == cid->cid_ids[i])
8930 return LLAPI_LAYOUT_ITER_CONT;
8932 cid->cid_ids[cid->cid_count] = id;
8936 return LLAPI_LAYOUT_ITER_CONT;
8939 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
8941 struct llapi_layout *layout;
8942 struct collect_ids_data cid = { .cid_ids = ids,
8944 .cid_exclude = exclude_id, };
8947 layout = llapi_layout_get_by_fd(fd, 0);
8948 if (layout == NULL) {
8949 fprintf(stderr, "could not get layout\n");
8953 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
8955 fprintf(stderr, "failed to iterate layout\n");
8956 llapi_layout_free(layout);
8960 llapi_layout_free(layout);
8962 return cid.cid_count;
8965 static inline int lfs_mirror_copy(int argc, char **argv)
8968 __u16 read_mirror_id = 0;
8969 __u16 ids[128] = { 0 };
8971 struct llapi_layout *layout = NULL;
8972 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8979 struct ll_ioc_lease *ioc = NULL;
8980 struct ll_ioc_lease_id *resync_ioc;
8982 struct option long_opts[] = {
8983 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
8984 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
8987 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
8992 read_mirror_id = strtoul(optarg, &end, 0);
8993 if (*end != '\0' || read_mirror_id == 0) {
8995 "%s %s: invalid read mirror ID '%s'\n",
8996 progname, argv[0], optarg);
9001 if (!strcmp(optarg, "-1")) {
9002 /* specify all other mirrors */
9006 count = parse_mirror_ids((__u16 *)ids,
9014 fprintf(stderr, "%s: option '%s' unrecognized\n",
9015 progname, argv[optind - 1]);
9020 if (argc == optind) {
9021 fprintf(stderr, "%s %s: no mirrored file provided\n",
9024 } else if (argc > optind + 1) {
9025 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9029 if (read_mirror_id == 0) {
9031 "%s %s: no valid read mirror ID %d is provided\n",
9032 progname, argv[0], read_mirror_id);
9038 "%s %s: no write mirror ID is provided\n",
9043 for (i = 0; i < count; i++) {
9044 if (read_mirror_id == ids[i]) {
9046 "%s %s: read and write mirror ID cannot be the same\n",
9052 /* open mirror file */
9053 fname = argv[optind];
9055 fd = open(fname, O_DIRECT | O_RDWR);
9057 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9058 progname, argv[0], fname, strerror(errno));
9062 /* write to all other mirrors */
9063 if (ids[0] == (__u16)-1) {
9064 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9068 "%s %s: failed to get other mirror ids in '%s': %d\n",
9069 progname, argv[0], fname, rc);
9074 /* verify mirror id */
9075 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9078 "%s %s: cannot find mirror with ID %u in '%s'\n",
9079 progname, argv[0], read_mirror_id, fname);
9083 for (i = 0; i < count; i++) {
9084 rc = verify_mirror_id_by_fd(fd, ids[i]);
9087 "%s %s: cannot find mirror with ID %u in '%s'\n",
9088 progname, argv[0], ids[i], fname);
9093 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9096 "%s %s: cannot alloc comp id array for ioc: %s\n",
9097 progname, argv[0], strerror(errno));
9102 /* get stale component info */
9103 layout = llapi_layout_get_by_fd(fd, 0);
9104 if (layout == NULL) {
9105 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
9106 progname, argv[0], fname, strerror(errno));
9110 comp_size = llapi_mirror_find_stale(layout, comp_array,
9111 ARRAY_SIZE(comp_array),
9113 llapi_layout_free(layout);
9114 if (comp_size < 0) {
9119 /* prepare target mirror components instantiation */
9120 resync_ioc = (struct ll_ioc_lease_id *)ioc;
9121 resync_ioc->lil_mode = LL_LEASE_WRLCK;
9122 resync_ioc->lil_flags = LL_LEASE_RESYNC;
9124 resync_ioc->lil_mirror_id = ids[0];
9126 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
9127 rc = llapi_lease_set(fd, ioc);
9130 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9131 progname, argv[0], fname, strerror(errno));
9135 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9138 fprintf(stderr, "%s %s: copy error: %d\n",
9139 progname, argv[0], rc);
9143 fprintf(stdout, "mirror copied successfully: ");
9144 for (i = 0; i < copied; i++)
9145 fprintf(stdout, "%d ", ids[i]);
9146 fprintf(stdout, "\n");
9148 ioc->lil_mode = LL_LEASE_UNLCK;
9149 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9151 for (i = 0; i < comp_size; i++) {
9154 for (j = 0; j < copied; j++) {
9155 if (comp_array[i].lrc_mirror_id != ids[j])
9158 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9162 rc = llapi_lease_set(fd, ioc);
9167 "%s %s: release lease lock of '%s' failed: %s\n",
9168 progname, argv[0], fname, strerror(errno));
9182 * struct verify_chunk - Mirror chunk to be verified.
9183 * @chunk: [start, end) of the chunk.
9184 * @mirror_count: Number of mirror ids in @mirror_id array.
9185 * @mirror_id: Array of valid mirror ids that cover the chunk.
9187 struct verify_chunk {
9188 struct lu_extent chunk;
9189 unsigned int mirror_count;
9190 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9194 * print_chunks() - Print chunk information.
9195 * @fname: Mirrored file name.
9196 * @chunks: Array of chunks.
9197 * @chunk_count: Number of chunks in @chunks array.
9199 * This function prints [start, end) of each chunk in @chunks
9200 * for mirrored file @fname, and also prints the valid mirror ids
9201 * that cover the chunk.
9206 void print_chunks(const char *fname, struct verify_chunk *chunks,
9212 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9213 for (i = 0; i < chunk_count; i++) {
9214 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9216 if (chunks[i].mirror_count == 0)
9217 fprintf(stdout, "\t[");
9219 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9220 for (j = 1; j < chunks[i].mirror_count; j++)
9221 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9223 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9225 fprintf(stdout, "\n");
9229 * print_checksums() - Print CRC-32 checksum values.
9230 * @chunk: A chunk and its corresponding valid mirror ids.
9231 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9233 * This function prints CRC-32 checksum values on @chunk for
9234 * each valid mirror that covers it.
9239 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9244 "CRC-32 checksum value for chunk "DEXT":\n",
9245 PEXT(&chunk->chunk));
9246 for (i = 0; i < chunk->mirror_count; i++)
9247 fprintf(stdout, "Mirror %u:\t%#lx\n",
9248 chunk->mirror_id[i], crc[i]);
9249 fprintf(stdout, "\n");
9253 * filter_mirror_id() - Filter specified mirror ids.
9254 * @chunks: Array of chunks.
9255 * @chunk_count: Number of chunks in @chunks array.
9256 * @mirror_ids: Specified mirror ids to be verified.
9257 * @ids_nr: Number of specified mirror ids.
9259 * This function scans valid mirror ids that cover each chunk in @chunks
9260 * and filters specified mirror ids.
9265 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9266 __u16 *mirror_ids, int ids_nr)
9271 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9272 unsigned int valid_count = 0;
9274 for (i = 0; i < chunk_count; i++) {
9275 if (chunks[i].mirror_count == 0)
9279 for (j = 0; j < ids_nr; j++) {
9280 for (k = 0; k < chunks[i].mirror_count; k++) {
9281 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9282 valid_id[valid_count] = mirror_ids[j];
9289 memcpy(chunks[i].mirror_id, valid_id,
9290 sizeof(__u16) * valid_count);
9291 chunks[i].mirror_count = valid_count;
9296 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9297 * @layout: Mirror component list.
9298 * @chunks: Array of chunks.
9299 * @chunks_size: Array size of @chunks.
9301 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9302 * to find out chunk segments and store them in @chunks array.
9304 * The @mirror_id array in each element of @chunks will store the valid
9305 * mirror ids that cover the chunk. If a mirror component covering the
9306 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9307 * will not be stored into the @mirror_id array, and the chunk for that
9308 * mirror will not be verified.
9310 * The @mirror_count in each element of @chunks will store the number of
9311 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9312 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9313 * indicates the chunk is valid in only one mirror. In both cases, the
9314 * chunk will not be verified.
9316 * Here is an example:
9319 * +------+-------------+--------------------+
9321 * +------+------+------+------+-------------+
9322 * | | S | S | | mirror2
9323 * +-------------+------+------+-------------+
9325 * prepared @chunks array will contain 5 elements:
9326 * (([0, 1M), [1, 2], 2),
9327 * ([1M, 2M), [1, 2], 2),
9328 * ([2M, 3M), [1], 1),
9329 * ([3M, 4M], [], 0),
9330 * ([4M, EOF), [2], 1))
9332 * Return: the actual array size of @chunks on success
9333 * or a negative error code on failure.
9336 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9337 struct verify_chunk *chunks,
9348 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9351 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9354 "%s: move to the first layout component: %s.\n",
9355 progname, strerror(errno));
9361 chunks[idx].chunk.e_end = LUSTRE_EOF;
9363 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9366 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9367 progname, strerror(errno));
9371 if (start > chunks[idx].chunk.e_start ||
9372 end <= chunks[idx].chunk.e_start)
9375 if (end < chunks[idx].chunk.e_end)
9376 chunks[idx].chunk.e_end = end;
9378 rc = llapi_layout_comp_flags_get(layout, &flags);
9381 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9382 progname, strerror(errno));
9386 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9389 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9392 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9393 progname, strerror(errno));
9397 chunks[idx].mirror_id[i] = mirror_id;
9399 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9401 "%s: mirror_id array is too small.\n",
9408 rc = llapi_layout_comp_use(layout,
9409 LLAPI_LAYOUT_COMP_USE_NEXT);
9412 "%s: move to the next layout component: %s.\n",
9413 progname, strerror(errno));
9416 } /* loop through all components */
9418 chunks[idx].mirror_count = i;
9420 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9424 if (idx >= chunks_size) {
9425 fprintf(stderr, "%s: chunks array is too small.\n",
9431 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9435 return rc < 0 ? rc : idx + 1;
9439 * lfs_mirror_verify_chunk() - Verify a chunk.
9440 * @fd: File descriptor of the mirrored file.
9441 * @file_size: Size of the mirrored file.
9442 * @chunk: A chunk and its corresponding valid mirror ids.
9443 * @verbose: Verbose mode.
9445 * This function verifies a @chunk contains exactly the same data
9446 * ammong the mirrors that cover it.
9448 * If @verbose is specified, then the function will print where the
9449 * differences are if the data do not match. Otherwise, it will
9450 * just return an error in that case.
9452 * Return: 0 on success or a negative error code on failure.
9455 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9456 struct verify_chunk *chunk, int verbose)
9458 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9460 size_t page_size = sysconf(_SC_PAGESIZE);
9466 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9473 rc = posix_memalign(&buf, page_size, buflen);
9474 if (rc) /* error code is returned directly */
9478 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9479 PEXT(&chunk->chunk));
9480 for (i = 0; i < chunk->mirror_count; i++)
9481 fprintf(stdout, " %u", chunk->mirror_id[i]);
9482 fprintf(stdout, "\n");
9486 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9487 pos = chunk->chunk.e_start;
9488 while (bytes_done < count) {
9489 /* compute initial CRC-32 checksum */
9490 crc = crc32(0L, Z_NULL, 0);
9491 memset(crc_array, 0, sizeof(crc_array));
9494 for (i = 0; i < chunk->mirror_count; i++) {
9495 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9497 if (bytes_read < 0) {
9500 "%s: failed to read data from mirror %u: %s.\n",
9501 progname, chunk->mirror_id[i],
9506 /* compute new CRC-32 checksum */
9507 crc_array[i] = crc32(crc, buf, bytes_read);
9511 print_checksums(chunk, crc_array);
9513 /* compare CRC-32 checksum values */
9514 for (i = 1; i < chunk->mirror_count; i++) {
9515 if (crc_array[i] != crc_array[0]) {
9519 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9520 progname, PEXT(&chunk->chunk),
9521 chunk->mirror_id[0],
9522 chunk->mirror_id[i]);
9527 bytes_done += bytes_read;
9530 if (verbose > 1 && rc == 0) {
9531 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9532 PEXT(&chunk->chunk));
9533 for (i = 0; i < chunk->mirror_count; i++)
9534 fprintf(stdout, " %u", chunk->mirror_id[i]);
9535 fprintf(stdout, " PASS\n\n");
9544 * lfs_mirror_verify_file() - Verify a mirrored file.
9545 * @fname: Mirrored file name.
9546 * @mirror_ids: Specified mirror ids to be verified.
9547 * @ids_nr: Number of specified mirror ids.
9548 * @verbose: Verbose mode.
9550 * This function verifies that each SYNC mirror of a mirrored file
9551 * specified by @fname contains exactly the same data.
9553 * If @mirror_ids is specified, then the function will verify the
9554 * mirrors specified by @mirror_ids contain exactly the same data.
9556 * If @verbose is specified, then the function will print where the
9557 * differences are if the data do not match. Otherwise, it will
9558 * just return an error in that case.
9560 * Return: 0 on success or a negative error code on failure.
9563 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9566 struct verify_chunk chunks_array[1024] = { };
9567 struct llapi_layout *layout = NULL;
9571 int chunk_count = 0;
9577 if (stat(fname, &stbuf) < 0) {
9578 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9579 progname, fname, strerror(errno));
9584 if (!S_ISREG(stbuf.st_mode)) {
9585 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9591 if (stbuf.st_size == 0) {
9593 fprintf(stdout, "%s: '%s' file size is 0.\n",
9599 fd = open(fname, O_DIRECT | O_RDONLY);
9601 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9602 progname, fname, strerror(errno));
9607 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9609 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9610 progname, fname, strerror(errno));
9614 layout = llapi_layout_get_by_fd(fd, 0);
9615 if (layout == NULL) {
9616 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9617 progname, fname, strerror(errno));
9619 llapi_lease_release(fd);
9623 rc = llapi_layout_flags_get(layout, &flr_state);
9625 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9626 progname, fname, strerror(errno));
9631 flr_state &= LCM_FL_FLR_MASK;
9632 switch (flr_state) {
9635 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9636 progname, fname, llapi_layout_flags_string(flr_state));
9642 /* find out mirror chunks to be verified */
9643 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9644 ARRAY_SIZE(chunks_array));
9645 if (chunk_count < 0) {
9651 /* filter specified mirror ids */
9652 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9655 print_chunks(fname, chunks_array, chunk_count);
9657 for (idx = 0; idx < chunk_count; idx++) {
9658 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9661 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9663 PEXT(&chunks_array[idx].chunk),
9664 (unsigned long long)stbuf.st_size);
9668 if (chunks_array[idx].mirror_count == 0) {
9670 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9672 PEXT(&chunks_array[idx].chunk));
9674 fprintf(stderr, "skipped\n");
9678 fprintf(stderr, "failed\n");
9682 if (chunks_array[idx].mirror_count == 1) {
9685 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9687 PEXT(&chunks_array[idx].chunk),
9688 chunks_array[idx].mirror_id[0]);
9692 rc = llapi_lease_check(fd);
9693 if (rc != LL_LEASE_RDLCK) {
9694 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9699 /* verify one chunk */
9700 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9701 &chunks_array[idx], verbose);
9715 llapi_layout_free(layout);
9716 llapi_lease_release(fd);
9724 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9725 * @argc: The count of lfs mirror verify command line arguments.
9726 * @argv: Array of strings for lfs mirror verify command line arguments.
9728 * This function parses lfs mirror verify command and verifies the
9729 * specified mirrored file(s).
9731 * Return: 0 on success or a negative error code on failure.
9733 static inline int lfs_mirror_verify(int argc, char **argv)
9735 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9743 struct option long_opts[] = {
9744 { .val = 'o', .name = "only", .has_arg = required_argument },
9745 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9748 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9750 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9753 rc = parse_mirror_ids(mirror_ids,
9754 ARRAY_SIZE(mirror_ids),
9758 "%s: bad mirror ids '%s'.\n",
9765 "%s: at least 2 mirror ids needed with '--only' option.\n",
9775 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9776 progname, argv[optind - 1]);
9782 if (argc == optind) {
9783 fprintf(stderr, "%s: no file name given.\n", progname);
9788 if (ids_nr > 0 && argc > optind + 1) {
9790 "%s: '--only' cannot be used upon multiple files.\n",
9798 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9804 for (; optind < argc; optind++) {
9805 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9815 * lfs_mirror() - Parse and execute lfs mirror commands.
9816 * @argc: The count of lfs mirror command line arguments.
9817 * @argv: Array of strings for lfs mirror command line arguments.
9819 * This function parses lfs mirror commands and performs the
9820 * corresponding functions specified in mirror_cmdlist[].
9822 * Return: 0 on success or an error code on failure.
9824 static int lfs_mirror(int argc, char **argv)
9831 Parser_init("lfs-mirror > ", mirror_cmdlist);
9833 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9835 program_invocation_short_name = cmd;
9837 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9839 rc = Parser_commands();
9841 return rc < 0 ? -rc : rc;
9844 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9846 #if __BYTE_ORDER == __BIG_ENDIAN
9847 __swab16s(&attrs->lsa_valid);
9848 __swab64s(&attrs->lsa_size);
9849 __swab64s(&attrs->lsa_blocks);
9855 LFS_SOM_BLOCKS = 0x2,
9856 LFS_SOM_FLAGS = 0x4,
9857 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9861 static int lfs_getsom(int argc, char **argv)
9864 struct lustre_som_attrs *attrs;
9865 char buf[sizeof(*attrs) + 64];
9866 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9869 while ((c = getopt(argc, argv, "sbf")) != -1) {
9872 type = LFS_SOM_SIZE;
9875 type = LFS_SOM_BLOCKS;
9878 type = LFS_SOM_FLAGS;
9881 fprintf(stderr, "%s: invalid option '%c'\n",
9891 fprintf(stderr, "%s: %s\n",
9892 progname, argc == 0 ? "miss file target" :
9893 "input more than 2 files");
9898 attrs = (void *)buf;
9899 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9902 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
9903 argv[0], strerror(errno), errno);
9907 lustre_som_swab(attrs);
9910 case LFS_SOM_ATTR_ALL:
9911 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9912 path, attrs->lsa_size, attrs->lsa_blocks,
9916 printf("%llu\n", attrs->lsa_size);
9918 case LFS_SOM_BLOCKS:
9919 printf("%llu\n", attrs->lsa_blocks);
9922 printf("%x\n", attrs->lsa_valid);
9925 fprintf(stderr, "%s: unknown option\n", progname);
9933 * lfs_mirror_list_commands() - List lfs mirror commands.
9934 * @argc: The count of command line arguments.
9935 * @argv: Array of strings for command line arguments.
9937 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9939 * Return: 0 on success.
9941 static int lfs_mirror_list_commands(int argc, char **argv)
9943 char buffer[81] = "";
9945 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9951 static int lfs_list_commands(int argc, char **argv)
9953 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9955 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9960 int main(int argc, char **argv)
9964 /* Ensure that liblustreapi constructor has run */
9965 if (!llapi_liblustreapi_initialized())
9966 fprintf(stderr, "liblustreapi was not properly initialized\n");
9971 Parser_init("lfs > ", cmdlist);
9973 progname = program_invocation_short_name; /* Used in error messages */
9975 llapi_set_command_name(argv[1]);
9976 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9977 llapi_clear_command_name();
9979 rc = Parser_commands();
9982 return rc < 0 ? -rc : rc;
9985 #ifdef _LUSTRE_IDL_H_
9986 /* Everything we need here should be included by lustreapi.h. */
9987 # error "lfs should not depend on lustre_idl.h"
9988 #endif /* _LUSTRE_IDL_H_ */