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/
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Robert Read <rread@clusterfs.com>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
57 #include <sys/types.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
78 #include <lnetconfig/cyaml.h>
80 #include <uapi/linux/lustre/lustre_idl.h>
84 # define NSEC_PER_SEC 1000000000UL
86 #define ONE_MB 0x100000
89 static int lfs_find(int argc, char **argv);
90 static int lfs_getstripe(int argc, char **argv);
91 static int lfs_getdirstripe(int argc, char **argv);
92 static int lfs_setdirstripe(int argc, char **argv);
93 static int lfs_rmentry(int argc, char **argv);
94 static int lfs_unlink_foreign(int argc, char **argv);
95 static int lfs_osts(int argc, char **argv);
96 static int lfs_mdts(int argc, char **argv);
97 static int lfs_df(int argc, char **argv);
98 static int lfs_getname(int argc, char **argv);
99 static int lfs_check(int argc, char **argv);
100 #ifdef HAVE_SYS_QUOTA_H
101 static int lfs_setquota(int argc, char **argv);
102 static int lfs_quota(int argc, char **argv);
103 static int lfs_project(int argc, char **argv);
105 static int lfs_flushctx(int argc, char **argv);
106 static int lfs_poollist(int argc, char **argv);
107 static int lfs_changelog(int argc, char **argv);
108 static int lfs_changelog_clear(int argc, char **argv);
109 static int lfs_fid2path(int argc, char **argv);
110 static int lfs_path2fid(int argc, char **argv);
111 static int lfs_rmfid(int argc, char **argv);
112 static int lfs_data_version(int argc, char **argv);
113 static int lfs_hsm_state(int argc, char **argv);
114 static int lfs_hsm_set(int argc, char **argv);
115 static int lfs_hsm_clear(int argc, char **argv);
116 static int lfs_hsm_action(int argc, char **argv);
117 static int lfs_hsm_archive(int argc, char **argv);
118 static int lfs_hsm_restore(int argc, char **argv);
119 static int lfs_hsm_release(int argc, char **argv);
120 static int lfs_hsm_remove(int argc, char **argv);
121 static int lfs_hsm_cancel(int argc, char **argv);
122 static int lfs_swap_layouts(int argc, char **argv);
123 static int lfs_mv(int argc, char **argv);
124 static int lfs_ladvise(int argc, char **argv);
125 static int lfs_getsom(int argc, char **argv);
126 static int lfs_heat_get(int argc, char **argv);
127 static int lfs_heat_set(int argc, char **argv);
128 static int lfs_mirror(int argc, char **argv);
129 static inline int lfs_mirror_resync(int argc, char **argv);
130 static inline int lfs_mirror_verify(int argc, char **argv);
131 static inline int lfs_mirror_read(int argc, char **argv);
132 static inline int lfs_mirror_write(int argc, char **argv);
133 static inline int lfs_mirror_copy(int argc, char **argv);
134 static int lfs_pcc_attach(int argc, char **argv);
135 static int lfs_pcc_attach_fid(int argc, char **argv);
136 static int lfs_pcc_detach(int argc, char **argv);
137 static int lfs_pcc_detach_fid(int argc, char **argv);
138 static int lfs_pcc_state(int argc, char **argv);
139 static int lfs_pcc(int argc, char **argv);
141 static int lfs_migrate_to_dom(int fd_src, int fd_dst, char *name,
142 __u64 migration_flags,
143 unsigned long long bandwidth_bytes_sec,
144 long stats_interval_sec);
146 struct pool_to_id_cbdata {
151 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
152 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
154 enum setstripe_origin {
164 static int lfs_setstripe_internal(int argc, char **argv,
165 enum setstripe_origin opc);
167 static inline int lfs_setstripe(int argc, char **argv)
169 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
172 static inline int lfs_setstripe_migrate(int argc, char **argv)
174 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
177 static inline int lfs_mirror_create(int argc, char **argv)
179 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
182 static inline int lfs_mirror_extend(int argc, char **argv)
184 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
187 static inline int lfs_mirror_split(int argc, char **argv)
189 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
192 static inline int lfs_mirror_delete(int argc, char **argv)
194 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
197 #define SSM_SETSTRIPE_OPT \
198 "[--component-add|--component-del|--delete|-d]\n" \
199 "\t\t[--comp-set --comp-id|-I COMP_ID|--comp-flags=COMP_FLAGS]\n" \
200 "\t\t[--component-end|-E END_OFFSET]\n" \
201 "\t\t[--copy=SOURCE_LAYOUT_FILE]|--yaml|-y YAML_TEMPLATE_FILE]\n" \
202 "\t\t[--extension-size|--ext-size|-z EXT_SIZE]\n" \
203 "\t\t[--help|-h]\n" \
204 "\t\t[--foreign=FOREIGN_TYPE --xattr|-x LAYOUT]\n" \
205 "\t\t[--layout|-L PATTERN] [--mode FILE_MODE]\n" \
206 "\t\t[--mirror-count|-N[MIRROR_COUNT]]\n" \
207 "\t\t[--ost|-o OST_INDEX[,OST_INDEX,...]]\n" \
208 "\t\t[--overstripe-count|-C STRIPE_COUNT]\n" \
209 "\t\t[--pool|-p POOL_NAME]\n" \
210 "\t\t[--stripe-count|-c STRIPE_COUNT]\n" \
211 "\t\t[--stripe-index|-i START_OST_IDX]\n" \
212 "\t\t[--stripe-size|-S STRIPE_SIZE]"
214 /* Setstripe and migrate share mostly the same parameters */
215 #define SSM_CMD_COMMON(cmd) \
217 " " SSM_SETSTRIPE_OPT "\n"
219 #define SETSTRIPE_USAGE \
220 SSM_CMD_COMMON("setstripe ") \
221 "\t\tFILENAME|DIRECTORY\n"
223 #define MIGRATE_USAGE \
224 SSM_CMD_COMMON("migrate ") \
225 "\t\t[--block|-b] [--non-block|-n]\n" \
226 "\t\t[--non-direct|-D] [--verbose|-v] FILENAME\n" \
228 #define SETDIRSTRIPE_USAGE \
229 " [--mdt-count|-c stripe_count>\n" \
230 " [--help|-h] [--mdt-hash|-H mdt_hash]\n" \
231 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
232 " [--mdt-overcount|-C stripe_count>\n" \
233 " [--default|-D] [--mode|-o mode]\n" \
234 " [--max-inherit|-X max_inherit]\n" \
235 " [--max-inherit-rr max_inherit_rr] <dir>\n" \
236 "To create dir with a foreign (free format) layout :\n" \
237 "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
238 " [--mode|-o MODE] [--flags HEX] DIRECTORY\n"
241 * command_t mirror_cmdlist - lfs mirror commands.
243 command_t mirror_cmdlist[] = {
244 { .pc_name = "create", .pc_func = lfs_mirror_create,
245 .pc_help = "Create a mirrored file.\n"
246 "usage: lfs mirror create --mirror-count|-N[MIRROR_COUNT]\n"
247 " [SETSTRIPE_OPTIONS] ... FILENAME|DIRECTORY ...\n" },
248 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
249 .pc_help = "Delete a mirror from a file.\n"
250 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
251 "\t --component-id|--comp-id|-I COMP_ID |\n"
252 "\t -p <pool>} MIRRORED_FILE ...\n"
254 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
255 .pc_help = "Extend a mirrored file.\n"
256 "Usage: lfs mirror extend "
257 "--mirror-count|-N[MIRROR_COUNT] [--no-verify]|\n"
258 "\t\t[--stats|--stats-interval=STATS_INTERVAL]|\n"
259 "\t\t[--bandwidth-limit|--W BANDWIDTH]\n"
260 "\t\t[[-f VICTIM_FILE] |\n"
261 "\t\t" SSM_SETSTRIPE_OPT "]"
263 { .pc_name = "split", .pc_func = lfs_mirror_split,
264 .pc_help = "Split a mirrored file.\n"
265 "usage: lfs mirror split {--mirror-id MIRROR_ID |\n"
266 "\t --component-id|-I COMP_ID|-p POOL} [--destroy|-d]\n"
267 "\t [-f NEW_FILE] MIRRORED_FILE ...\n" },
268 { .pc_name = "read", .pc_func = lfs_mirror_read,
269 .pc_help = "Read the content of a specified mirror of a file.\n"
270 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
271 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
272 { .pc_name = "write", .pc_func = lfs_mirror_write,
273 .pc_help = "Write to a specified mirror of a file.\n"
274 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
275 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
276 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
277 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
278 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
279 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
280 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
281 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
282 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]|\n"
283 "\t\t--stats|--stats-interval=<sec>|\n"
284 "\t\t--W <bandwidth>|--bandwidth-limit=<bandwidth>\n"
285 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
286 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
287 .pc_help = "Verify mirrored file(s).\n"
288 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
289 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
294 * command_t pcc_cmdlist - lfs pcc commands.
296 command_t pcc_cmdlist[] = {
297 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
298 .pc_help = "Attach given files to the Persistent Client Cache.\n"
299 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
300 "\t-i: archive id for RW-PCC\n" },
301 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
302 .pc_help = "Attach given files into PCC by FID(s).\n"
303 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
304 "\t-i: archive id for RW-PCC\n"
305 "\t-m: Lustre mount point\n" },
306 { .pc_name = "state", .pc_func = lfs_pcc_state,
307 .pc_help = "Display the PCC state for given files.\n"
308 "usage: lfs pcc state <file> ...\n" },
309 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
310 .pc_help = "Detach given files from the Persistent Client Cache.\n"
311 "usage: lfs pcc detach <file> ...\n" },
312 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
313 .pc_help = "Detach given files from PCC by FID(s).\n"
314 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
318 /* all available commands */
319 command_t cmdlist[] = {
320 {"setstripe", lfs_setstripe, 0,
321 "Create a file with specified striping/composite layout, or\n"
322 "set the default layout on an existing directory.\n"
324 {"getstripe", lfs_getstripe, 0,
325 "List the layout pattern for a given file or files in a\n"
326 "directory or recursively for all files in a directory tree.\n"
327 "Usage: getstripe [--ost|-O OST_NAME] [--quiet|-q] [--verbose|-v]\n"
328 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
329 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
330 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
331 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
332 " [--help|-h] [--hex-idx]\n"
333 " [--component-id|-I[=COMP_ID]]\n"
334 " [--component-flags[=COMP_FLAGS]]\n"
335 " [--component-count]\n"
336 " [--extension-size|--ext-size|-z]\n"
337 " [--component-start[=[+-]START_OFFSET]]\n"
338 " [--component-end|-E[[+-]END_OFFSET]]\n"
339 " [[!] --mirror-index=[+-]MIRROR_INDEX |\n"
340 " [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
342 " FILENAME|DIRECTORY"},
343 {"setdirstripe", lfs_setdirstripe, 0,
344 "Create striped directory on specified MDT, same as mkdir.\n"
345 "May be restricted to root or group users, depending on settings.\n"
346 "usage: setdirstripe [OPTION] <directory>\n"
348 {"getdirstripe", lfs_getdirstripe, 0,
349 "To list the layout pattern info for a given directory\n"
350 "or recursively for all directories in a directory tree.\n"
351 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
352 " [--help|-h] [--hex-idx] [--mdt-hash|-H]\n"
353 " [--obd|-O UUID] [--recursive|-r] [--raw|-R]\n"
354 " [--yaml|-y] [--verbose|-v] [--default|-D]\n"
355 " [--max-inherit|-X]\n"
356 " [--max-inherit-rr] <dir> ..."},
357 {"mkdir", lfs_setdirstripe, 0,
358 "Create striped directory on specified MDT, same as setdirstripe.\n"
359 "usage: mkdir [OPTION] <directory>\n"
361 {"rm_entry", lfs_rmentry, 0,
362 "To remove the name entry of the remote directory. Note: This\n"
363 "command will only delete the name entry, i.e. the remote directory\n"
364 "will become inaccessable after this command. This can only be done\n"
365 "by the administrator\n"
366 "usage: rm_entry <dir>\n"},
367 {"rmentry", lfs_rmentry, 0, "remove a dir entry, same as 'rm_entry'\n"},
368 {"unlink_foreign", lfs_unlink_foreign, 0,
369 "To remove the foreign file/dir.\n"
370 "Note: This is for files/dirs prevented to be removed using\n"
371 "unlink/rmdir, but works also for regular ones\n"
372 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
373 {"pool_list", lfs_poollist, 0,
374 "List pools or pool OSTs\n"
375 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
376 {"find", lfs_find, 0,
377 "find files matching given attributes recursively in directory tree.\n"
378 "usage: find <directory|filename> ...\n"
379 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --btime|-B [+-]N[smhdwy]]\n"
380 " [[!] --ctime|-C [+-]N[smhdwy]] [[!] --mtime|-M [+-]N[smhdwy]]\n"
381 " [[!] --attrs=[^]ATTR[,...]]\n"
382 " [[!] --blocks|-b N] [[!] --component-count [+-]<comp_cnt>]\n"
383 " [[!] --component-start [+-]N[kMGTPE]]\n"
384 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
385 " [[!] --component-flags {init,stale,prefer,prefrd,prefwr,offline,nosync,extension}]\n"
386 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
387 " [[!] --foreign[=<foreign_type>]]\n"
388 " [[!] --gid|-g|--group|-G <gid>|<gname>] [--help|-h]\n"
389 " [[!] --layout|-L released,raid0,mdt] [--lazy|-l] [[!] --links [+-]n]\n"
390 " [--maxdepth|-D N] [[!] --mdt-count|-T [+-]<stripes>]\n"
391 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
392 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
393 " [[!] --mirror-count|-N [+-]<n>]\n"
394 " [[!] --mirror-state <[^]state>]\n"
395 " [[!] --name|-n <pattern>] [[!] --newer[XY] <reference>]\n"
396 " [[!] --ost|-O <uuid|index,...>] [[!] --perm [/-]mode]\n"
397 " [[!] --pool <pool>] [--print|-P] [--print0|-0] [--printf <format>]\n"
398 " [[!] --projid <projid>] [[!] --size|-s [+-]N[bkMGTPE]]\n"
399 " [[!] --stripe-count|-c [+-]<stripes>]\n"
400 " [[!] --stripe-index|-i <index,...>]\n"
401 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
402 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
403 "\t !: used before an option indicates 'NOT' requested attribute\n"
404 "\t -: used before a value indicates less than requested value\n"
405 "\t +: used before a value indicates more than requested value\n"
406 "\t ^: used before a flag indicates to exclude it\n"},
407 {"check", lfs_check, 0,
408 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
409 "or all the servers (MGTs, MDTs and OSTs) [for specified path only].\n"
410 "usage: check {mgts|osts|mdts|all} [path]"},
411 {"osts", lfs_osts, 0, "list OSTs connected to client "
412 "[for specified path only]\n" "usage: osts [path]"},
413 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
414 "[for specified path only]\n" "usage: mdts [path]"},
416 "report filesystem disk space usage or inodes usage "
417 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
418 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
419 " [--pool|-p <fsname>[.<pool>]] [path]"},
420 {"getname", lfs_getname, 0,
421 "list instances and specified mount points [for specified path only]\n"
422 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
423 #ifdef HAVE_SYS_QUOTA_H
424 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
425 "usage: setquota [-t] {-u|-U|-g|-G|-p|-P ID} {-b|-B|-i|-I LIMIT} [--pool POOL] FILESYSTEM\n"
426 " setquota {-u|-g|-p ID} {--default|--delete} FILESYSTEM\n"},
427 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
428 "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
429 " [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
430 " [--pool OST_POOL_NAME] FILESYSTEM\n"
431 " quota -t {-u|-g|-p} [--pool OST_POOL_NAME] FILESYSTEM\n"
432 " quota [-hqv] {-U|-G|-P} [--pool OST_POOL_NAME] FILESYSTEM\n"
433 " quota -a {-u|-g|-p} [-s START_QID] [-e END_QID] FILESYSTEM"},
434 {"project", lfs_project, 0,
435 "Change or list project attribute for specified file or directory.\n"
436 "usage: project [-d|-r] <file|directory...>\n"
437 " list project ID and flags on file(s) or directories\n"
438 " project [-p id] [-s] [-r] <file|directory...>\n"
439 " set project ID and/or inherit flag for specified file(s) or directories\n"
440 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
441 " check project ID and flags on file(s) or directories, print outliers\n"
442 " project -C [-d|-r] [-k] <file|directory...>\n"
443 " clear the project inherit flag and ID on the file or directory\n"
446 {"flushctx", lfs_flushctx, 0,
447 "Flush security context for current user.\n"
448 "usage: flushctx [-k] [-r] [mountpoint...]"},
449 {"changelog", lfs_changelog, 0,
450 "Show the metadata changes on an MDT."
451 "\nusage: changelog <mdtname> [startrec [endrec]]"},
452 {"changelog_clear", lfs_changelog_clear, 0,
453 "Indicate that old changelog records up to <endrec> are no longer of "
454 "interest to consumer <id>, allowing the system to free up space.\n"
455 "An <endrec> of 0 means all records.\n"
456 "usage: changelog_clear <mdtname> <id> <endrec>"},
457 {"fid2path", lfs_fid2path, 0,
458 "Resolve the full path(s) for given FID(s). For a specific hardlink "
459 "specify link number <linkno>.\n"
460 "usage: fid2path [--print0|-0] [--print-fid|-f] [--print-link|-c] "
461 "[--link|-l <linkno>] [--name|-n] <fsname|root> <fid>..."},
462 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
463 "usage: path2fid [--parents] <path> ..."},
464 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
465 "usage: rmfid <fsname|rootpath> <fid> ..."},
466 {"data_version", lfs_data_version, 0, "Display file data version for "
467 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
468 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
469 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
470 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
471 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
472 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
473 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
475 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
476 "[--archived] [--lost] <file> ..."},
477 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
478 "given files.\n" "usage: hsm_action <file> ..."},
479 {"hsm_archive", lfs_hsm_archive, 0,
480 "Archive file to external storage.\n"
481 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
483 {"hsm_restore", lfs_hsm_restore, 0,
484 "Restore file from external storage.\n"
485 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
486 {"hsm_release", lfs_hsm_release, 0,
487 "Release files from Lustre.\n"
488 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
489 {"hsm_remove", lfs_hsm_remove, 0,
490 "Remove file copy from external storage.\n"
491 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
493 " (FILE [FILE ...] | "
494 "--mntpath MOUNTPATH FID [FID ...])\n"
496 "Note: To remove an archived copy of a file already deleted from a "
498 "--mntpath option and a list of FIDs must be specified"
500 {"hsm_cancel", lfs_hsm_cancel, 0,
501 "Cancel requests related to specified files.\n"
502 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
503 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
504 "usage: swap_layouts <path1> <path2>"},
505 {"migrate", lfs_setstripe_migrate, 0,
506 "migrate directories and their inodes between MDTs.\n"
507 "usage: migrate [--mdt-count|-c STRIPE_COUNT]\n"
508 " [--mdt-overcount|-C OVERSTRIPE_COUNT\n"
509 " [--directory|-d] [--mdt-hash|-H HASH_TYPE]\n"
510 " [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
513 "migrate file objects from one OST layout to another\n"
514 "(may be not safe with concurent writes).\n"
517 "To move directories between MDTs. This command is deprecated, "
518 "use \"migrate\" instead.\n"
519 "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
521 {"ladvise", lfs_ladvise, 0,
522 "Provide servers with advice about access patterns for a file.\n"
523 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
524 " [--background|-b] [--unset|-u]\n\n"
525 " {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
526 " {[--mode|-m [READ,WRITE]}\n"
528 {"mirror", lfs_mirror, mirror_cmdlist,
529 "lfs commands used to manage files with mirrored components:\n"
530 "lfs mirror create - create a mirrored file or directory\n"
531 "lfs mirror extend - add mirror(s) to an existing file\n"
532 "lfs mirror split - split a mirror from an existing mirrored file\n"
533 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
534 "lfs mirror read - read a mirror content of a mirrored file\n"
535 "lfs mirror write - write to a mirror of a mirrored file\n"
536 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
537 "lfs mirror verify - verify mirrored file(s)\n"},
538 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
539 "usage: getsom [-s] [-b] [-f] <path>\n"
540 "\t-s: Only show the size value of the SOM data for a given file\n"
541 "\t-b: Only show the blocks value of the SOM data for a given file\n"
542 "\t-f: Only show the flags value of the SOM data for a given file\n"},
543 {"heat_get", lfs_heat_get, 0,
544 "To get heat of files.\n"
545 "usage: heat_get <file> ...\n"},
546 {"heat_set", lfs_heat_set, 0,
547 "To set heat flags of files.\n"
548 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
549 "\t--clear|-c: Clear file heat for given files\n"
550 "\t--off|-o: Turn off file heat for given files\n"
551 "\t--on|-O: Turn on file heat for given files\n"},
552 {"pcc", lfs_pcc, pcc_cmdlist,
553 "lfs commands used to interact with PCC features:\n"
554 "lfs pcc attach - attach given files to Persistent Client Cache\n"
555 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
556 "lfs pcc state - display the PCC state for given files\n"
557 "lfs pcc detach - detach given files from Persistent Client Cache\n"
558 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
562 static int check_hashtype(const char *hashtype)
564 int type_num = atoi(hashtype);
567 /* numeric hash type */
568 if (hashtype && lmv_is_known_hash_type(type_num))
570 /* string hash type */
571 for (i = LMV_HASH_TYPE_ALL_CHARS; i < ARRAY_SIZE(mdt_hash_name); i++)
572 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
578 static uint32_t check_foreign_type_name(const char *foreign_type_name)
582 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
583 if (!lu_foreign_types[i].lft_name)
585 if (strcmp(foreign_type_name,
586 lu_foreign_types[i].lft_name) == 0)
587 return lu_foreign_types[i].lft_type;
590 return LU_FOREIGN_TYPE_UNKNOWN;
593 static const char *error_loc = "syserror";
596 migrate_open_files(const char *name, __u64 migration_flags,
597 const struct llapi_stripe_param *param,
598 struct llapi_layout *layout, int *fd_src_ptr,
606 char parent[PATH_MAX];
607 char volatile_file[PATH_MAX];
613 if (!param && !layout) {
614 error_loc = "layout information";
618 /* search for file directory pathname */
619 if (strlen(name) > sizeof(parent) - 1) {
620 error_loc = "source file name";
624 strncpy(parent, name, sizeof(parent));
625 ptr = strrchr(parent, '/');
627 if (!getcwd(parent, sizeof(parent))) {
628 error_loc = "getcwd";
632 if (ptr == parent) /* leading '/' */
637 /* even if the file is only read, WR mode is nedeed to allow
640 /* Allow migrating even without the key on encrypted files */
641 rflags = O_RDWR | O_NOATIME | O_CIPHERTEXT;
642 if (!(migration_flags & LLAPI_MIGRATION_NONDIRECT))
645 fd_src = open(name, rflags);
647 /* If encrypted file without the key,
648 * retry mirror extend in O_DIRECT.
650 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
651 migration_flags & LLAPI_MIGRATION_MIRROR) {
656 error_loc = "cannot open source file";
660 rc = llapi_file_fget_mdtidx(fd_src, &mdt_index);
662 error_loc = "cannot get MDT index";
667 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
668 /* Allow migrating without the key on encrypted files */
670 mode_t open_mode = S_IRUSR | S_IWUSR;
672 if (rflags & O_DIRECT)
673 open_flags |= O_DIRECT;
674 random_value = random();
675 rc = snprintf(volatile_file, sizeof(volatile_file),
676 "%s/%s:%.4X:%.4X:fd=%.2d", parent,
677 LUSTRE_VOLATILE_HDR, mdt_index,
678 random_value, fd_src);
679 if (rc >= sizeof(volatile_file)) {
684 /* create, open a volatile file, use caching (ie no directio) */
686 /* Returns -1 and sets errno on error: */
687 fd_dst = llapi_layout_file_open(volatile_file,
688 open_flags, open_mode,
693 /* Does the right thing on error: */
694 fd_dst = llapi_file_open_param(volatile_file,
698 } while (fd_dst < 0 && (rc = fd_dst) == -EEXIST);
701 error_loc = "cannot create volatile file";
706 * In case the MDT does not support creation of volatile files
707 * we should try to unlink it.
709 (void)unlink(volatile_file);
712 * Not-owner (root?) special case.
713 * Need to set owner/group of volatile file like original.
714 * This will allow to pass related check during layout_swap.
716 rc = fstat(fd_src, &st);
719 error_loc = "cannot stat source file";
723 rc = fstat(fd_dst, &stv);
726 error_loc = "cannot stat volatile";
730 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
731 rc = fchown(fd_dst, st.st_uid, st.st_gid);
734 error_loc = "cannot change ownwership of volatile";
746 *fd_src_ptr = fd_src;
747 *fd_dst_ptr = fd_dst;
753 static struct timespec timespec_sub(struct timespec *before,
754 struct timespec *after)
758 ret.tv_sec = after->tv_sec - before->tv_sec;
759 if (after->tv_nsec < before->tv_nsec) {
761 ret.tv_nsec = NSEC_PER_SEC + after->tv_nsec - before->tv_nsec;
763 ret.tv_nsec = after->tv_nsec - before->tv_nsec;
769 static void stats_log(struct timespec *now, struct timespec *start_time,
770 ssize_t read_bytes, size_t write_bytes,
771 off_t file_size_bytes)
773 struct timespec diff = timespec_sub(start_time, now);
775 if (file_size_bytes == 0)
778 if (diff.tv_sec == 0 && diff.tv_nsec == 0)
781 printf("- { seconds: %li, rmbps: %5.2g, wmbps: %5.2g, copied: %lu, size: %lu, pct: %lu%% }\n",
783 (double) read_bytes/((ONE_MB * diff.tv_sec) +
784 ((ONE_MB * diff.tv_nsec)/NSEC_PER_SEC)),
785 (double) write_bytes/((ONE_MB * diff.tv_sec) +
786 ((ONE_MB * diff.tv_nsec)/NSEC_PER_SEC)),
787 write_bytes/ONE_MB, file_size_bytes/ONE_MB,
788 ((write_bytes*100)/file_size_bytes));
791 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int),
792 unsigned long long bandwidth_bytes_sec,
793 long stats_interval_sec, off_t file_size_bytes)
795 struct llapi_layout *layout;
796 size_t buf_size = 64 * ONE_MB;
797 uint64_t stripe_size = ONE_MB;
804 size_t write_bytes = 0;
805 ssize_t read_bytes = 0;
806 struct timespec start_time;
808 struct timespec last_bw_print;
810 layout = llapi_layout_get_by_fd(fd_src, 0);
812 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
814 /* We like big bufs */
815 if (stripe_size > buf_size)
816 buf_size = stripe_size;
818 /* Trim to stripe_size multiple */
819 buf_size -= buf_size % stripe_size;
822 llapi_layout_free(layout);
825 /* limit transfer size to what can be sent in one second */
826 if (bandwidth_bytes_sec && bandwidth_bytes_sec < buf_size)
827 buf_size = (bandwidth_bytes_sec + stripe_size - 1) &
830 page_size = sysconf(_SC_PAGESIZE);
836 /* Use a page-aligned buffer for direct I/O */
837 rc = posix_memalign(&buf, page_size, buf_size);
841 sparse = llapi_file_is_sparse(fd_src);
843 rc = ftruncate(fd_dst, pos);
851 clock_gettime(CLOCK_MONOTONIC, &start_time);
852 now = last_bw_print = start_time;
856 size_t to_read, to_write;
859 if (sparse && pos >= data_end) {
862 data_off = llapi_data_seek(fd_src, pos, &data_size);
864 /* Non-fatal, switch to full copy */
868 /* hole at the end of file, truncate up to it */
870 rc = ftruncate(fd_dst, data_off);
874 pos = data_off & ~(page_size - 1);
875 data_end = data_off + data_size;
876 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
877 to_read = MIN(to_read, buf_size);
883 rc = check_file(fd_src);
888 rsize = pread(fd_src, buf, to_read, pos);
899 while (to_write > 0) {
900 unsigned long long write_target;
902 struct timespec diff;
904 written = pwrite(fd_dst, buf, to_write, pos);
911 write_bytes += written;
913 if (bandwidth_bytes_sec == 0)
916 clock_gettime(CLOCK_MONOTONIC, &now);
917 diff = timespec_sub(&start_time, &now);
918 write_target = ((bandwidth_bytes_sec * diff.tv_sec) +
919 ((bandwidth_bytes_sec *
920 diff.tv_nsec)/NSEC_PER_SEC));
922 if (write_target < write_bytes) {
923 unsigned long long excess;
924 struct timespec delay = { 0, 0 };
926 excess = write_bytes - write_target;
931 delay.tv_sec = excess / bandwidth_bytes_sec;
932 delay.tv_nsec = (excess % bandwidth_bytes_sec) *
933 NSEC_PER_SEC / bandwidth_bytes_sec;
936 rc = clock_nanosleep(CLOCK_MONOTONIC, 0,
938 } while (rc < 0 && errno == EINTR);
941 if (stats_interval_sec)
943 "error %s: delay for bandwidth control failed: %s\n",
951 clock_gettime(CLOCK_MONOTONIC, &now);
952 if (stats_interval_sec && (write_bytes != file_size_bytes) &&
953 (now.tv_sec >= last_bw_print.tv_sec +
954 stats_interval_sec)) {
955 stats_log(&now, &start_time,
956 read_bytes, write_bytes,
961 if (rc || rsize < to_read)
965 /* Output at least one log, regardless of stats_interval */
966 if (stats_interval_sec) {
967 clock_gettime(CLOCK_MONOTONIC, &now);
968 stats_log(&now, &start_time, read_bytes, write_bytes,
976 /* Try to avoid page cache pollution after migration. */
977 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
978 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
984 static int migrate_set_timestamps(int fd, const struct stat *st)
986 struct timeval tv[2] = {
987 {.tv_sec = st->st_atime},
988 {.tv_sec = st->st_mtime}
991 return futimes(fd, tv);
994 static int migrate_block(int fd_src, int fd_dst,
995 unsigned long long bandwidth_bytes_sec,
996 long stats_interval_sec)
1009 /* The grouplock blocks all concurrent accesses to the file. */
1010 rc = llapi_group_lock(fd_src, gid);
1012 error_loc = "cannot get group lock";
1016 rc = fstat(fd_src, &st);
1018 error_loc = "cannot stat source file";
1024 * LL_DV_RD_FLUSH should not be set, otherwise the servers will try to
1025 * get extent locks on the OST objects. This will conflict with our
1026 * extent group locks.
1028 rc = llapi_get_data_version(fd_src, &dv1, 0);
1030 error_loc = "cannot get dataversion";
1034 rc = migrate_copy_data(fd_src, fd_dst, NULL, bandwidth_bytes_sec,
1035 stats_interval_sec, st.st_size);
1037 error_loc = "data copy failed";
1041 /* Make sure we keep original atime/mtime values */
1042 rc = migrate_set_timestamps(fd_dst, &st);
1044 error_loc = "set target file timestamp failed";
1050 * for a migration we need to check data version on file did
1053 * Pass in gid=0 since we already own grouplock.
1055 rc = llapi_fswap_layouts_grouplock(fd_src, fd_dst, dv1, 0, 0,
1056 SWAP_LAYOUTS_CHECK_DV1);
1057 if (rc == -EAGAIN) {
1058 error_loc = "file changed";
1060 } else if (rc < 0) {
1061 error_loc = "cannot swap layout";
1066 rc2 = llapi_group_unlock(fd_src, gid);
1067 if (rc2 < 0 && rc == 0) {
1068 error_loc = "unlock group lock";
1076 * Internal helper for migrate_copy_data(). Check lease and report error if
1079 * \param[in] fd File descriptor on which to check the lease.
1081 * \retval 0 Migration can keep on going.
1082 * \retval -errno Error occurred, abort migration.
1084 static int check_lease(int fd)
1088 rc = llapi_lease_check(fd);
1090 return 0; /* llapi_check_lease returns > 0 on success. */
1095 static int migrate_nonblock(int fd_src, int fd_dst,
1096 unsigned long long bandwidth_bytes_sec,
1097 long stats_interval_sec,
1105 rc = fstat(fd_src, &st);
1107 error_loc = "cannot stat source file";
1111 rc = llapi_get_data_version(fd_src, &dv1, LL_DV_RD_FLUSH);
1113 error_loc = "cannot get data version";
1117 rc = migrate_copy_data(fd_src, fd_dst, check_lease,
1118 bandwidth_bytes_sec,
1119 stats_interval_sec, st.st_size);
1121 error_loc = "data copy failed";
1125 rc = llapi_get_data_version(fd_src, &dv2, LL_DV_RD_FLUSH);
1127 error_loc = "cannot get data version";
1136 error_loc = "source file changed";
1140 /* Make sure we keep original atime/mtime values */
1141 rc = migrate_set_timestamps(fd_dst, &st);
1143 error_loc = "set target file timestamp failed";
1150 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1152 struct pool_to_id_cbdata data = { .pool = pool };
1153 struct llapi_layout *layout = NULL;
1156 layout = llapi_layout_get_by_path(fname, 0);
1159 "error %s: file '%s' couldn't get layout: rc=%d\n",
1160 progname, fname, errno);
1164 rc = llapi_layout_sanity(layout, false, true);
1166 llapi_layout_sanity_perror(errno);
1169 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1178 llapi_layout_free(layout);
1182 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1183 __u32 flags, __u32 neg_flags)
1186 __u32 flags_array[2];
1193 "error %s: neither component id nor pool is specified\n",
1197 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1203 ids[count] = comp_id;
1204 flags_array[count] = flags;
1209 if (neg_flags & LCME_FL_STALE) {
1211 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1216 ids[count] = comp_id;
1217 flags_array[count] = neg_flags | LCME_FL_NEG;
1221 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1223 if (errno == EUCLEAN) {
1226 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1227 progname, comp_id, fname);
1230 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1231 progname, comp_id, fname, flags, neg_flags);
1238 static int lfs_component_del(char *fname, __u32 comp_id,
1239 __u32 flags, __u32 neg_flags)
1243 if (flags && neg_flags) {
1245 "%s: cannot specify both positive and negative flags\n",
1250 if (!flags && neg_flags)
1251 flags = neg_flags | LCME_FL_NEG;
1253 if (flags && comp_id) {
1255 "%s: cannot specify component ID and flags at the same time\n",
1260 if (!flags && !comp_id) {
1262 "%s: neither flags nor component ID is specified\n",
1268 if (flags & ~LCME_KNOWN_FLAGS) {
1270 "%s setstripe: unknown flags %#x\n",
1274 } else if (comp_id > LCME_ID_MAX) {
1275 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1280 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1283 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1284 progname, comp_id, fname, strerror(errno));
1288 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1295 rc = llapi_layout_file_comp_add(fname, layout);
1297 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1298 fname, strerror(errno));
1302 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1303 struct llapi_layout *layout)
1311 fd = lstat(fname, &st);
1312 if (fd == 0 && S_ISDIR(st.st_mode))
1313 open_flags = O_DIRECTORY | O_RDONLY;
1315 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1317 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1318 S_ISDIR(st.st_mode) ?
1319 "set default composite layout for" :
1320 "create composite file",
1321 fname, strerror(errno));
1325 static int lfs_migrate(char *name, __u64 migration_flags,
1326 struct llapi_stripe_param *param,
1327 struct llapi_layout *layout,
1328 unsigned long long bandwidth_bytes_sec,
1329 long stats_interval_sec)
1331 struct llapi_layout *existing;
1332 uint64_t dom_new, dom_cur;
1339 rc = migrate_open_files(name, migration_flags, param, layout,
1344 rc = llapi_layout_dom_size(layout, &dom_new);
1346 error_loc = "cannot get new layout DoM size";
1349 /* special case for migration to DOM layout*/
1350 existing = llapi_layout_get_by_fd(fd_src, 0);
1352 error_loc = "cannot get existing layout";
1356 rc = llapi_layout_dom_size(existing, &dom_cur);
1358 error_loc = "cannot get current layout DoM size";
1363 * if file has DoM layout already then migration is possible to
1364 * the new layout with the same DoM component via swap layout,
1365 * if new layout used bigger DOM size, then mirroring is used
1367 if (dom_new > dom_cur) {
1368 rc = lfs_migrate_to_dom(fd_src, fd_dst, name,
1370 bandwidth_bytes_sec,
1371 stats_interval_sec);
1373 error_loc = "cannot migrate to DOM layout";
1377 if (stats_interval_sec)
1378 printf("%s:\n", name);
1380 if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1382 * Blocking mode (forced if servers do not support file lease).
1383 * It is also the default mode, since we cannot distinguish
1384 * between a broken lease and a server that does not support
1385 * atomic swap/close (LU-6785)
1387 rc = migrate_block(fd_src, fd_dst, bandwidth_bytes_sec,
1388 stats_interval_sec);
1392 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
1394 error_loc = "cannot get lease";
1398 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
1399 stats_interval_sec, &dv_src);
1401 llapi_lease_release(fd_src);
1405 rc = llapi_get_data_version(fd_dst, &dv_dst, LL_DV_RD_FLUSH);
1407 error_loc = "cannot get data version";
1411 * Atomically put lease, swap layouts and close.
1412 * for a migration we need to check data version on file did
1415 rc = llapi_fswap_layouts(fd_src, fd_dst, dv_src, dv_dst,
1416 SWAP_LAYOUTS_CLOSE);
1418 error_loc = "cannot swap layout";
1430 fprintf(stderr, "error: %s: %s: %s: %s\n",
1431 progname, name, error_loc, strerror(-rc));
1432 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1433 printf("%s\n", name);
1438 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1441 char *dup_string = NULL;
1447 if (!string || !string[0])
1450 dup_string = strdup(string);
1452 llapi_printf(LLAPI_MSG_ERROR,
1453 "%s: insufficient memory\n",
1458 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1462 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1463 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1464 const char *comp_name = comp_flags_table[i].cfn_name;
1466 if (strcmp(name, comp_name) == 0) {
1467 *flags |= comp_flag;
1469 } else if (strncmp(name, "^", 1) == 0 &&
1470 strcmp(name + 1, comp_name) == 0) {
1471 *neg_flags |= comp_flag;
1476 llapi_printf(LLAPI_MSG_ERROR,
1477 "%s: component flag '%s' not supported\n",
1484 if (!*flags && !*neg_flags)
1487 /* don't allow to set and exclude the same flag */
1488 if (*flags & *neg_flags)
1496 static int mdthash_input(char *string, __u32 *inflags,
1497 __u32 *exflags, __u32 *type)
1504 {"migrating", LMV_HASH_FLAG_MIGRATION},
1505 {"bad_type", LMV_HASH_FLAG_BAD_TYPE},
1506 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1507 {"lost_lmv", LMV_HASH_FLAG_LOST_LMV},
1508 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1517 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1521 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1522 if (strcmp(name, mhflist[i].name) == 0 ||
1523 name[0] == mhflist[i].name[0]) {
1524 *inflags |= mhflist[i].flag;
1526 } else if (name[0] == '^' &&
1527 (strcmp(name + 1, mhflist[i].name) == 0 ||
1528 name[1] == mhflist[i].name[0])) {
1529 *exflags |= mhflist[i].flag;
1534 i = check_hashtype(name);
1539 llapi_printf(LLAPI_MSG_ERROR,
1540 "%s: invalid mdt_hash value '%s'\n",
1546 /* don't allow to include and exclude the same flag */
1547 if (*inflags & *exflags) {
1548 llapi_printf(LLAPI_MSG_ERROR,
1549 "%s: include and exclude same flag '%s'\n",
1557 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1565 if (strncmp(string, "^", 1) == 0) {
1566 *neg_state = llapi_layout_string_flags(string + 1);
1567 if (*neg_state != 0)
1570 *state = llapi_layout_string_flags(string);
1575 llapi_printf(LLAPI_MSG_ERROR,
1576 "%s: mirrored file state '%s' not supported\n",
1582 * struct mirror_args - Command-line arguments for mirror(s).
1583 * @m_count: Number of mirrors to be created with this layout.
1584 * @m_flags: Mirror level flags, only 'prefer' is supported.
1585 * @m_layout: Mirror layout.
1586 * @m_file: A victim file. Its layout will be split and used as a mirror.
1587 * @m_next: Point to the next node of the list.
1589 * Command-line arguments for mirror(s) will be parsed and stored in
1590 * a linked list that consists of this structure.
1592 struct mirror_args {
1595 struct llapi_layout *m_layout;
1597 struct mirror_args *m_next;
1602 * enum mirror_flags - Flags for extending a mirrored file.
1603 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1604 * in case the victim file(s) contains the same data as the
1605 * original mirrored file.
1606 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1607 * @MF_COMP_ID: specified component id instead of mirror id
1609 * Flags for extending a mirrored file.
1619 * mirror_create_sanity_check() - Check mirror list.
1620 * @list: A linked list that stores the mirror arguments.
1622 * This function does a sanity check on @list for creating
1625 * Return: 0 on success or a negative error code on failure.
1627 static int mirror_create_sanity_check(const char *fname,
1628 struct mirror_args *list)
1631 bool has_m_file = false;
1632 char fsname[MAX_OBD_NAME + 1] = { 0 };
1633 bool has_m_layout = false;
1639 rc = llapi_search_fsname(fname, fsname);
1642 "error: %s: file '%s' has no fsname\n",
1651 llapi_layout_free(list->m_layout);
1654 llapi_layout_get_by_path(list->m_file, 0);
1655 if (!list->m_layout) {
1657 "error: %s: file '%s' has no layout\n",
1658 progname, list->m_file);
1662 has_m_layout = true;
1663 if (!list->m_layout) {
1664 fprintf(stderr, "error: %s: no mirror layout\n",
1669 rc = llapi_layout_v2_sanity(list->m_layout, false, true,
1672 llapi_layout_sanity_perror(rc);
1676 list = list->m_next;
1679 if (has_m_file && has_m_layout) {
1681 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1689 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1691 __u32 mirror_flags = *(__u32 *)cbdata;
1695 rc = llapi_layout_comp_flags_get(layout, &flags);
1700 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1705 return LLAPI_LAYOUT_ITER_CONT;
1709 * mirror_create() - Create a mirrored file.
1710 * @fname: The file to be created.
1711 * @mirror_list: A linked list that stores the mirror arguments.
1713 * This function creates a mirrored file @fname with the mirror(s)
1714 * from @mirror_list.
1716 * Return: 0 on success or a negative error code on failure.
1718 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1720 struct llapi_layout *layout = NULL;
1721 struct mirror_args *cur_mirror = NULL;
1722 uint16_t mirror_count = 0;
1726 rc = mirror_create_sanity_check(fname, mirror_list);
1730 cur_mirror = mirror_list;
1731 while (cur_mirror) {
1732 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1734 &cur_mirror->m_flags);
1737 fprintf(stderr, "%s: failed to set mirror flags\n",
1742 for (i = 0; i < cur_mirror->m_count; i++) {
1743 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1747 "error: %s: merge layout failed: %s\n",
1748 progname, strerror(errno));
1752 mirror_count += cur_mirror->m_count;
1753 cur_mirror = cur_mirror->m_next;
1757 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1761 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1764 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1765 progname, strerror(errno));
1769 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1777 llapi_layout_free(layout);
1782 * Compare files and check lease on @fd.
1784 * \retval bytes number of bytes are the same
1786 static ssize_t mirror_file_compare(int fd_src, int fd_dst)
1788 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1790 ssize_t bytes_done = 0;
1791 ssize_t bytes_read = 0;
1793 buf = malloc(buflen * 2);
1798 if (!llapi_lease_check(fd_src)) {
1799 bytes_done = -EBUSY;
1803 bytes_read = read(fd_src, buf, buflen);
1804 if (bytes_read <= 0)
1807 if (bytes_read != read(fd_dst, buf + buflen, buflen))
1811 * XXX: should compute the checksum on each buffer and then
1812 * compare checksum to avoid cache collision
1814 if (memcmp(buf, buf + buflen, bytes_read))
1817 bytes_done += bytes_read;
1825 static int mirror_extend_file(const char *fname, const char *victim_file,
1826 enum mirror_flags mirror_flags)
1831 struct stat stbuf_v;
1832 struct ll_ioc_lease *data = NULL;
1835 fd = open(fname, O_RDWR);
1837 error_loc = "open source file";
1842 fdv = open(victim_file, O_RDWR);
1844 error_loc = "open target file";
1849 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1850 error_loc = "stat source or target file";
1855 if (stbuf.st_dev != stbuf_v.st_dev) {
1856 error_loc = "stat source and target file";
1861 /* mirrors should be of the same size */
1862 if (stbuf.st_size != stbuf_v.st_size) {
1863 error_loc = "file sizes don't match";
1868 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1870 error_loc = "cannot get lease";
1874 if (!(mirror_flags & MF_NO_VERIFY)) {
1876 /* mirrors should have the same contents */
1877 ret = mirror_file_compare(fd, fdv);
1878 if (ret != stbuf.st_size) {
1879 error_loc = "file busy or contents don't match";
1880 rc = ret < 0 ? ret : -EINVAL;
1885 /* Get rid of caching pages from clients */
1886 rc = llapi_file_flush(fd);
1888 error_loc = "cannot get data version";
1892 rc = llapi_file_flush(fdv);
1894 error_loc = "cannot get data version";
1898 rc = migrate_set_timestamps(fd, &stbuf);
1900 error_loc = "cannot set source file timestamp";
1904 /* Atomically put lease, merge layouts and close. */
1905 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1907 error_loc = "memory allocation";
1910 data->lil_mode = LL_LEASE_UNLCK;
1911 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1912 data->lil_count = 1;
1913 data->lil_ids[0] = fdv;
1914 rc = llapi_lease_set(fd, data);
1916 error_loc = "cannot merge layout";
1918 } else if (rc == 0) {
1920 error_loc = "lost lease lock";
1933 (void) unlink(victim_file);
1935 fprintf(stderr, "error: %s: %s: %s: %s\n",
1936 progname, fname, error_loc, strerror(-rc));
1940 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1941 bool inherit, uint32_t flags,
1942 unsigned long long bandwidth_bytes_sec,
1943 long stats_interval_sec)
1945 struct llapi_layout *f_layout = NULL;
1946 struct ll_ioc_lease *data = NULL;
1953 f_layout = llapi_layout_get_by_path(name, 0);
1956 fprintf(stderr, "%s: cannot get layout\n", progname);
1959 rc = llapi_layout_get_last_init_comp(f_layout);
1961 fprintf(stderr, "%s: cannot get the last init comp\n",
1965 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1968 "%s: cannot inherit from the last init comp\n",
1974 llapi_layout_comp_flags_set(m_layout, flags);
1975 rc = migrate_open_files(name,
1976 LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1977 NULL, m_layout, &fd_src, &fd_dst);
1981 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
1983 error_loc = "cannot get lease";
1987 rc = fstat(fd_src, &st);
1989 error_loc = "cannot stat source file";
1993 if (stats_interval_sec)
1994 printf("%s:\n", name);
1996 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
1997 stats_interval_sec, NULL);
1999 llapi_lease_release(fd_src);
2003 rc = migrate_set_timestamps(fd_src, &st);
2005 error_loc = "cannot set source file timestamp";
2009 /* Atomically put lease, merge layouts and close. */
2010 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2012 error_loc = "memory allocation";
2015 data->lil_mode = LL_LEASE_UNLCK;
2016 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2017 data->lil_count = 1;
2018 data->lil_ids[0] = fd_dst;
2019 rc = llapi_lease_set(fd_src, data);
2021 error_loc = "cannot merge layout";
2023 } else if (rc == 0) {
2025 error_loc = "lost lease lock";
2038 fprintf(stderr, "error: %s: %s: %s: %s\n",
2039 progname, name, error_loc, strerror(-rc));
2043 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2044 enum mirror_flags mirror_flags,
2045 unsigned long long bandwidth_bytes_sec,
2046 long stats_interval_sec)
2050 while (mirror_list) {
2051 if (mirror_list->m_file) {
2052 rc = mirror_extend_file(fname, mirror_list->m_file,
2055 __u32 mirror_count = mirror_list->m_count;
2057 while (mirror_count > 0) {
2058 rc = mirror_extend_layout(fname,
2059 mirror_list->m_layout,
2060 mirror_list->m_inherit,
2061 mirror_list->m_flags,
2062 bandwidth_bytes_sec,
2063 stats_interval_sec);
2073 mirror_list = mirror_list->m_next;
2079 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2084 rc = llapi_layout_mirror_id_get(layout, &id);
2088 if ((__u16)id == *(__u16 *)cbdata)
2089 return LLAPI_LAYOUT_ITER_STOP;
2091 return LLAPI_LAYOUT_ITER_CONT;
2094 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2099 rc = llapi_layout_comp_id_get(layout, &id);
2103 if (id == *(__u32 *)cbdata)
2104 return LLAPI_LAYOUT_ITER_STOP;
2106 return LLAPI_LAYOUT_ITER_CONT;
2109 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2111 char buf[LOV_MAXPOOLNAME + 1];
2112 struct pool_to_id_cbdata *d = (void *)cbdata;
2116 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2119 if (strcmp(d->pool, buf))
2120 return LLAPI_LAYOUT_ITER_CONT;
2122 rc = llapi_layout_mirror_id_get(layout, &id);
2127 return LLAPI_LAYOUT_ITER_STOP;
2130 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2132 char buf[LOV_MAXPOOLNAME + 1];
2133 struct pool_to_id_cbdata *d = (void *)cbdata;
2137 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2140 if (strcmp(d->pool, buf))
2141 return LLAPI_LAYOUT_ITER_CONT;
2143 rc = llapi_layout_comp_id_get(layout, &id);
2148 return LLAPI_LAYOUT_ITER_STOP;
2151 struct collect_ids_data {
2157 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2159 struct collect_ids_data *cid = cbdata;
2163 rc = llapi_layout_mirror_id_get(layout, &id);
2167 if ((__u16)id != cid->cid_exclude) {
2170 for (i = 0; i < cid->cid_count; i++) {
2171 /* already collected the mirror id */
2172 if (id == cid->cid_ids[i])
2173 return LLAPI_LAYOUT_ITER_CONT;
2175 cid->cid_ids[cid->cid_count] = id;
2179 return LLAPI_LAYOUT_ITER_CONT;
2183 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2184 * @mirror_id: Mirror id to be checked.
2185 * @layout: Mirror component list.
2187 * This function checks if a mirror with specified @mirror_id is the last
2188 * non-stale mirror of a layout @layout.
2190 * Return: true or false.
2193 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2195 __u16 mirror_ids[128] = { 0 };
2196 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2198 .cid_exclude = mirror_id, };
2201 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2203 for (i = 0; i < cid.cid_count; i++) {
2204 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2207 comp_size = llapi_mirror_find_stale(layout, comp_array,
2208 ARRAY_SIZE(comp_array),
2217 static int mirror_split(const char *fname, __u32 id, const char *pool,
2218 enum mirror_flags mflags, const char *victim_file)
2220 struct llapi_layout *layout;
2221 char parent[PATH_MAX];
2222 char victim[PATH_MAX];
2223 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2225 struct ll_ioc_lease *data;
2226 uint16_t mirror_count;
2230 bool purge = true; /* delete mirror by setting fdv=fd */
2234 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2236 "error %s: the source file '%s' and -f file are the same\n",
2241 /* check fname contains mirror with mirror_id/comp_id */
2242 layout = llapi_layout_get_by_path(fname, 0);
2245 "error %s: file '%s' couldn't get layout\n",
2250 rc = llapi_layout_sanity(layout, false, true);
2252 llapi_layout_sanity_perror(rc);
2256 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2259 "error %s: file '%s' couldn't get mirror count\n",
2263 if (mirror_count < 2) {
2265 "error %s: file '%s' has %d component, cannot split\n",
2266 progname, fname, mirror_count);
2270 if (mflags & MF_COMP_POOL) {
2271 struct pool_to_id_cbdata data = { .pool = pool };
2273 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2275 mirror_id = data.id;
2276 } else if (mflags & MF_COMP_ID) {
2277 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2278 mirror_id = mirror_id_of(id);
2280 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2284 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2287 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2288 if (mflags & MF_COMP_POOL) {
2290 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2291 progname, fname, pool);
2293 } else if (mflags & MF_COMP_ID) {
2295 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2296 progname, fname, id);
2300 "error %s: file '%s' does not contain mirror with id %u\n",
2301 progname, fname, id);
2306 if (last_non_stale_mirror(mirror_id, layout)) {
2309 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2314 if (!victim_file && mflags & MF_DESTROY)
2315 /* Allow mirror split even without the key on encrypted files,
2316 * and in this case of a 'split -d', open file with O_DIRECT
2317 * (no IOs will be done).
2319 fd = open(fname, O_RDWR | O_DIRECT | O_CIPHERTEXT);
2321 fd = open(fname, O_RDWR);
2325 "error %s: open file '%s' failed: %s\n",
2326 progname, fname, strerror(errno));
2330 /* get victim file directory pathname */
2331 if (strlen(fname) > sizeof(parent) - 1) {
2332 fprintf(stderr, "error %s: file name of '%s' too long\n",
2337 strncpy(parent, fname, sizeof(parent));
2338 ptr = strrchr(parent, '/');
2340 if (!getcwd(parent, sizeof(parent))) {
2341 fprintf(stderr, "error %s: getcwd failed: %s\n",
2342 progname, strerror(errno));
2352 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2354 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2359 rc = llapi_file_is_encrypted(fd);
2361 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2362 progname, fname, rc);
2369 /* use a temp file to store the splitted layout */
2370 if (mflags & MF_DESTROY) {
2371 char file_path[PATH_MAX];
2372 unsigned int rnumber;
2375 if (last_non_stale_mirror(mirror_id, layout)) {
2378 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2384 /* don't use volatile file for mirror destroy */
2388 * try the old way to delete mirror using
2393 rc = snprintf(file_path,
2395 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2399 rc >= sizeof(file_path)) {
2400 fdv = -ENAMETOOLONG;
2404 open_flags = O_RDWR |
2405 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2406 O_CREAT | O_EXCL | O_NOFOLLOW |
2407 /* O_DIRECT for mirror split -d */
2409 /* Allow split without the key */
2411 fdv = open(file_path, open_flags,
2415 } while (fdv < 0 && rc == -EEXIST);
2421 "error %s: not permitted on encrypted file '%s': %d\n",
2422 progname, fname, rc);
2426 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2428 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2431 /* user specified victim file */
2435 "error %s: not permitted on encrypted file '%s': %d\n",
2436 progname, fname, rc);
2439 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2444 "error %s: create victim file failed: %s\n",
2445 progname, strerror(errno));
2449 /* get lease lock of fname */
2450 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2453 "error %s: cannot get lease of file '%s': %d\n",
2454 progname, fname, rc);
2458 /* Atomatically put lease, split layouts and close. */
2459 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2465 data->lil_mode = LL_LEASE_UNLCK;
2466 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2467 data->lil_count = 2;
2468 data->lil_ids[0] = fdv;
2469 data->lil_ids[1] = mirror_id;
2470 rc = llapi_lease_set(fd, data);
2472 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2473 /* could be old MDS which prohibit fd==fdv */
2478 if (rc == 0) /* lost lease lock */
2481 "error %s: cannot split '%s': %s\n",
2482 progname, fname, strerror(-rc));
2494 llapi_layout_free(layout);
2499 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2500 __u16 *mirror_ids, int ids_nr,
2501 long stats_interval_sec, long bandwidth_bytes_sec);
2504 static int lfs_migrate_to_dom(int fd_src, int fd_dst, char *name,
2505 __u64 migration_flags,
2506 unsigned long long bandwidth_bytes_sec,
2507 long stats_interval_sec)
2509 struct ll_ioc_lease *data = NULL;
2512 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
2514 error_loc = "cannot get lease";
2518 if (stats_interval_sec)
2519 printf("%s:\n", name);
2521 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
2522 stats_interval_sec, NULL);
2526 /* Atomically put lease, merge layouts, resync and close. */
2527 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2529 error_loc = "memory allocation";
2532 data->lil_mode = LL_LEASE_UNLCK;
2533 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2534 data->lil_count = 1;
2535 data->lil_ids[0] = fd_dst;
2536 rc = llapi_lease_set(fd_src, data);
2538 error_loc = "cannot merge layout";
2540 } else if (rc == 0) {
2542 error_loc = "lost lease lock";
2548 rc = lfs_mirror_resync_file(name, data, NULL, 0,
2550 bandwidth_bytes_sec);
2552 error_loc = "cannot resync file";
2556 /* delete first mirror now */
2557 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2559 error_loc = "cannot delete old layout";
2563 llapi_lease_release(fd_src);
2569 fprintf(stderr, "error: %s: %s: %s: %s\n",
2570 progname, name, error_loc, strerror(-rc));
2571 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2572 printf("%s\n", name);
2579 * Parse a string containing an target index list into an array of integers.
2581 * The input string contains a comma delimited list of individual
2582 * indices and ranges, for example "1,2-4,7". Add the indices into the
2583 * \a tgts array and remove duplicates.
2585 * \param[out] tgts array to store indices in
2586 * \param[in] size size of \a tgts array
2587 * \param[in] offset starting index in \a tgts
2588 * \param[in] arg string containing OST index list
2589 * \param[out] duplicates tell caller list contains duplicates
2591 * \retval positive number of indices in \a tgts
2592 * \retval -EINVAL unable to parse \a arg
2594 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2599 int slots = size - offset;
2606 end_of_loop = false;
2607 while (!end_of_loop) {
2608 int start_index = 0;
2611 char *endptr = NULL;
2615 ptr = strchrnul(arg, ',');
2617 end_of_loop = *ptr == '\0';
2621 start_index = strtol(arg, &endptr, 0);
2622 if (endptr == arg) /* no data at all */
2624 if (errno != 0 || start_index < -1 ||
2625 (*endptr != '-' && *endptr != '\0'))
2628 end_index = start_index;
2629 if (*endptr == '-') {
2631 end_index = strtol(endptr + 1, &endptr, 0);
2632 if (errno != 0 || *endptr != '\0' || end_index < -1)
2634 if (end_index < start_index)
2638 for (i = start_index; i <= end_index && slots > 0; i++) {
2641 /* note presence of duplicates */
2642 for (j = 0; j < offset; j++) {
2649 if (j == offset) { /* check complete */
2655 if (slots == 0 && i < end_index)
2663 if (!end_of_loop && ptr)
2666 return rc < 0 ? rc : nr;
2669 struct lfs_setstripe_args {
2670 unsigned long long lsa_comp_end;
2671 unsigned long long lsa_stripe_size;
2672 unsigned long long lsa_extension_size;
2673 long long lsa_stripe_count;
2674 long long lsa_stripe_off;
2675 __u32 lsa_comp_flags;
2676 __u32 lsa_comp_neg_flags;
2677 unsigned long long lsa_pattern;
2678 unsigned int lsa_mirror_count;
2680 bool lsa_first_comp;
2681 bool lsa_extension_comp;
2683 char *lsa_pool_name;
2686 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2688 unsigned int mirror_count = lsa->lsa_mirror_count;
2689 bool first_comp = lsa->lsa_first_comp;
2691 memset(lsa, 0, sizeof(*lsa));
2693 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2694 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2695 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2696 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2697 lsa->lsa_pool_name = NULL;
2699 lsa->lsa_mirror_count = mirror_count;
2700 lsa->lsa_first_comp = first_comp;
2704 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2705 * @lsa: Stripe options to be initialized and inherited.
2707 * This function initializes stripe options in @lsa and inherit
2708 * stripe_size, stripe_count and OST pool_name options.
2712 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2714 unsigned long long stripe_size;
2715 long long stripe_count;
2716 char *pool_name = NULL;
2718 stripe_size = lsa->lsa_stripe_size;
2719 stripe_count = lsa->lsa_stripe_count;
2720 pool_name = lsa->lsa_pool_name;
2722 setstripe_args_init(lsa);
2724 lsa->lsa_stripe_size = stripe_size;
2725 lsa->lsa_stripe_count = stripe_count;
2726 lsa->lsa_pool_name = pool_name;
2729 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2731 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2732 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2733 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2734 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2735 lsa->lsa_comp_end != 0);
2738 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2740 if (lsa->lsa_nr_tgts) {
2741 if (lsa->lsa_nr_tgts < 0 ||
2742 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2743 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2749 if (lsa->lsa_stripe_count > 0 &&
2750 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2751 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2752 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2753 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2754 lsa->lsa_stripe_count,
2766 * comp_args_to_layout() - Create or extend a composite layout.
2767 * @composite: Pointer to the composite layout.
2768 * @lsa: Stripe options for the new component.
2770 * This function creates or extends a composite layout by adding a new
2771 * component with stripe options from @lsa.
2773 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2775 * Return: 0 on success or an error code on failure.
2777 static int comp_args_to_layout(struct llapi_layout **composite,
2778 struct lfs_setstripe_args *lsa,
2781 struct llapi_layout *layout = *composite;
2782 uint64_t prev_end = 0;
2788 layout = llapi_layout_alloc();
2790 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2795 *composite = layout;
2796 lsa->lsa_first_comp = true;
2801 * Get current component extent, current component
2802 * must be the tail component.
2804 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2806 fprintf(stderr, "Get comp extent failed. %s\n",
2811 if (lsa->lsa_first_comp) {
2813 rc = llapi_layout_add_first_comp(layout);
2815 rc = llapi_layout_comp_add(layout);
2818 fprintf(stderr, "Add component failed. %s\n",
2824 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2826 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2827 lsa->lsa_comp_flags, strerror(errno));
2832 uint64_t comp_end = lsa->lsa_comp_end;
2835 * The extendable component is 0-length, so it can be removed
2836 * if there is insufficient space to extend it.
2838 if (lsa->lsa_extension_comp)
2839 comp_end = prev_end;
2841 rc = llapi_layout_comp_extent_set(layout, prev_end,
2844 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2845 prev_end, comp_end, strerror(errno));
2849 /* reset lsa_first_comp */
2850 lsa->lsa_first_comp = false;
2852 /* Data-on-MDT component setting */
2853 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2855 if (lsa->lsa_stripe_count == 0)
2856 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2857 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2858 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2859 if (lsa->lsa_stripe_off == -1 ||
2860 lsa->lsa_stripe_off == 0)
2861 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2863 * In case of Data-on-MDT patterns the only extra option
2864 * applicable is stripe size option.
2866 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2868 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2869 lsa->lsa_stripe_count);
2873 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2874 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2876 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2877 lsa->lsa_stripe_size);
2881 if (lsa->lsa_nr_tgts != 0) {
2883 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2888 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2890 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2891 lsa->lsa_stripe_off);
2895 if (lsa->lsa_pool_name != 0) {
2897 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2898 lsa->lsa_pool_name);
2903 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2905 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2910 /* Data-on-MDT component has always single stripe up to end */
2911 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2912 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2913 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2915 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2922 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2923 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2925 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2926 rc = llapi_layout_extension_size_set(layout, size);
2928 rc = llapi_layout_stripe_size_set(layout, size);
2931 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2932 size, strerror(errno));
2936 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2938 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2939 lsa->lsa_stripe_count, strerror(errno));
2943 if (lsa->lsa_pool_name) {
2944 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2946 fprintf(stderr, "Set pool name: %s failed. %s\n",
2947 lsa->lsa_pool_name, strerror(errno));
2951 rc = llapi_layout_pool_name_set(layout, "");
2953 fprintf(stderr, "Clear pool name failed: %s\n",
2959 rc = lsa_args_stripe_count_check(lsa);
2963 if (lsa->lsa_nr_tgts > 0) {
2966 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2967 rc = llapi_layout_ost_index_set(layout, i,
2972 /* Make sure stripe offset is in OST list. */
2973 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2977 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2978 lsa->lsa_stripe_off);
2982 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2983 lsa->lsa_stripe_off != -1) {
2984 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2987 fprintf(stderr, "Set ost index %d failed. %s\n",
2988 i, strerror(errno));
2992 /* Create the second, virtual component of extension space */
2993 if (lsa->lsa_extension_comp) {
2994 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2995 lsa->lsa_extension_comp = false;
3002 static int build_component(struct llapi_layout **layout,
3003 struct lfs_setstripe_args *lsa, bool set_extent)
3007 rc = comp_args_to_layout(layout, lsa, set_extent);
3011 if (lsa->lsa_mirror_count > 0) {
3012 rc = llapi_layout_mirror_count_set(*layout,
3013 lsa->lsa_mirror_count);
3017 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
3020 lsa->lsa_mirror_count = 0;
3026 static int build_prev_component(struct llapi_layout **layout,
3027 struct lfs_setstripe_args *prev,
3028 struct lfs_setstripe_args *lsa,
3031 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
3034 if (prev->lsa_stripe_size) {
3036 prev->lsa_comp_end = lsa->lsa_comp_end;
3037 prev->lsa_extension_size = lsa->lsa_extension_size;
3038 prev->lsa_extension_comp = true;
3041 rc = build_component(layout, prev, true);
3047 * Copy lsa to previous lsa;
3048 * if this is an extension component, make the previous invalid;
3051 prev->lsa_stripe_size = 0;
3058 #ifndef LCME_TEMPLATE_FLAGS
3059 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
3063 static int build_layout_from_yaml_node(struct cYAML *node,
3064 struct llapi_layout **layout,
3065 struct lfs_setstripe_args *lsa,
3066 struct lfs_setstripe_args *prevp)
3068 struct lfs_setstripe_args prev = { 0 };
3069 __u32 *osts = lsa->lsa_tgts;
3077 string = node->cy_string;
3079 if (node->cy_type == CYAML_TYPE_OBJECT) {
3080 /* go deep to sub blocks */
3081 if (string && !strncmp(string, "component", 9) &&
3082 strncmp(string, "component0", 10) &&
3083 strncmp(string, "components", 10)) {
3084 rc = build_prev_component(layout, prevp, lsa,
3089 /* initialize lsa. */
3090 setstripe_args_init(lsa);
3091 lsa->lsa_first_comp = false;
3092 lsa->lsa_tgts = osts;
3095 rc = build_layout_from_yaml_node(node->cy_child, layout,
3100 if (!node->cy_string)
3103 /* skip leading lmm_ if present, to simplify parsing */
3104 if (strncmp(string, "lmm_", 4) == 0)
3107 if (node->cy_type == CYAML_TYPE_STRING) {
3108 if (!strcmp(string, "lcme_extent.e_end")) {
3109 if (!strcmp(node->cy_valuestring, "EOF") ||
3110 !strcmp(node->cy_valuestring, "eof"))
3111 lsa->lsa_comp_end = LUSTRE_EOF;
3112 } else if (!strcmp(string, "pool")) {
3113 lsa->lsa_pool_name = node->cy_valuestring;
3114 } else if (!strcmp(string, "pattern")) {
3115 if (!strcmp(node->cy_valuestring, "mdt"))
3116 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3117 if (!strcmp(node->cy_valuestring,
3118 "raid0,overstriped"))
3120 LLAPI_LAYOUT_OVERSTRIPING;
3121 } else if (!strcmp(string, "lcme_flags")) {
3122 rc = comp_str2flags(node->cy_valuestring,
3123 &lsa->lsa_comp_flags,
3124 &lsa->lsa_comp_neg_flags);
3128 * Only template flags have meaning in
3129 * the layout for a new file
3131 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3133 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3134 if (!strcmp(string, "lcm_mirror_count")) {
3135 lsa->lsa_mirror_count = node->cy_valueint;
3136 } else if (!strcmp(string, "lcme_extent.e_start")) {
3137 if (node->cy_valueint == 0)
3138 lsa->lsa_first_comp = true;
3139 } else if (!strcmp(string, "lcme_extent.e_end")) {
3140 if (node->cy_valueint == -1)
3141 lsa->lsa_comp_end = LUSTRE_EOF;
3143 lsa->lsa_comp_end = node->cy_valueint;
3144 } else if (!strcmp(string, "stripe_count")) {
3145 lsa->lsa_stripe_count = node->cy_valueint;
3146 } else if (!strcmp(string, "stripe_size")) {
3147 lsa->lsa_stripe_size = node->cy_valueint;
3148 } else if (!strcmp(string, "extension_size")) {
3149 lsa->lsa_extension_size = node->cy_valueint;
3150 lsa->lsa_extension_comp = true;
3151 } else if (!strcmp(string, "stripe_offset")) {
3152 lsa->lsa_stripe_off = node->cy_valueint;
3153 } else if (!strcmp(string, "l_ost_idx")) {
3154 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3159 node = node->cy_next;
3162 if (prevp == &prev) {
3163 rc = build_prev_component(layout, prevp, lsa, true);
3167 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3168 rc = build_component(layout, lsa, *layout != NULL);
3174 static int lfs_comp_create_from_yaml(char *template,
3175 struct llapi_layout **layout,
3176 struct lfs_setstripe_args *lsa,
3179 struct cYAML *tree = NULL, *err_rc = NULL;
3182 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3184 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3185 progname, template);
3186 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3187 "can't parse", &err_rc);
3188 cYAML_print_tree2file(stderr, err_rc);
3189 cYAML_free_tree(err_rc);
3194 /* initialize lsa for plain file */
3195 setstripe_args_init(lsa);
3196 lsa->lsa_tgts = osts;
3198 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3200 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3201 progname, template);
3204 /* clean clean lsa */
3205 setstripe_args_init(lsa);
3209 cYAML_free_tree(tree);
3214 * Get the extension size from the next (SEL) component and extend the
3215 * current component on it. The start of the next component is to be
3218 * \param[in] layout the current layout
3219 * \param[in] start the start of the current component
3220 * \param[in,out] end the end of the current component
3221 * \param[in] offset the offset to adjust the end position to instead of
3224 * \retval 0 - extended successfully
3225 * \retval < 0 - error
3227 static int layout_extend_comp(struct llapi_layout *layout,
3228 uint64_t start, uint64_t *end,
3231 uint64_t size, next_start, next_end;
3234 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3237 "%s setstripe: cannot move component cursor: %s\n",
3238 progname, strerror(errno));
3243 * Even if the @size will not be used below, this will fail if
3244 * this is not a SEL component - a good confirmation we are
3245 * working on right components.
3247 rc = llapi_layout_extension_size_get(layout, &size);
3250 "%s setstripe: cannot get component ext size: %s\n",
3251 progname, strerror(errno));
3255 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3257 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3258 progname, strerror(errno));
3262 next_start += offset ?: size;
3263 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3265 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3266 progname, strerror(errno));
3270 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3273 "%s setstripe: cannot move component cursor: %s\n",
3274 progname, strerror(errno));
3278 *end += offset ?: size;
3279 rc = llapi_layout_comp_extent_set(layout, start, *end);
3281 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3282 progname, strerror(errno));
3290 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3291 * end of the last component in the existing file, and adjust the
3292 * first extent start of the components to be added accordingly.
3294 * In the create mode, we need to check if the first component is an extendable
3295 * SEL component and extend its length to the extension size (first component
3296 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3298 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3301 struct llapi_layout *head;
3302 uint64_t start = 0, prev_end = 0;
3306 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3311 head = llapi_layout_get_by_path(fname, 0);
3314 "%s setstripe: cannot read layout from '%s': %s\n",
3315 progname, fname, strerror(errno));
3317 } else if (errno == ENODATA) {
3319 * file without LOVEA, this component-add will be turned
3320 * into a component-create.
3322 llapi_layout_free(head);
3326 * the new layout will be added to an empty one, it
3327 * still needs to be adjusted below
3331 } else if (!llapi_layout_is_composite(head)) {
3333 "%s setstripe: '%s' not a composite file\n",
3335 llapi_layout_free(head);
3339 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3342 "%s setstripe: cannot get prev extent: %s\n",
3343 progname, strerror(errno));
3344 llapi_layout_free(head);
3348 llapi_layout_free(head);
3352 /* Make sure we use the first component of the layout to be added. */
3353 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3356 "%s setstripe: cannot move component cursor: %s\n",
3357 progname, strerror(errno));
3361 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3363 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3364 progname, strerror(errno));
3368 if (start == 0 && end == 0) {
3369 rc = layout_extend_comp(layout, start, &end,
3370 comp_add ? prev_end : 0);
3375 if (start > prev_end || end < prev_end) {
3377 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3378 progname, start, end, prev_end);
3382 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3385 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3386 progname, prev_end, end, strerror(errno));
3393 static int mirror_adjust_first_extents(struct mirror_args *list)
3401 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3404 list = list->m_next;
3410 static inline bool arg_is_eof(char *arg)
3412 return !strncmp(arg, "-1", strlen("-1")) ||
3413 !strncmp(arg, "EOF", strlen("EOF")) ||
3414 !strncmp(arg, "eof", strlen("eof"));
3418 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3420 * Return: Valid mirror_args pointer on success and
3421 * NULL if memory allocation fails.
3423 static struct mirror_args *lfs_mirror_alloc(void)
3425 struct mirror_args *mirror = NULL;
3428 mirror = calloc(1, sizeof(*mirror));
3430 mirror->m_inherit = false;
3441 * lfs_mirror_free() - Free memory allocated for a mirror argument
3443 * @mirror: Previously allocated mirror argument structure by
3444 * lfs_mirror_alloc().
3446 * Free memory allocated for @mirror.
3450 static void lfs_mirror_free(struct mirror_args *mirror)
3452 if (mirror->m_layout)
3453 llapi_layout_free(mirror->m_layout);
3458 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3459 * @mirror_list: Previously allocated mirror list.
3461 * Free memory allocated for @mirror_list.
3465 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3467 struct mirror_args *next_mirror = NULL;
3469 while (mirror_list) {
3470 next_mirror = mirror_list->m_next;
3471 lfs_mirror_free(mirror_list);
3472 mirror_list = next_mirror;
3477 LFS_SETQUOTA_DELETE = (CHAR_MAX + 1),
3485 LFS_COMP_NO_VERIFY_OPT,
3487 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3489 LFS_MIRROR_STATE_OPT,
3491 LFS_MIRROR_INDEX_OPT,
3492 LFS_LAYOUT_FOREIGN_OPT,
3501 LFS_STATS_INTERVAL_OPT,
3504 LFS_XATTRS_MATCH_OPT
3507 #ifndef LCME_USER_MIRROR_FLAGS
3508 /* The mirror flags can be set by users at creation time. */
3509 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3513 static int lfs_setstripe_internal(int argc, char **argv,
3514 enum setstripe_origin opc)
3516 struct lfs_setstripe_args lsa = { 0 };
3517 struct llapi_stripe_param *param = NULL;
3518 struct find_param migrate_mdt_param = {
3528 unsigned long long size_units = 1;
3529 bool migrate_mode = false;
3530 bool migrate_mdt_mode = false;
3531 bool setstripe_mode = false;
3532 bool migration_block = false;
3533 __u64 migration_flags = 0;
3534 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3535 int comp_del = 0, comp_set = 0;
3538 struct llapi_layout *layout = NULL;
3539 struct llapi_layout **lpp = &layout;
3540 bool mirror_mode = false;
3541 bool has_m_file = false;
3542 __u32 mirror_count = 0;
3543 enum mirror_flags mirror_flags = 0;
3544 struct mirror_args *mirror_list = NULL;
3545 struct mirror_args *new_mirror = NULL;
3546 struct mirror_args *last_mirror = NULL;
3547 __u16 mirror_id = 0;
3549 bool from_yaml = false;
3550 bool from_copy = false;
3551 char *template = NULL;
3552 bool foreign_mode = false;
3554 bool overstriped = false;
3555 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3556 char *mode_opt = NULL;
3557 mode_t previous_umask = 0;
3558 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3559 unsigned long long bandwidth_bytes_sec = 0;
3560 unsigned long long bandwidth_unit = ONE_MB;
3561 long stats_interval_sec = 0;
3563 struct option long_opts[] = {
3564 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3565 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3566 /* --block is only valid in migrate mode */
3567 { .val = 'b', .name = "block", .has_arg = no_argument },
3568 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3569 { .val = LFS_COMP_ADD_OPT,
3570 .name = "comp-add", .has_arg = no_argument },
3571 { .val = LFS_COMP_ADD_OPT,
3572 .name = "component-add", .has_arg = no_argument },
3573 { .val = LFS_COMP_DEL_OPT,
3574 .name = "comp-del", .has_arg = no_argument },
3575 { .val = LFS_COMP_DEL_OPT,
3576 .name = "component-del", .has_arg = no_argument },
3577 { .val = LFS_COMP_FLAGS_OPT,
3578 .name = "comp-flags", .has_arg = required_argument },
3579 { .val = LFS_COMP_FLAGS_OPT,
3580 .name = "component-flags",
3581 .has_arg = required_argument },
3582 { .val = LFS_COMP_SET_OPT,
3583 .name = "comp-set", .has_arg = no_argument },
3584 { .val = LFS_COMP_SET_OPT,
3585 .name = "component-set",
3586 .has_arg = no_argument},
3587 { .val = LFS_COMP_NO_VERIFY_OPT,
3588 .name = "no-verify", .has_arg = no_argument},
3589 { .val = LFS_LAYOUT_FLAGS_OPT,
3590 .name = "flags", .has_arg = required_argument},
3591 { .val = LFS_LAYOUT_FOREIGN_OPT,
3592 .name = "foreign", .has_arg = optional_argument},
3593 { .val = LFS_MIRROR_ID_OPT,
3594 .name = "mirror-id", .has_arg = required_argument},
3595 { .val = LFS_MODE_OPT,
3596 .name = "mode", .has_arg = required_argument},
3597 { .val = LFS_LAYOUT_COPY,
3598 .name = "copy", .has_arg = required_argument},
3599 { .val = LFS_STATS_OPT,
3600 .name = "stats", .has_arg = no_argument},
3601 { .val = LFS_STATS_INTERVAL_OPT,
3602 .name = "stats-interval",
3603 .has_arg = required_argument},
3604 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3605 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3606 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3607 { .val = 'C', .name = "overstripe-count",
3608 .has_arg = required_argument},
3609 { .val = 'C', .name = "mdt-overcount",
3610 .has_arg = required_argument},
3611 { .val = 'd', .name = "delete", .has_arg = no_argument},
3612 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3613 /* used with "lfs migrate -m" */
3614 { .val = 'd', .name = "directory", .has_arg = no_argument},
3615 /* --non-direct is only valid in migrate mode */
3616 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3617 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3618 { .val = 'E', .name = "component-end",
3619 .has_arg = required_argument},
3620 { .val = 'f', .name = "file", .has_arg = required_argument },
3621 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3622 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3623 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3624 { .val = 'h', .name = "help", .has_arg = no_argument },
3625 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3626 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3627 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3628 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3629 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3630 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3631 { .val = 'L', .name = "layout", .has_arg = required_argument },
3632 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3633 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3634 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3635 /* --non-block is only valid in migrate mode */
3636 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3637 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3638 { .val = 'o', .name = "ost", .has_arg = required_argument },
3639 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3640 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3641 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3643 { .val = 'p', .name = "pool", .has_arg = required_argument },
3644 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3645 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3646 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3647 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3648 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3649 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3650 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3651 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3652 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3653 /* --verbose is only valid in migrate mode */
3654 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3655 { .val = 'W', .name = "bandwidth", .has_arg = required_argument },
3656 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3657 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3658 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3659 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3660 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3663 setstripe_args_init(&lsa);
3665 migrate_mode = (opc == SO_MIGRATE);
3666 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3667 setstripe_mode = (opc == SO_SETSTRIPE);
3668 if (opc == SO_MIRROR_DELETE) {
3670 mirror_flags = MF_DESTROY;
3673 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3675 while ((c = getopt_long(argc, argv,
3676 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:W:y:z:",
3677 long_opts, NULL)) >= 0) {
3683 case LFS_COMP_ADD_OPT:
3686 case LFS_COMP_DEL_OPT:
3689 case LFS_COMP_FLAGS_OPT:
3690 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3691 &lsa.lsa_comp_neg_flags);
3694 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3696 "%s: inverted flags are not supported\n",
3701 case LFS_COMP_SET_OPT:
3704 case LFS_COMP_NO_VERIFY_OPT:
3705 mirror_flags |= MF_NO_VERIFY;
3707 case LFS_MIRROR_ID_OPT: {
3708 unsigned long int id;
3711 id = strtoul(optarg, &end, 0);
3712 if (errno != 0 || *end != '\0' || id == 0 ||
3715 "%s %s: invalid mirror ID '%s'\n",
3716 progname, argv[0], optarg);
3720 mirror_id = (__u16)id;
3723 case LFS_LAYOUT_FLAGS_OPT: {
3726 /* check for numeric flags (foreign and mirror cases) */
3727 if (setstripe_mode && !mirror_mode && !last_mirror) {
3729 flags = strtoul(optarg, &end, 16);
3730 if (errno != 0 || *end != '\0' ||
3731 flags >= UINT32_MAX) {
3733 "%s %s: invalid hex flags '%s'\n",
3734 progname, argv[0], optarg);
3737 if (!foreign_mode) {
3739 "%s %s: hex flags must be specified with --foreign option\n",
3746 if (!mirror_mode || !last_mirror) {
3748 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3753 result = comp_str2flags(optarg, &last_mirror->m_flags,
3760 "%s: inverted flags are not supported\n",
3765 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3767 "%s: unsupported mirror flags: %s\n",
3774 case LFS_LAYOUT_FOREIGN_OPT:
3776 /* check pure numeric */
3777 type = strtoul(optarg, &end, 0);
3780 type = check_foreign_type_name(optarg);
3781 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3783 "%s %s: unrecognized foreign type '%s'\n",
3788 } else if (type >= UINT32_MAX) {
3790 "%s %s: invalid foreign type '%s'\n",
3791 progname, argv[0], optarg);
3795 foreign_mode = true;
3800 mode = strtoul(mode_opt, &end, 8);
3803 "%s %s: bad mode '%s'\n",
3804 progname, argv[0], mode_opt);
3807 previous_umask = umask(0);
3810 case LFS_LAYOUT_COPY:
3815 stats_interval_sec = 5;
3817 case LFS_STATS_INTERVAL_OPT:
3818 stats_interval_sec = strtol(optarg, &end, 0);
3819 if (stats_interval_sec == 0 && errno) {
3821 "%s %s: invalid stats interval %s\n",
3822 progname, argv[0], optarg);
3827 if (!migrate_mode) {
3829 "%s %s: -b|--block valid only for migrate command\n",
3833 migration_block = true;
3836 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3838 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3845 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3849 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3850 if (errno != 0 || *end != '\0'|| optarg == end ||
3851 lsa.lsa_stripe_count < -1 ||
3852 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3854 "%s %s: invalid stripe count '%s'\n",
3855 progname, argv[0], optarg);
3859 if (lsa.lsa_stripe_count == -1)
3860 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3864 migrate_mdt_param.fp_max_depth = 1;
3866 /* delete the default striping pattern */
3868 if (opc == SO_MIRROR_SPLIT) {
3871 "%s %s: -d cannot used with -f\n",
3875 mirror_flags |= MF_DESTROY;
3880 if (!migrate_mode) {
3882 "%s %s: -D|--non-direct is valid only for migrate command\n",
3886 migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3889 if (lsa.lsa_comp_end != 0) {
3890 result = comp_args_to_layout(lpp, &lsa, true);
3892 fprintf(stderr, "%s: invalid layout\n",
3897 setstripe_args_init_inherit(&lsa);
3900 if (arg_is_eof(optarg)) {
3901 lsa.lsa_comp_end = LUSTRE_EOF;
3903 result = llapi_parse_size(optarg,
3906 /* assume units of KB if too small */
3907 if (lsa.lsa_comp_end < 4096)
3908 lsa.lsa_comp_end *= 1024;
3910 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3912 "%s %s: invalid component end '%s'\n",
3913 progname, argv[0], optarg);
3919 if (!migrate_mode) {
3921 "--mdt-hash is valid only for migrate command\n");
3925 lsa.lsa_pattern = check_hashtype(optarg);
3926 if (lsa.lsa_pattern == 0) {
3928 "%s %s: bad stripe hash type '%s'\n",
3929 progname, argv[0], optarg);
3935 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3936 if (errno != 0 || *end != '\0' || optarg == end ||
3937 lsa.lsa_stripe_off < -1 ||
3938 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3940 "%s %s: invalid stripe offset '%s'\n",
3941 progname, argv[0], optarg);
3944 if (lsa.lsa_stripe_off == -1)
3945 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3948 comp_id = strtoul(optarg, &end, 0);
3949 if (*end != '\0' || comp_id == 0 ||
3950 comp_id > LCME_ID_MAX) {
3952 "%s %s: invalid component ID '%s'\n",
3953 progname, argv[0], optarg);
3958 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3960 "error: %s: invalid option: %s\n",
3961 progname, argv[optopt + 1]);
3964 if (opc == SO_MIRROR_EXTEND) {
3967 "error: %s: '-N' must exist in front of '%s'\n",
3968 progname, argv[optopt + 1]);
3971 last_mirror->m_file = optarg;
3972 last_mirror->m_count = 1;
3976 mirror_list = lfs_mirror_alloc();
3977 mirror_list->m_file = optarg;
3982 if (strcmp(argv[optind - 1], "mdt") == 0) {
3983 /* Can be only the first component */
3987 "error: 'mdt' layout can be only the first one\n");
3990 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3993 "error: 'mdt' layout size is too big\n");
3996 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3997 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3998 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
4001 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
4007 if (!migrate_mode) {
4009 "%s %s: -m|--mdt-index is valid only for migrate command\n",
4013 migrate_mdt_mode = true;
4014 lsa.lsa_nr_tgts = parse_targets(tgts,
4015 sizeof(tgts) / sizeof(__u32),
4016 lsa.lsa_nr_tgts, optarg,
4018 if (lsa.lsa_nr_tgts < 0) {
4020 "%s: invalid MDT target(s) '%s'\n",
4025 lsa.lsa_tgts = tgts;
4026 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4027 lsa.lsa_stripe_off = tgts[0];
4030 if (!migrate_mode) {
4032 "%s %s: -n|--non-block valid only for migrate command\n",
4036 migration_flags |= LLAPI_MIGRATION_NONBLOCK;
4039 if (opc == SO_SETSTRIPE) {
4040 opc = SO_MIRROR_CREATE;
4046 mirror_count = strtoul(optarg, &end, 0);
4047 if (errno != 0 || *end != '\0' ||
4048 mirror_count == 0 ||
4049 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
4051 "error: %s: bad mirror count: %s\n",
4058 new_mirror = lfs_mirror_alloc();
4059 new_mirror->m_count = mirror_count;
4062 mirror_list = new_mirror;
4065 /* wrap up last mirror */
4066 if (!setstripe_args_specified(&lsa))
4067 last_mirror->m_inherit = true;
4068 if (lsa.lsa_comp_end == 0)
4069 lsa.lsa_comp_end = LUSTRE_EOF;
4071 result = comp_args_to_layout(lpp, &lsa, true);
4073 lfs_mirror_free(new_mirror);
4077 setstripe_args_init_inherit(&lsa);
4079 last_mirror->m_next = new_mirror;
4082 last_mirror = new_mirror;
4083 lpp = &last_mirror->m_layout;
4086 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4087 if (strcmp(argv[optind - 1], "--ost-list") == 0)
4089 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
4091 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
4093 "%s %s: -o|--ost incompatible with DoM layout\n",
4098 * -o allows overstriping, and must note it because
4099 * parse_targets is shared with MDT striping, which
4100 * does not allow duplicates
4102 lsa.lsa_nr_tgts = parse_targets(tgts,
4103 sizeof(tgts) / sizeof(__u32),
4104 lsa.lsa_nr_tgts, optarg,
4106 if (lsa.lsa_nr_tgts < 0) {
4108 "%s %s: invalid OST target(s) '%s'\n",
4109 progname, argv[0], optarg);
4114 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
4116 lsa.lsa_tgts = tgts;
4117 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4118 lsa.lsa_stripe_off = tgts[0];
4124 if (optarg[0] == '\0' || lov_pool_is_inherited(optarg))
4125 lsa.lsa_pool_name = NULL;
4127 lsa.lsa_pool_name = optarg;
4130 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
4132 /* assume units of KB if too small to be valid */
4133 if (lsa.lsa_stripe_size < 4096)
4134 lsa.lsa_stripe_size *= 1024;
4136 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
4138 "%s %s: invalid stripe size '%s'\n",
4139 progname, argv[0], optarg);
4144 if (!migrate_mode) {
4146 "%s %s: -v|--verbose valid only for migrate command\n",
4150 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
4151 migration_flags = LLAPI_MIGRATION_VERBOSE;
4157 if (!migrate_mode && !mirror_mode) {
4159 "--bandwidth is valid only for migrate and mirror mode\n");
4162 if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
4163 &bandwidth_unit, 0) < 0) {
4165 "error: %s: bad value for bandwidth '%s'\n",
4175 result = llapi_parse_size(optarg,
4176 &lsa.lsa_extension_size,
4180 "%s %s: invalid extension size '%s'\n",
4181 progname, argv[0], optarg);
4185 lsa.lsa_extension_comp = true;
4188 fprintf(stderr, "%s: unrecognized option '%s'\n",
4189 progname, argv[optind - 1]);
4195 fname = argv[optind];
4197 if (optind == argc) {
4198 fprintf(stderr, "%s %s: FILE must be specified\n",
4203 /* lfs migrate $filename should keep the file's layout by default */
4204 if (migrate_mode && !layout && !from_yaml &&
4205 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4208 if (xattr && !foreign_mode) {
4210 * only print a warning as this is harmless and will be ignored
4213 "%s %s: xattr has been specified for non-foreign layout\n",
4215 } else if (foreign_mode && !xattr) {
4217 "%s %s: xattr must be provided in foreign mode\n",
4222 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4223 comp_set || comp_id || delete || from_copy ||
4224 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4227 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4232 if (mirror_mode && mirror_count == 0) {
4234 "error: %s: --mirror-count|-N option is required\n",
4241 if (!setstripe_args_specified(&lsa))
4242 last_mirror->m_inherit = true;
4243 if (lsa.lsa_comp_end == 0)
4244 lsa.lsa_comp_end = LUSTRE_EOF;
4247 if (lsa.lsa_comp_end != 0) {
4248 result = comp_args_to_layout(lpp, &lsa, true);
4250 fprintf(stderr, "error: %s: invalid layout\n",
4257 if (mirror_flags & MF_NO_VERIFY) {
4258 if (opc != SO_MIRROR_EXTEND) {
4260 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4264 } else if (!has_m_file) {
4266 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4273 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4275 "%s %s: --component-set doesn't have component-id set\n",
4280 if ((delete + comp_set + comp_del + comp_add) > 1) {
4282 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4287 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4288 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4290 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4295 if ((comp_set || comp_del) &&
4296 (setstripe_args_specified(&lsa) || layout != NULL)) {
4298 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4303 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4305 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4310 if (comp_add || comp_del) {
4313 result = lstat(fname, &st);
4314 if (result == 0 && S_ISDIR(st.st_mode)) {
4316 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4323 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4332 "%s %s: option -E must be specified with --component-add\n",
4338 if (from_yaml && from_copy) {
4340 "%s: can't specify --yaml and --copy together\n",
4345 if ((from_yaml || from_copy) &&
4346 (setstripe_args_specified(&lsa) || layout != NULL)) {
4348 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4353 if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4355 "%s %s: options --non-block and --block are mutually exclusive\n",
4360 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4361 opc != SO_MIRROR_DELETE && comp_id != 0) {
4363 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4368 if (migrate_mdt_mode) {
4369 struct lmv_user_md *lmu;
4371 /* initialize migrate mdt parameters */
4372 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4373 LMV_USER_MAGIC_SPECIFIC));
4376 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4377 progname, argv[0], strerror(ENOMEM));
4381 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4382 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4383 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4385 "%s %s: migrate should specify MDT index\n",
4390 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4392 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4393 lmu->lum_hash_type = lsa.lsa_pattern;
4395 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4398 lmu->lum_hash_type |= LMV_HASH_FLAG_OVERSTRIPED;
4400 if (lsa.lsa_pool_name) {
4401 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4402 sizeof(lmu->lum_pool_name) - 1);
4403 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4405 if (lsa.lsa_nr_tgts > 1) {
4408 if (lsa.lsa_stripe_count > 0 &&
4409 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4410 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4412 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4413 progname, lsa.lsa_stripe_count,
4419 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4420 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4421 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4422 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4424 lmu->lum_magic = LMV_USER_MAGIC;
4427 migrate_mdt_param.fp_lmv_md = lmu;
4428 migrate_mdt_param.fp_migrate = 1;
4429 } else if (!layout) {
4430 if (lsa_args_stripe_count_check(&lsa))
4433 /* initialize stripe parameters */
4434 param = calloc(1, offsetof(typeof(*param),
4435 lsp_osts[lsa.lsa_nr_tgts]));
4438 "%s %s: cannot allocate memory for parameters: %s\n",
4439 progname, argv[0], strerror(ENOMEM));
4444 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4445 param->lsp_stripe_size = lsa.lsa_stripe_size;
4446 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4447 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4448 param->lsp_stripe_count = -1;
4450 param->lsp_stripe_count = lsa.lsa_stripe_count;
4452 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4453 param->lsp_stripe_offset = -1;
4455 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4456 param->lsp_stripe_pattern =
4457 llapi_pattern_to_lov(lsa.lsa_pattern);
4458 if (param->lsp_stripe_pattern == EINVAL) {
4459 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4464 param->lsp_pool = lsa.lsa_pool_name;
4465 param->lsp_is_specific = false;
4467 if (lsa.lsa_nr_tgts > 0) {
4468 param->lsp_is_specific = true;
4469 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4470 memcpy(param->lsp_osts, tgts,
4471 sizeof(*tgts) * lsa.lsa_nr_tgts);
4476 /* generate a layout from a YAML template */
4477 result = lfs_comp_create_from_yaml(template, &layout,
4481 "error: %s: can't create composite layout from template file %s\n",
4487 if (layout != NULL || mirror_list != NULL) {
4489 result = mirror_adjust_first_extents(mirror_list);
4491 result = layout_adjust_first_extent(fname, layout,
4493 if (result == -ENODATA)
4495 else if (result != 0) {
4496 fprintf(stderr, "error: %s: invalid layout\n",
4502 for (fname = argv[optind]; (optind < argc) && (fname != NULL);
4503 fname = argv[++optind]) {
4505 layout = llapi_layout_get_by_path(template ?: fname, 0);
4508 "%s: can't create composite layout from file %s: %s\n",
4509 progname, template ?: fname,
4516 if (migrate_mdt_mode) {
4517 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4518 } else if (migrate_mode) {
4521 * Strip the source layout of specific
4522 * OST object/index values.
4524 result = llapi_layout_ost_index_reset(layout);
4527 "%s: set default ost index failed: %s\n",
4528 progname, strerror(errno));
4534 result = lfs_migrate(fname, migration_flags, param,
4535 layout, bandwidth_bytes_sec,
4536 stats_interval_sec);
4537 } else if (comp_set != 0) {
4538 result = lfs_component_set(fname, comp_id,
4541 lsa.lsa_comp_neg_flags);
4542 } else if (comp_del != 0) {
4543 result = lfs_component_del(fname, comp_id,
4545 lsa.lsa_comp_neg_flags);
4546 } else if (comp_add != 0) {
4547 result = lfs_component_add(fname, layout);
4548 } else if (opc == SO_MIRROR_CREATE) {
4549 result = mirror_create(fname, mirror_list);
4550 } else if (opc == SO_MIRROR_EXTEND) {
4551 result = mirror_extend(fname, mirror_list,
4553 bandwidth_bytes_sec,
4554 stats_interval_sec);
4555 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4556 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4558 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4562 if (lsa.lsa_pool_name)
4563 mirror_flags |= MF_COMP_POOL;
4564 else if (mirror_id != 0)
4565 comp_id = mirror_id;
4567 mirror_flags |= MF_COMP_ID;
4568 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4570 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4574 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4576 has_m_file ? mirror_list->m_file :
4578 } else if (layout) {
4579 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4585 } else if (foreign_mode) {
4586 result = llapi_file_create_foreign(fname, mode, type,
4593 result = llapi_file_open_param(fname,
4602 /* Save the first error encountered. */
4610 umask(previous_umask);
4613 free(migrate_mdt_param.fp_lmv_md);
4614 llapi_layout_free(layout);
4615 lfs_mirror_list_free(mirror_list);
4620 llapi_layout_free(layout);
4621 lfs_mirror_list_free(mirror_list);
4625 static int lfs_poollist(int argc, char **argv)
4630 return llapi_poollist(argv[1]);
4633 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4634 static int set_time(struct find_param *param, time_t *time, time_t *set,
4639 char *endptr = "AD";
4644 else if (str[0] == '-')
4650 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4651 long long val = strtoll(timebuf, &endptr, 0);
4656 unit *= 52; /* 52 weeks + 1 day below */
4660 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4661 param->fp_time_margin *= (1 + unit / 52);
4662 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4664 case '\0': /* days are default unit if none used */
4677 /* don't need to multiply by 1 for seconds */
4680 "%s find: bad time string '%s': %s\n",
4681 progname, timebuf, strerror(EINVAL));
4685 if (param->fp_time_margin == 0 ||
4686 (*endptr && unit < param->fp_time_margin))
4687 param->fp_time_margin = unit;
4694 fprintf(stderr, "%s find: bad time '%s': too large\n",
4704 static int str2quotaid(__u32 *id, const char *arg)
4706 unsigned long int projid_tmp = 0;
4707 char *endptr = NULL;
4709 projid_tmp = strtoul(arg, &endptr, 10);
4710 if (*endptr != '\0')
4712 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4713 if (projid_tmp >= UINT32_MAX)
4720 static int name2uid(unsigned int *id, const char *name)
4722 struct passwd *passwd;
4724 passwd = getpwnam(name);
4727 *id = passwd->pw_uid;
4732 static int name2gid(unsigned int *id, const char *name)
4734 struct group *group;
4736 group = getgrnam(name);
4739 *id = group->gr_gid;
4744 static inline int name2projid(unsigned int *id, const char *name)
4749 static int uid2name(char **name, unsigned int id)
4751 struct passwd *passwd;
4753 passwd = getpwuid(id);
4756 *name = passwd->pw_name;
4761 static inline int gid2name(char **name, unsigned int id)
4763 struct group *group;
4765 group = getgrgid(id);
4768 *name = group->gr_name;
4773 static int name2layout(__u32 *layout, char *name)
4775 char *ptr, *layout_name;
4778 for (ptr = name; ; ptr = NULL) {
4779 layout_name = strtok(ptr, ",");
4782 if (strcmp(layout_name, "released") == 0)
4783 *layout |= LOV_PATTERN_F_RELEASED;
4784 else if (strcmp(layout_name, "raid0") == 0)
4785 *layout |= LOV_PATTERN_RAID0;
4786 else if (strcmp(layout_name, "mdt") == 0)
4787 *layout |= LOV_PATTERN_MDT;
4788 else if (strcmp(layout_name, "overstriping") == 0)
4789 *layout |= LOV_PATTERN_OVERSTRIPING;
4790 else if (strcmp(layout_name, "foreign") == 0)
4791 *layout |= LOV_PATTERN_FOREIGN;
4798 static int name2attrs(char *name, __u64 *attrs, __u64 *neg_attrs)
4800 char *ptr, *attr_name = name;
4801 struct attrs_name *ap;
4802 int islongopt = 0; /* 1 true; 0 not known yet; -1 false. */
4807 if (strchr(name, ','))
4810 for (ptr = name; ; ptr = NULL) {
4811 if (islongopt != -1)
4812 attr_name = strtok(ptr, ",");
4814 attr_name = attr_name + 1;
4815 if (!attr_name || *attr_name == '\0')
4818 for (ap = (struct attrs_name *)attrs_array;
4821 if (islongopt != -1 &&
4822 strcmp(attr_name, ap->an_name) == 0) {
4823 *attrs |= ap->an_attr;
4826 } else if (islongopt != -1 && attr_name[0] == '^' &&
4827 strcmp(attr_name + 1, ap->an_name) == 0) {
4828 *neg_attrs |= ap->an_attr;
4831 } else if (islongopt != 1 &&
4832 *attr_name == ap->an_shortname) {
4833 *attrs |= ap->an_attr;
4836 } else if (islongopt != 1 && *attr_name == '^' &&
4837 attr_name[1] == ap->an_shortname) {
4838 *neg_attrs |= ap->an_attr;
4845 if (ap->an_attr == 0) {
4846 /* provided attr is unknown */
4847 fprintf(stderr, "error: bad attribute name '%s'\n",
4856 * xattr_match_info_append() - add the supplied name and value regex patterns
4857 * to the supplied xattr_match_info struct.
4859 * Return: 0 for success, nonzero if any errors encountered.
4861 int xattr_match_info_append(struct xattr_match_info *xmi, bool exclude,
4862 char *name_pattern, char *value_pattern)
4864 int flags = REG_EXTENDED;
4871 if (xmi->xattr_name_buf == NULL) {
4872 xmi->xattr_name_buf = malloc(XATTR_LIST_MAX);
4873 if (xmi->xattr_name_buf == NULL)
4877 if (xmi->xattr_value_buf == NULL) {
4879 * an xattr value need not be null-terminated, so allocate an
4880 * extra byte to append a '\0', since regexec() expects a null-
4881 * terminated string.
4883 xmi->xattr_value_buf = malloc(XATTR_SIZE_MAX + 1);
4884 if (xmi->xattr_value_buf == NULL)
4888 n = ++xmi->xattr_regex_count;
4890 nptr = realloc(xmi->xattr_regex_matched, n * sizeof(bool));
4893 xmi->xattr_regex_matched = nptr;
4895 nptr = realloc(xmi->xattr_regex_exclude, n * sizeof(bool));
4898 xmi->xattr_regex_exclude = nptr;
4900 nptr = realloc(xmi->xattr_regex_name, n * sizeof(regex_t *));
4903 xmi->xattr_regex_name = nptr;
4905 nptr = realloc(xmi->xattr_regex_value, n * sizeof(regex_t *));
4908 xmi->xattr_regex_value = nptr;
4912 xmi->xattr_regex_exclude[n] = exclude;
4914 xmi->xattr_regex_name[n] = malloc(sizeof(regex_t));
4915 if (xmi->xattr_regex_name[n] == NULL)
4918 ret = regcomp(xmi->xattr_regex_name[n], name_pattern, flags);
4920 err_len = regerror(ret, xmi->xattr_regex_name[n], NULL, 0);
4921 err_buf = malloc(err_len);
4922 if (err_buf == NULL)
4925 regerror(ret, xmi->xattr_regex_name[n], err_buf, err_len);
4926 fprintf(stderr, "%s: %s: %s\n",
4927 progname, name_pattern, err_buf);
4932 if (value_pattern && value_pattern[0] != '\0') {
4933 xmi->xattr_regex_value[n] = malloc(sizeof(regex_t));
4934 ret = regcomp(xmi->xattr_regex_value[n], value_pattern, flags);
4936 err_len = regerror(ret, xmi->xattr_regex_value[n],
4938 err_buf = malloc(err_len);
4939 if (err_buf == NULL)
4942 regerror(ret, xmi->xattr_regex_value[n], err_buf,
4944 fprintf(stderr, "%s: %s: %s\n",
4945 progname, value_pattern, err_buf);
4950 xmi->xattr_regex_value[n] = NULL;
4956 fprintf(stderr, "%s: %s\n", progname, strerror(ENOMEM));
4960 void xattr_match_info_free(struct xattr_match_info *xmi)
4964 free(xmi->xattr_regex_exclude);
4965 xmi->xattr_regex_exclude = NULL;
4967 free(xmi->xattr_regex_matched);
4968 xmi->xattr_regex_matched = NULL;
4970 for (i = 0; i < xmi->xattr_regex_count; i++) {
4971 if (xmi->xattr_regex_name[i]) {
4972 regfree(xmi->xattr_regex_name[i]);
4973 free(xmi->xattr_regex_name[i]);
4976 if (xmi->xattr_regex_value[i]) {
4977 regfree(xmi->xattr_regex_value[i]);
4978 free(xmi->xattr_regex_value[i]);
4982 xmi->xattr_regex_count = 0;
4984 free(xmi->xattr_regex_name);
4985 xmi->xattr_regex_name = NULL;
4987 free(xmi->xattr_regex_value);
4988 xmi->xattr_regex_value = NULL;
4990 free(xmi->xattr_name_buf);
4991 xmi->xattr_name_buf = NULL;
4993 free(xmi->xattr_value_buf);
4994 xmi->xattr_value_buf = NULL;
4998 * compile_xattr_match_regex() - Compile regexes for matching xattr names and
4999 * values, returning an error if either fails to compile.
5001 * The argument should be in the form "NAME=VALUE". The first '=' found
5002 * is assumed to be the separator between the name regex and the value regex.
5004 * VALUE may be empty. If it is empty, it is not compiled and left NULL.
5005 * NAME must not be empty.
5007 * Return: 0 if argument string is succesfully processed, nonzero if any
5008 * errors encountered.
5010 static int compile_xattr_match_regex(char *optarg, bool exclude,
5011 struct find_param *param)
5015 sep = strchr(optarg, '=');
5019 /* error if no NAME pattern specified */
5020 if (*optarg == '\0') {
5021 fprintf(stderr, "%s: must specify xattr pattern\n", progname);
5025 /* if first -xattr option seen */
5026 if (param->fp_xattr_match_info == NULL) {
5027 param->fp_xattr_match_info = calloc(1,
5028 sizeof(struct xattr_match_info));
5029 if (param->fp_xattr_match_info == NULL) {
5030 fprintf(stderr, "%s: %s\n", progname, strerror(ENOMEM));
5036 * if '=' was not provided, or if there is no value after the '=',
5037 * then pass NULL to xattr_match_info_append() so that no VALUE regex
5046 return xattr_match_info_append(param->fp_xattr_match_info, exclude,
5050 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
5053 int user, group, other;
5058 mode_t previous_flags;
5060 user = group = other = 0;
5080 user = group = other = 1;
5091 who = user || group || other;
5094 usermask = umask(0022);
5099 if (*input == '-' || *input == '+' || *input == '=')
5102 /* operation is required */
5105 /* get the flags in *outmode */
5108 previous_flags = (*outmode & 0700);
5109 perm |= user ? previous_flags : 0;
5110 perm |= group ? (previous_flags >> 3) : 0;
5111 perm |= other ? (previous_flags >> 6) : 0;
5115 previous_flags = (*outmode & 0070);
5116 perm |= user ? (previous_flags << 3) : 0;
5117 perm |= group ? previous_flags : 0;
5118 perm |= other ? (previous_flags >> 3) : 0;
5122 previous_flags = (*outmode & 0007);
5123 perm |= user ? (previous_flags << 6) : 0;
5124 perm |= group ? (previous_flags << 3) : 0;
5125 perm |= other ? previous_flags : 0;
5132 /* this part is optional,
5133 * if empty perm = 0 and *outmode is not modified
5140 perm |= user ? 0400 : 0;
5141 perm |= group ? 0040 : 0;
5142 perm |= other ? 0004 : 0;
5143 /* set read permission for uog except for umask's
5146 perm |= who ? 0 : (0444 & ~usermask);
5149 perm |= user ? 0200 : 0;
5150 perm |= group ? 0020 : 0;
5151 perm |= other ? 0002 : 0;
5152 /* set write permission for uog except for umask'
5155 perm |= who ? 0 : (0222 & ~usermask);
5158 perm |= user ? 0100 : 0;
5159 perm |= group ? 0010 : 0;
5160 perm |= other ? 0001 : 0;
5161 /* set execute permission for uog except for umask'
5164 perm |= who ? 0 : (0111 & ~usermask);
5168 * Adds execute permission to 'u', 'g' and/or 'g' if
5169 * specified and either 'u', 'g' or 'o' already has
5170 * execute permissions.
5172 if ((*outmode & 0111) != 0) {
5173 perm |= user ? 0100 : 0;
5174 perm |= group ? 0010 : 0;
5175 perm |= other ? 0001 : 0;
5176 perm |= !who ? 0111 : 0;
5180 /* s is ignored if o is given, but it's not an error */
5181 if (other && !group && !user)
5183 perm |= user ? S_ISUID : 0;
5184 perm |= group ? S_ISGID : 0;
5187 /* 't' should be used when 'a' is given
5190 perm |= (!who || all) ? S_ISVTX : 0;
5191 /* using ugo with t is not an error */
5202 /* uog flags should be only one character long */
5203 if (previous_flags && (*input != '\0' && *input != ','))
5208 /* remove the flags from outmode */
5212 /* add the flags to outmode */
5216 /* set the flags of outmode to perm */
5226 static int str2mode_t(const char *input, mode_t *outmode)
5233 if (*input >= '0' && *input <= '7') {
5234 /* parse octal representation */
5239 /* look for invalid digits in octal representation */
5240 while (isdigit(*iter))
5245 *outmode = strtoul(input, &end, 8);
5247 if (errno != 0 || *outmode > 07777) {
5252 } else if (*input == '8' || *input == '9') {
5253 /* error: invalid octal number */
5256 /* parse coma seperated list of symbolic representation */
5265 rc = parse_symbolic(input, outmode, &end);
5270 } while (*end == ',');
5278 static int lfs_find(int argc, char **argv)
5283 struct find_param param = {
5286 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
5288 struct option long_opts[] = {
5289 { .val = 'A', .name = "atime", .has_arg = required_argument },
5290 { .val = LFS_ATTRS_OPT,
5291 .name = "attrs", .has_arg = required_argument },
5292 { .val = 'b', .name = "blocks", .has_arg = required_argument },
5293 { .val = 'B', .name = "btime", .has_arg = required_argument },
5294 { .val = 'B', .name = "Btime", .has_arg = required_argument },
5295 { .val = LFS_COMP_COUNT_OPT,
5296 .name = "comp-count", .has_arg = required_argument },
5297 { .val = LFS_COMP_COUNT_OPT,
5298 .name = "component-count",
5299 .has_arg = required_argument },
5300 { .val = LFS_COMP_FLAGS_OPT,
5301 .name = "comp-flags", .has_arg = required_argument },
5302 { .val = LFS_COMP_FLAGS_OPT,
5303 .name = "component-flags",
5304 .has_arg = required_argument },
5305 { .val = LFS_COMP_START_OPT,
5306 .name = "comp-start", .has_arg = required_argument },
5307 { .val = LFS_COMP_START_OPT,
5308 .name = "component-start",
5309 .has_arg = required_argument },
5310 { .val = LFS_MIRROR_STATE_OPT,
5311 .name = "mirror-state", .has_arg = required_argument },
5312 { .val = LFS_NEWERXY_OPT,
5313 .name = "newer", .has_arg = required_argument},
5314 { .val = LFS_NEWERXY_OPT,
5315 .name = "neweraa", .has_arg = required_argument},
5316 { .val = LFS_NEWERXY_OPT,
5317 .name = "neweram", .has_arg = required_argument},
5318 { .val = LFS_NEWERXY_OPT,
5319 .name = "newerac", .has_arg = required_argument},
5320 { .val = LFS_NEWERXY_OPT,
5321 .name = "newerab", .has_arg = required_argument},
5322 { .val = LFS_NEWERXY_OPT,
5323 .name = "newerma", .has_arg = required_argument},
5324 { .val = LFS_NEWERXY_OPT,
5325 .name = "newermm", .has_arg = required_argument},
5326 { .val = LFS_NEWERXY_OPT,
5327 .name = "newermc", .has_arg = required_argument},
5328 { .val = LFS_NEWERXY_OPT,
5329 .name = "newermb", .has_arg = required_argument},
5330 { .val = LFS_NEWERXY_OPT,
5331 .name = "newerca", .has_arg = required_argument},
5332 { .val = LFS_NEWERXY_OPT,
5333 .name = "newercm", .has_arg = required_argument},
5334 { .val = LFS_NEWERXY_OPT,
5335 .name = "newercc", .has_arg = required_argument},
5336 { .val = LFS_NEWERXY_OPT,
5337 .name = "newercb", .has_arg = required_argument},
5338 { .val = LFS_NEWERXY_OPT,
5339 .name = "newerba", .has_arg = required_argument},
5340 { .val = LFS_NEWERXY_OPT,
5341 .name = "newerbm", .has_arg = required_argument},
5342 { .val = LFS_NEWERXY_OPT,
5343 .name = "newerbc", .has_arg = required_argument},
5344 { .val = LFS_NEWERXY_OPT,
5345 .name = "newerbb", .has_arg = required_argument},
5346 { .val = LFS_NEWERXY_OPT,
5347 .name = "newerBa", .has_arg = required_argument},
5348 { .val = LFS_NEWERXY_OPT,
5349 .name = "newerBm", .has_arg = required_argument},
5350 { .val = LFS_NEWERXY_OPT,
5351 .name = "newerBc", .has_arg = required_argument},
5352 { .val = LFS_NEWERXY_OPT,
5353 .name = "newerBB", .has_arg = required_argument},
5354 { .val = LFS_NEWERXY_OPT,
5355 .name = "newerat", .has_arg = required_argument},
5356 { .val = LFS_NEWERXY_OPT,
5357 .name = "newermt", .has_arg = required_argument},
5358 { .val = LFS_NEWERXY_OPT,
5359 .name = "newerct", .has_arg = required_argument},
5360 { .val = LFS_NEWERXY_OPT,
5361 .name = "newerbt", .has_arg = required_argument},
5362 { .val = LFS_NEWERXY_OPT,
5363 .name = "newerBt", .has_arg = required_argument},
5364 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
5365 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
5366 { .val = 'C', .name = "ctime", .has_arg = required_argument },
5367 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
5368 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
5369 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
5370 { .val = 'E', .name = "component-end",
5371 .has_arg = required_argument },
5372 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
5373 { .val = LFS_LAYOUT_FOREIGN_OPT,
5374 .name = "foreign", .has_arg = optional_argument},
5375 { .val = 'g', .name = "gid", .has_arg = required_argument },
5376 { .val = 'G', .name = "group", .has_arg = required_argument },
5377 { .val = 'h', .name = "help", .has_arg = no_argument },
5378 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5379 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
5380 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
5381 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
5382 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5383 { .val = 'L', .name = "layout", .has_arg = required_argument },
5384 { .val = LFS_LINKS_OPT,
5385 .name = "links", .has_arg = required_argument },
5386 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5387 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5388 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
5389 { .val = 'M', .name = "mtime", .has_arg = required_argument },
5390 { .val = 'n', .name = "name", .has_arg = required_argument },
5391 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
5392 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
5393 { .val = 'O', .name = "obd", .has_arg = required_argument },
5394 { .val = 'O', .name = "ost", .has_arg = required_argument },
5395 { .val = LFS_FIND_PERM,
5396 .name = "perm", .has_arg = required_argument },
5397 /* no short option for pool yet, can be 'p' after 2.18 */
5398 { .val = LFS_POOL_OPT,
5399 .name = "pool", .has_arg = required_argument },
5400 { .val = '0', .name = "print0", .has_arg = no_argument },
5401 { .val = 'P', .name = "print", .has_arg = no_argument },
5402 { .val = LFS_PRINTF_OPT,
5403 .name = "printf", .has_arg = required_argument },
5404 { .val = LFS_PROJID_OPT,
5405 .name = "projid", .has_arg = required_argument },
5406 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
5407 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
5408 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
5409 { .val = 's', .name = "size", .has_arg = required_argument },
5410 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
5411 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
5412 { .val = 't', .name = "type", .has_arg = required_argument },
5413 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5414 { .val = 'u', .name = "uid", .has_arg = required_argument },
5415 { .val = 'U', .name = "user", .has_arg = required_argument },
5416 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
5417 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
5418 { .val = LFS_XATTRS_MATCH_OPT,
5419 .name = "xattr", .has_arg = required_argument },
5420 { .val = 'z', .name = "extension-size",
5421 .has_arg = required_argument },
5422 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
5424 int prev_optind = optind;
5436 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
5437 while ((c = getopt_long_only(argc, argv,
5438 "-0A:b:B:c:C:D:E:g:G:hH:i:lL:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
5439 long_opts, &optidx)) >= 0) {
5444 /* '!' is part of option */
5446 * when getopt_long_only() finds a string which is not
5447 * an option nor a known option argument it returns 1
5448 * in that case if we already have found pathstart and pathend
5449 * (i.e. we have the list of pathnames),
5450 * the only supported value is "!"
5452 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5453 if (!isoption && pathend != -1) {
5455 "err: %s: filename|dirname must either precede options or follow options\n",
5460 if (!isoption && pathstart == -1)
5461 pathstart = prev_optind;
5462 if (isoption && pathstart != -1 && pathend == -1)
5463 pathend = prev_optind;
5465 prev_optind = optind;
5473 * unknown; opt is "!" or path component,
5474 * checking done above.
5476 if (strcmp(optarg, "!") == 0)
5480 xtime = ¶m.fp_atime;
5481 xsign = ¶m.fp_asign;
5482 param.fp_exclude_atime = !!neg_opt;
5483 /* no break, this falls through to 'B' for btime */
5487 xtime = ¶m.fp_btime;
5488 xsign = ¶m.fp_bsign;
5489 param.fp_exclude_btime = !!neg_opt;
5491 /* no break, this falls through to 'C' for ctime */
5495 xtime = ¶m.fp_ctime;
5496 xsign = ¶m.fp_csign;
5497 param.fp_exclude_ctime = !!neg_opt;
5499 /* no break, this falls through to 'M' for mtime */
5503 xtime = ¶m.fp_mtime;
5504 xsign = ¶m.fp_msign;
5505 param.fp_exclude_mtime = !!neg_opt;
5507 rc = set_time(¶m, &t, xtime, optarg);
5508 if (rc == INT_MAX) {
5516 ret = name2attrs(optarg, ¶m.fp_attrs,
5517 ¶m.fp_neg_attrs);
5520 param.fp_exclude_attrs = !!neg_opt;
5523 if (optarg[0] == '+') {
5524 param.fp_blocks_sign = -1;
5526 } else if (optarg[0] == '-') {
5527 param.fp_blocks_sign = 1;
5531 param.fp_blocks_units = 512;
5532 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5533 ¶m.fp_blocks_units, 0);
5535 fprintf(stderr, "error: bad blocks '%s'\n",
5539 param.fp_check_blocks = 1;
5540 param.fp_exclude_blocks = !!neg_opt;
5542 case LFS_COMP_COUNT_OPT:
5543 if (optarg[0] == '+') {
5544 param.fp_comp_count_sign = -1;
5546 } else if (optarg[0] == '-') {
5547 param.fp_comp_count_sign = 1;
5552 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5553 if (errno != 0 || *endptr != '\0' ||
5554 param.fp_comp_count > UINT32_MAX) {
5556 "error: bad component count '%s'\n",
5560 param.fp_check_comp_count = 1;
5561 param.fp_exclude_comp_count = !!neg_opt;
5563 case LFS_COMP_FLAGS_OPT:
5564 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5565 ¶m.fp_comp_neg_flags);
5568 "error: bad component flags '%s'\n",
5572 param.fp_check_comp_flags = 1;
5574 __u32 flags = param.fp_comp_neg_flags;
5576 param.fp_comp_neg_flags = param.fp_comp_flags;
5577 param.fp_comp_flags = flags;
5580 case LFS_COMP_START_OPT:
5581 if (optarg[0] == '+') {
5582 param.fp_comp_start_sign = -1;
5584 } else if (optarg[0] == '-') {
5585 param.fp_comp_start_sign = 1;
5589 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5590 ¶m.fp_comp_start_units, 0);
5593 "error: bad component start '%s'\n",
5597 param.fp_check_comp_start = 1;
5598 param.fp_exclude_comp_start = !!neg_opt;
5600 case LFS_MIRROR_STATE_OPT:
5601 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5602 ¶m.fp_mirror_neg_state);
5605 "error: bad mirrored file state '%s'\n",
5609 param.fp_check_mirror_state = 1;
5611 __u16 state = param.fp_mirror_neg_state;
5613 param.fp_mirror_neg_state =
5614 param.fp_mirror_state;
5615 param.fp_mirror_state = state;
5619 if (optarg[0] == '+') {
5620 param.fp_stripe_count_sign = -1;
5622 } else if (optarg[0] == '-') {
5623 param.fp_stripe_count_sign = 1;
5628 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5629 if (errno != 0 || *endptr != '\0' ||
5630 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5632 "error: bad stripe_count '%s'\n",
5637 param.fp_check_stripe_count = 1;
5638 param.fp_exclude_stripe_count = !!neg_opt;
5642 param.fp_max_depth = strtol(optarg, 0, 0);
5643 if (errno != 0 || param.fp_max_depth < 0) {
5645 "error: bad maxdepth '%s'\n",
5652 if (optarg[0] == '+') {
5653 param.fp_comp_end_sign = -1;
5655 } else if (optarg[0] == '-') {
5656 param.fp_comp_end_sign = 1;
5660 if (arg_is_eof(optarg)) {
5661 param.fp_comp_end = LUSTRE_EOF;
5662 param.fp_comp_end_units = 1;
5665 rc = llapi_parse_size(optarg,
5667 ¶m.fp_comp_end_units, 0);
5668 /* assume units of KB if too small */
5669 if (param.fp_comp_end < 4096)
5670 param.fp_comp_end *= 1024;
5674 "error: bad component end '%s'\n",
5678 param.fp_check_comp_end = 1;
5679 param.fp_exclude_comp_end = !!neg_opt;
5681 case LFS_LAYOUT_FOREIGN_OPT: {
5682 /* all types by default */
5683 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5686 /* check pure numeric */
5687 type = strtoul(optarg, &endptr, 0);
5690 type = check_foreign_type_name(optarg);
5691 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5693 "%s %s: unknown foreign type '%s'\n",
5698 } else if (type >= UINT32_MAX) {
5700 "%s %s: invalid foreign type '%s'\n",
5701 progname, argv[0], optarg);
5705 param.fp_foreign_type = type;
5706 param.fp_check_foreign = 1;
5707 param.fp_exclude_foreign = !!neg_opt;
5710 case LFS_NEWERXY_OPT: {
5716 time_t ref = time(NULL);
5718 /* no need to check bad options, they won't get here */
5719 if (strlen(long_opts[optidx].name) == 7) {
5720 x = long_opts[optidx].name[5];
5721 y = long_opts[optidx].name[6];
5725 static const char *const fmts[] = {
5726 "%Y-%m-%d %H:%M:%S",
5729 "%H:%M:%S", /* sometime today */
5738 for (i = 0; fmts[i] != NULL; i++) {
5741 /* Init for times relative to today */
5742 if (strncmp(fmts[i], "%H", 2) == 0) {
5743 localtime_r(&ref, &tm);
5745 memset(&tm, 0, sizeof(tm));
5748 ptr = strptime(optarg, fmts[i], &tm);
5750 while (ptr && isspace(*ptr))
5752 if (ptr == optarg + strlen(optarg)) {
5760 "%s: invalid time '%s'\n",
5763 "supported formats are:\n ");
5764 for (i = 0; fmts[i] != NULL; i++)
5765 fprintf(stderr, "'%s', ",
5767 fprintf(stderr, "\n");
5773 } else if (y == 'b' || y == 'B') {
5776 rc = llapi_get_lum_file(optarg, NULL, &stx,
5778 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5779 if (!(stx.stx_mask & STATX_BTIME))
5784 "%s: get btime failed '%s': %s\n",
5790 ref = stx.stx_btime.tv_sec;
5792 struct stat statbuf;
5794 if (stat(optarg, &statbuf) < 0) {
5796 "%s: cannot stat file '%s': %s\n",
5805 ref = statbuf.st_atime;
5808 ref = statbuf.st_mtime;
5811 ref = statbuf.st_ctime;
5815 "%s: invalid Y argument: '%c'\n",
5824 xidx = NEWERXY_ATIME;
5827 xidx = NEWERXY_MTIME;
5830 xidx = NEWERXY_CTIME;
5834 xidx = NEWERXY_BTIME;
5838 "%s: invalid X argument: '%c'\n",
5845 newery = ¶m.fp_newery[xidx][negidx];
5851 *newery = *newery > ref ? ref : *newery;
5853 *newery = *newery > ref ? *newery : ref;
5855 param.fp_newerxy = 1;
5860 rc = name2gid(¶m.fp_gid, optarg);
5862 if (str2quotaid(¶m.fp_gid, optarg)) {
5864 "Group/GID: %s cannot be found.\n",
5870 param.fp_exclude_gid = !!neg_opt;
5871 param.fp_check_gid = 1;
5874 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5875 ¶m.fp_hash_exflags,
5876 ¶m.fp_hash_type);
5881 if (param.fp_hash_inflags || param.fp_hash_exflags)
5882 param.fp_check_hash_flag = 1;
5883 param.fp_exclude_hash_type = !!neg_opt;
5889 ret = name2layout(¶m.fp_layout, optarg);
5892 param.fp_exclude_layout = !!neg_opt;
5893 param.fp_check_layout = 1;
5896 if (optarg[0] == '+') {
5897 param.fp_nlink_sign = -1;
5899 } else if (optarg[0] == '-') {
5900 param.fp_nlink_sign = 1;
5904 param.fp_nlink = strtoul(optarg, &endptr, 0);
5905 if (errno != 0 || *endptr != '\0' || !param.fp_nlink) {
5906 fprintf(stderr, "error: bad link count '%s'\n",
5911 param.fp_exclude_nlink = !!neg_opt;
5915 rc = name2uid(¶m.fp_uid, optarg);
5917 if (str2quotaid(¶m.fp_uid, optarg)) {
5919 "User/UID: %s cannot be found.\n",
5925 param.fp_exclude_uid = !!neg_opt;
5926 param.fp_check_uid = 1;
5929 param.fp_pattern = (char *)optarg;
5930 param.fp_exclude_pattern = !!neg_opt;
5933 if (optarg[0] == '+') {
5934 param.fp_mirror_count_sign = -1;
5936 } else if (optarg[0] == '-') {
5937 param.fp_mirror_count_sign = 1;
5942 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5943 if (errno != 0 || *endptr != '\0' ||
5944 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5946 "error: bad mirror count '%s'\n",
5950 param.fp_check_mirror_count = 1;
5951 param.fp_exclude_mirror_count = !!neg_opt;
5956 char *buf, *token, *next, *p;
5960 buf = strdup(optarg);
5966 param.fp_exclude_obd = !!neg_opt;
5969 while (token && *token) {
5970 token = strchr(token, ',');
5977 param.fp_exclude_mdt = !!neg_opt;
5978 param.fp_num_alloc_mdts += len;
5979 tmp = realloc(param.fp_mdt_uuid,
5980 param.fp_num_alloc_mdts *
5981 sizeof(*param.fp_mdt_uuid));
5987 param.fp_mdt_uuid = tmp;
5989 param.fp_exclude_obd = !!neg_opt;
5990 param.fp_num_alloc_obds += len;
5991 tmp = realloc(param.fp_obd_uuid,
5992 param.fp_num_alloc_obds *
5993 sizeof(*param.fp_obd_uuid));
5999 param.fp_obd_uuid = tmp;
6001 for (token = buf; token && *token; token = next) {
6002 struct obd_uuid *puuid;
6006 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
6009 ¶m.fp_obd_uuid[param.fp_num_obds++];
6011 p = strchr(token, ',');
6018 if (strlen(token) > sizeof(puuid->uuid) - 1) {
6023 strncpy(puuid->uuid, token,
6024 sizeof(puuid->uuid));
6031 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
6035 if (strlen(optarg) > LOV_MAXPOOLNAME) {
6037 "Pool name %s is too long (max %d)\n",
6038 optarg, LOV_MAXPOOLNAME);
6043 * We do check for empty pool because empty pool
6044 * is used to find V1 LOV attributes
6046 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
6047 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
6048 param.fp_exclude_pool = !!neg_opt;
6049 param.fp_check_pool = 1;
6052 param.fp_zero_end = 1;
6054 case 'P': /* we always print, this option is a no-op */
6056 case LFS_PRINTF_OPT:
6057 param.fp_format_printf_str = strdup(optarg);
6059 case LFS_PROJID_OPT:
6060 rc = name2projid(¶m.fp_projid, optarg);
6062 if (str2quotaid(¶m.fp_projid, optarg)) {
6064 "Invalid project ID: %s\n",
6070 param.fp_exclude_projid = !!neg_opt;
6071 param.fp_check_projid = 1;
6074 if (optarg[0] == '+') {
6075 param.fp_size_sign = -1;
6077 } else if (optarg[0] == '-') {
6078 param.fp_size_sign = 1;
6082 param.fp_size_units = 512;
6083 ret = llapi_parse_size(optarg, ¶m.fp_size,
6084 ¶m.fp_size_units, 0);
6086 fprintf(stderr, "error: bad file size '%s'\n",
6090 param.fp_check_size = 1;
6091 param.fp_exclude_size = !!neg_opt;
6094 if (optarg[0] == '+') {
6095 param.fp_stripe_size_sign = -1;
6097 } else if (optarg[0] == '-') {
6098 param.fp_stripe_size_sign = 1;
6102 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
6103 ¶m.fp_stripe_size_units, 0);
6104 /* assume units of KB if too small to be valid */
6105 if (param.fp_stripe_size < 4096)
6106 param.fp_stripe_size *= 1024;
6108 fprintf(stderr, "error: bad stripe_size '%s'\n",
6112 param.fp_check_stripe_size = 1;
6113 param.fp_exclude_stripe_size = !!neg_opt;
6116 param.fp_exclude_type = !!neg_opt;
6117 switch (optarg[0]) {
6119 param.fp_type = S_IFBLK;
6122 param.fp_type = S_IFCHR;
6125 param.fp_type = S_IFDIR;
6128 param.fp_type = S_IFREG;
6131 param.fp_type = S_IFLNK;
6134 param.fp_type = S_IFIFO;
6137 param.fp_type = S_IFSOCK;
6140 fprintf(stderr, "%s: bad type '%s'\n",
6147 param.fp_exclude_perm = !!neg_opt;
6148 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
6149 if (*optarg == '/') {
6150 param.fp_perm_sign = LFS_FIND_PERM_ANY;
6152 } else if (*optarg == '-') {
6153 param.fp_perm_sign = LFS_FIND_PERM_ALL;
6157 if (str2mode_t(optarg, ¶m.fp_perm)) {
6158 fprintf(stderr, "error: invalid mode '%s'\n",
6165 if (optarg[0] == '+') {
6166 param.fp_mdt_count_sign = -1;
6168 } else if (optarg[0] == '-') {
6169 param.fp_mdt_count_sign = 1;
6174 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
6175 if (errno != 0 || *endptr != '\0' ||
6176 param.fp_mdt_count >= UINT32_MAX) {
6177 fprintf(stderr, "error: bad mdt_count '%s'\n",
6182 param.fp_check_mdt_count = 1;
6183 param.fp_exclude_mdt_count = !!neg_opt;
6185 case LFS_XATTRS_MATCH_OPT:
6186 ret = compile_xattr_match_regex(optarg, neg_opt,
6192 if (optarg[0] == '+') {
6193 param.fp_ext_size_sign = -1;
6195 } else if (optarg[0] == '-') {
6196 param.fp_ext_size_sign = 1;
6200 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
6201 ¶m.fp_ext_size_units, 0);
6203 fprintf(stderr, "error: bad ext-size '%s'\n",
6207 param.fp_ext_size /= SEL_UNIT_SIZE;
6208 param.fp_ext_size_units /= SEL_UNIT_SIZE;
6209 param.fp_check_ext_size = 1;
6210 param.fp_exclude_ext_size = !!neg_opt;
6213 fprintf(stderr, "%s: unrecognized option '%s'\n",
6214 progname, argv[optind - 1]);
6220 if (!param.fp_verbose)
6221 param.fp_verbose = VERBOSE_DEFAULT;
6223 if (pathstart == -1) {
6224 fprintf(stderr, "error: %s: no filename|pathname\n",
6228 } else if (pathend == -1) {
6234 rc = llapi_find(argv[pathstart], ¶m);
6239 fprintf(stderr, "%s: failed for '%s': %s\n",
6240 progname, argv[pathstart], strerror(-rc));
6242 } while (++pathstart < pathend);
6245 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
6246 free(param.fp_obd_uuid);
6248 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
6249 free(param.fp_mdt_uuid);
6251 if (param.fp_format_printf_str)
6252 free(param.fp_format_printf_str);
6254 if (param.fp_xattr_match_info) {
6255 xattr_match_info_free(param.fp_xattr_match_info);
6256 free(param.fp_xattr_match_info);
6257 param.fp_xattr_match_info = NULL;
6263 static int lfs_getstripe_internal(int argc, char **argv,
6264 struct find_param *param)
6266 struct option long_opts[] = {
6267 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
6268 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
6269 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
6270 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
6271 { .val = LFS_COMP_COUNT_OPT,
6272 .name = "comp-count", .has_arg = no_argument },
6273 { .val = LFS_COMP_COUNT_OPT,
6274 .name = "component-count", .has_arg = no_argument },
6275 { .val = LFS_COMP_FLAGS_OPT,
6276 .name = "comp-flags", .has_arg = optional_argument },
6277 { .val = LFS_COMP_FLAGS_OPT,
6278 .name = "component-flags", .has_arg = optional_argument },
6279 { .val = LFS_COMP_START_OPT,
6280 .name = "comp-start", .has_arg = optional_argument },
6281 { .val = LFS_COMP_START_OPT,
6282 .name = "component-start", .has_arg = optional_argument },
6283 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
6284 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
6285 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
6286 { .val = 'd', .name = "directory", .has_arg = no_argument },
6287 { .val = 'D', .name = "default", .has_arg = no_argument },
6288 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
6289 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
6290 { .val = 'F', .name = "fid", .has_arg = no_argument },
6291 { .val = 'g', .name = "generation", .has_arg = no_argument },
6292 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
6293 { .val = 'h', .name = "help", .has_arg = no_argument },
6294 { .val = LFS_HEX_IDX_OPT,
6295 .name = "hex-idx", .has_arg = no_argument },
6296 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
6297 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
6298 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
6299 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
6300 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
6301 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6302 { .val = 'L', .name = "layout", .has_arg = no_argument },
6303 { .val = 'm', .name = "mdt", .has_arg = no_argument },
6304 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6305 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
6306 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
6307 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
6308 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
6309 { .val = LFS_MIRROR_INDEX_OPT,
6310 .name = "mirror-index", .has_arg = required_argument },
6311 { .val = LFS_MIRROR_ID_OPT,
6312 .name = "mirror-id", .has_arg = required_argument },
6313 { .val = LFS_NO_FOLLOW_OPT,
6314 .name = "no-follow", .has_arg = no_argument },
6315 { .val = 'O', .name = "obd", .has_arg = required_argument },
6316 { .val = 'O', .name = "ost", .has_arg = required_argument },
6317 { .val = 'p', .name = "pool", .has_arg = no_argument },
6318 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
6319 { .val = 'q', .name = "quiet", .has_arg = no_argument },
6320 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6321 { .val = 'R', .name = "raw", .has_arg = no_argument },
6322 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
6323 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
6324 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
6325 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
6326 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
6327 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
6328 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6329 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
6330 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }*/
6331 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6332 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
6333 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
6337 int pathstart = -1, pathend = -1;
6341 while ((c = getopt_long(argc, argv,
6342 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
6343 long_opts, NULL)) != -1) {
6347 /* '!' is part of option */
6348 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
6349 if (!isoption && pathend != -1) {
6351 "error: %s: filename|dirname must either precede options or follow options\n",
6355 if (!isoption && pathstart == -1)
6356 pathstart = optind - 1;
6357 if (isoption && pathstart != -1 && pathend == -1)
6358 pathend = optind - 2;
6362 /* unknown: opt is "!" */
6363 if (strcmp(optarg, "!") == 0)
6367 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6368 param->fp_verbose |= VERBOSE_COUNT;
6369 param->fp_max_depth = 0;
6372 case LFS_COMP_COUNT_OPT:
6373 param->fp_verbose |= VERBOSE_COMP_COUNT;
6374 param->fp_max_depth = 0;
6376 case LFS_COMP_FLAGS_OPT:
6378 rc = comp_str2flags(optarg,
6379 ¶m->fp_comp_flags,
6380 ¶m->fp_comp_neg_flags);
6383 "error: %s bad component flags '%s'.\n",
6387 param->fp_check_comp_flags = 1;
6389 param->fp_verbose |= VERBOSE_COMP_FLAGS;
6390 param->fp_max_depth = 0;
6393 case LFS_COMP_START_OPT:
6396 if (tmp[0] == '+') {
6397 param->fp_comp_start_sign = -1;
6399 } else if (tmp[0] == '-') {
6400 param->fp_comp_start_sign = 1;
6403 rc = llapi_parse_size(tmp,
6404 ¶m->fp_comp_start,
6405 ¶m->fp_comp_start_units, 0);
6408 "error: %s bad component start '%s'.\n",
6412 param->fp_check_comp_start = 1;
6414 param->fp_verbose |= VERBOSE_COMP_START;
6415 param->fp_max_depth = 0;
6418 case LFS_MIRROR_INDEX_OPT: {
6419 unsigned long int mirror_index;
6421 if (optarg[0] == '+') {
6422 param->fp_mirror_index_sign = -1;
6424 } else if (optarg[0] == '-') {
6425 param->fp_mirror_index_sign = 1;
6430 mirror_index = strtoul(optarg, &end, 0);
6431 if (errno != 0 || *end != '\0' ||
6432 mirror_index > UINT16_MAX || (mirror_index == 0 &&
6433 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
6435 "%s %s: invalid mirror index '%s'\n",
6436 progname, argv[0], optarg);
6440 param->fp_mirror_index = (__u16)mirror_index;
6442 if (param->fp_mirror_id != 0) {
6444 "%s %s: can't specify both mirror index and mirror ID\n",
6448 param->fp_check_mirror_index = 1;
6449 param->fp_exclude_mirror_index = !!neg_opt;
6452 case LFS_MIRROR_ID_OPT: {
6453 unsigned long int mirror_id;
6455 if (optarg[0] == '+') {
6456 param->fp_mirror_id_sign = -1;
6458 } else if (optarg[0] == '-') {
6459 param->fp_mirror_id_sign = 1;
6464 mirror_id = strtoul(optarg, &end, 0);
6465 if (errno != 0 || *end != '\0' ||
6466 mirror_id > UINT16_MAX || (mirror_id == 0 &&
6467 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
6469 "%s %s: invalid mirror ID '%s'\n",
6470 progname, argv[0], optarg);
6474 param->fp_mirror_id = (__u16)mirror_id;
6476 if (param->fp_mirror_index != 0) {
6478 "%s %s: can't specify both mirror index and mirror ID\n",
6482 param->fp_check_mirror_id = 1;
6483 param->fp_exclude_mirror_id = !!neg_opt;
6486 case LFS_NO_FOLLOW_OPT:
6487 param->fp_no_follow = true;
6489 case LFS_HEX_IDX_OPT:
6490 param->fp_hex_idx = true;
6493 param->fp_max_depth = 0;
6496 param->fp_get_default_lmv = 1;
6501 if (tmp[0] == '+') {
6502 param->fp_comp_end_sign = -1;
6504 } else if (tmp[0] == '-') {
6505 param->fp_comp_end_sign = 1;
6509 if (arg_is_eof(tmp)) {
6510 param->fp_comp_end = LUSTRE_EOF;
6511 param->fp_comp_end_units = 1;
6514 rc = llapi_parse_size(tmp,
6515 ¶m->fp_comp_end,
6516 ¶m->fp_comp_end_units, 0);
6517 /* assume units of KB if too small */
6518 if (param->fp_comp_end < 4096)
6519 param->fp_comp_end *= 1024;
6523 "error: %s bad component end '%s'.\n",
6527 param->fp_check_comp_end = 1;
6529 param->fp_verbose |= VERBOSE_COMP_END;
6530 param->fp_max_depth = 0;
6534 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6535 param->fp_verbose |= VERBOSE_DFID;
6536 param->fp_max_depth = 0;
6540 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6541 param->fp_verbose |= VERBOSE_GENERATION;
6542 param->fp_max_depth = 0;
6546 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6547 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6548 param->fp_max_depth = 0;
6553 param->fp_comp_id = strtoul(optarg, &end, 0);
6554 if (*end != '\0' || param->fp_comp_id == 0 ||
6555 param->fp_comp_id > LCME_ID_MAX) {
6557 "error: %s bad component id '%s'\n",
6561 param->fp_check_comp_id = 1;
6563 param->fp_max_depth = 0;
6564 param->fp_verbose |= VERBOSE_COMP_ID;
6568 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6569 param->fp_verbose |= VERBOSE_PATTERN;
6570 param->fp_max_depth = 0;
6573 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6576 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6579 if (!(param->fp_verbose & VERBOSE_DETAIL))
6580 param->fp_max_depth = 0;
6581 param->fp_verbose |= VERBOSE_MDTINDEX;
6584 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6585 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6586 param->fp_max_depth = 0;
6590 if (param->fp_obd_uuid) {
6592 "error: %s: only one obduuid allowed",
6596 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6599 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6600 param->fp_verbose |= VERBOSE_POOL;
6601 param->fp_max_depth = 0;
6608 param->fp_recursive = 1;
6614 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6615 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6616 param->fp_max_depth = 0;
6620 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6626 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6627 param->fp_verbose |= VERBOSE_EXT_SIZE;
6628 param->fp_max_depth = 0;
6632 fprintf(stderr, "%s: unrecognized option '%s'\n",
6633 progname, argv[optind - 1]);
6639 if (pathstart == -1) {
6640 fprintf(stderr, "error: %s: no filename|pathname\n",
6643 } else if (pathend == -1) {
6651 if (param->fp_recursive)
6652 param->fp_max_depth = -1;
6653 else if (param->fp_verbose & VERBOSE_DETAIL)
6654 param->fp_max_depth = 1;
6656 if (!param->fp_verbose)
6657 param->fp_verbose = VERBOSE_DEFAULT;
6658 if (param->fp_quiet)
6659 param->fp_verbose = VERBOSE_OBJID;
6664 rc2 = llapi_getstripe(argv[pathstart], param);
6666 fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6667 progname, argv[0], argv[pathstart],
6672 } while (++pathstart < pathend);
6677 static int lfs_tgts(int argc, char **argv)
6679 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6680 struct find_param param;
6681 int index = 0, rc = 0;
6686 if (argc == 2 && !realpath(argv[1], path)) {
6688 fprintf(stderr, "error: invalid path '%s': %s\n",
6689 argv[1], strerror(-rc));
6693 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6694 /* Check if we have a mount point */
6695 if (mntdir[0] == '\0')
6698 memset(¶m, 0, sizeof(param));
6699 if (!strcmp(argv[0], "mdts"))
6700 param.fp_get_lmv = 1;
6702 rc = llapi_ostlist(mntdir, ¶m);
6704 fprintf(stderr, "error: %s: failed on %s\n",
6707 if (path[0] != '\0')
6709 memset(mntdir, 0, PATH_MAX);
6715 static int lfs_getstripe(int argc, char **argv)
6717 struct find_param param = { 0 };
6719 param.fp_max_depth = 1;
6720 return lfs_getstripe_internal(argc, argv, ¶m);
6724 static int lfs_getdirstripe(int argc, char **argv)
6726 struct find_param param = { 0 };
6727 struct option long_opts[] = {
6728 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6729 { .val = 'D', .name = "default", .has_arg = no_argument },
6730 { .val = 'h', .name = "help", .has_arg = no_argument },
6731 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6732 { .val = LFS_HEX_IDX_OPT,
6733 .name = "hex-idx", .has_arg = no_argument },
6734 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6735 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6736 { .val = 'O', .name = "obd", .has_arg = required_argument },
6737 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6738 { .val = 'R', .name = "raw", .has_arg = no_argument },
6739 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6740 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6741 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6742 { .val = LFS_INHERIT_RR_OPT,
6743 .name = "max-inherit-rr", .has_arg = no_argument },
6744 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6748 param.fp_get_lmv = 1;
6750 while ((c = getopt_long(argc, argv,
6751 "cDhHimO:rRtTvXy", long_opts, NULL)) != -1) {
6755 param.fp_verbose |= VERBOSE_COUNT;
6758 param.fp_get_default_lmv = 1;
6760 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6763 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6767 param.fp_verbose |= VERBOSE_HASH_TYPE;
6769 case LFS_HEX_IDX_OPT:
6770 param.fp_hex_idx = 1;
6775 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6778 if (param.fp_obd_uuid) {
6780 "%s: only one obduuid allowed",
6784 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6787 param.fp_recursive = 1;
6793 param.fp_verbose |= VERBOSE_DEFAULT;
6794 param.fp_verbose |= VERBOSE_DETAIL;
6797 param.fp_verbose |= VERBOSE_INHERIT;
6799 case LFS_INHERIT_RR_OPT:
6800 param.fp_verbose |= VERBOSE_INHERIT_RR;
6806 fprintf(stderr, "%s: unrecognized option '%s'\n",
6807 progname, argv[optind - 1]);
6817 if (param.fp_recursive)
6818 param.fp_max_depth = -1;
6820 if (!param.fp_verbose)
6821 param.fp_verbose = VERBOSE_DEFAULT;
6826 rc2 = llapi_getstripe(argv[optind], ¶m);
6828 fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6829 progname, argv[0], argv[optind],
6834 } while (++optind < argc);
6840 MNTDF_INODES = 0x0001,
6841 MNTDF_COOKED = 0x0002,
6842 MNTDF_LAZY = 0x0004,
6843 MNTDF_VERBOSE = 0x0008,
6844 MNTDF_SHOW = 0x0010,
6845 MNTDF_DECIMAL = 0x0020,
6848 #define COOK(value, base) \
6851 while (value > base) { \
6859 #define CDF "%11llu"
6860 #define HDF "%8.1f%c"
6864 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6866 double avail, used, ratio = 0;
6869 avail = st->os_ffree;
6870 used = st->os_files - st->os_ffree;
6872 avail = st->os_bavail;
6873 used = st->os_blocks - st->os_bfree;
6875 if (avail + used > 0)
6876 ratio = used / (used + avail) * 100;
6878 /* Round up to match df(1) usage percentage */
6879 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6883 * This is to identify various problem states for "lfs df" if .osn_err = true,
6884 * so only show flags reflecting those states by default. Informational states
6885 * are only shown with "-v" and use lower-case names to distinguish them.
6886 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6888 static struct obd_statfs_state_names {
6889 enum obd_statfs_state osn_state;
6890 const char osn_name;
6893 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6894 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6895 { .osn_state = OS_STATFS_NOCREATE, .osn_name = 'N', .osn_err = true },
6896 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6897 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6898 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6899 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6900 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6901 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6904 static int showdf(char *mntdir, struct obd_statfs *stat,
6905 char *uuid, enum mntdf_flags flags,
6906 char *type, int index, int rc)
6908 long long avail, used, total;
6910 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6911 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6912 char tbuf[3 * sizeof(__u64)];
6913 char ubuf[3 * sizeof(__u64)];
6914 char abuf[3 * sizeof(__u64)];
6915 char rbuf[3 * sizeof(__u64)];
6922 if (flags & MNTDF_INODES) {
6923 avail = stat->os_ffree;
6924 used = stat->os_files - stat->os_ffree;
6925 total = stat->os_files;
6927 int shift = flags & MNTDF_COOKED ? 0 : 10;
6929 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6930 used = ((stat->os_blocks - stat->os_bfree) *
6931 stat->os_bsize) >> shift;
6932 total = (stat->os_blocks * stat->os_bsize) >> shift;
6935 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6937 if (flags & MNTDF_COOKED) {
6938 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6942 cook_val = (double)total;
6943 i = COOK(cook_val, base);
6945 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6948 snprintf(tbuf, sizeof(tbuf), CDF, total);
6950 cook_val = (double)used;
6951 i = COOK(cook_val, base);
6953 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6956 snprintf(ubuf, sizeof(ubuf), CDF, used);
6958 cook_val = (double)avail;
6959 i = COOK(cook_val, base);
6961 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6964 snprintf(abuf, sizeof(abuf), CDF, avail);
6966 snprintf(tbuf, sizeof(tbuf), CDF, total);
6967 snprintf(ubuf, sizeof(tbuf), CDF, used);
6968 snprintf(abuf, sizeof(tbuf), CDF, avail);
6971 sprintf(rbuf, RDF, ratio);
6972 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6973 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6975 printf("[%s:%d]", type, index);
6977 if (stat->os_state) {
6981 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6982 if (oss_names[i].osn_state & stat->os_state &&
6983 (oss_names[i].osn_err ||
6984 flags & MNTDF_VERBOSE))
6985 printf("%c", oss_names[i].osn_name);
6992 printf(UUF": inactive device\n", uuid);
6995 printf(UUF": %s\n", uuid, strerror(-rc));
7002 struct ll_stat_type {
7007 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
7009 struct ll_statfs_data {
7011 struct obd_statfs sd_st;
7014 struct ll_statfs_buf {
7016 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
7019 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
7020 int ops, struct ll_statfs_buf *lsb)
7022 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
7023 struct obd_uuid uuid_buf;
7024 char *poolname = NULL;
7025 struct ll_stat_type types[] = {
7026 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
7027 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
7028 { .st_name = NULL } };
7029 struct ll_stat_type *tp;
7030 __u64 ost_files = 0;
7031 __u64 ost_ffree = 0;
7039 poolname = strchr(pool, '.');
7041 if (strncmp(fsname, pool, strlen(fsname))) {
7042 fprintf(stderr, "filesystem name incorrect\n");
7050 fd = open(mntdir, O_RDONLY);
7053 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
7058 if (flags & MNTDF_SHOW) {
7059 if (flags & MNTDF_INODES)
7060 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
7061 "UUID", "Inodes", "IUsed", "IFree",
7062 "IUse%", "Mounted on");
7064 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
7066 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
7067 "Used", "Available", "Use%", "Mounted on");
7070 for (tp = types; tp->st_name != NULL; tp++) {
7071 bool have_ost = false;
7073 if (!(tp->st_op & ops))
7076 for (index = 0; index < LOV_ALL_STRIPES &&
7077 (!lsb || lsb->sb_count < LL_STATFS_MAX); index++) {
7078 memset(&stat_buf, 0, sizeof(struct obd_statfs));
7079 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
7080 type = flags & MNTDF_LAZY ?
7081 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
7082 rc2 = llapi_obd_fstatfs(fd, type, index,
7083 &stat_buf, &uuid_buf);
7088 if (rc2 == -ENODATA) { /* Inactive device, OK. */
7089 if (!(flags & MNTDF_VERBOSE))
7091 } else if (rc2 < 0 && rc == 0) {
7096 * If we have OSTs then don't report MDT block counts.
7097 * For MDT-only filesystems the expectation is that all
7098 * layouts have a DoM component. For filesystems with
7099 * OSTs, files are not necessarily going to store data
7100 * on MDTs, and MDT space is limited to a fraction of
7101 * OST space, so don't include it in the summary.
7103 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
7110 if (poolname && tp->st_op == LL_STATFS_LOV &&
7111 llapi_search_ost(fsname, poolname,
7112 obd_uuid2str(&uuid_buf)) != 1)
7116 * the llapi_obd_fstatfs() call may have returned with
7117 * an error, but if it filled in uuid_buf we will at
7118 * lease use that to print out a message for that OBD.
7119 * If we didn't get anything in the uuid_buf, then fill
7120 * it in so that we can print an error message.
7122 if (uuid_buf.uuid[0] == '\0')
7123 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
7124 "%s%04x", tp->st_name, index);
7126 lsb->sb_buf[lsb->sb_count].sd_index = index;
7127 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
7130 if (flags & MNTDF_SHOW)
7131 showdf(mntdir, &stat_buf,
7132 obd_uuid2str(&uuid_buf), flags,
7133 tp->st_name, index, rc2);
7138 if (tp->st_op == LL_STATFS_LMV) {
7139 sum.os_ffree += stat_buf.os_ffree;
7140 sum.os_files += stat_buf.os_files;
7141 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
7142 ost_files += stat_buf.os_files;
7143 ost_ffree += stat_buf.os_ffree;
7145 sum.os_blocks += stat_buf.os_blocks *
7147 sum.os_bfree += stat_buf.os_bfree *
7149 sum.os_bavail += stat_buf.os_bavail *
7157 * If we have _some_ OSTs, but don't have as many free objects on the
7158 * OST as inodes on the MDTs, reduce the reported number of inodes
7159 * to compensate, so that the "inodes in use" number is correct.
7160 * This should be kept in sync with ll_statfs_internal().
7162 if (ost_files && ost_ffree < sum.os_ffree) {
7163 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
7164 sum.os_ffree = ost_ffree;
7166 if (flags & MNTDF_SHOW) {
7168 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
7176 LAYOUT_INHERIT_UNSET = -2,
7180 static int lfs_setdirstripe(int argc, char **argv)
7183 struct lfs_setstripe_args lsa = { 0 };
7184 struct llapi_stripe_param *param = NULL;
7185 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
7188 char *mode_opt = NULL;
7189 bool default_stripe = false;
7190 bool delete = false;
7191 bool foreign_mode = false;
7192 bool mdt_count_set = false;
7193 bool overstriped = false;
7194 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
7195 mode_t previous_mode = 0;
7197 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
7198 int max_inherit = LAYOUT_INHERIT_UNSET;
7199 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
7200 struct option long_opts[] = {
7201 { .val = 'c', .name = "count", .has_arg = required_argument },
7202 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
7203 { .val = 'C', .name = "mdt-overcount", .has_arg = required_argument },
7204 { .val = 'd', .name = "delete", .has_arg = no_argument },
7205 { .val = 'D', .name = "default", .has_arg = no_argument },
7206 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
7207 { .val = LFS_LAYOUT_FLAGS_OPT,
7208 .name = "flags", .has_arg = required_argument },
7209 { .val = LFS_LAYOUT_FOREIGN_OPT,
7210 .name = "foreign", .has_arg = optional_argument},
7211 { .val = 'h', .name = "help", .has_arg = no_argument },
7212 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
7213 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
7214 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
7215 { .val = 'i', .name = "mdt", .has_arg = required_argument },
7217 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
7218 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7219 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7221 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7222 { .val = 'i', .name = "index", .has_arg = required_argument },
7224 { .val = 'o', .name = "mode", .has_arg = required_argument },
7225 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7226 { .val = 't', .name = "hash-type", .has_arg = required_argument },
7228 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
7229 { .val = 'x', .name = "xattr", .has_arg = required_argument },
7230 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
7231 { .val = LFS_INHERIT_RR_OPT,
7232 .name = "max-inherit-rr", .has_arg = required_argument},
7233 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
7234 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
7238 setstripe_args_init(&lsa);
7240 while ((c = getopt_long(argc, argv, "c:C:dDi:hH:m:o:t:T:x:X:",
7241 long_opts, NULL)) >= 0) {
7253 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
7254 if (errno != 0 || *end != '\0' ||
7255 lsa.lsa_stripe_count < -1 ||
7256 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
7258 "%s: invalid stripe count '%s'\n",
7262 mdt_count_set = true;
7266 default_stripe = true;
7269 default_stripe = true;
7271 case LFS_LAYOUT_FOREIGN_OPT:
7273 /* check pure numeric */
7274 type = strtoul(optarg, &end, 0);
7277 type = check_foreign_type_name(optarg);
7278 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
7280 "%s %s: unknown foreign type '%s'\n",
7285 } else if (type >= UINT32_MAX) {
7287 "%s %s: invalid foreign type '%s'\n",
7288 progname, argv[0], optarg);
7292 foreign_mode = true;
7294 case LFS_LAYOUT_FLAGS_OPT:
7296 flags = strtoul(optarg, &end, 16);
7297 if (errno != 0 || *end != '\0' ||
7298 flags >= UINT32_MAX) {
7300 "%s %s: invalid hex flags '%s'\n",
7301 progname, argv[0], optarg);
7304 if (!foreign_mode) {
7306 "%s %s: hex flags must be specified with --foreign option\n",
7311 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7314 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
7318 lsa.lsa_pattern = check_hashtype(optarg);
7319 if (lsa.lsa_pattern == 0) {
7321 "%s %s: bad directory hash type '%s'\n",
7322 progname, argv[0], optarg);
7327 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
7330 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7331 if (strcmp(argv[optind - 1], "--index") == 0)
7333 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
7336 lsa.lsa_nr_tgts = parse_targets(mdts,
7337 sizeof(mdts) / sizeof(__u32),
7338 lsa.lsa_nr_tgts, optarg,
7340 if (lsa.lsa_nr_tgts < 0) {
7342 "%s %s: invalid MDT target(s) '%s'\n",
7343 progname, argv[0], optarg);
7347 lsa.lsa_tgts = mdts;
7348 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7349 lsa.lsa_stripe_off = mdts[0];
7359 max_inherit = strtol(optarg, &end, 10);
7360 if (errno != 0 || *end != '\0' || max_inherit < -2) {
7362 "%s %s: invalid max-inherit '%s'\n",
7363 progname, argv[0], optarg);
7366 if (max_inherit == 0) {
7367 max_inherit = LMV_INHERIT_NONE;
7368 } else if (max_inherit == -1) {
7369 max_inherit = LMV_INHERIT_UNLIMITED;
7370 } else if (max_inherit > LMV_INHERIT_MAX) {
7372 "%s %s: max-inherit %d exceeds maximum %u\n",
7373 progname, argv[0], max_inherit,
7378 case LFS_INHERIT_RR_OPT:
7379 if (!default_stripe) {
7381 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
7386 max_inherit_rr = strtol(optarg, &end, 10);
7387 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
7389 "%s %s: invalid max-inherit-rr '%s'\n",
7390 progname, argv[0], optarg);
7393 if (max_inherit_rr == 0) {
7394 max_inherit_rr = LMV_INHERIT_RR_NONE;
7395 } else if (max_inherit_rr == -1) {
7396 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
7397 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
7399 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
7400 progname, argv[0], max_inherit_rr,
7401 LMV_INHERIT_RR_MAX);
7406 fprintf(stderr, "%s: unrecognized option '%s'\n",
7407 progname, argv[optind - 1]);
7414 if (optind == argc) {
7415 fprintf(stderr, "%s %s: DIR must be specified\n",
7420 if (xattr && !foreign_mode) {
7422 * only print a warning as this is armless and will be
7426 "%s %s: xattr has been specified for non-foreign layout\n",
7428 } else if (foreign_mode && !xattr) {
7430 "%s %s: xattr must be provided in foreign mode\n",
7435 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
7436 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
7438 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
7443 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
7444 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
7445 /* if no parameters set, create directory on least-used MDTs */
7446 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
7447 lsa.lsa_stripe_count = 1;
7451 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
7452 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
7454 "%s %s: cannot specify -d with -c or -i options\n",
7460 mode = strtoul(mode_opt, &end, 8);
7463 "%s %s: bad MODE '%s'\n",
7464 progname, argv[0], mode_opt);
7467 previous_mode = umask(0);
7470 /* check max-inherit and warn user in some cases */
7471 if (default_stripe &&
7472 (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
7473 if (max_inherit == LMV_INHERIT_UNLIMITED)
7475 "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
7476 progname, argv[0], lsa.lsa_stripe_count);
7477 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
7478 max_inherit != LMV_INHERIT_NONE)
7480 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
7481 progname, argv[0], max_inherit,
7482 lsa.lsa_stripe_count);
7485 if (default_stripe && lsa.lsa_nr_tgts > 1 && !mdt_count_set) {
7487 "%s %s: trying to create unrecommended default striped directory layout,\n"
7488 " '-D -i x,y,z' will stripe every new directory across all MDTs,\n"
7489 " add -c with the number of MDTs to do this anyway\n",
7494 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
7495 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
7496 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
7498 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
7499 progname, argv[0], lsa.lsa_stripe_off);
7503 /* foreign LMV/dir case */
7505 if (argc > optind + 1) {
7507 "%s %s: cannot specify multiple foreign dirs\n",
7512 dname = argv[optind];
7513 result = llapi_dir_create_foreign(dname, mode, type, flags,
7517 "%s mkdir: can't create foreign dir '%s': %s\n",
7518 progname, dname, strerror(-result));
7523 * initialize stripe parameters, in case param is converted to specific,
7524 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
7526 param = calloc(1, offsetof(typeof(*param),
7527 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
7528 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
7531 "%s %s: cannot allocate memory for parameters: %s\n",
7532 progname, argv[0], strerror(ENOMEM));
7536 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
7537 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
7538 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
7539 lsa.lsa_stripe_count == 0))
7540 lsa.lsa_stripe_count = 1;
7541 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
7542 param->lsp_stripe_count = lsa.lsa_stripe_count;
7543 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7544 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
7546 param->lsp_stripe_offset = lsa.lsa_stripe_off;
7548 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
7549 param->lsp_stripe_pattern = lsa.lsa_pattern;
7551 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
7554 param->lsp_stripe_pattern |= LMV_HASH_FLAG_OVERSTRIPED;
7555 max_inherit = LMV_INHERIT_DEFAULT_OVERSTRIPED;
7558 param->lsp_pool = lsa.lsa_pool_name;
7559 param->lsp_is_specific = false;
7561 if (max_inherit == LAYOUT_INHERIT_UNSET) {
7562 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1 ||
7563 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT)
7564 max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
7566 max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
7568 param->lsp_max_inherit = max_inherit;
7569 if (default_stripe) {
7571 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
7572 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
7573 param->lsp_max_inherit_rr = max_inherit_rr;
7575 if (strcmp(argv[0], "mkdir") == 0)
7576 param->lsp_is_create = true;
7577 if (lsa.lsa_nr_tgts > 1) {
7578 if (lsa.lsa_stripe_count > 0 &&
7579 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7580 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7582 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7583 argv[0], lsa.lsa_stripe_count,
7589 param->lsp_is_specific = true;
7590 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7591 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7594 dname = argv[optind];
7596 if (default_stripe) {
7597 result = llapi_dir_set_default_lmv(dname, param);
7600 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7601 progname, dname, strerror(-result));
7605 result = llapi_dir_create(dname, mode, param);
7608 "%s setdirstripe: cannot create dir '%s': %s\n",
7609 progname, dname, strerror(-result));
7610 } while ((dname = argv[++optind]));
7613 umask(previous_mode);
7619 static int lfs_rmentry(int argc, char **argv)
7626 fprintf(stderr, "error: %s: missing dirname\n",
7632 dname = argv[index];
7636 rc2 = llapi_direntry_remove(dname);
7639 "%s %s: remove dir entry '%s' failed: %s\n",
7640 progname, argv[0], dname, strerror(-rc2));
7644 dname = argv[++index];
7649 static int lfs_unlink_foreign(int argc, char **argv)
7656 fprintf(stderr, "error: %s: missing pathname\n",
7663 while (name != NULL) {
7664 result = llapi_unlink_foreign(name);
7667 "error: %s: unlink foreign entry '%s' failed\n",
7671 name = argv[++index];
7676 static int lfs_mv(int argc, char **argv)
7678 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7679 struct find_param param = {
7686 struct option long_opts[] = {
7687 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7688 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7689 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7692 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7694 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7697 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7701 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7702 if (errno != 0 || *end != '\0' ||
7703 lmu.lum_stripe_offset >= UINT32_MAX) {
7704 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7710 param.fp_verbose = VERBOSE_DETAIL;
7713 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7714 progname, argv[optind - 1]);
7719 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7720 fprintf(stderr, "%s mv: MDT index must be specified\n",
7725 if (optind >= argc) {
7726 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7730 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7732 /* initialize migrate mdt parameters */
7733 param.fp_lmv_md = &lmu;
7734 param.fp_migrate = 1;
7735 rc = llapi_migrate_mdt(argv[optind], ¶m);
7737 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7738 progname, argv[optind], lmu.lum_stripe_offset,
7743 static int lfs_osts(int argc, char **argv)
7745 return lfs_tgts(argc, argv);
7748 static int lfs_mdts(int argc, char **argv)
7750 return lfs_tgts(argc, argv);
7753 static int lfs_df(int argc, char **argv)
7755 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7756 enum mntdf_flags flags = MNTDF_SHOW;
7757 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7758 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7759 char fsname[PATH_MAX] = "", *pool_name = NULL;
7760 struct option long_opts[] = {
7761 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7762 { .val = 'H', .name = "si", .has_arg = no_argument },
7763 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7764 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7765 { .val = 'p', .name = "pool", .has_arg = required_argument },
7766 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7769 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7772 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7775 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7778 flags |= MNTDF_INODES;
7781 flags |= MNTDF_LAZY;
7787 flags |= MNTDF_VERBOSE;
7790 fprintf(stderr, "%s: unrecognized option '%s'\n",
7791 progname, argv[optind - 1]);
7796 /* Handle case where path is not specified */
7797 if (optind == argc) {
7798 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7799 /* Check if we have a mount point */
7800 if (mntdir[0] == '\0')
7803 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7804 if (rc || path[0] != '\0')
7807 fsname[0] = '\0'; /* avoid matching in next loop */
7808 mntdir[0] = '\0'; /* avoid matching in next loop */
7809 path[0] = '\0'; /* clean for next loop */
7814 /* Loop through all the remaining arguments. These are Lustre FS
7817 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7820 fsname[0] = '\0'; /* start clean */
7821 mntdir[0] = '\0'; /* start clean */
7822 path[0] = '\0'; /* start clean */
7824 /* path does not exists at all */
7825 if (!realpath(argv[arg_idx], path)) {
7827 fprintf(stderr, "error: invalid path '%s': %s\n",
7828 argv[arg_idx], strerror(-rc));
7829 /* save first seen error */
7836 /* path exists but may not be a Lustre filesystem */
7837 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7838 /* Check if we have a mount point */
7839 if (mntdir[0] == '\0')
7842 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7843 if (rc || path[0] != '\0') {
7846 /* save first seen error */
7854 llapi_printf(LLAPI_MSG_ERROR,
7855 "%s:%s Not a Lustre filesystem\n",
7856 argv[0], argv[arg_idx]);
7857 /* save first seen error */
7866 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7867 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7871 if (opt_fsname == opt_instance) { /* both true or both false */
7872 rc = llapi_getname(mntdir, buf, buflen);
7873 } else if (opt_fsname) {
7875 * llapi_search_mounts() fills @buf with fsname, but that is not
7876 * called if explicit paths are specified on the command-line
7879 rc = llapi_get_fsname(mntdir, buf, buflen);
7880 } else /* if (opt_instance) */ {
7881 rc = llapi_get_instance(mntdir, buf, buflen);
7885 fprintf(stderr, "cannot get instance for '%s': %s\n",
7886 mntdir, strerror(-rc));
7891 printf("%s %s\n", buf, mntdir);
7893 printf("%s\n", buf);
7898 static int lfs_getname(int argc, char **argv)
7900 struct option long_opts[] = {
7901 { .val = 'h', .name = "help", .has_arg = no_argument },
7902 { .val = 'i', .name = "instance", .has_arg = no_argument },
7903 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7905 bool opt_instance = false, opt_fsname = false;
7906 char fsname[PATH_MAX] = "";
7909 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7912 opt_instance = true;
7918 fprintf(stderr, "%s: unrecognized option '%s'\n",
7919 progname, argv[optind - 1]);
7926 if (optind == argc) { /* no paths specified, get all paths. */
7927 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7930 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7931 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7932 opt_instance, opt_fsname, true);
7935 path[0] = fsname[0] = mntdir[0] = '\0';
7937 } else { /* paths specified, only attempt to search these. */
7940 /* if only one path is given, print only requested info */
7941 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7943 for (; optind < argc; optind++) {
7944 rc2 = print_instance(argv[optind], fsname,
7945 sizeof(fsname), opt_instance,
7946 opt_fsname, opt_mntdir);
7956 static int lfs_check(int argc, char **argv)
7958 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7966 if (argc < 2 || argc > 3) {
7967 fprintf(stderr, "%s check: server type must be specified\n",
7972 obd_types[0] = obd_type1;
7973 obd_types[1] = obd_type2;
7974 obd_types[2] = obd_type3;
7976 if (strcmp(argv[1], "osts") == 0) {
7977 strcpy(obd_types[0], "osc");
7978 } else if (strcmp(argv[1], "mdts") == 0 ||
7979 strcmp(argv[1], "mds") == 0) {
7980 strcpy(obd_types[0], "mdc");
7981 } else if (strcmp(argv[1], "mgts") == 0) {
7982 strcpy(obd_types[0], "mgc");
7983 } else if (strcmp(argv[1], "all") == 0 ||
7984 strcmp(argv[1], "servers") == 0) {
7986 strcpy(obd_types[0], "osc");
7987 strcpy(obd_types[1], "mdc");
7988 strcpy(obd_types[2], "mgc");
7990 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7995 if (argc >= 3 && !realpath(argv[2], path)) {
7997 fprintf(stderr, "error: invalid path '%s': %s\n",
7998 argv[2], strerror(-rc));
8002 rc = llapi_search_mounts(path, 0, mntdir, NULL);
8003 if (rc < 0 || mntdir[0] == '\0') {
8005 "%s %s: cannot find mounted Lustre filesystem: %s\n",
8007 (rc < 0) ? strerror(-rc) : strerror(ENODEV));
8011 rc = llapi_target_check(num_types, obd_types, path);
8013 fprintf(stderr, "%s %s: cannot check target '%s': %s\n",
8014 progname, argv[0], argv[1], strerror(-rc));
8019 #ifdef HAVE_SYS_QUOTA_H
8020 #define ADD_OVERFLOW(a, b) \
8021 ((((a) + (b)) < (a)) ? \
8022 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
8024 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
8025 * returns the value or ULONG_MAX on integer overflow or incorrect format
8027 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
8028 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
8029 * 3. empty integer value is interpreted as 0
8031 static unsigned long str2sec(const char *timestr)
8033 const char spec[] = "smhdw";
8034 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8035 unsigned long val = 0;
8038 if (strpbrk(timestr, spec) == NULL) {
8040 * no specifiers inside the time string,
8041 * should treat it as an integer value
8043 val = strtoul(timestr, &tail, 10);
8044 return *tail ? ULONG_MAX : val;
8047 /* format string is XXwXXdXXhXXmXXs */
8053 v = strtoul(timestr, &tail, 10);
8054 if (v == ULONG_MAX || *tail == '\0')
8056 * value too large (ULONG_MAX or more)
8057 * or missing specifier
8061 ptr = strchr(spec, *tail);
8063 /* unknown specifier */
8068 /* check if product will overflow the type */
8069 if (!(v < ULONG_MAX / mult[ind]))
8072 ADD_OVERFLOW(val, mult[ind] * v);
8073 if (val == ULONG_MAX)
8085 #define ARG2ULL(nr, str, def_units) \
8087 unsigned long long limit, units = def_units; \
8090 rc = llapi_parse_size(str, &limit, &units, 1); \
8092 fprintf(stderr, "%s: invalid limit '%s'\n", \
8099 static inline int has_times_option(int argc, char **argv)
8103 for (i = 1; i < argc; i++)
8104 if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--times"))
8110 static inline int lfs_verify_poolarg(char *pool)
8112 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
8114 "Pool name '%.*s' is longer than %d\n",
8115 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
8121 /* special grace time, only notify the user when its quota is over soft limit
8122 * but doesn't block new writes until the hard limit is reached.
8124 #define NOTIFY_GRACE "notify"
8125 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
8128 static inline __u64 lustre_stoqb(size_t space)
8130 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
8133 #define lustre_stoqb toqb
8136 static int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
8139 char *mnt, *obd_type = (char *)qctl->obd_type;
8140 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8141 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
8142 struct option long_opts[] = {
8143 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
8144 { .val = 'g', .name = "group", .has_arg = no_argument },
8145 { .val = 'h', .name = "help", .has_arg = no_argument },
8146 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
8147 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
8148 { .val = 'p', .name = "projid", .has_arg = no_argument },
8149 { .val = 't', .name = "times", .has_arg = no_argument },
8150 { .val = 'u', .name = "user", .has_arg = no_argument },
8154 qctl->qc_cmd = LUSTRE_Q_SETINFO;
8155 qctl->qc_type = ALLQUOTA;
8157 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
8158 long_opts, NULL)) != -1) {
8161 if (strncmp(optarg, NOTIFY_GRACE,
8162 strlen(NOTIFY_GRACE)) == 0) {
8163 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
8165 dqi->dqi_bgrace = str2sec(optarg);
8166 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
8168 "%s: bad block-grace: %s\n",
8173 dqb->dqb_valid |= QIF_BTIME;
8179 if (strncmp(optarg, NOTIFY_GRACE,
8180 strlen(NOTIFY_GRACE)) == 0) {
8181 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
8183 dqi->dqi_igrace = str2sec(optarg);
8184 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
8186 "%s: bad inode-grace: %s\n",
8191 dqb->dqb_valid |= QIF_ITIME;
8197 if (lfs_verify_poolarg(optarg))
8199 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8200 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
8202 case 't': /* Yes, of course! */
8207 if (qctl->qc_type != ALLQUOTA) {
8209 "%s: -u/g/p cannot be used more than once\n",
8213 qctl->qc_type = qtype;
8215 /* getopt prints error message for us when opterr != 0 */
8217 fprintf(stderr, "%s: unrecognized option '%s'\n",
8218 progname, argv[optind - 1]);
8225 if (qctl->qc_type == ALLQUOTA) {
8226 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
8231 if (optind != argc - 1) {
8232 fprintf(stderr, "%s: unexpected parameter '%s'\n",
8233 progname, argv[optind + 1]);
8238 rc = llapi_quotactl(mnt, qctl);
8241 fprintf(stderr, "%s %s ", obd_type,
8242 obd_uuid2str(&qctl->obd_uuid));
8243 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8250 static int lfs_reset_quota(char *mnt, struct if_quotactl *qctl)
8252 struct if_quotactl tmp_qctl;
8253 int index, md_count, dt_count;
8254 int wait_phase = 0, wait_index = 0, wait_count = 0;
8257 /* reset the quota ID, the existing quota setting will be returned */
8258 rc = llapi_quotactl(mnt, qctl);
8263 if ((qctl->qc_dqblk.dqb_valid & QIF_LIMITS) != QIF_LIMITS) {
8265 "the existing quota settings are not returned!\n");
8269 rc = llapi_get_obd_count(mnt, &md_count, 1);
8271 fprintf(stderr, "can not get mdt count: %s\n", strerror(-rc));
8275 rc = llapi_get_obd_count(mnt, &dt_count, 0);
8277 fprintf(stderr, "can not get ost count: %s\n", strerror(-rc));
8281 memset(&tmp_qctl, 0, sizeof(tmp_qctl));
8282 tmp_qctl.qc_type = qctl->qc_type;
8283 tmp_qctl.qc_id = qctl->qc_id;
8284 tmp_qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
8287 if (wait_phase == 0) {
8288 for (index = wait_index; index < md_count; index++) {
8289 tmp_qctl.qc_idx = index;
8290 tmp_qctl.qc_valid = QC_MDTIDX;
8291 rc = llapi_quotactl(mnt, &tmp_qctl);
8292 if (rc == -ENODEV || rc == -ENODATA)
8295 fprintf(stderr, "quotactl mdt%d failed: %s\n",
8296 index, strerror(-rc));
8299 /* check whether the md quota grant is reset */
8300 if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
8301 tmp_qctl.qc_dqblk.dqb_ihardlimit != 0)
8305 if (index < md_count) {
8311 for (index = wait_index; index < dt_count; index++) {
8312 tmp_qctl.qc_idx = index;
8313 tmp_qctl.qc_valid = QC_OSTIDX;
8314 rc = llapi_quotactl(mnt, &tmp_qctl);
8315 if (rc == -ENODEV || rc == -ENODATA)
8318 fprintf(stderr, "quotactl mdt%d failed: %s\n",
8319 index, strerror(-rc));
8322 /* check whether the dt quota grant is reset */
8323 if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
8324 tmp_qctl.qc_dqblk.dqb_bhardlimit != 0)
8328 if (index < dt_count) {
8335 if (wait_phase == 0) {
8343 if (rc || wait_count > 30) {
8344 fprintf(stderr, "fail to reset the quota ID %d on OBDs\n",
8351 fprintf(stdout, "wait %d seconds for OBDs to reset the quota ID %u\n",
8352 wait_count, qctl->qc_id);
8357 /* restore the quota setting */
8358 if (qctl->qc_dqblk.dqb_isoftlimit == 0 &&
8359 qctl->qc_dqblk.dqb_ihardlimit == 0 &&
8360 qctl->qc_dqblk.dqb_bsoftlimit == 0 &&
8361 qctl->qc_dqblk.dqb_bhardlimit == 0)
8364 memcpy(&tmp_qctl, qctl, sizeof(tmp_qctl));
8365 tmp_qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
8366 rc2 = llapi_quotactl(mnt, &tmp_qctl);
8371 "fail to restore the quota setting: %s, please restore it manually by\n lfs setquota %s %d",
8373 qctl->qc_type == USRQUOTA ? "-u" :
8374 (qctl->qc_type == GRPQUOTA ? "-g" : "-p"),
8377 if (qctl->qc_dqblk.dqb_isoftlimit != 0)
8378 fprintf(stderr, " -i %llu",
8379 (unsigned long long)qctl->qc_dqblk.dqb_isoftlimit);
8380 if (qctl->qc_dqblk.dqb_ihardlimit != 0)
8381 fprintf(stderr, " -I %llu",
8382 (unsigned long long)qctl->qc_dqblk.dqb_ihardlimit);
8383 if (qctl->qc_dqblk.dqb_bsoftlimit != 0)
8384 fprintf(stderr, " -b %llu",
8385 (unsigned long long)qctl->qc_dqblk.dqb_bsoftlimit);
8386 if (qctl->qc_dqblk.dqb_bhardlimit != 0)
8387 fprintf(stderr, " -B %llu",
8388 (unsigned long long)qctl->qc_dqblk.dqb_bhardlimit);
8390 fprintf(stderr, " %s\n", mnt);
8397 #define BSLIMIT (1 << 0)
8398 #define BHLIMIT (1 << 1)
8399 #define ISLIMIT (1 << 2)
8400 #define IHLIMIT (1 << 3)
8402 int lfs_setquota(int argc, char **argv)
8405 struct if_quotactl *qctl;
8406 char *mnt, *obd_type;
8407 struct obd_dqblk *dqb;
8408 struct option long_opts[] = {
8409 { .val = 'b', .name = "block-softlimit",
8410 .has_arg = required_argument },
8411 { .val = 'B', .name = "block-hardlimit",
8412 .has_arg = required_argument },
8413 { .val = 'd', .name = "default", .has_arg = no_argument },
8414 { .val = LFS_SETQUOTA_DELETE,
8415 .name = "delete", .has_arg = no_argument },
8416 { .val = 'g', .name = "group", .has_arg = required_argument },
8417 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
8418 { .val = 'h', .name = "help", .has_arg = no_argument },
8419 { .val = 'i', .name = "inode-softlimit",
8420 .has_arg = required_argument },
8421 { .val = 'I', .name = "inode-hardlimit",
8422 .has_arg = required_argument },
8423 { .val = 'p', .name = "projid", .has_arg = required_argument },
8424 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
8425 { .val = 'r', .name = "reset", .has_arg = no_argument },
8426 { .val = 'u', .name = "user", .has_arg = required_argument },
8427 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
8428 { .val = LFS_POOL_OPT,
8429 .name = "pool", .has_arg = required_argument },
8431 unsigned int limit_mask = 0;
8432 bool use_default = false;
8433 int qtype, qctl_len;
8435 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
8436 qctl = malloc(qctl_len);
8440 memset(qctl, 0, qctl_len);
8441 obd_type = (char *)qctl->obd_type;
8442 dqb = &qctl->qc_dqblk;
8444 if (has_times_option(argc, argv)) {
8445 rc = lfs_setquota_times(argc, argv, qctl);
8449 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
8450 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
8451 * so it can be used as a marker that qc_type
8452 * isn't reinitialized from command line
8454 while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pru:U",
8455 long_opts, NULL)) != -1) {
8458 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8461 goto quota_type_def;
8464 rc = name2uid(&qctl->qc_id, optarg);
8467 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8470 goto quota_type_def;
8473 rc = name2gid(&qctl->qc_id, optarg);
8476 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8479 goto quota_type_def;
8482 rc = name2projid(&qctl->qc_id, optarg);
8485 if (str2quotaid(&qctl->qc_id, optarg)) {
8487 "%s setquota: invalid id '%s'\n",
8494 if (qctl->qc_id == 0) {
8496 "%s setquota: can't set quota for root usr/group/project.\n",
8503 if (qctl->qc_type != ALLQUOTA) {
8505 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
8510 qctl->qc_type = qtype;
8512 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 22, 53, 0)
8515 "%s setquota: '-d' deprecated, use '-D' or '--default'\n",
8521 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8523 case LFS_SETQUOTA_DELETE:
8524 qctl->qc_cmd = LUSTRE_Q_DELETEQID;
8527 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
8528 dqb->dqb_bsoftlimit >>= 10;
8529 limit_mask |= BSLIMIT;
8530 if (dqb->dqb_bsoftlimit &&
8531 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
8533 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8535 (unsigned long long)dqb->dqb_bsoftlimit,
8539 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
8540 dqb->dqb_bhardlimit >>= 10;
8541 limit_mask |= BHLIMIT;
8542 if (dqb->dqb_bhardlimit &&
8543 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
8545 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
8546 "See '%s help setquota' or Lustre manual for details\n",
8548 (unsigned long long)dqb->dqb_bhardlimit,
8552 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
8553 limit_mask |= ISLIMIT;
8554 if (dqb->dqb_isoftlimit &&
8555 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
8557 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8559 (unsigned long long)dqb->dqb_isoftlimit,
8563 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
8564 limit_mask |= IHLIMIT;
8565 if (dqb->dqb_ihardlimit &&
8566 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
8568 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8570 (unsigned long long)dqb->dqb_ihardlimit,
8574 if (lfs_verify_poolarg(optarg)) {
8578 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8579 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
8580 LUSTRE_Q_SETDEFAULT_POOL :
8581 LUSTRE_Q_SETQUOTAPOOL;
8584 qctl->qc_cmd = LUSTRE_Q_RESETQID;
8588 "%s setquota: unrecognized option '%s'\n",
8589 progname, argv[optind - 1]);
8597 if (qctl->qc_type == ALLQUOTA) {
8599 "%s setquota: either -u or -g must be specified\n",
8605 if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
8606 qctl->qc_cmd != LUSTRE_Q_RESETQID && limit_mask == 0) {
8608 "%s setquota: at least one limit must be specified\n",
8614 if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8615 qctl->qc_cmd == LUSTRE_Q_RESETQID) && limit_mask != 0) {
8617 "%s setquota: limits should not be specified when using default quota, deleting or resetting quota ID\n",
8623 if (use_default && qctl->qc_id == 0) {
8625 "%s setquota: can not set default quota for root user/group/project\n",
8631 if ((qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8632 qctl->qc_cmd == LUSTRE_Q_RESETQID) && qctl->qc_id == 0) {
8634 "%s setquota: can not delete or reset root user/group/project\n",
8640 if (optind != argc - 1) {
8642 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
8643 progname, argv[optind]);
8651 dqb->dqb_bhardlimit = 0;
8652 dqb->dqb_bsoftlimit = 0;
8653 dqb->dqb_ihardlimit = 0;
8654 dqb->dqb_isoftlimit = 0;
8657 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
8658 /* do not set inode limits for Pool Quotas */
8659 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
8660 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
8661 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
8662 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
8663 /* sigh, we can't just set blimits/ilimits */
8664 struct if_quotactl *tmp_qctl;
8666 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8670 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
8671 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8672 strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
8675 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8677 tmp_qctl->qc_type = qctl->qc_type;
8678 tmp_qctl->qc_id = qctl->qc_id;
8680 rc = llapi_quotactl(mnt, tmp_qctl);
8686 if (!(limit_mask & BHLIMIT))
8687 dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
8688 if (!(limit_mask & BSLIMIT))
8689 dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
8690 if (!(limit_mask & IHLIMIT))
8691 dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
8692 if (!(limit_mask & ISLIMIT))
8693 dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
8695 /* Keep grace times if we have got no softlimit arguments */
8696 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
8697 dqb->dqb_valid |= QIF_BTIME;
8698 dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
8701 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
8702 dqb->dqb_valid |= QIF_ITIME;
8703 dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
8708 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
8709 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
8711 if (qctl->qc_cmd == LUSTRE_Q_RESETQID)
8712 rc = lfs_reset_quota(mnt, qctl);
8714 rc = llapi_quotactl(mnt, qctl);
8719 "%s setquota: cannot quotactl '%s' '%s': %s\n",
8721 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
8724 "%s setquota: quotactl failed: %s\n",
8725 progname, strerror(-rc));
8729 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8735 /* Converts seconds value into format string
8736 * result is returned in buf
8738 * 1. result is in descenting order: 1w2d3h4m5s
8739 * 2. zero fields are not filled (except for p. 3): 5d1s
8740 * 3. zero seconds value is presented as "0s"
8742 static char *__sec2str(time_t seconds, char *buf)
8744 const char spec[] = "smhdw";
8745 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8750 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8751 c = seconds / mult[i];
8753 if (c > 0 || (i == 0 && buf == tail))
8754 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8763 static void sec2str(time_t seconds, char *buf, int rc)
8770 tail = __sec2str(seconds, tail);
8772 if (rc && tail - buf < 39) {
8778 static void diff2str(time_t seconds, char *buf, time_t now)
8783 if (seconds <= now) {
8784 strcpy(buf, "expired");
8787 __sec2str(seconds - now, buf);
8790 static void print_quota_title(char *name, struct if_quotactl *qctl,
8791 bool human_readable, bool show_default)
8794 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8795 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8796 "Filesystem", "bquota", "blimit", "bgrace",
8797 "iquota", "ilimit", "igrace");
8799 printf("Disk quotas for %s %s (%cid %u):\n",
8800 qtype_name(qctl->qc_type), name,
8801 *qtype_name(qctl->qc_type), qctl->qc_id);
8802 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8803 "Filesystem", human_readable ? "used" : "kbytes",
8804 "quota", "limit", "grace",
8805 "files", "quota", "limit", "grace");
8809 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8812 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8815 snprintf(buf, buflen, "%5.4gP",
8816 (double)num / ((__u64)1 << 40));
8818 snprintf(buf, buflen, "%5.4gT",
8819 (double)num / (1 << 30));
8821 snprintf(buf, buflen, "%5.4gG",
8822 (double)num / (1 << 20));
8824 snprintf(buf, buflen, "%5.4gM",
8825 (double)num / (1 << 10));
8827 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8831 #ifdef HAVE_NATIVE_CLIENT
8832 /* In the current Lustre implementation, the grace time is either the time
8833 * or the timestamp to be used after some quota ID exceeds the soft limt,
8834 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8836 #define LQUOTA_GRACE_BITS 48
8837 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8838 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8839 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8840 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8841 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8844 #define STRBUF_LEN 24
8845 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8846 int rc, bool h, bool show_default, bool show_qid)
8853 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8854 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8855 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8856 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8857 int bover = 0, iover = 0;
8858 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8859 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8861 char strbuf[STRBUF_LEN];
8863 dqb->dqb_btime &= LQUOTA_GRACE_MASK;
8864 dqb->dqb_itime &= LQUOTA_GRACE_MASK;
8866 if (dqb->dqb_bhardlimit &&
8867 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8869 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8870 if (dqb->dqb_btime > now)
8876 if (dqb->dqb_ihardlimit &&
8877 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8879 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8880 if (dqb->dqb_itime > now)
8887 if (qctl->qc_type == USRQUOTA) {
8888 if (uid2name(&name, qctl->qc_id))
8889 printf("%10u", qctl->qc_id);
8891 printf("%10s", name);
8892 } else if (qctl->qc_type == GRPQUOTA) {
8893 if (gid2name(&name, qctl->qc_id))
8894 printf("%10u", qctl->qc_id);
8896 printf("%10s", name);
8898 printf("%10u", qctl->qc_id);
8901 if (strlen(mnt) > 15)
8902 printf("%s\n%15s", mnt, "");
8904 printf("%15s", mnt);
8908 snprintf(timebuf, sizeof(timebuf), "%llu",
8909 (unsigned long long)dqb->dqb_btime);
8911 diff2str(dqb->dqb_btime, timebuf, now);
8913 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8914 strbuf, sizeof(strbuf), h);
8915 if (rc == -EREMOTEIO)
8916 sprintf(numbuf[0], "%s*", strbuf);
8918 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8919 "%s" : "[%s]", strbuf);
8921 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8922 if (type == QC_GENERAL)
8923 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8924 "%s" : "[%s]", strbuf);
8926 sprintf(numbuf[1], "%s", "-");
8928 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8929 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8930 "%s" : "[%s]", strbuf);
8933 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8935 printf(" %7s%c %6s %7s %7s",
8936 numbuf[0], bover ? '*' : ' ', numbuf[1],
8937 numbuf[2], bover > 1 ? timebuf : "-");
8940 snprintf(timebuf, sizeof(timebuf), "%llu",
8941 (unsigned long long)dqb->dqb_itime);
8943 diff2str(dqb->dqb_itime, timebuf, now);
8945 snprintf(numbuf[0], sizeof(numbuf),
8946 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8947 (uintmax_t)dqb->dqb_curinodes);
8949 if (type == QC_GENERAL)
8950 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8952 (uintmax_t)dqb->dqb_isoftlimit);
8954 sprintf(numbuf[1], "%s", "-");
8956 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8957 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8960 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8961 else if (type != QC_OSTIDX)
8962 printf(" %7s%c %6s %7s %7s",
8963 numbuf[0], iover ? '*' : ' ', numbuf[1],
8964 numbuf[2], iover > 1 ? timebuf : "-");
8966 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8968 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
8969 qctl->qc_cmd == LUSTRE_Q_GETINFOPOOL ||
8970 qctl->qc_cmd == Q_GETOINFO) {
8974 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8975 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8977 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8978 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8979 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8981 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8983 printf("Block grace time: %s; Inode grace time: %s\n",
8984 bgtimebuf, igtimebuf);
8988 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8992 /* format is "lustre-OST0001" */
8993 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8995 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
9000 *idx = strtoul(dash, &endp, 16);
9001 if (*idx > LOV_V1_INSANE_STRIPE_COUNT) {
9002 fprintf(stderr, "wrong index %s\n", tgtname);
9009 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
9010 bool h, __u64 *total)
9012 int rc = 0, rc1 = 0, count = 0, i = 0;
9013 char **list = NULL, *buffer = NULL;
9014 __u32 valid = qctl->qc_valid;
9016 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
9019 /* Is it correct for the case OST0000, OST0002, OST0003 -
9020 * we will ask OST0001 that is absent and won't ask OST0003? */
9021 rc = llapi_get_obd_count(mnt, &count, is_mdt);
9023 fprintf(stderr, "can not get %s count: %s\n",
9024 is_mdt ? "mdt" : "ost", strerror(-rc));
9028 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
9029 char fname[PATH_MAX];
9030 char fsname[LUSTRE_MAXFSNAME + 1];
9031 int bufsize = sizeof(struct obd_uuid) * count;
9033 rc = llapi_search_fsname(mnt, fsname);
9035 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
9039 buffer = malloc(bufsize + sizeof(*list) * count);
9042 list = (char **)(buffer + bufsize);
9043 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
9044 count = llapi_get_poolmembers(fname, list, count,
9050 for (i = 0; i < count; i++) {
9051 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
9054 if (tgt_name2index(list[i], &index))
9056 qctl->qc_idx = index;
9061 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
9062 rc = llapi_quotactl(mnt, qctl);
9064 /* It is remote client case. */
9065 if (rc == -EOPNOTSUPP) {
9070 /* no target for this index yet */
9071 if (rc == -ENODEV) {
9076 /* inactive target */
9077 if (rc == -ENODATA) {
9078 char name[UUID_MAX+8];
9080 snprintf(name, sizeof(name), "%s[inact]",
9081 obd_uuid2str(&qctl->obd_uuid));
9082 memset(&qctl->qc_dqinfo, 0,
9083 sizeof(qctl->qc_dqinfo));
9084 memset(&qctl->qc_dqblk, 0,
9085 sizeof(qctl->qc_dqblk));
9086 print_quota(name, qctl, qctl->qc_valid, 0, h,
9094 fprintf(stderr, "quotactl %s%d failed.\n",
9095 is_mdt ? "mdt" : "ost", qctl->qc_idx);
9099 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
9100 qctl->qc_valid, 0, h, false, false);
9101 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
9102 qctl->qc_dqblk.dqb_bhardlimit;
9107 qctl->qc_valid = valid;
9111 static int print_one_quota(char *mnt, char *name, struct if_quotactl *qctl,
9112 int verbose, int quiet, bool human_readable,
9113 bool show_default, bool show_qid, int rc)
9115 int rc1 = 0, rc2 = 0;
9116 char *obd_type = (char *)qctl->obd_type;
9117 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
9118 __u64 total_ialloc = 0, total_balloc = 0;
9121 if (!show_default && qctl->qc_id == 0) {
9122 qctl->qc_dqblk.dqb_bhardlimit = 0;
9123 qctl->qc_dqblk.dqb_bsoftlimit = 0;
9124 qctl->qc_dqblk.dqb_ihardlimit = 0;
9125 qctl->qc_dqblk.dqb_isoftlimit = 0;
9126 qctl->qc_dqblk.dqb_btime = 0;
9127 qctl->qc_dqblk.dqb_itime = 0;
9128 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
9131 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
9132 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT)
9133 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
9135 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
9136 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT)
9137 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
9139 if (!show_qid && (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9140 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
9141 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
9142 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
9143 print_quota_title(name, qctl, human_readable, show_default);
9145 if (rc && *obd_type)
9146 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
9148 if (qctl->qc_valid != QC_GENERAL)
9151 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9152 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
9153 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
9154 (QIF_LIMITS|QIF_USAGE));
9156 print_quota(mnt, qctl, QC_GENERAL, rc, human_readable, show_default,
9159 if (!show_qid && !show_default && verbose &&
9160 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
9161 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
9162 char strbuf[STRBUF_LEN];
9164 rc1 = print_obd_quota(mnt, qctl, 1, human_readable,
9166 rc2 = print_obd_quota(mnt, qctl, 0, human_readable,
9168 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
9170 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
9171 (uintmax_t)total_ialloc, strbuf);
9174 if (!show_qid && (rc || rc1 || rc2 || inacc))
9175 printf("%d Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n", inacc);
9189 static int iter_all_quota(char *mnt, struct if_quotactl *qctl, int quiet,
9190 bool human_readable)
9192 struct if_quotactl qctl_tmp, *qctl_iter;
9193 void *buffer = NULL;
9195 __u64 cur, buflen = 0;
9198 memcpy(&qctl_tmp, qctl, sizeof(struct if_quotactl));
9199 qctl_tmp.qc_cmd = LUSTRE_Q_ITERQUOTA;
9200 rc = llapi_quotactl(mnt, &qctl_tmp);
9204 buflen = qctl_tmp.qc_allquota_count * sizeof(struct if_quotactl);
9205 buffer = malloc(buflen);
9206 if (buffer == NULL) {
9211 mark = qctl_tmp.qc_allquota_mark;
9212 memcpy(&qctl_tmp, qctl, sizeof(struct if_quotactl));
9213 qctl_tmp.qc_cmd = LUSTRE_Q_GETALLQUOTA;
9214 qctl_tmp.qc_allquota_buffer = (__u64)buffer;
9215 qctl_tmp.qc_allquota_buflen = buflen;
9216 qctl_tmp.qc_allquota_mark = mark;
9217 rc = llapi_quotactl(mnt, &qctl_tmp);
9221 printf("Filesystem %s, Disk %s quotas\n", mnt,
9222 qtype_name(qctl->qc_type));
9223 printf("%10s%8s %7s%8s%8s%8s %7s%8s%8s\n", "quota_id",
9224 human_readable ? "used" : "kbytes", "quota", "limit", "grace",
9225 "files", "quota", "limit", "grace");
9228 while (cur < buflen) {
9229 if ((buflen - cur) < sizeof(struct if_quotactl)) {
9234 qctl_iter = buffer + cur;
9235 qctl_iter->qc_cmd = LUSTRE_Q_GETQUOTA;
9236 cur += sizeof(struct if_quotactl);
9238 /* Is no file created for this quota ID yet? */
9239 if ((qctl_iter->qc_dqblk.dqb_valid & QIF_USAGE) != QIF_USAGE)
9240 qctl_iter->qc_dqblk.dqb_valid |= QIF_USAGE;
9242 print_one_quota(mnt, NULL, qctl_iter, 0, quiet,
9243 human_readable, false, true, 0);
9251 fprintf(stderr, "get all quota failed %d\n", rc);
9256 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
9257 int verbose, int quiet, bool human_readable,
9262 rc = llapi_quotactl(mnt, qctl);
9266 fprintf(stderr, "%s quotas are not enabled.\n",
9267 qtype_name(qctl->qc_type));
9270 fprintf(stderr, "Permission denied.\n");
9273 /* We already got error message. */
9276 fprintf(stderr, "Unexpected quotactl error: %s\n",
9283 return print_one_quota(mnt, name, qctl, verbose, quiet, human_readable,
9284 show_default, false, rc);
9287 static int lfs_project(int argc, char **argv)
9289 int ret = 0, err = 0, c, i;
9290 struct project_handle_control phc = { 0 };
9291 enum lfs_project_ops_t op;
9294 phc.assign_projid = false;
9295 /* default action */
9296 op = LFS_PROJECT_LIST;
9298 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
9301 if (op != LFS_PROJECT_LIST) {
9303 "%s: cannot specify '-c' '-C' '-s' together\n",
9308 op = LFS_PROJECT_CHECK;
9311 if (op != LFS_PROJECT_LIST) {
9313 "%s: cannot specify '-c' '-C' '-s' together\n",
9318 op = LFS_PROJECT_CLEAR;
9321 if (op != LFS_PROJECT_LIST) {
9323 "%s: cannot specify '-c' '-C' '-s' together\n",
9328 phc.set_inherit = true;
9329 op = LFS_PROJECT_SET;
9335 phc.keep_projid = true;
9338 phc.recursive = true;
9341 if (str2quotaid(&phc.projid, optarg)) {
9343 "Invalid project ID: %s\n",
9348 phc.assign_projid = true;
9352 phc.newline = false;
9355 fprintf(stderr, "%s: invalid option '%c'\n",
9361 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
9362 op = LFS_PROJECT_SET;
9363 phc.set_projid = true;
9364 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
9365 phc.set_projid = true;
9369 case LFS_PROJECT_CHECK:
9370 if (phc.keep_projid) {
9372 "%s: '-k' is useless together with '-c'\n",
9377 case LFS_PROJECT_CLEAR:
9380 "%s: '-0' is useless together with '-C'\n",
9384 if (phc.assign_projid) {
9386 "%s: '-p' is useless together with '-C'\n",
9391 case LFS_PROJECT_SET:
9394 "%s: '-0' is useless together with '-s'\n",
9398 if (phc.keep_projid) {
9400 "%s: '-k' is useless together with '-s'\n",
9408 "%s: '-0' is useless for list operations\n",
9418 fprintf(stderr, "%s: missing file or directory target(s)\n",
9423 for (i = 0; i < argc; i++) {
9425 case LFS_PROJECT_CHECK:
9426 err = lfs_project_check(argv[i], &phc);
9428 case LFS_PROJECT_LIST:
9429 err = lfs_project_list(argv[i], &phc);
9431 case LFS_PROJECT_CLEAR:
9432 err = lfs_project_clear(argv[i], &phc);
9434 case LFS_PROJECT_SET:
9435 err = lfs_project_set(argv[i], &phc);
9447 static int lfs_quota(int argc, char **argv)
9450 char *mnt, *name = NULL;
9451 struct if_quotactl *qctl;
9452 char *obd_uuid, *endp;
9453 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
9454 __u32 valid = QC_GENERAL;
9456 __u32 start_qid = 0, end_qid = 0;
9457 bool human_readable = false;
9458 bool show_default = false;
9460 bool show_pools = false;
9461 struct option long_opts[] = {
9462 { .val = 'a', .name = "all", .has_arg = required_argument },
9463 { .val = 'e', .name = "end-qid", .has_arg = required_argument },
9464 { .val = 'g', .name = "group", .has_arg = required_argument },
9465 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
9466 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
9467 /* It is unfortunate that '-i' was used for mdt-index, and '-I' for
9468 * ost-index, because '-i' is used for ost-index everywhere else.
9469 * These options have been this way since ancient days, but I suspect
9470 * that they are not often used. Prefer --ost and --mdt instead.
9472 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
9473 { .val = 'm', .name = "mdt", .has_arg = required_argument },
9474 { .val = 'o', .name = "ost-index", .has_arg = required_argument },
9475 { .val = 'o', .name = "ost", .has_arg = required_argument },
9476 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
9477 { .val = 'p', .name = "projid", .has_arg = required_argument },
9478 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
9479 { .val = 'q', .name = "quiet", .has_arg = no_argument },
9480 { .val = 's', .name = "start-qid", .has_arg = required_argument },
9481 { .val = 't', .name = "times", .has_arg = no_argument },
9482 { .val = 'u', .name = "user", .has_arg = required_argument },
9483 { .val = 'U', .name = "default-usr", .has_arg = required_argument },
9484 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9486 char **poollist = NULL;
9490 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
9494 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
9495 qctl->qc_type = ALLQUOTA;
9496 obd_uuid = (char *)qctl->obd_uuid.uuid;
9498 while ((c = getopt_long(argc, argv, "ae:gGhi:I:m:o:pPqs:tuUv",
9499 long_opts, NULL)) != -1) {
9502 qctl->qc_cmd = LUSTRE_Q_ITERQUOTA;
9505 end_qid = strtoul(optarg, NULL, 0);
9508 show_default = true;
9514 human_readable = true;
9516 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 22, 53, 0)
9519 "'-i' deprecated, use '--ost' or '--mdt'\n");
9524 idx = strtol(optarg, &endp, 0);
9525 if (errno != 0 || idx > LOV_V1_INSANE_STRIPE_COUNT ||
9526 idx < 0 || *endp != '\0') {
9528 "%s quota: invalid MDT index '%s'\n",
9533 valid = qctl->qc_valid = QC_MDTIDX;
9536 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 22, 53, 0)
9538 fprintf(stderr, "'-I' deprecated, use '--ost'\n");
9543 idx = strtol(optarg, &endp, 0);
9544 /* simple digit, treat it as a numerical OST index */
9545 if (*endp == '\0') {
9546 if (idx > LOV_V1_INSANE_STRIPE_COUNT ||
9549 "%s quota: invalid OST index '%s'\n",
9554 valid = qctl->qc_valid = QC_OSTIDX;
9559 /* need to also handle a UUID for compatibility */
9560 valid = qctl->qc_valid = QC_UUID;
9561 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
9564 show_default = true;
9570 if ((!optarg) && (argv[optind] != NULL) &&
9571 (argv[optind][0] != '-') &&
9572 (argv[optind][0] != '/')) {
9573 optarg = argv[optind++];
9574 if (lfs_verify_poolarg(optarg)) {
9578 strncpy(qctl->qc_poolname, optarg,
9580 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
9581 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
9583 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9587 /* optarg is NULL */
9589 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9595 start_qid = strtoul(optarg, NULL, 0);
9598 qctl->qc_cmd = LUSTRE_Q_GETINFO;
9601 show_default = true;
9606 if (qctl->qc_type != ALLQUOTA) {
9608 "%s quota: only one of -u, -g, or -p may be specified\n",
9613 qctl->qc_type = qtype;
9619 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
9620 progname, argv[optind - 1]);
9626 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
9627 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9628 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
9629 qctl->qc_type == ALLQUOTA &&
9630 optind == argc - 1 && !show_default) {
9633 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
9634 qctl->qc_type = qtype;
9635 qctl->qc_valid = valid;
9636 if (qtype == USRQUOTA) {
9637 qctl->qc_id = geteuid();
9638 rc = uid2name(&name, qctl->qc_id);
9640 qctl->qc_id = getegid();
9641 rc = gid2name(&name, qctl->qc_id);
9642 memset(&qctl->qc_dqblk, 0,
9643 sizeof(qctl->qc_dqblk));
9648 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
9649 human_readable, show_default);
9654 /* lfs quota -u username /path/to/lustre/mount */
9655 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9656 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
9657 /* options should be followed by u/g-name and mntpoint */
9658 if ((!show_default && optind + 2 != argc) ||
9659 (show_default && optind + 1 != argc) ||
9660 qctl->qc_type == ALLQUOTA) {
9662 "%s quota: name and mount point must be specified\n",
9668 if (!show_default) {
9669 name = argv[optind++];
9670 switch (qctl->qc_type) {
9672 rc = name2uid(&qctl->qc_id, name);
9675 rc = name2gid(&qctl->qc_id, name);
9678 rc = name2projid(&qctl->qc_id, name);
9685 qctl->qc_valid = QC_GENERAL;
9686 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
9687 LUSTRE_Q_GETDEFAULT_POOL :
9688 LUSTRE_Q_GETDEFAULT;
9693 if (str2quotaid(&qctl->qc_id, name)) {
9694 fprintf(stderr, "%s quota: invalid id '%s'\n",
9700 } else if (qctl->qc_cmd == LUSTRE_Q_ITERQUOTA) {
9701 if (optind + 1 != argc) {
9703 "%s quota: mount point must be specified\n",
9709 if (qctl->qc_type == ALLQUOTA) {
9710 fprintf(stderr, "%s quota: no quota type to iterate\n",
9716 if (end_qid != 0 && start_qid > end_qid) {
9718 "%s quota: end qid is smaller than start qid\n",
9724 qctl->qc_allquota_qid_start = start_qid;
9725 qctl->qc_allquota_qid_end = end_qid;
9726 rc = iter_all_quota(argv[optind], qctl, quiet, human_readable);
9728 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
9729 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
9740 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
9744 for (i = 0; i < poolcount; i++) {
9745 p = memchr(poollist[i], '.', MAXNAMLEN);
9747 fprintf(stderr, "bad string format %.*s\n",
9748 MAXNAMLEN, poollist[i]);
9753 printf("Quotas for pool: %s\n", p);
9754 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
9755 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9756 human_readable, show_default);
9763 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9764 human_readable, show_default);
9770 #endif /* HAVE_SYS_QUOTA_H! */
9772 static int flushctx_ioctl(char *mp)
9776 fd = open(mp, O_RDONLY);
9778 fprintf(stderr, "flushctx: error open %s: %s\n",
9779 mp, strerror(errno));
9783 rc = ioctl(fd, LL_IOC_FLUSHCTX);
9785 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
9786 mp, strerror(errno));
9792 static int lfs_flushctx(int argc, char **argv)
9794 int kdestroy = 0, reap = 0, c;
9795 char **mnts = NULL, **mnt_ptr;
9796 int mnt_num = 1, index = 0, rc = 0, rc2;
9797 extern char **environ;
9799 while ((c = getopt(argc, argv, "kr")) != -1) {
9809 "error: %s: option '-%c' unrecognized\n",
9815 if (optind >= argc) {
9816 /* flush for all lustre mount points */
9818 mnt_ptr = realloc(mnts, mnt_num * sizeof(char *));
9825 mnts[mnt_num - 1] = (char *)calloc(PATH_MAX + 1, sizeof(char));
9826 if (!mnts[mnt_num - 1]) {
9831 if (!llapi_search_mounts(NULL, index++,
9832 mnts[mnt_num - 1], NULL)) {
9834 if (*mnts[mnt_num - 1] == '\0')
9839 *mnts[mnt_num - 1] = '\0';
9845 /* flush for mounts as specified on command line */
9846 mnt_ptr = argv + optind;
9847 mnt_num = argc - optind;
9850 for (index = 0; index < mnt_num; index++) {
9851 /* Check if we have a mount point */
9852 if (*mnt_ptr[index] == '\0')
9855 rc2 = flushctx_ioctl(mnt_ptr[index]);
9859 "error flushing contexts on mount point %s: %s\n",
9860 mnt_ptr[index], strerror(errno));
9867 static char *args[] = { "keyctl", "reap", NULL };
9869 /* use callvpe to bypass the shell */
9870 rc2 = callvpe("keyctl", args, environ);
9872 rc2 = WEXITSTATUS(rc2);
9873 fprintf(stderr, "error reaping keyring: %d\n", rc2);
9878 static char *args[] = { "kdestroy", NULL };
9880 /* use callvpe to bypass the shell */
9881 rc2 = callvpe("kdestroy", args, environ);
9883 rc2 = WEXITSTATUS(rc2);
9884 fprintf(stderr, "error destroying tickets: %d\n", rc2);
9889 for (index = 0; index < mnt_num; index++)
9896 static int lfs_changelog(int argc, char **argv)
9898 void *changelog_priv;
9899 struct changelog_rec *rec;
9900 long long startrec = 0, endrec = 0;
9902 struct option long_opts[] = {
9903 { .val = 'f', .name = "follow", .has_arg = no_argument },
9905 char short_opts[] = "f";
9908 while ((rc = getopt_long(argc, argv, short_opts,
9909 long_opts, NULL)) != -1) {
9916 "%s changelog: unrecognized option '%s'\n",
9917 progname, argv[optind - 1]);
9921 if (optind >= argc) {
9922 fprintf(stderr, "%s changelog: mdtname must be specified\n",
9927 mdd = argv[optind++];
9928 if (argc > optind) {
9930 startrec = strtoll(argv[optind++], NULL, 10);
9931 if (errno != 0 || startrec < 0) {
9933 "%s changelog: bad startrec\n",
9939 if (argc > optind) {
9941 endrec = strtoll(argv[optind++], NULL, 10);
9942 if (errno != 0 || endrec < 0) {
9944 "%s changelog: bad endrec\n",
9950 rc = llapi_changelog_start(&changelog_priv,
9951 CHANGELOG_FLAG_BLOCK |
9952 CHANGELOG_FLAG_JOBID |
9953 CHANGELOG_FLAG_EXTRA_FLAGS |
9954 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
9957 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
9958 progname, strerror(errno = -rc));
9962 rc = llapi_changelog_set_xflags(changelog_priv,
9963 CHANGELOG_EXTRA_FLAG_UIDGID |
9964 CHANGELOG_EXTRA_FLAG_NID |
9965 CHANGELOG_EXTRA_FLAG_OMODE |
9966 CHANGELOG_EXTRA_FLAG_XATTR);
9969 "%s changelog: cannot set xflags for changelog: %s\n",
9970 progname, strerror(errno = -rc));
9974 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9978 if (endrec && rec->cr_index > endrec) {
9979 llapi_changelog_free(&rec);
9982 if (rec->cr_index < startrec) {
9983 llapi_changelog_free(&rec);
9987 secs = rec->cr_time >> 30;
9988 gmtime_r(&secs, &ts);
9989 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9990 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9991 changelog_type2str(rec->cr_type),
9992 ts.tm_hour, ts.tm_min, ts.tm_sec,
9993 (int)(rec->cr_time & ((1 << 30) - 1)),
9994 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9995 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9997 if (rec->cr_flags & CLF_JOBID) {
9998 struct changelog_ext_jobid *jid =
9999 changelog_rec_jobid(rec);
10001 if (jid->cr_jobid[0] != '\0')
10002 printf(" j=%s", jid->cr_jobid);
10005 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
10006 struct changelog_ext_extra_flags *ef =
10007 changelog_rec_extra_flags(rec);
10009 printf(" ef=0x%llx",
10010 (unsigned long long)ef->cr_extra_flags);
10012 if (ef->cr_extra_flags & CLFE_UIDGID) {
10013 struct changelog_ext_uidgid *uidgid =
10014 changelog_rec_uidgid(rec);
10016 printf(" u=%llu:%llu",
10017 (unsigned long long)uidgid->cr_uid,
10018 (unsigned long long)uidgid->cr_gid);
10020 if (ef->cr_extra_flags & CLFE_NID) {
10021 struct changelog_ext_nid *nid =
10022 changelog_rec_nid(rec);
10025 libcfs_nid2str(nid->cr_nid));
10028 if (ef->cr_extra_flags & CLFE_OPEN) {
10029 struct changelog_ext_openmode *omd =
10030 changelog_rec_openmode(rec);
10031 char mode[] = "---";
10033 /* exec mode must be exclusive */
10034 if (omd->cr_openflags & MDS_FMODE_EXEC) {
10037 if (omd->cr_openflags & MDS_FMODE_READ)
10039 if (omd->cr_openflags &
10046 if (strcmp(mode, "---") != 0)
10047 printf(" m=%s", mode);
10050 if (ef->cr_extra_flags & CLFE_XATTR) {
10051 struct changelog_ext_xattr *xattr =
10052 changelog_rec_xattr(rec);
10054 if (xattr->cr_xattr[0] != '\0')
10055 printf(" x=%s", xattr->cr_xattr);
10059 if (!fid_is_zero(&rec->cr_pfid))
10060 printf(" p="DFID, PFID(&rec->cr_pfid));
10061 if (rec->cr_namelen)
10062 printf(" %.*s", rec->cr_namelen,
10063 changelog_rec_name(rec));
10065 if (rec->cr_flags & CLF_RENAME) {
10066 struct changelog_ext_rename *rnm =
10067 changelog_rec_rename(rec);
10069 if (!fid_is_zero(&rnm->cr_sfid))
10070 printf(" s="DFID" sp="DFID" %.*s",
10071 PFID(&rnm->cr_sfid),
10072 PFID(&rnm->cr_spfid),
10073 (int)changelog_rec_snamelen(rec),
10074 changelog_rec_sname(rec));
10078 llapi_changelog_free(&rec);
10081 llapi_changelog_fini(&changelog_priv);
10084 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
10085 progname, strerror(errno = -rc));
10087 return (rc == 1 ? 0 : rc);
10090 static int lfs_changelog_clear(int argc, char **argv)
10099 endrec = strtoll(argv[3], NULL, 10);
10100 if (errno != 0 || endrec < 0) {
10102 "%s: bad endrec '%s'\n",
10107 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
10110 fprintf(stderr, "%s: record out of range: %llu\n",
10112 else if (rc == -ENOENT)
10113 fprintf(stderr, "%s: no changelog user: %s\n",
10116 fprintf(stderr, "%s error: %s\n", argv[0],
10125 static void rstripc(char *str, int c)
10127 char *end = str + strlen(str);
10129 for (; str < end && end[-1] == c; --end)
10133 /* Helper function to lfs_fid2path. To print out only the file names and
10134 * not the full path. Do not call OBD_IOC_FID2PATH for every file. Instead
10135 * read the trusted.link xattr and loop over all the records to get all the
10138 static int lfs_fid2path_prn_name(char *mnt_dir, char *path_buf,
10139 bool print_linkno, bool print_fid, char *ptr,
10140 const char *fid_str, int linktmp)
10142 char buf[65536]; /* BUFFER_SIZE 65536 */
10143 char full_path[PATH_MAX * 2 + 2];
10144 struct link_ea_header *leh;
10145 struct link_ea_entry *lee;
10147 int reclen, i, rc = 0;
10149 /* Generate full_path */
10150 snprintf(full_path, sizeof(full_path) - 1, "%s/%s", mnt_dir, path_buf);
10152 size = getxattr(full_path, "trusted.link", buf, sizeof(buf));
10154 fprintf(stderr, "%s: failed to read %s xattr: %s\n", path_buf,
10155 "trusted.link", strerror(errno));
10160 leh = (struct link_ea_header *)buf;
10162 if (leh->leh_magic == __swab32(LINK_EA_MAGIC))
10163 leh->leh_reccount = __swab32(leh->leh_reccount);
10165 lee = (struct link_ea_entry *)(leh + 1);
10167 for (i = 0; i < leh->leh_reccount; i++) {
10168 reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
10170 /* handle -n -l case */
10171 if (print_linkno) {
10172 ptr = strrchr(path_buf, '/');
10178 if (strcmp(ptr, lee->lee_name) == 0) {
10180 printf("%s ", fid_str);
10182 printf("%d ", linktmp);
10183 printf("%s\n", lee->lee_name);
10188 printf("%s ", fid_str);
10189 printf("%s\n", lee->lee_name);
10192 /* Get next record */
10193 lee = (struct link_ea_entry *)((char *)lee + reclen);
10199 static int lfs_fid2path(int argc, char **argv)
10201 struct option long_opts[] = {
10202 { .val = '0', .name = "print0", .has_arg = no_argument },
10203 { .val = 'c', .name = "cur", .has_arg = no_argument },
10204 { .val = 'c', .name = "current", .has_arg = no_argument },
10205 { .val = 'c', .name = "print-link", .has_arg = no_argument },
10206 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
10207 { .val = 'l', .name = "link", .has_arg = required_argument },
10208 { .val = 'n', .name = "name", .has_arg = no_argument },
10209 { .name = NULL } };
10210 char short_opts[] = "0cfl:pr:n";
10211 bool print_only_fname = false;
10212 bool print_linkno = false;
10213 bool print_link = false;
10214 bool print_fid = false;
10215 bool print_mnt_dir;
10216 char mnt_dir[PATH_MAX] = "";
10218 char *path_or_fsname;
10219 long long recno = -1;
10221 char *endptr = NULL;
10222 char link_separator = '\n';
10227 while ((c = getopt_long(argc, argv, short_opts,long_opts, NULL)) !=
10231 link_separator = '\0';
10241 linkno = strtol(optarg, &endptr, 10);
10242 if (errno != 0 || *endptr != '\0' || linkno < 0) {
10244 "%s fid2path: invalid linkno '%s'\n",
10248 print_linkno = true;
10251 /* Bypass the full parent path if true
10252 * only print the final filename */
10253 print_only_fname = true;
10256 /* recno is something to do with changelogs
10257 * that was never implemented. We just pass it
10258 * through for the MDT to ignore.
10261 recno = strtoll(optarg, &endptr, 10);
10262 if (errno != 0 || *endptr != '\0' || recno < 0) {
10264 "%s fid2path: invalid recno '%s'\n",
10271 "%s fid2path: unrecognized option '%s'\n",
10272 progname, argv[optind - 1]);
10277 if (argc - optind < 2) {
10279 "Usage: %s fid2path FSNAME|ROOT FID...\n",
10284 path_or_fsname = argv[optind];
10286 if (*path_or_fsname == '/') {
10287 print_mnt_dir = true;
10288 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
10290 print_mnt_dir = false;
10291 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
10296 "%s fid2path: cannot resolve mount point for '%s': %s\n",
10297 progname, path_or_fsname, strerror(-rc));
10301 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
10304 "%s fid2path: cannot open mount point for '%s': %s\n",
10305 progname, path_or_fsname, strerror(-rc));
10309 /* Strip trailing slashes from mnt_dir. */
10310 rstripc(mnt_dir + 1, '/');
10312 for (i = optind + 1; i < argc; i++) {
10313 const char *fid_str = argv[i];
10318 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
10321 "%s fid2path: invalid FID '%s'\n",
10322 progname, fid_str);
10329 int linktmp = (linkno >= 0) ? linkno : 0;
10332 int oldtmp = linktmp;
10333 long long rectmp = recno;
10334 char path_buf[PATH_MAX];
10336 rc2 = llapi_fid2path_at(mnt_fd, &fid, path_buf,
10337 sizeof(path_buf), &rectmp,
10341 "%s fid2path: cannot find %s %s: %s\n",
10342 progname, path_or_fsname, fid_str,
10349 if (print_only_fname && !print_link) {
10350 /* '-n' is passed as option here.
10351 * For all other cases of -c fall back
10352 * to default(else) path as to get the link
10353 * count associated with the file name call
10354 * to OBD_IOC_FID2PATH is required
10356 rc = lfs_fid2path_prn_name(mnt_dir,
10361 /* llapi_fid2path_at() is already called once
10362 * in this case. No need to call it again.
10363 * Break out as we have all the filenames.
10369 printf("%s ", fid_str);
10372 printf("%d ", linktmp);
10374 /* You may think this looks wrong or weird (and it is!)
10375 * but we are actually trying to preserve the old quirky
10376 * behaviors (enforced by our old quirky tests!) that
10377 * make lfs so much fun to work on:
10379 * lustre 0x200000007:0x1:0x0 => "/"
10380 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
10382 * Note that llapi_fid2path() returns "" for the root
10384 if (!print_only_fname) {
10386 print_mnt_dir ? mnt_dir : "",
10387 (print_mnt_dir || *path_buf == '\0') ?
10388 "/" : "", path_buf, link_separator);
10390 ptr = strrchr(path_buf, '/');
10392 printf("%s\n", path_buf);
10394 printf("%s\n", ptr + 1);
10398 /* specified linkno */
10401 if (oldtmp == linktmp)
10402 /* no more links */
10413 static int lfs_path2fid(int argc, char **argv)
10415 struct option long_opts[] = {
10416 { .val = 'p', .name = "parents", .has_arg = no_argument },
10417 { .name = NULL } };
10419 const char short_opts[] = "p";
10420 const char *sep = "";
10423 bool show_parents = false;
10425 while ((rc = getopt_long(argc, argv, short_opts,
10426 long_opts, NULL)) != -1) {
10429 show_parents = true;
10433 "%s path2fid: unrecognized option '%s'\n",
10434 progname, argv[optind - 1]);
10439 if (optind > argc - 1) {
10440 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
10443 } else if (optind < argc - 1) {
10448 for (path = argv + optind; optind < argc; path++, optind++) {
10451 if (!show_parents) {
10452 err = llapi_path2fid(*path, &fid);
10454 printf("%s%s"DFID"\n",
10455 *sep != '\0' ? *path : "", sep,
10458 char name[NAME_MAX + 1];
10459 unsigned int linkno = 0;
10461 while ((err = llapi_path2parent(*path, linkno, &fid,
10462 name, sizeof(name))) == 0) {
10463 if (*sep != '\0' && linkno == 0)
10464 printf("%s%s", *path, sep);
10466 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
10471 /* err == -ENODATA is end-of-loop */
10472 if (linkno > 0 && err == -ENODATA) {
10480 "%s path2fid: cannot get %sfid for '%s': %s\n",
10481 progname, show_parents ? "parent " : "", *path,
10493 #define MAX_ERRNO 4095
10494 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
10496 static int lfs_rmfid_and_show_errors(int rootfd, struct fid_array *fa)
10500 rc = llapi_rmfid_at(rootfd, fa);
10502 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
10503 progname, strerror(-rc));
10507 for (k = 0; k < fa->fa_nr; k++) {
10508 rc2 = (__s32)fa->fa_fids[k].f_ver;
10509 if (!IS_ERR_VALUE(rc2))
10515 fa->fa_fids[k].f_ver = 0;
10516 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
10517 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
10523 static int lfs_rmfid(int argc, char **argv)
10525 int rc = 0, rc2, rc3 = 0, nr;
10526 struct fid_array *fa;
10527 const char *device;
10531 /* Interactive mode: Adjust optind */
10535 device = argv[optind++];
10537 if (optind > argc - 1) {
10538 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
10542 nr = argc - optind;
10544 rc = llapi_root_path_open(device, &rootfd);
10547 "%s rmfid: error opening device/fsname '%s': %s\n",
10548 progname, device, strerror(-rc));
10552 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
10554 rc = -errno ?: -ENOMEM;
10555 fprintf(stderr, "%s rmfid: error allocating %zd bytes: %s\n",
10556 progname, offsetof(struct fid_array, fa_fids[nr + 1]),
10563 while (optind < argc) {
10567 origfidstr = fidstr = argv[optind++];
10568 while (*fidstr == '[')
10570 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
10572 fprintf(stderr, "lfs rmfid: '%s': Wrong FID format\n",
10575 rc3 = -EINVAL; /* Invalid argument */
10579 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
10580 /* start another batch */
10581 rc2 = lfs_rmfid_and_show_errors(rootfd, fa);
10590 rc2 = lfs_rmfid_and_show_errors(rootfd, fa);
10607 static int lfs_data_version(int argc, char **argv)
10609 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
10610 __u64 data_version;
10617 fprintf(stderr, "%s: FILE must be specified\n",
10622 while ((c = getopt(argc, argv, "hnrw")) != -1) {
10625 data_version_flags = 0;
10628 data_version_flags |= LL_DV_RD_FLUSH;
10631 data_version_flags |= LL_DV_WR_FLUSH;
10635 "%s data_version: unrecognized option '%s'\n",
10636 progname, argv[optind - 1]);
10642 if (optind == argc) {
10643 fprintf(stderr, "%s data_version: FILE must be specified\n",
10648 path = argv[optind];
10649 fd = open(path, O_RDONLY);
10652 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
10653 progname, path, strerror(-rc));
10657 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
10660 "%s data_version: cannot get version for '%s': %s\n",
10661 progname, path, strerror(-rc));
10663 printf("%ju" "\n", (uintmax_t)data_version);
10669 static int lfs_hsm_state(int argc, char **argv)
10674 struct hsm_user_state hus;
10683 rc2 = llapi_hsm_state_get(path, &hus);
10686 "%s %s: get HSM state for '%s' failed: %s\n",
10687 progname, argv[0], path, strerror(-rc2));
10693 /* Display path name and status flags */
10694 printf("%s: (0x%08x)", path, hus.hus_states);
10696 if (hus.hus_states & HS_RELEASED)
10697 printf(" released");
10698 if (hus.hus_states & HS_EXISTS)
10700 if (hus.hus_states & HS_DIRTY)
10702 if (hus.hus_states & HS_ARCHIVED)
10703 printf(" archived");
10704 /* Display user-settable flags */
10705 if (hus.hus_states & HS_NORELEASE)
10706 printf(" never_release");
10707 if (hus.hus_states & HS_NOARCHIVE)
10708 printf(" never_archive");
10709 if (hus.hus_states & HS_LOST)
10710 printf(" lost_from_hsm");
10712 if (hus.hus_archive_id != 0)
10713 printf(", archive_id:%d", hus.hus_archive_id);
10716 } while (++i < argc);
10721 #define LFS_HSM_SET 0
10722 #define LFS_HSM_CLEAR 1
10725 * Generic function to set or clear HSM flags.
10726 * Used by hsm_set and hsm_clear.
10728 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
10730 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
10732 struct option long_opts[] = {
10733 { .val = 'A', .name = "archived", .has_arg = no_argument },
10734 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
10735 { .val = 'd', .name = "dirty", .has_arg = no_argument },
10736 { .val = 'e', .name = "exists", .has_arg = no_argument },
10737 { .val = 'h', .name = "help", .has_arg = no_argument },
10738 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
10739 { .val = 'l', .name = "lost", .has_arg = no_argument },
10740 { .val = 'r', .name = "norelease", .has_arg = no_argument },
10741 { .name = NULL } };
10745 __u32 archive_id = 0;
10751 while ((c = getopt_long(argc, argv, "aAdehi:lr",
10752 long_opts, NULL)) != -1) {
10758 mask |= HS_NOARCHIVE;
10761 mask |= HS_ARCHIVED;
10764 mask |= HS_NORELEASE;
10774 archive_id = strtol(optarg, &end, 10);
10775 if (errno != 0 || *end != '\0' || archive_id < 0) {
10777 "%s: invalid archive_id: '%s'\n",
10783 fprintf(stderr, "%s: unrecognized option '%s'\n",
10784 progname, argv[optind - 1]);
10791 /* User should have specified a flag */
10795 while (optind < argc) {
10797 path = argv[optind];
10799 /* If mode == 0, this means we apply the mask. */
10800 if (mode == LFS_HSM_SET)
10801 rc2 = llapi_hsm_state_set(path, mask, 0, archive_id);
10803 rc2 = llapi_hsm_state_set(path, 0, mask, 0);
10807 "%s %s: change hsm flags for '%s' failed: %s\n",
10808 progname, argv[0], path, strerror(-rc2));
10818 static int lfs_hsm_action(int argc, char **argv)
10820 struct hsm_current_action hca;
10821 struct hsm_extent he;
10822 enum hsm_user_action hua;
10823 enum hsm_progress_states hps;
10835 rc2 = llapi_hsm_current_action(path, &hca);
10838 "%s %s: get hsm action for '%s' failed: %s\n",
10839 progname, argv[0], path, strerror(-rc2));
10845 he = hca.hca_location;
10846 hua = hca.hca_action;
10847 hps = hca.hca_state;
10849 printf("%s: %s", path, hsm_user_action2name(hua));
10851 /* Skip file without action */
10852 if (hca.hca_action == HUA_NONE) {
10857 printf(" %s ", hsm_progress_state2name(hps));
10859 if ((hps == HPS_RUNNING) &&
10860 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
10861 printf("(%llu bytes moved)\n",
10862 (unsigned long long)he.length);
10863 else if ((he.offset + he.length) == LUSTRE_EOF)
10864 printf("(from %llu to EOF)\n",
10865 (unsigned long long)he.offset);
10867 printf("(from %llu to %llu)\n",
10868 (unsigned long long)he.offset,
10869 (unsigned long long)(he.offset + he.length));
10871 } while (++i < argc);
10876 static int lfs_hsm_set(int argc, char **argv)
10878 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
10881 static int lfs_hsm_clear(int argc, char **argv)
10883 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
10887 * Check file state and return its fid, to be used by lfs_hsm_request().
10889 * \param[in] file Path to file to check
10890 * \param[in,out] fid Pointer to allocated lu_fid struct.
10891 * \param[in,out] last_dev Pointer to last device id used.
10893 * \return 0 on success.
10895 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
10901 rc = lstat(file, &st);
10903 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
10907 * Checking for regular file as archiving as posix copytool
10908 * rejects archiving files other than regular files
10910 if (!S_ISREG(st.st_mode)) {
10911 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
10914 /* A request should be ... */
10915 if (*last_dev != st.st_dev && *last_dev != 0) {
10917 "All files should be on the same filesystem: %s\n",
10921 *last_dev = st.st_dev;
10923 rc = llapi_path2fid(file, fid);
10925 fprintf(stderr, "Cannot read FID of %s: %s\n",
10926 file, strerror(-rc));
10932 /* Fill an HSM HUR item with a given file name.
10934 * If mntpath is set, then the filename is actually a FID, and no
10935 * lookup on the filesystem will be performed.
10937 * \param[in] hur the user request to fill
10938 * \param[in] idx index of the item inside the HUR to fill
10939 * \param[in] mntpath mountpoint of Lustre
10940 * \param[in] fname filename (if mtnpath is NULL)
10941 * or FID (if mntpath is set)
10942 * \param[in] last_dev pointer to last device id used
10944 * \retval 0 on success
10945 * \retval CMD_HELP or a negative errno on error
10947 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
10948 const char *mntpath, const char *fname,
10951 struct hsm_user_item *hui = &hur->hur_user_item[idx];
10954 hui->hui_extent.length = -1;
10957 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
10959 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
10962 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
10966 hur->hur_request.hr_itemcount++;
10971 static int lfs_hsm_request(int argc, char **argv, int action)
10973 struct option long_opts[] = {
10974 { .val = 'a', .name = "archive", .has_arg = required_argument },
10975 { .val = 'D', .name = "data", .has_arg = required_argument },
10976 { .val = 'h', .name = "help", .has_arg = no_argument },
10977 { .val = 'l', .name = "filelist", .has_arg = required_argument },
10978 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
10979 { .name = NULL } };
10980 dev_t last_dev = 0;
10981 struct hsm_user_request *hur, *oldhur;
10986 char *filelist = NULL;
10987 char fullpath[PATH_MAX];
10988 char *opaque = NULL;
10989 int opaque_len = 0;
10990 int archive_id = 0;
10992 int nbfile_alloc = 0;
10993 char *some_file = NULL;
10994 char *mntpath = NULL;
11002 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
11003 long_opts, NULL)) != -1) {
11012 if (action != HUA_ARCHIVE &&
11013 action != HUA_REMOVE) {
11015 "error: -a is supported only when archiving or removing\n");
11019 archive_id = atoi(optarg);
11024 some_file = strdup(optarg);
11028 fprintf(stderr, "%s: unrecognized option '%s'\n",
11029 progname, argv[optind - 1]);
11037 /* All remaining args are files, so we have at least nbfile */
11038 nbfile = argc - optind;
11040 if ((nbfile == 0) && (!filelist)) {
11046 opaque_len = strlen(opaque);
11049 * Alloc the request structure with enough place to store all files
11050 * from command line.
11052 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
11054 fprintf(stderr, "Cannot create the request: %s\n",
11059 nbfile_alloc = nbfile;
11061 hur->hur_request.hr_action = action;
11062 hur->hur_request.hr_archive_id = archive_id;
11063 hur->hur_request.hr_flags = 0;
11065 /* All remaining args are files, add them */
11066 if (nbfile != 0 && some_file == NULL)
11067 some_file = strdup(argv[optind]);
11069 for (i = 0; i < nbfile; i++) {
11070 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
11076 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
11078 /* If a filelist was specified, read the filelist from it. */
11080 fp = fopen(filelist, "r");
11082 fprintf(stderr, "Cannot read the file list %s: %s\n",
11083 filelist, strerror(errno));
11088 while ((rc = getline(&line, &len, fp)) != -1) {
11090 * If allocated buffer was too small, get something
11093 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
11096 nbfile_alloc = nbfile_alloc * 2 + 1;
11098 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
11102 "hsm: cannot allocate the request: %s\n",
11109 size = hur_len(oldhur);
11112 "hsm: cannot allocate %u files + %u bytes data\n",
11113 oldhur->hur_request.hr_itemcount,
11114 oldhur->hur_request.hr_data_len);
11121 memcpy(hur, oldhur, size);
11126 if (line[strlen(line) - 1] == '\n')
11127 line[strlen(line) - 1] = '\0';
11129 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
11130 mntpath, line, &last_dev);
11146 /* If a --data was used, add it to the request */
11147 hur->hur_request.hr_data_len = opaque_len;
11149 memcpy(hur_data(hur), opaque, opaque_len);
11151 /* Send the HSM request */
11152 if (realpath(some_file, fullpath) == NULL) {
11153 fprintf(stderr, "Could not find path '%s': %s\n",
11154 some_file, strerror(errno));
11156 rc = llapi_hsm_request(fullpath, hur);
11158 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
11159 some_file, strerror(-rc));
11169 static int lfs_hsm_archive(int argc, char **argv)
11171 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
11174 static int lfs_hsm_restore(int argc, char **argv)
11176 return lfs_hsm_request(argc, argv, HUA_RESTORE);
11179 static int lfs_hsm_release(int argc, char **argv)
11181 return lfs_hsm_request(argc, argv, HUA_RELEASE);
11184 static int lfs_hsm_remove(int argc, char **argv)
11186 return lfs_hsm_request(argc, argv, HUA_REMOVE);
11189 static int lfs_hsm_cancel(int argc, char **argv)
11191 return lfs_hsm_request(argc, argv, HUA_CANCEL);
11194 static int lfs_swap_layouts(int argc, char **argv)
11198 if (argc == 4 && !strcmp(argv[1], "-n"))
11200 else if (argc != 3)
11203 return llapi_swap_layouts(argv[1+noxtime], argv[2+noxtime],
11204 0, 0, noxtime ? 0 :
11205 (SWAP_LAYOUTS_KEEP_MTIME |
11206 SWAP_LAYOUTS_KEEP_ATIME));
11209 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
11211 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
11213 static int lfs_get_mode(const char *string)
11215 enum lock_mode_user mode;
11217 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
11218 if (lock_mode_names[mode] == NULL)
11220 if (strcasecmp(string, lock_mode_names[mode]) == 0)
11227 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
11229 enum lu_ladvise_type advice;
11232 advice < ARRAY_SIZE(ladvise_names); advice++) {
11233 if (ladvise_names[advice] == NULL)
11235 if (strcmp(string, ladvise_names[advice]) == 0)
11239 return LU_LADVISE_INVALID;
11242 static int lfs_ladvise(int argc, char **argv)
11244 struct option long_opts[] = {
11245 { .val = 'a', .name = "advice", .has_arg = required_argument },
11246 { .val = 'b', .name = "background", .has_arg = no_argument },
11247 { .val = 'e', .name = "end", .has_arg = required_argument },
11248 { .val = 'h', .name = "help", .has_arg = no_argument },
11249 { .val = 'l', .name = "length", .has_arg = required_argument },
11250 { .val = 'm', .name = "mode", .has_arg = required_argument },
11251 { .val = 's', .name = "start", .has_arg = required_argument },
11252 { .val = 'u', .name = "unset", .has_arg = no_argument },
11253 { .name = NULL } };
11254 struct llapi_lu_ladvise advice;
11255 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
11256 unsigned long long start = 0;
11257 unsigned long long end = LUSTRE_EOF;
11258 unsigned long long length = 0;
11259 unsigned long long size_units;
11260 unsigned long long flags = 0;
11266 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
11267 long_opts, NULL)) != -1) {
11270 advice_type = lfs_get_ladvice(optarg);
11271 if (advice_type == LU_LADVISE_INVALID) {
11273 "%s: invalid advice type '%s'\n",
11275 fprintf(stderr, "Valid types:");
11277 for (advice_type = 0;
11278 advice_type < ARRAY_SIZE(ladvise_names);
11280 if (ladvise_names[advice_type] == NULL)
11282 fprintf(stderr, " %s",
11283 ladvise_names[advice_type]);
11285 fprintf(stderr, "\n");
11298 rc = llapi_parse_size(optarg, &end,
11301 fprintf(stderr, "%s: bad end offset '%s'\n",
11308 rc = llapi_parse_size(optarg, &start,
11312 "%s: bad start offset '%s'\n",
11319 rc = llapi_parse_size(optarg, &length,
11322 fprintf(stderr, "%s: bad length '%s'\n",
11328 mode = lfs_get_mode(optarg);
11331 "%s: bad mode '%s', valid modes are READ or WRITE\n",
11337 fprintf(stderr, "%s: unrecognized option '%s'\n",
11338 progname, argv[optind - 1]);
11345 if (advice_type == LU_LADVISE_INVALID) {
11346 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
11347 fprintf(stderr, "Valid types:");
11348 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
11350 if (ladvise_names[advice_type] == NULL)
11352 fprintf(stderr, " %s", ladvise_names[advice_type]);
11354 fprintf(stderr, "\n");
11358 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
11360 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
11365 if (argc <= optind) {
11366 fprintf(stderr, "%s: please give one or more file names\n",
11371 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
11372 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
11377 if (end == LUSTRE_EOF && length != 0)
11378 end = start + length;
11380 if (end <= start) {
11381 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
11382 argv[0], start, end);
11386 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
11387 fprintf(stderr, "%s: mode is only valid with lockahead\n",
11392 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
11393 fprintf(stderr, "%s: mode is required with lockahead\n",
11398 while (optind < argc) {
11401 path = argv[optind++];
11403 fd = open(path, O_RDONLY);
11406 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11407 argv[0], path, strerror(-rc2));
11413 advice.lla_start = start;
11414 advice.lla_end = end;
11415 advice.lla_advice = advice_type;
11416 advice.lla_value1 = 0;
11417 advice.lla_value2 = 0;
11418 advice.lla_value3 = 0;
11419 advice.lla_value4 = 0;
11420 if (advice_type == LU_LADVISE_LOCKAHEAD) {
11421 advice.lla_lockahead_mode = mode;
11422 advice.lla_peradvice_flags = flags;
11425 rc2 = llapi_ladvise(fd, flags, 1, &advice);
11429 "%s: cannot give advice '%s' to file '%s': %s\n",
11430 argv[0], ladvise_names[advice_type],
11431 path, strerror(errno));
11442 static const char *const heat_names[] = LU_HEAT_NAMES;
11444 static int lfs_heat_get(int argc, char **argv)
11446 struct lu_heat *heat;
11455 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
11457 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
11462 while (optind < argc) {
11463 path = argv[optind++];
11465 fd = open(path, O_RDONLY);
11467 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11468 argv[0], path, strerror(errno));
11473 heat->lh_count = OBD_HEAT_COUNT;
11474 rc2 = llapi_heat_get(fd, heat);
11478 "%s: cannot get heat of file '%s': %s\n",
11479 argv[0], path, strerror(errno));
11483 printf("flags: %x\n", heat->lh_flags);
11484 for (i = 0; i < heat->lh_count; i++)
11485 printf("%s: %llu\n", heat_names[i],
11486 (unsigned long long)heat->lh_heat[i]);
11488 if (rc == 0 && rc2 < 0)
11496 static int lfs_heat_set(int argc, char **argv)
11498 struct option long_opts[] = {
11499 { .val = 'c', .name = "clear", .has_arg = no_argument },
11500 { .val = 'h', .name = "help", .has_arg = no_argument },
11501 { .val = 'o', .name = "off", .has_arg = no_argument },
11502 { .val = 'O', .name = "on", .has_arg = no_argument },
11503 { .name = NULL } };
11504 enum lu_heat_flag flags = 0;
11514 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
11517 flags |= LU_HEAT_FLAG_CLEAR;
11520 flags |= LU_HEAT_FLAG_CLEAR;
11521 flags |= LU_HEAT_FLAG_OFF;
11524 flags &= ~LU_HEAT_FLAG_OFF;
11527 fprintf(stderr, "%s: unrecognized option '%s'\n",
11528 progname, argv[optind - 1]);
11535 if (argc <= optind) {
11536 fprintf(stderr, "%s: please give one or more file names\n",
11541 while (optind < argc) {
11542 path = argv[optind++];
11544 fd = open(path, O_RDONLY);
11546 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11547 argv[0], path, strerror(errno));
11552 rc2 = llapi_heat_set(fd, flags);
11556 "%s: cannot setflags heat of file '%s': %s\n",
11557 argv[0], path, strerror(errno));
11561 if (rc == 0 && rc2 < 0)
11568 * The input string contains a comma delimited list of component ids and
11569 * ranges, for example "1,2-4,7".
11571 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
11573 bool end_of_loop = false;
11581 while (!end_of_loop) {
11585 char *endptr = NULL;
11588 ptr = strchrnul(arg, ',');
11589 end_of_loop = *ptr == '\0';
11592 start_index = strtol(arg, &endptr, 0);
11593 if (endptr == arg) /* no data at all */
11595 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
11597 if (start_index < 0)
11600 end_index = start_index;
11601 if (*endptr == '-') {
11602 end_index = strtol(endptr + 1, &endptr, 0);
11603 if (*endptr != '\0')
11605 if (end_index < start_index)
11609 for (i = start_index; i <= end_index && size > 0; i++) {
11612 /* remove duplicate */
11613 for (j = 0; j < nr; j++) {
11617 if (j == nr) { /* no duplicate */
11623 if (size == 0 && i < end_index)
11630 if (!end_of_loop && ptr)
11633 return rc < 0 ? rc : nr;
11637 * struct verify_mirror_id - Mirror id to be verified.
11638 * @mirror_id: A specified mirror id.
11639 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
11641 struct verify_mirror_id {
11647 * compare_mirror_ids() - Compare mirror ids.
11648 * @layout: Mirror component list.
11649 * @cbdata: Callback data in verify_mirror_id structure.
11651 * This is a callback function called by llapi_layout_comp_iterate()
11652 * to compare the specified mirror id with the one in the current
11653 * component of @layout. If they are the same, then the specified
11654 * mirror id is valid.
11656 * Return: a negative error code on failure or
11657 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
11658 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
11661 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
11663 struct verify_mirror_id *mirror_id_cbdata =
11664 (struct verify_mirror_id *)cbdata;
11665 uint32_t mirror_id;
11668 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11672 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11673 progname, strerror(errno));
11677 if (mirror_id_cbdata->mirror_id == mirror_id) {
11678 mirror_id_cbdata->is_valid_id = true;
11679 return LLAPI_LAYOUT_ITER_STOP;
11682 return LLAPI_LAYOUT_ITER_CONT;
11686 * verify_mirror_ids() - Verify specified mirror ids.
11687 * @fname: Mirrored file name.
11688 * @mirror_ids: Specified mirror ids to be verified.
11689 * @ids_nr: Number of specified mirror ids.
11691 * This function verifies that specified @mirror_ids are valid
11692 * in the mirrored file @fname.
11694 * Return: 0 on success or a negative error code on failure.
11697 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
11699 struct llapi_layout *layout = NULL;
11700 struct verify_mirror_id mirror_id_cbdata = { 0 };
11702 uint32_t flr_state;
11711 if (stat(fname, &stbuf) < 0) {
11712 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11713 progname, fname, strerror(errno));
11718 if (!S_ISREG(stbuf.st_mode)) {
11719 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11725 fd = open(fname, O_DIRECT | O_RDONLY);
11727 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11728 progname, fname, strerror(errno));
11733 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11735 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11736 progname, fname, strerror(errno));
11740 layout = llapi_layout_get_by_fd(fd, 0);
11742 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11743 progname, fname, strerror(errno));
11745 llapi_lease_release(fd);
11749 rc = llapi_layout_flags_get(layout, &flr_state);
11751 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11752 progname, fname, strerror(errno));
11757 flr_state &= LCM_FL_FLR_MASK;
11758 switch (flr_state) {
11761 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11762 progname, fname, llapi_layout_flags_string(flr_state));
11769 for (i = 0; i < ids_nr; i++) {
11770 mirror_id_cbdata.mirror_id = mirror_ids[i];
11771 mirror_id_cbdata.is_valid_id = false;
11773 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
11774 &mirror_id_cbdata);
11778 "%s: '%s' failed to verify mirror id: %u.\n",
11779 progname, fname, mirror_ids[i]);
11783 if (!mirror_id_cbdata.is_valid_id) {
11786 "%s: '%s' invalid specified mirror id: %u.\n",
11787 progname, fname, mirror_ids[i]);
11793 llapi_layout_free(layout);
11794 llapi_lease_release(fd);
11802 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
11803 __u16 *mirror_ids, int ids_nr,
11804 long stats_interval_sec, long bandwidth_bytes_sec)
11806 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11807 struct llapi_layout *layout;
11809 uint32_t flr_state;
11818 if (stat(fname, &stbuf) < 0) {
11819 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11820 progname, fname, strerror(errno));
11824 if (!S_ISREG(stbuf.st_mode)) {
11825 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11831 /* Allow mirror resync even without the key on encrypted files */
11832 fd = open(fname, O_DIRECT | O_RDWR | O_CIPHERTEXT);
11834 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11835 progname, fname, strerror(errno));
11840 layout = llapi_layout_get_by_fd(fd, 0);
11842 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11843 progname, fname, strerror(errno));
11848 rc = llapi_layout_flags_get(layout, &flr_state);
11850 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11851 progname, fname, strerror(errno));
11856 flr_state &= LCM_FL_FLR_MASK;
11857 if (flr_state == LCM_FL_NONE) {
11859 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
11864 /* get stale component info */
11865 comp_size = llapi_mirror_find_stale(layout, comp_array,
11866 ARRAY_SIZE(comp_array),
11867 mirror_ids, ids_nr);
11868 if (comp_size <= 0) {
11873 ioc->lil_mode = LL_LEASE_WRLCK;
11874 ioc->lil_flags = LL_LEASE_RESYNC;
11875 rc = llapi_lease_set(fd, ioc);
11877 if (rc == -EALREADY)
11881 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
11882 progname, fname, strerror(-rc));
11886 /* get the read range [start, end) */
11887 start = comp_array[0].lrc_start;
11888 end = comp_array[0].lrc_end;
11889 for (idx = 1; idx < comp_size; idx++) {
11890 if (comp_array[idx].lrc_start < start)
11891 start = comp_array[idx].lrc_start;
11892 if (end < comp_array[idx].lrc_end)
11893 end = comp_array[idx].lrc_end;
11896 rc = llapi_lease_check(fd);
11897 if (rc != LL_LEASE_WRLCK) {
11898 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11903 rc = llapi_mirror_resync_many_params(fd, layout, comp_array, comp_size,
11904 start, end, stats_interval_sec,
11905 bandwidth_bytes_sec);
11907 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
11908 progname, fname, strerror(-rc));
11910 rc2 = migrate_set_timestamps(fd, &stbuf);
11912 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
11913 progname, fname, strerror(-rc2));
11919 /* need to do the lease unlock even resync fails */
11920 ioc->lil_mode = LL_LEASE_UNLCK;
11921 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11922 ioc->lil_count = 0;
11923 for (idx = 0; idx < comp_size; idx++) {
11924 if (comp_array[idx].lrc_synced) {
11925 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
11930 rc2 = llapi_lease_set(fd, ioc);
11932 * llapi_lease_set returns lease mode when it request to unlock
11936 /* rc2 == 0 means lost lease lock */
11937 if (rc2 == 0 && rc == 0)
11941 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
11943 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
11945 llapi_lease_release(fd);
11950 llapi_layout_free(layout);
11957 static inline int lfs_mirror_resync(int argc, char **argv)
11959 struct option long_opts[] = {
11960 { .val = 'h', .name = "help", .has_arg = no_argument },
11961 { .val = 'o', .name = "only", .has_arg = required_argument },
11962 { .val = 'W', .name = "bandwidth", .has_arg = required_argument },
11963 { .val = LFS_STATS_OPT,
11964 .name = "stats", .has_arg = no_argument},
11965 { .val = LFS_STATS_INTERVAL_OPT,
11966 .name = "stats-interval",
11967 .has_arg = required_argument},
11968 { .name = NULL } };
11969 struct ll_ioc_lease *ioc = NULL;
11970 __u16 mirror_ids[128] = { 0 };
11971 unsigned int stats_interval_sec = 0;
11972 unsigned long long bandwidth_bytes_sec = 0;
11973 unsigned long long bandwidth_unit = ONE_MB;
11978 while ((c = getopt_long(argc, argv, "ho:W:", long_opts, NULL)) >= 0) {
11982 rc = parse_mirror_ids(mirror_ids,
11983 sizeof(mirror_ids) / sizeof(__u16),
11987 "%s: bad mirror ids '%s'.\n",
11994 if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
11995 &bandwidth_unit, 0) < 0) {
11997 "error: %s: bad value for bandwidth '%s'\n",
12002 case LFS_STATS_OPT:
12003 stats_interval_sec = 5;
12005 case LFS_STATS_INTERVAL_OPT:
12006 stats_interval_sec = strtol(optarg, &end, 0);
12009 fprintf(stderr, "%s: unrecognized option '%s'\n",
12010 progname, argv[optind - 1]);
12018 if (argc == optind) {
12019 fprintf(stderr, "%s: no file name given.\n", argv[0]);
12024 if (ids_nr > 0 && argc > optind + 1) {
12026 "%s: option '--only' cannot be used upon multiple files.\n",
12033 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12038 /* set the lease on the file */
12039 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
12041 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
12042 argv[0], strerror(errno));
12047 for (; optind < argc; optind++) {
12048 rc = lfs_mirror_resync_file(argv[optind], ioc,
12049 mirror_ids, ids_nr,
12050 stats_interval_sec,
12051 bandwidth_bytes_sec);
12052 /* ignore previous file's error, continue with next file */
12055 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
12063 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
12065 struct llapi_layout *layout;
12068 layout = llapi_layout_get_by_fd(fd, 0);
12070 fprintf(stderr, "could not get layout.\n");
12074 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
12076 fprintf(stderr, "failed to iterate layout\n");
12077 llapi_layout_free(layout);
12080 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
12081 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
12082 llapi_layout_free(layout);
12086 llapi_layout_free(layout);
12092 * Check whether two files are the same file
12093 * \retval 0 same file
12094 * \retval 1 not the same file
12095 * \retval <0 error code
12097 static inline int check_same_file(int fd, const char *f2)
12099 struct stat stbuf1;
12100 struct stat stbuf2;
12102 if (fstat(fd, &stbuf1) < 0)
12105 if (stat(f2, &stbuf2) < 0)
12108 if (stbuf1.st_rdev == stbuf2.st_rdev &&
12109 stbuf1.st_ino == stbuf2.st_ino)
12115 static inline int lfs_mirror_read(int argc, char **argv)
12118 __u16 mirror_id = 0;
12119 const char *outfile = NULL;
12125 const size_t buflen = 4 << 20;
12128 struct option long_opts[] = {
12129 { .val = 'h', .name = "help", .has_arg = no_argument },
12130 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
12131 { .val = 'o', .name = "outfile", .has_arg = required_argument },
12132 { .name = NULL } };
12134 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
12139 unsigned long int id;
12142 id = strtoul(optarg, &end, 0);
12143 if (errno != 0 || *end != '\0' || id == 0 ||
12146 "%s %s: invalid mirror ID '%s'\n",
12147 progname, argv[0], optarg);
12151 mirror_id = (__u16)id;
12158 fprintf(stderr, "%s: unrecognized option '%s'\n",
12159 progname, argv[optind - 1]);
12166 if (argc == optind) {
12167 fprintf(stderr, "%s %s: no mirrored file provided\n",
12168 progname, argv[0]);
12170 } else if (argc > optind + 1) {
12171 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
12175 if (mirror_id == 0) {
12176 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
12177 progname, argv[0]);
12181 /* open mirror file */
12182 fname = argv[optind];
12183 fd = open(fname, O_DIRECT | O_RDONLY);
12185 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
12186 progname, argv[0], fname, strerror(errno));
12190 /* verify mirror id */
12191 rc = verify_mirror_id_by_fd(fd, mirror_id);
12194 "%s %s: cannot find mirror with ID %u in '%s'\n",
12195 progname, argv[0], mirror_id, fname);
12199 /* open output file - O_EXCL ensures output is not the same as input */
12201 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
12203 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
12204 progname, argv[0], outfile, strerror(errno));
12209 outfd = STDOUT_FILENO;
12212 page_size = sysconf(_SC_PAGESIZE);
12213 if (page_size < 0) {
12218 /* allocate buffer */
12219 rc = posix_memalign(&buf, page_size, buflen);
12221 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
12222 progname, argv[0], rc);
12228 ssize_t bytes_read;
12229 ssize_t written = 0;
12231 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
12232 if (bytes_read < 0) {
12235 "%s %s: fail to read data from mirror %u: %s\n",
12236 progname, argv[0], mirror_id, strerror(-rc));
12241 if (bytes_read == 0)
12244 while (written < bytes_read) {
12247 written2 = write(outfd, buf + written,
12248 bytes_read - written);
12249 if (written2 < 0) {
12251 "%s %s: fail to write %s: %s\n",
12252 progname, argv[0], outfile ? : "STDOUT",
12257 written += written2;
12260 if (written != bytes_read) {
12262 "%s %s: written %ld bytes does not match with %ld read.\n",
12263 progname, argv[0], written, bytes_read);
12285 static inline int lfs_mirror_write(int argc, char **argv)
12288 __u16 mirror_id = 0;
12289 const char *inputfile = NULL;
12295 const size_t buflen = 4 << 20;
12297 ssize_t page_size = sysconf(_SC_PAGESIZE);
12298 struct ll_ioc_lease_id ioc;
12299 struct option long_opts[] = {
12300 { .val = 'h', .name = "help", .has_arg = no_argument },
12301 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
12302 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
12303 { .name = NULL } };
12305 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
12310 unsigned long int id;
12313 id = strtoul(optarg, &end, 0);
12314 if (errno != 0 || *end != '\0' || id == 0 ||
12317 "%s %s: invalid mirror ID '%s'\n",
12318 progname, argv[0], optarg);
12322 mirror_id = (__u16)id;
12326 inputfile = optarg;
12329 fprintf(stderr, "%s: unrecognized option '%s'\n",
12330 progname, argv[optind - 1]);
12337 if (argc == optind) {
12338 fprintf(stderr, "%s %s: no mirrored file provided\n",
12339 progname, argv[0]);
12341 } else if (argc > optind + 1) {
12342 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
12346 if (mirror_id == 0) {
12347 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
12348 progname, argv[0]);
12352 /* open mirror file */
12353 fname = argv[optind];
12354 fd = open(fname, O_DIRECT | O_WRONLY);
12356 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
12357 progname, argv[0], fname, strerror(errno));
12361 /* verify mirror id */
12362 rc = verify_mirror_id_by_fd(fd, mirror_id);
12365 "%s %s: cannot find mirror with ID %u in '%s'\n",
12366 progname, argv[0], mirror_id, fname);
12370 /* open input file */
12372 rc = check_same_file(fd, inputfile);
12375 "%s %s: input file cannot be the mirrored file '%s'\n",
12376 progname, argv[0], fname);
12382 inputfd = open(inputfile, O_RDONLY, 0644);
12384 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
12385 progname, argv[0], inputfile, strerror(errno));
12390 inputfd = STDIN_FILENO;
12393 /* allocate buffer */
12394 rc = posix_memalign(&buf, page_size, buflen);
12396 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
12397 progname, argv[0], rc);
12398 goto close_inputfd;
12401 /* prepare target mirror components instantiation */
12402 ioc.lil_mode = LL_LEASE_WRLCK;
12403 ioc.lil_flags = LL_LEASE_RESYNC;
12404 ioc.lil_mirror_id = mirror_id;
12405 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
12408 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
12409 progname, argv[0], fname, strerror(errno));
12415 ssize_t bytes_read;
12419 rc = llapi_lease_check(fd);
12420 if (rc != LL_LEASE_WRLCK) {
12421 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
12422 progname, argv[0], fname);
12426 bytes_read = read(inputfd, buf, buflen);
12427 if (bytes_read < 0) {
12430 "%s %s: fail to read data from '%s': %s\n",
12431 progname, argv[0], inputfile ? : "STDIN",
12438 if (bytes_read == 0)
12441 /* round up to page align to make direct IO happy. */
12442 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
12444 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
12449 "%s %s: fail to write to mirror %u: %s\n",
12450 progname, argv[0], mirror_id,
12458 if (pos & (page_size - 1)) {
12459 rc = llapi_mirror_truncate(fd, mirror_id, pos);
12464 ioc.lil_mode = LL_LEASE_UNLCK;
12465 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
12467 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
12472 "%s %s: release lease lock of '%s' failed: %s\n",
12473 progname, argv[0], fname, strerror(-rc));
12490 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
12492 struct llapi_layout *layout;
12493 struct collect_ids_data cid = { .cid_ids = ids,
12495 .cid_exclude = exclude_id, };
12498 layout = llapi_layout_get_by_fd(fd, 0);
12500 fprintf(stderr, "could not get layout\n");
12504 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
12506 fprintf(stderr, "failed to iterate layout\n");
12507 llapi_layout_free(layout);
12511 llapi_layout_free(layout);
12513 return cid.cid_count;
12516 #ifndef MIRROR_ID_NEG
12517 #define MIRROR_ID_NEG 0x8000
12520 static inline int lfs_mirror_copy(int argc, char **argv)
12523 __u16 read_mirror_id = 0;
12524 __u16 ids[128] = { 0 };
12526 struct llapi_layout *layout = NULL;
12527 struct llapi_resync_comp comp_array[1024] = { { 0 } };
12534 struct ll_ioc_lease *ioc = NULL;
12535 struct ll_ioc_lease_id *resync_ioc;
12536 struct option long_opts[] = {
12537 { .val = 'h', .name = "help", .has_arg = no_argument },
12538 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
12539 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
12540 { .name = NULL } };
12541 char cmd[PATH_MAX];
12543 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12545 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
12550 unsigned long int id;
12553 id = strtoul(optarg, &end, 0);
12554 if (errno != 0 || *end != '\0' || id == 0 ||
12557 "%s: invalid read mirror ID '%s'\n",
12562 read_mirror_id = (__u16)id;
12566 if (!strcmp(optarg, "-1")) {
12567 /* specify all other mirrors */
12568 ids[0] = (__u16)-1;
12571 count = parse_mirror_ids((__u16 *)ids,
12579 fprintf(stderr, "%s: unrecognized option '%s'\n",
12580 progname, argv[optind - 1]);
12587 if (argc == optind) {
12588 fprintf(stderr, "%s %s: no mirrored file provided\n",
12589 progname, argv[0]);
12591 } else if (argc > optind + 1) {
12592 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
12596 if (read_mirror_id == 0) {
12598 "%s %s: no valid read mirror ID %d is provided\n",
12599 progname, argv[0], read_mirror_id);
12605 "%s %s: no write mirror ID is provided\n",
12606 progname, argv[0]);
12610 for (i = 0; i < count; i++) {
12611 if (read_mirror_id == ids[i]) {
12613 "%s %s: read and write mirror ID cannot be the same\n",
12614 progname, argv[0]);
12619 /* open mirror file */
12620 fname = argv[optind];
12622 fd = open(fname, O_DIRECT | O_RDWR);
12624 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
12625 progname, argv[0], fname, strerror(errno));
12629 /* write to all other mirrors */
12630 if (ids[0] == (__u16)-1) {
12631 count = get_other_mirror_ids(fd, ids, read_mirror_id);
12635 "%s %s: failed to get other mirror ids in '%s': %d\n",
12636 progname, argv[0], fname, rc);
12641 /* verify mirror id */
12642 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
12645 "%s %s: cannot find mirror with ID %u in '%s'\n",
12646 progname, argv[0], read_mirror_id, fname);
12650 for (i = 0; i < count; i++) {
12651 rc = verify_mirror_id_by_fd(fd, ids[i]);
12654 "%s %s: cannot find mirror with ID %u in '%s'\n",
12655 progname, argv[0], ids[i], fname);
12660 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
12663 "%s %s: cannot alloc comp id array for ioc: %s\n",
12664 progname, argv[0], strerror(errno));
12669 /* get stale component info */
12670 layout = llapi_layout_get_by_fd(fd, 0);
12672 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
12673 progname, argv[0], fname, strerror(errno));
12677 comp_size = llapi_mirror_find_stale(layout, comp_array,
12678 ARRAY_SIZE(comp_array),
12680 llapi_layout_free(layout);
12681 if (comp_size < 0) {
12686 /* prepare target mirror components instantiation */
12687 resync_ioc = (struct ll_ioc_lease_id *)ioc;
12688 resync_ioc->lil_mode = LL_LEASE_WRLCK;
12689 resync_ioc->lil_flags = LL_LEASE_RESYNC;
12691 resync_ioc->lil_mirror_id = ids[0];
12693 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
12694 rc = llapi_lease_set(fd, ioc);
12697 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
12698 progname, argv[0], fname, strerror(errno));
12702 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
12705 fprintf(stderr, "%s %s: copy error: %d\n",
12706 progname, argv[0], rc);
12710 fprintf(stdout, "mirror copied successfully: ");
12711 for (i = 0; i < copied; i++)
12712 fprintf(stdout, "%d ", ids[i]);
12713 fprintf(stdout, "\n");
12715 ioc->lil_mode = LL_LEASE_UNLCK;
12716 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
12717 ioc->lil_count = 0;
12718 for (i = 0; i < comp_size; i++) {
12721 for (j = 0; j < copied; j++) {
12722 if (comp_array[i].lrc_mirror_id != ids[j])
12725 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
12729 rc = llapi_lease_set(fd, ioc);
12734 "%s %s: release lease lock of '%s' failed: %s\n",
12735 progname, argv[0], fname, strerror(errno));
12750 * struct verify_chunk - Mirror chunk to be verified.
12751 * @chunk: [start, end) of the chunk.
12752 * @mirror_count: Number of mirror ids in @mirror_id array.
12753 * @mirror_id: Array of valid mirror ids that cover the chunk.
12755 struct verify_chunk {
12756 struct lu_extent chunk;
12757 unsigned int mirror_count;
12758 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
12762 * print_chunks() - Print chunk information.
12763 * @fname: Mirrored file name.
12764 * @chunks: Array of chunks.
12765 * @chunk_count: Number of chunks in @chunks array.
12767 * This function prints [start, end) of each chunk in @chunks
12768 * for mirrored file @fname, and also prints the valid mirror ids
12769 * that cover the chunk.
12774 void print_chunks(const char *fname, struct verify_chunk *chunks,
12780 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
12781 for (i = 0; i < chunk_count; i++) {
12782 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
12784 if (chunks[i].mirror_count == 0)
12785 fprintf(stdout, "\t[");
12787 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
12788 for (j = 1; j < chunks[i].mirror_count; j++)
12789 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
12791 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
12793 fprintf(stdout, "\n");
12797 * print_checksums() - Print CRC-32 checksum values.
12798 * @chunk: A chunk and its corresponding valid mirror ids.
12799 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
12801 * This function prints CRC-32 checksum values on @chunk for
12802 * each valid mirror that covers it.
12807 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
12808 unsigned long long pos, unsigned long long len)
12813 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
12814 for (i = 0; i < chunk->mirror_count; i++)
12815 fprintf(stdout, "Mirror %u:\t%#lx\n",
12816 chunk->mirror_id[i], crc[i]);
12817 fprintf(stdout, "\n");
12821 * filter_mirror_id() - Filter specified mirror ids.
12822 * @chunks: Array of chunks.
12823 * @chunk_count: Number of chunks in @chunks array.
12824 * @mirror_ids: Specified mirror ids to be verified.
12825 * @ids_nr: Number of specified mirror ids.
12827 * This function scans valid mirror ids that cover each chunk in @chunks
12828 * and filters specified mirror ids.
12833 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
12834 __u16 *mirror_ids, int ids_nr)
12839 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12840 unsigned int valid_count = 0;
12842 for (i = 0; i < chunk_count; i++) {
12843 if (chunks[i].mirror_count == 0)
12847 for (j = 0; j < ids_nr; j++) {
12848 for (k = 0; k < chunks[i].mirror_count; k++) {
12849 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
12850 valid_id[valid_count] = mirror_ids[j];
12857 memcpy(chunks[i].mirror_id, valid_id,
12858 sizeof(__u16) * valid_count);
12859 chunks[i].mirror_count = valid_count;
12864 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
12865 * @layout: Mirror component list.
12866 * @chunks: Array of chunks.
12867 * @chunks_size: Array size of @chunks.
12869 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
12870 * to find out chunk segments and store them in @chunks array.
12872 * The @mirror_id array in each element of @chunks will store the valid
12873 * mirror ids that cover the chunk. If a mirror component covering the
12874 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
12875 * will not be stored into the @mirror_id array, and the chunk for that
12876 * mirror will not be verified.
12878 * The @mirror_count in each element of @chunks will store the number of
12879 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
12880 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
12881 * indicates the chunk is valid in only one mirror. In both cases, the
12882 * chunk will not be verified.
12884 * Here is an example:
12886 * 0 1M 2M 3M 4M EOF
12887 * +------+-------------+--------------------+
12888 * | | | S | mirror1
12889 * +------+------+------+------+-------------+
12890 * | | S | S | | mirror2
12891 * +-------------+------+------+-------------+
12893 * prepared @chunks array will contain 5 elements:
12894 * (([0, 1M), [1, 2], 2),
12895 * ([1M, 2M), [1, 2], 2),
12896 * ([2M, 3M), [1], 1),
12897 * ([3M, 4M], [], 0),
12898 * ([4M, EOF), [2], 1))
12900 * Return: the actual array size of @chunks on success
12901 * or a negative error code on failure.
12904 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
12905 struct verify_chunk *chunks,
12906 size_t chunks_size)
12910 uint32_t mirror_id;
12916 memset(chunks, 0, sizeof(*chunks) * chunks_size);
12919 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
12922 "%s: move to the first layout component: %s.\n",
12923 progname, strerror(errno));
12929 chunks[idx].chunk.e_end = LUSTRE_EOF;
12931 rc = llapi_layout_comp_extent_get(layout, &start, &end);
12934 "%s: llapi_layout_comp_extent_get failed: %s.\n",
12935 progname, strerror(errno));
12939 if (start > chunks[idx].chunk.e_start ||
12940 end <= chunks[idx].chunk.e_start)
12943 if (end < chunks[idx].chunk.e_end)
12944 chunks[idx].chunk.e_end = end;
12946 rc = llapi_layout_comp_flags_get(layout, &flags);
12949 "%s: llapi_layout_comp_flags_get failed: %s.\n",
12950 progname, strerror(errno));
12954 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
12957 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
12960 "%s: llapi_layout_mirror_id_get failed: %s.\n",
12961 progname, strerror(errno));
12965 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
12967 "%s: mirror_id array is too small.\n",
12972 chunks[idx].mirror_id[i] = mirror_id;
12976 rc = llapi_layout_comp_use(layout,
12977 LLAPI_LAYOUT_COMP_USE_NEXT);
12980 "%s: move to the next layout component: %s.\n",
12981 progname, strerror(errno));
12984 } /* loop through all components */
12986 chunks[idx].mirror_count = i;
12988 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
12992 if (idx >= chunks_size) {
12993 fprintf(stderr, "%s: chunks array is too small.\n",
12999 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
13003 return rc < 0 ? rc : idx + 1;
13007 * lfs_mirror_verify_chunk() - Verify a chunk.
13008 * @fd: File descriptor of the mirrored file.
13009 * @file_size: Size of the mirrored file.
13010 * @chunk: A chunk and its corresponding valid mirror ids.
13011 * @verbose: Verbose mode.
13013 * This function verifies a @chunk contains exactly the same data
13014 * ammong the mirrors that cover it.
13016 * If @verbose is specified, then the function will print where the
13017 * differences are if the data do not match. Otherwise, it will
13018 * just return an error in that case.
13020 * Return: 0 on success or a negative error code on failure.
13023 int lfs_mirror_verify_chunk(int fd, size_t file_size,
13024 struct verify_chunk *chunk, int verbose)
13026 const size_t buflen = 4 * 1024 * 1024; /* 4M */
13029 ssize_t bytes_read;
13030 ssize_t bytes_done;
13034 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
13038 if (file_size == 0)
13041 page_size = sysconf(_SC_PAGESIZE);
13042 if (page_size < 0) {
13047 rc = posix_memalign(&buf, page_size, buflen);
13048 if (rc) /* error code is returned directly */
13052 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
13053 PEXT(&chunk->chunk));
13054 for (i = 0; i < chunk->mirror_count; i++)
13055 fprintf(stdout, " %u", chunk->mirror_id[i]);
13056 fprintf(stdout, "\n");
13060 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
13061 pos = chunk->chunk.e_start;
13062 while (bytes_done < count) {
13063 /* compute initial CRC-32 checksum */
13064 crc = crc32(0L, Z_NULL, 0);
13065 memset(crc_array, 0, sizeof(crc_array));
13068 for (i = 0; i < chunk->mirror_count; i++) {
13069 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
13071 if (bytes_read < 0) {
13074 "%s: failed to read data from mirror %u: %s.\n",
13075 progname, chunk->mirror_id[i],
13080 /* compute new CRC-32 checksum */
13081 crc_array[i] = crc32(crc, buf, bytes_read);
13085 print_checksums(chunk, crc_array, pos, buflen);
13087 /* compare CRC-32 checksum values */
13088 for (i = 1; i < chunk->mirror_count; i++) {
13089 if (crc_array[i] != crc_array[0]) {
13093 "%s: chunk "DEXT" has different checksum value on mirror %u:%lx and mirror %u:%lx.\n",
13094 progname, PEXT(&chunk->chunk),
13095 chunk->mirror_id[0], crc_array[0],
13096 chunk->mirror_id[i], crc_array[i]);
13097 print_checksums(chunk, crc_array, pos, buflen);
13102 bytes_done += bytes_read;
13105 if (verbose > 1 && rc == 0) {
13106 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
13107 PEXT(&chunk->chunk));
13108 for (i = 0; i < chunk->mirror_count; i++)
13109 fprintf(stdout, " %u", chunk->mirror_id[i]);
13110 fprintf(stdout, " PASS\n\n");
13119 * lfs_mirror_verify_file() - Verify a mirrored file.
13120 * @fname: Mirrored file name.
13121 * @mirror_ids: Specified mirror ids to be verified.
13122 * @ids_nr: Number of specified mirror ids.
13123 * @verbose: Verbose mode.
13125 * This function verifies that each SYNC mirror of a mirrored file
13126 * specified by @fname contains exactly the same data.
13128 * If @mirror_ids is specified, then the function will verify the
13129 * mirrors specified by @mirror_ids contain exactly the same data.
13131 * If @verbose is specified, then the function will print where the
13132 * differences are if the data do not match. Otherwise, it will
13133 * just return an error in that case.
13135 * Return: 0 on success or a negative error code on failure.
13138 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
13141 struct verify_chunk chunks_array[1024] = { };
13142 struct llapi_layout *layout = NULL;
13144 uint32_t flr_state;
13146 int chunk_count = 0;
13152 if (stat(fname, &stbuf) < 0) {
13153 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
13154 progname, fname, strerror(errno));
13159 if (!S_ISREG(stbuf.st_mode)) {
13160 fprintf(stderr, "%s: '%s' is not a regular file.\n",
13166 if (stbuf.st_size == 0) {
13168 fprintf(stdout, "%s: '%s' file size is 0.\n",
13174 /* Allow mirror verify even without the key on encrypted files */
13175 fd = open(fname, O_DIRECT | O_RDONLY | O_CIPHERTEXT);
13177 fprintf(stderr, "%s: cannot open '%s': %s.\n",
13178 progname, fname, strerror(errno));
13183 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
13185 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
13186 progname, fname, strerror(errno));
13190 layout = llapi_layout_get_by_fd(fd, 0);
13192 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
13193 progname, fname, strerror(errno));
13195 llapi_lease_release(fd);
13199 rc = llapi_layout_flags_get(layout, &flr_state);
13201 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
13202 progname, fname, strerror(errno));
13207 flr_state &= LCM_FL_FLR_MASK;
13208 switch (flr_state) {
13211 fprintf(stderr, "%s: '%s' file state error: %s.\n",
13212 progname, fname, llapi_layout_flags_string(flr_state));
13218 /* find out mirror chunks to be verified */
13219 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
13220 ARRAY_SIZE(chunks_array));
13221 if (chunk_count < 0) {
13227 /* filter specified mirror ids */
13228 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
13231 print_chunks(fname, chunks_array, chunk_count);
13233 for (idx = 0; idx < chunk_count; idx++) {
13234 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
13237 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
13239 PEXT(&chunks_array[idx].chunk),
13240 (unsigned long long)stbuf.st_size);
13244 if (chunks_array[idx].mirror_count == 0) {
13246 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
13248 PEXT(&chunks_array[idx].chunk));
13250 fprintf(stderr, "skipped\n");
13254 fprintf(stderr, "failed\n");
13258 if (chunks_array[idx].mirror_count == 1) {
13261 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
13263 PEXT(&chunks_array[idx].chunk),
13264 chunks_array[idx].mirror_id[0]);
13268 rc = llapi_lease_check(fd);
13269 if (rc != LL_LEASE_RDLCK) {
13270 fprintf(stderr, "%s: '%s' lost lease lock.\n",
13275 /* verify one chunk */
13276 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
13277 &chunks_array[idx], verbose);
13291 llapi_layout_free(layout);
13292 llapi_lease_release(fd);
13300 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
13301 * @argc: The count of lfs mirror verify command line arguments.
13302 * @argv: Array of strings for lfs mirror verify command line arguments.
13304 * This function parses lfs mirror verify command and verifies the
13305 * specified mirrored file(s).
13307 * Return: 0 on success or a negative error code on failure.
13309 static inline int lfs_mirror_verify(int argc, char **argv)
13311 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
13317 char cmd[PATH_MAX];
13319 struct option long_opts[] = {
13320 { .val = 'h', .name = "help", .has_arg = no_argument },
13321 { .val = 'o', .name = "only", .has_arg = required_argument },
13322 { .val = 'v', .name = "verbose", .has_arg = no_argument },
13323 { .name = NULL } };
13325 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
13327 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
13330 rc = parse_mirror_ids(mirror_ids,
13331 ARRAY_SIZE(mirror_ids),
13335 "%s: bad mirror ids '%s'.\n",
13342 "%s: at least 2 mirror ids needed with '--only' option.\n",
13352 fprintf(stderr, "%s: unrecognized option '%s'\n",
13353 progname, argv[optind - 1]);
13361 if (argc == optind) {
13362 fprintf(stderr, "%s: no file name given.\n", progname);
13367 if (ids_nr > 0 && argc > optind + 1) {
13369 "%s: '--only' cannot be used upon multiple files.\n",
13376 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
13382 for (; optind < argc; optind++) {
13383 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
13393 * lfs_mirror() - Parse and execute lfs mirror commands.
13394 * @argc: The count of lfs mirror command line arguments.
13395 * @argv: Array of strings for lfs mirror command line arguments.
13397 * This function parses lfs mirror commands and performs the
13398 * corresponding functions specified in mirror_cmdlist[].
13400 * Return: 0 on success or an error code on failure.
13402 static int lfs_mirror(int argc, char **argv)
13404 char cmd[PATH_MAX];
13407 setlinebuf(stdout);
13409 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
13411 program_invocation_short_name = cmd;
13412 rc = cfs_parser(argc, argv, mirror_cmdlist);
13414 return rc < 0 ? -rc : rc;
13417 static void lustre_som_swab(struct lustre_som_attrs *attrs)
13419 #if __BYTE_ORDER == __BIG_ENDIAN
13420 __swab16s(&attrs->lsa_valid);
13421 __swab64s(&attrs->lsa_size);
13422 __swab64s(&attrs->lsa_blocks);
13426 enum lfs_som_type {
13427 LFS_SOM_SIZE = 0x1,
13428 LFS_SOM_BLOCKS = 0x2,
13429 LFS_SOM_FLAGS = 0x4,
13430 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
13434 static int lfs_getsom(int argc, char **argv)
13437 struct lustre_som_attrs *attrs;
13438 char buf[sizeof(*attrs) + 64];
13439 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
13442 while ((c = getopt(argc, argv, "bfhs")) != -1) {
13445 type = LFS_SOM_BLOCKS;
13448 type = LFS_SOM_FLAGS;
13451 type = LFS_SOM_SIZE;
13454 fprintf(stderr, "%s: unrecognized option '%s'\n",
13455 progname, argv[optind - 1]);
13466 fprintf(stderr, "%s: %s\n",
13467 progname, argc == 0 ? "miss file target" :
13468 "input more than 2 files");
13473 attrs = (void *)buf;
13474 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
13477 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
13478 argv[0], strerror(errno), errno);
13482 lustre_som_swab(attrs);
13485 case LFS_SOM_ATTR_ALL:
13486 printf("file: %s size: %llu blocks: %llu flags: %x\n",
13487 path, (unsigned long long)attrs->lsa_size,
13488 (unsigned long long)attrs->lsa_blocks,
13492 printf("%llu\n", (unsigned long long)attrs->lsa_size);
13494 case LFS_SOM_BLOCKS:
13495 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
13497 case LFS_SOM_FLAGS:
13498 printf("%x\n", attrs->lsa_valid);
13501 fprintf(stderr, "%s: unknown option\n", progname);
13508 static int lfs_pcc_attach(int argc, char **argv)
13510 struct option long_opts[] = {
13511 { .val = 'h', .name = "help", .has_arg = no_argument },
13512 { .val = 'i', .name = "id", .has_arg = required_argument },
13513 { .val = 'r', .name = "readonly", .has_arg = no_argument },
13514 { .name = NULL } };
13517 __u32 attach_id = 0;
13520 char fullpath[PATH_MAX];
13521 enum lu_pcc_type type = LU_PCC_READWRITE;
13524 while ((c = getopt_long(argc, argv, "hi:r",
13525 long_opts, NULL)) != -1) {
13529 attach_id = strtoul(optarg, &end, 0);
13530 if (errno != 0 || *end != '\0' ||
13531 attach_id == 0 || attach_id > UINT32_MAX) {
13533 "error: %s: bad archive ID '%s'\n",
13539 type = LU_PCC_READONLY;
13544 fprintf(stderr, "%s: unrecognized option '%s'\n",
13545 progname, argv[optind - 1]);
13552 if (attach_id == 0) {
13553 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
13557 if (argc <= optind) {
13558 fprintf(stderr, "%s: must specify one or more file names\n",
13563 while (optind < argc) {
13566 path = argv[optind++];
13567 if (!realpath(path, fullpath)) {
13568 fprintf(stderr, "%s: could not find path '%s': %s\n",
13569 argv[0], path, strerror(errno));
13575 rc2 = llapi_pcc_attach(fullpath, attach_id, type);
13578 "%s: cannot attach '%s' to PCC with attach ID '%u': %s\n",
13579 argv[0], path, attach_id, strerror(-rc2));
13587 static int lfs_pcc_attach_fid(int argc, char **argv)
13589 struct option long_opts[] = {
13590 { .val = 'h', .name = "help", .has_arg = no_argument },
13591 { .val = 'i', .name = "id", .has_arg = required_argument },
13592 { .val = 'r', .name = "readonly", .has_arg = no_argument },
13593 { .val = 'm', .name = "mnt", .has_arg = required_argument },
13594 { .name = NULL } };
13597 __u32 attach_id = 0;
13599 const char *mntpath = NULL;
13600 const char *fidstr;
13601 enum lu_pcc_type type = LU_PCC_READWRITE;
13604 while ((c = getopt_long(argc, argv, "hi:m:r",
13605 long_opts, NULL)) != -1) {
13609 attach_id = strtoul(optarg, &end, 0);
13610 if (errno != 0 || *end != '\0' ||
13611 attach_id > UINT32_MAX) {
13613 "error: %s: bad attach ID '%s'\n",
13619 type = LU_PCC_READONLY;
13625 fprintf(stderr, "%s: unrecognized option '%s'\n",
13626 progname, argv[optind - 1]);
13633 if (attach_id == 0) {
13634 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
13639 fprintf(stderr, "%s: must specify Lustre mount point\n",
13644 if (argc <= optind) {
13645 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
13649 while (optind < argc) {
13652 fidstr = argv[optind++];
13654 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
13658 "%s: cannot attach '%s' on '%s' to PCC with attach ID '%u': %s\n",
13659 argv[0], fidstr, mntpath, attach_id,
13662 if (rc == 0 && rc2 < 0)
13668 static int lfs_pcc_detach(int argc, char **argv)
13670 struct option long_opts[] = {
13671 { .val = 'h', .name = "help", .has_arg = no_argument },
13672 { .val = 'k', .name = "keep", .has_arg = no_argument },
13673 { .name = NULL } };
13677 char fullpath[PATH_MAX];
13678 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13681 while ((c = getopt_long(argc, argv, "hk",
13682 long_opts, NULL)) != -1) {
13685 detach_opt = PCC_DETACH_OPT_NONE;
13688 fprintf(stderr, "%s: unrecognized option '%s'\n",
13689 progname, argv[optind - 1]);
13696 while (optind < argc) {
13699 path = argv[optind++];
13700 if (!realpath(path, fullpath)) {
13701 fprintf(stderr, "%s: could not find path '%s': %s\n",
13702 argv[0], path, strerror(errno));
13708 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
13712 "%s: cannot detach '%s' from PCC: %s\n",
13713 argv[0], path, strerror(errno));
13721 static int lfs_pcc_detach_fid(int argc, char **argv)
13723 struct option long_opts[] = {
13724 { .val = 'h', .name = "help", .has_arg = no_argument },
13725 { .val = 'k', .name = "keep", .has_arg = no_argument },
13726 { .name = NULL } };
13730 const char *mntpath;
13731 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13734 while ((c = getopt_long(argc, argv, "hk",
13735 long_opts, NULL)) != -1) {
13738 detach_opt = PCC_DETACH_OPT_NONE;
13741 fprintf(stderr, "%s: unrecognized option '%s'\n",
13742 progname, argv[optind - 1]);
13749 mntpath = argv[optind++];
13751 while (optind < argc) {
13754 fid = argv[optind++];
13756 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
13759 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
13760 argv[0], fid, mntpath, strerror(-rc2));
13768 static int lfs_pcc_state(int argc, char **argv)
13772 char fullpath[PATH_MAX];
13773 struct lu_pcc_state state;
13778 fprintf(stderr, "%s: must specify one or more file names\n",
13783 while (optind < argc) {
13786 path = argv[optind++];
13787 if (!realpath(path, fullpath)) {
13788 fprintf(stderr, "%s: could not find path '%s': %s\n",
13789 argv[0], path, strerror(errno));
13795 rc2 = llapi_pcc_state_get(fullpath, &state);
13800 "%s: cannot get PCC state of '%s': %s\n",
13801 argv[0], path, strerror(-rc2));
13805 printf("file: %s", path);
13806 printf(", type: %s", pcc_type2string(state.pccs_type));
13807 if (state.pccs_type == LU_PCC_NONE &&
13808 state.pccs_open_count == 0) {
13813 printf(", PCC file: %s", state.pccs_path);
13814 printf(", user number: %u", state.pccs_open_count);
13815 printf(", flags: %x", state.pccs_flags);
13822 * lfs_pcc() - Parse and execute lfs pcc commands.
13823 * @argc: The count of lfs pcc command line arguments.
13824 * @argv: Array of strings for lfs pcc command line arguments.
13826 * This function parses lfs pcc commands and performs the
13827 * corresponding functions specified in pcc_cmdlist[].
13829 * Return: 0 on success or an error code on failure.
13831 static int lfs_pcc(int argc, char **argv)
13833 char cmd[PATH_MAX];
13836 setlinebuf(stdout);
13838 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
13840 program_invocation_short_name = cmd;
13841 rc = cfs_parser(argc, argv, pcc_cmdlist);
13843 return rc < 0 ? -rc : rc;
13846 int main(int argc, char **argv)
13850 /* Ensure that liblustreapi constructor has run */
13851 if (!llapi_liblustreapi_initialized())
13852 fprintf(stderr, "liblustreapi was not properly initialized\n");
13854 setlinebuf(stdout);
13857 progname = program_invocation_short_name; /* Used in error messages */
13858 llapi_set_command_name(argv[1]);
13859 rc = cfs_parser(argc, argv, cmdlist);
13860 llapi_clear_command_name();
13862 return rc < 0 ? -rc : rc;