4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
66 #include "lfs_project.h"
68 #include <libcfs/util/string.h>
69 #include <libcfs/util/ioctl.h>
70 #include <libcfs/util/parser.h>
71 #include <lustre/lustreapi.h>
72 #include <linux/lustre/lustre_ver.h>
73 #include <linux/lustre/lustre_param.h>
74 #include <linux/lnet/nidstr.h>
77 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
78 #endif /* !ARRAY_SIZE */
81 static int lfs_find(int argc, char **argv);
82 static int lfs_getstripe(int argc, char **argv);
83 static int lfs_getdirstripe(int argc, char **argv);
84 static int lfs_setdirstripe(int argc, char **argv);
85 static int lfs_rmentry(int argc, char **argv);
86 static int lfs_osts(int argc, char **argv);
87 static int lfs_mdts(int argc, char **argv);
88 static int lfs_df(int argc, char **argv);
89 static int lfs_getname(int argc, char **argv);
90 static int lfs_check(int argc, char **argv);
91 #ifdef HAVE_SYS_QUOTA_H
92 static int lfs_setquota(int argc, char **argv);
93 static int lfs_quota(int argc, char **argv);
94 static int lfs_project(int argc, char **argv);
96 static int lfs_flushctx(int argc, char **argv);
97 static int lfs_cp(int argc, char **argv);
98 static int lfs_ls(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_data_version(int argc, char **argv);
105 static int lfs_hsm_state(int argc, char **argv);
106 static int lfs_hsm_set(int argc, char **argv);
107 static int lfs_hsm_clear(int argc, char **argv);
108 static int lfs_hsm_action(int argc, char **argv);
109 static int lfs_hsm_archive(int argc, char **argv);
110 static int lfs_hsm_restore(int argc, char **argv);
111 static int lfs_hsm_release(int argc, char **argv);
112 static int lfs_hsm_remove(int argc, char **argv);
113 static int lfs_hsm_cancel(int argc, char **argv);
114 static int lfs_swap_layouts(int argc, char **argv);
115 static int lfs_mv(int argc, char **argv);
116 static int lfs_ladvise(int argc, char **argv);
117 static int lfs_mirror(int argc, char **argv);
118 static int lfs_mirror_list_commands(int argc, char **argv);
119 static int lfs_list_commands(int argc, char **argv);
120 static inline int lfs_mirror_resync(int argc, char **argv);
121 static inline int lfs_mirror_verify(int argc, char **argv);
123 enum setstripe_origin {
130 static int lfs_setstripe_internal(int argc, char **argv,
131 enum setstripe_origin opc);
133 static inline int lfs_setstripe(int argc, char **argv)
135 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
137 static inline int lfs_setstripe_migrate(int argc, char **argv)
139 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
141 static inline int lfs_mirror_create(int argc, char **argv)
143 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
145 static inline int lfs_mirror_extend(int argc, char **argv)
147 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
149 static inline int lfs_mirror_split(int argc, char **argv)
151 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
154 /* Setstripe and migrate share mostly the same parameters */
155 #define SSM_CMD_COMMON(cmd) \
156 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
157 " [--stripe-count|-c <stripe_count>]\n" \
158 " [--stripe-index|-i <start_ost_idx>]\n" \
159 " [--stripe-size|-S <stripe_size>]\n" \
160 " [--layout|-L <pattern>]\n" \
161 " [--pool|-p <pool_name>]\n" \
162 " [--ost|-o <ost_indices>]\n"
164 #define SSM_HELP_COMMON \
165 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
166 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
167 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
168 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
169 "\t respectively)\n" \
170 "\tpool_name: Name of OST pool to use (default none)\n" \
171 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
172 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
173 "\t Indices be specified in a format of:\n" \
174 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
176 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
177 "\t If --pool is set with --ost, then the OSTs\n" \
178 "\t must be the members of the pool.\n" \
179 "\tcomp_end: Extent end of component, start after previous end.\n"\
180 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
181 "\t respectively, -1 for EOF). Must be a multiple of\n"\
184 #define MIRROR_CREATE_HELP \
185 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
186 "\t setstripe layout options\n" \
187 "\t It defaults to 1 if not specified; if specified,\n" \
188 "\t it must follow the option without a space.\n" \
189 "\t The option can also be repeated multiple times to\n" \
190 "\t separate mirrors that have different layouts.\n" \
191 "\tsetstripe options: Mirror layout\n" \
192 "\t It can be a plain layout or a composite layout.\n" \
193 "\t If not specified, the stripe options inherited\n" \
194 "\t from the previous component will be used.\n" \
195 "\tparent: Use default stripe options from parent directory\n" \
196 "\tflags: set flags to the component of the current mirror.\n" \
197 "\t Only \"prefer\" flag is supported so far.\n"
199 #define MIRROR_EXTEND_HELP \
201 "\tvictim_file: The layout of victim_file will be split and used\n" \
202 "\t as a mirror added to the mirrored file.\n" \
203 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
204 "\t from victim file(s) in case the victim file(s)\n" \
205 "\t contains the same data as the original mirrored\n" \
208 #define MIRROR_EXTEND_USAGE \
209 " <--mirror-count|-N[mirror_count]>\n" \
210 " [setstripe options|--parent|-f <victim_file>]\n" \
213 #define SETSTRIPE_USAGE \
214 SSM_CMD_COMMON("setstripe") \
215 MIRROR_EXTEND_USAGE \
216 " <directory|filename>\n" \
220 #define MIGRATE_USAGE \
221 SSM_CMD_COMMON("migrate ") \
223 " [--non-block|-n]\n" \
227 "\tblock: Block file access during data migration (default)\n" \
228 "\tnon-block: Abort migrations if concurrent access is detected\n" \
230 #define SETDIRSTRIPE_USAGE \
231 " [--mdt-count|-c stripe_count>\n" \
232 " [--mdt-index|-i mdt_index]\n" \
233 " [--mdt-hash|-H mdt_hash]\n" \
234 " [--default|-D] [--mode|-m mode] <dir>\n" \
235 "\tstripe_count: stripe count of the striped directory\n" \
236 "\tmdt_index: MDT index of first stripe\n" \
237 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
238 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
239 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
240 "\tdefault_stripe: set default dirstripe of the directory\n" \
241 "\tmode: the mode of the directory\n"
244 * command_t mirror_cmdlist - lfs mirror commands.
246 command_t mirror_cmdlist[] = {
247 { .pc_name = "create", .pc_func = lfs_mirror_create,
248 .pc_help = "Create a mirrored file.\n"
249 "usage: lfs mirror create "
250 "<--mirror-count|-N[mirror_count]> "
251 "[setstripe options|--parent] ... <filename|directory>\n"
252 MIRROR_CREATE_HELP },
253 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
254 .pc_help = "Extend a mirrored file.\n"
255 "usage: lfs mirror extend "
256 "<--mirror-count|-N[mirror_count]> [--no-verify] "
257 "[setstripe options|--parent|-f <victim_file>] ... <filename>\n"
258 MIRROR_EXTEND_HELP },
259 { .pc_name = "split", .pc_func = lfs_mirror_split,
260 .pc_help = "Split a mirrored file.\n"
261 "usage: lfs mirror split <--mirror-id <mirror_id>> [--destroy|-d] "
262 "[-f <new_file>] <mirrored file>\n"
263 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
264 "\t can be fetched by lfs getstripe command.\n"
265 "\tnew_file: This option indicates the layout of the split\n"
266 "\t mirror will be stored into. If not specified,\n"
267 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
268 "\t will be used.\n" },
269 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
270 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
271 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
272 "<mirrored file> [<mirrored file2>...]\n"},
273 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
274 .pc_help = "Verify mirrored file(s).\n"
275 "usage: lfs mirror verify "
276 "[--only <mirror_id,mirror_id2[,...]>] "
277 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
278 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
279 .pc_help = "list commands supported by lfs mirror"},
280 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
281 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
282 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
286 /* all available commands */
287 command_t cmdlist[] = {
288 {"setstripe", lfs_setstripe, 0,
289 "To create a file with specified striping/composite layout, or\n"
290 "create/replace the default layout on an existing directory:\n"
291 SSM_CMD_COMMON("setstripe")
292 " <directory|filename>\n"
294 "To add component(s) to an existing composite file:\n"
295 SSM_CMD_COMMON("setstripe --component-add")
297 "To totally delete the default striping from an existing directory:\n"
298 "usage: setstripe -d <directory>\n"
300 "To delete the last component(s) from an existing composite file\n"
301 "(note that this will also delete any data in those components):\n"
302 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
303 " [--component-flags|-F <comp_flags>]\n"
305 "\tcomp_id: Unique component ID to delete\n"
306 "\tcomp_flags: 'init' indicating all instantiated components\n"
307 "\t '^init' indicating all uninstantiated components\n"
308 "\t-I and -F cannot be specified at the same time\n"},
309 {"getstripe", lfs_getstripe, 0,
310 "To list the layout pattern for a given file or files in a\n"
311 "directory or recursively for all files in a directory tree.\n"
312 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
313 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
314 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
315 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
316 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
317 " [--component-id[=comp_id]|-I[comp_id]]\n"
318 " [--component-flags[=comp_flags]]\n"
319 " [--component-count]\n"
320 " [--component-start[=[+-]comp_start]]\n"
321 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
322 " <directory|filename> ..."},
323 {"setdirstripe", lfs_setdirstripe, 0,
324 "To create a striped directory on a specified MDT. This can only\n"
325 "be done on MDT0 with the right of administrator.\n"
326 "usage: setdirstripe [OPTION] <directory>\n"
328 {"getdirstripe", lfs_getdirstripe, 0,
329 "To list the layout pattern info for a given directory\n"
330 "or recursively for all directories in a directory tree.\n"
331 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
332 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
333 " [--recursive|-r] [--yaml|-y]\n"
334 " [--default|-D] <dir> ..."},
335 {"mkdir", lfs_setdirstripe, 0,
336 "To create a striped directory on a specified MDT. This can only\n"
337 "be done on MDT0 with the right of administrator.\n"
338 "usage: mkdir [OPTION] <directory>\n"
340 {"rm_entry", lfs_rmentry, 0,
341 "To remove the name entry of the remote directory. Note: This\n"
342 "command will only delete the name entry, i.e. the remote directory\n"
343 "will become inaccessable after this command. This can only be done\n"
344 "by the administrator\n"
345 "usage: rm_entry <dir>\n"},
346 {"pool_list", lfs_poollist, 0,
347 "List pools or pool OSTs\n"
348 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
349 {"find", lfs_find, 0,
350 "find files matching given attributes recursively in directory tree.\n"
351 "usage: find <directory|filename> ...\n"
352 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
353 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N]\n"
354 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
355 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
356 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
357 " [[!] --stripe-count|-c [+-]<stripes>]\n"
358 " [[!] --stripe-index|-i <index,...>]\n"
359 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
360 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
361 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
362 " [[!] --projid <projid>]\n"
363 " [[!] --layout|-L released,raid0,mdt]\n"
364 " [[!] --component-count [+-]<comp_cnt>]\n"
365 " [[!] --component-start [+-]N[kMGTPE]]\n"
366 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
367 " [[!] --component-flags <comp_flags>]\n"
368 " [[!] --mdt-count|-T [+-]<stripes>]\n"
369 " [[!] --mdt-hash|-H <hashtype>\n"
370 "\t !: used before an option indicates 'NOT' requested attribute\n"
371 "\t -: used before a value indicates less than requested value\n"
372 "\t +: used before a value indicates more than requested value\n"
373 "\thashtype: hash type of the striped directory.\n"
374 "\t fnv_1a_64 FNV-1a hash algorithm\n"
375 "\t all_char sum of characters % MDT_COUNT\n"},
376 {"check", lfs_check, 0,
377 "Display the status of MDS or OSTs (as specified in the command)\n"
378 "or all the servers (MDS and OSTs).\n"
379 "usage: check <osts|mds|servers>"},
380 {"osts", lfs_osts, 0, "list OSTs connected to client "
381 "[for specified path only]\n" "usage: osts [path]"},
382 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
383 "[for specified path only]\n" "usage: mdts [path]"},
385 "report filesystem disk space usage or inodes usage"
386 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
387 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
388 {"getname", lfs_getname, 0, "list instances and specified mount points "
389 "[for specified path only]\n"
390 "Usage: getname [-h]|[path ...] "},
391 #ifdef HAVE_SYS_QUOTA_H
392 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
393 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
394 " -b <block-softlimit> -B <block-hardlimit>\n"
395 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
396 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
397 " [--block-softlimit <block-softlimit>]\n"
398 " [--block-hardlimit <block-hardlimit>]\n"
399 " [--inode-softlimit <inode-softlimit>]\n"
400 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
401 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
402 " [--block-grace <block-grace>]\n"
403 " [--inode-grace <inode-grace>] <filesystem>\n"
404 " -b can be used instead of --block-softlimit/--block-grace\n"
405 " -B can be used instead of --block-hardlimit\n"
406 " -i can be used instead of --inode-softlimit/--inode-grace\n"
407 " -I can be used instead of --inode-hardlimit\n\n"
408 "Note: The total quota space will be split into many qunits and\n"
409 " balanced over all server targets, the minimal qunit size is\n"
410 " 1M bytes for block space and 1K inodes for inode space.\n\n"
411 " Quota space rebalancing process will stop when this mininum\n"
412 " value is reached. As a result, quota exceeded can be returned\n"
413 " while many targets still have 1MB or 1K inodes of spare\n"
415 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
416 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
418 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
419 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
420 {"project", lfs_project, 0,
421 "Change or list project attribute for specified file or directory.\n"
422 "usage: project [-d|-r] <file|directory...>\n"
423 " list project ID and flags on file(s) or directories\n"
424 " project [-p id] [-s] [-r] <file|directory...>\n"
425 " set project ID and/or inherit flag for specified file(s) or directories\n"
426 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
427 " check project ID and flags on file(s) or directories, print outliers\n"
428 " project -C [-r] [-k] <file|directory...>\n"
429 " clear the project inherit flag and ID on the file or directory\n"
432 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
433 "usage: flushctx [-k] [mountpoint...]"},
435 "Remote user copy files and directories.\n"
436 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
438 "Remote user list directory contents.\n"
439 "usage: ls [OPTION]... [FILE]..."},
440 {"changelog", lfs_changelog, 0,
441 "Show the metadata changes on an MDT."
442 "\nusage: changelog <mdtname> [startrec [endrec]]"},
443 {"changelog_clear", lfs_changelog_clear, 0,
444 "Indicate that old changelog records up to <endrec> are no longer of "
445 "interest to consumer <id>, allowing the system to free up space.\n"
446 "An <endrec> of 0 means all records.\n"
447 "usage: changelog_clear <mdtname> <id> <endrec>"},
448 {"fid2path", lfs_fid2path, 0,
449 "Resolve the full path(s) for given FID(s). For a specific hardlink "
450 "specify link number <linkno>.\n"
451 /* "For a historical link name, specify changelog record <recno>.\n" */
452 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
453 /* [ --rec <recno> ] */ },
454 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
455 "usage: path2fid [--parents] <path> ..."},
456 {"data_version", lfs_data_version, 0, "Display file data version for "
457 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
458 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
459 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
460 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
461 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
462 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
463 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
465 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
466 "[--archived] [--lost] <file> ..."},
467 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
468 "given files.\n" "usage: hsm_action <file> ..."},
469 {"hsm_archive", lfs_hsm_archive, 0,
470 "Archive file to external storage.\n"
471 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
473 {"hsm_restore", lfs_hsm_restore, 0,
474 "Restore file from external storage.\n"
475 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
476 {"hsm_release", lfs_hsm_release, 0,
477 "Release files from Lustre.\n"
478 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
479 {"hsm_remove", lfs_hsm_remove, 0,
480 "Remove file copy from external storage.\n"
481 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
483 " (FILE [FILE ...] | "
484 "--mntpath MOUNTPATH FID [FID ...])\n"
486 "Note: To remove an archived copy of a file already deleted from a "
488 "--mntpath option and a list of FIDs must be specified"
490 {"hsm_cancel", lfs_hsm_cancel, 0,
491 "Cancel requests related to specified files.\n"
492 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
493 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
494 "usage: swap_layouts <path1> <path2>"},
495 {"migrate", lfs_setstripe_migrate, 0,
496 "migrate a directory between MDTs.\n"
497 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
499 "\tmdt_idx: index of the destination MDT\n"
501 "migrate file objects from one OST "
502 "layout\nto another (may be not safe with concurent writes).\n"
504 "[--stripe-count|-c] <stripe_count>\n"
505 " [--stripe-index|-i] <start_ost_index>\n"
506 " [--stripe-size|-S] <stripe_size>\n"
507 " [--pool|-p] <pool_name>\n"
508 " [--ost|-o] <ost_indices>\n"
510 " [--non-block|-n]\n"
511 " <file|directory>\n"
512 "\tstripe_count: number of OSTs to stripe a file over\n"
513 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
514 "\tstripe_size: number of bytes to store before moving to the next OST\n"
515 "\tpool_name: name of the predefined pool of OSTs\n"
516 "\tost_indices: OSTs to stripe over, in order\n"
517 "\tblock: wait for the operation to return before continuing\n"
518 "\tnon-block: do not wait for the operation to return.\n"},
520 "To move directories between MDTs. This command is deprecated, "
521 "use \"migrate\" instead.\n"
522 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
524 {"ladvise", lfs_ladvise, 0,
525 "Provide servers with advice about access patterns for a file.\n"
526 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
527 " [--background|-b] [--unset|-u]\n\n"
528 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
529 " {[--mode|-m [READ,WRITE]}\n"
531 {"mirror", lfs_mirror, mirror_cmdlist,
532 "lfs commands used to manage files with mirrored components:\n"
533 "lfs mirror create - create a mirrored file or directory\n"
534 "lfs mirror extend - add mirror(s) to an existing file\n"
535 "lfs mirror split - split a mirror from an existing mirrored file\n"
536 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
537 "lfs mirror verify - verify mirrored file(s)\n"},
538 {"help", Parser_help, 0, "help"},
539 {"exit", Parser_quit, 0, "quit"},
540 {"quit", Parser_quit, 0, "quit"},
541 {"--version", Parser_version, 0,
542 "output build version of the utility and exit"},
543 {"--list-commands", lfs_list_commands, 0,
544 "list commands supported by the utility and exit"},
549 static int check_hashtype(const char *hashtype)
553 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
554 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
561 static const char *error_loc = "syserror";
564 MIGRATION_NONBLOCK = 1 << 0,
565 MIGRATION_MIRROR = 1 << 1,
568 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
569 struct llapi_layout *layout);
572 migrate_open_files(const char *name, const struct llapi_stripe_param *param,
573 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
579 char parent[PATH_MAX];
580 char volatile_file[PATH_MAX];
586 if (param == NULL && layout == NULL) {
587 error_loc = "layout information";
591 /* search for file directory pathname */
592 if (strlen(name) > sizeof(parent) - 1) {
593 error_loc = "source file name";
597 strncpy(parent, name, sizeof(parent));
598 ptr = strrchr(parent, '/');
600 if (getcwd(parent, sizeof(parent)) == NULL) {
601 error_loc = "getcwd";
605 if (ptr == parent) /* leading '/' */
610 /* open file, direct io */
611 /* even if the file is only read, WR mode is nedeed to allow
612 * layout swap on fd */
613 fd = open(name, O_RDWR | O_DIRECT);
616 error_loc = "cannot open source file";
620 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
622 error_loc = "cannot get MDT index";
627 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
628 mode_t open_mode = S_IRUSR | S_IWUSR;
630 random_value = random();
631 rc = snprintf(volatile_file, sizeof(volatile_file),
632 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
633 mdt_index, random_value);
634 if (rc >= sizeof(volatile_file)) {
639 /* create, open a volatile file, use caching (ie no directio) */
641 fdv = llapi_file_open_param(volatile_file, open_flags,
644 fdv = lfs_component_create(volatile_file, open_flags,
646 } while (fdv < 0 && (rc = fdv) == -EEXIST);
649 error_loc = "cannot create volatile file";
653 /* In case the MDT does not support creation of volatile files
654 * we should try to unlink it. */
655 (void)unlink(volatile_file);
657 /* Not-owner (root?) special case.
658 * Need to set owner/group of volatile file like original.
659 * This will allow to pass related check during layout_swap.
664 error_loc = "cannot stat source file";
668 rc = fstat(fdv, &stv);
671 error_loc = "cannot stat volatile";
675 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
676 rc = fchown(fdv, st.st_uid, st.st_gid);
679 error_loc = "cannot change ownwership of volatile";
698 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
700 struct llapi_layout *layout;
701 size_t buf_size = 4 * 1024 * 1024;
710 layout = llapi_layout_get_by_fd(fd_src, 0);
711 if (layout != NULL) {
712 uint64_t stripe_size;
714 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
716 buf_size = stripe_size;
718 llapi_layout_free(layout);
721 /* Use a page-aligned buffer for direct I/O */
722 rc = posix_memalign(&buf, getpagesize(), buf_size);
727 /* read new data only if we have written all
728 * previously read data */
731 rc = check_file(fd_src);
736 rsize = read(fd_src, buf, buf_size);
748 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
767 static int migrate_copy_timestamps(int fd, int fdv)
771 if (fstat(fd, &st) == 0) {
772 struct timeval tv[2] = {
773 {.tv_sec = st.st_atime},
774 {.tv_sec = st.st_mtime}
777 return futimes(fdv, tv);
783 static int migrate_block(int fd, int fdv)
790 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
792 error_loc = "cannot get dataversion";
800 /* The grouplock blocks all concurrent accesses to the file.
801 * It has to be taken after llapi_get_data_version as it would
803 rc = llapi_group_lock(fd, gid);
805 error_loc = "cannot get group lock";
809 rc = migrate_copy_data(fd, fdv, NULL);
811 error_loc = "data copy failed";
815 /* Make sure we keep original atime/mtime values */
816 rc = migrate_copy_timestamps(fd, fdv);
818 error_loc = "timestamp copy failed";
823 * for a migration we need to check data version on file did
826 * Pass in gid=0 since we already own grouplock. */
827 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
828 SWAP_LAYOUTS_CHECK_DV1);
830 error_loc = "file changed";
833 error_loc = "cannot swap layout";
838 rc2 = llapi_group_unlock(fd, gid);
839 if (rc2 < 0 && rc == 0) {
840 error_loc = "unlock group lock";
848 * Internal helper for migrate_copy_data(). Check lease and report error if
851 * \param[in] fd File descriptor on which to check the lease.
853 * \retval 0 Migration can keep on going.
854 * \retval -errno Error occurred, abort migration.
856 static int check_lease(int fd)
860 rc = llapi_lease_check(fd);
862 return 0; /* llapi_check_lease returns > 0 on success. */
867 static int migrate_nonblock(int fd, int fdv)
873 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
875 error_loc = "cannot get data version";
879 rc = migrate_copy_data(fd, fdv, check_lease);
881 error_loc = "data copy failed";
885 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
887 error_loc = "cannot get data version";
893 error_loc = "source file changed";
897 /* Make sure we keep original atime/mtime values */
898 rc = migrate_copy_timestamps(fd, fdv);
900 error_loc = "timestamp copy failed";
907 static int lfs_component_set(char *fname, int comp_id,
908 __u32 flags, __u32 neg_flags)
911 __u32 flags_array[2];
916 ids[count] = comp_id;
917 flags_array[count] = flags;
922 ids[count] = comp_id;
923 flags_array[count] = neg_flags | LCME_FL_NEG;
927 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
930 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
931 progname, comp_id, fname, flags, neg_flags);
936 static int lfs_component_del(char *fname, __u32 comp_id,
937 __u32 flags, __u32 neg_flags)
941 if (flags && neg_flags)
944 if (!flags && neg_flags)
945 flags = neg_flags | LCME_FL_NEG;
947 if ((flags && comp_id) || (!flags && !comp_id))
950 /* LCME_FL_INIT is the only supported flag in PFL */
952 if (flags & ~LCME_KNOWN_FLAGS) {
954 "%s setstripe: unknown flags %#x\n",
958 } else if (comp_id > LCME_ID_MAX) {
959 fprintf(stderr, "%s setstripe: invalid component id %u\n",
964 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
967 "%s setstripe: cannot delete component %#x from '%s': %s\n",
968 progname, comp_id, fname, strerror(errno));
972 static int lfs_component_add(char *fname, struct llapi_layout *layout)
979 rc = llapi_layout_file_comp_add(fname, layout);
981 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
982 fname, strerror(errno));
986 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
987 struct llapi_layout *layout)
995 fd = lstat(fname, &st);
996 if (fd == 0 && S_ISDIR(st.st_mode))
997 open_flags = O_DIRECTORY | O_RDONLY;
999 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1001 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1002 S_ISDIR(st.st_mode) ?
1003 "set default composite layout for" :
1004 "create composite file",
1005 fname, strerror(errno));
1009 static int lfs_migrate(char *name, __u64 migration_flags,
1010 struct llapi_stripe_param *param,
1011 struct llapi_layout *layout)
1017 rc = migrate_open_files(name, param, layout, &fd, &fdv);
1021 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1022 /* Blocking mode (forced if servers do not support file lease).
1023 * It is also the default mode, since we cannot distinguish
1024 * between a broken lease and a server that does not support
1025 * atomic swap/close (LU-6785) */
1026 rc = migrate_block(fd, fdv);
1030 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1032 error_loc = "cannot get lease";
1036 rc = migrate_nonblock(fd, fdv);
1038 llapi_lease_release(fd);
1042 /* Atomically put lease, swap layouts and close.
1043 * for a migration we need to check data version on file did
1045 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1047 error_loc = "cannot swap layout";
1059 fprintf(stderr, "error: %s: %s: %s: %s\n",
1060 progname, name, error_loc, strerror(-rc));
1064 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1073 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1077 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1078 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1079 const char *comp_name = comp_flags_table[i].cfn_name;
1081 if (strcmp(name, comp_name) == 0) {
1082 *flags |= comp_flag;
1084 } else if (strncmp(name, "^", 1) == 0 &&
1085 strcmp(name + 1, comp_name) == 0) {
1086 *neg_flags |= comp_flag;
1091 llapi_printf(LLAPI_MSG_ERROR,
1092 "%s: component flag '%s' not supported\n",
1098 if (!*flags && !*neg_flags)
1101 /* don't allow to set and exclude the same flag */
1102 if (*flags & *neg_flags)
1109 * struct mirror_args - Command-line arguments for mirror(s).
1110 * @m_count: Number of mirrors to be created with this layout.
1111 * @m_flags: Mirror level flags, only 'prefer' is supported.
1112 * @m_layout: Mirror layout.
1113 * @m_file: A victim file. Its layout will be split and used as a mirror.
1114 * @m_next: Point to the next node of the list.
1116 * Command-line arguments for mirror(s) will be parsed and stored in
1117 * a linked list that consists of this structure.
1119 struct mirror_args {
1122 struct llapi_layout *m_layout;
1124 struct mirror_args *m_next;
1127 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1132 rc = llapi_layout_comp_flags_get(layout, &flags);
1136 if (flags & LCME_FL_NEG) {
1137 fprintf(stderr, "error: %s: negative flags are not supported\n",
1142 if (flags & LCME_FL_STALE) {
1143 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1144 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1148 return LLAPI_LAYOUT_ITER_CONT;
1151 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1153 uint64_t start, end;
1157 /* LU-10112: do not support dom+flr in phase 1 */
1158 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1162 rc = llapi_layout_pattern_get(layout, &pattern);
1166 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1167 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1172 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1176 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1180 if (end != LUSTRE_EOF) {
1181 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1186 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1191 * enum mirror_flags - Flags for extending a mirrored file.
1192 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1193 * in case the victim file(s) contains the same data as the
1194 * original mirrored file.
1195 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1197 * Flags for extending a mirrored file.
1205 * mirror_create_sanity_check() - Check mirror list.
1206 * @list: A linked list that stores the mirror arguments.
1208 * This function does a sanity check on @list for creating
1211 * Return: 0 on success or a negative error code on failure.
1213 static int mirror_create_sanity_check(const char *fname,
1214 struct mirror_args *list)
1217 bool has_m_file = false;
1218 bool has_m_layout = false;
1224 struct llapi_layout *layout;
1226 layout = llapi_layout_get_by_path(fname, 0);
1229 "error: %s: file '%s' couldn't get layout\n",
1234 rc = mirror_sanity_check_one(layout);
1235 llapi_layout_free(layout);
1241 while (list != NULL) {
1242 if (list->m_file != NULL) {
1244 llapi_layout_free(list->m_layout);
1247 llapi_layout_get_by_path(list->m_file, 0);
1248 if (list->m_layout == NULL) {
1250 "error: %s: file '%s' has no layout\n",
1251 progname, list->m_file);
1255 has_m_layout = true;
1256 if (list->m_layout == NULL) {
1257 fprintf(stderr, "error: %s: no mirror layout\n",
1263 rc = mirror_sanity_check_one(list->m_layout);
1267 list = list->m_next;
1270 if (has_m_file && has_m_layout) {
1271 fprintf(stderr, "error: %s: -f <victim_file> option should not "
1272 "be specified with setstripe options or "
1273 "--parent option\n", progname);
1280 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1282 __u32 mirror_flags = *(__u32 *)cbdata;
1286 rc = llapi_layout_comp_flags_get(layout, &flags);
1291 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1296 return LLAPI_LAYOUT_ITER_CONT;
1300 * mirror_create() - Create a mirrored file.
1301 * @fname: The file to be created.
1302 * @mirror_list: A linked list that stores the mirror arguments.
1304 * This function creates a mirrored file @fname with the mirror(s)
1305 * from @mirror_list.
1307 * Return: 0 on success or a negative error code on failure.
1309 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1311 struct llapi_layout *layout = NULL;
1312 struct mirror_args *cur_mirror = NULL;
1313 uint16_t mirror_count = 0;
1317 rc = mirror_create_sanity_check(NULL, mirror_list);
1321 cur_mirror = mirror_list;
1322 while (cur_mirror != NULL) {
1323 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1325 &cur_mirror->m_flags);
1328 fprintf(stderr, "%s: failed to set mirror flags\n",
1333 for (i = 0; i < cur_mirror->m_count; i++) {
1334 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1337 fprintf(stderr, "error: %s: "
1338 "merge layout failed: %s\n",
1339 progname, strerror(errno));
1343 mirror_count += cur_mirror->m_count;
1344 cur_mirror = cur_mirror->m_next;
1347 if (layout == NULL) {
1348 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1352 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1355 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1356 progname, strerror(errno));
1360 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0644,
1368 llapi_layout_free(layout);
1373 * Compare files and check lease on @fd.
1375 * \retval bytes number of bytes are the same
1377 static ssize_t mirror_file_compare(int fd, int fdv)
1379 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1381 ssize_t bytes_done = 0;
1382 ssize_t bytes_read = 0;
1384 buf = malloc(buflen * 2);
1389 if (!llapi_lease_check(fd)) {
1390 bytes_done = -EBUSY;
1394 bytes_read = read(fd, buf, buflen);
1395 if (bytes_read <= 0)
1398 if (bytes_read != read(fdv, buf + buflen, buflen))
1401 /* XXX: should compute the checksum on each buffer and then
1402 * compare checksum to avoid cache collision */
1403 if (memcmp(buf, buf + buflen, bytes_read))
1406 bytes_done += bytes_read;
1414 static int mirror_extend_file(const char *fname, const char *victim_file,
1415 enum mirror_flags mirror_flags)
1420 struct stat stbuf_v;
1421 struct ll_ioc_lease *data = NULL;
1424 fd = open(fname, O_RDWR);
1426 error_loc = "open source file";
1431 fdv = open(victim_file, O_RDWR);
1433 error_loc = "open target file";
1438 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1439 error_loc = "stat source or target file";
1444 if (stbuf.st_dev != stbuf_v.st_dev) {
1445 error_loc = "stat source and target file";
1450 /* mirrors should be of the same size */
1451 if (stbuf.st_size != stbuf_v.st_size) {
1452 error_loc = "file sizes don't match";
1457 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1459 error_loc = "cannot get lease";
1463 if (!(mirror_flags & MF_NO_VERIFY)) {
1465 /* mirrors should have the same contents */
1466 ret = mirror_file_compare(fd, fdv);
1467 if (ret != stbuf.st_size) {
1468 error_loc = "file busy or contents don't match";
1469 rc = ret < 0 ? ret : -EINVAL;
1474 /* Get rid of caching pages from clients */
1475 rc = llapi_file_flush(fd);
1477 error_loc = "cannot get data version";
1481 rc = llapi_file_flush(fdv);
1483 error_loc = "cannot get data version";
1488 /* Make sure we keep original atime/mtime values */
1489 rc = migrate_copy_timestamps(fd, fdv);
1491 error_loc = "cannot copy timestamp";
1495 /* Atomically put lease, merge layouts and close. */
1496 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1498 error_loc = "memory allocation";
1501 data->lil_mode = LL_LEASE_UNLCK;
1502 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1503 data->lil_count = 1;
1504 data->lil_ids[0] = fdv;
1505 rc = llapi_lease_set(fd, data);
1507 error_loc = "cannot merge layout";
1509 } else if (rc == 0) {
1511 error_loc = "lost lease lock";
1524 (void) unlink(victim_file);
1526 fprintf(stderr, "error: %s: %s: %s: %s\n",
1527 progname, fname, error_loc, strerror(-rc));
1531 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1533 struct ll_ioc_lease *data = NULL;
1538 rc = migrate_open_files(name, NULL, layout, &fd, &fdv);
1542 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1544 error_loc = "cannot get lease";
1548 rc = migrate_nonblock(fd, fdv);
1550 llapi_lease_release(fd);
1554 /* Atomically put lease, merge layouts and close. */
1555 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1557 error_loc = "memory allocation";
1560 data->lil_mode = LL_LEASE_UNLCK;
1561 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1562 data->lil_count = 1;
1563 data->lil_ids[0] = fdv;
1564 rc = llapi_lease_set(fd, data);
1566 error_loc = "cannot merge layout";
1568 } else if (rc == 0) {
1570 error_loc = "lost lease lock";
1583 fprintf(stderr, "error: %s: %s: %s: %s\n",
1584 progname, name, error_loc, strerror(-rc));
1588 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1589 enum mirror_flags mirror_flags)
1593 rc = mirror_create_sanity_check(fname, mirror_list);
1597 while (mirror_list) {
1598 if (mirror_list->m_file != NULL) {
1599 rc = mirror_extend_file(fname, mirror_list->m_file,
1602 __u32 mirror_count = mirror_list->m_count;
1604 while (mirror_count > 0) {
1605 rc = mirror_extend_layout(fname,
1606 mirror_list->m_layout);
1616 mirror_list = mirror_list->m_next;
1622 static int verify_id(struct llapi_layout *layout, void *cbdata)
1627 rc = llapi_layout_mirror_id_get(layout, &id);
1631 if ((__u16)id == *(__u16 *)cbdata)
1632 return LLAPI_LAYOUT_ITER_STOP;
1634 return LLAPI_LAYOUT_ITER_CONT;
1637 static int mirror_split(const char *fname, __u16 mirror_id,
1638 enum mirror_flags mflags, const char *victim_file)
1640 struct llapi_layout *layout;
1641 char parent[PATH_MAX];
1642 char victim[PATH_MAX];
1643 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1645 struct ll_ioc_lease *data;
1646 uint16_t mirror_count;
1651 /* check fname contains mirror with mirror_id */
1652 layout = llapi_layout_get_by_path(fname, 0);
1655 "error %s: file '%s' couldn't get layout\n",
1660 rc = mirror_sanity_check_one(layout);
1664 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1667 "error %s: file '%s' couldn't get mirror count\n",
1671 if (mirror_count < 2) {
1673 "error %s: file '%s' has %d component, cannot split\n",
1674 progname, fname, mirror_count);
1678 rc = llapi_layout_comp_iterate(layout, verify_id, &mirror_id);
1680 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1683 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1685 "error %s: file '%s' does not contain mirror with id %u\n",
1686 progname, fname, mirror_id);
1690 fd = open(fname, O_RDWR);
1693 "error %s: open file '%s' failed: %s\n",
1694 progname, fname, strerror(errno));
1698 /* get victim file directory pathname */
1699 if (strlen(fname) > sizeof(parent) - 1) {
1700 fprintf(stderr, "error %s: file name of '%s' too long\n",
1705 strncpy(parent, fname, sizeof(parent));
1706 ptr = strrchr(parent, '/');
1708 if (getcwd(parent, sizeof(parent)) == NULL) {
1709 fprintf(stderr, "error %s: getcwd failed: %s\n",
1710 progname, strerror(errno));
1720 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1722 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1727 if (victim_file == NULL) {
1728 /* use a temp file to store the splitted layout */
1729 if (mflags & MF_DESTROY) {
1730 fdv = llapi_create_volatile_idx(parent, mdt_index,
1731 O_LOV_DELAY_CREATE);
1733 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1735 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1738 /* user specified victim file */
1739 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1744 "error %s: create victim file failed: %s\n",
1745 progname, strerror(errno));
1749 /* get lease lock of fname */
1750 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1753 "error %s: cannot get lease of file '%s': %d\n",
1754 progname, fname, rc);
1758 /* Atomatically put lease, split layouts and close. */
1759 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1765 data->lil_mode = LL_LEASE_UNLCK;
1766 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1767 data->lil_count = 2;
1768 data->lil_ids[0] = fdv;
1769 data->lil_ids[1] = mirror_id;
1770 rc = llapi_lease_set(fd, data);
1772 if (rc == 0) /* lost lease lock */
1775 "error %s: cannot split '%s': %s\n",
1776 progname, fname, strerror(-rc));
1787 llapi_layout_free(layout);
1792 * Parse a string containing an target index list into an array of integers.
1794 * The input string contains a comma delimited list of individual
1795 * indices and ranges, for example "1,2-4,7". Add the indices into the
1796 * \a tgts array and remove duplicates.
1798 * \param[out] tgts array to store indices in
1799 * \param[in] size size of \a tgts array
1800 * \param[in] offset starting index in \a tgts
1801 * \param[in] arg string containing OST index list
1803 * \retval positive number of indices in \a tgts
1804 * \retval -EINVAL unable to parse \a arg
1806 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1810 int slots = size - offset;
1817 end_of_loop = false;
1818 while (!end_of_loop) {
1822 char *endptr = NULL;
1826 ptr = strchrnul(arg, ',');
1828 end_of_loop = *ptr == '\0';
1831 start_index = strtol(arg, &endptr, 0);
1832 if (endptr == arg) /* no data at all */
1834 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1837 end_index = start_index;
1838 if (*endptr == '-') {
1839 end_index = strtol(endptr + 1, &endptr, 0);
1840 if (*endptr != '\0')
1842 if (end_index < start_index)
1846 for (i = start_index; i <= end_index && slots > 0; i++) {
1849 /* remove duplicate */
1850 for (j = 0; j < offset; j++) {
1854 if (j == offset) { /* no duplicate */
1859 if (slots == 0 && i < end_index)
1867 if (!end_of_loop && ptr != NULL)
1870 return rc < 0 ? rc : nr;
1873 struct lfs_setstripe_args {
1874 unsigned long long lsa_comp_end;
1875 unsigned long long lsa_stripe_size;
1876 long long lsa_stripe_count;
1877 long long lsa_stripe_off;
1878 __u32 lsa_comp_flags;
1879 __u32 lsa_comp_neg_flags;
1880 unsigned long long lsa_pattern;
1883 char *lsa_pool_name;
1886 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1888 memset(lsa, 0, sizeof(*lsa));
1890 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1891 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1892 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1893 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1894 lsa->lsa_pool_name = NULL;
1898 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1899 * @lsa: Stripe options to be initialized and inherited.
1901 * This function initializes stripe options in @lsa and inherit
1902 * stripe_size, stripe_count and OST pool_name options.
1906 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1908 unsigned long long stripe_size;
1909 long long stripe_count;
1910 char *pool_name = NULL;
1912 stripe_size = lsa->lsa_stripe_size;
1913 stripe_count = lsa->lsa_stripe_count;
1914 pool_name = lsa->lsa_pool_name;
1916 setstripe_args_init(lsa);
1918 lsa->lsa_stripe_size = stripe_size;
1919 lsa->lsa_stripe_count = stripe_count;
1920 lsa->lsa_pool_name = pool_name;
1923 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1925 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1926 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1927 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1928 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1929 lsa->lsa_pool_name != NULL ||
1930 lsa->lsa_comp_end != 0);
1934 * comp_args_to_layout() - Create or extend a composite layout.
1935 * @composite: Pointer to the composite layout.
1936 * @lsa: Stripe options for the new component.
1938 * This function creates or extends a composite layout by adding a new
1939 * component with stripe options from @lsa.
1941 * Return: 0 on success or an error code on failure.
1943 static int comp_args_to_layout(struct llapi_layout **composite,
1944 struct lfs_setstripe_args *lsa)
1946 struct llapi_layout *layout = *composite;
1947 uint64_t prev_end = 0;
1950 if (layout == NULL) {
1951 layout = llapi_layout_alloc();
1952 if (layout == NULL) {
1953 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1957 *composite = layout;
1961 /* Get current component extent, current component
1962 * must be the tail component. */
1963 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
1965 fprintf(stderr, "Get comp extent failed. %s\n",
1970 rc = llapi_layout_comp_add(layout);
1972 fprintf(stderr, "Add component failed. %s\n",
1978 rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
1980 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
1981 prev_end, lsa->lsa_comp_end, strerror(errno));
1985 /* Data-on-MDT component setting */
1986 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
1987 /* In case of Data-on-MDT patterns the only extra option
1988 * applicable is stripe size option. */
1989 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
1990 fprintf(stderr, "Option 'stripe-count' can't be "
1991 "specified with Data-on-MDT component: %lld\n",
1992 lsa->lsa_stripe_count);
1995 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
1996 fprintf(stderr, "Option 'stripe-size' can't be "
1997 "specified with Data-on-MDT component: %llu\n",
1998 lsa->lsa_stripe_size);
2001 if (lsa->lsa_nr_tgts != 0) {
2002 fprintf(stderr, "Option 'ost-list' can't be specified "
2003 "with Data-on-MDT component: '%i'\n",
2007 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2008 fprintf(stderr, "Option 'stripe-offset' can't be "
2009 "specified with Data-on-MDT component: %lld\n",
2010 lsa->lsa_stripe_off);
2013 if (lsa->lsa_pool_name != 0) {
2014 fprintf(stderr, "Option 'pool' can't be specified "
2015 "with Data-on-MDT component: '%s'\n",
2016 lsa->lsa_pool_name);
2020 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2022 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2023 lsa->lsa_pattern, strerror(errno));
2026 /* Data-on-MDT component has always single stripe up to end */
2027 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2030 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2032 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2033 lsa->lsa_stripe_size, strerror(errno));
2037 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2039 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2040 lsa->lsa_stripe_count, strerror(errno));
2044 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2046 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2047 lsa->lsa_comp_flags, strerror(errno));
2051 if (lsa->lsa_pool_name != NULL) {
2052 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2054 fprintf(stderr, "Set pool name: %s failed. %s\n",
2055 lsa->lsa_pool_name, strerror(errno));
2059 rc = llapi_layout_pool_name_set(layout, "");
2061 fprintf(stderr, "Clear pool name failed: %s\n",
2067 if (lsa->lsa_nr_tgts > 0) {
2068 if (lsa->lsa_stripe_count > 0 &&
2069 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2070 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2071 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2072 fprintf(stderr, "stripe_count(%lld) != nr_osts(%d)\n",
2073 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2076 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2077 rc = llapi_layout_ost_index_set(layout, i,
2082 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2083 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2086 fprintf(stderr, "Set ost index %d failed. %s\n",
2087 i, strerror(errno));
2094 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2095 * end of the last component in the existing file, and adjust the
2096 * first extent start of the components to be added accordingly. */
2097 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2099 struct llapi_layout *head;
2100 uint64_t start, end, stripe_size, prev_end = 0;
2103 if (layout == NULL) {
2105 "%s setstripe: layout must be specified\n",
2111 head = llapi_layout_get_by_path(fname, 0);
2114 "%s setstripe: cannot read layout from '%s': %s\n",
2115 progname, fname, strerror(errno));
2117 } else if (errno == ENODATA) {
2118 /* file without LOVEA, this component-add will be turned
2119 * into a component-create. */
2120 llapi_layout_free(head);
2122 } else if (!llapi_layout_is_composite(head)) {
2123 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2125 llapi_layout_free(head);
2129 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2131 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2132 progname, strerror(errno));
2133 llapi_layout_free(head);
2137 llapi_layout_free(head);
2139 /* Make sure we use the first component of the layout to be added. */
2140 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2143 "%s setstripe: cannot move component cursor: %s\n",
2144 progname, strerror(errno));
2148 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2150 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2151 progname, strerror(errno));
2155 if (start > prev_end || end <= prev_end) {
2157 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2158 progname, start, end, prev_end);
2162 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2164 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2165 progname, strerror(errno));
2169 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2170 (prev_end & (stripe_size - 1))) {
2172 "%s setstripe: stripe size %lu not aligned with %lu\n",
2173 progname, stripe_size, prev_end);
2177 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2180 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2181 progname, prev_end, end, strerror(errno));
2188 static inline bool arg_is_eof(char *arg)
2190 return !strncmp(arg, "-1", strlen("-1")) ||
2191 !strncmp(arg, "EOF", strlen("EOF")) ||
2192 !strncmp(arg, "eof", strlen("eof"));
2196 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2198 * Return: Valid mirror_args pointer on success and
2199 * NULL if memory allocation fails.
2201 static struct mirror_args *lfs_mirror_alloc(void)
2203 struct mirror_args *mirror = NULL;
2206 mirror = calloc(1, sizeof(*mirror));
2217 * lfs_mirror_free() - Free memory allocated for a mirror argument
2219 * @mirror: Previously allocated mirror argument structure by
2220 * lfs_mirror_alloc().
2222 * Free memory allocated for @mirror.
2226 static void lfs_mirror_free(struct mirror_args *mirror)
2228 if (mirror->m_layout != NULL)
2229 llapi_layout_free(mirror->m_layout);
2234 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2235 * @mirror_list: Previously allocated mirror list.
2237 * Free memory allocated for @mirror_list.
2241 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2243 struct mirror_args *next_mirror = NULL;
2245 while (mirror_list != NULL) {
2246 next_mirror = mirror_list->m_next;
2247 lfs_mirror_free(mirror_list);
2248 mirror_list = next_mirror;
2260 LFS_COMP_USE_PARENT_OPT,
2261 LFS_COMP_NO_VERIFY_OPT,
2263 LFS_MIRROR_FLAGS_OPT,
2268 static int lfs_setstripe_internal(int argc, char **argv,
2269 enum setstripe_origin opc)
2271 struct lfs_setstripe_args lsa;
2272 struct llapi_stripe_param *param = NULL;
2273 struct find_param migrate_mdt_param = {
2283 char *mdt_idx_arg = NULL;
2284 unsigned long long size_units = 1;
2285 bool migrate_mode = false;
2286 bool migration_block = false;
2287 __u64 migration_flags = 0;
2288 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2289 int comp_del = 0, comp_set = 0;
2292 struct llapi_layout *layout = NULL;
2293 struct llapi_layout **lpp = &layout;
2294 bool mirror_mode = false;
2295 bool has_m_file = false;
2296 __u32 mirror_count = 0;
2297 enum mirror_flags mirror_flags = 0;
2298 struct mirror_args *mirror_list = NULL;
2299 struct mirror_args *new_mirror = NULL;
2300 struct mirror_args *last_mirror = NULL;
2301 __u16 mirror_id = 0;
2304 struct option long_opts[] = {
2305 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2306 /* --block is only valid in migrate mode */
2307 { .val = 'b', .name = "block", .has_arg = no_argument },
2308 { .val = LFS_COMP_ADD_OPT,
2309 .name = "comp-add", .has_arg = no_argument },
2310 { .val = LFS_COMP_ADD_OPT,
2311 .name = "component-add", .has_arg = no_argument },
2312 { .val = LFS_COMP_DEL_OPT,
2313 .name = "comp-del", .has_arg = no_argument },
2314 { .val = LFS_COMP_DEL_OPT,
2315 .name = "component-del", .has_arg = no_argument },
2316 { .val = LFS_COMP_FLAGS_OPT,
2317 .name = "comp-flags", .has_arg = required_argument },
2318 { .val = LFS_COMP_FLAGS_OPT,
2319 .name = "component-flags",
2320 .has_arg = required_argument },
2321 { .val = LFS_COMP_SET_OPT,
2322 .name = "comp-set", .has_arg = no_argument },
2323 { .val = LFS_COMP_SET_OPT,
2324 .name = "component-set",
2325 .has_arg = no_argument},
2326 { .val = LFS_COMP_USE_PARENT_OPT,
2327 .name = "parent", .has_arg = no_argument},
2328 { .val = LFS_COMP_NO_VERIFY_OPT,
2329 .name = "no-verify", .has_arg = no_argument},
2330 { .val = LFS_MIRROR_FLAGS_OPT,
2331 .name = "flags", .has_arg = required_argument},
2332 { .val = LFS_MIRROR_ID_OPT,
2333 .name = "mirror-id", .has_arg = required_argument},
2334 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2335 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2336 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2337 { .val = 'd', .name = "delete", .has_arg = no_argument},
2338 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2339 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2340 { .val = 'E', .name = "component-end",
2341 .has_arg = required_argument},
2342 { .val = 'f', .name = "file", .has_arg = required_argument },
2343 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2344 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2345 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2346 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
2347 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2348 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2349 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2350 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2351 { .val = 'L', .name = "layout", .has_arg = required_argument },
2352 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2353 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2354 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2355 /* --non-block is only valid in migrate mode */
2356 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2357 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2358 { .val = 'o', .name = "ost", .has_arg = required_argument },
2359 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2360 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2361 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2363 { .val = 'p', .name = "pool", .has_arg = required_argument },
2364 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2365 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2366 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2367 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2368 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2369 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2370 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2371 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2372 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2373 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2374 /* --verbose is only valid in migrate mode */
2375 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2376 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
2379 setstripe_args_init(&lsa);
2381 migrate_mode = (opc == SO_MIGRATE);
2382 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2384 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2386 while ((c = getopt_long(argc, argv, "bc:dE:f:i:I:m:N::no:p:L:s:S:v",
2387 long_opts, NULL)) >= 0) {
2392 case LFS_COMP_ADD_OPT:
2395 case LFS_COMP_DEL_OPT:
2398 case LFS_COMP_FLAGS_OPT:
2399 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2400 &lsa.lsa_comp_neg_flags);
2403 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2404 fprintf(stderr, "%s: inverted flags are not supported\n",
2408 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2410 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2417 case LFS_COMP_SET_OPT:
2420 case LFS_COMP_USE_PARENT_OPT:
2422 fprintf(stderr, "error: %s: --parent must be "
2423 "specified with --mirror-count|-N "
2424 "option\n", progname);
2427 setstripe_args_init(&lsa);
2429 case LFS_COMP_NO_VERIFY_OPT:
2430 mirror_flags |= MF_NO_VERIFY;
2432 case LFS_MIRROR_ID_OPT:
2433 mirror_id = strtoul(optarg, &end, 0);
2434 if (*end != '\0' || mirror_id == 0) {
2436 "%s %s: invalid mirror ID '%s'\n",
2437 progname, argv[0], optarg);
2441 case LFS_MIRROR_FLAGS_OPT: {
2444 if (!mirror_mode || !last_mirror) {
2445 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2450 result = comp_str2flags(optarg, &last_mirror->m_flags,
2456 fprintf(stderr, "%s: inverted flags are not supported\n",
2461 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2463 "%s: unsupported mirror flags: %s\n",
2471 if (!migrate_mode) {
2473 "%s %s: -b|--block valid only for migrate command\n",
2477 migration_block = true;
2480 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2483 "%s %s: invalid stripe count '%s'\n",
2484 progname, argv[0], optarg);
2488 if (lsa.lsa_stripe_count == -1)
2489 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2492 /* delete the default striping pattern */
2494 if (opc == SO_MIRROR_SPLIT) {
2497 "%s %s: -d cannot used with -f\n",
2501 mirror_flags |= MF_DESTROY;
2505 if (lsa.lsa_comp_end != 0) {
2506 result = comp_args_to_layout(lpp, &lsa);
2509 "%s %s: invalid layout\n",
2514 setstripe_args_init_inherit(&lsa);
2517 if (arg_is_eof(optarg)) {
2518 lsa.lsa_comp_end = LUSTRE_EOF;
2520 result = llapi_parse_size(optarg,
2525 "%s %s: invalid component end '%s'\n",
2526 progname, argv[0], optarg);
2532 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2535 "%s %s: invalid stripe offset '%s'\n",
2536 progname, argv[0], optarg);
2539 if (lsa.lsa_stripe_off == -1)
2540 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2543 comp_id = strtoul(optarg, &end, 0);
2544 if (*end != '\0' || comp_id == 0 ||
2545 comp_id > LCME_ID_MAX) {
2547 "%s %s: invalid component ID '%s'\n",
2548 progname, argv[0], optarg);
2553 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2555 "error: %s: invalid option: %s\n",
2556 progname, argv[optopt + 1]);
2559 if (opc == SO_MIRROR_EXTEND) {
2560 if (last_mirror == NULL) {
2562 "error: %s: '-N' must exist in front of '%s'\n",
2563 progname, argv[optopt + 1]);
2566 last_mirror->m_file = optarg;
2567 last_mirror->m_count = 1;
2570 if (mirror_list == NULL)
2571 mirror_list = lfs_mirror_alloc();
2572 mirror_list->m_file = optarg;
2577 if (strcmp(argv[optind - 1], "mdt") == 0) {
2578 /* Can be only the first component */
2579 if (layout != NULL) {
2581 fprintf(stderr, "error: 'mdt' layout "
2582 "can be only the first one\n");
2585 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2587 fprintf(stderr, "error: 'mdt' layout "
2588 "size is too big\n");
2591 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2592 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2594 fprintf(stderr, "error: layout '%s' is "
2595 "unknown, supported layouts are: "
2596 "'mdt', 'raid0'\n", argv[optind]);
2601 if (!migrate_mode) {
2603 "%s %s: -m|--mdt-index valid only for migrate command\n",
2607 mdt_idx_arg = optarg;
2610 if (!migrate_mode) {
2612 "%s %s: -n|--non-block valid only for migrate command\n",
2616 migration_flags |= MIGRATION_NONBLOCK;
2619 if (opc == SO_SETSTRIPE) {
2620 opc = SO_MIRROR_CREATE;
2624 if (optarg != NULL) {
2625 mirror_count = strtoul(optarg, &end, 0);
2626 if (*end != '\0' || mirror_count == 0) {
2628 "error: %s: bad mirror count: %s\n",
2635 new_mirror = lfs_mirror_alloc();
2636 new_mirror->m_count = mirror_count;
2638 if (mirror_list == NULL)
2639 mirror_list = new_mirror;
2641 if (last_mirror != NULL) {
2642 /* wrap up last mirror */
2643 if (lsa.lsa_comp_end == 0)
2644 lsa.lsa_comp_end = LUSTRE_EOF;
2646 result = comp_args_to_layout(lpp, &lsa);
2648 lfs_mirror_free(new_mirror);
2652 setstripe_args_init_inherit(&lsa);
2654 last_mirror->m_next = new_mirror;
2657 last_mirror = new_mirror;
2658 lpp = &last_mirror->m_layout;
2661 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2662 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2663 fprintf(stderr, "warning: '--ost-list' is "
2664 "deprecated, use '--ost' instead\n");
2666 lsa.lsa_nr_tgts = parse_targets(osts,
2667 sizeof(osts) / sizeof(__u32),
2668 lsa.lsa_nr_tgts, optarg);
2669 if (lsa.lsa_nr_tgts < 0) {
2671 "%s %s: invalid OST target(s) '%s'\n",
2672 progname, argv[0], optarg);
2676 lsa.lsa_tgts = osts;
2677 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2678 lsa.lsa_stripe_off = osts[0];
2683 lsa.lsa_pool_name = optarg;
2686 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2690 "%s %s: invalid stripe size '%s'\n",
2691 progname, argv[0], optarg);
2696 if (!migrate_mode) {
2698 "%s %s: -v|--verbose valid only for migrate command\n",
2702 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2705 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2706 progname, argv[0], argv[optind - 1]);
2711 fname = argv[optind];
2713 if (optind == argc) {
2714 fprintf(stderr, "%s %s: FILE must be specified\n",
2719 if (mirror_mode && mirror_count == 0) {
2721 "error: %s: --mirror-count|-N option is required\n",
2728 if (lsa.lsa_comp_end == 0)
2729 lsa.lsa_comp_end = LUSTRE_EOF;
2732 if (lsa.lsa_comp_end != 0) {
2733 result = comp_args_to_layout(lpp, &lsa);
2738 if (mirror_flags & MF_NO_VERIFY) {
2739 if (opc != SO_MIRROR_EXTEND) {
2741 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2745 } else if (!has_m_file) {
2747 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2754 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2755 * altered by user space tool, so we don't need to support the
2756 * --component-set for this moment. */
2757 if (comp_set && !comp_id) {
2758 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2763 if ((delete + comp_set + comp_del + comp_add) > 1) {
2765 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2770 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2771 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2773 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2778 if ((comp_set || comp_del) &&
2779 (setstripe_args_specified(&lsa) || layout != NULL)) {
2781 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
2786 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
2788 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
2793 if (comp_add || comp_del) {
2796 result = lstat(fname, &st);
2797 if (result == 0 && S_ISDIR(st.st_mode)) {
2799 "%s setstripe: cannot use --component-add or --component-del for directory\n",
2805 fprintf(stderr, "error: %s: can't use --component-add "
2806 "or --component-del for mirror operation\n",
2813 if (layout == NULL) {
2815 "%s %s: option -E must be specified with --component-add\n",
2820 result = adjust_first_extent(fname, layout);
2821 if (result == -ENODATA)
2823 else if (result != 0)
2827 if (mdt_idx_arg != NULL && optind > 3) {
2829 "%s %s: option -m cannot be used with other options\n",
2834 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
2836 "%s %s: options --non-block and --block are mutually exclusive\n",
2841 if (!comp_del && !comp_set && comp_id != 0) {
2843 "%s %s: option -I can only be used with --component-del\n",
2848 if (mdt_idx_arg != NULL) {
2849 /* initialize migrate mdt parameters */
2850 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
2852 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
2853 progname, argv[0], mdt_idx_arg);
2856 migrate_mdt_param.fp_migrate = 1;
2857 } else if (layout == NULL) {
2858 /* initialize stripe parameters */
2859 param = calloc(1, offsetof(typeof(*param),
2860 lsp_osts[lsa.lsa_nr_tgts]));
2861 if (param == NULL) {
2863 "%s %s: cannot allocate memory for parameters: %s\n",
2864 progname, argv[0], strerror(ENOMEM));
2869 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
2870 param->lsp_stripe_size = lsa.lsa_stripe_size;
2871 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2872 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
2873 param->lsp_stripe_count = -1;
2875 param->lsp_stripe_count = lsa.lsa_stripe_count;
2877 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2878 param->lsp_stripe_offset = -1;
2880 param->lsp_stripe_offset = lsa.lsa_stripe_off;
2881 param->lsp_pool = lsa.lsa_pool_name;
2882 param->lsp_is_specific = false;
2883 if (lsa.lsa_nr_tgts > 0) {
2884 if (lsa.lsa_stripe_count > 0 &&
2885 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2886 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2887 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
2888 fprintf(stderr, "error: %s: stripe count %lld "
2889 "doesn't match the number of OSTs: %d\n"
2890 , argv[0], lsa.lsa_stripe_count,
2896 param->lsp_is_specific = true;
2897 param->lsp_stripe_count = lsa.lsa_nr_tgts;
2898 memcpy(param->lsp_osts, osts,
2899 sizeof(*osts) * lsa.lsa_nr_tgts);
2903 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
2904 if (mdt_idx_arg != NULL) {
2905 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
2906 } else if (migrate_mode) {
2907 result = lfs_migrate(fname, migration_flags, param,
2909 } else if (comp_set != 0) {
2910 result = lfs_component_set(fname, comp_id,
2912 lsa.lsa_comp_neg_flags);
2913 } else if (comp_del != 0) {
2914 result = lfs_component_del(fname, comp_id,
2916 lsa.lsa_comp_neg_flags);
2917 } else if (comp_add != 0) {
2918 result = lfs_component_add(fname, layout);
2919 } else if (opc == SO_MIRROR_CREATE) {
2920 result = mirror_create(fname, mirror_list);
2921 } else if (opc == SO_MIRROR_EXTEND) {
2922 result = mirror_extend(fname, mirror_list,
2924 } else if (opc == SO_MIRROR_SPLIT) {
2925 if (mirror_id == 0) {
2927 "%s %s: no mirror id is specified\n",
2931 result = mirror_split(fname, mirror_id, mirror_flags,
2932 has_m_file ? mirror_list->m_file :
2934 } else if (layout != NULL) {
2935 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
2942 result = llapi_file_open_param(fname,
2951 /* Save the first error encountered. */
2959 llapi_layout_free(layout);
2960 lfs_mirror_list_free(mirror_list);
2965 llapi_layout_free(layout);
2966 lfs_mirror_list_free(mirror_list);
2970 static int lfs_poollist(int argc, char **argv)
2975 return llapi_poollist(argv[1]);
2978 static int set_time(time_t *time, time_t *set, char *str)
2985 else if (str[0] == '-')
2991 t = strtol(str, NULL, 0);
2992 if (*time < t * 24 * 60 * 60) {
2995 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
2999 *set = *time - t * 24 * 60 * 60;
3002 static int name2uid(unsigned int *id, const char *name)
3004 struct passwd *passwd;
3006 passwd = getpwnam(name);
3009 *id = passwd->pw_uid;
3014 static int name2gid(unsigned int *id, const char *name)
3016 struct group *group;
3018 group = getgrnam(name);
3021 *id = group->gr_gid;
3026 static inline int name2projid(unsigned int *id, const char *name)
3031 static int uid2name(char **name, unsigned int id)
3033 struct passwd *passwd;
3035 passwd = getpwuid(id);
3038 *name = passwd->pw_name;
3043 static inline int gid2name(char **name, unsigned int id)
3045 struct group *group;
3047 group = getgrgid(id);
3050 *name = group->gr_name;
3055 static int name2layout(__u32 *layout, char *name)
3057 char *ptr, *layout_name;
3060 for (ptr = name; ; ptr = NULL) {
3061 layout_name = strtok(ptr, ",");
3062 if (layout_name == NULL)
3064 if (strcmp(layout_name, "released") == 0)
3065 *layout |= LOV_PATTERN_F_RELEASED;
3066 else if (strcmp(layout_name, "raid0") == 0)
3067 *layout |= LOV_PATTERN_RAID0;
3068 else if (strcmp(layout_name, "mdt") == 0)
3069 *layout |= LOV_PATTERN_MDT;
3076 static int lfs_find(int argc, char **argv)
3081 struct find_param param = {
3085 struct option long_opts[] = {
3086 { .val = 'A', .name = "atime", .has_arg = required_argument },
3087 { .val = LFS_COMP_COUNT_OPT,
3088 .name = "comp-count", .has_arg = required_argument },
3089 { .val = LFS_COMP_COUNT_OPT,
3090 .name = "component-count",
3091 .has_arg = required_argument },
3092 { .val = LFS_COMP_FLAGS_OPT,
3093 .name = "comp-flags", .has_arg = required_argument },
3094 { .val = LFS_COMP_FLAGS_OPT,
3095 .name = "component-flags",
3096 .has_arg = required_argument },
3097 { .val = LFS_COMP_START_OPT,
3098 .name = "comp-start", .has_arg = required_argument },
3099 { .val = LFS_COMP_START_OPT,
3100 .name = "component-start",
3101 .has_arg = required_argument },
3102 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3103 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3104 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3105 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3106 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3107 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3108 { .val = 'E', .name = "component-end",
3109 .has_arg = required_argument },
3110 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3111 { .val = 'g', .name = "gid", .has_arg = required_argument },
3112 { .val = 'G', .name = "group", .has_arg = required_argument },
3113 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3114 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3115 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3116 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3117 { .val = 'L', .name = "layout", .has_arg = required_argument },
3118 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3119 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3120 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3121 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3122 { .val = 'n', .name = "name", .has_arg = required_argument },
3123 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3124 { .val = 'O', .name = "obd", .has_arg = required_argument },
3125 { .val = 'O', .name = "ost", .has_arg = required_argument },
3126 /* no short option for pool yet, can be 'p' after 2.18 */
3127 { .val = LFS_POOL_OPT,
3128 .name = "pool", .has_arg = required_argument },
3129 { .val = '0', .name = "print0", .has_arg = no_argument },
3130 { .val = 'P', .name = "print", .has_arg = no_argument },
3131 { .val = LFS_PROJID_OPT,
3132 .name = "projid", .has_arg = required_argument },
3133 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3134 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3135 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3136 { .val = 's', .name = "size", .has_arg = required_argument },
3137 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3138 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3139 { .val = 't', .name = "type", .has_arg = required_argument },
3140 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3141 { .val = 'u', .name = "uid", .has_arg = required_argument },
3142 { .val = 'U', .name = "user", .has_arg = required_argument },
3143 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3144 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3156 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3157 while ((c = getopt_long_only(argc, argv,
3158 "-0A:c:C:D:E:g:G:H:i:L:m:M:n:O:Ppqrs:S:t:T:u:U:v",
3159 long_opts, NULL)) >= 0) {
3164 /* '!' is part of option */
3165 /* when getopt_long_only() finds a string which is not
3166 * an option nor a known option argument it returns 1
3167 * in that case if we already have found pathstart and pathend
3168 * (i.e. we have the list of pathnames),
3169 * the only supported value is "!"
3171 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3172 if (!isoption && pathend != -1) {
3173 fprintf(stderr, "err: %s: filename|dirname must either "
3174 "precede options or follow options\n",
3179 if (!isoption && pathstart == -1)
3180 pathstart = optind - 1;
3181 if (isoption && pathstart != -1 && pathend == -1)
3182 pathend = optind - 2;
3188 /* unknown; opt is "!" or path component,
3189 * checking done above.
3191 if (strcmp(optarg, "!") == 0)
3195 xtime = ¶m.fp_atime;
3196 xsign = ¶m.fp_asign;
3197 param.fp_exclude_atime = !!neg_opt;
3198 /* no break, this falls through to 'C' for ctime */
3201 xtime = ¶m.fp_ctime;
3202 xsign = ¶m.fp_csign;
3203 param.fp_exclude_ctime = !!neg_opt;
3205 /* no break, this falls through to 'M' for mtime */
3208 xtime = ¶m.fp_mtime;
3209 xsign = ¶m.fp_msign;
3210 param.fp_exclude_mtime = !!neg_opt;
3212 rc = set_time(&t, xtime, optarg);
3213 if (rc == INT_MAX) {
3220 case LFS_COMP_COUNT_OPT:
3221 if (optarg[0] == '+') {
3222 param.fp_comp_count_sign = -1;
3224 } else if (optarg[0] == '-') {
3225 param.fp_comp_count_sign = 1;
3229 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3230 if (*endptr != '\0') {
3231 fprintf(stderr, "error: bad component count "
3235 param.fp_check_comp_count = 1;
3236 param.fp_exclude_comp_count = !!neg_opt;
3238 case LFS_COMP_FLAGS_OPT:
3239 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3240 ¶m.fp_comp_neg_flags);
3242 fprintf(stderr, "error: bad component flags "
3246 param.fp_check_comp_flags = 1;
3248 __u32 flags = param.fp_comp_neg_flags;
3249 param.fp_comp_neg_flags = param.fp_comp_flags;
3250 param.fp_comp_flags = flags;
3253 case LFS_COMP_START_OPT:
3254 if (optarg[0] == '+') {
3255 param.fp_comp_start_sign = -1;
3257 } else if (optarg[0] == '-') {
3258 param.fp_comp_start_sign = 1;
3262 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3263 ¶m.fp_comp_start_units, 0);
3265 fprintf(stderr, "error: bad component start "
3269 param.fp_check_comp_start = 1;
3270 param.fp_exclude_comp_start = !!neg_opt;
3273 if (optarg[0] == '+') {
3274 param.fp_stripe_count_sign = -1;
3276 } else if (optarg[0] == '-') {
3277 param.fp_stripe_count_sign = 1;
3281 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3282 if (*endptr != '\0') {
3283 fprintf(stderr,"error: bad stripe_count '%s'\n",
3288 param.fp_check_stripe_count = 1;
3289 param.fp_exclude_stripe_count = !!neg_opt;
3292 param.fp_max_depth = strtol(optarg, 0, 0);
3295 if (optarg[0] == '+') {
3296 param.fp_comp_end_sign = -1;
3298 } else if (optarg[0] == '-') {
3299 param.fp_comp_end_sign = 1;
3303 if (arg_is_eof(optarg)) {
3304 param.fp_comp_end = LUSTRE_EOF;
3305 param.fp_comp_end_units = 1;
3308 rc = llapi_parse_size(optarg,
3310 ¶m.fp_comp_end_units, 0);
3313 fprintf(stderr, "error: bad component end "
3317 param.fp_check_comp_end = 1;
3318 param.fp_exclude_comp_end = !!neg_opt;
3322 rc = name2gid(¶m.fp_gid, optarg);
3324 param.fp_gid = strtoul(optarg, &endptr, 10);
3325 if (*endptr != '\0') {
3326 fprintf(stderr, "Group/GID: %s cannot "
3327 "be found.\n", optarg);
3332 param.fp_exclude_gid = !!neg_opt;
3333 param.fp_check_gid = 1;
3336 param.fp_hash_type = check_hashtype(optarg);
3337 if (param.fp_hash_type == 0) {
3338 fprintf(stderr, "error: bad hash_type '%s'\n",
3343 param.fp_check_hash_type = 1;
3344 param.fp_exclude_hash_type = !!neg_opt;
3347 ret = name2layout(¶m.fp_layout, optarg);
3350 param.fp_exclude_layout = !!neg_opt;
3351 param.fp_check_layout = 1;
3355 rc = name2uid(¶m.fp_uid, optarg);
3357 param.fp_uid = strtoul(optarg, &endptr, 10);
3358 if (*endptr != '\0') {
3359 fprintf(stderr, "User/UID: %s cannot "
3360 "be found.\n", optarg);
3365 param.fp_exclude_uid = !!neg_opt;
3366 param.fp_check_uid = 1;
3369 param.fp_pattern = (char *)optarg;
3370 param.fp_exclude_pattern = !!neg_opt;
3375 char *buf, *token, *next, *p;
3379 buf = strdup(optarg);
3385 param.fp_exclude_obd = !!neg_opt;
3388 while (token && *token) {
3389 token = strchr(token, ',');
3396 param.fp_exclude_mdt = !!neg_opt;
3397 param.fp_num_alloc_mdts += len;
3398 tmp = realloc(param.fp_mdt_uuid,
3399 param.fp_num_alloc_mdts *
3400 sizeof(*param.fp_mdt_uuid));
3406 param.fp_mdt_uuid = tmp;
3408 param.fp_exclude_obd = !!neg_opt;
3409 param.fp_num_alloc_obds += len;
3410 tmp = realloc(param.fp_obd_uuid,
3411 param.fp_num_alloc_obds *
3412 sizeof(*param.fp_obd_uuid));
3418 param.fp_obd_uuid = tmp;
3420 for (token = buf; token && *token; token = next) {
3421 struct obd_uuid *puuid;
3424 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3427 ¶m.fp_obd_uuid[param.fp_num_obds++];
3429 p = strchr(token, ',');
3436 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3441 strncpy(puuid->uuid, token,
3442 sizeof(puuid->uuid));
3449 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3453 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3455 "Pool name %s is too long (max %d)\n",
3456 optarg, LOV_MAXPOOLNAME);
3461 * We do check for empty pool because empty pool
3462 * is used to find V1 LOV attributes
3464 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3465 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3466 param.fp_exclude_pool = !!neg_opt;
3467 param.fp_check_pool = 1;
3469 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3470 case 'p': /* want this for --pool, to match getstripe/find */
3472 "warning: -p deprecated, use --print0 or -0\n");
3475 param.fp_zero_end = 1;
3477 case 'P': /* we always print, this option is a no-op */
3479 case LFS_PROJID_OPT:
3480 rc = name2projid(¶m.fp_projid, optarg);
3482 param.fp_projid = strtoul(optarg, &endptr, 10);
3483 if (*endptr != '\0') {
3485 "Invalid project ID: %s",
3491 param.fp_exclude_projid = !!neg_opt;
3492 param.fp_check_projid = 1;
3495 if (optarg[0] == '+') {
3496 param.fp_size_sign = -1;
3498 } else if (optarg[0] == '-') {
3499 param.fp_size_sign = 1;
3503 ret = llapi_parse_size(optarg, ¶m.fp_size,
3504 ¶m.fp_size_units, 0);
3506 fprintf(stderr, "error: bad file size '%s'\n",
3510 param.fp_check_size = 1;
3511 param.fp_exclude_size = !!neg_opt;
3514 if (optarg[0] == '+') {
3515 param.fp_stripe_size_sign = -1;
3517 } else if (optarg[0] == '-') {
3518 param.fp_stripe_size_sign = 1;
3522 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3523 ¶m.fp_stripe_size_units, 0);
3525 fprintf(stderr, "error: bad stripe_size '%s'\n",
3529 param.fp_check_stripe_size = 1;
3530 param.fp_exclude_stripe_size = !!neg_opt;
3533 param.fp_exclude_type = !!neg_opt;
3534 switch (optarg[0]) {
3536 param.fp_type = S_IFBLK;
3539 param.fp_type = S_IFCHR;
3542 param.fp_type = S_IFDIR;
3545 param.fp_type = S_IFREG;
3548 param.fp_type = S_IFLNK;
3551 param.fp_type = S_IFIFO;
3554 param.fp_type = S_IFSOCK;
3557 fprintf(stderr, "error: %s: bad type '%s'\n",
3564 if (optarg[0] == '+') {
3565 param.fp_mdt_count_sign = -1;
3567 } else if (optarg[0] == '-') {
3568 param.fp_mdt_count_sign = 1;
3572 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3573 if (*endptr != '\0') {
3574 fprintf(stderr, "error: bad mdt_count '%s'\n",
3579 param.fp_check_mdt_count = 1;
3580 param.fp_exclude_mdt_count = !!neg_opt;
3588 if (pathstart == -1) {
3589 fprintf(stderr, "error: %s: no filename|pathname\n",
3593 } else if (pathend == -1) {
3599 rc = llapi_find(argv[pathstart], ¶m);
3600 if (rc != 0 && ret == 0)
3602 } while (++pathstart < pathend);
3605 fprintf(stderr, "error: %s failed for %s.\n",
3606 argv[0], argv[optind - 1]);
3608 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3609 free(param.fp_obd_uuid);
3611 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3612 free(param.fp_mdt_uuid);
3617 static int lfs_getstripe_internal(int argc, char **argv,
3618 struct find_param *param)
3620 struct option long_opts[] = {
3621 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3622 { .val = LFS_COMP_COUNT_OPT,
3623 .name = "comp-count", .has_arg = no_argument },
3624 { .val = LFS_COMP_COUNT_OPT,
3625 .name = "component-count", .has_arg = no_argument },
3626 { .val = LFS_COMP_FLAGS_OPT,
3627 .name = "comp-flags", .has_arg = optional_argument },
3628 { .val = LFS_COMP_FLAGS_OPT,
3629 .name = "component-flags", .has_arg = optional_argument },
3630 { .val = LFS_COMP_START_OPT,
3631 .name = "comp-start", .has_arg = optional_argument },
3632 { .val = LFS_COMP_START_OPT,
3633 .name = "component-start", .has_arg = optional_argument },
3634 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3635 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3636 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
3637 { .val = 'd', .name = "directory", .has_arg = no_argument },
3638 { .val = 'D', .name = "default", .has_arg = no_argument },
3639 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3640 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
3641 { .val = 'F', .name = "fid", .has_arg = no_argument },
3642 { .val = 'g', .name = "generation", .has_arg = no_argument },
3643 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3644 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
3645 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3646 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3647 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3648 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3649 { .val = 'L', .name = "layout", .has_arg = no_argument },
3650 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3651 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3652 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3653 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
3654 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
3655 { .val = 'O', .name = "obd", .has_arg = required_argument },
3656 { .val = 'O', .name = "ost", .has_arg = required_argument },
3657 { .val = 'p', .name = "pool", .has_arg = no_argument },
3658 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3659 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3660 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3661 { .val = 'R', .name = "raw", .has_arg = no_argument },
3662 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3663 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3664 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3665 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3666 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3667 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3668 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3669 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3674 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
3675 long_opts, NULL)) != -1) {
3678 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3679 param->fp_verbose |= VERBOSE_COUNT;
3680 param->fp_max_depth = 0;
3683 case LFS_COMP_COUNT_OPT:
3684 param->fp_verbose |= VERBOSE_COMP_COUNT;
3685 param->fp_max_depth = 0;
3687 case LFS_COMP_FLAGS_OPT:
3688 if (optarg != NULL) {
3689 rc = comp_str2flags(optarg,
3690 ¶m->fp_comp_flags,
3691 ¶m->fp_comp_neg_flags);
3693 fprintf(stderr, "error: %s bad "
3694 "component flags '%s'.\n",
3698 param->fp_check_comp_flags = 1;
3700 param->fp_verbose |= VERBOSE_COMP_FLAGS;
3701 param->fp_max_depth = 0;
3704 case LFS_COMP_START_OPT:
3705 if (optarg != NULL) {
3707 if (tmp[0] == '+') {
3708 param->fp_comp_start_sign = -1;
3710 } else if (tmp[0] == '-') {
3711 param->fp_comp_start_sign = 1;
3714 rc = llapi_parse_size(tmp,
3715 ¶m->fp_comp_start,
3716 ¶m->fp_comp_start_units, 0);
3718 fprintf(stderr, "error: %s bad "
3719 "component start '%s'.\n",
3723 param->fp_check_comp_start = 1;
3726 param->fp_verbose |= VERBOSE_COMP_START;
3727 param->fp_max_depth = 0;
3731 param->fp_max_depth = 0;
3734 param->fp_get_default_lmv = 1;
3737 if (optarg != NULL) {
3739 if (tmp[0] == '+') {
3740 param->fp_comp_end_sign = -1;
3742 } else if (tmp[0] == '-') {
3743 param->fp_comp_end_sign = 1;
3747 if (arg_is_eof(tmp)) {
3748 param->fp_comp_end = LUSTRE_EOF;
3749 param->fp_comp_end_units = 1;
3752 rc = llapi_parse_size(tmp,
3753 ¶m->fp_comp_end,
3754 ¶m->fp_comp_end_units, 0);
3757 fprintf(stderr, "error: %s bad "
3758 "component end '%s'.\n",
3762 param->fp_check_comp_end = 1;
3764 param->fp_verbose |= VERBOSE_COMP_END;
3765 param->fp_max_depth = 0;
3769 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3770 param->fp_verbose |= VERBOSE_DFID;
3771 param->fp_max_depth = 0;
3775 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3776 param->fp_verbose |= VERBOSE_GENERATION;
3777 param->fp_max_depth = 0;
3781 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3782 param->fp_verbose |= VERBOSE_OFFSET;
3783 param->fp_max_depth = 0;
3787 if (optarg != NULL) {
3788 param->fp_comp_id = strtoul(optarg, &end, 0);
3789 if (*end != '\0' || param->fp_comp_id == 0 ||
3790 param->fp_comp_id > LCME_ID_MAX) {
3791 fprintf(stderr, "error: %s bad "
3792 "component id '%s'\n",
3796 param->fp_check_comp_id = 1;
3799 param->fp_max_depth = 0;
3800 param->fp_verbose |= VERBOSE_COMP_ID;
3804 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3805 param->fp_verbose |= VERBOSE_LAYOUT;
3806 param->fp_max_depth = 0;
3809 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3811 fprintf(stderr, "warning: '-M' deprecated"
3812 ", use '--mdt-index' or '-m' instead\n");
3815 if (!(param->fp_verbose & VERBOSE_DETAIL))
3816 param->fp_max_depth = 0;
3817 param->fp_verbose |= VERBOSE_MDTINDEX;
3820 if (param->fp_obd_uuid) {
3822 "error: %s: only one obduuid allowed",
3826 param->fp_obd_uuid = (struct obd_uuid *)optarg;
3829 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3830 param->fp_verbose |= VERBOSE_POOL;
3831 param->fp_max_depth = 0;
3838 param->fp_recursive = 1;
3844 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3845 param->fp_verbose |= VERBOSE_SIZE;
3846 param->fp_max_depth = 0;
3850 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
3863 if (param->fp_recursive)
3864 param->fp_max_depth = -1;
3865 else if (param->fp_verbose & VERBOSE_DETAIL)
3866 param->fp_max_depth = 1;
3868 if (!param->fp_verbose)
3869 param->fp_verbose = VERBOSE_DEFAULT;
3870 if (param->fp_quiet)
3871 param->fp_verbose = VERBOSE_OBJID;
3874 rc = llapi_getstripe(argv[optind], param);
3875 } while (++optind < argc && !rc);
3878 fprintf(stderr, "error: %s failed for %s.\n",
3879 argv[0], argv[optind - 1]);
3883 static int lfs_tgts(int argc, char **argv)
3885 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3886 struct find_param param;
3887 int index = 0, rc=0;
3892 if (argc == 2 && !realpath(argv[1], path)) {
3894 fprintf(stderr, "error: invalid path '%s': %s\n",
3895 argv[1], strerror(-rc));
3899 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
3900 /* Check if we have a mount point */
3901 if (mntdir[0] == '\0')
3904 memset(¶m, 0, sizeof(param));
3905 if (!strcmp(argv[0], "mdts"))
3906 param.fp_get_lmv = 1;
3908 rc = llapi_ostlist(mntdir, ¶m);
3910 fprintf(stderr, "error: %s: failed on %s\n",
3913 if (path[0] != '\0')
3915 memset(mntdir, 0, PATH_MAX);
3921 static int lfs_getstripe(int argc, char **argv)
3923 struct find_param param = { 0 };
3925 param.fp_max_depth = 1;
3926 return lfs_getstripe_internal(argc, argv, ¶m);
3930 static int lfs_getdirstripe(int argc, char **argv)
3932 struct find_param param = { 0 };
3933 struct option long_opts[] = {
3934 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
3935 { .val = 'D', .name = "default", .has_arg = no_argument },
3936 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
3937 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
3938 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3939 { .val = 'O', .name = "obd", .has_arg = required_argument },
3940 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3941 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
3942 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3946 param.fp_get_lmv = 1;
3948 while ((c = getopt_long(argc, argv,
3949 "cDHimO:rtTy", long_opts, NULL)) != -1)
3954 param.fp_verbose |= VERBOSE_COUNT;
3957 param.fp_get_default_lmv = 1;
3961 param.fp_verbose |= VERBOSE_OFFSET;
3963 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3965 fprintf(stderr, "warning: '-t' deprecated, "
3966 "use '--mdt-hash' or '-H' instead\n");
3969 param.fp_verbose |= VERBOSE_HASH_TYPE;
3972 if (param.fp_obd_uuid) {
3974 "error: %s: only one obduuid allowed",
3978 param.fp_obd_uuid = (struct obd_uuid *)optarg;
3981 param.fp_recursive = 1;
3994 if (param.fp_recursive)
3995 param.fp_max_depth = -1;
3997 if (!param.fp_verbose)
3998 param.fp_verbose = VERBOSE_DEFAULT;
4001 rc = llapi_getstripe(argv[optind], ¶m);
4002 } while (++optind < argc && !rc);
4005 fprintf(stderr, "error: %s failed for %s.\n",
4006 argv[0], argv[optind - 1]);
4011 static int lfs_setdirstripe(int argc, char **argv)
4015 struct lfs_setstripe_args lsa;
4016 struct llapi_stripe_param *param = NULL;
4017 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4020 char *mode_opt = NULL;
4021 bool default_stripe = false;
4022 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4023 mode_t previous_mode = 0;
4024 bool delete = false;
4026 struct option long_opts[] = {
4027 { .val = 'c', .name = "count", .has_arg = required_argument },
4028 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4029 { .val = 'd', .name = "delete", .has_arg = no_argument },
4030 { .val = 'D', .name = "default", .has_arg = no_argument },
4031 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4032 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4033 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4034 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4035 { .val = 'i', .name = "index", .has_arg = required_argument },
4037 { .val = 'o', .name = "mode", .has_arg = required_argument },
4038 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4039 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4041 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4044 setstripe_args_init(&lsa);
4046 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4054 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4057 "%s %s: invalid stripe count '%s'\n",
4058 progname, argv[0], optarg);
4064 default_stripe = true;
4067 default_stripe = true;
4069 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4071 fprintf(stderr, "warning: '--hash-type' and '-t' "
4072 "deprecated, use '--mdt-hash' or '-H' instead\n");
4075 lsa.lsa_pattern = check_hashtype(optarg);
4076 if (lsa.lsa_pattern == 0) {
4078 "%s %s: bad stripe hash type '%s'\n",
4079 progname, argv[0], optarg);
4084 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4085 if (strcmp(argv[optind - 1], "--index") == 0)
4087 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4090 lsa.lsa_nr_tgts = parse_targets(mdts,
4091 sizeof(mdts) / sizeof(__u32),
4092 lsa.lsa_nr_tgts, optarg);
4093 if (lsa.lsa_nr_tgts < 0) {
4095 "%s %s: invalid MDT target(s) '%s'\n",
4096 progname, argv[0], optarg);
4100 lsa.lsa_tgts = mdts;
4101 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4102 lsa.lsa_stripe_off = mdts[0];
4104 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4106 fprintf(stderr, "warning: '-m' is deprecated, "
4107 "use '--mode' or '-o' instead\n");
4113 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4114 progname, argv[0], argv[optind - 1]);
4119 if (optind == argc) {
4120 fprintf(stderr, "%s %s: DIR must be specified\n",
4125 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
4126 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
4128 "%s %s: stripe offset and count must be specified\n",
4134 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
4135 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
4137 "%s %s: cannot specify -d with -c or -i options\n",
4142 if (mode_opt != NULL) {
4143 mode = strtoul(mode_opt, &end, 8);
4146 "%s %s: bad MODE '%s'\n",
4147 progname, argv[0], mode_opt);
4150 previous_mode = umask(0);
4153 /* initialize stripe parameters */
4154 param = calloc(1, offsetof(typeof(*param), lsp_osts[lsa.lsa_nr_tgts]));
4155 if (param == NULL) {
4157 "%s %s: cannot allocate memory for parameters: %s\n",
4158 progname, argv[0], strerror(ENOMEM));
4162 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4163 param->lsp_stripe_count = lsa.lsa_stripe_count;
4164 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4165 param->lsp_stripe_offset = -1;
4167 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4168 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4169 param->lsp_stripe_pattern = lsa.lsa_pattern;
4171 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
4172 param->lsp_pool = lsa.lsa_pool_name;
4173 param->lsp_is_specific = false;
4174 if (lsa.lsa_nr_tgts > 1) {
4175 if (lsa.lsa_stripe_count > 0 &&
4176 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4177 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4178 fprintf(stderr, "error: %s: stripe count %lld doesn't "
4179 "match the number of MDTs: %d\n",
4180 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
4185 param->lsp_is_specific = true;
4186 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4187 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
4190 dname = argv[optind];
4193 result = llapi_dir_set_default_lmv(dname, param);
4195 result = llapi_dir_create_param(dname, mode, param);
4199 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
4200 progname, dname, strerror(-result));
4203 dname = argv[++optind];
4204 } while (dname != NULL);
4206 if (mode_opt != NULL)
4207 umask(previous_mode);
4214 static int lfs_rmentry(int argc, char **argv)
4221 fprintf(stderr, "error: %s: missing dirname\n",
4227 dname = argv[index];
4228 while (dname != NULL) {
4229 result = llapi_direntry_remove(dname);
4231 fprintf(stderr, "error: %s: remove dir entry '%s' "
4232 "failed\n", argv[0], dname);
4235 dname = argv[++index];
4240 static int lfs_mv(int argc, char **argv)
4242 struct find_param param = {
4249 struct option long_opts[] = {
4250 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4251 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4254 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
4256 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4258 fprintf(stderr, "warning: '-M' deprecated"
4259 ", use '--mdt-index' or '-m' instead\n");
4262 param.fp_mdt_index = strtoul(optarg, &end, 0);
4264 fprintf(stderr, "%s: invalid MDT index'%s'\n",
4270 param.fp_verbose = VERBOSE_DETAIL;
4273 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
4274 argv[0], argv[optind - 1]);
4279 if (param.fp_mdt_index == -1) {
4280 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
4284 if (optind >= argc) {
4285 fprintf(stderr, "%s: missing operand path\n", argv[0]);
4289 param.fp_migrate = 1;
4290 rc = llapi_migrate_mdt(argv[optind], ¶m);
4292 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
4293 argv[0], argv[optind], param.fp_mdt_index,
4298 static int lfs_osts(int argc, char **argv)
4300 return lfs_tgts(argc, argv);
4303 static int lfs_mdts(int argc, char **argv)
4305 return lfs_tgts(argc, argv);
4308 #define COOK(value) \
4311 while (value > 1024) { \
4319 #define CDF "%11llu"
4320 #define HDF "%8.1f%c"
4325 MNTDF_INODES = 0x0001,
4326 MNTDF_COOKED = 0x0002,
4327 MNTDF_LAZY = 0x0004,
4328 MNTDF_VERBOSE = 0x0008,
4331 static int showdf(char *mntdir, struct obd_statfs *stat,
4332 char *uuid, enum mntdf_flags flags,
4333 char *type, int index, int rc)
4335 long long avail, used, total;
4337 char *suffix = "KMGTPEZY";
4338 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4339 char tbuf[3 * sizeof(__u64)];
4340 char ubuf[3 * sizeof(__u64)];
4341 char abuf[3 * sizeof(__u64)];
4342 char rbuf[3 * sizeof(__u64)];
4349 if (flags & MNTDF_INODES) {
4350 avail = stat->os_ffree;
4351 used = stat->os_files - stat->os_ffree;
4352 total = stat->os_files;
4354 int shift = flags & MNTDF_COOKED ? 0 : 10;
4356 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4357 used = ((stat->os_blocks - stat->os_bfree) *
4358 stat->os_bsize) >> shift;
4359 total = (stat->os_blocks * stat->os_bsize) >> shift;
4362 if ((used + avail) > 0)
4363 ratio = (double)used / (double)(used + avail);
4365 if (flags & MNTDF_COOKED) {
4369 cook_val = (double)total;
4372 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4375 snprintf(tbuf, sizeof(tbuf), CDF, total);
4377 cook_val = (double)used;
4380 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4383 snprintf(ubuf, sizeof(ubuf), CDF, used);
4385 cook_val = (double)avail;
4388 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4391 snprintf(abuf, sizeof(abuf), CDF, avail);
4393 snprintf(tbuf, sizeof(tbuf), CDF, total);
4394 snprintf(ubuf, sizeof(tbuf), CDF, used);
4395 snprintf(abuf, sizeof(tbuf), CDF, avail);
4398 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
4399 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4400 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4402 printf("[%s:%d]", type, index);
4404 if (stat->os_state) {
4406 * Each character represents the matching
4409 const char state_names[] = "DRSI";
4414 for (i = 0, state = stat->os_state;
4415 state && i < sizeof(state_names); i++) {
4416 if (!(state & (1 << i)))
4418 printf("%c", state_names[i]);
4426 printf(UUF": inactive device\n", uuid);
4429 printf(UUF": %s\n", uuid, strerror(-rc));
4436 struct ll_stat_type {
4441 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags)
4443 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4444 struct obd_uuid uuid_buf;
4445 char *poolname = NULL;
4446 struct ll_stat_type types[] = {
4447 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4448 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4449 { .st_name = NULL } };
4450 struct ll_stat_type *tp;
4451 __u64 ost_ffree = 0;
4459 poolname = strchr(pool, '.');
4460 if (poolname != NULL) {
4461 if (strncmp(fsname, pool, strlen(fsname))) {
4462 fprintf(stderr, "filesystem name incorrect\n");
4470 fd = open(mntdir, O_RDONLY);
4473 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4478 if (flags & MNTDF_INODES)
4479 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4480 "UUID", "Inodes", "IUsed", "IFree",
4481 "IUse%", "Mounted on");
4483 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4484 "UUID", flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4485 "Used", "Available", "Use%", "Mounted on");
4487 for (tp = types; tp->st_name != NULL; tp++) {
4488 for (index = 0; ; index++) {
4489 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4490 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4491 type = flags & MNTDF_LAZY ?
4492 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4493 rc2 = llapi_obd_fstatfs(fd, type, index,
4494 &stat_buf, &uuid_buf);
4499 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4500 if (!(flags & MNTDF_VERBOSE))
4502 } else if (rc2 < 0 && rc == 0) {
4506 if (poolname && tp->st_op == LL_STATFS_LOV &&
4507 llapi_search_ost(fsname, poolname,
4508 obd_uuid2str(&uuid_buf)) != 1)
4511 /* the llapi_obd_statfs() call may have returned with
4512 * an error, but if it filled in uuid_buf we will at
4513 * lease use that to print out a message for that OBD.
4514 * If we didn't get anything in the uuid_buf, then fill
4515 * it in so that we can print an error message. */
4516 if (uuid_buf.uuid[0] == '\0')
4517 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4518 "%s%04x", tp->st_name, index);
4519 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
4520 flags, tp->st_name, index, rc2);
4523 if (tp->st_op == LL_STATFS_LMV) {
4524 sum.os_ffree += stat_buf.os_ffree;
4525 sum.os_files += stat_buf.os_files;
4526 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4527 sum.os_blocks += stat_buf.os_blocks *
4529 sum.os_bfree += stat_buf.os_bfree *
4531 sum.os_bavail += stat_buf.os_bavail *
4533 ost_ffree += stat_buf.os_ffree;
4541 /* If we don't have as many objects free on the OST as inodes
4542 * on the MDS, we reduce the total number of inodes to
4543 * compensate, so that the "inodes in use" number is correct.
4544 * Matches ll_statfs_internal() so the results are consistent. */
4545 if (ost_ffree < sum.os_ffree) {
4546 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4547 sum.os_ffree = ost_ffree;
4550 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4556 static int lfs_df(int argc, char **argv)
4558 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4559 enum mntdf_flags flags = 0;
4560 int c, rc = 0, index = 0;
4561 char fsname[PATH_MAX] = "", *pool_name = NULL;
4562 struct option long_opts[] = {
4563 { .val = 'h', .name = "human-readable",
4564 .has_arg = no_argument },
4565 { .val = 'i', .name = "inodes", .has_arg = no_argument },
4566 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4567 { .val = 'p', .name = "pool", .has_arg = required_argument },
4568 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4571 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
4574 flags |= MNTDF_COOKED;
4577 flags |= MNTDF_INODES;
4580 flags |= MNTDF_LAZY;
4586 flags |= MNTDF_VERBOSE;
4592 if (optind < argc && !realpath(argv[optind], path)) {
4594 fprintf(stderr, "error: invalid path '%s': %s\n",
4595 argv[optind], strerror(-rc));
4599 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4600 /* Check if we have a mount point */
4601 if (mntdir[0] == '\0')
4604 rc = mntdf(mntdir, fsname, pool_name, flags);
4605 if (rc || path[0] != '\0')
4607 fsname[0] = '\0'; /* avoid matching in next loop */
4608 mntdir[0] = '\0'; /* avoid matching in next loop */
4614 static int lfs_getname(int argc, char **argv)
4616 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
4617 int rc = 0, index = 0, c;
4618 char buf[sizeof(struct obd_uuid)];
4620 while ((c = getopt(argc, argv, "h")) != -1)
4623 if (optind == argc) { /* no paths specified, get all paths. */
4624 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4625 rc = llapi_getname(mntdir, buf, sizeof(buf));
4628 "cannot get name for `%s': %s\n",
4629 mntdir, strerror(-rc));
4633 printf("%s %s\n", buf, mntdir);
4635 path[0] = fsname[0] = mntdir[0] = 0;
4637 } else { /* paths specified, only attempt to search these. */
4638 for (; optind < argc; optind++) {
4639 rc = llapi_getname(argv[optind], buf, sizeof(buf));
4642 "cannot get name for `%s': %s\n",
4643 argv[optind], strerror(-rc));
4647 printf("%s %s\n", buf, argv[optind]);
4653 static int lfs_check(int argc, char **argv)
4656 char mntdir[PATH_MAX] = {'\0'};
4663 fprintf(stderr, "%s check: server type must be specified\n",
4668 obd_types[0] = obd_type1;
4669 obd_types[1] = obd_type2;
4671 if (strcmp(argv[1], "osts") == 0) {
4672 strcpy(obd_types[0], "osc");
4673 } else if (strcmp(argv[1], "mds") == 0) {
4674 strcpy(obd_types[0], "mdc");
4675 } else if (strcmp(argv[1], "servers") == 0) {
4677 strcpy(obd_types[0], "osc");
4678 strcpy(obd_types[1], "mdc");
4680 fprintf(stderr, "%s check: unrecognized option '%s'\n",
4685 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
4686 if (rc < 0 || mntdir[0] == '\0') {
4688 "%s check: cannot find mounted Lustre filesystem: %s\n",
4689 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
4693 rc = llapi_target_check(num_types, obd_types, mntdir);
4695 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
4696 progname, argv[1], strerror(-rc));
4702 #ifdef HAVE_SYS_QUOTA_H
4703 #define ARG2INT(nr, str, msg) \
4706 nr = strtol(str, &endp, 0); \
4707 if (*endp != '\0') { \
4708 fprintf(stderr, "%s: bad %s '%s'\n", \
4709 progname, msg, str); \
4714 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
4716 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
4717 * returns the value or ULONG_MAX on integer overflow or incorrect format
4719 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
4720 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
4721 * 3. empty integer value is interpreted as 0
4723 static unsigned long str2sec(const char* timestr)
4725 const char spec[] = "smhdw";
4726 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4727 unsigned long val = 0;
4730 if (strpbrk(timestr, spec) == NULL) {
4731 /* no specifiers inside the time string,
4732 should treat it as an integer value */
4733 val = strtoul(timestr, &tail, 10);
4734 return *tail ? ULONG_MAX : val;
4737 /* format string is XXwXXdXXhXXmXXs */
4743 v = strtoul(timestr, &tail, 10);
4744 if (v == ULONG_MAX || *tail == '\0')
4745 /* value too large (ULONG_MAX or more)
4746 or missing specifier */
4749 ptr = strchr(spec, *tail);
4751 /* unknown specifier */
4756 /* check if product will overflow the type */
4757 if (!(v < ULONG_MAX / mult[ind]))
4760 ADD_OVERFLOW(val, mult[ind] * v);
4761 if (val == ULONG_MAX)
4773 #define ARG2ULL(nr, str, def_units) \
4775 unsigned long long limit, units = def_units; \
4778 rc = llapi_parse_size(str, &limit, &units, 1); \
4780 fprintf(stderr, "%s: invalid limit '%s'\n", \
4787 static inline int has_times_option(int argc, char **argv)
4791 for (i = 1; i < argc; i++)
4792 if (!strcmp(argv[i], "-t"))
4798 int lfs_setquota_times(int argc, char **argv)
4801 struct if_quotactl qctl;
4802 char *mnt, *obd_type = (char *)qctl.obd_type;
4803 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4804 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
4805 struct option long_opts[] = {
4806 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
4807 { .val = 'g', .name = "group", .has_arg = no_argument },
4808 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
4809 { .val = 'p', .name = "projid", .has_arg = no_argument },
4810 { .val = 't', .name = "times", .has_arg = no_argument },
4811 { .val = 'u', .name = "user", .has_arg = no_argument },
4815 memset(&qctl, 0, sizeof(qctl));
4816 qctl.qc_cmd = LUSTRE_Q_SETINFO;
4817 qctl.qc_type = ALLQUOTA;
4819 while ((c = getopt_long(argc, argv, "b:gi:ptu",
4820 long_opts, NULL)) != -1) {
4831 if (qctl.qc_type != ALLQUOTA) {
4832 fprintf(stderr, "error: -u/g/p can't be used "
4833 "more than once\n");
4836 qctl.qc_type = qtype;
4839 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
4840 fprintf(stderr, "error: bad block-grace: %s\n",
4844 dqb->dqb_valid |= QIF_BTIME;
4847 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
4848 fprintf(stderr, "error: bad inode-grace: %s\n",
4852 dqb->dqb_valid |= QIF_ITIME;
4854 case 't': /* Yes, of course! */
4856 default: /* getopt prints error message for us when opterr != 0 */
4861 if (qctl.qc_type == ALLQUOTA) {
4862 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
4866 if (optind != argc - 1) {
4867 fprintf(stderr, "error: unexpected parameters encountered\n");
4872 rc = llapi_quotactl(mnt, &qctl);
4875 fprintf(stderr, "%s %s ", obd_type,
4876 obd_uuid2str(&qctl.obd_uuid));
4877 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
4884 #define BSLIMIT (1 << 0)
4885 #define BHLIMIT (1 << 1)
4886 #define ISLIMIT (1 << 2)
4887 #define IHLIMIT (1 << 3)
4889 int lfs_setquota(int argc, char **argv)
4892 struct if_quotactl qctl;
4893 char *mnt, *obd_type = (char *)qctl.obd_type;
4894 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4895 struct option long_opts[] = {
4896 { .val = 'b', .name = "block-softlimit",
4897 .has_arg = required_argument },
4898 { .val = 'B', .name = "block-hardlimit",
4899 .has_arg = required_argument },
4900 { .val = 'g', .name = "group", .has_arg = required_argument },
4901 { .val = 'i', .name = "inode-softlimit",
4902 .has_arg = required_argument },
4903 { .val = 'I', .name = "inode-hardlimit",
4904 .has_arg = required_argument },
4905 { .val = 'p', .name = "projid", .has_arg = required_argument },
4906 { .val = 'u', .name = "user", .has_arg = required_argument },
4908 unsigned limit_mask = 0;
4912 if (has_times_option(argc, argv))
4913 return lfs_setquota_times(argc, argv);
4915 memset(&qctl, 0, sizeof(qctl));
4916 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
4917 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
4918 * so it can be used as a marker that qc_type
4919 * isn't reinitialized from command line */
4921 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
4922 long_opts, NULL)) != -1) {
4926 rc = name2uid(&qctl.qc_id, optarg);
4930 rc = name2gid(&qctl.qc_id, optarg);
4934 rc = name2projid(&qctl.qc_id, optarg);
4936 if (qctl.qc_type != ALLQUOTA) {
4938 "%s setquota: only one of -u, -g or -p may be specified\n",
4942 qctl.qc_type = qtype;
4944 qctl.qc_id = strtoul(optarg, &endptr, 10);
4945 if (*endptr != '\0') {
4947 "%s setquota: invalid id '%s'\n",
4954 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
4955 dqb->dqb_bsoftlimit >>= 10;
4956 limit_mask |= BSLIMIT;
4957 if (dqb->dqb_bsoftlimit &&
4958 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
4960 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
4961 "See '%s help setquota' or Lustre manual for details\n",
4962 progname, dqb->dqb_bsoftlimit,
4966 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
4967 dqb->dqb_bhardlimit >>= 10;
4968 limit_mask |= BHLIMIT;
4969 if (dqb->dqb_bhardlimit &&
4970 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
4972 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
4973 "See '%s help setquota' or Lustre manual for details\n",
4974 progname, dqb->dqb_bhardlimit,
4978 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
4979 limit_mask |= ISLIMIT;
4980 if (dqb->dqb_isoftlimit &&
4981 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
4983 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
4984 "See '%s help setquota' or Lustre manual for details\n",
4985 progname, dqb->dqb_isoftlimit,
4989 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
4990 limit_mask |= IHLIMIT;
4991 if (dqb->dqb_ihardlimit &&
4992 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
4994 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
4995 "See '%s help setquota' or Lustre manual for details\n",
4996 progname, dqb->dqb_ihardlimit,
5001 "%s setquota: unrecognized option '%s'\n",
5002 progname, argv[optind - 1]);
5007 if (qctl.qc_type == ALLQUOTA) {
5009 "%s setquota: either -u or -g must be specified\n",
5014 if (limit_mask == 0) {
5016 "%s setquota: at least one limit must be specified\n",
5021 if (optind != argc - 1) {
5023 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5024 progname, argv[optind]);
5030 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5031 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5032 /* sigh, we can't just set blimits/ilimits */
5033 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5034 .qc_type = qctl.qc_type,
5035 .qc_id = qctl.qc_id};
5037 rc = llapi_quotactl(mnt, &tmp_qctl);
5041 if (!(limit_mask & BHLIMIT))
5042 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5043 if (!(limit_mask & BSLIMIT))
5044 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5045 if (!(limit_mask & IHLIMIT))
5046 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5047 if (!(limit_mask & ISLIMIT))
5048 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5050 /* Keep grace times if we have got no softlimit arguments */
5051 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5052 dqb->dqb_valid |= QIF_BTIME;
5053 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5056 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5057 dqb->dqb_valid |= QIF_ITIME;
5058 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5062 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5063 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5065 rc = llapi_quotactl(mnt, &qctl);
5069 "%s setquota: cannot quotactl '%s' '%s': %s",
5071 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5078 /* Converts seconds value into format string
5079 * result is returned in buf
5081 * 1. result is in descenting order: 1w2d3h4m5s
5082 * 2. zero fields are not filled (except for p. 3): 5d1s
5083 * 3. zero seconds value is presented as "0s"
5085 static char * __sec2str(time_t seconds, char *buf)
5087 const char spec[] = "smhdw";
5088 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5093 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5094 c = seconds / mult[i];
5096 if (c > 0 || (i == 0 && buf == tail))
5097 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5105 static void sec2str(time_t seconds, char *buf, int rc)
5112 tail = __sec2str(seconds, tail);
5114 if (rc && tail - buf < 39) {
5120 static void diff2str(time_t seconds, char *buf, time_t now)
5126 if (seconds <= now) {
5127 strcpy(buf, "none");
5130 __sec2str(seconds - now, buf);
5133 static void print_quota_title(char *name, struct if_quotactl *qctl,
5134 bool human_readable)
5136 printf("Disk quotas for %s %s (%cid %u):\n",
5137 qtype_name(qctl->qc_type), name,
5138 *qtype_name(qctl->qc_type), qctl->qc_id);
5139 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5140 "Filesystem", human_readable ? "used" : "kbytes",
5141 "quota", "limit", "grace",
5142 "files", "quota", "limit", "grace");
5145 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5148 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5151 snprintf(buf, buflen, "%5.4gP",
5152 (double)num / ((__u64)1 << 40));
5154 snprintf(buf, buflen, "%5.4gT",
5155 (double)num / (1 << 30));
5157 snprintf(buf, buflen, "%5.4gG",
5158 (double)num / (1 << 20));
5160 snprintf(buf, buflen, "%5.4gM",
5161 (double)num / (1 << 10));
5163 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5167 #define STRBUF_LEN 32
5168 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5175 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
5176 int bover = 0, iover = 0;
5177 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5178 char numbuf[3][STRBUF_LEN];
5180 char strbuf[STRBUF_LEN];
5182 if (dqb->dqb_bhardlimit &&
5183 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5185 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5186 if (dqb->dqb_btime > now) {
5193 if (dqb->dqb_ihardlimit &&
5194 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5196 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5197 if (dqb->dqb_itime > now) {
5205 if (strlen(mnt) > 15)
5206 printf("%s\n%15s", mnt, "");
5208 printf("%15s", mnt);
5211 diff2str(dqb->dqb_btime, timebuf, now);
5213 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5214 strbuf, sizeof(strbuf), h);
5215 if (rc == -EREMOTEIO)
5216 sprintf(numbuf[0], "%s*", strbuf);
5218 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5219 "%s" : "[%s]", strbuf);
5221 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5222 if (type == QC_GENERAL)
5223 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5224 "%s" : "[%s]", strbuf);
5226 sprintf(numbuf[1], "%s", "-");
5228 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5229 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5230 "%s" : "[%s]", strbuf);
5232 printf(" %7s%c %6s %7s %7s",
5233 numbuf[0], bover ? '*' : ' ', numbuf[1],
5234 numbuf[2], bover > 1 ? timebuf : "-");
5237 diff2str(dqb->dqb_itime, timebuf, now);
5239 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
5240 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
5242 if (type == QC_GENERAL)
5243 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5245 (uintmax_t)dqb->dqb_isoftlimit);
5247 sprintf(numbuf[1], "%s", "-");
5249 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5250 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5252 if (type != QC_OSTIDX)
5253 printf(" %7s%c %6s %7s %7s",
5254 numbuf[0], iover ? '*' : ' ', numbuf[1],
5255 numbuf[2], iover > 1 ? timebuf : "-");
5257 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5260 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5261 qctl->qc_cmd == Q_GETOINFO) {
5265 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5266 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5267 printf("Block grace time: %s; Inode grace time: %s\n",
5268 bgtimebuf, igtimebuf);
5272 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5273 bool h, __u64 *total)
5275 int rc = 0, rc1 = 0, count = 0;
5276 __u32 valid = qctl->qc_valid;
5278 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5280 fprintf(stderr, "can not get %s count: %s\n",
5281 is_mdt ? "mdt": "ost", strerror(-rc));
5285 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5286 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5287 rc = llapi_quotactl(mnt, qctl);
5289 /* It is remote client case. */
5290 if (rc == -EOPNOTSUPP) {
5297 fprintf(stderr, "quotactl %s%d failed.\n",
5298 is_mdt ? "mdt": "ost", qctl->qc_idx);
5302 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5303 qctl->qc_valid, 0, h);
5304 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5305 qctl->qc_dqblk.dqb_bhardlimit;
5308 qctl->qc_valid = valid;
5312 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5313 int verbose, int quiet, bool human_readable)
5315 int rc1 = 0, rc2 = 0, rc3 = 0;
5316 char *obd_type = (char *)qctl->obd_type;
5317 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5318 __u64 total_ialloc = 0, total_balloc = 0;
5321 rc1 = llapi_quotactl(mnt, qctl);
5325 fprintf(stderr, "%s quotas are not enabled.\n",
5326 qtype_name(qctl->qc_type));
5329 fprintf(stderr, "Permission denied.\n");
5332 /* We already got error message. */
5335 fprintf(stderr, "Unexpected quotactl error: %s\n",
5340 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
5341 print_quota_title(name, qctl, human_readable);
5343 if (rc1 && *obd_type)
5344 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5346 if (qctl->qc_valid != QC_GENERAL)
5349 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5350 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5351 (QIF_LIMITS|QIF_USAGE));
5353 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
5355 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
5357 char strbuf[STRBUF_LEN];
5359 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5361 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5363 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5365 printf("Total allocated inode limit: %ju, total "
5366 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5370 if (rc1 || rc2 || rc3 || inacc)
5371 printf("Some errors happened when getting quota info. "
5372 "Some devices may be not working or deactivated. "
5373 "The data in \"[]\" is inaccurate.\n");
5379 static int lfs_project(int argc, char **argv)
5381 int ret = 0, err = 0, c, i;
5382 struct project_handle_control phc = { 0 };
5383 enum lfs_project_ops_t op;
5386 phc.assign_projid = false;
5387 /* default action */
5388 op = LFS_PROJECT_LIST;
5390 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
5393 if (op != LFS_PROJECT_LIST) {
5395 "%s: cannot specify '-c' '-C' '-s' together\n",
5400 op = LFS_PROJECT_CHECK;
5403 if (op != LFS_PROJECT_LIST) {
5405 "%s: cannot specify '-c' '-C' '-s' together\n",
5410 op = LFS_PROJECT_CLEAR;
5413 if (op != LFS_PROJECT_LIST) {
5415 "%s: cannot specify '-c' '-C' '-s' together\n",
5420 phc.set_inherit = true;
5421 op = LFS_PROJECT_SET;
5427 phc.keep_projid = true;
5430 phc.recursive = true;
5433 phc.projid = strtoul(optarg, NULL, 0);
5434 phc.assign_projid = true;
5438 phc.newline = false;
5441 fprintf(stderr, "%s: invalid option '%c'\n",
5447 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
5448 op = LFS_PROJECT_SET;
5449 phc.set_projid = true;
5450 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
5451 phc.set_projid = true;
5455 case LFS_PROJECT_CHECK:
5456 if (phc.keep_projid) {
5458 "%s: '-k' is useless together with '-c'\n",
5463 case LFS_PROJECT_CLEAR:
5466 "%s: '-0' is useless together with '-C'\n",
5470 if (phc.assign_projid) {
5472 "%s: '-p' is useless together with '-C'\n",
5477 case LFS_PROJECT_SET:
5480 "%s: '-0' is useless together with '-s'\n",
5484 if (phc.keep_projid) {
5486 "%s: '-k' is useless together with '-s'\n",
5494 "%s: '-0' is useless for list operations\n",
5504 fprintf(stderr, "%s: missing file or directory target(s)\n",
5509 for (i = 0; i < argc; i++) {
5511 case LFS_PROJECT_CHECK:
5512 err = lfs_project_check(argv[i], &phc);
5514 case LFS_PROJECT_LIST:
5515 err = lfs_project_list(argv[i], &phc);
5517 case LFS_PROJECT_CLEAR:
5518 err = lfs_project_clear(argv[i], &phc);
5520 case LFS_PROJECT_SET:
5521 err = lfs_project_set(argv[i], &phc);
5533 static int lfs_quota(int argc, char **argv)
5536 char *mnt, *name = NULL;
5537 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
5538 .qc_type = ALLQUOTA };
5539 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
5540 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
5542 __u32 valid = QC_GENERAL, idx = 0;
5543 bool human_readable = false;
5546 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
5557 if (qctl.qc_type != ALLQUOTA) {
5559 "%s quota: only one of -u, -g, or -p may be specified\n",
5563 qctl.qc_type = qtype;
5566 qctl.qc_cmd = LUSTRE_Q_GETINFO;
5569 valid = qctl.qc_valid = QC_UUID;
5570 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
5573 valid = qctl.qc_valid = QC_MDTIDX;
5574 idx = qctl.qc_idx = atoi(optarg);
5575 if (idx == 0 && *optarg != '0') {
5577 "%s quota: invalid MDT index '%s'\n",
5583 valid = qctl.qc_valid = QC_OSTIDX;
5584 idx = qctl.qc_idx = atoi(optarg);
5585 if (idx == 0 && *optarg != '0') {
5587 "%s quota: invalid OST index '%s'\n",
5599 human_readable = true;
5602 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
5603 progname, argv[optind - 1]);
5608 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
5609 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
5610 optind == argc - 1) {
5612 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
5613 qctl.qc_valid = valid;
5616 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
5617 qctl.qc_type = qtype;
5618 if (qtype == USRQUOTA) {
5619 qctl.qc_id = geteuid();
5620 rc = uid2name(&name, qctl.qc_id);
5622 qctl.qc_id = getegid();
5623 rc = gid2name(&name, qctl.qc_id);
5628 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
5633 /* lfs quota -u username /path/to/lustre/mount */
5634 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
5635 /* options should be followed by u/g-name and mntpoint */
5636 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
5638 "%s quota: name and mount point must be specified\n",
5643 name = argv[optind++];
5644 switch (qctl.qc_type) {
5646 rc = name2uid(&qctl.qc_id, name);
5649 rc = name2gid(&qctl.qc_id, name);
5652 rc = name2projid(&qctl.qc_id, name);
5659 qctl.qc_id = strtoul(name, &endptr, 10);
5660 if (*endptr != '\0') {
5661 fprintf(stderr, "%s quota: invalid id '%s'\n",
5667 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
5669 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
5670 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
5677 #endif /* HAVE_SYS_QUOTA_H! */
5679 static int flushctx_ioctl(char *mp)
5683 fd = open(mp, O_RDONLY);
5685 fprintf(stderr, "flushctx: error open %s: %s\n",
5686 mp, strerror(errno));
5690 rc = ioctl(fd, LL_IOC_FLUSHCTX);
5692 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
5693 mp, strerror(errno));
5699 static int lfs_flushctx(int argc, char **argv)
5701 int kdestroy = 0, c;
5702 char mntdir[PATH_MAX] = {'\0'};
5706 while ((c = getopt(argc, argv, "k")) != -1) {
5712 fprintf(stderr, "error: %s: option '-%c' "
5713 "unrecognized\n", argv[0], c);
5719 if ((rc = system("kdestroy > /dev/null")) != 0) {
5720 rc = WEXITSTATUS(rc);
5721 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
5725 if (optind >= argc) {
5726 /* flush for all mounted lustre fs. */
5727 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
5728 /* Check if we have a mount point */
5729 if (mntdir[0] == '\0')
5732 if (flushctx_ioctl(mntdir))
5735 mntdir[0] = '\0'; /* avoid matching in next loop */
5738 /* flush fs as specified */
5739 while (optind < argc) {
5740 if (flushctx_ioctl(argv[optind++]))
5747 static int lfs_cp(int argc, char **argv)
5749 fprintf(stderr, "remote client copy file(s).\n"
5750 "obsolete, does not support it anymore.\n");
5754 static int lfs_ls(int argc, char **argv)
5756 fprintf(stderr, "remote client lists directory contents.\n"
5757 "obsolete, does not support it anymore.\n");
5761 static int lfs_changelog(int argc, char **argv)
5763 void *changelog_priv;
5764 struct changelog_rec *rec;
5765 long long startrec = 0, endrec = 0;
5767 struct option long_opts[] = {
5768 { .val = 'f', .name = "follow", .has_arg = no_argument },
5770 char short_opts[] = "f";
5773 while ((rc = getopt_long(argc, argv, short_opts,
5774 long_opts, NULL)) != -1) {
5782 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5783 argv[0], argv[optind - 1]);
5790 mdd = argv[optind++];
5792 startrec = strtoll(argv[optind++], NULL, 10);
5794 endrec = strtoll(argv[optind++], NULL, 10);
5796 rc = llapi_changelog_start(&changelog_priv,
5797 CHANGELOG_FLAG_BLOCK |
5798 CHANGELOG_FLAG_JOBID |
5799 CHANGELOG_FLAG_EXTRA_FLAGS |
5800 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
5803 fprintf(stderr, "Can't start changelog: %s\n",
5804 strerror(errno = -rc));
5808 rc = llapi_changelog_set_xflags(changelog_priv,
5809 CHANGELOG_EXTRA_FLAG_UIDGID |
5810 CHANGELOG_EXTRA_FLAG_NID |
5811 CHANGELOG_EXTRA_FLAG_OMODE);
5813 fprintf(stderr, "Can't set xflags for changelog: %s\n",
5814 strerror(errno = -rc));
5818 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
5822 if (endrec && rec->cr_index > endrec) {
5823 llapi_changelog_free(&rec);
5826 if (rec->cr_index < startrec) {
5827 llapi_changelog_free(&rec);
5831 secs = rec->cr_time >> 30;
5832 gmtime_r(&secs, &ts);
5833 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
5834 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
5835 changelog_type2str(rec->cr_type),
5836 ts.tm_hour, ts.tm_min, ts.tm_sec,
5837 (int)(rec->cr_time & ((1 << 30) - 1)),
5838 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
5839 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
5841 if (rec->cr_flags & CLF_JOBID) {
5842 struct changelog_ext_jobid *jid =
5843 changelog_rec_jobid(rec);
5845 if (jid->cr_jobid[0] != '\0')
5846 printf(" j=%s", jid->cr_jobid);
5849 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
5850 struct changelog_ext_extra_flags *ef =
5851 changelog_rec_extra_flags(rec);
5853 printf(" ef=0x%llx", ef->cr_extra_flags);
5855 if (ef->cr_extra_flags & CLFE_UIDGID) {
5856 struct changelog_ext_uidgid *uidgid =
5857 changelog_rec_uidgid(rec);
5859 printf(" u=%llu:%llu",
5860 uidgid->cr_uid, uidgid->cr_gid);
5862 if (ef->cr_extra_flags & CLFE_NID) {
5863 struct changelog_ext_nid *nid =
5864 changelog_rec_nid(rec);
5867 libcfs_nid2str(nid->cr_nid));
5870 if (ef->cr_extra_flags & CLFE_OPEN) {
5871 struct changelog_ext_openmode *omd =
5872 changelog_rec_openmode(rec);
5873 char mode[] = "---";
5875 /* exec mode must be exclusive */
5876 if (omd->cr_openflags & MDS_FMODE_EXEC) {
5879 if (omd->cr_openflags & FMODE_READ)
5881 if (omd->cr_openflags &
5888 if (strcmp(mode, "---") != 0)
5889 printf(" m=%s", mode);
5894 if (rec->cr_namelen)
5895 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
5896 rec->cr_namelen, changelog_rec_name(rec));
5898 if (rec->cr_flags & CLF_RENAME) {
5899 struct changelog_ext_rename *rnm =
5900 changelog_rec_rename(rec);
5902 if (!fid_is_zero(&rnm->cr_sfid))
5903 printf(" s="DFID" sp="DFID" %.*s",
5904 PFID(&rnm->cr_sfid),
5905 PFID(&rnm->cr_spfid),
5906 (int)changelog_rec_snamelen(rec),
5907 changelog_rec_sname(rec));
5911 llapi_changelog_free(&rec);
5914 llapi_changelog_fini(&changelog_priv);
5917 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
5919 return (rc == 1 ? 0 : rc);
5922 static int lfs_changelog_clear(int argc, char **argv)
5930 endrec = strtoll(argv[3], NULL, 10);
5932 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
5935 fprintf(stderr, "%s: record out of range: %llu\n",
5937 else if (rc == -ENOENT)
5938 fprintf(stderr, "%s: no changelog user: %s\n",
5941 fprintf(stderr, "%s error: %s\n", argv[0],
5950 static int lfs_fid2path(int argc, char **argv)
5952 struct option long_opts[] = {
5953 { .val = 'c', .name = "cur", .has_arg = no_argument },
5954 { .val = 'l', .name = "link", .has_arg = required_argument },
5955 { .val = 'r', .name = "rec", .has_arg = required_argument },
5957 char short_opts[] = "cl:r:";
5958 char *device, *fid, *path;
5959 long long recno = -1;
5965 while ((rc = getopt_long(argc, argv, short_opts,
5966 long_opts, NULL)) != -1) {
5972 linkno = strtol(optarg, NULL, 10);
5975 recno = strtoll(optarg, NULL, 10);
5980 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5981 argv[0], argv[optind - 1]);
5989 device = argv[optind++];
5990 path = calloc(1, PATH_MAX);
5992 fprintf(stderr, "error: Not enough memory\n");
5997 while (optind < argc) {
5998 fid = argv[optind++];
6000 lnktmp = (linkno >= 0) ? linkno : 0;
6002 int oldtmp = lnktmp;
6003 long long rectmp = recno;
6005 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6008 fprintf(stderr, "%s: error on FID %s: %s\n",
6009 argv[0], fid, strerror(errno = -rc2));
6016 fprintf(stdout, "%lld ", rectmp);
6017 if (device[0] == '/') {
6018 fprintf(stdout, "%s", device);
6019 if (device[strlen(device) - 1] != '/')
6020 fprintf(stdout, "/");
6021 } else if (path[0] == '\0') {
6022 fprintf(stdout, "/");
6024 fprintf(stdout, "%s\n", path);
6027 /* specified linkno */
6029 if (oldtmp == lnktmp)
6039 static int lfs_path2fid(int argc, char **argv)
6041 struct option long_opts[] = {
6042 { .val = 'p', .name = "parents", .has_arg = no_argument },
6045 const char short_opts[] = "p";
6046 const char *sep = "";
6049 bool show_parents = false;
6051 while ((rc = getopt_long(argc, argv, short_opts,
6052 long_opts, NULL)) != -1) {
6055 show_parents = true;
6058 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6059 argv[0], argv[optind - 1]);
6064 if (optind > argc - 1)
6066 else if (optind < argc - 1)
6070 for (path = argv + optind; *path != NULL; path++) {
6072 if (!show_parents) {
6073 err = llapi_path2fid(*path, &fid);
6075 printf("%s%s"DFID"\n",
6076 *sep != '\0' ? *path : "", sep,
6079 char name[NAME_MAX + 1];
6080 unsigned int linkno = 0;
6082 while ((err = llapi_path2parent(*path, linkno, &fid,
6083 name, sizeof(name))) == 0) {
6084 if (*sep != '\0' && linkno == 0)
6085 printf("%s%s", *path, sep);
6087 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6092 /* err == -ENODATA is end-of-loop */
6093 if (linkno > 0 && err == -ENODATA) {
6100 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
6101 argv[0], show_parents ? "parent " : "", *path,
6113 static int lfs_data_version(int argc, char **argv)
6120 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
6125 while ((c = getopt(argc, argv, "nrw")) != -1) {
6128 data_version_flags = 0;
6131 data_version_flags |= LL_DV_RD_FLUSH;
6134 data_version_flags |= LL_DV_WR_FLUSH;
6143 path = argv[optind];
6144 fd = open(path, O_RDONLY);
6146 err(errno, "cannot open file %s", path);
6148 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
6150 err(errno, "cannot get version for %s", path);
6152 printf("%ju" "\n", (uintmax_t)data_version);
6158 static int lfs_hsm_state(int argc, char **argv)
6163 struct hsm_user_state hus;
6171 rc = llapi_hsm_state_get(path, &hus);
6173 fprintf(stderr, "can't get hsm state for %s: %s\n",
6174 path, strerror(errno = -rc));
6178 /* Display path name and status flags */
6179 printf("%s: (0x%08x)", path, hus.hus_states);
6181 if (hus.hus_states & HS_RELEASED)
6182 printf(" released");
6183 if (hus.hus_states & HS_EXISTS)
6185 if (hus.hus_states & HS_DIRTY)
6187 if (hus.hus_states & HS_ARCHIVED)
6188 printf(" archived");
6189 /* Display user-settable flags */
6190 if (hus.hus_states & HS_NORELEASE)
6191 printf(" never_release");
6192 if (hus.hus_states & HS_NOARCHIVE)
6193 printf(" never_archive");
6194 if (hus.hus_states & HS_LOST)
6195 printf(" lost_from_hsm");
6197 if (hus.hus_archive_id != 0)
6198 printf(", archive_id:%d", hus.hus_archive_id);
6201 } while (++i < argc);
6206 #define LFS_HSM_SET 0
6207 #define LFS_HSM_CLEAR 1
6210 * Generic function to set or clear HSM flags.
6211 * Used by hsm_set and hsm_clear.
6213 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
6215 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
6217 struct option long_opts[] = {
6218 { .val = 'A', .name = "archived", .has_arg = no_argument },
6219 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
6220 { .val = 'd', .name = "dirty", .has_arg = no_argument },
6221 { .val = 'e', .name = "exists", .has_arg = no_argument },
6222 { .val = 'l', .name = "lost", .has_arg = no_argument },
6223 { .val = 'r', .name = "norelease", .has_arg = no_argument },
6224 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
6226 char short_opts[] = "lraAdei:";
6230 __u32 archive_id = 0;
6236 while ((c = getopt_long(argc, argv, short_opts,
6237 long_opts, NULL)) != -1) {
6243 mask |= HS_NOARCHIVE;
6246 mask |= HS_ARCHIVED;
6249 mask |= HS_NORELEASE;
6258 archive_id = strtol(optarg, &end, 10);
6260 fprintf(stderr, "invalid archive_id: '%s'\n",
6268 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6269 argv[0], argv[optind - 1]);
6274 /* User should have specified a flag */
6278 while (optind < argc) {
6280 path = argv[optind];
6282 /* If mode == 0, this means we apply the mask. */
6283 if (mode == LFS_HSM_SET)
6284 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6286 rc = llapi_hsm_state_set(path, 0, mask, 0);
6289 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6290 path, strerror(errno = -rc));
6299 static int lfs_hsm_action(int argc, char **argv)
6304 struct hsm_current_action hca;
6305 struct hsm_extent he;
6306 enum hsm_user_action hua;
6307 enum hsm_progress_states hps;
6315 rc = llapi_hsm_current_action(path, &hca);
6317 fprintf(stderr, "can't get hsm action for %s: %s\n",
6318 path, strerror(errno = -rc));
6321 he = hca.hca_location;
6322 hua = hca.hca_action;
6323 hps = hca.hca_state;
6325 printf("%s: %s", path, hsm_user_action2name(hua));
6327 /* Skip file without action */
6328 if (hca.hca_action == HUA_NONE) {
6333 printf(" %s ", hsm_progress_state2name(hps));
6335 if ((hps == HPS_RUNNING) &&
6336 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
6337 printf("(%llu bytes moved)\n",
6338 (unsigned long long)he.length);
6339 else if ((he.offset + he.length) == LUSTRE_EOF)
6340 printf("(from %llu to EOF)\n",
6341 (unsigned long long)he.offset);
6343 printf("(from %llu to %llu)\n",
6344 (unsigned long long)he.offset,
6345 (unsigned long long)(he.offset + he.length));
6347 } while (++i < argc);
6352 static int lfs_hsm_set(int argc, char **argv)
6354 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
6357 static int lfs_hsm_clear(int argc, char **argv)
6359 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
6363 * Check file state and return its fid, to be used by lfs_hsm_request().
6365 * \param[in] file Path to file to check
6366 * \param[in,out] fid Pointer to allocated lu_fid struct.
6367 * \param[in,out] last_dev Pointer to last device id used.
6369 * \return 0 on success.
6371 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
6377 rc = lstat(file, &st);
6379 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
6382 /* Checking for regular file as archiving as posix copytool
6383 * rejects archiving files other than regular files
6385 if (!S_ISREG(st.st_mode)) {
6386 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
6389 /* A request should be ... */
6390 if (*last_dev != st.st_dev && *last_dev != 0) {
6391 fprintf(stderr, "All files should be "
6392 "on the same filesystem: %s\n", file);
6395 *last_dev = st.st_dev;
6397 rc = llapi_path2fid(file, fid);
6399 fprintf(stderr, "Cannot read FID of %s: %s\n",
6400 file, strerror(-rc));
6406 /* Fill an HSM HUR item with a given file name.
6408 * If mntpath is set, then the filename is actually a FID, and no
6409 * lookup on the filesystem will be performed.
6411 * \param[in] hur the user request to fill
6412 * \param[in] idx index of the item inside the HUR to fill
6413 * \param[in] mntpath mountpoint of Lustre
6414 * \param[in] fname filename (if mtnpath is NULL)
6415 * or FID (if mntpath is set)
6416 * \param[in] last_dev pointer to last device id used
6418 * \retval 0 on success
6419 * \retval CMD_HELP or a negative errno on error
6421 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
6422 const char *mntpath, const char *fname,
6425 struct hsm_user_item *hui = &hur->hur_user_item[idx];
6428 hui->hui_extent.length = -1;
6430 if (mntpath != NULL) {
6433 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
6437 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
6442 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
6446 hur->hur_request.hr_itemcount++;
6451 static int lfs_hsm_request(int argc, char **argv, int action)
6453 struct option long_opts[] = {
6454 { .val = 'a', .name = "archive", .has_arg = required_argument },
6455 { .val = 'D', .name = "data", .has_arg = required_argument },
6456 { .val = 'l', .name = "filelist", .has_arg = required_argument },
6457 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
6460 char short_opts[] = "l:D:a:m:";
6461 struct hsm_user_request *hur, *oldhur;
6466 char *filelist = NULL;
6467 char fullpath[PATH_MAX];
6468 char *opaque = NULL;
6472 int nbfile_alloc = 0;
6473 char *some_file = NULL;
6474 char *mntpath = NULL;
6480 while ((c = getopt_long(argc, argv, short_opts,
6481 long_opts, NULL)) != -1) {
6490 if (action != HUA_ARCHIVE &&
6491 action != HUA_REMOVE) {
6493 "error: -a is supported only "
6494 "when archiving or removing\n");
6497 archive_id = atoi(optarg);
6500 if (some_file == NULL) {
6502 some_file = strdup(optarg);
6508 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6509 argv[0], argv[optind - 1]);
6514 /* All remaining args are files, so we have at least nbfile */
6515 nbfile = argc - optind;
6517 if ((nbfile == 0) && (filelist == NULL))
6521 opaque_len = strlen(opaque);
6523 /* Alloc the request structure with enough place to store all files
6524 * from command line. */
6525 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
6527 fprintf(stderr, "Cannot create the request: %s\n",
6531 nbfile_alloc = nbfile;
6533 hur->hur_request.hr_action = action;
6534 hur->hur_request.hr_archive_id = archive_id;
6535 hur->hur_request.hr_flags = 0;
6537 /* All remaining args are files, add them */
6538 if (nbfile != 0 && some_file == NULL)
6539 some_file = strdup(argv[optind]);
6541 for (i = 0; i < nbfile; i++) {
6542 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
6548 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
6550 /* If a filelist was specified, read the filelist from it. */
6551 if (filelist != NULL) {
6552 fp = fopen(filelist, "r");
6554 fprintf(stderr, "Cannot read the file list %s: %s\n",
6555 filelist, strerror(errno));
6560 while ((rc = getline(&line, &len, fp)) != -1) {
6561 /* If allocated buffer was too small, get something
6563 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
6566 nbfile_alloc = nbfile_alloc * 2 + 1;
6568 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
6571 fprintf(stderr, "hsm: cannot allocate "
6572 "the request: %s\n",
6579 size = hur_len(oldhur);
6581 fprintf(stderr, "hsm: cannot allocate "
6582 "%u files + %u bytes data\n",
6583 oldhur->hur_request.hr_itemcount,
6584 oldhur->hur_request.hr_data_len);
6591 memcpy(hur, oldhur, size);
6596 if (line[strlen(line) - 1] == '\n')
6597 line[strlen(line) - 1] = '\0';
6599 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
6600 mntpath, line, &last_dev);
6606 if (some_file == NULL) {
6616 /* If a --data was used, add it to the request */
6617 hur->hur_request.hr_data_len = opaque_len;
6619 memcpy(hur_data(hur), opaque, opaque_len);
6621 /* Send the HSM request */
6622 if (realpath(some_file, fullpath) == NULL) {
6623 fprintf(stderr, "Could not find path '%s': %s\n",
6624 some_file, strerror(errno));
6626 rc = llapi_hsm_request(fullpath, hur);
6628 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
6629 some_file, strerror(-rc));
6639 static int lfs_hsm_archive(int argc, char **argv)
6641 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
6644 static int lfs_hsm_restore(int argc, char **argv)
6646 return lfs_hsm_request(argc, argv, HUA_RESTORE);
6649 static int lfs_hsm_release(int argc, char **argv)
6651 return lfs_hsm_request(argc, argv, HUA_RELEASE);
6654 static int lfs_hsm_remove(int argc, char **argv)
6656 return lfs_hsm_request(argc, argv, HUA_REMOVE);
6659 static int lfs_hsm_cancel(int argc, char **argv)
6661 return lfs_hsm_request(argc, argv, HUA_CANCEL);
6664 static int lfs_swap_layouts(int argc, char **argv)
6669 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
6670 SWAP_LAYOUTS_KEEP_MTIME |
6671 SWAP_LAYOUTS_KEEP_ATIME);
6674 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
6676 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
6678 int lfs_get_mode(const char *string)
6680 enum lock_mode_user mode;
6682 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
6683 if (lock_mode_names[mode] == NULL)
6685 if (strcmp(string, lock_mode_names[mode]) == 0)
6692 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
6694 enum lu_ladvise_type advice;
6697 advice < ARRAY_SIZE(ladvise_names); advice++) {
6698 if (ladvise_names[advice] == NULL)
6700 if (strcmp(string, ladvise_names[advice]) == 0)
6704 return LU_LADVISE_INVALID;
6707 static int lfs_ladvise(int argc, char **argv)
6709 struct option long_opts[] = {
6710 { .val = 'a', .name = "advice", .has_arg = required_argument },
6711 { .val = 'b', .name = "background", .has_arg = no_argument },
6712 { .val = 'e', .name = "end", .has_arg = required_argument },
6713 { .val = 'l', .name = "length", .has_arg = required_argument },
6714 { .val = 'm', .name = "mode", .has_arg = required_argument },
6715 { .val = 's', .name = "start", .has_arg = required_argument },
6716 { .val = 'u', .name = "unset", .has_arg = no_argument },
6718 char short_opts[] = "a:be:l:m:s:u";
6723 struct llapi_lu_ladvise advice;
6724 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
6725 unsigned long long start = 0;
6726 unsigned long long end = LUSTRE_EOF;
6727 unsigned long long length = 0;
6728 unsigned long long size_units;
6729 unsigned long long flags = 0;
6733 while ((c = getopt_long(argc, argv, short_opts,
6734 long_opts, NULL)) != -1) {
6737 advice_type = lfs_get_ladvice(optarg);
6738 if (advice_type == LU_LADVISE_INVALID) {
6739 fprintf(stderr, "%s: invalid advice type "
6740 "'%s'\n", argv[0], optarg);
6741 fprintf(stderr, "Valid types:");
6743 for (advice_type = 0;
6744 advice_type < ARRAY_SIZE(ladvise_names);
6746 if (ladvise_names[advice_type] == NULL)
6748 fprintf(stderr, " %s",
6749 ladvise_names[advice_type]);
6751 fprintf(stderr, "\n");
6764 rc = llapi_parse_size(optarg, &end,
6767 fprintf(stderr, "%s: bad end offset '%s'\n",
6774 rc = llapi_parse_size(optarg, &start,
6777 fprintf(stderr, "%s: bad start offset "
6778 "'%s'\n", argv[0], optarg);
6784 rc = llapi_parse_size(optarg, &length,
6787 fprintf(stderr, "%s: bad length '%s'\n",
6793 mode = lfs_get_mode(optarg);
6795 fprintf(stderr, "%s: bad mode '%s', valid "
6796 "modes are READ or WRITE\n",
6804 fprintf(stderr, "%s: option '%s' unrecognized\n",
6805 argv[0], argv[optind - 1]);
6810 if (advice_type == LU_LADVISE_INVALID) {
6811 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
6812 fprintf(stderr, "Valid types:");
6813 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
6815 if (ladvise_names[advice_type] == NULL)
6817 fprintf(stderr, " %s", ladvise_names[advice_type]);
6819 fprintf(stderr, "\n");
6823 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
6824 fprintf(stderr, "%s: Lock no expand advice is a per file "
6825 "descriptor advice, so when called from lfs, "
6826 "it does nothing.\n", argv[0]);
6830 if (argc <= optind) {
6831 fprintf(stderr, "%s: please give one or more file names\n",
6836 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
6837 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
6842 if (end == LUSTRE_EOF && length != 0)
6843 end = start + length;
6846 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
6847 argv[0], start, end);
6851 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
6852 fprintf(stderr, "%s: mode is only valid with lockahead\n",
6857 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
6858 fprintf(stderr, "%s: mode is required with lockahead\n",
6863 while (optind < argc) {
6866 path = argv[optind++];
6868 fd = open(path, O_RDONLY);
6870 fprintf(stderr, "%s: cannot open file '%s': %s\n",
6871 argv[0], path, strerror(errno));
6876 advice.lla_start = start;
6877 advice.lla_end = end;
6878 advice.lla_advice = advice_type;
6879 advice.lla_value1 = 0;
6880 advice.lla_value2 = 0;
6881 advice.lla_value3 = 0;
6882 advice.lla_value4 = 0;
6883 if (advice_type == LU_LADVISE_LOCKAHEAD) {
6884 advice.lla_lockahead_mode = mode;
6885 advice.lla_peradvice_flags = flags;
6888 rc2 = llapi_ladvise(fd, flags, 1, &advice);
6891 fprintf(stderr, "%s: cannot give advice '%s' to file "
6892 "'%s': %s\n", argv[0],
6893 ladvise_names[advice_type],
6894 path, strerror(errno));
6900 if (rc == 0 && rc2 < 0)
6906 /** The input string contains a comma delimited list of component ids and
6907 * ranges, for example "1,2-4,7".
6909 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
6911 bool end_of_loop = false;
6919 while (!end_of_loop) {
6923 char *endptr = NULL;
6926 ptr = strchrnul(arg, ',');
6927 end_of_loop = *ptr == '\0';
6930 start_index = strtol(arg, &endptr, 0);
6931 if (endptr == arg) /* no data at all */
6933 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
6935 if (start_index < 0)
6938 end_index = start_index;
6939 if (*endptr == '-') {
6940 end_index = strtol(endptr + 1, &endptr, 0);
6941 if (*endptr != '\0')
6943 if (end_index < start_index)
6947 for (i = start_index; i <= end_index && size > 0; i++) {
6950 /* remove duplicate */
6951 for (j = 0; j < nr; j++) {
6955 if (j == nr) { /* no duplicate */
6961 if (size == 0 && i < end_index)
6968 if (!end_of_loop && ptr != NULL)
6971 return rc < 0 ? rc : nr;
6975 * struct verify_mirror_id - Mirror id to be verified.
6976 * @mirror_id: A specified mirror id.
6977 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
6979 struct verify_mirror_id {
6985 * compare_mirror_ids() - Compare mirror ids.
6986 * @layout: Mirror component list.
6987 * @cbdata: Callback data in verify_mirror_id structure.
6989 * This is a callback function called by llapi_layout_comp_iterate()
6990 * to compare the specified mirror id with the one in the current
6991 * component of @layout. If they are the same, then the specified
6992 * mirror id is valid.
6994 * Return: a negative error code on failure or
6995 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
6996 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
6999 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7001 struct verify_mirror_id *mirror_id_cbdata =
7002 (struct verify_mirror_id *)cbdata;
7006 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7010 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7011 progname, strerror(errno));
7015 if (mirror_id_cbdata->mirror_id == mirror_id) {
7016 mirror_id_cbdata->is_valid_id = true;
7017 return LLAPI_LAYOUT_ITER_STOP;
7020 return LLAPI_LAYOUT_ITER_CONT;
7024 * verify_mirror_ids() - Verify specified mirror ids.
7025 * @fname: Mirrored file name.
7026 * @mirror_ids: Specified mirror ids to be verified.
7027 * @ids_nr: Number of specified mirror ids.
7029 * This function verifies that specified @mirror_ids are valid
7030 * in the mirrored file @fname.
7032 * Return: 0 on success or a negative error code on failure.
7035 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7037 struct llapi_layout *layout = NULL;
7038 struct verify_mirror_id mirror_id_cbdata = { 0 };
7049 if (stat(fname, &stbuf) < 0) {
7050 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7051 progname, fname, strerror(errno));
7056 if (!S_ISREG(stbuf.st_mode)) {
7057 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7063 fd = open(fname, O_DIRECT | O_RDONLY);
7065 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7066 progname, fname, strerror(errno));
7071 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
7073 fprintf(stderr, "%s: '%s' llapi_lease_get failed: %s.\n",
7074 progname, fname, strerror(errno));
7078 layout = llapi_layout_get_by_fd(fd, 0);
7079 if (layout == NULL) {
7080 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7081 progname, fname, strerror(errno));
7083 llapi_lease_put(fd);
7087 rc = llapi_layout_flags_get(layout, &flr_state);
7089 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7090 progname, fname, strerror(errno));
7095 flr_state &= LCM_FL_FLR_MASK;
7096 switch (flr_state) {
7097 case LCM_FL_NOT_FLR:
7099 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7100 progname, fname, lcm_flags_string(flr_state));
7107 for (i = 0; i < ids_nr; i++) {
7108 mirror_id_cbdata.mirror_id = mirror_ids[i];
7109 mirror_id_cbdata.is_valid_id = false;
7111 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
7116 "%s: '%s' failed to verify mirror id: %u.\n",
7117 progname, fname, mirror_ids[i]);
7121 if (!mirror_id_cbdata.is_valid_id) {
7124 "%s: '%s' invalid specified mirror id: %u.\n",
7125 progname, fname, mirror_ids[i]);
7131 llapi_layout_free(layout);
7132 llapi_lease_put(fd);
7140 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
7141 __u16 *mirror_ids, int ids_nr)
7143 struct llapi_resync_comp comp_array[1024] = { { 0 } };
7144 struct llapi_layout *layout;
7152 if (stat(fname, &stbuf) < 0) {
7153 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7154 progname, fname, strerror(errno));
7158 if (!S_ISREG(stbuf.st_mode)) {
7159 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7165 fd = open(fname, O_DIRECT | O_RDWR);
7167 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7168 progname, fname, strerror(errno));
7173 ioc->lil_mode = LL_LEASE_WRLCK;
7174 ioc->lil_flags = LL_LEASE_RESYNC;
7175 rc = llapi_lease_set(fd, ioc);
7178 "%s: '%s' llapi_lease_set resync failed: %s.\n",
7179 progname, fname, strerror(errno));
7183 layout = llapi_layout_get_by_fd(fd, 0);
7184 if (layout == NULL) {
7185 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7186 progname, fname, strerror(errno));
7191 rc = llapi_layout_flags_get(layout, &flr_state);
7193 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7194 progname, fname, strerror(errno));
7199 flr_state &= LCM_FL_FLR_MASK;
7200 switch (flr_state) {
7201 case LCM_FL_NOT_FLR:
7204 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7205 progname, fname, lcm_flags_string(flr_state));
7211 /* get stale component info */
7212 comp_size = llapi_mirror_find_stale(layout, comp_array,
7213 ARRAY_SIZE(comp_array),
7214 mirror_ids, ids_nr);
7215 if (comp_size < 0) {
7221 while (idx < comp_size) {
7227 rc = llapi_lease_check(fd);
7228 if (rc != LL_LEASE_WRLCK) {
7229 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7234 mirror_id = comp_array[idx].lrc_mirror_id;
7235 end = comp_array[idx].lrc_end;
7237 /* try to combine adjacent component */
7238 for (i = idx + 1; i < comp_size; i++) {
7239 if (mirror_id != comp_array[i].lrc_mirror_id ||
7240 end != comp_array[i].lrc_start)
7242 end = comp_array[i].lrc_end;
7245 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7246 comp_array[idx].lrc_start,
7249 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7250 "%ld.\n", progname, fname, result);
7253 } else if (result > 0) {
7256 /* mark synced components */
7257 for (j = idx; j < i; j++)
7258 comp_array[j].lrc_synced = true;
7264 /* prepare ioc for lease put */
7265 ioc->lil_mode = LL_LEASE_UNLCK;
7266 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7268 for (idx = 0; idx < comp_size; idx++) {
7269 if (comp_array[idx].lrc_synced) {
7270 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7275 llapi_layout_free(layout);
7277 rc = llapi_lease_set(fd, ioc);
7279 if (rc == 0) /* lost lease lock */
7281 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7282 progname, fname, strerror(errno));
7286 * llapi_lease_set returns lease mode when it request to unlock
7297 static inline int lfs_mirror_resync(int argc, char **argv)
7299 struct ll_ioc_lease *ioc = NULL;
7300 __u16 mirror_ids[128] = { 0 };
7305 struct option long_opts[] = {
7306 { .val = 'o', .name = "only", .has_arg = required_argument },
7309 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
7312 rc = parse_mirror_ids(mirror_ids,
7313 sizeof(mirror_ids) / sizeof(__u16),
7317 "%s: bad mirror ids '%s'.\n",
7324 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7325 argv[0], argv[optind - 1]);
7331 if (argc == optind) {
7332 fprintf(stderr, "%s: no file name given.\n", argv[0]);
7337 if (ids_nr > 0 && argc > optind + 1) {
7339 "%s: option '--only' cannot be used upon multiple files.\n",
7347 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7352 /* set the lease on the file */
7353 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
7355 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
7356 argv[0], strerror(errno));
7361 for (; optind < argc; optind++) {
7362 rc = lfs_mirror_resync_file(argv[optind], ioc,
7363 mirror_ids, ids_nr);
7364 /* ignore previous file's error, continue with next file */
7367 memset(ioc, 0, sizeof(__u32) * 4096);
7376 * struct verify_chunk - Mirror chunk to be verified.
7377 * @chunk: [start, end) of the chunk.
7378 * @mirror_count: Number of mirror ids in @mirror_id array.
7379 * @mirror_id: Array of valid mirror ids that cover the chunk.
7381 struct verify_chunk {
7382 struct lu_extent chunk;
7383 unsigned int mirror_count;
7384 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
7388 * print_chunks() - Print chunk information.
7389 * @fname: Mirrored file name.
7390 * @chunks: Array of chunks.
7391 * @chunk_count: Number of chunks in @chunks array.
7393 * This function prints [start, end) of each chunk in @chunks
7394 * for mirrored file @fname, and also prints the valid mirror ids
7395 * that cover the chunk.
7400 void print_chunks(const char *fname, struct verify_chunk *chunks,
7406 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
7407 for (i = 0; i < chunk_count; i++) {
7408 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
7410 if (chunks[i].mirror_count == 0)
7411 fprintf(stdout, "\t[");
7413 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
7414 for (j = 1; j < chunks[i].mirror_count; j++)
7415 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
7417 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
7419 fprintf(stdout, "\n");
7423 * print_checksums() - Print CRC-32 checksum values.
7424 * @chunk: A chunk and its corresponding valid mirror ids.
7425 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
7427 * This function prints CRC-32 checksum values on @chunk for
7428 * each valid mirror that covers it.
7433 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
7438 "CRC-32 checksum value for chunk "DEXT":\n",
7439 PEXT(&chunk->chunk));
7440 for (i = 0; i < chunk->mirror_count; i++)
7441 fprintf(stdout, "Mirror %u:\t%#lx\n",
7442 chunk->mirror_id[i], crc[i]);
7443 fprintf(stdout, "\n");
7447 * filter_mirror_id() - Filter specified mirror ids.
7448 * @chunks: Array of chunks.
7449 * @chunk_count: Number of chunks in @chunks array.
7450 * @mirror_ids: Specified mirror ids to be verified.
7451 * @ids_nr: Number of specified mirror ids.
7453 * This function scans valid mirror ids that cover each chunk in @chunks
7454 * and filters specified mirror ids.
7459 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
7460 __u16 *mirror_ids, int ids_nr)
7465 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7466 unsigned int valid_count = 0;
7468 for (i = 0; i < chunk_count; i++) {
7469 if (chunks[i].mirror_count == 0)
7473 for (j = 0; j < ids_nr; j++) {
7474 for (k = 0; k < chunks[i].mirror_count; k++) {
7475 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
7476 valid_id[valid_count] = mirror_ids[j];
7483 memcpy(chunks[i].mirror_id, valid_id,
7484 sizeof(__u16) * valid_count);
7485 chunks[i].mirror_count = valid_count;
7490 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
7491 * @layout: Mirror component list.
7492 * @chunks: Array of chunks.
7493 * @chunks_size: Array size of @chunks.
7495 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
7496 * to find out chunk segments and store them in @chunks array.
7498 * The @mirror_id array in each element of @chunks will store the valid
7499 * mirror ids that cover the chunk. If a mirror component covering the
7500 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
7501 * will not be stored into the @mirror_id array, and the chunk for that
7502 * mirror will not be verified.
7504 * The @mirror_count in each element of @chunks will store the number of
7505 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
7506 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
7507 * indicates the chunk is valid in only one mirror. In both cases, the
7508 * chunk will not be verified.
7510 * Here is an example:
7513 * +------+-------------+--------------------+
7515 * +------+------+------+------+-------------+
7516 * | | S | S | | mirror2
7517 * +-------------+------+------+-------------+
7519 * prepared @chunks array will contain 5 elements:
7520 * (([0, 1M), [1, 2], 2),
7521 * ([1M, 2M), [1, 2], 2),
7522 * ([2M, 3M), [1], 1),
7523 * ([3M, 4M], [], 0),
7524 * ([4M, EOF), [2], 1))
7526 * Return: the actual array size of @chunks on success
7527 * or a negative error code on failure.
7530 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
7531 struct verify_chunk *chunks,
7542 memset(chunks, 0, sizeof(*chunks) * chunks_size);
7545 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
7548 "%s: move to the first layout component: %s.\n",
7549 progname, strerror(errno));
7555 chunks[idx].chunk.e_end = LUSTRE_EOF;
7557 rc = llapi_layout_comp_extent_get(layout, &start, &end);
7560 "%s: llapi_layout_comp_extent_get failed: %s.\n",
7561 progname, strerror(errno));
7565 if (start > chunks[idx].chunk.e_start ||
7566 end <= chunks[idx].chunk.e_start)
7569 if (end < chunks[idx].chunk.e_end)
7570 chunks[idx].chunk.e_end = end;
7572 rc = llapi_layout_comp_flags_get(layout, &flags);
7575 "%s: llapi_layout_comp_flags_get failed: %s.\n",
7576 progname, strerror(errno));
7580 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
7583 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7586 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7587 progname, strerror(errno));
7591 chunks[idx].mirror_id[i] = mirror_id;
7593 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
7595 "%s: mirror_id array is too small.\n",
7602 rc = llapi_layout_comp_use(layout,
7603 LLAPI_LAYOUT_COMP_USE_NEXT);
7606 "%s: move to the next layout component: %s.\n",
7607 progname, strerror(errno));
7610 } /* loop through all components */
7612 chunks[idx].mirror_count = i;
7614 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
7618 if (idx >= chunks_size) {
7619 fprintf(stderr, "%s: chunks array is too small.\n",
7625 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
7629 return rc < 0 ? rc : idx + 1;
7633 * lfs_mirror_verify_chunk() - Verify a chunk.
7634 * @fd: File descriptor of the mirrored file.
7635 * @file_size: Size of the mirrored file.
7636 * @chunk: A chunk and its corresponding valid mirror ids.
7637 * @verbose: Verbose mode.
7639 * This function verifies a @chunk contains exactly the same data
7640 * ammong the mirrors that cover it.
7642 * If @verbose is specified, then the function will print where the
7643 * differences are if the data do not match. Otherwise, it will
7644 * just return an error in that case.
7646 * Return: 0 on success or a negative error code on failure.
7649 int lfs_mirror_verify_chunk(int fd, size_t file_size,
7650 struct verify_chunk *chunk, int verbose)
7652 const size_t buflen = 4 * 1024 * 1024; /* 4M */
7654 size_t page_size = sysconf(_SC_PAGESIZE);
7660 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7667 rc = posix_memalign(&buf, page_size, buflen);
7668 if (rc) /* error code is returned directly */
7672 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7673 PEXT(&chunk->chunk));
7674 for (i = 0; i < chunk->mirror_count; i++)
7675 fprintf(stdout, " %u", chunk->mirror_id[i]);
7676 fprintf(stdout, "\n");
7680 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
7681 pos = chunk->chunk.e_start;
7682 while (bytes_done < count) {
7683 /* compute initial CRC-32 checksum */
7684 crc = crc32(0L, Z_NULL, 0);
7685 memset(crc_array, 0, sizeof(crc_array));
7688 for (i = 0; i < chunk->mirror_count; i++) {
7689 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
7691 if (bytes_read < 0) {
7694 "%s: failed to read data from mirror %u: %s.\n",
7695 progname, chunk->mirror_id[i],
7700 /* compute new CRC-32 checksum */
7701 crc_array[i] = crc32(crc, buf, bytes_read);
7705 print_checksums(chunk, crc_array);
7707 /* compare CRC-32 checksum values */
7708 for (i = 1; i < chunk->mirror_count; i++) {
7709 if (crc_array[i] != crc_array[0]) {
7715 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
7716 progname, PEXT(&chunk->chunk),
7717 chunk->mirror_id[0],
7718 chunk->mirror_id[i]);
7723 bytes_done += bytes_read;
7726 if (verbose > 1 && rc == 0) {
7727 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7728 PEXT(&chunk->chunk));
7729 for (i = 0; i < chunk->mirror_count; i++)
7730 fprintf(stdout, " %u", chunk->mirror_id[i]);
7731 fprintf(stdout, " PASS\n\n");
7740 * lfs_mirror_verify_file() - Verify a mirrored file.
7741 * @fname: Mirrored file name.
7742 * @mirror_ids: Specified mirror ids to be verified.
7743 * @ids_nr: Number of specified mirror ids.
7744 * @verbose: Verbose mode.
7746 * This function verifies that each SYNC mirror of a mirrored file
7747 * specified by @fname contains exactly the same data.
7749 * If @mirror_ids is specified, then the function will verify the
7750 * mirrors specified by @mirror_ids contain exactly the same data.
7752 * If @verbose is specified, then the function will print where the
7753 * differences are if the data do not match. Otherwise, it will
7754 * just return an error in that case.
7756 * Return: 0 on success or a negative error code on failure.
7759 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
7762 struct verify_chunk chunks_array[1024] = { };
7763 struct llapi_layout *layout = NULL;
7767 int chunk_count = 0;
7773 if (stat(fname, &stbuf) < 0) {
7774 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7775 progname, fname, strerror(errno));
7780 if (!S_ISREG(stbuf.st_mode)) {
7781 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7787 if (stbuf.st_size == 0) {
7789 fprintf(stdout, "%s: '%s' file size is 0.\n",
7795 fd = open(fname, O_DIRECT | O_RDONLY);
7797 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7798 progname, fname, strerror(errno));
7803 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
7805 fprintf(stderr, "%s: '%s' llapi_lease_get failed: %s.\n",
7806 progname, fname, strerror(errno));
7810 layout = llapi_layout_get_by_fd(fd, 0);
7811 if (layout == NULL) {
7812 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7813 progname, fname, strerror(errno));
7815 llapi_lease_put(fd);
7819 rc = llapi_layout_flags_get(layout, &flr_state);
7821 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7822 progname, fname, strerror(errno));
7827 flr_state &= LCM_FL_FLR_MASK;
7828 switch (flr_state) {
7829 case LCM_FL_NOT_FLR:
7831 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7832 progname, fname, lcm_flags_string(flr_state));
7838 /* find out mirror chunks to be verified */
7839 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
7840 ARRAY_SIZE(chunks_array));
7841 if (chunk_count < 0) {
7847 /* filter specified mirror ids */
7848 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
7851 print_chunks(fname, chunks_array, chunk_count);
7853 for (idx = 0; idx < chunk_count; idx++) {
7854 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
7857 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
7859 PEXT(&chunks_array[idx].chunk),
7860 (unsigned long long)stbuf.st_size);
7864 if (chunks_array[idx].mirror_count == 0) {
7866 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
7868 PEXT(&chunks_array[idx].chunk));
7870 fprintf(stderr, "skipped\n");
7874 fprintf(stderr, "failed\n");
7878 if (chunks_array[idx].mirror_count == 1) {
7881 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
7883 PEXT(&chunks_array[idx].chunk),
7884 chunks_array[idx].mirror_id[0]);
7888 rc = llapi_lease_check(fd);
7889 if (rc != LL_LEASE_RDLCK) {
7890 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7895 /* verify one chunk */
7896 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
7897 &chunks_array[idx], verbose);
7911 llapi_layout_free(layout);
7912 llapi_lease_put(fd);
7920 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
7921 * @argc: The count of lfs mirror verify command line arguments.
7922 * @argv: Array of strings for lfs mirror verify command line arguments.
7924 * This function parses lfs mirror verify command and verifies the
7925 * specified mirrored file(s).
7927 * Return: 0 on success or a negative error code on failure.
7929 static inline int lfs_mirror_verify(int argc, char **argv)
7931 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7939 struct option long_opts[] = {
7940 { .val = 'o', .name = "only", .has_arg = required_argument },
7941 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7944 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
7946 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
7949 rc = parse_mirror_ids(mirror_ids,
7950 ARRAY_SIZE(mirror_ids),
7954 "%s: bad mirror ids '%s'.\n",
7961 "%s: at least 2 mirror ids needed with '--only' option.\n",
7971 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7972 progname, argv[optind - 1]);
7978 if (argc == optind) {
7979 fprintf(stderr, "%s: no file name given.\n", progname);
7984 if (ids_nr > 0 && argc > optind + 1) {
7986 "%s: '--only' cannot be used upon multiple files.\n",
7994 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8000 for (; optind < argc; optind++) {
8001 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
8011 * lfs_mirror() - Parse and execute lfs mirror commands.
8012 * @argc: The count of lfs mirror command line arguments.
8013 * @argv: Array of strings for lfs mirror command line arguments.
8015 * This function parses lfs mirror commands and performs the
8016 * corresponding functions specified in mirror_cmdlist[].
8018 * Return: 0 on success or an error code on failure.
8020 static int lfs_mirror(int argc, char **argv)
8027 Parser_init("lfs-mirror > ", mirror_cmdlist);
8029 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8031 program_invocation_short_name = cmd;
8033 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
8035 rc = Parser_commands();
8037 return rc < 0 ? -rc : rc;
8041 * lfs_mirror_list_commands() - List lfs mirror commands.
8042 * @argc: The count of command line arguments.
8043 * @argv: Array of strings for command line arguments.
8045 * This function lists lfs mirror commands defined in mirror_cmdlist[].
8047 * Return: 0 on success.
8049 static int lfs_mirror_list_commands(int argc, char **argv)
8051 char buffer[81] = "";
8053 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
8059 static int lfs_list_commands(int argc, char **argv)
8061 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
8063 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
8068 int main(int argc, char **argv)
8072 /* Ensure that liblustreapi constructor has run */
8073 if (!llapi_liblustreapi_initialized())
8074 fprintf(stderr, "liblustreapi was not properly initialized\n");
8079 Parser_init("lfs > ", cmdlist);
8081 progname = program_invocation_short_name; /* Used in error messages */
8083 llapi_set_command_name(argv[1]);
8084 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
8085 llapi_clear_command_name();
8087 rc = Parser_commands();
8090 return rc < 0 ? -rc : rc;
8093 #ifdef _LUSTRE_IDL_H_
8094 /* Everything we need here should be included by lustreapi.h. */
8095 # error "lfs should not depend on lustre_idl.h"
8096 #endif /* _LUSTRE_IDL_H_ */