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_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_read(int argc, char **argv);
128 static inline int lfs_mirror_write(int argc, char **argv);
129 static inline int lfs_mirror_copy(int argc, char **argv);
131 enum setstripe_origin {
139 static int lfs_setstripe_internal(int argc, char **argv,
140 enum setstripe_origin opc);
142 static inline int lfs_setstripe(int argc, char **argv)
144 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
146 static inline int lfs_setstripe_migrate(int argc, char **argv)
148 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
150 static inline int lfs_mirror_create(int argc, char **argv)
152 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
154 static inline int lfs_mirror_extend(int argc, char **argv)
156 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
158 static inline int lfs_mirror_split(int argc, char **argv)
160 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
163 /* Setstripe and migrate share mostly the same parameters */
164 #define SSM_CMD_COMMON(cmd) \
165 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
166 " [--stripe-count|-c <stripe_count>]\n" \
167 " [--stripe-index|-i <start_ost_idx>]\n" \
168 " [--stripe-size|-S <stripe_size>]\n" \
169 " [--layout|-L <pattern>]\n" \
170 " [--pool|-p <pool_name>]\n" \
171 " [--ost|-o <ost_indices>]\n" \
172 " [--yaml|-y <yaml_template_file>]\n" \
173 " [--copy=<lustre_src>]\n"
175 #define SSM_HELP_COMMON \
176 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
177 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
178 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
179 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
180 "\t respectively)\n" \
181 "\tpool_name: Name of OST pool to use (default none)\n" \
182 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
183 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
184 "\t Indices be specified in a format of:\n" \
185 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
187 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
188 "\t If --pool is set with --ost, then the OSTs\n" \
189 "\t must be the members of the pool.\n" \
190 "\tcomp_end: Extent end of component, start after previous end.\n"\
191 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
192 "\t respectively, -1 for EOF). Must be a multiple of\n"\
193 "\t stripe_size.\n" \
194 "\tyaml_template_file:\n" \
195 "\t YAML layout template file, can't be used with -c,\n" \
196 "\t -i, -S, -p, -o, or -E arguments.\n" \
197 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
198 "\t another lustre file or directory, can't used with\n" \
199 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
201 #define MIRROR_CREATE_HELP \
202 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
203 "\t setstripe layout options\n" \
204 "\t It defaults to 1 if not specified; if specified,\n" \
205 "\t it must follow the option without a space.\n" \
206 "\t The option can also be repeated multiple times to\n" \
207 "\t separate mirrors that have different layouts.\n" \
208 "\tsetstripe options: Mirror layout\n" \
209 "\t It can be a plain layout or a composite layout.\n" \
210 "\t If not specified, the stripe options inherited\n" \
211 "\t from the previous component will be used.\n" \
212 "\tflags: set flags to the component of the current mirror.\n" \
213 "\t Only \"prefer\" flag is supported so far.\n"
215 #define MIRROR_EXTEND_HELP \
217 "\tvictim_file: The layout of victim_file will be split and used\n" \
218 "\t as a mirror added to the mirrored file.\n" \
219 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
220 "\t from victim file(s) in case the victim file(s)\n" \
221 "\t contains the same data as the original mirrored\n" \
224 #define MIRROR_EXTEND_USAGE \
225 " <--mirror-count|-N[mirror_count]>\n" \
226 " [setstripe options|-f <victim_file>]\n" \
229 #define SETSTRIPE_USAGE \
230 SSM_CMD_COMMON("setstripe") \
231 MIRROR_EXTEND_USAGE \
232 " <directory|filename>\n" \
236 #define MIGRATE_USAGE \
237 SSM_CMD_COMMON("migrate ") \
238 " [--block|-b] [--non-block|-n]\n" \
239 " [--non-direct|-D] [--verbose|-v]\n" \
243 "\tblock: Block file access during data migration (default)\n" \
244 "\tnon-block: Abort migrations if concurrent access is detected\n" \
245 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
246 "\tverbose: Print each filename as it is migrated\n" \
248 #define SETDIRSTRIPE_USAGE \
249 " [--mdt-count|-c stripe_count>\n" \
250 " [--mdt-hash|-H mdt_hash]\n" \
251 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
252 " [--default|-D] [--mode|-o mode] <dir>\n" \
253 "\tstripe_count: stripe count of the striped directory\n" \
254 "\tmdt_index: MDT index of first stripe\n" \
255 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
256 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
257 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
258 "\tdefault_stripe: set default dirstripe of the directory\n" \
259 "\tmode: the file access permission of the directory (octal)\n"
262 * command_t mirror_cmdlist - lfs mirror commands.
264 command_t mirror_cmdlist[] = {
265 { .pc_name = "create", .pc_func = lfs_mirror_create,
266 .pc_help = "Create a mirrored file.\n"
267 "usage: lfs mirror create "
268 "<--mirror-count|-N[mirror_count]> "
269 "[setstripe options] ... <filename|directory>\n"
270 MIRROR_CREATE_HELP },
271 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
272 .pc_help = "Extend a mirrored file.\n"
273 "usage: lfs mirror extend "
274 "<--mirror-count|-N[mirror_count]> [--no-verify] "
275 "[setstripe options|-f <victim_file>] ... <filename>\n"
276 MIRROR_EXTEND_HELP },
277 { .pc_name = "split", .pc_func = lfs_mirror_split,
278 .pc_help = "Split a mirrored file.\n"
279 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
280 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
281 "\t [-f <new_file>] <mirrored file>\n"
282 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
283 "\t can be fetched by lfs getstripe command.\n"
284 "\tcomp_id: Unique component ID within a mirror.\n"
285 "\tnew_file: This option indicates the layout of the split\n"
286 "\t mirror will be stored into. If not specified,\n"
287 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
288 "\t will be used.\n" },
289 { .pc_name = "read", .pc_func = lfs_mirror_read,
290 .pc_help = "Read the content of a specified mirror of a file.\n"
291 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
292 "[--outfile|-o <output_file>] <mirrored_file>\n" },
293 { .pc_name = "write", .pc_func = lfs_mirror_write,
294 .pc_help = "Write to a specified mirror of a file.\n"
295 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
296 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
297 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
298 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
299 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
300 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
301 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
302 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
303 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
304 "<mirrored file> [<mirrored file2>...]\n"},
305 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
306 .pc_help = "Verify mirrored file(s).\n"
307 "usage: lfs mirror verify "
308 "[--only <mirror_id,mirror_id2[,...]>] "
309 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
310 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
311 .pc_help = "list commands supported by lfs mirror"},
312 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
313 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
314 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
318 /* all available commands */
319 command_t cmdlist[] = {
320 {"setstripe", lfs_setstripe, 0,
321 "To create a file with specified striping/composite layout, or\n"
322 "create/replace the default layout on an existing directory:\n"
323 SSM_CMD_COMMON("setstripe")
324 " <directory|filename>\n"
326 "To add component(s) to an existing composite file:\n"
327 SSM_CMD_COMMON("setstripe --component-add")
329 "To totally delete the default striping from an existing directory:\n"
330 "usage: setstripe -d <directory>\n"
332 "To create a mirrored file or set s default mirror layout on a directory:\n"
333 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
335 "To delete the last component(s) from an existing composite file\n"
336 "(note that this will also delete any data in those components):\n"
337 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
338 " [--component-flags|-F <comp_flags>]\n"
340 "\tcomp_id: Unique component ID to delete\n"
341 "\tcomp_flags: 'init' indicating all instantiated components\n"
342 "\t '^init' indicating all uninstantiated components\n"
343 "\t-I and -F cannot be specified at the same time\n"},
344 {"getstripe", lfs_getstripe, 0,
345 "To list the layout pattern for a given file or files in a\n"
346 "directory or recursively for all files in a directory tree.\n"
347 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
348 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
349 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
350 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
351 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
352 " [--component-id[=comp_id]|-I[comp_id]]\n"
353 " [--component-flags[=comp_flags]]\n"
354 " [--component-count]\n"
355 " [--component-start[=[+-]comp_start]]\n"
356 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
357 " [[!] --mirror-index=[+-]<index> |\n"
358 " [!] --mirror-id=[+-]<id>]\n"
359 " <directory|filename> ..."},
360 {"setdirstripe", lfs_setdirstripe, 0,
361 "To create a striped directory on a specified MDT. This can only\n"
362 "be done on MDT0 with the right of administrator.\n"
363 "usage: setdirstripe [OPTION] <directory>\n"
365 {"getdirstripe", lfs_getdirstripe, 0,
366 "To list the layout pattern info for a given directory\n"
367 "or recursively for all directories in a directory tree.\n"
368 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
369 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
370 " [--recursive|-r] [--yaml|-y]\n"
371 " [--default|-D] <dir> ..."},
372 {"mkdir", lfs_setdirstripe, 0,
373 "To create a striped directory on a specified MDT. This can only\n"
374 "be done on MDT0 with the right of administrator.\n"
375 "usage: mkdir [OPTION] <directory>\n"
377 {"rm_entry", lfs_rmentry, 0,
378 "To remove the name entry of the remote directory. Note: This\n"
379 "command will only delete the name entry, i.e. the remote directory\n"
380 "will become inaccessable after this command. This can only be done\n"
381 "by the administrator\n"
382 "usage: rm_entry <dir>\n"},
383 {"pool_list", lfs_poollist, 0,
384 "List pools or pool OSTs\n"
385 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
386 {"find", lfs_find, 0,
387 "find files matching given attributes recursively in directory tree.\n"
388 "usage: find <directory|filename> ...\n"
389 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
390 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
391 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
392 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
393 " [[!] --stripe-count|-c [+-]<stripes>]\n"
394 " [[!] --stripe-index|-i <index,...>]\n"
395 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
396 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
397 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
398 " [[!] --projid <projid>]\n"
399 " [[!] --layout|-L released,raid0,mdt]\n"
400 " [[!] --component-count [+-]<comp_cnt>]\n"
401 " [[!] --component-start [+-]N[kMGTPE]]\n"
402 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
403 " [[!] --component-flags <comp_flags>]\n"
404 " [[!] --mirror-count|-N [+-]<n>]\n"
405 " [[!] --mirror-state <[^]state>]\n"
406 " [[!] --mdt-count|-T [+-]<stripes>]\n"
407 " [[!] --mdt-hash|-H <hashtype>\n"
408 " [[!] --mdt-index|-m <uuid|index,...>]\n"
409 "\t !: used before an option indicates 'NOT' requested attribute\n"
410 "\t -: used before a value indicates less than requested value\n"
411 "\t +: used before a value indicates more than requested value\n"
412 "\thashtype: hash type of the striped directory.\n"
413 "\t fnv_1a_64 FNV-1a hash algorithm\n"
414 "\t all_char sum of characters % MDT_COUNT\n"},
415 {"check", lfs_check, 0,
416 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
417 "or all the servers (MGTs, MDTs and OSTs).\n"
418 "usage: check <mgts|osts|mdts|all>"},
419 {"osts", lfs_osts, 0, "list OSTs connected to client "
420 "[for specified path only]\n" "usage: osts [path]"},
421 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
422 "[for specified path only]\n" "usage: mdts [path]"},
424 "report filesystem disk space usage or inodes usage"
425 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
426 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
427 {"getname", lfs_getname, 0, "list instances and specified mount points "
428 "[for specified path only]\n"
429 "Usage: getname [-h]|[path ...] "},
430 #ifdef HAVE_SYS_QUOTA_H
431 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
432 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
433 " -b <block-softlimit> -B <block-hardlimit>\n"
434 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
435 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
436 " [--block-softlimit <block-softlimit>]\n"
437 " [--block-hardlimit <block-hardlimit>]\n"
438 " [--inode-softlimit <inode-softlimit>]\n"
439 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
440 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
441 " [--block-grace <block-grace>]\n"
442 " [--inode-grace <inode-grace>] <filesystem>\n"
443 " setquota <-U|-G|-P>\n"
444 " -b <block-softlimit> -B <block-hardlimit>\n"
445 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
446 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
447 " [--block-softlimit <block-softlimit>]\n"
448 " [--block-hardlimit <block-hardlimit>]\n"
449 " [--inode-softlimit <inode-softlimit>]\n"
450 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
451 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
453 " -b can be used instead of --block-softlimit/--block-grace\n"
454 " -B can be used instead of --block-hardlimit\n"
455 " -i can be used instead of --inode-softlimit/--inode-grace\n"
456 " -I can be used instead of --inode-hardlimit\n"
457 " -d can be used instead of --default\n\n"
458 "Note: The total quota space will be split into many qunits and\n"
459 " balanced over all server targets, the minimal qunit size is\n"
460 " 1M bytes for block space and 1K inodes for inode space.\n\n"
461 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
462 " Quota space rebalancing process will stop when this mininum\n"
463 " value is reached. As a result, quota exceeded can be returned\n"
464 " while many targets still have 1MB or 1K inodes of spare\n"
466 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
467 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
469 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
470 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
471 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
472 {"project", lfs_project, 0,
473 "Change or list project attribute for specified file or directory.\n"
474 "usage: project [-d|-r] <file|directory...>\n"
475 " list project ID and flags on file(s) or directories\n"
476 " project [-p id] [-s] [-r] <file|directory...>\n"
477 " set project ID and/or inherit flag for specified file(s) or directories\n"
478 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
479 " check project ID and flags on file(s) or directories, print outliers\n"
480 " project -C [-r] [-k] <file|directory...>\n"
481 " clear the project inherit flag and ID on the file or directory\n"
484 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
485 "usage: flushctx [-k] [mountpoint...]"},
487 "Remote user copy files and directories.\n"
488 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
490 "Remote user list directory contents.\n"
491 "usage: ls [OPTION]... [FILE]..."},
492 {"changelog", lfs_changelog, 0,
493 "Show the metadata changes on an MDT."
494 "\nusage: changelog <mdtname> [startrec [endrec]]"},
495 {"changelog_clear", lfs_changelog_clear, 0,
496 "Indicate that old changelog records up to <endrec> are no longer of "
497 "interest to consumer <id>, allowing the system to free up space.\n"
498 "An <endrec> of 0 means all records.\n"
499 "usage: changelog_clear <mdtname> <id> <endrec>"},
500 {"fid2path", lfs_fid2path, 0,
501 "Resolve the full path(s) for given FID(s). For a specific hardlink "
502 "specify link number <linkno>.\n"
503 /* "For a historical link name, specify changelog record <recno>.\n" */
504 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
505 /* [ --rec <recno> ] */ },
506 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
507 "usage: path2fid [--parents] <path> ..."},
508 {"data_version", lfs_data_version, 0, "Display file data version for "
509 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
510 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
511 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
512 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
513 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
514 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
515 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
517 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
518 "[--archived] [--lost] <file> ..."},
519 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
520 "given files.\n" "usage: hsm_action <file> ..."},
521 {"hsm_archive", lfs_hsm_archive, 0,
522 "Archive file to external storage.\n"
523 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
525 {"hsm_restore", lfs_hsm_restore, 0,
526 "Restore file from external storage.\n"
527 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
528 {"hsm_release", lfs_hsm_release, 0,
529 "Release files from Lustre.\n"
530 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
531 {"hsm_remove", lfs_hsm_remove, 0,
532 "Remove file copy from external storage.\n"
533 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
535 " (FILE [FILE ...] | "
536 "--mntpath MOUNTPATH FID [FID ...])\n"
538 "Note: To remove an archived copy of a file already deleted from a "
540 "--mntpath option and a list of FIDs must be specified"
542 {"hsm_cancel", lfs_hsm_cancel, 0,
543 "Cancel requests related to specified files.\n"
544 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
545 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
546 "usage: swap_layouts <path1> <path2>"},
547 {"migrate", lfs_setstripe_migrate, 0,
548 "migrate a directory between MDTs.\n"
549 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
550 " [--mdt-hash|-H] <hash_type>\n"
551 " [--mdt-index|-m] <start_mdt_index>\n"
554 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
555 " it's the MDT index of first stripe\n"
556 "\tmdt_count: number of MDTs to stripe a directory over\n"
557 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
558 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
559 " all_char sum of characters % MDT_COUNT\n"
561 "migrate file objects from one OST "
562 "layout\nto another (may be not safe with concurent writes).\n"
564 "[--stripe-count|-c] <stripe_count>\n"
565 " [--stripe-index|-i] <start_ost_index>\n"
566 " [--stripe-size|-S] <stripe_size>\n"
567 " [--pool|-p] <pool_name>\n"
568 " [--ost|-o] <ost_indices>\n"
570 " [--non-block|-n]\n"
571 " [--non-direct|-D]\n"
572 " <file|directory>\n"
573 "\tstripe_count: number of OSTs to stripe a file over\n"
574 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
575 "\tstripe_size: number of bytes to store before moving to the next OST\n"
576 "\tpool_name: name of the predefined pool of OSTs\n"
577 "\tost_indices: OSTs to stripe over, in order\n"
578 "\tblock: Block file access during data migration (default)\n"
579 "\tnon-block: Abort migrations if concurrent access is detected\n"
580 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
582 "To move directories between MDTs. This command is deprecated, "
583 "use \"migrate\" instead.\n"
584 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
586 {"ladvise", lfs_ladvise, 0,
587 "Provide servers with advice about access patterns for a file.\n"
588 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
589 " [--background|-b] [--unset|-u]\n\n"
590 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
591 " {[--mode|-m [READ,WRITE]}\n"
593 {"mirror", lfs_mirror, mirror_cmdlist,
594 "lfs commands used to manage files with mirrored components:\n"
595 "lfs mirror create - create a mirrored file or directory\n"
596 "lfs mirror extend - add mirror(s) to an existing file\n"
597 "lfs mirror split - split a mirror from an existing mirrored file\n"
598 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
599 "lfs mirror read - read a mirror content of a mirrored file\n"
600 "lfs mirror write - write to a mirror of a mirrored file\n"
601 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
602 "lfs mirror verify - verify mirrored file(s)\n"},
603 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
604 "usage: getsom [-s] [-b] [-f] <path>\n"
605 "\t-s: Only show the size value of the SOM data for a given file\n"
606 "\t-b: Only show the blocks value of the SOM data for a given file\n"
607 "\t-f: Only show the flags value of the SOM data for a given file\n"},
608 {"help", Parser_help, 0, "help"},
609 {"exit", Parser_quit, 0, "quit"},
610 {"quit", Parser_quit, 0, "quit"},
611 {"--version", Parser_version, 0,
612 "output build version of the utility and exit"},
613 {"--list-commands", lfs_list_commands, 0,
614 "list commands supported by the utility and exit"},
619 static int check_hashtype(const char *hashtype)
623 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
624 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
631 static const char *error_loc = "syserror";
634 MIGRATION_NONBLOCK = 0x0001,
635 MIGRATION_MIRROR = 0x0002,
636 MIGRATION_NONDIRECT = 0x0004,
637 MIGRATION_VERBOSE = 0x0008,
640 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
641 struct llapi_layout *layout);
644 migrate_open_files(const char *name, __u64 migration_flags,
645 const struct llapi_stripe_param *param,
646 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
653 char parent[PATH_MAX];
654 char volatile_file[PATH_MAX];
660 if (param == NULL && layout == NULL) {
661 error_loc = "layout information";
665 /* search for file directory pathname */
666 if (strlen(name) > sizeof(parent) - 1) {
667 error_loc = "source file name";
671 strncpy(parent, name, sizeof(parent));
672 ptr = strrchr(parent, '/');
674 if (getcwd(parent, sizeof(parent)) == NULL) {
675 error_loc = "getcwd";
679 if (ptr == parent) /* leading '/' */
684 /* open file, direct io */
685 /* even if the file is only read, WR mode is nedeed to allow
686 * layout swap on fd */
688 if (!(migration_flags & MIGRATION_NONDIRECT))
690 fd = open(name, rflags);
693 error_loc = "cannot open source file";
697 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
699 error_loc = "cannot get MDT index";
704 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
705 mode_t open_mode = S_IRUSR | S_IWUSR;
707 random_value = random();
708 rc = snprintf(volatile_file, sizeof(volatile_file),
709 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
710 mdt_index, random_value);
711 if (rc >= sizeof(volatile_file)) {
716 /* create, open a volatile file, use caching (ie no directio) */
718 fdv = llapi_file_open_param(volatile_file, open_flags,
721 fdv = lfs_component_create(volatile_file, open_flags,
723 } while (fdv < 0 && (rc = fdv) == -EEXIST);
726 error_loc = "cannot create volatile file";
730 /* In case the MDT does not support creation of volatile files
731 * we should try to unlink it. */
732 (void)unlink(volatile_file);
734 /* Not-owner (root?) special case.
735 * Need to set owner/group of volatile file like original.
736 * This will allow to pass related check during layout_swap.
741 error_loc = "cannot stat source file";
745 rc = fstat(fdv, &stv);
748 error_loc = "cannot stat volatile";
752 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
753 rc = fchown(fdv, st.st_uid, st.st_gid);
756 error_loc = "cannot change ownwership of volatile";
775 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
777 struct llapi_layout *layout;
778 size_t buf_size = 4 * 1024 * 1024;
787 layout = llapi_layout_get_by_fd(fd_src, 0);
788 if (layout != NULL) {
789 uint64_t stripe_size;
791 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
793 buf_size = stripe_size;
795 llapi_layout_free(layout);
798 /* Use a page-aligned buffer for direct I/O */
799 rc = posix_memalign(&buf, getpagesize(), buf_size);
804 /* read new data only if we have written all
805 * previously read data */
808 rc = check_file(fd_src);
813 rsize = read(fd_src, buf, buf_size);
825 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
844 static int migrate_copy_timestamps(int fd, int fdv)
848 if (fstat(fd, &st) == 0) {
849 struct timeval tv[2] = {
850 {.tv_sec = st.st_atime},
851 {.tv_sec = st.st_mtime}
854 return futimes(fdv, tv);
860 static int migrate_block(int fd, int fdv)
867 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
869 error_loc = "cannot get dataversion";
877 /* The grouplock blocks all concurrent accesses to the file.
878 * It has to be taken after llapi_get_data_version as it would
880 rc = llapi_group_lock(fd, gid);
882 error_loc = "cannot get group lock";
886 rc = migrate_copy_data(fd, fdv, NULL);
888 error_loc = "data copy failed";
892 /* Make sure we keep original atime/mtime values */
893 rc = migrate_copy_timestamps(fd, fdv);
895 error_loc = "timestamp copy failed";
900 * for a migration we need to check data version on file did
903 * Pass in gid=0 since we already own grouplock. */
904 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
905 SWAP_LAYOUTS_CHECK_DV1);
907 error_loc = "file changed";
910 error_loc = "cannot swap layout";
915 rc2 = llapi_group_unlock(fd, gid);
916 if (rc2 < 0 && rc == 0) {
917 error_loc = "unlock group lock";
925 * Internal helper for migrate_copy_data(). Check lease and report error if
928 * \param[in] fd File descriptor on which to check the lease.
930 * \retval 0 Migration can keep on going.
931 * \retval -errno Error occurred, abort migration.
933 static int check_lease(int fd)
937 rc = llapi_lease_check(fd);
939 return 0; /* llapi_check_lease returns > 0 on success. */
944 static int migrate_nonblock(int fd, int fdv)
950 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
952 error_loc = "cannot get data version";
956 rc = migrate_copy_data(fd, fdv, check_lease);
958 error_loc = "data copy failed";
962 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
964 error_loc = "cannot get data version";
970 error_loc = "source file changed";
974 /* Make sure we keep original atime/mtime values */
975 rc = migrate_copy_timestamps(fd, fdv);
977 error_loc = "timestamp copy failed";
984 static int lfs_component_set(char *fname, int comp_id,
985 __u32 flags, __u32 neg_flags)
988 __u32 flags_array[2];
993 ids[count] = comp_id;
994 flags_array[count] = flags;
999 ids[count] = comp_id;
1000 flags_array[count] = neg_flags | LCME_FL_NEG;
1004 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1007 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1008 progname, comp_id, fname, flags, neg_flags);
1013 static int lfs_component_del(char *fname, __u32 comp_id,
1014 __u32 flags, __u32 neg_flags)
1018 if (flags && neg_flags)
1021 if (!flags && neg_flags)
1022 flags = neg_flags | LCME_FL_NEG;
1024 if ((flags && comp_id) || (!flags && !comp_id))
1027 /* LCME_FL_INIT is the only supported flag in PFL */
1029 if (flags & ~LCME_KNOWN_FLAGS) {
1031 "%s setstripe: unknown flags %#x\n",
1035 } else if (comp_id > LCME_ID_MAX) {
1036 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1041 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1044 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1045 progname, comp_id, fname, strerror(errno));
1049 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1056 rc = llapi_layout_file_comp_add(fname, layout);
1058 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1059 fname, strerror(errno));
1063 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1064 struct llapi_layout *layout)
1072 fd = lstat(fname, &st);
1073 if (fd == 0 && S_ISDIR(st.st_mode))
1074 open_flags = O_DIRECTORY | O_RDONLY;
1076 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1078 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1079 S_ISDIR(st.st_mode) ?
1080 "set default composite layout for" :
1081 "create composite file",
1082 fname, strerror(errno));
1086 static int lfs_migrate(char *name, __u64 migration_flags,
1087 struct llapi_stripe_param *param,
1088 struct llapi_layout *layout)
1094 rc = migrate_open_files(name, migration_flags, param, layout,
1099 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1100 /* Blocking mode (forced if servers do not support file lease).
1101 * It is also the default mode, since we cannot distinguish
1102 * between a broken lease and a server that does not support
1103 * atomic swap/close (LU-6785) */
1104 rc = migrate_block(fd, fdv);
1108 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1110 error_loc = "cannot get lease";
1114 rc = migrate_nonblock(fd, fdv);
1116 llapi_lease_release(fd);
1120 /* Atomically put lease, swap layouts and close.
1121 * for a migration we need to check data version on file did
1123 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1125 error_loc = "cannot swap layout";
1137 fprintf(stderr, "error: %s: %s: %s: %s\n",
1138 progname, name, error_loc, strerror(-rc));
1139 else if (migration_flags & MIGRATION_VERBOSE)
1140 printf("%s\n", name);
1145 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1154 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1158 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1159 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1160 const char *comp_name = comp_flags_table[i].cfn_name;
1162 if (strcmp(name, comp_name) == 0) {
1163 *flags |= comp_flag;
1165 } else if (strncmp(name, "^", 1) == 0 &&
1166 strcmp(name + 1, comp_name) == 0) {
1167 *neg_flags |= comp_flag;
1172 llapi_printf(LLAPI_MSG_ERROR,
1173 "%s: component flag '%s' not supported\n",
1179 if (!*flags && !*neg_flags)
1182 /* don't allow to set and exclude the same flag */
1183 if (*flags & *neg_flags)
1189 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1197 if (strncmp(string, "^", 1) == 0) {
1198 *neg_state = llapi_layout_string_flags(string + 1);
1199 if (*neg_state != 0)
1202 *state = llapi_layout_string_flags(string);
1207 llapi_printf(LLAPI_MSG_ERROR,
1208 "%s: mirrored file state '%s' not supported\n",
1214 * struct mirror_args - Command-line arguments for mirror(s).
1215 * @m_count: Number of mirrors to be created with this layout.
1216 * @m_flags: Mirror level flags, only 'prefer' is supported.
1217 * @m_layout: Mirror layout.
1218 * @m_file: A victim file. Its layout will be split and used as a mirror.
1219 * @m_next: Point to the next node of the list.
1221 * Command-line arguments for mirror(s) will be parsed and stored in
1222 * a linked list that consists of this structure.
1224 struct mirror_args {
1227 struct llapi_layout *m_layout;
1229 struct mirror_args *m_next;
1232 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1237 rc = llapi_layout_comp_flags_get(layout, &flags);
1241 if (flags & LCME_FL_NEG) {
1242 fprintf(stderr, "error: %s: negative flags are not supported\n",
1247 if (flags & LCME_FL_STALE) {
1248 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1249 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1253 return LLAPI_LAYOUT_ITER_CONT;
1256 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1258 uint64_t start, end;
1262 /* LU-10112: do not support dom+flr in phase 1 */
1263 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1267 rc = llapi_layout_pattern_get(layout, &pattern);
1271 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1272 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1277 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1281 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1285 if (end != LUSTRE_EOF) {
1286 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1291 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1296 * enum mirror_flags - Flags for extending a mirrored file.
1297 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1298 * in case the victim file(s) contains the same data as the
1299 * original mirrored file.
1300 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1301 * @MF_COMP_ID: specified component id instead of mirror id
1303 * Flags for extending a mirrored file.
1312 * mirror_create_sanity_check() - Check mirror list.
1313 * @list: A linked list that stores the mirror arguments.
1315 * This function does a sanity check on @list for creating
1318 * Return: 0 on success or a negative error code on failure.
1320 static int mirror_create_sanity_check(const char *fname,
1321 struct mirror_args *list)
1324 bool has_m_file = false;
1325 bool has_m_layout = false;
1331 struct llapi_layout *layout;
1333 layout = llapi_layout_get_by_path(fname, 0);
1336 "error: %s: file '%s' couldn't get layout\n",
1341 rc = mirror_sanity_check_one(layout);
1342 llapi_layout_free(layout);
1348 while (list != NULL) {
1349 if (list->m_file != NULL) {
1351 llapi_layout_free(list->m_layout);
1354 llapi_layout_get_by_path(list->m_file, 0);
1355 if (list->m_layout == NULL) {
1357 "error: %s: file '%s' has no layout\n",
1358 progname, list->m_file);
1362 has_m_layout = true;
1363 if (list->m_layout == NULL) {
1364 fprintf(stderr, "error: %s: no mirror layout\n",
1370 rc = mirror_sanity_check_one(list->m_layout);
1374 list = list->m_next;
1377 if (has_m_file && has_m_layout) {
1379 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1387 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1389 __u32 mirror_flags = *(__u32 *)cbdata;
1393 rc = llapi_layout_comp_flags_get(layout, &flags);
1398 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1403 return LLAPI_LAYOUT_ITER_CONT;
1407 * mirror_create() - Create a mirrored file.
1408 * @fname: The file to be created.
1409 * @mirror_list: A linked list that stores the mirror arguments.
1411 * This function creates a mirrored file @fname with the mirror(s)
1412 * from @mirror_list.
1414 * Return: 0 on success or a negative error code on failure.
1416 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1418 struct llapi_layout *layout = NULL;
1419 struct mirror_args *cur_mirror = NULL;
1420 uint16_t mirror_count = 0;
1424 rc = mirror_create_sanity_check(NULL, mirror_list);
1428 cur_mirror = mirror_list;
1429 while (cur_mirror != NULL) {
1430 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1432 &cur_mirror->m_flags);
1435 fprintf(stderr, "%s: failed to set mirror flags\n",
1440 for (i = 0; i < cur_mirror->m_count; i++) {
1441 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1444 fprintf(stderr, "error: %s: "
1445 "merge layout failed: %s\n",
1446 progname, strerror(errno));
1450 mirror_count += cur_mirror->m_count;
1451 cur_mirror = cur_mirror->m_next;
1454 if (layout == NULL) {
1455 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1459 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1462 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1463 progname, strerror(errno));
1467 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1475 llapi_layout_free(layout);
1480 * Compare files and check lease on @fd.
1482 * \retval bytes number of bytes are the same
1484 static ssize_t mirror_file_compare(int fd, int fdv)
1486 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1488 ssize_t bytes_done = 0;
1489 ssize_t bytes_read = 0;
1491 buf = malloc(buflen * 2);
1496 if (!llapi_lease_check(fd)) {
1497 bytes_done = -EBUSY;
1501 bytes_read = read(fd, buf, buflen);
1502 if (bytes_read <= 0)
1505 if (bytes_read != read(fdv, buf + buflen, buflen))
1508 /* XXX: should compute the checksum on each buffer and then
1509 * compare checksum to avoid cache collision */
1510 if (memcmp(buf, buf + buflen, bytes_read))
1513 bytes_done += bytes_read;
1521 static int mirror_extend_file(const char *fname, const char *victim_file,
1522 enum mirror_flags mirror_flags)
1527 struct stat stbuf_v;
1528 struct ll_ioc_lease *data = NULL;
1531 fd = open(fname, O_RDWR);
1533 error_loc = "open source file";
1538 fdv = open(victim_file, O_RDWR);
1540 error_loc = "open target file";
1545 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1546 error_loc = "stat source or target file";
1551 if (stbuf.st_dev != stbuf_v.st_dev) {
1552 error_loc = "stat source and target file";
1557 /* mirrors should be of the same size */
1558 if (stbuf.st_size != stbuf_v.st_size) {
1559 error_loc = "file sizes don't match";
1564 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1566 error_loc = "cannot get lease";
1570 if (!(mirror_flags & MF_NO_VERIFY)) {
1572 /* mirrors should have the same contents */
1573 ret = mirror_file_compare(fd, fdv);
1574 if (ret != stbuf.st_size) {
1575 error_loc = "file busy or contents don't match";
1576 rc = ret < 0 ? ret : -EINVAL;
1581 /* Get rid of caching pages from clients */
1582 rc = llapi_file_flush(fd);
1584 error_loc = "cannot get data version";
1588 rc = llapi_file_flush(fdv);
1590 error_loc = "cannot get data version";
1595 /* Make sure we keep original atime/mtime values */
1596 rc = migrate_copy_timestamps(fd, fdv);
1598 error_loc = "cannot copy timestamp";
1602 /* Atomically put lease, merge layouts and close. */
1603 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1605 error_loc = "memory allocation";
1608 data->lil_mode = LL_LEASE_UNLCK;
1609 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1610 data->lil_count = 1;
1611 data->lil_ids[0] = fdv;
1612 rc = llapi_lease_set(fd, data);
1614 error_loc = "cannot merge layout";
1616 } else if (rc == 0) {
1618 error_loc = "lost lease lock";
1631 (void) unlink(victim_file);
1633 fprintf(stderr, "error: %s: %s: %s: %s\n",
1634 progname, fname, error_loc, strerror(-rc));
1638 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1640 struct ll_ioc_lease *data = NULL;
1645 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1649 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1651 error_loc = "cannot get lease";
1655 rc = migrate_nonblock(fd, fdv);
1657 llapi_lease_release(fd);
1661 /* Atomically put lease, merge layouts and close. */
1662 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1664 error_loc = "memory allocation";
1667 data->lil_mode = LL_LEASE_UNLCK;
1668 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1669 data->lil_count = 1;
1670 data->lil_ids[0] = fdv;
1671 rc = llapi_lease_set(fd, data);
1673 error_loc = "cannot merge layout";
1675 } else if (rc == 0) {
1677 error_loc = "lost lease lock";
1690 fprintf(stderr, "error: %s: %s: %s: %s\n",
1691 progname, name, error_loc, strerror(-rc));
1695 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1696 enum mirror_flags mirror_flags)
1700 rc = mirror_create_sanity_check(fname, mirror_list);
1704 while (mirror_list) {
1705 if (mirror_list->m_file != NULL) {
1706 rc = mirror_extend_file(fname, mirror_list->m_file,
1709 __u32 mirror_count = mirror_list->m_count;
1711 while (mirror_count > 0) {
1712 rc = mirror_extend_layout(fname,
1713 mirror_list->m_layout);
1723 mirror_list = mirror_list->m_next;
1729 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1734 rc = llapi_layout_mirror_id_get(layout, &id);
1738 if ((__u16)id == *(__u16 *)cbdata)
1739 return LLAPI_LAYOUT_ITER_STOP;
1741 return LLAPI_LAYOUT_ITER_CONT;
1744 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1749 rc = llapi_layout_comp_id_get(layout, &id);
1753 if (id == *(__u32 *)cbdata)
1754 return LLAPI_LAYOUT_ITER_STOP;
1756 return LLAPI_LAYOUT_ITER_CONT;
1758 static int mirror_split(const char *fname, __u32 id,
1759 enum mirror_flags mflags, const char *victim_file)
1761 struct llapi_layout *layout;
1762 char parent[PATH_MAX];
1763 char victim[PATH_MAX];
1764 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1766 struct ll_ioc_lease *data;
1767 uint16_t mirror_count;
1772 /* check fname contains mirror with mirror_id/comp_id */
1773 layout = llapi_layout_get_by_path(fname, 0);
1776 "error %s: file '%s' couldn't get layout\n",
1781 rc = mirror_sanity_check_one(layout);
1785 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1788 "error %s: file '%s' couldn't get mirror count\n",
1792 if (mirror_count < 2) {
1794 "error %s: file '%s' has %d component, cannot split\n",
1795 progname, fname, mirror_count);
1799 if (mflags & MF_COMP_ID) {
1800 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1801 id = mirror_id_of(id);
1803 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1806 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1809 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1811 "error %s: file '%s' does not contain mirror with id %u\n",
1812 progname, fname, id);
1816 fd = open(fname, O_RDWR);
1819 "error %s: open file '%s' failed: %s\n",
1820 progname, fname, strerror(errno));
1824 /* get victim file directory pathname */
1825 if (strlen(fname) > sizeof(parent) - 1) {
1826 fprintf(stderr, "error %s: file name of '%s' too long\n",
1831 strncpy(parent, fname, sizeof(parent));
1832 ptr = strrchr(parent, '/');
1834 if (getcwd(parent, sizeof(parent)) == NULL) {
1835 fprintf(stderr, "error %s: getcwd failed: %s\n",
1836 progname, strerror(errno));
1846 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1848 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1853 if (victim_file == NULL) {
1854 /* use a temp file to store the splitted layout */
1855 if (mflags & MF_DESTROY) {
1856 fdv = llapi_create_volatile_idx(parent, mdt_index,
1857 O_LOV_DELAY_CREATE);
1859 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1861 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1864 /* user specified victim file */
1865 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1870 "error %s: create victim file failed: %s\n",
1871 progname, strerror(errno));
1875 /* get lease lock of fname */
1876 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1879 "error %s: cannot get lease of file '%s': %d\n",
1880 progname, fname, rc);
1884 /* Atomatically put lease, split layouts and close. */
1885 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1891 data->lil_mode = LL_LEASE_UNLCK;
1892 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1893 data->lil_count = 2;
1894 data->lil_ids[0] = fdv;
1895 data->lil_ids[1] = id;
1896 rc = llapi_lease_set(fd, data);
1898 if (rc == 0) /* lost lease lock */
1901 "error %s: cannot split '%s': %s\n",
1902 progname, fname, strerror(-rc));
1913 llapi_layout_free(layout);
1918 * Parse a string containing an target index list into an array of integers.
1920 * The input string contains a comma delimited list of individual
1921 * indices and ranges, for example "1,2-4,7". Add the indices into the
1922 * \a tgts array and remove duplicates.
1924 * \param[out] tgts array to store indices in
1925 * \param[in] size size of \a tgts array
1926 * \param[in] offset starting index in \a tgts
1927 * \param[in] arg string containing OST index list
1929 * \retval positive number of indices in \a tgts
1930 * \retval -EINVAL unable to parse \a arg
1932 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1936 int slots = size - offset;
1943 end_of_loop = false;
1944 while (!end_of_loop) {
1948 char *endptr = NULL;
1952 ptr = strchrnul(arg, ',');
1954 end_of_loop = *ptr == '\0';
1957 start_index = strtol(arg, &endptr, 0);
1958 if (endptr == arg) /* no data at all */
1960 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1963 end_index = start_index;
1964 if (*endptr == '-') {
1965 end_index = strtol(endptr + 1, &endptr, 0);
1966 if (*endptr != '\0')
1968 if (end_index < start_index)
1972 for (i = start_index; i <= end_index && slots > 0; i++) {
1975 /* remove duplicate */
1976 for (j = 0; j < offset; j++) {
1980 if (j == offset) { /* no duplicate */
1985 if (slots == 0 && i < end_index)
1993 if (!end_of_loop && ptr != NULL)
1996 return rc < 0 ? rc : nr;
1999 struct lfs_setstripe_args {
2000 unsigned long long lsa_comp_end;
2001 unsigned long long lsa_stripe_size;
2002 long long lsa_stripe_count;
2003 long long lsa_stripe_off;
2004 __u32 lsa_comp_flags;
2005 __u32 lsa_comp_neg_flags;
2006 unsigned long long lsa_pattern;
2007 unsigned int lsa_mirror_count;
2009 bool lsa_first_comp;
2011 char *lsa_pool_name;
2014 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2016 unsigned int mirror_count = lsa->lsa_mirror_count;
2017 bool first_comp = lsa->lsa_first_comp;
2019 memset(lsa, 0, sizeof(*lsa));
2021 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2022 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2023 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2024 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2025 lsa->lsa_pool_name = NULL;
2027 lsa->lsa_mirror_count = mirror_count;
2028 lsa->lsa_first_comp = first_comp;
2032 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2033 * @lsa: Stripe options to be initialized and inherited.
2035 * This function initializes stripe options in @lsa and inherit
2036 * stripe_size, stripe_count and OST pool_name options.
2040 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2042 unsigned long long stripe_size;
2043 long long stripe_count;
2044 char *pool_name = NULL;
2046 stripe_size = lsa->lsa_stripe_size;
2047 stripe_count = lsa->lsa_stripe_count;
2048 pool_name = lsa->lsa_pool_name;
2050 setstripe_args_init(lsa);
2052 lsa->lsa_stripe_size = stripe_size;
2053 lsa->lsa_stripe_count = stripe_count;
2054 lsa->lsa_pool_name = pool_name;
2057 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2059 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2060 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2061 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2062 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2063 lsa->lsa_pool_name != NULL ||
2064 lsa->lsa_comp_end != 0);
2068 * comp_args_to_layout() - Create or extend a composite layout.
2069 * @composite: Pointer to the composite layout.
2070 * @lsa: Stripe options for the new component.
2072 * This function creates or extends a composite layout by adding a new
2073 * component with stripe options from @lsa.
2075 * Return: 0 on success or an error code on failure.
2077 static int comp_args_to_layout(struct llapi_layout **composite,
2078 struct lfs_setstripe_args *lsa,
2081 struct llapi_layout *layout = *composite;
2082 uint64_t prev_end = 0;
2085 if (layout == NULL) {
2086 layout = llapi_layout_alloc();
2087 if (layout == NULL) {
2088 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2092 *composite = layout;
2096 /* Get current component extent, current component
2097 * must be the tail component. */
2098 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2100 fprintf(stderr, "Get comp extent failed. %s\n",
2105 if (lsa->lsa_first_comp)
2108 if (lsa->lsa_first_comp)
2109 rc = llapi_layout_add_first_comp(layout);
2111 rc = llapi_layout_comp_add(layout);
2113 fprintf(stderr, "Add component failed. %s\n",
2118 /* reset lsa_first_comp */
2119 lsa->lsa_first_comp = false;
2122 rc = llapi_layout_comp_extent_set(layout, prev_end,
2125 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2126 prev_end, lsa->lsa_comp_end, strerror(errno));
2131 /* Data-on-MDT component setting */
2132 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2133 /* In case of Data-on-MDT patterns the only extra option
2134 * applicable is stripe size option. */
2135 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2136 fprintf(stderr, "Option 'stripe-count' can't be "
2137 "specified with Data-on-MDT component: %lld\n",
2138 lsa->lsa_stripe_count);
2141 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2142 fprintf(stderr, "Option 'stripe-size' can't be "
2143 "specified with Data-on-MDT component: %llu\n",
2144 lsa->lsa_stripe_size);
2147 if (lsa->lsa_nr_tgts != 0) {
2148 fprintf(stderr, "Option 'ost-list' can't be specified "
2149 "with Data-on-MDT component: '%i'\n",
2153 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2154 fprintf(stderr, "Option 'stripe-offset' can't be "
2155 "specified with Data-on-MDT component: %lld\n",
2156 lsa->lsa_stripe_off);
2159 if (lsa->lsa_pool_name != 0) {
2160 fprintf(stderr, "Option 'pool' can't be specified "
2161 "with Data-on-MDT component: '%s'\n",
2162 lsa->lsa_pool_name);
2166 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2168 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2169 lsa->lsa_pattern, strerror(errno));
2172 /* Data-on-MDT component has always single stripe up to end */
2173 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2176 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2178 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2179 lsa->lsa_stripe_size, strerror(errno));
2183 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2185 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2186 lsa->lsa_stripe_count, strerror(errno));
2190 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2192 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2193 lsa->lsa_comp_flags, strerror(errno));
2197 if (lsa->lsa_pool_name != NULL) {
2198 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2200 fprintf(stderr, "Set pool name: %s failed. %s\n",
2201 lsa->lsa_pool_name, strerror(errno));
2205 rc = llapi_layout_pool_name_set(layout, "");
2207 fprintf(stderr, "Clear pool name failed: %s\n",
2213 if (lsa->lsa_nr_tgts > 0) {
2214 if (lsa->lsa_stripe_count > 0 &&
2215 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2216 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2217 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2218 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2219 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2222 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2223 rc = llapi_layout_ost_index_set(layout, i,
2228 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2229 lsa->lsa_stripe_off != -1) {
2230 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2233 fprintf(stderr, "Set ost index %d failed. %s\n",
2234 i, strerror(errno));
2241 static int build_component(struct llapi_layout **layout,
2242 struct lfs_setstripe_args *lsa, bool set_extent)
2246 rc = comp_args_to_layout(layout, lsa, set_extent);
2250 if (lsa->lsa_mirror_count > 0) {
2251 rc = llapi_layout_mirror_count_set(*layout,
2252 lsa->lsa_mirror_count);
2256 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2259 lsa->lsa_mirror_count = 0;
2265 static int build_layout_from_yaml_node(struct cYAML *node,
2266 struct llapi_layout **layout,
2267 struct lfs_setstripe_args *lsa,
2274 if (node->cy_type == CYAML_TYPE_OBJECT) {
2275 /* go deep to sub blocks */
2276 rc = build_layout_from_yaml_node(node->cy_child, layout,
2281 if (node->cy_string == NULL)
2284 string = node->cy_string;
2285 /* skip leading lmm_ if present, to simplify parsing */
2286 if (strncmp(string, "lmm_", 4) == 0)
2289 if (node->cy_type == CYAML_TYPE_STRING) {
2290 if (!strcmp(string, "lcme_extent.e_end")) {
2291 if (!strcmp(node->cy_valuestring, "EOF") ||
2292 !strcmp(node->cy_valuestring, "eof"))
2293 lsa->lsa_comp_end = LUSTRE_EOF;
2294 } else if (!strcmp(string, "pool")) {
2295 lsa->lsa_pool_name = node->cy_valuestring;
2296 } else if (!strcmp(string, "pattern")) {
2297 if (!strcmp(node->cy_valuestring, "mdt"))
2298 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2299 } else if (!strcmp(string, "lcme_flags")) {
2300 rc = comp_str2flags(node->cy_valuestring,
2301 &lsa->lsa_comp_flags,
2302 &lsa->lsa_comp_neg_flags);
2305 /* Only template flags have meaning in
2306 * the layout for a new file
2308 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2310 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2311 if (!strcmp(string, "lcm_mirror_count")) {
2312 lsa->lsa_mirror_count = node->cy_valueint;
2313 } else if (!strcmp(string, "lcme_extent.e_start")) {
2314 if (node->cy_valueint != 0 || *layout != NULL) {
2315 rc = build_component(layout, lsa, true);
2320 if (node->cy_valueint == 0)
2321 lsa->lsa_first_comp = true;
2323 /* initialize lsa */
2324 setstripe_args_init(lsa);
2325 lsa->lsa_tgts = osts;
2326 } else if (!strcmp(string, "lcme_extent.e_end")) {
2327 if (node->cy_valueint == -1)
2328 lsa->lsa_comp_end = LUSTRE_EOF;
2330 lsa->lsa_comp_end = node->cy_valueint;
2331 } else if (!strcmp(string, "stripe_count")) {
2332 lsa->lsa_stripe_count = node->cy_valueint;
2333 } else if (!strcmp(string, "stripe_size")) {
2334 lsa->lsa_stripe_size = node->cy_valueint;
2335 } else if (!strcmp(string, "stripe_offset")) {
2336 lsa->lsa_stripe_off = node->cy_valueint;
2337 } else if (!strcmp(string, "l_ost_idx")) {
2338 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2343 node = node->cy_next;
2349 static int lfs_comp_create_from_yaml(char *template,
2350 struct llapi_layout **layout,
2351 struct lfs_setstripe_args *lsa,
2354 struct cYAML *tree = NULL, *err_rc = NULL;
2357 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2359 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2360 progname, template);
2361 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2362 "can't parse", &err_rc);
2363 cYAML_print_tree2file(stderr, err_rc);
2364 cYAML_free_tree(err_rc);
2369 /* initialize lsa for plain file */
2370 setstripe_args_init(lsa);
2371 lsa->lsa_tgts = osts;
2373 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2375 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2376 progname, template);
2379 rc = build_component(layout, lsa, *layout != NULL);
2381 /* clean clean lsa */
2382 setstripe_args_init(lsa);
2386 cYAML_free_tree(tree);
2390 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2391 * end of the last component in the existing file, and adjust the
2392 * first extent start of the components to be added accordingly. */
2393 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2395 struct llapi_layout *head;
2396 uint64_t start, end, stripe_size, prev_end = 0;
2399 if (layout == NULL) {
2401 "%s setstripe: layout must be specified\n",
2407 head = llapi_layout_get_by_path(fname, 0);
2410 "%s setstripe: cannot read layout from '%s': %s\n",
2411 progname, fname, strerror(errno));
2413 } else if (errno == ENODATA) {
2414 /* file without LOVEA, this component-add will be turned
2415 * into a component-create. */
2416 llapi_layout_free(head);
2418 } else if (!llapi_layout_is_composite(head)) {
2419 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2421 llapi_layout_free(head);
2425 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2427 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2428 progname, strerror(errno));
2429 llapi_layout_free(head);
2433 llapi_layout_free(head);
2435 /* Make sure we use the first component of the layout to be added. */
2436 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2439 "%s setstripe: cannot move component cursor: %s\n",
2440 progname, strerror(errno));
2444 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2446 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2447 progname, strerror(errno));
2451 if (start > prev_end || end <= prev_end) {
2453 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2454 progname, start, end, prev_end);
2458 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2460 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2461 progname, strerror(errno));
2465 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2466 (prev_end & (stripe_size - 1))) {
2468 "%s setstripe: stripe size %lu not aligned with %lu\n",
2469 progname, stripe_size, prev_end);
2473 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2476 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2477 progname, prev_end, end, strerror(errno));
2484 static inline bool arg_is_eof(char *arg)
2486 return !strncmp(arg, "-1", strlen("-1")) ||
2487 !strncmp(arg, "EOF", strlen("EOF")) ||
2488 !strncmp(arg, "eof", strlen("eof"));
2492 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2494 * Return: Valid mirror_args pointer on success and
2495 * NULL if memory allocation fails.
2497 static struct mirror_args *lfs_mirror_alloc(void)
2499 struct mirror_args *mirror = NULL;
2502 mirror = calloc(1, sizeof(*mirror));
2513 * lfs_mirror_free() - Free memory allocated for a mirror argument
2515 * @mirror: Previously allocated mirror argument structure by
2516 * lfs_mirror_alloc().
2518 * Free memory allocated for @mirror.
2522 static void lfs_mirror_free(struct mirror_args *mirror)
2524 if (mirror->m_layout != NULL)
2525 llapi_layout_free(mirror->m_layout);
2530 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2531 * @mirror_list: Previously allocated mirror list.
2533 * Free memory allocated for @mirror_list.
2537 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2539 struct mirror_args *next_mirror = NULL;
2541 while (mirror_list != NULL) {
2542 next_mirror = mirror_list->m_next;
2543 lfs_mirror_free(mirror_list);
2544 mirror_list = next_mirror;
2556 LFS_COMP_NO_VERIFY_OPT,
2558 LFS_MIRROR_FLAGS_OPT,
2560 LFS_MIRROR_STATE_OPT,
2562 LFS_MIRROR_INDEX_OPT,
2566 static int lfs_setstripe_internal(int argc, char **argv,
2567 enum setstripe_origin opc)
2569 struct lfs_setstripe_args lsa = { 0 };
2570 struct llapi_stripe_param *param = NULL;
2571 struct find_param migrate_mdt_param = {
2581 unsigned long long size_units = 1;
2582 bool migrate_mode = false;
2583 bool migrate_mdt_mode = false;
2584 bool migration_block = false;
2585 __u64 migration_flags = 0;
2586 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2587 int comp_del = 0, comp_set = 0;
2590 struct llapi_layout *layout = NULL;
2591 struct llapi_layout **lpp = &layout;
2592 bool mirror_mode = false;
2593 bool has_m_file = false;
2594 __u32 mirror_count = 0;
2595 enum mirror_flags mirror_flags = 0;
2596 struct mirror_args *mirror_list = NULL;
2597 struct mirror_args *new_mirror = NULL;
2598 struct mirror_args *last_mirror = NULL;
2599 __u16 mirror_id = 0;
2601 bool from_yaml = false;
2602 bool from_copy = false;
2603 char *template = NULL;
2605 struct option long_opts[] = {
2606 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2607 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2608 /* --block is only valid in migrate mode */
2609 { .val = 'b', .name = "block", .has_arg = no_argument },
2610 { .val = LFS_COMP_ADD_OPT,
2611 .name = "comp-add", .has_arg = no_argument },
2612 { .val = LFS_COMP_ADD_OPT,
2613 .name = "component-add", .has_arg = no_argument },
2614 { .val = LFS_COMP_DEL_OPT,
2615 .name = "comp-del", .has_arg = no_argument },
2616 { .val = LFS_COMP_DEL_OPT,
2617 .name = "component-del", .has_arg = no_argument },
2618 { .val = LFS_COMP_FLAGS_OPT,
2619 .name = "comp-flags", .has_arg = required_argument },
2620 { .val = LFS_COMP_FLAGS_OPT,
2621 .name = "component-flags",
2622 .has_arg = required_argument },
2623 { .val = LFS_COMP_SET_OPT,
2624 .name = "comp-set", .has_arg = no_argument },
2625 { .val = LFS_COMP_SET_OPT,
2626 .name = "component-set",
2627 .has_arg = no_argument},
2628 { .val = LFS_COMP_NO_VERIFY_OPT,
2629 .name = "no-verify", .has_arg = no_argument},
2630 { .val = LFS_MIRROR_FLAGS_OPT,
2631 .name = "flags", .has_arg = required_argument},
2632 { .val = LFS_MIRROR_ID_OPT,
2633 .name = "mirror-id", .has_arg = required_argument},
2634 { .val = LFS_LAYOUT_COPY,
2635 .name = "copy", .has_arg = required_argument},
2636 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2637 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2638 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2639 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2640 { .val = 'd', .name = "delete", .has_arg = no_argument},
2641 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2642 /* --non-direct is only valid in migrate mode */
2643 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2644 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2645 { .val = 'E', .name = "component-end",
2646 .has_arg = required_argument},
2647 { .val = 'f', .name = "file", .has_arg = required_argument },
2648 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2649 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2650 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2651 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2652 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2653 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2654 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2655 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2656 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2657 { .val = 'L', .name = "layout", .has_arg = required_argument },
2658 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2659 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2660 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2661 /* --non-block is only valid in migrate mode */
2662 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2663 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2664 { .val = 'o', .name = "ost", .has_arg = required_argument },
2665 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2666 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2667 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2669 { .val = 'p', .name = "pool", .has_arg = required_argument },
2670 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2671 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2672 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2673 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2674 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2675 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2676 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2677 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2678 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2679 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2680 /* --verbose is only valid in migrate mode */
2681 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2682 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2685 setstripe_args_init(&lsa);
2687 migrate_mode = (opc == SO_MIGRATE);
2688 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2690 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2692 while ((c = getopt_long(argc, argv,
2693 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2699 case LFS_COMP_ADD_OPT:
2702 case LFS_COMP_DEL_OPT:
2705 case LFS_COMP_FLAGS_OPT:
2706 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2707 &lsa.lsa_comp_neg_flags);
2710 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2711 fprintf(stderr, "%s: inverted flags are not supported\n",
2715 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2717 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2724 case LFS_COMP_SET_OPT:
2727 case LFS_COMP_NO_VERIFY_OPT:
2728 mirror_flags |= MF_NO_VERIFY;
2730 case LFS_MIRROR_ID_OPT:
2731 mirror_id = strtoul(optarg, &end, 0);
2732 if (*end != '\0' || mirror_id == 0) {
2734 "%s %s: invalid mirror ID '%s'\n",
2735 progname, argv[0], optarg);
2739 case LFS_MIRROR_FLAGS_OPT: {
2742 if (!mirror_mode || !last_mirror) {
2743 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2748 result = comp_str2flags(optarg, &last_mirror->m_flags,
2754 fprintf(stderr, "%s: inverted flags are not supported\n",
2759 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2761 "%s: unsupported mirror flags: %s\n",
2768 case LFS_LAYOUT_COPY:
2773 if (!migrate_mode) {
2775 "%s %s: -b|--block valid only for migrate command\n",
2779 migration_block = true;
2782 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2785 "%s %s: invalid stripe count '%s'\n",
2786 progname, argv[0], optarg);
2790 if (lsa.lsa_stripe_count == -1)
2791 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2794 /* delete the default striping pattern */
2796 if (opc == SO_MIRROR_SPLIT) {
2799 "%s %s: -d cannot used with -f\n",
2803 mirror_flags |= MF_DESTROY;
2807 if (!migrate_mode) {
2809 "%s %s: -D|--non-direct is valid "
2810 "only for migrate command\n",
2814 migration_flags |= MIGRATION_NONDIRECT;
2817 if (lsa.lsa_comp_end != 0) {
2818 result = comp_args_to_layout(lpp, &lsa, true);
2821 "%s %s: invalid layout\n",
2826 setstripe_args_init_inherit(&lsa);
2829 if (arg_is_eof(optarg)) {
2830 lsa.lsa_comp_end = LUSTRE_EOF;
2832 result = llapi_parse_size(optarg,
2837 "%s %s: invalid component end '%s'\n",
2838 progname, argv[0], optarg);
2844 if (!migrate_mode) {
2845 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2849 lsa.lsa_pattern = check_hashtype(optarg);
2850 if (lsa.lsa_pattern == 0) {
2852 "%s %s: bad stripe hash type '%s'\n",
2853 progname, argv[0], optarg);
2858 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2861 "%s %s: invalid stripe offset '%s'\n",
2862 progname, argv[0], optarg);
2865 if (lsa.lsa_stripe_off == -1)
2866 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2869 comp_id = strtoul(optarg, &end, 0);
2870 if (*end != '\0' || comp_id == 0 ||
2871 comp_id > LCME_ID_MAX) {
2873 "%s %s: invalid component ID '%s'\n",
2874 progname, argv[0], optarg);
2879 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2881 "error: %s: invalid option: %s\n",
2882 progname, argv[optopt + 1]);
2885 if (opc == SO_MIRROR_EXTEND) {
2886 if (last_mirror == NULL) {
2888 "error: %s: '-N' must exist in front of '%s'\n",
2889 progname, argv[optopt + 1]);
2892 last_mirror->m_file = optarg;
2893 last_mirror->m_count = 1;
2896 if (mirror_list == NULL)
2897 mirror_list = lfs_mirror_alloc();
2898 mirror_list->m_file = optarg;
2903 if (strcmp(argv[optind - 1], "mdt") == 0) {
2904 /* Can be only the first component */
2905 if (layout != NULL) {
2907 fprintf(stderr, "error: 'mdt' layout "
2908 "can be only the first one\n");
2911 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2913 fprintf(stderr, "error: 'mdt' layout "
2914 "size is too big\n");
2917 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2918 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2920 fprintf(stderr, "error: layout '%s' is "
2921 "unknown, supported layouts are: "
2922 "'mdt', 'raid0'\n", argv[optind]);
2927 if (!migrate_mode) {
2929 "%s %s: -m|--mdt-index is valid only for migrate command\n",
2933 migrate_mdt_mode = true;
2934 lsa.lsa_nr_tgts = parse_targets(tgts,
2935 sizeof(tgts) / sizeof(__u32),
2936 lsa.lsa_nr_tgts, optarg);
2937 if (lsa.lsa_nr_tgts < 0) {
2939 "%s %s: invalid MDT target(s) '%s'\n",
2940 progname, argv[0], optarg);
2944 lsa.lsa_tgts = tgts;
2945 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2946 lsa.lsa_stripe_off = tgts[0];
2949 if (!migrate_mode) {
2951 "%s %s: -n|--non-block valid only for migrate command\n",
2955 migration_flags |= MIGRATION_NONBLOCK;
2958 if (opc == SO_SETSTRIPE) {
2959 opc = SO_MIRROR_CREATE;
2963 if (optarg != NULL) {
2964 mirror_count = strtoul(optarg, &end, 0);
2965 if (*end != '\0' || mirror_count == 0) {
2967 "error: %s: bad mirror count: %s\n",
2974 new_mirror = lfs_mirror_alloc();
2975 new_mirror->m_count = mirror_count;
2977 if (mirror_list == NULL)
2978 mirror_list = new_mirror;
2980 if (last_mirror != NULL) {
2981 /* wrap up last mirror */
2982 if (lsa.lsa_comp_end == 0)
2983 lsa.lsa_comp_end = LUSTRE_EOF;
2985 result = comp_args_to_layout(lpp, &lsa, true);
2987 lfs_mirror_free(new_mirror);
2991 setstripe_args_init_inherit(&lsa);
2993 last_mirror->m_next = new_mirror;
2996 last_mirror = new_mirror;
2997 lpp = &last_mirror->m_layout;
3000 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3001 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3002 fprintf(stderr, "warning: '--ost-list' is "
3003 "deprecated, use '--ost' instead\n");
3005 lsa.lsa_nr_tgts = parse_targets(tgts,
3006 sizeof(tgts) / sizeof(__u32),
3007 lsa.lsa_nr_tgts, optarg);
3008 if (lsa.lsa_nr_tgts < 0) {
3010 "%s %s: invalid OST target(s) '%s'\n",
3011 progname, argv[0], optarg);
3015 lsa.lsa_tgts = tgts;
3016 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3017 lsa.lsa_stripe_off = tgts[0];
3022 lsa.lsa_pool_name = optarg;
3024 if (strlen(lsa.lsa_pool_name) == 0 ||
3025 strncmp(lsa.lsa_pool_name, "none",
3026 LOV_MAXPOOLNAME) == 0)
3027 lsa.lsa_pool_name = NULL;
3030 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3034 "%s %s: invalid stripe size '%s'\n",
3035 progname, argv[0], optarg);
3040 if (!migrate_mode) {
3042 "%s %s: -v|--verbose valid only for migrate command\n",
3046 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3047 migration_flags = MIGRATION_VERBOSE;
3054 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3055 progname, argv[0], argv[optind - 1]);
3060 fname = argv[optind];
3062 if (optind == argc) {
3063 fprintf(stderr, "%s %s: FILE must be specified\n",
3068 if (mirror_mode && mirror_count == 0) {
3070 "error: %s: --mirror-count|-N option is required\n",
3077 if (lsa.lsa_comp_end == 0)
3078 lsa.lsa_comp_end = LUSTRE_EOF;
3081 if (lsa.lsa_comp_end != 0) {
3082 result = comp_args_to_layout(lpp, &lsa, true);
3087 if (mirror_flags & MF_NO_VERIFY) {
3088 if (opc != SO_MIRROR_EXTEND) {
3090 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3094 } else if (!has_m_file) {
3096 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3103 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3104 * altered by user space tool, so we don't need to support the
3105 * --component-set for this moment. */
3106 if (comp_set && !comp_id) {
3107 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3112 if ((delete + comp_set + comp_del + comp_add) > 1) {
3114 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3119 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3120 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3122 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3127 if ((comp_set || comp_del) &&
3128 (setstripe_args_specified(&lsa) || layout != NULL)) {
3130 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3135 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3137 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3142 if (comp_add || comp_del) {
3145 result = lstat(fname, &st);
3146 if (result == 0 && S_ISDIR(st.st_mode)) {
3148 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3154 fprintf(stderr, "error: %s: can't use --component-add "
3155 "or --component-del for mirror operation\n",
3162 if (layout == NULL) {
3164 "%s %s: option -E must be specified with --component-add\n",
3169 result = adjust_first_extent(fname, layout);
3170 if (result == -ENODATA)
3172 else if (result != 0)
3176 if (from_yaml && from_copy) {
3178 "%s: can't specify --yaml and --copy together\n",
3183 if ((from_yaml || from_copy) &&
3184 (setstripe_args_specified(&lsa) || layout != NULL)) {
3185 fprintf(stderr, "error: %s: can't specify --yaml with "
3186 "-c, -S, -i, -o, -p or -E options.\n",
3191 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3193 "%s %s: options --non-block and --block are mutually exclusive\n",
3198 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3201 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3206 if (migrate_mdt_mode) {
3207 struct lmv_user_md *lmu;
3209 /* initialize migrate mdt parameters */
3210 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3211 LMV_USER_MAGIC_SPECIFIC));
3214 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3215 progname, argv[0], strerror(ENOMEM));
3219 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3220 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3221 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3223 "%s %s: migrate should specify MDT index\n",
3228 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3229 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3230 lmu->lum_hash_type = lsa.lsa_pattern;
3232 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3233 if (lsa.lsa_pool_name)
3234 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3235 sizeof(lmu->lum_pool_name));
3236 if (lsa.lsa_nr_tgts > 1) {
3239 if (lsa.lsa_stripe_count > 0 &&
3240 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3241 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3243 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3244 progname, lsa.lsa_stripe_count,
3250 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3251 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3252 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3253 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3255 lmu->lum_magic = LMV_USER_MAGIC;
3258 migrate_mdt_param.fp_lmv_md = lmu;
3259 migrate_mdt_param.fp_migrate = 1;
3260 } else if (layout == NULL) {
3261 /* initialize stripe parameters */
3262 param = calloc(1, offsetof(typeof(*param),
3263 lsp_osts[lsa.lsa_nr_tgts]));
3264 if (param == NULL) {
3266 "%s %s: cannot allocate memory for parameters: %s\n",
3267 progname, argv[0], strerror(ENOMEM));
3272 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3273 param->lsp_stripe_size = lsa.lsa_stripe_size;
3274 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3275 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3276 param->lsp_stripe_count = -1;
3278 param->lsp_stripe_count = lsa.lsa_stripe_count;
3280 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3281 param->lsp_stripe_offset = -1;
3283 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3284 param->lsp_pool = lsa.lsa_pool_name;
3285 param->lsp_is_specific = false;
3286 if (lsa.lsa_nr_tgts > 0) {
3287 if (lsa.lsa_stripe_count > 0 &&
3288 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3289 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3290 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3291 fprintf(stderr, "error: %s: stripe count %lld "
3292 "doesn't match the number of OSTs: %d\n"
3293 , argv[0], lsa.lsa_stripe_count,
3299 param->lsp_is_specific = true;
3300 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3301 memcpy(param->lsp_osts, tgts,
3302 sizeof(*tgts) * lsa.lsa_nr_tgts);
3307 /* generate a layout from a YAML template */
3308 result = lfs_comp_create_from_yaml(template, &layout,
3311 fprintf(stderr, "error: %s: can't create composite "
3312 "layout from template file %s\n",
3316 } else if (from_copy) {
3317 layout = llapi_layout_get_by_path(template, 0);
3318 if (layout == NULL) {
3320 "%s: can't create composite layout from file %s.\n",
3321 progname, template);
3326 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3327 if (migrate_mdt_mode) {
3328 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3329 } else if (migrate_mode) {
3330 result = lfs_migrate(fname, migration_flags, param,
3332 } else if (comp_set != 0) {
3333 result = lfs_component_set(fname, comp_id,
3335 lsa.lsa_comp_neg_flags);
3336 } else if (comp_del != 0) {
3337 result = lfs_component_del(fname, comp_id,
3339 lsa.lsa_comp_neg_flags);
3340 } else if (comp_add != 0) {
3341 result = lfs_component_add(fname, layout);
3342 } else if (opc == SO_MIRROR_CREATE) {
3343 result = mirror_create(fname, mirror_list);
3344 } else if (opc == SO_MIRROR_EXTEND) {
3345 result = mirror_extend(fname, mirror_list,
3347 } else if (opc == SO_MIRROR_SPLIT) {
3348 if (mirror_id == 0 && comp_id == 0) {
3350 "%s %s: no mirror id or component id is specified\n",
3355 comp_id = mirror_id;
3357 mirror_flags |= MF_COMP_ID;
3358 result = mirror_split(fname, comp_id, mirror_flags,
3359 has_m_file ? mirror_list->m_file :
3361 } else if (layout != NULL) {
3362 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3369 result = llapi_file_open_param(fname,
3378 /* Save the first error encountered. */
3386 free(migrate_mdt_param.fp_lmv_md);
3387 llapi_layout_free(layout);
3388 lfs_mirror_list_free(mirror_list);
3393 llapi_layout_free(layout);
3394 lfs_mirror_list_free(mirror_list);
3398 static int lfs_poollist(int argc, char **argv)
3403 return llapi_poollist(argv[1]);
3406 static int set_time(time_t *time, time_t *set, char *str)
3414 else if (str[0] == '-')
3420 t = strtol(str, &endptr, 0);
3421 if (*endptr != '\0') {
3423 "%s find: bad time '%s': %s\n",
3424 progname, str, strerror(EINVAL));
3427 if (*time < t * 24 * 60 * 60) {
3431 "%s find: bad time '%s': too large\n",
3436 *set = *time - t * 24 * 60 * 60;
3440 static int name2uid(unsigned int *id, const char *name)
3442 struct passwd *passwd;
3444 passwd = getpwnam(name);
3447 *id = passwd->pw_uid;
3452 static int name2gid(unsigned int *id, const char *name)
3454 struct group *group;
3456 group = getgrnam(name);
3459 *id = group->gr_gid;
3464 static inline int name2projid(unsigned int *id, const char *name)
3469 static int uid2name(char **name, unsigned int id)
3471 struct passwd *passwd;
3473 passwd = getpwuid(id);
3476 *name = passwd->pw_name;
3481 static inline int gid2name(char **name, unsigned int id)
3483 struct group *group;
3485 group = getgrgid(id);
3488 *name = group->gr_name;
3493 static int name2layout(__u32 *layout, char *name)
3495 char *ptr, *layout_name;
3498 for (ptr = name; ; ptr = NULL) {
3499 layout_name = strtok(ptr, ",");
3500 if (layout_name == NULL)
3502 if (strcmp(layout_name, "released") == 0)
3503 *layout |= LOV_PATTERN_F_RELEASED;
3504 else if (strcmp(layout_name, "raid0") == 0)
3505 *layout |= LOV_PATTERN_RAID0;
3506 else if (strcmp(layout_name, "mdt") == 0)
3507 *layout |= LOV_PATTERN_MDT;
3514 static int lfs_find(int argc, char **argv)
3519 struct find_param param = {
3523 struct option long_opts[] = {
3524 { .val = 'A', .name = "atime", .has_arg = required_argument },
3525 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3526 { .val = LFS_COMP_COUNT_OPT,
3527 .name = "comp-count", .has_arg = required_argument },
3528 { .val = LFS_COMP_COUNT_OPT,
3529 .name = "component-count",
3530 .has_arg = required_argument },
3531 { .val = LFS_COMP_FLAGS_OPT,
3532 .name = "comp-flags", .has_arg = required_argument },
3533 { .val = LFS_COMP_FLAGS_OPT,
3534 .name = "component-flags",
3535 .has_arg = required_argument },
3536 { .val = LFS_COMP_START_OPT,
3537 .name = "comp-start", .has_arg = required_argument },
3538 { .val = LFS_COMP_START_OPT,
3539 .name = "component-start",
3540 .has_arg = required_argument },
3541 { .val = LFS_MIRROR_STATE_OPT,
3542 .name = "mirror-state", .has_arg = required_argument },
3543 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3544 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3545 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3546 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3547 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3548 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3549 { .val = 'E', .name = "component-end",
3550 .has_arg = required_argument },
3551 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3552 { .val = 'g', .name = "gid", .has_arg = required_argument },
3553 { .val = 'G', .name = "group", .has_arg = required_argument },
3554 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3555 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3556 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3557 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3558 { .val = 'L', .name = "layout", .has_arg = required_argument },
3559 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3560 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3561 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3562 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3563 { .val = 'n', .name = "name", .has_arg = required_argument },
3564 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3565 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3566 { .val = 'O', .name = "obd", .has_arg = required_argument },
3567 { .val = 'O', .name = "ost", .has_arg = required_argument },
3568 /* no short option for pool yet, can be 'p' after 2.18 */
3569 { .val = LFS_POOL_OPT,
3570 .name = "pool", .has_arg = required_argument },
3571 { .val = '0', .name = "print0", .has_arg = no_argument },
3572 { .val = 'P', .name = "print", .has_arg = no_argument },
3573 { .val = LFS_PROJID_OPT,
3574 .name = "projid", .has_arg = required_argument },
3575 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3576 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3577 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3578 { .val = 's', .name = "size", .has_arg = required_argument },
3579 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3580 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3581 { .val = 't', .name = "type", .has_arg = required_argument },
3582 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3583 { .val = 'u', .name = "uid", .has_arg = required_argument },
3584 { .val = 'U', .name = "user", .has_arg = required_argument },
3585 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3586 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3598 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3599 while ((c = getopt_long_only(argc, argv,
3600 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3601 long_opts, NULL)) >= 0) {
3606 /* '!' is part of option */
3607 /* when getopt_long_only() finds a string which is not
3608 * an option nor a known option argument it returns 1
3609 * in that case if we already have found pathstart and pathend
3610 * (i.e. we have the list of pathnames),
3611 * the only supported value is "!"
3613 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3614 if (!isoption && pathend != -1) {
3615 fprintf(stderr, "err: %s: filename|dirname must either "
3616 "precede options or follow options\n",
3621 if (!isoption && pathstart == -1)
3622 pathstart = optind - 1;
3623 if (isoption && pathstart != -1 && pathend == -1)
3624 pathend = optind - 2;
3630 /* unknown; opt is "!" or path component,
3631 * checking done above.
3633 if (strcmp(optarg, "!") == 0)
3637 xtime = ¶m.fp_atime;
3638 xsign = ¶m.fp_asign;
3639 param.fp_exclude_atime = !!neg_opt;
3640 /* no break, this falls through to 'C' for ctime */
3643 xtime = ¶m.fp_ctime;
3644 xsign = ¶m.fp_csign;
3645 param.fp_exclude_ctime = !!neg_opt;
3647 /* no break, this falls through to 'M' for mtime */
3650 xtime = ¶m.fp_mtime;
3651 xsign = ¶m.fp_msign;
3652 param.fp_exclude_mtime = !!neg_opt;
3654 rc = set_time(&t, xtime, optarg);
3655 if (rc == INT_MAX) {
3663 if (optarg[0] == '+') {
3664 param.fp_blocks_sign = -1;
3666 } else if (optarg[0] == '-') {
3667 param.fp_blocks_sign = 1;
3671 param.fp_blocks_units = 1024;
3672 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3673 ¶m.fp_blocks_units, 0);
3675 fprintf(stderr, "error: bad blocks '%s'\n",
3679 param.fp_check_blocks = 1;
3680 param.fp_exclude_blocks = !!neg_opt;
3682 case LFS_COMP_COUNT_OPT:
3683 if (optarg[0] == '+') {
3684 param.fp_comp_count_sign = -1;
3686 } else if (optarg[0] == '-') {
3687 param.fp_comp_count_sign = 1;
3691 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3692 if (*endptr != '\0') {
3693 fprintf(stderr, "error: bad component count "
3697 param.fp_check_comp_count = 1;
3698 param.fp_exclude_comp_count = !!neg_opt;
3700 case LFS_COMP_FLAGS_OPT:
3701 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3702 ¶m.fp_comp_neg_flags);
3704 fprintf(stderr, "error: bad component flags "
3708 param.fp_check_comp_flags = 1;
3710 __u32 flags = param.fp_comp_neg_flags;
3711 param.fp_comp_neg_flags = param.fp_comp_flags;
3712 param.fp_comp_flags = flags;
3715 case LFS_COMP_START_OPT:
3716 if (optarg[0] == '+') {
3717 param.fp_comp_start_sign = -1;
3719 } else if (optarg[0] == '-') {
3720 param.fp_comp_start_sign = 1;
3724 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3725 ¶m.fp_comp_start_units, 0);
3727 fprintf(stderr, "error: bad component start "
3731 param.fp_check_comp_start = 1;
3732 param.fp_exclude_comp_start = !!neg_opt;
3734 case LFS_MIRROR_STATE_OPT:
3735 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3736 ¶m.fp_mirror_neg_state);
3739 "error: bad mirrored file state '%s'\n",
3743 param.fp_check_mirror_state = 1;
3745 __u16 state = param.fp_mirror_neg_state;
3746 param.fp_mirror_neg_state =
3747 param.fp_mirror_state;
3748 param.fp_mirror_state = state;
3752 if (optarg[0] == '+') {
3753 param.fp_stripe_count_sign = -1;
3755 } else if (optarg[0] == '-') {
3756 param.fp_stripe_count_sign = 1;
3760 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3761 if (*endptr != '\0') {
3762 fprintf(stderr,"error: bad stripe_count '%s'\n",
3767 param.fp_check_stripe_count = 1;
3768 param.fp_exclude_stripe_count = !!neg_opt;
3771 param.fp_max_depth = strtol(optarg, 0, 0);
3774 if (optarg[0] == '+') {
3775 param.fp_comp_end_sign = -1;
3777 } else if (optarg[0] == '-') {
3778 param.fp_comp_end_sign = 1;
3782 if (arg_is_eof(optarg)) {
3783 param.fp_comp_end = LUSTRE_EOF;
3784 param.fp_comp_end_units = 1;
3787 rc = llapi_parse_size(optarg,
3789 ¶m.fp_comp_end_units, 0);
3792 fprintf(stderr, "error: bad component end "
3796 param.fp_check_comp_end = 1;
3797 param.fp_exclude_comp_end = !!neg_opt;
3801 rc = name2gid(¶m.fp_gid, optarg);
3803 param.fp_gid = strtoul(optarg, &endptr, 10);
3804 if (*endptr != '\0') {
3805 fprintf(stderr, "Group/GID: %s cannot "
3806 "be found.\n", optarg);
3811 param.fp_exclude_gid = !!neg_opt;
3812 param.fp_check_gid = 1;
3815 param.fp_hash_type = check_hashtype(optarg);
3816 if (param.fp_hash_type == 0) {
3817 fprintf(stderr, "error: bad hash_type '%s'\n",
3822 param.fp_check_hash_type = 1;
3823 param.fp_exclude_hash_type = !!neg_opt;
3826 ret = name2layout(¶m.fp_layout, optarg);
3829 param.fp_exclude_layout = !!neg_opt;
3830 param.fp_check_layout = 1;
3834 rc = name2uid(¶m.fp_uid, optarg);
3836 param.fp_uid = strtoul(optarg, &endptr, 10);
3837 if (*endptr != '\0') {
3838 fprintf(stderr, "User/UID: %s cannot "
3839 "be found.\n", optarg);
3844 param.fp_exclude_uid = !!neg_opt;
3845 param.fp_check_uid = 1;
3848 param.fp_pattern = (char *)optarg;
3849 param.fp_exclude_pattern = !!neg_opt;
3852 if (optarg[0] == '+') {
3853 param.fp_mirror_count_sign = -1;
3855 } else if (optarg[0] == '-') {
3856 param.fp_mirror_count_sign = 1;
3860 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3861 if (*endptr != '\0') {
3863 "error: bad mirror count '%s'\n",
3867 param.fp_check_mirror_count = 1;
3868 param.fp_exclude_mirror_count = !!neg_opt;
3873 char *buf, *token, *next, *p;
3877 buf = strdup(optarg);
3883 param.fp_exclude_obd = !!neg_opt;
3886 while (token && *token) {
3887 token = strchr(token, ',');
3894 param.fp_exclude_mdt = !!neg_opt;
3895 param.fp_num_alloc_mdts += len;
3896 tmp = realloc(param.fp_mdt_uuid,
3897 param.fp_num_alloc_mdts *
3898 sizeof(*param.fp_mdt_uuid));
3904 param.fp_mdt_uuid = tmp;
3906 param.fp_exclude_obd = !!neg_opt;
3907 param.fp_num_alloc_obds += len;
3908 tmp = realloc(param.fp_obd_uuid,
3909 param.fp_num_alloc_obds *
3910 sizeof(*param.fp_obd_uuid));
3916 param.fp_obd_uuid = tmp;
3918 for (token = buf; token && *token; token = next) {
3919 struct obd_uuid *puuid;
3922 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3925 ¶m.fp_obd_uuid[param.fp_num_obds++];
3927 p = strchr(token, ',');
3934 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3939 strncpy(puuid->uuid, token,
3940 sizeof(puuid->uuid));
3947 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3951 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3953 "Pool name %s is too long (max %d)\n",
3954 optarg, LOV_MAXPOOLNAME);
3959 * We do check for empty pool because empty pool
3960 * is used to find V1 LOV attributes
3962 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3963 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3964 param.fp_exclude_pool = !!neg_opt;
3965 param.fp_check_pool = 1;
3967 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3968 case 'p': /* want this for --pool, to match getstripe/find */
3970 "warning: -p deprecated, use --print0 or -0\n");
3973 param.fp_zero_end = 1;
3975 case 'P': /* we always print, this option is a no-op */
3977 case LFS_PROJID_OPT:
3978 rc = name2projid(¶m.fp_projid, optarg);
3980 param.fp_projid = strtoul(optarg, &endptr, 10);
3981 if (*endptr != '\0') {
3983 "Invalid project ID: %s",
3989 param.fp_exclude_projid = !!neg_opt;
3990 param.fp_check_projid = 1;
3993 if (optarg[0] == '+') {
3994 param.fp_size_sign = -1;
3996 } else if (optarg[0] == '-') {
3997 param.fp_size_sign = 1;
4001 ret = llapi_parse_size(optarg, ¶m.fp_size,
4002 ¶m.fp_size_units, 0);
4004 fprintf(stderr, "error: bad file size '%s'\n",
4008 param.fp_check_size = 1;
4009 param.fp_exclude_size = !!neg_opt;
4012 if (optarg[0] == '+') {
4013 param.fp_stripe_size_sign = -1;
4015 } else if (optarg[0] == '-') {
4016 param.fp_stripe_size_sign = 1;
4020 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4021 ¶m.fp_stripe_size_units, 0);
4023 fprintf(stderr, "error: bad stripe_size '%s'\n",
4027 param.fp_check_stripe_size = 1;
4028 param.fp_exclude_stripe_size = !!neg_opt;
4031 param.fp_exclude_type = !!neg_opt;
4032 switch (optarg[0]) {
4034 param.fp_type = S_IFBLK;
4037 param.fp_type = S_IFCHR;
4040 param.fp_type = S_IFDIR;
4043 param.fp_type = S_IFREG;
4046 param.fp_type = S_IFLNK;
4049 param.fp_type = S_IFIFO;
4052 param.fp_type = S_IFSOCK;
4055 fprintf(stderr, "error: %s: bad type '%s'\n",
4062 if (optarg[0] == '+') {
4063 param.fp_mdt_count_sign = -1;
4065 } else if (optarg[0] == '-') {
4066 param.fp_mdt_count_sign = 1;
4070 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4071 if (*endptr != '\0') {
4072 fprintf(stderr, "error: bad mdt_count '%s'\n",
4077 param.fp_check_mdt_count = 1;
4078 param.fp_exclude_mdt_count = !!neg_opt;
4086 if (pathstart == -1) {
4087 fprintf(stderr, "error: %s: no filename|pathname\n",
4091 } else if (pathend == -1) {
4097 rc = llapi_find(argv[pathstart], ¶m);
4098 if (rc != 0 && ret == 0)
4100 } while (++pathstart < pathend);
4103 fprintf(stderr, "error: %s failed for %s.\n",
4104 argv[0], argv[optind - 1]);
4106 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4107 free(param.fp_obd_uuid);
4109 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4110 free(param.fp_mdt_uuid);
4115 static int lfs_getstripe_internal(int argc, char **argv,
4116 struct find_param *param)
4118 struct option long_opts[] = {
4119 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4120 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4121 { .val = LFS_COMP_COUNT_OPT,
4122 .name = "comp-count", .has_arg = no_argument },
4123 { .val = LFS_COMP_COUNT_OPT,
4124 .name = "component-count", .has_arg = no_argument },
4125 { .val = LFS_COMP_FLAGS_OPT,
4126 .name = "comp-flags", .has_arg = optional_argument },
4127 { .val = LFS_COMP_FLAGS_OPT,
4128 .name = "component-flags", .has_arg = optional_argument },
4129 { .val = LFS_COMP_START_OPT,
4130 .name = "comp-start", .has_arg = optional_argument },
4131 { .val = LFS_COMP_START_OPT,
4132 .name = "component-start", .has_arg = optional_argument },
4133 { .val = LFS_MIRROR_INDEX_OPT,
4134 .name = "mirror-index", .has_arg = required_argument },
4135 { .val = LFS_MIRROR_ID_OPT,
4136 .name = "mirror-id", .has_arg = required_argument },
4137 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4138 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4139 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4140 { .val = 'd', .name = "directory", .has_arg = no_argument },
4141 { .val = 'D', .name = "default", .has_arg = no_argument },
4142 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4143 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4144 { .val = 'F', .name = "fid", .has_arg = no_argument },
4145 { .val = 'g', .name = "generation", .has_arg = no_argument },
4146 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4147 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4148 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4149 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4150 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4151 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4152 { .val = 'L', .name = "layout", .has_arg = no_argument },
4153 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4154 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4155 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4156 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4157 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4158 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4159 { .val = 'O', .name = "obd", .has_arg = required_argument },
4160 { .val = 'O', .name = "ost", .has_arg = required_argument },
4161 { .val = 'p', .name = "pool", .has_arg = no_argument },
4162 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4163 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4164 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4165 { .val = 'R', .name = "raw", .has_arg = no_argument },
4166 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4167 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4168 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4169 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4170 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4171 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4172 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4173 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4177 int pathstart = -1, pathend = -1;
4181 while ((c = getopt_long(argc, argv,
4182 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4183 long_opts, NULL)) != -1) {
4187 /* '!' is part of option */
4188 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4189 if (!isoption && pathend != -1) {
4191 "error: %s: filename|dirname must either precede options or follow options\n",
4195 if (!isoption && pathstart == -1)
4196 pathstart = optind - 1;
4197 if (isoption && pathstart != -1 && pathend == -1)
4198 pathend = optind - 2;
4202 /* unknown: opt is "!" */
4203 if (strcmp(optarg, "!") == 0)
4207 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4208 param->fp_verbose |= VERBOSE_COUNT;
4209 param->fp_max_depth = 0;
4212 case LFS_COMP_COUNT_OPT:
4213 param->fp_verbose |= VERBOSE_COMP_COUNT;
4214 param->fp_max_depth = 0;
4216 case LFS_COMP_FLAGS_OPT:
4217 if (optarg != NULL) {
4218 rc = comp_str2flags(optarg,
4219 ¶m->fp_comp_flags,
4220 ¶m->fp_comp_neg_flags);
4222 fprintf(stderr, "error: %s bad "
4223 "component flags '%s'.\n",
4227 param->fp_check_comp_flags = 1;
4229 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4230 param->fp_max_depth = 0;
4233 case LFS_COMP_START_OPT:
4234 if (optarg != NULL) {
4236 if (tmp[0] == '+') {
4237 param->fp_comp_start_sign = -1;
4239 } else if (tmp[0] == '-') {
4240 param->fp_comp_start_sign = 1;
4243 rc = llapi_parse_size(tmp,
4244 ¶m->fp_comp_start,
4245 ¶m->fp_comp_start_units, 0);
4247 fprintf(stderr, "error: %s bad "
4248 "component start '%s'.\n",
4252 param->fp_check_comp_start = 1;
4255 param->fp_verbose |= VERBOSE_COMP_START;
4256 param->fp_max_depth = 0;
4259 case LFS_MIRROR_INDEX_OPT:
4260 if (optarg[0] == '+') {
4261 param->fp_mirror_index_sign = -1;
4263 } else if (optarg[0] == '-') {
4264 param->fp_mirror_index_sign = 1;
4268 param->fp_mirror_index = strtoul(optarg, &end, 0);
4269 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4270 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4272 "%s %s: invalid mirror index '%s'\n",
4273 progname, argv[0], optarg);
4276 if (param->fp_mirror_id != 0) {
4278 "%s %s: can't specify both mirror index and mirror ID\n",
4282 param->fp_check_mirror_index = 1;
4283 param->fp_exclude_mirror_index = !!neg_opt;
4285 case LFS_MIRROR_ID_OPT:
4286 if (optarg[0] == '+') {
4287 param->fp_mirror_id_sign = -1;
4289 } else if (optarg[0] == '-') {
4290 param->fp_mirror_id_sign = 1;
4294 param->fp_mirror_id = strtoul(optarg, &end, 0);
4295 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4296 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4298 "%s %s: invalid mirror ID '%s'\n",
4299 progname, argv[0], optarg);
4302 if (param->fp_mirror_index != 0) {
4304 "%s %s: can't specify both mirror index and mirror ID\n",
4308 param->fp_check_mirror_id = 1;
4309 param->fp_exclude_mirror_id = !!neg_opt;
4312 param->fp_max_depth = 0;
4315 param->fp_get_default_lmv = 1;
4318 if (optarg != NULL) {
4320 if (tmp[0] == '+') {
4321 param->fp_comp_end_sign = -1;
4323 } else if (tmp[0] == '-') {
4324 param->fp_comp_end_sign = 1;
4328 if (arg_is_eof(tmp)) {
4329 param->fp_comp_end = LUSTRE_EOF;
4330 param->fp_comp_end_units = 1;
4333 rc = llapi_parse_size(tmp,
4334 ¶m->fp_comp_end,
4335 ¶m->fp_comp_end_units, 0);
4338 fprintf(stderr, "error: %s bad "
4339 "component end '%s'.\n",
4343 param->fp_check_comp_end = 1;
4345 param->fp_verbose |= VERBOSE_COMP_END;
4346 param->fp_max_depth = 0;
4350 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4351 param->fp_verbose |= VERBOSE_DFID;
4352 param->fp_max_depth = 0;
4356 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4357 param->fp_verbose |= VERBOSE_GENERATION;
4358 param->fp_max_depth = 0;
4362 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4363 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4364 param->fp_max_depth = 0;
4368 if (optarg != NULL) {
4369 param->fp_comp_id = strtoul(optarg, &end, 0);
4370 if (*end != '\0' || param->fp_comp_id == 0 ||
4371 param->fp_comp_id > LCME_ID_MAX) {
4372 fprintf(stderr, "error: %s bad "
4373 "component id '%s'\n",
4377 param->fp_check_comp_id = 1;
4380 param->fp_max_depth = 0;
4381 param->fp_verbose |= VERBOSE_COMP_ID;
4385 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4386 param->fp_verbose |= VERBOSE_PATTERN;
4387 param->fp_max_depth = 0;
4390 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4392 fprintf(stderr, "warning: '-M' deprecated"
4393 ", use '--mdt-index' or '-m' instead\n");
4396 if (!(param->fp_verbose & VERBOSE_DETAIL))
4397 param->fp_max_depth = 0;
4398 param->fp_verbose |= VERBOSE_MDTINDEX;
4401 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4402 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4403 param->fp_max_depth = 0;
4407 if (param->fp_obd_uuid) {
4409 "error: %s: only one obduuid allowed",
4413 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4416 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4417 param->fp_verbose |= VERBOSE_POOL;
4418 param->fp_max_depth = 0;
4425 param->fp_recursive = 1;
4431 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4432 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4433 param->fp_max_depth = 0;
4437 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4447 if (pathstart == -1) {
4448 fprintf(stderr, "error: %s: no filename|pathname\n",
4451 } else if (pathend == -1) {
4459 if (param->fp_recursive)
4460 param->fp_max_depth = -1;
4461 else if (param->fp_verbose & VERBOSE_DETAIL)
4462 param->fp_max_depth = 1;
4464 if (!param->fp_verbose)
4465 param->fp_verbose = VERBOSE_DEFAULT;
4466 if (param->fp_quiet)
4467 param->fp_verbose = VERBOSE_OBJID;
4470 rc = llapi_getstripe(argv[pathstart], param);
4471 } while (++pathstart < pathend && !rc);
4474 fprintf(stderr, "error: %s failed for %s.\n",
4475 argv[0], argv[optind - 1]);
4479 static int lfs_tgts(int argc, char **argv)
4481 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4482 struct find_param param;
4483 int index = 0, rc=0;
4488 if (argc == 2 && !realpath(argv[1], path)) {
4490 fprintf(stderr, "error: invalid path '%s': %s\n",
4491 argv[1], strerror(-rc));
4495 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4496 /* Check if we have a mount point */
4497 if (mntdir[0] == '\0')
4500 memset(¶m, 0, sizeof(param));
4501 if (!strcmp(argv[0], "mdts"))
4502 param.fp_get_lmv = 1;
4504 rc = llapi_ostlist(mntdir, ¶m);
4506 fprintf(stderr, "error: %s: failed on %s\n",
4509 if (path[0] != '\0')
4511 memset(mntdir, 0, PATH_MAX);
4517 static int lfs_getstripe(int argc, char **argv)
4519 struct find_param param = { 0 };
4521 param.fp_max_depth = 1;
4522 return lfs_getstripe_internal(argc, argv, ¶m);
4526 static int lfs_getdirstripe(int argc, char **argv)
4528 struct find_param param = { 0 };
4529 struct option long_opts[] = {
4530 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4531 { .val = 'D', .name = "default", .has_arg = no_argument },
4532 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4533 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4534 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4535 { .val = 'O', .name = "obd", .has_arg = required_argument },
4536 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4537 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4538 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4542 param.fp_get_lmv = 1;
4544 while ((c = getopt_long(argc, argv,
4545 "cDHimO:rtTy", long_opts, NULL)) != -1)
4550 param.fp_verbose |= VERBOSE_COUNT;
4553 param.fp_get_default_lmv = 1;
4557 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4559 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4561 fprintf(stderr, "warning: '-t' deprecated, "
4562 "use '--mdt-hash' or '-H' instead\n");
4565 param.fp_verbose |= VERBOSE_HASH_TYPE;
4568 if (param.fp_obd_uuid) {
4570 "error: %s: only one obduuid allowed",
4574 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4577 param.fp_recursive = 1;
4590 if (param.fp_recursive)
4591 param.fp_max_depth = -1;
4593 if (!param.fp_verbose)
4594 param.fp_verbose = VERBOSE_DEFAULT;
4597 rc = llapi_getstripe(argv[optind], ¶m);
4598 } while (++optind < argc && !rc);
4601 fprintf(stderr, "error: %s failed for %s.\n",
4602 argv[0], argv[optind - 1]);
4607 MNTDF_INODES = 0x0001,
4608 MNTDF_COOKED = 0x0002,
4609 MNTDF_LAZY = 0x0004,
4610 MNTDF_VERBOSE = 0x0008,
4611 MNTDF_SHOW = 0x0010,
4614 #define COOK(value) \
4617 while (value > 1024) { \
4625 #define CDF "%11llu"
4626 #define HDF "%8.1f%c"
4630 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
4632 double avail, used, ratio = 0;
4635 avail = st->os_ffree;
4636 used = st->os_files - st->os_ffree;
4638 avail = st->os_bavail;
4639 used = st->os_blocks - st->os_bfree;
4641 if (avail + used > 0)
4642 ratio = used / (used + avail) * 100;
4644 /* Round up to match df(1) usage percentage */
4645 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
4648 static int showdf(char *mntdir, struct obd_statfs *stat,
4649 char *uuid, enum mntdf_flags flags,
4650 char *type, int index, int rc)
4652 long long avail, used, total;
4654 char *suffix = "KMGTPEZY";
4655 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4656 char tbuf[3 * sizeof(__u64)];
4657 char ubuf[3 * sizeof(__u64)];
4658 char abuf[3 * sizeof(__u64)];
4659 char rbuf[3 * sizeof(__u64)];
4666 if (flags & MNTDF_INODES) {
4667 avail = stat->os_ffree;
4668 used = stat->os_files - stat->os_ffree;
4669 total = stat->os_files;
4671 int shift = flags & MNTDF_COOKED ? 0 : 10;
4673 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4674 used = ((stat->os_blocks - stat->os_bfree) *
4675 stat->os_bsize) >> shift;
4676 total = (stat->os_blocks * stat->os_bsize) >> shift;
4679 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
4681 if (flags & MNTDF_COOKED) {
4685 cook_val = (double)total;
4688 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4691 snprintf(tbuf, sizeof(tbuf), CDF, total);
4693 cook_val = (double)used;
4696 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4699 snprintf(ubuf, sizeof(ubuf), CDF, used);
4701 cook_val = (double)avail;
4704 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4707 snprintf(abuf, sizeof(abuf), CDF, avail);
4709 snprintf(tbuf, sizeof(tbuf), CDF, total);
4710 snprintf(ubuf, sizeof(tbuf), CDF, used);
4711 snprintf(abuf, sizeof(tbuf), CDF, avail);
4714 sprintf(rbuf, RDF, ratio);
4715 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4716 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4718 printf("[%s:%d]", type, index);
4720 if (stat->os_state) {
4722 * Each character represents the matching
4725 const char state_names[] = "DRSI";
4730 for (i = 0, state = stat->os_state;
4731 state && i < sizeof(state_names); i++) {
4732 if (!(state & (1 << i)))
4734 printf("%c", state_names[i]);
4742 printf(UUF": inactive device\n", uuid);
4745 printf(UUF": %s\n", uuid, strerror(-rc));
4752 struct ll_stat_type {
4757 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4759 struct ll_statfs_data {
4761 struct obd_statfs sd_st;
4764 struct ll_statfs_buf {
4766 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4769 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4770 int ops, struct ll_statfs_buf *lsb)
4772 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4773 struct obd_uuid uuid_buf;
4774 char *poolname = NULL;
4775 struct ll_stat_type types[] = {
4776 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4777 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4778 { .st_name = NULL } };
4779 struct ll_stat_type *tp;
4780 __u64 ost_ffree = 0;
4788 poolname = strchr(pool, '.');
4789 if (poolname != NULL) {
4790 if (strncmp(fsname, pool, strlen(fsname))) {
4791 fprintf(stderr, "filesystem name incorrect\n");
4799 fd = open(mntdir, O_RDONLY);
4802 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4807 if (flags & MNTDF_SHOW) {
4808 if (flags & MNTDF_INODES)
4809 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4810 "UUID", "Inodes", "IUsed", "IFree",
4811 "IUse%", "Mounted on");
4813 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4815 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4816 "Used", "Available", "Use%", "Mounted on");
4819 for (tp = types; tp->st_name != NULL; tp++) {
4820 bool have_ost = false;
4822 if (!(tp->st_op & ops))
4825 for (index = 0; ; index++) {
4826 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4827 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4828 type = flags & MNTDF_LAZY ?
4829 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4830 rc2 = llapi_obd_fstatfs(fd, type, index,
4831 &stat_buf, &uuid_buf);
4836 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4837 if (!(flags & MNTDF_VERBOSE))
4839 } else if (rc2 < 0 && rc == 0) {
4843 /* If we have OSTs then don't report MDT block counts.
4844 * For MDT-only filesystems the expectation is that all
4845 * layouts have a DoM component. For filesystems with
4846 * OSTs, files are not necessarily going to store data
4847 * on MDTs, and MDT space is limited to a fraction of
4848 * OST space, so don't include it in the summary.
4850 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
4857 if (poolname && tp->st_op == LL_STATFS_LOV &&
4858 llapi_search_ost(fsname, poolname,
4859 obd_uuid2str(&uuid_buf)) != 1)
4862 /* the llapi_obd_statfs() call may have returned with
4863 * an error, but if it filled in uuid_buf we will at
4864 * lease use that to print out a message for that OBD.
4865 * If we didn't get anything in the uuid_buf, then fill
4866 * it in so that we can print an error message. */
4867 if (uuid_buf.uuid[0] == '\0')
4868 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4869 "%s%04x", tp->st_name, index);
4871 lsb->sb_buf[lsb->sb_count].sd_index = index;
4872 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4875 if (flags & MNTDF_SHOW)
4876 showdf(mntdir, &stat_buf,
4877 obd_uuid2str(&uuid_buf), flags,
4878 tp->st_name, index, rc2);
4883 if (tp->st_op == LL_STATFS_LMV) {
4884 sum.os_ffree += stat_buf.os_ffree;
4885 sum.os_files += stat_buf.os_files;
4886 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4887 ost_ffree += stat_buf.os_ffree;
4889 sum.os_blocks += stat_buf.os_blocks *
4891 sum.os_bfree += stat_buf.os_bfree *
4893 sum.os_bavail += stat_buf.os_bavail *
4900 /* If we don't have as many objects free on the OST as inodes
4901 * on the MDS, we reduce the total number of inodes to
4902 * compensate, so that the "inodes in use" number is correct.
4903 * Matches ll_statfs_internal() so the results are consistent. */
4904 if (ost_ffree < sum.os_ffree) {
4905 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4906 sum.os_ffree = ost_ffree;
4908 if (flags & MNTDF_SHOW) {
4910 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4917 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4919 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4921 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4923 int r1 = obd_statfs_ratio(st1, false);
4924 int r2 = obd_statfs_ratio(st2, false);
4925 int64_t result = r1 - r2;
4927 /* if both space usage are above 90, compare free inodes */
4928 if (r1 > 90 && r2 > 90)
4929 result = st2->os_ffree - st1->os_ffree;
4933 else if (result == 0)
4940 static int lfs_setdirstripe(int argc, char **argv)
4944 struct lfs_setstripe_args lsa = { 0 };
4945 struct llapi_stripe_param *param = NULL;
4946 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4949 char *mode_opt = NULL;
4950 bool default_stripe = false;
4951 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4952 mode_t previous_mode = 0;
4953 bool delete = false;
4954 struct ll_statfs_buf *lsb = NULL;
4955 char mntdir[PATH_MAX] = "";
4956 bool auto_distributed = false;
4958 struct option long_opts[] = {
4959 { .val = 'c', .name = "count", .has_arg = required_argument },
4960 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4961 { .val = 'd', .name = "delete", .has_arg = no_argument },
4962 { .val = 'D', .name = "default", .has_arg = no_argument },
4963 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4964 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4965 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
4966 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4967 { .val = 'i', .name = "mdt", .has_arg = required_argument },
4969 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4970 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4972 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4973 { .val = 'i', .name = "index", .has_arg = required_argument },
4975 { .val = 'o', .name = "mode", .has_arg = required_argument },
4976 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4977 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4979 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4980 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4983 setstripe_args_init(&lsa);
4985 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4993 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4996 "%s %s: invalid stripe count '%s'\n",
4997 progname, argv[0], optarg);
5003 default_stripe = true;
5006 default_stripe = true;
5008 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5010 fprintf(stderr, "warning: '--hash-type' and '-t' "
5011 "deprecated, use '--mdt-hash' or '-H' instead\n");
5014 lsa.lsa_pattern = check_hashtype(optarg);
5015 if (lsa.lsa_pattern == 0) {
5017 "%s %s: bad stripe hash type '%s'\n",
5018 progname, argv[0], optarg);
5023 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5026 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5027 if (strcmp(argv[optind - 1], "--index") == 0)
5029 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5032 lsa.lsa_nr_tgts = parse_targets(mdts,
5033 sizeof(mdts) / sizeof(__u32),
5034 lsa.lsa_nr_tgts, optarg);
5035 if (lsa.lsa_nr_tgts < 0) {
5037 "%s %s: invalid MDT target(s) '%s'\n",
5038 progname, argv[0], optarg);
5042 lsa.lsa_tgts = mdts;
5043 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5044 lsa.lsa_stripe_off = mdts[0];
5046 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5048 fprintf(stderr, "warning: '-m' is deprecated, "
5049 "use '--mode' or '-o' instead\n");
5055 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5056 progname, argv[0], argv[optind - 1]);
5061 if (optind == argc) {
5062 fprintf(stderr, "%s %s: DIR must be specified\n",
5067 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5068 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5070 "%s %s: stripe offset and count must be specified\n",
5076 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5077 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5079 "%s %s: cannot specify -d with -c or -i options\n",
5084 if (mode_opt != NULL) {
5085 mode = strtoul(mode_opt, &end, 8);
5088 "%s %s: bad MODE '%s'\n",
5089 progname, argv[0], mode_opt);
5092 previous_mode = umask(0);
5096 * initialize stripe parameters, in case param is converted to specific,
5097 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5099 param = calloc(1, offsetof(typeof(*param),
5100 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5101 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5102 if (param == NULL) {
5104 "%s %s: cannot allocate memory for parameters: %s\n",
5105 progname, argv[0], strerror(ENOMEM));
5109 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5110 param->lsp_stripe_count = lsa.lsa_stripe_count;
5111 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5112 param->lsp_stripe_offset = -1;
5114 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5115 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5116 param->lsp_stripe_pattern = lsa.lsa_pattern;
5118 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5119 param->lsp_pool = lsa.lsa_pool_name;
5120 param->lsp_is_specific = false;
5121 if (lsa.lsa_nr_tgts > 1) {
5122 if (lsa.lsa_stripe_count > 0 &&
5123 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5124 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5125 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5126 "match the number of MDTs: %d\n",
5127 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5132 param->lsp_is_specific = true;
5133 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5134 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5137 dname = argv[optind];
5139 if (default_stripe) {
5140 result = llapi_dir_set_default_lmv(dname, param);
5142 /* if current \a dname isn't under the same \a mntdir
5143 * as the last one, and the last one was
5144 * auto-distributed, restore \a param.
5146 if (mntdir[0] != '\0' &&
5147 strncmp(dname, mntdir, strlen(mntdir)) &&
5149 param->lsp_is_specific = false;
5150 param->lsp_stripe_offset = -1;
5151 auto_distributed = false;
5154 if (!param->lsp_is_specific &&
5155 param->lsp_stripe_offset == -1) {
5156 char path[PATH_MAX] = "";
5159 lsb = malloc(sizeof(*lsb));
5167 /* use mntdir for dirname() temporarily */
5168 strncpy(mntdir, dname, sizeof(mntdir));
5169 if (!realpath(dirname(mntdir), path)) {
5172 "error: invalid path '%s': %s\n",
5173 argv[optind], strerror(errno));
5178 result = llapi_search_mounts(path, 0, mntdir,
5180 if (result < 0 || mntdir[0] == '\0') {
5182 "No suitable Lustre mount found\n");
5186 result = mntdf(mntdir, NULL, NULL, 0,
5187 LL_STATFS_LMV, lsb);
5191 if (param->lsp_stripe_count > lsb->sb_count) {
5193 "error: stripe count %d is too big\n",
5194 param->lsp_stripe_count);
5199 qsort(lsb->sb_buf, lsb->sb_count,
5200 sizeof(struct ll_statfs_data),
5201 ll_statfs_data_comp);
5203 auto_distributed = true;
5206 if (auto_distributed) {
5208 int nr = MAX(param->lsp_stripe_count,
5211 /* don't use server whose usage is above 90% */
5212 while (nr != param->lsp_stripe_count &&
5213 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st,
5215 nr = MAX(param->lsp_stripe_count,
5218 /* get \a r between [0, nr) */
5221 param->lsp_stripe_offset =
5222 lsb->sb_buf[r].sd_index;
5223 if (param->lsp_stripe_count > 1) {
5226 param->lsp_is_specific = true;
5227 for (; i < param->lsp_stripe_count; i++)
5228 param->lsp_tgts[(i + r) % nr] =
5229 lsb->sb_buf[i].sd_index;
5233 result = llapi_dir_create(dname, mode, param);
5238 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5239 progname, dname, strerror(-result));
5242 dname = argv[++optind];
5243 } while (dname != NULL);
5245 if (mode_opt != NULL)
5246 umask(previous_mode);
5254 static int lfs_rmentry(int argc, char **argv)
5261 fprintf(stderr, "error: %s: missing dirname\n",
5267 dname = argv[index];
5268 while (dname != NULL) {
5269 result = llapi_direntry_remove(dname);
5271 fprintf(stderr, "error: %s: remove dir entry '%s' "
5272 "failed\n", argv[0], dname);
5275 dname = argv[++index];
5280 static int lfs_mv(int argc, char **argv)
5282 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5283 struct find_param param = {
5290 struct option long_opts[] = {
5291 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5292 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5293 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5296 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5298 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5300 fprintf(stderr, "warning: '-M' deprecated"
5301 ", use '--mdt-index' or '-m' instead\n");
5304 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5306 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5312 param.fp_verbose = VERBOSE_DETAIL;
5315 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5316 progname, argv[optind - 1]);
5321 if (lmu.lum_stripe_offset == -1) {
5322 fprintf(stderr, "%s mv: MDT index must be specified\n",
5327 if (optind >= argc) {
5328 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5333 /* initialize migrate mdt parameters */
5334 param.fp_lmv_md = &lmu;
5335 param.fp_migrate = 1;
5336 rc = llapi_migrate_mdt(argv[optind], ¶m);
5338 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5339 progname, argv[optind], param.fp_mdt_index,
5344 static int lfs_osts(int argc, char **argv)
5346 return lfs_tgts(argc, argv);
5349 static int lfs_mdts(int argc, char **argv)
5351 return lfs_tgts(argc, argv);
5354 static int lfs_df(int argc, char **argv)
5356 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5357 enum mntdf_flags flags = MNTDF_SHOW;
5358 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5359 int c, rc = 0, index = 0;
5360 char fsname[PATH_MAX] = "", *pool_name = NULL;
5361 struct option long_opts[] = {
5362 { .val = 'h', .name = "human-readable",
5363 .has_arg = no_argument },
5364 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5365 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5366 { .val = 'p', .name = "pool", .has_arg = required_argument },
5367 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5370 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5373 flags |= MNTDF_COOKED;
5376 flags |= MNTDF_INODES;
5379 flags |= MNTDF_LAZY;
5385 flags |= MNTDF_VERBOSE;
5391 if (optind < argc && !realpath(argv[optind], path)) {
5393 fprintf(stderr, "error: invalid path '%s': %s\n",
5394 argv[optind], strerror(-rc));
5398 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5399 /* Check if we have a mount point */
5400 if (mntdir[0] == '\0')
5403 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5404 if (rc || path[0] != '\0')
5406 fsname[0] = '\0'; /* avoid matching in next loop */
5407 mntdir[0] = '\0'; /* avoid matching in next loop */
5413 static int lfs_getname(int argc, char **argv)
5415 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5416 int rc = 0, index = 0, c;
5417 char buf[sizeof(struct obd_uuid)];
5419 while ((c = getopt(argc, argv, "h")) != -1)
5422 if (optind == argc) { /* no paths specified, get all paths. */
5423 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5424 rc = llapi_getname(mntdir, buf, sizeof(buf));
5427 "cannot get name for `%s': %s\n",
5428 mntdir, strerror(-rc));
5432 printf("%s %s\n", buf, mntdir);
5434 path[0] = fsname[0] = mntdir[0] = 0;
5436 } else { /* paths specified, only attempt to search these. */
5437 for (; optind < argc; optind++) {
5438 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5441 "cannot get name for `%s': %s\n",
5442 argv[optind], strerror(-rc));
5446 printf("%s %s\n", buf, argv[optind]);
5452 static int lfs_check(int argc, char **argv)
5454 char mntdir[PATH_MAX] = {'\0'};
5463 fprintf(stderr, "%s check: server type must be specified\n",
5468 obd_types[0] = obd_type1;
5469 obd_types[1] = obd_type2;
5470 obd_types[2] = obd_type3;
5472 if (strcmp(argv[1], "osts") == 0) {
5473 strcpy(obd_types[0], "osc");
5474 } else if (strcmp(argv[1], "mdts") == 0 ||
5475 strcmp(argv[1], "mds") == 0) {
5476 strcpy(obd_types[0], "mdc");
5477 } else if (strcmp(argv[1], "mgts") == 0) {
5478 strcpy(obd_types[0], "mgc");
5479 } else if (strcmp(argv[1], "all") == 0 ||
5480 strcmp(argv[1], "servers") == 0) {
5482 strcpy(obd_types[0], "osc");
5483 strcpy(obd_types[1], "mdc");
5484 strcpy(obd_types[2], "mgc");
5486 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5491 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5492 if (rc < 0 || mntdir[0] == '\0') {
5494 "%s check: cannot find mounted Lustre filesystem: %s\n",
5495 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5499 rc = llapi_target_check(num_types, obd_types, mntdir);
5501 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5502 progname, argv[1], strerror(-rc));
5508 #ifdef HAVE_SYS_QUOTA_H
5509 #define ARG2INT(nr, str, msg) \
5512 nr = strtol(str, &endp, 0); \
5513 if (*endp != '\0') { \
5514 fprintf(stderr, "%s: bad %s '%s'\n", \
5515 progname, msg, str); \
5520 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5522 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5523 * returns the value or ULONG_MAX on integer overflow or incorrect format
5525 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5526 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5527 * 3. empty integer value is interpreted as 0
5529 static unsigned long str2sec(const char* timestr)
5531 const char spec[] = "smhdw";
5532 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5533 unsigned long val = 0;
5536 if (strpbrk(timestr, spec) == NULL) {
5537 /* no specifiers inside the time string,
5538 should treat it as an integer value */
5539 val = strtoul(timestr, &tail, 10);
5540 return *tail ? ULONG_MAX : val;
5543 /* format string is XXwXXdXXhXXmXXs */
5549 v = strtoul(timestr, &tail, 10);
5550 if (v == ULONG_MAX || *tail == '\0')
5551 /* value too large (ULONG_MAX or more)
5552 or missing specifier */
5555 ptr = strchr(spec, *tail);
5557 /* unknown specifier */
5562 /* check if product will overflow the type */
5563 if (!(v < ULONG_MAX / mult[ind]))
5566 ADD_OVERFLOW(val, mult[ind] * v);
5567 if (val == ULONG_MAX)
5579 #define ARG2ULL(nr, str, def_units) \
5581 unsigned long long limit, units = def_units; \
5584 rc = llapi_parse_size(str, &limit, &units, 1); \
5586 fprintf(stderr, "%s: invalid limit '%s'\n", \
5593 static inline int has_times_option(int argc, char **argv)
5597 for (i = 1; i < argc; i++)
5598 if (!strcmp(argv[i], "-t"))
5604 int lfs_setquota_times(int argc, char **argv)
5607 struct if_quotactl qctl;
5608 char *mnt, *obd_type = (char *)qctl.obd_type;
5609 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5610 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5611 struct option long_opts[] = {
5612 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5613 { .val = 'g', .name = "group", .has_arg = no_argument },
5614 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5615 { .val = 'p', .name = "projid", .has_arg = no_argument },
5616 { .val = 't', .name = "times", .has_arg = no_argument },
5617 { .val = 'u', .name = "user", .has_arg = no_argument },
5621 memset(&qctl, 0, sizeof(qctl));
5622 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5623 qctl.qc_type = ALLQUOTA;
5625 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5626 long_opts, NULL)) != -1) {
5637 if (qctl.qc_type != ALLQUOTA) {
5638 fprintf(stderr, "error: -u/g/p can't be used "
5639 "more than once\n");
5642 qctl.qc_type = qtype;
5645 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5646 fprintf(stderr, "error: bad block-grace: %s\n",
5650 dqb->dqb_valid |= QIF_BTIME;
5653 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5654 fprintf(stderr, "error: bad inode-grace: %s\n",
5658 dqb->dqb_valid |= QIF_ITIME;
5660 case 't': /* Yes, of course! */
5662 default: /* getopt prints error message for us when opterr != 0 */
5667 if (qctl.qc_type == ALLQUOTA) {
5668 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5672 if (optind != argc - 1) {
5673 fprintf(stderr, "error: unexpected parameters encountered\n");
5677 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5678 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5679 fprintf(stderr, "error: grace time is too large\n");
5684 rc = llapi_quotactl(mnt, &qctl);
5687 fprintf(stderr, "%s %s ", obd_type,
5688 obd_uuid2str(&qctl.obd_uuid));
5689 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5696 #define BSLIMIT (1 << 0)
5697 #define BHLIMIT (1 << 1)
5698 #define ISLIMIT (1 << 2)
5699 #define IHLIMIT (1 << 3)
5701 int lfs_setquota(int argc, char **argv)
5704 struct if_quotactl qctl;
5705 char *mnt, *obd_type = (char *)qctl.obd_type;
5706 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5707 struct option long_opts[] = {
5708 { .val = 'b', .name = "block-softlimit",
5709 .has_arg = required_argument },
5710 { .val = 'B', .name = "block-hardlimit",
5711 .has_arg = required_argument },
5712 { .val = 'd', .name = "default", .has_arg = no_argument },
5713 { .val = 'g', .name = "group", .has_arg = required_argument },
5714 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5715 { .val = 'i', .name = "inode-softlimit",
5716 .has_arg = required_argument },
5717 { .val = 'I', .name = "inode-hardlimit",
5718 .has_arg = required_argument },
5719 { .val = 'p', .name = "projid", .has_arg = required_argument },
5720 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5721 { .val = 'u', .name = "user", .has_arg = required_argument },
5722 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5724 unsigned limit_mask = 0;
5726 bool use_default = false;
5729 if (has_times_option(argc, argv))
5730 return lfs_setquota_times(argc, argv);
5732 memset(&qctl, 0, sizeof(qctl));
5733 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5734 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5735 * so it can be used as a marker that qc_type
5736 * isn't reinitialized from command line */
5738 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5739 long_opts, NULL)) != -1) {
5742 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5745 goto quota_type_def;
5748 rc = name2uid(&qctl.qc_id, optarg);
5751 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5754 goto quota_type_def;
5757 rc = name2gid(&qctl.qc_id, optarg);
5760 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5763 goto quota_type_def;
5766 rc = name2projid(&qctl.qc_id, optarg);
5769 qctl.qc_id = strtoul(optarg, &endptr, 10);
5770 if (*endptr != '\0') {
5771 fprintf(stderr, "%s setquota: invalid"
5772 " id '%s'\n", progname, optarg);
5777 if (qctl.qc_id == 0) {
5778 fprintf(stderr, "%s setquota: can't set quota"
5779 " for root usr/group/project.\n",
5785 if (qctl.qc_type != ALLQUOTA) {
5787 "%s setquota: only one of -u, -U, -g,"
5788 " -G, -p or -P may be specified\n",
5792 qctl.qc_type = qtype;
5795 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5799 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5800 dqb->dqb_bsoftlimit >>= 10;
5801 limit_mask |= BSLIMIT;
5802 if (dqb->dqb_bsoftlimit &&
5803 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5805 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5806 "See '%s help setquota' or Lustre manual for details\n",
5807 progname, dqb->dqb_bsoftlimit,
5811 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5812 dqb->dqb_bhardlimit >>= 10;
5813 limit_mask |= BHLIMIT;
5814 if (dqb->dqb_bhardlimit &&
5815 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5817 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5818 "See '%s help setquota' or Lustre manual for details\n",
5819 progname, dqb->dqb_bhardlimit,
5823 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5824 limit_mask |= ISLIMIT;
5825 if (dqb->dqb_isoftlimit &&
5826 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5828 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5829 "See '%s help setquota' or Lustre manual for details\n",
5830 progname, dqb->dqb_isoftlimit,
5834 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5835 limit_mask |= IHLIMIT;
5836 if (dqb->dqb_ihardlimit &&
5837 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5839 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5840 "See '%s help setquota' or Lustre manual for details\n",
5841 progname, dqb->dqb_ihardlimit,
5846 "%s setquota: unrecognized option '%s'\n",
5847 progname, argv[optind - 1]);
5852 if (qctl.qc_type == ALLQUOTA) {
5854 "%s setquota: either -u or -g must be specified\n",
5859 if (!use_default && limit_mask == 0) {
5861 "%s setquota: at least one limit must be specified\n",
5866 if (use_default && limit_mask != 0) {
5868 "%s setquota: limits should not be specified when"
5869 " using default quota\n",
5874 if (use_default && qctl.qc_id == 0) {
5876 "%s setquota: can not set default quota for root"
5877 " user/group/project\n",
5882 if (optind != argc - 1) {
5884 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5885 progname, argv[optind]);
5892 dqb->dqb_bhardlimit = 0;
5893 dqb->dqb_bsoftlimit = 0;
5894 dqb->dqb_ihardlimit = 0;
5895 dqb->dqb_isoftlimit = 0;
5898 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5899 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5900 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5901 /* sigh, we can't just set blimits/ilimits */
5902 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5903 .qc_type = qctl.qc_type,
5904 .qc_id = qctl.qc_id};
5906 rc = llapi_quotactl(mnt, &tmp_qctl);
5910 if (!(limit_mask & BHLIMIT))
5911 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5912 if (!(limit_mask & BSLIMIT))
5913 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5914 if (!(limit_mask & IHLIMIT))
5915 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5916 if (!(limit_mask & ISLIMIT))
5917 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5919 /* Keep grace times if we have got no softlimit arguments */
5920 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5921 dqb->dqb_valid |= QIF_BTIME;
5922 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5925 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5926 dqb->dqb_valid |= QIF_ITIME;
5927 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5931 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5932 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5934 rc = llapi_quotactl(mnt, &qctl);
5938 "%s setquota: cannot quotactl '%s' '%s': %s",
5940 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5947 /* Converts seconds value into format string
5948 * result is returned in buf
5950 * 1. result is in descenting order: 1w2d3h4m5s
5951 * 2. zero fields are not filled (except for p. 3): 5d1s
5952 * 3. zero seconds value is presented as "0s"
5954 static char * __sec2str(time_t seconds, char *buf)
5956 const char spec[] = "smhdw";
5957 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5962 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5963 c = seconds / mult[i];
5965 if (c > 0 || (i == 0 && buf == tail))
5966 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5974 static void sec2str(time_t seconds, char *buf, int rc)
5981 tail = __sec2str(seconds, tail);
5983 if (rc && tail - buf < 39) {
5989 static void diff2str(time_t seconds, char *buf, time_t now)
5995 if (seconds <= now) {
5996 strcpy(buf, "none");
5999 __sec2str(seconds - now, buf);
6002 static void print_quota_title(char *name, struct if_quotactl *qctl,
6003 bool human_readable, bool show_default)
6006 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6007 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6008 "Filesystem", "bquota", "blimit", "bgrace",
6009 "iquota", "ilimit", "igrace");
6011 printf("Disk quotas for %s %s (%cid %u):\n",
6012 qtype_name(qctl->qc_type), name,
6013 *qtype_name(qctl->qc_type), qctl->qc_id);
6014 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6015 "Filesystem", human_readable ? "used" : "kbytes",
6016 "quota", "limit", "grace",
6017 "files", "quota", "limit", "grace");
6021 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6024 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6027 snprintf(buf, buflen, "%5.4gP",
6028 (double)num / ((__u64)1 << 40));
6030 snprintf(buf, buflen, "%5.4gT",
6031 (double)num / (1 << 30));
6033 snprintf(buf, buflen, "%5.4gG",
6034 (double)num / (1 << 20));
6036 snprintf(buf, buflen, "%5.4gM",
6037 (double)num / (1 << 10));
6039 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6043 #define STRBUF_LEN 32
6044 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6045 int rc, bool h, bool show_default)
6051 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6052 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6053 int bover = 0, iover = 0;
6054 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6055 char numbuf[3][STRBUF_LEN];
6057 char strbuf[STRBUF_LEN];
6059 if (dqb->dqb_bhardlimit &&
6060 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6062 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6063 if (dqb->dqb_btime > now) {
6070 if (dqb->dqb_ihardlimit &&
6071 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6073 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6074 if (dqb->dqb_itime > now) {
6082 if (strlen(mnt) > 15)
6083 printf("%s\n%15s", mnt, "");
6085 printf("%15s", mnt);
6088 diff2str(dqb->dqb_btime, timebuf, now);
6089 else if (show_default)
6090 snprintf(timebuf, sizeof(timebuf), "%llu",
6093 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6094 strbuf, sizeof(strbuf), h);
6095 if (rc == -EREMOTEIO)
6096 sprintf(numbuf[0], "%s*", strbuf);
6098 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6099 "%s" : "[%s]", strbuf);
6101 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6102 if (type == QC_GENERAL)
6103 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6104 "%s" : "[%s]", strbuf);
6106 sprintf(numbuf[1], "%s", "-");
6108 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6109 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6110 "%s" : "[%s]", strbuf);
6113 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6115 printf(" %7s%c %6s %7s %7s",
6116 numbuf[0], bover ? '*' : ' ', numbuf[1],
6117 numbuf[2], bover > 1 ? timebuf : "-");
6121 diff2str(dqb->dqb_itime, timebuf, now);
6122 else if (show_default)
6123 snprintf(timebuf, sizeof(timebuf), "%llu",
6126 snprintf(numbuf[0], sizeof(numbuf),
6127 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6128 (uintmax_t)dqb->dqb_curinodes);
6130 if (type == QC_GENERAL)
6131 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6133 (uintmax_t)dqb->dqb_isoftlimit);
6135 sprintf(numbuf[1], "%s", "-");
6137 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6138 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6141 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6142 else if (type != QC_OSTIDX)
6143 printf(" %7s%c %6s %7s %7s",
6144 numbuf[0], iover ? '*' : ' ', numbuf[1],
6145 numbuf[2], iover > 1 ? timebuf : "-");
6147 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6150 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6151 qctl->qc_cmd == Q_GETOINFO) {
6155 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6156 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6157 printf("Block grace time: %s; Inode grace time: %s\n",
6158 bgtimebuf, igtimebuf);
6162 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6163 bool h, __u64 *total)
6165 int rc = 0, rc1 = 0, count = 0;
6166 __u32 valid = qctl->qc_valid;
6168 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6170 fprintf(stderr, "can not get %s count: %s\n",
6171 is_mdt ? "mdt": "ost", strerror(-rc));
6175 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6176 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6177 rc = llapi_quotactl(mnt, qctl);
6179 /* It is remote client case. */
6180 if (rc == -EOPNOTSUPP) {
6187 fprintf(stderr, "quotactl %s%d failed.\n",
6188 is_mdt ? "mdt": "ost", qctl->qc_idx);
6192 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6193 qctl->qc_valid, 0, h, false);
6194 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6195 qctl->qc_dqblk.dqb_bhardlimit;
6198 qctl->qc_valid = valid;
6202 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6203 int verbose, int quiet, bool human_readable,
6206 int rc1 = 0, rc2 = 0, rc3 = 0;
6207 char *obd_type = (char *)qctl->obd_type;
6208 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6209 __u64 total_ialloc = 0, total_balloc = 0;
6210 bool use_default_for_blk = false;
6211 bool use_default_for_file = false;
6214 rc1 = llapi_quotactl(mnt, qctl);
6218 fprintf(stderr, "%s quotas are not enabled.\n",
6219 qtype_name(qctl->qc_type));
6222 fprintf(stderr, "Permission denied.\n");
6225 /* We already got error message. */
6228 fprintf(stderr, "Unexpected quotactl error: %s\n",
6233 if (!show_default && qctl->qc_id == 0) {
6234 qctl->qc_dqblk.dqb_bhardlimit = 0;
6235 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6236 qctl->qc_dqblk.dqb_ihardlimit = 0;
6237 qctl->qc_dqblk.dqb_isoftlimit = 0;
6238 qctl->qc_dqblk.dqb_btime = 0;
6239 qctl->qc_dqblk.dqb_itime = 0;
6240 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6243 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6244 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6245 use_default_for_blk = true;
6246 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6249 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6250 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6251 use_default_for_file = true;
6252 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6255 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6256 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6257 print_quota_title(name, qctl, human_readable, show_default);
6259 if (rc1 && *obd_type)
6260 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6262 if (qctl->qc_valid != QC_GENERAL)
6265 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6266 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6267 (QIF_LIMITS|QIF_USAGE));
6269 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6271 if (!show_default && verbose &&
6272 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6273 char strbuf[STRBUF_LEN];
6275 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6277 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6279 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6281 printf("Total allocated inode limit: %ju, total "
6282 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6286 if (use_default_for_blk)
6287 printf("%cid %u is using default block quota setting\n",
6288 *qtype_name(qctl->qc_type), qctl->qc_id);
6290 if (use_default_for_file)
6291 printf("%cid %u is using default file quota setting\n",
6292 *qtype_name(qctl->qc_type), qctl->qc_id);
6294 if (rc1 || rc2 || rc3 || inacc)
6295 printf("Some errors happened when getting quota info. "
6296 "Some devices may be not working or deactivated. "
6297 "The data in \"[]\" is inaccurate.\n");
6303 static int lfs_project(int argc, char **argv)
6305 int ret = 0, err = 0, c, i;
6306 struct project_handle_control phc = { 0 };
6307 enum lfs_project_ops_t op;
6310 phc.assign_projid = false;
6311 /* default action */
6312 op = LFS_PROJECT_LIST;
6314 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6317 if (op != LFS_PROJECT_LIST) {
6319 "%s: cannot specify '-c' '-C' '-s' together\n",
6324 op = LFS_PROJECT_CHECK;
6327 if (op != LFS_PROJECT_LIST) {
6329 "%s: cannot specify '-c' '-C' '-s' together\n",
6334 op = LFS_PROJECT_CLEAR;
6337 if (op != LFS_PROJECT_LIST) {
6339 "%s: cannot specify '-c' '-C' '-s' together\n",
6344 phc.set_inherit = true;
6345 op = LFS_PROJECT_SET;
6351 phc.keep_projid = true;
6354 phc.recursive = true;
6357 phc.projid = strtoul(optarg, NULL, 0);
6358 phc.assign_projid = true;
6362 phc.newline = false;
6365 fprintf(stderr, "%s: invalid option '%c'\n",
6371 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6372 op = LFS_PROJECT_SET;
6373 phc.set_projid = true;
6374 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6375 phc.set_projid = true;
6379 case LFS_PROJECT_CHECK:
6380 if (phc.keep_projid) {
6382 "%s: '-k' is useless together with '-c'\n",
6387 case LFS_PROJECT_CLEAR:
6390 "%s: '-0' is useless together with '-C'\n",
6394 if (phc.assign_projid) {
6396 "%s: '-p' is useless together with '-C'\n",
6401 case LFS_PROJECT_SET:
6404 "%s: '-0' is useless together with '-s'\n",
6408 if (phc.keep_projid) {
6410 "%s: '-k' is useless together with '-s'\n",
6418 "%s: '-0' is useless for list operations\n",
6428 fprintf(stderr, "%s: missing file or directory target(s)\n",
6433 for (i = 0; i < argc; i++) {
6435 case LFS_PROJECT_CHECK:
6436 err = lfs_project_check(argv[i], &phc);
6438 case LFS_PROJECT_LIST:
6439 err = lfs_project_list(argv[i], &phc);
6441 case LFS_PROJECT_CLEAR:
6442 err = lfs_project_clear(argv[i], &phc);
6444 case LFS_PROJECT_SET:
6445 err = lfs_project_set(argv[i], &phc);
6457 static int lfs_quota(int argc, char **argv)
6460 char *mnt, *name = NULL;
6461 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6462 .qc_type = ALLQUOTA };
6463 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6464 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6466 __u32 valid = QC_GENERAL, idx = 0;
6467 bool human_readable = false;
6468 bool show_default = false;
6471 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6474 show_default = true;
6479 show_default = true;
6484 show_default = true;
6488 if (qctl.qc_type != ALLQUOTA) {
6490 "%s quota: only one of -u, -g, or -p may be specified\n",
6494 qctl.qc_type = qtype;
6497 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6500 valid = qctl.qc_valid = QC_UUID;
6501 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6504 valid = qctl.qc_valid = QC_MDTIDX;
6505 idx = qctl.qc_idx = atoi(optarg);
6506 if (idx == 0 && *optarg != '0') {
6508 "%s quota: invalid MDT index '%s'\n",
6514 valid = qctl.qc_valid = QC_OSTIDX;
6515 idx = qctl.qc_idx = atoi(optarg);
6516 if (idx == 0 && *optarg != '0') {
6518 "%s quota: invalid OST index '%s'\n",
6530 human_readable = true;
6533 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6534 progname, argv[optind - 1]);
6539 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6540 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6541 optind == argc - 1 && !show_default) {
6543 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6544 qctl.qc_valid = valid;
6547 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6548 qctl.qc_type = qtype;
6549 if (qtype == USRQUOTA) {
6550 qctl.qc_id = geteuid();
6551 rc = uid2name(&name, qctl.qc_id);
6553 qctl.qc_id = getegid();
6554 rc = gid2name(&name, qctl.qc_id);
6559 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6560 human_readable, show_default);
6565 /* lfs quota -u username /path/to/lustre/mount */
6566 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6567 /* options should be followed by u/g-name and mntpoint */
6568 if ((!show_default && optind + 2 != argc) ||
6569 (show_default && optind + 1 != argc) ||
6570 qctl.qc_type == ALLQUOTA) {
6572 "%s quota: name and mount point must be specified\n",
6577 if (!show_default) {
6578 name = argv[optind++];
6579 switch (qctl.qc_type) {
6581 rc = name2uid(&qctl.qc_id, name);
6584 rc = name2gid(&qctl.qc_id, name);
6587 rc = name2projid(&qctl.qc_id, name);
6594 qctl.qc_valid = QC_GENERAL;
6595 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6600 qctl.qc_id = strtoul(name, &endptr, 10);
6601 if (*endptr != '\0') {
6602 fprintf(stderr, "%s quota: invalid id '%s'\n",
6607 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6608 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6614 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6615 human_readable, show_default);
6618 #endif /* HAVE_SYS_QUOTA_H! */
6620 static int flushctx_ioctl(char *mp)
6624 fd = open(mp, O_RDONLY);
6626 fprintf(stderr, "flushctx: error open %s: %s\n",
6627 mp, strerror(errno));
6631 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6633 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6634 mp, strerror(errno));
6640 static int lfs_flushctx(int argc, char **argv)
6642 int kdestroy = 0, c;
6643 char mntdir[PATH_MAX] = {'\0'};
6647 while ((c = getopt(argc, argv, "k")) != -1) {
6653 fprintf(stderr, "error: %s: option '-%c' "
6654 "unrecognized\n", argv[0], c);
6660 if ((rc = system("kdestroy > /dev/null")) != 0) {
6661 rc = WEXITSTATUS(rc);
6662 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6666 if (optind >= argc) {
6667 /* flush for all mounted lustre fs. */
6668 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6669 /* Check if we have a mount point */
6670 if (mntdir[0] == '\0')
6673 if (flushctx_ioctl(mntdir))
6676 mntdir[0] = '\0'; /* avoid matching in next loop */
6679 /* flush fs as specified */
6680 while (optind < argc) {
6681 if (flushctx_ioctl(argv[optind++]))
6688 static int lfs_cp(int argc, char **argv)
6690 fprintf(stderr, "remote client copy file(s).\n"
6691 "obsolete, does not support it anymore.\n");
6695 static int lfs_ls(int argc, char **argv)
6697 fprintf(stderr, "remote client lists directory contents.\n"
6698 "obsolete, does not support it anymore.\n");
6702 static int lfs_changelog(int argc, char **argv)
6704 void *changelog_priv;
6705 struct changelog_rec *rec;
6706 long long startrec = 0, endrec = 0;
6708 struct option long_opts[] = {
6709 { .val = 'f', .name = "follow", .has_arg = no_argument },
6711 char short_opts[] = "f";
6714 while ((rc = getopt_long(argc, argv, short_opts,
6715 long_opts, NULL)) != -1) {
6722 "%s changelog: unrecognized option '%s'\n",
6723 progname, argv[optind - 1]);
6727 if (optind >= argc) {
6728 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6733 mdd = argv[optind++];
6735 startrec = strtoll(argv[optind++], NULL, 10);
6737 endrec = strtoll(argv[optind++], NULL, 10);
6739 rc = llapi_changelog_start(&changelog_priv,
6740 CHANGELOG_FLAG_BLOCK |
6741 CHANGELOG_FLAG_JOBID |
6742 CHANGELOG_FLAG_EXTRA_FLAGS |
6743 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6746 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6747 progname, strerror(errno = -rc));
6751 rc = llapi_changelog_set_xflags(changelog_priv,
6752 CHANGELOG_EXTRA_FLAG_UIDGID |
6753 CHANGELOG_EXTRA_FLAG_NID |
6754 CHANGELOG_EXTRA_FLAG_OMODE |
6755 CHANGELOG_EXTRA_FLAG_XATTR);
6758 "%s changelog: cannot set xflags for changelog: %s\n",
6759 progname, strerror(errno = -rc));
6763 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6767 if (endrec && rec->cr_index > endrec) {
6768 llapi_changelog_free(&rec);
6771 if (rec->cr_index < startrec) {
6772 llapi_changelog_free(&rec);
6776 secs = rec->cr_time >> 30;
6777 gmtime_r(&secs, &ts);
6778 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6779 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6780 changelog_type2str(rec->cr_type),
6781 ts.tm_hour, ts.tm_min, ts.tm_sec,
6782 (int)(rec->cr_time & ((1 << 30) - 1)),
6783 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6784 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6786 if (rec->cr_flags & CLF_JOBID) {
6787 struct changelog_ext_jobid *jid =
6788 changelog_rec_jobid(rec);
6790 if (jid->cr_jobid[0] != '\0')
6791 printf(" j=%s", jid->cr_jobid);
6794 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6795 struct changelog_ext_extra_flags *ef =
6796 changelog_rec_extra_flags(rec);
6798 printf(" ef=0x%llx", ef->cr_extra_flags);
6800 if (ef->cr_extra_flags & CLFE_UIDGID) {
6801 struct changelog_ext_uidgid *uidgid =
6802 changelog_rec_uidgid(rec);
6804 printf(" u=%llu:%llu",
6805 uidgid->cr_uid, uidgid->cr_gid);
6807 if (ef->cr_extra_flags & CLFE_NID) {
6808 struct changelog_ext_nid *nid =
6809 changelog_rec_nid(rec);
6812 libcfs_nid2str(nid->cr_nid));
6815 if (ef->cr_extra_flags & CLFE_OPEN) {
6816 struct changelog_ext_openmode *omd =
6817 changelog_rec_openmode(rec);
6818 char mode[] = "---";
6820 /* exec mode must be exclusive */
6821 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6824 if (omd->cr_openflags & MDS_FMODE_READ)
6826 if (omd->cr_openflags &
6833 if (strcmp(mode, "---") != 0)
6834 printf(" m=%s", mode);
6838 if (ef->cr_extra_flags & CLFE_XATTR) {
6839 struct changelog_ext_xattr *xattr =
6840 changelog_rec_xattr(rec);
6842 if (xattr->cr_xattr[0] != '\0')
6843 printf(" x=%s", xattr->cr_xattr);
6847 if (rec->cr_namelen)
6848 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6849 rec->cr_namelen, changelog_rec_name(rec));
6851 if (rec->cr_flags & CLF_RENAME) {
6852 struct changelog_ext_rename *rnm =
6853 changelog_rec_rename(rec);
6855 if (!fid_is_zero(&rnm->cr_sfid))
6856 printf(" s="DFID" sp="DFID" %.*s",
6857 PFID(&rnm->cr_sfid),
6858 PFID(&rnm->cr_spfid),
6859 (int)changelog_rec_snamelen(rec),
6860 changelog_rec_sname(rec));
6864 llapi_changelog_free(&rec);
6867 llapi_changelog_fini(&changelog_priv);
6870 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6871 progname, strerror(errno = -rc));
6873 return (rc == 1 ? 0 : rc);
6876 static int lfs_changelog_clear(int argc, char **argv)
6884 endrec = strtoll(argv[3], NULL, 10);
6886 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6889 fprintf(stderr, "%s: record out of range: %llu\n",
6891 else if (rc == -ENOENT)
6892 fprintf(stderr, "%s: no changelog user: %s\n",
6895 fprintf(stderr, "%s error: %s\n", argv[0],
6904 static int lfs_fid2path(int argc, char **argv)
6906 struct option long_opts[] = {
6907 { .val = 'c', .name = "cur", .has_arg = no_argument },
6908 { .val = 'l', .name = "link", .has_arg = required_argument },
6909 { .val = 'r', .name = "rec", .has_arg = required_argument },
6911 char short_opts[] = "cl:r:";
6912 char *device, *fid, *path;
6913 long long recno = -1;
6918 char *endptr = NULL;
6920 while ((rc = getopt_long(argc, argv, short_opts,
6921 long_opts, NULL)) != -1) {
6927 linkno = strtol(optarg, &endptr, 10);
6928 if (*endptr != '\0') {
6930 "%s fid2path: invalid linkno '%s'\n",
6936 recno = strtoll(optarg, &endptr, 10);
6937 if (*endptr != '\0') {
6939 "%s fid2path: invalid recno '%s'\n",
6946 "%s fid2path: unrecognized option '%s'\n",
6947 progname, argv[optind - 1]);
6954 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6959 device = argv[optind++];
6960 path = calloc(1, PATH_MAX);
6964 "%s fid2path: cannot allocate memory for path: %s\n",
6965 progname, strerror(-rc));
6970 while (optind < argc) {
6971 fid = argv[optind++];
6973 lnktmp = (linkno >= 0) ? linkno : 0;
6975 int oldtmp = lnktmp;
6976 long long rectmp = recno;
6978 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6982 "%s fid2path: cannot find '%s': %s\n",
6983 progname, fid, strerror(errno = -rc2));
6990 fprintf(stdout, "%lld ", rectmp);
6991 if (device[0] == '/') {
6992 fprintf(stdout, "%s", device);
6993 if (device[strlen(device) - 1] != '/')
6994 fprintf(stdout, "/");
6995 } else if (path[0] == '\0') {
6996 fprintf(stdout, "/");
6998 fprintf(stdout, "%s\n", path);
7001 /* specified linkno */
7003 if (oldtmp == lnktmp)
7013 static int lfs_path2fid(int argc, char **argv)
7015 struct option long_opts[] = {
7016 { .val = 'p', .name = "parents", .has_arg = no_argument },
7019 const char short_opts[] = "p";
7020 const char *sep = "";
7023 bool show_parents = false;
7025 while ((rc = getopt_long(argc, argv, short_opts,
7026 long_opts, NULL)) != -1) {
7029 show_parents = true;
7033 "%s path2fid: unrecognized option '%s'\n",
7034 progname, argv[optind - 1]);
7039 if (optind > argc - 1) {
7040 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7044 else if (optind < argc - 1)
7048 for (path = argv + optind; *path != NULL; path++) {
7050 if (!show_parents) {
7051 err = llapi_path2fid(*path, &fid);
7053 printf("%s%s"DFID"\n",
7054 *sep != '\0' ? *path : "", sep,
7057 char name[NAME_MAX + 1];
7058 unsigned int linkno = 0;
7060 while ((err = llapi_path2parent(*path, linkno, &fid,
7061 name, sizeof(name))) == 0) {
7062 if (*sep != '\0' && linkno == 0)
7063 printf("%s%s", *path, sep);
7065 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7070 /* err == -ENODATA is end-of-loop */
7071 if (linkno > 0 && err == -ENODATA) {
7079 "%s path2fid: cannot get %sfid for '%s': %s\n",
7080 progname, show_parents ? "parent " : "", *path,
7092 static int lfs_data_version(int argc, char **argv)
7099 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7102 fprintf(stderr, "%s data_version: FILE must be specified\n",
7107 while ((c = getopt(argc, argv, "nrw")) != -1) {
7110 data_version_flags = 0;
7113 data_version_flags |= LL_DV_RD_FLUSH;
7116 data_version_flags |= LL_DV_WR_FLUSH;
7120 "%s data_version: unrecognized option '%s'\n",
7121 progname, argv[optind - 1]);
7125 if (optind == argc) {
7126 fprintf(stderr, "%s data_version: FILE must be specified\n",
7131 path = argv[optind];
7132 fd = open(path, O_RDONLY);
7135 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7136 progname, path, strerror(-rc));
7140 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7143 "%s data_version: cannot get version for '%s': %s\n",
7144 progname, path, strerror(-rc));
7146 printf("%ju" "\n", (uintmax_t)data_version);
7152 static int lfs_hsm_state(int argc, char **argv)
7157 struct hsm_user_state hus;
7165 rc = llapi_hsm_state_get(path, &hus);
7167 fprintf(stderr, "can't get hsm state for %s: %s\n",
7168 path, strerror(errno = -rc));
7172 /* Display path name and status flags */
7173 printf("%s: (0x%08x)", path, hus.hus_states);
7175 if (hus.hus_states & HS_RELEASED)
7176 printf(" released");
7177 if (hus.hus_states & HS_EXISTS)
7179 if (hus.hus_states & HS_DIRTY)
7181 if (hus.hus_states & HS_ARCHIVED)
7182 printf(" archived");
7183 /* Display user-settable flags */
7184 if (hus.hus_states & HS_NORELEASE)
7185 printf(" never_release");
7186 if (hus.hus_states & HS_NOARCHIVE)
7187 printf(" never_archive");
7188 if (hus.hus_states & HS_LOST)
7189 printf(" lost_from_hsm");
7191 if (hus.hus_archive_id != 0)
7192 printf(", archive_id:%d", hus.hus_archive_id);
7195 } while (++i < argc);
7200 #define LFS_HSM_SET 0
7201 #define LFS_HSM_CLEAR 1
7204 * Generic function to set or clear HSM flags.
7205 * Used by hsm_set and hsm_clear.
7207 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7209 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7211 struct option long_opts[] = {
7212 { .val = 'A', .name = "archived", .has_arg = no_argument },
7213 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7214 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7215 { .val = 'e', .name = "exists", .has_arg = no_argument },
7216 { .val = 'l', .name = "lost", .has_arg = no_argument },
7217 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7218 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7220 char short_opts[] = "lraAdei:";
7224 __u32 archive_id = 0;
7230 while ((c = getopt_long(argc, argv, short_opts,
7231 long_opts, NULL)) != -1) {
7237 mask |= HS_NOARCHIVE;
7240 mask |= HS_ARCHIVED;
7243 mask |= HS_NORELEASE;
7252 archive_id = strtol(optarg, &end, 10);
7254 fprintf(stderr, "invalid archive_id: '%s'\n",
7262 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7263 argv[0], argv[optind - 1]);
7268 /* User should have specified a flag */
7272 while (optind < argc) {
7274 path = argv[optind];
7276 /* If mode == 0, this means we apply the mask. */
7277 if (mode == LFS_HSM_SET)
7278 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7280 rc = llapi_hsm_state_set(path, 0, mask, 0);
7283 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7284 path, strerror(errno = -rc));
7293 static int lfs_hsm_action(int argc, char **argv)
7298 struct hsm_current_action hca;
7299 struct hsm_extent he;
7300 enum hsm_user_action hua;
7301 enum hsm_progress_states hps;
7309 rc = llapi_hsm_current_action(path, &hca);
7311 fprintf(stderr, "can't get hsm action for %s: %s\n",
7312 path, strerror(errno = -rc));
7315 he = hca.hca_location;
7316 hua = hca.hca_action;
7317 hps = hca.hca_state;
7319 printf("%s: %s", path, hsm_user_action2name(hua));
7321 /* Skip file without action */
7322 if (hca.hca_action == HUA_NONE) {
7327 printf(" %s ", hsm_progress_state2name(hps));
7329 if ((hps == HPS_RUNNING) &&
7330 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7331 printf("(%llu bytes moved)\n",
7332 (unsigned long long)he.length);
7333 else if ((he.offset + he.length) == LUSTRE_EOF)
7334 printf("(from %llu to EOF)\n",
7335 (unsigned long long)he.offset);
7337 printf("(from %llu to %llu)\n",
7338 (unsigned long long)he.offset,
7339 (unsigned long long)(he.offset + he.length));
7341 } while (++i < argc);
7346 static int lfs_hsm_set(int argc, char **argv)
7348 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7351 static int lfs_hsm_clear(int argc, char **argv)
7353 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7357 * Check file state and return its fid, to be used by lfs_hsm_request().
7359 * \param[in] file Path to file to check
7360 * \param[in,out] fid Pointer to allocated lu_fid struct.
7361 * \param[in,out] last_dev Pointer to last device id used.
7363 * \return 0 on success.
7365 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7371 rc = lstat(file, &st);
7373 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7376 /* Checking for regular file as archiving as posix copytool
7377 * rejects archiving files other than regular files
7379 if (!S_ISREG(st.st_mode)) {
7380 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7383 /* A request should be ... */
7384 if (*last_dev != st.st_dev && *last_dev != 0) {
7385 fprintf(stderr, "All files should be "
7386 "on the same filesystem: %s\n", file);
7389 *last_dev = st.st_dev;
7391 rc = llapi_path2fid(file, fid);
7393 fprintf(stderr, "Cannot read FID of %s: %s\n",
7394 file, strerror(-rc));
7400 /* Fill an HSM HUR item with a given file name.
7402 * If mntpath is set, then the filename is actually a FID, and no
7403 * lookup on the filesystem will be performed.
7405 * \param[in] hur the user request to fill
7406 * \param[in] idx index of the item inside the HUR to fill
7407 * \param[in] mntpath mountpoint of Lustre
7408 * \param[in] fname filename (if mtnpath is NULL)
7409 * or FID (if mntpath is set)
7410 * \param[in] last_dev pointer to last device id used
7412 * \retval 0 on success
7413 * \retval CMD_HELP or a negative errno on error
7415 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7416 const char *mntpath, const char *fname,
7419 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7422 hui->hui_extent.length = -1;
7424 if (mntpath != NULL) {
7427 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7431 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7436 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7440 hur->hur_request.hr_itemcount++;
7445 static int lfs_hsm_request(int argc, char **argv, int action)
7447 struct option long_opts[] = {
7448 { .val = 'a', .name = "archive", .has_arg = required_argument },
7449 { .val = 'D', .name = "data", .has_arg = required_argument },
7450 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7451 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7454 char short_opts[] = "l:D:a:m:";
7455 struct hsm_user_request *hur, *oldhur;
7460 char *filelist = NULL;
7461 char fullpath[PATH_MAX];
7462 char *opaque = NULL;
7466 int nbfile_alloc = 0;
7467 char *some_file = NULL;
7468 char *mntpath = NULL;
7474 while ((c = getopt_long(argc, argv, short_opts,
7475 long_opts, NULL)) != -1) {
7484 if (action != HUA_ARCHIVE &&
7485 action != HUA_REMOVE) {
7487 "error: -a is supported only "
7488 "when archiving or removing\n");
7491 archive_id = atoi(optarg);
7494 if (some_file == NULL) {
7496 some_file = strdup(optarg);
7502 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7503 argv[0], argv[optind - 1]);
7508 /* All remaining args are files, so we have at least nbfile */
7509 nbfile = argc - optind;
7511 if ((nbfile == 0) && (filelist == NULL))
7515 opaque_len = strlen(opaque);
7517 /* Alloc the request structure with enough place to store all files
7518 * from command line. */
7519 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7521 fprintf(stderr, "Cannot create the request: %s\n",
7525 nbfile_alloc = nbfile;
7527 hur->hur_request.hr_action = action;
7528 hur->hur_request.hr_archive_id = archive_id;
7529 hur->hur_request.hr_flags = 0;
7531 /* All remaining args are files, add them */
7532 if (nbfile != 0 && some_file == NULL)
7533 some_file = strdup(argv[optind]);
7535 for (i = 0; i < nbfile; i++) {
7536 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7542 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7544 /* If a filelist was specified, read the filelist from it. */
7545 if (filelist != NULL) {
7546 fp = fopen(filelist, "r");
7548 fprintf(stderr, "Cannot read the file list %s: %s\n",
7549 filelist, strerror(errno));
7554 while ((rc = getline(&line, &len, fp)) != -1) {
7555 /* If allocated buffer was too small, get something
7557 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7560 nbfile_alloc = nbfile_alloc * 2 + 1;
7562 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7565 fprintf(stderr, "hsm: cannot allocate "
7566 "the request: %s\n",
7573 size = hur_len(oldhur);
7575 fprintf(stderr, "hsm: cannot allocate "
7576 "%u files + %u bytes data\n",
7577 oldhur->hur_request.hr_itemcount,
7578 oldhur->hur_request.hr_data_len);
7585 memcpy(hur, oldhur, size);
7590 if (line[strlen(line) - 1] == '\n')
7591 line[strlen(line) - 1] = '\0';
7593 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7594 mntpath, line, &last_dev);
7600 if (some_file == NULL) {
7610 /* If a --data was used, add it to the request */
7611 hur->hur_request.hr_data_len = opaque_len;
7613 memcpy(hur_data(hur), opaque, opaque_len);
7615 /* Send the HSM request */
7616 if (realpath(some_file, fullpath) == NULL) {
7617 fprintf(stderr, "Could not find path '%s': %s\n",
7618 some_file, strerror(errno));
7620 rc = llapi_hsm_request(fullpath, hur);
7622 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7623 some_file, strerror(-rc));
7633 static int lfs_hsm_archive(int argc, char **argv)
7635 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7638 static int lfs_hsm_restore(int argc, char **argv)
7640 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7643 static int lfs_hsm_release(int argc, char **argv)
7645 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7648 static int lfs_hsm_remove(int argc, char **argv)
7650 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7653 static int lfs_hsm_cancel(int argc, char **argv)
7655 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7658 static int lfs_swap_layouts(int argc, char **argv)
7663 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7664 SWAP_LAYOUTS_KEEP_MTIME |
7665 SWAP_LAYOUTS_KEEP_ATIME);
7668 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7670 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7672 int lfs_get_mode(const char *string)
7674 enum lock_mode_user mode;
7676 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7677 if (lock_mode_names[mode] == NULL)
7679 if (strcmp(string, lock_mode_names[mode]) == 0)
7686 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7688 enum lu_ladvise_type advice;
7691 advice < ARRAY_SIZE(ladvise_names); advice++) {
7692 if (ladvise_names[advice] == NULL)
7694 if (strcmp(string, ladvise_names[advice]) == 0)
7698 return LU_LADVISE_INVALID;
7701 static int lfs_ladvise(int argc, char **argv)
7703 struct option long_opts[] = {
7704 { .val = 'a', .name = "advice", .has_arg = required_argument },
7705 { .val = 'b', .name = "background", .has_arg = no_argument },
7706 { .val = 'e', .name = "end", .has_arg = required_argument },
7707 { .val = 'l', .name = "length", .has_arg = required_argument },
7708 { .val = 'm', .name = "mode", .has_arg = required_argument },
7709 { .val = 's', .name = "start", .has_arg = required_argument },
7710 { .val = 'u', .name = "unset", .has_arg = no_argument },
7712 char short_opts[] = "a:be:l:m:s:u";
7717 struct llapi_lu_ladvise advice;
7718 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7719 unsigned long long start = 0;
7720 unsigned long long end = LUSTRE_EOF;
7721 unsigned long long length = 0;
7722 unsigned long long size_units;
7723 unsigned long long flags = 0;
7727 while ((c = getopt_long(argc, argv, short_opts,
7728 long_opts, NULL)) != -1) {
7731 advice_type = lfs_get_ladvice(optarg);
7732 if (advice_type == LU_LADVISE_INVALID) {
7733 fprintf(stderr, "%s: invalid advice type "
7734 "'%s'\n", argv[0], optarg);
7735 fprintf(stderr, "Valid types:");
7737 for (advice_type = 0;
7738 advice_type < ARRAY_SIZE(ladvise_names);
7740 if (ladvise_names[advice_type] == NULL)
7742 fprintf(stderr, " %s",
7743 ladvise_names[advice_type]);
7745 fprintf(stderr, "\n");
7758 rc = llapi_parse_size(optarg, &end,
7761 fprintf(stderr, "%s: bad end offset '%s'\n",
7768 rc = llapi_parse_size(optarg, &start,
7771 fprintf(stderr, "%s: bad start offset "
7772 "'%s'\n", argv[0], optarg);
7778 rc = llapi_parse_size(optarg, &length,
7781 fprintf(stderr, "%s: bad length '%s'\n",
7787 mode = lfs_get_mode(optarg);
7789 fprintf(stderr, "%s: bad mode '%s', valid "
7790 "modes are READ or WRITE\n",
7798 fprintf(stderr, "%s: option '%s' unrecognized\n",
7799 argv[0], argv[optind - 1]);
7804 if (advice_type == LU_LADVISE_INVALID) {
7805 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7806 fprintf(stderr, "Valid types:");
7807 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7809 if (ladvise_names[advice_type] == NULL)
7811 fprintf(stderr, " %s", ladvise_names[advice_type]);
7813 fprintf(stderr, "\n");
7817 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7818 fprintf(stderr, "%s: Lock no expand advice is a per file "
7819 "descriptor advice, so when called from lfs, "
7820 "it does nothing.\n", argv[0]);
7824 if (argc <= optind) {
7825 fprintf(stderr, "%s: please give one or more file names\n",
7830 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7831 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7836 if (end == LUSTRE_EOF && length != 0)
7837 end = start + length;
7840 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7841 argv[0], start, end);
7845 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7846 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7851 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7852 fprintf(stderr, "%s: mode is required with lockahead\n",
7857 while (optind < argc) {
7860 path = argv[optind++];
7862 fd = open(path, O_RDONLY);
7864 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7865 argv[0], path, strerror(errno));
7870 advice.lla_start = start;
7871 advice.lla_end = end;
7872 advice.lla_advice = advice_type;
7873 advice.lla_value1 = 0;
7874 advice.lla_value2 = 0;
7875 advice.lla_value3 = 0;
7876 advice.lla_value4 = 0;
7877 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7878 advice.lla_lockahead_mode = mode;
7879 advice.lla_peradvice_flags = flags;
7882 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7885 fprintf(stderr, "%s: cannot give advice '%s' to file "
7886 "'%s': %s\n", argv[0],
7887 ladvise_names[advice_type],
7888 path, strerror(errno));
7894 if (rc == 0 && rc2 < 0)
7900 /** The input string contains a comma delimited list of component ids and
7901 * ranges, for example "1,2-4,7".
7903 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7905 bool end_of_loop = false;
7913 while (!end_of_loop) {
7917 char *endptr = NULL;
7920 ptr = strchrnul(arg, ',');
7921 end_of_loop = *ptr == '\0';
7924 start_index = strtol(arg, &endptr, 0);
7925 if (endptr == arg) /* no data at all */
7927 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7929 if (start_index < 0)
7932 end_index = start_index;
7933 if (*endptr == '-') {
7934 end_index = strtol(endptr + 1, &endptr, 0);
7935 if (*endptr != '\0')
7937 if (end_index < start_index)
7941 for (i = start_index; i <= end_index && size > 0; i++) {
7944 /* remove duplicate */
7945 for (j = 0; j < nr; j++) {
7949 if (j == nr) { /* no duplicate */
7955 if (size == 0 && i < end_index)
7962 if (!end_of_loop && ptr != NULL)
7965 return rc < 0 ? rc : nr;
7969 * struct verify_mirror_id - Mirror id to be verified.
7970 * @mirror_id: A specified mirror id.
7971 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7973 struct verify_mirror_id {
7979 * compare_mirror_ids() - Compare mirror ids.
7980 * @layout: Mirror component list.
7981 * @cbdata: Callback data in verify_mirror_id structure.
7983 * This is a callback function called by llapi_layout_comp_iterate()
7984 * to compare the specified mirror id with the one in the current
7985 * component of @layout. If they are the same, then the specified
7986 * mirror id is valid.
7988 * Return: a negative error code on failure or
7989 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7990 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7993 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7995 struct verify_mirror_id *mirror_id_cbdata =
7996 (struct verify_mirror_id *)cbdata;
8000 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8004 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8005 progname, strerror(errno));
8009 if (mirror_id_cbdata->mirror_id == mirror_id) {
8010 mirror_id_cbdata->is_valid_id = true;
8011 return LLAPI_LAYOUT_ITER_STOP;
8014 return LLAPI_LAYOUT_ITER_CONT;
8018 * verify_mirror_ids() - Verify specified mirror ids.
8019 * @fname: Mirrored file name.
8020 * @mirror_ids: Specified mirror ids to be verified.
8021 * @ids_nr: Number of specified mirror ids.
8023 * This function verifies that specified @mirror_ids are valid
8024 * in the mirrored file @fname.
8026 * Return: 0 on success or a negative error code on failure.
8029 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
8031 struct llapi_layout *layout = NULL;
8032 struct verify_mirror_id mirror_id_cbdata = { 0 };
8043 if (stat(fname, &stbuf) < 0) {
8044 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8045 progname, fname, strerror(errno));
8050 if (!S_ISREG(stbuf.st_mode)) {
8051 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8057 fd = open(fname, O_DIRECT | O_RDONLY);
8059 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8060 progname, fname, strerror(errno));
8065 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8067 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8068 progname, fname, strerror(errno));
8072 layout = llapi_layout_get_by_fd(fd, 0);
8073 if (layout == NULL) {
8074 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8075 progname, fname, strerror(errno));
8077 llapi_lease_release(fd);
8081 rc = llapi_layout_flags_get(layout, &flr_state);
8083 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8084 progname, fname, strerror(errno));
8089 flr_state &= LCM_FL_FLR_MASK;
8090 switch (flr_state) {
8093 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8094 progname, fname, llapi_layout_flags_string(flr_state));
8101 for (i = 0; i < ids_nr; i++) {
8102 mirror_id_cbdata.mirror_id = mirror_ids[i];
8103 mirror_id_cbdata.is_valid_id = false;
8105 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8110 "%s: '%s' failed to verify mirror id: %u.\n",
8111 progname, fname, mirror_ids[i]);
8115 if (!mirror_id_cbdata.is_valid_id) {
8118 "%s: '%s' invalid specified mirror id: %u.\n",
8119 progname, fname, mirror_ids[i]);
8125 llapi_layout_free(layout);
8126 llapi_lease_release(fd);
8134 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8135 __u16 *mirror_ids, int ids_nr)
8137 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8138 struct llapi_layout *layout;
8148 if (stat(fname, &stbuf) < 0) {
8149 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8150 progname, fname, strerror(errno));
8154 if (!S_ISREG(stbuf.st_mode)) {
8155 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8161 fd = open(fname, O_DIRECT | O_RDWR);
8163 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8164 progname, fname, strerror(errno));
8169 layout = llapi_layout_get_by_fd(fd, 0);
8170 if (layout == NULL) {
8171 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8172 progname, fname, strerror(errno));
8177 rc = llapi_layout_flags_get(layout, &flr_state);
8179 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8180 progname, fname, strerror(errno));
8185 flr_state &= LCM_FL_FLR_MASK;
8186 if (flr_state == LCM_FL_NONE) {
8188 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8193 /* get stale component info */
8194 comp_size = llapi_mirror_find_stale(layout, comp_array,
8195 ARRAY_SIZE(comp_array),
8196 mirror_ids, ids_nr);
8197 if (comp_size <= 0) {
8202 ioc->lil_mode = LL_LEASE_WRLCK;
8203 ioc->lil_flags = LL_LEASE_RESYNC;
8204 rc = llapi_lease_set(fd, ioc);
8206 if (rc == -EALREADY)
8210 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8211 progname, fname, strerror(errno));
8215 /* get the read range [start, end) */
8216 start = comp_array[0].lrc_start;
8217 end = comp_array[0].lrc_end;
8218 for (idx = 1; idx < comp_size; idx++) {
8219 if (comp_array[idx].lrc_start < start)
8220 start = comp_array[idx].lrc_start;
8221 if (end < comp_array[idx].lrc_end)
8222 end = comp_array[idx].lrc_end;
8225 rc = llapi_lease_check(fd);
8226 if (rc != LL_LEASE_WRLCK) {
8227 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8232 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8235 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8236 progname, fname, rc);
8238 /* prepare ioc for lease put */
8239 ioc->lil_mode = LL_LEASE_UNLCK;
8240 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8242 for (idx = 0; idx < comp_size; idx++) {
8243 if (comp_array[idx].lrc_synced) {
8244 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8249 rc = llapi_lease_set(fd, ioc);
8251 if (rc == 0) /* lost lease lock */
8253 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8254 progname, fname, strerror(errno));
8258 * llapi_lease_set returns lease mode when it request to unlock
8264 llapi_layout_free(layout);
8271 static inline int lfs_mirror_resync(int argc, char **argv)
8273 struct ll_ioc_lease *ioc = NULL;
8274 __u16 mirror_ids[128] = { 0 };
8279 struct option long_opts[] = {
8280 { .val = 'o', .name = "only", .has_arg = required_argument },
8283 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8286 rc = parse_mirror_ids(mirror_ids,
8287 sizeof(mirror_ids) / sizeof(__u16),
8291 "%s: bad mirror ids '%s'.\n",
8298 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8299 argv[0], argv[optind - 1]);
8305 if (argc == optind) {
8306 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8311 if (ids_nr > 0 && argc > optind + 1) {
8313 "%s: option '--only' cannot be used upon multiple files.\n",
8321 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8326 /* set the lease on the file */
8327 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8329 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8330 argv[0], strerror(errno));
8335 for (; optind < argc; optind++) {
8336 rc = lfs_mirror_resync_file(argv[optind], ioc,
8337 mirror_ids, ids_nr);
8338 /* ignore previous file's error, continue with next file */
8341 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
8349 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
8351 struct llapi_layout *layout;
8354 layout = llapi_layout_get_by_fd(fd, 0);
8355 if (layout == NULL) {
8356 fprintf(stderr, "could not get layout.\n");
8360 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8362 fprintf(stderr, "failed to iterate layout\n");
8363 llapi_layout_free(layout);
8366 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8367 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
8368 llapi_layout_free(layout);
8372 llapi_layout_free(layout);
8378 * Check whether two files are the same file
8379 * \retval 0 same file
8380 * \retval 1 not the same file
8381 * \retval <0 error code
8383 static inline int check_same_file(const char *f1, const char *f2)
8388 if (stat(f1, &stbuf1) < 0) {
8389 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8390 progname, f1, strerror(errno));
8394 if (stat(f2, &stbuf2) < 0) {
8395 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
8396 progname, f2, strerror(errno));
8400 if (stbuf1.st_rdev == stbuf2.st_rdev &&
8401 stbuf1.st_ino == stbuf2.st_ino)
8407 static inline int lfs_mirror_read(int argc, char **argv)
8410 __u16 mirror_id = 0;
8411 const char *outfile = NULL;
8417 const size_t buflen = 4 << 20;
8419 struct option long_opts[] = {
8420 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8421 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8424 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8429 mirror_id = strtoul(optarg, &end, 0);
8430 if (*end != '\0' || mirror_id == 0) {
8432 "%s %s: invalid mirror ID '%s'\n",
8433 progname, argv[0], optarg);
8441 fprintf(stderr, "%s: option '%s' unrecognized.\n",
8442 progname, argv[optind - 1]);
8447 if (argc == optind) {
8448 fprintf(stderr, "%s %s: no mirrored file provided\n",
8451 } else if (argc > optind + 1) {
8452 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8456 if (mirror_id == 0) {
8457 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8462 /* open mirror file */
8463 fname = argv[optind];
8466 rc = check_same_file(fname, outfile);
8469 "%s %s: output file cannot be the mirrored file\n",
8477 fd = open(fname, O_DIRECT | O_RDONLY);
8479 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8480 progname, argv[0], fname, strerror(errno));
8484 /* verify mirror id */
8485 rc = verify_mirror_id_by_fd(fd, mirror_id);
8488 "%s %s: cannot find mirror with ID %u in '%s'\n",
8489 progname, argv[0], mirror_id, fname);
8493 /* open output file */
8495 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8497 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
8498 progname, argv[0], outfile, strerror(errno));
8503 outfd = STDOUT_FILENO;
8506 /* allocate buffer */
8507 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8509 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8510 progname, argv[0], rc);
8517 ssize_t written = 0;
8519 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8520 if (bytes_read < 0) {
8523 "%s %s: fail to read data from mirror %u: %s\n",
8524 progname, argv[0], mirror_id, strerror(-rc));
8529 if (bytes_read == 0)
8532 while (written < bytes_read) {
8535 written2 = write(outfd, buf + written,
8536 bytes_read - written);
8539 "%s %s: fail to write %s: %s\n",
8540 progname, argv[0], outfile ? : "STDOUT",
8545 written += written2;
8548 if (written != bytes_read) {
8550 "%s %s: written %ld bytes does not match with %ld read.\n",
8551 progname, argv[0], written, bytes_read);
8573 static inline int lfs_mirror_write(int argc, char **argv)
8576 __u16 mirror_id = 0;
8577 const char *inputfile = NULL;
8583 const size_t buflen = 4 << 20;
8585 size_t page_size = sysconf(_SC_PAGESIZE);
8586 struct ll_ioc_lease_id ioc;
8588 struct option long_opts[] = {
8589 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8590 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
8593 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
8598 mirror_id = strtoul(optarg, &end, 0);
8599 if (*end != '\0' || mirror_id == 0) {
8601 "%s %s: invalid mirror ID '%s'\n",
8602 progname, argv[0], optarg);
8610 fprintf(stderr, "%s: option '%s' unrecognized\n",
8611 progname, argv[optind - 1]);
8616 if (argc == optind) {
8617 fprintf(stderr, "%s %s: no mirrored file provided\n",
8620 } else if (argc > optind + 1) {
8621 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8625 if (mirror_id == 0) {
8626 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8631 /* open mirror file */
8632 fname = argv[optind];
8635 rc = check_same_file(fname, inputfile);
8638 "%s %s: input file cannot be the mirrored file\n",
8646 fd = open(fname, O_DIRECT | O_WRONLY);
8648 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8649 progname, argv[0], fname, strerror(errno));
8653 /* verify mirror id */
8654 rc = verify_mirror_id_by_fd(fd, mirror_id);
8657 "%s %s: cannot find mirror with ID %u in '%s'\n",
8658 progname, argv[0], mirror_id, fname);
8662 /* open input file */
8664 inputfd = open(inputfile, O_RDONLY, 0644);
8666 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
8667 progname, argv[0], inputfile, strerror(errno));
8672 inputfd = STDIN_FILENO;
8675 /* allocate buffer */
8676 rc = posix_memalign(&buf, page_size, buflen);
8678 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
8679 progname, argv[0], rc);
8683 /* prepare target mirror components instantiation */
8684 ioc.lil_mode = LL_LEASE_WRLCK;
8685 ioc.lil_flags = LL_LEASE_RESYNC;
8686 ioc.lil_mirror_id = mirror_id;
8687 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8690 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8691 progname, argv[0], fname, strerror(errno));
8701 rc = llapi_lease_check(fd);
8702 if (rc != LL_LEASE_WRLCK) {
8703 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
8704 progname, argv[0], fname);
8708 bytes_read = read(inputfd, buf, buflen);
8709 if (bytes_read < 0) {
8712 "%s %s: fail to read data from '%s': %s\n",
8713 progname, argv[0], inputfile ? : "STDIN",
8720 if (bytes_read == 0)
8723 /* round up to page align to make direct IO happy. */
8724 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
8726 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
8731 "%s %s: fail to write to mirror %u: %s\n",
8732 progname, argv[0], mirror_id,
8740 if (pos & (page_size - 1)) {
8741 rc = llapi_mirror_truncate(fd, mirror_id, pos);
8746 ioc.lil_mode = LL_LEASE_UNLCK;
8747 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
8749 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
8754 "%s %s: release lease lock of '%s' failed: %s\n",
8755 progname, argv[0], fname, strerror(errno));
8772 struct collect_ids_data {
8778 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
8780 struct collect_ids_data *cid = cbdata;
8784 rc = llapi_layout_mirror_id_get(layout, &id);
8788 if ((__u16)id != cid->cid_exclude) {
8791 for (i = 0; i < cid->cid_count; i++) {
8792 /* already collected the mirror id */
8793 if (id == cid->cid_ids[i])
8794 return LLAPI_LAYOUT_ITER_CONT;
8796 cid->cid_ids[cid->cid_count] = id;
8800 return LLAPI_LAYOUT_ITER_CONT;
8803 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
8805 struct llapi_layout *layout;
8806 struct collect_ids_data cid = { .cid_ids = ids,
8808 .cid_exclude = exclude_id, };
8811 layout = llapi_layout_get_by_fd(fd, 0);
8812 if (layout == NULL) {
8813 fprintf(stderr, "could not get layout\n");
8817 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
8819 fprintf(stderr, "failed to iterate layout\n");
8820 llapi_layout_free(layout);
8824 llapi_layout_free(layout);
8826 return cid.cid_count;
8829 static inline int lfs_mirror_copy(int argc, char **argv)
8832 __u16 read_mirror_id = 0;
8833 __u16 ids[128] = { 0 };
8835 struct llapi_layout *layout = NULL;
8836 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8843 struct ll_ioc_lease *ioc = NULL;
8844 struct ll_ioc_lease_id *resync_ioc;
8846 struct option long_opts[] = {
8847 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
8848 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
8851 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
8856 read_mirror_id = strtoul(optarg, &end, 0);
8857 if (*end != '\0' || read_mirror_id == 0) {
8859 "%s %s: invalid read mirror ID '%s'\n",
8860 progname, argv[0], optarg);
8865 if (!strcmp(optarg, "-1")) {
8866 /* specify all other mirrors */
8870 count = parse_mirror_ids((__u16 *)ids,
8878 fprintf(stderr, "%s: option '%s' unrecognized\n",
8879 progname, argv[optind - 1]);
8884 if (argc == optind) {
8885 fprintf(stderr, "%s %s: no mirrored file provided\n",
8888 } else if (argc > optind + 1) {
8889 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8893 if (read_mirror_id == 0) {
8895 "%s %s: no valid read mirror ID %d is provided\n",
8896 progname, argv[0], read_mirror_id);
8902 "%s %s: no write mirror ID is provided\n",
8907 for (i = 0; i < count; i++) {
8908 if (read_mirror_id == ids[i]) {
8910 "%s %s: read and write mirror ID cannot be the same\n",
8916 /* open mirror file */
8917 fname = argv[optind];
8919 fd = open(fname, O_DIRECT | O_RDWR);
8921 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
8922 progname, argv[0], fname, strerror(errno));
8926 /* write to all other mirrors */
8927 if (ids[0] == (__u16)-1) {
8928 count = get_other_mirror_ids(fd, ids, read_mirror_id);
8932 "%s %s: failed to get other mirror ids in '%s': %d\n",
8933 progname, argv[0], fname, rc);
8938 /* verify mirror id */
8939 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
8942 "%s %s: cannot find mirror with ID %u in '%s'\n",
8943 progname, argv[0], read_mirror_id, fname);
8947 for (i = 0; i < count; i++) {
8948 rc = verify_mirror_id_by_fd(fd, ids[i]);
8951 "%s %s: cannot find mirror with ID %u in '%s'\n",
8952 progname, argv[0], ids[i], fname);
8957 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8960 "%s %s: cannot alloc comp id array for ioc: %s\n",
8961 progname, argv[0], strerror(errno));
8966 /* get stale component info */
8967 layout = llapi_layout_get_by_fd(fd, 0);
8968 if (layout == NULL) {
8969 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
8970 progname, argv[0], fname, strerror(errno));
8974 comp_size = llapi_mirror_find_stale(layout, comp_array,
8975 ARRAY_SIZE(comp_array),
8977 llapi_layout_free(layout);
8978 if (comp_size < 0) {
8983 /* prepare target mirror components instantiation */
8984 resync_ioc = (struct ll_ioc_lease_id *)ioc;
8985 resync_ioc->lil_mode = LL_LEASE_WRLCK;
8986 resync_ioc->lil_flags = LL_LEASE_RESYNC;
8988 resync_ioc->lil_mirror_id = ids[0];
8990 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
8991 rc = llapi_lease_set(fd, ioc);
8994 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
8995 progname, argv[0], fname, strerror(errno));
8999 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
9002 fprintf(stderr, "%s %s: copy error: %d\n",
9003 progname, argv[0], rc);
9007 fprintf(stdout, "mirror copied successfully: ");
9008 for (i = 0; i < copied; i++)
9009 fprintf(stdout, "%d ", ids[i]);
9010 fprintf(stdout, "\n");
9012 ioc->lil_mode = LL_LEASE_UNLCK;
9013 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9015 for (i = 0; i < comp_size; i++) {
9018 for (j = 0; j < copied; j++) {
9019 if (comp_array[i].lrc_mirror_id != ids[j])
9022 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
9026 rc = llapi_lease_set(fd, ioc);
9031 "%s %s: release lease lock of '%s' failed: %s\n",
9032 progname, argv[0], fname, strerror(errno));
9046 * struct verify_chunk - Mirror chunk to be verified.
9047 * @chunk: [start, end) of the chunk.
9048 * @mirror_count: Number of mirror ids in @mirror_id array.
9049 * @mirror_id: Array of valid mirror ids that cover the chunk.
9051 struct verify_chunk {
9052 struct lu_extent chunk;
9053 unsigned int mirror_count;
9054 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
9058 * print_chunks() - Print chunk information.
9059 * @fname: Mirrored file name.
9060 * @chunks: Array of chunks.
9061 * @chunk_count: Number of chunks in @chunks array.
9063 * This function prints [start, end) of each chunk in @chunks
9064 * for mirrored file @fname, and also prints the valid mirror ids
9065 * that cover the chunk.
9070 void print_chunks(const char *fname, struct verify_chunk *chunks,
9076 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
9077 for (i = 0; i < chunk_count; i++) {
9078 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
9080 if (chunks[i].mirror_count == 0)
9081 fprintf(stdout, "\t[");
9083 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
9084 for (j = 1; j < chunks[i].mirror_count; j++)
9085 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
9087 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
9089 fprintf(stdout, "\n");
9093 * print_checksums() - Print CRC-32 checksum values.
9094 * @chunk: A chunk and its corresponding valid mirror ids.
9095 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
9097 * This function prints CRC-32 checksum values on @chunk for
9098 * each valid mirror that covers it.
9103 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
9108 "CRC-32 checksum value for chunk "DEXT":\n",
9109 PEXT(&chunk->chunk));
9110 for (i = 0; i < chunk->mirror_count; i++)
9111 fprintf(stdout, "Mirror %u:\t%#lx\n",
9112 chunk->mirror_id[i], crc[i]);
9113 fprintf(stdout, "\n");
9117 * filter_mirror_id() - Filter specified mirror ids.
9118 * @chunks: Array of chunks.
9119 * @chunk_count: Number of chunks in @chunks array.
9120 * @mirror_ids: Specified mirror ids to be verified.
9121 * @ids_nr: Number of specified mirror ids.
9123 * This function scans valid mirror ids that cover each chunk in @chunks
9124 * and filters specified mirror ids.
9129 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
9130 __u16 *mirror_ids, int ids_nr)
9135 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9136 unsigned int valid_count = 0;
9138 for (i = 0; i < chunk_count; i++) {
9139 if (chunks[i].mirror_count == 0)
9143 for (j = 0; j < ids_nr; j++) {
9144 for (k = 0; k < chunks[i].mirror_count; k++) {
9145 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
9146 valid_id[valid_count] = mirror_ids[j];
9153 memcpy(chunks[i].mirror_id, valid_id,
9154 sizeof(__u16) * valid_count);
9155 chunks[i].mirror_count = valid_count;
9160 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
9161 * @layout: Mirror component list.
9162 * @chunks: Array of chunks.
9163 * @chunks_size: Array size of @chunks.
9165 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
9166 * to find out chunk segments and store them in @chunks array.
9168 * The @mirror_id array in each element of @chunks will store the valid
9169 * mirror ids that cover the chunk. If a mirror component covering the
9170 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
9171 * will not be stored into the @mirror_id array, and the chunk for that
9172 * mirror will not be verified.
9174 * The @mirror_count in each element of @chunks will store the number of
9175 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
9176 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
9177 * indicates the chunk is valid in only one mirror. In both cases, the
9178 * chunk will not be verified.
9180 * Here is an example:
9183 * +------+-------------+--------------------+
9185 * +------+------+------+------+-------------+
9186 * | | S | S | | mirror2
9187 * +-------------+------+------+-------------+
9189 * prepared @chunks array will contain 5 elements:
9190 * (([0, 1M), [1, 2], 2),
9191 * ([1M, 2M), [1, 2], 2),
9192 * ([2M, 3M), [1], 1),
9193 * ([3M, 4M], [], 0),
9194 * ([4M, EOF), [2], 1))
9196 * Return: the actual array size of @chunks on success
9197 * or a negative error code on failure.
9200 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
9201 struct verify_chunk *chunks,
9212 memset(chunks, 0, sizeof(*chunks) * chunks_size);
9215 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
9218 "%s: move to the first layout component: %s.\n",
9219 progname, strerror(errno));
9225 chunks[idx].chunk.e_end = LUSTRE_EOF;
9227 rc = llapi_layout_comp_extent_get(layout, &start, &end);
9230 "%s: llapi_layout_comp_extent_get failed: %s.\n",
9231 progname, strerror(errno));
9235 if (start > chunks[idx].chunk.e_start ||
9236 end <= chunks[idx].chunk.e_start)
9239 if (end < chunks[idx].chunk.e_end)
9240 chunks[idx].chunk.e_end = end;
9242 rc = llapi_layout_comp_flags_get(layout, &flags);
9245 "%s: llapi_layout_comp_flags_get failed: %s.\n",
9246 progname, strerror(errno));
9250 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
9253 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9256 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9257 progname, strerror(errno));
9261 chunks[idx].mirror_id[i] = mirror_id;
9263 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
9265 "%s: mirror_id array is too small.\n",
9272 rc = llapi_layout_comp_use(layout,
9273 LLAPI_LAYOUT_COMP_USE_NEXT);
9276 "%s: move to the next layout component: %s.\n",
9277 progname, strerror(errno));
9280 } /* loop through all components */
9282 chunks[idx].mirror_count = i;
9284 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
9288 if (idx >= chunks_size) {
9289 fprintf(stderr, "%s: chunks array is too small.\n",
9295 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
9299 return rc < 0 ? rc : idx + 1;
9303 * lfs_mirror_verify_chunk() - Verify a chunk.
9304 * @fd: File descriptor of the mirrored file.
9305 * @file_size: Size of the mirrored file.
9306 * @chunk: A chunk and its corresponding valid mirror ids.
9307 * @verbose: Verbose mode.
9309 * This function verifies a @chunk contains exactly the same data
9310 * ammong the mirrors that cover it.
9312 * If @verbose is specified, then the function will print where the
9313 * differences are if the data do not match. Otherwise, it will
9314 * just return an error in that case.
9316 * Return: 0 on success or a negative error code on failure.
9319 int lfs_mirror_verify_chunk(int fd, size_t file_size,
9320 struct verify_chunk *chunk, int verbose)
9322 const size_t buflen = 4 * 1024 * 1024; /* 4M */
9324 size_t page_size = sysconf(_SC_PAGESIZE);
9330 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9337 rc = posix_memalign(&buf, page_size, buflen);
9338 if (rc) /* error code is returned directly */
9342 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9343 PEXT(&chunk->chunk));
9344 for (i = 0; i < chunk->mirror_count; i++)
9345 fprintf(stdout, " %u", chunk->mirror_id[i]);
9346 fprintf(stdout, "\n");
9350 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
9351 pos = chunk->chunk.e_start;
9352 while (bytes_done < count) {
9353 /* compute initial CRC-32 checksum */
9354 crc = crc32(0L, Z_NULL, 0);
9355 memset(crc_array, 0, sizeof(crc_array));
9358 for (i = 0; i < chunk->mirror_count; i++) {
9359 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
9361 if (bytes_read < 0) {
9364 "%s: failed to read data from mirror %u: %s.\n",
9365 progname, chunk->mirror_id[i],
9370 /* compute new CRC-32 checksum */
9371 crc_array[i] = crc32(crc, buf, bytes_read);
9375 print_checksums(chunk, crc_array);
9377 /* compare CRC-32 checksum values */
9378 for (i = 1; i < chunk->mirror_count; i++) {
9379 if (crc_array[i] != crc_array[0]) {
9383 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
9384 progname, PEXT(&chunk->chunk),
9385 chunk->mirror_id[0],
9386 chunk->mirror_id[i]);
9391 bytes_done += bytes_read;
9394 if (verbose > 1 && rc == 0) {
9395 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
9396 PEXT(&chunk->chunk));
9397 for (i = 0; i < chunk->mirror_count; i++)
9398 fprintf(stdout, " %u", chunk->mirror_id[i]);
9399 fprintf(stdout, " PASS\n\n");
9408 * lfs_mirror_verify_file() - Verify a mirrored file.
9409 * @fname: Mirrored file name.
9410 * @mirror_ids: Specified mirror ids to be verified.
9411 * @ids_nr: Number of specified mirror ids.
9412 * @verbose: Verbose mode.
9414 * This function verifies that each SYNC mirror of a mirrored file
9415 * specified by @fname contains exactly the same data.
9417 * If @mirror_ids is specified, then the function will verify the
9418 * mirrors specified by @mirror_ids contain exactly the same data.
9420 * If @verbose is specified, then the function will print where the
9421 * differences are if the data do not match. Otherwise, it will
9422 * just return an error in that case.
9424 * Return: 0 on success or a negative error code on failure.
9427 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
9430 struct verify_chunk chunks_array[1024] = { };
9431 struct llapi_layout *layout = NULL;
9435 int chunk_count = 0;
9441 if (stat(fname, &stbuf) < 0) {
9442 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9443 progname, fname, strerror(errno));
9448 if (!S_ISREG(stbuf.st_mode)) {
9449 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9455 if (stbuf.st_size == 0) {
9457 fprintf(stdout, "%s: '%s' file size is 0.\n",
9463 fd = open(fname, O_DIRECT | O_RDONLY);
9465 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9466 progname, fname, strerror(errno));
9471 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9473 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9474 progname, fname, strerror(errno));
9478 layout = llapi_layout_get_by_fd(fd, 0);
9479 if (layout == NULL) {
9480 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9481 progname, fname, strerror(errno));
9483 llapi_lease_release(fd);
9487 rc = llapi_layout_flags_get(layout, &flr_state);
9489 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9490 progname, fname, strerror(errno));
9495 flr_state &= LCM_FL_FLR_MASK;
9496 switch (flr_state) {
9499 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9500 progname, fname, llapi_layout_flags_string(flr_state));
9506 /* find out mirror chunks to be verified */
9507 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
9508 ARRAY_SIZE(chunks_array));
9509 if (chunk_count < 0) {
9515 /* filter specified mirror ids */
9516 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
9519 print_chunks(fname, chunks_array, chunk_count);
9521 for (idx = 0; idx < chunk_count; idx++) {
9522 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
9525 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
9527 PEXT(&chunks_array[idx].chunk),
9528 (unsigned long long)stbuf.st_size);
9532 if (chunks_array[idx].mirror_count == 0) {
9534 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
9536 PEXT(&chunks_array[idx].chunk));
9538 fprintf(stderr, "skipped\n");
9542 fprintf(stderr, "failed\n");
9546 if (chunks_array[idx].mirror_count == 1) {
9549 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
9551 PEXT(&chunks_array[idx].chunk),
9552 chunks_array[idx].mirror_id[0]);
9556 rc = llapi_lease_check(fd);
9557 if (rc != LL_LEASE_RDLCK) {
9558 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9563 /* verify one chunk */
9564 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
9565 &chunks_array[idx], verbose);
9579 llapi_layout_free(layout);
9580 llapi_lease_release(fd);
9588 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9589 * @argc: The count of lfs mirror verify command line arguments.
9590 * @argv: Array of strings for lfs mirror verify command line arguments.
9592 * This function parses lfs mirror verify command and verifies the
9593 * specified mirrored file(s).
9595 * Return: 0 on success or a negative error code on failure.
9597 static inline int lfs_mirror_verify(int argc, char **argv)
9599 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9607 struct option long_opts[] = {
9608 { .val = 'o', .name = "only", .has_arg = required_argument },
9609 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9612 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9614 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9617 rc = parse_mirror_ids(mirror_ids,
9618 ARRAY_SIZE(mirror_ids),
9622 "%s: bad mirror ids '%s'.\n",
9629 "%s: at least 2 mirror ids needed with '--only' option.\n",
9639 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9640 progname, argv[optind - 1]);
9646 if (argc == optind) {
9647 fprintf(stderr, "%s: no file name given.\n", progname);
9652 if (ids_nr > 0 && argc > optind + 1) {
9654 "%s: '--only' cannot be used upon multiple files.\n",
9662 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9668 for (; optind < argc; optind++) {
9669 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9679 * lfs_mirror() - Parse and execute lfs mirror commands.
9680 * @argc: The count of lfs mirror command line arguments.
9681 * @argv: Array of strings for lfs mirror command line arguments.
9683 * This function parses lfs mirror commands and performs the
9684 * corresponding functions specified in mirror_cmdlist[].
9686 * Return: 0 on success or an error code on failure.
9688 static int lfs_mirror(int argc, char **argv)
9695 Parser_init("lfs-mirror > ", mirror_cmdlist);
9697 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9699 program_invocation_short_name = cmd;
9701 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9703 rc = Parser_commands();
9705 return rc < 0 ? -rc : rc;
9708 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9710 #if __BYTE_ORDER == __BIG_ENDIAN
9711 __swab16s(&attrs->lsa_valid);
9712 __swab64s(&attrs->lsa_size);
9713 __swab64s(&attrs->lsa_blocks);
9719 LFS_SOM_BLOCKS = 0x2,
9720 LFS_SOM_FLAGS = 0x4,
9721 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9725 static int lfs_getsom(int argc, char **argv)
9728 struct lustre_som_attrs *attrs;
9729 char buf[sizeof(*attrs) + 64];
9730 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9733 while ((c = getopt(argc, argv, "sbf")) != -1) {
9736 type = LFS_SOM_SIZE;
9739 type = LFS_SOM_BLOCKS;
9742 type = LFS_SOM_FLAGS;
9745 fprintf(stderr, "%s: invalid option '%c'\n",
9755 fprintf(stderr, "%s: %s\n",
9756 progname, argc == 0 ? "miss file target" :
9757 "input more than 2 files");
9762 attrs = (void *)buf;
9763 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9766 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
9767 argv[0], strerror(errno), errno);
9771 lustre_som_swab(attrs);
9774 case LFS_SOM_ATTR_ALL:
9775 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9776 path, attrs->lsa_size, attrs->lsa_blocks,
9780 printf("%llu\n", attrs->lsa_size);
9782 case LFS_SOM_BLOCKS:
9783 printf("%llu\n", attrs->lsa_blocks);
9786 printf("%x\n", attrs->lsa_valid);
9789 fprintf(stderr, "%s: unknown option\n", progname);
9797 * lfs_mirror_list_commands() - List lfs mirror commands.
9798 * @argc: The count of command line arguments.
9799 * @argv: Array of strings for command line arguments.
9801 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9803 * Return: 0 on success.
9805 static int lfs_mirror_list_commands(int argc, char **argv)
9807 char buffer[81] = "";
9809 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9815 static int lfs_list_commands(int argc, char **argv)
9817 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9819 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9824 int main(int argc, char **argv)
9828 /* Ensure that liblustreapi constructor has run */
9829 if (!llapi_liblustreapi_initialized())
9830 fprintf(stderr, "liblustreapi was not properly initialized\n");
9835 Parser_init("lfs > ", cmdlist);
9837 progname = program_invocation_short_name; /* Used in error messages */
9839 llapi_set_command_name(argv[1]);
9840 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9841 llapi_clear_command_name();
9843 rc = Parser_commands();
9846 return rc < 0 ? -rc : rc;
9849 #ifdef _LUSTRE_IDL_H_
9850 /* Everything we need here should be included by lustreapi.h. */
9851 # error "lfs should not depend on lustre_idl.h"
9852 #endif /* _LUSTRE_IDL_H_ */