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,prefrd,prefwr,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);
869 clock_gettime(CLOCK_MONOTONIC, &start_time);
870 now = last_bw_print = start_time;
874 size_t to_read, to_write;
877 if (sparse && pos >= data_end) {
880 data_off = llapi_data_seek(fd_src, pos, &data_size);
882 /* Non-fatal, switch to full copy */
886 /* hole at the end of file, truncate up to it */
888 rc = ftruncate(fd_dst, data_off);
892 pos = data_off & ~(page_size - 1);
893 data_end = data_off + data_size;
894 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
895 to_read = MIN(to_read, buf_size);
901 rc = check_file(fd_src);
906 rsize = pread(fd_src, buf, to_read, pos);
917 while (to_write > 0) {
918 unsigned long long write_target;
920 struct timespec diff;
922 written = pwrite(fd_dst, buf, to_write, pos);
929 write_bytes += written;
931 if (bandwidth_bytes_sec == 0)
934 clock_gettime(CLOCK_MONOTONIC, &now);
935 diff = timespec_sub(&start_time, &now);
936 write_target = ((bandwidth_bytes_sec * diff.tv_sec) +
937 ((bandwidth_bytes_sec *
938 diff.tv_nsec)/NSEC_PER_SEC));
940 if (write_target < write_bytes) {
941 unsigned long long excess;
942 struct timespec delay = { 0, 0 };
944 excess = write_bytes - write_target;
949 delay.tv_sec = excess / bandwidth_bytes_sec;
950 delay.tv_nsec = (excess % bandwidth_bytes_sec) *
951 NSEC_PER_SEC / bandwidth_bytes_sec;
954 rc = clock_nanosleep(CLOCK_MONOTONIC, 0,
956 } while (rc < 0 && errno == EINTR);
959 if (stats_interval_sec)
961 "error %s: delay for bandwidth control failed: %s\n",
969 clock_gettime(CLOCK_MONOTONIC, &now);
970 if (stats_interval_sec && (write_bytes != file_size_bytes) &&
971 (now.tv_sec >= last_bw_print.tv_sec +
972 stats_interval_sec)) {
973 stats_log(&now, &start_time,
974 read_bytes, write_bytes,
979 if (rc || rsize < to_read)
983 /* Output at least one log, regardless of stats_interval */
984 if (stats_interval_sec) {
985 clock_gettime(CLOCK_MONOTONIC, &now);
986 stats_log(&now, &start_time, read_bytes, write_bytes,
994 /* Try to avoid page cache pollution after migration. */
995 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
996 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1002 static int migrate_set_timestamps(int fd, const struct stat *st)
1004 struct timeval tv[2] = {
1005 {.tv_sec = st->st_atime},
1006 {.tv_sec = st->st_mtime}
1009 return futimes(fd, tv);
1012 static int migrate_block(int fd_src, int fd_dst,
1013 unsigned long long bandwidth_bytes_sec,
1014 long stats_interval_sec)
1027 /* The grouplock blocks all concurrent accesses to the file. */
1028 rc = llapi_group_lock(fd_src, gid);
1030 error_loc = "cannot get group lock";
1034 rc = fstat(fd_src, &st);
1036 error_loc = "cannot stat source file";
1042 * LL_DV_RD_FLUSH should not be set, otherwise the servers will try to
1043 * get extent locks on the OST objects. This will conflict with our
1044 * extent group locks.
1046 rc = llapi_get_data_version(fd_src, &dv1, 0);
1048 error_loc = "cannot get dataversion";
1052 rc = migrate_copy_data(fd_src, fd_dst, NULL, bandwidth_bytes_sec,
1053 stats_interval_sec, st.st_size);
1055 error_loc = "data copy failed";
1059 /* Make sure we keep original atime/mtime values */
1060 rc = migrate_set_timestamps(fd_dst, &st);
1062 error_loc = "set target file timestamp failed";
1068 * for a migration we need to check data version on file did
1071 * Pass in gid=0 since we already own grouplock.
1073 rc = llapi_fswap_layouts_grouplock(fd_src, fd_dst, dv1, 0, 0,
1074 SWAP_LAYOUTS_CHECK_DV1);
1075 if (rc == -EAGAIN) {
1076 error_loc = "file changed";
1078 } else if (rc < 0) {
1079 error_loc = "cannot swap layout";
1084 rc2 = llapi_group_unlock(fd_src, gid);
1085 if (rc2 < 0 && rc == 0) {
1086 error_loc = "unlock group lock";
1094 * Internal helper for migrate_copy_data(). Check lease and report error if
1097 * \param[in] fd File descriptor on which to check the lease.
1099 * \retval 0 Migration can keep on going.
1100 * \retval -errno Error occurred, abort migration.
1102 static int check_lease(int fd)
1106 rc = llapi_lease_check(fd);
1108 return 0; /* llapi_check_lease returns > 0 on success. */
1113 static int migrate_nonblock(int fd_src, int fd_dst,
1114 unsigned long long bandwidth_bytes_sec,
1115 long stats_interval_sec)
1122 rc = fstat(fd_src, &st);
1124 error_loc = "cannot stat source file";
1128 rc = llapi_get_data_version(fd_src, &dv1, LL_DV_RD_FLUSH);
1130 error_loc = "cannot get data version";
1134 rc = migrate_copy_data(fd_src, fd_dst, check_lease,
1135 bandwidth_bytes_sec,
1136 stats_interval_sec, st.st_size);
1138 error_loc = "data copy failed";
1142 rc = llapi_get_data_version(fd_src, &dv2, LL_DV_RD_FLUSH);
1144 error_loc = "cannot get data version";
1150 error_loc = "source file changed";
1154 /* Make sure we keep original atime/mtime values */
1155 rc = migrate_set_timestamps(fd_dst, &st);
1157 error_loc = "set target file timestamp failed";
1164 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1166 struct pool_to_id_cbdata data = { .pool = pool };
1167 struct llapi_layout *layout = NULL;
1170 layout = llapi_layout_get_by_path(fname, 0);
1173 "error %s: file '%s' couldn't get layout: rc=%d\n",
1174 progname, fname, errno);
1178 rc = llapi_layout_sanity(layout, false, true);
1180 llapi_layout_sanity_perror(errno);
1183 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1192 llapi_layout_free(layout);
1196 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1197 __u32 flags, __u32 neg_flags)
1200 __u32 flags_array[2];
1207 "error %s: neither component id nor pool is specified\n",
1211 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1217 ids[count] = comp_id;
1218 flags_array[count] = flags;
1223 if (neg_flags & LCME_FL_STALE) {
1225 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1230 ids[count] = comp_id;
1231 flags_array[count] = neg_flags | LCME_FL_NEG;
1235 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1237 if (errno == EUCLEAN) {
1240 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1241 progname, comp_id, fname);
1244 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1245 progname, comp_id, fname, flags, neg_flags);
1252 static int lfs_component_del(char *fname, __u32 comp_id,
1253 __u32 flags, __u32 neg_flags)
1257 if (flags && neg_flags) {
1259 "%s: cannot specify both positive and negative flags\n",
1264 if (!flags && neg_flags)
1265 flags = neg_flags | LCME_FL_NEG;
1267 if (flags && comp_id) {
1269 "%s: cannot specify component ID and flags at the same time\n",
1274 if (!flags && !comp_id) {
1276 "%s: neither flags nor component ID is specified\n",
1282 if (flags & ~LCME_KNOWN_FLAGS) {
1284 "%s setstripe: unknown flags %#x\n",
1288 } else if (comp_id > LCME_ID_MAX) {
1289 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1294 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1297 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1298 progname, comp_id, fname, strerror(errno));
1302 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1309 rc = llapi_layout_file_comp_add(fname, layout);
1311 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1312 fname, strerror(errno));
1316 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1317 struct llapi_layout *layout)
1325 fd = lstat(fname, &st);
1326 if (fd == 0 && S_ISDIR(st.st_mode))
1327 open_flags = O_DIRECTORY | O_RDONLY;
1329 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1331 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1332 S_ISDIR(st.st_mode) ?
1333 "set default composite layout for" :
1334 "create composite file",
1335 fname, strerror(errno));
1339 static int lfs_migrate(char *name, __u64 migration_flags,
1340 struct llapi_stripe_param *param,
1341 struct llapi_layout *layout,
1342 unsigned long long bandwidth_bytes_sec,
1343 long stats_interval_sec)
1345 struct llapi_layout *existing;
1346 uint64_t dom_new, dom_cur;
1351 rc = migrate_open_files(name, migration_flags, param, layout,
1356 rc = llapi_layout_dom_size(layout, &dom_new);
1358 error_loc = "cannot get new layout DoM size";
1361 /* special case for migration to DOM layout*/
1362 existing = llapi_layout_get_by_fd(fd_src, 0);
1364 error_loc = "cannot get existing layout";
1368 rc = llapi_layout_dom_size(existing, &dom_cur);
1370 error_loc = "cannot get current layout DoM size";
1375 * if file has DoM layout already then migration is possible to
1376 * the new layout with the same DoM component via swap layout,
1377 * if new layout used bigger DOM size, then mirroring is used
1379 if (dom_new > dom_cur) {
1380 rc = lfs_migrate_to_dom(fd_src, fd_dst, name,
1382 bandwidth_bytes_sec,
1383 stats_interval_sec);
1385 error_loc = "cannot migrate to DOM layout";
1389 if (stats_interval_sec)
1390 printf("%s:\n", name);
1392 if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1394 * Blocking mode (forced if servers do not support file lease).
1395 * It is also the default mode, since we cannot distinguish
1396 * between a broken lease and a server that does not support
1397 * atomic swap/close (LU-6785)
1399 rc = migrate_block(fd_src, fd_dst, bandwidth_bytes_sec,
1400 stats_interval_sec);
1404 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
1406 error_loc = "cannot get lease";
1410 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
1411 stats_interval_sec);
1413 llapi_lease_release(fd_src);
1418 * Atomically put lease, swap layouts and close.
1419 * for a migration we need to check data version on file did
1422 rc = llapi_fswap_layouts(fd_src, fd_dst, 0, 0, SWAP_LAYOUTS_CLOSE);
1424 error_loc = "cannot swap layout";
1436 fprintf(stderr, "error: %s: %s: %s: %s\n",
1437 progname, name, error_loc, strerror(-rc));
1438 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1439 printf("%s\n", name);
1444 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1447 char *dup_string = NULL;
1453 if (!string || !string[0])
1456 dup_string = strdup(string);
1458 llapi_printf(LLAPI_MSG_ERROR,
1459 "%s: insufficient memory\n",
1464 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1468 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1469 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1470 const char *comp_name = comp_flags_table[i].cfn_name;
1472 if (strcmp(name, comp_name) == 0) {
1473 *flags |= comp_flag;
1475 } else if (strncmp(name, "^", 1) == 0 &&
1476 strcmp(name + 1, comp_name) == 0) {
1477 *neg_flags |= comp_flag;
1482 llapi_printf(LLAPI_MSG_ERROR,
1483 "%s: component flag '%s' not supported\n",
1490 if (!*flags && !*neg_flags)
1493 /* don't allow to set and exclude the same flag */
1494 if (*flags & *neg_flags)
1502 static int mdthash_input(char *string, __u32 *inflags,
1503 __u32 *exflags, __u32 *type)
1510 {"migrating", LMV_HASH_FLAG_MIGRATION},
1511 {"bad_type", LMV_HASH_FLAG_BAD_TYPE},
1512 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1513 {"lost_lmv", LMV_HASH_FLAG_LOST_LMV},
1514 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1523 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1527 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1528 if (strcmp(name, mhflist[i].name) == 0 ||
1529 name[0] == mhflist[i].name[0]) {
1530 *inflags |= mhflist[i].flag;
1532 } else if (name[0] == '^' &&
1533 (strcmp(name + 1, mhflist[i].name) == 0 ||
1534 name[1] == mhflist[i].name[0])) {
1535 *exflags |= mhflist[i].flag;
1540 i = check_hashtype(name);
1545 llapi_printf(LLAPI_MSG_ERROR,
1546 "%s: invalid mdt_hash value '%s'\n",
1552 /* don't allow to include and exclude the same flag */
1553 if (*inflags & *exflags) {
1554 llapi_printf(LLAPI_MSG_ERROR,
1555 "%s: include and exclude same flag '%s'\n",
1563 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1571 if (strncmp(string, "^", 1) == 0) {
1572 *neg_state = llapi_layout_string_flags(string + 1);
1573 if (*neg_state != 0)
1576 *state = llapi_layout_string_flags(string);
1581 llapi_printf(LLAPI_MSG_ERROR,
1582 "%s: mirrored file state '%s' not supported\n",
1588 * struct mirror_args - Command-line arguments for mirror(s).
1589 * @m_count: Number of mirrors to be created with this layout.
1590 * @m_flags: Mirror level flags, only 'prefer' is supported.
1591 * @m_layout: Mirror layout.
1592 * @m_file: A victim file. Its layout will be split and used as a mirror.
1593 * @m_next: Point to the next node of the list.
1595 * Command-line arguments for mirror(s) will be parsed and stored in
1596 * a linked list that consists of this structure.
1598 struct mirror_args {
1601 struct llapi_layout *m_layout;
1603 struct mirror_args *m_next;
1608 * enum mirror_flags - Flags for extending a mirrored file.
1609 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1610 * in case the victim file(s) contains the same data as the
1611 * original mirrored file.
1612 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1613 * @MF_COMP_ID: specified component id instead of mirror id
1615 * Flags for extending a mirrored file.
1625 * mirror_create_sanity_check() - Check mirror list.
1626 * @list: A linked list that stores the mirror arguments.
1628 * This function does a sanity check on @list for creating
1631 * Return: 0 on success or a negative error code on failure.
1633 static int mirror_create_sanity_check(const char *fname,
1634 struct mirror_args *list,
1638 bool has_m_file = false;
1639 bool has_m_layout = false;
1644 if (fname && check_fname) {
1645 struct llapi_layout *layout;
1647 layout = llapi_layout_get_by_path(fname, 0);
1650 "error: %s: file '%s' couldn't get layout\n",
1655 rc = llapi_layout_sanity(layout, false, true);
1657 llapi_layout_free(layout);
1660 llapi_layout_sanity_perror(rc);
1668 llapi_layout_free(list->m_layout);
1671 llapi_layout_get_by_path(list->m_file, 0);
1672 if (!list->m_layout) {
1674 "error: %s: file '%s' has no layout\n",
1675 progname, list->m_file);
1679 has_m_layout = true;
1680 if (!list->m_layout) {
1681 fprintf(stderr, "error: %s: no mirror layout\n",
1687 rc = llapi_layout_sanity(list->m_layout, false, true);
1689 llapi_layout_sanity_perror(rc);
1693 list = list->m_next;
1696 if (has_m_file && has_m_layout) {
1698 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1706 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1708 __u32 mirror_flags = *(__u32 *)cbdata;
1712 rc = llapi_layout_comp_flags_get(layout, &flags);
1717 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1722 return LLAPI_LAYOUT_ITER_CONT;
1726 * mirror_create() - Create a mirrored file.
1727 * @fname: The file to be created.
1728 * @mirror_list: A linked list that stores the mirror arguments.
1730 * This function creates a mirrored file @fname with the mirror(s)
1731 * from @mirror_list.
1733 * Return: 0 on success or a negative error code on failure.
1735 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1737 struct llapi_layout *layout = NULL;
1738 struct mirror_args *cur_mirror = NULL;
1739 uint16_t mirror_count = 0;
1743 rc = mirror_create_sanity_check(fname, mirror_list, false);
1747 cur_mirror = mirror_list;
1748 while (cur_mirror) {
1749 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1751 &cur_mirror->m_flags);
1754 fprintf(stderr, "%s: failed to set mirror flags\n",
1759 for (i = 0; i < cur_mirror->m_count; i++) {
1760 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1764 "error: %s: merge layout failed: %s\n",
1765 progname, strerror(errno));
1769 mirror_count += cur_mirror->m_count;
1770 cur_mirror = cur_mirror->m_next;
1774 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1778 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1781 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1782 progname, strerror(errno));
1786 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1794 llapi_layout_free(layout);
1799 * Compare files and check lease on @fd.
1801 * \retval bytes number of bytes are the same
1803 static ssize_t mirror_file_compare(int fd_src, int fd_dst)
1805 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1807 ssize_t bytes_done = 0;
1808 ssize_t bytes_read = 0;
1810 buf = malloc(buflen * 2);
1815 if (!llapi_lease_check(fd_src)) {
1816 bytes_done = -EBUSY;
1820 bytes_read = read(fd_src, buf, buflen);
1821 if (bytes_read <= 0)
1824 if (bytes_read != read(fd_dst, buf + buflen, buflen))
1828 * XXX: should compute the checksum on each buffer and then
1829 * compare checksum to avoid cache collision
1831 if (memcmp(buf, buf + buflen, bytes_read))
1834 bytes_done += bytes_read;
1842 static int mirror_extend_file(const char *fname, const char *victim_file,
1843 enum mirror_flags mirror_flags)
1848 struct stat stbuf_v;
1849 struct ll_ioc_lease *data = NULL;
1852 fd = open(fname, O_RDWR);
1854 error_loc = "open source file";
1859 fdv = open(victim_file, O_RDWR);
1861 error_loc = "open target file";
1866 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1867 error_loc = "stat source or target file";
1872 if (stbuf.st_dev != stbuf_v.st_dev) {
1873 error_loc = "stat source and target file";
1878 /* mirrors should be of the same size */
1879 if (stbuf.st_size != stbuf_v.st_size) {
1880 error_loc = "file sizes don't match";
1885 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1887 error_loc = "cannot get lease";
1891 if (!(mirror_flags & MF_NO_VERIFY)) {
1893 /* mirrors should have the same contents */
1894 ret = mirror_file_compare(fd, fdv);
1895 if (ret != stbuf.st_size) {
1896 error_loc = "file busy or contents don't match";
1897 rc = ret < 0 ? ret : -EINVAL;
1902 /* Get rid of caching pages from clients */
1903 rc = llapi_file_flush(fd);
1905 error_loc = "cannot get data version";
1909 rc = llapi_file_flush(fdv);
1911 error_loc = "cannot get data version";
1915 rc = migrate_set_timestamps(fd, &stbuf);
1917 error_loc = "cannot set source file timestamp";
1921 /* Atomically put lease, merge layouts and close. */
1922 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1924 error_loc = "memory allocation";
1927 data->lil_mode = LL_LEASE_UNLCK;
1928 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1929 data->lil_count = 1;
1930 data->lil_ids[0] = fdv;
1931 rc = llapi_lease_set(fd, data);
1933 error_loc = "cannot merge layout";
1935 } else if (rc == 0) {
1937 error_loc = "lost lease lock";
1950 (void) unlink(victim_file);
1952 fprintf(stderr, "error: %s: %s: %s: %s\n",
1953 progname, fname, error_loc, strerror(-rc));
1957 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1958 bool inherit, uint32_t flags,
1959 unsigned long long bandwidth_bytes_sec,
1960 long stats_interval_sec)
1962 struct llapi_layout *f_layout = NULL;
1963 struct ll_ioc_lease *data = NULL;
1970 f_layout = llapi_layout_get_by_path(name, 0);
1973 fprintf(stderr, "%s: cannot get layout\n", progname);
1976 rc = llapi_layout_get_last_init_comp(f_layout);
1978 fprintf(stderr, "%s: cannot get the last init comp\n",
1982 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1985 "%s: cannot inherit from the last init comp\n",
1991 llapi_layout_comp_flags_set(m_layout, flags);
1992 rc = migrate_open_files(name,
1993 LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1994 NULL, m_layout, &fd_src, &fd_dst);
1998 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
2000 error_loc = "cannot get lease";
2004 rc = fstat(fd_src, &st);
2006 error_loc = "cannot stat source file";
2010 if (stats_interval_sec)
2011 printf("%s:\n", name);
2013 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
2014 stats_interval_sec);
2016 llapi_lease_release(fd_src);
2020 rc = migrate_set_timestamps(fd_src, &st);
2022 error_loc = "cannot set source file timestamp";
2026 /* Atomically put lease, merge layouts and close. */
2027 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2029 error_loc = "memory allocation";
2032 data->lil_mode = LL_LEASE_UNLCK;
2033 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2034 data->lil_count = 1;
2035 data->lil_ids[0] = fd_dst;
2036 rc = llapi_lease_set(fd_src, data);
2038 error_loc = "cannot merge layout";
2040 } else if (rc == 0) {
2042 error_loc = "lost lease lock";
2055 fprintf(stderr, "error: %s: %s: %s: %s\n",
2056 progname, name, error_loc, strerror(-rc));
2060 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2061 enum mirror_flags mirror_flags,
2062 unsigned long long bandwidth_bytes_sec,
2063 long stats_interval_sec)
2067 while (mirror_list) {
2068 if (mirror_list->m_file) {
2069 rc = mirror_extend_file(fname, mirror_list->m_file,
2072 __u32 mirror_count = mirror_list->m_count;
2074 while (mirror_count > 0) {
2075 rc = mirror_extend_layout(fname,
2076 mirror_list->m_layout,
2077 mirror_list->m_inherit,
2078 mirror_list->m_flags,
2079 bandwidth_bytes_sec,
2080 stats_interval_sec);
2090 mirror_list = mirror_list->m_next;
2096 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2101 rc = llapi_layout_mirror_id_get(layout, &id);
2105 if ((__u16)id == *(__u16 *)cbdata)
2106 return LLAPI_LAYOUT_ITER_STOP;
2108 return LLAPI_LAYOUT_ITER_CONT;
2111 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2116 rc = llapi_layout_comp_id_get(layout, &id);
2120 if (id == *(__u32 *)cbdata)
2121 return LLAPI_LAYOUT_ITER_STOP;
2123 return LLAPI_LAYOUT_ITER_CONT;
2126 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2128 char buf[LOV_MAXPOOLNAME + 1];
2129 struct pool_to_id_cbdata *d = (void *)cbdata;
2133 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2136 if (strcmp(d->pool, buf))
2137 return LLAPI_LAYOUT_ITER_CONT;
2139 rc = llapi_layout_mirror_id_get(layout, &id);
2144 return LLAPI_LAYOUT_ITER_STOP;
2147 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2149 char buf[LOV_MAXPOOLNAME + 1];
2150 struct pool_to_id_cbdata *d = (void *)cbdata;
2154 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2157 if (strcmp(d->pool, buf))
2158 return LLAPI_LAYOUT_ITER_CONT;
2160 rc = llapi_layout_comp_id_get(layout, &id);
2165 return LLAPI_LAYOUT_ITER_STOP;
2168 struct collect_ids_data {
2174 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2176 struct collect_ids_data *cid = cbdata;
2180 rc = llapi_layout_mirror_id_get(layout, &id);
2184 if ((__u16)id != cid->cid_exclude) {
2187 for (i = 0; i < cid->cid_count; i++) {
2188 /* already collected the mirror id */
2189 if (id == cid->cid_ids[i])
2190 return LLAPI_LAYOUT_ITER_CONT;
2192 cid->cid_ids[cid->cid_count] = id;
2196 return LLAPI_LAYOUT_ITER_CONT;
2200 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2201 * @mirror_id: Mirror id to be checked.
2202 * @layout: Mirror component list.
2204 * This function checks if a mirror with specified @mirror_id is the last
2205 * non-stale mirror of a layout @layout.
2207 * Return: true or false.
2210 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2212 __u16 mirror_ids[128] = { 0 };
2213 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2215 .cid_exclude = mirror_id, };
2218 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2220 for (i = 0; i < cid.cid_count; i++) {
2221 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2224 comp_size = llapi_mirror_find_stale(layout, comp_array,
2225 ARRAY_SIZE(comp_array),
2234 static int mirror_split(const char *fname, __u32 id, const char *pool,
2235 enum mirror_flags mflags, const char *victim_file)
2237 struct llapi_layout *layout;
2238 char parent[PATH_MAX];
2239 char victim[PATH_MAX];
2240 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2242 struct ll_ioc_lease *data;
2243 uint16_t mirror_count;
2247 bool purge = true; /* delete mirror by setting fdv=fd */
2251 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2253 "error %s: the source file '%s' and -f file are the same\n",
2258 /* check fname contains mirror with mirror_id/comp_id */
2259 layout = llapi_layout_get_by_path(fname, 0);
2262 "error %s: file '%s' couldn't get layout\n",
2267 rc = llapi_layout_sanity(layout, false, true);
2269 llapi_layout_sanity_perror(rc);
2273 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2276 "error %s: file '%s' couldn't get mirror count\n",
2280 if (mirror_count < 2) {
2282 "error %s: file '%s' has %d component, cannot split\n",
2283 progname, fname, mirror_count);
2287 if (mflags & MF_COMP_POOL) {
2288 struct pool_to_id_cbdata data = { .pool = pool };
2290 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2292 mirror_id = data.id;
2293 } else if (mflags & MF_COMP_ID) {
2294 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2295 mirror_id = mirror_id_of(id);
2297 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2301 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2304 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2305 if (mflags & MF_COMP_POOL) {
2307 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2308 progname, fname, pool);
2310 } else if (mflags & MF_COMP_ID) {
2312 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2313 progname, fname, id);
2317 "error %s: file '%s' does not contain mirror with id %u\n",
2318 progname, fname, id);
2323 if (last_non_stale_mirror(mirror_id, layout)) {
2326 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2331 if (!victim_file && mflags & MF_DESTROY)
2332 /* Allow mirror split even without the key on encrypted files,
2333 * and in this case of a 'split -d', open file with O_DIRECT
2334 * (no IOs will be done).
2336 fd = open(fname, O_RDWR | O_DIRECT | O_CIPHERTEXT);
2338 fd = open(fname, O_RDWR);
2342 "error %s: open file '%s' failed: %s\n",
2343 progname, fname, strerror(errno));
2347 /* get victim file directory pathname */
2348 if (strlen(fname) > sizeof(parent) - 1) {
2349 fprintf(stderr, "error %s: file name of '%s' too long\n",
2354 strncpy(parent, fname, sizeof(parent));
2355 ptr = strrchr(parent, '/');
2357 if (!getcwd(parent, sizeof(parent))) {
2358 fprintf(stderr, "error %s: getcwd failed: %s\n",
2359 progname, strerror(errno));
2369 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2371 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2376 rc = llapi_file_is_encrypted(fd);
2378 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2379 progname, fname, rc);
2386 /* use a temp file to store the splitted layout */
2387 if (mflags & MF_DESTROY) {
2388 char file_path[PATH_MAX];
2389 unsigned int rnumber;
2392 if (last_non_stale_mirror(mirror_id, layout)) {
2395 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2401 /* don't use volatile file for mirror destroy */
2405 * try the old way to delete mirror using
2410 rc = snprintf(file_path,
2412 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2416 rc >= sizeof(file_path)) {
2417 fdv = -ENAMETOOLONG;
2421 open_flags = O_RDWR |
2422 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2423 O_CREAT | O_EXCL | O_NOFOLLOW |
2424 /* O_DIRECT for mirror split -d */
2426 /* Allow split without the key */
2428 fdv = open(file_path, open_flags,
2432 } while (fdv < 0 && rc == -EEXIST);
2438 "error %s: not permitted on encrypted file '%s': %d\n",
2439 progname, fname, rc);
2443 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2445 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2448 /* user specified victim file */
2452 "error %s: not permitted on encrypted file '%s': %d\n",
2453 progname, fname, rc);
2456 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2461 "error %s: create victim file failed: %s\n",
2462 progname, strerror(errno));
2466 /* get lease lock of fname */
2467 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2470 "error %s: cannot get lease of file '%s': %d\n",
2471 progname, fname, rc);
2475 /* Atomatically put lease, split layouts and close. */
2476 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2482 data->lil_mode = LL_LEASE_UNLCK;
2483 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2484 data->lil_count = 2;
2485 data->lil_ids[0] = fdv;
2486 data->lil_ids[1] = mirror_id;
2487 rc = llapi_lease_set(fd, data);
2489 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2490 /* could be old MDS which prohibit fd==fdv */
2495 if (rc == 0) /* lost lease lock */
2498 "error %s: cannot split '%s': %s\n",
2499 progname, fname, strerror(-rc));
2511 llapi_layout_free(layout);
2516 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2517 __u16 *mirror_ids, int ids_nr,
2518 long stats_interval_sec, long bandwidth_bytes_sec);
2521 static int lfs_migrate_to_dom(int fd_src, int fd_dst, char *name,
2522 __u64 migration_flags,
2523 unsigned long long bandwidth_bytes_sec,
2524 long stats_interval_sec)
2526 struct ll_ioc_lease *data = NULL;
2529 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
2531 error_loc = "cannot get lease";
2535 if (stats_interval_sec)
2536 printf("%s:\n", name);
2538 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
2539 stats_interval_sec);
2543 /* Atomically put lease, merge layouts, resync and close. */
2544 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2546 error_loc = "memory allocation";
2549 data->lil_mode = LL_LEASE_UNLCK;
2550 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2551 data->lil_count = 1;
2552 data->lil_ids[0] = fd_dst;
2553 rc = llapi_lease_set(fd_src, data);
2555 error_loc = "cannot merge layout";
2557 } else if (rc == 0) {
2559 error_loc = "lost lease lock";
2565 rc = lfs_mirror_resync_file(name, data, NULL, 0,
2567 bandwidth_bytes_sec);
2569 error_loc = "cannot resync file";
2573 /* delete first mirror now */
2574 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2576 error_loc = "cannot delete old layout";
2580 llapi_lease_release(fd_src);
2586 fprintf(stderr, "error: %s: %s: %s: %s\n",
2587 progname, name, error_loc, strerror(-rc));
2588 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2589 printf("%s\n", name);
2596 * Parse a string containing an target index list into an array of integers.
2598 * The input string contains a comma delimited list of individual
2599 * indices and ranges, for example "1,2-4,7". Add the indices into the
2600 * \a tgts array and remove duplicates.
2602 * \param[out] tgts array to store indices in
2603 * \param[in] size size of \a tgts array
2604 * \param[in] offset starting index in \a tgts
2605 * \param[in] arg string containing OST index list
2606 * \param[in/out] overstriping index list may contain duplicates
2608 * \retval positive number of indices in \a tgts
2609 * \retval -EINVAL unable to parse \a arg
2611 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2612 unsigned long long *pattern)
2616 int slots = size - offset;
2618 bool overstriped = false;
2624 end_of_loop = false;
2625 while (!end_of_loop) {
2626 int start_index = 0;
2629 char *endptr = NULL;
2633 ptr = strchrnul(arg, ',');
2635 end_of_loop = *ptr == '\0';
2639 start_index = strtol(arg, &endptr, 0);
2640 if (endptr == arg) /* no data at all */
2642 if (errno != 0 || start_index < -1 ||
2643 (*endptr != '-' && *endptr != '\0'))
2646 end_index = start_index;
2647 if (*endptr == '-') {
2649 end_index = strtol(endptr + 1, &endptr, 0);
2650 if (errno != 0 || *endptr != '\0' || end_index < -1)
2652 if (end_index < start_index)
2656 for (i = start_index; i <= end_index && slots > 0; i++) {
2659 /* remove duplicate */
2660 for (j = 0; j < offset; j++) {
2661 if (tgts[j] == i && pattern &&
2662 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2664 else if (tgts[j] == i)
2670 if (j == offset) { /* check complete */
2676 if (slots == 0 && i < end_index)
2684 if (!end_of_loop && ptr)
2687 if (!overstriped && pattern)
2688 *pattern = LLAPI_LAYOUT_DEFAULT;
2690 return rc < 0 ? rc : nr;
2693 struct lfs_setstripe_args {
2694 unsigned long long lsa_comp_end;
2695 unsigned long long lsa_stripe_size;
2696 unsigned long long lsa_extension_size;
2697 long long lsa_stripe_count;
2698 long long lsa_stripe_off;
2699 __u32 lsa_comp_flags;
2700 __u32 lsa_comp_neg_flags;
2701 unsigned long long lsa_pattern;
2702 unsigned int lsa_mirror_count;
2704 bool lsa_first_comp;
2705 bool lsa_extension_comp;
2707 char *lsa_pool_name;
2710 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2712 unsigned int mirror_count = lsa->lsa_mirror_count;
2713 bool first_comp = lsa->lsa_first_comp;
2715 memset(lsa, 0, sizeof(*lsa));
2717 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2718 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2719 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2720 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2721 lsa->lsa_pool_name = NULL;
2723 lsa->lsa_mirror_count = mirror_count;
2724 lsa->lsa_first_comp = first_comp;
2728 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2729 * @lsa: Stripe options to be initialized and inherited.
2731 * This function initializes stripe options in @lsa and inherit
2732 * stripe_size, stripe_count and OST pool_name options.
2736 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2738 unsigned long long stripe_size;
2739 long long stripe_count;
2740 char *pool_name = NULL;
2742 stripe_size = lsa->lsa_stripe_size;
2743 stripe_count = lsa->lsa_stripe_count;
2744 pool_name = lsa->lsa_pool_name;
2746 setstripe_args_init(lsa);
2748 lsa->lsa_stripe_size = stripe_size;
2749 lsa->lsa_stripe_count = stripe_count;
2750 lsa->lsa_pool_name = pool_name;
2753 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2755 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2756 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2757 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2758 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2759 lsa->lsa_comp_end != 0);
2762 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2764 if (lsa->lsa_nr_tgts) {
2765 if (lsa->lsa_nr_tgts < 0 ||
2766 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2767 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2773 if (lsa->lsa_stripe_count > 0 &&
2774 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2775 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2776 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2777 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2778 lsa->lsa_stripe_count,
2790 * comp_args_to_layout() - Create or extend a composite layout.
2791 * @composite: Pointer to the composite layout.
2792 * @lsa: Stripe options for the new component.
2794 * This function creates or extends a composite layout by adding a new
2795 * component with stripe options from @lsa.
2797 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2799 * Return: 0 on success or an error code on failure.
2801 static int comp_args_to_layout(struct llapi_layout **composite,
2802 struct lfs_setstripe_args *lsa,
2805 struct llapi_layout *layout = *composite;
2806 uint64_t prev_end = 0;
2812 layout = llapi_layout_alloc();
2814 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2819 *composite = layout;
2820 lsa->lsa_first_comp = true;
2825 * Get current component extent, current component
2826 * must be the tail component.
2828 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2830 fprintf(stderr, "Get comp extent failed. %s\n",
2835 if (lsa->lsa_first_comp) {
2837 rc = llapi_layout_add_first_comp(layout);
2839 rc = llapi_layout_comp_add(layout);
2842 fprintf(stderr, "Add component failed. %s\n",
2848 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2850 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2851 lsa->lsa_comp_flags, strerror(errno));
2856 uint64_t comp_end = lsa->lsa_comp_end;
2859 * The extendable component is 0-length, so it can be removed
2860 * if there is insufficient space to extend it.
2862 if (lsa->lsa_extension_comp)
2863 comp_end = prev_end;
2865 rc = llapi_layout_comp_extent_set(layout, prev_end,
2868 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2869 prev_end, comp_end, strerror(errno));
2873 /* reset lsa_first_comp */
2874 lsa->lsa_first_comp = false;
2876 /* Data-on-MDT component setting */
2877 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2879 if (lsa->lsa_stripe_count == 0)
2880 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2881 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2882 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2883 if (lsa->lsa_stripe_off == -1 ||
2884 lsa->lsa_stripe_off == 0)
2885 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2887 * In case of Data-on-MDT patterns the only extra option
2888 * applicable is stripe size option.
2890 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2892 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2893 lsa->lsa_stripe_count);
2897 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2898 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2900 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2901 lsa->lsa_stripe_size);
2905 if (lsa->lsa_nr_tgts != 0) {
2907 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2912 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2914 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2915 lsa->lsa_stripe_off);
2919 if (lsa->lsa_pool_name != 0) {
2921 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2922 lsa->lsa_pool_name);
2927 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2929 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2934 /* Data-on-MDT component has always single stripe up to end */
2935 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2936 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2937 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2939 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2946 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2947 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2949 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2950 rc = llapi_layout_extension_size_set(layout, size);
2952 rc = llapi_layout_stripe_size_set(layout, size);
2955 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2956 size, strerror(errno));
2960 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2962 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2963 lsa->lsa_stripe_count, strerror(errno));
2967 if (lsa->lsa_pool_name) {
2968 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2970 fprintf(stderr, "Set pool name: %s failed. %s\n",
2971 lsa->lsa_pool_name, strerror(errno));
2975 rc = llapi_layout_pool_name_set(layout, "");
2977 fprintf(stderr, "Clear pool name failed: %s\n",
2983 rc = lsa_args_stripe_count_check(lsa);
2987 if (lsa->lsa_nr_tgts > 0) {
2990 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2991 rc = llapi_layout_ost_index_set(layout, i,
2996 /* Make sure stripe offset is in OST list. */
2997 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
3001 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
3002 lsa->lsa_stripe_off);
3006 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
3007 lsa->lsa_stripe_off != -1) {
3008 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
3011 fprintf(stderr, "Set ost index %d failed. %s\n",
3012 i, strerror(errno));
3016 /* Create the second, virtual component of extension space */
3017 if (lsa->lsa_extension_comp) {
3018 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
3019 lsa->lsa_extension_comp = false;
3026 static int build_component(struct llapi_layout **layout,
3027 struct lfs_setstripe_args *lsa, bool set_extent)
3031 rc = comp_args_to_layout(layout, lsa, set_extent);
3035 if (lsa->lsa_mirror_count > 0) {
3036 rc = llapi_layout_mirror_count_set(*layout,
3037 lsa->lsa_mirror_count);
3041 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
3044 lsa->lsa_mirror_count = 0;
3050 static int build_prev_component(struct llapi_layout **layout,
3051 struct lfs_setstripe_args *prev,
3052 struct lfs_setstripe_args *lsa,
3055 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
3058 if (prev->lsa_stripe_size) {
3060 prev->lsa_comp_end = lsa->lsa_comp_end;
3061 prev->lsa_extension_size = lsa->lsa_extension_size;
3062 prev->lsa_extension_comp = true;
3065 rc = build_component(layout, prev, true);
3071 * Copy lsa to previous lsa;
3072 * if this is an extension component, make the previous invalid;
3075 prev->lsa_stripe_size = 0;
3082 #ifndef LCME_TEMPLATE_FLAGS
3083 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
3087 static int build_layout_from_yaml_node(struct cYAML *node,
3088 struct llapi_layout **layout,
3089 struct lfs_setstripe_args *lsa,
3090 struct lfs_setstripe_args *prevp)
3092 struct lfs_setstripe_args prev = { 0 };
3093 __u32 *osts = lsa->lsa_tgts;
3101 string = node->cy_string;
3103 if (node->cy_type == CYAML_TYPE_OBJECT) {
3104 /* go deep to sub blocks */
3105 if (string && !strncmp(string, "component", 9) &&
3106 strncmp(string, "component0", 10) &&
3107 strncmp(string, "components", 10)) {
3108 rc = build_prev_component(layout, prevp, lsa,
3113 /* initialize lsa. */
3114 setstripe_args_init(lsa);
3115 lsa->lsa_first_comp = false;
3116 lsa->lsa_tgts = osts;
3119 rc = build_layout_from_yaml_node(node->cy_child, layout,
3124 if (!node->cy_string)
3127 /* skip leading lmm_ if present, to simplify parsing */
3128 if (strncmp(string, "lmm_", 4) == 0)
3131 if (node->cy_type == CYAML_TYPE_STRING) {
3132 if (!strcmp(string, "lcme_extent.e_end")) {
3133 if (!strcmp(node->cy_valuestring, "EOF") ||
3134 !strcmp(node->cy_valuestring, "eof"))
3135 lsa->lsa_comp_end = LUSTRE_EOF;
3136 } else if (!strcmp(string, "pool")) {
3137 lsa->lsa_pool_name = node->cy_valuestring;
3138 } else if (!strcmp(string, "pattern")) {
3139 if (!strcmp(node->cy_valuestring, "mdt"))
3140 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3141 if (!strcmp(node->cy_valuestring,
3142 "raid0,overstriped"))
3144 LLAPI_LAYOUT_OVERSTRIPING;
3145 } else if (!strcmp(string, "lcme_flags")) {
3146 rc = comp_str2flags(node->cy_valuestring,
3147 &lsa->lsa_comp_flags,
3148 &lsa->lsa_comp_neg_flags);
3152 * Only template flags have meaning in
3153 * the layout for a new file
3155 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3157 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3158 if (!strcmp(string, "lcm_mirror_count")) {
3159 lsa->lsa_mirror_count = node->cy_valueint;
3160 } else if (!strcmp(string, "lcme_extent.e_start")) {
3161 if (node->cy_valueint == 0)
3162 lsa->lsa_first_comp = true;
3163 } else if (!strcmp(string, "lcme_extent.e_end")) {
3164 if (node->cy_valueint == -1)
3165 lsa->lsa_comp_end = LUSTRE_EOF;
3167 lsa->lsa_comp_end = node->cy_valueint;
3168 } else if (!strcmp(string, "stripe_count")) {
3169 lsa->lsa_stripe_count = node->cy_valueint;
3170 } else if (!strcmp(string, "stripe_size")) {
3171 lsa->lsa_stripe_size = node->cy_valueint;
3172 } else if (!strcmp(string, "extension_size")) {
3173 lsa->lsa_extension_size = node->cy_valueint;
3174 lsa->lsa_extension_comp = true;
3175 } else if (!strcmp(string, "stripe_offset")) {
3176 lsa->lsa_stripe_off = node->cy_valueint;
3177 } else if (!strcmp(string, "l_ost_idx")) {
3178 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3183 node = node->cy_next;
3186 if (prevp == &prev) {
3187 rc = build_prev_component(layout, prevp, lsa, true);
3191 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3192 rc = build_component(layout, lsa, *layout != NULL);
3198 static int lfs_comp_create_from_yaml(char *template,
3199 struct llapi_layout **layout,
3200 struct lfs_setstripe_args *lsa,
3203 struct cYAML *tree = NULL, *err_rc = NULL;
3206 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3208 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3209 progname, template);
3210 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3211 "can't parse", &err_rc);
3212 cYAML_print_tree2file(stderr, err_rc);
3213 cYAML_free_tree(err_rc);
3218 /* initialize lsa for plain file */
3219 setstripe_args_init(lsa);
3220 lsa->lsa_tgts = osts;
3222 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3224 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3225 progname, template);
3228 /* clean clean lsa */
3229 setstripe_args_init(lsa);
3233 cYAML_free_tree(tree);
3238 * Get the extension size from the next (SEL) component and extend the
3239 * current component on it. The start of the next component is to be
3242 * \param[in] layout the current layout
3243 * \param[in] start the start of the current component
3244 * \param[in,out] end the end of the current component
3245 * \param[in] offset the offset to adjust the end position to instead of
3248 * \retval 0 - extended successfully
3249 * \retval < 0 - error
3251 static int layout_extend_comp(struct llapi_layout *layout,
3252 uint64_t start, uint64_t *end,
3255 uint64_t size, next_start, next_end;
3258 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3261 "%s setstripe: cannot move component cursor: %s\n",
3262 progname, strerror(errno));
3267 * Even if the @size will not be used below, this will fail if
3268 * this is not a SEL component - a good confirmation we are
3269 * working on right components.
3271 rc = llapi_layout_extension_size_get(layout, &size);
3274 "%s setstripe: cannot get component ext size: %s\n",
3275 progname, strerror(errno));
3279 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3281 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3282 progname, strerror(errno));
3286 next_start += offset ?: size;
3287 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3289 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3290 progname, strerror(errno));
3294 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3297 "%s setstripe: cannot move component cursor: %s\n",
3298 progname, strerror(errno));
3302 *end += offset ?: size;
3303 rc = llapi_layout_comp_extent_set(layout, start, *end);
3305 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3306 progname, strerror(errno));
3314 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3315 * end of the last component in the existing file, and adjust the
3316 * first extent start of the components to be added accordingly.
3318 * In the create mode, we need to check if the first component is an extendable
3319 * SEL component and extend its length to the extension size (first component
3320 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3322 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3325 struct llapi_layout *head;
3326 uint64_t start = 0, prev_end = 0;
3330 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3335 head = llapi_layout_get_by_path(fname, 0);
3338 "%s setstripe: cannot read layout from '%s': %s\n",
3339 progname, fname, strerror(errno));
3341 } else if (errno == ENODATA) {
3343 * file without LOVEA, this component-add will be turned
3344 * into a component-create.
3346 llapi_layout_free(head);
3350 * the new layout will be added to an empty one, it
3351 * still needs to be adjusted below
3355 } else if (!llapi_layout_is_composite(head)) {
3357 "%s setstripe: '%s' not a composite file\n",
3359 llapi_layout_free(head);
3363 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3366 "%s setstripe: cannot get prev extent: %s\n",
3367 progname, strerror(errno));
3368 llapi_layout_free(head);
3372 llapi_layout_free(head);
3376 /* Make sure we use the first component of the layout to be added. */
3377 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3380 "%s setstripe: cannot move component cursor: %s\n",
3381 progname, strerror(errno));
3385 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3387 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3388 progname, strerror(errno));
3392 if (start == 0 && end == 0) {
3393 rc = layout_extend_comp(layout, start, &end,
3394 comp_add ? prev_end : 0);
3399 if (start > prev_end || end < prev_end) {
3401 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3402 progname, start, end, prev_end);
3406 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3409 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3410 progname, prev_end, end, strerror(errno));
3417 static int mirror_adjust_first_extents(struct mirror_args *list)
3425 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3428 list = list->m_next;
3434 static inline bool arg_is_eof(char *arg)
3436 return !strncmp(arg, "-1", strlen("-1")) ||
3437 !strncmp(arg, "EOF", strlen("EOF")) ||
3438 !strncmp(arg, "eof", strlen("eof"));
3442 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3444 * Return: Valid mirror_args pointer on success and
3445 * NULL if memory allocation fails.
3447 static struct mirror_args *lfs_mirror_alloc(void)
3449 struct mirror_args *mirror = NULL;
3452 mirror = calloc(1, sizeof(*mirror));
3454 mirror->m_inherit = false;
3465 * lfs_mirror_free() - Free memory allocated for a mirror argument
3467 * @mirror: Previously allocated mirror argument structure by
3468 * lfs_mirror_alloc().
3470 * Free memory allocated for @mirror.
3474 static void lfs_mirror_free(struct mirror_args *mirror)
3476 if (mirror->m_layout)
3477 llapi_layout_free(mirror->m_layout);
3482 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3483 * @mirror_list: Previously allocated mirror list.
3485 * Free memory allocated for @mirror_list.
3489 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3491 struct mirror_args *next_mirror = NULL;
3493 while (mirror_list) {
3494 next_mirror = mirror_list->m_next;
3495 lfs_mirror_free(mirror_list);
3496 mirror_list = next_mirror;
3501 LFS_SETQUOTA_DELETE = (CHAR_MAX + 1),
3509 LFS_COMP_NO_VERIFY_OPT,
3511 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3513 LFS_MIRROR_STATE_OPT,
3515 LFS_MIRROR_INDEX_OPT,
3516 LFS_LAYOUT_FOREIGN_OPT,
3525 LFS_STATS_INTERVAL_OPT,
3530 #ifndef LCME_USER_MIRROR_FLAGS
3531 /* The mirror flags can be set by users at creation time. */
3532 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3536 static int lfs_setstripe_internal(int argc, char **argv,
3537 enum setstripe_origin opc)
3539 struct lfs_setstripe_args lsa = { 0 };
3540 struct llapi_stripe_param *param = NULL;
3541 struct find_param migrate_mdt_param = {
3551 unsigned long long size_units = 1;
3552 bool migrate_mode = false;
3553 bool migrate_mdt_mode = false;
3554 bool setstripe_mode = false;
3555 bool migration_block = false;
3556 __u64 migration_flags = 0;
3557 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3558 int comp_del = 0, comp_set = 0;
3561 struct llapi_layout *layout = NULL;
3562 struct llapi_layout **lpp = &layout;
3563 bool mirror_mode = false;
3564 bool has_m_file = false;
3565 __u32 mirror_count = 0;
3566 enum mirror_flags mirror_flags = 0;
3567 struct mirror_args *mirror_list = NULL;
3568 struct mirror_args *new_mirror = NULL;
3569 struct mirror_args *last_mirror = NULL;
3570 __u16 mirror_id = 0;
3572 bool from_yaml = false;
3573 bool from_copy = false;
3574 char *template = NULL;
3575 bool foreign_mode = false;
3577 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3578 char *mode_opt = NULL;
3579 mode_t previous_umask = 0;
3580 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3581 unsigned long long bandwidth_bytes_sec = 0;
3582 unsigned long long bandwidth_unit = ONE_MB;
3583 long stats_interval_sec = 0;
3585 struct option long_opts[] = {
3586 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3587 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3588 /* --block is only valid in migrate mode */
3589 { .val = 'b', .name = "block", .has_arg = no_argument },
3590 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3591 { .val = LFS_COMP_ADD_OPT,
3592 .name = "comp-add", .has_arg = no_argument },
3593 { .val = LFS_COMP_ADD_OPT,
3594 .name = "component-add", .has_arg = no_argument },
3595 { .val = LFS_COMP_DEL_OPT,
3596 .name = "comp-del", .has_arg = no_argument },
3597 { .val = LFS_COMP_DEL_OPT,
3598 .name = "component-del", .has_arg = no_argument },
3599 { .val = LFS_COMP_FLAGS_OPT,
3600 .name = "comp-flags", .has_arg = required_argument },
3601 { .val = LFS_COMP_FLAGS_OPT,
3602 .name = "component-flags",
3603 .has_arg = required_argument },
3604 { .val = LFS_COMP_SET_OPT,
3605 .name = "comp-set", .has_arg = no_argument },
3606 { .val = LFS_COMP_SET_OPT,
3607 .name = "component-set",
3608 .has_arg = no_argument},
3609 { .val = LFS_COMP_NO_VERIFY_OPT,
3610 .name = "no-verify", .has_arg = no_argument},
3611 { .val = LFS_LAYOUT_FLAGS_OPT,
3612 .name = "flags", .has_arg = required_argument},
3613 { .val = LFS_LAYOUT_FOREIGN_OPT,
3614 .name = "foreign", .has_arg = optional_argument},
3615 { .val = LFS_MIRROR_ID_OPT,
3616 .name = "mirror-id", .has_arg = required_argument},
3617 { .val = LFS_MODE_OPT,
3618 .name = "mode", .has_arg = required_argument},
3619 { .val = LFS_LAYOUT_COPY,
3620 .name = "copy", .has_arg = required_argument},
3621 { .val = LFS_STATS_OPT,
3622 .name = "stats", .has_arg = no_argument},
3623 { .val = LFS_STATS_INTERVAL_OPT,
3624 .name = "stats-interval",
3625 .has_arg = required_argument},
3626 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3627 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3628 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3629 { .val = 'C', .name = "overstripe-count",
3630 .has_arg = required_argument},
3631 { .val = 'd', .name = "delete", .has_arg = no_argument},
3632 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3633 /* used with "lfs migrate -m" */
3634 { .val = 'd', .name = "directory", .has_arg = no_argument},
3635 /* --non-direct is only valid in migrate mode */
3636 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3637 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3638 { .val = 'E', .name = "component-end",
3639 .has_arg = required_argument},
3640 { .val = 'f', .name = "file", .has_arg = required_argument },
3641 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3642 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3643 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3644 { .val = 'h', .name = "help", .has_arg = no_argument },
3645 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3646 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3647 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3648 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3649 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3650 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3651 { .val = 'L', .name = "layout", .has_arg = required_argument },
3652 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3653 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3654 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3655 /* --non-block is only valid in migrate mode */
3656 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3657 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3658 { .val = 'o', .name = "ost", .has_arg = required_argument },
3659 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3660 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3661 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3663 { .val = 'p', .name = "pool", .has_arg = required_argument },
3664 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3665 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3666 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3667 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3668 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3669 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3670 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3671 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3672 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3673 /* --verbose is only valid in migrate mode */
3674 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3675 { .val = 'W', .name = "bandwidth", .has_arg = required_argument },
3676 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3677 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3678 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3679 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3680 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3683 setstripe_args_init(&lsa);
3685 migrate_mode = (opc == SO_MIGRATE);
3686 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3687 setstripe_mode = (opc == SO_SETSTRIPE);
3688 if (opc == SO_MIRROR_DELETE) {
3690 mirror_flags = MF_DESTROY;
3693 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3695 while ((c = getopt_long(argc, argv,
3696 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:W:y:z:",
3697 long_opts, NULL)) >= 0) {
3703 case LFS_COMP_ADD_OPT:
3706 case LFS_COMP_DEL_OPT:
3709 case LFS_COMP_FLAGS_OPT:
3710 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3711 &lsa.lsa_comp_neg_flags);
3714 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3716 "%s: inverted flags are not supported\n",
3721 case LFS_COMP_SET_OPT:
3724 case LFS_COMP_NO_VERIFY_OPT:
3725 mirror_flags |= MF_NO_VERIFY;
3727 case LFS_MIRROR_ID_OPT: {
3728 unsigned long int id;
3731 id = strtoul(optarg, &end, 0);
3732 if (errno != 0 || *end != '\0' || id == 0 ||
3735 "%s %s: invalid mirror ID '%s'\n",
3736 progname, argv[0], optarg);
3740 mirror_id = (__u16)id;
3743 case LFS_LAYOUT_FLAGS_OPT: {
3746 /* check for numeric flags (foreign and mirror cases) */
3747 if (setstripe_mode && !mirror_mode && !last_mirror) {
3749 flags = strtoul(optarg, &end, 16);
3750 if (errno != 0 || *end != '\0' ||
3751 flags >= UINT32_MAX) {
3753 "%s %s: invalid hex flags '%s'\n",
3754 progname, argv[0], optarg);
3757 if (!foreign_mode) {
3759 "%s %s: hex flags must be specified with --foreign option\n",
3766 if (!mirror_mode || !last_mirror) {
3768 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3773 result = comp_str2flags(optarg, &last_mirror->m_flags,
3780 "%s: inverted flags are not supported\n",
3785 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3787 "%s: unsupported mirror flags: %s\n",
3794 case LFS_LAYOUT_FOREIGN_OPT:
3796 /* check pure numeric */
3797 type = strtoul(optarg, &end, 0);
3800 type = check_foreign_type_name(optarg);
3801 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3803 "%s %s: unrecognized foreign type '%s'\n",
3808 } else if (type >= UINT32_MAX) {
3810 "%s %s: invalid foreign type '%s'\n",
3811 progname, argv[0], optarg);
3815 foreign_mode = true;
3820 mode = strtoul(mode_opt, &end, 8);
3823 "%s %s: bad mode '%s'\n",
3824 progname, argv[0], mode_opt);
3827 previous_umask = umask(0);
3830 case LFS_LAYOUT_COPY:
3835 stats_interval_sec = 5;
3837 case LFS_STATS_INTERVAL_OPT:
3838 stats_interval_sec = strtol(optarg, &end, 0);
3839 if (stats_interval_sec == 0 && errno) {
3841 "%s %s: invalid stats interval %s\n",
3842 progname, argv[0], optarg);
3847 if (!migrate_mode) {
3849 "%s %s: -b|--block valid only for migrate command\n",
3853 migration_block = true;
3856 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3858 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3862 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3866 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3867 if (errno != 0 || *end != '\0'|| optarg == end ||
3868 lsa.lsa_stripe_count < -1 ||
3869 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3871 "%s %s: invalid stripe count '%s'\n",
3872 progname, argv[0], optarg);
3876 if (lsa.lsa_stripe_count == -1)
3877 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3881 migrate_mdt_param.fp_max_depth = 1;
3883 /* delete the default striping pattern */
3885 if (opc == SO_MIRROR_SPLIT) {
3888 "%s %s: -d cannot used with -f\n",
3892 mirror_flags |= MF_DESTROY;
3897 if (!migrate_mode) {
3899 "%s %s: -D|--non-direct is valid only for migrate command\n",
3903 migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3906 if (lsa.lsa_comp_end != 0) {
3907 result = comp_args_to_layout(lpp, &lsa, true);
3909 fprintf(stderr, "%s: invalid layout\n",
3914 setstripe_args_init_inherit(&lsa);
3917 if (arg_is_eof(optarg)) {
3918 lsa.lsa_comp_end = LUSTRE_EOF;
3920 result = llapi_parse_size(optarg,
3923 /* assume units of KB if too small */
3924 if (lsa.lsa_comp_end < 4096)
3925 lsa.lsa_comp_end *= 1024;
3927 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3929 "%s %s: invalid component end '%s'\n",
3930 progname, argv[0], optarg);
3936 if (!migrate_mode) {
3938 "--mdt-hash is valid only for migrate command\n");
3942 lsa.lsa_pattern = check_hashtype(optarg);
3943 if (lsa.lsa_pattern == 0) {
3945 "%s %s: bad stripe hash type '%s'\n",
3946 progname, argv[0], optarg);
3952 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3953 if (errno != 0 || *end != '\0' || optarg == end ||
3954 lsa.lsa_stripe_off < -1 ||
3955 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3957 "%s %s: invalid stripe offset '%s'\n",
3958 progname, argv[0], optarg);
3961 if (lsa.lsa_stripe_off == -1)
3962 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3965 comp_id = strtoul(optarg, &end, 0);
3966 if (*end != '\0' || comp_id == 0 ||
3967 comp_id > LCME_ID_MAX) {
3969 "%s %s: invalid component ID '%s'\n",
3970 progname, argv[0], optarg);
3975 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3977 "error: %s: invalid option: %s\n",
3978 progname, argv[optopt + 1]);
3981 if (opc == SO_MIRROR_EXTEND) {
3984 "error: %s: '-N' must exist in front of '%s'\n",
3985 progname, argv[optopt + 1]);
3988 last_mirror->m_file = optarg;
3989 last_mirror->m_count = 1;
3993 mirror_list = lfs_mirror_alloc();
3994 mirror_list->m_file = optarg;
3999 if (strcmp(argv[optind - 1], "mdt") == 0) {
4000 /* Can be only the first component */
4004 "error: 'mdt' layout can be only the first one\n");
4007 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
4010 "error: 'mdt' layout size is too big\n");
4013 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
4014 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
4015 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
4018 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
4024 if (!migrate_mode) {
4026 "%s %s: -m|--mdt-index is valid only for migrate command\n",
4030 migrate_mdt_mode = true;
4031 lsa.lsa_nr_tgts = parse_targets(tgts,
4032 sizeof(tgts) / sizeof(__u32),
4033 lsa.lsa_nr_tgts, optarg, NULL);
4034 if (lsa.lsa_nr_tgts < 0) {
4036 "%s: invalid MDT target(s) '%s'\n",
4041 lsa.lsa_tgts = tgts;
4042 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4043 lsa.lsa_stripe_off = tgts[0];
4046 if (!migrate_mode) {
4048 "%s %s: -n|--non-block valid only for migrate command\n",
4052 migration_flags |= LLAPI_MIGRATION_NONBLOCK;
4055 if (opc == SO_SETSTRIPE) {
4056 opc = SO_MIRROR_CREATE;
4062 mirror_count = strtoul(optarg, &end, 0);
4063 if (errno != 0 || *end != '\0' ||
4064 mirror_count == 0 ||
4065 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
4067 "error: %s: bad mirror count: %s\n",
4074 new_mirror = lfs_mirror_alloc();
4075 new_mirror->m_count = mirror_count;
4078 mirror_list = new_mirror;
4081 /* wrap up last mirror */
4082 if (!setstripe_args_specified(&lsa))
4083 last_mirror->m_inherit = true;
4084 if (lsa.lsa_comp_end == 0)
4085 lsa.lsa_comp_end = LUSTRE_EOF;
4087 result = comp_args_to_layout(lpp, &lsa, true);
4089 lfs_mirror_free(new_mirror);
4093 setstripe_args_init_inherit(&lsa);
4095 last_mirror->m_next = new_mirror;
4098 last_mirror = new_mirror;
4099 lpp = &last_mirror->m_layout;
4102 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4103 if (strcmp(argv[optind - 1], "--ost-list") == 0)
4105 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
4107 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
4109 "%s %s: -o|--ost incompatible with DoM layout\n",
4114 * -o allows overstriping, and must note it because
4115 * parse_targets is shared with MDT striping, which
4116 * does not allow duplicates
4118 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
4119 lsa.lsa_nr_tgts = parse_targets(tgts,
4120 sizeof(tgts) / sizeof(__u32),
4121 lsa.lsa_nr_tgts, optarg,
4123 if (lsa.lsa_nr_tgts < 0) {
4125 "%s %s: invalid OST target(s) '%s'\n",
4126 progname, argv[0], optarg);
4130 lsa.lsa_tgts = tgts;
4131 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4132 lsa.lsa_stripe_off = tgts[0];
4138 if (optarg[0] == '\0' || lov_pool_is_inherited(optarg))
4139 lsa.lsa_pool_name = NULL;
4141 lsa.lsa_pool_name = optarg;
4144 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
4146 /* assume units of KB if too small to be valid */
4147 if (lsa.lsa_stripe_size < 4096)
4148 lsa.lsa_stripe_size *= 1024;
4150 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
4152 "%s %s: invalid stripe size '%s'\n",
4153 progname, argv[0], optarg);
4158 if (!migrate_mode) {
4160 "%s %s: -v|--verbose valid only for migrate command\n",
4164 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
4165 migration_flags = LLAPI_MIGRATION_VERBOSE;
4171 if (!migrate_mode && !mirror_mode) {
4173 "--bandwidth is valid only for migrate and mirror mode\n");
4176 if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
4177 &bandwidth_unit, 0) < 0) {
4179 "error: %s: bad value for bandwidth '%s'\n",
4189 result = llapi_parse_size(optarg,
4190 &lsa.lsa_extension_size,
4194 "%s %s: invalid extension size '%s'\n",
4195 progname, argv[0], optarg);
4199 lsa.lsa_extension_comp = true;
4202 fprintf(stderr, "%s: unrecognized option '%s'\n",
4203 progname, argv[optind - 1]);
4209 fname = argv[optind];
4211 if (optind == argc) {
4212 fprintf(stderr, "%s %s: FILE must be specified\n",
4217 /* lfs migrate $filename should keep the file's layout by default */
4218 if (migrate_mode && !layout && !from_yaml &&
4219 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4222 if (xattr && !foreign_mode) {
4224 * only print a warning as this is harmless and will be ignored
4227 "%s %s: xattr has been specified for non-foreign layout\n",
4229 } else if (foreign_mode && !xattr) {
4231 "%s %s: xattr must be provided in foreign mode\n",
4236 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4237 comp_set || comp_id || delete || from_copy ||
4238 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4241 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4246 if (mirror_mode && mirror_count == 0) {
4248 "error: %s: --mirror-count|-N option is required\n",
4255 if (!setstripe_args_specified(&lsa))
4256 last_mirror->m_inherit = true;
4257 if (lsa.lsa_comp_end == 0)
4258 lsa.lsa_comp_end = LUSTRE_EOF;
4261 if (lsa.lsa_comp_end != 0) {
4262 result = comp_args_to_layout(lpp, &lsa, true);
4264 fprintf(stderr, "error: %s: invalid layout\n",
4271 if (mirror_flags & MF_NO_VERIFY) {
4272 if (opc != SO_MIRROR_EXTEND) {
4274 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4278 } else if (!has_m_file) {
4280 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4287 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4289 "%s %s: --component-set doesn't have component-id set\n",
4294 if ((delete + comp_set + comp_del + comp_add) > 1) {
4296 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4301 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4302 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4304 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4309 if ((comp_set || comp_del) &&
4310 (setstripe_args_specified(&lsa) || layout != NULL)) {
4312 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4317 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4319 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4324 if (comp_add || comp_del) {
4327 result = lstat(fname, &st);
4328 if (result == 0 && S_ISDIR(st.st_mode)) {
4330 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4337 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4346 "%s %s: option -E must be specified with --component-add\n",
4352 if (from_yaml && from_copy) {
4354 "%s: can't specify --yaml and --copy together\n",
4359 if ((from_yaml || from_copy) &&
4360 (setstripe_args_specified(&lsa) || layout != NULL)) {
4362 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4367 if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4369 "%s %s: options --non-block and --block are mutually exclusive\n",
4374 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4375 opc != SO_MIRROR_DELETE && comp_id != 0) {
4377 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4382 if (migrate_mdt_mode) {
4383 struct lmv_user_md *lmu;
4385 /* initialize migrate mdt parameters */
4386 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4387 LMV_USER_MAGIC_SPECIFIC));
4390 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4391 progname, argv[0], strerror(ENOMEM));
4395 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4396 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4397 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4399 "%s %s: migrate should specify MDT index\n",
4404 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4405 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4406 lmu->lum_hash_type = lsa.lsa_pattern;
4408 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4409 if (lsa.lsa_pool_name) {
4410 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4411 sizeof(lmu->lum_pool_name) - 1);
4412 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4414 if (lsa.lsa_nr_tgts > 1) {
4417 if (lsa.lsa_stripe_count > 0 &&
4418 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4419 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4421 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4422 progname, lsa.lsa_stripe_count,
4428 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4429 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4430 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4431 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4433 lmu->lum_magic = LMV_USER_MAGIC;
4436 migrate_mdt_param.fp_lmv_md = lmu;
4437 migrate_mdt_param.fp_migrate = 1;
4438 } else if (!layout) {
4439 if (lsa_args_stripe_count_check(&lsa))
4442 /* initialize stripe parameters */
4443 param = calloc(1, offsetof(typeof(*param),
4444 lsp_osts[lsa.lsa_nr_tgts]));
4447 "%s %s: cannot allocate memory for parameters: %s\n",
4448 progname, argv[0], strerror(ENOMEM));
4453 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4454 param->lsp_stripe_size = lsa.lsa_stripe_size;
4455 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4456 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4457 param->lsp_stripe_count = -1;
4459 param->lsp_stripe_count = lsa.lsa_stripe_count;
4461 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4462 param->lsp_stripe_offset = -1;
4464 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4465 param->lsp_stripe_pattern =
4466 llapi_pattern_to_lov(lsa.lsa_pattern);
4467 if (param->lsp_stripe_pattern == EINVAL) {
4468 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4473 param->lsp_pool = lsa.lsa_pool_name;
4474 param->lsp_is_specific = false;
4476 if (lsa.lsa_nr_tgts > 0) {
4477 param->lsp_is_specific = true;
4478 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4479 memcpy(param->lsp_osts, tgts,
4480 sizeof(*tgts) * lsa.lsa_nr_tgts);
4485 /* generate a layout from a YAML template */
4486 result = lfs_comp_create_from_yaml(template, &layout,
4490 "error: %s: can't create composite layout from template file %s\n",
4496 if (layout != NULL || mirror_list != NULL) {
4498 result = mirror_adjust_first_extents(mirror_list);
4500 result = layout_adjust_first_extent(fname, layout,
4502 if (result == -ENODATA)
4504 else if (result != 0) {
4505 fprintf(stderr, "error: %s: invalid layout\n",
4511 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4513 layout = llapi_layout_get_by_path(template ?: fname, 0);
4516 "%s: can't create composite layout from file %s: %s\n",
4517 progname, template ?: fname,
4524 if (migrate_mdt_mode) {
4525 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4526 } else if (migrate_mode) {
4529 * Strip the source layout of specific
4530 * OST object/index values.
4532 result = llapi_layout_ost_index_set(layout, 0,
4533 LLAPI_LAYOUT_DEFAULT);
4536 "%s: set default ost index failed: %s\n",
4537 progname, strerror(errno));
4543 result = lfs_migrate(fname, migration_flags, param,
4544 layout, bandwidth_bytes_sec,
4545 stats_interval_sec);
4546 } else if (comp_set != 0) {
4547 result = lfs_component_set(fname, comp_id,
4550 lsa.lsa_comp_neg_flags);
4551 } else if (comp_del != 0) {
4552 result = lfs_component_del(fname, comp_id,
4554 lsa.lsa_comp_neg_flags);
4555 } else if (comp_add != 0) {
4556 result = lfs_component_add(fname, layout);
4557 } else if (opc == SO_MIRROR_CREATE) {
4558 result = mirror_create(fname, mirror_list);
4559 } else if (opc == SO_MIRROR_EXTEND) {
4560 result = mirror_extend(fname, mirror_list,
4562 bandwidth_bytes_sec,
4563 stats_interval_sec);
4564 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4565 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4567 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4571 if (lsa.lsa_pool_name)
4572 mirror_flags |= MF_COMP_POOL;
4573 else if (mirror_id != 0)
4574 comp_id = mirror_id;
4576 mirror_flags |= MF_COMP_ID;
4577 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4579 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4583 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4585 has_m_file ? mirror_list->m_file :
4587 } else if (layout) {
4588 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4594 } else if (foreign_mode) {
4595 result = llapi_file_create_foreign(fname, mode, type,
4602 result = llapi_file_open_param(fname,
4611 /* Save the first error encountered. */
4619 umask(previous_umask);
4622 free(migrate_mdt_param.fp_lmv_md);
4623 llapi_layout_free(layout);
4624 lfs_mirror_list_free(mirror_list);
4629 llapi_layout_free(layout);
4630 lfs_mirror_list_free(mirror_list);
4634 static int lfs_poollist(int argc, char **argv)
4639 return llapi_poollist(argv[1]);
4642 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4643 static int set_time(struct find_param *param, time_t *time, time_t *set,
4648 char *endptr = "AD";
4653 else if (str[0] == '-')
4659 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4660 long long val = strtoll(timebuf, &endptr, 0);
4665 unit *= 52; /* 52 weeks + 1 day below */
4669 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4670 param->fp_time_margin *= (1 + unit / 52);
4671 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4673 case '\0': /* days are default unit if none used */
4686 /* don't need to multiply by 1 for seconds */
4689 "%s find: bad time string '%s': %s\n",
4690 progname, timebuf, strerror(EINVAL));
4694 if (param->fp_time_margin == 0 ||
4695 (*endptr && unit < param->fp_time_margin))
4696 param->fp_time_margin = unit;
4703 fprintf(stderr, "%s find: bad time '%s': too large\n",
4713 static int str2quotaid(__u32 *id, const char *arg)
4715 unsigned long int projid_tmp = 0;
4716 char *endptr = NULL;
4718 projid_tmp = strtoul(arg, &endptr, 10);
4719 if (*endptr != '\0')
4721 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4722 if (projid_tmp >= UINT32_MAX)
4729 static int name2uid(unsigned int *id, const char *name)
4731 struct passwd *passwd;
4733 passwd = getpwnam(name);
4736 *id = passwd->pw_uid;
4741 static int name2gid(unsigned int *id, const char *name)
4743 struct group *group;
4745 group = getgrnam(name);
4748 *id = group->gr_gid;
4753 static inline int name2projid(unsigned int *id, const char *name)
4758 static int uid2name(char **name, unsigned int id)
4760 struct passwd *passwd;
4762 passwd = getpwuid(id);
4765 *name = passwd->pw_name;
4770 static inline int gid2name(char **name, unsigned int id)
4772 struct group *group;
4774 group = getgrgid(id);
4777 *name = group->gr_name;
4782 static int name2layout(__u32 *layout, char *name)
4784 char *ptr, *layout_name;
4787 for (ptr = name; ; ptr = NULL) {
4788 layout_name = strtok(ptr, ",");
4791 if (strcmp(layout_name, "released") == 0)
4792 *layout |= LOV_PATTERN_F_RELEASED;
4793 else if (strcmp(layout_name, "raid0") == 0)
4794 *layout |= LOV_PATTERN_RAID0;
4795 else if (strcmp(layout_name, "mdt") == 0)
4796 *layout |= LOV_PATTERN_MDT;
4797 else if (strcmp(layout_name, "overstriping") == 0)
4798 *layout |= LOV_PATTERN_OVERSTRIPING;
4805 static int name2attrs(char *name, __u64 *attrs, __u64 *neg_attrs)
4807 char *ptr, *attr_name = name;
4808 struct attrs_name *ap;
4809 int islongopt = 0; /* 1 true; 0 not known yet; -1 false. */
4814 if (strchr(name, ','))
4817 for (ptr = name; ; ptr = NULL) {
4818 if (islongopt != -1)
4819 attr_name = strtok(ptr, ",");
4821 attr_name = attr_name + 1;
4822 if (!attr_name || *attr_name == '\0')
4825 for (ap = (struct attrs_name *)attrs_array;
4828 if (islongopt != -1 &&
4829 strcmp(attr_name, ap->an_name) == 0) {
4830 *attrs |= ap->an_attr;
4833 } else if (islongopt != -1 && attr_name[0] == '^' &&
4834 strcmp(attr_name + 1, ap->an_name) == 0) {
4835 *neg_attrs |= ap->an_attr;
4838 } else if (islongopt != 1 &&
4839 *attr_name == ap->an_shortname) {
4840 *attrs |= ap->an_attr;
4843 } else if (islongopt != 1 && *attr_name == '^' &&
4844 attr_name[1] == ap->an_shortname) {
4845 *neg_attrs |= ap->an_attr;
4852 if (ap->an_attr == 0) {
4853 /* provided attr is unknown */
4854 fprintf(stderr, "error: bad attribute name '%s'\n",
4862 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4865 int user, group, other;
4870 mode_t previous_flags;
4872 user = group = other = 0;
4892 user = group = other = 1;
4903 who = user || group || other;
4906 usermask = umask(0022);
4911 if (*input == '-' || *input == '+' || *input == '=')
4914 /* operation is required */
4917 /* get the flags in *outmode */
4920 previous_flags = (*outmode & 0700);
4921 perm |= user ? previous_flags : 0;
4922 perm |= group ? (previous_flags >> 3) : 0;
4923 perm |= other ? (previous_flags >> 6) : 0;
4927 previous_flags = (*outmode & 0070);
4928 perm |= user ? (previous_flags << 3) : 0;
4929 perm |= group ? previous_flags : 0;
4930 perm |= other ? (previous_flags >> 3) : 0;
4934 previous_flags = (*outmode & 0007);
4935 perm |= user ? (previous_flags << 6) : 0;
4936 perm |= group ? (previous_flags << 3) : 0;
4937 perm |= other ? previous_flags : 0;
4944 /* this part is optional,
4945 * if empty perm = 0 and *outmode is not modified
4952 perm |= user ? 0400 : 0;
4953 perm |= group ? 0040 : 0;
4954 perm |= other ? 0004 : 0;
4955 /* set read permission for uog except for umask's
4958 perm |= who ? 0 : (0444 & ~usermask);
4961 perm |= user ? 0200 : 0;
4962 perm |= group ? 0020 : 0;
4963 perm |= other ? 0002 : 0;
4964 /* set write permission for uog except for umask'
4967 perm |= who ? 0 : (0222 & ~usermask);
4970 perm |= user ? 0100 : 0;
4971 perm |= group ? 0010 : 0;
4972 perm |= other ? 0001 : 0;
4973 /* set execute permission for uog except for umask'
4976 perm |= who ? 0 : (0111 & ~usermask);
4980 * Adds execute permission to 'u', 'g' and/or 'g' if
4981 * specified and either 'u', 'g' or 'o' already has
4982 * execute permissions.
4984 if ((*outmode & 0111) != 0) {
4985 perm |= user ? 0100 : 0;
4986 perm |= group ? 0010 : 0;
4987 perm |= other ? 0001 : 0;
4988 perm |= !who ? 0111 : 0;
4992 /* s is ignored if o is given, but it's not an error */
4993 if (other && !group && !user)
4995 perm |= user ? S_ISUID : 0;
4996 perm |= group ? S_ISGID : 0;
4999 /* 't' should be used when 'a' is given
5002 perm |= (!who || all) ? S_ISVTX : 0;
5003 /* using ugo with t is not an error */
5014 /* uog flags should be only one character long */
5015 if (previous_flags && (*input != '\0' && *input != ','))
5020 /* remove the flags from outmode */
5024 /* add the flags to outmode */
5028 /* set the flags of outmode to perm */
5038 static int str2mode_t(const char *input, mode_t *outmode)
5045 if (*input >= '0' && *input <= '7') {
5046 /* parse octal representation */
5051 /* look for invalid digits in octal representation */
5052 while (isdigit(*iter))
5057 *outmode = strtoul(input, &end, 8);
5059 if (errno != 0 || *outmode > 07777) {
5064 } else if (*input == '8' || *input == '9') {
5065 /* error: invalid octal number */
5068 /* parse coma seperated list of symbolic representation */
5077 rc = parse_symbolic(input, outmode, &end);
5082 } while (*end == ',');
5090 static int lfs_find(int argc, char **argv)
5095 struct find_param param = {
5098 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
5100 struct option long_opts[] = {
5101 { .val = 'A', .name = "atime", .has_arg = required_argument },
5102 { .val = LFS_ATTRS_OPT,
5103 .name = "attrs", .has_arg = required_argument },
5104 { .val = 'b', .name = "blocks", .has_arg = required_argument },
5105 { .val = 'B', .name = "btime", .has_arg = required_argument },
5106 { .val = 'B', .name = "Btime", .has_arg = required_argument },
5107 { .val = LFS_COMP_COUNT_OPT,
5108 .name = "comp-count", .has_arg = required_argument },
5109 { .val = LFS_COMP_COUNT_OPT,
5110 .name = "component-count",
5111 .has_arg = required_argument },
5112 { .val = LFS_COMP_FLAGS_OPT,
5113 .name = "comp-flags", .has_arg = required_argument },
5114 { .val = LFS_COMP_FLAGS_OPT,
5115 .name = "component-flags",
5116 .has_arg = required_argument },
5117 { .val = LFS_COMP_START_OPT,
5118 .name = "comp-start", .has_arg = required_argument },
5119 { .val = LFS_COMP_START_OPT,
5120 .name = "component-start",
5121 .has_arg = required_argument },
5122 { .val = LFS_MIRROR_STATE_OPT,
5123 .name = "mirror-state", .has_arg = required_argument },
5124 { .val = LFS_NEWERXY_OPT,
5125 .name = "newer", .has_arg = required_argument},
5126 { .val = LFS_NEWERXY_OPT,
5127 .name = "neweraa", .has_arg = required_argument},
5128 { .val = LFS_NEWERXY_OPT,
5129 .name = "neweram", .has_arg = required_argument},
5130 { .val = LFS_NEWERXY_OPT,
5131 .name = "newerac", .has_arg = required_argument},
5132 { .val = LFS_NEWERXY_OPT,
5133 .name = "newerab", .has_arg = required_argument},
5134 { .val = LFS_NEWERXY_OPT,
5135 .name = "newerma", .has_arg = required_argument},
5136 { .val = LFS_NEWERXY_OPT,
5137 .name = "newermm", .has_arg = required_argument},
5138 { .val = LFS_NEWERXY_OPT,
5139 .name = "newermc", .has_arg = required_argument},
5140 { .val = LFS_NEWERXY_OPT,
5141 .name = "newermb", .has_arg = required_argument},
5142 { .val = LFS_NEWERXY_OPT,
5143 .name = "newerca", .has_arg = required_argument},
5144 { .val = LFS_NEWERXY_OPT,
5145 .name = "newercm", .has_arg = required_argument},
5146 { .val = LFS_NEWERXY_OPT,
5147 .name = "newercc", .has_arg = required_argument},
5148 { .val = LFS_NEWERXY_OPT,
5149 .name = "newercb", .has_arg = required_argument},
5150 { .val = LFS_NEWERXY_OPT,
5151 .name = "newerba", .has_arg = required_argument},
5152 { .val = LFS_NEWERXY_OPT,
5153 .name = "newerbm", .has_arg = required_argument},
5154 { .val = LFS_NEWERXY_OPT,
5155 .name = "newerbc", .has_arg = required_argument},
5156 { .val = LFS_NEWERXY_OPT,
5157 .name = "newerbb", .has_arg = required_argument},
5158 { .val = LFS_NEWERXY_OPT,
5159 .name = "newerBa", .has_arg = required_argument},
5160 { .val = LFS_NEWERXY_OPT,
5161 .name = "newerBm", .has_arg = required_argument},
5162 { .val = LFS_NEWERXY_OPT,
5163 .name = "newerBc", .has_arg = required_argument},
5164 { .val = LFS_NEWERXY_OPT,
5165 .name = "newerBB", .has_arg = required_argument},
5166 { .val = LFS_NEWERXY_OPT,
5167 .name = "newerat", .has_arg = required_argument},
5168 { .val = LFS_NEWERXY_OPT,
5169 .name = "newermt", .has_arg = required_argument},
5170 { .val = LFS_NEWERXY_OPT,
5171 .name = "newerct", .has_arg = required_argument},
5172 { .val = LFS_NEWERXY_OPT,
5173 .name = "newerbt", .has_arg = required_argument},
5174 { .val = LFS_NEWERXY_OPT,
5175 .name = "newerBt", .has_arg = required_argument},
5176 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
5177 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
5178 { .val = 'C', .name = "ctime", .has_arg = required_argument },
5179 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
5180 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
5181 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
5182 { .val = 'E', .name = "component-end",
5183 .has_arg = required_argument },
5184 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
5185 { .val = LFS_LAYOUT_FOREIGN_OPT,
5186 .name = "foreign", .has_arg = optional_argument},
5187 { .val = 'g', .name = "gid", .has_arg = required_argument },
5188 { .val = 'G', .name = "group", .has_arg = required_argument },
5189 { .val = 'h', .name = "help", .has_arg = no_argument },
5190 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5191 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
5192 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
5193 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
5194 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5195 { .val = 'L', .name = "layout", .has_arg = required_argument },
5196 { .val = LFS_LINKS_OPT,
5197 .name = "links", .has_arg = required_argument },
5198 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5199 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5200 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
5201 { .val = 'M', .name = "mtime", .has_arg = required_argument },
5202 { .val = 'n', .name = "name", .has_arg = required_argument },
5203 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
5204 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
5205 { .val = 'O', .name = "obd", .has_arg = required_argument },
5206 { .val = 'O', .name = "ost", .has_arg = required_argument },
5207 { .val = LFS_FIND_PERM,
5208 .name = "perm", .has_arg = required_argument },
5209 /* no short option for pool yet, can be 'p' after 2.18 */
5210 { .val = LFS_POOL_OPT,
5211 .name = "pool", .has_arg = required_argument },
5212 { .val = '0', .name = "print0", .has_arg = no_argument },
5213 { .val = 'P', .name = "print", .has_arg = no_argument },
5214 { .val = LFS_PRINTF_OPT,
5215 .name = "printf", .has_arg = required_argument },
5216 { .val = LFS_PROJID_OPT,
5217 .name = "projid", .has_arg = required_argument },
5218 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
5219 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
5220 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
5221 { .val = 's', .name = "size", .has_arg = required_argument },
5222 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
5223 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
5224 { .val = 't', .name = "type", .has_arg = required_argument },
5225 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5226 { .val = 'u', .name = "uid", .has_arg = required_argument },
5227 { .val = 'U', .name = "user", .has_arg = required_argument },
5228 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
5229 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
5230 { .val = 'z', .name = "extension-size",
5231 .has_arg = required_argument },
5232 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
5234 int prev_optind = optind;
5247 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
5248 while ((c = getopt_long_only(argc, argv,
5249 "-0A:b:B:c:C:D:E:g:G:hH:i:lL:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
5250 long_opts, &optidx)) >= 0) {
5255 /* '!' is part of option */
5257 * when getopt_long_only() finds a string which is not
5258 * an option nor a known option argument it returns 1
5259 * in that case if we already have found pathstart and pathend
5260 * (i.e. we have the list of pathnames),
5261 * the only supported value is "!"
5263 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5264 if (!isoption && pathend != -1) {
5266 "err: %s: filename|dirname must either precede options or follow options\n",
5271 if (!isoption && pathstart == -1)
5272 pathstart = prev_optind;
5273 if (isoption && pathstart != -1 && pathend == -1)
5274 pathend = prev_optind;
5276 prev_optind = optind;
5284 * unknown; opt is "!" or path component,
5285 * checking done above.
5287 if (strcmp(optarg, "!") == 0)
5291 xtime = ¶m.fp_atime;
5292 xsign = ¶m.fp_asign;
5293 param.fp_exclude_atime = !!neg_opt;
5294 /* no break, this falls through to 'B' for btime */
5298 xtime = ¶m.fp_btime;
5299 xsign = ¶m.fp_bsign;
5300 param.fp_exclude_btime = !!neg_opt;
5302 /* no break, this falls through to 'C' for ctime */
5306 xtime = ¶m.fp_ctime;
5307 xsign = ¶m.fp_csign;
5308 param.fp_exclude_ctime = !!neg_opt;
5310 /* no break, this falls through to 'M' for mtime */
5314 xtime = ¶m.fp_mtime;
5315 xsign = ¶m.fp_msign;
5316 param.fp_exclude_mtime = !!neg_opt;
5318 rc = set_time(¶m, &t, xtime, optarg);
5319 if (rc == INT_MAX) {
5327 ret = name2attrs(optarg, ¶m.fp_attrs,
5328 ¶m.fp_neg_attrs);
5331 param.fp_exclude_attrs = !!neg_opt;
5334 if (optarg[0] == '+') {
5335 param.fp_blocks_sign = -1;
5337 } else if (optarg[0] == '-') {
5338 param.fp_blocks_sign = 1;
5342 param.fp_blocks_units = 512;
5343 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5344 ¶m.fp_blocks_units, 0);
5346 fprintf(stderr, "error: bad blocks '%s'\n",
5350 param.fp_check_blocks = 1;
5351 param.fp_exclude_blocks = !!neg_opt;
5353 case LFS_COMP_COUNT_OPT:
5354 if (optarg[0] == '+') {
5355 param.fp_comp_count_sign = -1;
5357 } else if (optarg[0] == '-') {
5358 param.fp_comp_count_sign = 1;
5363 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5364 if (errno != 0 || *endptr != '\0' ||
5365 param.fp_comp_count > UINT32_MAX) {
5367 "error: bad component count '%s'\n",
5371 param.fp_check_comp_count = 1;
5372 param.fp_exclude_comp_count = !!neg_opt;
5374 case LFS_COMP_FLAGS_OPT:
5375 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5376 ¶m.fp_comp_neg_flags);
5379 "error: bad component flags '%s'\n",
5383 param.fp_check_comp_flags = 1;
5385 __u32 flags = param.fp_comp_neg_flags;
5387 param.fp_comp_neg_flags = param.fp_comp_flags;
5388 param.fp_comp_flags = flags;
5391 case LFS_COMP_START_OPT:
5392 if (optarg[0] == '+') {
5393 param.fp_comp_start_sign = -1;
5395 } else if (optarg[0] == '-') {
5396 param.fp_comp_start_sign = 1;
5400 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5401 ¶m.fp_comp_start_units, 0);
5404 "error: bad component start '%s'\n",
5408 param.fp_check_comp_start = 1;
5409 param.fp_exclude_comp_start = !!neg_opt;
5411 case LFS_MIRROR_STATE_OPT:
5412 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5413 ¶m.fp_mirror_neg_state);
5416 "error: bad mirrored file state '%s'\n",
5420 param.fp_check_mirror_state = 1;
5422 __u16 state = param.fp_mirror_neg_state;
5424 param.fp_mirror_neg_state =
5425 param.fp_mirror_state;
5426 param.fp_mirror_state = state;
5430 if (optarg[0] == '+') {
5431 param.fp_stripe_count_sign = -1;
5433 } else if (optarg[0] == '-') {
5434 param.fp_stripe_count_sign = 1;
5439 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5440 if (errno != 0 || *endptr != '\0' ||
5441 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5443 "error: bad stripe_count '%s'\n",
5448 param.fp_check_stripe_count = 1;
5449 param.fp_exclude_stripe_count = !!neg_opt;
5453 param.fp_max_depth = strtol(optarg, 0, 0);
5454 if (errno != 0 || param.fp_max_depth < 0) {
5456 "error: bad maxdepth '%s'\n",
5463 if (optarg[0] == '+') {
5464 param.fp_comp_end_sign = -1;
5466 } else if (optarg[0] == '-') {
5467 param.fp_comp_end_sign = 1;
5471 if (arg_is_eof(optarg)) {
5472 param.fp_comp_end = LUSTRE_EOF;
5473 param.fp_comp_end_units = 1;
5476 rc = llapi_parse_size(optarg,
5478 ¶m.fp_comp_end_units, 0);
5479 /* assume units of KB if too small */
5480 if (param.fp_comp_end < 4096)
5481 param.fp_comp_end *= 1024;
5485 "error: bad component end '%s'\n",
5489 param.fp_check_comp_end = 1;
5490 param.fp_exclude_comp_end = !!neg_opt;
5492 case LFS_LAYOUT_FOREIGN_OPT: {
5493 /* all types by default */
5494 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5497 /* check pure numeric */
5498 type = strtoul(optarg, &endptr, 0);
5501 type = check_foreign_type_name(optarg);
5502 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5504 "%s %s: unknown foreign type '%s'\n",
5509 } else if (type >= UINT32_MAX) {
5511 "%s %s: invalid foreign type '%s'\n",
5512 progname, argv[0], optarg);
5516 param.fp_foreign_type = type;
5517 param.fp_check_foreign = 1;
5518 param.fp_exclude_foreign = !!neg_opt;
5521 case LFS_NEWERXY_OPT: {
5527 time_t ref = time(NULL);
5529 /* no need to check bad options, they won't get here */
5530 if (strlen(long_opts[optidx].name) == 7) {
5531 x = long_opts[optidx].name[5];
5532 y = long_opts[optidx].name[6];
5536 static const char *const fmts[] = {
5537 "%Y-%m-%d %H:%M:%S",
5540 "%H:%M:%S", /* sometime today */
5549 for (i = 0; fmts[i] != NULL; i++) {
5552 /* Init for times relative to today */
5553 if (strncmp(fmts[i], "%H", 2) == 0) {
5554 localtime_r(&ref, &tm);
5556 memset(&tm, 0, sizeof(tm));
5559 ptr = strptime(optarg, fmts[i], &tm);
5561 while (ptr && isspace(*ptr))
5563 if (ptr == optarg + strlen(optarg)) {
5571 "%s: invalid time '%s'\n",
5574 "supported formats are:\n ");
5575 for (i = 0; fmts[i] != NULL; i++)
5576 fprintf(stderr, "'%s', ",
5578 fprintf(stderr, "\n");
5584 } else if (y == 'b' || y == 'B') {
5587 rc = llapi_get_lum_file(optarg, NULL, &stx,
5589 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5590 if (!(stx.stx_mask & STATX_BTIME))
5595 "%s: get btime failed '%s': %s\n",
5601 ref = stx.stx_btime.tv_sec;
5603 struct stat statbuf;
5605 if (stat(optarg, &statbuf) < 0) {
5607 "%s: cannot stat file '%s': %s\n",
5616 ref = statbuf.st_atime;
5619 ref = statbuf.st_mtime;
5622 ref = statbuf.st_ctime;
5626 "%s: invalid Y argument: '%c'\n",
5635 xidx = NEWERXY_ATIME;
5638 xidx = NEWERXY_MTIME;
5641 xidx = NEWERXY_CTIME;
5645 xidx = NEWERXY_BTIME;
5649 "%s: invalid X argument: '%c'\n",
5656 newery = ¶m.fp_newery[xidx][negidx];
5662 *newery = *newery > ref ? ref : *newery;
5664 *newery = *newery > ref ? *newery : ref;
5666 param.fp_newerxy = 1;
5671 rc = name2gid(¶m.fp_gid, optarg);
5673 if (str2quotaid(¶m.fp_gid, optarg)) {
5675 "Group/GID: %s cannot be found.\n",
5681 param.fp_exclude_gid = !!neg_opt;
5682 param.fp_check_gid = 1;
5685 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5686 ¶m.fp_hash_exflags,
5687 ¶m.fp_hash_type);
5692 if (param.fp_hash_inflags || param.fp_hash_exflags)
5693 param.fp_check_hash_flag = 1;
5694 param.fp_exclude_hash_type = !!neg_opt;
5700 ret = name2layout(¶m.fp_layout, optarg);
5703 param.fp_exclude_layout = !!neg_opt;
5704 param.fp_check_layout = 1;
5707 if (optarg[0] == '+') {
5708 param.fp_nlink_sign = -1;
5710 } else if (optarg[0] == '-') {
5711 param.fp_nlink_sign = 1;
5715 param.fp_nlink = strtoul(optarg, &endptr, 0);
5716 if (errno != 0 || *endptr != '\0' || !param.fp_nlink) {
5717 fprintf(stderr, "error: bad link count '%s'\n",
5722 param.fp_exclude_nlink = !!neg_opt;
5726 rc = name2uid(¶m.fp_uid, optarg);
5728 if (str2quotaid(¶m.fp_uid, optarg)) {
5730 "User/UID: %s cannot be found.\n",
5736 param.fp_exclude_uid = !!neg_opt;
5737 param.fp_check_uid = 1;
5740 param.fp_pattern = (char *)optarg;
5741 param.fp_exclude_pattern = !!neg_opt;
5744 if (optarg[0] == '+') {
5745 param.fp_mirror_count_sign = -1;
5747 } else if (optarg[0] == '-') {
5748 param.fp_mirror_count_sign = 1;
5753 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5754 if (errno != 0 || *endptr != '\0' ||
5755 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5757 "error: bad mirror count '%s'\n",
5761 param.fp_check_mirror_count = 1;
5762 param.fp_exclude_mirror_count = !!neg_opt;
5767 char *buf, *token, *next, *p;
5771 buf = strdup(optarg);
5777 param.fp_exclude_obd = !!neg_opt;
5780 while (token && *token) {
5781 token = strchr(token, ',');
5788 param.fp_exclude_mdt = !!neg_opt;
5789 param.fp_num_alloc_mdts += len;
5790 tmp = realloc(param.fp_mdt_uuid,
5791 param.fp_num_alloc_mdts *
5792 sizeof(*param.fp_mdt_uuid));
5798 param.fp_mdt_uuid = tmp;
5800 param.fp_exclude_obd = !!neg_opt;
5801 param.fp_num_alloc_obds += len;
5802 tmp = realloc(param.fp_obd_uuid,
5803 param.fp_num_alloc_obds *
5804 sizeof(*param.fp_obd_uuid));
5810 param.fp_obd_uuid = tmp;
5812 for (token = buf; token && *token; token = next) {
5813 struct obd_uuid *puuid;
5817 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5820 ¶m.fp_obd_uuid[param.fp_num_obds++];
5822 p = strchr(token, ',');
5829 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5834 strncpy(puuid->uuid, token,
5835 sizeof(puuid->uuid));
5842 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5846 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5848 "Pool name %s is too long (max %d)\n",
5849 optarg, LOV_MAXPOOLNAME);
5854 * We do check for empty pool because empty pool
5855 * is used to find V1 LOV attributes
5857 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5858 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5859 param.fp_exclude_pool = !!neg_opt;
5860 param.fp_check_pool = 1;
5863 param.fp_zero_end = 1;
5865 case 'P': /* we always print, this option is a no-op */
5867 case LFS_PRINTF_OPT:
5868 param.fp_format_printf_str = strdup(optarg);
5870 case LFS_PROJID_OPT:
5871 rc = name2projid(¶m.fp_projid, optarg);
5873 if (str2quotaid(¶m.fp_projid, optarg)) {
5875 "Invalid project ID: %s\n",
5881 param.fp_exclude_projid = !!neg_opt;
5882 param.fp_check_projid = 1;
5885 if (optarg[0] == '+') {
5886 param.fp_size_sign = -1;
5888 } else if (optarg[0] == '-') {
5889 param.fp_size_sign = 1;
5893 ret = llapi_parse_size(optarg, ¶m.fp_size,
5894 ¶m.fp_size_units, 0);
5896 fprintf(stderr, "error: bad file size '%s'\n",
5900 param.fp_check_size = 1;
5901 param.fp_exclude_size = !!neg_opt;
5904 if (optarg[0] == '+') {
5905 param.fp_stripe_size_sign = -1;
5907 } else if (optarg[0] == '-') {
5908 param.fp_stripe_size_sign = 1;
5912 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5913 ¶m.fp_stripe_size_units, 0);
5914 /* assume units of KB if too small to be valid */
5915 if (param.fp_stripe_size < 4096)
5916 param.fp_stripe_size *= 1024;
5918 fprintf(stderr, "error: bad stripe_size '%s'\n",
5922 param.fp_check_stripe_size = 1;
5923 param.fp_exclude_stripe_size = !!neg_opt;
5926 param.fp_exclude_type = !!neg_opt;
5927 switch (optarg[0]) {
5929 param.fp_type = S_IFBLK;
5932 param.fp_type = S_IFCHR;
5935 param.fp_type = S_IFDIR;
5938 param.fp_type = S_IFREG;
5941 param.fp_type = S_IFLNK;
5944 param.fp_type = S_IFIFO;
5947 param.fp_type = S_IFSOCK;
5950 fprintf(stderr, "%s: bad type '%s'\n",
5957 param.fp_exclude_perm = !!neg_opt;
5958 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5959 if (*optarg == '/') {
5960 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5962 } else if (*optarg == '-') {
5963 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5967 if (str2mode_t(optarg, ¶m.fp_perm)) {
5968 fprintf(stderr, "error: invalid mode '%s'\n",
5975 if (optarg[0] == '+') {
5976 param.fp_mdt_count_sign = -1;
5978 } else if (optarg[0] == '-') {
5979 param.fp_mdt_count_sign = 1;
5984 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5985 if (errno != 0 || *endptr != '\0' ||
5986 param.fp_mdt_count >= UINT32_MAX) {
5987 fprintf(stderr, "error: bad mdt_count '%s'\n",
5992 param.fp_check_mdt_count = 1;
5993 param.fp_exclude_mdt_count = !!neg_opt;
5996 if (optarg[0] == '+') {
5997 param.fp_ext_size_sign = -1;
5999 } else if (optarg[0] == '-') {
6000 param.fp_ext_size_sign = 1;
6004 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
6005 ¶m.fp_ext_size_units, 0);
6007 fprintf(stderr, "error: bad ext-size '%s'\n",
6011 param.fp_ext_size /= SEL_UNIT_SIZE;
6012 param.fp_ext_size_units /= SEL_UNIT_SIZE;
6013 param.fp_check_ext_size = 1;
6014 param.fp_exclude_ext_size = !!neg_opt;
6017 fprintf(stderr, "%s: unrecognized option '%s'\n",
6018 progname, argv[optind - 1]);
6024 if (!param.fp_verbose)
6025 param.fp_verbose = VERBOSE_DEFAULT;
6027 if (pathstart == -1) {
6028 fprintf(stderr, "error: %s: no filename|pathname\n",
6032 } else if (pathend == -1) {
6038 rc = llapi_find(argv[pathstart], ¶m);
6041 pathbad = pathstart;
6043 } while (++pathstart < pathend);
6046 fprintf(stderr, "%s: failed for '%s': %s\n",
6047 progname, argv[pathbad], strerror(-rc));
6050 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
6051 free(param.fp_obd_uuid);
6053 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
6054 free(param.fp_mdt_uuid);
6056 if (param.fp_format_printf_str)
6057 free(param.fp_format_printf_str);
6062 static int lfs_getstripe_internal(int argc, char **argv,
6063 struct find_param *param)
6065 struct option long_opts[] = {
6066 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
6067 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
6068 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
6069 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
6070 { .val = LFS_COMP_COUNT_OPT,
6071 .name = "comp-count", .has_arg = no_argument },
6072 { .val = LFS_COMP_COUNT_OPT,
6073 .name = "component-count", .has_arg = no_argument },
6074 { .val = LFS_COMP_FLAGS_OPT,
6075 .name = "comp-flags", .has_arg = optional_argument },
6076 { .val = LFS_COMP_FLAGS_OPT,
6077 .name = "component-flags", .has_arg = optional_argument },
6078 { .val = LFS_COMP_START_OPT,
6079 .name = "comp-start", .has_arg = optional_argument },
6080 { .val = LFS_COMP_START_OPT,
6081 .name = "component-start", .has_arg = optional_argument },
6082 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
6083 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
6084 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
6085 { .val = 'd', .name = "directory", .has_arg = no_argument },
6086 { .val = 'D', .name = "default", .has_arg = no_argument },
6087 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
6088 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
6089 { .val = 'F', .name = "fid", .has_arg = no_argument },
6090 { .val = 'g', .name = "generation", .has_arg = no_argument },
6091 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
6092 { .val = 'h', .name = "help", .has_arg = no_argument },
6093 { .val = LFS_HEX_IDX_OPT,
6094 .name = "hex-idx", .has_arg = no_argument },
6095 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
6096 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
6097 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
6098 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
6099 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
6100 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6101 { .val = 'L', .name = "layout", .has_arg = no_argument },
6102 { .val = 'm', .name = "mdt", .has_arg = no_argument },
6103 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6104 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
6105 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
6106 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
6107 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
6108 { .val = LFS_MIRROR_INDEX_OPT,
6109 .name = "mirror-index", .has_arg = required_argument },
6110 { .val = LFS_MIRROR_ID_OPT,
6111 .name = "mirror-id", .has_arg = required_argument },
6112 { .val = LFS_NO_FOLLOW_OPT,
6113 .name = "no-follow", .has_arg = no_argument },
6114 { .val = 'O', .name = "obd", .has_arg = required_argument },
6115 { .val = 'O', .name = "ost", .has_arg = required_argument },
6116 { .val = 'p', .name = "pool", .has_arg = no_argument },
6117 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
6118 { .val = 'q', .name = "quiet", .has_arg = no_argument },
6119 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6120 { .val = 'R', .name = "raw", .has_arg = no_argument },
6121 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
6122 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
6123 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
6124 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
6125 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
6126 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
6127 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6128 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
6129 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }*/
6130 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6131 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
6132 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
6136 int pathstart = -1, pathend = -1;
6140 while ((c = getopt_long(argc, argv,
6141 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
6142 long_opts, NULL)) != -1) {
6146 /* '!' is part of option */
6147 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
6148 if (!isoption && pathend != -1) {
6150 "error: %s: filename|dirname must either precede options or follow options\n",
6154 if (!isoption && pathstart == -1)
6155 pathstart = optind - 1;
6156 if (isoption && pathstart != -1 && pathend == -1)
6157 pathend = optind - 2;
6161 /* unknown: opt is "!" */
6162 if (strcmp(optarg, "!") == 0)
6166 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6167 param->fp_verbose |= VERBOSE_COUNT;
6168 param->fp_max_depth = 0;
6171 case LFS_COMP_COUNT_OPT:
6172 param->fp_verbose |= VERBOSE_COMP_COUNT;
6173 param->fp_max_depth = 0;
6175 case LFS_COMP_FLAGS_OPT:
6177 rc = comp_str2flags(optarg,
6178 ¶m->fp_comp_flags,
6179 ¶m->fp_comp_neg_flags);
6182 "error: %s bad component flags '%s'.\n",
6186 param->fp_check_comp_flags = 1;
6188 param->fp_verbose |= VERBOSE_COMP_FLAGS;
6189 param->fp_max_depth = 0;
6192 case LFS_COMP_START_OPT:
6195 if (tmp[0] == '+') {
6196 param->fp_comp_start_sign = -1;
6198 } else if (tmp[0] == '-') {
6199 param->fp_comp_start_sign = 1;
6202 rc = llapi_parse_size(tmp,
6203 ¶m->fp_comp_start,
6204 ¶m->fp_comp_start_units, 0);
6207 "error: %s bad component start '%s'.\n",
6211 param->fp_check_comp_start = 1;
6213 param->fp_verbose |= VERBOSE_COMP_START;
6214 param->fp_max_depth = 0;
6217 case LFS_MIRROR_INDEX_OPT: {
6218 unsigned long int mirror_index;
6220 if (optarg[0] == '+') {
6221 param->fp_mirror_index_sign = -1;
6223 } else if (optarg[0] == '-') {
6224 param->fp_mirror_index_sign = 1;
6229 mirror_index = strtoul(optarg, &end, 0);
6230 if (errno != 0 || *end != '\0' ||
6231 mirror_index > UINT16_MAX || (mirror_index == 0 &&
6232 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
6234 "%s %s: invalid mirror index '%s'\n",
6235 progname, argv[0], optarg);
6239 param->fp_mirror_index = (__u16)mirror_index;
6241 if (param->fp_mirror_id != 0) {
6243 "%s %s: can't specify both mirror index and mirror ID\n",
6247 param->fp_check_mirror_index = 1;
6248 param->fp_exclude_mirror_index = !!neg_opt;
6251 case LFS_MIRROR_ID_OPT: {
6252 unsigned long int mirror_id;
6254 if (optarg[0] == '+') {
6255 param->fp_mirror_id_sign = -1;
6257 } else if (optarg[0] == '-') {
6258 param->fp_mirror_id_sign = 1;
6263 mirror_id = strtoul(optarg, &end, 0);
6264 if (errno != 0 || *end != '\0' ||
6265 mirror_id > UINT16_MAX || (mirror_id == 0 &&
6266 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
6268 "%s %s: invalid mirror ID '%s'\n",
6269 progname, argv[0], optarg);
6273 param->fp_mirror_id = (__u16)mirror_id;
6275 if (param->fp_mirror_index != 0) {
6277 "%s %s: can't specify both mirror index and mirror ID\n",
6281 param->fp_check_mirror_id = 1;
6282 param->fp_exclude_mirror_id = !!neg_opt;
6285 case LFS_NO_FOLLOW_OPT:
6286 param->fp_no_follow = true;
6288 case LFS_HEX_IDX_OPT:
6289 param->fp_hex_idx = true;
6292 param->fp_max_depth = 0;
6295 param->fp_get_default_lmv = 1;
6300 if (tmp[0] == '+') {
6301 param->fp_comp_end_sign = -1;
6303 } else if (tmp[0] == '-') {
6304 param->fp_comp_end_sign = 1;
6308 if (arg_is_eof(tmp)) {
6309 param->fp_comp_end = LUSTRE_EOF;
6310 param->fp_comp_end_units = 1;
6313 rc = llapi_parse_size(tmp,
6314 ¶m->fp_comp_end,
6315 ¶m->fp_comp_end_units, 0);
6316 /* assume units of KB if too small */
6317 if (param->fp_comp_end < 4096)
6318 param->fp_comp_end *= 1024;
6322 "error: %s bad component end '%s'.\n",
6326 param->fp_check_comp_end = 1;
6328 param->fp_verbose |= VERBOSE_COMP_END;
6329 param->fp_max_depth = 0;
6333 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6334 param->fp_verbose |= VERBOSE_DFID;
6335 param->fp_max_depth = 0;
6339 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6340 param->fp_verbose |= VERBOSE_GENERATION;
6341 param->fp_max_depth = 0;
6345 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6346 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6347 param->fp_max_depth = 0;
6352 param->fp_comp_id = strtoul(optarg, &end, 0);
6353 if (*end != '\0' || param->fp_comp_id == 0 ||
6354 param->fp_comp_id > LCME_ID_MAX) {
6356 "error: %s bad component id '%s'\n",
6360 param->fp_check_comp_id = 1;
6362 param->fp_max_depth = 0;
6363 param->fp_verbose |= VERBOSE_COMP_ID;
6367 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6368 param->fp_verbose |= VERBOSE_PATTERN;
6369 param->fp_max_depth = 0;
6372 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6375 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6378 if (!(param->fp_verbose & VERBOSE_DETAIL))
6379 param->fp_max_depth = 0;
6380 param->fp_verbose |= VERBOSE_MDTINDEX;
6383 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6384 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6385 param->fp_max_depth = 0;
6389 if (param->fp_obd_uuid) {
6391 "error: %s: only one obduuid allowed",
6395 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6398 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6399 param->fp_verbose |= VERBOSE_POOL;
6400 param->fp_max_depth = 0;
6407 param->fp_recursive = 1;
6413 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6414 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6415 param->fp_max_depth = 0;
6419 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6425 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6426 param->fp_verbose |= VERBOSE_EXT_SIZE;
6427 param->fp_max_depth = 0;
6431 fprintf(stderr, "%s: unrecognized option '%s'\n",
6432 progname, argv[optind - 1]);
6438 if (pathstart == -1) {
6439 fprintf(stderr, "error: %s: no filename|pathname\n",
6442 } else if (pathend == -1) {
6450 if (param->fp_recursive)
6451 param->fp_max_depth = -1;
6452 else if (param->fp_verbose & VERBOSE_DETAIL)
6453 param->fp_max_depth = 1;
6455 if (!param->fp_verbose)
6456 param->fp_verbose = VERBOSE_DEFAULT;
6457 if (param->fp_quiet)
6458 param->fp_verbose = VERBOSE_OBJID;
6463 rc2 = llapi_getstripe(argv[pathstart], param);
6465 fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6466 progname, argv[0], argv[optind - 1],
6471 } while (++pathstart < pathend);
6476 static int lfs_tgts(int argc, char **argv)
6478 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6479 struct find_param param;
6480 int index = 0, rc = 0;
6485 if (argc == 2 && !realpath(argv[1], path)) {
6487 fprintf(stderr, "error: invalid path '%s': %s\n",
6488 argv[1], strerror(-rc));
6492 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6493 /* Check if we have a mount point */
6494 if (mntdir[0] == '\0')
6497 memset(¶m, 0, sizeof(param));
6498 if (!strcmp(argv[0], "mdts"))
6499 param.fp_get_lmv = 1;
6501 rc = llapi_ostlist(mntdir, ¶m);
6503 fprintf(stderr, "error: %s: failed on %s\n",
6506 if (path[0] != '\0')
6508 memset(mntdir, 0, PATH_MAX);
6514 static int lfs_getstripe(int argc, char **argv)
6516 struct find_param param = { 0 };
6518 param.fp_max_depth = 1;
6519 return lfs_getstripe_internal(argc, argv, ¶m);
6523 static int lfs_getdirstripe(int argc, char **argv)
6525 struct find_param param = { 0 };
6526 struct option long_opts[] = {
6527 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6528 { .val = 'D', .name = "default", .has_arg = no_argument },
6529 { .val = 'h', .name = "help", .has_arg = no_argument },
6530 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6531 { .val = LFS_HEX_IDX_OPT,
6532 .name = "hex-idx", .has_arg = no_argument },
6533 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6534 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6535 { .val = 'O', .name = "obd", .has_arg = required_argument },
6536 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6537 { .val = 'R', .name = "raw", .has_arg = no_argument },
6538 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6539 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6540 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6541 { .val = LFS_INHERIT_RR_OPT,
6542 .name = "max-inherit-rr", .has_arg = no_argument },
6543 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6547 param.fp_get_lmv = 1;
6549 while ((c = getopt_long(argc, argv,
6550 "cDhHimO:rRtTvXy", long_opts, NULL)) != -1) {
6554 param.fp_verbose |= VERBOSE_COUNT;
6557 param.fp_get_default_lmv = 1;
6559 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6562 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6566 param.fp_verbose |= VERBOSE_HASH_TYPE;
6568 case LFS_HEX_IDX_OPT:
6569 param.fp_hex_idx = 1;
6574 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6577 if (param.fp_obd_uuid) {
6579 "%s: only one obduuid allowed",
6583 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6586 param.fp_recursive = 1;
6592 param.fp_verbose |= VERBOSE_DEFAULT;
6593 param.fp_verbose |= VERBOSE_DETAIL;
6596 param.fp_verbose |= VERBOSE_INHERIT;
6598 case LFS_INHERIT_RR_OPT:
6599 param.fp_verbose |= VERBOSE_INHERIT_RR;
6605 fprintf(stderr, "%s: unrecognized option '%s'\n",
6606 progname, argv[optind - 1]);
6616 if (param.fp_recursive)
6617 param.fp_max_depth = -1;
6619 if (!param.fp_verbose)
6620 param.fp_verbose = VERBOSE_DEFAULT;
6625 rc2 = llapi_getstripe(argv[optind], ¶m);
6627 fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6628 progname, argv[0], argv[optind],
6633 } while (++optind < argc);
6639 MNTDF_INODES = 0x0001,
6640 MNTDF_COOKED = 0x0002,
6641 MNTDF_LAZY = 0x0004,
6642 MNTDF_VERBOSE = 0x0008,
6643 MNTDF_SHOW = 0x0010,
6644 MNTDF_DECIMAL = 0x0020,
6647 #define COOK(value, base) \
6650 while (value > base) { \
6658 #define CDF "%11llu"
6659 #define HDF "%8.1f%c"
6663 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6665 double avail, used, ratio = 0;
6668 avail = st->os_ffree;
6669 used = st->os_files - st->os_ffree;
6671 avail = st->os_bavail;
6672 used = st->os_blocks - st->os_bfree;
6674 if (avail + used > 0)
6675 ratio = used / (used + avail) * 100;
6677 /* Round up to match df(1) usage percentage */
6678 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6682 * This is to identify various problem states for "lfs df" if .osn_err = true,
6683 * so only show flags reflecting those states by default. Informational states
6684 * are only shown with "-v" and use lower-case names to distinguish them.
6685 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6687 static struct obd_statfs_state_names {
6688 enum obd_statfs_state osn_state;
6689 const char osn_name;
6692 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6693 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6694 { .osn_state = OS_STATFS_NOCREATE, .osn_name = 'N', .osn_err = true },
6695 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6696 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6697 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6698 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6699 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6700 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6703 static int showdf(char *mntdir, struct obd_statfs *stat,
6704 char *uuid, enum mntdf_flags flags,
6705 char *type, int index, int rc)
6707 long long avail, used, total;
6709 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6710 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6711 char tbuf[3 * sizeof(__u64)];
6712 char ubuf[3 * sizeof(__u64)];
6713 char abuf[3 * sizeof(__u64)];
6714 char rbuf[3 * sizeof(__u64)];
6721 if (flags & MNTDF_INODES) {
6722 avail = stat->os_ffree;
6723 used = stat->os_files - stat->os_ffree;
6724 total = stat->os_files;
6726 int shift = flags & MNTDF_COOKED ? 0 : 10;
6728 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6729 used = ((stat->os_blocks - stat->os_bfree) *
6730 stat->os_bsize) >> shift;
6731 total = (stat->os_blocks * stat->os_bsize) >> shift;
6734 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6736 if (flags & MNTDF_COOKED) {
6737 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6741 cook_val = (double)total;
6742 i = COOK(cook_val, base);
6744 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6747 snprintf(tbuf, sizeof(tbuf), CDF, total);
6749 cook_val = (double)used;
6750 i = COOK(cook_val, base);
6752 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6755 snprintf(ubuf, sizeof(ubuf), CDF, used);
6757 cook_val = (double)avail;
6758 i = COOK(cook_val, base);
6760 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6763 snprintf(abuf, sizeof(abuf), CDF, avail);
6765 snprintf(tbuf, sizeof(tbuf), CDF, total);
6766 snprintf(ubuf, sizeof(tbuf), CDF, used);
6767 snprintf(abuf, sizeof(tbuf), CDF, avail);
6770 sprintf(rbuf, RDF, ratio);
6771 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6772 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6774 printf("[%s:%d]", type, index);
6776 if (stat->os_state) {
6780 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6781 if (oss_names[i].osn_state & stat->os_state &&
6782 (oss_names[i].osn_err ||
6783 flags & MNTDF_VERBOSE))
6784 printf("%c", oss_names[i].osn_name);
6791 printf(UUF": inactive device\n", uuid);
6794 printf(UUF": %s\n", uuid, strerror(-rc));
6801 struct ll_stat_type {
6806 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6808 struct ll_statfs_data {
6810 struct obd_statfs sd_st;
6813 struct ll_statfs_buf {
6815 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6818 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6819 int ops, struct ll_statfs_buf *lsb)
6821 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6822 struct obd_uuid uuid_buf;
6823 char *poolname = NULL;
6824 struct ll_stat_type types[] = {
6825 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6826 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6827 { .st_name = NULL } };
6828 struct ll_stat_type *tp;
6829 __u64 ost_files = 0;
6830 __u64 ost_ffree = 0;
6838 poolname = strchr(pool, '.');
6840 if (strncmp(fsname, pool, strlen(fsname))) {
6841 fprintf(stderr, "filesystem name incorrect\n");
6849 fd = open(mntdir, O_RDONLY);
6852 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6857 if (flags & MNTDF_SHOW) {
6858 if (flags & MNTDF_INODES)
6859 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6860 "UUID", "Inodes", "IUsed", "IFree",
6861 "IUse%", "Mounted on");
6863 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6865 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6866 "Used", "Available", "Use%", "Mounted on");
6869 for (tp = types; tp->st_name != NULL; tp++) {
6870 bool have_ost = false;
6872 if (!(tp->st_op & ops))
6875 for (index = 0; index < LOV_ALL_STRIPES &&
6876 (!lsb || lsb->sb_count < LL_STATFS_MAX); index++) {
6877 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6878 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6879 type = flags & MNTDF_LAZY ?
6880 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6881 rc2 = llapi_obd_fstatfs(fd, type, index,
6882 &stat_buf, &uuid_buf);
6887 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6888 if (!(flags & MNTDF_VERBOSE))
6890 } else if (rc2 < 0 && rc == 0) {
6895 * If we have OSTs then don't report MDT block counts.
6896 * For MDT-only filesystems the expectation is that all
6897 * layouts have a DoM component. For filesystems with
6898 * OSTs, files are not necessarily going to store data
6899 * on MDTs, and MDT space is limited to a fraction of
6900 * OST space, so don't include it in the summary.
6902 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6909 if (poolname && tp->st_op == LL_STATFS_LOV &&
6910 llapi_search_ost(fsname, poolname,
6911 obd_uuid2str(&uuid_buf)) != 1)
6915 * the llapi_obd_fstatfs() call may have returned with
6916 * an error, but if it filled in uuid_buf we will at
6917 * lease use that to print out a message for that OBD.
6918 * If we didn't get anything in the uuid_buf, then fill
6919 * it in so that we can print an error message.
6921 if (uuid_buf.uuid[0] == '\0')
6922 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6923 "%s%04x", tp->st_name, index);
6925 lsb->sb_buf[lsb->sb_count].sd_index = index;
6926 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6929 if (flags & MNTDF_SHOW)
6930 showdf(mntdir, &stat_buf,
6931 obd_uuid2str(&uuid_buf), flags,
6932 tp->st_name, index, rc2);
6937 if (tp->st_op == LL_STATFS_LMV) {
6938 sum.os_ffree += stat_buf.os_ffree;
6939 sum.os_files += stat_buf.os_files;
6940 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6941 ost_files += stat_buf.os_files;
6942 ost_ffree += stat_buf.os_ffree;
6944 sum.os_blocks += stat_buf.os_blocks *
6946 sum.os_bfree += stat_buf.os_bfree *
6948 sum.os_bavail += stat_buf.os_bavail *
6956 * If we have _some_ OSTs, but don't have as many free objects on the
6957 * OST as inodes on the MDTs, reduce the reported number of inodes
6958 * to compensate, so that the "inodes in use" number is correct.
6959 * This should be kept in sync with ll_statfs_internal().
6961 if (ost_files && ost_ffree < sum.os_ffree) {
6962 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6963 sum.os_ffree = ost_ffree;
6965 if (flags & MNTDF_SHOW) {
6967 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6975 LAYOUT_INHERIT_UNSET = -2,
6979 static int lfs_setdirstripe(int argc, char **argv)
6982 struct lfs_setstripe_args lsa = { 0 };
6983 struct llapi_stripe_param *param = NULL;
6984 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6987 char *mode_opt = NULL;
6988 bool default_stripe = false;
6989 bool delete = false;
6990 bool foreign_mode = false;
6991 bool mdt_count_set = false;
6992 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6993 mode_t previous_mode = 0;
6995 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6996 int max_inherit = LAYOUT_INHERIT_UNSET;
6997 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6998 struct option long_opts[] = {
6999 { .val = 'c', .name = "count", .has_arg = required_argument },
7000 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
7001 { .val = 'd', .name = "delete", .has_arg = no_argument },
7002 { .val = 'D', .name = "default", .has_arg = no_argument },
7003 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
7004 { .val = LFS_LAYOUT_FLAGS_OPT,
7005 .name = "flags", .has_arg = required_argument },
7006 { .val = LFS_LAYOUT_FOREIGN_OPT,
7007 .name = "foreign", .has_arg = optional_argument},
7008 { .val = 'h', .name = "help", .has_arg = no_argument },
7009 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
7010 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
7011 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
7012 { .val = 'i', .name = "mdt", .has_arg = required_argument },
7014 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
7015 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7016 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7018 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7019 { .val = 'i', .name = "index", .has_arg = required_argument },
7021 { .val = 'o', .name = "mode", .has_arg = required_argument },
7022 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7023 { .val = 't', .name = "hash-type", .has_arg = required_argument },
7025 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
7026 { .val = 'x', .name = "xattr", .has_arg = required_argument },
7027 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
7028 { .val = LFS_INHERIT_RR_OPT,
7029 .name = "max-inherit-rr", .has_arg = required_argument},
7030 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
7031 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
7035 setstripe_args_init(&lsa);
7037 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
7038 long_opts, NULL)) >= 0) {
7046 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
7047 if (errno != 0 || *end != '\0' ||
7048 lsa.lsa_stripe_count < -1 ||
7049 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
7051 "%s: invalid stripe count '%s'\n",
7055 mdt_count_set = true;
7059 default_stripe = true;
7062 default_stripe = true;
7064 case LFS_LAYOUT_FOREIGN_OPT:
7066 /* check pure numeric */
7067 type = strtoul(optarg, &end, 0);
7070 type = check_foreign_type_name(optarg);
7071 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
7073 "%s %s: unknown foreign type '%s'\n",
7078 } else if (type >= UINT32_MAX) {
7080 "%s %s: invalid foreign type '%s'\n",
7081 progname, argv[0], optarg);
7085 foreign_mode = true;
7087 case LFS_LAYOUT_FLAGS_OPT:
7089 flags = strtoul(optarg, &end, 16);
7090 if (errno != 0 || *end != '\0' ||
7091 flags >= UINT32_MAX) {
7093 "%s %s: invalid hex flags '%s'\n",
7094 progname, argv[0], optarg);
7097 if (!foreign_mode) {
7099 "%s %s: hex flags must be specified with --foreign option\n",
7104 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7107 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
7111 lsa.lsa_pattern = check_hashtype(optarg);
7112 if (lsa.lsa_pattern == 0) {
7114 "%s %s: bad directory hash type '%s'\n",
7115 progname, argv[0], optarg);
7120 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
7123 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7124 if (strcmp(argv[optind - 1], "--index") == 0)
7126 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
7129 lsa.lsa_nr_tgts = parse_targets(mdts,
7130 sizeof(mdts) / sizeof(__u32),
7131 lsa.lsa_nr_tgts, optarg, NULL);
7132 if (lsa.lsa_nr_tgts < 0) {
7134 "%s %s: invalid MDT target(s) '%s'\n",
7135 progname, argv[0], optarg);
7139 lsa.lsa_tgts = mdts;
7140 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7141 lsa.lsa_stripe_off = mdts[0];
7151 max_inherit = strtol(optarg, &end, 10);
7152 if (errno != 0 || *end != '\0' || max_inherit < -2) {
7154 "%s %s: invalid max-inherit '%s'\n",
7155 progname, argv[0], optarg);
7158 if (max_inherit == 0) {
7159 max_inherit = LMV_INHERIT_NONE;
7160 } else if (max_inherit == -1) {
7161 max_inherit = LMV_INHERIT_UNLIMITED;
7162 } else if (max_inherit > LMV_INHERIT_MAX) {
7164 "%s %s: max-inherit %d exceeds maximum %u\n",
7165 progname, argv[0], max_inherit,
7170 case LFS_INHERIT_RR_OPT:
7171 if (!default_stripe) {
7173 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
7178 max_inherit_rr = strtol(optarg, &end, 10);
7179 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
7181 "%s %s: invalid max-inherit-rr '%s'\n",
7182 progname, argv[0], optarg);
7185 if (max_inherit_rr == 0) {
7186 max_inherit_rr = LMV_INHERIT_RR_NONE;
7187 } else if (max_inherit_rr == -1) {
7188 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
7189 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
7191 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
7192 progname, argv[0], max_inherit_rr,
7193 LMV_INHERIT_RR_MAX);
7198 fprintf(stderr, "%s: unrecognized option '%s'\n",
7199 progname, argv[optind - 1]);
7206 if (optind == argc) {
7207 fprintf(stderr, "%s %s: DIR must be specified\n",
7212 if (xattr && !foreign_mode) {
7214 * only print a warning as this is armless and will be
7218 "%s %s: xattr has been specified for non-foreign layout\n",
7220 } else if (foreign_mode && !xattr) {
7222 "%s %s: xattr must be provided in foreign mode\n",
7227 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
7228 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
7230 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
7235 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
7236 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
7237 /* if no parameters set, create directory on least-used MDTs */
7238 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
7239 lsa.lsa_stripe_count = 1;
7243 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
7244 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
7246 "%s %s: cannot specify -d with -c or -i options\n",
7252 mode = strtoul(mode_opt, &end, 8);
7255 "%s %s: bad MODE '%s'\n",
7256 progname, argv[0], mode_opt);
7259 previous_mode = umask(0);
7262 /* check max-inherit and warn user in some cases */
7263 if (default_stripe &&
7264 (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
7265 if (max_inherit == LMV_INHERIT_UNLIMITED)
7267 "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
7268 progname, argv[0], lsa.lsa_stripe_count);
7269 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
7270 max_inherit != LMV_INHERIT_NONE)
7272 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
7273 progname, argv[0], max_inherit,
7274 lsa.lsa_stripe_count);
7277 if (default_stripe && lsa.lsa_nr_tgts > 1 && !mdt_count_set) {
7279 "%s %s: trying to create unrecommended default striped directory layout,\n"
7280 " '-D -i x,y,z' will stripe every new directory across all MDTs,\n"
7281 " add -c with the number of MDTs to do this anyway\n",
7286 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
7287 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
7288 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
7290 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
7291 progname, argv[0], lsa.lsa_stripe_off);
7295 /* foreign LMV/dir case */
7297 if (argc > optind + 1) {
7299 "%s %s: cannot specify multiple foreign dirs\n",
7304 dname = argv[optind];
7305 result = llapi_dir_create_foreign(dname, mode, type, flags,
7309 "%s mkdir: can't create foreign dir '%s': %s\n",
7310 progname, dname, strerror(-result));
7315 * initialize stripe parameters, in case param is converted to specific,
7316 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
7318 param = calloc(1, offsetof(typeof(*param),
7319 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
7320 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
7323 "%s %s: cannot allocate memory for parameters: %s\n",
7324 progname, argv[0], strerror(ENOMEM));
7328 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
7329 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
7330 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
7331 lsa.lsa_stripe_count == 0))
7332 lsa.lsa_stripe_count = 1;
7333 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
7334 param->lsp_stripe_count = lsa.lsa_stripe_count;
7335 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7336 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
7338 param->lsp_stripe_offset = lsa.lsa_stripe_off;
7339 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
7340 param->lsp_stripe_pattern = lsa.lsa_pattern;
7342 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
7343 param->lsp_pool = lsa.lsa_pool_name;
7344 param->lsp_is_specific = false;
7346 if (max_inherit == LAYOUT_INHERIT_UNSET) {
7347 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1 ||
7348 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT)
7349 max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
7351 max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
7353 param->lsp_max_inherit = max_inherit;
7354 if (default_stripe) {
7356 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
7357 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
7358 param->lsp_max_inherit_rr = max_inherit_rr;
7360 if (strcmp(argv[0], "mkdir") == 0)
7361 param->lsp_is_create = true;
7362 if (lsa.lsa_nr_tgts > 1) {
7363 if (lsa.lsa_stripe_count > 0 &&
7364 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7365 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7367 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7368 argv[0], lsa.lsa_stripe_count,
7374 param->lsp_is_specific = true;
7375 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7376 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7379 dname = argv[optind];
7381 if (default_stripe) {
7382 result = llapi_dir_set_default_lmv(dname, param);
7385 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7386 progname, dname, strerror(-result));
7390 result = llapi_dir_create(dname, mode, param);
7393 "%s setdirstripe: cannot create dir '%s': %s\n",
7394 progname, dname, strerror(-result));
7395 } while (!result && (dname = argv[++optind]));
7398 umask(previous_mode);
7404 static int lfs_rmentry(int argc, char **argv)
7411 fprintf(stderr, "error: %s: missing dirname\n",
7417 dname = argv[index];
7421 rc2 = llapi_direntry_remove(dname);
7424 "%s %s: remove dir entry '%s' failed: %s\n",
7425 progname, argv[0], dname, strerror(-rc2));
7429 dname = argv[++index];
7434 static int lfs_unlink_foreign(int argc, char **argv)
7441 fprintf(stderr, "error: %s: missing pathname\n",
7448 while (name != NULL) {
7449 result = llapi_unlink_foreign(name);
7452 "error: %s: unlink foreign entry '%s' failed\n",
7456 name = argv[++index];
7461 static int lfs_mv(int argc, char **argv)
7463 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7464 struct find_param param = {
7471 struct option long_opts[] = {
7472 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7473 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7474 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7477 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7479 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7482 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7486 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7487 if (errno != 0 || *end != '\0' ||
7488 lmu.lum_stripe_offset >= UINT32_MAX) {
7489 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7495 param.fp_verbose = VERBOSE_DETAIL;
7498 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7499 progname, argv[optind - 1]);
7504 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7505 fprintf(stderr, "%s mv: MDT index must be specified\n",
7510 if (optind >= argc) {
7511 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7515 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7517 /* initialize migrate mdt parameters */
7518 param.fp_lmv_md = &lmu;
7519 param.fp_migrate = 1;
7520 rc = llapi_migrate_mdt(argv[optind], ¶m);
7522 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7523 progname, argv[optind], lmu.lum_stripe_offset,
7528 static int lfs_osts(int argc, char **argv)
7530 return lfs_tgts(argc, argv);
7533 static int lfs_mdts(int argc, char **argv)
7535 return lfs_tgts(argc, argv);
7538 static int lfs_df(int argc, char **argv)
7540 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7541 enum mntdf_flags flags = MNTDF_SHOW;
7542 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7543 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7544 char fsname[PATH_MAX] = "", *pool_name = NULL;
7545 struct option long_opts[] = {
7546 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7547 { .val = 'H', .name = "si", .has_arg = no_argument },
7548 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7549 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7550 { .val = 'p', .name = "pool", .has_arg = required_argument },
7551 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7554 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7557 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7560 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7563 flags |= MNTDF_INODES;
7566 flags |= MNTDF_LAZY;
7572 flags |= MNTDF_VERBOSE;
7575 fprintf(stderr, "%s: unrecognized option '%s'\n",
7576 progname, argv[optind - 1]);
7581 /* Handle case where path is not specified */
7582 if (optind == argc) {
7583 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7584 /* Check if we have a mount point */
7585 if (mntdir[0] == '\0')
7588 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7589 if (rc || path[0] != '\0')
7592 fsname[0] = '\0'; /* avoid matching in next loop */
7593 mntdir[0] = '\0'; /* avoid matching in next loop */
7594 path[0] = '\0'; /* clean for next loop */
7599 /* Loop through all the remaining arguments. These are Lustre FS
7602 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7605 fsname[0] = '\0'; /* start clean */
7606 mntdir[0] = '\0'; /* start clean */
7607 path[0] = '\0'; /* start clean */
7609 /* path does not exists at all */
7610 if (!realpath(argv[arg_idx], path)) {
7612 fprintf(stderr, "error: invalid path '%s': %s\n",
7613 argv[arg_idx], strerror(-rc));
7614 /* save first seen error */
7621 /* path exists but may not be a Lustre filesystem */
7622 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7623 /* Check if we have a mount point */
7624 if (mntdir[0] == '\0')
7627 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7628 if (rc || path[0] != '\0') {
7631 /* save first seen error */
7639 llapi_printf(LLAPI_MSG_ERROR,
7640 "%s:%s Not a Lustre filesystem\n",
7641 argv[0], argv[arg_idx]);
7642 /* save first seen error */
7651 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7652 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7656 if (opt_fsname == opt_instance) { /* both true or both false */
7657 rc = llapi_getname(mntdir, buf, buflen);
7658 } else if (opt_fsname) {
7660 * llapi_search_mounts() fills @buf with fsname, but that is not
7661 * called if explicit paths are specified on the command-line
7664 rc = llapi_get_fsname(mntdir, buf, buflen);
7665 } else /* if (opt_instance) */ {
7666 rc = llapi_get_instance(mntdir, buf, buflen);
7670 fprintf(stderr, "cannot get instance for '%s': %s\n",
7671 mntdir, strerror(-rc));
7676 printf("%s %s\n", buf, mntdir);
7678 printf("%s\n", buf);
7683 static int lfs_getname(int argc, char **argv)
7685 struct option long_opts[] = {
7686 { .val = 'h', .name = "help", .has_arg = no_argument },
7687 { .val = 'i', .name = "instance", .has_arg = no_argument },
7688 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7690 bool opt_instance = false, opt_fsname = false;
7691 char fsname[PATH_MAX] = "";
7694 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7697 opt_instance = true;
7703 fprintf(stderr, "%s: unrecognized option '%s'\n",
7704 progname, argv[optind - 1]);
7711 if (optind == argc) { /* no paths specified, get all paths. */
7712 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7715 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7716 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7717 opt_instance, opt_fsname, true);
7720 path[0] = fsname[0] = mntdir[0] = '\0';
7722 } else { /* paths specified, only attempt to search these. */
7725 /* if only one path is given, print only requested info */
7726 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7728 for (; optind < argc; optind++) {
7729 rc2 = print_instance(argv[optind], fsname,
7730 sizeof(fsname), opt_instance,
7731 opt_fsname, opt_mntdir);
7741 static int lfs_check(int argc, char **argv)
7743 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7751 if (argc < 2 || argc > 3) {
7752 fprintf(stderr, "%s check: server type must be specified\n",
7757 obd_types[0] = obd_type1;
7758 obd_types[1] = obd_type2;
7759 obd_types[2] = obd_type3;
7761 if (strcmp(argv[1], "osts") == 0) {
7762 strcpy(obd_types[0], "osc");
7763 } else if (strcmp(argv[1], "mdts") == 0 ||
7764 strcmp(argv[1], "mds") == 0) {
7765 strcpy(obd_types[0], "mdc");
7766 } else if (strcmp(argv[1], "mgts") == 0) {
7767 strcpy(obd_types[0], "mgc");
7768 } else if (strcmp(argv[1], "all") == 0 ||
7769 strcmp(argv[1], "servers") == 0) {
7771 strcpy(obd_types[0], "osc");
7772 strcpy(obd_types[1], "mdc");
7773 strcpy(obd_types[2], "mgc");
7775 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7780 if (argc >= 3 && !realpath(argv[2], path)) {
7782 fprintf(stderr, "error: invalid path '%s': %s\n",
7783 argv[2], strerror(-rc));
7787 rc = llapi_search_mounts(path, 0, mntdir, NULL);
7788 if (rc < 0 || mntdir[0] == '\0') {
7790 "%s %s: cannot find mounted Lustre filesystem: %s\n",
7792 (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7796 rc = llapi_target_check(num_types, obd_types, path);
7798 fprintf(stderr, "%s %s: cannot check target '%s': %s\n",
7799 progname, argv[0], argv[1], strerror(-rc));
7804 #ifdef HAVE_SYS_QUOTA_H
7805 #define ADD_OVERFLOW(a, b) \
7806 ((((a) + (b)) < (a)) ? \
7807 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7809 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7810 * returns the value or ULONG_MAX on integer overflow or incorrect format
7812 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7813 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7814 * 3. empty integer value is interpreted as 0
7816 static unsigned long str2sec(const char *timestr)
7818 const char spec[] = "smhdw";
7819 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7820 unsigned long val = 0;
7823 if (strpbrk(timestr, spec) == NULL) {
7825 * no specifiers inside the time string,
7826 * should treat it as an integer value
7828 val = strtoul(timestr, &tail, 10);
7829 return *tail ? ULONG_MAX : val;
7832 /* format string is XXwXXdXXhXXmXXs */
7838 v = strtoul(timestr, &tail, 10);
7839 if (v == ULONG_MAX || *tail == '\0')
7841 * value too large (ULONG_MAX or more)
7842 * or missing specifier
7846 ptr = strchr(spec, *tail);
7848 /* unknown specifier */
7853 /* check if product will overflow the type */
7854 if (!(v < ULONG_MAX / mult[ind]))
7857 ADD_OVERFLOW(val, mult[ind] * v);
7858 if (val == ULONG_MAX)
7870 #define ARG2ULL(nr, str, def_units) \
7872 unsigned long long limit, units = def_units; \
7875 rc = llapi_parse_size(str, &limit, &units, 1); \
7877 fprintf(stderr, "%s: invalid limit '%s'\n", \
7884 static inline int has_times_option(int argc, char **argv)
7888 for (i = 1; i < argc; i++)
7889 if (!strcmp(argv[i], "-t"))
7895 static inline int lfs_verify_poolarg(char *pool)
7897 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7899 "Pool name '%.*s' is longer than %d\n",
7900 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7906 /* special grace time, only notify the user when its quota is over soft limit
7907 * but doesn't block new writes until the hard limit is reached.
7909 #define NOTIFY_GRACE "notify"
7910 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7913 static inline __u64 lustre_stoqb(size_t space)
7915 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7918 #define lustre_stoqb toqb
7921 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7924 char *mnt, *obd_type = (char *)qctl->obd_type;
7925 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7926 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7927 struct option long_opts[] = {
7928 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7929 { .val = 'g', .name = "group", .has_arg = no_argument },
7930 { .val = 'h', .name = "help", .has_arg = no_argument },
7931 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7932 { .val = 'p', .name = "projid", .has_arg = no_argument },
7933 { .val = 't', .name = "times", .has_arg = no_argument },
7934 { .val = 'u', .name = "user", .has_arg = no_argument },
7935 { .val = LFS_POOL_OPT,
7936 .name = "pool", .has_arg = required_argument },
7940 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7941 qctl->qc_type = ALLQUOTA;
7943 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7944 long_opts, NULL)) != -1) {
7955 if (qctl->qc_type != ALLQUOTA) {
7957 "%s: -u/g/p cannot be used more than once\n",
7961 qctl->qc_type = qtype;
7964 if (strncmp(optarg, NOTIFY_GRACE,
7965 strlen(NOTIFY_GRACE)) == 0) {
7966 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7968 dqi->dqi_bgrace = str2sec(optarg);
7969 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7971 "%s: bad block-grace: %s\n",
7976 dqb->dqb_valid |= QIF_BTIME;
7979 if (strncmp(optarg, NOTIFY_GRACE,
7980 strlen(NOTIFY_GRACE)) == 0) {
7981 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7983 dqi->dqi_igrace = str2sec(optarg);
7984 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7986 "%s: bad inode-grace: %s\n",
7991 dqb->dqb_valid |= QIF_ITIME;
7993 case 't': /* Yes, of course! */
7996 if (lfs_verify_poolarg(optarg))
7998 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7999 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
8001 /* getopt prints error message for us when opterr != 0 */
8003 fprintf(stderr, "%s: unrecognized option '%s'\n",
8004 progname, argv[optind - 1]);
8011 if (qctl->qc_type == ALLQUOTA) {
8012 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
8017 if (optind != argc - 1) {
8018 fprintf(stderr, "%s: unexpected parameter '%s'\n",
8019 progname, argv[optind + 1]);
8024 rc = llapi_quotactl(mnt, qctl);
8027 fprintf(stderr, "%s %s ", obd_type,
8028 obd_uuid2str(&qctl->obd_uuid));
8029 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8036 static int lfs_reset_quota(char *mnt, struct if_quotactl *qctl)
8038 struct if_quotactl tmp_qctl;
8039 int index, md_count, dt_count;
8040 int wait_phase = 0, wait_index = 0, wait_count = 0;
8043 /* reset the quota ID, the existing quota setting will be returned */
8044 rc = llapi_quotactl(mnt, qctl);
8049 if ((qctl->qc_dqblk.dqb_valid & QIF_LIMITS) != QIF_LIMITS) {
8051 "the existing quota settings are not returned!\n");
8055 rc = llapi_get_obd_count(mnt, &md_count, 1);
8057 fprintf(stderr, "can not get mdt count: %s\n", strerror(-rc));
8061 rc = llapi_get_obd_count(mnt, &dt_count, 0);
8063 fprintf(stderr, "can not get ost count: %s\n", strerror(-rc));
8067 memset(&tmp_qctl, 0, sizeof(tmp_qctl));
8068 tmp_qctl.qc_type = qctl->qc_type;
8069 tmp_qctl.qc_id = qctl->qc_id;
8070 tmp_qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
8073 if (wait_phase == 0) {
8074 for (index = wait_index; index < md_count; index++) {
8075 tmp_qctl.qc_idx = index;
8076 tmp_qctl.qc_valid = QC_MDTIDX;
8077 rc = llapi_quotactl(mnt, &tmp_qctl);
8078 if (rc == -ENODEV || rc == -ENODATA)
8081 fprintf(stderr, "quotactl mdt%d failed: %s\n",
8082 index, strerror(-rc));
8085 /* check whether the md quota grant is reset */
8086 if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
8087 tmp_qctl.qc_dqblk.dqb_ihardlimit != 0)
8091 if (index < md_count) {
8097 for (index = wait_index; index < dt_count; index++) {
8098 tmp_qctl.qc_idx = index;
8099 tmp_qctl.qc_valid = QC_OSTIDX;
8100 rc = llapi_quotactl(mnt, &tmp_qctl);
8101 if (rc == -ENODEV || rc == -ENODATA)
8104 fprintf(stderr, "quotactl mdt%d failed: %s\n",
8105 index, strerror(-rc));
8108 /* check whether the dt quota grant is reset */
8109 if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
8110 tmp_qctl.qc_dqblk.dqb_bhardlimit != 0)
8114 if (index < dt_count) {
8121 if (wait_phase == 0) {
8129 if (rc || wait_count > 30) {
8130 fprintf(stderr, "fail to reset the quota ID %d on OBDs\n",
8137 fprintf(stdout, "wait %d seconds for OBDs to reset the quota ID %u\n",
8138 wait_count, qctl->qc_id);
8143 /* restore the quota setting */
8144 if (qctl->qc_dqblk.dqb_isoftlimit == 0 &&
8145 qctl->qc_dqblk.dqb_ihardlimit == 0 &&
8146 qctl->qc_dqblk.dqb_bsoftlimit == 0 &&
8147 qctl->qc_dqblk.dqb_bhardlimit == 0)
8150 memcpy(&tmp_qctl, qctl, sizeof(tmp_qctl));
8151 tmp_qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
8152 rc2 = llapi_quotactl(mnt, &tmp_qctl);
8157 "fail to restore the quota setting: %s, please restore it manually by\n lfs setquota %s %d",
8159 qctl->qc_type == USRQUOTA ? "-u" :
8160 (qctl->qc_type == GRPQUOTA ? "-g" : "-p"),
8163 if (qctl->qc_dqblk.dqb_isoftlimit != 0)
8164 fprintf(stderr, " -i %llu",
8165 (unsigned long long)qctl->qc_dqblk.dqb_isoftlimit);
8166 if (qctl->qc_dqblk.dqb_ihardlimit != 0)
8167 fprintf(stderr, " -I %llu",
8168 (unsigned long long)qctl->qc_dqblk.dqb_ihardlimit);
8169 if (qctl->qc_dqblk.dqb_bsoftlimit != 0)
8170 fprintf(stderr, " -b %llu",
8171 (unsigned long long)qctl->qc_dqblk.dqb_bsoftlimit);
8172 if (qctl->qc_dqblk.dqb_bhardlimit != 0)
8173 fprintf(stderr, " -B %llu",
8174 (unsigned long long)qctl->qc_dqblk.dqb_bhardlimit);
8176 fprintf(stderr, " %s\n", mnt);
8183 #define BSLIMIT (1 << 0)
8184 #define BHLIMIT (1 << 1)
8185 #define ISLIMIT (1 << 2)
8186 #define IHLIMIT (1 << 3)
8188 int lfs_setquota(int argc, char **argv)
8191 struct if_quotactl *qctl;
8192 char *mnt, *obd_type;
8193 struct obd_dqblk *dqb;
8194 struct option long_opts[] = {
8195 { .val = 'b', .name = "block-softlimit",
8196 .has_arg = required_argument },
8197 { .val = 'B', .name = "block-hardlimit",
8198 .has_arg = required_argument },
8199 { .val = 'd', .name = "default", .has_arg = no_argument },
8200 { .val = LFS_SETQUOTA_DELETE,
8201 .name = "delete", .has_arg = no_argument },
8202 { .val = 'g', .name = "group", .has_arg = required_argument },
8203 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
8204 { .val = 'h', .name = "help", .has_arg = no_argument },
8205 { .val = 'i', .name = "inode-softlimit",
8206 .has_arg = required_argument },
8207 { .val = 'I', .name = "inode-hardlimit",
8208 .has_arg = required_argument },
8209 { .val = 'p', .name = "projid", .has_arg = required_argument },
8210 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
8211 { .val = 'r', .name = "reset", .has_arg = no_argument },
8212 { .val = 'u', .name = "user", .has_arg = required_argument },
8213 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
8214 { .val = LFS_POOL_OPT,
8215 .name = "pool", .has_arg = required_argument },
8217 unsigned int limit_mask = 0;
8218 bool use_default = false;
8219 int qtype, qctl_len;
8221 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
8222 qctl = malloc(qctl_len);
8226 memset(qctl, 0, qctl_len);
8227 obd_type = (char *)qctl->obd_type;
8228 dqb = &qctl->qc_dqblk;
8230 if (has_times_option(argc, argv)) {
8231 rc = lfs_setquota_times(argc, argv, qctl);
8235 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
8236 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
8237 * so it can be used as a marker that qc_type
8238 * isn't reinitialized from command line
8240 while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pru:U",
8241 long_opts, NULL)) != -1) {
8244 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8247 goto quota_type_def;
8250 rc = name2uid(&qctl->qc_id, optarg);
8253 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8256 goto quota_type_def;
8259 rc = name2gid(&qctl->qc_id, optarg);
8262 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8265 goto quota_type_def;
8268 rc = name2projid(&qctl->qc_id, optarg);
8271 if (str2quotaid(&qctl->qc_id, optarg)) {
8273 "%s setquota: invalid id '%s'\n",
8280 if (qctl->qc_id == 0) {
8282 "%s setquota: can't set quota for root usr/group/project.\n",
8289 if (qctl->qc_type != ALLQUOTA) {
8291 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
8296 qctl->qc_type = qtype;
8298 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
8301 "%s setquota: '-d' deprecated, use '-D' or '--default'\n",
8307 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8309 case LFS_SETQUOTA_DELETE:
8310 qctl->qc_cmd = LUSTRE_Q_DELETEQID;
8313 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
8314 dqb->dqb_bsoftlimit >>= 10;
8315 limit_mask |= BSLIMIT;
8316 if (dqb->dqb_bsoftlimit &&
8317 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
8319 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8321 (unsigned long long)dqb->dqb_bsoftlimit,
8325 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
8326 dqb->dqb_bhardlimit >>= 10;
8327 limit_mask |= BHLIMIT;
8328 if (dqb->dqb_bhardlimit &&
8329 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
8331 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
8332 "See '%s help setquota' or Lustre manual for details\n",
8334 (unsigned long long)dqb->dqb_bhardlimit,
8338 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
8339 limit_mask |= ISLIMIT;
8340 if (dqb->dqb_isoftlimit &&
8341 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
8343 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8345 (unsigned long long)dqb->dqb_isoftlimit,
8349 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
8350 limit_mask |= IHLIMIT;
8351 if (dqb->dqb_ihardlimit &&
8352 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
8354 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8356 (unsigned long long)dqb->dqb_ihardlimit,
8360 if (lfs_verify_poolarg(optarg)) {
8364 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8365 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
8366 LUSTRE_Q_SETDEFAULT_POOL :
8367 LUSTRE_Q_SETQUOTAPOOL;
8370 qctl->qc_cmd = LUSTRE_Q_RESETQID;
8374 "%s setquota: unrecognized option '%s'\n",
8375 progname, argv[optind - 1]);
8383 if (qctl->qc_type == ALLQUOTA) {
8385 "%s setquota: either -u or -g must be specified\n",
8391 if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
8392 qctl->qc_cmd != LUSTRE_Q_RESETQID && limit_mask == 0) {
8394 "%s setquota: at least one limit must be specified\n",
8400 if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8401 qctl->qc_cmd == LUSTRE_Q_RESETQID) && limit_mask != 0) {
8403 "%s setquota: limits should not be specified when using default quota, deleting or resetting quota ID\n",
8409 if (use_default && qctl->qc_id == 0) {
8411 "%s setquota: can not set default quota for root user/group/project\n",
8417 if ((qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8418 qctl->qc_cmd == LUSTRE_Q_RESETQID) && qctl->qc_id == 0) {
8420 "%s setquota: can not delete or reset root user/group/project\n",
8426 if (optind != argc - 1) {
8428 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
8429 progname, argv[optind]);
8437 dqb->dqb_bhardlimit = 0;
8438 dqb->dqb_bsoftlimit = 0;
8439 dqb->dqb_ihardlimit = 0;
8440 dqb->dqb_isoftlimit = 0;
8443 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
8444 /* do not set inode limits for Pool Quotas */
8445 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
8446 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
8447 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
8448 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
8449 /* sigh, we can't just set blimits/ilimits */
8450 struct if_quotactl *tmp_qctl;
8452 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8456 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
8457 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8458 strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
8461 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8463 tmp_qctl->qc_type = qctl->qc_type;
8464 tmp_qctl->qc_id = qctl->qc_id;
8466 rc = llapi_quotactl(mnt, tmp_qctl);
8472 if (!(limit_mask & BHLIMIT))
8473 dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
8474 if (!(limit_mask & BSLIMIT))
8475 dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
8476 if (!(limit_mask & IHLIMIT))
8477 dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
8478 if (!(limit_mask & ISLIMIT))
8479 dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
8481 /* Keep grace times if we have got no softlimit arguments */
8482 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
8483 dqb->dqb_valid |= QIF_BTIME;
8484 dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
8487 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
8488 dqb->dqb_valid |= QIF_ITIME;
8489 dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
8494 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
8495 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
8497 if (qctl->qc_cmd == LUSTRE_Q_RESETQID)
8498 rc = lfs_reset_quota(mnt, qctl);
8500 rc = llapi_quotactl(mnt, qctl);
8505 "%s setquota: cannot quotactl '%s' '%s': %s\n",
8507 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
8510 "%s setquota: quotactl failed: %s\n",
8511 progname, strerror(-rc));
8515 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8521 /* Converts seconds value into format string
8522 * result is returned in buf
8524 * 1. result is in descenting order: 1w2d3h4m5s
8525 * 2. zero fields are not filled (except for p. 3): 5d1s
8526 * 3. zero seconds value is presented as "0s"
8528 static char *__sec2str(time_t seconds, char *buf)
8530 const char spec[] = "smhdw";
8531 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8536 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8537 c = seconds / mult[i];
8539 if (c > 0 || (i == 0 && buf == tail))
8540 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8549 static void sec2str(time_t seconds, char *buf, int rc)
8556 tail = __sec2str(seconds, tail);
8558 if (rc && tail - buf < 39) {
8564 static void diff2str(time_t seconds, char *buf, time_t now)
8569 if (seconds <= now) {
8570 strcpy(buf, "expired");
8573 __sec2str(seconds - now, buf);
8576 static void print_quota_title(char *name, struct if_quotactl *qctl,
8577 bool human_readable, bool show_default)
8580 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8581 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8582 "Filesystem", "bquota", "blimit", "bgrace",
8583 "iquota", "ilimit", "igrace");
8585 printf("Disk quotas for %s %s (%cid %u):\n",
8586 qtype_name(qctl->qc_type), name,
8587 *qtype_name(qctl->qc_type), qctl->qc_id);
8588 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8589 "Filesystem", human_readable ? "used" : "kbytes",
8590 "quota", "limit", "grace",
8591 "files", "quota", "limit", "grace");
8595 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8598 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8601 snprintf(buf, buflen, "%5.4gP",
8602 (double)num / ((__u64)1 << 40));
8604 snprintf(buf, buflen, "%5.4gT",
8605 (double)num / (1 << 30));
8607 snprintf(buf, buflen, "%5.4gG",
8608 (double)num / (1 << 20));
8610 snprintf(buf, buflen, "%5.4gM",
8611 (double)num / (1 << 10));
8613 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8617 #ifdef HAVE_NATIVE_CLIENT
8618 /* In the current Lustre implementation, the grace time is either the time
8619 * or the timestamp to be used after some quota ID exceeds the soft limt,
8620 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8622 #define LQUOTA_GRACE_BITS 48
8623 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8624 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8625 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8626 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8627 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8630 #define STRBUF_LEN 24
8631 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8632 int rc, bool h, bool show_default)
8638 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8639 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8640 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8641 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8642 int bover = 0, iover = 0;
8643 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8644 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8646 char strbuf[STRBUF_LEN];
8648 dqb->dqb_btime &= LQUOTA_GRACE_MASK;
8649 dqb->dqb_itime &= LQUOTA_GRACE_MASK;
8651 if (dqb->dqb_bhardlimit &&
8652 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8654 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8655 if (dqb->dqb_btime > now)
8661 if (dqb->dqb_ihardlimit &&
8662 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8664 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8665 if (dqb->dqb_itime > now)
8671 if (strlen(mnt) > 15)
8672 printf("%s\n%15s", mnt, "");
8674 printf("%15s", mnt);
8677 snprintf(timebuf, sizeof(timebuf), "%llu",
8678 (unsigned long long)dqb->dqb_btime);
8680 diff2str(dqb->dqb_btime, timebuf, now);
8682 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8683 strbuf, sizeof(strbuf), h);
8684 if (rc == -EREMOTEIO)
8685 sprintf(numbuf[0], "%s*", strbuf);
8687 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8688 "%s" : "[%s]", strbuf);
8690 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8691 if (type == QC_GENERAL)
8692 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8693 "%s" : "[%s]", strbuf);
8695 sprintf(numbuf[1], "%s", "-");
8697 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8698 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8699 "%s" : "[%s]", strbuf);
8702 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8704 printf(" %7s%c %6s %7s %7s",
8705 numbuf[0], bover ? '*' : ' ', numbuf[1],
8706 numbuf[2], bover > 1 ? timebuf : "-");
8709 snprintf(timebuf, sizeof(timebuf), "%llu",
8710 (unsigned long long)dqb->dqb_itime);
8712 diff2str(dqb->dqb_itime, timebuf, now);
8714 snprintf(numbuf[0], sizeof(numbuf),
8715 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8716 (uintmax_t)dqb->dqb_curinodes);
8718 if (type == QC_GENERAL)
8719 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8721 (uintmax_t)dqb->dqb_isoftlimit);
8723 sprintf(numbuf[1], "%s", "-");
8725 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8726 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8729 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8730 else if (type != QC_OSTIDX)
8731 printf(" %7s%c %6s %7s %7s",
8732 numbuf[0], iover ? '*' : ' ', numbuf[1],
8733 numbuf[2], iover > 1 ? timebuf : "-");
8735 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8737 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
8738 qctl->qc_cmd == LUSTRE_Q_GETINFOPOOL ||
8739 qctl->qc_cmd == Q_GETOINFO) {
8743 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8744 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8746 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8747 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8748 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8750 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8752 printf("Block grace time: %s; Inode grace time: %s\n",
8753 bgtimebuf, igtimebuf);
8757 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8761 /* format is "lustre-OST0001" */
8762 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8764 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8769 *idx = strtoul(dash, &endp, 16);
8770 if (*idx > 0xffff) {
8771 fprintf(stderr, "wrong index %s\n", tgtname);
8778 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8779 bool h, __u64 *total)
8781 int rc = 0, rc1 = 0, count = 0, i = 0;
8782 char **list = NULL, *buffer = NULL;
8783 __u32 valid = qctl->qc_valid;
8785 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8788 /* Is it correct for the case OST0000, OST0002, OST0003 -
8789 * we will ask OST0001 that is absent and won't ask OST0003? */
8790 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8792 fprintf(stderr, "can not get %s count: %s\n",
8793 is_mdt ? "mdt" : "ost", strerror(-rc));
8797 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8798 char fname[PATH_MAX];
8799 char fsname[LUSTRE_MAXFSNAME + 1];
8800 int bufsize = sizeof(struct obd_uuid) * count;
8802 rc = llapi_search_fsname(mnt, fsname);
8804 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8808 buffer = malloc(bufsize + sizeof(*list) * count);
8811 list = (char **)(buffer + bufsize);
8812 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8813 count = llapi_get_poolmembers(fname, list, count,
8819 for (i = 0; i < count; i++) {
8820 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8823 if (tgt_name2index(list[i], &index))
8825 qctl->qc_idx = index;
8830 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8831 rc = llapi_quotactl(mnt, qctl);
8833 /* It is remote client case. */
8834 if (rc == -EOPNOTSUPP) {
8839 /* no target for this index yet */
8840 if (rc == -ENODEV) {
8845 /* inactive target */
8846 if (rc == -ENODATA) {
8847 char name[UUID_MAX+8];
8849 snprintf(name, sizeof(name), "%s[inact]",
8850 obd_uuid2str(&qctl->obd_uuid));
8851 memset(&qctl->qc_dqinfo, 0,
8852 sizeof(qctl->qc_dqinfo));
8853 memset(&qctl->qc_dqblk, 0,
8854 sizeof(qctl->qc_dqblk));
8855 print_quota(name, qctl, qctl->qc_valid, 0, h,
8863 fprintf(stderr, "quotactl %s%d failed.\n",
8864 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8868 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8869 qctl->qc_valid, 0, h, false);
8870 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8871 qctl->qc_dqblk.dqb_bhardlimit;
8876 qctl->qc_valid = valid;
8880 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8881 int verbose, int quiet, bool human_readable,
8884 int rc1 = 0, rc2 = 0, rc3 = 0;
8885 char *obd_type = (char *)qctl->obd_type;
8886 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8887 __u64 total_ialloc = 0, total_balloc = 0;
8888 bool use_default_for_blk = false;
8889 bool use_default_for_file = false;
8892 rc1 = llapi_quotactl(mnt, qctl);
8896 fprintf(stderr, "%s quotas are not enabled.\n",
8897 qtype_name(qctl->qc_type));
8900 fprintf(stderr, "Permission denied.\n");
8903 /* We already got error message. */
8906 fprintf(stderr, "Unexpected quotactl error: %s\n",
8911 if (!show_default && qctl->qc_id == 0) {
8912 qctl->qc_dqblk.dqb_bhardlimit = 0;
8913 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8914 qctl->qc_dqblk.dqb_ihardlimit = 0;
8915 qctl->qc_dqblk.dqb_isoftlimit = 0;
8916 qctl->qc_dqblk.dqb_btime = 0;
8917 qctl->qc_dqblk.dqb_itime = 0;
8918 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8921 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8922 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8923 use_default_for_blk = true;
8924 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8927 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8928 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8929 use_default_for_file = true;
8930 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8933 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8934 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8935 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8936 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8937 print_quota_title(name, qctl, human_readable, show_default);
8939 if (rc1 && *obd_type)
8940 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8942 if (qctl->qc_valid != QC_GENERAL)
8945 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8946 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8947 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8948 (QIF_LIMITS|QIF_USAGE));
8950 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8952 if (!show_default && verbose &&
8953 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8954 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8955 char strbuf[STRBUF_LEN];
8957 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8959 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8961 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8963 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8964 (uintmax_t)total_ialloc, strbuf);
8967 if (use_default_for_blk)
8968 printf("%cid %u is using default block quota setting\n",
8969 *qtype_name(qctl->qc_type), qctl->qc_id);
8971 if (use_default_for_file)
8972 printf("%cid %u is using default file quota setting\n",
8973 *qtype_name(qctl->qc_type), qctl->qc_id);
8975 if (rc1 || rc2 || rc3 || inacc)
8976 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8990 static int lfs_project(int argc, char **argv)
8992 int ret = 0, err = 0, c, i;
8993 struct project_handle_control phc = { 0 };
8994 enum lfs_project_ops_t op;
8997 phc.assign_projid = false;
8998 /* default action */
8999 op = LFS_PROJECT_LIST;
9001 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
9004 if (op != LFS_PROJECT_LIST) {
9006 "%s: cannot specify '-c' '-C' '-s' together\n",
9011 op = LFS_PROJECT_CHECK;
9014 if (op != LFS_PROJECT_LIST) {
9016 "%s: cannot specify '-c' '-C' '-s' together\n",
9021 op = LFS_PROJECT_CLEAR;
9024 if (op != LFS_PROJECT_LIST) {
9026 "%s: cannot specify '-c' '-C' '-s' together\n",
9031 phc.set_inherit = true;
9032 op = LFS_PROJECT_SET;
9038 phc.keep_projid = true;
9041 phc.recursive = true;
9044 if (str2quotaid(&phc.projid, optarg)) {
9046 "Invalid project ID: %s\n",
9051 phc.assign_projid = true;
9055 phc.newline = false;
9058 fprintf(stderr, "%s: invalid option '%c'\n",
9064 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
9065 op = LFS_PROJECT_SET;
9066 phc.set_projid = true;
9067 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
9068 phc.set_projid = true;
9072 case LFS_PROJECT_CHECK:
9073 if (phc.keep_projid) {
9075 "%s: '-k' is useless together with '-c'\n",
9080 case LFS_PROJECT_CLEAR:
9083 "%s: '-0' is useless together with '-C'\n",
9087 if (phc.assign_projid) {
9089 "%s: '-p' is useless together with '-C'\n",
9094 case LFS_PROJECT_SET:
9097 "%s: '-0' is useless together with '-s'\n",
9101 if (phc.keep_projid) {
9103 "%s: '-k' is useless together with '-s'\n",
9111 "%s: '-0' is useless for list operations\n",
9121 fprintf(stderr, "%s: missing file or directory target(s)\n",
9126 for (i = 0; i < argc; i++) {
9128 case LFS_PROJECT_CHECK:
9129 err = lfs_project_check(argv[i], &phc);
9131 case LFS_PROJECT_LIST:
9132 err = lfs_project_list(argv[i], &phc);
9134 case LFS_PROJECT_CLEAR:
9135 err = lfs_project_clear(argv[i], &phc);
9137 case LFS_PROJECT_SET:
9138 err = lfs_project_set(argv[i], &phc);
9150 static int lfs_quota(int argc, char **argv)
9153 char *mnt, *name = NULL;
9154 struct if_quotactl *qctl;
9156 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
9157 __u32 valid = QC_GENERAL, idx = 0;
9158 bool human_readable = false;
9159 bool show_default = false;
9161 bool show_pools = false;
9162 struct option long_opts[] = {
9163 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
9165 char **poollist = NULL;
9169 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
9173 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
9174 qctl->qc_type = ALLQUOTA;
9175 obd_uuid = (char *)qctl->obd_uuid.uuid;
9177 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
9178 long_opts, NULL)) != -1) {
9181 show_default = true;
9186 show_default = true;
9191 show_default = true;
9195 if (qctl->qc_type != ALLQUOTA) {
9197 "%s quota: only one of -u, -g, or -p may be specified\n",
9202 qctl->qc_type = qtype;
9205 qctl->qc_cmd = LUSTRE_Q_GETINFO;
9208 valid = qctl->qc_valid = QC_UUID;
9209 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
9212 valid = qctl->qc_valid = QC_MDTIDX;
9213 idx = qctl->qc_idx = atoi(optarg);
9214 if (idx == 0 && *optarg != '0') {
9216 "%s quota: invalid MDT index '%s'\n",
9223 valid = qctl->qc_valid = QC_OSTIDX;
9224 idx = qctl->qc_idx = atoi(optarg);
9225 if (idx == 0 && *optarg != '0') {
9227 "%s quota: invalid OST index '%s'\n",
9240 human_readable = true;
9243 if ((!optarg) && (argv[optind] != NULL) &&
9244 (argv[optind][0] != '-') &&
9245 (argv[optind][0] != '/')) {
9246 optarg = argv[optind++];
9247 if (lfs_verify_poolarg(optarg)) {
9251 strncpy(qctl->qc_poolname, optarg,
9253 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
9254 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
9256 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9260 /* optarg is NULL */
9262 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9265 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
9266 progname, argv[optind - 1]);
9272 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
9273 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9274 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
9275 qctl->qc_type == ALLQUOTA &&
9276 optind == argc - 1 && !show_default) {
9279 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
9280 qctl->qc_type = qtype;
9281 qctl->qc_valid = valid;
9282 if (qtype == USRQUOTA) {
9283 qctl->qc_id = geteuid();
9284 rc = uid2name(&name, qctl->qc_id);
9286 qctl->qc_id = getegid();
9287 rc = gid2name(&name, qctl->qc_id);
9288 memset(&qctl->qc_dqblk, 0,
9289 sizeof(qctl->qc_dqblk));
9294 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
9295 human_readable, show_default);
9300 /* lfs quota -u username /path/to/lustre/mount */
9301 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9302 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
9303 /* options should be followed by u/g-name and mntpoint */
9304 if ((!show_default && optind + 2 != argc) ||
9305 (show_default && optind + 1 != argc) ||
9306 qctl->qc_type == ALLQUOTA) {
9308 "%s quota: name and mount point must be specified\n",
9314 if (!show_default) {
9315 name = argv[optind++];
9316 switch (qctl->qc_type) {
9318 rc = name2uid(&qctl->qc_id, name);
9321 rc = name2gid(&qctl->qc_id, name);
9324 rc = name2projid(&qctl->qc_id, name);
9331 qctl->qc_valid = QC_GENERAL;
9332 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
9333 LUSTRE_Q_GETDEFAULT_POOL :
9334 LUSTRE_Q_GETDEFAULT;
9339 if (str2quotaid(&qctl->qc_id, name)) {
9340 fprintf(stderr, "%s quota: invalid id '%s'\n",
9346 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
9347 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
9358 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
9362 for (i = 0; i < poolcount; i++) {
9363 p = memchr(poollist[i], '.', MAXNAMLEN);
9365 fprintf(stderr, "bad string format %.*s\n",
9366 MAXNAMLEN, poollist[i]);
9371 printf("Quotas for pool: %s\n", p);
9372 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
9373 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9374 human_readable, show_default);
9381 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9382 human_readable, show_default);
9388 #endif /* HAVE_SYS_QUOTA_H! */
9390 static int flushctx_ioctl(char *mp)
9394 fd = open(mp, O_RDONLY);
9396 fprintf(stderr, "flushctx: error open %s: %s\n",
9397 mp, strerror(errno));
9401 rc = ioctl(fd, LL_IOC_FLUSHCTX);
9403 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
9404 mp, strerror(errno));
9410 static int lfs_flushctx(int argc, char **argv)
9412 int kdestroy = 0, reap = 0, c;
9413 char mntdir[PATH_MAX] = {'\0'};
9417 while ((c = getopt(argc, argv, "kr")) != -1) {
9427 "error: %s: option '-%c' unrecognized\n",
9434 rc = system("kdestroy > /dev/null");
9436 rc = WEXITSTATUS(rc);
9438 "error destroying tickets: %d, continuing\n",
9443 if (optind >= argc) {
9444 /* flush for all mounted lustre fs. */
9445 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
9446 /* Check if we have a mount point */
9447 if (mntdir[0] == '\0')
9450 if (flushctx_ioctl(mntdir))
9453 mntdir[0] = '\0'; /* avoid matching in next loop */
9456 /* flush fs as specified */
9457 while (optind < argc) {
9458 if (flushctx_ioctl(argv[optind++]))
9464 rc = system("keyctl reap > /dev/null");
9466 rc = WEXITSTATUS(rc);
9467 fprintf(stderr, "error reaping keyring: %d\n", rc);
9474 static int lfs_changelog(int argc, char **argv)
9476 void *changelog_priv;
9477 struct changelog_rec *rec;
9478 long long startrec = 0, endrec = 0;
9480 struct option long_opts[] = {
9481 { .val = 'f', .name = "follow", .has_arg = no_argument },
9483 char short_opts[] = "f";
9486 while ((rc = getopt_long(argc, argv, short_opts,
9487 long_opts, NULL)) != -1) {
9494 "%s changelog: unrecognized option '%s'\n",
9495 progname, argv[optind - 1]);
9499 if (optind >= argc) {
9500 fprintf(stderr, "%s changelog: mdtname must be specified\n",
9505 mdd = argv[optind++];
9506 if (argc > optind) {
9508 startrec = strtoll(argv[optind++], NULL, 10);
9509 if (errno != 0 || startrec < 0) {
9511 "%s changelog: bad startrec\n",
9517 if (argc > optind) {
9519 endrec = strtoll(argv[optind++], NULL, 10);
9520 if (errno != 0 || endrec < 0) {
9522 "%s changelog: bad endrec\n",
9528 rc = llapi_changelog_start(&changelog_priv,
9529 CHANGELOG_FLAG_BLOCK |
9530 CHANGELOG_FLAG_JOBID |
9531 CHANGELOG_FLAG_EXTRA_FLAGS |
9532 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
9535 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
9536 progname, strerror(errno = -rc));
9540 rc = llapi_changelog_set_xflags(changelog_priv,
9541 CHANGELOG_EXTRA_FLAG_UIDGID |
9542 CHANGELOG_EXTRA_FLAG_NID |
9543 CHANGELOG_EXTRA_FLAG_OMODE |
9544 CHANGELOG_EXTRA_FLAG_XATTR);
9547 "%s changelog: cannot set xflags for changelog: %s\n",
9548 progname, strerror(errno = -rc));
9552 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9556 if (endrec && rec->cr_index > endrec) {
9557 llapi_changelog_free(&rec);
9560 if (rec->cr_index < startrec) {
9561 llapi_changelog_free(&rec);
9565 secs = rec->cr_time >> 30;
9566 gmtime_r(&secs, &ts);
9567 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9568 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9569 changelog_type2str(rec->cr_type),
9570 ts.tm_hour, ts.tm_min, ts.tm_sec,
9571 (int)(rec->cr_time & ((1 << 30) - 1)),
9572 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9573 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9575 if (rec->cr_flags & CLF_JOBID) {
9576 struct changelog_ext_jobid *jid =
9577 changelog_rec_jobid(rec);
9579 if (jid->cr_jobid[0] != '\0')
9580 printf(" j=%s", jid->cr_jobid);
9583 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
9584 struct changelog_ext_extra_flags *ef =
9585 changelog_rec_extra_flags(rec);
9587 printf(" ef=0x%llx",
9588 (unsigned long long)ef->cr_extra_flags);
9590 if (ef->cr_extra_flags & CLFE_UIDGID) {
9591 struct changelog_ext_uidgid *uidgid =
9592 changelog_rec_uidgid(rec);
9594 printf(" u=%llu:%llu",
9595 (unsigned long long)uidgid->cr_uid,
9596 (unsigned long long)uidgid->cr_gid);
9598 if (ef->cr_extra_flags & CLFE_NID) {
9599 struct changelog_ext_nid *nid =
9600 changelog_rec_nid(rec);
9603 libcfs_nid2str(nid->cr_nid));
9606 if (ef->cr_extra_flags & CLFE_OPEN) {
9607 struct changelog_ext_openmode *omd =
9608 changelog_rec_openmode(rec);
9609 char mode[] = "---";
9611 /* exec mode must be exclusive */
9612 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9615 if (omd->cr_openflags & MDS_FMODE_READ)
9617 if (omd->cr_openflags &
9624 if (strcmp(mode, "---") != 0)
9625 printf(" m=%s", mode);
9628 if (ef->cr_extra_flags & CLFE_XATTR) {
9629 struct changelog_ext_xattr *xattr =
9630 changelog_rec_xattr(rec);
9632 if (xattr->cr_xattr[0] != '\0')
9633 printf(" x=%s", xattr->cr_xattr);
9637 if (!fid_is_zero(&rec->cr_pfid))
9638 printf(" p="DFID, PFID(&rec->cr_pfid));
9639 if (rec->cr_namelen)
9640 printf(" %.*s", rec->cr_namelen,
9641 changelog_rec_name(rec));
9643 if (rec->cr_flags & CLF_RENAME) {
9644 struct changelog_ext_rename *rnm =
9645 changelog_rec_rename(rec);
9647 if (!fid_is_zero(&rnm->cr_sfid))
9648 printf(" s="DFID" sp="DFID" %.*s",
9649 PFID(&rnm->cr_sfid),
9650 PFID(&rnm->cr_spfid),
9651 (int)changelog_rec_snamelen(rec),
9652 changelog_rec_sname(rec));
9656 llapi_changelog_free(&rec);
9659 llapi_changelog_fini(&changelog_priv);
9662 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9663 progname, strerror(errno = -rc));
9665 return (rc == 1 ? 0 : rc);
9668 static int lfs_changelog_clear(int argc, char **argv)
9677 endrec = strtoll(argv[3], NULL, 10);
9678 if (errno != 0 || endrec < 0) {
9680 "%s: bad endrec '%s'\n",
9685 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9688 fprintf(stderr, "%s: record out of range: %llu\n",
9690 else if (rc == -ENOENT)
9691 fprintf(stderr, "%s: no changelog user: %s\n",
9694 fprintf(stderr, "%s error: %s\n", argv[0],
9703 static void rstripc(char *str, int c)
9705 char *end = str + strlen(str);
9707 for (; str < end && end[-1] == c; --end)
9711 /* Helper function to lfs_fid2path. To print out only the file names and
9712 * not the full path. Do not call OBD_IOC_FID2PATH for every file. Instead
9713 * read the trusted.link xattr and loop over all the records to get all the
9716 static int lfs_fid2path_prn_name(char *mnt_dir, char *path_buf,
9717 bool print_linkno, bool print_fid, char *ptr,
9718 const char *fid_str, int linktmp)
9720 char buf[65536]; /* BUFFER_SIZE 65536 */
9721 char full_path[PATH_MAX * 2 + 2];
9722 struct link_ea_header *leh;
9723 struct link_ea_entry *lee;
9725 int reclen, i, rc = 0;
9727 /* Generate full_path */
9728 snprintf(full_path, sizeof(full_path) - 1, "%s/%s", mnt_dir, path_buf);
9730 size = getxattr(full_path, "trusted.link", buf, sizeof(buf));
9732 fprintf(stderr, "%s: failed to read %s xattr: %s\n", path_buf,
9733 "trusted.link", strerror(errno));
9738 leh = (struct link_ea_header *)buf;
9740 if (leh->leh_magic == __swab32(LINK_EA_MAGIC))
9741 leh->leh_reccount = __swab32(leh->leh_reccount);
9743 lee = (struct link_ea_entry *)(leh + 1);
9745 for (i = 0; i < leh->leh_reccount; i++) {
9746 reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
9748 /* handle -n -l case */
9750 ptr = strrchr(path_buf, '/');
9756 if (strcmp(ptr, lee->lee_name) == 0) {
9758 printf("%s ", fid_str);
9760 printf("%d ", linktmp);
9761 printf("%s\n", lee->lee_name);
9766 printf("%s ", fid_str);
9767 printf("%s\n", lee->lee_name);
9770 /* Get next record */
9771 lee = (struct link_ea_entry *)((char *)lee + reclen);
9777 static int lfs_fid2path(int argc, char **argv)
9779 struct option long_opts[] = {
9780 { .val = '0', .name = "print0", .has_arg = no_argument },
9781 { .val = 'c', .name = "cur", .has_arg = no_argument },
9782 { .val = 'c', .name = "current", .has_arg = no_argument },
9783 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9784 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9785 { .val = 'l', .name = "link", .has_arg = required_argument },
9786 { .val = 'n', .name = "name", .has_arg = no_argument },
9788 char short_opts[] = "0cfl:pr:n";
9789 bool print_only_fname = false;
9790 bool print_linkno = false;
9791 bool print_link = false;
9792 bool print_fid = false;
9794 char mnt_dir[PATH_MAX] = "";
9796 char *path_or_fsname;
9797 long long recno = -1;
9799 char *endptr = NULL;
9800 char link_separator = '\n';
9805 while ((c = getopt_long(argc, argv, short_opts,long_opts, NULL)) !=
9809 link_separator = '\0';
9819 linkno = strtol(optarg, &endptr, 10);
9820 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9822 "%s fid2path: invalid linkno '%s'\n",
9826 print_linkno = true;
9829 /* Bypass the full parent path if true
9830 * only print the final filename */
9831 print_only_fname = true;
9834 /* recno is something to do with changelogs
9835 * that was never implemented. We just pass it
9836 * through for the MDT to ignore.
9839 recno = strtoll(optarg, &endptr, 10);
9840 if (errno != 0 || *endptr != '\0' || recno < 0) {
9842 "%s fid2path: invalid recno '%s'\n",
9849 "%s fid2path: unrecognized option '%s'\n",
9850 progname, argv[optind - 1]);
9855 if (argc - optind < 2) {
9857 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9862 path_or_fsname = argv[optind];
9864 if (*path_or_fsname == '/') {
9865 print_mnt_dir = true;
9866 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9868 print_mnt_dir = false;
9869 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9874 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9875 progname, path_or_fsname, strerror(-rc));
9879 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9882 "%s fid2path: cannot open mount point for '%s': %s\n",
9883 progname, path_or_fsname, strerror(-rc));
9887 /* Strip trailing slashes from mnt_dir. */
9888 rstripc(mnt_dir + 1, '/');
9890 for (i = optind + 1; i < argc; i++) {
9891 const char *fid_str = argv[i];
9896 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9899 "%s fid2path: invalid FID '%s'\n",
9907 int linktmp = (linkno >= 0) ? linkno : 0;
9910 int oldtmp = linktmp;
9911 long long rectmp = recno;
9912 char path_buf[PATH_MAX];
9914 rc2 = llapi_fid2path_at(mnt_fd, &fid, path_buf,
9915 sizeof(path_buf), &rectmp,
9919 "%s fid2path: cannot find %s %s: %s\n",
9920 progname, path_or_fsname, fid_str,
9927 if (print_only_fname && !print_link) {
9928 /* '-n' is passed as option here.
9929 * For all other cases of -c fall back
9930 * to default(else) path as to get the link
9931 * count associated with the file name call
9932 * to OBD_IOC_FID2PATH is required
9934 rc = lfs_fid2path_prn_name(mnt_dir,
9939 /* llapi_fid2path_at() is already called once
9940 * in this case. No need to call it again.
9941 * Break out as we have all the filenames.
9947 printf("%s ", fid_str);
9950 printf("%d ", linktmp);
9952 /* You may think this looks wrong or weird (and it is!)
9953 * but we are actually trying to preserve the old quirky
9954 * behaviors (enforced by our old quirky tests!) that
9955 * make lfs so much fun to work on:
9957 * lustre 0x200000007:0x1:0x0 => "/"
9958 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9960 * Note that llapi_fid2path() returns "" for the root
9962 if (!print_only_fname) {
9964 print_mnt_dir ? mnt_dir : "",
9965 (print_mnt_dir || *path_buf == '\0') ?
9966 "/" : "", path_buf, link_separator);
9968 ptr = strrchr(path_buf, '/');
9970 printf("%s\n", path_buf);
9972 printf("%s\n", ptr + 1);
9976 /* specified linkno */
9979 if (oldtmp == linktmp)
9991 static int lfs_path2fid(int argc, char **argv)
9993 struct option long_opts[] = {
9994 { .val = 'p', .name = "parents", .has_arg = no_argument },
9997 const char short_opts[] = "p";
9998 const char *sep = "";
10001 bool show_parents = false;
10003 while ((rc = getopt_long(argc, argv, short_opts,
10004 long_opts, NULL)) != -1) {
10007 show_parents = true;
10011 "%s path2fid: unrecognized option '%s'\n",
10012 progname, argv[optind - 1]);
10017 if (optind > argc - 1) {
10018 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
10021 } else if (optind < argc - 1) {
10026 for (path = argv + optind; optind < argc; path++, optind++) {
10029 if (!show_parents) {
10030 err = llapi_path2fid(*path, &fid);
10032 printf("%s%s"DFID"\n",
10033 *sep != '\0' ? *path : "", sep,
10036 char name[NAME_MAX + 1];
10037 unsigned int linkno = 0;
10039 while ((err = llapi_path2parent(*path, linkno, &fid,
10040 name, sizeof(name))) == 0) {
10041 if (*sep != '\0' && linkno == 0)
10042 printf("%s%s", *path, sep);
10044 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
10049 /* err == -ENODATA is end-of-loop */
10050 if (linkno > 0 && err == -ENODATA) {
10058 "%s path2fid: cannot get %sfid for '%s': %s\n",
10059 progname, show_parents ? "parent " : "", *path,
10071 #define MAX_ERRNO 4095
10072 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
10074 static int lfs_rmfid_and_show_errors(int rootfd, struct fid_array *fa)
10078 rc = llapi_rmfid_at(rootfd, fa);
10080 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
10081 progname, strerror(-rc));
10085 for (k = 0; k < fa->fa_nr; k++) {
10086 rc2 = (__s32)fa->fa_fids[k].f_ver;
10087 if (!IS_ERR_VALUE(rc2))
10093 fa->fa_fids[k].f_ver = 0;
10094 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
10095 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
10101 static int lfs_rmfid(int argc, char **argv)
10103 int rc = 0, rc2, rc3 = 0, nr;
10104 struct fid_array *fa;
10105 const char *device;
10109 /* Interactive mode: Adjust optind */
10113 device = argv[optind++];
10115 if (optind > argc - 1) {
10116 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
10120 nr = argc - optind;
10122 rc = llapi_root_path_open(device, &rootfd);
10125 "%s rmfid: error opening device/fsname '%s': %s\n",
10126 progname, device, strerror(-rc));
10130 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
10132 fprintf(stderr, "%s rmfid: error allocating %zd bytes: %s\n",
10133 progname, offsetof(struct fid_array, fa_fids[nr + 1]),
10140 while (optind < argc) {
10144 origfidstr = fidstr = argv[optind++];
10145 while (*fidstr == '[')
10147 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
10149 fprintf(stderr, "lfs rmfid: '%s': Wrong FID format\n",
10152 rc3 = -EINVAL; /* Invalid argument */
10156 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
10157 /* start another batch */
10158 rc2 = lfs_rmfid_and_show_errors(rootfd, fa);
10167 rc2 = lfs_rmfid_and_show_errors(rootfd, fa);
10183 static int lfs_data_version(int argc, char **argv)
10185 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
10186 __u64 data_version;
10193 fprintf(stderr, "%s: FILE must be specified\n",
10198 while ((c = getopt(argc, argv, "hnrw")) != -1) {
10201 data_version_flags = 0;
10204 data_version_flags |= LL_DV_RD_FLUSH;
10207 data_version_flags |= LL_DV_WR_FLUSH;
10211 "%s data_version: unrecognized option '%s'\n",
10212 progname, argv[optind - 1]);
10218 if (optind == argc) {
10219 fprintf(stderr, "%s data_version: FILE must be specified\n",
10224 path = argv[optind];
10225 fd = open(path, O_RDONLY);
10228 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
10229 progname, path, strerror(-rc));
10233 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
10236 "%s data_version: cannot get version for '%s': %s\n",
10237 progname, path, strerror(-rc));
10239 printf("%ju" "\n", (uintmax_t)data_version);
10245 static int lfs_hsm_state(int argc, char **argv)
10250 struct hsm_user_state hus;
10259 rc2 = llapi_hsm_state_get(path, &hus);
10262 "%s %s: get HSM state for '%s' failed: %s\n",
10263 progname, argv[0], path, strerror(-rc2));
10269 /* Display path name and status flags */
10270 printf("%s: (0x%08x)", path, hus.hus_states);
10272 if (hus.hus_states & HS_RELEASED)
10273 printf(" released");
10274 if (hus.hus_states & HS_EXISTS)
10276 if (hus.hus_states & HS_DIRTY)
10278 if (hus.hus_states & HS_ARCHIVED)
10279 printf(" archived");
10280 /* Display user-settable flags */
10281 if (hus.hus_states & HS_NORELEASE)
10282 printf(" never_release");
10283 if (hus.hus_states & HS_NOARCHIVE)
10284 printf(" never_archive");
10285 if (hus.hus_states & HS_LOST)
10286 printf(" lost_from_hsm");
10288 if (hus.hus_archive_id != 0)
10289 printf(", archive_id:%d", hus.hus_archive_id);
10292 } while (++i < argc);
10297 #define LFS_HSM_SET 0
10298 #define LFS_HSM_CLEAR 1
10301 * Generic function to set or clear HSM flags.
10302 * Used by hsm_set and hsm_clear.
10304 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
10306 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
10308 struct option long_opts[] = {
10309 { .val = 'A', .name = "archived", .has_arg = no_argument },
10310 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
10311 { .val = 'd', .name = "dirty", .has_arg = no_argument },
10312 { .val = 'e', .name = "exists", .has_arg = no_argument },
10313 { .val = 'h', .name = "help", .has_arg = no_argument },
10314 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
10315 { .val = 'l', .name = "lost", .has_arg = no_argument },
10316 { .val = 'r', .name = "norelease", .has_arg = no_argument },
10317 { .name = NULL } };
10321 __u32 archive_id = 0;
10327 while ((c = getopt_long(argc, argv, "aAdehi:lr",
10328 long_opts, NULL)) != -1) {
10334 mask |= HS_NOARCHIVE;
10337 mask |= HS_ARCHIVED;
10340 mask |= HS_NORELEASE;
10350 archive_id = strtol(optarg, &end, 10);
10351 if (errno != 0 || *end != '\0' || archive_id < 0) {
10353 "%s: invalid archive_id: '%s'\n",
10359 fprintf(stderr, "%s: unrecognized option '%s'\n",
10360 progname, argv[optind - 1]);
10367 /* User should have specified a flag */
10371 while (optind < argc) {
10373 path = argv[optind];
10375 /* If mode == 0, this means we apply the mask. */
10376 if (mode == LFS_HSM_SET)
10377 rc2 = llapi_hsm_state_set(path, mask, 0, archive_id);
10379 rc2 = llapi_hsm_state_set(path, 0, mask, 0);
10383 "%s %s: change hsm flags for '%s' failed: %s\n",
10384 progname, argv[0], path, strerror(-rc2));
10394 static int lfs_hsm_action(int argc, char **argv)
10396 struct hsm_current_action hca;
10397 struct hsm_extent he;
10398 enum hsm_user_action hua;
10399 enum hsm_progress_states hps;
10411 rc2 = llapi_hsm_current_action(path, &hca);
10414 "%s %s: get hsm action for '%s' failed: %s\n",
10415 progname, argv[0], path, strerror(-rc2));
10421 he = hca.hca_location;
10422 hua = hca.hca_action;
10423 hps = hca.hca_state;
10425 printf("%s: %s", path, hsm_user_action2name(hua));
10427 /* Skip file without action */
10428 if (hca.hca_action == HUA_NONE) {
10433 printf(" %s ", hsm_progress_state2name(hps));
10435 if ((hps == HPS_RUNNING) &&
10436 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
10437 printf("(%llu bytes moved)\n",
10438 (unsigned long long)he.length);
10439 else if ((he.offset + he.length) == LUSTRE_EOF)
10440 printf("(from %llu to EOF)\n",
10441 (unsigned long long)he.offset);
10443 printf("(from %llu to %llu)\n",
10444 (unsigned long long)he.offset,
10445 (unsigned long long)(he.offset + he.length));
10447 } while (++i < argc);
10452 static int lfs_hsm_set(int argc, char **argv)
10454 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
10457 static int lfs_hsm_clear(int argc, char **argv)
10459 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
10463 * Check file state and return its fid, to be used by lfs_hsm_request().
10465 * \param[in] file Path to file to check
10466 * \param[in,out] fid Pointer to allocated lu_fid struct.
10467 * \param[in,out] last_dev Pointer to last device id used.
10469 * \return 0 on success.
10471 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
10477 rc = lstat(file, &st);
10479 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
10483 * Checking for regular file as archiving as posix copytool
10484 * rejects archiving files other than regular files
10486 if (!S_ISREG(st.st_mode)) {
10487 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
10490 /* A request should be ... */
10491 if (*last_dev != st.st_dev && *last_dev != 0) {
10493 "All files should be on the same filesystem: %s\n",
10497 *last_dev = st.st_dev;
10499 rc = llapi_path2fid(file, fid);
10501 fprintf(stderr, "Cannot read FID of %s: %s\n",
10502 file, strerror(-rc));
10508 /* Fill an HSM HUR item with a given file name.
10510 * If mntpath is set, then the filename is actually a FID, and no
10511 * lookup on the filesystem will be performed.
10513 * \param[in] hur the user request to fill
10514 * \param[in] idx index of the item inside the HUR to fill
10515 * \param[in] mntpath mountpoint of Lustre
10516 * \param[in] fname filename (if mtnpath is NULL)
10517 * or FID (if mntpath is set)
10518 * \param[in] last_dev pointer to last device id used
10520 * \retval 0 on success
10521 * \retval CMD_HELP or a negative errno on error
10523 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
10524 const char *mntpath, const char *fname,
10527 struct hsm_user_item *hui = &hur->hur_user_item[idx];
10530 hui->hui_extent.length = -1;
10533 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
10535 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
10538 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
10542 hur->hur_request.hr_itemcount++;
10547 static int lfs_hsm_request(int argc, char **argv, int action)
10549 struct option long_opts[] = {
10550 { .val = 'a', .name = "archive", .has_arg = required_argument },
10551 { .val = 'D', .name = "data", .has_arg = required_argument },
10552 { .val = 'h', .name = "help", .has_arg = no_argument },
10553 { .val = 'l', .name = "filelist", .has_arg = required_argument },
10554 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
10555 { .name = NULL } };
10556 dev_t last_dev = 0;
10557 struct hsm_user_request *hur, *oldhur;
10562 char *filelist = NULL;
10563 char fullpath[PATH_MAX];
10564 char *opaque = NULL;
10565 int opaque_len = 0;
10566 int archive_id = 0;
10568 int nbfile_alloc = 0;
10569 char *some_file = NULL;
10570 char *mntpath = NULL;
10578 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
10579 long_opts, NULL)) != -1) {
10588 if (action != HUA_ARCHIVE &&
10589 action != HUA_REMOVE) {
10591 "error: -a is supported only when archiving or removing\n");
10595 archive_id = atoi(optarg);
10600 some_file = strdup(optarg);
10604 fprintf(stderr, "%s: unrecognized option '%s'\n",
10605 progname, argv[optind - 1]);
10613 /* All remaining args are files, so we have at least nbfile */
10614 nbfile = argc - optind;
10616 if ((nbfile == 0) && (!filelist)) {
10622 opaque_len = strlen(opaque);
10625 * Alloc the request structure with enough place to store all files
10626 * from command line.
10628 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
10630 fprintf(stderr, "Cannot create the request: %s\n",
10635 nbfile_alloc = nbfile;
10637 hur->hur_request.hr_action = action;
10638 hur->hur_request.hr_archive_id = archive_id;
10639 hur->hur_request.hr_flags = 0;
10641 /* All remaining args are files, add them */
10642 if (nbfile != 0 && some_file == NULL)
10643 some_file = strdup(argv[optind]);
10645 for (i = 0; i < nbfile; i++) {
10646 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
10652 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
10654 /* If a filelist was specified, read the filelist from it. */
10656 fp = fopen(filelist, "r");
10658 fprintf(stderr, "Cannot read the file list %s: %s\n",
10659 filelist, strerror(errno));
10664 while ((rc = getline(&line, &len, fp)) != -1) {
10666 * If allocated buffer was too small, get something
10669 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
10672 nbfile_alloc = nbfile_alloc * 2 + 1;
10674 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
10678 "hsm: cannot allocate the request: %s\n",
10685 size = hur_len(oldhur);
10688 "hsm: cannot allocate %u files + %u bytes data\n",
10689 oldhur->hur_request.hr_itemcount,
10690 oldhur->hur_request.hr_data_len);
10697 memcpy(hur, oldhur, size);
10702 if (line[strlen(line) - 1] == '\n')
10703 line[strlen(line) - 1] = '\0';
10705 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
10706 mntpath, line, &last_dev);
10722 /* If a --data was used, add it to the request */
10723 hur->hur_request.hr_data_len = opaque_len;
10725 memcpy(hur_data(hur), opaque, opaque_len);
10727 /* Send the HSM request */
10728 if (realpath(some_file, fullpath) == NULL) {
10729 fprintf(stderr, "Could not find path '%s': %s\n",
10730 some_file, strerror(errno));
10732 rc = llapi_hsm_request(fullpath, hur);
10734 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
10735 some_file, strerror(-rc));
10745 static int lfs_hsm_archive(int argc, char **argv)
10747 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
10750 static int lfs_hsm_restore(int argc, char **argv)
10752 return lfs_hsm_request(argc, argv, HUA_RESTORE);
10755 static int lfs_hsm_release(int argc, char **argv)
10757 return lfs_hsm_request(argc, argv, HUA_RELEASE);
10760 static int lfs_hsm_remove(int argc, char **argv)
10762 return lfs_hsm_request(argc, argv, HUA_REMOVE);
10765 static int lfs_hsm_cancel(int argc, char **argv)
10767 return lfs_hsm_request(argc, argv, HUA_CANCEL);
10770 static int lfs_swap_layouts(int argc, char **argv)
10775 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
10776 SWAP_LAYOUTS_KEEP_MTIME |
10777 SWAP_LAYOUTS_KEEP_ATIME);
10780 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10782 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10784 int lfs_get_mode(const char *string)
10786 enum lock_mode_user mode;
10788 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10789 if (lock_mode_names[mode] == NULL)
10791 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10798 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10800 enum lu_ladvise_type advice;
10803 advice < ARRAY_SIZE(ladvise_names); advice++) {
10804 if (ladvise_names[advice] == NULL)
10806 if (strcmp(string, ladvise_names[advice]) == 0)
10810 return LU_LADVISE_INVALID;
10813 static int lfs_ladvise(int argc, char **argv)
10815 struct option long_opts[] = {
10816 { .val = 'a', .name = "advice", .has_arg = required_argument },
10817 { .val = 'b', .name = "background", .has_arg = no_argument },
10818 { .val = 'e', .name = "end", .has_arg = required_argument },
10819 { .val = 'h', .name = "help", .has_arg = no_argument },
10820 { .val = 'l', .name = "length", .has_arg = required_argument },
10821 { .val = 'm', .name = "mode", .has_arg = required_argument },
10822 { .val = 's', .name = "start", .has_arg = required_argument },
10823 { .val = 'u', .name = "unset", .has_arg = no_argument },
10824 { .name = NULL } };
10825 struct llapi_lu_ladvise advice;
10826 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10827 unsigned long long start = 0;
10828 unsigned long long end = LUSTRE_EOF;
10829 unsigned long long length = 0;
10830 unsigned long long size_units;
10831 unsigned long long flags = 0;
10837 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10838 long_opts, NULL)) != -1) {
10841 advice_type = lfs_get_ladvice(optarg);
10842 if (advice_type == LU_LADVISE_INVALID) {
10844 "%s: invalid advice type '%s'\n",
10846 fprintf(stderr, "Valid types:");
10848 for (advice_type = 0;
10849 advice_type < ARRAY_SIZE(ladvise_names);
10851 if (ladvise_names[advice_type] == NULL)
10853 fprintf(stderr, " %s",
10854 ladvise_names[advice_type]);
10856 fprintf(stderr, "\n");
10869 rc = llapi_parse_size(optarg, &end,
10872 fprintf(stderr, "%s: bad end offset '%s'\n",
10879 rc = llapi_parse_size(optarg, &start,
10883 "%s: bad start offset '%s'\n",
10890 rc = llapi_parse_size(optarg, &length,
10893 fprintf(stderr, "%s: bad length '%s'\n",
10899 mode = lfs_get_mode(optarg);
10902 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10908 fprintf(stderr, "%s: unrecognized option '%s'\n",
10909 progname, argv[optind - 1]);
10916 if (advice_type == LU_LADVISE_INVALID) {
10917 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10918 fprintf(stderr, "Valid types:");
10919 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10921 if (ladvise_names[advice_type] == NULL)
10923 fprintf(stderr, " %s", ladvise_names[advice_type]);
10925 fprintf(stderr, "\n");
10929 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10931 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10936 if (argc <= optind) {
10937 fprintf(stderr, "%s: please give one or more file names\n",
10942 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10943 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10948 if (end == LUSTRE_EOF && length != 0)
10949 end = start + length;
10951 if (end <= start) {
10952 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10953 argv[0], start, end);
10957 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10958 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10963 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10964 fprintf(stderr, "%s: mode is required with lockahead\n",
10969 while (optind < argc) {
10972 path = argv[optind++];
10974 fd = open(path, O_RDONLY);
10977 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10978 argv[0], path, strerror(-rc2));
10984 advice.lla_start = start;
10985 advice.lla_end = end;
10986 advice.lla_advice = advice_type;
10987 advice.lla_value1 = 0;
10988 advice.lla_value2 = 0;
10989 advice.lla_value3 = 0;
10990 advice.lla_value4 = 0;
10991 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10992 advice.lla_lockahead_mode = mode;
10993 advice.lla_peradvice_flags = flags;
10996 rc2 = llapi_ladvise(fd, flags, 1, &advice);
11000 "%s: cannot give advice '%s' to file '%s': %s\n",
11001 argv[0], ladvise_names[advice_type],
11002 path, strerror(errno));
11013 static const char *const heat_names[] = LU_HEAT_NAMES;
11015 static int lfs_heat_get(int argc, char **argv)
11017 struct lu_heat *heat;
11026 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
11028 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
11033 while (optind < argc) {
11034 path = argv[optind++];
11036 fd = open(path, O_RDONLY);
11038 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11039 argv[0], path, strerror(errno));
11044 heat->lh_count = OBD_HEAT_COUNT;
11045 rc2 = llapi_heat_get(fd, heat);
11049 "%s: cannot get heat of file '%s': %s\n",
11050 argv[0], path, strerror(errno));
11054 printf("flags: %x\n", heat->lh_flags);
11055 for (i = 0; i < heat->lh_count; i++)
11056 printf("%s: %llu\n", heat_names[i],
11057 (unsigned long long)heat->lh_heat[i]);
11059 if (rc == 0 && rc2 < 0)
11067 static int lfs_heat_set(int argc, char **argv)
11069 struct option long_opts[] = {
11070 { .val = 'c', .name = "clear", .has_arg = no_argument },
11071 { .val = 'h', .name = "help", .has_arg = no_argument },
11072 { .val = 'o', .name = "off", .has_arg = no_argument },
11073 { .val = 'O', .name = "on", .has_arg = no_argument },
11074 { .name = NULL } };
11075 enum lu_heat_flag flags = 0;
11085 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
11088 flags |= LU_HEAT_FLAG_CLEAR;
11091 flags |= LU_HEAT_FLAG_CLEAR;
11092 flags |= LU_HEAT_FLAG_OFF;
11095 flags &= ~LU_HEAT_FLAG_OFF;
11098 fprintf(stderr, "%s: unrecognized option '%s'\n",
11099 progname, argv[optind - 1]);
11106 if (argc <= optind) {
11107 fprintf(stderr, "%s: please give one or more file names\n",
11112 while (optind < argc) {
11113 path = argv[optind++];
11115 fd = open(path, O_RDONLY);
11117 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11118 argv[0], path, strerror(errno));
11123 rc2 = llapi_heat_set(fd, flags);
11127 "%s: cannot setflags heat of file '%s': %s\n",
11128 argv[0], path, strerror(errno));
11132 if (rc == 0 && rc2 < 0)
11139 * The input string contains a comma delimited list of component ids and
11140 * ranges, for example "1,2-4,7".
11142 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
11144 bool end_of_loop = false;
11152 while (!end_of_loop) {
11156 char *endptr = NULL;
11159 ptr = strchrnul(arg, ',');
11160 end_of_loop = *ptr == '\0';
11163 start_index = strtol(arg, &endptr, 0);
11164 if (endptr == arg) /* no data at all */
11166 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
11168 if (start_index < 0)
11171 end_index = start_index;
11172 if (*endptr == '-') {
11173 end_index = strtol(endptr + 1, &endptr, 0);
11174 if (*endptr != '\0')
11176 if (end_index < start_index)
11180 for (i = start_index; i <= end_index && size > 0; i++) {
11183 /* remove duplicate */
11184 for (j = 0; j < nr; j++) {
11188 if (j == nr) { /* no duplicate */
11194 if (size == 0 && i < end_index)
11201 if (!end_of_loop && ptr)
11204 return rc < 0 ? rc : nr;
11208 * struct verify_mirror_id - Mirror id to be verified.
11209 * @mirror_id: A specified mirror id.
11210 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
11212 struct verify_mirror_id {
11218 * compare_mirror_ids() - Compare mirror ids.
11219 * @layout: Mirror component list.
11220 * @cbdata: Callback data in verify_mirror_id structure.
11222 * This is a callback function called by llapi_layout_comp_iterate()
11223 * to compare the specified mirror id with the one in the current
11224 * component of @layout. If they are the same, then the specified
11225 * mirror id is valid.
11227 * Return: a negative error code on failure or
11228 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
11229 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
11232 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
11234 struct verify_mirror_id *mirror_id_cbdata =
11235 (struct verify_mirror_id *)cbdata;
11236 uint32_t mirror_id;
11239 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11243 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11244 progname, strerror(errno));
11248 if (mirror_id_cbdata->mirror_id == mirror_id) {
11249 mirror_id_cbdata->is_valid_id = true;
11250 return LLAPI_LAYOUT_ITER_STOP;
11253 return LLAPI_LAYOUT_ITER_CONT;
11257 * verify_mirror_ids() - Verify specified mirror ids.
11258 * @fname: Mirrored file name.
11259 * @mirror_ids: Specified mirror ids to be verified.
11260 * @ids_nr: Number of specified mirror ids.
11262 * This function verifies that specified @mirror_ids are valid
11263 * in the mirrored file @fname.
11265 * Return: 0 on success or a negative error code on failure.
11268 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
11270 struct llapi_layout *layout = NULL;
11271 struct verify_mirror_id mirror_id_cbdata = { 0 };
11273 uint32_t flr_state;
11282 if (stat(fname, &stbuf) < 0) {
11283 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11284 progname, fname, strerror(errno));
11289 if (!S_ISREG(stbuf.st_mode)) {
11290 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11296 fd = open(fname, O_DIRECT | O_RDONLY);
11298 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11299 progname, fname, strerror(errno));
11304 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11306 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11307 progname, fname, strerror(errno));
11311 layout = llapi_layout_get_by_fd(fd, 0);
11313 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11314 progname, fname, strerror(errno));
11316 llapi_lease_release(fd);
11320 rc = llapi_layout_flags_get(layout, &flr_state);
11322 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11323 progname, fname, strerror(errno));
11328 flr_state &= LCM_FL_FLR_MASK;
11329 switch (flr_state) {
11332 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11333 progname, fname, llapi_layout_flags_string(flr_state));
11340 for (i = 0; i < ids_nr; i++) {
11341 mirror_id_cbdata.mirror_id = mirror_ids[i];
11342 mirror_id_cbdata.is_valid_id = false;
11344 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
11345 &mirror_id_cbdata);
11349 "%s: '%s' failed to verify mirror id: %u.\n",
11350 progname, fname, mirror_ids[i]);
11354 if (!mirror_id_cbdata.is_valid_id) {
11357 "%s: '%s' invalid specified mirror id: %u.\n",
11358 progname, fname, mirror_ids[i]);
11364 llapi_layout_free(layout);
11365 llapi_lease_release(fd);
11373 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
11374 __u16 *mirror_ids, int ids_nr,
11375 long stats_interval_sec, long bandwidth_bytes_sec)
11377 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11378 struct llapi_layout *layout;
11380 uint32_t flr_state;
11389 if (stat(fname, &stbuf) < 0) {
11390 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11391 progname, fname, strerror(errno));
11395 if (!S_ISREG(stbuf.st_mode)) {
11396 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11402 /* Allow mirror resync even without the key on encrypted files */
11403 fd = open(fname, O_DIRECT | O_RDWR | O_CIPHERTEXT);
11405 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11406 progname, fname, strerror(errno));
11411 layout = llapi_layout_get_by_fd(fd, 0);
11413 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11414 progname, fname, strerror(errno));
11419 rc = llapi_layout_flags_get(layout, &flr_state);
11421 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11422 progname, fname, strerror(errno));
11427 flr_state &= LCM_FL_FLR_MASK;
11428 if (flr_state == LCM_FL_NONE) {
11430 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
11435 /* get stale component info */
11436 comp_size = llapi_mirror_find_stale(layout, comp_array,
11437 ARRAY_SIZE(comp_array),
11438 mirror_ids, ids_nr);
11439 if (comp_size <= 0) {
11444 ioc->lil_mode = LL_LEASE_WRLCK;
11445 ioc->lil_flags = LL_LEASE_RESYNC;
11446 rc = llapi_lease_set(fd, ioc);
11448 if (rc == -EALREADY)
11452 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
11453 progname, fname, strerror(-rc));
11457 /* get the read range [start, end) */
11458 start = comp_array[0].lrc_start;
11459 end = comp_array[0].lrc_end;
11460 for (idx = 1; idx < comp_size; idx++) {
11461 if (comp_array[idx].lrc_start < start)
11462 start = comp_array[idx].lrc_start;
11463 if (end < comp_array[idx].lrc_end)
11464 end = comp_array[idx].lrc_end;
11467 rc = llapi_lease_check(fd);
11468 if (rc != LL_LEASE_WRLCK) {
11469 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11474 rc = llapi_mirror_resync_many_params(fd, layout, comp_array, comp_size,
11475 start, end, stats_interval_sec,
11476 bandwidth_bytes_sec);
11478 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
11479 progname, fname, strerror(-rc));
11481 rc2 = migrate_set_timestamps(fd, &stbuf);
11483 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
11484 progname, fname, strerror(-rc2));
11490 /* need to do the lease unlock even resync fails */
11491 ioc->lil_mode = LL_LEASE_UNLCK;
11492 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11493 ioc->lil_count = 0;
11494 for (idx = 0; idx < comp_size; idx++) {
11495 if (comp_array[idx].lrc_synced) {
11496 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
11501 rc2 = llapi_lease_set(fd, ioc);
11503 * llapi_lease_set returns lease mode when it request to unlock
11507 /* rc2 == 0 means lost lease lock */
11508 if (rc2 == 0 && rc == 0)
11512 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
11514 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
11516 llapi_lease_release(fd);
11521 llapi_layout_free(layout);
11528 static inline int lfs_mirror_resync(int argc, char **argv)
11530 struct option long_opts[] = {
11531 { .val = 'h', .name = "help", .has_arg = no_argument },
11532 { .val = 'o', .name = "only", .has_arg = required_argument },
11533 { .val = 'W', .name = "bandwidth", .has_arg = required_argument },
11534 { .val = LFS_STATS_OPT,
11535 .name = "stats", .has_arg = no_argument},
11536 { .val = LFS_STATS_INTERVAL_OPT,
11537 .name = "stats-interval",
11538 .has_arg = required_argument},
11539 { .name = NULL } };
11540 struct ll_ioc_lease *ioc = NULL;
11541 __u16 mirror_ids[128] = { 0 };
11542 unsigned int stats_interval_sec = 0;
11543 unsigned long long bandwidth_bytes_sec = 0;
11544 unsigned long long bandwidth_unit = ONE_MB;
11549 while ((c = getopt_long(argc, argv, "ho:W:", long_opts, NULL)) >= 0) {
11553 rc = parse_mirror_ids(mirror_ids,
11554 sizeof(mirror_ids) / sizeof(__u16),
11558 "%s: bad mirror ids '%s'.\n",
11565 if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
11566 &bandwidth_unit, 0) < 0) {
11568 "error: %s: bad value for bandwidth '%s'\n",
11573 case LFS_STATS_OPT:
11574 stats_interval_sec = 5;
11576 case LFS_STATS_INTERVAL_OPT:
11577 stats_interval_sec = strtol(optarg, &end, 0);
11580 fprintf(stderr, "%s: unrecognized option '%s'\n",
11581 progname, argv[optind - 1]);
11589 if (argc == optind) {
11590 fprintf(stderr, "%s: no file name given.\n", argv[0]);
11595 if (ids_nr > 0 && argc > optind + 1) {
11597 "%s: option '--only' cannot be used upon multiple files.\n",
11604 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11609 /* set the lease on the file */
11610 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11612 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
11613 argv[0], strerror(errno));
11618 for (; optind < argc; optind++) {
11619 rc = lfs_mirror_resync_file(argv[optind], ioc,
11620 mirror_ids, ids_nr,
11621 stats_interval_sec,
11622 bandwidth_bytes_sec);
11623 /* ignore previous file's error, continue with next file */
11626 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
11634 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
11636 struct llapi_layout *layout;
11639 layout = llapi_layout_get_by_fd(fd, 0);
11641 fprintf(stderr, "could not get layout.\n");
11645 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
11647 fprintf(stderr, "failed to iterate layout\n");
11648 llapi_layout_free(layout);
11651 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
11652 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
11653 llapi_layout_free(layout);
11657 llapi_layout_free(layout);
11663 * Check whether two files are the same file
11664 * \retval 0 same file
11665 * \retval 1 not the same file
11666 * \retval <0 error code
11668 static inline int check_same_file(int fd, const char *f2)
11670 struct stat stbuf1;
11671 struct stat stbuf2;
11673 if (fstat(fd, &stbuf1) < 0)
11676 if (stat(f2, &stbuf2) < 0)
11679 if (stbuf1.st_rdev == stbuf2.st_rdev &&
11680 stbuf1.st_ino == stbuf2.st_ino)
11686 static inline int lfs_mirror_read(int argc, char **argv)
11689 __u16 mirror_id = 0;
11690 const char *outfile = NULL;
11696 const size_t buflen = 4 << 20;
11698 struct option long_opts[] = {
11699 { .val = 'h', .name = "help", .has_arg = no_argument },
11700 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11701 { .val = 'o', .name = "outfile", .has_arg = required_argument },
11702 { .name = NULL } };
11704 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
11709 unsigned long int id;
11712 id = strtoul(optarg, &end, 0);
11713 if (errno != 0 || *end != '\0' || id == 0 ||
11716 "%s %s: invalid mirror ID '%s'\n",
11717 progname, argv[0], optarg);
11721 mirror_id = (__u16)id;
11728 fprintf(stderr, "%s: unrecognized option '%s'\n",
11729 progname, argv[optind - 1]);
11736 if (argc == optind) {
11737 fprintf(stderr, "%s %s: no mirrored file provided\n",
11738 progname, argv[0]);
11740 } else if (argc > optind + 1) {
11741 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11745 if (mirror_id == 0) {
11746 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11747 progname, argv[0]);
11751 /* open mirror file */
11752 fname = argv[optind];
11753 fd = open(fname, O_DIRECT | O_RDONLY);
11755 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11756 progname, argv[0], fname, strerror(errno));
11760 /* verify mirror id */
11761 rc = verify_mirror_id_by_fd(fd, mirror_id);
11764 "%s %s: cannot find mirror with ID %u in '%s'\n",
11765 progname, argv[0], mirror_id, fname);
11769 /* open output file - O_EXCL ensures output is not the same as input */
11771 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
11773 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
11774 progname, argv[0], outfile, strerror(errno));
11779 outfd = STDOUT_FILENO;
11782 /* allocate buffer */
11783 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
11785 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11786 progname, argv[0], rc);
11792 ssize_t bytes_read;
11793 ssize_t written = 0;
11795 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
11796 if (bytes_read < 0) {
11799 "%s %s: fail to read data from mirror %u: %s\n",
11800 progname, argv[0], mirror_id, strerror(-rc));
11805 if (bytes_read == 0)
11808 while (written < bytes_read) {
11811 written2 = write(outfd, buf + written,
11812 bytes_read - written);
11813 if (written2 < 0) {
11815 "%s %s: fail to write %s: %s\n",
11816 progname, argv[0], outfile ? : "STDOUT",
11821 written += written2;
11824 if (written != bytes_read) {
11826 "%s %s: written %ld bytes does not match with %ld read.\n",
11827 progname, argv[0], written, bytes_read);
11849 static inline int lfs_mirror_write(int argc, char **argv)
11852 __u16 mirror_id = 0;
11853 const char *inputfile = NULL;
11859 const size_t buflen = 4 << 20;
11861 size_t page_size = sysconf(_SC_PAGESIZE);
11862 struct ll_ioc_lease_id ioc;
11863 struct option long_opts[] = {
11864 { .val = 'h', .name = "help", .has_arg = no_argument },
11865 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11866 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11867 { .name = NULL } };
11869 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11874 unsigned long int id;
11877 id = strtoul(optarg, &end, 0);
11878 if (errno != 0 || *end != '\0' || id == 0 ||
11881 "%s %s: invalid mirror ID '%s'\n",
11882 progname, argv[0], optarg);
11886 mirror_id = (__u16)id;
11890 inputfile = optarg;
11893 fprintf(stderr, "%s: unrecognized option '%s'\n",
11894 progname, argv[optind - 1]);
11901 if (argc == optind) {
11902 fprintf(stderr, "%s %s: no mirrored file provided\n",
11903 progname, argv[0]);
11905 } else if (argc > optind + 1) {
11906 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11910 if (mirror_id == 0) {
11911 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11912 progname, argv[0]);
11916 /* open mirror file */
11917 fname = argv[optind];
11918 fd = open(fname, O_DIRECT | O_WRONLY);
11920 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11921 progname, argv[0], fname, strerror(errno));
11925 /* verify mirror id */
11926 rc = verify_mirror_id_by_fd(fd, mirror_id);
11929 "%s %s: cannot find mirror with ID %u in '%s'\n",
11930 progname, argv[0], mirror_id, fname);
11934 /* open input file */
11936 rc = check_same_file(fd, inputfile);
11939 "%s %s: input file cannot be the mirrored file '%s'\n",
11940 progname, argv[0], fname);
11946 inputfd = open(inputfile, O_RDONLY, 0644);
11948 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11949 progname, argv[0], inputfile, strerror(errno));
11954 inputfd = STDIN_FILENO;
11957 /* allocate buffer */
11958 rc = posix_memalign(&buf, page_size, buflen);
11960 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11961 progname, argv[0], rc);
11962 goto close_inputfd;
11965 /* prepare target mirror components instantiation */
11966 ioc.lil_mode = LL_LEASE_WRLCK;
11967 ioc.lil_flags = LL_LEASE_RESYNC;
11968 ioc.lil_mirror_id = mirror_id;
11969 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11972 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11973 progname, argv[0], fname, strerror(errno));
11979 ssize_t bytes_read;
11983 rc = llapi_lease_check(fd);
11984 if (rc != LL_LEASE_WRLCK) {
11985 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11986 progname, argv[0], fname);
11990 bytes_read = read(inputfd, buf, buflen);
11991 if (bytes_read < 0) {
11994 "%s %s: fail to read data from '%s': %s\n",
11995 progname, argv[0], inputfile ? : "STDIN",
12002 if (bytes_read == 0)
12005 /* round up to page align to make direct IO happy. */
12006 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
12008 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
12013 "%s %s: fail to write to mirror %u: %s\n",
12014 progname, argv[0], mirror_id,
12022 if (pos & (page_size - 1)) {
12023 rc = llapi_mirror_truncate(fd, mirror_id, pos);
12028 ioc.lil_mode = LL_LEASE_UNLCK;
12029 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
12031 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
12036 "%s %s: release lease lock of '%s' failed: %s\n",
12037 progname, argv[0], fname, strerror(-rc));
12054 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
12056 struct llapi_layout *layout;
12057 struct collect_ids_data cid = { .cid_ids = ids,
12059 .cid_exclude = exclude_id, };
12062 layout = llapi_layout_get_by_fd(fd, 0);
12064 fprintf(stderr, "could not get layout\n");
12068 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
12070 fprintf(stderr, "failed to iterate layout\n");
12071 llapi_layout_free(layout);
12075 llapi_layout_free(layout);
12077 return cid.cid_count;
12080 #ifndef MIRROR_ID_NEG
12081 #define MIRROR_ID_NEG 0x8000
12084 static inline int lfs_mirror_copy(int argc, char **argv)
12087 __u16 read_mirror_id = 0;
12088 __u16 ids[128] = { 0 };
12090 struct llapi_layout *layout = NULL;
12091 struct llapi_resync_comp comp_array[1024] = { { 0 } };
12098 struct ll_ioc_lease *ioc = NULL;
12099 struct ll_ioc_lease_id *resync_ioc;
12100 struct option long_opts[] = {
12101 { .val = 'h', .name = "help", .has_arg = no_argument },
12102 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
12103 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
12104 { .name = NULL } };
12105 char cmd[PATH_MAX];
12107 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12109 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
12114 unsigned long int id;
12117 id = strtoul(optarg, &end, 0);
12118 if (errno != 0 || *end != '\0' || id == 0 ||
12121 "%s: invalid read mirror ID '%s'\n",
12126 read_mirror_id = (__u16)id;
12130 if (!strcmp(optarg, "-1")) {
12131 /* specify all other mirrors */
12132 ids[0] = (__u16)-1;
12135 count = parse_mirror_ids((__u16 *)ids,
12143 fprintf(stderr, "%s: unrecognized option '%s'\n",
12144 progname, argv[optind - 1]);
12151 if (argc == optind) {
12152 fprintf(stderr, "%s %s: no mirrored file provided\n",
12153 progname, argv[0]);
12155 } else if (argc > optind + 1) {
12156 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
12160 if (read_mirror_id == 0) {
12162 "%s %s: no valid read mirror ID %d is provided\n",
12163 progname, argv[0], read_mirror_id);
12169 "%s %s: no write mirror ID is provided\n",
12170 progname, argv[0]);
12174 for (i = 0; i < count; i++) {
12175 if (read_mirror_id == ids[i]) {
12177 "%s %s: read and write mirror ID cannot be the same\n",
12178 progname, argv[0]);
12183 /* open mirror file */
12184 fname = argv[optind];
12186 fd = open(fname, O_DIRECT | O_RDWR);
12188 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
12189 progname, argv[0], fname, strerror(errno));
12193 /* write to all other mirrors */
12194 if (ids[0] == (__u16)-1) {
12195 count = get_other_mirror_ids(fd, ids, read_mirror_id);
12199 "%s %s: failed to get other mirror ids in '%s': %d\n",
12200 progname, argv[0], fname, rc);
12205 /* verify mirror id */
12206 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
12209 "%s %s: cannot find mirror with ID %u in '%s'\n",
12210 progname, argv[0], read_mirror_id, fname);
12214 for (i = 0; i < count; i++) {
12215 rc = verify_mirror_id_by_fd(fd, ids[i]);
12218 "%s %s: cannot find mirror with ID %u in '%s'\n",
12219 progname, argv[0], ids[i], fname);
12224 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
12227 "%s %s: cannot alloc comp id array for ioc: %s\n",
12228 progname, argv[0], strerror(errno));
12233 /* get stale component info */
12234 layout = llapi_layout_get_by_fd(fd, 0);
12236 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
12237 progname, argv[0], fname, strerror(errno));
12241 comp_size = llapi_mirror_find_stale(layout, comp_array,
12242 ARRAY_SIZE(comp_array),
12244 llapi_layout_free(layout);
12245 if (comp_size < 0) {
12250 /* prepare target mirror components instantiation */
12251 resync_ioc = (struct ll_ioc_lease_id *)ioc;
12252 resync_ioc->lil_mode = LL_LEASE_WRLCK;
12253 resync_ioc->lil_flags = LL_LEASE_RESYNC;
12255 resync_ioc->lil_mirror_id = ids[0];
12257 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
12258 rc = llapi_lease_set(fd, ioc);
12261 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
12262 progname, argv[0], fname, strerror(errno));
12266 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
12269 fprintf(stderr, "%s %s: copy error: %d\n",
12270 progname, argv[0], rc);
12274 fprintf(stdout, "mirror copied successfully: ");
12275 for (i = 0; i < copied; i++)
12276 fprintf(stdout, "%d ", ids[i]);
12277 fprintf(stdout, "\n");
12279 ioc->lil_mode = LL_LEASE_UNLCK;
12280 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
12281 ioc->lil_count = 0;
12282 for (i = 0; i < comp_size; i++) {
12285 for (j = 0; j < copied; j++) {
12286 if (comp_array[i].lrc_mirror_id != ids[j])
12289 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
12293 rc = llapi_lease_set(fd, ioc);
12298 "%s %s: release lease lock of '%s' failed: %s\n",
12299 progname, argv[0], fname, strerror(errno));
12314 * struct verify_chunk - Mirror chunk to be verified.
12315 * @chunk: [start, end) of the chunk.
12316 * @mirror_count: Number of mirror ids in @mirror_id array.
12317 * @mirror_id: Array of valid mirror ids that cover the chunk.
12319 struct verify_chunk {
12320 struct lu_extent chunk;
12321 unsigned int mirror_count;
12322 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
12326 * print_chunks() - Print chunk information.
12327 * @fname: Mirrored file name.
12328 * @chunks: Array of chunks.
12329 * @chunk_count: Number of chunks in @chunks array.
12331 * This function prints [start, end) of each chunk in @chunks
12332 * for mirrored file @fname, and also prints the valid mirror ids
12333 * that cover the chunk.
12338 void print_chunks(const char *fname, struct verify_chunk *chunks,
12344 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
12345 for (i = 0; i < chunk_count; i++) {
12346 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
12348 if (chunks[i].mirror_count == 0)
12349 fprintf(stdout, "\t[");
12351 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
12352 for (j = 1; j < chunks[i].mirror_count; j++)
12353 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
12355 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
12357 fprintf(stdout, "\n");
12361 * print_checksums() - Print CRC-32 checksum values.
12362 * @chunk: A chunk and its corresponding valid mirror ids.
12363 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
12365 * This function prints CRC-32 checksum values on @chunk for
12366 * each valid mirror that covers it.
12371 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
12372 unsigned long long pos, unsigned long long len)
12377 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
12378 for (i = 0; i < chunk->mirror_count; i++)
12379 fprintf(stdout, "Mirror %u:\t%#lx\n",
12380 chunk->mirror_id[i], crc[i]);
12381 fprintf(stdout, "\n");
12385 * filter_mirror_id() - Filter specified mirror ids.
12386 * @chunks: Array of chunks.
12387 * @chunk_count: Number of chunks in @chunks array.
12388 * @mirror_ids: Specified mirror ids to be verified.
12389 * @ids_nr: Number of specified mirror ids.
12391 * This function scans valid mirror ids that cover each chunk in @chunks
12392 * and filters specified mirror ids.
12397 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
12398 __u16 *mirror_ids, int ids_nr)
12403 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12404 unsigned int valid_count = 0;
12406 for (i = 0; i < chunk_count; i++) {
12407 if (chunks[i].mirror_count == 0)
12411 for (j = 0; j < ids_nr; j++) {
12412 for (k = 0; k < chunks[i].mirror_count; k++) {
12413 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
12414 valid_id[valid_count] = mirror_ids[j];
12421 memcpy(chunks[i].mirror_id, valid_id,
12422 sizeof(__u16) * valid_count);
12423 chunks[i].mirror_count = valid_count;
12428 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
12429 * @layout: Mirror component list.
12430 * @chunks: Array of chunks.
12431 * @chunks_size: Array size of @chunks.
12433 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
12434 * to find out chunk segments and store them in @chunks array.
12436 * The @mirror_id array in each element of @chunks will store the valid
12437 * mirror ids that cover the chunk. If a mirror component covering the
12438 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
12439 * will not be stored into the @mirror_id array, and the chunk for that
12440 * mirror will not be verified.
12442 * The @mirror_count in each element of @chunks will store the number of
12443 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
12444 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
12445 * indicates the chunk is valid in only one mirror. In both cases, the
12446 * chunk will not be verified.
12448 * Here is an example:
12450 * 0 1M 2M 3M 4M EOF
12451 * +------+-------------+--------------------+
12452 * | | | S | mirror1
12453 * +------+------+------+------+-------------+
12454 * | | S | S | | mirror2
12455 * +-------------+------+------+-------------+
12457 * prepared @chunks array will contain 5 elements:
12458 * (([0, 1M), [1, 2], 2),
12459 * ([1M, 2M), [1, 2], 2),
12460 * ([2M, 3M), [1], 1),
12461 * ([3M, 4M], [], 0),
12462 * ([4M, EOF), [2], 1))
12464 * Return: the actual array size of @chunks on success
12465 * or a negative error code on failure.
12468 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
12469 struct verify_chunk *chunks,
12470 size_t chunks_size)
12474 uint32_t mirror_id;
12480 memset(chunks, 0, sizeof(*chunks) * chunks_size);
12483 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
12486 "%s: move to the first layout component: %s.\n",
12487 progname, strerror(errno));
12493 chunks[idx].chunk.e_end = LUSTRE_EOF;
12495 rc = llapi_layout_comp_extent_get(layout, &start, &end);
12498 "%s: llapi_layout_comp_extent_get failed: %s.\n",
12499 progname, strerror(errno));
12503 if (start > chunks[idx].chunk.e_start ||
12504 end <= chunks[idx].chunk.e_start)
12507 if (end < chunks[idx].chunk.e_end)
12508 chunks[idx].chunk.e_end = end;
12510 rc = llapi_layout_comp_flags_get(layout, &flags);
12513 "%s: llapi_layout_comp_flags_get failed: %s.\n",
12514 progname, strerror(errno));
12518 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
12521 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
12524 "%s: llapi_layout_mirror_id_get failed: %s.\n",
12525 progname, strerror(errno));
12529 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
12531 "%s: mirror_id array is too small.\n",
12536 chunks[idx].mirror_id[i] = mirror_id;
12540 rc = llapi_layout_comp_use(layout,
12541 LLAPI_LAYOUT_COMP_USE_NEXT);
12544 "%s: move to the next layout component: %s.\n",
12545 progname, strerror(errno));
12548 } /* loop through all components */
12550 chunks[idx].mirror_count = i;
12552 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
12556 if (idx >= chunks_size) {
12557 fprintf(stderr, "%s: chunks array is too small.\n",
12563 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
12567 return rc < 0 ? rc : idx + 1;
12571 * lfs_mirror_verify_chunk() - Verify a chunk.
12572 * @fd: File descriptor of the mirrored file.
12573 * @file_size: Size of the mirrored file.
12574 * @chunk: A chunk and its corresponding valid mirror ids.
12575 * @verbose: Verbose mode.
12577 * This function verifies a @chunk contains exactly the same data
12578 * ammong the mirrors that cover it.
12580 * If @verbose is specified, then the function will print where the
12581 * differences are if the data do not match. Otherwise, it will
12582 * just return an error in that case.
12584 * Return: 0 on success or a negative error code on failure.
12587 int lfs_mirror_verify_chunk(int fd, size_t file_size,
12588 struct verify_chunk *chunk, int verbose)
12590 const size_t buflen = 4 * 1024 * 1024; /* 4M */
12592 size_t page_size = sysconf(_SC_PAGESIZE);
12593 ssize_t bytes_read;
12594 ssize_t bytes_done;
12598 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12602 if (file_size == 0)
12605 rc = posix_memalign(&buf, page_size, buflen);
12606 if (rc) /* error code is returned directly */
12610 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12611 PEXT(&chunk->chunk));
12612 for (i = 0; i < chunk->mirror_count; i++)
12613 fprintf(stdout, " %u", chunk->mirror_id[i]);
12614 fprintf(stdout, "\n");
12618 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
12619 pos = chunk->chunk.e_start;
12620 while (bytes_done < count) {
12621 /* compute initial CRC-32 checksum */
12622 crc = crc32(0L, Z_NULL, 0);
12623 memset(crc_array, 0, sizeof(crc_array));
12626 for (i = 0; i < chunk->mirror_count; i++) {
12627 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
12629 if (bytes_read < 0) {
12632 "%s: failed to read data from mirror %u: %s.\n",
12633 progname, chunk->mirror_id[i],
12638 /* compute new CRC-32 checksum */
12639 crc_array[i] = crc32(crc, buf, bytes_read);
12643 print_checksums(chunk, crc_array, pos, buflen);
12645 /* compare CRC-32 checksum values */
12646 for (i = 1; i < chunk->mirror_count; i++) {
12647 if (crc_array[i] != crc_array[0]) {
12651 "%s: chunk "DEXT" has different checksum value on mirror %u:%lx and mirror %u:%lx.\n",
12652 progname, PEXT(&chunk->chunk),
12653 chunk->mirror_id[0], crc_array[0],
12654 chunk->mirror_id[i], crc_array[i]);
12655 print_checksums(chunk, crc_array, pos, buflen);
12660 bytes_done += bytes_read;
12663 if (verbose > 1 && rc == 0) {
12664 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12665 PEXT(&chunk->chunk));
12666 for (i = 0; i < chunk->mirror_count; i++)
12667 fprintf(stdout, " %u", chunk->mirror_id[i]);
12668 fprintf(stdout, " PASS\n\n");
12677 * lfs_mirror_verify_file() - Verify a mirrored file.
12678 * @fname: Mirrored file name.
12679 * @mirror_ids: Specified mirror ids to be verified.
12680 * @ids_nr: Number of specified mirror ids.
12681 * @verbose: Verbose mode.
12683 * This function verifies that each SYNC mirror of a mirrored file
12684 * specified by @fname contains exactly the same data.
12686 * If @mirror_ids is specified, then the function will verify the
12687 * mirrors specified by @mirror_ids contain exactly the same data.
12689 * If @verbose is specified, then the function will print where the
12690 * differences are if the data do not match. Otherwise, it will
12691 * just return an error in that case.
12693 * Return: 0 on success or a negative error code on failure.
12696 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
12699 struct verify_chunk chunks_array[1024] = { };
12700 struct llapi_layout *layout = NULL;
12702 uint32_t flr_state;
12704 int chunk_count = 0;
12710 if (stat(fname, &stbuf) < 0) {
12711 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
12712 progname, fname, strerror(errno));
12717 if (!S_ISREG(stbuf.st_mode)) {
12718 fprintf(stderr, "%s: '%s' is not a regular file.\n",
12724 if (stbuf.st_size == 0) {
12726 fprintf(stdout, "%s: '%s' file size is 0.\n",
12732 /* Allow mirror verify even without the key on encrypted files */
12733 fd = open(fname, O_DIRECT | O_RDONLY | O_CIPHERTEXT);
12735 fprintf(stderr, "%s: cannot open '%s': %s.\n",
12736 progname, fname, strerror(errno));
12741 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
12743 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
12744 progname, fname, strerror(errno));
12748 layout = llapi_layout_get_by_fd(fd, 0);
12750 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
12751 progname, fname, strerror(errno));
12753 llapi_lease_release(fd);
12757 rc = llapi_layout_flags_get(layout, &flr_state);
12759 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
12760 progname, fname, strerror(errno));
12765 flr_state &= LCM_FL_FLR_MASK;
12766 switch (flr_state) {
12769 fprintf(stderr, "%s: '%s' file state error: %s.\n",
12770 progname, fname, llapi_layout_flags_string(flr_state));
12776 /* find out mirror chunks to be verified */
12777 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
12778 ARRAY_SIZE(chunks_array));
12779 if (chunk_count < 0) {
12785 /* filter specified mirror ids */
12786 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
12789 print_chunks(fname, chunks_array, chunk_count);
12791 for (idx = 0; idx < chunk_count; idx++) {
12792 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
12795 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
12797 PEXT(&chunks_array[idx].chunk),
12798 (unsigned long long)stbuf.st_size);
12802 if (chunks_array[idx].mirror_count == 0) {
12804 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12806 PEXT(&chunks_array[idx].chunk));
12808 fprintf(stderr, "skipped\n");
12812 fprintf(stderr, "failed\n");
12816 if (chunks_array[idx].mirror_count == 1) {
12819 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12821 PEXT(&chunks_array[idx].chunk),
12822 chunks_array[idx].mirror_id[0]);
12826 rc = llapi_lease_check(fd);
12827 if (rc != LL_LEASE_RDLCK) {
12828 fprintf(stderr, "%s: '%s' lost lease lock.\n",
12833 /* verify one chunk */
12834 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12835 &chunks_array[idx], verbose);
12849 llapi_layout_free(layout);
12850 llapi_lease_release(fd);
12858 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12859 * @argc: The count of lfs mirror verify command line arguments.
12860 * @argv: Array of strings for lfs mirror verify command line arguments.
12862 * This function parses lfs mirror verify command and verifies the
12863 * specified mirrored file(s).
12865 * Return: 0 on success or a negative error code on failure.
12867 static inline int lfs_mirror_verify(int argc, char **argv)
12869 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12875 char cmd[PATH_MAX];
12877 struct option long_opts[] = {
12878 { .val = 'h', .name = "help", .has_arg = no_argument },
12879 { .val = 'o', .name = "only", .has_arg = required_argument },
12880 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12881 { .name = NULL } };
12883 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12885 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12888 rc = parse_mirror_ids(mirror_ids,
12889 ARRAY_SIZE(mirror_ids),
12893 "%s: bad mirror ids '%s'.\n",
12900 "%s: at least 2 mirror ids needed with '--only' option.\n",
12910 fprintf(stderr, "%s: unrecognized option '%s'\n",
12911 progname, argv[optind - 1]);
12919 if (argc == optind) {
12920 fprintf(stderr, "%s: no file name given.\n", progname);
12925 if (ids_nr > 0 && argc > optind + 1) {
12927 "%s: '--only' cannot be used upon multiple files.\n",
12934 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12940 for (; optind < argc; optind++) {
12941 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12951 * lfs_mirror() - Parse and execute lfs mirror commands.
12952 * @argc: The count of lfs mirror command line arguments.
12953 * @argv: Array of strings for lfs mirror command line arguments.
12955 * This function parses lfs mirror commands and performs the
12956 * corresponding functions specified in mirror_cmdlist[].
12958 * Return: 0 on success or an error code on failure.
12960 static int lfs_mirror(int argc, char **argv)
12962 char cmd[PATH_MAX];
12965 setlinebuf(stdout);
12967 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12969 program_invocation_short_name = cmd;
12970 rc = cfs_parser(argc, argv, mirror_cmdlist);
12972 return rc < 0 ? -rc : rc;
12975 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12977 #if __BYTE_ORDER == __BIG_ENDIAN
12978 __swab16s(&attrs->lsa_valid);
12979 __swab64s(&attrs->lsa_size);
12980 __swab64s(&attrs->lsa_blocks);
12984 enum lfs_som_type {
12985 LFS_SOM_SIZE = 0x1,
12986 LFS_SOM_BLOCKS = 0x2,
12987 LFS_SOM_FLAGS = 0x4,
12988 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12992 static int lfs_getsom(int argc, char **argv)
12995 struct lustre_som_attrs *attrs;
12996 char buf[sizeof(*attrs) + 64];
12997 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
13000 while ((c = getopt(argc, argv, "bfhs")) != -1) {
13003 type = LFS_SOM_BLOCKS;
13006 type = LFS_SOM_FLAGS;
13009 type = LFS_SOM_SIZE;
13012 fprintf(stderr, "%s: unrecognized option '%s'\n",
13013 progname, argv[optind - 1]);
13024 fprintf(stderr, "%s: %s\n",
13025 progname, argc == 0 ? "miss file target" :
13026 "input more than 2 files");
13031 attrs = (void *)buf;
13032 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
13035 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
13036 argv[0], strerror(errno), errno);
13040 lustre_som_swab(attrs);
13043 case LFS_SOM_ATTR_ALL:
13044 printf("file: %s size: %llu blocks: %llu flags: %x\n",
13045 path, (unsigned long long)attrs->lsa_size,
13046 (unsigned long long)attrs->lsa_blocks,
13050 printf("%llu\n", (unsigned long long)attrs->lsa_size);
13052 case LFS_SOM_BLOCKS:
13053 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
13055 case LFS_SOM_FLAGS:
13056 printf("%x\n", attrs->lsa_valid);
13059 fprintf(stderr, "%s: unknown option\n", progname);
13066 static int lfs_pcc_attach(int argc, char **argv)
13068 struct option long_opts[] = {
13069 { .val = 'h', .name = "help", .has_arg = no_argument },
13070 { .val = 'i', .name = "id", .has_arg = required_argument },
13071 { .name = NULL } };
13074 __u32 archive_id = 0;
13077 char fullpath[PATH_MAX];
13078 enum lu_pcc_type type = LU_PCC_READWRITE;
13081 while ((c = getopt_long(argc, argv, "hi:",
13082 long_opts, NULL)) != -1) {
13086 archive_id = strtoul(optarg, &end, 0);
13087 if (errno != 0 || *end != '\0' ||
13088 archive_id == 0 || archive_id > UINT32_MAX) {
13090 "error: %s: bad archive ID '%s'\n",
13096 fprintf(stderr, "%s: unrecognized option '%s'\n",
13097 progname, argv[optind - 1]);
13104 if (archive_id == 0) {
13105 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
13109 if (argc <= optind) {
13110 fprintf(stderr, "%s: must specify one or more file names\n",
13115 while (optind < argc) {
13118 path = argv[optind++];
13119 if (!realpath(path, fullpath)) {
13120 fprintf(stderr, "%s: could not find path '%s': %s\n",
13121 argv[0], path, strerror(errno));
13127 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
13130 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
13131 argv[0], path, archive_id, strerror(-rc2));
13139 static int lfs_pcc_attach_fid(int argc, char **argv)
13141 struct option long_opts[] = {
13142 { .val = 'h', .name = "help", .has_arg = no_argument },
13143 { .val = 'i', .name = "id", .has_arg = required_argument },
13144 { .val = 'm', .name = "mnt", .has_arg = required_argument },
13145 { .name = NULL } };
13148 __u32 archive_id = 0;
13150 const char *mntpath = NULL;
13151 const char *fidstr;
13152 enum lu_pcc_type type = LU_PCC_READWRITE;
13155 while ((c = getopt_long(argc, argv, "hi:m:",
13156 long_opts, NULL)) != -1) {
13160 archive_id = strtoul(optarg, &end, 0);
13161 if (errno != 0 || *end != '\0' ||
13162 archive_id > UINT32_MAX) {
13164 "error: %s: bad archive ID '%s'\n",
13173 fprintf(stderr, "%s: unrecognized option '%s'\n",
13174 progname, argv[optind - 1]);
13181 if (archive_id == 0) {
13182 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
13187 fprintf(stderr, "%s: must specify Lustre mount point\n",
13192 if (argc <= optind) {
13193 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
13197 while (optind < argc) {
13200 fidstr = argv[optind++];
13202 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
13206 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
13207 argv[0], fidstr, mntpath, archive_id,
13210 if (rc == 0 && rc2 < 0)
13216 static int lfs_pcc_detach(int argc, char **argv)
13218 struct option long_opts[] = {
13219 { .val = 'h', .name = "help", .has_arg = no_argument },
13220 { .val = 'k', .name = "keep", .has_arg = no_argument },
13221 { .name = NULL } };
13225 char fullpath[PATH_MAX];
13226 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13229 while ((c = getopt_long(argc, argv, "hk",
13230 long_opts, NULL)) != -1) {
13233 detach_opt = PCC_DETACH_OPT_NONE;
13236 fprintf(stderr, "%s: unrecognized option '%s'\n",
13237 progname, argv[optind - 1]);
13244 while (optind < argc) {
13247 path = argv[optind++];
13248 if (!realpath(path, fullpath)) {
13249 fprintf(stderr, "%s: could not find path '%s': %s\n",
13250 argv[0], path, strerror(errno));
13256 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
13260 "%s: cannot detach '%s' from PCC: %s\n",
13261 argv[0], path, strerror(errno));
13269 static int lfs_pcc_detach_fid(int argc, char **argv)
13271 struct option long_opts[] = {
13272 { .val = 'h', .name = "help", .has_arg = no_argument },
13273 { .val = 'k', .name = "keep", .has_arg = no_argument },
13274 { .name = NULL } };
13278 const char *mntpath;
13279 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13282 while ((c = getopt_long(argc, argv, "hk",
13283 long_opts, NULL)) != -1) {
13286 detach_opt = PCC_DETACH_OPT_NONE;
13289 fprintf(stderr, "%s: unrecognized option '%s'\n",
13290 progname, argv[optind - 1]);
13297 mntpath = argv[optind++];
13299 while (optind < argc) {
13302 fid = argv[optind++];
13304 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
13307 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
13308 argv[0], fid, mntpath, strerror(-rc2));
13316 static int lfs_pcc_state(int argc, char **argv)
13320 char fullpath[PATH_MAX];
13321 struct lu_pcc_state state;
13326 fprintf(stderr, "%s: must specify one or more file names\n",
13331 while (optind < argc) {
13334 path = argv[optind++];
13335 if (!realpath(path, fullpath)) {
13336 fprintf(stderr, "%s: could not find path '%s': %s\n",
13337 argv[0], path, strerror(errno));
13343 rc2 = llapi_pcc_state_get(fullpath, &state);
13348 "%s: cannot get PCC state of '%s': %s\n",
13349 argv[0], path, strerror(-rc2));
13353 printf("file: %s", path);
13354 printf(", type: %s", pcc_type2string(state.pccs_type));
13355 if (state.pccs_type == LU_PCC_NONE &&
13356 state.pccs_open_count == 0) {
13361 printf(", PCC file: %s", state.pccs_path);
13362 printf(", user number: %u", state.pccs_open_count);
13363 printf(", flags: %x", state.pccs_flags);
13370 * lfs_pcc() - Parse and execute lfs pcc commands.
13371 * @argc: The count of lfs pcc command line arguments.
13372 * @argv: Array of strings for lfs pcc command line arguments.
13374 * This function parses lfs pcc commands and performs the
13375 * corresponding functions specified in pcc_cmdlist[].
13377 * Return: 0 on success or an error code on failure.
13379 static int lfs_pcc(int argc, char **argv)
13381 char cmd[PATH_MAX];
13384 setlinebuf(stdout);
13386 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
13388 program_invocation_short_name = cmd;
13389 rc = cfs_parser(argc, argv, pcc_cmdlist);
13391 return rc < 0 ? -rc : rc;
13394 int main(int argc, char **argv)
13398 /* Ensure that liblustreapi constructor has run */
13399 if (!llapi_liblustreapi_initialized())
13400 fprintf(stderr, "liblustreapi was not properly initialized\n");
13402 setlinebuf(stdout);
13405 progname = program_invocation_short_name; /* Used in error messages */
13406 llapi_set_command_name(argv[1]);
13407 rc = cfs_parser(argc, argv, cmdlist);
13408 llapi_clear_command_name();
13410 return rc < 0 ? -rc : rc;