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);
128 enum setstripe_origin {
136 static int lfs_setstripe_internal(int argc, char **argv,
137 enum setstripe_origin opc);
139 static inline int lfs_setstripe(int argc, char **argv)
141 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
143 static inline int lfs_setstripe_migrate(int argc, char **argv)
145 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
147 static inline int lfs_mirror_create(int argc, char **argv)
149 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
151 static inline int lfs_mirror_extend(int argc, char **argv)
153 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
155 static inline int lfs_mirror_split(int argc, char **argv)
157 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
160 /* Setstripe and migrate share mostly the same parameters */
161 #define SSM_CMD_COMMON(cmd) \
162 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
163 " [--stripe-count|-c <stripe_count>]\n" \
164 " [--stripe-index|-i <start_ost_idx>]\n" \
165 " [--stripe-size|-S <stripe_size>]\n" \
166 " [--layout|-L <pattern>]\n" \
167 " [--pool|-p <pool_name>]\n" \
168 " [--ost|-o <ost_indices>]\n" \
169 " [--yaml|-y <yaml_template_file>]\n" \
170 " [--copy=<lustre_src>]\n"
172 #define SSM_HELP_COMMON \
173 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
174 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
175 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
176 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
177 "\t respectively)\n" \
178 "\tpool_name: Name of OST pool to use (default none)\n" \
179 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
180 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
181 "\t Indices be specified in a format of:\n" \
182 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
184 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
185 "\t If --pool is set with --ost, then the OSTs\n" \
186 "\t must be the members of the pool.\n" \
187 "\tcomp_end: Extent end of component, start after previous end.\n"\
188 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
189 "\t respectively, -1 for EOF). Must be a multiple of\n"\
190 "\t stripe_size.\n" \
191 "\tyaml_template_file:\n" \
192 "\t YAML layout template file, can't be used with -c,\n" \
193 "\t -i, -S, -p, -o, or -E arguments.\n" \
194 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
195 "\t another lustre file or directory, can't used with\n" \
196 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
198 #define MIRROR_CREATE_HELP \
199 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
200 "\t setstripe layout options\n" \
201 "\t It defaults to 1 if not specified; if specified,\n" \
202 "\t it must follow the option without a space.\n" \
203 "\t The option can also be repeated multiple times to\n" \
204 "\t separate mirrors that have different layouts.\n" \
205 "\tsetstripe options: Mirror layout\n" \
206 "\t It can be a plain layout or a composite layout.\n" \
207 "\t If not specified, the stripe options inherited\n" \
208 "\t from the previous component will be used.\n" \
209 "\tflags: set flags to the component of the current mirror.\n" \
210 "\t Only \"prefer\" flag is supported so far.\n"
212 #define MIRROR_EXTEND_HELP \
214 "\tvictim_file: The layout of victim_file will be split and used\n" \
215 "\t as a mirror added to the mirrored file.\n" \
216 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
217 "\t from victim file(s) in case the victim file(s)\n" \
218 "\t contains the same data as the original mirrored\n" \
221 #define MIRROR_EXTEND_USAGE \
222 " <--mirror-count|-N[mirror_count]>\n" \
223 " [setstripe options|-f <victim_file>]\n" \
226 #define SETSTRIPE_USAGE \
227 SSM_CMD_COMMON("setstripe") \
228 MIRROR_EXTEND_USAGE \
229 " <directory|filename>\n" \
233 #define MIGRATE_USAGE \
234 SSM_CMD_COMMON("migrate ") \
236 " [--non-block|-n]\n" \
237 " [--non-direct|-D]\n" \
241 "\tblock: Block file access during data migration (default)\n" \
242 "\tnon-block: Abort migrations if concurrent access is detected\n" \
243 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
245 #define SETDIRSTRIPE_USAGE \
246 " [--mdt-count|-c stripe_count>\n" \
247 " [--mdt-index|-i mdt_index]\n" \
248 " [--mdt-hash|-H mdt_hash]\n" \
249 " [--default|-D] [--mode|-m mode] <dir>\n" \
250 "\tstripe_count: stripe count of the striped directory\n" \
251 "\tmdt_index: MDT index of first stripe\n" \
252 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
253 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
254 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
255 "\tdefault_stripe: set default dirstripe of the directory\n" \
256 "\tmode: the mode of the directory\n"
259 * command_t mirror_cmdlist - lfs mirror commands.
261 command_t mirror_cmdlist[] = {
262 { .pc_name = "create", .pc_func = lfs_mirror_create,
263 .pc_help = "Create a mirrored file.\n"
264 "usage: lfs mirror create "
265 "<--mirror-count|-N[mirror_count]> "
266 "[setstripe options] ... <filename|directory>\n"
267 MIRROR_CREATE_HELP },
268 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
269 .pc_help = "Extend a mirrored file.\n"
270 "usage: lfs mirror extend "
271 "<--mirror-count|-N[mirror_count]> [--no-verify] "
272 "[setstripe options|-f <victim_file>] ... <filename>\n"
273 MIRROR_EXTEND_HELP },
274 { .pc_name = "split", .pc_func = lfs_mirror_split,
275 .pc_help = "Split a mirrored file.\n"
276 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
277 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
278 "\t [-f <new_file>] <mirrored file>\n"
279 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
280 "\t can be fetched by lfs getstripe command.\n"
281 "\tcomp_id: Unique component ID within a mirror.\n"
282 "\tnew_file: This option indicates the layout of the split\n"
283 "\t mirror will be stored into. If not specified,\n"
284 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
285 "\t will be used.\n" },
286 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
287 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
288 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
289 "<mirrored file> [<mirrored file2>...]\n"},
290 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
291 .pc_help = "Verify mirrored file(s).\n"
292 "usage: lfs mirror verify "
293 "[--only <mirror_id,mirror_id2[,...]>] "
294 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
295 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
296 .pc_help = "list commands supported by lfs mirror"},
297 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
298 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
299 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
303 /* all available commands */
304 command_t cmdlist[] = {
305 {"setstripe", lfs_setstripe, 0,
306 "To create a file with specified striping/composite layout, or\n"
307 "create/replace the default layout on an existing directory:\n"
308 SSM_CMD_COMMON("setstripe")
309 " <directory|filename>\n"
311 "To add component(s) to an existing composite file:\n"
312 SSM_CMD_COMMON("setstripe --component-add")
314 "To totally delete the default striping from an existing directory:\n"
315 "usage: setstripe -d <directory>\n"
317 "To create a mirrored file or set s default mirror layout on a directory:\n"
318 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
320 "To delete the last component(s) from an existing composite file\n"
321 "(note that this will also delete any data in those components):\n"
322 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
323 " [--component-flags|-F <comp_flags>]\n"
325 "\tcomp_id: Unique component ID to delete\n"
326 "\tcomp_flags: 'init' indicating all instantiated components\n"
327 "\t '^init' indicating all uninstantiated components\n"
328 "\t-I and -F cannot be specified at the same time\n"},
329 {"getstripe", lfs_getstripe, 0,
330 "To list the layout pattern for a given file or files in a\n"
331 "directory or recursively for all files in a directory tree.\n"
332 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
333 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
334 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
335 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
336 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
337 " [--component-id[=comp_id]|-I[comp_id]]\n"
338 " [--component-flags[=comp_flags]]\n"
339 " [--component-count]\n"
340 " [--component-start[=[+-]comp_start]]\n"
341 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
342 " [[!] --mirror-index=[+-]<index> |\n"
343 " [!] --mirror-id=[+-]<id>]\n"
344 " <directory|filename> ..."},
345 {"setdirstripe", lfs_setdirstripe, 0,
346 "To create a striped directory on a specified MDT. This can only\n"
347 "be done on MDT0 with the right of administrator.\n"
348 "usage: setdirstripe [OPTION] <directory>\n"
350 {"getdirstripe", lfs_getdirstripe, 0,
351 "To list the layout pattern info for a given directory\n"
352 "or recursively for all directories in a directory tree.\n"
353 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
354 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
355 " [--recursive|-r] [--yaml|-y]\n"
356 " [--default|-D] <dir> ..."},
357 {"mkdir", lfs_setdirstripe, 0,
358 "To create a striped directory on a specified MDT. This can only\n"
359 "be done on MDT0 with the right of administrator.\n"
360 "usage: mkdir [OPTION] <directory>\n"
362 {"rm_entry", lfs_rmentry, 0,
363 "To remove the name entry of the remote directory. Note: This\n"
364 "command will only delete the name entry, i.e. the remote directory\n"
365 "will become inaccessable after this command. This can only be done\n"
366 "by the administrator\n"
367 "usage: rm_entry <dir>\n"},
368 {"pool_list", lfs_poollist, 0,
369 "List pools or pool OSTs\n"
370 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
371 {"find", lfs_find, 0,
372 "find files matching given attributes recursively in directory tree.\n"
373 "usage: find <directory|filename> ...\n"
374 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
375 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
376 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
377 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
378 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
379 " [[!] --stripe-count|-c [+-]<stripes>]\n"
380 " [[!] --stripe-index|-i <index,...>]\n"
381 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
382 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
383 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
384 " [[!] --projid <projid>]\n"
385 " [[!] --layout|-L released,raid0,mdt]\n"
386 " [[!] --component-count [+-]<comp_cnt>]\n"
387 " [[!] --component-start [+-]N[kMGTPE]]\n"
388 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
389 " [[!] --component-flags <comp_flags>]\n"
390 " [[!] --mirror-count|-N [+-]<n>]\n"
391 " [[!] --mirror-state <[^]state>]\n"
392 " [[!] --mdt-count|-T [+-]<stripes>]\n"
393 " [[!] --mdt-hash|-H <hashtype>\n"
394 "\t !: used before an option indicates 'NOT' requested attribute\n"
395 "\t -: used before a value indicates less than requested value\n"
396 "\t +: used before a value indicates more than requested value\n"
397 "\thashtype: hash type of the striped directory.\n"
398 "\t fnv_1a_64 FNV-1a hash algorithm\n"
399 "\t all_char sum of characters % MDT_COUNT\n"},
400 {"check", lfs_check, 0,
401 "Display the status of MDS or OSTs (as specified in the command)\n"
402 "or all the servers (MDS and OSTs).\n"
403 "usage: check <osts|mds|servers>"},
404 {"osts", lfs_osts, 0, "list OSTs connected to client "
405 "[for specified path only]\n" "usage: osts [path]"},
406 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
407 "[for specified path only]\n" "usage: mdts [path]"},
409 "report filesystem disk space usage or inodes usage"
410 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
411 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
412 {"getname", lfs_getname, 0, "list instances and specified mount points "
413 "[for specified path only]\n"
414 "Usage: getname [-h]|[path ...] "},
415 #ifdef HAVE_SYS_QUOTA_H
416 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
417 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
418 " -b <block-softlimit> -B <block-hardlimit>\n"
419 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
420 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
421 " [--block-softlimit <block-softlimit>]\n"
422 " [--block-hardlimit <block-hardlimit>]\n"
423 " [--inode-softlimit <inode-softlimit>]\n"
424 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
425 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
426 " [--block-grace <block-grace>]\n"
427 " [--inode-grace <inode-grace>] <filesystem>\n"
428 " setquota <-U|-G|-P>\n"
429 " -b <block-softlimit> -B <block-hardlimit>\n"
430 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
431 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
432 " [--block-softlimit <block-softlimit>]\n"
433 " [--block-hardlimit <block-hardlimit>]\n"
434 " [--inode-softlimit <inode-softlimit>]\n"
435 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
436 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
438 " -b can be used instead of --block-softlimit/--block-grace\n"
439 " -B can be used instead of --block-hardlimit\n"
440 " -i can be used instead of --inode-softlimit/--inode-grace\n"
441 " -I can be used instead of --inode-hardlimit\n"
442 " -d can be used instead of --default\n\n"
443 "Note: The total quota space will be split into many qunits and\n"
444 " balanced over all server targets, the minimal qunit size is\n"
445 " 1M bytes for block space and 1K inodes for inode space.\n\n"
446 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
447 " Quota space rebalancing process will stop when this mininum\n"
448 " value is reached. As a result, quota exceeded can be returned\n"
449 " while many targets still have 1MB or 1K inodes of spare\n"
451 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
452 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
454 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
455 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
456 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
457 {"project", lfs_project, 0,
458 "Change or list project attribute for specified file or directory.\n"
459 "usage: project [-d|-r] <file|directory...>\n"
460 " list project ID and flags on file(s) or directories\n"
461 " project [-p id] [-s] [-r] <file|directory...>\n"
462 " set project ID and/or inherit flag for specified file(s) or directories\n"
463 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
464 " check project ID and flags on file(s) or directories, print outliers\n"
465 " project -C [-r] [-k] <file|directory...>\n"
466 " clear the project inherit flag and ID on the file or directory\n"
469 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
470 "usage: flushctx [-k] [mountpoint...]"},
472 "Remote user copy files and directories.\n"
473 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
475 "Remote user list directory contents.\n"
476 "usage: ls [OPTION]... [FILE]..."},
477 {"changelog", lfs_changelog, 0,
478 "Show the metadata changes on an MDT."
479 "\nusage: changelog <mdtname> [startrec [endrec]]"},
480 {"changelog_clear", lfs_changelog_clear, 0,
481 "Indicate that old changelog records up to <endrec> are no longer of "
482 "interest to consumer <id>, allowing the system to free up space.\n"
483 "An <endrec> of 0 means all records.\n"
484 "usage: changelog_clear <mdtname> <id> <endrec>"},
485 {"fid2path", lfs_fid2path, 0,
486 "Resolve the full path(s) for given FID(s). For a specific hardlink "
487 "specify link number <linkno>.\n"
488 /* "For a historical link name, specify changelog record <recno>.\n" */
489 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
490 /* [ --rec <recno> ] */ },
491 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
492 "usage: path2fid [--parents] <path> ..."},
493 {"data_version", lfs_data_version, 0, "Display file data version for "
494 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
495 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
496 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
497 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
498 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
499 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
500 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
502 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
503 "[--archived] [--lost] <file> ..."},
504 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
505 "given files.\n" "usage: hsm_action <file> ..."},
506 {"hsm_archive", lfs_hsm_archive, 0,
507 "Archive file to external storage.\n"
508 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
510 {"hsm_restore", lfs_hsm_restore, 0,
511 "Restore file from external storage.\n"
512 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
513 {"hsm_release", lfs_hsm_release, 0,
514 "Release files from Lustre.\n"
515 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
516 {"hsm_remove", lfs_hsm_remove, 0,
517 "Remove file copy from external storage.\n"
518 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
520 " (FILE [FILE ...] | "
521 "--mntpath MOUNTPATH FID [FID ...])\n"
523 "Note: To remove an archived copy of a file already deleted from a "
525 "--mntpath option and a list of FIDs must be specified"
527 {"hsm_cancel", lfs_hsm_cancel, 0,
528 "Cancel requests related to specified files.\n"
529 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
530 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
531 "usage: swap_layouts <path1> <path2>"},
532 {"migrate", lfs_setstripe_migrate, 0,
533 "migrate a directory between MDTs.\n"
534 "usage: migrate [--mdt|-m] <start_mdt_index>\n"
535 " [--mdt-count|-c] <stripe_count>\n"
536 " [--mdt-hash|-H] <hash_type>\n"
539 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
540 " it's the MDT index of first stripe\n"
541 "\tmdt_count: number of MDTs to stripe a directory over\n"
542 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
543 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
544 " all_char sum of characters % MDT_COUNT\n"
546 "migrate file objects from one OST "
547 "layout\nto another (may be not safe with concurent writes).\n"
549 "[--stripe-count|-c] <stripe_count>\n"
550 " [--stripe-index|-i] <start_ost_index>\n"
551 " [--stripe-size|-S] <stripe_size>\n"
552 " [--pool|-p] <pool_name>\n"
553 " [--ost|-o] <ost_indices>\n"
555 " [--non-block|-n]\n"
556 " [--non-direct|-D]\n"
557 " <file|directory>\n"
558 "\tstripe_count: number of OSTs to stripe a file over\n"
559 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
560 "\tstripe_size: number of bytes to store before moving to the next OST\n"
561 "\tpool_name: name of the predefined pool of OSTs\n"
562 "\tost_indices: OSTs to stripe over, in order\n"
563 "\tblock: wait for the operation to return before continuing\n"
564 "\tnon-block: do not wait for the operation to return\n"
565 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
567 "To move directories between MDTs. This command is deprecated, "
568 "use \"migrate\" instead.\n"
569 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
571 {"ladvise", lfs_ladvise, 0,
572 "Provide servers with advice about access patterns for a file.\n"
573 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
574 " [--background|-b] [--unset|-u]\n\n"
575 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
576 " {[--mode|-m [READ,WRITE]}\n"
578 {"mirror", lfs_mirror, mirror_cmdlist,
579 "lfs commands used to manage files with mirrored components:\n"
580 "lfs mirror create - create a mirrored file or directory\n"
581 "lfs mirror extend - add mirror(s) to an existing file\n"
582 "lfs mirror split - split a mirror from an existing mirrored file\n"
583 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
584 "lfs mirror verify - verify mirrored file(s)\n"},
585 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
586 "usage: getsom [-s] [-b] [-f] <path>\n"
587 "\t-s: Only show the size value of the SOM data for a given file\n"
588 "\t-b: Only show the blocks value of the SOM data for a given file\n"
589 "\t-f: Only show the flags value of the SOM data for a given file\n"},
590 {"help", Parser_help, 0, "help"},
591 {"exit", Parser_quit, 0, "quit"},
592 {"quit", Parser_quit, 0, "quit"},
593 {"--version", Parser_version, 0,
594 "output build version of the utility and exit"},
595 {"--list-commands", lfs_list_commands, 0,
596 "list commands supported by the utility and exit"},
601 static int check_hashtype(const char *hashtype)
605 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
606 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
613 static const char *error_loc = "syserror";
616 MIGRATION_NONBLOCK = 1 << 0,
617 MIGRATION_MIRROR = 1 << 1,
618 MIGRATION_NONDIRECT = 1 << 2,
621 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
622 struct llapi_layout *layout);
625 migrate_open_files(const char *name, __u64 migration_flags,
626 const struct llapi_stripe_param *param,
627 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
634 char parent[PATH_MAX];
635 char volatile_file[PATH_MAX];
641 if (param == NULL && layout == NULL) {
642 error_loc = "layout information";
646 /* search for file directory pathname */
647 if (strlen(name) > sizeof(parent) - 1) {
648 error_loc = "source file name";
652 strncpy(parent, name, sizeof(parent));
653 ptr = strrchr(parent, '/');
655 if (getcwd(parent, sizeof(parent)) == NULL) {
656 error_loc = "getcwd";
660 if (ptr == parent) /* leading '/' */
665 /* open file, direct io */
666 /* even if the file is only read, WR mode is nedeed to allow
667 * layout swap on fd */
669 if (!(migration_flags & MIGRATION_NONDIRECT))
671 fd = open(name, rflags);
674 error_loc = "cannot open source file";
678 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
680 error_loc = "cannot get MDT index";
685 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
686 mode_t open_mode = S_IRUSR | S_IWUSR;
688 random_value = random();
689 rc = snprintf(volatile_file, sizeof(volatile_file),
690 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
691 mdt_index, random_value);
692 if (rc >= sizeof(volatile_file)) {
697 /* create, open a volatile file, use caching (ie no directio) */
699 fdv = llapi_file_open_param(volatile_file, open_flags,
702 fdv = lfs_component_create(volatile_file, open_flags,
704 } while (fdv < 0 && (rc = fdv) == -EEXIST);
707 error_loc = "cannot create volatile file";
711 /* In case the MDT does not support creation of volatile files
712 * we should try to unlink it. */
713 (void)unlink(volatile_file);
715 /* Not-owner (root?) special case.
716 * Need to set owner/group of volatile file like original.
717 * This will allow to pass related check during layout_swap.
722 error_loc = "cannot stat source file";
726 rc = fstat(fdv, &stv);
729 error_loc = "cannot stat volatile";
733 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
734 rc = fchown(fdv, st.st_uid, st.st_gid);
737 error_loc = "cannot change ownwership of volatile";
756 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
758 struct llapi_layout *layout;
759 size_t buf_size = 4 * 1024 * 1024;
768 layout = llapi_layout_get_by_fd(fd_src, 0);
769 if (layout != NULL) {
770 uint64_t stripe_size;
772 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
774 buf_size = stripe_size;
776 llapi_layout_free(layout);
779 /* Use a page-aligned buffer for direct I/O */
780 rc = posix_memalign(&buf, getpagesize(), buf_size);
785 /* read new data only if we have written all
786 * previously read data */
789 rc = check_file(fd_src);
794 rsize = read(fd_src, buf, buf_size);
806 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
825 static int migrate_copy_timestamps(int fd, int fdv)
829 if (fstat(fd, &st) == 0) {
830 struct timeval tv[2] = {
831 {.tv_sec = st.st_atime},
832 {.tv_sec = st.st_mtime}
835 return futimes(fdv, tv);
841 static int migrate_block(int fd, int fdv)
848 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
850 error_loc = "cannot get dataversion";
858 /* The grouplock blocks all concurrent accesses to the file.
859 * It has to be taken after llapi_get_data_version as it would
861 rc = llapi_group_lock(fd, gid);
863 error_loc = "cannot get group lock";
867 rc = migrate_copy_data(fd, fdv, NULL);
869 error_loc = "data copy failed";
873 /* Make sure we keep original atime/mtime values */
874 rc = migrate_copy_timestamps(fd, fdv);
876 error_loc = "timestamp copy failed";
881 * for a migration we need to check data version on file did
884 * Pass in gid=0 since we already own grouplock. */
885 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
886 SWAP_LAYOUTS_CHECK_DV1);
888 error_loc = "file changed";
891 error_loc = "cannot swap layout";
896 rc2 = llapi_group_unlock(fd, gid);
897 if (rc2 < 0 && rc == 0) {
898 error_loc = "unlock group lock";
906 * Internal helper for migrate_copy_data(). Check lease and report error if
909 * \param[in] fd File descriptor on which to check the lease.
911 * \retval 0 Migration can keep on going.
912 * \retval -errno Error occurred, abort migration.
914 static int check_lease(int fd)
918 rc = llapi_lease_check(fd);
920 return 0; /* llapi_check_lease returns > 0 on success. */
925 static int migrate_nonblock(int fd, int fdv)
931 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
933 error_loc = "cannot get data version";
937 rc = migrate_copy_data(fd, fdv, check_lease);
939 error_loc = "data copy failed";
943 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
945 error_loc = "cannot get data version";
951 error_loc = "source file changed";
955 /* Make sure we keep original atime/mtime values */
956 rc = migrate_copy_timestamps(fd, fdv);
958 error_loc = "timestamp copy failed";
965 static int lfs_component_set(char *fname, int comp_id,
966 __u32 flags, __u32 neg_flags)
969 __u32 flags_array[2];
974 ids[count] = comp_id;
975 flags_array[count] = flags;
980 ids[count] = comp_id;
981 flags_array[count] = neg_flags | LCME_FL_NEG;
985 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
988 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
989 progname, comp_id, fname, flags, neg_flags);
994 static int lfs_component_del(char *fname, __u32 comp_id,
995 __u32 flags, __u32 neg_flags)
999 if (flags && neg_flags)
1002 if (!flags && neg_flags)
1003 flags = neg_flags | LCME_FL_NEG;
1005 if ((flags && comp_id) || (!flags && !comp_id))
1008 /* LCME_FL_INIT is the only supported flag in PFL */
1010 if (flags & ~LCME_KNOWN_FLAGS) {
1012 "%s setstripe: unknown flags %#x\n",
1016 } else if (comp_id > LCME_ID_MAX) {
1017 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1022 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1025 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1026 progname, comp_id, fname, strerror(errno));
1030 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1037 rc = llapi_layout_file_comp_add(fname, layout);
1039 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1040 fname, strerror(errno));
1044 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1045 struct llapi_layout *layout)
1053 fd = lstat(fname, &st);
1054 if (fd == 0 && S_ISDIR(st.st_mode))
1055 open_flags = O_DIRECTORY | O_RDONLY;
1057 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1059 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1060 S_ISDIR(st.st_mode) ?
1061 "set default composite layout for" :
1062 "create composite file",
1063 fname, strerror(errno));
1067 static int lfs_migrate(char *name, __u64 migration_flags,
1068 struct llapi_stripe_param *param,
1069 struct llapi_layout *layout)
1075 rc = migrate_open_files(name, migration_flags, param, layout,
1080 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1081 /* Blocking mode (forced if servers do not support file lease).
1082 * It is also the default mode, since we cannot distinguish
1083 * between a broken lease and a server that does not support
1084 * atomic swap/close (LU-6785) */
1085 rc = migrate_block(fd, fdv);
1089 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1091 error_loc = "cannot get lease";
1095 rc = migrate_nonblock(fd, fdv);
1097 llapi_lease_release(fd);
1101 /* Atomically put lease, swap layouts and close.
1102 * for a migration we need to check data version on file did
1104 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1106 error_loc = "cannot swap layout";
1118 fprintf(stderr, "error: %s: %s: %s: %s\n",
1119 progname, name, error_loc, strerror(-rc));
1123 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1132 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1136 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1137 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1138 const char *comp_name = comp_flags_table[i].cfn_name;
1140 if (strcmp(name, comp_name) == 0) {
1141 *flags |= comp_flag;
1143 } else if (strncmp(name, "^", 1) == 0 &&
1144 strcmp(name + 1, comp_name) == 0) {
1145 *neg_flags |= comp_flag;
1150 llapi_printf(LLAPI_MSG_ERROR,
1151 "%s: component flag '%s' not supported\n",
1157 if (!*flags && !*neg_flags)
1160 /* don't allow to set and exclude the same flag */
1161 if (*flags & *neg_flags)
1167 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1175 if (strncmp(string, "^", 1) == 0) {
1176 *neg_state = llapi_layout_string_flags(string + 1);
1177 if (*neg_state != 0)
1180 *state = llapi_layout_string_flags(string);
1185 llapi_printf(LLAPI_MSG_ERROR,
1186 "%s: mirrored file state '%s' not supported\n",
1192 * struct mirror_args - Command-line arguments for mirror(s).
1193 * @m_count: Number of mirrors to be created with this layout.
1194 * @m_flags: Mirror level flags, only 'prefer' is supported.
1195 * @m_layout: Mirror layout.
1196 * @m_file: A victim file. Its layout will be split and used as a mirror.
1197 * @m_next: Point to the next node of the list.
1199 * Command-line arguments for mirror(s) will be parsed and stored in
1200 * a linked list that consists of this structure.
1202 struct mirror_args {
1205 struct llapi_layout *m_layout;
1207 struct mirror_args *m_next;
1210 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1215 rc = llapi_layout_comp_flags_get(layout, &flags);
1219 if (flags & LCME_FL_NEG) {
1220 fprintf(stderr, "error: %s: negative flags are not supported\n",
1225 if (flags & LCME_FL_STALE) {
1226 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1227 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1231 return LLAPI_LAYOUT_ITER_CONT;
1234 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1236 uint64_t start, end;
1240 /* LU-10112: do not support dom+flr in phase 1 */
1241 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1245 rc = llapi_layout_pattern_get(layout, &pattern);
1249 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1250 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1255 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1259 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1263 if (end != LUSTRE_EOF) {
1264 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1269 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1274 * enum mirror_flags - Flags for extending a mirrored file.
1275 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1276 * in case the victim file(s) contains the same data as the
1277 * original mirrored file.
1278 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1279 * @MF_COMP_ID: specified component id instead of mirror id
1281 * Flags for extending a mirrored file.
1290 * mirror_create_sanity_check() - Check mirror list.
1291 * @list: A linked list that stores the mirror arguments.
1293 * This function does a sanity check on @list for creating
1296 * Return: 0 on success or a negative error code on failure.
1298 static int mirror_create_sanity_check(const char *fname,
1299 struct mirror_args *list)
1302 bool has_m_file = false;
1303 bool has_m_layout = false;
1309 struct llapi_layout *layout;
1311 layout = llapi_layout_get_by_path(fname, 0);
1314 "error: %s: file '%s' couldn't get layout\n",
1319 rc = mirror_sanity_check_one(layout);
1320 llapi_layout_free(layout);
1326 while (list != NULL) {
1327 if (list->m_file != NULL) {
1329 llapi_layout_free(list->m_layout);
1332 llapi_layout_get_by_path(list->m_file, 0);
1333 if (list->m_layout == NULL) {
1335 "error: %s: file '%s' has no layout\n",
1336 progname, list->m_file);
1340 has_m_layout = true;
1341 if (list->m_layout == NULL) {
1342 fprintf(stderr, "error: %s: no mirror layout\n",
1348 rc = mirror_sanity_check_one(list->m_layout);
1352 list = list->m_next;
1355 if (has_m_file && has_m_layout) {
1357 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1365 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1367 __u32 mirror_flags = *(__u32 *)cbdata;
1371 rc = llapi_layout_comp_flags_get(layout, &flags);
1376 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1381 return LLAPI_LAYOUT_ITER_CONT;
1385 * mirror_create() - Create a mirrored file.
1386 * @fname: The file to be created.
1387 * @mirror_list: A linked list that stores the mirror arguments.
1389 * This function creates a mirrored file @fname with the mirror(s)
1390 * from @mirror_list.
1392 * Return: 0 on success or a negative error code on failure.
1394 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1396 struct llapi_layout *layout = NULL;
1397 struct mirror_args *cur_mirror = NULL;
1398 uint16_t mirror_count = 0;
1402 rc = mirror_create_sanity_check(NULL, mirror_list);
1406 cur_mirror = mirror_list;
1407 while (cur_mirror != NULL) {
1408 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1410 &cur_mirror->m_flags);
1413 fprintf(stderr, "%s: failed to set mirror flags\n",
1418 for (i = 0; i < cur_mirror->m_count; i++) {
1419 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1422 fprintf(stderr, "error: %s: "
1423 "merge layout failed: %s\n",
1424 progname, strerror(errno));
1428 mirror_count += cur_mirror->m_count;
1429 cur_mirror = cur_mirror->m_next;
1432 if (layout == NULL) {
1433 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1437 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1440 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1441 progname, strerror(errno));
1445 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1453 llapi_layout_free(layout);
1458 * Compare files and check lease on @fd.
1460 * \retval bytes number of bytes are the same
1462 static ssize_t mirror_file_compare(int fd, int fdv)
1464 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1466 ssize_t bytes_done = 0;
1467 ssize_t bytes_read = 0;
1469 buf = malloc(buflen * 2);
1474 if (!llapi_lease_check(fd)) {
1475 bytes_done = -EBUSY;
1479 bytes_read = read(fd, buf, buflen);
1480 if (bytes_read <= 0)
1483 if (bytes_read != read(fdv, buf + buflen, buflen))
1486 /* XXX: should compute the checksum on each buffer and then
1487 * compare checksum to avoid cache collision */
1488 if (memcmp(buf, buf + buflen, bytes_read))
1491 bytes_done += bytes_read;
1499 static int mirror_extend_file(const char *fname, const char *victim_file,
1500 enum mirror_flags mirror_flags)
1505 struct stat stbuf_v;
1506 struct ll_ioc_lease *data = NULL;
1509 fd = open(fname, O_RDWR);
1511 error_loc = "open source file";
1516 fdv = open(victim_file, O_RDWR);
1518 error_loc = "open target file";
1523 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1524 error_loc = "stat source or target file";
1529 if (stbuf.st_dev != stbuf_v.st_dev) {
1530 error_loc = "stat source and target file";
1535 /* mirrors should be of the same size */
1536 if (stbuf.st_size != stbuf_v.st_size) {
1537 error_loc = "file sizes don't match";
1542 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1544 error_loc = "cannot get lease";
1548 if (!(mirror_flags & MF_NO_VERIFY)) {
1550 /* mirrors should have the same contents */
1551 ret = mirror_file_compare(fd, fdv);
1552 if (ret != stbuf.st_size) {
1553 error_loc = "file busy or contents don't match";
1554 rc = ret < 0 ? ret : -EINVAL;
1559 /* Get rid of caching pages from clients */
1560 rc = llapi_file_flush(fd);
1562 error_loc = "cannot get data version";
1566 rc = llapi_file_flush(fdv);
1568 error_loc = "cannot get data version";
1573 /* Make sure we keep original atime/mtime values */
1574 rc = migrate_copy_timestamps(fd, fdv);
1576 error_loc = "cannot copy timestamp";
1580 /* Atomically put lease, merge layouts and close. */
1581 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1583 error_loc = "memory allocation";
1586 data->lil_mode = LL_LEASE_UNLCK;
1587 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1588 data->lil_count = 1;
1589 data->lil_ids[0] = fdv;
1590 rc = llapi_lease_set(fd, data);
1592 error_loc = "cannot merge layout";
1594 } else if (rc == 0) {
1596 error_loc = "lost lease lock";
1609 (void) unlink(victim_file);
1611 fprintf(stderr, "error: %s: %s: %s: %s\n",
1612 progname, fname, error_loc, strerror(-rc));
1616 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1618 struct ll_ioc_lease *data = NULL;
1623 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1627 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1629 error_loc = "cannot get lease";
1633 rc = migrate_nonblock(fd, fdv);
1635 llapi_lease_release(fd);
1639 /* Atomically put lease, merge layouts and close. */
1640 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1642 error_loc = "memory allocation";
1645 data->lil_mode = LL_LEASE_UNLCK;
1646 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1647 data->lil_count = 1;
1648 data->lil_ids[0] = fdv;
1649 rc = llapi_lease_set(fd, data);
1651 error_loc = "cannot merge layout";
1653 } else if (rc == 0) {
1655 error_loc = "lost lease lock";
1668 fprintf(stderr, "error: %s: %s: %s: %s\n",
1669 progname, name, error_loc, strerror(-rc));
1673 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1674 enum mirror_flags mirror_flags)
1678 rc = mirror_create_sanity_check(fname, mirror_list);
1682 while (mirror_list) {
1683 if (mirror_list->m_file != NULL) {
1684 rc = mirror_extend_file(fname, mirror_list->m_file,
1687 __u32 mirror_count = mirror_list->m_count;
1689 while (mirror_count > 0) {
1690 rc = mirror_extend_layout(fname,
1691 mirror_list->m_layout);
1701 mirror_list = mirror_list->m_next;
1707 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1712 rc = llapi_layout_mirror_id_get(layout, &id);
1716 if ((__u16)id == *(__u16 *)cbdata)
1717 return LLAPI_LAYOUT_ITER_STOP;
1719 return LLAPI_LAYOUT_ITER_CONT;
1722 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1727 rc = llapi_layout_comp_id_get(layout, &id);
1731 if (id == *(__u32 *)cbdata)
1732 return LLAPI_LAYOUT_ITER_STOP;
1734 return LLAPI_LAYOUT_ITER_CONT;
1736 static int mirror_split(const char *fname, __u32 id,
1737 enum mirror_flags mflags, const char *victim_file)
1739 struct llapi_layout *layout;
1740 char parent[PATH_MAX];
1741 char victim[PATH_MAX];
1742 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1744 struct ll_ioc_lease *data;
1745 uint16_t mirror_count;
1750 /* check fname contains mirror with mirror_id/comp_id */
1751 layout = llapi_layout_get_by_path(fname, 0);
1754 "error %s: file '%s' couldn't get layout\n",
1759 rc = mirror_sanity_check_one(layout);
1763 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1766 "error %s: file '%s' couldn't get mirror count\n",
1770 if (mirror_count < 2) {
1772 "error %s: file '%s' has %d component, cannot split\n",
1773 progname, fname, mirror_count);
1777 if (mflags & MF_COMP_ID) {
1778 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1779 id = mirror_id_of(id);
1781 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1784 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1787 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1789 "error %s: file '%s' does not contain mirror with id %u\n",
1790 progname, fname, id);
1794 fd = open(fname, O_RDWR);
1797 "error %s: open file '%s' failed: %s\n",
1798 progname, fname, strerror(errno));
1802 /* get victim file directory pathname */
1803 if (strlen(fname) > sizeof(parent) - 1) {
1804 fprintf(stderr, "error %s: file name of '%s' too long\n",
1809 strncpy(parent, fname, sizeof(parent));
1810 ptr = strrchr(parent, '/');
1812 if (getcwd(parent, sizeof(parent)) == NULL) {
1813 fprintf(stderr, "error %s: getcwd failed: %s\n",
1814 progname, strerror(errno));
1824 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1826 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1831 if (victim_file == NULL) {
1832 /* use a temp file to store the splitted layout */
1833 if (mflags & MF_DESTROY) {
1834 fdv = llapi_create_volatile_idx(parent, mdt_index,
1835 O_LOV_DELAY_CREATE);
1837 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1839 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1842 /* user specified victim file */
1843 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1848 "error %s: create victim file failed: %s\n",
1849 progname, strerror(errno));
1853 /* get lease lock of fname */
1854 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1857 "error %s: cannot get lease of file '%s': %d\n",
1858 progname, fname, rc);
1862 /* Atomatically put lease, split layouts and close. */
1863 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1869 data->lil_mode = LL_LEASE_UNLCK;
1870 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1871 data->lil_count = 2;
1872 data->lil_ids[0] = fdv;
1873 data->lil_ids[1] = id;
1874 rc = llapi_lease_set(fd, data);
1876 if (rc == 0) /* lost lease lock */
1879 "error %s: cannot split '%s': %s\n",
1880 progname, fname, strerror(-rc));
1891 llapi_layout_free(layout);
1896 * Parse a string containing an target index list into an array of integers.
1898 * The input string contains a comma delimited list of individual
1899 * indices and ranges, for example "1,2-4,7". Add the indices into the
1900 * \a tgts array and remove duplicates.
1902 * \param[out] tgts array to store indices in
1903 * \param[in] size size of \a tgts array
1904 * \param[in] offset starting index in \a tgts
1905 * \param[in] arg string containing OST index list
1907 * \retval positive number of indices in \a tgts
1908 * \retval -EINVAL unable to parse \a arg
1910 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1914 int slots = size - offset;
1921 end_of_loop = false;
1922 while (!end_of_loop) {
1926 char *endptr = NULL;
1930 ptr = strchrnul(arg, ',');
1932 end_of_loop = *ptr == '\0';
1935 start_index = strtol(arg, &endptr, 0);
1936 if (endptr == arg) /* no data at all */
1938 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1941 end_index = start_index;
1942 if (*endptr == '-') {
1943 end_index = strtol(endptr + 1, &endptr, 0);
1944 if (*endptr != '\0')
1946 if (end_index < start_index)
1950 for (i = start_index; i <= end_index && slots > 0; i++) {
1953 /* remove duplicate */
1954 for (j = 0; j < offset; j++) {
1958 if (j == offset) { /* no duplicate */
1963 if (slots == 0 && i < end_index)
1971 if (!end_of_loop && ptr != NULL)
1974 return rc < 0 ? rc : nr;
1977 struct lfs_setstripe_args {
1978 unsigned long long lsa_comp_end;
1979 unsigned long long lsa_stripe_size;
1980 long long lsa_stripe_count;
1981 long long lsa_stripe_off;
1982 __u32 lsa_comp_flags;
1983 __u32 lsa_comp_neg_flags;
1984 unsigned long long lsa_pattern;
1985 unsigned int lsa_mirror_count;
1987 bool lsa_first_comp;
1989 char *lsa_pool_name;
1992 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1994 unsigned int mirror_count = lsa->lsa_mirror_count;
1995 bool first_comp = lsa->lsa_first_comp;
1997 memset(lsa, 0, sizeof(*lsa));
1999 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2000 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2001 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2002 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2003 lsa->lsa_pool_name = NULL;
2005 lsa->lsa_mirror_count = mirror_count;
2006 lsa->lsa_first_comp = first_comp;
2010 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2011 * @lsa: Stripe options to be initialized and inherited.
2013 * This function initializes stripe options in @lsa and inherit
2014 * stripe_size, stripe_count and OST pool_name options.
2018 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2020 unsigned long long stripe_size;
2021 long long stripe_count;
2022 char *pool_name = NULL;
2024 stripe_size = lsa->lsa_stripe_size;
2025 stripe_count = lsa->lsa_stripe_count;
2026 pool_name = lsa->lsa_pool_name;
2028 setstripe_args_init(lsa);
2030 lsa->lsa_stripe_size = stripe_size;
2031 lsa->lsa_stripe_count = stripe_count;
2032 lsa->lsa_pool_name = pool_name;
2035 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2037 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2038 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2039 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2040 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2041 lsa->lsa_pool_name != NULL ||
2042 lsa->lsa_comp_end != 0);
2046 * comp_args_to_layout() - Create or extend a composite layout.
2047 * @composite: Pointer to the composite layout.
2048 * @lsa: Stripe options for the new component.
2050 * This function creates or extends a composite layout by adding a new
2051 * component with stripe options from @lsa.
2053 * Return: 0 on success or an error code on failure.
2055 static int comp_args_to_layout(struct llapi_layout **composite,
2056 struct lfs_setstripe_args *lsa,
2059 struct llapi_layout *layout = *composite;
2060 uint64_t prev_end = 0;
2063 if (layout == NULL) {
2064 layout = llapi_layout_alloc();
2065 if (layout == NULL) {
2066 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2070 *composite = layout;
2074 /* Get current component extent, current component
2075 * must be the tail component. */
2076 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2078 fprintf(stderr, "Get comp extent failed. %s\n",
2083 if (lsa->lsa_first_comp)
2086 if (lsa->lsa_first_comp)
2087 rc = llapi_layout_add_first_comp(layout);
2089 rc = llapi_layout_comp_add(layout);
2091 fprintf(stderr, "Add component failed. %s\n",
2096 /* reset lsa_first_comp */
2097 lsa->lsa_first_comp = false;
2100 rc = llapi_layout_comp_extent_set(layout, prev_end,
2103 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2104 prev_end, lsa->lsa_comp_end, strerror(errno));
2109 /* Data-on-MDT component setting */
2110 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2111 /* In case of Data-on-MDT patterns the only extra option
2112 * applicable is stripe size option. */
2113 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2114 fprintf(stderr, "Option 'stripe-count' can't be "
2115 "specified with Data-on-MDT component: %lld\n",
2116 lsa->lsa_stripe_count);
2119 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2120 fprintf(stderr, "Option 'stripe-size' can't be "
2121 "specified with Data-on-MDT component: %llu\n",
2122 lsa->lsa_stripe_size);
2125 if (lsa->lsa_nr_tgts != 0) {
2126 fprintf(stderr, "Option 'ost-list' can't be specified "
2127 "with Data-on-MDT component: '%i'\n",
2131 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2132 fprintf(stderr, "Option 'stripe-offset' can't be "
2133 "specified with Data-on-MDT component: %lld\n",
2134 lsa->lsa_stripe_off);
2137 if (lsa->lsa_pool_name != 0) {
2138 fprintf(stderr, "Option 'pool' can't be specified "
2139 "with Data-on-MDT component: '%s'\n",
2140 lsa->lsa_pool_name);
2144 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2146 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2147 lsa->lsa_pattern, strerror(errno));
2150 /* Data-on-MDT component has always single stripe up to end */
2151 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2154 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2156 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2157 lsa->lsa_stripe_size, strerror(errno));
2161 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2163 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2164 lsa->lsa_stripe_count, strerror(errno));
2168 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2170 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2171 lsa->lsa_comp_flags, strerror(errno));
2175 if (lsa->lsa_pool_name != NULL) {
2176 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2178 fprintf(stderr, "Set pool name: %s failed. %s\n",
2179 lsa->lsa_pool_name, strerror(errno));
2183 rc = llapi_layout_pool_name_set(layout, "");
2185 fprintf(stderr, "Clear pool name failed: %s\n",
2191 if (lsa->lsa_nr_tgts > 0) {
2192 if (lsa->lsa_stripe_count > 0 &&
2193 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2194 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2195 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2196 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2197 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2200 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2201 rc = llapi_layout_ost_index_set(layout, i,
2206 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2207 lsa->lsa_stripe_off != -1) {
2208 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2211 fprintf(stderr, "Set ost index %d failed. %s\n",
2212 i, strerror(errno));
2219 static int build_component(struct llapi_layout **layout,
2220 struct lfs_setstripe_args *lsa, bool set_extent)
2224 rc = comp_args_to_layout(layout, lsa, set_extent);
2228 if (lsa->lsa_mirror_count > 0) {
2229 rc = llapi_layout_mirror_count_set(*layout,
2230 lsa->lsa_mirror_count);
2234 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2237 lsa->lsa_mirror_count = 0;
2243 static int build_layout_from_yaml_node(struct cYAML *node,
2244 struct llapi_layout **layout,
2245 struct lfs_setstripe_args *lsa,
2252 string = node->cy_string;
2253 /* skip leading lmm_ if present, to simplify parsing */
2254 if (string != NULL && strncmp(string, "lmm_", 4) == 0)
2257 if (node->cy_type == CYAML_TYPE_STRING) {
2258 if (!strcmp(string, "lcme_extent.e_end")) {
2259 if (!strcmp(node->cy_valuestring, "EOF") ||
2260 !strcmp(node->cy_valuestring, "eof"))
2261 lsa->lsa_comp_end = LUSTRE_EOF;
2262 } else if (!strcmp(string, "pool")) {
2263 lsa->lsa_pool_name = node->cy_valuestring;
2264 } else if (!strcmp(string, "pattern")) {
2265 if (!strcmp(node->cy_valuestring, "mdt"))
2266 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2268 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2269 if (!strcmp(string, "lcm_mirror_count")) {
2270 lsa->lsa_mirror_count = node->cy_valueint;
2271 } else if (!strcmp(string, "lcme_extent.e_start")) {
2272 if (node->cy_valueint != 0 || *layout != NULL) {
2273 rc = build_component(layout, lsa, true);
2278 if (node->cy_valueint == 0)
2279 lsa->lsa_first_comp = true;
2281 /* initialize lsa */
2282 setstripe_args_init(lsa);
2283 lsa->lsa_tgts = osts;
2284 } else if (!strcmp(string, "lcme_extent.e_end")) {
2285 if (node->cy_valueint == -1)
2286 lsa->lsa_comp_end = LUSTRE_EOF;
2288 lsa->lsa_comp_end = node->cy_valueint;
2289 } else if (!strcmp(string, "stripe_count")) {
2290 lsa->lsa_stripe_count = node->cy_valueint;
2291 } else if (!strcmp(string, "stripe_size")) {
2292 lsa->lsa_stripe_size = node->cy_valueint;
2293 } else if (!strcmp(string, "stripe_offset")) {
2294 lsa->lsa_stripe_off = node->cy_valueint;
2295 } else if (!strcmp(string, "l_ost_idx")) {
2296 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2299 } else if (node->cy_type == CYAML_TYPE_OBJECT) {
2300 /* go deep to sub blocks */
2301 rc = build_layout_from_yaml_node(node->cy_child, layout,
2306 node = node->cy_next;
2312 static int lfs_comp_create_from_yaml(char *template,
2313 struct llapi_layout **layout,
2314 struct lfs_setstripe_args *lsa,
2317 struct cYAML *tree = NULL, *err_rc = NULL;
2320 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2322 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2323 progname, template);
2324 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2325 "can't parse", &err_rc);
2326 cYAML_print_tree2file(stderr, err_rc);
2327 cYAML_free_tree(err_rc);
2332 /* initialize lsa for plain file */
2333 setstripe_args_init(lsa);
2334 lsa->lsa_tgts = osts;
2336 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2338 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2339 progname, template);
2342 rc = build_component(layout, lsa, *layout != NULL);
2344 /* clean clean lsa */
2345 setstripe_args_init(lsa);
2349 cYAML_free_tree(tree);
2353 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2354 * end of the last component in the existing file, and adjust the
2355 * first extent start of the components to be added accordingly. */
2356 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2358 struct llapi_layout *head;
2359 uint64_t start, end, stripe_size, prev_end = 0;
2362 if (layout == NULL) {
2364 "%s setstripe: layout must be specified\n",
2370 head = llapi_layout_get_by_path(fname, 0);
2373 "%s setstripe: cannot read layout from '%s': %s\n",
2374 progname, fname, strerror(errno));
2376 } else if (errno == ENODATA) {
2377 /* file without LOVEA, this component-add will be turned
2378 * into a component-create. */
2379 llapi_layout_free(head);
2381 } else if (!llapi_layout_is_composite(head)) {
2382 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2384 llapi_layout_free(head);
2388 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2390 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2391 progname, strerror(errno));
2392 llapi_layout_free(head);
2396 llapi_layout_free(head);
2398 /* Make sure we use the first component of the layout to be added. */
2399 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2402 "%s setstripe: cannot move component cursor: %s\n",
2403 progname, strerror(errno));
2407 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2409 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2410 progname, strerror(errno));
2414 if (start > prev_end || end <= prev_end) {
2416 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2417 progname, start, end, prev_end);
2421 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2423 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2424 progname, strerror(errno));
2428 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2429 (prev_end & (stripe_size - 1))) {
2431 "%s setstripe: stripe size %lu not aligned with %lu\n",
2432 progname, stripe_size, prev_end);
2436 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2439 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2440 progname, prev_end, end, strerror(errno));
2447 static inline bool arg_is_eof(char *arg)
2449 return !strncmp(arg, "-1", strlen("-1")) ||
2450 !strncmp(arg, "EOF", strlen("EOF")) ||
2451 !strncmp(arg, "eof", strlen("eof"));
2455 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2457 * Return: Valid mirror_args pointer on success and
2458 * NULL if memory allocation fails.
2460 static struct mirror_args *lfs_mirror_alloc(void)
2462 struct mirror_args *mirror = NULL;
2465 mirror = calloc(1, sizeof(*mirror));
2476 * lfs_mirror_free() - Free memory allocated for a mirror argument
2478 * @mirror: Previously allocated mirror argument structure by
2479 * lfs_mirror_alloc().
2481 * Free memory allocated for @mirror.
2485 static void lfs_mirror_free(struct mirror_args *mirror)
2487 if (mirror->m_layout != NULL)
2488 llapi_layout_free(mirror->m_layout);
2493 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2494 * @mirror_list: Previously allocated mirror list.
2496 * Free memory allocated for @mirror_list.
2500 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2502 struct mirror_args *next_mirror = NULL;
2504 while (mirror_list != NULL) {
2505 next_mirror = mirror_list->m_next;
2506 lfs_mirror_free(mirror_list);
2507 mirror_list = next_mirror;
2519 LFS_COMP_NO_VERIFY_OPT,
2521 LFS_MIRROR_FLAGS_OPT,
2523 LFS_MIRROR_STATE_OPT,
2525 LFS_MIRROR_INDEX_OPT,
2529 static int lfs_setstripe_internal(int argc, char **argv,
2530 enum setstripe_origin opc)
2532 struct lfs_setstripe_args lsa = { 0 };
2533 struct llapi_stripe_param *param = NULL;
2534 struct find_param migrate_mdt_param = {
2544 unsigned long long size_units = 1;
2545 bool migrate_mode = false;
2546 bool migrate_mdt_mode = false;
2547 bool migration_block = false;
2548 __u64 migration_flags = 0;
2549 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2550 int comp_del = 0, comp_set = 0;
2553 struct llapi_layout *layout = NULL;
2554 struct llapi_layout **lpp = &layout;
2555 bool mirror_mode = false;
2556 bool has_m_file = false;
2557 __u32 mirror_count = 0;
2558 enum mirror_flags mirror_flags = 0;
2559 struct mirror_args *mirror_list = NULL;
2560 struct mirror_args *new_mirror = NULL;
2561 struct mirror_args *last_mirror = NULL;
2562 __u16 mirror_id = 0;
2564 bool from_yaml = false;
2565 bool from_copy = false;
2566 char *template = NULL;
2568 struct option long_opts[] = {
2569 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2570 /* --block is only valid in migrate mode */
2571 { .val = 'b', .name = "block", .has_arg = no_argument },
2572 { .val = LFS_COMP_ADD_OPT,
2573 .name = "comp-add", .has_arg = no_argument },
2574 { .val = LFS_COMP_ADD_OPT,
2575 .name = "component-add", .has_arg = no_argument },
2576 { .val = LFS_COMP_DEL_OPT,
2577 .name = "comp-del", .has_arg = no_argument },
2578 { .val = LFS_COMP_DEL_OPT,
2579 .name = "component-del", .has_arg = no_argument },
2580 { .val = LFS_COMP_FLAGS_OPT,
2581 .name = "comp-flags", .has_arg = required_argument },
2582 { .val = LFS_COMP_FLAGS_OPT,
2583 .name = "component-flags",
2584 .has_arg = required_argument },
2585 { .val = LFS_COMP_SET_OPT,
2586 .name = "comp-set", .has_arg = no_argument },
2587 { .val = LFS_COMP_SET_OPT,
2588 .name = "component-set",
2589 .has_arg = no_argument},
2590 { .val = LFS_COMP_NO_VERIFY_OPT,
2591 .name = "no-verify", .has_arg = no_argument},
2592 { .val = LFS_MIRROR_FLAGS_OPT,
2593 .name = "flags", .has_arg = required_argument},
2594 { .val = LFS_MIRROR_ID_OPT,
2595 .name = "mirror-id", .has_arg = required_argument},
2596 { .val = LFS_LAYOUT_COPY,
2597 .name = "copy", .has_arg = required_argument},
2598 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2599 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2600 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2601 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2602 { .val = 'd', .name = "delete", .has_arg = no_argument},
2603 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2604 /* --non-direct is only valid in migrate mode */
2605 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2606 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2607 { .val = 'E', .name = "component-end",
2608 .has_arg = required_argument},
2609 { .val = 'f', .name = "file", .has_arg = required_argument },
2610 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2611 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2612 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2613 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2614 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2615 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2616 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2617 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2618 { .val = 'L', .name = "layout", .has_arg = required_argument },
2619 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2620 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2621 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2622 /* --non-block is only valid in migrate mode */
2623 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2624 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2625 { .val = 'o', .name = "ost", .has_arg = required_argument },
2626 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2627 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2628 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2630 { .val = 'p', .name = "pool", .has_arg = required_argument },
2631 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2632 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2633 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2634 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2635 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2636 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2637 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2638 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2639 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2640 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2641 /* --verbose is only valid in migrate mode */
2642 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2643 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2646 setstripe_args_init(&lsa);
2648 migrate_mode = (opc == SO_MIGRATE);
2649 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2651 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2653 while ((c = getopt_long(argc, argv,
2654 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2660 case LFS_COMP_ADD_OPT:
2663 case LFS_COMP_DEL_OPT:
2666 case LFS_COMP_FLAGS_OPT:
2667 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2668 &lsa.lsa_comp_neg_flags);
2671 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2672 fprintf(stderr, "%s: inverted flags are not supported\n",
2676 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2678 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2685 case LFS_COMP_SET_OPT:
2688 case LFS_COMP_NO_VERIFY_OPT:
2689 mirror_flags |= MF_NO_VERIFY;
2691 case LFS_MIRROR_ID_OPT:
2692 mirror_id = strtoul(optarg, &end, 0);
2693 if (*end != '\0' || mirror_id == 0) {
2695 "%s %s: invalid mirror ID '%s'\n",
2696 progname, argv[0], optarg);
2700 case LFS_MIRROR_FLAGS_OPT: {
2703 if (!mirror_mode || !last_mirror) {
2704 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2709 result = comp_str2flags(optarg, &last_mirror->m_flags,
2715 fprintf(stderr, "%s: inverted flags are not supported\n",
2720 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2722 "%s: unsupported mirror flags: %s\n",
2729 case LFS_LAYOUT_COPY:
2734 if (!migrate_mode) {
2736 "%s %s: -b|--block valid only for migrate command\n",
2740 migration_block = true;
2743 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2746 "%s %s: invalid stripe count '%s'\n",
2747 progname, argv[0], optarg);
2751 if (lsa.lsa_stripe_count == -1)
2752 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2755 /* delete the default striping pattern */
2757 if (opc == SO_MIRROR_SPLIT) {
2760 "%s %s: -d cannot used with -f\n",
2764 mirror_flags |= MF_DESTROY;
2768 if (!migrate_mode) {
2770 "%s %s: -D|--non-direct is valid "
2771 "only for migrate command\n",
2775 migration_flags |= MIGRATION_NONDIRECT;
2778 if (lsa.lsa_comp_end != 0) {
2779 result = comp_args_to_layout(lpp, &lsa, true);
2782 "%s %s: invalid layout\n",
2787 setstripe_args_init_inherit(&lsa);
2790 if (arg_is_eof(optarg)) {
2791 lsa.lsa_comp_end = LUSTRE_EOF;
2793 result = llapi_parse_size(optarg,
2798 "%s %s: invalid component end '%s'\n",
2799 progname, argv[0], optarg);
2805 if (!migrate_mode) {
2806 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2810 lsa.lsa_pattern = check_hashtype(optarg);
2811 if (lsa.lsa_pattern == 0) {
2813 "%s %s: bad stripe hash type '%s'\n",
2814 progname, argv[0], optarg);
2819 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2822 "%s %s: invalid stripe offset '%s'\n",
2823 progname, argv[0], optarg);
2826 if (lsa.lsa_stripe_off == -1)
2827 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2830 comp_id = strtoul(optarg, &end, 0);
2831 if (*end != '\0' || comp_id == 0 ||
2832 comp_id > LCME_ID_MAX) {
2834 "%s %s: invalid component ID '%s'\n",
2835 progname, argv[0], optarg);
2840 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2842 "error: %s: invalid option: %s\n",
2843 progname, argv[optopt + 1]);
2846 if (opc == SO_MIRROR_EXTEND) {
2847 if (last_mirror == NULL) {
2849 "error: %s: '-N' must exist in front of '%s'\n",
2850 progname, argv[optopt + 1]);
2853 last_mirror->m_file = optarg;
2854 last_mirror->m_count = 1;
2857 if (mirror_list == NULL)
2858 mirror_list = lfs_mirror_alloc();
2859 mirror_list->m_file = optarg;
2864 if (strcmp(argv[optind - 1], "mdt") == 0) {
2865 /* Can be only the first component */
2866 if (layout != NULL) {
2868 fprintf(stderr, "error: 'mdt' layout "
2869 "can be only the first one\n");
2872 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2874 fprintf(stderr, "error: 'mdt' layout "
2875 "size is too big\n");
2878 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2879 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2881 fprintf(stderr, "error: layout '%s' is "
2882 "unknown, supported layouts are: "
2883 "'mdt', 'raid0'\n", argv[optind]);
2888 if (!migrate_mode) {
2890 "%s %s: -m|--mdt is valid only for migrate command\n",
2894 migrate_mdt_mode = true;
2895 lsa.lsa_nr_tgts = parse_targets(tgts,
2896 sizeof(tgts) / sizeof(__u32),
2897 lsa.lsa_nr_tgts, optarg);
2898 if (lsa.lsa_nr_tgts < 0) {
2900 "%s %s: invalid MDT target(s) '%s'\n",
2901 progname, argv[0], optarg);
2905 lsa.lsa_tgts = tgts;
2906 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2907 lsa.lsa_stripe_off = tgts[0];
2910 if (!migrate_mode) {
2912 "%s %s: -n|--non-block valid only for migrate command\n",
2916 migration_flags |= MIGRATION_NONBLOCK;
2919 if (opc == SO_SETSTRIPE) {
2920 opc = SO_MIRROR_CREATE;
2924 if (optarg != NULL) {
2925 mirror_count = strtoul(optarg, &end, 0);
2926 if (*end != '\0' || mirror_count == 0) {
2928 "error: %s: bad mirror count: %s\n",
2935 new_mirror = lfs_mirror_alloc();
2936 new_mirror->m_count = mirror_count;
2938 if (mirror_list == NULL)
2939 mirror_list = new_mirror;
2941 if (last_mirror != NULL) {
2942 /* wrap up last mirror */
2943 if (lsa.lsa_comp_end == 0)
2944 lsa.lsa_comp_end = LUSTRE_EOF;
2946 result = comp_args_to_layout(lpp, &lsa, true);
2948 lfs_mirror_free(new_mirror);
2952 setstripe_args_init_inherit(&lsa);
2954 last_mirror->m_next = new_mirror;
2957 last_mirror = new_mirror;
2958 lpp = &last_mirror->m_layout;
2961 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2962 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2963 fprintf(stderr, "warning: '--ost-list' is "
2964 "deprecated, use '--ost' instead\n");
2966 lsa.lsa_nr_tgts = parse_targets(tgts,
2967 sizeof(tgts) / sizeof(__u32),
2968 lsa.lsa_nr_tgts, optarg);
2969 if (lsa.lsa_nr_tgts < 0) {
2971 "%s %s: invalid OST target(s) '%s'\n",
2972 progname, argv[0], optarg);
2976 lsa.lsa_tgts = tgts;
2977 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2978 lsa.lsa_stripe_off = tgts[0];
2983 lsa.lsa_pool_name = optarg;
2985 if (strlen(lsa.lsa_pool_name) == 0 ||
2986 strncmp(lsa.lsa_pool_name, "none",
2987 LOV_MAXPOOLNAME) == 0)
2988 lsa.lsa_pool_name = NULL;
2991 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2995 "%s %s: invalid stripe size '%s'\n",
2996 progname, argv[0], optarg);
3001 if (!migrate_mode) {
3003 "%s %s: -v|--verbose valid only for migrate command\n",
3007 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3014 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3015 progname, argv[0], argv[optind - 1]);
3020 fname = argv[optind];
3022 if (optind == argc) {
3023 fprintf(stderr, "%s %s: FILE must be specified\n",
3028 if (mirror_mode && mirror_count == 0) {
3030 "error: %s: --mirror-count|-N option is required\n",
3037 if (lsa.lsa_comp_end == 0)
3038 lsa.lsa_comp_end = LUSTRE_EOF;
3041 if (lsa.lsa_comp_end != 0) {
3042 result = comp_args_to_layout(lpp, &lsa, true);
3047 if (mirror_flags & MF_NO_VERIFY) {
3048 if (opc != SO_MIRROR_EXTEND) {
3050 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3054 } else if (!has_m_file) {
3056 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3063 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3064 * altered by user space tool, so we don't need to support the
3065 * --component-set for this moment. */
3066 if (comp_set && !comp_id) {
3067 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3072 if ((delete + comp_set + comp_del + comp_add) > 1) {
3074 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3079 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3080 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3082 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3087 if ((comp_set || comp_del) &&
3088 (setstripe_args_specified(&lsa) || layout != NULL)) {
3090 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3095 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3097 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3102 if (comp_add || comp_del) {
3105 result = lstat(fname, &st);
3106 if (result == 0 && S_ISDIR(st.st_mode)) {
3108 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3114 fprintf(stderr, "error: %s: can't use --component-add "
3115 "or --component-del for mirror operation\n",
3122 if (layout == NULL) {
3124 "%s %s: option -E must be specified with --component-add\n",
3129 result = adjust_first_extent(fname, layout);
3130 if (result == -ENODATA)
3132 else if (result != 0)
3136 if (from_yaml && from_copy) {
3138 "%s: can't specify --yaml and --copy together\n",
3143 if ((from_yaml || from_copy) &&
3144 (setstripe_args_specified(&lsa) || layout != NULL)) {
3145 fprintf(stderr, "error: %s: can't specify --yaml with "
3146 "-c, -S, -i, -o, -p or -E options.\n",
3151 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3153 "%s %s: options --non-block and --block are mutually exclusive\n",
3158 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3161 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3166 if (migrate_mdt_mode) {
3167 struct lmv_user_md *lmu;
3169 /* initialize migrate mdt parameters */
3170 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3171 LMV_USER_MAGIC_SPECIFIC));
3174 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3175 progname, argv[0], strerror(ENOMEM));
3179 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3180 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3181 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3183 "%s %s: migrate should specify MDT index\n",
3188 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3189 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3190 lmu->lum_hash_type = lsa.lsa_pattern;
3192 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3193 if (lsa.lsa_pool_name)
3194 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3195 sizeof(lmu->lum_pool_name));
3196 if (lsa.lsa_nr_tgts > 1) {
3199 if (lsa.lsa_stripe_count > 0 &&
3200 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3201 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3203 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3204 progname, lsa.lsa_stripe_count,
3210 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3211 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3212 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3213 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3215 lmu->lum_magic = LMV_USER_MAGIC;
3218 migrate_mdt_param.fp_lmv_md = lmu;
3219 migrate_mdt_param.fp_migrate = 1;
3220 } else if (layout == NULL) {
3221 /* initialize stripe parameters */
3222 param = calloc(1, offsetof(typeof(*param),
3223 lsp_osts[lsa.lsa_nr_tgts]));
3224 if (param == NULL) {
3226 "%s %s: cannot allocate memory for parameters: %s\n",
3227 progname, argv[0], strerror(ENOMEM));
3232 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3233 param->lsp_stripe_size = lsa.lsa_stripe_size;
3234 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3235 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3236 param->lsp_stripe_count = -1;
3238 param->lsp_stripe_count = lsa.lsa_stripe_count;
3240 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3241 param->lsp_stripe_offset = -1;
3243 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3244 param->lsp_pool = lsa.lsa_pool_name;
3245 param->lsp_is_specific = false;
3246 if (lsa.lsa_nr_tgts > 0) {
3247 if (lsa.lsa_stripe_count > 0 &&
3248 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3249 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3250 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3251 fprintf(stderr, "error: %s: stripe count %lld "
3252 "doesn't match the number of OSTs: %d\n"
3253 , argv[0], lsa.lsa_stripe_count,
3259 param->lsp_is_specific = true;
3260 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3261 memcpy(param->lsp_osts, tgts,
3262 sizeof(*tgts) * lsa.lsa_nr_tgts);
3267 /* generate a layout from a YAML template */
3268 result = lfs_comp_create_from_yaml(template, &layout,
3271 fprintf(stderr, "error: %s: can't create composite "
3272 "layout from template file %s\n",
3276 } else if (from_copy) {
3277 layout = llapi_layout_get_by_path(template, 0);
3278 if (layout == NULL) {
3280 "%s: can't create composite layout from file %s.\n",
3281 progname, template);
3286 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3287 if (migrate_mdt_mode) {
3288 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3289 } else if (migrate_mode) {
3290 result = lfs_migrate(fname, migration_flags, param,
3292 } else if (comp_set != 0) {
3293 result = lfs_component_set(fname, comp_id,
3295 lsa.lsa_comp_neg_flags);
3296 } else if (comp_del != 0) {
3297 result = lfs_component_del(fname, comp_id,
3299 lsa.lsa_comp_neg_flags);
3300 } else if (comp_add != 0) {
3301 result = lfs_component_add(fname, layout);
3302 } else if (opc == SO_MIRROR_CREATE) {
3303 result = mirror_create(fname, mirror_list);
3304 } else if (opc == SO_MIRROR_EXTEND) {
3305 result = mirror_extend(fname, mirror_list,
3307 } else if (opc == SO_MIRROR_SPLIT) {
3308 if (mirror_id == 0 && comp_id == 0) {
3310 "%s %s: no mirror id or component id is specified\n",
3315 comp_id = mirror_id;
3317 mirror_flags |= MF_COMP_ID;
3318 result = mirror_split(fname, comp_id, mirror_flags,
3319 has_m_file ? mirror_list->m_file :
3321 } else if (layout != NULL) {
3322 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3329 result = llapi_file_open_param(fname,
3338 /* Save the first error encountered. */
3346 free(migrate_mdt_param.fp_lmv_md);
3347 llapi_layout_free(layout);
3348 lfs_mirror_list_free(mirror_list);
3353 llapi_layout_free(layout);
3354 lfs_mirror_list_free(mirror_list);
3358 static int lfs_poollist(int argc, char **argv)
3363 return llapi_poollist(argv[1]);
3366 static int set_time(time_t *time, time_t *set, char *str)
3374 else if (str[0] == '-')
3380 t = strtol(str, &endptr, 0);
3381 if (*endptr != '\0') {
3383 "%s find: bad time '%s': %s\n",
3384 progname, str, strerror(EINVAL));
3387 if (*time < t * 24 * 60 * 60) {
3391 "%s find: bad time '%s': too large\n",
3396 *set = *time - t * 24 * 60 * 60;
3400 static int name2uid(unsigned int *id, const char *name)
3402 struct passwd *passwd;
3404 passwd = getpwnam(name);
3407 *id = passwd->pw_uid;
3412 static int name2gid(unsigned int *id, const char *name)
3414 struct group *group;
3416 group = getgrnam(name);
3419 *id = group->gr_gid;
3424 static inline int name2projid(unsigned int *id, const char *name)
3429 static int uid2name(char **name, unsigned int id)
3431 struct passwd *passwd;
3433 passwd = getpwuid(id);
3436 *name = passwd->pw_name;
3441 static inline int gid2name(char **name, unsigned int id)
3443 struct group *group;
3445 group = getgrgid(id);
3448 *name = group->gr_name;
3453 static int name2layout(__u32 *layout, char *name)
3455 char *ptr, *layout_name;
3458 for (ptr = name; ; ptr = NULL) {
3459 layout_name = strtok(ptr, ",");
3460 if (layout_name == NULL)
3462 if (strcmp(layout_name, "released") == 0)
3463 *layout |= LOV_PATTERN_F_RELEASED;
3464 else if (strcmp(layout_name, "raid0") == 0)
3465 *layout |= LOV_PATTERN_RAID0;
3466 else if (strcmp(layout_name, "mdt") == 0)
3467 *layout |= LOV_PATTERN_MDT;
3474 static int lfs_find(int argc, char **argv)
3479 struct find_param param = {
3483 struct option long_opts[] = {
3484 { .val = 'A', .name = "atime", .has_arg = required_argument },
3485 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3486 { .val = LFS_COMP_COUNT_OPT,
3487 .name = "comp-count", .has_arg = required_argument },
3488 { .val = LFS_COMP_COUNT_OPT,
3489 .name = "component-count",
3490 .has_arg = required_argument },
3491 { .val = LFS_COMP_FLAGS_OPT,
3492 .name = "comp-flags", .has_arg = required_argument },
3493 { .val = LFS_COMP_FLAGS_OPT,
3494 .name = "component-flags",
3495 .has_arg = required_argument },
3496 { .val = LFS_COMP_START_OPT,
3497 .name = "comp-start", .has_arg = required_argument },
3498 { .val = LFS_COMP_START_OPT,
3499 .name = "component-start",
3500 .has_arg = required_argument },
3501 { .val = LFS_MIRROR_STATE_OPT,
3502 .name = "mirror-state", .has_arg = required_argument },
3503 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3504 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3505 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3506 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3507 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3508 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3509 { .val = 'E', .name = "component-end",
3510 .has_arg = required_argument },
3511 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3512 { .val = 'g', .name = "gid", .has_arg = required_argument },
3513 { .val = 'G', .name = "group", .has_arg = required_argument },
3514 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3515 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3516 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3517 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3518 { .val = 'L', .name = "layout", .has_arg = required_argument },
3519 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3520 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3521 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3522 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3523 { .val = 'n', .name = "name", .has_arg = required_argument },
3524 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3525 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3526 { .val = 'O', .name = "obd", .has_arg = required_argument },
3527 { .val = 'O', .name = "ost", .has_arg = required_argument },
3528 /* no short option for pool yet, can be 'p' after 2.18 */
3529 { .val = LFS_POOL_OPT,
3530 .name = "pool", .has_arg = required_argument },
3531 { .val = '0', .name = "print0", .has_arg = no_argument },
3532 { .val = 'P', .name = "print", .has_arg = no_argument },
3533 { .val = LFS_PROJID_OPT,
3534 .name = "projid", .has_arg = required_argument },
3535 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3536 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3537 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3538 { .val = 's', .name = "size", .has_arg = required_argument },
3539 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3540 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3541 { .val = 't', .name = "type", .has_arg = required_argument },
3542 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3543 { .val = 'u', .name = "uid", .has_arg = required_argument },
3544 { .val = 'U', .name = "user", .has_arg = required_argument },
3545 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3546 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3558 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3559 while ((c = getopt_long_only(argc, argv,
3560 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3561 long_opts, NULL)) >= 0) {
3566 /* '!' is part of option */
3567 /* when getopt_long_only() finds a string which is not
3568 * an option nor a known option argument it returns 1
3569 * in that case if we already have found pathstart and pathend
3570 * (i.e. we have the list of pathnames),
3571 * the only supported value is "!"
3573 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3574 if (!isoption && pathend != -1) {
3575 fprintf(stderr, "err: %s: filename|dirname must either "
3576 "precede options or follow options\n",
3581 if (!isoption && pathstart == -1)
3582 pathstart = optind - 1;
3583 if (isoption && pathstart != -1 && pathend == -1)
3584 pathend = optind - 2;
3590 /* unknown; opt is "!" or path component,
3591 * checking done above.
3593 if (strcmp(optarg, "!") == 0)
3597 xtime = ¶m.fp_atime;
3598 xsign = ¶m.fp_asign;
3599 param.fp_exclude_atime = !!neg_opt;
3600 /* no break, this falls through to 'C' for ctime */
3603 xtime = ¶m.fp_ctime;
3604 xsign = ¶m.fp_csign;
3605 param.fp_exclude_ctime = !!neg_opt;
3607 /* no break, this falls through to 'M' for mtime */
3610 xtime = ¶m.fp_mtime;
3611 xsign = ¶m.fp_msign;
3612 param.fp_exclude_mtime = !!neg_opt;
3614 rc = set_time(&t, xtime, optarg);
3615 if (rc == INT_MAX) {
3623 if (optarg[0] == '+') {
3624 param.fp_blocks_sign = -1;
3626 } else if (optarg[0] == '-') {
3627 param.fp_blocks_sign = 1;
3631 param.fp_blocks_units = 1024;
3632 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3633 ¶m.fp_blocks_units, 0);
3635 fprintf(stderr, "error: bad blocks '%s'\n",
3639 param.fp_check_blocks = 1;
3640 param.fp_exclude_blocks = !!neg_opt;
3642 case LFS_COMP_COUNT_OPT:
3643 if (optarg[0] == '+') {
3644 param.fp_comp_count_sign = -1;
3646 } else if (optarg[0] == '-') {
3647 param.fp_comp_count_sign = 1;
3651 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3652 if (*endptr != '\0') {
3653 fprintf(stderr, "error: bad component count "
3657 param.fp_check_comp_count = 1;
3658 param.fp_exclude_comp_count = !!neg_opt;
3660 case LFS_COMP_FLAGS_OPT:
3661 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3662 ¶m.fp_comp_neg_flags);
3664 fprintf(stderr, "error: bad component flags "
3668 param.fp_check_comp_flags = 1;
3670 __u32 flags = param.fp_comp_neg_flags;
3671 param.fp_comp_neg_flags = param.fp_comp_flags;
3672 param.fp_comp_flags = flags;
3675 case LFS_COMP_START_OPT:
3676 if (optarg[0] == '+') {
3677 param.fp_comp_start_sign = -1;
3679 } else if (optarg[0] == '-') {
3680 param.fp_comp_start_sign = 1;
3684 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3685 ¶m.fp_comp_start_units, 0);
3687 fprintf(stderr, "error: bad component start "
3691 param.fp_check_comp_start = 1;
3692 param.fp_exclude_comp_start = !!neg_opt;
3694 case LFS_MIRROR_STATE_OPT:
3695 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3696 ¶m.fp_mirror_neg_state);
3699 "error: bad mirrored file state '%s'\n",
3703 param.fp_check_mirror_state = 1;
3705 __u16 state = param.fp_mirror_neg_state;
3706 param.fp_mirror_neg_state =
3707 param.fp_mirror_state;
3708 param.fp_mirror_state = state;
3712 if (optarg[0] == '+') {
3713 param.fp_stripe_count_sign = -1;
3715 } else if (optarg[0] == '-') {
3716 param.fp_stripe_count_sign = 1;
3720 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3721 if (*endptr != '\0') {
3722 fprintf(stderr,"error: bad stripe_count '%s'\n",
3727 param.fp_check_stripe_count = 1;
3728 param.fp_exclude_stripe_count = !!neg_opt;
3731 param.fp_max_depth = strtol(optarg, 0, 0);
3734 if (optarg[0] == '+') {
3735 param.fp_comp_end_sign = -1;
3737 } else if (optarg[0] == '-') {
3738 param.fp_comp_end_sign = 1;
3742 if (arg_is_eof(optarg)) {
3743 param.fp_comp_end = LUSTRE_EOF;
3744 param.fp_comp_end_units = 1;
3747 rc = llapi_parse_size(optarg,
3749 ¶m.fp_comp_end_units, 0);
3752 fprintf(stderr, "error: bad component end "
3756 param.fp_check_comp_end = 1;
3757 param.fp_exclude_comp_end = !!neg_opt;
3761 rc = name2gid(¶m.fp_gid, optarg);
3763 param.fp_gid = strtoul(optarg, &endptr, 10);
3764 if (*endptr != '\0') {
3765 fprintf(stderr, "Group/GID: %s cannot "
3766 "be found.\n", optarg);
3771 param.fp_exclude_gid = !!neg_opt;
3772 param.fp_check_gid = 1;
3775 param.fp_hash_type = check_hashtype(optarg);
3776 if (param.fp_hash_type == 0) {
3777 fprintf(stderr, "error: bad hash_type '%s'\n",
3782 param.fp_check_hash_type = 1;
3783 param.fp_exclude_hash_type = !!neg_opt;
3786 ret = name2layout(¶m.fp_layout, optarg);
3789 param.fp_exclude_layout = !!neg_opt;
3790 param.fp_check_layout = 1;
3794 rc = name2uid(¶m.fp_uid, optarg);
3796 param.fp_uid = strtoul(optarg, &endptr, 10);
3797 if (*endptr != '\0') {
3798 fprintf(stderr, "User/UID: %s cannot "
3799 "be found.\n", optarg);
3804 param.fp_exclude_uid = !!neg_opt;
3805 param.fp_check_uid = 1;
3808 param.fp_pattern = (char *)optarg;
3809 param.fp_exclude_pattern = !!neg_opt;
3812 if (optarg[0] == '+') {
3813 param.fp_mirror_count_sign = -1;
3815 } else if (optarg[0] == '-') {
3816 param.fp_mirror_count_sign = 1;
3820 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3821 if (*endptr != '\0') {
3823 "error: bad mirror count '%s'\n",
3827 param.fp_check_mirror_count = 1;
3828 param.fp_exclude_mirror_count = !!neg_opt;
3833 char *buf, *token, *next, *p;
3837 buf = strdup(optarg);
3843 param.fp_exclude_obd = !!neg_opt;
3846 while (token && *token) {
3847 token = strchr(token, ',');
3854 param.fp_exclude_mdt = !!neg_opt;
3855 param.fp_num_alloc_mdts += len;
3856 tmp = realloc(param.fp_mdt_uuid,
3857 param.fp_num_alloc_mdts *
3858 sizeof(*param.fp_mdt_uuid));
3864 param.fp_mdt_uuid = tmp;
3866 param.fp_exclude_obd = !!neg_opt;
3867 param.fp_num_alloc_obds += len;
3868 tmp = realloc(param.fp_obd_uuid,
3869 param.fp_num_alloc_obds *
3870 sizeof(*param.fp_obd_uuid));
3876 param.fp_obd_uuid = tmp;
3878 for (token = buf; token && *token; token = next) {
3879 struct obd_uuid *puuid;
3882 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3885 ¶m.fp_obd_uuid[param.fp_num_obds++];
3887 p = strchr(token, ',');
3894 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3899 strncpy(puuid->uuid, token,
3900 sizeof(puuid->uuid));
3907 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3911 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3913 "Pool name %s is too long (max %d)\n",
3914 optarg, LOV_MAXPOOLNAME);
3919 * We do check for empty pool because empty pool
3920 * is used to find V1 LOV attributes
3922 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3923 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3924 param.fp_exclude_pool = !!neg_opt;
3925 param.fp_check_pool = 1;
3927 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3928 case 'p': /* want this for --pool, to match getstripe/find */
3930 "warning: -p deprecated, use --print0 or -0\n");
3933 param.fp_zero_end = 1;
3935 case 'P': /* we always print, this option is a no-op */
3937 case LFS_PROJID_OPT:
3938 rc = name2projid(¶m.fp_projid, optarg);
3940 param.fp_projid = strtoul(optarg, &endptr, 10);
3941 if (*endptr != '\0') {
3943 "Invalid project ID: %s",
3949 param.fp_exclude_projid = !!neg_opt;
3950 param.fp_check_projid = 1;
3953 if (optarg[0] == '+') {
3954 param.fp_size_sign = -1;
3956 } else if (optarg[0] == '-') {
3957 param.fp_size_sign = 1;
3961 ret = llapi_parse_size(optarg, ¶m.fp_size,
3962 ¶m.fp_size_units, 0);
3964 fprintf(stderr, "error: bad file size '%s'\n",
3968 param.fp_check_size = 1;
3969 param.fp_exclude_size = !!neg_opt;
3972 if (optarg[0] == '+') {
3973 param.fp_stripe_size_sign = -1;
3975 } else if (optarg[0] == '-') {
3976 param.fp_stripe_size_sign = 1;
3980 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3981 ¶m.fp_stripe_size_units, 0);
3983 fprintf(stderr, "error: bad stripe_size '%s'\n",
3987 param.fp_check_stripe_size = 1;
3988 param.fp_exclude_stripe_size = !!neg_opt;
3991 param.fp_exclude_type = !!neg_opt;
3992 switch (optarg[0]) {
3994 param.fp_type = S_IFBLK;
3997 param.fp_type = S_IFCHR;
4000 param.fp_type = S_IFDIR;
4003 param.fp_type = S_IFREG;
4006 param.fp_type = S_IFLNK;
4009 param.fp_type = S_IFIFO;
4012 param.fp_type = S_IFSOCK;
4015 fprintf(stderr, "error: %s: bad type '%s'\n",
4022 if (optarg[0] == '+') {
4023 param.fp_mdt_count_sign = -1;
4025 } else if (optarg[0] == '-') {
4026 param.fp_mdt_count_sign = 1;
4030 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4031 if (*endptr != '\0') {
4032 fprintf(stderr, "error: bad mdt_count '%s'\n",
4037 param.fp_check_mdt_count = 1;
4038 param.fp_exclude_mdt_count = !!neg_opt;
4046 if (pathstart == -1) {
4047 fprintf(stderr, "error: %s: no filename|pathname\n",
4051 } else if (pathend == -1) {
4057 rc = llapi_find(argv[pathstart], ¶m);
4058 if (rc != 0 && ret == 0)
4060 } while (++pathstart < pathend);
4063 fprintf(stderr, "error: %s failed for %s.\n",
4064 argv[0], argv[optind - 1]);
4066 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4067 free(param.fp_obd_uuid);
4069 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4070 free(param.fp_mdt_uuid);
4075 static int lfs_getstripe_internal(int argc, char **argv,
4076 struct find_param *param)
4078 struct option long_opts[] = {
4079 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4080 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4081 { .val = LFS_COMP_COUNT_OPT,
4082 .name = "comp-count", .has_arg = no_argument },
4083 { .val = LFS_COMP_COUNT_OPT,
4084 .name = "component-count", .has_arg = no_argument },
4085 { .val = LFS_COMP_FLAGS_OPT,
4086 .name = "comp-flags", .has_arg = optional_argument },
4087 { .val = LFS_COMP_FLAGS_OPT,
4088 .name = "component-flags", .has_arg = optional_argument },
4089 { .val = LFS_COMP_START_OPT,
4090 .name = "comp-start", .has_arg = optional_argument },
4091 { .val = LFS_COMP_START_OPT,
4092 .name = "component-start", .has_arg = optional_argument },
4093 { .val = LFS_MIRROR_INDEX_OPT,
4094 .name = "mirror-index", .has_arg = required_argument },
4095 { .val = LFS_MIRROR_ID_OPT,
4096 .name = "mirror-id", .has_arg = required_argument },
4097 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4098 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4099 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4100 { .val = 'd', .name = "directory", .has_arg = no_argument },
4101 { .val = 'D', .name = "default", .has_arg = no_argument },
4102 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4103 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4104 { .val = 'F', .name = "fid", .has_arg = no_argument },
4105 { .val = 'g', .name = "generation", .has_arg = no_argument },
4106 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4107 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4108 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4109 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4110 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4111 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4112 { .val = 'L', .name = "layout", .has_arg = no_argument },
4113 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4114 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4115 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4116 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4117 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4118 { .val = 'O', .name = "obd", .has_arg = required_argument },
4119 { .val = 'O', .name = "ost", .has_arg = required_argument },
4120 { .val = 'p', .name = "pool", .has_arg = no_argument },
4121 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4122 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4123 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4124 { .val = 'R', .name = "raw", .has_arg = no_argument },
4125 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4126 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4127 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4128 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4129 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4130 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4131 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4132 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4136 int pathstart = -1, pathend = -1;
4140 while ((c = getopt_long(argc, argv,
4141 "-cdDE::FghiI::LmMoO:pqrRsSvy",
4142 long_opts, NULL)) != -1) {
4146 /* '!' is part of option */
4147 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4148 if (!isoption && pathend != -1) {
4150 "error: %s: filename|dirname must either precede options or follow options\n",
4154 if (!isoption && pathstart == -1)
4155 pathstart = optind - 1;
4156 if (isoption && pathstart != -1 && pathend == -1)
4157 pathend = optind - 2;
4161 /* unknown: opt is "!" */
4162 if (strcmp(optarg, "!") == 0)
4166 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4167 param->fp_verbose |= VERBOSE_COUNT;
4168 param->fp_max_depth = 0;
4171 case LFS_COMP_COUNT_OPT:
4172 param->fp_verbose |= VERBOSE_COMP_COUNT;
4173 param->fp_max_depth = 0;
4175 case LFS_COMP_FLAGS_OPT:
4176 if (optarg != NULL) {
4177 rc = comp_str2flags(optarg,
4178 ¶m->fp_comp_flags,
4179 ¶m->fp_comp_neg_flags);
4181 fprintf(stderr, "error: %s bad "
4182 "component flags '%s'.\n",
4186 param->fp_check_comp_flags = 1;
4188 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4189 param->fp_max_depth = 0;
4192 case LFS_COMP_START_OPT:
4193 if (optarg != NULL) {
4195 if (tmp[0] == '+') {
4196 param->fp_comp_start_sign = -1;
4198 } else if (tmp[0] == '-') {
4199 param->fp_comp_start_sign = 1;
4202 rc = llapi_parse_size(tmp,
4203 ¶m->fp_comp_start,
4204 ¶m->fp_comp_start_units, 0);
4206 fprintf(stderr, "error: %s bad "
4207 "component start '%s'.\n",
4211 param->fp_check_comp_start = 1;
4214 param->fp_verbose |= VERBOSE_COMP_START;
4215 param->fp_max_depth = 0;
4218 case LFS_MIRROR_INDEX_OPT:
4219 if (optarg[0] == '+') {
4220 param->fp_mirror_index_sign = -1;
4222 } else if (optarg[0] == '-') {
4223 param->fp_mirror_index_sign = 1;
4227 param->fp_mirror_index = strtoul(optarg, &end, 0);
4228 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4229 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4231 "%s %s: invalid mirror index '%s'\n",
4232 progname, argv[0], optarg);
4235 if (param->fp_mirror_id != 0) {
4237 "%s %s: can't specify both mirror index and mirror ID\n",
4241 param->fp_check_mirror_index = 1;
4242 param->fp_exclude_mirror_index = !!neg_opt;
4244 case LFS_MIRROR_ID_OPT:
4245 if (optarg[0] == '+') {
4246 param->fp_mirror_id_sign = -1;
4248 } else if (optarg[0] == '-') {
4249 param->fp_mirror_id_sign = 1;
4253 param->fp_mirror_id = strtoul(optarg, &end, 0);
4254 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4255 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4257 "%s %s: invalid mirror ID '%s'\n",
4258 progname, argv[0], optarg);
4261 if (param->fp_mirror_index != 0) {
4263 "%s %s: can't specify both mirror index and mirror ID\n",
4267 param->fp_check_mirror_id = 1;
4268 param->fp_exclude_mirror_id = !!neg_opt;
4271 param->fp_max_depth = 0;
4274 param->fp_get_default_lmv = 1;
4277 if (optarg != NULL) {
4279 if (tmp[0] == '+') {
4280 param->fp_comp_end_sign = -1;
4282 } else if (tmp[0] == '-') {
4283 param->fp_comp_end_sign = 1;
4287 if (arg_is_eof(tmp)) {
4288 param->fp_comp_end = LUSTRE_EOF;
4289 param->fp_comp_end_units = 1;
4292 rc = llapi_parse_size(tmp,
4293 ¶m->fp_comp_end,
4294 ¶m->fp_comp_end_units, 0);
4297 fprintf(stderr, "error: %s bad "
4298 "component end '%s'.\n",
4302 param->fp_check_comp_end = 1;
4304 param->fp_verbose |= VERBOSE_COMP_END;
4305 param->fp_max_depth = 0;
4309 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4310 param->fp_verbose |= VERBOSE_DFID;
4311 param->fp_max_depth = 0;
4315 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4316 param->fp_verbose |= VERBOSE_GENERATION;
4317 param->fp_max_depth = 0;
4321 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4322 param->fp_verbose |= VERBOSE_OFFSET;
4323 param->fp_max_depth = 0;
4327 if (optarg != NULL) {
4328 param->fp_comp_id = strtoul(optarg, &end, 0);
4329 if (*end != '\0' || param->fp_comp_id == 0 ||
4330 param->fp_comp_id > LCME_ID_MAX) {
4331 fprintf(stderr, "error: %s bad "
4332 "component id '%s'\n",
4336 param->fp_check_comp_id = 1;
4339 param->fp_max_depth = 0;
4340 param->fp_verbose |= VERBOSE_COMP_ID;
4344 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4345 param->fp_verbose |= VERBOSE_LAYOUT;
4346 param->fp_max_depth = 0;
4349 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4351 fprintf(stderr, "warning: '-M' deprecated"
4352 ", use '--mdt-index' or '-m' instead\n");
4355 if (!(param->fp_verbose & VERBOSE_DETAIL))
4356 param->fp_max_depth = 0;
4357 param->fp_verbose |= VERBOSE_MDTINDEX;
4360 if (param->fp_obd_uuid) {
4362 "error: %s: only one obduuid allowed",
4366 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4369 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4370 param->fp_verbose |= VERBOSE_POOL;
4371 param->fp_max_depth = 0;
4378 param->fp_recursive = 1;
4384 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4385 param->fp_verbose |= VERBOSE_SIZE;
4386 param->fp_max_depth = 0;
4390 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4400 if (pathstart == -1) {
4401 fprintf(stderr, "error: %s: no filename|pathname\n",
4404 } else if (pathend == -1) {
4412 if (param->fp_recursive)
4413 param->fp_max_depth = -1;
4414 else if (param->fp_verbose & VERBOSE_DETAIL)
4415 param->fp_max_depth = 1;
4417 if (!param->fp_verbose)
4418 param->fp_verbose = VERBOSE_DEFAULT;
4419 if (param->fp_quiet)
4420 param->fp_verbose = VERBOSE_OBJID;
4423 rc = llapi_getstripe(argv[pathstart], param);
4424 } while (++pathstart < pathend && !rc);
4427 fprintf(stderr, "error: %s failed for %s.\n",
4428 argv[0], argv[optind - 1]);
4432 static int lfs_tgts(int argc, char **argv)
4434 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4435 struct find_param param;
4436 int index = 0, rc=0;
4441 if (argc == 2 && !realpath(argv[1], path)) {
4443 fprintf(stderr, "error: invalid path '%s': %s\n",
4444 argv[1], strerror(-rc));
4448 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4449 /* Check if we have a mount point */
4450 if (mntdir[0] == '\0')
4453 memset(¶m, 0, sizeof(param));
4454 if (!strcmp(argv[0], "mdts"))
4455 param.fp_get_lmv = 1;
4457 rc = llapi_ostlist(mntdir, ¶m);
4459 fprintf(stderr, "error: %s: failed on %s\n",
4462 if (path[0] != '\0')
4464 memset(mntdir, 0, PATH_MAX);
4470 static int lfs_getstripe(int argc, char **argv)
4472 struct find_param param = { 0 };
4474 param.fp_max_depth = 1;
4475 return lfs_getstripe_internal(argc, argv, ¶m);
4479 static int lfs_getdirstripe(int argc, char **argv)
4481 struct find_param param = { 0 };
4482 struct option long_opts[] = {
4483 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4484 { .val = 'D', .name = "default", .has_arg = no_argument },
4485 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4486 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4487 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4488 { .val = 'O', .name = "obd", .has_arg = required_argument },
4489 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4490 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4491 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4495 param.fp_get_lmv = 1;
4497 while ((c = getopt_long(argc, argv,
4498 "cDHimO:rtTy", long_opts, NULL)) != -1)
4503 param.fp_verbose |= VERBOSE_COUNT;
4506 param.fp_get_default_lmv = 1;
4510 param.fp_verbose |= VERBOSE_OFFSET;
4512 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4514 fprintf(stderr, "warning: '-t' deprecated, "
4515 "use '--mdt-hash' or '-H' instead\n");
4518 param.fp_verbose |= VERBOSE_HASH_TYPE;
4521 if (param.fp_obd_uuid) {
4523 "error: %s: only one obduuid allowed",
4527 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4530 param.fp_recursive = 1;
4543 if (param.fp_recursive)
4544 param.fp_max_depth = -1;
4546 if (!param.fp_verbose)
4547 param.fp_verbose = VERBOSE_DEFAULT;
4550 rc = llapi_getstripe(argv[optind], ¶m);
4551 } while (++optind < argc && !rc);
4554 fprintf(stderr, "error: %s failed for %s.\n",
4555 argv[0], argv[optind - 1]);
4560 MNTDF_INODES = 0x0001,
4561 MNTDF_COOKED = 0x0002,
4562 MNTDF_LAZY = 0x0004,
4563 MNTDF_VERBOSE = 0x0008,
4564 MNTDF_SHOW = 0x0010,
4567 #define COOK(value) \
4570 while (value > 1024) { \
4578 #define CDF "%11llu"
4579 #define HDF "%8.1f%c"
4583 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4585 double avail, used, ratio = 0;
4587 avail = st->os_bavail;
4588 used = st->os_blocks - st->os_bfree;
4589 if (avail + used > 0)
4590 ratio = used / (used + avail) * 100 + 0.5;
4595 static int showdf(char *mntdir, struct obd_statfs *stat,
4596 char *uuid, enum mntdf_flags flags,
4597 char *type, int index, int rc)
4599 long long avail, used, total;
4601 char *suffix = "KMGTPEZY";
4602 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4603 char tbuf[3 * sizeof(__u64)];
4604 char ubuf[3 * sizeof(__u64)];
4605 char abuf[3 * sizeof(__u64)];
4606 char rbuf[3 * sizeof(__u64)];
4613 if (flags & MNTDF_INODES) {
4614 avail = stat->os_ffree;
4615 used = stat->os_files - stat->os_ffree;
4616 total = stat->os_files;
4618 int shift = flags & MNTDF_COOKED ? 0 : 10;
4620 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4621 used = ((stat->os_blocks - stat->os_bfree) *
4622 stat->os_bsize) >> shift;
4623 total = (stat->os_blocks * stat->os_bsize) >> shift;
4626 ratio = obd_statfs_ratio(stat);
4628 if (flags & MNTDF_COOKED) {
4632 cook_val = (double)total;
4635 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4638 snprintf(tbuf, sizeof(tbuf), CDF, total);
4640 cook_val = (double)used;
4643 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4646 snprintf(ubuf, sizeof(ubuf), CDF, used);
4648 cook_val = (double)avail;
4651 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4654 snprintf(abuf, sizeof(abuf), CDF, avail);
4656 snprintf(tbuf, sizeof(tbuf), CDF, total);
4657 snprintf(ubuf, sizeof(tbuf), CDF, used);
4658 snprintf(abuf, sizeof(tbuf), CDF, avail);
4661 sprintf(rbuf, RDF, ratio);
4662 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4663 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4665 printf("[%s:%d]", type, index);
4667 if (stat->os_state) {
4669 * Each character represents the matching
4672 const char state_names[] = "DRSI";
4677 for (i = 0, state = stat->os_state;
4678 state && i < sizeof(state_names); i++) {
4679 if (!(state & (1 << i)))
4681 printf("%c", state_names[i]);
4689 printf(UUF": inactive device\n", uuid);
4692 printf(UUF": %s\n", uuid, strerror(-rc));
4699 struct ll_stat_type {
4704 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4706 struct ll_statfs_data {
4708 struct obd_statfs sd_st;
4711 struct ll_statfs_buf {
4713 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4716 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4717 int ops, struct ll_statfs_buf *lsb)
4719 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4720 struct obd_uuid uuid_buf;
4721 char *poolname = NULL;
4722 struct ll_stat_type types[] = {
4723 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4724 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4725 { .st_name = NULL } };
4726 struct ll_stat_type *tp;
4727 __u64 ost_ffree = 0;
4735 poolname = strchr(pool, '.');
4736 if (poolname != NULL) {
4737 if (strncmp(fsname, pool, strlen(fsname))) {
4738 fprintf(stderr, "filesystem name incorrect\n");
4746 fd = open(mntdir, O_RDONLY);
4749 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4754 if (flags & MNTDF_SHOW) {
4755 if (flags & MNTDF_INODES)
4756 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4757 "UUID", "Inodes", "IUsed", "IFree",
4758 "IUse%", "Mounted on");
4760 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4762 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4763 "Used", "Available", "Use%", "Mounted on");
4766 for (tp = types; tp->st_name != NULL; tp++) {
4767 if (!(tp->st_op & ops))
4770 for (index = 0; ; index++) {
4771 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4772 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4773 type = flags & MNTDF_LAZY ?
4774 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4775 rc2 = llapi_obd_fstatfs(fd, type, index,
4776 &stat_buf, &uuid_buf);
4781 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4782 if (!(flags & MNTDF_VERBOSE))
4784 } else if (rc2 < 0 && rc == 0) {
4788 if (poolname && tp->st_op == LL_STATFS_LOV &&
4789 llapi_search_ost(fsname, poolname,
4790 obd_uuid2str(&uuid_buf)) != 1)
4793 /* the llapi_obd_statfs() call may have returned with
4794 * an error, but if it filled in uuid_buf we will at
4795 * lease use that to print out a message for that OBD.
4796 * If we didn't get anything in the uuid_buf, then fill
4797 * it in so that we can print an error message. */
4798 if (uuid_buf.uuid[0] == '\0')
4799 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4800 "%s%04x", tp->st_name, index);
4802 lsb->sb_buf[lsb->sb_count].sd_index = index;
4803 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4806 if (flags & MNTDF_SHOW)
4807 showdf(mntdir, &stat_buf,
4808 obd_uuid2str(&uuid_buf), flags,
4809 tp->st_name, index, rc2);
4812 if (tp->st_op == LL_STATFS_LMV) {
4813 sum.os_ffree += stat_buf.os_ffree;
4814 sum.os_files += stat_buf.os_files;
4815 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4816 sum.os_blocks += stat_buf.os_blocks *
4818 sum.os_bfree += stat_buf.os_bfree *
4820 sum.os_bavail += stat_buf.os_bavail *
4822 ost_ffree += stat_buf.os_ffree;
4830 /* If we don't have as many objects free on the OST as inodes
4831 * on the MDS, we reduce the total number of inodes to
4832 * compensate, so that the "inodes in use" number is correct.
4833 * Matches ll_statfs_internal() so the results are consistent. */
4834 if (ost_ffree < sum.os_ffree) {
4835 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4836 sum.os_ffree = ost_ffree;
4838 if (flags & MNTDF_SHOW) {
4840 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4847 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4849 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4851 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4853 int r1 = obd_statfs_ratio(st1);
4854 int r2 = obd_statfs_ratio(st2);
4855 int64_t result = r1 - r2;
4857 /* if both space usage are above 90, compare free inodes */
4858 if (r1 > 90 && r2 > 90)
4859 result = st2->os_ffree - st1->os_ffree;
4863 else if (result == 0)
4870 static int lfs_setdirstripe(int argc, char **argv)
4874 struct lfs_setstripe_args lsa = { 0 };
4875 struct llapi_stripe_param *param = NULL;
4876 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4879 char *mode_opt = NULL;
4880 bool default_stripe = false;
4881 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4882 mode_t previous_mode = 0;
4883 bool delete = false;
4884 struct ll_statfs_buf *lsb = NULL;
4885 char mntdir[PATH_MAX] = "";
4886 bool auto_distributed = false;
4888 struct option long_opts[] = {
4889 { .val = 'c', .name = "count", .has_arg = required_argument },
4890 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4891 { .val = 'd', .name = "delete", .has_arg = no_argument },
4892 { .val = 'D', .name = "default", .has_arg = no_argument },
4893 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4894 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4895 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4896 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4897 { .val = 'i', .name = "index", .has_arg = required_argument },
4899 { .val = 'o', .name = "mode", .has_arg = required_argument },
4900 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4901 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4903 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4904 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4907 setstripe_args_init(&lsa);
4909 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4917 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4920 "%s %s: invalid stripe count '%s'\n",
4921 progname, argv[0], optarg);
4927 default_stripe = true;
4930 default_stripe = true;
4932 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4934 fprintf(stderr, "warning: '--hash-type' and '-t' "
4935 "deprecated, use '--mdt-hash' or '-H' instead\n");
4938 lsa.lsa_pattern = check_hashtype(optarg);
4939 if (lsa.lsa_pattern == 0) {
4941 "%s %s: bad stripe hash type '%s'\n",
4942 progname, argv[0], optarg);
4947 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4948 if (strcmp(argv[optind - 1], "--index") == 0)
4950 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4953 lsa.lsa_nr_tgts = parse_targets(mdts,
4954 sizeof(mdts) / sizeof(__u32),
4955 lsa.lsa_nr_tgts, optarg);
4956 if (lsa.lsa_nr_tgts < 0) {
4958 "%s %s: invalid MDT target(s) '%s'\n",
4959 progname, argv[0], optarg);
4963 lsa.lsa_tgts = mdts;
4964 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4965 lsa.lsa_stripe_off = mdts[0];
4967 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4969 fprintf(stderr, "warning: '-m' is deprecated, "
4970 "use '--mode' or '-o' instead\n");
4976 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4977 progname, argv[0], argv[optind - 1]);
4982 if (optind == argc) {
4983 fprintf(stderr, "%s %s: DIR must be specified\n",
4988 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
4989 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
4991 "%s %s: stripe offset and count must be specified\n",
4997 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
4998 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5000 "%s %s: cannot specify -d with -c or -i options\n",
5005 if (mode_opt != NULL) {
5006 mode = strtoul(mode_opt, &end, 8);
5009 "%s %s: bad MODE '%s'\n",
5010 progname, argv[0], mode_opt);
5013 previous_mode = umask(0);
5017 * initialize stripe parameters, in case param is converted to specific,
5018 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5020 param = calloc(1, offsetof(typeof(*param),
5021 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5022 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5023 if (param == NULL) {
5025 "%s %s: cannot allocate memory for parameters: %s\n",
5026 progname, argv[0], strerror(ENOMEM));
5030 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5031 param->lsp_stripe_count = lsa.lsa_stripe_count;
5032 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5033 param->lsp_stripe_offset = -1;
5035 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5036 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5037 param->lsp_stripe_pattern = lsa.lsa_pattern;
5039 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5040 param->lsp_pool = lsa.lsa_pool_name;
5041 param->lsp_is_specific = false;
5042 if (lsa.lsa_nr_tgts > 1) {
5043 if (lsa.lsa_stripe_count > 0 &&
5044 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5045 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5046 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5047 "match the number of MDTs: %d\n",
5048 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5053 param->lsp_is_specific = true;
5054 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5055 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5058 dname = argv[optind];
5060 if (default_stripe) {
5061 result = llapi_dir_set_default_lmv(dname, param);
5063 /* if current \a dname isn't under the same \a mntdir
5064 * as the last one, and the last one was
5065 * auto-distributed, restore \a param.
5067 if (mntdir[0] != '\0' &&
5068 strncmp(dname, mntdir, strlen(mntdir)) &&
5070 param->lsp_is_specific = false;
5071 param->lsp_stripe_offset = -1;
5072 auto_distributed = false;
5075 if (!param->lsp_is_specific &&
5076 param->lsp_stripe_offset == -1) {
5077 char path[PATH_MAX] = "";
5080 lsb = malloc(sizeof(*lsb));
5088 /* use mntdir for dirname() temporarily */
5089 strncpy(mntdir, dname, sizeof(mntdir));
5090 if (!realpath(dirname(mntdir), path)) {
5093 "error: invalid path '%s': %s\n",
5094 argv[optind], strerror(errno));
5099 result = llapi_search_mounts(path, 0, mntdir,
5101 if (result < 0 || mntdir[0] == '\0') {
5103 "No suitable Lustre mount found\n");
5107 result = mntdf(mntdir, NULL, NULL, 0,
5108 LL_STATFS_LMV, lsb);
5112 if (param->lsp_stripe_count > lsb->sb_count) {
5114 "error: stripe count %d is too big\n",
5115 param->lsp_stripe_count);
5120 qsort(lsb->sb_buf, lsb->sb_count,
5121 sizeof(struct ll_statfs_data),
5122 ll_statfs_data_comp);
5124 auto_distributed = true;
5127 if (auto_distributed) {
5129 int nr = MAX(param->lsp_stripe_count,
5132 /* don't use server whose usage is above 90% */
5133 while (nr != param->lsp_stripe_count &&
5134 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
5136 nr = MAX(param->lsp_stripe_count,
5139 /* get \a r between [0, nr) */
5142 param->lsp_stripe_offset =
5143 lsb->sb_buf[r].sd_index;
5144 if (param->lsp_stripe_count > 1) {
5147 param->lsp_is_specific = true;
5148 for (; i < param->lsp_stripe_count; i++)
5149 param->lsp_tgts[(i + r) % nr] =
5150 lsb->sb_buf[i].sd_index;
5154 result = llapi_dir_create(dname, mode, param);
5159 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5160 progname, dname, strerror(-result));
5163 dname = argv[++optind];
5164 } while (dname != NULL);
5166 if (mode_opt != NULL)
5167 umask(previous_mode);
5175 static int lfs_rmentry(int argc, char **argv)
5182 fprintf(stderr, "error: %s: missing dirname\n",
5188 dname = argv[index];
5189 while (dname != NULL) {
5190 result = llapi_direntry_remove(dname);
5192 fprintf(stderr, "error: %s: remove dir entry '%s' "
5193 "failed\n", argv[0], dname);
5196 dname = argv[++index];
5201 static int lfs_mv(int argc, char **argv)
5203 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5204 struct find_param param = {
5211 struct option long_opts[] = {
5212 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5213 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5216 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5218 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5220 fprintf(stderr, "warning: '-M' deprecated"
5221 ", use '--mdt-index' or '-m' instead\n");
5224 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5226 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5232 param.fp_verbose = VERBOSE_DETAIL;
5235 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5236 progname, argv[optind - 1]);
5241 if (lmu.lum_stripe_offset == -1) {
5242 fprintf(stderr, "%s mv: MDT index must be specified\n",
5247 if (optind >= argc) {
5248 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5253 /* initialize migrate mdt parameters */
5254 param.fp_lmv_md = &lmu;
5255 param.fp_migrate = 1;
5256 rc = llapi_migrate_mdt(argv[optind], ¶m);
5258 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5259 progname, argv[optind], param.fp_mdt_index,
5264 static int lfs_osts(int argc, char **argv)
5266 return lfs_tgts(argc, argv);
5269 static int lfs_mdts(int argc, char **argv)
5271 return lfs_tgts(argc, argv);
5274 static int lfs_df(int argc, char **argv)
5276 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5277 enum mntdf_flags flags = MNTDF_SHOW;
5278 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5279 int c, rc = 0, index = 0;
5280 char fsname[PATH_MAX] = "", *pool_name = NULL;
5281 struct option long_opts[] = {
5282 { .val = 'h', .name = "human-readable",
5283 .has_arg = no_argument },
5284 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5285 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5286 { .val = 'p', .name = "pool", .has_arg = required_argument },
5287 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5290 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5293 flags |= MNTDF_COOKED;
5296 flags |= MNTDF_INODES;
5299 flags |= MNTDF_LAZY;
5305 flags |= MNTDF_VERBOSE;
5311 if (optind < argc && !realpath(argv[optind], path)) {
5313 fprintf(stderr, "error: invalid path '%s': %s\n",
5314 argv[optind], strerror(-rc));
5318 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5319 /* Check if we have a mount point */
5320 if (mntdir[0] == '\0')
5323 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5324 if (rc || path[0] != '\0')
5326 fsname[0] = '\0'; /* avoid matching in next loop */
5327 mntdir[0] = '\0'; /* avoid matching in next loop */
5333 static int lfs_getname(int argc, char **argv)
5335 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5336 int rc = 0, index = 0, c;
5337 char buf[sizeof(struct obd_uuid)];
5339 while ((c = getopt(argc, argv, "h")) != -1)
5342 if (optind == argc) { /* no paths specified, get all paths. */
5343 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5344 rc = llapi_getname(mntdir, buf, sizeof(buf));
5347 "cannot get name for `%s': %s\n",
5348 mntdir, strerror(-rc));
5352 printf("%s %s\n", buf, mntdir);
5354 path[0] = fsname[0] = mntdir[0] = 0;
5356 } else { /* paths specified, only attempt to search these. */
5357 for (; optind < argc; optind++) {
5358 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5361 "cannot get name for `%s': %s\n",
5362 argv[optind], strerror(-rc));
5366 printf("%s %s\n", buf, argv[optind]);
5372 static int lfs_check(int argc, char **argv)
5375 char mntdir[PATH_MAX] = {'\0'};
5382 fprintf(stderr, "%s check: server type must be specified\n",
5387 obd_types[0] = obd_type1;
5388 obd_types[1] = obd_type2;
5390 if (strcmp(argv[1], "osts") == 0) {
5391 strcpy(obd_types[0], "osc");
5392 } else if (strcmp(argv[1], "mds") == 0) {
5393 strcpy(obd_types[0], "mdc");
5394 } else if (strcmp(argv[1], "servers") == 0) {
5396 strcpy(obd_types[0], "osc");
5397 strcpy(obd_types[1], "mdc");
5399 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5404 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5405 if (rc < 0 || mntdir[0] == '\0') {
5407 "%s check: cannot find mounted Lustre filesystem: %s\n",
5408 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5412 rc = llapi_target_check(num_types, obd_types, mntdir);
5414 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5415 progname, argv[1], strerror(-rc));
5421 #ifdef HAVE_SYS_QUOTA_H
5422 #define ARG2INT(nr, str, msg) \
5425 nr = strtol(str, &endp, 0); \
5426 if (*endp != '\0') { \
5427 fprintf(stderr, "%s: bad %s '%s'\n", \
5428 progname, msg, str); \
5433 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5435 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5436 * returns the value or ULONG_MAX on integer overflow or incorrect format
5438 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5439 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5440 * 3. empty integer value is interpreted as 0
5442 static unsigned long str2sec(const char* timestr)
5444 const char spec[] = "smhdw";
5445 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5446 unsigned long val = 0;
5449 if (strpbrk(timestr, spec) == NULL) {
5450 /* no specifiers inside the time string,
5451 should treat it as an integer value */
5452 val = strtoul(timestr, &tail, 10);
5453 return *tail ? ULONG_MAX : val;
5456 /* format string is XXwXXdXXhXXmXXs */
5462 v = strtoul(timestr, &tail, 10);
5463 if (v == ULONG_MAX || *tail == '\0')
5464 /* value too large (ULONG_MAX or more)
5465 or missing specifier */
5468 ptr = strchr(spec, *tail);
5470 /* unknown specifier */
5475 /* check if product will overflow the type */
5476 if (!(v < ULONG_MAX / mult[ind]))
5479 ADD_OVERFLOW(val, mult[ind] * v);
5480 if (val == ULONG_MAX)
5492 #define ARG2ULL(nr, str, def_units) \
5494 unsigned long long limit, units = def_units; \
5497 rc = llapi_parse_size(str, &limit, &units, 1); \
5499 fprintf(stderr, "%s: invalid limit '%s'\n", \
5506 static inline int has_times_option(int argc, char **argv)
5510 for (i = 1; i < argc; i++)
5511 if (!strcmp(argv[i], "-t"))
5517 int lfs_setquota_times(int argc, char **argv)
5520 struct if_quotactl qctl;
5521 char *mnt, *obd_type = (char *)qctl.obd_type;
5522 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5523 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5524 struct option long_opts[] = {
5525 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5526 { .val = 'g', .name = "group", .has_arg = no_argument },
5527 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5528 { .val = 'p', .name = "projid", .has_arg = no_argument },
5529 { .val = 't', .name = "times", .has_arg = no_argument },
5530 { .val = 'u', .name = "user", .has_arg = no_argument },
5534 memset(&qctl, 0, sizeof(qctl));
5535 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5536 qctl.qc_type = ALLQUOTA;
5538 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5539 long_opts, NULL)) != -1) {
5550 if (qctl.qc_type != ALLQUOTA) {
5551 fprintf(stderr, "error: -u/g/p can't be used "
5552 "more than once\n");
5555 qctl.qc_type = qtype;
5558 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5559 fprintf(stderr, "error: bad block-grace: %s\n",
5563 dqb->dqb_valid |= QIF_BTIME;
5566 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5567 fprintf(stderr, "error: bad inode-grace: %s\n",
5571 dqb->dqb_valid |= QIF_ITIME;
5573 case 't': /* Yes, of course! */
5575 default: /* getopt prints error message for us when opterr != 0 */
5580 if (qctl.qc_type == ALLQUOTA) {
5581 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5585 if (optind != argc - 1) {
5586 fprintf(stderr, "error: unexpected parameters encountered\n");
5590 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5591 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5592 fprintf(stderr, "error: grace time is too large\n");
5597 rc = llapi_quotactl(mnt, &qctl);
5600 fprintf(stderr, "%s %s ", obd_type,
5601 obd_uuid2str(&qctl.obd_uuid));
5602 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5609 #define BSLIMIT (1 << 0)
5610 #define BHLIMIT (1 << 1)
5611 #define ISLIMIT (1 << 2)
5612 #define IHLIMIT (1 << 3)
5614 int lfs_setquota(int argc, char **argv)
5617 struct if_quotactl qctl;
5618 char *mnt, *obd_type = (char *)qctl.obd_type;
5619 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5620 struct option long_opts[] = {
5621 { .val = 'b', .name = "block-softlimit",
5622 .has_arg = required_argument },
5623 { .val = 'B', .name = "block-hardlimit",
5624 .has_arg = required_argument },
5625 { .val = 'd', .name = "default", .has_arg = no_argument },
5626 { .val = 'g', .name = "group", .has_arg = required_argument },
5627 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5628 { .val = 'i', .name = "inode-softlimit",
5629 .has_arg = required_argument },
5630 { .val = 'I', .name = "inode-hardlimit",
5631 .has_arg = required_argument },
5632 { .val = 'p', .name = "projid", .has_arg = required_argument },
5633 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5634 { .val = 'u', .name = "user", .has_arg = required_argument },
5635 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5637 unsigned limit_mask = 0;
5639 bool use_default = false;
5642 if (has_times_option(argc, argv))
5643 return lfs_setquota_times(argc, argv);
5645 memset(&qctl, 0, sizeof(qctl));
5646 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5647 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5648 * so it can be used as a marker that qc_type
5649 * isn't reinitialized from command line */
5651 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5652 long_opts, NULL)) != -1) {
5655 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5658 goto quota_type_def;
5661 rc = name2uid(&qctl.qc_id, optarg);
5664 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5667 goto quota_type_def;
5670 rc = name2gid(&qctl.qc_id, optarg);
5673 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5676 goto quota_type_def;
5679 rc = name2projid(&qctl.qc_id, optarg);
5682 qctl.qc_id = strtoul(optarg, &endptr, 10);
5683 if (*endptr != '\0') {
5684 fprintf(stderr, "%s setquota: invalid"
5685 " id '%s'\n", progname, optarg);
5690 if (qctl.qc_id == 0) {
5691 fprintf(stderr, "%s setquota: can't set quota"
5692 " for root usr/group/project.\n",
5698 if (qctl.qc_type != ALLQUOTA) {
5700 "%s setquota: only one of -u, -U, -g,"
5701 " -G, -p or -P may be specified\n",
5705 qctl.qc_type = qtype;
5708 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5712 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5713 dqb->dqb_bsoftlimit >>= 10;
5714 limit_mask |= BSLIMIT;
5715 if (dqb->dqb_bsoftlimit &&
5716 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5718 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5719 "See '%s help setquota' or Lustre manual for details\n",
5720 progname, dqb->dqb_bsoftlimit,
5724 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5725 dqb->dqb_bhardlimit >>= 10;
5726 limit_mask |= BHLIMIT;
5727 if (dqb->dqb_bhardlimit &&
5728 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5730 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5731 "See '%s help setquota' or Lustre manual for details\n",
5732 progname, dqb->dqb_bhardlimit,
5736 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5737 limit_mask |= ISLIMIT;
5738 if (dqb->dqb_isoftlimit &&
5739 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5741 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5742 "See '%s help setquota' or Lustre manual for details\n",
5743 progname, dqb->dqb_isoftlimit,
5747 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5748 limit_mask |= IHLIMIT;
5749 if (dqb->dqb_ihardlimit &&
5750 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5752 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5753 "See '%s help setquota' or Lustre manual for details\n",
5754 progname, dqb->dqb_ihardlimit,
5759 "%s setquota: unrecognized option '%s'\n",
5760 progname, argv[optind - 1]);
5765 if (qctl.qc_type == ALLQUOTA) {
5767 "%s setquota: either -u or -g must be specified\n",
5772 if (!use_default && limit_mask == 0) {
5774 "%s setquota: at least one limit must be specified\n",
5779 if (use_default && limit_mask != 0) {
5781 "%s setquota: limits should not be specified when"
5782 " using default quota\n",
5787 if (use_default && qctl.qc_id == 0) {
5789 "%s setquota: can not set default quota for root"
5790 " user/group/project\n",
5795 if (optind != argc - 1) {
5797 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5798 progname, argv[optind]);
5805 dqb->dqb_bhardlimit = 0;
5806 dqb->dqb_bsoftlimit = 0;
5807 dqb->dqb_ihardlimit = 0;
5808 dqb->dqb_isoftlimit = 0;
5811 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5812 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5813 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5814 /* sigh, we can't just set blimits/ilimits */
5815 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5816 .qc_type = qctl.qc_type,
5817 .qc_id = qctl.qc_id};
5819 rc = llapi_quotactl(mnt, &tmp_qctl);
5823 if (!(limit_mask & BHLIMIT))
5824 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5825 if (!(limit_mask & BSLIMIT))
5826 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5827 if (!(limit_mask & IHLIMIT))
5828 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5829 if (!(limit_mask & ISLIMIT))
5830 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5832 /* Keep grace times if we have got no softlimit arguments */
5833 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5834 dqb->dqb_valid |= QIF_BTIME;
5835 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5838 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5839 dqb->dqb_valid |= QIF_ITIME;
5840 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5844 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5845 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5847 rc = llapi_quotactl(mnt, &qctl);
5851 "%s setquota: cannot quotactl '%s' '%s': %s",
5853 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5860 /* Converts seconds value into format string
5861 * result is returned in buf
5863 * 1. result is in descenting order: 1w2d3h4m5s
5864 * 2. zero fields are not filled (except for p. 3): 5d1s
5865 * 3. zero seconds value is presented as "0s"
5867 static char * __sec2str(time_t seconds, char *buf)
5869 const char spec[] = "smhdw";
5870 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5875 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5876 c = seconds / mult[i];
5878 if (c > 0 || (i == 0 && buf == tail))
5879 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5887 static void sec2str(time_t seconds, char *buf, int rc)
5894 tail = __sec2str(seconds, tail);
5896 if (rc && tail - buf < 39) {
5902 static void diff2str(time_t seconds, char *buf, time_t now)
5908 if (seconds <= now) {
5909 strcpy(buf, "none");
5912 __sec2str(seconds - now, buf);
5915 static void print_quota_title(char *name, struct if_quotactl *qctl,
5916 bool human_readable, bool show_default)
5919 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
5920 printf("%15s %8s%8s%8s %8s%8s%8s\n",
5921 "Filesystem", "bquota", "blimit", "bgrace",
5922 "iquota", "ilimit", "igrace");
5924 printf("Disk quotas for %s %s (%cid %u):\n",
5925 qtype_name(qctl->qc_type), name,
5926 *qtype_name(qctl->qc_type), qctl->qc_id);
5927 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5928 "Filesystem", human_readable ? "used" : "kbytes",
5929 "quota", "limit", "grace",
5930 "files", "quota", "limit", "grace");
5934 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5937 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5940 snprintf(buf, buflen, "%5.4gP",
5941 (double)num / ((__u64)1 << 40));
5943 snprintf(buf, buflen, "%5.4gT",
5944 (double)num / (1 << 30));
5946 snprintf(buf, buflen, "%5.4gG",
5947 (double)num / (1 << 20));
5949 snprintf(buf, buflen, "%5.4gM",
5950 (double)num / (1 << 10));
5952 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5956 #define STRBUF_LEN 32
5957 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5958 int rc, bool h, bool show_default)
5964 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
5965 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
5966 int bover = 0, iover = 0;
5967 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5968 char numbuf[3][STRBUF_LEN];
5970 char strbuf[STRBUF_LEN];
5972 if (dqb->dqb_bhardlimit &&
5973 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5975 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5976 if (dqb->dqb_btime > now) {
5983 if (dqb->dqb_ihardlimit &&
5984 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5986 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5987 if (dqb->dqb_itime > now) {
5995 if (strlen(mnt) > 15)
5996 printf("%s\n%15s", mnt, "");
5998 printf("%15s", mnt);
6001 diff2str(dqb->dqb_btime, timebuf, now);
6002 else if (show_default)
6003 snprintf(timebuf, sizeof(timebuf), "%llu",
6006 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6007 strbuf, sizeof(strbuf), h);
6008 if (rc == -EREMOTEIO)
6009 sprintf(numbuf[0], "%s*", strbuf);
6011 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6012 "%s" : "[%s]", strbuf);
6014 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6015 if (type == QC_GENERAL)
6016 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6017 "%s" : "[%s]", strbuf);
6019 sprintf(numbuf[1], "%s", "-");
6021 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6022 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6023 "%s" : "[%s]", strbuf);
6026 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6028 printf(" %7s%c %6s %7s %7s",
6029 numbuf[0], bover ? '*' : ' ', numbuf[1],
6030 numbuf[2], bover > 1 ? timebuf : "-");
6034 diff2str(dqb->dqb_itime, timebuf, now);
6035 else if (show_default)
6036 snprintf(timebuf, sizeof(timebuf), "%llu",
6039 snprintf(numbuf[0], sizeof(numbuf),
6040 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6041 (uintmax_t)dqb->dqb_curinodes);
6043 if (type == QC_GENERAL)
6044 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6046 (uintmax_t)dqb->dqb_isoftlimit);
6048 sprintf(numbuf[1], "%s", "-");
6050 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6051 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6054 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6055 else if (type != QC_OSTIDX)
6056 printf(" %7s%c %6s %7s %7s",
6057 numbuf[0], iover ? '*' : ' ', numbuf[1],
6058 numbuf[2], iover > 1 ? timebuf : "-");
6060 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6063 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6064 qctl->qc_cmd == Q_GETOINFO) {
6068 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6069 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6070 printf("Block grace time: %s; Inode grace time: %s\n",
6071 bgtimebuf, igtimebuf);
6075 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6076 bool h, __u64 *total)
6078 int rc = 0, rc1 = 0, count = 0;
6079 __u32 valid = qctl->qc_valid;
6081 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6083 fprintf(stderr, "can not get %s count: %s\n",
6084 is_mdt ? "mdt": "ost", strerror(-rc));
6088 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6089 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6090 rc = llapi_quotactl(mnt, qctl);
6092 /* It is remote client case. */
6093 if (rc == -EOPNOTSUPP) {
6100 fprintf(stderr, "quotactl %s%d failed.\n",
6101 is_mdt ? "mdt": "ost", qctl->qc_idx);
6105 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6106 qctl->qc_valid, 0, h, false);
6107 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6108 qctl->qc_dqblk.dqb_bhardlimit;
6111 qctl->qc_valid = valid;
6115 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6116 int verbose, int quiet, bool human_readable,
6119 int rc1 = 0, rc2 = 0, rc3 = 0;
6120 char *obd_type = (char *)qctl->obd_type;
6121 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6122 __u64 total_ialloc = 0, total_balloc = 0;
6123 bool use_default_for_blk = false;
6124 bool use_default_for_file = false;
6127 rc1 = llapi_quotactl(mnt, qctl);
6131 fprintf(stderr, "%s quotas are not enabled.\n",
6132 qtype_name(qctl->qc_type));
6135 fprintf(stderr, "Permission denied.\n");
6138 /* We already got error message. */
6141 fprintf(stderr, "Unexpected quotactl error: %s\n",
6146 if (!show_default && qctl->qc_id == 0) {
6147 qctl->qc_dqblk.dqb_bhardlimit = 0;
6148 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6149 qctl->qc_dqblk.dqb_ihardlimit = 0;
6150 qctl->qc_dqblk.dqb_isoftlimit = 0;
6151 qctl->qc_dqblk.dqb_btime = 0;
6152 qctl->qc_dqblk.dqb_itime = 0;
6153 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6156 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6157 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6158 use_default_for_blk = true;
6159 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6162 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6163 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6164 use_default_for_file = true;
6165 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6168 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6169 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6170 print_quota_title(name, qctl, human_readable, show_default);
6172 if (rc1 && *obd_type)
6173 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6175 if (qctl->qc_valid != QC_GENERAL)
6178 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6179 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6180 (QIF_LIMITS|QIF_USAGE));
6182 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6184 if (!show_default && verbose &&
6185 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6186 char strbuf[STRBUF_LEN];
6188 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6190 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6192 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6194 printf("Total allocated inode limit: %ju, total "
6195 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6199 if (use_default_for_blk)
6200 printf("%cid %u is using default block quota setting\n",
6201 *qtype_name(qctl->qc_type), qctl->qc_id);
6203 if (use_default_for_file)
6204 printf("%cid %u is using default file quota setting\n",
6205 *qtype_name(qctl->qc_type), qctl->qc_id);
6207 if (rc1 || rc2 || rc3 || inacc)
6208 printf("Some errors happened when getting quota info. "
6209 "Some devices may be not working or deactivated. "
6210 "The data in \"[]\" is inaccurate.\n");
6216 static int lfs_project(int argc, char **argv)
6218 int ret = 0, err = 0, c, i;
6219 struct project_handle_control phc = { 0 };
6220 enum lfs_project_ops_t op;
6223 phc.assign_projid = false;
6224 /* default action */
6225 op = LFS_PROJECT_LIST;
6227 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6230 if (op != LFS_PROJECT_LIST) {
6232 "%s: cannot specify '-c' '-C' '-s' together\n",
6237 op = LFS_PROJECT_CHECK;
6240 if (op != LFS_PROJECT_LIST) {
6242 "%s: cannot specify '-c' '-C' '-s' together\n",
6247 op = LFS_PROJECT_CLEAR;
6250 if (op != LFS_PROJECT_LIST) {
6252 "%s: cannot specify '-c' '-C' '-s' together\n",
6257 phc.set_inherit = true;
6258 op = LFS_PROJECT_SET;
6264 phc.keep_projid = true;
6267 phc.recursive = true;
6270 phc.projid = strtoul(optarg, NULL, 0);
6271 phc.assign_projid = true;
6275 phc.newline = false;
6278 fprintf(stderr, "%s: invalid option '%c'\n",
6284 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6285 op = LFS_PROJECT_SET;
6286 phc.set_projid = true;
6287 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6288 phc.set_projid = true;
6292 case LFS_PROJECT_CHECK:
6293 if (phc.keep_projid) {
6295 "%s: '-k' is useless together with '-c'\n",
6300 case LFS_PROJECT_CLEAR:
6303 "%s: '-0' is useless together with '-C'\n",
6307 if (phc.assign_projid) {
6309 "%s: '-p' is useless together with '-C'\n",
6314 case LFS_PROJECT_SET:
6317 "%s: '-0' is useless together with '-s'\n",
6321 if (phc.keep_projid) {
6323 "%s: '-k' is useless together with '-s'\n",
6331 "%s: '-0' is useless for list operations\n",
6341 fprintf(stderr, "%s: missing file or directory target(s)\n",
6346 for (i = 0; i < argc; i++) {
6348 case LFS_PROJECT_CHECK:
6349 err = lfs_project_check(argv[i], &phc);
6351 case LFS_PROJECT_LIST:
6352 err = lfs_project_list(argv[i], &phc);
6354 case LFS_PROJECT_CLEAR:
6355 err = lfs_project_clear(argv[i], &phc);
6357 case LFS_PROJECT_SET:
6358 err = lfs_project_set(argv[i], &phc);
6370 static int lfs_quota(int argc, char **argv)
6373 char *mnt, *name = NULL;
6374 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6375 .qc_type = ALLQUOTA };
6376 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6377 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6379 __u32 valid = QC_GENERAL, idx = 0;
6380 bool human_readable = false;
6381 bool show_default = false;
6384 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6387 show_default = true;
6392 show_default = true;
6397 show_default = true;
6401 if (qctl.qc_type != ALLQUOTA) {
6403 "%s quota: only one of -u, -g, or -p may be specified\n",
6407 qctl.qc_type = qtype;
6410 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6413 valid = qctl.qc_valid = QC_UUID;
6414 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6417 valid = qctl.qc_valid = QC_MDTIDX;
6418 idx = qctl.qc_idx = atoi(optarg);
6419 if (idx == 0 && *optarg != '0') {
6421 "%s quota: invalid MDT index '%s'\n",
6427 valid = qctl.qc_valid = QC_OSTIDX;
6428 idx = qctl.qc_idx = atoi(optarg);
6429 if (idx == 0 && *optarg != '0') {
6431 "%s quota: invalid OST index '%s'\n",
6443 human_readable = true;
6446 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6447 progname, argv[optind - 1]);
6452 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6453 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6454 optind == argc - 1 && !show_default) {
6456 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6457 qctl.qc_valid = valid;
6460 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6461 qctl.qc_type = qtype;
6462 if (qtype == USRQUOTA) {
6463 qctl.qc_id = geteuid();
6464 rc = uid2name(&name, qctl.qc_id);
6466 qctl.qc_id = getegid();
6467 rc = gid2name(&name, qctl.qc_id);
6472 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6473 human_readable, show_default);
6478 /* lfs quota -u username /path/to/lustre/mount */
6479 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6480 /* options should be followed by u/g-name and mntpoint */
6481 if ((!show_default && optind + 2 != argc) ||
6482 (show_default && optind + 1 != argc) ||
6483 qctl.qc_type == ALLQUOTA) {
6485 "%s quota: name and mount point must be specified\n",
6490 if (!show_default) {
6491 name = argv[optind++];
6492 switch (qctl.qc_type) {
6494 rc = name2uid(&qctl.qc_id, name);
6497 rc = name2gid(&qctl.qc_id, name);
6500 rc = name2projid(&qctl.qc_id, name);
6507 qctl.qc_valid = QC_GENERAL;
6508 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6513 qctl.qc_id = strtoul(name, &endptr, 10);
6514 if (*endptr != '\0') {
6515 fprintf(stderr, "%s quota: invalid id '%s'\n",
6520 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6521 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6527 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6528 human_readable, show_default);
6531 #endif /* HAVE_SYS_QUOTA_H! */
6533 static int flushctx_ioctl(char *mp)
6537 fd = open(mp, O_RDONLY);
6539 fprintf(stderr, "flushctx: error open %s: %s\n",
6540 mp, strerror(errno));
6544 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6546 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6547 mp, strerror(errno));
6553 static int lfs_flushctx(int argc, char **argv)
6555 int kdestroy = 0, c;
6556 char mntdir[PATH_MAX] = {'\0'};
6560 while ((c = getopt(argc, argv, "k")) != -1) {
6566 fprintf(stderr, "error: %s: option '-%c' "
6567 "unrecognized\n", argv[0], c);
6573 if ((rc = system("kdestroy > /dev/null")) != 0) {
6574 rc = WEXITSTATUS(rc);
6575 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6579 if (optind >= argc) {
6580 /* flush for all mounted lustre fs. */
6581 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6582 /* Check if we have a mount point */
6583 if (mntdir[0] == '\0')
6586 if (flushctx_ioctl(mntdir))
6589 mntdir[0] = '\0'; /* avoid matching in next loop */
6592 /* flush fs as specified */
6593 while (optind < argc) {
6594 if (flushctx_ioctl(argv[optind++]))
6601 static int lfs_cp(int argc, char **argv)
6603 fprintf(stderr, "remote client copy file(s).\n"
6604 "obsolete, does not support it anymore.\n");
6608 static int lfs_ls(int argc, char **argv)
6610 fprintf(stderr, "remote client lists directory contents.\n"
6611 "obsolete, does not support it anymore.\n");
6615 static int lfs_changelog(int argc, char **argv)
6617 void *changelog_priv;
6618 struct changelog_rec *rec;
6619 long long startrec = 0, endrec = 0;
6621 struct option long_opts[] = {
6622 { .val = 'f', .name = "follow", .has_arg = no_argument },
6624 char short_opts[] = "f";
6627 while ((rc = getopt_long(argc, argv, short_opts,
6628 long_opts, NULL)) != -1) {
6635 "%s changelog: unrecognized option '%s'\n",
6636 progname, argv[optind - 1]);
6640 if (optind >= argc) {
6641 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6646 mdd = argv[optind++];
6648 startrec = strtoll(argv[optind++], NULL, 10);
6650 endrec = strtoll(argv[optind++], NULL, 10);
6652 rc = llapi_changelog_start(&changelog_priv,
6653 CHANGELOG_FLAG_BLOCK |
6654 CHANGELOG_FLAG_JOBID |
6655 CHANGELOG_FLAG_EXTRA_FLAGS |
6656 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6659 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6660 progname, strerror(errno = -rc));
6664 rc = llapi_changelog_set_xflags(changelog_priv,
6665 CHANGELOG_EXTRA_FLAG_UIDGID |
6666 CHANGELOG_EXTRA_FLAG_NID |
6667 CHANGELOG_EXTRA_FLAG_OMODE |
6668 CHANGELOG_EXTRA_FLAG_XATTR);
6671 "%s changelog: cannot set xflags for changelog: %s\n",
6672 progname, strerror(errno = -rc));
6676 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6680 if (endrec && rec->cr_index > endrec) {
6681 llapi_changelog_free(&rec);
6684 if (rec->cr_index < startrec) {
6685 llapi_changelog_free(&rec);
6689 secs = rec->cr_time >> 30;
6690 gmtime_r(&secs, &ts);
6691 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6692 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6693 changelog_type2str(rec->cr_type),
6694 ts.tm_hour, ts.tm_min, ts.tm_sec,
6695 (int)(rec->cr_time & ((1 << 30) - 1)),
6696 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6697 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6699 if (rec->cr_flags & CLF_JOBID) {
6700 struct changelog_ext_jobid *jid =
6701 changelog_rec_jobid(rec);
6703 if (jid->cr_jobid[0] != '\0')
6704 printf(" j=%s", jid->cr_jobid);
6707 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6708 struct changelog_ext_extra_flags *ef =
6709 changelog_rec_extra_flags(rec);
6711 printf(" ef=0x%llx", ef->cr_extra_flags);
6713 if (ef->cr_extra_flags & CLFE_UIDGID) {
6714 struct changelog_ext_uidgid *uidgid =
6715 changelog_rec_uidgid(rec);
6717 printf(" u=%llu:%llu",
6718 uidgid->cr_uid, uidgid->cr_gid);
6720 if (ef->cr_extra_flags & CLFE_NID) {
6721 struct changelog_ext_nid *nid =
6722 changelog_rec_nid(rec);
6725 libcfs_nid2str(nid->cr_nid));
6728 if (ef->cr_extra_flags & CLFE_OPEN) {
6729 struct changelog_ext_openmode *omd =
6730 changelog_rec_openmode(rec);
6731 char mode[] = "---";
6733 /* exec mode must be exclusive */
6734 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6737 if (omd->cr_openflags & MDS_FMODE_READ)
6739 if (omd->cr_openflags &
6746 if (strcmp(mode, "---") != 0)
6747 printf(" m=%s", mode);
6751 if (ef->cr_extra_flags & CLFE_XATTR) {
6752 struct changelog_ext_xattr *xattr =
6753 changelog_rec_xattr(rec);
6755 if (xattr->cr_xattr[0] != '\0')
6756 printf(" x=%s", xattr->cr_xattr);
6760 if (rec->cr_namelen)
6761 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6762 rec->cr_namelen, changelog_rec_name(rec));
6764 if (rec->cr_flags & CLF_RENAME) {
6765 struct changelog_ext_rename *rnm =
6766 changelog_rec_rename(rec);
6768 if (!fid_is_zero(&rnm->cr_sfid))
6769 printf(" s="DFID" sp="DFID" %.*s",
6770 PFID(&rnm->cr_sfid),
6771 PFID(&rnm->cr_spfid),
6772 (int)changelog_rec_snamelen(rec),
6773 changelog_rec_sname(rec));
6777 llapi_changelog_free(&rec);
6780 llapi_changelog_fini(&changelog_priv);
6783 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6784 progname, strerror(errno = -rc));
6786 return (rc == 1 ? 0 : rc);
6789 static int lfs_changelog_clear(int argc, char **argv)
6797 endrec = strtoll(argv[3], NULL, 10);
6799 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6802 fprintf(stderr, "%s: record out of range: %llu\n",
6804 else if (rc == -ENOENT)
6805 fprintf(stderr, "%s: no changelog user: %s\n",
6808 fprintf(stderr, "%s error: %s\n", argv[0],
6817 static int lfs_fid2path(int argc, char **argv)
6819 struct option long_opts[] = {
6820 { .val = 'c', .name = "cur", .has_arg = no_argument },
6821 { .val = 'l', .name = "link", .has_arg = required_argument },
6822 { .val = 'r', .name = "rec", .has_arg = required_argument },
6824 char short_opts[] = "cl:r:";
6825 char *device, *fid, *path;
6826 long long recno = -1;
6831 char *endptr = NULL;
6833 while ((rc = getopt_long(argc, argv, short_opts,
6834 long_opts, NULL)) != -1) {
6840 linkno = strtol(optarg, &endptr, 10);
6841 if (*endptr != '\0') {
6843 "%s fid2path: invalid linkno '%s'\n",
6849 recno = strtoll(optarg, &endptr, 10);
6850 if (*endptr != '\0') {
6852 "%s fid2path: invalid recno '%s'\n",
6859 "%s fid2path: unrecognized option '%s'\n",
6860 progname, argv[optind - 1]);
6867 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6872 device = argv[optind++];
6873 path = calloc(1, PATH_MAX);
6877 "%s fid2path: cannot allocate memory for path: %s\n",
6878 progname, strerror(-rc));
6883 while (optind < argc) {
6884 fid = argv[optind++];
6886 lnktmp = (linkno >= 0) ? linkno : 0;
6888 int oldtmp = lnktmp;
6889 long long rectmp = recno;
6891 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6895 "%s fid2path: cannot find '%s': %s\n",
6896 progname, fid, strerror(errno = -rc2));
6903 fprintf(stdout, "%lld ", rectmp);
6904 if (device[0] == '/') {
6905 fprintf(stdout, "%s", device);
6906 if (device[strlen(device) - 1] != '/')
6907 fprintf(stdout, "/");
6908 } else if (path[0] == '\0') {
6909 fprintf(stdout, "/");
6911 fprintf(stdout, "%s\n", path);
6914 /* specified linkno */
6916 if (oldtmp == lnktmp)
6926 static int lfs_path2fid(int argc, char **argv)
6928 struct option long_opts[] = {
6929 { .val = 'p', .name = "parents", .has_arg = no_argument },
6932 const char short_opts[] = "p";
6933 const char *sep = "";
6936 bool show_parents = false;
6938 while ((rc = getopt_long(argc, argv, short_opts,
6939 long_opts, NULL)) != -1) {
6942 show_parents = true;
6946 "%s path2fid: unrecognized option '%s'\n",
6947 progname, argv[optind - 1]);
6952 if (optind > argc - 1) {
6953 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
6957 else if (optind < argc - 1)
6961 for (path = argv + optind; *path != NULL; path++) {
6963 if (!show_parents) {
6964 err = llapi_path2fid(*path, &fid);
6966 printf("%s%s"DFID"\n",
6967 *sep != '\0' ? *path : "", sep,
6970 char name[NAME_MAX + 1];
6971 unsigned int linkno = 0;
6973 while ((err = llapi_path2parent(*path, linkno, &fid,
6974 name, sizeof(name))) == 0) {
6975 if (*sep != '\0' && linkno == 0)
6976 printf("%s%s", *path, sep);
6978 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6983 /* err == -ENODATA is end-of-loop */
6984 if (linkno > 0 && err == -ENODATA) {
6992 "%s path2fid: cannot get %sfid for '%s': %s\n",
6993 progname, show_parents ? "parent " : "", *path,
7005 static int lfs_data_version(int argc, char **argv)
7012 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7015 fprintf(stderr, "%s data_version: FILE must be specified\n",
7020 while ((c = getopt(argc, argv, "nrw")) != -1) {
7023 data_version_flags = 0;
7026 data_version_flags |= LL_DV_RD_FLUSH;
7029 data_version_flags |= LL_DV_WR_FLUSH;
7033 "%s data_version: unrecognized option '%s'\n",
7034 progname, argv[optind - 1]);
7038 if (optind == argc) {
7039 fprintf(stderr, "%s data_version: FILE must be specified\n",
7044 path = argv[optind];
7045 fd = open(path, O_RDONLY);
7048 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7049 progname, path, strerror(-rc));
7053 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7056 "%s data_version: cannot get version for '%s': %s\n",
7057 progname, path, strerror(-rc));
7059 printf("%ju" "\n", (uintmax_t)data_version);
7065 static int lfs_hsm_state(int argc, char **argv)
7070 struct hsm_user_state hus;
7078 rc = llapi_hsm_state_get(path, &hus);
7080 fprintf(stderr, "can't get hsm state for %s: %s\n",
7081 path, strerror(errno = -rc));
7085 /* Display path name and status flags */
7086 printf("%s: (0x%08x)", path, hus.hus_states);
7088 if (hus.hus_states & HS_RELEASED)
7089 printf(" released");
7090 if (hus.hus_states & HS_EXISTS)
7092 if (hus.hus_states & HS_DIRTY)
7094 if (hus.hus_states & HS_ARCHIVED)
7095 printf(" archived");
7096 /* Display user-settable flags */
7097 if (hus.hus_states & HS_NORELEASE)
7098 printf(" never_release");
7099 if (hus.hus_states & HS_NOARCHIVE)
7100 printf(" never_archive");
7101 if (hus.hus_states & HS_LOST)
7102 printf(" lost_from_hsm");
7104 if (hus.hus_archive_id != 0)
7105 printf(", archive_id:%d", hus.hus_archive_id);
7108 } while (++i < argc);
7113 #define LFS_HSM_SET 0
7114 #define LFS_HSM_CLEAR 1
7117 * Generic function to set or clear HSM flags.
7118 * Used by hsm_set and hsm_clear.
7120 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7122 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7124 struct option long_opts[] = {
7125 { .val = 'A', .name = "archived", .has_arg = no_argument },
7126 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7127 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7128 { .val = 'e', .name = "exists", .has_arg = no_argument },
7129 { .val = 'l', .name = "lost", .has_arg = no_argument },
7130 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7131 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7133 char short_opts[] = "lraAdei:";
7137 __u32 archive_id = 0;
7143 while ((c = getopt_long(argc, argv, short_opts,
7144 long_opts, NULL)) != -1) {
7150 mask |= HS_NOARCHIVE;
7153 mask |= HS_ARCHIVED;
7156 mask |= HS_NORELEASE;
7165 archive_id = strtol(optarg, &end, 10);
7167 fprintf(stderr, "invalid archive_id: '%s'\n",
7175 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7176 argv[0], argv[optind - 1]);
7181 /* User should have specified a flag */
7185 while (optind < argc) {
7187 path = argv[optind];
7189 /* If mode == 0, this means we apply the mask. */
7190 if (mode == LFS_HSM_SET)
7191 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7193 rc = llapi_hsm_state_set(path, 0, mask, 0);
7196 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7197 path, strerror(errno = -rc));
7206 static int lfs_hsm_action(int argc, char **argv)
7211 struct hsm_current_action hca;
7212 struct hsm_extent he;
7213 enum hsm_user_action hua;
7214 enum hsm_progress_states hps;
7222 rc = llapi_hsm_current_action(path, &hca);
7224 fprintf(stderr, "can't get hsm action for %s: %s\n",
7225 path, strerror(errno = -rc));
7228 he = hca.hca_location;
7229 hua = hca.hca_action;
7230 hps = hca.hca_state;
7232 printf("%s: %s", path, hsm_user_action2name(hua));
7234 /* Skip file without action */
7235 if (hca.hca_action == HUA_NONE) {
7240 printf(" %s ", hsm_progress_state2name(hps));
7242 if ((hps == HPS_RUNNING) &&
7243 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7244 printf("(%llu bytes moved)\n",
7245 (unsigned long long)he.length);
7246 else if ((he.offset + he.length) == LUSTRE_EOF)
7247 printf("(from %llu to EOF)\n",
7248 (unsigned long long)he.offset);
7250 printf("(from %llu to %llu)\n",
7251 (unsigned long long)he.offset,
7252 (unsigned long long)(he.offset + he.length));
7254 } while (++i < argc);
7259 static int lfs_hsm_set(int argc, char **argv)
7261 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7264 static int lfs_hsm_clear(int argc, char **argv)
7266 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7270 * Check file state and return its fid, to be used by lfs_hsm_request().
7272 * \param[in] file Path to file to check
7273 * \param[in,out] fid Pointer to allocated lu_fid struct.
7274 * \param[in,out] last_dev Pointer to last device id used.
7276 * \return 0 on success.
7278 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7284 rc = lstat(file, &st);
7286 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7289 /* Checking for regular file as archiving as posix copytool
7290 * rejects archiving files other than regular files
7292 if (!S_ISREG(st.st_mode)) {
7293 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7296 /* A request should be ... */
7297 if (*last_dev != st.st_dev && *last_dev != 0) {
7298 fprintf(stderr, "All files should be "
7299 "on the same filesystem: %s\n", file);
7302 *last_dev = st.st_dev;
7304 rc = llapi_path2fid(file, fid);
7306 fprintf(stderr, "Cannot read FID of %s: %s\n",
7307 file, strerror(-rc));
7313 /* Fill an HSM HUR item with a given file name.
7315 * If mntpath is set, then the filename is actually a FID, and no
7316 * lookup on the filesystem will be performed.
7318 * \param[in] hur the user request to fill
7319 * \param[in] idx index of the item inside the HUR to fill
7320 * \param[in] mntpath mountpoint of Lustre
7321 * \param[in] fname filename (if mtnpath is NULL)
7322 * or FID (if mntpath is set)
7323 * \param[in] last_dev pointer to last device id used
7325 * \retval 0 on success
7326 * \retval CMD_HELP or a negative errno on error
7328 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7329 const char *mntpath, const char *fname,
7332 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7335 hui->hui_extent.length = -1;
7337 if (mntpath != NULL) {
7340 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7344 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7349 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7353 hur->hur_request.hr_itemcount++;
7358 static int lfs_hsm_request(int argc, char **argv, int action)
7360 struct option long_opts[] = {
7361 { .val = 'a', .name = "archive", .has_arg = required_argument },
7362 { .val = 'D', .name = "data", .has_arg = required_argument },
7363 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7364 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7367 char short_opts[] = "l:D:a:m:";
7368 struct hsm_user_request *hur, *oldhur;
7373 char *filelist = NULL;
7374 char fullpath[PATH_MAX];
7375 char *opaque = NULL;
7379 int nbfile_alloc = 0;
7380 char *some_file = NULL;
7381 char *mntpath = NULL;
7387 while ((c = getopt_long(argc, argv, short_opts,
7388 long_opts, NULL)) != -1) {
7397 if (action != HUA_ARCHIVE &&
7398 action != HUA_REMOVE) {
7400 "error: -a is supported only "
7401 "when archiving or removing\n");
7404 archive_id = atoi(optarg);
7407 if (some_file == NULL) {
7409 some_file = strdup(optarg);
7415 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7416 argv[0], argv[optind - 1]);
7421 /* All remaining args are files, so we have at least nbfile */
7422 nbfile = argc - optind;
7424 if ((nbfile == 0) && (filelist == NULL))
7428 opaque_len = strlen(opaque);
7430 /* Alloc the request structure with enough place to store all files
7431 * from command line. */
7432 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7434 fprintf(stderr, "Cannot create the request: %s\n",
7438 nbfile_alloc = nbfile;
7440 hur->hur_request.hr_action = action;
7441 hur->hur_request.hr_archive_id = archive_id;
7442 hur->hur_request.hr_flags = 0;
7444 /* All remaining args are files, add them */
7445 if (nbfile != 0 && some_file == NULL)
7446 some_file = strdup(argv[optind]);
7448 for (i = 0; i < nbfile; i++) {
7449 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7455 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7457 /* If a filelist was specified, read the filelist from it. */
7458 if (filelist != NULL) {
7459 fp = fopen(filelist, "r");
7461 fprintf(stderr, "Cannot read the file list %s: %s\n",
7462 filelist, strerror(errno));
7467 while ((rc = getline(&line, &len, fp)) != -1) {
7468 /* If allocated buffer was too small, get something
7470 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7473 nbfile_alloc = nbfile_alloc * 2 + 1;
7475 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7478 fprintf(stderr, "hsm: cannot allocate "
7479 "the request: %s\n",
7486 size = hur_len(oldhur);
7488 fprintf(stderr, "hsm: cannot allocate "
7489 "%u files + %u bytes data\n",
7490 oldhur->hur_request.hr_itemcount,
7491 oldhur->hur_request.hr_data_len);
7498 memcpy(hur, oldhur, size);
7503 if (line[strlen(line) - 1] == '\n')
7504 line[strlen(line) - 1] = '\0';
7506 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7507 mntpath, line, &last_dev);
7513 if (some_file == NULL) {
7523 /* If a --data was used, add it to the request */
7524 hur->hur_request.hr_data_len = opaque_len;
7526 memcpy(hur_data(hur), opaque, opaque_len);
7528 /* Send the HSM request */
7529 if (realpath(some_file, fullpath) == NULL) {
7530 fprintf(stderr, "Could not find path '%s': %s\n",
7531 some_file, strerror(errno));
7533 rc = llapi_hsm_request(fullpath, hur);
7535 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7536 some_file, strerror(-rc));
7546 static int lfs_hsm_archive(int argc, char **argv)
7548 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7551 static int lfs_hsm_restore(int argc, char **argv)
7553 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7556 static int lfs_hsm_release(int argc, char **argv)
7558 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7561 static int lfs_hsm_remove(int argc, char **argv)
7563 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7566 static int lfs_hsm_cancel(int argc, char **argv)
7568 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7571 static int lfs_swap_layouts(int argc, char **argv)
7576 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7577 SWAP_LAYOUTS_KEEP_MTIME |
7578 SWAP_LAYOUTS_KEEP_ATIME);
7581 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7583 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7585 int lfs_get_mode(const char *string)
7587 enum lock_mode_user mode;
7589 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7590 if (lock_mode_names[mode] == NULL)
7592 if (strcmp(string, lock_mode_names[mode]) == 0)
7599 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7601 enum lu_ladvise_type advice;
7604 advice < ARRAY_SIZE(ladvise_names); advice++) {
7605 if (ladvise_names[advice] == NULL)
7607 if (strcmp(string, ladvise_names[advice]) == 0)
7611 return LU_LADVISE_INVALID;
7614 static int lfs_ladvise(int argc, char **argv)
7616 struct option long_opts[] = {
7617 { .val = 'a', .name = "advice", .has_arg = required_argument },
7618 { .val = 'b', .name = "background", .has_arg = no_argument },
7619 { .val = 'e', .name = "end", .has_arg = required_argument },
7620 { .val = 'l', .name = "length", .has_arg = required_argument },
7621 { .val = 'm', .name = "mode", .has_arg = required_argument },
7622 { .val = 's', .name = "start", .has_arg = required_argument },
7623 { .val = 'u', .name = "unset", .has_arg = no_argument },
7625 char short_opts[] = "a:be:l:m:s:u";
7630 struct llapi_lu_ladvise advice;
7631 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7632 unsigned long long start = 0;
7633 unsigned long long end = LUSTRE_EOF;
7634 unsigned long long length = 0;
7635 unsigned long long size_units;
7636 unsigned long long flags = 0;
7640 while ((c = getopt_long(argc, argv, short_opts,
7641 long_opts, NULL)) != -1) {
7644 advice_type = lfs_get_ladvice(optarg);
7645 if (advice_type == LU_LADVISE_INVALID) {
7646 fprintf(stderr, "%s: invalid advice type "
7647 "'%s'\n", argv[0], optarg);
7648 fprintf(stderr, "Valid types:");
7650 for (advice_type = 0;
7651 advice_type < ARRAY_SIZE(ladvise_names);
7653 if (ladvise_names[advice_type] == NULL)
7655 fprintf(stderr, " %s",
7656 ladvise_names[advice_type]);
7658 fprintf(stderr, "\n");
7671 rc = llapi_parse_size(optarg, &end,
7674 fprintf(stderr, "%s: bad end offset '%s'\n",
7681 rc = llapi_parse_size(optarg, &start,
7684 fprintf(stderr, "%s: bad start offset "
7685 "'%s'\n", argv[0], optarg);
7691 rc = llapi_parse_size(optarg, &length,
7694 fprintf(stderr, "%s: bad length '%s'\n",
7700 mode = lfs_get_mode(optarg);
7702 fprintf(stderr, "%s: bad mode '%s', valid "
7703 "modes are READ or WRITE\n",
7711 fprintf(stderr, "%s: option '%s' unrecognized\n",
7712 argv[0], argv[optind - 1]);
7717 if (advice_type == LU_LADVISE_INVALID) {
7718 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7719 fprintf(stderr, "Valid types:");
7720 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7722 if (ladvise_names[advice_type] == NULL)
7724 fprintf(stderr, " %s", ladvise_names[advice_type]);
7726 fprintf(stderr, "\n");
7730 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7731 fprintf(stderr, "%s: Lock no expand advice is a per file "
7732 "descriptor advice, so when called from lfs, "
7733 "it does nothing.\n", argv[0]);
7737 if (argc <= optind) {
7738 fprintf(stderr, "%s: please give one or more file names\n",
7743 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7744 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7749 if (end == LUSTRE_EOF && length != 0)
7750 end = start + length;
7753 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7754 argv[0], start, end);
7758 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7759 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7764 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7765 fprintf(stderr, "%s: mode is required with lockahead\n",
7770 while (optind < argc) {
7773 path = argv[optind++];
7775 fd = open(path, O_RDONLY);
7777 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7778 argv[0], path, strerror(errno));
7783 advice.lla_start = start;
7784 advice.lla_end = end;
7785 advice.lla_advice = advice_type;
7786 advice.lla_value1 = 0;
7787 advice.lla_value2 = 0;
7788 advice.lla_value3 = 0;
7789 advice.lla_value4 = 0;
7790 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7791 advice.lla_lockahead_mode = mode;
7792 advice.lla_peradvice_flags = flags;
7795 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7798 fprintf(stderr, "%s: cannot give advice '%s' to file "
7799 "'%s': %s\n", argv[0],
7800 ladvise_names[advice_type],
7801 path, strerror(errno));
7807 if (rc == 0 && rc2 < 0)
7813 /** The input string contains a comma delimited list of component ids and
7814 * ranges, for example "1,2-4,7".
7816 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7818 bool end_of_loop = false;
7826 while (!end_of_loop) {
7830 char *endptr = NULL;
7833 ptr = strchrnul(arg, ',');
7834 end_of_loop = *ptr == '\0';
7837 start_index = strtol(arg, &endptr, 0);
7838 if (endptr == arg) /* no data at all */
7840 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7842 if (start_index < 0)
7845 end_index = start_index;
7846 if (*endptr == '-') {
7847 end_index = strtol(endptr + 1, &endptr, 0);
7848 if (*endptr != '\0')
7850 if (end_index < start_index)
7854 for (i = start_index; i <= end_index && size > 0; i++) {
7857 /* remove duplicate */
7858 for (j = 0; j < nr; j++) {
7862 if (j == nr) { /* no duplicate */
7868 if (size == 0 && i < end_index)
7875 if (!end_of_loop && ptr != NULL)
7878 return rc < 0 ? rc : nr;
7882 * struct verify_mirror_id - Mirror id to be verified.
7883 * @mirror_id: A specified mirror id.
7884 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7886 struct verify_mirror_id {
7892 * compare_mirror_ids() - Compare mirror ids.
7893 * @layout: Mirror component list.
7894 * @cbdata: Callback data in verify_mirror_id structure.
7896 * This is a callback function called by llapi_layout_comp_iterate()
7897 * to compare the specified mirror id with the one in the current
7898 * component of @layout. If they are the same, then the specified
7899 * mirror id is valid.
7901 * Return: a negative error code on failure or
7902 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7903 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7906 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7908 struct verify_mirror_id *mirror_id_cbdata =
7909 (struct verify_mirror_id *)cbdata;
7913 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7917 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7918 progname, strerror(errno));
7922 if (mirror_id_cbdata->mirror_id == mirror_id) {
7923 mirror_id_cbdata->is_valid_id = true;
7924 return LLAPI_LAYOUT_ITER_STOP;
7927 return LLAPI_LAYOUT_ITER_CONT;
7931 * verify_mirror_ids() - Verify specified mirror ids.
7932 * @fname: Mirrored file name.
7933 * @mirror_ids: Specified mirror ids to be verified.
7934 * @ids_nr: Number of specified mirror ids.
7936 * This function verifies that specified @mirror_ids are valid
7937 * in the mirrored file @fname.
7939 * Return: 0 on success or a negative error code on failure.
7942 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7944 struct llapi_layout *layout = NULL;
7945 struct verify_mirror_id mirror_id_cbdata = { 0 };
7956 if (stat(fname, &stbuf) < 0) {
7957 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7958 progname, fname, strerror(errno));
7963 if (!S_ISREG(stbuf.st_mode)) {
7964 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7970 fd = open(fname, O_DIRECT | O_RDONLY);
7972 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7973 progname, fname, strerror(errno));
7978 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
7980 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
7981 progname, fname, strerror(errno));
7985 layout = llapi_layout_get_by_fd(fd, 0);
7986 if (layout == NULL) {
7987 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7988 progname, fname, strerror(errno));
7990 llapi_lease_release(fd);
7994 rc = llapi_layout_flags_get(layout, &flr_state);
7996 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7997 progname, fname, strerror(errno));
8002 flr_state &= LCM_FL_FLR_MASK;
8003 switch (flr_state) {
8006 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8007 progname, fname, llapi_layout_flags_string(flr_state));
8014 for (i = 0; i < ids_nr; i++) {
8015 mirror_id_cbdata.mirror_id = mirror_ids[i];
8016 mirror_id_cbdata.is_valid_id = false;
8018 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8023 "%s: '%s' failed to verify mirror id: %u.\n",
8024 progname, fname, mirror_ids[i]);
8028 if (!mirror_id_cbdata.is_valid_id) {
8031 "%s: '%s' invalid specified mirror id: %u.\n",
8032 progname, fname, mirror_ids[i]);
8038 llapi_layout_free(layout);
8039 llapi_lease_release(fd);
8047 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8048 __u16 *mirror_ids, int ids_nr)
8050 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8051 struct llapi_layout *layout;
8061 if (stat(fname, &stbuf) < 0) {
8062 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8063 progname, fname, strerror(errno));
8067 if (!S_ISREG(stbuf.st_mode)) {
8068 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8074 fd = open(fname, O_DIRECT | O_RDWR);
8076 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8077 progname, fname, strerror(errno));
8082 layout = llapi_layout_get_by_fd(fd, 0);
8083 if (layout == NULL) {
8084 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8085 progname, fname, strerror(errno));
8090 rc = llapi_layout_flags_get(layout, &flr_state);
8092 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8093 progname, fname, strerror(errno));
8098 flr_state &= LCM_FL_FLR_MASK;
8099 if (flr_state == LCM_FL_NONE) {
8101 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8106 /* get stale component info */
8107 comp_size = llapi_mirror_find_stale(layout, comp_array,
8108 ARRAY_SIZE(comp_array),
8109 mirror_ids, ids_nr);
8110 if (comp_size <= 0) {
8115 ioc->lil_mode = LL_LEASE_WRLCK;
8116 ioc->lil_flags = LL_LEASE_RESYNC;
8117 rc = llapi_lease_set(fd, ioc);
8119 if (rc == -EALREADY)
8123 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8124 progname, fname, strerror(errno));
8128 /* get the read range [start, end) */
8129 start = comp_array[0].lrc_start;
8130 end = comp_array[0].lrc_end;
8131 for (idx = 1; idx < comp_size; idx++) {
8132 if (comp_array[idx].lrc_start < start)
8133 start = comp_array[idx].lrc_start;
8134 if (end < comp_array[idx].lrc_end)
8135 end = comp_array[idx].lrc_end;
8138 rc = llapi_lease_check(fd);
8139 if (rc != LL_LEASE_WRLCK) {
8140 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8145 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8148 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8149 progname, fname, rc);
8151 /* prepare ioc for lease put */
8152 ioc->lil_mode = LL_LEASE_UNLCK;
8153 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8155 for (idx = 0; idx < comp_size; idx++) {
8156 if (comp_array[idx].lrc_synced) {
8157 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8162 rc = llapi_lease_set(fd, ioc);
8164 if (rc == 0) /* lost lease lock */
8166 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8167 progname, fname, strerror(errno));
8171 * llapi_lease_set returns lease mode when it request to unlock
8177 llapi_layout_free(layout);
8184 static inline int lfs_mirror_resync(int argc, char **argv)
8186 struct ll_ioc_lease *ioc = NULL;
8187 __u16 mirror_ids[128] = { 0 };
8192 struct option long_opts[] = {
8193 { .val = 'o', .name = "only", .has_arg = required_argument },
8196 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8199 rc = parse_mirror_ids(mirror_ids,
8200 sizeof(mirror_ids) / sizeof(__u16),
8204 "%s: bad mirror ids '%s'.\n",
8211 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8212 argv[0], argv[optind - 1]);
8218 if (argc == optind) {
8219 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8224 if (ids_nr > 0 && argc > optind + 1) {
8226 "%s: option '--only' cannot be used upon multiple files.\n",
8234 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8239 /* set the lease on the file */
8240 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8242 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8243 argv[0], strerror(errno));
8248 for (; optind < argc; optind++) {
8249 rc = lfs_mirror_resync_file(argv[optind], ioc,
8250 mirror_ids, ids_nr);
8251 /* ignore previous file's error, continue with next file */
8254 memset(ioc, 0, sizeof(__u32) * 4096);
8263 * struct verify_chunk - Mirror chunk to be verified.
8264 * @chunk: [start, end) of the chunk.
8265 * @mirror_count: Number of mirror ids in @mirror_id array.
8266 * @mirror_id: Array of valid mirror ids that cover the chunk.
8268 struct verify_chunk {
8269 struct lu_extent chunk;
8270 unsigned int mirror_count;
8271 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
8275 * print_chunks() - Print chunk information.
8276 * @fname: Mirrored file name.
8277 * @chunks: Array of chunks.
8278 * @chunk_count: Number of chunks in @chunks array.
8280 * This function prints [start, end) of each chunk in @chunks
8281 * for mirrored file @fname, and also prints the valid mirror ids
8282 * that cover the chunk.
8287 void print_chunks(const char *fname, struct verify_chunk *chunks,
8293 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
8294 for (i = 0; i < chunk_count; i++) {
8295 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
8297 if (chunks[i].mirror_count == 0)
8298 fprintf(stdout, "\t[");
8300 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
8301 for (j = 1; j < chunks[i].mirror_count; j++)
8302 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
8304 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
8306 fprintf(stdout, "\n");
8310 * print_checksums() - Print CRC-32 checksum values.
8311 * @chunk: A chunk and its corresponding valid mirror ids.
8312 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
8314 * This function prints CRC-32 checksum values on @chunk for
8315 * each valid mirror that covers it.
8320 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
8325 "CRC-32 checksum value for chunk "DEXT":\n",
8326 PEXT(&chunk->chunk));
8327 for (i = 0; i < chunk->mirror_count; i++)
8328 fprintf(stdout, "Mirror %u:\t%#lx\n",
8329 chunk->mirror_id[i], crc[i]);
8330 fprintf(stdout, "\n");
8334 * filter_mirror_id() - Filter specified mirror ids.
8335 * @chunks: Array of chunks.
8336 * @chunk_count: Number of chunks in @chunks array.
8337 * @mirror_ids: Specified mirror ids to be verified.
8338 * @ids_nr: Number of specified mirror ids.
8340 * This function scans valid mirror ids that cover each chunk in @chunks
8341 * and filters specified mirror ids.
8346 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
8347 __u16 *mirror_ids, int ids_nr)
8352 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8353 unsigned int valid_count = 0;
8355 for (i = 0; i < chunk_count; i++) {
8356 if (chunks[i].mirror_count == 0)
8360 for (j = 0; j < ids_nr; j++) {
8361 for (k = 0; k < chunks[i].mirror_count; k++) {
8362 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
8363 valid_id[valid_count] = mirror_ids[j];
8370 memcpy(chunks[i].mirror_id, valid_id,
8371 sizeof(__u16) * valid_count);
8372 chunks[i].mirror_count = valid_count;
8377 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
8378 * @layout: Mirror component list.
8379 * @chunks: Array of chunks.
8380 * @chunks_size: Array size of @chunks.
8382 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
8383 * to find out chunk segments and store them in @chunks array.
8385 * The @mirror_id array in each element of @chunks will store the valid
8386 * mirror ids that cover the chunk. If a mirror component covering the
8387 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
8388 * will not be stored into the @mirror_id array, and the chunk for that
8389 * mirror will not be verified.
8391 * The @mirror_count in each element of @chunks will store the number of
8392 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
8393 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
8394 * indicates the chunk is valid in only one mirror. In both cases, the
8395 * chunk will not be verified.
8397 * Here is an example:
8400 * +------+-------------+--------------------+
8402 * +------+------+------+------+-------------+
8403 * | | S | S | | mirror2
8404 * +-------------+------+------+-------------+
8406 * prepared @chunks array will contain 5 elements:
8407 * (([0, 1M), [1, 2], 2),
8408 * ([1M, 2M), [1, 2], 2),
8409 * ([2M, 3M), [1], 1),
8410 * ([3M, 4M], [], 0),
8411 * ([4M, EOF), [2], 1))
8413 * Return: the actual array size of @chunks on success
8414 * or a negative error code on failure.
8417 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
8418 struct verify_chunk *chunks,
8429 memset(chunks, 0, sizeof(*chunks) * chunks_size);
8432 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
8435 "%s: move to the first layout component: %s.\n",
8436 progname, strerror(errno));
8442 chunks[idx].chunk.e_end = LUSTRE_EOF;
8444 rc = llapi_layout_comp_extent_get(layout, &start, &end);
8447 "%s: llapi_layout_comp_extent_get failed: %s.\n",
8448 progname, strerror(errno));
8452 if (start > chunks[idx].chunk.e_start ||
8453 end <= chunks[idx].chunk.e_start)
8456 if (end < chunks[idx].chunk.e_end)
8457 chunks[idx].chunk.e_end = end;
8459 rc = llapi_layout_comp_flags_get(layout, &flags);
8462 "%s: llapi_layout_comp_flags_get failed: %s.\n",
8463 progname, strerror(errno));
8467 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8470 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8473 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8474 progname, strerror(errno));
8478 chunks[idx].mirror_id[i] = mirror_id;
8480 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8482 "%s: mirror_id array is too small.\n",
8489 rc = llapi_layout_comp_use(layout,
8490 LLAPI_LAYOUT_COMP_USE_NEXT);
8493 "%s: move to the next layout component: %s.\n",
8494 progname, strerror(errno));
8497 } /* loop through all components */
8499 chunks[idx].mirror_count = i;
8501 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8505 if (idx >= chunks_size) {
8506 fprintf(stderr, "%s: chunks array is too small.\n",
8512 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
8516 return rc < 0 ? rc : idx + 1;
8520 * lfs_mirror_verify_chunk() - Verify a chunk.
8521 * @fd: File descriptor of the mirrored file.
8522 * @file_size: Size of the mirrored file.
8523 * @chunk: A chunk and its corresponding valid mirror ids.
8524 * @verbose: Verbose mode.
8526 * This function verifies a @chunk contains exactly the same data
8527 * ammong the mirrors that cover it.
8529 * If @verbose is specified, then the function will print where the
8530 * differences are if the data do not match. Otherwise, it will
8531 * just return an error in that case.
8533 * Return: 0 on success or a negative error code on failure.
8536 int lfs_mirror_verify_chunk(int fd, size_t file_size,
8537 struct verify_chunk *chunk, int verbose)
8539 const size_t buflen = 4 * 1024 * 1024; /* 4M */
8541 size_t page_size = sysconf(_SC_PAGESIZE);
8547 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8554 rc = posix_memalign(&buf, page_size, buflen);
8555 if (rc) /* error code is returned directly */
8559 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8560 PEXT(&chunk->chunk));
8561 for (i = 0; i < chunk->mirror_count; i++)
8562 fprintf(stdout, " %u", chunk->mirror_id[i]);
8563 fprintf(stdout, "\n");
8567 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
8568 pos = chunk->chunk.e_start;
8569 while (bytes_done < count) {
8570 /* compute initial CRC-32 checksum */
8571 crc = crc32(0L, Z_NULL, 0);
8572 memset(crc_array, 0, sizeof(crc_array));
8575 for (i = 0; i < chunk->mirror_count; i++) {
8576 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
8578 if (bytes_read < 0) {
8581 "%s: failed to read data from mirror %u: %s.\n",
8582 progname, chunk->mirror_id[i],
8587 /* compute new CRC-32 checksum */
8588 crc_array[i] = crc32(crc, buf, bytes_read);
8592 print_checksums(chunk, crc_array);
8594 /* compare CRC-32 checksum values */
8595 for (i = 1; i < chunk->mirror_count; i++) {
8596 if (crc_array[i] != crc_array[0]) {
8602 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
8603 progname, PEXT(&chunk->chunk),
8604 chunk->mirror_id[0],
8605 chunk->mirror_id[i]);
8610 bytes_done += bytes_read;
8613 if (verbose > 1 && rc == 0) {
8614 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8615 PEXT(&chunk->chunk));
8616 for (i = 0; i < chunk->mirror_count; i++)
8617 fprintf(stdout, " %u", chunk->mirror_id[i]);
8618 fprintf(stdout, " PASS\n\n");
8627 * lfs_mirror_verify_file() - Verify a mirrored file.
8628 * @fname: Mirrored file name.
8629 * @mirror_ids: Specified mirror ids to be verified.
8630 * @ids_nr: Number of specified mirror ids.
8631 * @verbose: Verbose mode.
8633 * This function verifies that each SYNC mirror of a mirrored file
8634 * specified by @fname contains exactly the same data.
8636 * If @mirror_ids is specified, then the function will verify the
8637 * mirrors specified by @mirror_ids contain exactly the same data.
8639 * If @verbose is specified, then the function will print where the
8640 * differences are if the data do not match. Otherwise, it will
8641 * just return an error in that case.
8643 * Return: 0 on success or a negative error code on failure.
8646 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8649 struct verify_chunk chunks_array[1024] = { };
8650 struct llapi_layout *layout = NULL;
8654 int chunk_count = 0;
8660 if (stat(fname, &stbuf) < 0) {
8661 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8662 progname, fname, strerror(errno));
8667 if (!S_ISREG(stbuf.st_mode)) {
8668 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8674 if (stbuf.st_size == 0) {
8676 fprintf(stdout, "%s: '%s' file size is 0.\n",
8682 fd = open(fname, O_DIRECT | O_RDONLY);
8684 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8685 progname, fname, strerror(errno));
8690 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8692 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8693 progname, fname, strerror(errno));
8697 layout = llapi_layout_get_by_fd(fd, 0);
8698 if (layout == NULL) {
8699 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8700 progname, fname, strerror(errno));
8702 llapi_lease_release(fd);
8706 rc = llapi_layout_flags_get(layout, &flr_state);
8708 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8709 progname, fname, strerror(errno));
8714 flr_state &= LCM_FL_FLR_MASK;
8715 switch (flr_state) {
8718 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8719 progname, fname, llapi_layout_flags_string(flr_state));
8725 /* find out mirror chunks to be verified */
8726 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8727 ARRAY_SIZE(chunks_array));
8728 if (chunk_count < 0) {
8734 /* filter specified mirror ids */
8735 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8738 print_chunks(fname, chunks_array, chunk_count);
8740 for (idx = 0; idx < chunk_count; idx++) {
8741 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8744 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8746 PEXT(&chunks_array[idx].chunk),
8747 (unsigned long long)stbuf.st_size);
8751 if (chunks_array[idx].mirror_count == 0) {
8753 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8755 PEXT(&chunks_array[idx].chunk));
8757 fprintf(stderr, "skipped\n");
8761 fprintf(stderr, "failed\n");
8765 if (chunks_array[idx].mirror_count == 1) {
8768 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8770 PEXT(&chunks_array[idx].chunk),
8771 chunks_array[idx].mirror_id[0]);
8775 rc = llapi_lease_check(fd);
8776 if (rc != LL_LEASE_RDLCK) {
8777 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8782 /* verify one chunk */
8783 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8784 &chunks_array[idx], verbose);
8798 llapi_layout_free(layout);
8799 llapi_lease_release(fd);
8807 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
8808 * @argc: The count of lfs mirror verify command line arguments.
8809 * @argv: Array of strings for lfs mirror verify command line arguments.
8811 * This function parses lfs mirror verify command and verifies the
8812 * specified mirrored file(s).
8814 * Return: 0 on success or a negative error code on failure.
8816 static inline int lfs_mirror_verify(int argc, char **argv)
8818 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8826 struct option long_opts[] = {
8827 { .val = 'o', .name = "only", .has_arg = required_argument },
8828 { .val = 'v', .name = "verbose", .has_arg = no_argument },
8831 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8833 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
8836 rc = parse_mirror_ids(mirror_ids,
8837 ARRAY_SIZE(mirror_ids),
8841 "%s: bad mirror ids '%s'.\n",
8848 "%s: at least 2 mirror ids needed with '--only' option.\n",
8858 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8859 progname, argv[optind - 1]);
8865 if (argc == optind) {
8866 fprintf(stderr, "%s: no file name given.\n", progname);
8871 if (ids_nr > 0 && argc > optind + 1) {
8873 "%s: '--only' cannot be used upon multiple files.\n",
8881 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8887 for (; optind < argc; optind++) {
8888 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
8898 * lfs_mirror() - Parse and execute lfs mirror commands.
8899 * @argc: The count of lfs mirror command line arguments.
8900 * @argv: Array of strings for lfs mirror command line arguments.
8902 * This function parses lfs mirror commands and performs the
8903 * corresponding functions specified in mirror_cmdlist[].
8905 * Return: 0 on success or an error code on failure.
8907 static int lfs_mirror(int argc, char **argv)
8914 Parser_init("lfs-mirror > ", mirror_cmdlist);
8916 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8918 program_invocation_short_name = cmd;
8920 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
8922 rc = Parser_commands();
8924 return rc < 0 ? -rc : rc;
8927 static void lustre_som_swab(struct lustre_som_attrs *attrs)
8929 #if __BYTE_ORDER == __BIG_ENDIAN
8930 __swab16s(&attrs->lsa_valid);
8931 __swab64s(&attrs->lsa_size);
8932 __swab64s(&attrs->lsa_blocks);
8938 LFS_SOM_BLOCKS = 0x2,
8939 LFS_SOM_FLAGS = 0x4,
8940 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
8944 static int lfs_getsom(int argc, char **argv)
8947 struct lustre_som_attrs *attrs;
8948 char buf[sizeof(*attrs) + 64];
8949 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
8952 while ((c = getopt(argc, argv, "sbf")) != -1) {
8955 type = LFS_SOM_SIZE;
8958 type = LFS_SOM_BLOCKS;
8961 type = LFS_SOM_FLAGS;
8964 fprintf(stderr, "%s: invalid option '%c'\n",
8974 fprintf(stderr, "%s: %s\n",
8975 progname, argc == 0 ? "miss file target" :
8976 "input more than 2 files");
8981 attrs = (void *)buf;
8982 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
8984 fprintf(stderr, "%s failed to get som xattr: %s\n", argv[0],
8989 lustre_som_swab(attrs);
8992 case LFS_SOM_ATTR_ALL:
8993 printf("file: %s size: %llu blocks: %llu flags: %x\n",
8994 path, attrs->lsa_size, attrs->lsa_blocks,
8998 printf("%llu\n", attrs->lsa_size);
9000 case LFS_SOM_BLOCKS:
9001 printf("%llu\n", attrs->lsa_blocks);
9004 printf("%x\n", attrs->lsa_valid);
9007 fprintf(stderr, "%s: unknown option\n", progname);
9015 * lfs_mirror_list_commands() - List lfs mirror commands.
9016 * @argc: The count of command line arguments.
9017 * @argv: Array of strings for command line arguments.
9019 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9021 * Return: 0 on success.
9023 static int lfs_mirror_list_commands(int argc, char **argv)
9025 char buffer[81] = "";
9027 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9033 static int lfs_list_commands(int argc, char **argv)
9035 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9037 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9042 int main(int argc, char **argv)
9046 /* Ensure that liblustreapi constructor has run */
9047 if (!llapi_liblustreapi_initialized())
9048 fprintf(stderr, "liblustreapi was not properly initialized\n");
9053 Parser_init("lfs > ", cmdlist);
9055 progname = program_invocation_short_name; /* Used in error messages */
9057 llapi_set_command_name(argv[1]);
9058 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9059 llapi_clear_command_name();
9061 rc = Parser_commands();
9064 return rc < 0 ? -rc : rc;
9067 #ifdef _LUSTRE_IDL_H_
9068 /* Everything we need here should be included by lustreapi.h. */
9069 # error "lfs should not depend on lustre_idl.h"
9070 #endif /* _LUSTRE_IDL_H_ */