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>
83 # define NSEC_PER_SEC 1000000000UL
85 #define ONE_MB 0x100000
88 static int lfs_find(int argc, char **argv);
89 static int lfs_getstripe(int argc, char **argv);
90 static int lfs_getdirstripe(int argc, char **argv);
91 static int lfs_setdirstripe(int argc, char **argv);
92 static int lfs_rmentry(int argc, char **argv);
93 static int lfs_unlink_foreign(int argc, char **argv);
94 static int lfs_osts(int argc, char **argv);
95 static int lfs_mdts(int argc, char **argv);
96 static int lfs_df(int argc, char **argv);
97 static int lfs_getname(int argc, char **argv);
98 static int lfs_check(int argc, char **argv);
99 #ifdef HAVE_SYS_QUOTA_H
100 static int lfs_setquota(int argc, char **argv);
101 static int lfs_quota(int argc, char **argv);
102 static int lfs_project(int argc, char **argv);
104 static int lfs_flushctx(int argc, char **argv);
105 static int lfs_poollist(int argc, char **argv);
106 static int lfs_changelog(int argc, char **argv);
107 static int lfs_changelog_clear(int argc, char **argv);
108 static int lfs_fid2path(int argc, char **argv);
109 static int lfs_path2fid(int argc, char **argv);
110 static int lfs_rmfid(int argc, char **argv);
111 static int lfs_data_version(int argc, char **argv);
112 static int lfs_hsm_state(int argc, char **argv);
113 static int lfs_hsm_set(int argc, char **argv);
114 static int lfs_hsm_clear(int argc, char **argv);
115 static int lfs_hsm_action(int argc, char **argv);
116 static int lfs_hsm_archive(int argc, char **argv);
117 static int lfs_hsm_restore(int argc, char **argv);
118 static int lfs_hsm_release(int argc, char **argv);
119 static int lfs_hsm_remove(int argc, char **argv);
120 static int lfs_hsm_cancel(int argc, char **argv);
121 static int lfs_swap_layouts(int argc, char **argv);
122 static int lfs_mv(int argc, char **argv);
123 static int lfs_ladvise(int argc, char **argv);
124 static int lfs_getsom(int argc, char **argv);
125 static int lfs_heat_get(int argc, char **argv);
126 static int lfs_heat_set(int argc, char **argv);
127 static int lfs_mirror(int argc, char **argv);
128 static inline int lfs_mirror_resync(int argc, char **argv);
129 static inline int lfs_mirror_verify(int argc, char **argv);
130 static inline int lfs_mirror_read(int argc, char **argv);
131 static inline int lfs_mirror_write(int argc, char **argv);
132 static inline int lfs_mirror_copy(int argc, char **argv);
133 static int lfs_pcc_attach(int argc, char **argv);
134 static int lfs_pcc_attach_fid(int argc, char **argv);
135 static int lfs_pcc_detach(int argc, char **argv);
136 static int lfs_pcc_detach_fid(int argc, char **argv);
137 static int lfs_pcc_state(int argc, char **argv);
138 static int lfs_pcc(int argc, char **argv);
140 static int lfs_migrate_to_dom(int fd_src, int fd_dst, char *name,
141 __u64 migration_flags,
142 unsigned long long bandwidth_bytes_sec,
143 long stats_interval_sec);
145 struct pool_to_id_cbdata {
150 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
151 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
153 enum setstripe_origin {
163 static int lfs_setstripe_internal(int argc, char **argv,
164 enum setstripe_origin opc);
166 static inline int lfs_setstripe(int argc, char **argv)
168 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
171 static inline int lfs_setstripe_migrate(int argc, char **argv)
173 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
176 static inline int lfs_mirror_create(int argc, char **argv)
178 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
181 static inline int lfs_mirror_extend(int argc, char **argv)
183 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
186 static inline int lfs_mirror_split(int argc, char **argv)
188 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
191 static inline int lfs_mirror_delete(int argc, char **argv)
193 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
196 /* Setstripe and migrate share mostly the same parameters */
197 #define SSM_CMD_COMMON(cmd) \
198 "usage: "cmd" [--component-end|-E COMP_END]\n" \
199 " [--copy=LUSTRE_SRC]\n" \
200 " [--extension-size|--ext-size|-z SIZE]\n" \
201 " [--help|-h] [--layout|-L PATTERN]\n" \
202 " [--layout|-L PATTERN]\n" \
203 " [--mirror-count|-N[MIRROR_COUNT]]\n" \
204 " [--ost|-o OST_INDICES]\n" \
205 " [--overstripe-count|-C STRIPE_COUNT]\n" \
206 " [--pool|-p POOL_NAME]\n" \
207 " [--stripe-count|-c STRIPE_COUNT]\n" \
208 " [--stripe-index|-i START_OST_IDX]\n" \
209 " [--stripe-size|-S STRIPE_SIZE]\n" \
210 " [--yaml|-y YAML_TEMPLATE_FILE]\n"
212 #define MIRROR_EXTEND_USAGE \
213 " {--mirror-count|-N[MIRROR_COUNT]}\n" \
214 " [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n" \
217 #define SETSTRIPE_USAGE \
218 SSM_CMD_COMMON("setstripe") \
219 MIRROR_EXTEND_USAGE \
220 " DIRECTORY|FILENAME\n"
222 #define MIGRATE_USAGE \
223 SSM_CMD_COMMON("migrate ") \
224 " [--block|-b] [--non-block|-n]\n" \
225 " [--non-direct|-D] [--verbose|-v]\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 " [--default|-D] [--mode|-o mode]\n" \
233 " [--max-inherit|-X max_inherit]\n" \
234 " [--max-inherit-rr max_inherit_rr] <dir>\n" \
235 "To create dir with a foreign (free format) layout :\n" \
236 "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
237 " [--mode|-o MODE] [--flags HEX] DIRECTORY\n"
240 * command_t mirror_cmdlist - lfs mirror commands.
242 command_t mirror_cmdlist[] = {
243 { .pc_name = "create", .pc_func = lfs_mirror_create,
244 .pc_help = "Create a mirrored file.\n"
245 "usage: lfs mirror create --mirror-count|-N[MIRROR_COUNT]\n"
246 " [SETSTRIPE_OPTIONS] ... FILENAME|DIRECTORY ...\n" },
247 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
248 .pc_help = "Delete a mirror from a file.\n"
249 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
250 "\t --component-id|--comp-id|-I COMP_ID |\n"
251 "\t -p <pool>} MIRRORED_FILE ...\n"
253 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
254 .pc_help = "Extend a mirrored file.\n"
255 "usage: lfs mirror extend "
256 "{--mirror-count|-N[MIRROR_COUNT]} [--no-verify]|\n"
257 "\t\t--stats|--stats-interval=<sec>|\n"
258 "\t\t--W <bandwidth>|--bandwidth-limit=<bandwidth>\n"
259 "\t\t[SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... FILENAME ...\n" },
260 { .pc_name = "split", .pc_func = lfs_mirror_split,
261 .pc_help = "Split a mirrored file.\n"
262 "usage: lfs mirror split {--mirror-id MIRROR_ID |\n"
263 "\t --component-id|-I COMP_ID|-p POOL} [--destroy|-d]\n"
264 "\t [-f NEW_FILE] MIRRORED_FILE ...\n" },
265 { .pc_name = "read", .pc_func = lfs_mirror_read,
266 .pc_help = "Read the content of a specified mirror of a file.\n"
267 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
268 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
269 { .pc_name = "write", .pc_func = lfs_mirror_write,
270 .pc_help = "Write to a specified mirror of a file.\n"
271 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
272 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
273 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
274 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
275 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
276 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
277 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
278 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
279 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]|\n"
280 "\t\t--stats|--stats-interval=<sec>|\n"
281 "\t\t--W <bandwidth>|--bandwidth-limit=<bandwidth>\n"
282 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
283 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
284 .pc_help = "Verify mirrored file(s).\n"
285 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
286 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
291 * command_t pcc_cmdlist - lfs pcc commands.
293 command_t pcc_cmdlist[] = {
294 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
295 .pc_help = "Attach given files to the Persistent Client Cache.\n"
296 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
297 "\t-i: archive id for RW-PCC\n" },
298 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
299 .pc_help = "Attach given files into PCC by FID(s).\n"
300 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
301 "\t-i: archive id for RW-PCC\n"
302 "\t-m: Lustre mount point\n" },
303 { .pc_name = "state", .pc_func = lfs_pcc_state,
304 .pc_help = "Display the PCC state for given files.\n"
305 "usage: lfs pcc state <file> ...\n" },
306 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
307 .pc_help = "Detach given files from the Persistent Client Cache.\n"
308 "usage: lfs pcc detach <file> ...\n" },
309 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
310 .pc_help = "Detach given files from PCC by FID(s).\n"
311 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
315 /* all available commands */
316 command_t cmdlist[] = {
317 {"setstripe", lfs_setstripe, 0,
318 "To create a file with specified striping/composite layout, or\n"
319 "create/replace the default layout on an existing directory:\n"
320 SSM_CMD_COMMON("setstripe")
322 " <directory|filename>\n"
324 "To add component(s) to an existing composite file:\n"
325 SSM_CMD_COMMON("setstripe --component-add")
326 "To totally delete the default striping from an existing directory:\n"
327 "usage: setstripe [--delete|-d] <directory>\n"
329 "To create a mirrored file or set s default mirror layout on a directory:\n"
330 "usage: setstripe {--mirror-count|-N}[MIRROR_COUNT] [SETSTRIPE_OPTIONS] <directory|filename>\n"
332 "To delete the last component(s) from an existing composite file\n"
333 "(note that this will also delete any data in those components):\n"
334 "usage: setstripe --component-del [--component-id|-I COMP_ID]\n"
335 " [--component-flags|-F COMP_FLAGS]\n"
337 "\tCOMP_ID: Unique component ID to delete\n"
338 "\tCOMP_FLAGS: 'init' indicating all instantiated components\n"
339 "\t '^init' indicating all uninstantiated components\n"
340 "\t-I and -F cannot be specified at the same time\n"
342 "To set or clear flags on a specific component\n"
343 "(note that this command can only be applied to mirrored files:\n"
344 "usage: setstripe --comp-set {-I COMP_ID|--comp-flags=COMP_FLAGS}\n"
347 "To create a file with a foreign (free format) layout:\n"
348 "usage: setstripe --foreign[=FOREIGN_TYPE]\n"
349 " --xattr|-x LAYOUT_STRING [--flags HEX]\n"
350 " [--mode MODE] <filename>\n"},
351 {"getstripe", lfs_getstripe, 0,
352 "To list the layout pattern for a given file or files in a\n"
353 "directory or recursively for all files in a directory tree.\n"
354 "usage: getstripe [--ost|-O UUID] [--quiet|-q] [--verbose|-v]\n"
355 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
356 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
357 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
358 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
359 " [--help|-h] [--hex-idx]\n"
360 " [--component-id|-I[=COMP_ID]]\n"
361 " [--component-flags[=COMP_FLAGS]]\n"
362 " [--component-count]\n"
363 " [--extension-size|--ext-size|-z]\n"
364 " [--component-start[=[+-]COMP_START]]\n"
365 " [--component-end[=[+-]COMP_END]|-E[[+-]comp_end]]\n"
366 " [[!] --mirror-index=[+-]INDEX |\n"
367 " [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
369 " <directory|filename> ..."},
370 {"setdirstripe", lfs_setdirstripe, 0,
371 "Create striped directory on specified MDT, same as mkdir.\n"
372 "May be restricted to root or group users, depending on settings.\n"
373 "usage: setdirstripe [OPTION] <directory>\n"
375 {"getdirstripe", lfs_getdirstripe, 0,
376 "To list the layout pattern info for a given directory\n"
377 "or recursively for all directories in a directory tree.\n"
378 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
379 " [--help|-h] [--hex-idx] [--mdt-hash|-H]\n"
380 " [--obd|-O UUID] [--recursive|-r] [--raw|-R]\n"
381 " [--yaml|-y] [--verbose|-v] [--default|-D]\n"
382 " [--max-inherit|-X]\n"
383 " [--max-inherit-rr] <dir> ..."},
384 {"mkdir", lfs_setdirstripe, 0,
385 "Create striped directory on specified MDT, same as setdirstripe.\n"
386 "usage: mkdir [OPTION] <directory>\n"
388 {"rm_entry", lfs_rmentry, 0,
389 "To remove the name entry of the remote directory. Note: This\n"
390 "command will only delete the name entry, i.e. the remote directory\n"
391 "will become inaccessable after this command. This can only be done\n"
392 "by the administrator\n"
393 "usage: rm_entry <dir>\n"},
394 {"rmentry", lfs_rmentry, 0, "remove a dir entry, same as 'rm_entry'\n"},
395 {"unlink_foreign", lfs_unlink_foreign, 0,
396 "To remove the foreign file/dir.\n"
397 "Note: This is for files/dirs prevented to be removed using\n"
398 "unlink/rmdir, but works also for regular ones\n"
399 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
400 {"pool_list", lfs_poollist, 0,
401 "List pools or pool OSTs\n"
402 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
403 {"find", lfs_find, 0,
404 "find files matching given attributes recursively in directory tree.\n"
405 "usage: find <directory|filename> ...\n"
406 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --btime|-B [+-]N[smhdwy]]\n"
407 " [[!] --ctime|-C [+-]N[smhdwy]] [[!] --mtime|-M [+-]N[smhdwy]]\n"
408 " [[!] --attrs=[^]ATTR[,...]]\n"
409 " [[!] --blocks|-b N] [[!] --component-count [+-]<comp_cnt>]\n"
410 " [[!] --component-start [+-]N[kMGTPE]]\n"
411 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
412 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
413 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
414 " [[!] --foreign[=<foreign_type>]]\n"
415 " [[!] --gid|-g|--group|-G <gid>|<gname>] [--help|-h]\n"
416 " [[!] --layout|-L released,raid0,mdt] [--lazy|-l] [[!] --links [+-]n]\n"
417 " [--maxdepth|-D N] [[!] --mdt-count|-T [+-]<stripes>]\n"
418 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
419 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
420 " [[!] --mirror-count|-N [+-]<n>]\n"
421 " [[!] --mirror-state <[^]state>]\n"
422 " [[!] --name|-n <pattern>] [[!] --newer[XY] <reference>]\n"
423 " [[!] --ost|-O <uuid|index,...>] [[!] --perm [/-]mode]\n"
424 " [[!] --pool <pool>] [--print|-P] [--print0|-0] [--printf <format>]\n"
425 " [[!] --projid <projid>] [[!] --size|-s [+-]N[bkMGTPE]]\n"
426 " [[!] --stripe-count|-c [+-]<stripes>]\n"
427 " [[!] --stripe-index|-i <index,...>]\n"
428 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
429 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
430 "\t !: used before an option indicates 'NOT' requested attribute\n"
431 "\t -: used before a value indicates less than requested value\n"
432 "\t +: used before a value indicates more than requested value\n"
433 "\t ^: used before a flag indicates to exclude it\n"},
434 {"check", lfs_check, 0,
435 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
436 "or all the servers (MGTs, MDTs and OSTs) [for specified path only].\n"
437 "usage: check {mgts|osts|mdts|all} [path]"},
438 {"osts", lfs_osts, 0, "list OSTs connected to client "
439 "[for specified path only]\n" "usage: osts [path]"},
440 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
441 "[for specified path only]\n" "usage: mdts [path]"},
443 "report filesystem disk space usage or inodes usage "
444 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
445 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
446 " [--pool|-p <fsname>[.<pool>]] [path]"},
447 {"getname", lfs_getname, 0,
448 "list instances and specified mount points [for specified path only]\n"
449 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
450 #ifdef HAVE_SYS_QUOTA_H
451 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
452 "usage: setquota [-t][-D] {-u|-U|-g|-G|-p|-P} {-b|-B|-i|-I LIMIT} [--pool POOL] FILESYSTEM\n"
453 " setquota {-u|-g|-p} --delete FILESYSTEM\n"},
454 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
455 "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
456 " [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
457 " [--pool <OST pool name>] <filesystem>\n"
458 " quota -t <-u|-g|-p> [--pool <OST pool name>] <filesystem>\n"
459 " quota [-q] [-v] [h] {-U|-G|-P} [--pool <OST pool name>] <filesystem>"},
460 {"project", lfs_project, 0,
461 "Change or list project attribute for specified file or directory.\n"
462 "usage: project [-d|-r] <file|directory...>\n"
463 " list project ID and flags on file(s) or directories\n"
464 " project [-p id] [-s] [-r] <file|directory...>\n"
465 " set project ID and/or inherit flag for specified file(s) or directories\n"
466 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
467 " check project ID and flags on file(s) or directories, print outliers\n"
468 " project -C [-d|-r] [-k] <file|directory...>\n"
469 " clear the project inherit flag and ID on the file or directory\n"
472 {"flushctx", lfs_flushctx, 0,
473 "Flush security context for current user.\n"
474 "usage: flushctx [-k] [-r] [mountpoint...]"},
475 {"changelog", lfs_changelog, 0,
476 "Show the metadata changes on an MDT."
477 "\nusage: changelog <mdtname> [startrec [endrec]]"},
478 {"changelog_clear", lfs_changelog_clear, 0,
479 "Indicate that old changelog records up to <endrec> are no longer of "
480 "interest to consumer <id>, allowing the system to free up space.\n"
481 "An <endrec> of 0 means all records.\n"
482 "usage: changelog_clear <mdtname> <id> <endrec>"},
483 {"fid2path", lfs_fid2path, 0,
484 "Resolve the full path(s) for given FID(s). For a specific hardlink "
485 "specify link number <linkno>.\n"
486 "usage: fid2path [--print0|-0] [--print-fid|-f] [--print-link|-c] "
487 "[--link|-l <linkno>] [--name|-n] <fsname|root> <fid>..."},
488 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
489 "usage: path2fid [--parents] <path> ..."},
490 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
491 "usage: rmfid <fsname|rootpath> <fid> ..."},
492 {"data_version", lfs_data_version, 0, "Display file data version for "
493 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
494 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
495 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
496 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
497 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
498 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
499 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
501 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
502 "[--archived] [--lost] <file> ..."},
503 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
504 "given files.\n" "usage: hsm_action <file> ..."},
505 {"hsm_archive", lfs_hsm_archive, 0,
506 "Archive file to external storage.\n"
507 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
509 {"hsm_restore", lfs_hsm_restore, 0,
510 "Restore file from external storage.\n"
511 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
512 {"hsm_release", lfs_hsm_release, 0,
513 "Release files from Lustre.\n"
514 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
515 {"hsm_remove", lfs_hsm_remove, 0,
516 "Remove file copy from external storage.\n"
517 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
519 " (FILE [FILE ...] | "
520 "--mntpath MOUNTPATH FID [FID ...])\n"
522 "Note: To remove an archived copy of a file already deleted from a "
524 "--mntpath option and a list of FIDs must be specified"
526 {"hsm_cancel", lfs_hsm_cancel, 0,
527 "Cancel requests related to specified files.\n"
528 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
529 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
530 "usage: swap_layouts <path1> <path2>"},
531 {"migrate", lfs_setstripe_migrate, 0,
532 "migrate directories and their inodes between MDTs.\n"
533 "usage: migrate [--mdt-count|-c STRIPE_COUNT] [--directory|-d]\n"
534 " [--mdt-hash|-H HASH_TYPE]\n"
535 " [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
538 "migrate file objects from one OST layout to another\n"
539 "(may be not safe with concurent writes).\n"
542 "To move directories between MDTs. This command is deprecated, "
543 "use \"migrate\" instead.\n"
544 "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
546 {"ladvise", lfs_ladvise, 0,
547 "Provide servers with advice about access patterns for a file.\n"
548 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
549 " [--background|-b] [--unset|-u]\n\n"
550 " {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
551 " {[--mode|-m [READ,WRITE]}\n"
553 {"mirror", lfs_mirror, mirror_cmdlist,
554 "lfs commands used to manage files with mirrored components:\n"
555 "lfs mirror create - create a mirrored file or directory\n"
556 "lfs mirror extend - add mirror(s) to an existing file\n"
557 "lfs mirror split - split a mirror from an existing mirrored file\n"
558 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
559 "lfs mirror read - read a mirror content of a mirrored file\n"
560 "lfs mirror write - write to a mirror of a mirrored file\n"
561 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
562 "lfs mirror verify - verify mirrored file(s)\n"},
563 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
564 "usage: getsom [-s] [-b] [-f] <path>\n"
565 "\t-s: Only show the size value of the SOM data for a given file\n"
566 "\t-b: Only show the blocks value of the SOM data for a given file\n"
567 "\t-f: Only show the flags value of the SOM data for a given file\n"},
568 {"heat_get", lfs_heat_get, 0,
569 "To get heat of files.\n"
570 "usage: heat_get <file> ...\n"},
571 {"heat_set", lfs_heat_set, 0,
572 "To set heat flags of files.\n"
573 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
574 "\t--clear|-c: Clear file heat for given files\n"
575 "\t--off|-o: Turn off file heat for given files\n"
576 "\t--on|-O: Turn on file heat for given files\n"},
577 {"pcc", lfs_pcc, pcc_cmdlist,
578 "lfs commands used to interact with PCC features:\n"
579 "lfs pcc attach - attach given files to Persistent Client Cache\n"
580 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
581 "lfs pcc state - display the PCC state for given files\n"
582 "lfs pcc detach - detach given files from Persistent Client Cache\n"
583 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
587 static int check_hashtype(const char *hashtype)
589 int type_num = atoi(hashtype);
592 /* numeric hash type */
593 if (hashtype && lmv_is_known_hash_type(type_num))
595 /* string hash type */
596 for (i = LMV_HASH_TYPE_ALL_CHARS; i < ARRAY_SIZE(mdt_hash_name); i++)
597 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
603 static uint32_t check_foreign_type_name(const char *foreign_type_name)
607 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
608 if (!lu_foreign_types[i].lft_name)
610 if (strcmp(foreign_type_name,
611 lu_foreign_types[i].lft_name) == 0)
612 return lu_foreign_types[i].lft_type;
615 return LU_FOREIGN_TYPE_UNKNOWN;
618 static const char *error_loc = "syserror";
621 migrate_open_files(const char *name, __u64 migration_flags,
622 const struct llapi_stripe_param *param,
623 struct llapi_layout *layout, int *fd_src_ptr,
631 char parent[PATH_MAX];
632 char volatile_file[PATH_MAX];
638 if (!param && !layout) {
639 error_loc = "layout information";
643 /* search for file directory pathname */
644 if (strlen(name) > sizeof(parent) - 1) {
645 error_loc = "source file name";
649 strncpy(parent, name, sizeof(parent));
650 ptr = strrchr(parent, '/');
652 if (!getcwd(parent, sizeof(parent))) {
653 error_loc = "getcwd";
657 if (ptr == parent) /* leading '/' */
662 /* even if the file is only read, WR mode is nedeed to allow
665 /* Allow migrating even without the key on encrypted files */
666 rflags = O_RDWR | O_NOATIME | O_CIPHERTEXT;
667 if (!(migration_flags & LLAPI_MIGRATION_NONDIRECT))
670 fd_src = open(name, rflags);
672 /* If encrypted file without the key,
673 * retry mirror extend in O_DIRECT.
675 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
676 migration_flags & LLAPI_MIGRATION_MIRROR) {
681 error_loc = "cannot open source file";
685 rc = llapi_file_fget_mdtidx(fd_src, &mdt_index);
687 error_loc = "cannot get MDT index";
692 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
693 /* Allow migrating without the key on encrypted files */
695 mode_t open_mode = S_IRUSR | S_IWUSR;
697 if (rflags & O_DIRECT)
698 open_flags |= O_DIRECT;
699 random_value = random();
700 rc = snprintf(volatile_file, sizeof(volatile_file),
701 "%s/%s:%.4X:%.4X:fd=%.2d", parent,
702 LUSTRE_VOLATILE_HDR, mdt_index,
703 random_value, fd_src);
704 if (rc >= sizeof(volatile_file)) {
709 /* create, open a volatile file, use caching (ie no directio) */
711 /* Returns -1 and sets errno on error: */
712 fd_dst = llapi_layout_file_open(volatile_file,
713 open_flags, open_mode,
718 /* Does the right thing on error: */
719 fd_dst = llapi_file_open_param(volatile_file,
723 } while (fd_dst < 0 && (rc = fd_dst) == -EEXIST);
726 error_loc = "cannot create volatile file";
731 * In case the MDT does not support creation of volatile files
732 * we should try to unlink it.
734 (void)unlink(volatile_file);
737 * Not-owner (root?) special case.
738 * Need to set owner/group of volatile file like original.
739 * This will allow to pass related check during layout_swap.
741 rc = fstat(fd_src, &st);
744 error_loc = "cannot stat source file";
748 rc = fstat(fd_dst, &stv);
751 error_loc = "cannot stat volatile";
755 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
756 rc = fchown(fd_dst, st.st_uid, st.st_gid);
759 error_loc = "cannot change ownwership of volatile";
771 *fd_src_ptr = fd_src;
772 *fd_dst_ptr = fd_dst;
778 struct timespec timespec_sub(struct timespec *before, struct timespec *after)
782 ret.tv_sec = after->tv_sec - before->tv_sec;
783 if (after->tv_nsec < before->tv_nsec) {
785 ret.tv_nsec = NSEC_PER_SEC + after->tv_nsec - before->tv_nsec;
787 ret.tv_nsec = after->tv_nsec - before->tv_nsec;
793 static void stats_log(struct timespec *now, struct timespec *start_time,
794 ssize_t read_bytes, size_t write_bytes,
795 off_t file_size_bytes)
797 struct timespec diff = timespec_sub(start_time, now);
799 if (file_size_bytes == 0)
802 if (diff.tv_sec == 0 && diff.tv_nsec == 0)
805 printf("- { seconds: %li, rmbps: %5.2g, wmbps: %5.2g, copied: %lu, size: %lu, pct: %lu%% }\n",
807 (double) read_bytes/((ONE_MB * diff.tv_sec) +
808 ((ONE_MB * diff.tv_nsec)/NSEC_PER_SEC)),
809 (double) write_bytes/((ONE_MB * diff.tv_sec) +
810 ((ONE_MB * diff.tv_nsec)/NSEC_PER_SEC)),
811 write_bytes/ONE_MB, file_size_bytes/ONE_MB,
812 ((write_bytes*100)/file_size_bytes));
815 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int),
816 unsigned long long bandwidth_bytes_sec,
817 long stats_interval_sec, off_t file_size_bytes)
819 struct llapi_layout *layout;
820 size_t buf_size = 64 * ONE_MB;
821 uint64_t stripe_size = ONE_MB;
825 size_t page_size = sysconf(_SC_PAGESIZE);
828 size_t write_bytes = 0;
829 ssize_t read_bytes = 0;
830 struct timespec start_time;
832 struct timespec last_bw_print;
834 layout = llapi_layout_get_by_fd(fd_src, 0);
836 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
838 /* We like big bufs */
839 if (stripe_size > buf_size)
840 buf_size = stripe_size;
842 /* Trim to stripe_size multiple */
843 buf_size -= buf_size % stripe_size;
846 llapi_layout_free(layout);
849 /* limit transfer size to what can be sent in one second */
850 if (bandwidth_bytes_sec && bandwidth_bytes_sec < buf_size)
851 buf_size = (bandwidth_bytes_sec + stripe_size - 1) &
854 /* Use a page-aligned buffer for direct I/O */
855 rc = posix_memalign(&buf, page_size, buf_size);
859 sparse = llapi_file_is_sparse(fd_src);
861 rc = ftruncate(fd_dst, pos);
868 clock_gettime(CLOCK_REALTIME, &start_time);
869 now = last_bw_print = start_time;
873 size_t to_read, to_write;
876 if (sparse && pos >= data_end) {
879 data_off = llapi_data_seek(fd_src, pos, &data_size);
881 /* Non-fatal, switch to full copy */
885 /* hole at the end of file, truncate up to it */
887 rc = ftruncate(fd_dst, data_off);
891 pos = data_off & ~(page_size - 1);
892 data_end = data_off + data_size;
893 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
894 to_read = MIN(to_read, buf_size);
900 rc = check_file(fd_src);
905 rsize = pread(fd_src, buf, to_read, pos);
916 while (to_write > 0) {
917 unsigned long long write_target;
919 struct timespec diff;
921 written = pwrite(fd_dst, buf, to_write, pos);
928 write_bytes += written;
930 if (bandwidth_bytes_sec == 0)
933 clock_gettime(CLOCK_REALTIME, &now);
934 diff = timespec_sub(&start_time, &now);
935 write_target = ((bandwidth_bytes_sec * diff.tv_sec) +
936 ((bandwidth_bytes_sec *
937 diff.tv_nsec)/NSEC_PER_SEC));
939 if (write_target < write_bytes) {
940 unsigned long long excess;
941 struct timespec delay = { 0, 0 };
943 excess = write_bytes - write_target;
948 delay.tv_sec = excess / bandwidth_bytes_sec;
949 delay.tv_nsec = (excess % bandwidth_bytes_sec) *
950 NSEC_PER_SEC / bandwidth_bytes_sec;
953 rc = clock_nanosleep(CLOCK_REALTIME, 0,
955 } while (rc < 0 && errno == EINTR);
958 if (stats_interval_sec)
960 "error %s: delay for bandwidth control failed: %s\n",
968 clock_gettime(CLOCK_REALTIME, &now);
969 if (stats_interval_sec && (write_bytes != file_size_bytes) &&
970 (now.tv_sec >= last_bw_print.tv_sec +
971 stats_interval_sec)) {
972 stats_log(&now, &start_time,
973 read_bytes, write_bytes,
978 if (rc || rsize < to_read)
982 /* Output at least one log, regardless of stats_interval */
983 if (stats_interval_sec) {
984 clock_gettime(CLOCK_REALTIME, &now);
985 stats_log(&now, &start_time, read_bytes, write_bytes,
993 /* Try to avoid page cache pollution after migration. */
994 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
995 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1001 static int migrate_set_timestamps(int fd, const struct stat *st)
1003 struct timeval tv[2] = {
1004 {.tv_sec = st->st_atime},
1005 {.tv_sec = st->st_mtime}
1008 return futimes(fd, tv);
1011 static int migrate_block(int fd_src, int fd_dst,
1012 unsigned long long bandwidth_bytes_sec,
1013 long stats_interval_sec)
1026 /* The grouplock blocks all concurrent accesses to the file. */
1027 rc = llapi_group_lock(fd_src, gid);
1029 error_loc = "cannot get group lock";
1033 rc = fstat(fd_src, &st);
1035 error_loc = "cannot stat source file";
1041 * LL_DV_RD_FLUSH should not be set, otherwise the servers will try to
1042 * get extent locks on the OST objects. This will conflict with our
1043 * extent group locks.
1045 rc = llapi_get_data_version(fd_src, &dv1, 0);
1047 error_loc = "cannot get dataversion";
1051 rc = migrate_copy_data(fd_src, fd_dst, NULL, bandwidth_bytes_sec,
1052 stats_interval_sec, st.st_size);
1054 error_loc = "data copy failed";
1058 /* Make sure we keep original atime/mtime values */
1059 rc = migrate_set_timestamps(fd_dst, &st);
1061 error_loc = "set target file timestamp failed";
1067 * for a migration we need to check data version on file did
1070 * Pass in gid=0 since we already own grouplock.
1072 rc = llapi_fswap_layouts_grouplock(fd_src, fd_dst, dv1, 0, 0,
1073 SWAP_LAYOUTS_CHECK_DV1);
1074 if (rc == -EAGAIN) {
1075 error_loc = "file changed";
1077 } else if (rc < 0) {
1078 error_loc = "cannot swap layout";
1083 rc2 = llapi_group_unlock(fd_src, gid);
1084 if (rc2 < 0 && rc == 0) {
1085 error_loc = "unlock group lock";
1093 * Internal helper for migrate_copy_data(). Check lease and report error if
1096 * \param[in] fd File descriptor on which to check the lease.
1098 * \retval 0 Migration can keep on going.
1099 * \retval -errno Error occurred, abort migration.
1101 static int check_lease(int fd)
1105 rc = llapi_lease_check(fd);
1107 return 0; /* llapi_check_lease returns > 0 on success. */
1112 static int migrate_nonblock(int fd_src, int fd_dst,
1113 unsigned long long bandwidth_bytes_sec,
1114 long stats_interval_sec)
1121 rc = fstat(fd_src, &st);
1123 error_loc = "cannot stat source file";
1127 rc = llapi_get_data_version(fd_src, &dv1, LL_DV_RD_FLUSH);
1129 error_loc = "cannot get data version";
1133 rc = migrate_copy_data(fd_src, fd_dst, check_lease,
1134 bandwidth_bytes_sec,
1135 stats_interval_sec, st.st_size);
1137 error_loc = "data copy failed";
1141 rc = llapi_get_data_version(fd_src, &dv2, LL_DV_RD_FLUSH);
1143 error_loc = "cannot get data version";
1149 error_loc = "source file changed";
1153 /* Make sure we keep original atime/mtime values */
1154 rc = migrate_set_timestamps(fd_dst, &st);
1156 error_loc = "set target file timestamp failed";
1163 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1165 struct pool_to_id_cbdata data = { .pool = pool };
1166 struct llapi_layout *layout = NULL;
1169 layout = llapi_layout_get_by_path(fname, 0);
1172 "error %s: file '%s' couldn't get layout: rc=%d\n",
1173 progname, fname, errno);
1177 rc = llapi_layout_sanity(layout, false, true);
1179 llapi_layout_sanity_perror(errno);
1182 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1191 llapi_layout_free(layout);
1195 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1196 __u32 flags, __u32 neg_flags)
1199 __u32 flags_array[2];
1206 "error %s: neither component id nor pool is specified\n",
1210 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1216 ids[count] = comp_id;
1217 flags_array[count] = flags;
1222 if (neg_flags & LCME_FL_STALE) {
1224 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1229 ids[count] = comp_id;
1230 flags_array[count] = neg_flags | LCME_FL_NEG;
1234 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1236 if (errno == EUCLEAN) {
1239 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1240 progname, comp_id, fname);
1243 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1244 progname, comp_id, fname, flags, neg_flags);
1251 static int lfs_component_del(char *fname, __u32 comp_id,
1252 __u32 flags, __u32 neg_flags)
1256 if (flags && neg_flags) {
1258 "%s: cannot specify both positive and negative flags\n",
1263 if (!flags && neg_flags)
1264 flags = neg_flags | LCME_FL_NEG;
1266 if (flags && comp_id) {
1268 "%s: cannot specify component ID and flags at the same time\n",
1273 if (!flags && !comp_id) {
1275 "%s: neither flags nor component ID is specified\n",
1281 if (flags & ~LCME_KNOWN_FLAGS) {
1283 "%s setstripe: unknown flags %#x\n",
1287 } else if (comp_id > LCME_ID_MAX) {
1288 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1293 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1296 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1297 progname, comp_id, fname, strerror(errno));
1301 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1308 rc = llapi_layout_file_comp_add(fname, layout);
1310 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1311 fname, strerror(errno));
1315 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1316 struct llapi_layout *layout)
1324 fd = lstat(fname, &st);
1325 if (fd == 0 && S_ISDIR(st.st_mode))
1326 open_flags = O_DIRECTORY | O_RDONLY;
1328 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1330 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1331 S_ISDIR(st.st_mode) ?
1332 "set default composite layout for" :
1333 "create composite file",
1334 fname, strerror(errno));
1338 static int lfs_migrate(char *name, __u64 migration_flags,
1339 struct llapi_stripe_param *param,
1340 struct llapi_layout *layout,
1341 unsigned long long bandwidth_bytes_sec,
1342 long stats_interval_sec)
1344 struct llapi_layout *existing;
1345 uint64_t dom_new, dom_cur;
1350 rc = migrate_open_files(name, migration_flags, param, layout,
1355 rc = llapi_layout_dom_size(layout, &dom_new);
1357 error_loc = "cannot get new layout DoM size";
1360 /* special case for migration to DOM layout*/
1361 existing = llapi_layout_get_by_fd(fd_src, 0);
1363 error_loc = "cannot get existing layout";
1367 rc = llapi_layout_dom_size(existing, &dom_cur);
1369 error_loc = "cannot get current layout DoM size";
1374 * if file has DoM layout already then migration is possible to
1375 * the new layout with the same DoM component via swap layout,
1376 * if new layout used bigger DOM size, then mirroring is used
1378 if (dom_new > dom_cur) {
1379 rc = lfs_migrate_to_dom(fd_src, fd_dst, name,
1381 bandwidth_bytes_sec,
1382 stats_interval_sec);
1384 error_loc = "cannot migrate to DOM layout";
1388 if (stats_interval_sec)
1389 printf("%s:\n", name);
1391 if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1393 * Blocking mode (forced if servers do not support file lease).
1394 * It is also the default mode, since we cannot distinguish
1395 * between a broken lease and a server that does not support
1396 * atomic swap/close (LU-6785)
1398 rc = migrate_block(fd_src, fd_dst, bandwidth_bytes_sec,
1399 stats_interval_sec);
1403 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
1405 error_loc = "cannot get lease";
1409 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
1410 stats_interval_sec);
1412 llapi_lease_release(fd_src);
1417 * Atomically put lease, swap layouts and close.
1418 * for a migration we need to check data version on file did
1421 rc = llapi_fswap_layouts(fd_src, fd_dst, 0, 0, SWAP_LAYOUTS_CLOSE);
1423 error_loc = "cannot swap layout";
1435 fprintf(stderr, "error: %s: %s: %s: %s\n",
1436 progname, name, error_loc, strerror(-rc));
1437 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1438 printf("%s\n", name);
1443 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1446 char *dup_string = NULL;
1452 if (!string || !string[0])
1455 dup_string = strdup(string);
1457 llapi_printf(LLAPI_MSG_ERROR,
1458 "%s: insufficient memory\n",
1463 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1467 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1468 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1469 const char *comp_name = comp_flags_table[i].cfn_name;
1471 if (strcmp(name, comp_name) == 0) {
1472 *flags |= comp_flag;
1474 } else if (strncmp(name, "^", 1) == 0 &&
1475 strcmp(name + 1, comp_name) == 0) {
1476 *neg_flags |= comp_flag;
1481 llapi_printf(LLAPI_MSG_ERROR,
1482 "%s: component flag '%s' not supported\n",
1489 if (!*flags && !*neg_flags)
1492 /* don't allow to set and exclude the same flag */
1493 if (*flags & *neg_flags)
1501 static int mdthash_input(char *string, __u32 *inflags,
1502 __u32 *exflags, __u32 *type)
1509 {"migrating", LMV_HASH_FLAG_MIGRATION},
1510 {"bad_type", LMV_HASH_FLAG_BAD_TYPE},
1511 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1512 {"lost_lmv", LMV_HASH_FLAG_LOST_LMV},
1513 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1522 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1526 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1527 if (strcmp(name, mhflist[i].name) == 0 ||
1528 name[0] == mhflist[i].name[0]) {
1529 *inflags |= mhflist[i].flag;
1531 } else if (name[0] == '^' &&
1532 (strcmp(name + 1, mhflist[i].name) == 0 ||
1533 name[1] == mhflist[i].name[0])) {
1534 *exflags |= mhflist[i].flag;
1539 i = check_hashtype(name);
1544 llapi_printf(LLAPI_MSG_ERROR,
1545 "%s: invalid mdt_hash value '%s'\n",
1551 /* don't allow to include and exclude the same flag */
1552 if (*inflags & *exflags) {
1553 llapi_printf(LLAPI_MSG_ERROR,
1554 "%s: include and exclude same flag '%s'\n",
1562 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1570 if (strncmp(string, "^", 1) == 0) {
1571 *neg_state = llapi_layout_string_flags(string + 1);
1572 if (*neg_state != 0)
1575 *state = llapi_layout_string_flags(string);
1580 llapi_printf(LLAPI_MSG_ERROR,
1581 "%s: mirrored file state '%s' not supported\n",
1587 * struct mirror_args - Command-line arguments for mirror(s).
1588 * @m_count: Number of mirrors to be created with this layout.
1589 * @m_flags: Mirror level flags, only 'prefer' is supported.
1590 * @m_layout: Mirror layout.
1591 * @m_file: A victim file. Its layout will be split and used as a mirror.
1592 * @m_next: Point to the next node of the list.
1594 * Command-line arguments for mirror(s) will be parsed and stored in
1595 * a linked list that consists of this structure.
1597 struct mirror_args {
1600 struct llapi_layout *m_layout;
1602 struct mirror_args *m_next;
1607 * enum mirror_flags - Flags for extending a mirrored file.
1608 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1609 * in case the victim file(s) contains the same data as the
1610 * original mirrored file.
1611 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1612 * @MF_COMP_ID: specified component id instead of mirror id
1614 * Flags for extending a mirrored file.
1624 * mirror_create_sanity_check() - Check mirror list.
1625 * @list: A linked list that stores the mirror arguments.
1627 * This function does a sanity check on @list for creating
1630 * Return: 0 on success or a negative error code on failure.
1632 static int mirror_create_sanity_check(const char *fname,
1633 struct mirror_args *list,
1637 bool has_m_file = false;
1638 bool has_m_layout = false;
1643 if (fname && check_fname) {
1644 struct llapi_layout *layout;
1646 layout = llapi_layout_get_by_path(fname, 0);
1649 "error: %s: file '%s' couldn't get layout\n",
1654 rc = llapi_layout_sanity(layout, false, true);
1656 llapi_layout_free(layout);
1659 llapi_layout_sanity_perror(rc);
1667 llapi_layout_free(list->m_layout);
1670 llapi_layout_get_by_path(list->m_file, 0);
1671 if (!list->m_layout) {
1673 "error: %s: file '%s' has no layout\n",
1674 progname, list->m_file);
1678 has_m_layout = true;
1679 if (!list->m_layout) {
1680 fprintf(stderr, "error: %s: no mirror layout\n",
1686 rc = llapi_layout_sanity(list->m_layout, false, true);
1688 llapi_layout_sanity_perror(rc);
1692 list = list->m_next;
1695 if (has_m_file && has_m_layout) {
1697 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1705 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1707 __u32 mirror_flags = *(__u32 *)cbdata;
1711 rc = llapi_layout_comp_flags_get(layout, &flags);
1716 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1721 return LLAPI_LAYOUT_ITER_CONT;
1725 * mirror_create() - Create a mirrored file.
1726 * @fname: The file to be created.
1727 * @mirror_list: A linked list that stores the mirror arguments.
1729 * This function creates a mirrored file @fname with the mirror(s)
1730 * from @mirror_list.
1732 * Return: 0 on success or a negative error code on failure.
1734 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1736 struct llapi_layout *layout = NULL;
1737 struct mirror_args *cur_mirror = NULL;
1738 uint16_t mirror_count = 0;
1742 rc = mirror_create_sanity_check(fname, mirror_list, false);
1746 cur_mirror = mirror_list;
1747 while (cur_mirror) {
1748 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1750 &cur_mirror->m_flags);
1753 fprintf(stderr, "%s: failed to set mirror flags\n",
1758 for (i = 0; i < cur_mirror->m_count; i++) {
1759 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1763 "error: %s: merge layout failed: %s\n",
1764 progname, strerror(errno));
1768 mirror_count += cur_mirror->m_count;
1769 cur_mirror = cur_mirror->m_next;
1773 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1777 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1780 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1781 progname, strerror(errno));
1785 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1793 llapi_layout_free(layout);
1798 * Compare files and check lease on @fd.
1800 * \retval bytes number of bytes are the same
1802 static ssize_t mirror_file_compare(int fd_src, int fd_dst)
1804 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1806 ssize_t bytes_done = 0;
1807 ssize_t bytes_read = 0;
1809 buf = malloc(buflen * 2);
1814 if (!llapi_lease_check(fd_src)) {
1815 bytes_done = -EBUSY;
1819 bytes_read = read(fd_src, buf, buflen);
1820 if (bytes_read <= 0)
1823 if (bytes_read != read(fd_dst, buf + buflen, buflen))
1827 * XXX: should compute the checksum on each buffer and then
1828 * compare checksum to avoid cache collision
1830 if (memcmp(buf, buf + buflen, bytes_read))
1833 bytes_done += bytes_read;
1841 static int mirror_extend_file(const char *fname, const char *victim_file,
1842 enum mirror_flags mirror_flags)
1847 struct stat stbuf_v;
1848 struct ll_ioc_lease *data = NULL;
1851 fd = open(fname, O_RDWR);
1853 error_loc = "open source file";
1858 fdv = open(victim_file, O_RDWR);
1860 error_loc = "open target file";
1865 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1866 error_loc = "stat source or target file";
1871 if (stbuf.st_dev != stbuf_v.st_dev) {
1872 error_loc = "stat source and target file";
1877 /* mirrors should be of the same size */
1878 if (stbuf.st_size != stbuf_v.st_size) {
1879 error_loc = "file sizes don't match";
1884 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1886 error_loc = "cannot get lease";
1890 if (!(mirror_flags & MF_NO_VERIFY)) {
1892 /* mirrors should have the same contents */
1893 ret = mirror_file_compare(fd, fdv);
1894 if (ret != stbuf.st_size) {
1895 error_loc = "file busy or contents don't match";
1896 rc = ret < 0 ? ret : -EINVAL;
1901 /* Get rid of caching pages from clients */
1902 rc = llapi_file_flush(fd);
1904 error_loc = "cannot get data version";
1908 rc = llapi_file_flush(fdv);
1910 error_loc = "cannot get data version";
1914 rc = migrate_set_timestamps(fd, &stbuf);
1916 error_loc = "cannot set source file timestamp";
1920 /* Atomically put lease, merge layouts and close. */
1921 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1923 error_loc = "memory allocation";
1926 data->lil_mode = LL_LEASE_UNLCK;
1927 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1928 data->lil_count = 1;
1929 data->lil_ids[0] = fdv;
1930 rc = llapi_lease_set(fd, data);
1932 error_loc = "cannot merge layout";
1934 } else if (rc == 0) {
1936 error_loc = "lost lease lock";
1949 (void) unlink(victim_file);
1951 fprintf(stderr, "error: %s: %s: %s: %s\n",
1952 progname, fname, error_loc, strerror(-rc));
1956 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1957 bool inherit, uint32_t flags,
1958 unsigned long long bandwidth_bytes_sec,
1959 long stats_interval_sec)
1961 struct llapi_layout *f_layout = NULL;
1962 struct ll_ioc_lease *data = NULL;
1969 f_layout = llapi_layout_get_by_path(name, 0);
1972 fprintf(stderr, "%s: cannot get layout\n", progname);
1975 rc = llapi_layout_get_last_init_comp(f_layout);
1977 fprintf(stderr, "%s: cannot get the last init comp\n",
1981 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1984 "%s: cannot inherit from the last init comp\n",
1990 llapi_layout_comp_flags_set(m_layout, flags);
1991 rc = migrate_open_files(name,
1992 LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1993 NULL, m_layout, &fd_src, &fd_dst);
1997 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
1999 error_loc = "cannot get lease";
2003 rc = fstat(fd_src, &st);
2005 error_loc = "cannot stat source file";
2009 if (stats_interval_sec)
2010 printf("%s:\n", name);
2012 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
2013 stats_interval_sec);
2015 llapi_lease_release(fd_src);
2019 rc = migrate_set_timestamps(fd_src, &st);
2021 error_loc = "cannot set source file timestamp";
2025 /* Atomically put lease, merge layouts and close. */
2026 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2028 error_loc = "memory allocation";
2031 data->lil_mode = LL_LEASE_UNLCK;
2032 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2033 data->lil_count = 1;
2034 data->lil_ids[0] = fd_dst;
2035 rc = llapi_lease_set(fd_src, data);
2037 error_loc = "cannot merge layout";
2039 } else if (rc == 0) {
2041 error_loc = "lost lease lock";
2054 fprintf(stderr, "error: %s: %s: %s: %s\n",
2055 progname, name, error_loc, strerror(-rc));
2059 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2060 enum mirror_flags mirror_flags,
2061 unsigned long long bandwidth_bytes_sec,
2062 long stats_interval_sec)
2066 while (mirror_list) {
2067 if (mirror_list->m_file) {
2068 rc = mirror_extend_file(fname, mirror_list->m_file,
2071 __u32 mirror_count = mirror_list->m_count;
2073 while (mirror_count > 0) {
2074 rc = mirror_extend_layout(fname,
2075 mirror_list->m_layout,
2076 mirror_list->m_inherit,
2077 mirror_list->m_flags,
2078 bandwidth_bytes_sec,
2079 stats_interval_sec);
2089 mirror_list = mirror_list->m_next;
2095 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2100 rc = llapi_layout_mirror_id_get(layout, &id);
2104 if ((__u16)id == *(__u16 *)cbdata)
2105 return LLAPI_LAYOUT_ITER_STOP;
2107 return LLAPI_LAYOUT_ITER_CONT;
2110 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2115 rc = llapi_layout_comp_id_get(layout, &id);
2119 if (id == *(__u32 *)cbdata)
2120 return LLAPI_LAYOUT_ITER_STOP;
2122 return LLAPI_LAYOUT_ITER_CONT;
2125 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2127 char buf[LOV_MAXPOOLNAME + 1];
2128 struct pool_to_id_cbdata *d = (void *)cbdata;
2132 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2135 if (strcmp(d->pool, buf))
2136 return LLAPI_LAYOUT_ITER_CONT;
2138 rc = llapi_layout_mirror_id_get(layout, &id);
2143 return LLAPI_LAYOUT_ITER_STOP;
2146 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2148 char buf[LOV_MAXPOOLNAME + 1];
2149 struct pool_to_id_cbdata *d = (void *)cbdata;
2153 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2156 if (strcmp(d->pool, buf))
2157 return LLAPI_LAYOUT_ITER_CONT;
2159 rc = llapi_layout_comp_id_get(layout, &id);
2164 return LLAPI_LAYOUT_ITER_STOP;
2167 struct collect_ids_data {
2173 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2175 struct collect_ids_data *cid = cbdata;
2179 rc = llapi_layout_mirror_id_get(layout, &id);
2183 if ((__u16)id != cid->cid_exclude) {
2186 for (i = 0; i < cid->cid_count; i++) {
2187 /* already collected the mirror id */
2188 if (id == cid->cid_ids[i])
2189 return LLAPI_LAYOUT_ITER_CONT;
2191 cid->cid_ids[cid->cid_count] = id;
2195 return LLAPI_LAYOUT_ITER_CONT;
2199 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2200 * @mirror_id: Mirror id to be checked.
2201 * @layout: Mirror component list.
2203 * This function checks if a mirror with specified @mirror_id is the last
2204 * non-stale mirror of a layout @layout.
2206 * Return: true or false.
2209 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2211 __u16 mirror_ids[128] = { 0 };
2212 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2214 .cid_exclude = mirror_id, };
2217 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2219 for (i = 0; i < cid.cid_count; i++) {
2220 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2223 comp_size = llapi_mirror_find_stale(layout, comp_array,
2224 ARRAY_SIZE(comp_array),
2233 static int mirror_split(const char *fname, __u32 id, const char *pool,
2234 enum mirror_flags mflags, const char *victim_file)
2236 struct llapi_layout *layout;
2237 char parent[PATH_MAX];
2238 char victim[PATH_MAX];
2239 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2241 struct ll_ioc_lease *data;
2242 uint16_t mirror_count;
2246 bool purge = true; /* delete mirror by setting fdv=fd */
2250 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2252 "error %s: the source file '%s' and -f file are the same\n",
2257 /* check fname contains mirror with mirror_id/comp_id */
2258 layout = llapi_layout_get_by_path(fname, 0);
2261 "error %s: file '%s' couldn't get layout\n",
2266 rc = llapi_layout_sanity(layout, false, true);
2268 llapi_layout_sanity_perror(rc);
2272 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2275 "error %s: file '%s' couldn't get mirror count\n",
2279 if (mirror_count < 2) {
2281 "error %s: file '%s' has %d component, cannot split\n",
2282 progname, fname, mirror_count);
2286 if (mflags & MF_COMP_POOL) {
2287 struct pool_to_id_cbdata data = { .pool = pool };
2289 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2291 mirror_id = data.id;
2292 } else if (mflags & MF_COMP_ID) {
2293 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2294 mirror_id = mirror_id_of(id);
2296 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2300 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2303 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2304 if (mflags & MF_COMP_POOL) {
2306 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2307 progname, fname, pool);
2309 } else if (mflags & MF_COMP_ID) {
2311 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2312 progname, fname, id);
2316 "error %s: file '%s' does not contain mirror with id %u\n",
2317 progname, fname, id);
2322 if (last_non_stale_mirror(mirror_id, layout)) {
2325 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2330 if (!victim_file && mflags & MF_DESTROY)
2331 /* Allow mirror split even without the key on encrypted files,
2332 * and in this case of a 'split -d', open file with O_DIRECT
2333 * (no IOs will be done).
2335 fd = open(fname, O_RDWR | O_DIRECT | O_CIPHERTEXT);
2337 fd = open(fname, O_RDWR);
2341 "error %s: open file '%s' failed: %s\n",
2342 progname, fname, strerror(errno));
2346 /* get victim file directory pathname */
2347 if (strlen(fname) > sizeof(parent) - 1) {
2348 fprintf(stderr, "error %s: file name of '%s' too long\n",
2353 strncpy(parent, fname, sizeof(parent));
2354 ptr = strrchr(parent, '/');
2356 if (!getcwd(parent, sizeof(parent))) {
2357 fprintf(stderr, "error %s: getcwd failed: %s\n",
2358 progname, strerror(errno));
2368 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2370 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2375 rc = llapi_file_is_encrypted(fd);
2377 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2378 progname, fname, rc);
2385 /* use a temp file to store the splitted layout */
2386 if (mflags & MF_DESTROY) {
2387 char file_path[PATH_MAX];
2388 unsigned int rnumber;
2391 if (last_non_stale_mirror(mirror_id, layout)) {
2394 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2400 /* don't use volatile file for mirror destroy */
2404 * try the old way to delete mirror using
2409 rc = snprintf(file_path,
2411 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2415 rc >= sizeof(file_path)) {
2416 fdv = -ENAMETOOLONG;
2420 open_flags = O_RDWR |
2421 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2422 O_CREAT | O_EXCL | O_NOFOLLOW |
2423 /* O_DIRECT for mirror split -d */
2425 /* Allow split without the key */
2427 fdv = open(file_path, open_flags,
2431 } while (fdv < 0 && rc == -EEXIST);
2437 "error %s: not permitted on encrypted file '%s': %d\n",
2438 progname, fname, rc);
2442 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2444 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2447 /* user specified victim file */
2451 "error %s: not permitted on encrypted file '%s': %d\n",
2452 progname, fname, rc);
2455 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2460 "error %s: create victim file failed: %s\n",
2461 progname, strerror(errno));
2465 /* get lease lock of fname */
2466 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2469 "error %s: cannot get lease of file '%s': %d\n",
2470 progname, fname, rc);
2474 /* Atomatically put lease, split layouts and close. */
2475 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2481 data->lil_mode = LL_LEASE_UNLCK;
2482 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2483 data->lil_count = 2;
2484 data->lil_ids[0] = fdv;
2485 data->lil_ids[1] = mirror_id;
2486 rc = llapi_lease_set(fd, data);
2488 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2489 /* could be old MDS which prohibit fd==fdv */
2494 if (rc == 0) /* lost lease lock */
2497 "error %s: cannot split '%s': %s\n",
2498 progname, fname, strerror(-rc));
2510 llapi_layout_free(layout);
2515 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2516 __u16 *mirror_ids, int ids_nr,
2517 long stats_interval_sec, long bandwidth_bytes_sec);
2520 static int lfs_migrate_to_dom(int fd_src, int fd_dst, char *name,
2521 __u64 migration_flags,
2522 unsigned long long bandwidth_bytes_sec,
2523 long stats_interval_sec)
2525 struct ll_ioc_lease *data = NULL;
2528 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
2530 error_loc = "cannot get lease";
2534 if (stats_interval_sec)
2535 printf("%s:\n", name);
2537 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
2538 stats_interval_sec);
2542 /* Atomically put lease, merge layouts, resync and close. */
2543 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2545 error_loc = "memory allocation";
2548 data->lil_mode = LL_LEASE_UNLCK;
2549 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2550 data->lil_count = 1;
2551 data->lil_ids[0] = fd_dst;
2552 rc = llapi_lease_set(fd_src, data);
2554 error_loc = "cannot merge layout";
2556 } else if (rc == 0) {
2558 error_loc = "lost lease lock";
2564 rc = lfs_mirror_resync_file(name, data, NULL, 0,
2566 bandwidth_bytes_sec);
2568 error_loc = "cannot resync file";
2572 /* delete first mirror now */
2573 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2575 error_loc = "cannot delete old layout";
2579 llapi_lease_release(fd_src);
2585 fprintf(stderr, "error: %s: %s: %s: %s\n",
2586 progname, name, error_loc, strerror(-rc));
2587 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2588 printf("%s\n", name);
2595 * Parse a string containing an target index list into an array of integers.
2597 * The input string contains a comma delimited list of individual
2598 * indices and ranges, for example "1,2-4,7". Add the indices into the
2599 * \a tgts array and remove duplicates.
2601 * \param[out] tgts array to store indices in
2602 * \param[in] size size of \a tgts array
2603 * \param[in] offset starting index in \a tgts
2604 * \param[in] arg string containing OST index list
2605 * \param[in/out] overstriping index list may contain duplicates
2607 * \retval positive number of indices in \a tgts
2608 * \retval -EINVAL unable to parse \a arg
2610 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2611 unsigned long long *pattern)
2615 int slots = size - offset;
2617 bool overstriped = false;
2623 end_of_loop = false;
2624 while (!end_of_loop) {
2625 int start_index = 0;
2628 char *endptr = NULL;
2632 ptr = strchrnul(arg, ',');
2634 end_of_loop = *ptr == '\0';
2638 start_index = strtol(arg, &endptr, 0);
2639 if (endptr == arg) /* no data at all */
2641 if (errno != 0 || start_index < -1 ||
2642 (*endptr != '-' && *endptr != '\0'))
2645 end_index = start_index;
2646 if (*endptr == '-') {
2648 end_index = strtol(endptr + 1, &endptr, 0);
2649 if (errno != 0 || *endptr != '\0' || end_index < -1)
2651 if (end_index < start_index)
2655 for (i = start_index; i <= end_index && slots > 0; i++) {
2658 /* remove duplicate */
2659 for (j = 0; j < offset; j++) {
2660 if (tgts[j] == i && pattern &&
2661 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2663 else if (tgts[j] == i)
2669 if (j == offset) { /* check complete */
2675 if (slots == 0 && i < end_index)
2683 if (!end_of_loop && ptr)
2686 if (!overstriped && pattern)
2687 *pattern = LLAPI_LAYOUT_DEFAULT;
2689 return rc < 0 ? rc : nr;
2692 struct lfs_setstripe_args {
2693 unsigned long long lsa_comp_end;
2694 unsigned long long lsa_stripe_size;
2695 unsigned long long lsa_extension_size;
2696 long long lsa_stripe_count;
2697 long long lsa_stripe_off;
2698 __u32 lsa_comp_flags;
2699 __u32 lsa_comp_neg_flags;
2700 unsigned long long lsa_pattern;
2701 unsigned int lsa_mirror_count;
2703 bool lsa_first_comp;
2704 bool lsa_extension_comp;
2706 char *lsa_pool_name;
2709 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2711 unsigned int mirror_count = lsa->lsa_mirror_count;
2712 bool first_comp = lsa->lsa_first_comp;
2714 memset(lsa, 0, sizeof(*lsa));
2716 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2717 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2718 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2719 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2720 lsa->lsa_pool_name = NULL;
2722 lsa->lsa_mirror_count = mirror_count;
2723 lsa->lsa_first_comp = first_comp;
2727 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2728 * @lsa: Stripe options to be initialized and inherited.
2730 * This function initializes stripe options in @lsa and inherit
2731 * stripe_size, stripe_count and OST pool_name options.
2735 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2737 unsigned long long stripe_size;
2738 long long stripe_count;
2739 char *pool_name = NULL;
2741 stripe_size = lsa->lsa_stripe_size;
2742 stripe_count = lsa->lsa_stripe_count;
2743 pool_name = lsa->lsa_pool_name;
2745 setstripe_args_init(lsa);
2747 lsa->lsa_stripe_size = stripe_size;
2748 lsa->lsa_stripe_count = stripe_count;
2749 lsa->lsa_pool_name = pool_name;
2752 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2754 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2755 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2756 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2757 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2758 lsa->lsa_comp_end != 0);
2761 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2763 if (lsa->lsa_nr_tgts) {
2764 if (lsa->lsa_nr_tgts < 0 ||
2765 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2766 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2772 if (lsa->lsa_stripe_count > 0 &&
2773 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2774 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2775 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2776 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2777 lsa->lsa_stripe_count,
2789 * comp_args_to_layout() - Create or extend a composite layout.
2790 * @composite: Pointer to the composite layout.
2791 * @lsa: Stripe options for the new component.
2793 * This function creates or extends a composite layout by adding a new
2794 * component with stripe options from @lsa.
2796 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2798 * Return: 0 on success or an error code on failure.
2800 static int comp_args_to_layout(struct llapi_layout **composite,
2801 struct lfs_setstripe_args *lsa,
2804 struct llapi_layout *layout = *composite;
2805 uint64_t prev_end = 0;
2811 layout = llapi_layout_alloc();
2813 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2818 *composite = layout;
2819 lsa->lsa_first_comp = true;
2824 * Get current component extent, current component
2825 * must be the tail component.
2827 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2829 fprintf(stderr, "Get comp extent failed. %s\n",
2834 if (lsa->lsa_first_comp) {
2836 rc = llapi_layout_add_first_comp(layout);
2838 rc = llapi_layout_comp_add(layout);
2841 fprintf(stderr, "Add component failed. %s\n",
2847 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2849 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2850 lsa->lsa_comp_flags, strerror(errno));
2855 uint64_t comp_end = lsa->lsa_comp_end;
2858 * The extendable component is 0-length, so it can be removed
2859 * if there is insufficient space to extend it.
2861 if (lsa->lsa_extension_comp)
2862 comp_end = prev_end;
2864 rc = llapi_layout_comp_extent_set(layout, prev_end,
2867 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2868 prev_end, comp_end, strerror(errno));
2872 /* reset lsa_first_comp */
2873 lsa->lsa_first_comp = false;
2875 /* Data-on-MDT component setting */
2876 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2878 if (lsa->lsa_stripe_count == 0)
2879 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2880 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2881 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2882 if (lsa->lsa_stripe_off == -1 ||
2883 lsa->lsa_stripe_off == 0)
2884 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2886 * In case of Data-on-MDT patterns the only extra option
2887 * applicable is stripe size option.
2889 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2891 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2892 lsa->lsa_stripe_count);
2896 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2897 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2899 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2900 lsa->lsa_stripe_size);
2904 if (lsa->lsa_nr_tgts != 0) {
2906 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2911 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2913 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2914 lsa->lsa_stripe_off);
2918 if (lsa->lsa_pool_name != 0) {
2920 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2921 lsa->lsa_pool_name);
2926 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2928 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2933 /* Data-on-MDT component has always single stripe up to end */
2934 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2935 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2936 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2938 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2945 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2946 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2948 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2949 rc = llapi_layout_extension_size_set(layout, size);
2951 rc = llapi_layout_stripe_size_set(layout, size);
2954 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2955 size, strerror(errno));
2959 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2961 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2962 lsa->lsa_stripe_count, strerror(errno));
2966 if (lsa->lsa_pool_name) {
2967 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2969 fprintf(stderr, "Set pool name: %s failed. %s\n",
2970 lsa->lsa_pool_name, strerror(errno));
2974 rc = llapi_layout_pool_name_set(layout, "");
2976 fprintf(stderr, "Clear pool name failed: %s\n",
2982 rc = lsa_args_stripe_count_check(lsa);
2986 if (lsa->lsa_nr_tgts > 0) {
2989 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2990 rc = llapi_layout_ost_index_set(layout, i,
2995 /* Make sure stripe offset is in OST list. */
2996 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
3000 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
3001 lsa->lsa_stripe_off);
3005 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
3006 lsa->lsa_stripe_off != -1) {
3007 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
3010 fprintf(stderr, "Set ost index %d failed. %s\n",
3011 i, strerror(errno));
3015 /* Create the second, virtual component of extension space */
3016 if (lsa->lsa_extension_comp) {
3017 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
3018 lsa->lsa_extension_comp = false;
3025 static int build_component(struct llapi_layout **layout,
3026 struct lfs_setstripe_args *lsa, bool set_extent)
3030 rc = comp_args_to_layout(layout, lsa, set_extent);
3034 if (lsa->lsa_mirror_count > 0) {
3035 rc = llapi_layout_mirror_count_set(*layout,
3036 lsa->lsa_mirror_count);
3040 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
3043 lsa->lsa_mirror_count = 0;
3049 static int build_prev_component(struct llapi_layout **layout,
3050 struct lfs_setstripe_args *prev,
3051 struct lfs_setstripe_args *lsa,
3054 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
3057 if (prev->lsa_stripe_size) {
3059 prev->lsa_comp_end = lsa->lsa_comp_end;
3060 prev->lsa_extension_size = lsa->lsa_extension_size;
3061 prev->lsa_extension_comp = true;
3064 rc = build_component(layout, prev, true);
3070 * Copy lsa to previous lsa;
3071 * if this is an extension component, make the previous invalid;
3074 prev->lsa_stripe_size = 0;
3081 #ifndef LCME_TEMPLATE_FLAGS
3082 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
3086 static int build_layout_from_yaml_node(struct cYAML *node,
3087 struct llapi_layout **layout,
3088 struct lfs_setstripe_args *lsa,
3089 struct lfs_setstripe_args *prevp)
3091 struct lfs_setstripe_args prev = { 0 };
3092 __u32 *osts = lsa->lsa_tgts;
3100 string = node->cy_string;
3102 if (node->cy_type == CYAML_TYPE_OBJECT) {
3103 /* go deep to sub blocks */
3104 if (string && !strncmp(string, "component", 9) &&
3105 strncmp(string, "component0", 10) &&
3106 strncmp(string, "components", 10)) {
3107 rc = build_prev_component(layout, prevp, lsa,
3112 /* initialize lsa. */
3113 setstripe_args_init(lsa);
3114 lsa->lsa_first_comp = false;
3115 lsa->lsa_tgts = osts;
3118 rc = build_layout_from_yaml_node(node->cy_child, layout,
3123 if (!node->cy_string)
3126 /* skip leading lmm_ if present, to simplify parsing */
3127 if (strncmp(string, "lmm_", 4) == 0)
3130 if (node->cy_type == CYAML_TYPE_STRING) {
3131 if (!strcmp(string, "lcme_extent.e_end")) {
3132 if (!strcmp(node->cy_valuestring, "EOF") ||
3133 !strcmp(node->cy_valuestring, "eof"))
3134 lsa->lsa_comp_end = LUSTRE_EOF;
3135 } else if (!strcmp(string, "pool")) {
3136 lsa->lsa_pool_name = node->cy_valuestring;
3137 } else if (!strcmp(string, "pattern")) {
3138 if (!strcmp(node->cy_valuestring, "mdt"))
3139 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3140 if (!strcmp(node->cy_valuestring,
3141 "raid0,overstriped"))
3143 LLAPI_LAYOUT_OVERSTRIPING;
3144 } else if (!strcmp(string, "lcme_flags")) {
3145 rc = comp_str2flags(node->cy_valuestring,
3146 &lsa->lsa_comp_flags,
3147 &lsa->lsa_comp_neg_flags);
3151 * Only template flags have meaning in
3152 * the layout for a new file
3154 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3156 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3157 if (!strcmp(string, "lcm_mirror_count")) {
3158 lsa->lsa_mirror_count = node->cy_valueint;
3159 } else if (!strcmp(string, "lcme_extent.e_start")) {
3160 if (node->cy_valueint == 0)
3161 lsa->lsa_first_comp = true;
3162 } else if (!strcmp(string, "lcme_extent.e_end")) {
3163 if (node->cy_valueint == -1)
3164 lsa->lsa_comp_end = LUSTRE_EOF;
3166 lsa->lsa_comp_end = node->cy_valueint;
3167 } else if (!strcmp(string, "stripe_count")) {
3168 lsa->lsa_stripe_count = node->cy_valueint;
3169 } else if (!strcmp(string, "stripe_size")) {
3170 lsa->lsa_stripe_size = node->cy_valueint;
3171 } else if (!strcmp(string, "extension_size")) {
3172 lsa->lsa_extension_size = node->cy_valueint;
3173 lsa->lsa_extension_comp = true;
3174 } else if (!strcmp(string, "stripe_offset")) {
3175 lsa->lsa_stripe_off = node->cy_valueint;
3176 } else if (!strcmp(string, "l_ost_idx")) {
3177 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3182 node = node->cy_next;
3185 if (prevp == &prev) {
3186 rc = build_prev_component(layout, prevp, lsa, true);
3190 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3191 rc = build_component(layout, lsa, *layout != NULL);
3197 static int lfs_comp_create_from_yaml(char *template,
3198 struct llapi_layout **layout,
3199 struct lfs_setstripe_args *lsa,
3202 struct cYAML *tree = NULL, *err_rc = NULL;
3205 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3207 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3208 progname, template);
3209 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3210 "can't parse", &err_rc);
3211 cYAML_print_tree2file(stderr, err_rc);
3212 cYAML_free_tree(err_rc);
3217 /* initialize lsa for plain file */
3218 setstripe_args_init(lsa);
3219 lsa->lsa_tgts = osts;
3221 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3223 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3224 progname, template);
3227 /* clean clean lsa */
3228 setstripe_args_init(lsa);
3232 cYAML_free_tree(tree);
3237 * Get the extension size from the next (SEL) component and extend the
3238 * current component on it. The start of the next component is to be
3241 * \param[in] layout the current layout
3242 * \param[in] start the start of the current component
3243 * \param[in,out] end the end of the current component
3244 * \param[in] offset the offset to adjust the end position to instead of
3247 * \retval 0 - extended successfully
3248 * \retval < 0 - error
3250 static int layout_extend_comp(struct llapi_layout *layout,
3251 uint64_t start, uint64_t *end,
3254 uint64_t size, next_start, next_end;
3257 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3260 "%s setstripe: cannot move component cursor: %s\n",
3261 progname, strerror(errno));
3266 * Even if the @size will not be used below, this will fail if
3267 * this is not a SEL component - a good confirmation we are
3268 * working on right components.
3270 rc = llapi_layout_extension_size_get(layout, &size);
3273 "%s setstripe: cannot get component ext size: %s\n",
3274 progname, strerror(errno));
3278 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3280 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3281 progname, strerror(errno));
3285 next_start += offset ?: size;
3286 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3288 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3289 progname, strerror(errno));
3293 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3296 "%s setstripe: cannot move component cursor: %s\n",
3297 progname, strerror(errno));
3301 *end += offset ?: size;
3302 rc = llapi_layout_comp_extent_set(layout, start, *end);
3304 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3305 progname, strerror(errno));
3313 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3314 * end of the last component in the existing file, and adjust the
3315 * first extent start of the components to be added accordingly.
3317 * In the create mode, we need to check if the first component is an extendable
3318 * SEL component and extend its length to the extension size (first component
3319 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3321 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3324 struct llapi_layout *head;
3325 uint64_t start = 0, prev_end = 0;
3329 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3334 head = llapi_layout_get_by_path(fname, 0);
3337 "%s setstripe: cannot read layout from '%s': %s\n",
3338 progname, fname, strerror(errno));
3340 } else if (errno == ENODATA) {
3342 * file without LOVEA, this component-add will be turned
3343 * into a component-create.
3345 llapi_layout_free(head);
3349 * the new layout will be added to an empty one, it
3350 * still needs to be adjusted below
3354 } else if (!llapi_layout_is_composite(head)) {
3356 "%s setstripe: '%s' not a composite file\n",
3358 llapi_layout_free(head);
3362 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3365 "%s setstripe: cannot get prev extent: %s\n",
3366 progname, strerror(errno));
3367 llapi_layout_free(head);
3371 llapi_layout_free(head);
3375 /* Make sure we use the first component of the layout to be added. */
3376 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3379 "%s setstripe: cannot move component cursor: %s\n",
3380 progname, strerror(errno));
3384 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3386 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3387 progname, strerror(errno));
3391 if (start == 0 && end == 0) {
3392 rc = layout_extend_comp(layout, start, &end,
3393 comp_add ? prev_end : 0);
3398 if (start > prev_end || end < prev_end) {
3400 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3401 progname, start, end, prev_end);
3405 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3408 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3409 progname, prev_end, end, strerror(errno));
3416 static int mirror_adjust_first_extents(struct mirror_args *list)
3424 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3427 list = list->m_next;
3433 static inline bool arg_is_eof(char *arg)
3435 return !strncmp(arg, "-1", strlen("-1")) ||
3436 !strncmp(arg, "EOF", strlen("EOF")) ||
3437 !strncmp(arg, "eof", strlen("eof"));
3441 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3443 * Return: Valid mirror_args pointer on success and
3444 * NULL if memory allocation fails.
3446 static struct mirror_args *lfs_mirror_alloc(void)
3448 struct mirror_args *mirror = NULL;
3451 mirror = calloc(1, sizeof(*mirror));
3453 mirror->m_inherit = false;
3464 * lfs_mirror_free() - Free memory allocated for a mirror argument
3466 * @mirror: Previously allocated mirror argument structure by
3467 * lfs_mirror_alloc().
3469 * Free memory allocated for @mirror.
3473 static void lfs_mirror_free(struct mirror_args *mirror)
3475 if (mirror->m_layout)
3476 llapi_layout_free(mirror->m_layout);
3481 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3482 * @mirror_list: Previously allocated mirror list.
3484 * Free memory allocated for @mirror_list.
3488 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3490 struct mirror_args *next_mirror = NULL;
3492 while (mirror_list) {
3493 next_mirror = mirror_list->m_next;
3494 lfs_mirror_free(mirror_list);
3495 mirror_list = next_mirror;
3500 LFS_SETQUOTA_DELETE = (CHAR_MAX + 1),
3508 LFS_COMP_NO_VERIFY_OPT,
3510 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3512 LFS_MIRROR_STATE_OPT,
3514 LFS_MIRROR_INDEX_OPT,
3515 LFS_LAYOUT_FOREIGN_OPT,
3524 LFS_STATS_INTERVAL_OPT,
3529 #ifndef LCME_USER_MIRROR_FLAGS
3530 /* The mirror flags can be set by users at creation time. */
3531 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3535 static int lfs_setstripe_internal(int argc, char **argv,
3536 enum setstripe_origin opc)
3538 struct lfs_setstripe_args lsa = { 0 };
3539 struct llapi_stripe_param *param = NULL;
3540 struct find_param migrate_mdt_param = {
3550 unsigned long long size_units = 1;
3551 bool migrate_mode = false;
3552 bool migrate_mdt_mode = false;
3553 bool setstripe_mode = false;
3554 bool migration_block = false;
3555 __u64 migration_flags = 0;
3556 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3557 int comp_del = 0, comp_set = 0;
3560 struct llapi_layout *layout = NULL;
3561 struct llapi_layout **lpp = &layout;
3562 bool mirror_mode = false;
3563 bool has_m_file = false;
3564 __u32 mirror_count = 0;
3565 enum mirror_flags mirror_flags = 0;
3566 struct mirror_args *mirror_list = NULL;
3567 struct mirror_args *new_mirror = NULL;
3568 struct mirror_args *last_mirror = NULL;
3569 __u16 mirror_id = 0;
3571 bool from_yaml = false;
3572 bool from_copy = false;
3573 char *template = NULL;
3574 bool foreign_mode = false;
3576 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3577 char *mode_opt = NULL;
3578 mode_t previous_umask = 0;
3579 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3580 unsigned long long bandwidth_bytes_sec = 0;
3581 unsigned long long bandwidth_unit = ONE_MB;
3582 long stats_interval_sec = 0;
3584 struct option long_opts[] = {
3585 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3586 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3587 /* --block is only valid in migrate mode */
3588 { .val = 'b', .name = "block", .has_arg = no_argument },
3589 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3590 { .val = LFS_COMP_ADD_OPT,
3591 .name = "comp-add", .has_arg = no_argument },
3592 { .val = LFS_COMP_ADD_OPT,
3593 .name = "component-add", .has_arg = no_argument },
3594 { .val = LFS_COMP_DEL_OPT,
3595 .name = "comp-del", .has_arg = no_argument },
3596 { .val = LFS_COMP_DEL_OPT,
3597 .name = "component-del", .has_arg = no_argument },
3598 { .val = LFS_COMP_FLAGS_OPT,
3599 .name = "comp-flags", .has_arg = required_argument },
3600 { .val = LFS_COMP_FLAGS_OPT,
3601 .name = "component-flags",
3602 .has_arg = required_argument },
3603 { .val = LFS_COMP_SET_OPT,
3604 .name = "comp-set", .has_arg = no_argument },
3605 { .val = LFS_COMP_SET_OPT,
3606 .name = "component-set",
3607 .has_arg = no_argument},
3608 { .val = LFS_COMP_NO_VERIFY_OPT,
3609 .name = "no-verify", .has_arg = no_argument},
3610 { .val = LFS_LAYOUT_FLAGS_OPT,
3611 .name = "flags", .has_arg = required_argument},
3612 { .val = LFS_LAYOUT_FOREIGN_OPT,
3613 .name = "foreign", .has_arg = optional_argument},
3614 { .val = LFS_MIRROR_ID_OPT,
3615 .name = "mirror-id", .has_arg = required_argument},
3616 { .val = LFS_MODE_OPT,
3617 .name = "mode", .has_arg = required_argument},
3618 { .val = LFS_LAYOUT_COPY,
3619 .name = "copy", .has_arg = required_argument},
3620 { .val = LFS_STATS_OPT,
3621 .name = "stats", .has_arg = no_argument},
3622 { .val = LFS_STATS_INTERVAL_OPT,
3623 .name = "stats-interval",
3624 .has_arg = required_argument},
3625 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3626 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3627 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3628 { .val = 'C', .name = "overstripe-count",
3629 .has_arg = required_argument},
3630 { .val = 'd', .name = "delete", .has_arg = no_argument},
3631 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3632 /* used with "lfs migrate -m" */
3633 { .val = 'd', .name = "directory", .has_arg = no_argument},
3634 /* --non-direct is only valid in migrate mode */
3635 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3636 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3637 { .val = 'E', .name = "component-end",
3638 .has_arg = required_argument},
3639 { .val = 'f', .name = "file", .has_arg = required_argument },
3640 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3641 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3642 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3643 { .val = 'h', .name = "help", .has_arg = no_argument },
3644 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3645 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3646 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3647 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3648 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3649 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3650 { .val = 'L', .name = "layout", .has_arg = required_argument },
3651 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3652 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3653 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3654 /* --non-block is only valid in migrate mode */
3655 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3656 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3657 { .val = 'o', .name = "ost", .has_arg = required_argument },
3658 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3659 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3660 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3662 { .val = 'p', .name = "pool", .has_arg = required_argument },
3663 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3664 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3665 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3666 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3667 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3668 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3669 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3670 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3671 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3672 /* --verbose is only valid in migrate mode */
3673 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3674 { .val = 'W', .name = "bandwidth", .has_arg = required_argument },
3675 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3676 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3677 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3678 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3679 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3682 setstripe_args_init(&lsa);
3684 migrate_mode = (opc == SO_MIGRATE);
3685 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3686 setstripe_mode = (opc == SO_SETSTRIPE);
3687 if (opc == SO_MIRROR_DELETE) {
3689 mirror_flags = MF_DESTROY;
3692 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3694 while ((c = getopt_long(argc, argv,
3695 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:W:y:z:",
3696 long_opts, NULL)) >= 0) {
3702 case LFS_COMP_ADD_OPT:
3705 case LFS_COMP_DEL_OPT:
3708 case LFS_COMP_FLAGS_OPT:
3709 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3710 &lsa.lsa_comp_neg_flags);
3713 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3715 "%s: inverted flags are not supported\n",
3720 case LFS_COMP_SET_OPT:
3723 case LFS_COMP_NO_VERIFY_OPT:
3724 mirror_flags |= MF_NO_VERIFY;
3726 case LFS_MIRROR_ID_OPT: {
3727 unsigned long int id;
3730 id = strtoul(optarg, &end, 0);
3731 if (errno != 0 || *end != '\0' || id == 0 ||
3734 "%s %s: invalid mirror ID '%s'\n",
3735 progname, argv[0], optarg);
3739 mirror_id = (__u16)id;
3742 case LFS_LAYOUT_FLAGS_OPT: {
3745 /* check for numeric flags (foreign and mirror cases) */
3746 if (setstripe_mode && !mirror_mode && !last_mirror) {
3748 flags = strtoul(optarg, &end, 16);
3749 if (errno != 0 || *end != '\0' ||
3750 flags >= UINT32_MAX) {
3752 "%s %s: invalid hex flags '%s'\n",
3753 progname, argv[0], optarg);
3756 if (!foreign_mode) {
3758 "%s %s: hex flags must be specified with --foreign option\n",
3765 if (!mirror_mode || !last_mirror) {
3767 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3772 result = comp_str2flags(optarg, &last_mirror->m_flags,
3779 "%s: inverted flags are not supported\n",
3784 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3786 "%s: unsupported mirror flags: %s\n",
3793 case LFS_LAYOUT_FOREIGN_OPT:
3795 /* check pure numeric */
3796 type = strtoul(optarg, &end, 0);
3799 type = check_foreign_type_name(optarg);
3800 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3802 "%s %s: unrecognized foreign type '%s'\n",
3807 } else if (type >= UINT32_MAX) {
3809 "%s %s: invalid foreign type '%s'\n",
3810 progname, argv[0], optarg);
3814 foreign_mode = true;
3819 mode = strtoul(mode_opt, &end, 8);
3822 "%s %s: bad mode '%s'\n",
3823 progname, argv[0], mode_opt);
3826 previous_umask = umask(0);
3829 case LFS_LAYOUT_COPY:
3834 stats_interval_sec = 5;
3836 case LFS_STATS_INTERVAL_OPT:
3837 stats_interval_sec = strtol(optarg, &end, 0);
3838 if (stats_interval_sec == 0 && errno) {
3840 "%s %s: invalid stats interval %s\n",
3841 progname, argv[0], optarg);
3846 if (!migrate_mode) {
3848 "%s %s: -b|--block valid only for migrate command\n",
3852 migration_block = true;
3855 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3857 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3861 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3865 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3866 if (errno != 0 || *end != '\0'|| optarg == end ||
3867 lsa.lsa_stripe_count < -1 ||
3868 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3870 "%s %s: invalid stripe count '%s'\n",
3871 progname, argv[0], optarg);
3875 if (lsa.lsa_stripe_count == -1)
3876 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3880 migrate_mdt_param.fp_max_depth = 1;
3882 /* delete the default striping pattern */
3884 if (opc == SO_MIRROR_SPLIT) {
3887 "%s %s: -d cannot used with -f\n",
3891 mirror_flags |= MF_DESTROY;
3896 if (!migrate_mode) {
3898 "%s %s: -D|--non-direct is valid only for migrate command\n",
3902 migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3905 if (lsa.lsa_comp_end != 0) {
3906 result = comp_args_to_layout(lpp, &lsa, true);
3908 fprintf(stderr, "%s: invalid layout\n",
3913 setstripe_args_init_inherit(&lsa);
3916 if (arg_is_eof(optarg)) {
3917 lsa.lsa_comp_end = LUSTRE_EOF;
3919 result = llapi_parse_size(optarg,
3922 /* assume units of KB if too small */
3923 if (lsa.lsa_comp_end < 4096)
3924 lsa.lsa_comp_end *= 1024;
3926 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3928 "%s %s: invalid component end '%s'\n",
3929 progname, argv[0], optarg);
3935 if (!migrate_mode) {
3937 "--mdt-hash is valid only for migrate command\n");
3941 lsa.lsa_pattern = check_hashtype(optarg);
3942 if (lsa.lsa_pattern == 0) {
3944 "%s %s: bad stripe hash type '%s'\n",
3945 progname, argv[0], optarg);
3951 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3952 if (errno != 0 || *end != '\0' || optarg == end ||
3953 lsa.lsa_stripe_off < -1 ||
3954 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3956 "%s %s: invalid stripe offset '%s'\n",
3957 progname, argv[0], optarg);
3960 if (lsa.lsa_stripe_off == -1)
3961 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3964 comp_id = strtoul(optarg, &end, 0);
3965 if (*end != '\0' || comp_id == 0 ||
3966 comp_id > LCME_ID_MAX) {
3968 "%s %s: invalid component ID '%s'\n",
3969 progname, argv[0], optarg);
3974 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3976 "error: %s: invalid option: %s\n",
3977 progname, argv[optopt + 1]);
3980 if (opc == SO_MIRROR_EXTEND) {
3983 "error: %s: '-N' must exist in front of '%s'\n",
3984 progname, argv[optopt + 1]);
3987 last_mirror->m_file = optarg;
3988 last_mirror->m_count = 1;
3992 mirror_list = lfs_mirror_alloc();
3993 mirror_list->m_file = optarg;
3998 if (strcmp(argv[optind - 1], "mdt") == 0) {
3999 /* Can be only the first component */
4003 "error: 'mdt' layout can be only the first one\n");
4006 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
4009 "error: 'mdt' layout size is too big\n");
4012 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
4013 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
4014 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
4017 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
4023 if (!migrate_mode) {
4025 "%s %s: -m|--mdt-index is valid only for migrate command\n",
4029 migrate_mdt_mode = true;
4030 lsa.lsa_nr_tgts = parse_targets(tgts,
4031 sizeof(tgts) / sizeof(__u32),
4032 lsa.lsa_nr_tgts, optarg, NULL);
4033 if (lsa.lsa_nr_tgts < 0) {
4035 "%s: invalid MDT target(s) '%s'\n",
4040 lsa.lsa_tgts = tgts;
4041 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4042 lsa.lsa_stripe_off = tgts[0];
4045 if (!migrate_mode) {
4047 "%s %s: -n|--non-block valid only for migrate command\n",
4051 migration_flags |= LLAPI_MIGRATION_NONBLOCK;
4054 if (opc == SO_SETSTRIPE) {
4055 opc = SO_MIRROR_CREATE;
4061 mirror_count = strtoul(optarg, &end, 0);
4062 if (errno != 0 || *end != '\0' ||
4063 mirror_count == 0 ||
4064 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
4066 "error: %s: bad mirror count: %s\n",
4073 new_mirror = lfs_mirror_alloc();
4074 new_mirror->m_count = mirror_count;
4077 mirror_list = new_mirror;
4080 /* wrap up last mirror */
4081 if (!setstripe_args_specified(&lsa))
4082 last_mirror->m_inherit = true;
4083 if (lsa.lsa_comp_end == 0)
4084 lsa.lsa_comp_end = LUSTRE_EOF;
4086 result = comp_args_to_layout(lpp, &lsa, true);
4088 lfs_mirror_free(new_mirror);
4092 setstripe_args_init_inherit(&lsa);
4094 last_mirror->m_next = new_mirror;
4097 last_mirror = new_mirror;
4098 lpp = &last_mirror->m_layout;
4101 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4102 if (strcmp(argv[optind - 1], "--ost-list") == 0)
4104 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
4106 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
4108 "%s %s: -o|--ost incompatible with DoM layout\n",
4113 * -o allows overstriping, and must note it because
4114 * parse_targets is shared with MDT striping, which
4115 * does not allow duplicates
4117 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
4118 lsa.lsa_nr_tgts = parse_targets(tgts,
4119 sizeof(tgts) / sizeof(__u32),
4120 lsa.lsa_nr_tgts, optarg,
4122 if (lsa.lsa_nr_tgts < 0) {
4124 "%s %s: invalid OST target(s) '%s'\n",
4125 progname, argv[0], optarg);
4129 lsa.lsa_tgts = tgts;
4130 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4131 lsa.lsa_stripe_off = tgts[0];
4137 if (optarg[0] == '\0' || lov_pool_is_inherited(optarg))
4138 lsa.lsa_pool_name = NULL;
4140 lsa.lsa_pool_name = optarg;
4143 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
4145 /* assume units of KB if too small to be valid */
4146 if (lsa.lsa_stripe_size < 4096)
4147 lsa.lsa_stripe_size *= 1024;
4149 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
4151 "%s %s: invalid stripe size '%s'\n",
4152 progname, argv[0], optarg);
4157 if (!migrate_mode) {
4159 "%s %s: -v|--verbose valid only for migrate command\n",
4163 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
4164 migration_flags = LLAPI_MIGRATION_VERBOSE;
4170 if (!migrate_mode && !mirror_mode) {
4172 "--bandwidth is valid only for migrate and mirror mode\n");
4175 if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
4176 &bandwidth_unit, 0) < 0) {
4178 "error: %s: bad value for bandwidth '%s'\n",
4188 result = llapi_parse_size(optarg,
4189 &lsa.lsa_extension_size,
4193 "%s %s: invalid extension size '%s'\n",
4194 progname, argv[0], optarg);
4198 lsa.lsa_extension_comp = true;
4201 fprintf(stderr, "%s: unrecognized option '%s'\n",
4202 progname, argv[optind - 1]);
4208 fname = argv[optind];
4210 if (optind == argc) {
4211 fprintf(stderr, "%s %s: FILE must be specified\n",
4216 /* lfs migrate $filename should keep the file's layout by default */
4217 if (migrate_mode && !layout && !from_yaml &&
4218 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4221 if (xattr && !foreign_mode) {
4223 * only print a warning as this is harmless and will be ignored
4226 "%s %s: xattr has been specified for non-foreign layout\n",
4228 } else if (foreign_mode && !xattr) {
4230 "%s %s: xattr must be provided in foreign mode\n",
4235 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4236 comp_set || comp_id || delete || from_copy ||
4237 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4240 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4245 if (mirror_mode && mirror_count == 0) {
4247 "error: %s: --mirror-count|-N option is required\n",
4254 if (!setstripe_args_specified(&lsa))
4255 last_mirror->m_inherit = true;
4256 if (lsa.lsa_comp_end == 0)
4257 lsa.lsa_comp_end = LUSTRE_EOF;
4260 if (lsa.lsa_comp_end != 0) {
4261 result = comp_args_to_layout(lpp, &lsa, true);
4263 fprintf(stderr, "error: %s: invalid layout\n",
4270 if (mirror_flags & MF_NO_VERIFY) {
4271 if (opc != SO_MIRROR_EXTEND) {
4273 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4277 } else if (!has_m_file) {
4279 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4286 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4288 "%s %s: --component-set doesn't have component-id set\n",
4293 if ((delete + comp_set + comp_del + comp_add) > 1) {
4295 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4300 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4301 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4303 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4308 if ((comp_set || comp_del) &&
4309 (setstripe_args_specified(&lsa) || layout != NULL)) {
4311 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4316 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4318 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4323 if (comp_add || comp_del) {
4326 result = lstat(fname, &st);
4327 if (result == 0 && S_ISDIR(st.st_mode)) {
4329 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4336 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4345 "%s %s: option -E must be specified with --component-add\n",
4351 if (from_yaml && from_copy) {
4353 "%s: can't specify --yaml and --copy together\n",
4358 if ((from_yaml || from_copy) &&
4359 (setstripe_args_specified(&lsa) || layout != NULL)) {
4361 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4366 if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4368 "%s %s: options --non-block and --block are mutually exclusive\n",
4373 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4374 opc != SO_MIRROR_DELETE && comp_id != 0) {
4376 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4381 if (migrate_mdt_mode) {
4382 struct lmv_user_md *lmu;
4384 /* initialize migrate mdt parameters */
4385 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4386 LMV_USER_MAGIC_SPECIFIC));
4389 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4390 progname, argv[0], strerror(ENOMEM));
4394 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4395 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4396 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4398 "%s %s: migrate should specify MDT index\n",
4403 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4404 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4405 lmu->lum_hash_type = lsa.lsa_pattern;
4407 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4408 if (lsa.lsa_pool_name) {
4409 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4410 sizeof(lmu->lum_pool_name) - 1);
4411 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4413 if (lsa.lsa_nr_tgts > 1) {
4416 if (lsa.lsa_stripe_count > 0 &&
4417 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4418 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4420 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4421 progname, lsa.lsa_stripe_count,
4427 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4428 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4429 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4430 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4432 lmu->lum_magic = LMV_USER_MAGIC;
4435 migrate_mdt_param.fp_lmv_md = lmu;
4436 migrate_mdt_param.fp_migrate = 1;
4437 } else if (!layout) {
4438 if (lsa_args_stripe_count_check(&lsa))
4441 /* initialize stripe parameters */
4442 param = calloc(1, offsetof(typeof(*param),
4443 lsp_osts[lsa.lsa_nr_tgts]));
4446 "%s %s: cannot allocate memory for parameters: %s\n",
4447 progname, argv[0], strerror(ENOMEM));
4452 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4453 param->lsp_stripe_size = lsa.lsa_stripe_size;
4454 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4455 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4456 param->lsp_stripe_count = -1;
4458 param->lsp_stripe_count = lsa.lsa_stripe_count;
4460 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4461 param->lsp_stripe_offset = -1;
4463 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4464 param->lsp_stripe_pattern =
4465 llapi_pattern_to_lov(lsa.lsa_pattern);
4466 if (param->lsp_stripe_pattern == EINVAL) {
4467 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4472 param->lsp_pool = lsa.lsa_pool_name;
4473 param->lsp_is_specific = false;
4475 if (lsa.lsa_nr_tgts > 0) {
4476 param->lsp_is_specific = true;
4477 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4478 memcpy(param->lsp_osts, tgts,
4479 sizeof(*tgts) * lsa.lsa_nr_tgts);
4484 /* generate a layout from a YAML template */
4485 result = lfs_comp_create_from_yaml(template, &layout,
4489 "error: %s: can't create composite layout from template file %s\n",
4495 if (layout != NULL || mirror_list != NULL) {
4497 result = mirror_adjust_first_extents(mirror_list);
4499 result = layout_adjust_first_extent(fname, layout,
4501 if (result == -ENODATA)
4503 else if (result != 0) {
4504 fprintf(stderr, "error: %s: invalid layout\n",
4510 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4512 layout = llapi_layout_get_by_path(template ?: fname, 0);
4515 "%s: can't create composite layout from file %s: %s\n",
4516 progname, template ?: fname,
4523 if (migrate_mdt_mode) {
4524 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4525 } else if (migrate_mode) {
4528 * Strip the source layout of specific
4529 * OST object/index values.
4531 result = llapi_layout_ost_index_set(layout, 0,
4532 LLAPI_LAYOUT_DEFAULT);
4535 "%s: set default ost index failed: %s\n",
4536 progname, strerror(errno));
4542 result = lfs_migrate(fname, migration_flags, param,
4543 layout, bandwidth_bytes_sec,
4544 stats_interval_sec);
4545 } else if (comp_set != 0) {
4546 result = lfs_component_set(fname, comp_id,
4549 lsa.lsa_comp_neg_flags);
4550 } else if (comp_del != 0) {
4551 result = lfs_component_del(fname, comp_id,
4553 lsa.lsa_comp_neg_flags);
4554 } else if (comp_add != 0) {
4555 result = lfs_component_add(fname, layout);
4556 } else if (opc == SO_MIRROR_CREATE) {
4557 result = mirror_create(fname, mirror_list);
4558 } else if (opc == SO_MIRROR_EXTEND) {
4559 result = mirror_extend(fname, mirror_list,
4561 bandwidth_bytes_sec,
4562 stats_interval_sec);
4563 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4564 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4566 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4570 if (lsa.lsa_pool_name)
4571 mirror_flags |= MF_COMP_POOL;
4572 else if (mirror_id != 0)
4573 comp_id = mirror_id;
4575 mirror_flags |= MF_COMP_ID;
4576 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4578 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4582 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4584 has_m_file ? mirror_list->m_file :
4586 } else if (layout) {
4587 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4593 } else if (foreign_mode) {
4594 result = llapi_file_create_foreign(fname, mode, type,
4601 result = llapi_file_open_param(fname,
4610 /* Save the first error encountered. */
4618 umask(previous_umask);
4621 free(migrate_mdt_param.fp_lmv_md);
4622 llapi_layout_free(layout);
4623 lfs_mirror_list_free(mirror_list);
4628 llapi_layout_free(layout);
4629 lfs_mirror_list_free(mirror_list);
4633 static int lfs_poollist(int argc, char **argv)
4638 return llapi_poollist(argv[1]);
4641 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4642 static int set_time(struct find_param *param, time_t *time, time_t *set,
4647 char *endptr = "AD";
4652 else if (str[0] == '-')
4658 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4659 long long val = strtoll(timebuf, &endptr, 0);
4664 unit *= 52; /* 52 weeks + 1 day below */
4668 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4669 param->fp_time_margin *= (1 + unit / 52);
4670 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4672 case '\0': /* days are default unit if none used */
4685 /* don't need to multiply by 1 for seconds */
4688 "%s find: bad time string '%s': %s\n",
4689 progname, timebuf, strerror(EINVAL));
4693 if (param->fp_time_margin == 0 ||
4694 (*endptr && unit < param->fp_time_margin))
4695 param->fp_time_margin = unit;
4702 fprintf(stderr, "%s find: bad time '%s': too large\n",
4712 static int str2quotaid(__u32 *id, const char *arg)
4714 unsigned long int projid_tmp = 0;
4715 char *endptr = NULL;
4717 projid_tmp = strtoul(arg, &endptr, 10);
4718 if (*endptr != '\0')
4720 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4721 if (projid_tmp >= UINT32_MAX)
4728 static int name2uid(unsigned int *id, const char *name)
4730 struct passwd *passwd;
4732 passwd = getpwnam(name);
4735 *id = passwd->pw_uid;
4740 static int name2gid(unsigned int *id, const char *name)
4742 struct group *group;
4744 group = getgrnam(name);
4747 *id = group->gr_gid;
4752 static inline int name2projid(unsigned int *id, const char *name)
4757 static int uid2name(char **name, unsigned int id)
4759 struct passwd *passwd;
4761 passwd = getpwuid(id);
4764 *name = passwd->pw_name;
4769 static inline int gid2name(char **name, unsigned int id)
4771 struct group *group;
4773 group = getgrgid(id);
4776 *name = group->gr_name;
4781 static int name2layout(__u32 *layout, char *name)
4783 char *ptr, *layout_name;
4786 for (ptr = name; ; ptr = NULL) {
4787 layout_name = strtok(ptr, ",");
4790 if (strcmp(layout_name, "released") == 0)
4791 *layout |= LOV_PATTERN_F_RELEASED;
4792 else if (strcmp(layout_name, "raid0") == 0)
4793 *layout |= LOV_PATTERN_RAID0;
4794 else if (strcmp(layout_name, "mdt") == 0)
4795 *layout |= LOV_PATTERN_MDT;
4796 else if (strcmp(layout_name, "overstriping") == 0)
4797 *layout |= LOV_PATTERN_OVERSTRIPING;
4804 static int name2attrs(char *name, __u64 *attrs, __u64 *neg_attrs)
4806 char *ptr, *attr_name = name;
4807 struct attrs_name *ap;
4808 int islongopt = 0; /* 1 true; 0 not known yet; -1 false. */
4813 if (strchr(name, ','))
4816 for (ptr = name; ; ptr = NULL) {
4817 if (islongopt != -1)
4818 attr_name = strtok(ptr, ",");
4820 attr_name = attr_name + 1;
4821 if (!attr_name || *attr_name == '\0')
4824 for (ap = (struct attrs_name *)attrs_array;
4827 if (islongopt != -1 &&
4828 strcmp(attr_name, ap->an_name) == 0) {
4829 *attrs |= ap->an_attr;
4832 } else if (islongopt != -1 && attr_name[0] == '^' &&
4833 strcmp(attr_name + 1, ap->an_name) == 0) {
4834 *neg_attrs |= ap->an_attr;
4837 } else if (islongopt != 1 &&
4838 *attr_name == ap->an_shortname) {
4839 *attrs |= ap->an_attr;
4842 } else if (islongopt != 1 && *attr_name == '^' &&
4843 attr_name[1] == ap->an_shortname) {
4844 *neg_attrs |= ap->an_attr;
4851 if (ap->an_attr == 0) {
4852 /* provided attr is unknown */
4853 fprintf(stderr, "error: bad attribute name '%s'\n",
4861 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4864 int user, group, other;
4869 mode_t previous_flags;
4871 user = group = other = 0;
4891 user = group = other = 1;
4902 who = user || group || other;
4905 usermask = umask(0022);
4910 if (*input == '-' || *input == '+' || *input == '=')
4913 /* operation is required */
4916 /* get the flags in *outmode */
4919 previous_flags = (*outmode & 0700);
4920 perm |= user ? previous_flags : 0;
4921 perm |= group ? (previous_flags >> 3) : 0;
4922 perm |= other ? (previous_flags >> 6) : 0;
4926 previous_flags = (*outmode & 0070);
4927 perm |= user ? (previous_flags << 3) : 0;
4928 perm |= group ? previous_flags : 0;
4929 perm |= other ? (previous_flags >> 3) : 0;
4933 previous_flags = (*outmode & 0007);
4934 perm |= user ? (previous_flags << 6) : 0;
4935 perm |= group ? (previous_flags << 3) : 0;
4936 perm |= other ? previous_flags : 0;
4943 /* this part is optional,
4944 * if empty perm = 0 and *outmode is not modified
4951 perm |= user ? 0400 : 0;
4952 perm |= group ? 0040 : 0;
4953 perm |= other ? 0004 : 0;
4954 /* set read permission for uog except for umask's
4957 perm |= who ? 0 : (0444 & ~usermask);
4960 perm |= user ? 0200 : 0;
4961 perm |= group ? 0020 : 0;
4962 perm |= other ? 0002 : 0;
4963 /* set write permission for uog except for umask'
4966 perm |= who ? 0 : (0222 & ~usermask);
4969 perm |= user ? 0100 : 0;
4970 perm |= group ? 0010 : 0;
4971 perm |= other ? 0001 : 0;
4972 /* set execute permission for uog except for umask'
4975 perm |= who ? 0 : (0111 & ~usermask);
4979 * Adds execute permission to 'u', 'g' and/or 'g' if
4980 * specified and either 'u', 'g' or 'o' already has
4981 * execute permissions.
4983 if ((*outmode & 0111) != 0) {
4984 perm |= user ? 0100 : 0;
4985 perm |= group ? 0010 : 0;
4986 perm |= other ? 0001 : 0;
4987 perm |= !who ? 0111 : 0;
4991 /* s is ignored if o is given, but it's not an error */
4992 if (other && !group && !user)
4994 perm |= user ? S_ISUID : 0;
4995 perm |= group ? S_ISGID : 0;
4998 /* 't' should be used when 'a' is given
5001 perm |= (!who || all) ? S_ISVTX : 0;
5002 /* using ugo with t is not an error */
5013 /* uog flags should be only one character long */
5014 if (previous_flags && (*input != '\0' && *input != ','))
5019 /* remove the flags from outmode */
5023 /* add the flags to outmode */
5027 /* set the flags of outmode to perm */
5037 static int str2mode_t(const char *input, mode_t *outmode)
5044 if (*input >= '0' && *input <= '7') {
5045 /* parse octal representation */
5050 /* look for invalid digits in octal representation */
5051 while (isdigit(*iter))
5056 *outmode = strtoul(input, &end, 8);
5058 if (errno != 0 || *outmode > 07777) {
5063 } else if (*input == '8' || *input == '9') {
5064 /* error: invalid octal number */
5067 /* parse coma seperated list of symbolic representation */
5076 rc = parse_symbolic(input, outmode, &end);
5081 } while (*end == ',');
5089 static int lfs_find(int argc, char **argv)
5094 struct find_param param = {
5097 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
5099 struct option long_opts[] = {
5100 { .val = 'A', .name = "atime", .has_arg = required_argument },
5101 { .val = LFS_ATTRS_OPT,
5102 .name = "attrs", .has_arg = required_argument },
5103 { .val = 'b', .name = "blocks", .has_arg = required_argument },
5104 { .val = 'B', .name = "btime", .has_arg = required_argument },
5105 { .val = 'B', .name = "Btime", .has_arg = required_argument },
5106 { .val = LFS_COMP_COUNT_OPT,
5107 .name = "comp-count", .has_arg = required_argument },
5108 { .val = LFS_COMP_COUNT_OPT,
5109 .name = "component-count",
5110 .has_arg = required_argument },
5111 { .val = LFS_COMP_FLAGS_OPT,
5112 .name = "comp-flags", .has_arg = required_argument },
5113 { .val = LFS_COMP_FLAGS_OPT,
5114 .name = "component-flags",
5115 .has_arg = required_argument },
5116 { .val = LFS_COMP_START_OPT,
5117 .name = "comp-start", .has_arg = required_argument },
5118 { .val = LFS_COMP_START_OPT,
5119 .name = "component-start",
5120 .has_arg = required_argument },
5121 { .val = LFS_MIRROR_STATE_OPT,
5122 .name = "mirror-state", .has_arg = required_argument },
5123 { .val = LFS_NEWERXY_OPT,
5124 .name = "newer", .has_arg = required_argument},
5125 { .val = LFS_NEWERXY_OPT,
5126 .name = "neweraa", .has_arg = required_argument},
5127 { .val = LFS_NEWERXY_OPT,
5128 .name = "neweram", .has_arg = required_argument},
5129 { .val = LFS_NEWERXY_OPT,
5130 .name = "newerac", .has_arg = required_argument},
5131 { .val = LFS_NEWERXY_OPT,
5132 .name = "newerab", .has_arg = required_argument},
5133 { .val = LFS_NEWERXY_OPT,
5134 .name = "newerma", .has_arg = required_argument},
5135 { .val = LFS_NEWERXY_OPT,
5136 .name = "newermm", .has_arg = required_argument},
5137 { .val = LFS_NEWERXY_OPT,
5138 .name = "newermc", .has_arg = required_argument},
5139 { .val = LFS_NEWERXY_OPT,
5140 .name = "newermb", .has_arg = required_argument},
5141 { .val = LFS_NEWERXY_OPT,
5142 .name = "newerca", .has_arg = required_argument},
5143 { .val = LFS_NEWERXY_OPT,
5144 .name = "newercm", .has_arg = required_argument},
5145 { .val = LFS_NEWERXY_OPT,
5146 .name = "newercc", .has_arg = required_argument},
5147 { .val = LFS_NEWERXY_OPT,
5148 .name = "newercb", .has_arg = required_argument},
5149 { .val = LFS_NEWERXY_OPT,
5150 .name = "newerba", .has_arg = required_argument},
5151 { .val = LFS_NEWERXY_OPT,
5152 .name = "newerbm", .has_arg = required_argument},
5153 { .val = LFS_NEWERXY_OPT,
5154 .name = "newerbc", .has_arg = required_argument},
5155 { .val = LFS_NEWERXY_OPT,
5156 .name = "newerbb", .has_arg = required_argument},
5157 { .val = LFS_NEWERXY_OPT,
5158 .name = "newerBa", .has_arg = required_argument},
5159 { .val = LFS_NEWERXY_OPT,
5160 .name = "newerBm", .has_arg = required_argument},
5161 { .val = LFS_NEWERXY_OPT,
5162 .name = "newerBc", .has_arg = required_argument},
5163 { .val = LFS_NEWERXY_OPT,
5164 .name = "newerBB", .has_arg = required_argument},
5165 { .val = LFS_NEWERXY_OPT,
5166 .name = "newerat", .has_arg = required_argument},
5167 { .val = LFS_NEWERXY_OPT,
5168 .name = "newermt", .has_arg = required_argument},
5169 { .val = LFS_NEWERXY_OPT,
5170 .name = "newerct", .has_arg = required_argument},
5171 { .val = LFS_NEWERXY_OPT,
5172 .name = "newerbt", .has_arg = required_argument},
5173 { .val = LFS_NEWERXY_OPT,
5174 .name = "newerBt", .has_arg = required_argument},
5175 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
5176 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
5177 { .val = 'C', .name = "ctime", .has_arg = required_argument },
5178 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
5179 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
5180 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
5181 { .val = 'E', .name = "component-end",
5182 .has_arg = required_argument },
5183 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
5184 { .val = LFS_LAYOUT_FOREIGN_OPT,
5185 .name = "foreign", .has_arg = optional_argument},
5186 { .val = 'g', .name = "gid", .has_arg = required_argument },
5187 { .val = 'G', .name = "group", .has_arg = required_argument },
5188 { .val = 'h', .name = "help", .has_arg = no_argument },
5189 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5190 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
5191 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
5192 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
5193 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5194 { .val = 'L', .name = "layout", .has_arg = required_argument },
5195 { .val = LFS_LINKS_OPT,
5196 .name = "links", .has_arg = required_argument },
5197 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5198 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5199 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
5200 { .val = 'M', .name = "mtime", .has_arg = required_argument },
5201 { .val = 'n', .name = "name", .has_arg = required_argument },
5202 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
5203 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
5204 { .val = 'O', .name = "obd", .has_arg = required_argument },
5205 { .val = 'O', .name = "ost", .has_arg = required_argument },
5206 { .val = LFS_FIND_PERM,
5207 .name = "perm", .has_arg = required_argument },
5208 /* no short option for pool yet, can be 'p' after 2.18 */
5209 { .val = LFS_POOL_OPT,
5210 .name = "pool", .has_arg = required_argument },
5211 { .val = '0', .name = "print0", .has_arg = no_argument },
5212 { .val = 'P', .name = "print", .has_arg = no_argument },
5213 { .val = LFS_PRINTF_OPT,
5214 .name = "printf", .has_arg = required_argument },
5215 { .val = LFS_PROJID_OPT,
5216 .name = "projid", .has_arg = required_argument },
5217 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
5218 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
5219 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
5220 { .val = 's', .name = "size", .has_arg = required_argument },
5221 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
5222 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
5223 { .val = 't', .name = "type", .has_arg = required_argument },
5224 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5225 { .val = 'u', .name = "uid", .has_arg = required_argument },
5226 { .val = 'U', .name = "user", .has_arg = required_argument },
5227 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
5228 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
5229 { .val = 'z', .name = "extension-size",
5230 .has_arg = required_argument },
5231 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
5233 int prev_optind = optind;
5246 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
5247 while ((c = getopt_long_only(argc, argv,
5248 "-0A:b:B:c:C:D:E:g:G:hH:i:lL:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
5249 long_opts, &optidx)) >= 0) {
5254 /* '!' is part of option */
5256 * when getopt_long_only() finds a string which is not
5257 * an option nor a known option argument it returns 1
5258 * in that case if we already have found pathstart and pathend
5259 * (i.e. we have the list of pathnames),
5260 * the only supported value is "!"
5262 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5263 if (!isoption && pathend != -1) {
5265 "err: %s: filename|dirname must either precede options or follow options\n",
5270 if (!isoption && pathstart == -1)
5271 pathstart = prev_optind;
5272 if (isoption && pathstart != -1 && pathend == -1)
5273 pathend = prev_optind;
5275 prev_optind = optind;
5283 * unknown; opt is "!" or path component,
5284 * checking done above.
5286 if (strcmp(optarg, "!") == 0)
5290 xtime = ¶m.fp_atime;
5291 xsign = ¶m.fp_asign;
5292 param.fp_exclude_atime = !!neg_opt;
5293 /* no break, this falls through to 'B' for btime */
5297 xtime = ¶m.fp_btime;
5298 xsign = ¶m.fp_bsign;
5299 param.fp_exclude_btime = !!neg_opt;
5301 /* no break, this falls through to 'C' for ctime */
5305 xtime = ¶m.fp_ctime;
5306 xsign = ¶m.fp_csign;
5307 param.fp_exclude_ctime = !!neg_opt;
5309 /* no break, this falls through to 'M' for mtime */
5313 xtime = ¶m.fp_mtime;
5314 xsign = ¶m.fp_msign;
5315 param.fp_exclude_mtime = !!neg_opt;
5317 rc = set_time(¶m, &t, xtime, optarg);
5318 if (rc == INT_MAX) {
5326 ret = name2attrs(optarg, ¶m.fp_attrs,
5327 ¶m.fp_neg_attrs);
5330 param.fp_exclude_attrs = !!neg_opt;
5333 if (optarg[0] == '+') {
5334 param.fp_blocks_sign = -1;
5336 } else if (optarg[0] == '-') {
5337 param.fp_blocks_sign = 1;
5341 param.fp_blocks_units = 1024;
5342 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5343 ¶m.fp_blocks_units, 0);
5345 fprintf(stderr, "error: bad blocks '%s'\n",
5349 param.fp_check_blocks = 1;
5350 param.fp_exclude_blocks = !!neg_opt;
5352 case LFS_COMP_COUNT_OPT:
5353 if (optarg[0] == '+') {
5354 param.fp_comp_count_sign = -1;
5356 } else if (optarg[0] == '-') {
5357 param.fp_comp_count_sign = 1;
5362 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5363 if (errno != 0 || *endptr != '\0' ||
5364 param.fp_comp_count > UINT32_MAX) {
5366 "error: bad component count '%s'\n",
5370 param.fp_check_comp_count = 1;
5371 param.fp_exclude_comp_count = !!neg_opt;
5373 case LFS_COMP_FLAGS_OPT:
5374 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5375 ¶m.fp_comp_neg_flags);
5378 "error: bad component flags '%s'\n",
5382 param.fp_check_comp_flags = 1;
5384 __u32 flags = param.fp_comp_neg_flags;
5386 param.fp_comp_neg_flags = param.fp_comp_flags;
5387 param.fp_comp_flags = flags;
5390 case LFS_COMP_START_OPT:
5391 if (optarg[0] == '+') {
5392 param.fp_comp_start_sign = -1;
5394 } else if (optarg[0] == '-') {
5395 param.fp_comp_start_sign = 1;
5399 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5400 ¶m.fp_comp_start_units, 0);
5403 "error: bad component start '%s'\n",
5407 param.fp_check_comp_start = 1;
5408 param.fp_exclude_comp_start = !!neg_opt;
5410 case LFS_MIRROR_STATE_OPT:
5411 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5412 ¶m.fp_mirror_neg_state);
5415 "error: bad mirrored file state '%s'\n",
5419 param.fp_check_mirror_state = 1;
5421 __u16 state = param.fp_mirror_neg_state;
5423 param.fp_mirror_neg_state =
5424 param.fp_mirror_state;
5425 param.fp_mirror_state = state;
5429 if (optarg[0] == '+') {
5430 param.fp_stripe_count_sign = -1;
5432 } else if (optarg[0] == '-') {
5433 param.fp_stripe_count_sign = 1;
5438 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5439 if (errno != 0 || *endptr != '\0' ||
5440 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5442 "error: bad stripe_count '%s'\n",
5447 param.fp_check_stripe_count = 1;
5448 param.fp_exclude_stripe_count = !!neg_opt;
5452 param.fp_max_depth = strtol(optarg, 0, 0);
5453 if (errno != 0 || param.fp_max_depth < 0) {
5455 "error: bad maxdepth '%s'\n",
5462 if (optarg[0] == '+') {
5463 param.fp_comp_end_sign = -1;
5465 } else if (optarg[0] == '-') {
5466 param.fp_comp_end_sign = 1;
5470 if (arg_is_eof(optarg)) {
5471 param.fp_comp_end = LUSTRE_EOF;
5472 param.fp_comp_end_units = 1;
5475 rc = llapi_parse_size(optarg,
5477 ¶m.fp_comp_end_units, 0);
5478 /* assume units of KB if too small */
5479 if (param.fp_comp_end < 4096)
5480 param.fp_comp_end *= 1024;
5484 "error: bad component end '%s'\n",
5488 param.fp_check_comp_end = 1;
5489 param.fp_exclude_comp_end = !!neg_opt;
5491 case LFS_LAYOUT_FOREIGN_OPT: {
5492 /* all types by default */
5493 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5496 /* check pure numeric */
5497 type = strtoul(optarg, &endptr, 0);
5500 type = check_foreign_type_name(optarg);
5501 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5503 "%s %s: unknown foreign type '%s'\n",
5508 } else if (type >= UINT32_MAX) {
5510 "%s %s: invalid foreign type '%s'\n",
5511 progname, argv[0], optarg);
5515 param.fp_foreign_type = type;
5516 param.fp_check_foreign = 1;
5517 param.fp_exclude_foreign = !!neg_opt;
5520 case LFS_NEWERXY_OPT: {
5526 time_t ref = time(NULL);
5528 /* no need to check bad options, they won't get here */
5529 if (strlen(long_opts[optidx].name) == 7) {
5530 x = long_opts[optidx].name[5];
5531 y = long_opts[optidx].name[6];
5535 static const char *const fmts[] = {
5536 "%Y-%m-%d %H:%M:%S",
5539 "%H:%M:%S", /* sometime today */
5548 for (i = 0; fmts[i] != NULL; i++) {
5551 /* Init for times relative to today */
5552 if (strncmp(fmts[i], "%H", 2) == 0) {
5553 localtime_r(&ref, &tm);
5555 memset(&tm, 0, sizeof(tm));
5558 ptr = strptime(optarg, fmts[i], &tm);
5560 while (ptr && isspace(*ptr))
5562 if (ptr == optarg + strlen(optarg)) {
5570 "%s: invalid time '%s'\n",
5573 "supported formats are:\n ");
5574 for (i = 0; fmts[i] != NULL; i++)
5575 fprintf(stderr, "'%s', ",
5577 fprintf(stderr, "\n");
5583 } else if (y == 'b' || y == 'B') {
5586 rc = llapi_get_lum_file(optarg, NULL, &stx,
5588 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5589 if (!(stx.stx_mask & STATX_BTIME))
5594 "%s: get btime failed '%s': %s\n",
5600 ref = stx.stx_btime.tv_sec;
5602 struct stat statbuf;
5604 if (stat(optarg, &statbuf) < 0) {
5606 "%s: cannot stat file '%s': %s\n",
5615 ref = statbuf.st_atime;
5618 ref = statbuf.st_mtime;
5621 ref = statbuf.st_ctime;
5625 "%s: invalid Y argument: '%c'\n",
5634 xidx = NEWERXY_ATIME;
5637 xidx = NEWERXY_MTIME;
5640 xidx = NEWERXY_CTIME;
5644 xidx = NEWERXY_BTIME;
5648 "%s: invalid X argument: '%c'\n",
5655 newery = ¶m.fp_newery[xidx][negidx];
5661 *newery = *newery > ref ? ref : *newery;
5663 *newery = *newery > ref ? *newery : ref;
5665 param.fp_newerxy = 1;
5670 rc = name2gid(¶m.fp_gid, optarg);
5672 if (str2quotaid(¶m.fp_gid, optarg)) {
5674 "Group/GID: %s cannot be found.\n",
5680 param.fp_exclude_gid = !!neg_opt;
5681 param.fp_check_gid = 1;
5684 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5685 ¶m.fp_hash_exflags,
5686 ¶m.fp_hash_type);
5691 if (param.fp_hash_inflags || param.fp_hash_exflags)
5692 param.fp_check_hash_flag = 1;
5693 param.fp_exclude_hash_type = !!neg_opt;
5699 ret = name2layout(¶m.fp_layout, optarg);
5702 param.fp_exclude_layout = !!neg_opt;
5703 param.fp_check_layout = 1;
5706 if (optarg[0] == '+') {
5707 param.fp_nlink_sign = -1;
5709 } else if (optarg[0] == '-') {
5710 param.fp_nlink_sign = 1;
5714 param.fp_nlink = strtoul(optarg, &endptr, 0);
5715 if (errno != 0 || *endptr != '\0' || !param.fp_nlink) {
5716 fprintf(stderr, "error: bad link count '%s'\n",
5721 param.fp_exclude_nlink = !!neg_opt;
5725 rc = name2uid(¶m.fp_uid, optarg);
5727 if (str2quotaid(¶m.fp_uid, optarg)) {
5729 "User/UID: %s cannot be found.\n",
5735 param.fp_exclude_uid = !!neg_opt;
5736 param.fp_check_uid = 1;
5739 param.fp_pattern = (char *)optarg;
5740 param.fp_exclude_pattern = !!neg_opt;
5743 if (optarg[0] == '+') {
5744 param.fp_mirror_count_sign = -1;
5746 } else if (optarg[0] == '-') {
5747 param.fp_mirror_count_sign = 1;
5752 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5753 if (errno != 0 || *endptr != '\0' ||
5754 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5756 "error: bad mirror count '%s'\n",
5760 param.fp_check_mirror_count = 1;
5761 param.fp_exclude_mirror_count = !!neg_opt;
5766 char *buf, *token, *next, *p;
5770 buf = strdup(optarg);
5776 param.fp_exclude_obd = !!neg_opt;
5779 while (token && *token) {
5780 token = strchr(token, ',');
5787 param.fp_exclude_mdt = !!neg_opt;
5788 param.fp_num_alloc_mdts += len;
5789 tmp = realloc(param.fp_mdt_uuid,
5790 param.fp_num_alloc_mdts *
5791 sizeof(*param.fp_mdt_uuid));
5797 param.fp_mdt_uuid = tmp;
5799 param.fp_exclude_obd = !!neg_opt;
5800 param.fp_num_alloc_obds += len;
5801 tmp = realloc(param.fp_obd_uuid,
5802 param.fp_num_alloc_obds *
5803 sizeof(*param.fp_obd_uuid));
5809 param.fp_obd_uuid = tmp;
5811 for (token = buf; token && *token; token = next) {
5812 struct obd_uuid *puuid;
5816 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5819 ¶m.fp_obd_uuid[param.fp_num_obds++];
5821 p = strchr(token, ',');
5828 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5833 strncpy(puuid->uuid, token,
5834 sizeof(puuid->uuid));
5841 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5845 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5847 "Pool name %s is too long (max %d)\n",
5848 optarg, LOV_MAXPOOLNAME);
5853 * We do check for empty pool because empty pool
5854 * is used to find V1 LOV attributes
5856 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5857 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5858 param.fp_exclude_pool = !!neg_opt;
5859 param.fp_check_pool = 1;
5862 param.fp_zero_end = 1;
5864 case 'P': /* we always print, this option is a no-op */
5866 case LFS_PRINTF_OPT:
5867 param.fp_format_printf_str = strdup(optarg);
5869 case LFS_PROJID_OPT:
5870 rc = name2projid(¶m.fp_projid, optarg);
5872 if (str2quotaid(¶m.fp_projid, optarg)) {
5874 "Invalid project ID: %s\n",
5880 param.fp_exclude_projid = !!neg_opt;
5881 param.fp_check_projid = 1;
5884 if (optarg[0] == '+') {
5885 param.fp_size_sign = -1;
5887 } else if (optarg[0] == '-') {
5888 param.fp_size_sign = 1;
5892 ret = llapi_parse_size(optarg, ¶m.fp_size,
5893 ¶m.fp_size_units, 0);
5895 fprintf(stderr, "error: bad file size '%s'\n",
5899 param.fp_check_size = 1;
5900 param.fp_exclude_size = !!neg_opt;
5903 if (optarg[0] == '+') {
5904 param.fp_stripe_size_sign = -1;
5906 } else if (optarg[0] == '-') {
5907 param.fp_stripe_size_sign = 1;
5911 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5912 ¶m.fp_stripe_size_units, 0);
5913 /* assume units of KB if too small to be valid */
5914 if (param.fp_stripe_size < 4096)
5915 param.fp_stripe_size *= 1024;
5917 fprintf(stderr, "error: bad stripe_size '%s'\n",
5921 param.fp_check_stripe_size = 1;
5922 param.fp_exclude_stripe_size = !!neg_opt;
5925 param.fp_exclude_type = !!neg_opt;
5926 switch (optarg[0]) {
5928 param.fp_type = S_IFBLK;
5931 param.fp_type = S_IFCHR;
5934 param.fp_type = S_IFDIR;
5937 param.fp_type = S_IFREG;
5940 param.fp_type = S_IFLNK;
5943 param.fp_type = S_IFIFO;
5946 param.fp_type = S_IFSOCK;
5949 fprintf(stderr, "%s: bad type '%s'\n",
5956 param.fp_exclude_perm = !!neg_opt;
5957 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5958 if (*optarg == '/') {
5959 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5961 } else if (*optarg == '-') {
5962 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5966 if (str2mode_t(optarg, ¶m.fp_perm)) {
5967 fprintf(stderr, "error: invalid mode '%s'\n",
5974 if (optarg[0] == '+') {
5975 param.fp_mdt_count_sign = -1;
5977 } else if (optarg[0] == '-') {
5978 param.fp_mdt_count_sign = 1;
5983 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5984 if (errno != 0 || *endptr != '\0' ||
5985 param.fp_mdt_count >= UINT32_MAX) {
5986 fprintf(stderr, "error: bad mdt_count '%s'\n",
5991 param.fp_check_mdt_count = 1;
5992 param.fp_exclude_mdt_count = !!neg_opt;
5995 if (optarg[0] == '+') {
5996 param.fp_ext_size_sign = -1;
5998 } else if (optarg[0] == '-') {
5999 param.fp_ext_size_sign = 1;
6003 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
6004 ¶m.fp_ext_size_units, 0);
6006 fprintf(stderr, "error: bad ext-size '%s'\n",
6010 param.fp_ext_size /= SEL_UNIT_SIZE;
6011 param.fp_ext_size_units /= SEL_UNIT_SIZE;
6012 param.fp_check_ext_size = 1;
6013 param.fp_exclude_ext_size = !!neg_opt;
6016 fprintf(stderr, "%s: unrecognized option '%s'\n",
6017 progname, argv[optind - 1]);
6023 if (!param.fp_verbose)
6024 param.fp_verbose = VERBOSE_DEFAULT;
6026 if (pathstart == -1) {
6027 fprintf(stderr, "error: %s: no filename|pathname\n",
6031 } else if (pathend == -1) {
6037 rc = llapi_find(argv[pathstart], ¶m);
6040 pathbad = pathstart;
6042 } while (++pathstart < pathend);
6045 fprintf(stderr, "%s: failed for '%s': %s\n",
6046 progname, argv[pathbad], strerror(-rc));
6049 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
6050 free(param.fp_obd_uuid);
6052 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
6053 free(param.fp_mdt_uuid);
6055 if (param.fp_format_printf_str)
6056 free(param.fp_format_printf_str);
6061 static int lfs_getstripe_internal(int argc, char **argv,
6062 struct find_param *param)
6064 struct option long_opts[] = {
6065 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
6066 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
6067 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
6068 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
6069 { .val = LFS_COMP_COUNT_OPT,
6070 .name = "comp-count", .has_arg = no_argument },
6071 { .val = LFS_COMP_COUNT_OPT,
6072 .name = "component-count", .has_arg = no_argument },
6073 { .val = LFS_COMP_FLAGS_OPT,
6074 .name = "comp-flags", .has_arg = optional_argument },
6075 { .val = LFS_COMP_FLAGS_OPT,
6076 .name = "component-flags", .has_arg = optional_argument },
6077 { .val = LFS_COMP_START_OPT,
6078 .name = "comp-start", .has_arg = optional_argument },
6079 { .val = LFS_COMP_START_OPT,
6080 .name = "component-start", .has_arg = optional_argument },
6081 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
6082 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
6083 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
6084 { .val = 'd', .name = "directory", .has_arg = no_argument },
6085 { .val = 'D', .name = "default", .has_arg = no_argument },
6086 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
6087 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
6088 { .val = 'F', .name = "fid", .has_arg = no_argument },
6089 { .val = 'g', .name = "generation", .has_arg = no_argument },
6090 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
6091 { .val = 'h', .name = "help", .has_arg = no_argument },
6092 { .val = LFS_HEX_IDX_OPT,
6093 .name = "hex-idx", .has_arg = no_argument },
6094 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
6095 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
6096 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
6097 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
6098 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
6099 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6100 { .val = 'L', .name = "layout", .has_arg = no_argument },
6101 { .val = 'm', .name = "mdt", .has_arg = no_argument },
6102 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6103 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
6104 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
6105 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
6106 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
6107 { .val = LFS_MIRROR_INDEX_OPT,
6108 .name = "mirror-index", .has_arg = required_argument },
6109 { .val = LFS_MIRROR_ID_OPT,
6110 .name = "mirror-id", .has_arg = required_argument },
6111 { .val = LFS_NO_FOLLOW_OPT,
6112 .name = "no-follow", .has_arg = no_argument },
6113 { .val = 'O', .name = "obd", .has_arg = required_argument },
6114 { .val = 'O', .name = "ost", .has_arg = required_argument },
6115 { .val = 'p', .name = "pool", .has_arg = no_argument },
6116 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
6117 { .val = 'q', .name = "quiet", .has_arg = no_argument },
6118 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6119 { .val = 'R', .name = "raw", .has_arg = no_argument },
6120 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
6121 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
6122 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
6123 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
6124 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
6125 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
6126 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6127 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
6128 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }*/
6129 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6130 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
6131 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
6135 int pathstart = -1, pathend = -1;
6139 while ((c = getopt_long(argc, argv,
6140 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
6141 long_opts, NULL)) != -1) {
6145 /* '!' is part of option */
6146 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
6147 if (!isoption && pathend != -1) {
6149 "error: %s: filename|dirname must either precede options or follow options\n",
6153 if (!isoption && pathstart == -1)
6154 pathstart = optind - 1;
6155 if (isoption && pathstart != -1 && pathend == -1)
6156 pathend = optind - 2;
6160 /* unknown: opt is "!" */
6161 if (strcmp(optarg, "!") == 0)
6165 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6166 param->fp_verbose |= VERBOSE_COUNT;
6167 param->fp_max_depth = 0;
6170 case LFS_COMP_COUNT_OPT:
6171 param->fp_verbose |= VERBOSE_COMP_COUNT;
6172 param->fp_max_depth = 0;
6174 case LFS_COMP_FLAGS_OPT:
6176 rc = comp_str2flags(optarg,
6177 ¶m->fp_comp_flags,
6178 ¶m->fp_comp_neg_flags);
6181 "error: %s bad component flags '%s'.\n",
6185 param->fp_check_comp_flags = 1;
6187 param->fp_verbose |= VERBOSE_COMP_FLAGS;
6188 param->fp_max_depth = 0;
6191 case LFS_COMP_START_OPT:
6194 if (tmp[0] == '+') {
6195 param->fp_comp_start_sign = -1;
6197 } else if (tmp[0] == '-') {
6198 param->fp_comp_start_sign = 1;
6201 rc = llapi_parse_size(tmp,
6202 ¶m->fp_comp_start,
6203 ¶m->fp_comp_start_units, 0);
6206 "error: %s bad component start '%s'.\n",
6210 param->fp_check_comp_start = 1;
6212 param->fp_verbose |= VERBOSE_COMP_START;
6213 param->fp_max_depth = 0;
6216 case LFS_MIRROR_INDEX_OPT: {
6217 unsigned long int mirror_index;
6219 if (optarg[0] == '+') {
6220 param->fp_mirror_index_sign = -1;
6222 } else if (optarg[0] == '-') {
6223 param->fp_mirror_index_sign = 1;
6228 mirror_index = strtoul(optarg, &end, 0);
6229 if (errno != 0 || *end != '\0' ||
6230 mirror_index > UINT16_MAX || (mirror_index == 0 &&
6231 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
6233 "%s %s: invalid mirror index '%s'\n",
6234 progname, argv[0], optarg);
6238 param->fp_mirror_index = (__u16)mirror_index;
6240 if (param->fp_mirror_id != 0) {
6242 "%s %s: can't specify both mirror index and mirror ID\n",
6246 param->fp_check_mirror_index = 1;
6247 param->fp_exclude_mirror_index = !!neg_opt;
6250 case LFS_MIRROR_ID_OPT: {
6251 unsigned long int mirror_id;
6253 if (optarg[0] == '+') {
6254 param->fp_mirror_id_sign = -1;
6256 } else if (optarg[0] == '-') {
6257 param->fp_mirror_id_sign = 1;
6262 mirror_id = strtoul(optarg, &end, 0);
6263 if (errno != 0 || *end != '\0' ||
6264 mirror_id > UINT16_MAX || (mirror_id == 0 &&
6265 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
6267 "%s %s: invalid mirror ID '%s'\n",
6268 progname, argv[0], optarg);
6272 param->fp_mirror_id = (__u16)mirror_id;
6274 if (param->fp_mirror_index != 0) {
6276 "%s %s: can't specify both mirror index and mirror ID\n",
6280 param->fp_check_mirror_id = 1;
6281 param->fp_exclude_mirror_id = !!neg_opt;
6284 case LFS_NO_FOLLOW_OPT:
6285 param->fp_no_follow = true;
6287 case LFS_HEX_IDX_OPT:
6288 param->fp_hex_idx = true;
6291 param->fp_max_depth = 0;
6294 param->fp_get_default_lmv = 1;
6299 if (tmp[0] == '+') {
6300 param->fp_comp_end_sign = -1;
6302 } else if (tmp[0] == '-') {
6303 param->fp_comp_end_sign = 1;
6307 if (arg_is_eof(tmp)) {
6308 param->fp_comp_end = LUSTRE_EOF;
6309 param->fp_comp_end_units = 1;
6312 rc = llapi_parse_size(tmp,
6313 ¶m->fp_comp_end,
6314 ¶m->fp_comp_end_units, 0);
6315 /* assume units of KB if too small */
6316 if (param->fp_comp_end < 4096)
6317 param->fp_comp_end *= 1024;
6321 "error: %s bad component end '%s'.\n",
6325 param->fp_check_comp_end = 1;
6327 param->fp_verbose |= VERBOSE_COMP_END;
6328 param->fp_max_depth = 0;
6332 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6333 param->fp_verbose |= VERBOSE_DFID;
6334 param->fp_max_depth = 0;
6338 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6339 param->fp_verbose |= VERBOSE_GENERATION;
6340 param->fp_max_depth = 0;
6344 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6345 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6346 param->fp_max_depth = 0;
6351 param->fp_comp_id = strtoul(optarg, &end, 0);
6352 if (*end != '\0' || param->fp_comp_id == 0 ||
6353 param->fp_comp_id > LCME_ID_MAX) {
6355 "error: %s bad component id '%s'\n",
6359 param->fp_check_comp_id = 1;
6361 param->fp_max_depth = 0;
6362 param->fp_verbose |= VERBOSE_COMP_ID;
6366 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6367 param->fp_verbose |= VERBOSE_PATTERN;
6368 param->fp_max_depth = 0;
6371 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6374 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6377 if (!(param->fp_verbose & VERBOSE_DETAIL))
6378 param->fp_max_depth = 0;
6379 param->fp_verbose |= VERBOSE_MDTINDEX;
6382 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6383 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6384 param->fp_max_depth = 0;
6388 if (param->fp_obd_uuid) {
6390 "error: %s: only one obduuid allowed",
6394 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6397 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6398 param->fp_verbose |= VERBOSE_POOL;
6399 param->fp_max_depth = 0;
6406 param->fp_recursive = 1;
6412 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6413 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6414 param->fp_max_depth = 0;
6418 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6424 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6425 param->fp_verbose |= VERBOSE_EXT_SIZE;
6426 param->fp_max_depth = 0;
6430 fprintf(stderr, "%s: unrecognized option '%s'\n",
6431 progname, argv[optind - 1]);
6437 if (pathstart == -1) {
6438 fprintf(stderr, "error: %s: no filename|pathname\n",
6441 } else if (pathend == -1) {
6449 if (param->fp_recursive)
6450 param->fp_max_depth = -1;
6451 else if (param->fp_verbose & VERBOSE_DETAIL)
6452 param->fp_max_depth = 1;
6454 if (!param->fp_verbose)
6455 param->fp_verbose = VERBOSE_DEFAULT;
6456 if (param->fp_quiet)
6457 param->fp_verbose = VERBOSE_OBJID;
6462 rc2 = llapi_getstripe(argv[pathstart], param);
6464 fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6465 progname, argv[0], argv[optind - 1],
6470 } while (++pathstart < pathend);
6475 static int lfs_tgts(int argc, char **argv)
6477 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6478 struct find_param param;
6479 int index = 0, rc = 0;
6484 if (argc == 2 && !realpath(argv[1], path)) {
6486 fprintf(stderr, "error: invalid path '%s': %s\n",
6487 argv[1], strerror(-rc));
6491 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6492 /* Check if we have a mount point */
6493 if (mntdir[0] == '\0')
6496 memset(¶m, 0, sizeof(param));
6497 if (!strcmp(argv[0], "mdts"))
6498 param.fp_get_lmv = 1;
6500 rc = llapi_ostlist(mntdir, ¶m);
6502 fprintf(stderr, "error: %s: failed on %s\n",
6505 if (path[0] != '\0')
6507 memset(mntdir, 0, PATH_MAX);
6513 static int lfs_getstripe(int argc, char **argv)
6515 struct find_param param = { 0 };
6517 param.fp_max_depth = 1;
6518 return lfs_getstripe_internal(argc, argv, ¶m);
6522 static int lfs_getdirstripe(int argc, char **argv)
6524 struct find_param param = { 0 };
6525 struct option long_opts[] = {
6526 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6527 { .val = 'D', .name = "default", .has_arg = no_argument },
6528 { .val = 'h', .name = "help", .has_arg = no_argument },
6529 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6530 { .val = LFS_HEX_IDX_OPT,
6531 .name = "hex-idx", .has_arg = no_argument },
6532 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6533 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6534 { .val = 'O', .name = "obd", .has_arg = required_argument },
6535 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6536 { .val = 'R', .name = "raw", .has_arg = no_argument },
6537 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6538 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6539 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6540 { .val = LFS_INHERIT_RR_OPT,
6541 .name = "max-inherit-rr", .has_arg = no_argument },
6542 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6546 param.fp_get_lmv = 1;
6548 while ((c = getopt_long(argc, argv,
6549 "cDhHimO:rRtTvXy", long_opts, NULL)) != -1) {
6553 param.fp_verbose |= VERBOSE_COUNT;
6556 param.fp_get_default_lmv = 1;
6558 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6561 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6565 param.fp_verbose |= VERBOSE_HASH_TYPE;
6567 case LFS_HEX_IDX_OPT:
6568 param.fp_hex_idx = 1;
6573 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6576 if (param.fp_obd_uuid) {
6578 "%s: only one obduuid allowed",
6582 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6585 param.fp_recursive = 1;
6591 param.fp_verbose |= VERBOSE_DEFAULT;
6592 param.fp_verbose |= VERBOSE_DETAIL;
6595 param.fp_verbose |= VERBOSE_INHERIT;
6597 case LFS_INHERIT_RR_OPT:
6598 param.fp_verbose |= VERBOSE_INHERIT_RR;
6604 fprintf(stderr, "%s: unrecognized option '%s'\n",
6605 progname, argv[optind - 1]);
6615 if (param.fp_recursive)
6616 param.fp_max_depth = -1;
6618 if (!param.fp_verbose)
6619 param.fp_verbose = VERBOSE_DEFAULT;
6624 rc2 = llapi_getstripe(argv[optind], ¶m);
6626 fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6627 progname, argv[0], argv[optind],
6632 } while (++optind < argc);
6638 MNTDF_INODES = 0x0001,
6639 MNTDF_COOKED = 0x0002,
6640 MNTDF_LAZY = 0x0004,
6641 MNTDF_VERBOSE = 0x0008,
6642 MNTDF_SHOW = 0x0010,
6643 MNTDF_DECIMAL = 0x0020,
6646 #define COOK(value, base) \
6649 while (value > base) { \
6657 #define CDF "%11llu"
6658 #define HDF "%8.1f%c"
6662 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6664 double avail, used, ratio = 0;
6667 avail = st->os_ffree;
6668 used = st->os_files - st->os_ffree;
6670 avail = st->os_bavail;
6671 used = st->os_blocks - st->os_bfree;
6673 if (avail + used > 0)
6674 ratio = used / (used + avail) * 100;
6676 /* Round up to match df(1) usage percentage */
6677 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6681 * This is to identify various problem states for "lfs df" if .osn_err = true,
6682 * so only show flags reflecting those states by default. Informational states
6683 * are only shown with "-v" and use lower-case names to distinguish them.
6684 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6686 static struct obd_statfs_state_names {
6687 enum obd_statfs_state osn_state;
6688 const char osn_name;
6691 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6692 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6693 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6694 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6695 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6696 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6697 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6698 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6699 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6702 static int showdf(char *mntdir, struct obd_statfs *stat,
6703 char *uuid, enum mntdf_flags flags,
6704 char *type, int index, int rc)
6706 long long avail, used, total;
6708 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6709 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6710 char tbuf[3 * sizeof(__u64)];
6711 char ubuf[3 * sizeof(__u64)];
6712 char abuf[3 * sizeof(__u64)];
6713 char rbuf[3 * sizeof(__u64)];
6720 if (flags & MNTDF_INODES) {
6721 avail = stat->os_ffree;
6722 used = stat->os_files - stat->os_ffree;
6723 total = stat->os_files;
6725 int shift = flags & MNTDF_COOKED ? 0 : 10;
6727 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6728 used = ((stat->os_blocks - stat->os_bfree) *
6729 stat->os_bsize) >> shift;
6730 total = (stat->os_blocks * stat->os_bsize) >> shift;
6733 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6735 if (flags & MNTDF_COOKED) {
6736 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6740 cook_val = (double)total;
6741 i = COOK(cook_val, base);
6743 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6746 snprintf(tbuf, sizeof(tbuf), CDF, total);
6748 cook_val = (double)used;
6749 i = COOK(cook_val, base);
6751 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6754 snprintf(ubuf, sizeof(ubuf), CDF, used);
6756 cook_val = (double)avail;
6757 i = COOK(cook_val, base);
6759 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6762 snprintf(abuf, sizeof(abuf), CDF, avail);
6764 snprintf(tbuf, sizeof(tbuf), CDF, total);
6765 snprintf(ubuf, sizeof(tbuf), CDF, used);
6766 snprintf(abuf, sizeof(tbuf), CDF, avail);
6769 sprintf(rbuf, RDF, ratio);
6770 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6771 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6773 printf("[%s:%d]", type, index);
6775 if (stat->os_state) {
6779 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6780 if (oss_names[i].osn_state & stat->os_state &&
6781 (oss_names[i].osn_err ||
6782 flags & MNTDF_VERBOSE))
6783 printf("%c", oss_names[i].osn_name);
6790 printf(UUF": inactive device\n", uuid);
6793 printf(UUF": %s\n", uuid, strerror(-rc));
6800 struct ll_stat_type {
6805 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6807 struct ll_statfs_data {
6809 struct obd_statfs sd_st;
6812 struct ll_statfs_buf {
6814 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6817 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6818 int ops, struct ll_statfs_buf *lsb)
6820 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6821 struct obd_uuid uuid_buf;
6822 char *poolname = NULL;
6823 struct ll_stat_type types[] = {
6824 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6825 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6826 { .st_name = NULL } };
6827 struct ll_stat_type *tp;
6828 __u64 ost_files = 0;
6829 __u64 ost_ffree = 0;
6837 poolname = strchr(pool, '.');
6839 if (strncmp(fsname, pool, strlen(fsname))) {
6840 fprintf(stderr, "filesystem name incorrect\n");
6848 fd = open(mntdir, O_RDONLY);
6851 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6856 if (flags & MNTDF_SHOW) {
6857 if (flags & MNTDF_INODES)
6858 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6859 "UUID", "Inodes", "IUsed", "IFree",
6860 "IUse%", "Mounted on");
6862 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6864 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6865 "Used", "Available", "Use%", "Mounted on");
6868 for (tp = types; tp->st_name != NULL; tp++) {
6869 bool have_ost = false;
6871 if (!(tp->st_op & ops))
6874 for (index = 0; index < LOV_ALL_STRIPES &&
6875 (!lsb || lsb->sb_count < LL_STATFS_MAX); index++) {
6876 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6877 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6878 type = flags & MNTDF_LAZY ?
6879 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6880 rc2 = llapi_obd_fstatfs(fd, type, index,
6881 &stat_buf, &uuid_buf);
6886 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6887 if (!(flags & MNTDF_VERBOSE))
6889 } else if (rc2 < 0 && rc == 0) {
6894 * If we have OSTs then don't report MDT block counts.
6895 * For MDT-only filesystems the expectation is that all
6896 * layouts have a DoM component. For filesystems with
6897 * OSTs, files are not necessarily going to store data
6898 * on MDTs, and MDT space is limited to a fraction of
6899 * OST space, so don't include it in the summary.
6901 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6908 if (poolname && tp->st_op == LL_STATFS_LOV &&
6909 llapi_search_ost(fsname, poolname,
6910 obd_uuid2str(&uuid_buf)) != 1)
6914 * the llapi_obd_fstatfs() call may have returned with
6915 * an error, but if it filled in uuid_buf we will at
6916 * lease use that to print out a message for that OBD.
6917 * If we didn't get anything in the uuid_buf, then fill
6918 * it in so that we can print an error message.
6920 if (uuid_buf.uuid[0] == '\0')
6921 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6922 "%s%04x", tp->st_name, index);
6924 lsb->sb_buf[lsb->sb_count].sd_index = index;
6925 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6928 if (flags & MNTDF_SHOW)
6929 showdf(mntdir, &stat_buf,
6930 obd_uuid2str(&uuid_buf), flags,
6931 tp->st_name, index, rc2);
6936 if (tp->st_op == LL_STATFS_LMV) {
6937 sum.os_ffree += stat_buf.os_ffree;
6938 sum.os_files += stat_buf.os_files;
6939 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6940 ost_files += stat_buf.os_files;
6941 ost_ffree += stat_buf.os_ffree;
6943 sum.os_blocks += stat_buf.os_blocks *
6945 sum.os_bfree += stat_buf.os_bfree *
6947 sum.os_bavail += stat_buf.os_bavail *
6955 * If we have _some_ OSTs, but don't have as many free objects on the
6956 * OST as inodes on the MDTs, reduce the reported number of inodes
6957 * to compensate, so that the "inodes in use" number is correct.
6958 * This should be kept in sync with ll_statfs_internal().
6960 if (ost_files && ost_ffree < sum.os_ffree) {
6961 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6962 sum.os_ffree = ost_ffree;
6964 if (flags & MNTDF_SHOW) {
6966 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6974 LAYOUT_INHERIT_UNSET = -2,
6978 static int lfs_setdirstripe(int argc, char **argv)
6981 struct lfs_setstripe_args lsa = { 0 };
6982 struct llapi_stripe_param *param = NULL;
6983 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6986 char *mode_opt = NULL;
6987 bool default_stripe = false;
6988 bool delete = false;
6989 bool foreign_mode = false;
6990 bool mdt_count_set = false;
6991 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6992 mode_t previous_mode = 0;
6994 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6995 int max_inherit = LAYOUT_INHERIT_UNSET;
6996 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6997 struct option long_opts[] = {
6998 { .val = 'c', .name = "count", .has_arg = required_argument },
6999 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
7000 { .val = 'd', .name = "delete", .has_arg = no_argument },
7001 { .val = 'D', .name = "default", .has_arg = no_argument },
7002 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
7003 { .val = LFS_LAYOUT_FLAGS_OPT,
7004 .name = "flags", .has_arg = required_argument },
7005 { .val = LFS_LAYOUT_FOREIGN_OPT,
7006 .name = "foreign", .has_arg = optional_argument},
7007 { .val = 'h', .name = "help", .has_arg = no_argument },
7008 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
7009 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
7010 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
7011 { .val = 'i', .name = "mdt", .has_arg = required_argument },
7013 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
7014 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7015 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7017 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7018 { .val = 'i', .name = "index", .has_arg = required_argument },
7020 { .val = 'o', .name = "mode", .has_arg = required_argument },
7021 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7022 { .val = 't', .name = "hash-type", .has_arg = required_argument },
7024 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
7025 { .val = 'x', .name = "xattr", .has_arg = required_argument },
7026 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
7027 { .val = LFS_INHERIT_RR_OPT,
7028 .name = "max-inherit-rr", .has_arg = required_argument},
7029 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
7030 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
7034 setstripe_args_init(&lsa);
7036 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
7037 long_opts, NULL)) >= 0) {
7045 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
7046 if (errno != 0 || *end != '\0' ||
7047 lsa.lsa_stripe_count < -1 ||
7048 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
7050 "%s: invalid stripe count '%s'\n",
7054 mdt_count_set = true;
7058 default_stripe = true;
7061 default_stripe = true;
7063 case LFS_LAYOUT_FOREIGN_OPT:
7065 /* check pure numeric */
7066 type = strtoul(optarg, &end, 0);
7069 type = check_foreign_type_name(optarg);
7070 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
7072 "%s %s: unknown foreign type '%s'\n",
7077 } else if (type >= UINT32_MAX) {
7079 "%s %s: invalid foreign type '%s'\n",
7080 progname, argv[0], optarg);
7084 foreign_mode = true;
7086 case LFS_LAYOUT_FLAGS_OPT:
7088 flags = strtoul(optarg, &end, 16);
7089 if (errno != 0 || *end != '\0' ||
7090 flags >= UINT32_MAX) {
7092 "%s %s: invalid hex flags '%s'\n",
7093 progname, argv[0], optarg);
7096 if (!foreign_mode) {
7098 "%s %s: hex flags must be specified with --foreign option\n",
7103 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7106 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
7110 lsa.lsa_pattern = check_hashtype(optarg);
7111 if (lsa.lsa_pattern == 0) {
7113 "%s %s: bad directory hash type '%s'\n",
7114 progname, argv[0], optarg);
7119 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
7122 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7123 if (strcmp(argv[optind - 1], "--index") == 0)
7125 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
7128 lsa.lsa_nr_tgts = parse_targets(mdts,
7129 sizeof(mdts) / sizeof(__u32),
7130 lsa.lsa_nr_tgts, optarg, NULL);
7131 if (lsa.lsa_nr_tgts < 0) {
7133 "%s %s: invalid MDT target(s) '%s'\n",
7134 progname, argv[0], optarg);
7138 lsa.lsa_tgts = mdts;
7139 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7140 lsa.lsa_stripe_off = mdts[0];
7150 max_inherit = strtol(optarg, &end, 10);
7151 if (errno != 0 || *end != '\0' || max_inherit < -2) {
7153 "%s %s: invalid max-inherit '%s'\n",
7154 progname, argv[0], optarg);
7157 if (max_inherit == 0) {
7158 max_inherit = LMV_INHERIT_NONE;
7159 } else if (max_inherit == -1) {
7160 max_inherit = LMV_INHERIT_UNLIMITED;
7161 } else if (max_inherit > LMV_INHERIT_MAX) {
7163 "%s %s: max-inherit %d exceeds maximum %u\n",
7164 progname, argv[0], max_inherit,
7169 case LFS_INHERIT_RR_OPT:
7170 if (!default_stripe) {
7172 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
7177 max_inherit_rr = strtol(optarg, &end, 10);
7178 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
7180 "%s %s: invalid max-inherit-rr '%s'\n",
7181 progname, argv[0], optarg);
7184 if (max_inherit_rr == 0) {
7185 max_inherit_rr = LMV_INHERIT_RR_NONE;
7186 } else if (max_inherit_rr == -1) {
7187 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
7188 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
7190 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
7191 progname, argv[0], max_inherit_rr,
7192 LMV_INHERIT_RR_MAX);
7197 fprintf(stderr, "%s: unrecognized option '%s'\n",
7198 progname, argv[optind - 1]);
7205 if (optind == argc) {
7206 fprintf(stderr, "%s %s: DIR must be specified\n",
7211 if (xattr && !foreign_mode) {
7213 * only print a warning as this is armless and will be
7217 "%s %s: xattr has been specified for non-foreign layout\n",
7219 } else if (foreign_mode && !xattr) {
7221 "%s %s: xattr must be provided in foreign mode\n",
7226 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
7227 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
7229 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
7234 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
7235 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
7236 /* if no parameters set, create directory on least-used MDTs */
7237 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
7238 lsa.lsa_stripe_count = 1;
7242 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
7243 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
7245 "%s %s: cannot specify -d with -c or -i options\n",
7251 mode = strtoul(mode_opt, &end, 8);
7254 "%s %s: bad MODE '%s'\n",
7255 progname, argv[0], mode_opt);
7258 previous_mode = umask(0);
7261 /* check max-inherit and warn user in some cases */
7262 if (default_stripe &&
7263 (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
7264 if (max_inherit == LMV_INHERIT_UNLIMITED)
7266 "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
7267 progname, argv[0], lsa.lsa_stripe_count);
7268 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
7269 max_inherit != LMV_INHERIT_NONE)
7271 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
7272 progname, argv[0], max_inherit,
7273 lsa.lsa_stripe_count);
7276 if (default_stripe && lsa.lsa_nr_tgts > 1 && !mdt_count_set) {
7278 "%s %s: trying to create unrecommended default striped directory layout,\n"
7279 " '-D -i x,y,z' will stripe every new directory across all MDTs,\n"
7280 " add -c with the number of MDTs to do this anyway\n",
7285 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
7286 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
7287 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
7289 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
7290 progname, argv[0], lsa.lsa_stripe_off);
7294 /* foreign LMV/dir case */
7296 if (argc > optind + 1) {
7298 "%s %s: cannot specify multiple foreign dirs\n",
7303 dname = argv[optind];
7304 result = llapi_dir_create_foreign(dname, mode, type, flags,
7308 "%s mkdir: can't create foreign dir '%s': %s\n",
7309 progname, dname, strerror(-result));
7314 * initialize stripe parameters, in case param is converted to specific,
7315 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
7317 param = calloc(1, offsetof(typeof(*param),
7318 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
7319 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
7322 "%s %s: cannot allocate memory for parameters: %s\n",
7323 progname, argv[0], strerror(ENOMEM));
7327 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
7328 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
7329 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
7330 lsa.lsa_stripe_count == 0))
7331 lsa.lsa_stripe_count = 1;
7332 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
7333 param->lsp_stripe_count = lsa.lsa_stripe_count;
7334 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7335 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
7337 param->lsp_stripe_offset = lsa.lsa_stripe_off;
7338 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
7339 param->lsp_stripe_pattern = lsa.lsa_pattern;
7341 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
7342 param->lsp_pool = lsa.lsa_pool_name;
7343 param->lsp_is_specific = false;
7345 if (max_inherit == LAYOUT_INHERIT_UNSET) {
7346 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1 ||
7347 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT)
7348 max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
7350 max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
7352 param->lsp_max_inherit = max_inherit;
7353 if (default_stripe) {
7355 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
7356 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
7357 param->lsp_max_inherit_rr = max_inherit_rr;
7359 if (strcmp(argv[0], "mkdir") == 0)
7360 param->lsp_is_create = true;
7361 if (lsa.lsa_nr_tgts > 1) {
7362 if (lsa.lsa_stripe_count > 0 &&
7363 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7364 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7366 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7367 argv[0], lsa.lsa_stripe_count,
7373 param->lsp_is_specific = true;
7374 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7375 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7378 dname = argv[optind];
7380 if (default_stripe) {
7381 result = llapi_dir_set_default_lmv(dname, param);
7384 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7385 progname, dname, strerror(-result));
7389 result = llapi_dir_create(dname, mode, param);
7392 "%s setdirstripe: cannot create dir '%s': %s\n",
7393 progname, dname, strerror(-result));
7394 } while (!result && (dname = argv[++optind]));
7397 umask(previous_mode);
7403 static int lfs_rmentry(int argc, char **argv)
7410 fprintf(stderr, "error: %s: missing dirname\n",
7416 dname = argv[index];
7420 rc2 = llapi_direntry_remove(dname);
7423 "%s %s: remove dir entry '%s' failed: %s\n",
7424 progname, argv[0], dname, strerror(-rc2));
7428 dname = argv[++index];
7433 static int lfs_unlink_foreign(int argc, char **argv)
7440 fprintf(stderr, "error: %s: missing pathname\n",
7447 while (name != NULL) {
7448 result = llapi_unlink_foreign(name);
7451 "error: %s: unlink foreign entry '%s' failed\n",
7455 name = argv[++index];
7460 static int lfs_mv(int argc, char **argv)
7462 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7463 struct find_param param = {
7470 struct option long_opts[] = {
7471 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7472 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7473 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7476 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7478 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7481 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7485 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7486 if (errno != 0 || *end != '\0' ||
7487 lmu.lum_stripe_offset >= UINT32_MAX) {
7488 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7494 param.fp_verbose = VERBOSE_DETAIL;
7497 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7498 progname, argv[optind - 1]);
7503 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7504 fprintf(stderr, "%s mv: MDT index must be specified\n",
7509 if (optind >= argc) {
7510 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7514 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7516 /* initialize migrate mdt parameters */
7517 param.fp_lmv_md = &lmu;
7518 param.fp_migrate = 1;
7519 rc = llapi_migrate_mdt(argv[optind], ¶m);
7521 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7522 progname, argv[optind], lmu.lum_stripe_offset,
7527 static int lfs_osts(int argc, char **argv)
7529 return lfs_tgts(argc, argv);
7532 static int lfs_mdts(int argc, char **argv)
7534 return lfs_tgts(argc, argv);
7537 static int lfs_df(int argc, char **argv)
7539 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7540 enum mntdf_flags flags = MNTDF_SHOW;
7541 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7542 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7543 char fsname[PATH_MAX] = "", *pool_name = NULL;
7544 struct option long_opts[] = {
7545 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7546 { .val = 'H', .name = "si", .has_arg = no_argument },
7547 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7548 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7549 { .val = 'p', .name = "pool", .has_arg = required_argument },
7550 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7553 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7556 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7559 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7562 flags |= MNTDF_INODES;
7565 flags |= MNTDF_LAZY;
7571 flags |= MNTDF_VERBOSE;
7574 fprintf(stderr, "%s: unrecognized option '%s'\n",
7575 progname, argv[optind - 1]);
7580 /* Handle case where path is not specified */
7581 if (optind == argc) {
7582 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7583 /* Check if we have a mount point */
7584 if (mntdir[0] == '\0')
7587 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7588 if (rc || path[0] != '\0')
7591 fsname[0] = '\0'; /* avoid matching in next loop */
7592 mntdir[0] = '\0'; /* avoid matching in next loop */
7593 path[0] = '\0'; /* clean for next loop */
7598 /* Loop through all the remaining arguments. These are Lustre FS
7601 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7604 fsname[0] = '\0'; /* start clean */
7605 mntdir[0] = '\0'; /* start clean */
7606 path[0] = '\0'; /* start clean */
7608 /* path does not exists at all */
7609 if (!realpath(argv[arg_idx], path)) {
7611 fprintf(stderr, "error: invalid path '%s': %s\n",
7612 argv[arg_idx], strerror(-rc));
7613 /* save first seen error */
7620 /* path exists but may not be a Lustre filesystem */
7621 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7622 /* Check if we have a mount point */
7623 if (mntdir[0] == '\0')
7626 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7627 if (rc || path[0] != '\0') {
7630 /* save first seen error */
7638 llapi_printf(LLAPI_MSG_ERROR,
7639 "%s:%s Not a Lustre filesystem\n",
7640 argv[0], argv[arg_idx]);
7641 /* save first seen error */
7650 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7651 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7655 if (opt_fsname == opt_instance) { /* both true or both false */
7656 rc = llapi_getname(mntdir, buf, buflen);
7657 } else if (opt_fsname) {
7659 * llapi_search_mounts() fills @buf with fsname, but that is not
7660 * called if explicit paths are specified on the command-line
7663 rc = llapi_get_fsname(mntdir, buf, buflen);
7664 } else /* if (opt_instance) */ {
7665 rc = llapi_get_instance(mntdir, buf, buflen);
7669 fprintf(stderr, "cannot get instance for '%s': %s\n",
7670 mntdir, strerror(-rc));
7675 printf("%s %s\n", buf, mntdir);
7677 printf("%s\n", buf);
7682 static int lfs_getname(int argc, char **argv)
7684 struct option long_opts[] = {
7685 { .val = 'h', .name = "help", .has_arg = no_argument },
7686 { .val = 'i', .name = "instance", .has_arg = no_argument },
7687 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7689 bool opt_instance = false, opt_fsname = false;
7690 char fsname[PATH_MAX] = "";
7693 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7696 opt_instance = true;
7702 fprintf(stderr, "%s: unrecognized option '%s'\n",
7703 progname, argv[optind - 1]);
7710 if (optind == argc) { /* no paths specified, get all paths. */
7711 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7714 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7715 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7716 opt_instance, opt_fsname, true);
7719 path[0] = fsname[0] = mntdir[0] = '\0';
7721 } else { /* paths specified, only attempt to search these. */
7724 /* if only one path is given, print only requested info */
7725 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7727 for (; optind < argc; optind++) {
7728 rc2 = print_instance(argv[optind], fsname,
7729 sizeof(fsname), opt_instance,
7730 opt_fsname, opt_mntdir);
7740 static int lfs_check(int argc, char **argv)
7742 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7750 if (argc < 2 || argc > 3) {
7751 fprintf(stderr, "%s check: server type must be specified\n",
7756 obd_types[0] = obd_type1;
7757 obd_types[1] = obd_type2;
7758 obd_types[2] = obd_type3;
7760 if (strcmp(argv[1], "osts") == 0) {
7761 strcpy(obd_types[0], "osc");
7762 } else if (strcmp(argv[1], "mdts") == 0 ||
7763 strcmp(argv[1], "mds") == 0) {
7764 strcpy(obd_types[0], "mdc");
7765 } else if (strcmp(argv[1], "mgts") == 0) {
7766 strcpy(obd_types[0], "mgc");
7767 } else if (strcmp(argv[1], "all") == 0 ||
7768 strcmp(argv[1], "servers") == 0) {
7770 strcpy(obd_types[0], "osc");
7771 strcpy(obd_types[1], "mdc");
7772 strcpy(obd_types[2], "mgc");
7774 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7779 if (argc >= 3 && !realpath(argv[2], path)) {
7781 fprintf(stderr, "error: invalid path '%s': %s\n",
7782 argv[2], strerror(-rc));
7786 rc = llapi_search_mounts(path, 0, mntdir, NULL);
7787 if (rc < 0 || mntdir[0] == '\0') {
7789 "%s %s: cannot find mounted Lustre filesystem: %s\n",
7791 (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7795 rc = llapi_target_check(num_types, obd_types, path);
7797 fprintf(stderr, "%s %s: cannot check target '%s': %s\n",
7798 progname, argv[0], argv[1], strerror(-rc));
7803 #ifdef HAVE_SYS_QUOTA_H
7804 #define ADD_OVERFLOW(a, b) \
7805 ((((a) + (b)) < (a)) ? \
7806 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7808 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7809 * returns the value or ULONG_MAX on integer overflow or incorrect format
7811 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7812 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7813 * 3. empty integer value is interpreted as 0
7815 static unsigned long str2sec(const char *timestr)
7817 const char spec[] = "smhdw";
7818 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7819 unsigned long val = 0;
7822 if (strpbrk(timestr, spec) == NULL) {
7824 * no specifiers inside the time string,
7825 * should treat it as an integer value
7827 val = strtoul(timestr, &tail, 10);
7828 return *tail ? ULONG_MAX : val;
7831 /* format string is XXwXXdXXhXXmXXs */
7837 v = strtoul(timestr, &tail, 10);
7838 if (v == ULONG_MAX || *tail == '\0')
7840 * value too large (ULONG_MAX or more)
7841 * or missing specifier
7845 ptr = strchr(spec, *tail);
7847 /* unknown specifier */
7852 /* check if product will overflow the type */
7853 if (!(v < ULONG_MAX / mult[ind]))
7856 ADD_OVERFLOW(val, mult[ind] * v);
7857 if (val == ULONG_MAX)
7869 #define ARG2ULL(nr, str, def_units) \
7871 unsigned long long limit, units = def_units; \
7874 rc = llapi_parse_size(str, &limit, &units, 1); \
7876 fprintf(stderr, "%s: invalid limit '%s'\n", \
7883 static inline int has_times_option(int argc, char **argv)
7887 for (i = 1; i < argc; i++)
7888 if (!strcmp(argv[i], "-t"))
7894 static inline int lfs_verify_poolarg(char *pool)
7896 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7898 "Pool name '%.*s' is longer than %d\n",
7899 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7905 /* special grace time, only notify the user when its quota is over soft limit
7906 * but doesn't block new writes until the hard limit is reached.
7908 #define NOTIFY_GRACE "notify"
7909 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7912 static inline __u64 lustre_stoqb(size_t space)
7914 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7917 #define lustre_stoqb toqb
7920 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7923 char *mnt, *obd_type = (char *)qctl->obd_type;
7924 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7925 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7926 struct option long_opts[] = {
7927 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7928 { .val = 'g', .name = "group", .has_arg = no_argument },
7929 { .val = 'h', .name = "help", .has_arg = no_argument },
7930 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7931 { .val = 'p', .name = "projid", .has_arg = no_argument },
7932 { .val = 't', .name = "times", .has_arg = no_argument },
7933 { .val = 'u', .name = "user", .has_arg = no_argument },
7934 { .val = LFS_POOL_OPT,
7935 .name = "pool", .has_arg = required_argument },
7939 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7940 qctl->qc_type = ALLQUOTA;
7942 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7943 long_opts, NULL)) != -1) {
7954 if (qctl->qc_type != ALLQUOTA) {
7956 "%s: -u/g/p cannot be used more than once\n",
7960 qctl->qc_type = qtype;
7963 if (strncmp(optarg, NOTIFY_GRACE,
7964 strlen(NOTIFY_GRACE)) == 0) {
7965 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7967 dqi->dqi_bgrace = str2sec(optarg);
7968 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7970 "%s: bad block-grace: %s\n",
7975 dqb->dqb_valid |= QIF_BTIME;
7978 if (strncmp(optarg, NOTIFY_GRACE,
7979 strlen(NOTIFY_GRACE)) == 0) {
7980 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7982 dqi->dqi_igrace = str2sec(optarg);
7983 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7985 "%s: bad inode-grace: %s\n",
7990 dqb->dqb_valid |= QIF_ITIME;
7992 case 't': /* Yes, of course! */
7995 if (lfs_verify_poolarg(optarg))
7997 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7998 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
8000 /* getopt prints error message for us when opterr != 0 */
8002 fprintf(stderr, "%s: unrecognized option '%s'\n",
8003 progname, argv[optind - 1]);
8010 if (qctl->qc_type == ALLQUOTA) {
8011 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
8016 if (optind != argc - 1) {
8017 fprintf(stderr, "%s: unexpected parameter '%s'\n",
8018 progname, argv[optind + 1]);
8023 rc = llapi_quotactl(mnt, qctl);
8026 fprintf(stderr, "%s %s ", obd_type,
8027 obd_uuid2str(&qctl->obd_uuid));
8028 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8035 static int lfs_reset_quota(char *mnt, struct if_quotactl *qctl)
8037 struct if_quotactl tmp_qctl;
8038 int index, md_count, dt_count;
8039 int wait_phase = 0, wait_index = 0, wait_count = 0;
8042 /* reset the quota ID, the existing quota setting will be returned */
8043 rc = llapi_quotactl(mnt, qctl);
8048 if ((qctl->qc_dqblk.dqb_valid & QIF_LIMITS) != QIF_LIMITS) {
8050 "the existing quota settings are not returned!\n");
8054 rc = llapi_get_obd_count(mnt, &md_count, 1);
8056 fprintf(stderr, "can not get mdt count: %s\n", strerror(-rc));
8060 rc = llapi_get_obd_count(mnt, &dt_count, 0);
8062 fprintf(stderr, "can not get ost count: %s\n", strerror(-rc));
8066 memset(&tmp_qctl, 0, sizeof(tmp_qctl));
8067 tmp_qctl.qc_type = qctl->qc_type;
8068 tmp_qctl.qc_id = qctl->qc_id;
8069 tmp_qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
8072 if (wait_phase == 0) {
8073 for (index = wait_index; index < md_count; index++) {
8074 tmp_qctl.qc_idx = index;
8075 tmp_qctl.qc_valid = QC_MDTIDX;
8076 rc = llapi_quotactl(mnt, &tmp_qctl);
8077 if (rc == -ENODEV || rc == -ENODATA)
8080 fprintf(stderr, "quotactl mdt%d failed: %s\n",
8081 index, strerror(-rc));
8084 /* check whether the md quota grant is reset */
8085 if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
8086 tmp_qctl.qc_dqblk.dqb_ihardlimit != 0)
8090 if (index < md_count) {
8096 for (index = wait_index; index < dt_count; index++) {
8097 tmp_qctl.qc_idx = index;
8098 tmp_qctl.qc_valid = QC_OSTIDX;
8099 rc = llapi_quotactl(mnt, &tmp_qctl);
8100 if (rc == -ENODEV || rc == -ENODATA)
8103 fprintf(stderr, "quotactl mdt%d failed: %s\n",
8104 index, strerror(-rc));
8107 /* check whether the dt quota grant is reset */
8108 if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
8109 tmp_qctl.qc_dqblk.dqb_bhardlimit != 0)
8113 if (index < dt_count) {
8120 if (wait_phase == 0) {
8128 if (rc || wait_count > 30) {
8129 fprintf(stderr, "fail to reset the quota ID %d on OBDs\n",
8136 fprintf(stdout, "wait %d seconds for OBDs to reset the quota ID %u\n",
8137 wait_count, qctl->qc_id);
8142 /* restore the quota setting */
8143 if (qctl->qc_dqblk.dqb_isoftlimit == 0 &&
8144 qctl->qc_dqblk.dqb_ihardlimit == 0 &&
8145 qctl->qc_dqblk.dqb_bsoftlimit == 0 &&
8146 qctl->qc_dqblk.dqb_bhardlimit == 0)
8149 memcpy(&tmp_qctl, qctl, sizeof(tmp_qctl));
8150 tmp_qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
8151 rc2 = llapi_quotactl(mnt, &tmp_qctl);
8156 "fail to restore the quota setting: %s, please restore it manually by\n lfs setquota %s %d",
8158 qctl->qc_type == USRQUOTA ? "-u" :
8159 (qctl->qc_type == GRPQUOTA ? "-g" : "-p"),
8162 if (qctl->qc_dqblk.dqb_isoftlimit != 0)
8163 fprintf(stderr, " -i %llu",
8164 (unsigned long long)qctl->qc_dqblk.dqb_isoftlimit);
8165 if (qctl->qc_dqblk.dqb_ihardlimit != 0)
8166 fprintf(stderr, " -I %llu",
8167 (unsigned long long)qctl->qc_dqblk.dqb_ihardlimit);
8168 if (qctl->qc_dqblk.dqb_bsoftlimit != 0)
8169 fprintf(stderr, " -b %llu",
8170 (unsigned long long)qctl->qc_dqblk.dqb_bsoftlimit);
8171 if (qctl->qc_dqblk.dqb_bhardlimit != 0)
8172 fprintf(stderr, " -B %llu",
8173 (unsigned long long)qctl->qc_dqblk.dqb_bhardlimit);
8175 fprintf(stderr, " %s\n", mnt);
8182 #define BSLIMIT (1 << 0)
8183 #define BHLIMIT (1 << 1)
8184 #define ISLIMIT (1 << 2)
8185 #define IHLIMIT (1 << 3)
8187 int lfs_setquota(int argc, char **argv)
8190 struct if_quotactl *qctl;
8191 char *mnt, *obd_type;
8192 struct obd_dqblk *dqb;
8193 struct option long_opts[] = {
8194 { .val = 'b', .name = "block-softlimit",
8195 .has_arg = required_argument },
8196 { .val = 'B', .name = "block-hardlimit",
8197 .has_arg = required_argument },
8198 { .val = 'd', .name = "default", .has_arg = no_argument },
8199 { .val = LFS_SETQUOTA_DELETE,
8200 .name = "delete", .has_arg = no_argument },
8201 { .val = 'g', .name = "group", .has_arg = required_argument },
8202 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
8203 { .val = 'h', .name = "help", .has_arg = no_argument },
8204 { .val = 'i', .name = "inode-softlimit",
8205 .has_arg = required_argument },
8206 { .val = 'I', .name = "inode-hardlimit",
8207 .has_arg = required_argument },
8208 { .val = 'p', .name = "projid", .has_arg = required_argument },
8209 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
8210 { .val = 'r', .name = "reset", .has_arg = no_argument },
8211 { .val = 'u', .name = "user", .has_arg = required_argument },
8212 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
8213 { .val = LFS_POOL_OPT,
8214 .name = "pool", .has_arg = required_argument },
8216 unsigned int limit_mask = 0;
8217 bool use_default = false;
8218 int qtype, qctl_len;
8220 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
8221 qctl = malloc(qctl_len);
8225 memset(qctl, 0, qctl_len);
8226 obd_type = (char *)qctl->obd_type;
8227 dqb = &qctl->qc_dqblk;
8229 if (has_times_option(argc, argv)) {
8230 rc = lfs_setquota_times(argc, argv, qctl);
8234 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
8235 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
8236 * so it can be used as a marker that qc_type
8237 * isn't reinitialized from command line
8239 while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pru:U",
8240 long_opts, NULL)) != -1) {
8243 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8246 goto quota_type_def;
8249 rc = name2uid(&qctl->qc_id, optarg);
8252 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8255 goto quota_type_def;
8258 rc = name2gid(&qctl->qc_id, optarg);
8261 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8264 goto quota_type_def;
8267 rc = name2projid(&qctl->qc_id, optarg);
8270 if (str2quotaid(&qctl->qc_id, optarg)) {
8272 "%s setquota: invalid id '%s'\n",
8279 if (qctl->qc_id == 0) {
8281 "%s setquota: can't set quota for root usr/group/project.\n",
8288 if (qctl->qc_type != ALLQUOTA) {
8290 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
8295 qctl->qc_type = qtype;
8297 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
8300 "%s setquota: '-d' deprecated, use '-D' or '--default'\n",
8306 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8308 case LFS_SETQUOTA_DELETE:
8309 qctl->qc_cmd = LUSTRE_Q_DELETEQID;
8312 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
8313 dqb->dqb_bsoftlimit >>= 10;
8314 limit_mask |= BSLIMIT;
8315 if (dqb->dqb_bsoftlimit &&
8316 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
8318 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8320 (unsigned long long)dqb->dqb_bsoftlimit,
8324 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
8325 dqb->dqb_bhardlimit >>= 10;
8326 limit_mask |= BHLIMIT;
8327 if (dqb->dqb_bhardlimit &&
8328 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
8330 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
8331 "See '%s help setquota' or Lustre manual for details\n",
8333 (unsigned long long)dqb->dqb_bhardlimit,
8337 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
8338 limit_mask |= ISLIMIT;
8339 if (dqb->dqb_isoftlimit &&
8340 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
8342 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8344 (unsigned long long)dqb->dqb_isoftlimit,
8348 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
8349 limit_mask |= IHLIMIT;
8350 if (dqb->dqb_ihardlimit &&
8351 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
8353 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8355 (unsigned long long)dqb->dqb_ihardlimit,
8359 if (lfs_verify_poolarg(optarg)) {
8363 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8364 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
8365 LUSTRE_Q_SETDEFAULT_POOL :
8366 LUSTRE_Q_SETQUOTAPOOL;
8369 qctl->qc_cmd = LUSTRE_Q_RESETQID;
8373 "%s setquota: unrecognized option '%s'\n",
8374 progname, argv[optind - 1]);
8382 if (qctl->qc_type == ALLQUOTA) {
8384 "%s setquota: either -u or -g must be specified\n",
8390 if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
8391 qctl->qc_cmd != LUSTRE_Q_RESETQID && limit_mask == 0) {
8393 "%s setquota: at least one limit must be specified\n",
8399 if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8400 qctl->qc_cmd == LUSTRE_Q_RESETQID) && limit_mask != 0) {
8402 "%s setquota: limits should not be specified when using default quota, deleting or resetting quota ID\n",
8408 if (use_default && qctl->qc_id == 0) {
8410 "%s setquota: can not set default quota for root user/group/project\n",
8416 if ((qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8417 qctl->qc_cmd == LUSTRE_Q_RESETQID) && qctl->qc_id == 0) {
8419 "%s setquota: can not delete or reset root user/group/project\n",
8425 if (optind != argc - 1) {
8427 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
8428 progname, argv[optind]);
8436 dqb->dqb_bhardlimit = 0;
8437 dqb->dqb_bsoftlimit = 0;
8438 dqb->dqb_ihardlimit = 0;
8439 dqb->dqb_isoftlimit = 0;
8442 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
8443 /* do not set inode limits for Pool Quotas */
8444 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
8445 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
8446 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
8447 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
8448 /* sigh, we can't just set blimits/ilimits */
8449 struct if_quotactl *tmp_qctl;
8451 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8455 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
8456 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8457 strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
8460 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8462 tmp_qctl->qc_type = qctl->qc_type;
8463 tmp_qctl->qc_id = qctl->qc_id;
8465 rc = llapi_quotactl(mnt, tmp_qctl);
8471 if (!(limit_mask & BHLIMIT))
8472 dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
8473 if (!(limit_mask & BSLIMIT))
8474 dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
8475 if (!(limit_mask & IHLIMIT))
8476 dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
8477 if (!(limit_mask & ISLIMIT))
8478 dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
8480 /* Keep grace times if we have got no softlimit arguments */
8481 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
8482 dqb->dqb_valid |= QIF_BTIME;
8483 dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
8486 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
8487 dqb->dqb_valid |= QIF_ITIME;
8488 dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
8493 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
8494 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
8496 if (qctl->qc_cmd == LUSTRE_Q_RESETQID)
8497 rc = lfs_reset_quota(mnt, qctl);
8499 rc = llapi_quotactl(mnt, qctl);
8504 "%s setquota: cannot quotactl '%s' '%s': %s\n",
8506 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
8509 "%s setquota: quotactl failed: %s\n",
8510 progname, strerror(-rc));
8514 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8520 /* Converts seconds value into format string
8521 * result is returned in buf
8523 * 1. result is in descenting order: 1w2d3h4m5s
8524 * 2. zero fields are not filled (except for p. 3): 5d1s
8525 * 3. zero seconds value is presented as "0s"
8527 static char *__sec2str(time_t seconds, char *buf)
8529 const char spec[] = "smhdw";
8530 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8535 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8536 c = seconds / mult[i];
8538 if (c > 0 || (i == 0 && buf == tail))
8539 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8548 static void sec2str(time_t seconds, char *buf, int rc)
8555 tail = __sec2str(seconds, tail);
8557 if (rc && tail - buf < 39) {
8563 static void diff2str(time_t seconds, char *buf, time_t now)
8568 if (seconds <= now) {
8569 strcpy(buf, "expired");
8572 __sec2str(seconds - now, buf);
8575 static void print_quota_title(char *name, struct if_quotactl *qctl,
8576 bool human_readable, bool show_default)
8579 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8580 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8581 "Filesystem", "bquota", "blimit", "bgrace",
8582 "iquota", "ilimit", "igrace");
8584 printf("Disk quotas for %s %s (%cid %u):\n",
8585 qtype_name(qctl->qc_type), name,
8586 *qtype_name(qctl->qc_type), qctl->qc_id);
8587 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8588 "Filesystem", human_readable ? "used" : "kbytes",
8589 "quota", "limit", "grace",
8590 "files", "quota", "limit", "grace");
8594 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8597 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8600 snprintf(buf, buflen, "%5.4gP",
8601 (double)num / ((__u64)1 << 40));
8603 snprintf(buf, buflen, "%5.4gT",
8604 (double)num / (1 << 30));
8606 snprintf(buf, buflen, "%5.4gG",
8607 (double)num / (1 << 20));
8609 snprintf(buf, buflen, "%5.4gM",
8610 (double)num / (1 << 10));
8612 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8616 #ifdef HAVE_NATIVE_CLIENT
8617 /* In the current Lustre implementation, the grace time is either the time
8618 * or the timestamp to be used after some quota ID exceeds the soft limt,
8619 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8621 #define LQUOTA_GRACE_BITS 48
8622 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8623 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8624 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8625 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8626 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8629 #define STRBUF_LEN 24
8630 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8631 int rc, bool h, bool show_default)
8637 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8638 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8639 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8640 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8641 int bover = 0, iover = 0;
8642 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8643 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8645 char strbuf[STRBUF_LEN];
8647 dqb->dqb_btime &= LQUOTA_GRACE_MASK;
8648 dqb->dqb_itime &= LQUOTA_GRACE_MASK;
8650 if (dqb->dqb_bhardlimit &&
8651 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8653 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8654 if (dqb->dqb_btime > now)
8660 if (dqb->dqb_ihardlimit &&
8661 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8663 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8664 if (dqb->dqb_itime > now)
8670 if (strlen(mnt) > 15)
8671 printf("%s\n%15s", mnt, "");
8673 printf("%15s", mnt);
8676 snprintf(timebuf, sizeof(timebuf), "%llu",
8677 (unsigned long long)dqb->dqb_btime);
8679 diff2str(dqb->dqb_btime, timebuf, now);
8681 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8682 strbuf, sizeof(strbuf), h);
8683 if (rc == -EREMOTEIO)
8684 sprintf(numbuf[0], "%s*", strbuf);
8686 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8687 "%s" : "[%s]", strbuf);
8689 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8690 if (type == QC_GENERAL)
8691 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8692 "%s" : "[%s]", strbuf);
8694 sprintf(numbuf[1], "%s", "-");
8696 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8697 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8698 "%s" : "[%s]", strbuf);
8701 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8703 printf(" %7s%c %6s %7s %7s",
8704 numbuf[0], bover ? '*' : ' ', numbuf[1],
8705 numbuf[2], bover > 1 ? timebuf : "-");
8708 snprintf(timebuf, sizeof(timebuf), "%llu",
8709 (unsigned long long)dqb->dqb_itime);
8711 diff2str(dqb->dqb_itime, timebuf, now);
8713 snprintf(numbuf[0], sizeof(numbuf),
8714 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8715 (uintmax_t)dqb->dqb_curinodes);
8717 if (type == QC_GENERAL)
8718 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8720 (uintmax_t)dqb->dqb_isoftlimit);
8722 sprintf(numbuf[1], "%s", "-");
8724 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8725 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8728 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8729 else if (type != QC_OSTIDX)
8730 printf(" %7s%c %6s %7s %7s",
8731 numbuf[0], iover ? '*' : ' ', numbuf[1],
8732 numbuf[2], iover > 1 ? timebuf : "-");
8734 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8736 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
8737 qctl->qc_cmd == LUSTRE_Q_GETINFOPOOL ||
8738 qctl->qc_cmd == Q_GETOINFO) {
8742 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8743 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8745 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8746 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8747 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8749 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8751 printf("Block grace time: %s; Inode grace time: %s\n",
8752 bgtimebuf, igtimebuf);
8756 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8760 /* format is "lustre-OST0001" */
8761 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8763 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8768 *idx = strtoul(dash, &endp, 16);
8769 if (*idx > 0xffff) {
8770 fprintf(stderr, "wrong index %s\n", tgtname);
8777 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8778 bool h, __u64 *total)
8780 int rc = 0, rc1 = 0, count = 0, i = 0;
8781 char **list = NULL, *buffer = NULL;
8782 __u32 valid = qctl->qc_valid;
8784 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8787 /* Is it correct for the case OST0000, OST0002, OST0003 -
8788 * we will ask OST0001 that is absent and won't ask OST0003? */
8789 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8791 fprintf(stderr, "can not get %s count: %s\n",
8792 is_mdt ? "mdt" : "ost", strerror(-rc));
8796 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8797 char fname[PATH_MAX];
8798 char fsname[LUSTRE_MAXFSNAME + 1];
8799 int bufsize = sizeof(struct obd_uuid) * count;
8801 rc = llapi_search_fsname(mnt, fsname);
8803 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8807 buffer = malloc(bufsize + sizeof(*list) * count);
8810 list = (char **)(buffer + bufsize);
8811 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8812 count = llapi_get_poolmembers(fname, list, count,
8818 for (i = 0; i < count; i++) {
8819 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8822 if (tgt_name2index(list[i], &index))
8824 qctl->qc_idx = index;
8829 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8830 rc = llapi_quotactl(mnt, qctl);
8832 /* It is remote client case. */
8833 if (rc == -EOPNOTSUPP) {
8838 /* no target for this index yet */
8839 if (rc == -ENODEV) {
8844 /* inactive target */
8845 if (rc == -ENODATA) {
8846 char name[UUID_MAX+8];
8848 snprintf(name, sizeof(name), "%s[inact]",
8849 obd_uuid2str(&qctl->obd_uuid));
8850 memset(&qctl->qc_dqinfo, 0,
8851 sizeof(qctl->qc_dqinfo));
8852 memset(&qctl->qc_dqblk, 0,
8853 sizeof(qctl->qc_dqblk));
8854 print_quota(name, qctl, qctl->qc_valid, 0, h,
8862 fprintf(stderr, "quotactl %s%d failed.\n",
8863 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8867 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8868 qctl->qc_valid, 0, h, false);
8869 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8870 qctl->qc_dqblk.dqb_bhardlimit;
8875 qctl->qc_valid = valid;
8879 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8880 int verbose, int quiet, bool human_readable,
8883 int rc1 = 0, rc2 = 0, rc3 = 0;
8884 char *obd_type = (char *)qctl->obd_type;
8885 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8886 __u64 total_ialloc = 0, total_balloc = 0;
8887 bool use_default_for_blk = false;
8888 bool use_default_for_file = false;
8891 rc1 = llapi_quotactl(mnt, qctl);
8895 fprintf(stderr, "%s quotas are not enabled.\n",
8896 qtype_name(qctl->qc_type));
8899 fprintf(stderr, "Permission denied.\n");
8902 /* We already got error message. */
8905 fprintf(stderr, "Unexpected quotactl error: %s\n",
8910 if (!show_default && qctl->qc_id == 0) {
8911 qctl->qc_dqblk.dqb_bhardlimit = 0;
8912 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8913 qctl->qc_dqblk.dqb_ihardlimit = 0;
8914 qctl->qc_dqblk.dqb_isoftlimit = 0;
8915 qctl->qc_dqblk.dqb_btime = 0;
8916 qctl->qc_dqblk.dqb_itime = 0;
8917 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8920 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8921 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8922 use_default_for_blk = true;
8923 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8926 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8927 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8928 use_default_for_file = true;
8929 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8932 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8933 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8934 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8935 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8936 print_quota_title(name, qctl, human_readable, show_default);
8938 if (rc1 && *obd_type)
8939 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8941 if (qctl->qc_valid != QC_GENERAL)
8944 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8945 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8946 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8947 (QIF_LIMITS|QIF_USAGE));
8949 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8951 if (!show_default && verbose &&
8952 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8953 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8954 char strbuf[STRBUF_LEN];
8956 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8958 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8960 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8962 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8963 (uintmax_t)total_ialloc, strbuf);
8966 if (use_default_for_blk)
8967 printf("%cid %u is using default block quota setting\n",
8968 *qtype_name(qctl->qc_type), qctl->qc_id);
8970 if (use_default_for_file)
8971 printf("%cid %u is using default file quota setting\n",
8972 *qtype_name(qctl->qc_type), qctl->qc_id);
8974 if (rc1 || rc2 || rc3 || inacc)
8975 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8989 static int lfs_project(int argc, char **argv)
8991 int ret = 0, err = 0, c, i;
8992 struct project_handle_control phc = { 0 };
8993 enum lfs_project_ops_t op;
8996 phc.assign_projid = false;
8997 /* default action */
8998 op = LFS_PROJECT_LIST;
9000 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
9003 if (op != LFS_PROJECT_LIST) {
9005 "%s: cannot specify '-c' '-C' '-s' together\n",
9010 op = LFS_PROJECT_CHECK;
9013 if (op != LFS_PROJECT_LIST) {
9015 "%s: cannot specify '-c' '-C' '-s' together\n",
9020 op = LFS_PROJECT_CLEAR;
9023 if (op != LFS_PROJECT_LIST) {
9025 "%s: cannot specify '-c' '-C' '-s' together\n",
9030 phc.set_inherit = true;
9031 op = LFS_PROJECT_SET;
9037 phc.keep_projid = true;
9040 phc.recursive = true;
9043 if (str2quotaid(&phc.projid, optarg)) {
9045 "Invalid project ID: %s\n",
9050 phc.assign_projid = true;
9054 phc.newline = false;
9057 fprintf(stderr, "%s: invalid option '%c'\n",
9063 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
9064 op = LFS_PROJECT_SET;
9065 phc.set_projid = true;
9066 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
9067 phc.set_projid = true;
9071 case LFS_PROJECT_CHECK:
9072 if (phc.keep_projid) {
9074 "%s: '-k' is useless together with '-c'\n",
9079 case LFS_PROJECT_CLEAR:
9082 "%s: '-0' is useless together with '-C'\n",
9086 if (phc.assign_projid) {
9088 "%s: '-p' is useless together with '-C'\n",
9093 case LFS_PROJECT_SET:
9096 "%s: '-0' is useless together with '-s'\n",
9100 if (phc.keep_projid) {
9102 "%s: '-k' is useless together with '-s'\n",
9110 "%s: '-0' is useless for list operations\n",
9120 fprintf(stderr, "%s: missing file or directory target(s)\n",
9125 for (i = 0; i < argc; i++) {
9127 case LFS_PROJECT_CHECK:
9128 err = lfs_project_check(argv[i], &phc);
9130 case LFS_PROJECT_LIST:
9131 err = lfs_project_list(argv[i], &phc);
9133 case LFS_PROJECT_CLEAR:
9134 err = lfs_project_clear(argv[i], &phc);
9136 case LFS_PROJECT_SET:
9137 err = lfs_project_set(argv[i], &phc);
9149 static int lfs_quota(int argc, char **argv)
9152 char *mnt, *name = NULL;
9153 struct if_quotactl *qctl;
9155 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
9156 __u32 valid = QC_GENERAL, idx = 0;
9157 bool human_readable = false;
9158 bool show_default = false;
9160 bool show_pools = false;
9161 struct option long_opts[] = {
9162 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
9164 char **poollist = NULL;
9168 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
9172 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
9173 qctl->qc_type = ALLQUOTA;
9174 obd_uuid = (char *)qctl->obd_uuid.uuid;
9176 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
9177 long_opts, NULL)) != -1) {
9180 show_default = true;
9185 show_default = true;
9190 show_default = true;
9194 if (qctl->qc_type != ALLQUOTA) {
9196 "%s quota: only one of -u, -g, or -p may be specified\n",
9201 qctl->qc_type = qtype;
9204 qctl->qc_cmd = LUSTRE_Q_GETINFO;
9207 valid = qctl->qc_valid = QC_UUID;
9208 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
9211 valid = qctl->qc_valid = QC_MDTIDX;
9212 idx = qctl->qc_idx = atoi(optarg);
9213 if (idx == 0 && *optarg != '0') {
9215 "%s quota: invalid MDT index '%s'\n",
9222 valid = qctl->qc_valid = QC_OSTIDX;
9223 idx = qctl->qc_idx = atoi(optarg);
9224 if (idx == 0 && *optarg != '0') {
9226 "%s quota: invalid OST index '%s'\n",
9239 human_readable = true;
9242 if ((!optarg) && (argv[optind] != NULL) &&
9243 (argv[optind][0] != '-') &&
9244 (argv[optind][0] != '/')) {
9245 optarg = argv[optind++];
9246 if (lfs_verify_poolarg(optarg)) {
9250 strncpy(qctl->qc_poolname, optarg,
9252 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
9253 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
9255 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9259 /* optarg is NULL */
9261 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9264 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
9265 progname, argv[optind - 1]);
9271 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
9272 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9273 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
9274 qctl->qc_type == ALLQUOTA &&
9275 optind == argc - 1 && !show_default) {
9278 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
9279 qctl->qc_type = qtype;
9280 qctl->qc_valid = valid;
9281 if (qtype == USRQUOTA) {
9282 qctl->qc_id = geteuid();
9283 rc = uid2name(&name, qctl->qc_id);
9285 qctl->qc_id = getegid();
9286 rc = gid2name(&name, qctl->qc_id);
9287 memset(&qctl->qc_dqblk, 0,
9288 sizeof(qctl->qc_dqblk));
9293 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
9294 human_readable, show_default);
9299 /* lfs quota -u username /path/to/lustre/mount */
9300 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9301 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
9302 /* options should be followed by u/g-name and mntpoint */
9303 if ((!show_default && optind + 2 != argc) ||
9304 (show_default && optind + 1 != argc) ||
9305 qctl->qc_type == ALLQUOTA) {
9307 "%s quota: name and mount point must be specified\n",
9313 if (!show_default) {
9314 name = argv[optind++];
9315 switch (qctl->qc_type) {
9317 rc = name2uid(&qctl->qc_id, name);
9320 rc = name2gid(&qctl->qc_id, name);
9323 rc = name2projid(&qctl->qc_id, name);
9330 qctl->qc_valid = QC_GENERAL;
9331 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
9332 LUSTRE_Q_GETDEFAULT_POOL :
9333 LUSTRE_Q_GETDEFAULT;
9338 if (str2quotaid(&qctl->qc_id, name)) {
9339 fprintf(stderr, "%s quota: invalid id '%s'\n",
9345 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
9346 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
9357 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
9361 for (i = 0; i < poolcount; i++) {
9362 p = memchr(poollist[i], '.', MAXNAMLEN);
9364 fprintf(stderr, "bad string format %.*s\n",
9365 MAXNAMLEN, poollist[i]);
9370 printf("Quotas for pool: %s\n", p);
9371 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
9372 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9373 human_readable, show_default);
9380 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9381 human_readable, show_default);
9387 #endif /* HAVE_SYS_QUOTA_H! */
9389 static int flushctx_ioctl(char *mp)
9393 fd = open(mp, O_RDONLY);
9395 fprintf(stderr, "flushctx: error open %s: %s\n",
9396 mp, strerror(errno));
9400 rc = ioctl(fd, LL_IOC_FLUSHCTX);
9402 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
9403 mp, strerror(errno));
9409 static int lfs_flushctx(int argc, char **argv)
9411 int kdestroy = 0, reap = 0, c;
9412 char mntdir[PATH_MAX] = {'\0'};
9416 while ((c = getopt(argc, argv, "kr")) != -1) {
9426 "error: %s: option '-%c' unrecognized\n",
9433 rc = system("kdestroy > /dev/null");
9435 rc = WEXITSTATUS(rc);
9437 "error destroying tickets: %d, continuing\n",
9442 if (optind >= argc) {
9443 /* flush for all mounted lustre fs. */
9444 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
9445 /* Check if we have a mount point */
9446 if (mntdir[0] == '\0')
9449 if (flushctx_ioctl(mntdir))
9452 mntdir[0] = '\0'; /* avoid matching in next loop */
9455 /* flush fs as specified */
9456 while (optind < argc) {
9457 if (flushctx_ioctl(argv[optind++]))
9463 rc = system("keyctl reap > /dev/null");
9465 rc = WEXITSTATUS(rc);
9466 fprintf(stderr, "error reaping keyring: %d\n", rc);
9473 static int lfs_changelog(int argc, char **argv)
9475 void *changelog_priv;
9476 struct changelog_rec *rec;
9477 long long startrec = 0, endrec = 0;
9479 struct option long_opts[] = {
9480 { .val = 'f', .name = "follow", .has_arg = no_argument },
9482 char short_opts[] = "f";
9485 while ((rc = getopt_long(argc, argv, short_opts,
9486 long_opts, NULL)) != -1) {
9493 "%s changelog: unrecognized option '%s'\n",
9494 progname, argv[optind - 1]);
9498 if (optind >= argc) {
9499 fprintf(stderr, "%s changelog: mdtname must be specified\n",
9504 mdd = argv[optind++];
9505 if (argc > optind) {
9507 startrec = strtoll(argv[optind++], NULL, 10);
9508 if (errno != 0 || startrec < 0) {
9510 "%s changelog: bad startrec\n",
9516 if (argc > optind) {
9518 endrec = strtoll(argv[optind++], NULL, 10);
9519 if (errno != 0 || endrec < 0) {
9521 "%s changelog: bad endrec\n",
9527 rc = llapi_changelog_start(&changelog_priv,
9528 CHANGELOG_FLAG_BLOCK |
9529 CHANGELOG_FLAG_JOBID |
9530 CHANGELOG_FLAG_EXTRA_FLAGS |
9531 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
9534 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
9535 progname, strerror(errno = -rc));
9539 rc = llapi_changelog_set_xflags(changelog_priv,
9540 CHANGELOG_EXTRA_FLAG_UIDGID |
9541 CHANGELOG_EXTRA_FLAG_NID |
9542 CHANGELOG_EXTRA_FLAG_OMODE |
9543 CHANGELOG_EXTRA_FLAG_XATTR);
9546 "%s changelog: cannot set xflags for changelog: %s\n",
9547 progname, strerror(errno = -rc));
9551 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9555 if (endrec && rec->cr_index > endrec) {
9556 llapi_changelog_free(&rec);
9559 if (rec->cr_index < startrec) {
9560 llapi_changelog_free(&rec);
9564 secs = rec->cr_time >> 30;
9565 gmtime_r(&secs, &ts);
9566 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9567 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9568 changelog_type2str(rec->cr_type),
9569 ts.tm_hour, ts.tm_min, ts.tm_sec,
9570 (int)(rec->cr_time & ((1 << 30) - 1)),
9571 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9572 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9574 if (rec->cr_flags & CLF_JOBID) {
9575 struct changelog_ext_jobid *jid =
9576 changelog_rec_jobid(rec);
9578 if (jid->cr_jobid[0] != '\0')
9579 printf(" j=%s", jid->cr_jobid);
9582 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
9583 struct changelog_ext_extra_flags *ef =
9584 changelog_rec_extra_flags(rec);
9586 printf(" ef=0x%llx",
9587 (unsigned long long)ef->cr_extra_flags);
9589 if (ef->cr_extra_flags & CLFE_UIDGID) {
9590 struct changelog_ext_uidgid *uidgid =
9591 changelog_rec_uidgid(rec);
9593 printf(" u=%llu:%llu",
9594 (unsigned long long)uidgid->cr_uid,
9595 (unsigned long long)uidgid->cr_gid);
9597 if (ef->cr_extra_flags & CLFE_NID) {
9598 struct changelog_ext_nid *nid =
9599 changelog_rec_nid(rec);
9602 libcfs_nid2str(nid->cr_nid));
9605 if (ef->cr_extra_flags & CLFE_OPEN) {
9606 struct changelog_ext_openmode *omd =
9607 changelog_rec_openmode(rec);
9608 char mode[] = "---";
9610 /* exec mode must be exclusive */
9611 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9614 if (omd->cr_openflags & MDS_FMODE_READ)
9616 if (omd->cr_openflags &
9623 if (strcmp(mode, "---") != 0)
9624 printf(" m=%s", mode);
9627 if (ef->cr_extra_flags & CLFE_XATTR) {
9628 struct changelog_ext_xattr *xattr =
9629 changelog_rec_xattr(rec);
9631 if (xattr->cr_xattr[0] != '\0')
9632 printf(" x=%s", xattr->cr_xattr);
9636 if (!fid_is_zero(&rec->cr_pfid))
9637 printf(" p="DFID, PFID(&rec->cr_pfid));
9638 if (rec->cr_namelen)
9639 printf(" %.*s", rec->cr_namelen,
9640 changelog_rec_name(rec));
9642 if (rec->cr_flags & CLF_RENAME) {
9643 struct changelog_ext_rename *rnm =
9644 changelog_rec_rename(rec);
9646 if (!fid_is_zero(&rnm->cr_sfid))
9647 printf(" s="DFID" sp="DFID" %.*s",
9648 PFID(&rnm->cr_sfid),
9649 PFID(&rnm->cr_spfid),
9650 (int)changelog_rec_snamelen(rec),
9651 changelog_rec_sname(rec));
9655 llapi_changelog_free(&rec);
9658 llapi_changelog_fini(&changelog_priv);
9661 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9662 progname, strerror(errno = -rc));
9664 return (rc == 1 ? 0 : rc);
9667 static int lfs_changelog_clear(int argc, char **argv)
9676 endrec = strtoll(argv[3], NULL, 10);
9677 if (errno != 0 || endrec < 0) {
9679 "%s: bad endrec '%s'\n",
9684 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9687 fprintf(stderr, "%s: record out of range: %llu\n",
9689 else if (rc == -ENOENT)
9690 fprintf(stderr, "%s: no changelog user: %s\n",
9693 fprintf(stderr, "%s error: %s\n", argv[0],
9702 static void rstripc(char *str, int c)
9704 char *end = str + strlen(str);
9706 for (; str < end && end[-1] == c; --end)
9710 /* Helper function to lfs_fid2path. To print out only the file names and
9711 * not the full path. Do not call OBD_IOC_FID2PATH for every file. Instead
9712 * read the trusted.link xattr and loop over all the records to get all the
9715 static int lfs_fid2path_prn_name(char *mnt_dir, char *path_buf,
9716 bool print_linkno, bool print_fid, char *ptr,
9717 const char *fid_str, int linktmp)
9719 char buf[65536]; /* BUFFER_SIZE 65536 */
9720 char full_path[PATH_MAX * 2 + 2];
9721 struct link_ea_header *leh;
9722 struct link_ea_entry *lee;
9724 int reclen, i, rc = 0;
9726 /* Generate full_path */
9727 snprintf(full_path, sizeof(full_path) - 1, "%s/%s", mnt_dir, path_buf);
9729 size = getxattr(full_path, "trusted.link", buf, sizeof(buf));
9731 fprintf(stderr, "%s: failed to read %s xattr: %s\n", path_buf,
9732 "trusted.link", strerror(errno));
9737 leh = (struct link_ea_header *)buf;
9739 if (leh->leh_magic == __swab32(LINK_EA_MAGIC))
9740 leh->leh_reccount = __swab32(leh->leh_reccount);
9742 lee = (struct link_ea_entry *)(leh + 1);
9744 for (i = 0; i < leh->leh_reccount; i++) {
9745 reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
9747 /* handle -n -l case */
9749 ptr = strrchr(path_buf, '/');
9755 if (strcmp(ptr, lee->lee_name) == 0) {
9757 printf("%s ", fid_str);
9759 printf("%d ", linktmp);
9760 printf("%s\n", lee->lee_name);
9765 printf("%s ", fid_str);
9766 printf("%s\n", lee->lee_name);
9769 /* Get next record */
9770 lee = (struct link_ea_entry *)((char *)lee + reclen);
9776 static int lfs_fid2path(int argc, char **argv)
9778 struct option long_opts[] = {
9779 { .val = '0', .name = "print0", .has_arg = no_argument },
9780 { .val = 'c', .name = "cur", .has_arg = no_argument },
9781 { .val = 'c', .name = "current", .has_arg = no_argument },
9782 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9783 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9784 { .val = 'l', .name = "link", .has_arg = required_argument },
9785 { .val = 'n', .name = "name", .has_arg = no_argument },
9787 char short_opts[] = "0cfl:pr:n";
9788 bool print_only_fname = false;
9789 bool print_linkno = false;
9790 bool print_link = false;
9791 bool print_fid = false;
9793 char mnt_dir[PATH_MAX] = "";
9795 char *path_or_fsname;
9796 long long recno = -1;
9798 char *endptr = NULL;
9799 char link_separator = '\n';
9804 while ((c = getopt_long(argc, argv, short_opts,long_opts, NULL)) !=
9808 link_separator = '\0';
9818 linkno = strtol(optarg, &endptr, 10);
9819 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9821 "%s fid2path: invalid linkno '%s'\n",
9825 print_linkno = true;
9828 /* Bypass the full parent path if true
9829 * only print the final filename */
9830 print_only_fname = true;
9833 /* recno is something to do with changelogs
9834 * that was never implemented. We just pass it
9835 * through for the MDT to ignore.
9838 recno = strtoll(optarg, &endptr, 10);
9839 if (errno != 0 || *endptr != '\0' || recno < 0) {
9841 "%s fid2path: invalid recno '%s'\n",
9848 "%s fid2path: unrecognized option '%s'\n",
9849 progname, argv[optind - 1]);
9854 if (argc - optind < 2) {
9856 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9861 path_or_fsname = argv[optind];
9863 if (*path_or_fsname == '/') {
9864 print_mnt_dir = true;
9865 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9867 print_mnt_dir = false;
9868 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9873 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9874 progname, path_or_fsname, strerror(-rc));
9878 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9881 "%s fid2path: cannot open mount point for '%s': %s\n",
9882 progname, path_or_fsname, strerror(-rc));
9886 /* Strip trailing slashes from mnt_dir. */
9887 rstripc(mnt_dir + 1, '/');
9889 for (i = optind + 1; i < argc; i++) {
9890 const char *fid_str = argv[i];
9895 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9898 "%s fid2path: invalid FID '%s'\n",
9906 int linktmp = (linkno >= 0) ? linkno : 0;
9909 int oldtmp = linktmp;
9910 long long rectmp = recno;
9911 char path_buf[PATH_MAX];
9913 rc2 = llapi_fid2path_at(mnt_fd, &fid, path_buf,
9914 sizeof(path_buf), &rectmp,
9918 "%s fid2path: cannot find %s %s: %s\n",
9919 progname, path_or_fsname, fid_str,
9926 if (print_only_fname && !print_link) {
9927 /* '-n' is passed as option here.
9928 * For all other cases of -c fall back
9929 * to default(else) path as to get the link
9930 * count associated with the file name call
9931 * to OBD_IOC_FID2PATH is required
9933 rc = lfs_fid2path_prn_name(mnt_dir,
9938 /* llapi_fid2path_at() is already called once
9939 * in this case. No need to call it again.
9940 * Break out as we have all the filenames.
9946 printf("%s ", fid_str);
9949 printf("%d ", linktmp);
9951 /* You may think this looks wrong or weird (and it is!)
9952 * but we are actually trying to preserve the old quirky
9953 * behaviors (enforced by our old quirky tests!) that
9954 * make lfs so much fun to work on:
9956 * lustre 0x200000007:0x1:0x0 => "/"
9957 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9959 * Note that llapi_fid2path() returns "" for the root
9961 if (!print_only_fname) {
9963 print_mnt_dir ? mnt_dir : "",
9964 (print_mnt_dir || *path_buf == '\0') ?
9965 "/" : "", path_buf, link_separator);
9967 ptr = strrchr(path_buf, '/');
9969 printf("%s\n", path_buf);
9971 printf("%s\n", ptr + 1);
9975 /* specified linkno */
9978 if (oldtmp == linktmp)
9990 static int lfs_path2fid(int argc, char **argv)
9992 struct option long_opts[] = {
9993 { .val = 'p', .name = "parents", .has_arg = no_argument },
9996 const char short_opts[] = "p";
9997 const char *sep = "";
10000 bool show_parents = false;
10002 while ((rc = getopt_long(argc, argv, short_opts,
10003 long_opts, NULL)) != -1) {
10006 show_parents = true;
10010 "%s path2fid: unrecognized option '%s'\n",
10011 progname, argv[optind - 1]);
10016 if (optind > argc - 1) {
10017 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
10020 } else if (optind < argc - 1) {
10025 for (path = argv + optind; optind < argc; path++, optind++) {
10028 if (!show_parents) {
10029 err = llapi_path2fid(*path, &fid);
10031 printf("%s%s"DFID"\n",
10032 *sep != '\0' ? *path : "", sep,
10035 char name[NAME_MAX + 1];
10036 unsigned int linkno = 0;
10038 while ((err = llapi_path2parent(*path, linkno, &fid,
10039 name, sizeof(name))) == 0) {
10040 if (*sep != '\0' && linkno == 0)
10041 printf("%s%s", *path, sep);
10043 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
10048 /* err == -ENODATA is end-of-loop */
10049 if (linkno > 0 && err == -ENODATA) {
10057 "%s path2fid: cannot get %sfid for '%s': %s\n",
10058 progname, show_parents ? "parent " : "", *path,
10070 #define MAX_ERRNO 4095
10071 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
10073 static int lfs_rmfid_and_show_errors(int rootfd, struct fid_array *fa)
10077 rc = llapi_rmfid_at(rootfd, fa);
10079 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
10080 progname, strerror(-rc));
10084 for (k = 0; k < fa->fa_nr; k++) {
10085 rc2 = (__s32)fa->fa_fids[k].f_ver;
10086 if (!IS_ERR_VALUE(rc2))
10092 fa->fa_fids[k].f_ver = 0;
10093 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
10094 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
10100 static int lfs_rmfid(int argc, char **argv)
10102 int rc = 0, rc2, rc3 = 0, nr;
10103 struct fid_array *fa;
10104 const char *device;
10108 /* Interactive mode: Adjust optind */
10112 device = argv[optind++];
10114 if (optind > argc - 1) {
10115 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
10119 nr = argc - optind;
10121 rc = llapi_root_path_open(device, &rootfd);
10124 "%s rmfid: error opening device/fsname '%s': %s\n",
10125 progname, device, strerror(-rc));
10129 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
10131 fprintf(stderr, "%s rmfid: error allocating %zd bytes: %s\n",
10132 progname, offsetof(struct fid_array, fa_fids[nr + 1]),
10139 while (optind < argc) {
10143 origfidstr = fidstr = argv[optind++];
10144 while (*fidstr == '[')
10146 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
10148 fprintf(stderr, "lfs rmfid: '%s': Wrong FID format\n",
10151 rc3 = -EINVAL; /* Invalid argument */
10155 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
10156 /* start another batch */
10157 rc2 = lfs_rmfid_and_show_errors(rootfd, fa);
10166 rc2 = lfs_rmfid_and_show_errors(rootfd, fa);
10182 static int lfs_data_version(int argc, char **argv)
10184 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
10185 __u64 data_version;
10192 fprintf(stderr, "%s: FILE must be specified\n",
10197 while ((c = getopt(argc, argv, "hnrw")) != -1) {
10200 data_version_flags = 0;
10203 data_version_flags |= LL_DV_RD_FLUSH;
10206 data_version_flags |= LL_DV_WR_FLUSH;
10210 "%s data_version: unrecognized option '%s'\n",
10211 progname, argv[optind - 1]);
10217 if (optind == argc) {
10218 fprintf(stderr, "%s data_version: FILE must be specified\n",
10223 path = argv[optind];
10224 fd = open(path, O_RDONLY);
10227 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
10228 progname, path, strerror(-rc));
10232 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
10235 "%s data_version: cannot get version for '%s': %s\n",
10236 progname, path, strerror(-rc));
10238 printf("%ju" "\n", (uintmax_t)data_version);
10244 static int lfs_hsm_state(int argc, char **argv)
10249 struct hsm_user_state hus;
10258 rc2 = llapi_hsm_state_get(path, &hus);
10261 "%s %s: get HSM state for '%s' failed: %s\n",
10262 progname, argv[0], path, strerror(-rc2));
10268 /* Display path name and status flags */
10269 printf("%s: (0x%08x)", path, hus.hus_states);
10271 if (hus.hus_states & HS_RELEASED)
10272 printf(" released");
10273 if (hus.hus_states & HS_EXISTS)
10275 if (hus.hus_states & HS_DIRTY)
10277 if (hus.hus_states & HS_ARCHIVED)
10278 printf(" archived");
10279 /* Display user-settable flags */
10280 if (hus.hus_states & HS_NORELEASE)
10281 printf(" never_release");
10282 if (hus.hus_states & HS_NOARCHIVE)
10283 printf(" never_archive");
10284 if (hus.hus_states & HS_LOST)
10285 printf(" lost_from_hsm");
10287 if (hus.hus_archive_id != 0)
10288 printf(", archive_id:%d", hus.hus_archive_id);
10291 } while (++i < argc);
10296 #define LFS_HSM_SET 0
10297 #define LFS_HSM_CLEAR 1
10300 * Generic function to set or clear HSM flags.
10301 * Used by hsm_set and hsm_clear.
10303 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
10305 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
10307 struct option long_opts[] = {
10308 { .val = 'A', .name = "archived", .has_arg = no_argument },
10309 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
10310 { .val = 'd', .name = "dirty", .has_arg = no_argument },
10311 { .val = 'e', .name = "exists", .has_arg = no_argument },
10312 { .val = 'h', .name = "help", .has_arg = no_argument },
10313 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
10314 { .val = 'l', .name = "lost", .has_arg = no_argument },
10315 { .val = 'r', .name = "norelease", .has_arg = no_argument },
10316 { .name = NULL } };
10320 __u32 archive_id = 0;
10326 while ((c = getopt_long(argc, argv, "aAdehi:lr",
10327 long_opts, NULL)) != -1) {
10333 mask |= HS_NOARCHIVE;
10336 mask |= HS_ARCHIVED;
10339 mask |= HS_NORELEASE;
10349 archive_id = strtol(optarg, &end, 10);
10350 if (errno != 0 || *end != '\0' || archive_id < 0) {
10352 "%s: invalid archive_id: '%s'\n",
10358 fprintf(stderr, "%s: unrecognized option '%s'\n",
10359 progname, argv[optind - 1]);
10366 /* User should have specified a flag */
10370 while (optind < argc) {
10372 path = argv[optind];
10374 /* If mode == 0, this means we apply the mask. */
10375 if (mode == LFS_HSM_SET)
10376 rc2 = llapi_hsm_state_set(path, mask, 0, archive_id);
10378 rc2 = llapi_hsm_state_set(path, 0, mask, 0);
10382 "%s %s: change hsm flags for '%s' failed: %s\n",
10383 progname, argv[0], path, strerror(-rc2));
10393 static int lfs_hsm_action(int argc, char **argv)
10395 struct hsm_current_action hca;
10396 struct hsm_extent he;
10397 enum hsm_user_action hua;
10398 enum hsm_progress_states hps;
10410 rc2 = llapi_hsm_current_action(path, &hca);
10413 "%s %s: get hsm action for '%s' failed: %s\n",
10414 progname, argv[0], path, strerror(-rc2));
10420 he = hca.hca_location;
10421 hua = hca.hca_action;
10422 hps = hca.hca_state;
10424 printf("%s: %s", path, hsm_user_action2name(hua));
10426 /* Skip file without action */
10427 if (hca.hca_action == HUA_NONE) {
10432 printf(" %s ", hsm_progress_state2name(hps));
10434 if ((hps == HPS_RUNNING) &&
10435 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
10436 printf("(%llu bytes moved)\n",
10437 (unsigned long long)he.length);
10438 else if ((he.offset + he.length) == LUSTRE_EOF)
10439 printf("(from %llu to EOF)\n",
10440 (unsigned long long)he.offset);
10442 printf("(from %llu to %llu)\n",
10443 (unsigned long long)he.offset,
10444 (unsigned long long)(he.offset + he.length));
10446 } while (++i < argc);
10451 static int lfs_hsm_set(int argc, char **argv)
10453 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
10456 static int lfs_hsm_clear(int argc, char **argv)
10458 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
10462 * Check file state and return its fid, to be used by lfs_hsm_request().
10464 * \param[in] file Path to file to check
10465 * \param[in,out] fid Pointer to allocated lu_fid struct.
10466 * \param[in,out] last_dev Pointer to last device id used.
10468 * \return 0 on success.
10470 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
10476 rc = lstat(file, &st);
10478 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
10482 * Checking for regular file as archiving as posix copytool
10483 * rejects archiving files other than regular files
10485 if (!S_ISREG(st.st_mode)) {
10486 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
10489 /* A request should be ... */
10490 if (*last_dev != st.st_dev && *last_dev != 0) {
10492 "All files should be on the same filesystem: %s\n",
10496 *last_dev = st.st_dev;
10498 rc = llapi_path2fid(file, fid);
10500 fprintf(stderr, "Cannot read FID of %s: %s\n",
10501 file, strerror(-rc));
10507 /* Fill an HSM HUR item with a given file name.
10509 * If mntpath is set, then the filename is actually a FID, and no
10510 * lookup on the filesystem will be performed.
10512 * \param[in] hur the user request to fill
10513 * \param[in] idx index of the item inside the HUR to fill
10514 * \param[in] mntpath mountpoint of Lustre
10515 * \param[in] fname filename (if mtnpath is NULL)
10516 * or FID (if mntpath is set)
10517 * \param[in] last_dev pointer to last device id used
10519 * \retval 0 on success
10520 * \retval CMD_HELP or a negative errno on error
10522 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
10523 const char *mntpath, const char *fname,
10526 struct hsm_user_item *hui = &hur->hur_user_item[idx];
10529 hui->hui_extent.length = -1;
10532 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
10534 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
10537 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
10541 hur->hur_request.hr_itemcount++;
10546 static int lfs_hsm_request(int argc, char **argv, int action)
10548 struct option long_opts[] = {
10549 { .val = 'a', .name = "archive", .has_arg = required_argument },
10550 { .val = 'D', .name = "data", .has_arg = required_argument },
10551 { .val = 'h', .name = "help", .has_arg = no_argument },
10552 { .val = 'l', .name = "filelist", .has_arg = required_argument },
10553 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
10554 { .name = NULL } };
10555 dev_t last_dev = 0;
10556 struct hsm_user_request *hur, *oldhur;
10561 char *filelist = NULL;
10562 char fullpath[PATH_MAX];
10563 char *opaque = NULL;
10564 int opaque_len = 0;
10565 int archive_id = 0;
10567 int nbfile_alloc = 0;
10568 char *some_file = NULL;
10569 char *mntpath = NULL;
10575 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
10576 long_opts, NULL)) != -1) {
10585 if (action != HUA_ARCHIVE &&
10586 action != HUA_REMOVE) {
10588 "error: -a is supported only when archiving or removing\n");
10591 archive_id = atoi(optarg);
10596 some_file = strdup(optarg);
10600 fprintf(stderr, "%s: unrecognized option '%s'\n",
10601 progname, argv[optind - 1]);
10608 /* All remaining args are files, so we have at least nbfile */
10609 nbfile = argc - optind;
10611 if ((nbfile == 0) && (!filelist))
10615 opaque_len = strlen(opaque);
10618 * Alloc the request structure with enough place to store all files
10619 * from command line.
10621 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
10623 fprintf(stderr, "Cannot create the request: %s\n",
10627 nbfile_alloc = nbfile;
10629 hur->hur_request.hr_action = action;
10630 hur->hur_request.hr_archive_id = archive_id;
10631 hur->hur_request.hr_flags = 0;
10633 /* All remaining args are files, add them */
10634 if (nbfile != 0 && some_file == NULL)
10635 some_file = strdup(argv[optind]);
10637 for (i = 0; i < nbfile; i++) {
10638 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
10644 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
10646 /* If a filelist was specified, read the filelist from it. */
10648 fp = fopen(filelist, "r");
10650 fprintf(stderr, "Cannot read the file list %s: %s\n",
10651 filelist, strerror(errno));
10656 while ((rc = getline(&line, &len, fp)) != -1) {
10658 * If allocated buffer was too small, get something
10661 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
10664 nbfile_alloc = nbfile_alloc * 2 + 1;
10666 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
10670 "hsm: cannot allocate the request: %s\n",
10677 size = hur_len(oldhur);
10680 "hsm: cannot allocate %u files + %u bytes data\n",
10681 oldhur->hur_request.hr_itemcount,
10682 oldhur->hur_request.hr_data_len);
10689 memcpy(hur, oldhur, size);
10694 if (line[strlen(line) - 1] == '\n')
10695 line[strlen(line) - 1] = '\0';
10697 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
10698 mntpath, line, &last_dev);
10714 /* If a --data was used, add it to the request */
10715 hur->hur_request.hr_data_len = opaque_len;
10717 memcpy(hur_data(hur), opaque, opaque_len);
10719 /* Send the HSM request */
10720 if (realpath(some_file, fullpath) == NULL) {
10721 fprintf(stderr, "Could not find path '%s': %s\n",
10722 some_file, strerror(errno));
10724 rc = llapi_hsm_request(fullpath, hur);
10726 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
10727 some_file, strerror(-rc));
10737 static int lfs_hsm_archive(int argc, char **argv)
10739 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
10742 static int lfs_hsm_restore(int argc, char **argv)
10744 return lfs_hsm_request(argc, argv, HUA_RESTORE);
10747 static int lfs_hsm_release(int argc, char **argv)
10749 return lfs_hsm_request(argc, argv, HUA_RELEASE);
10752 static int lfs_hsm_remove(int argc, char **argv)
10754 return lfs_hsm_request(argc, argv, HUA_REMOVE);
10757 static int lfs_hsm_cancel(int argc, char **argv)
10759 return lfs_hsm_request(argc, argv, HUA_CANCEL);
10762 static int lfs_swap_layouts(int argc, char **argv)
10767 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
10768 SWAP_LAYOUTS_KEEP_MTIME |
10769 SWAP_LAYOUTS_KEEP_ATIME);
10772 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10774 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10776 int lfs_get_mode(const char *string)
10778 enum lock_mode_user mode;
10780 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10781 if (lock_mode_names[mode] == NULL)
10783 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10790 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10792 enum lu_ladvise_type advice;
10795 advice < ARRAY_SIZE(ladvise_names); advice++) {
10796 if (ladvise_names[advice] == NULL)
10798 if (strcmp(string, ladvise_names[advice]) == 0)
10802 return LU_LADVISE_INVALID;
10805 static int lfs_ladvise(int argc, char **argv)
10807 struct option long_opts[] = {
10808 { .val = 'a', .name = "advice", .has_arg = required_argument },
10809 { .val = 'b', .name = "background", .has_arg = no_argument },
10810 { .val = 'e', .name = "end", .has_arg = required_argument },
10811 { .val = 'h', .name = "help", .has_arg = no_argument },
10812 { .val = 'l', .name = "length", .has_arg = required_argument },
10813 { .val = 'm', .name = "mode", .has_arg = required_argument },
10814 { .val = 's', .name = "start", .has_arg = required_argument },
10815 { .val = 'u', .name = "unset", .has_arg = no_argument },
10816 { .name = NULL } };
10817 struct llapi_lu_ladvise advice;
10818 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10819 unsigned long long start = 0;
10820 unsigned long long end = LUSTRE_EOF;
10821 unsigned long long length = 0;
10822 unsigned long long size_units;
10823 unsigned long long flags = 0;
10829 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10830 long_opts, NULL)) != -1) {
10833 advice_type = lfs_get_ladvice(optarg);
10834 if (advice_type == LU_LADVISE_INVALID) {
10836 "%s: invalid advice type '%s'\n",
10838 fprintf(stderr, "Valid types:");
10840 for (advice_type = 0;
10841 advice_type < ARRAY_SIZE(ladvise_names);
10843 if (ladvise_names[advice_type] == NULL)
10845 fprintf(stderr, " %s",
10846 ladvise_names[advice_type]);
10848 fprintf(stderr, "\n");
10861 rc = llapi_parse_size(optarg, &end,
10864 fprintf(stderr, "%s: bad end offset '%s'\n",
10871 rc = llapi_parse_size(optarg, &start,
10875 "%s: bad start offset '%s'\n",
10882 rc = llapi_parse_size(optarg, &length,
10885 fprintf(stderr, "%s: bad length '%s'\n",
10891 mode = lfs_get_mode(optarg);
10894 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10900 fprintf(stderr, "%s: unrecognized option '%s'\n",
10901 progname, argv[optind - 1]);
10908 if (advice_type == LU_LADVISE_INVALID) {
10909 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10910 fprintf(stderr, "Valid types:");
10911 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10913 if (ladvise_names[advice_type] == NULL)
10915 fprintf(stderr, " %s", ladvise_names[advice_type]);
10917 fprintf(stderr, "\n");
10921 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10923 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10928 if (argc <= optind) {
10929 fprintf(stderr, "%s: please give one or more file names\n",
10934 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10935 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10940 if (end == LUSTRE_EOF && length != 0)
10941 end = start + length;
10943 if (end <= start) {
10944 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10945 argv[0], start, end);
10949 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10950 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10955 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10956 fprintf(stderr, "%s: mode is required with lockahead\n",
10961 while (optind < argc) {
10964 path = argv[optind++];
10966 fd = open(path, O_RDONLY);
10969 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10970 argv[0], path, strerror(-rc2));
10976 advice.lla_start = start;
10977 advice.lla_end = end;
10978 advice.lla_advice = advice_type;
10979 advice.lla_value1 = 0;
10980 advice.lla_value2 = 0;
10981 advice.lla_value3 = 0;
10982 advice.lla_value4 = 0;
10983 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10984 advice.lla_lockahead_mode = mode;
10985 advice.lla_peradvice_flags = flags;
10988 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10992 "%s: cannot give advice '%s' to file '%s': %s\n",
10993 argv[0], ladvise_names[advice_type],
10994 path, strerror(errno));
11005 static const char *const heat_names[] = LU_HEAT_NAMES;
11007 static int lfs_heat_get(int argc, char **argv)
11009 struct lu_heat *heat;
11018 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
11020 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
11025 while (optind < argc) {
11026 path = argv[optind++];
11028 fd = open(path, O_RDONLY);
11030 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11031 argv[0], path, strerror(errno));
11036 heat->lh_count = OBD_HEAT_COUNT;
11037 rc2 = llapi_heat_get(fd, heat);
11041 "%s: cannot get heat of file '%s': %s\n",
11042 argv[0], path, strerror(errno));
11046 printf("flags: %x\n", heat->lh_flags);
11047 for (i = 0; i < heat->lh_count; i++)
11048 printf("%s: %llu\n", heat_names[i],
11049 (unsigned long long)heat->lh_heat[i]);
11051 if (rc == 0 && rc2 < 0)
11059 static int lfs_heat_set(int argc, char **argv)
11061 struct option long_opts[] = {
11062 { .val = 'c', .name = "clear", .has_arg = no_argument },
11063 { .val = 'h', .name = "help", .has_arg = no_argument },
11064 { .val = 'o', .name = "off", .has_arg = no_argument },
11065 { .val = 'O', .name = "on", .has_arg = no_argument },
11066 { .name = NULL } };
11067 enum lu_heat_flag flags = 0;
11077 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
11080 flags |= LU_HEAT_FLAG_CLEAR;
11083 flags |= LU_HEAT_FLAG_CLEAR;
11084 flags |= LU_HEAT_FLAG_OFF;
11087 flags &= ~LU_HEAT_FLAG_OFF;
11090 fprintf(stderr, "%s: unrecognized option '%s'\n",
11091 progname, argv[optind - 1]);
11098 if (argc <= optind) {
11099 fprintf(stderr, "%s: please give one or more file names\n",
11104 while (optind < argc) {
11105 path = argv[optind++];
11107 fd = open(path, O_RDONLY);
11109 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11110 argv[0], path, strerror(errno));
11115 rc2 = llapi_heat_set(fd, flags);
11119 "%s: cannot setflags heat of file '%s': %s\n",
11120 argv[0], path, strerror(errno));
11124 if (rc == 0 && rc2 < 0)
11131 * The input string contains a comma delimited list of component ids and
11132 * ranges, for example "1,2-4,7".
11134 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
11136 bool end_of_loop = false;
11144 while (!end_of_loop) {
11148 char *endptr = NULL;
11151 ptr = strchrnul(arg, ',');
11152 end_of_loop = *ptr == '\0';
11155 start_index = strtol(arg, &endptr, 0);
11156 if (endptr == arg) /* no data at all */
11158 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
11160 if (start_index < 0)
11163 end_index = start_index;
11164 if (*endptr == '-') {
11165 end_index = strtol(endptr + 1, &endptr, 0);
11166 if (*endptr != '\0')
11168 if (end_index < start_index)
11172 for (i = start_index; i <= end_index && size > 0; i++) {
11175 /* remove duplicate */
11176 for (j = 0; j < nr; j++) {
11180 if (j == nr) { /* no duplicate */
11186 if (size == 0 && i < end_index)
11193 if (!end_of_loop && ptr)
11196 return rc < 0 ? rc : nr;
11200 * struct verify_mirror_id - Mirror id to be verified.
11201 * @mirror_id: A specified mirror id.
11202 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
11204 struct verify_mirror_id {
11210 * compare_mirror_ids() - Compare mirror ids.
11211 * @layout: Mirror component list.
11212 * @cbdata: Callback data in verify_mirror_id structure.
11214 * This is a callback function called by llapi_layout_comp_iterate()
11215 * to compare the specified mirror id with the one in the current
11216 * component of @layout. If they are the same, then the specified
11217 * mirror id is valid.
11219 * Return: a negative error code on failure or
11220 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
11221 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
11224 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
11226 struct verify_mirror_id *mirror_id_cbdata =
11227 (struct verify_mirror_id *)cbdata;
11228 uint32_t mirror_id;
11231 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11235 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11236 progname, strerror(errno));
11240 if (mirror_id_cbdata->mirror_id == mirror_id) {
11241 mirror_id_cbdata->is_valid_id = true;
11242 return LLAPI_LAYOUT_ITER_STOP;
11245 return LLAPI_LAYOUT_ITER_CONT;
11249 * verify_mirror_ids() - Verify specified mirror ids.
11250 * @fname: Mirrored file name.
11251 * @mirror_ids: Specified mirror ids to be verified.
11252 * @ids_nr: Number of specified mirror ids.
11254 * This function verifies that specified @mirror_ids are valid
11255 * in the mirrored file @fname.
11257 * Return: 0 on success or a negative error code on failure.
11260 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
11262 struct llapi_layout *layout = NULL;
11263 struct verify_mirror_id mirror_id_cbdata = { 0 };
11265 uint32_t flr_state;
11274 if (stat(fname, &stbuf) < 0) {
11275 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11276 progname, fname, strerror(errno));
11281 if (!S_ISREG(stbuf.st_mode)) {
11282 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11288 fd = open(fname, O_DIRECT | O_RDONLY);
11290 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11291 progname, fname, strerror(errno));
11296 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11298 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11299 progname, fname, strerror(errno));
11303 layout = llapi_layout_get_by_fd(fd, 0);
11305 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11306 progname, fname, strerror(errno));
11308 llapi_lease_release(fd);
11312 rc = llapi_layout_flags_get(layout, &flr_state);
11314 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11315 progname, fname, strerror(errno));
11320 flr_state &= LCM_FL_FLR_MASK;
11321 switch (flr_state) {
11324 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11325 progname, fname, llapi_layout_flags_string(flr_state));
11332 for (i = 0; i < ids_nr; i++) {
11333 mirror_id_cbdata.mirror_id = mirror_ids[i];
11334 mirror_id_cbdata.is_valid_id = false;
11336 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
11337 &mirror_id_cbdata);
11341 "%s: '%s' failed to verify mirror id: %u.\n",
11342 progname, fname, mirror_ids[i]);
11346 if (!mirror_id_cbdata.is_valid_id) {
11349 "%s: '%s' invalid specified mirror id: %u.\n",
11350 progname, fname, mirror_ids[i]);
11356 llapi_layout_free(layout);
11357 llapi_lease_release(fd);
11365 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
11366 __u16 *mirror_ids, int ids_nr,
11367 long stats_interval_sec, long bandwidth_bytes_sec)
11369 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11370 struct llapi_layout *layout;
11372 uint32_t flr_state;
11381 if (stat(fname, &stbuf) < 0) {
11382 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11383 progname, fname, strerror(errno));
11387 if (!S_ISREG(stbuf.st_mode)) {
11388 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11394 /* Allow mirror resync even without the key on encrypted files */
11395 fd = open(fname, O_DIRECT | O_RDWR | O_CIPHERTEXT);
11397 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11398 progname, fname, strerror(errno));
11403 layout = llapi_layout_get_by_fd(fd, 0);
11405 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11406 progname, fname, strerror(errno));
11411 rc = llapi_layout_flags_get(layout, &flr_state);
11413 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11414 progname, fname, strerror(errno));
11419 flr_state &= LCM_FL_FLR_MASK;
11420 if (flr_state == LCM_FL_NONE) {
11422 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
11427 /* get stale component info */
11428 comp_size = llapi_mirror_find_stale(layout, comp_array,
11429 ARRAY_SIZE(comp_array),
11430 mirror_ids, ids_nr);
11431 if (comp_size <= 0) {
11436 ioc->lil_mode = LL_LEASE_WRLCK;
11437 ioc->lil_flags = LL_LEASE_RESYNC;
11438 rc = llapi_lease_set(fd, ioc);
11440 if (rc == -EALREADY)
11444 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
11445 progname, fname, strerror(-rc));
11449 /* get the read range [start, end) */
11450 start = comp_array[0].lrc_start;
11451 end = comp_array[0].lrc_end;
11452 for (idx = 1; idx < comp_size; idx++) {
11453 if (comp_array[idx].lrc_start < start)
11454 start = comp_array[idx].lrc_start;
11455 if (end < comp_array[idx].lrc_end)
11456 end = comp_array[idx].lrc_end;
11459 rc = llapi_lease_check(fd);
11460 if (rc != LL_LEASE_WRLCK) {
11461 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11466 rc = llapi_mirror_resync_many_params(fd, layout, comp_array, comp_size,
11467 start, end, stats_interval_sec,
11468 bandwidth_bytes_sec);
11470 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
11471 progname, fname, strerror(-rc));
11473 rc = migrate_set_timestamps(fd, &stbuf);
11475 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
11476 progname, fname, strerror(-rc));
11480 /* need to do the lease unlock even resync fails */
11481 ioc->lil_mode = LL_LEASE_UNLCK;
11482 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11483 ioc->lil_count = 0;
11484 for (idx = 0; idx < comp_size; idx++) {
11485 if (comp_array[idx].lrc_synced) {
11486 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
11491 rc2 = llapi_lease_set(fd, ioc);
11493 * llapi_lease_set returns lease mode when it request to unlock
11497 /* rc2 == 0 means lost lease lock */
11498 if (rc2 == 0 && rc == 0)
11502 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
11504 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
11506 llapi_lease_release(fd);
11511 llapi_layout_free(layout);
11518 static inline int lfs_mirror_resync(int argc, char **argv)
11520 struct option long_opts[] = {
11521 { .val = 'h', .name = "help", .has_arg = no_argument },
11522 { .val = 'o', .name = "only", .has_arg = required_argument },
11523 { .val = 'W', .name = "bandwidth", .has_arg = required_argument },
11524 { .val = LFS_STATS_OPT,
11525 .name = "stats", .has_arg = no_argument},
11526 { .val = LFS_STATS_INTERVAL_OPT,
11527 .name = "stats-interval",
11528 .has_arg = required_argument},
11529 { .name = NULL } };
11530 struct ll_ioc_lease *ioc = NULL;
11531 __u16 mirror_ids[128] = { 0 };
11532 unsigned int stats_interval_sec = 0;
11533 unsigned long long bandwidth_bytes_sec = 0;
11534 unsigned long long bandwidth_unit = ONE_MB;
11539 while ((c = getopt_long(argc, argv, "ho:W:", long_opts, NULL)) >= 0) {
11543 rc = parse_mirror_ids(mirror_ids,
11544 sizeof(mirror_ids) / sizeof(__u16),
11548 "%s: bad mirror ids '%s'.\n",
11555 if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
11556 &bandwidth_unit, 0) < 0) {
11558 "error: %s: bad value for bandwidth '%s'\n",
11563 case LFS_STATS_OPT:
11564 stats_interval_sec = 5;
11566 case LFS_STATS_INTERVAL_OPT:
11567 stats_interval_sec = strtol(optarg, &end, 0);
11570 fprintf(stderr, "%s: unrecognized option '%s'\n",
11571 progname, argv[optind - 1]);
11579 if (argc == optind) {
11580 fprintf(stderr, "%s: no file name given.\n", argv[0]);
11585 if (ids_nr > 0 && argc > optind + 1) {
11587 "%s: option '--only' cannot be used upon multiple files.\n",
11594 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11599 /* set the lease on the file */
11600 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11602 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
11603 argv[0], strerror(errno));
11608 for (; optind < argc; optind++) {
11609 rc = lfs_mirror_resync_file(argv[optind], ioc,
11610 mirror_ids, ids_nr,
11611 stats_interval_sec,
11612 bandwidth_bytes_sec);
11613 /* ignore previous file's error, continue with next file */
11616 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
11624 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
11626 struct llapi_layout *layout;
11629 layout = llapi_layout_get_by_fd(fd, 0);
11631 fprintf(stderr, "could not get layout.\n");
11635 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
11637 fprintf(stderr, "failed to iterate layout\n");
11638 llapi_layout_free(layout);
11641 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
11642 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
11643 llapi_layout_free(layout);
11647 llapi_layout_free(layout);
11653 * Check whether two files are the same file
11654 * \retval 0 same file
11655 * \retval 1 not the same file
11656 * \retval <0 error code
11658 static inline int check_same_file(int fd, const char *f2)
11660 struct stat stbuf1;
11661 struct stat stbuf2;
11663 if (fstat(fd, &stbuf1) < 0)
11666 if (stat(f2, &stbuf2) < 0)
11669 if (stbuf1.st_rdev == stbuf2.st_rdev &&
11670 stbuf1.st_ino == stbuf2.st_ino)
11676 static inline int lfs_mirror_read(int argc, char **argv)
11679 __u16 mirror_id = 0;
11680 const char *outfile = NULL;
11686 const size_t buflen = 4 << 20;
11688 struct option long_opts[] = {
11689 { .val = 'h', .name = "help", .has_arg = no_argument },
11690 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11691 { .val = 'o', .name = "outfile", .has_arg = required_argument },
11692 { .name = NULL } };
11694 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
11699 unsigned long int id;
11702 id = strtoul(optarg, &end, 0);
11703 if (errno != 0 || *end != '\0' || id == 0 ||
11706 "%s %s: invalid mirror ID '%s'\n",
11707 progname, argv[0], optarg);
11711 mirror_id = (__u16)id;
11718 fprintf(stderr, "%s: unrecognized option '%s'\n",
11719 progname, argv[optind - 1]);
11726 if (argc == optind) {
11727 fprintf(stderr, "%s %s: no mirrored file provided\n",
11728 progname, argv[0]);
11730 } else if (argc > optind + 1) {
11731 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11735 if (mirror_id == 0) {
11736 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11737 progname, argv[0]);
11741 /* open mirror file */
11742 fname = argv[optind];
11743 fd = open(fname, O_DIRECT | O_RDONLY);
11745 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11746 progname, argv[0], fname, strerror(errno));
11750 /* verify mirror id */
11751 rc = verify_mirror_id_by_fd(fd, mirror_id);
11754 "%s %s: cannot find mirror with ID %u in '%s'\n",
11755 progname, argv[0], mirror_id, fname);
11759 /* open output file - O_EXCL ensures output is not the same as input */
11761 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
11763 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
11764 progname, argv[0], outfile, strerror(errno));
11769 outfd = STDOUT_FILENO;
11772 /* allocate buffer */
11773 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
11775 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11776 progname, argv[0], rc);
11782 ssize_t bytes_read;
11783 ssize_t written = 0;
11785 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
11786 if (bytes_read < 0) {
11789 "%s %s: fail to read data from mirror %u: %s\n",
11790 progname, argv[0], mirror_id, strerror(-rc));
11795 if (bytes_read == 0)
11798 while (written < bytes_read) {
11801 written2 = write(outfd, buf + written,
11802 bytes_read - written);
11803 if (written2 < 0) {
11805 "%s %s: fail to write %s: %s\n",
11806 progname, argv[0], outfile ? : "STDOUT",
11811 written += written2;
11814 if (written != bytes_read) {
11816 "%s %s: written %ld bytes does not match with %ld read.\n",
11817 progname, argv[0], written, bytes_read);
11839 static inline int lfs_mirror_write(int argc, char **argv)
11842 __u16 mirror_id = 0;
11843 const char *inputfile = NULL;
11849 const size_t buflen = 4 << 20;
11851 size_t page_size = sysconf(_SC_PAGESIZE);
11852 struct ll_ioc_lease_id ioc;
11853 struct option long_opts[] = {
11854 { .val = 'h', .name = "help", .has_arg = no_argument },
11855 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11856 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11857 { .name = NULL } };
11859 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11864 unsigned long int id;
11867 id = strtoul(optarg, &end, 0);
11868 if (errno != 0 || *end != '\0' || id == 0 ||
11871 "%s %s: invalid mirror ID '%s'\n",
11872 progname, argv[0], optarg);
11876 mirror_id = (__u16)id;
11880 inputfile = optarg;
11883 fprintf(stderr, "%s: unrecognized option '%s'\n",
11884 progname, argv[optind - 1]);
11891 if (argc == optind) {
11892 fprintf(stderr, "%s %s: no mirrored file provided\n",
11893 progname, argv[0]);
11895 } else if (argc > optind + 1) {
11896 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11900 if (mirror_id == 0) {
11901 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11902 progname, argv[0]);
11906 /* open mirror file */
11907 fname = argv[optind];
11908 fd = open(fname, O_DIRECT | O_WRONLY);
11910 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11911 progname, argv[0], fname, strerror(errno));
11915 /* verify mirror id */
11916 rc = verify_mirror_id_by_fd(fd, mirror_id);
11919 "%s %s: cannot find mirror with ID %u in '%s'\n",
11920 progname, argv[0], mirror_id, fname);
11924 /* open input file */
11926 rc = check_same_file(fd, inputfile);
11929 "%s %s: input file cannot be the mirrored file '%s'\n",
11930 progname, argv[0], fname);
11936 inputfd = open(inputfile, O_RDONLY, 0644);
11938 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11939 progname, argv[0], inputfile, strerror(errno));
11944 inputfd = STDIN_FILENO;
11947 /* allocate buffer */
11948 rc = posix_memalign(&buf, page_size, buflen);
11950 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11951 progname, argv[0], rc);
11952 goto close_inputfd;
11955 /* prepare target mirror components instantiation */
11956 ioc.lil_mode = LL_LEASE_WRLCK;
11957 ioc.lil_flags = LL_LEASE_RESYNC;
11958 ioc.lil_mirror_id = mirror_id;
11959 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11962 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11963 progname, argv[0], fname, strerror(errno));
11969 ssize_t bytes_read;
11973 rc = llapi_lease_check(fd);
11974 if (rc != LL_LEASE_WRLCK) {
11975 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11976 progname, argv[0], fname);
11980 bytes_read = read(inputfd, buf, buflen);
11981 if (bytes_read < 0) {
11984 "%s %s: fail to read data from '%s': %s\n",
11985 progname, argv[0], inputfile ? : "STDIN",
11992 if (bytes_read == 0)
11995 /* round up to page align to make direct IO happy. */
11996 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11998 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
12003 "%s %s: fail to write to mirror %u: %s\n",
12004 progname, argv[0], mirror_id,
12012 if (pos & (page_size - 1)) {
12013 rc = llapi_mirror_truncate(fd, mirror_id, pos);
12018 ioc.lil_mode = LL_LEASE_UNLCK;
12019 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
12021 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
12026 "%s %s: release lease lock of '%s' failed: %s\n",
12027 progname, argv[0], fname, strerror(-rc));
12044 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
12046 struct llapi_layout *layout;
12047 struct collect_ids_data cid = { .cid_ids = ids,
12049 .cid_exclude = exclude_id, };
12052 layout = llapi_layout_get_by_fd(fd, 0);
12054 fprintf(stderr, "could not get layout\n");
12058 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
12060 fprintf(stderr, "failed to iterate layout\n");
12061 llapi_layout_free(layout);
12065 llapi_layout_free(layout);
12067 return cid.cid_count;
12070 #ifndef MIRROR_ID_NEG
12071 #define MIRROR_ID_NEG 0x8000
12074 static inline int lfs_mirror_copy(int argc, char **argv)
12077 __u16 read_mirror_id = 0;
12078 __u16 ids[128] = { 0 };
12080 struct llapi_layout *layout = NULL;
12081 struct llapi_resync_comp comp_array[1024] = { { 0 } };
12088 struct ll_ioc_lease *ioc = NULL;
12089 struct ll_ioc_lease_id *resync_ioc;
12090 struct option long_opts[] = {
12091 { .val = 'h', .name = "help", .has_arg = no_argument },
12092 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
12093 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
12094 { .name = NULL } };
12095 char cmd[PATH_MAX];
12097 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12099 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
12104 unsigned long int id;
12107 id = strtoul(optarg, &end, 0);
12108 if (errno != 0 || *end != '\0' || id == 0 ||
12111 "%s: invalid read mirror ID '%s'\n",
12116 read_mirror_id = (__u16)id;
12120 if (!strcmp(optarg, "-1")) {
12121 /* specify all other mirrors */
12122 ids[0] = (__u16)-1;
12125 count = parse_mirror_ids((__u16 *)ids,
12133 fprintf(stderr, "%s: unrecognized option '%s'\n",
12134 progname, argv[optind - 1]);
12141 if (argc == optind) {
12142 fprintf(stderr, "%s %s: no mirrored file provided\n",
12143 progname, argv[0]);
12145 } else if (argc > optind + 1) {
12146 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
12150 if (read_mirror_id == 0) {
12152 "%s %s: no valid read mirror ID %d is provided\n",
12153 progname, argv[0], read_mirror_id);
12159 "%s %s: no write mirror ID is provided\n",
12160 progname, argv[0]);
12164 for (i = 0; i < count; i++) {
12165 if (read_mirror_id == ids[i]) {
12167 "%s %s: read and write mirror ID cannot be the same\n",
12168 progname, argv[0]);
12173 /* open mirror file */
12174 fname = argv[optind];
12176 fd = open(fname, O_DIRECT | O_RDWR);
12178 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
12179 progname, argv[0], fname, strerror(errno));
12183 /* write to all other mirrors */
12184 if (ids[0] == (__u16)-1) {
12185 count = get_other_mirror_ids(fd, ids, read_mirror_id);
12189 "%s %s: failed to get other mirror ids in '%s': %d\n",
12190 progname, argv[0], fname, rc);
12195 /* verify mirror id */
12196 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
12199 "%s %s: cannot find mirror with ID %u in '%s'\n",
12200 progname, argv[0], read_mirror_id, fname);
12204 for (i = 0; i < count; i++) {
12205 rc = verify_mirror_id_by_fd(fd, ids[i]);
12208 "%s %s: cannot find mirror with ID %u in '%s'\n",
12209 progname, argv[0], ids[i], fname);
12214 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
12217 "%s %s: cannot alloc comp id array for ioc: %s\n",
12218 progname, argv[0], strerror(errno));
12223 /* get stale component info */
12224 layout = llapi_layout_get_by_fd(fd, 0);
12226 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
12227 progname, argv[0], fname, strerror(errno));
12231 comp_size = llapi_mirror_find_stale(layout, comp_array,
12232 ARRAY_SIZE(comp_array),
12234 llapi_layout_free(layout);
12235 if (comp_size < 0) {
12240 /* prepare target mirror components instantiation */
12241 resync_ioc = (struct ll_ioc_lease_id *)ioc;
12242 resync_ioc->lil_mode = LL_LEASE_WRLCK;
12243 resync_ioc->lil_flags = LL_LEASE_RESYNC;
12245 resync_ioc->lil_mirror_id = ids[0];
12247 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
12248 rc = llapi_lease_set(fd, ioc);
12251 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
12252 progname, argv[0], fname, strerror(errno));
12256 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
12259 fprintf(stderr, "%s %s: copy error: %d\n",
12260 progname, argv[0], rc);
12264 fprintf(stdout, "mirror copied successfully: ");
12265 for (i = 0; i < copied; i++)
12266 fprintf(stdout, "%d ", ids[i]);
12267 fprintf(stdout, "\n");
12269 ioc->lil_mode = LL_LEASE_UNLCK;
12270 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
12271 ioc->lil_count = 0;
12272 for (i = 0; i < comp_size; i++) {
12275 for (j = 0; j < copied; j++) {
12276 if (comp_array[i].lrc_mirror_id != ids[j])
12279 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
12283 rc = llapi_lease_set(fd, ioc);
12288 "%s %s: release lease lock of '%s' failed: %s\n",
12289 progname, argv[0], fname, strerror(errno));
12304 * struct verify_chunk - Mirror chunk to be verified.
12305 * @chunk: [start, end) of the chunk.
12306 * @mirror_count: Number of mirror ids in @mirror_id array.
12307 * @mirror_id: Array of valid mirror ids that cover the chunk.
12309 struct verify_chunk {
12310 struct lu_extent chunk;
12311 unsigned int mirror_count;
12312 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
12316 * print_chunks() - Print chunk information.
12317 * @fname: Mirrored file name.
12318 * @chunks: Array of chunks.
12319 * @chunk_count: Number of chunks in @chunks array.
12321 * This function prints [start, end) of each chunk in @chunks
12322 * for mirrored file @fname, and also prints the valid mirror ids
12323 * that cover the chunk.
12328 void print_chunks(const char *fname, struct verify_chunk *chunks,
12334 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
12335 for (i = 0; i < chunk_count; i++) {
12336 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
12338 if (chunks[i].mirror_count == 0)
12339 fprintf(stdout, "\t[");
12341 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
12342 for (j = 1; j < chunks[i].mirror_count; j++)
12343 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
12345 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
12347 fprintf(stdout, "\n");
12351 * print_checksums() - Print CRC-32 checksum values.
12352 * @chunk: A chunk and its corresponding valid mirror ids.
12353 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
12355 * This function prints CRC-32 checksum values on @chunk for
12356 * each valid mirror that covers it.
12361 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
12362 unsigned long long pos, unsigned long long len)
12367 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
12368 for (i = 0; i < chunk->mirror_count; i++)
12369 fprintf(stdout, "Mirror %u:\t%#lx\n",
12370 chunk->mirror_id[i], crc[i]);
12371 fprintf(stdout, "\n");
12375 * filter_mirror_id() - Filter specified mirror ids.
12376 * @chunks: Array of chunks.
12377 * @chunk_count: Number of chunks in @chunks array.
12378 * @mirror_ids: Specified mirror ids to be verified.
12379 * @ids_nr: Number of specified mirror ids.
12381 * This function scans valid mirror ids that cover each chunk in @chunks
12382 * and filters specified mirror ids.
12387 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
12388 __u16 *mirror_ids, int ids_nr)
12393 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12394 unsigned int valid_count = 0;
12396 for (i = 0; i < chunk_count; i++) {
12397 if (chunks[i].mirror_count == 0)
12401 for (j = 0; j < ids_nr; j++) {
12402 for (k = 0; k < chunks[i].mirror_count; k++) {
12403 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
12404 valid_id[valid_count] = mirror_ids[j];
12411 memcpy(chunks[i].mirror_id, valid_id,
12412 sizeof(__u16) * valid_count);
12413 chunks[i].mirror_count = valid_count;
12418 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
12419 * @layout: Mirror component list.
12420 * @chunks: Array of chunks.
12421 * @chunks_size: Array size of @chunks.
12423 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
12424 * to find out chunk segments and store them in @chunks array.
12426 * The @mirror_id array in each element of @chunks will store the valid
12427 * mirror ids that cover the chunk. If a mirror component covering the
12428 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
12429 * will not be stored into the @mirror_id array, and the chunk for that
12430 * mirror will not be verified.
12432 * The @mirror_count in each element of @chunks will store the number of
12433 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
12434 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
12435 * indicates the chunk is valid in only one mirror. In both cases, the
12436 * chunk will not be verified.
12438 * Here is an example:
12440 * 0 1M 2M 3M 4M EOF
12441 * +------+-------------+--------------------+
12442 * | | | S | mirror1
12443 * +------+------+------+------+-------------+
12444 * | | S | S | | mirror2
12445 * +-------------+------+------+-------------+
12447 * prepared @chunks array will contain 5 elements:
12448 * (([0, 1M), [1, 2], 2),
12449 * ([1M, 2M), [1, 2], 2),
12450 * ([2M, 3M), [1], 1),
12451 * ([3M, 4M], [], 0),
12452 * ([4M, EOF), [2], 1))
12454 * Return: the actual array size of @chunks on success
12455 * or a negative error code on failure.
12458 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
12459 struct verify_chunk *chunks,
12460 size_t chunks_size)
12464 uint32_t mirror_id;
12470 memset(chunks, 0, sizeof(*chunks) * chunks_size);
12473 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
12476 "%s: move to the first layout component: %s.\n",
12477 progname, strerror(errno));
12483 chunks[idx].chunk.e_end = LUSTRE_EOF;
12485 rc = llapi_layout_comp_extent_get(layout, &start, &end);
12488 "%s: llapi_layout_comp_extent_get failed: %s.\n",
12489 progname, strerror(errno));
12493 if (start > chunks[idx].chunk.e_start ||
12494 end <= chunks[idx].chunk.e_start)
12497 if (end < chunks[idx].chunk.e_end)
12498 chunks[idx].chunk.e_end = end;
12500 rc = llapi_layout_comp_flags_get(layout, &flags);
12503 "%s: llapi_layout_comp_flags_get failed: %s.\n",
12504 progname, strerror(errno));
12508 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
12511 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
12514 "%s: llapi_layout_mirror_id_get failed: %s.\n",
12515 progname, strerror(errno));
12519 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
12521 "%s: mirror_id array is too small.\n",
12526 chunks[idx].mirror_id[i] = mirror_id;
12530 rc = llapi_layout_comp_use(layout,
12531 LLAPI_LAYOUT_COMP_USE_NEXT);
12534 "%s: move to the next layout component: %s.\n",
12535 progname, strerror(errno));
12538 } /* loop through all components */
12540 chunks[idx].mirror_count = i;
12542 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
12546 if (idx >= chunks_size) {
12547 fprintf(stderr, "%s: chunks array is too small.\n",
12553 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
12557 return rc < 0 ? rc : idx + 1;
12561 * lfs_mirror_verify_chunk() - Verify a chunk.
12562 * @fd: File descriptor of the mirrored file.
12563 * @file_size: Size of the mirrored file.
12564 * @chunk: A chunk and its corresponding valid mirror ids.
12565 * @verbose: Verbose mode.
12567 * This function verifies a @chunk contains exactly the same data
12568 * ammong the mirrors that cover it.
12570 * If @verbose is specified, then the function will print where the
12571 * differences are if the data do not match. Otherwise, it will
12572 * just return an error in that case.
12574 * Return: 0 on success or a negative error code on failure.
12577 int lfs_mirror_verify_chunk(int fd, size_t file_size,
12578 struct verify_chunk *chunk, int verbose)
12580 const size_t buflen = 4 * 1024 * 1024; /* 4M */
12582 size_t page_size = sysconf(_SC_PAGESIZE);
12583 ssize_t bytes_read;
12584 ssize_t bytes_done;
12588 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12592 if (file_size == 0)
12595 rc = posix_memalign(&buf, page_size, buflen);
12596 if (rc) /* error code is returned directly */
12600 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12601 PEXT(&chunk->chunk));
12602 for (i = 0; i < chunk->mirror_count; i++)
12603 fprintf(stdout, " %u", chunk->mirror_id[i]);
12604 fprintf(stdout, "\n");
12608 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
12609 pos = chunk->chunk.e_start;
12610 while (bytes_done < count) {
12611 /* compute initial CRC-32 checksum */
12612 crc = crc32(0L, Z_NULL, 0);
12613 memset(crc_array, 0, sizeof(crc_array));
12616 for (i = 0; i < chunk->mirror_count; i++) {
12617 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
12619 if (bytes_read < 0) {
12622 "%s: failed to read data from mirror %u: %s.\n",
12623 progname, chunk->mirror_id[i],
12628 /* compute new CRC-32 checksum */
12629 crc_array[i] = crc32(crc, buf, bytes_read);
12633 print_checksums(chunk, crc_array, pos, buflen);
12635 /* compare CRC-32 checksum values */
12636 for (i = 1; i < chunk->mirror_count; i++) {
12637 if (crc_array[i] != crc_array[0]) {
12641 "%s: chunk "DEXT" has different checksum value on mirror %u:%lx and mirror %u:%lx.\n",
12642 progname, PEXT(&chunk->chunk),
12643 chunk->mirror_id[0], crc_array[0],
12644 chunk->mirror_id[i], crc_array[i]);
12645 print_checksums(chunk, crc_array, pos, buflen);
12650 bytes_done += bytes_read;
12653 if (verbose > 1 && rc == 0) {
12654 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12655 PEXT(&chunk->chunk));
12656 for (i = 0; i < chunk->mirror_count; i++)
12657 fprintf(stdout, " %u", chunk->mirror_id[i]);
12658 fprintf(stdout, " PASS\n\n");
12667 * lfs_mirror_verify_file() - Verify a mirrored file.
12668 * @fname: Mirrored file name.
12669 * @mirror_ids: Specified mirror ids to be verified.
12670 * @ids_nr: Number of specified mirror ids.
12671 * @verbose: Verbose mode.
12673 * This function verifies that each SYNC mirror of a mirrored file
12674 * specified by @fname contains exactly the same data.
12676 * If @mirror_ids is specified, then the function will verify the
12677 * mirrors specified by @mirror_ids contain exactly the same data.
12679 * If @verbose is specified, then the function will print where the
12680 * differences are if the data do not match. Otherwise, it will
12681 * just return an error in that case.
12683 * Return: 0 on success or a negative error code on failure.
12686 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
12689 struct verify_chunk chunks_array[1024] = { };
12690 struct llapi_layout *layout = NULL;
12692 uint32_t flr_state;
12694 int chunk_count = 0;
12700 if (stat(fname, &stbuf) < 0) {
12701 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
12702 progname, fname, strerror(errno));
12707 if (!S_ISREG(stbuf.st_mode)) {
12708 fprintf(stderr, "%s: '%s' is not a regular file.\n",
12714 if (stbuf.st_size == 0) {
12716 fprintf(stdout, "%s: '%s' file size is 0.\n",
12722 /* Allow mirror verify even without the key on encrypted files */
12723 fd = open(fname, O_DIRECT | O_RDONLY | O_CIPHERTEXT);
12725 fprintf(stderr, "%s: cannot open '%s': %s.\n",
12726 progname, fname, strerror(errno));
12731 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
12733 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
12734 progname, fname, strerror(errno));
12738 layout = llapi_layout_get_by_fd(fd, 0);
12740 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
12741 progname, fname, strerror(errno));
12743 llapi_lease_release(fd);
12747 rc = llapi_layout_flags_get(layout, &flr_state);
12749 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
12750 progname, fname, strerror(errno));
12755 flr_state &= LCM_FL_FLR_MASK;
12756 switch (flr_state) {
12759 fprintf(stderr, "%s: '%s' file state error: %s.\n",
12760 progname, fname, llapi_layout_flags_string(flr_state));
12766 /* find out mirror chunks to be verified */
12767 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
12768 ARRAY_SIZE(chunks_array));
12769 if (chunk_count < 0) {
12775 /* filter specified mirror ids */
12776 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
12779 print_chunks(fname, chunks_array, chunk_count);
12781 for (idx = 0; idx < chunk_count; idx++) {
12782 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
12785 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
12787 PEXT(&chunks_array[idx].chunk),
12788 (unsigned long long)stbuf.st_size);
12792 if (chunks_array[idx].mirror_count == 0) {
12794 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12796 PEXT(&chunks_array[idx].chunk));
12798 fprintf(stderr, "skipped\n");
12802 fprintf(stderr, "failed\n");
12806 if (chunks_array[idx].mirror_count == 1) {
12809 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12811 PEXT(&chunks_array[idx].chunk),
12812 chunks_array[idx].mirror_id[0]);
12816 rc = llapi_lease_check(fd);
12817 if (rc != LL_LEASE_RDLCK) {
12818 fprintf(stderr, "%s: '%s' lost lease lock.\n",
12823 /* verify one chunk */
12824 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12825 &chunks_array[idx], verbose);
12839 llapi_layout_free(layout);
12840 llapi_lease_release(fd);
12848 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12849 * @argc: The count of lfs mirror verify command line arguments.
12850 * @argv: Array of strings for lfs mirror verify command line arguments.
12852 * This function parses lfs mirror verify command and verifies the
12853 * specified mirrored file(s).
12855 * Return: 0 on success or a negative error code on failure.
12857 static inline int lfs_mirror_verify(int argc, char **argv)
12859 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12865 char cmd[PATH_MAX];
12867 struct option long_opts[] = {
12868 { .val = 'h', .name = "help", .has_arg = no_argument },
12869 { .val = 'o', .name = "only", .has_arg = required_argument },
12870 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12871 { .name = NULL } };
12873 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12875 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12878 rc = parse_mirror_ids(mirror_ids,
12879 ARRAY_SIZE(mirror_ids),
12883 "%s: bad mirror ids '%s'.\n",
12890 "%s: at least 2 mirror ids needed with '--only' option.\n",
12900 fprintf(stderr, "%s: unrecognized option '%s'\n",
12901 progname, argv[optind - 1]);
12909 if (argc == optind) {
12910 fprintf(stderr, "%s: no file name given.\n", progname);
12915 if (ids_nr > 0 && argc > optind + 1) {
12917 "%s: '--only' cannot be used upon multiple files.\n",
12924 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12930 for (; optind < argc; optind++) {
12931 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12941 * lfs_mirror() - Parse and execute lfs mirror commands.
12942 * @argc: The count of lfs mirror command line arguments.
12943 * @argv: Array of strings for lfs mirror command line arguments.
12945 * This function parses lfs mirror commands and performs the
12946 * corresponding functions specified in mirror_cmdlist[].
12948 * Return: 0 on success or an error code on failure.
12950 static int lfs_mirror(int argc, char **argv)
12952 char cmd[PATH_MAX];
12955 setlinebuf(stdout);
12957 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12959 program_invocation_short_name = cmd;
12960 rc = cfs_parser(argc, argv, mirror_cmdlist);
12962 return rc < 0 ? -rc : rc;
12965 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12967 #if __BYTE_ORDER == __BIG_ENDIAN
12968 __swab16s(&attrs->lsa_valid);
12969 __swab64s(&attrs->lsa_size);
12970 __swab64s(&attrs->lsa_blocks);
12974 enum lfs_som_type {
12975 LFS_SOM_SIZE = 0x1,
12976 LFS_SOM_BLOCKS = 0x2,
12977 LFS_SOM_FLAGS = 0x4,
12978 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12982 static int lfs_getsom(int argc, char **argv)
12985 struct lustre_som_attrs *attrs;
12986 char buf[sizeof(*attrs) + 64];
12987 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12990 while ((c = getopt(argc, argv, "bfhs")) != -1) {
12993 type = LFS_SOM_BLOCKS;
12996 type = LFS_SOM_FLAGS;
12999 type = LFS_SOM_SIZE;
13002 fprintf(stderr, "%s: unrecognized option '%s'\n",
13003 progname, argv[optind - 1]);
13014 fprintf(stderr, "%s: %s\n",
13015 progname, argc == 0 ? "miss file target" :
13016 "input more than 2 files");
13021 attrs = (void *)buf;
13022 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
13025 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
13026 argv[0], strerror(errno), errno);
13030 lustre_som_swab(attrs);
13033 case LFS_SOM_ATTR_ALL:
13034 printf("file: %s size: %llu blocks: %llu flags: %x\n",
13035 path, (unsigned long long)attrs->lsa_size,
13036 (unsigned long long)attrs->lsa_blocks,
13040 printf("%llu\n", (unsigned long long)attrs->lsa_size);
13042 case LFS_SOM_BLOCKS:
13043 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
13045 case LFS_SOM_FLAGS:
13046 printf("%x\n", attrs->lsa_valid);
13049 fprintf(stderr, "%s: unknown option\n", progname);
13056 static int lfs_pcc_attach(int argc, char **argv)
13058 struct option long_opts[] = {
13059 { .val = 'h', .name = "help", .has_arg = no_argument },
13060 { .val = 'i', .name = "id", .has_arg = required_argument },
13061 { .name = NULL } };
13064 __u32 archive_id = 0;
13067 char fullpath[PATH_MAX];
13068 enum lu_pcc_type type = LU_PCC_READWRITE;
13071 while ((c = getopt_long(argc, argv, "hi:",
13072 long_opts, NULL)) != -1) {
13076 archive_id = strtoul(optarg, &end, 0);
13077 if (errno != 0 || *end != '\0' ||
13078 archive_id == 0 || archive_id > UINT32_MAX) {
13080 "error: %s: bad archive ID '%s'\n",
13086 fprintf(stderr, "%s: unrecognized option '%s'\n",
13087 progname, argv[optind - 1]);
13094 if (archive_id == 0) {
13095 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
13099 if (argc <= optind) {
13100 fprintf(stderr, "%s: must specify one or more file names\n",
13105 while (optind < argc) {
13108 path = argv[optind++];
13109 if (!realpath(path, fullpath)) {
13110 fprintf(stderr, "%s: could not find path '%s': %s\n",
13111 argv[0], path, strerror(errno));
13117 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
13120 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
13121 argv[0], path, archive_id, strerror(-rc2));
13129 static int lfs_pcc_attach_fid(int argc, char **argv)
13131 struct option long_opts[] = {
13132 { .val = 'h', .name = "help", .has_arg = no_argument },
13133 { .val = 'i', .name = "id", .has_arg = required_argument },
13134 { .val = 'm', .name = "mnt", .has_arg = required_argument },
13135 { .name = NULL } };
13138 __u32 archive_id = 0;
13140 const char *mntpath = NULL;
13141 const char *fidstr;
13142 enum lu_pcc_type type = LU_PCC_READWRITE;
13145 while ((c = getopt_long(argc, argv, "hi:m:",
13146 long_opts, NULL)) != -1) {
13150 archive_id = strtoul(optarg, &end, 0);
13151 if (errno != 0 || *end != '\0' ||
13152 archive_id > UINT32_MAX) {
13154 "error: %s: bad archive ID '%s'\n",
13163 fprintf(stderr, "%s: unrecognized option '%s'\n",
13164 progname, argv[optind - 1]);
13171 if (archive_id == 0) {
13172 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
13177 fprintf(stderr, "%s: must specify Lustre mount point\n",
13182 if (argc <= optind) {
13183 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
13187 while (optind < argc) {
13190 fidstr = argv[optind++];
13192 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
13196 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
13197 argv[0], fidstr, mntpath, archive_id,
13200 if (rc == 0 && rc2 < 0)
13206 static int lfs_pcc_detach(int argc, char **argv)
13208 struct option long_opts[] = {
13209 { .val = 'h', .name = "help", .has_arg = no_argument },
13210 { .val = 'k', .name = "keep", .has_arg = no_argument },
13211 { .name = NULL } };
13215 char fullpath[PATH_MAX];
13216 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13219 while ((c = getopt_long(argc, argv, "hk",
13220 long_opts, NULL)) != -1) {
13223 detach_opt = PCC_DETACH_OPT_NONE;
13226 fprintf(stderr, "%s: unrecognized option '%s'\n",
13227 progname, argv[optind - 1]);
13234 while (optind < argc) {
13237 path = argv[optind++];
13238 if (!realpath(path, fullpath)) {
13239 fprintf(stderr, "%s: could not find path '%s': %s\n",
13240 argv[0], path, strerror(errno));
13246 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
13250 "%s: cannot detach '%s' from PCC: %s\n",
13251 argv[0], path, strerror(errno));
13259 static int lfs_pcc_detach_fid(int argc, char **argv)
13261 struct option long_opts[] = {
13262 { .val = 'h', .name = "help", .has_arg = no_argument },
13263 { .val = 'k', .name = "keep", .has_arg = no_argument },
13264 { .name = NULL } };
13268 const char *mntpath;
13269 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13272 while ((c = getopt_long(argc, argv, "hk",
13273 long_opts, NULL)) != -1) {
13276 detach_opt = PCC_DETACH_OPT_NONE;
13279 fprintf(stderr, "%s: unrecognized option '%s'\n",
13280 progname, argv[optind - 1]);
13287 mntpath = argv[optind++];
13289 while (optind < argc) {
13292 fid = argv[optind++];
13294 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
13297 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
13298 argv[0], fid, mntpath, strerror(-rc2));
13306 static int lfs_pcc_state(int argc, char **argv)
13310 char fullpath[PATH_MAX];
13311 struct lu_pcc_state state;
13316 fprintf(stderr, "%s: must specify one or more file names\n",
13321 while (optind < argc) {
13324 path = argv[optind++];
13325 if (!realpath(path, fullpath)) {
13326 fprintf(stderr, "%s: could not find path '%s': %s\n",
13327 argv[0], path, strerror(errno));
13333 rc2 = llapi_pcc_state_get(fullpath, &state);
13338 "%s: cannot get PCC state of '%s': %s\n",
13339 argv[0], path, strerror(-rc2));
13343 printf("file: %s", path);
13344 printf(", type: %s", pcc_type2string(state.pccs_type));
13345 if (state.pccs_type == LU_PCC_NONE &&
13346 state.pccs_open_count == 0) {
13351 printf(", PCC file: %s", state.pccs_path);
13352 printf(", user number: %u", state.pccs_open_count);
13353 printf(", flags: %x", state.pccs_flags);
13360 * lfs_pcc() - Parse and execute lfs pcc commands.
13361 * @argc: The count of lfs pcc command line arguments.
13362 * @argv: Array of strings for lfs pcc command line arguments.
13364 * This function parses lfs pcc commands and performs the
13365 * corresponding functions specified in pcc_cmdlist[].
13367 * Return: 0 on success or an error code on failure.
13369 static int lfs_pcc(int argc, char **argv)
13371 char cmd[PATH_MAX];
13374 setlinebuf(stdout);
13376 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
13378 program_invocation_short_name = cmd;
13379 rc = cfs_parser(argc, argv, pcc_cmdlist);
13381 return rc < 0 ? -rc : rc;
13384 int main(int argc, char **argv)
13388 /* Ensure that liblustreapi constructor has run */
13389 if (!llapi_liblustreapi_initialized())
13390 fprintf(stderr, "liblustreapi was not properly initialized\n");
13392 setlinebuf(stdout);
13395 progname = program_invocation_short_name; /* Used in error messages */
13396 llapi_set_command_name(argv[1]);
13397 rc = cfs_parser(argc, argv, cmdlist);
13398 llapi_clear_command_name();
13400 return rc < 0 ? -rc : rc;