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 /* XXX: A temporary solution for transition to help text update */
213 #define SSM_CMD_COMMON_1(cmd) \
214 "usage: "cmd" [--component-add|--component-del|--delete|-d]\n" \
215 " [--comp-set --comp-id|-I COMP_ID|--comp-flags=COMP_FLAGS]\n" \
216 " [--component-end|-E END_OFFSET]\n" \
217 " [--copy=SOURCE_LAYOUT_FILE]|--yaml|-y YAML_TEMPLATE_FILE]\n" \
218 " [--extension-size|--ext-size|-z EXT_SIZE]\n" \
220 " [--foreign=FOREIGN_TYPE --xattr|-x LAYOUT]\n" \
221 " [--layout|-L PATTERN] [--mode FILE_MODE]\n" \
222 " [--mirror-count|-N[MIRROR_COUNT]]\n" \
223 " [--ost|-o OST_INDEX[,OST_INDEX,...]]\n" \
224 " [--overstripe-count|-C STRIPE_COUNT]\n" \
225 " [--pool|-p POOL_NAME]\n" \
226 " [--stripe-count|-c STRIPE_COUNT]\n" \
227 " [--stripe-index|-i START_OST_IDX]\n" \
228 " [--stripe-size|-S STRIPE_SIZE]\n" \
229 " FILENAME|DIRECTORY\n"
231 #define MIRROR_EXTEND_USAGE \
232 " {--mirror-count|-N[MIRROR_COUNT]}\n" \
233 " [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n" \
236 #define MIGRATE_USAGE \
237 SSM_CMD_COMMON("migrate ") \
238 " [--block|-b] [--non-block|-n]\n" \
239 " [--non-direct|-D] [--verbose|-v]\n" \
242 #define SETDIRSTRIPE_USAGE \
243 " [--mdt-count|-c stripe_count>\n" \
244 " [--help|-h] [--mdt-hash|-H mdt_hash]\n" \
245 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
246 " [--default|-D] [--mode|-o mode]\n" \
247 " [--max-inherit|-X max_inherit]\n" \
248 " [--max-inherit-rr max_inherit_rr] <dir>\n" \
249 "To create dir with a foreign (free format) layout :\n" \
250 "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
251 " [--mode|-o MODE] [--flags HEX] DIRECTORY\n"
254 * command_t mirror_cmdlist - lfs mirror commands.
256 command_t mirror_cmdlist[] = {
257 { .pc_name = "create", .pc_func = lfs_mirror_create,
258 .pc_help = "Create a mirrored file.\n"
259 "usage: lfs mirror create --mirror-count|-N[MIRROR_COUNT]\n"
260 " [SETSTRIPE_OPTIONS] ... FILENAME|DIRECTORY ...\n" },
261 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
262 .pc_help = "Delete a mirror from a file.\n"
263 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
264 "\t --component-id|--comp-id|-I COMP_ID |\n"
265 "\t -p <pool>} MIRRORED_FILE ...\n"
267 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
268 .pc_help = "Extend a mirrored file.\n"
269 "usage: lfs mirror extend "
270 "{--mirror-count|-N[MIRROR_COUNT]} [--no-verify]|\n"
271 "\t\t--stats|--stats-interval=<sec>|\n"
272 "\t\t--W <bandwidth>|--bandwidth-limit=<bandwidth>\n"
273 "\t\t[SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... FILENAME ...\n" },
274 { .pc_name = "split", .pc_func = lfs_mirror_split,
275 .pc_help = "Split a mirrored file.\n"
276 "usage: lfs mirror split {--mirror-id MIRROR_ID |\n"
277 "\t --component-id|-I COMP_ID|-p POOL} [--destroy|-d]\n"
278 "\t [-f NEW_FILE] MIRRORED_FILE ...\n" },
279 { .pc_name = "read", .pc_func = lfs_mirror_read,
280 .pc_help = "Read the content of a specified mirror of a file.\n"
281 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
282 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
283 { .pc_name = "write", .pc_func = lfs_mirror_write,
284 .pc_help = "Write to a specified mirror of a file.\n"
285 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
286 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
287 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
288 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
289 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
290 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
291 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
292 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
293 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]|\n"
294 "\t\t--stats|--stats-interval=<sec>|\n"
295 "\t\t--W <bandwidth>|--bandwidth-limit=<bandwidth>\n"
296 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
297 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
298 .pc_help = "Verify mirrored file(s).\n"
299 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
300 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
305 * command_t pcc_cmdlist - lfs pcc commands.
307 command_t pcc_cmdlist[] = {
308 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
309 .pc_help = "Attach given files to the Persistent Client Cache.\n"
310 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
311 "\t-i: archive id for RW-PCC\n" },
312 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
313 .pc_help = "Attach given files into PCC by FID(s).\n"
314 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
315 "\t-i: archive id for RW-PCC\n"
316 "\t-m: Lustre mount point\n" },
317 { .pc_name = "state", .pc_func = lfs_pcc_state,
318 .pc_help = "Display the PCC state for given files.\n"
319 "usage: lfs pcc state <file> ...\n" },
320 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
321 .pc_help = "Detach given files from the Persistent Client Cache.\n"
322 "usage: lfs pcc detach <file> ...\n" },
323 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
324 .pc_help = "Detach given files from PCC by FID(s).\n"
325 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
329 /* all available commands */
330 command_t cmdlist[] = {
331 {"setstripe", lfs_setstripe, 0,
332 "Create a file with specified striping/composite layout, or\n"
333 "set the default layout on an existing directory.\n"
334 SSM_CMD_COMMON_1("setstripe")},
335 {"getstripe", lfs_getstripe, 0,
336 "List the layout pattern for a given file or files in a\n"
337 "directory or recursively for all files in a directory tree.\n"
338 "Usage: getstripe [--ost|-O OST_NAME] [--quiet|-q] [--verbose|-v]\n"
339 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
340 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
341 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
342 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
343 " [--help|-h] [--hex-idx]\n"
344 " [--component-id|-I[=COMP_ID]]\n"
345 " [--component-flags[=COMP_FLAGS]]\n"
346 " [--component-count]\n"
347 " [--extension-size|--ext-size|-z]\n"
348 " [--component-start[=[+-]START_OFFSET]]\n"
349 " [--component-end|-E[[+-]END_OFFSET]]\n"
350 " [[!] --mirror-index=[+-]MIRROR_INDEX |\n"
351 " [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
353 " FILENAME|DIRECTORY"},
354 {"setdirstripe", lfs_setdirstripe, 0,
355 "Create striped directory on specified MDT, same as mkdir.\n"
356 "May be restricted to root or group users, depending on settings.\n"
357 "usage: setdirstripe [OPTION] <directory>\n"
359 {"getdirstripe", lfs_getdirstripe, 0,
360 "To list the layout pattern info for a given directory\n"
361 "or recursively for all directories in a directory tree.\n"
362 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
363 " [--help|-h] [--hex-idx] [--mdt-hash|-H]\n"
364 " [--obd|-O UUID] [--recursive|-r] [--raw|-R]\n"
365 " [--yaml|-y] [--verbose|-v] [--default|-D]\n"
366 " [--max-inherit|-X]\n"
367 " [--max-inherit-rr] <dir> ..."},
368 {"mkdir", lfs_setdirstripe, 0,
369 "Create striped directory on specified MDT, same as setdirstripe.\n"
370 "usage: mkdir [OPTION] <directory>\n"
372 {"rm_entry", lfs_rmentry, 0,
373 "To remove the name entry of the remote directory. Note: This\n"
374 "command will only delete the name entry, i.e. the remote directory\n"
375 "will become inaccessable after this command. This can only be done\n"
376 "by the administrator\n"
377 "usage: rm_entry <dir>\n"},
378 {"rmentry", lfs_rmentry, 0, "remove a dir entry, same as 'rm_entry'\n"},
379 {"unlink_foreign", lfs_unlink_foreign, 0,
380 "To remove the foreign file/dir.\n"
381 "Note: This is for files/dirs prevented to be removed using\n"
382 "unlink/rmdir, but works also for regular ones\n"
383 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
384 {"pool_list", lfs_poollist, 0,
385 "List pools or pool OSTs\n"
386 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
387 {"find", lfs_find, 0,
388 "find files matching given attributes recursively in directory tree.\n"
389 "usage: find <directory|filename> ...\n"
390 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --btime|-B [+-]N[smhdwy]]\n"
391 " [[!] --ctime|-C [+-]N[smhdwy]] [[!] --mtime|-M [+-]N[smhdwy]]\n"
392 " [[!] --attrs=[^]ATTR[,...]]\n"
393 " [[!] --blocks|-b N] [[!] --component-count [+-]<comp_cnt>]\n"
394 " [[!] --component-start [+-]N[kMGTPE]]\n"
395 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
396 " [[!] --component-flags {init,stale,prefer,prefrd,prefwr,offline,nosync,extension}]\n"
397 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
398 " [[!] --foreign[=<foreign_type>]]\n"
399 " [[!] --gid|-g|--group|-G <gid>|<gname>] [--help|-h]\n"
400 " [[!] --layout|-L released,raid0,mdt] [--lazy|-l] [[!] --links [+-]n]\n"
401 " [--maxdepth|-D N] [[!] --mdt-count|-T [+-]<stripes>]\n"
402 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
403 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
404 " [[!] --mirror-count|-N [+-]<n>]\n"
405 " [[!] --mirror-state <[^]state>]\n"
406 " [[!] --name|-n <pattern>] [[!] --newer[XY] <reference>]\n"
407 " [[!] --ost|-O <uuid|index,...>] [[!] --perm [/-]mode]\n"
408 " [[!] --pool <pool>] [--print|-P] [--print0|-0] [--printf <format>]\n"
409 " [[!] --projid <projid>] [[!] --size|-s [+-]N[bkMGTPE]]\n"
410 " [[!] --stripe-count|-c [+-]<stripes>]\n"
411 " [[!] --stripe-index|-i <index,...>]\n"
412 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
413 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
414 "\t !: used before an option indicates 'NOT' requested attribute\n"
415 "\t -: used before a value indicates less than requested value\n"
416 "\t +: used before a value indicates more than requested value\n"
417 "\t ^: used before a flag indicates to exclude it\n"},
418 {"check", lfs_check, 0,
419 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
420 "or all the servers (MGTs, MDTs and OSTs) [for specified path only].\n"
421 "usage: check {mgts|osts|mdts|all} [path]"},
422 {"osts", lfs_osts, 0, "list OSTs connected to client "
423 "[for specified path only]\n" "usage: osts [path]"},
424 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
425 "[for specified path only]\n" "usage: mdts [path]"},
427 "report filesystem disk space usage or inodes usage "
428 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
429 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
430 " [--pool|-p <fsname>[.<pool>]] [path]"},
431 {"getname", lfs_getname, 0,
432 "list instances and specified mount points [for specified path only]\n"
433 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
434 #ifdef HAVE_SYS_QUOTA_H
435 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
436 "usage: setquota [-t][-D] {-u|-U|-g|-G|-p|-P} {-b|-B|-i|-I LIMIT} [--pool POOL] FILESYSTEM\n"
437 " setquota {-u|-g|-p} --delete FILESYSTEM\n"},
438 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
439 "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
440 " [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
441 " [--pool <OST pool name>] <filesystem>\n"
442 " quota -t <-u|-g|-p> [--pool <OST pool name>] <filesystem>\n"
443 " quota [-q] [-v] [h] {-U|-G|-P} [--pool <OST pool name>] <filesystem>"},
444 {"project", lfs_project, 0,
445 "Change or list project attribute for specified file or directory.\n"
446 "usage: project [-d|-r] <file|directory...>\n"
447 " list project ID and flags on file(s) or directories\n"
448 " project [-p id] [-s] [-r] <file|directory...>\n"
449 " set project ID and/or inherit flag for specified file(s) or directories\n"
450 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
451 " check project ID and flags on file(s) or directories, print outliers\n"
452 " project -C [-d|-r] [-k] <file|directory...>\n"
453 " clear the project inherit flag and ID on the file or directory\n"
456 {"flushctx", lfs_flushctx, 0,
457 "Flush security context for current user.\n"
458 "usage: flushctx [-k] [-r] [mountpoint...]"},
459 {"changelog", lfs_changelog, 0,
460 "Show the metadata changes on an MDT."
461 "\nusage: changelog <mdtname> [startrec [endrec]]"},
462 {"changelog_clear", lfs_changelog_clear, 0,
463 "Indicate that old changelog records up to <endrec> are no longer of "
464 "interest to consumer <id>, allowing the system to free up space.\n"
465 "An <endrec> of 0 means all records.\n"
466 "usage: changelog_clear <mdtname> <id> <endrec>"},
467 {"fid2path", lfs_fid2path, 0,
468 "Resolve the full path(s) for given FID(s). For a specific hardlink "
469 "specify link number <linkno>.\n"
470 "usage: fid2path [--print0|-0] [--print-fid|-f] [--print-link|-c] "
471 "[--link|-l <linkno>] [--name|-n] <fsname|root> <fid>..."},
472 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
473 "usage: path2fid [--parents] <path> ..."},
474 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
475 "usage: rmfid <fsname|rootpath> <fid> ..."},
476 {"data_version", lfs_data_version, 0, "Display file data version for "
477 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
478 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
479 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
480 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
481 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
482 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
483 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
485 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
486 "[--archived] [--lost] <file> ..."},
487 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
488 "given files.\n" "usage: hsm_action <file> ..."},
489 {"hsm_archive", lfs_hsm_archive, 0,
490 "Archive file to external storage.\n"
491 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
493 {"hsm_restore", lfs_hsm_restore, 0,
494 "Restore file from external storage.\n"
495 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
496 {"hsm_release", lfs_hsm_release, 0,
497 "Release files from Lustre.\n"
498 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
499 {"hsm_remove", lfs_hsm_remove, 0,
500 "Remove file copy from external storage.\n"
501 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
503 " (FILE [FILE ...] | "
504 "--mntpath MOUNTPATH FID [FID ...])\n"
506 "Note: To remove an archived copy of a file already deleted from a "
508 "--mntpath option and a list of FIDs must be specified"
510 {"hsm_cancel", lfs_hsm_cancel, 0,
511 "Cancel requests related to specified files.\n"
512 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
513 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
514 "usage: swap_layouts <path1> <path2>"},
515 {"migrate", lfs_setstripe_migrate, 0,
516 "migrate directories and their inodes between MDTs.\n"
517 "usage: migrate [--mdt-count|-c STRIPE_COUNT] [--directory|-d]\n"
518 " [--mdt-hash|-H HASH_TYPE]\n"
519 " [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
522 "migrate file objects from one OST layout to another\n"
523 "(may be not safe with concurent writes).\n"
526 "To move directories between MDTs. This command is deprecated, "
527 "use \"migrate\" instead.\n"
528 "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
530 {"ladvise", lfs_ladvise, 0,
531 "Provide servers with advice about access patterns for a file.\n"
532 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
533 " [--background|-b] [--unset|-u]\n\n"
534 " {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
535 " {[--mode|-m [READ,WRITE]}\n"
537 {"mirror", lfs_mirror, mirror_cmdlist,
538 "lfs commands used to manage files with mirrored components:\n"
539 "lfs mirror create - create a mirrored file or directory\n"
540 "lfs mirror extend - add mirror(s) to an existing file\n"
541 "lfs mirror split - split a mirror from an existing mirrored file\n"
542 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
543 "lfs mirror read - read a mirror content of a mirrored file\n"
544 "lfs mirror write - write to a mirror of a mirrored file\n"
545 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
546 "lfs mirror verify - verify mirrored file(s)\n"},
547 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
548 "usage: getsom [-s] [-b] [-f] <path>\n"
549 "\t-s: Only show the size value of the SOM data for a given file\n"
550 "\t-b: Only show the blocks value of the SOM data for a given file\n"
551 "\t-f: Only show the flags value of the SOM data for a given file\n"},
552 {"heat_get", lfs_heat_get, 0,
553 "To get heat of files.\n"
554 "usage: heat_get <file> ...\n"},
555 {"heat_set", lfs_heat_set, 0,
556 "To set heat flags of files.\n"
557 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
558 "\t--clear|-c: Clear file heat for given files\n"
559 "\t--off|-o: Turn off file heat for given files\n"
560 "\t--on|-O: Turn on file heat for given files\n"},
561 {"pcc", lfs_pcc, pcc_cmdlist,
562 "lfs commands used to interact with PCC features:\n"
563 "lfs pcc attach - attach given files to Persistent Client Cache\n"
564 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
565 "lfs pcc state - display the PCC state for given files\n"
566 "lfs pcc detach - detach given files from Persistent Client Cache\n"
567 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
571 static int check_hashtype(const char *hashtype)
573 int type_num = atoi(hashtype);
576 /* numeric hash type */
577 if (hashtype && lmv_is_known_hash_type(type_num))
579 /* string hash type */
580 for (i = LMV_HASH_TYPE_ALL_CHARS; i < ARRAY_SIZE(mdt_hash_name); i++)
581 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
587 static uint32_t check_foreign_type_name(const char *foreign_type_name)
591 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
592 if (!lu_foreign_types[i].lft_name)
594 if (strcmp(foreign_type_name,
595 lu_foreign_types[i].lft_name) == 0)
596 return lu_foreign_types[i].lft_type;
599 return LU_FOREIGN_TYPE_UNKNOWN;
602 static const char *error_loc = "syserror";
605 migrate_open_files(const char *name, __u64 migration_flags,
606 const struct llapi_stripe_param *param,
607 struct llapi_layout *layout, int *fd_src_ptr,
615 char parent[PATH_MAX];
616 char volatile_file[PATH_MAX];
622 if (!param && !layout) {
623 error_loc = "layout information";
627 /* search for file directory pathname */
628 if (strlen(name) > sizeof(parent) - 1) {
629 error_loc = "source file name";
633 strncpy(parent, name, sizeof(parent));
634 ptr = strrchr(parent, '/');
636 if (!getcwd(parent, sizeof(parent))) {
637 error_loc = "getcwd";
641 if (ptr == parent) /* leading '/' */
646 /* even if the file is only read, WR mode is nedeed to allow
649 /* Allow migrating even without the key on encrypted files */
650 rflags = O_RDWR | O_NOATIME | O_CIPHERTEXT;
651 if (!(migration_flags & LLAPI_MIGRATION_NONDIRECT))
654 fd_src = open(name, rflags);
656 /* If encrypted file without the key,
657 * retry mirror extend in O_DIRECT.
659 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
660 migration_flags & LLAPI_MIGRATION_MIRROR) {
665 error_loc = "cannot open source file";
669 rc = llapi_file_fget_mdtidx(fd_src, &mdt_index);
671 error_loc = "cannot get MDT index";
676 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
677 /* Allow migrating without the key on encrypted files */
679 mode_t open_mode = S_IRUSR | S_IWUSR;
681 if (rflags & O_DIRECT)
682 open_flags |= O_DIRECT;
683 random_value = random();
684 rc = snprintf(volatile_file, sizeof(volatile_file),
685 "%s/%s:%.4X:%.4X:fd=%.2d", parent,
686 LUSTRE_VOLATILE_HDR, mdt_index,
687 random_value, fd_src);
688 if (rc >= sizeof(volatile_file)) {
693 /* create, open a volatile file, use caching (ie no directio) */
695 /* Returns -1 and sets errno on error: */
696 fd_dst = llapi_layout_file_open(volatile_file,
697 open_flags, open_mode,
702 /* Does the right thing on error: */
703 fd_dst = llapi_file_open_param(volatile_file,
707 } while (fd_dst < 0 && (rc = fd_dst) == -EEXIST);
710 error_loc = "cannot create volatile file";
715 * In case the MDT does not support creation of volatile files
716 * we should try to unlink it.
718 (void)unlink(volatile_file);
721 * Not-owner (root?) special case.
722 * Need to set owner/group of volatile file like original.
723 * This will allow to pass related check during layout_swap.
725 rc = fstat(fd_src, &st);
728 error_loc = "cannot stat source file";
732 rc = fstat(fd_dst, &stv);
735 error_loc = "cannot stat volatile";
739 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
740 rc = fchown(fd_dst, st.st_uid, st.st_gid);
743 error_loc = "cannot change ownwership of volatile";
755 *fd_src_ptr = fd_src;
756 *fd_dst_ptr = fd_dst;
762 struct timespec timespec_sub(struct timespec *before, struct timespec *after)
766 ret.tv_sec = after->tv_sec - before->tv_sec;
767 if (after->tv_nsec < before->tv_nsec) {
769 ret.tv_nsec = NSEC_PER_SEC + after->tv_nsec - before->tv_nsec;
771 ret.tv_nsec = after->tv_nsec - before->tv_nsec;
777 static void stats_log(struct timespec *now, struct timespec *start_time,
778 ssize_t read_bytes, size_t write_bytes,
779 off_t file_size_bytes)
781 struct timespec diff = timespec_sub(start_time, now);
783 if (file_size_bytes == 0)
786 if (diff.tv_sec == 0 && diff.tv_nsec == 0)
789 printf("- { seconds: %li, rmbps: %5.2g, wmbps: %5.2g, copied: %lu, size: %lu, pct: %lu%% }\n",
791 (double) read_bytes/((ONE_MB * diff.tv_sec) +
792 ((ONE_MB * diff.tv_nsec)/NSEC_PER_SEC)),
793 (double) write_bytes/((ONE_MB * diff.tv_sec) +
794 ((ONE_MB * diff.tv_nsec)/NSEC_PER_SEC)),
795 write_bytes/ONE_MB, file_size_bytes/ONE_MB,
796 ((write_bytes*100)/file_size_bytes));
799 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int),
800 unsigned long long bandwidth_bytes_sec,
801 long stats_interval_sec, off_t file_size_bytes)
803 struct llapi_layout *layout;
804 size_t buf_size = 64 * ONE_MB;
805 uint64_t stripe_size = ONE_MB;
809 size_t page_size = sysconf(_SC_PAGESIZE);
812 size_t write_bytes = 0;
813 ssize_t read_bytes = 0;
814 struct timespec start_time;
816 struct timespec last_bw_print;
818 layout = llapi_layout_get_by_fd(fd_src, 0);
820 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
822 /* We like big bufs */
823 if (stripe_size > buf_size)
824 buf_size = stripe_size;
826 /* Trim to stripe_size multiple */
827 buf_size -= buf_size % stripe_size;
830 llapi_layout_free(layout);
833 /* limit transfer size to what can be sent in one second */
834 if (bandwidth_bytes_sec && bandwidth_bytes_sec < buf_size)
835 buf_size = (bandwidth_bytes_sec + stripe_size - 1) &
838 /* Use a page-aligned buffer for direct I/O */
839 rc = posix_memalign(&buf, page_size, buf_size);
843 sparse = llapi_file_is_sparse(fd_src);
845 rc = ftruncate(fd_dst, pos);
853 clock_gettime(CLOCK_MONOTONIC, &start_time);
854 now = last_bw_print = start_time;
858 size_t to_read, to_write;
861 if (sparse && pos >= data_end) {
864 data_off = llapi_data_seek(fd_src, pos, &data_size);
866 /* Non-fatal, switch to full copy */
870 /* hole at the end of file, truncate up to it */
872 rc = ftruncate(fd_dst, data_off);
876 pos = data_off & ~(page_size - 1);
877 data_end = data_off + data_size;
878 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
879 to_read = MIN(to_read, buf_size);
885 rc = check_file(fd_src);
890 rsize = pread(fd_src, buf, to_read, pos);
901 while (to_write > 0) {
902 unsigned long long write_target;
904 struct timespec diff;
906 written = pwrite(fd_dst, buf, to_write, pos);
913 write_bytes += written;
915 if (bandwidth_bytes_sec == 0)
918 clock_gettime(CLOCK_MONOTONIC, &now);
919 diff = timespec_sub(&start_time, &now);
920 write_target = ((bandwidth_bytes_sec * diff.tv_sec) +
921 ((bandwidth_bytes_sec *
922 diff.tv_nsec)/NSEC_PER_SEC));
924 if (write_target < write_bytes) {
925 unsigned long long excess;
926 struct timespec delay = { 0, 0 };
928 excess = write_bytes - write_target;
933 delay.tv_sec = excess / bandwidth_bytes_sec;
934 delay.tv_nsec = (excess % bandwidth_bytes_sec) *
935 NSEC_PER_SEC / bandwidth_bytes_sec;
938 rc = clock_nanosleep(CLOCK_MONOTONIC, 0,
940 } while (rc < 0 && errno == EINTR);
943 if (stats_interval_sec)
945 "error %s: delay for bandwidth control failed: %s\n",
953 clock_gettime(CLOCK_MONOTONIC, &now);
954 if (stats_interval_sec && (write_bytes != file_size_bytes) &&
955 (now.tv_sec >= last_bw_print.tv_sec +
956 stats_interval_sec)) {
957 stats_log(&now, &start_time,
958 read_bytes, write_bytes,
963 if (rc || rsize < to_read)
967 /* Output at least one log, regardless of stats_interval */
968 if (stats_interval_sec) {
969 clock_gettime(CLOCK_MONOTONIC, &now);
970 stats_log(&now, &start_time, read_bytes, write_bytes,
978 /* Try to avoid page cache pollution after migration. */
979 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
980 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
986 static int migrate_set_timestamps(int fd, const struct stat *st)
988 struct timeval tv[2] = {
989 {.tv_sec = st->st_atime},
990 {.tv_sec = st->st_mtime}
993 return futimes(fd, tv);
996 static int migrate_block(int fd_src, int fd_dst,
997 unsigned long long bandwidth_bytes_sec,
998 long stats_interval_sec)
1011 /* The grouplock blocks all concurrent accesses to the file. */
1012 rc = llapi_group_lock(fd_src, gid);
1014 error_loc = "cannot get group lock";
1018 rc = fstat(fd_src, &st);
1020 error_loc = "cannot stat source file";
1026 * LL_DV_RD_FLUSH should not be set, otherwise the servers will try to
1027 * get extent locks on the OST objects. This will conflict with our
1028 * extent group locks.
1030 rc = llapi_get_data_version(fd_src, &dv1, 0);
1032 error_loc = "cannot get dataversion";
1036 rc = migrate_copy_data(fd_src, fd_dst, NULL, bandwidth_bytes_sec,
1037 stats_interval_sec, st.st_size);
1039 error_loc = "data copy failed";
1043 /* Make sure we keep original atime/mtime values */
1044 rc = migrate_set_timestamps(fd_dst, &st);
1046 error_loc = "set target file timestamp failed";
1052 * for a migration we need to check data version on file did
1055 * Pass in gid=0 since we already own grouplock.
1057 rc = llapi_fswap_layouts_grouplock(fd_src, fd_dst, dv1, 0, 0,
1058 SWAP_LAYOUTS_CHECK_DV1);
1059 if (rc == -EAGAIN) {
1060 error_loc = "file changed";
1062 } else if (rc < 0) {
1063 error_loc = "cannot swap layout";
1068 rc2 = llapi_group_unlock(fd_src, gid);
1069 if (rc2 < 0 && rc == 0) {
1070 error_loc = "unlock group lock";
1078 * Internal helper for migrate_copy_data(). Check lease and report error if
1081 * \param[in] fd File descriptor on which to check the lease.
1083 * \retval 0 Migration can keep on going.
1084 * \retval -errno Error occurred, abort migration.
1086 static int check_lease(int fd)
1090 rc = llapi_lease_check(fd);
1092 return 0; /* llapi_check_lease returns > 0 on success. */
1097 static int migrate_nonblock(int fd_src, int fd_dst,
1098 unsigned long long bandwidth_bytes_sec,
1099 long stats_interval_sec)
1106 rc = fstat(fd_src, &st);
1108 error_loc = "cannot stat source file";
1112 rc = llapi_get_data_version(fd_src, &dv1, LL_DV_RD_FLUSH);
1114 error_loc = "cannot get data version";
1118 rc = migrate_copy_data(fd_src, fd_dst, check_lease,
1119 bandwidth_bytes_sec,
1120 stats_interval_sec, st.st_size);
1122 error_loc = "data copy failed";
1126 rc = llapi_get_data_version(fd_src, &dv2, LL_DV_RD_FLUSH);
1128 error_loc = "cannot get data version";
1134 error_loc = "source file changed";
1138 /* Make sure we keep original atime/mtime values */
1139 rc = migrate_set_timestamps(fd_dst, &st);
1141 error_loc = "set target file timestamp failed";
1148 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1150 struct pool_to_id_cbdata data = { .pool = pool };
1151 struct llapi_layout *layout = NULL;
1154 layout = llapi_layout_get_by_path(fname, 0);
1157 "error %s: file '%s' couldn't get layout: rc=%d\n",
1158 progname, fname, errno);
1162 rc = llapi_layout_sanity(layout, false, true);
1164 llapi_layout_sanity_perror(errno);
1167 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1176 llapi_layout_free(layout);
1180 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1181 __u32 flags, __u32 neg_flags)
1184 __u32 flags_array[2];
1191 "error %s: neither component id nor pool is specified\n",
1195 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1201 ids[count] = comp_id;
1202 flags_array[count] = flags;
1207 if (neg_flags & LCME_FL_STALE) {
1209 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1214 ids[count] = comp_id;
1215 flags_array[count] = neg_flags | LCME_FL_NEG;
1219 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1221 if (errno == EUCLEAN) {
1224 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1225 progname, comp_id, fname);
1228 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1229 progname, comp_id, fname, flags, neg_flags);
1236 static int lfs_component_del(char *fname, __u32 comp_id,
1237 __u32 flags, __u32 neg_flags)
1241 if (flags && neg_flags) {
1243 "%s: cannot specify both positive and negative flags\n",
1248 if (!flags && neg_flags)
1249 flags = neg_flags | LCME_FL_NEG;
1251 if (flags && comp_id) {
1253 "%s: cannot specify component ID and flags at the same time\n",
1258 if (!flags && !comp_id) {
1260 "%s: neither flags nor component ID is specified\n",
1266 if (flags & ~LCME_KNOWN_FLAGS) {
1268 "%s setstripe: unknown flags %#x\n",
1272 } else if (comp_id > LCME_ID_MAX) {
1273 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1278 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1281 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1282 progname, comp_id, fname, strerror(errno));
1286 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1293 rc = llapi_layout_file_comp_add(fname, layout);
1295 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1296 fname, strerror(errno));
1300 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1301 struct llapi_layout *layout)
1309 fd = lstat(fname, &st);
1310 if (fd == 0 && S_ISDIR(st.st_mode))
1311 open_flags = O_DIRECTORY | O_RDONLY;
1313 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1315 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1316 S_ISDIR(st.st_mode) ?
1317 "set default composite layout for" :
1318 "create composite file",
1319 fname, strerror(errno));
1323 static int lfs_migrate(char *name, __u64 migration_flags,
1324 struct llapi_stripe_param *param,
1325 struct llapi_layout *layout,
1326 unsigned long long bandwidth_bytes_sec,
1327 long stats_interval_sec)
1329 struct llapi_layout *existing;
1330 uint64_t dom_new, dom_cur;
1335 rc = migrate_open_files(name, migration_flags, param, layout,
1340 rc = llapi_layout_dom_size(layout, &dom_new);
1342 error_loc = "cannot get new layout DoM size";
1345 /* special case for migration to DOM layout*/
1346 existing = llapi_layout_get_by_fd(fd_src, 0);
1348 error_loc = "cannot get existing layout";
1352 rc = llapi_layout_dom_size(existing, &dom_cur);
1354 error_loc = "cannot get current layout DoM size";
1359 * if file has DoM layout already then migration is possible to
1360 * the new layout with the same DoM component via swap layout,
1361 * if new layout used bigger DOM size, then mirroring is used
1363 if (dom_new > dom_cur) {
1364 rc = lfs_migrate_to_dom(fd_src, fd_dst, name,
1366 bandwidth_bytes_sec,
1367 stats_interval_sec);
1369 error_loc = "cannot migrate to DOM layout";
1373 if (stats_interval_sec)
1374 printf("%s:\n", name);
1376 if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1378 * Blocking mode (forced if servers do not support file lease).
1379 * It is also the default mode, since we cannot distinguish
1380 * between a broken lease and a server that does not support
1381 * atomic swap/close (LU-6785)
1383 rc = migrate_block(fd_src, fd_dst, bandwidth_bytes_sec,
1384 stats_interval_sec);
1388 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
1390 error_loc = "cannot get lease";
1394 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
1395 stats_interval_sec);
1397 llapi_lease_release(fd_src);
1402 * Atomically put lease, swap layouts and close.
1403 * for a migration we need to check data version on file did
1406 rc = llapi_fswap_layouts(fd_src, fd_dst, 0, 0, SWAP_LAYOUTS_CLOSE);
1408 error_loc = "cannot swap layout";
1420 fprintf(stderr, "error: %s: %s: %s: %s\n",
1421 progname, name, error_loc, strerror(-rc));
1422 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1423 printf("%s\n", name);
1428 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1431 char *dup_string = NULL;
1437 if (!string || !string[0])
1440 dup_string = strdup(string);
1442 llapi_printf(LLAPI_MSG_ERROR,
1443 "%s: insufficient memory\n",
1448 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1452 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1453 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1454 const char *comp_name = comp_flags_table[i].cfn_name;
1456 if (strcmp(name, comp_name) == 0) {
1457 *flags |= comp_flag;
1459 } else if (strncmp(name, "^", 1) == 0 &&
1460 strcmp(name + 1, comp_name) == 0) {
1461 *neg_flags |= comp_flag;
1466 llapi_printf(LLAPI_MSG_ERROR,
1467 "%s: component flag '%s' not supported\n",
1474 if (!*flags && !*neg_flags)
1477 /* don't allow to set and exclude the same flag */
1478 if (*flags & *neg_flags)
1486 static int mdthash_input(char *string, __u32 *inflags,
1487 __u32 *exflags, __u32 *type)
1494 {"migrating", LMV_HASH_FLAG_MIGRATION},
1495 {"bad_type", LMV_HASH_FLAG_BAD_TYPE},
1496 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1497 {"lost_lmv", LMV_HASH_FLAG_LOST_LMV},
1498 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1507 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1511 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1512 if (strcmp(name, mhflist[i].name) == 0 ||
1513 name[0] == mhflist[i].name[0]) {
1514 *inflags |= mhflist[i].flag;
1516 } else if (name[0] == '^' &&
1517 (strcmp(name + 1, mhflist[i].name) == 0 ||
1518 name[1] == mhflist[i].name[0])) {
1519 *exflags |= mhflist[i].flag;
1524 i = check_hashtype(name);
1529 llapi_printf(LLAPI_MSG_ERROR,
1530 "%s: invalid mdt_hash value '%s'\n",
1536 /* don't allow to include and exclude the same flag */
1537 if (*inflags & *exflags) {
1538 llapi_printf(LLAPI_MSG_ERROR,
1539 "%s: include and exclude same flag '%s'\n",
1547 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1555 if (strncmp(string, "^", 1) == 0) {
1556 *neg_state = llapi_layout_string_flags(string + 1);
1557 if (*neg_state != 0)
1560 *state = llapi_layout_string_flags(string);
1565 llapi_printf(LLAPI_MSG_ERROR,
1566 "%s: mirrored file state '%s' not supported\n",
1572 * struct mirror_args - Command-line arguments for mirror(s).
1573 * @m_count: Number of mirrors to be created with this layout.
1574 * @m_flags: Mirror level flags, only 'prefer' is supported.
1575 * @m_layout: Mirror layout.
1576 * @m_file: A victim file. Its layout will be split and used as a mirror.
1577 * @m_next: Point to the next node of the list.
1579 * Command-line arguments for mirror(s) will be parsed and stored in
1580 * a linked list that consists of this structure.
1582 struct mirror_args {
1585 struct llapi_layout *m_layout;
1587 struct mirror_args *m_next;
1592 * enum mirror_flags - Flags for extending a mirrored file.
1593 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1594 * in case the victim file(s) contains the same data as the
1595 * original mirrored file.
1596 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1597 * @MF_COMP_ID: specified component id instead of mirror id
1599 * Flags for extending a mirrored file.
1609 * mirror_create_sanity_check() - Check mirror list.
1610 * @list: A linked list that stores the mirror arguments.
1612 * This function does a sanity check on @list for creating
1615 * Return: 0 on success or a negative error code on failure.
1617 static int mirror_create_sanity_check(const char *fname,
1618 struct mirror_args *list)
1621 bool has_m_file = false;
1622 char fsname[MAX_OBD_NAME + 1] = { 0 };
1623 bool has_m_layout = false;
1629 rc = llapi_search_fsname(fname, fsname);
1632 "error: %s: file '%s' has no fsname\n",
1641 llapi_layout_free(list->m_layout);
1644 llapi_layout_get_by_path(list->m_file, 0);
1645 if (!list->m_layout) {
1647 "error: %s: file '%s' has no layout\n",
1648 progname, list->m_file);
1652 has_m_layout = true;
1653 if (!list->m_layout) {
1654 fprintf(stderr, "error: %s: no mirror layout\n",
1659 rc = llapi_layout_v2_sanity(list->m_layout, false, true,
1662 llapi_layout_sanity_perror(rc);
1666 list = list->m_next;
1669 if (has_m_file && has_m_layout) {
1671 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1679 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1681 __u32 mirror_flags = *(__u32 *)cbdata;
1685 rc = llapi_layout_comp_flags_get(layout, &flags);
1690 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1695 return LLAPI_LAYOUT_ITER_CONT;
1699 * mirror_create() - Create a mirrored file.
1700 * @fname: The file to be created.
1701 * @mirror_list: A linked list that stores the mirror arguments.
1703 * This function creates a mirrored file @fname with the mirror(s)
1704 * from @mirror_list.
1706 * Return: 0 on success or a negative error code on failure.
1708 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1710 struct llapi_layout *layout = NULL;
1711 struct mirror_args *cur_mirror = NULL;
1712 uint16_t mirror_count = 0;
1716 rc = mirror_create_sanity_check(fname, mirror_list);
1720 cur_mirror = mirror_list;
1721 while (cur_mirror) {
1722 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1724 &cur_mirror->m_flags);
1727 fprintf(stderr, "%s: failed to set mirror flags\n",
1732 for (i = 0; i < cur_mirror->m_count; i++) {
1733 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1737 "error: %s: merge layout failed: %s\n",
1738 progname, strerror(errno));
1742 mirror_count += cur_mirror->m_count;
1743 cur_mirror = cur_mirror->m_next;
1747 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1751 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1754 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1755 progname, strerror(errno));
1759 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1767 llapi_layout_free(layout);
1772 * Compare files and check lease on @fd.
1774 * \retval bytes number of bytes are the same
1776 static ssize_t mirror_file_compare(int fd_src, int fd_dst)
1778 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1780 ssize_t bytes_done = 0;
1781 ssize_t bytes_read = 0;
1783 buf = malloc(buflen * 2);
1788 if (!llapi_lease_check(fd_src)) {
1789 bytes_done = -EBUSY;
1793 bytes_read = read(fd_src, buf, buflen);
1794 if (bytes_read <= 0)
1797 if (bytes_read != read(fd_dst, buf + buflen, buflen))
1801 * XXX: should compute the checksum on each buffer and then
1802 * compare checksum to avoid cache collision
1804 if (memcmp(buf, buf + buflen, bytes_read))
1807 bytes_done += bytes_read;
1815 static int mirror_extend_file(const char *fname, const char *victim_file,
1816 enum mirror_flags mirror_flags)
1821 struct stat stbuf_v;
1822 struct ll_ioc_lease *data = NULL;
1825 fd = open(fname, O_RDWR);
1827 error_loc = "open source file";
1832 fdv = open(victim_file, O_RDWR);
1834 error_loc = "open target file";
1839 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1840 error_loc = "stat source or target file";
1845 if (stbuf.st_dev != stbuf_v.st_dev) {
1846 error_loc = "stat source and target file";
1851 /* mirrors should be of the same size */
1852 if (stbuf.st_size != stbuf_v.st_size) {
1853 error_loc = "file sizes don't match";
1858 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1860 error_loc = "cannot get lease";
1864 if (!(mirror_flags & MF_NO_VERIFY)) {
1866 /* mirrors should have the same contents */
1867 ret = mirror_file_compare(fd, fdv);
1868 if (ret != stbuf.st_size) {
1869 error_loc = "file busy or contents don't match";
1870 rc = ret < 0 ? ret : -EINVAL;
1875 /* Get rid of caching pages from clients */
1876 rc = llapi_file_flush(fd);
1878 error_loc = "cannot get data version";
1882 rc = llapi_file_flush(fdv);
1884 error_loc = "cannot get data version";
1888 rc = migrate_set_timestamps(fd, &stbuf);
1890 error_loc = "cannot set source file timestamp";
1894 /* Atomically put lease, merge layouts and close. */
1895 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1897 error_loc = "memory allocation";
1900 data->lil_mode = LL_LEASE_UNLCK;
1901 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1902 data->lil_count = 1;
1903 data->lil_ids[0] = fdv;
1904 rc = llapi_lease_set(fd, data);
1906 error_loc = "cannot merge layout";
1908 } else if (rc == 0) {
1910 error_loc = "lost lease lock";
1923 (void) unlink(victim_file);
1925 fprintf(stderr, "error: %s: %s: %s: %s\n",
1926 progname, fname, error_loc, strerror(-rc));
1930 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1931 bool inherit, uint32_t flags,
1932 unsigned long long bandwidth_bytes_sec,
1933 long stats_interval_sec)
1935 struct llapi_layout *f_layout = NULL;
1936 struct ll_ioc_lease *data = NULL;
1943 f_layout = llapi_layout_get_by_path(name, 0);
1946 fprintf(stderr, "%s: cannot get layout\n", progname);
1949 rc = llapi_layout_get_last_init_comp(f_layout);
1951 fprintf(stderr, "%s: cannot get the last init comp\n",
1955 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1958 "%s: cannot inherit from the last init comp\n",
1964 llapi_layout_comp_flags_set(m_layout, flags);
1965 rc = migrate_open_files(name,
1966 LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1967 NULL, m_layout, &fd_src, &fd_dst);
1971 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
1973 error_loc = "cannot get lease";
1977 rc = fstat(fd_src, &st);
1979 error_loc = "cannot stat source file";
1983 if (stats_interval_sec)
1984 printf("%s:\n", name);
1986 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
1987 stats_interval_sec);
1989 llapi_lease_release(fd_src);
1993 rc = migrate_set_timestamps(fd_src, &st);
1995 error_loc = "cannot set source file timestamp";
1999 /* Atomically put lease, merge layouts and close. */
2000 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2002 error_loc = "memory allocation";
2005 data->lil_mode = LL_LEASE_UNLCK;
2006 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2007 data->lil_count = 1;
2008 data->lil_ids[0] = fd_dst;
2009 rc = llapi_lease_set(fd_src, data);
2011 error_loc = "cannot merge layout";
2013 } else if (rc == 0) {
2015 error_loc = "lost lease lock";
2028 fprintf(stderr, "error: %s: %s: %s: %s\n",
2029 progname, name, error_loc, strerror(-rc));
2033 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2034 enum mirror_flags mirror_flags,
2035 unsigned long long bandwidth_bytes_sec,
2036 long stats_interval_sec)
2040 while (mirror_list) {
2041 if (mirror_list->m_file) {
2042 rc = mirror_extend_file(fname, mirror_list->m_file,
2045 __u32 mirror_count = mirror_list->m_count;
2047 while (mirror_count > 0) {
2048 rc = mirror_extend_layout(fname,
2049 mirror_list->m_layout,
2050 mirror_list->m_inherit,
2051 mirror_list->m_flags,
2052 bandwidth_bytes_sec,
2053 stats_interval_sec);
2063 mirror_list = mirror_list->m_next;
2069 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2074 rc = llapi_layout_mirror_id_get(layout, &id);
2078 if ((__u16)id == *(__u16 *)cbdata)
2079 return LLAPI_LAYOUT_ITER_STOP;
2081 return LLAPI_LAYOUT_ITER_CONT;
2084 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2089 rc = llapi_layout_comp_id_get(layout, &id);
2093 if (id == *(__u32 *)cbdata)
2094 return LLAPI_LAYOUT_ITER_STOP;
2096 return LLAPI_LAYOUT_ITER_CONT;
2099 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2101 char buf[LOV_MAXPOOLNAME + 1];
2102 struct pool_to_id_cbdata *d = (void *)cbdata;
2106 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2109 if (strcmp(d->pool, buf))
2110 return LLAPI_LAYOUT_ITER_CONT;
2112 rc = llapi_layout_mirror_id_get(layout, &id);
2117 return LLAPI_LAYOUT_ITER_STOP;
2120 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2122 char buf[LOV_MAXPOOLNAME + 1];
2123 struct pool_to_id_cbdata *d = (void *)cbdata;
2127 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2130 if (strcmp(d->pool, buf))
2131 return LLAPI_LAYOUT_ITER_CONT;
2133 rc = llapi_layout_comp_id_get(layout, &id);
2138 return LLAPI_LAYOUT_ITER_STOP;
2141 struct collect_ids_data {
2147 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2149 struct collect_ids_data *cid = cbdata;
2153 rc = llapi_layout_mirror_id_get(layout, &id);
2157 if ((__u16)id != cid->cid_exclude) {
2160 for (i = 0; i < cid->cid_count; i++) {
2161 /* already collected the mirror id */
2162 if (id == cid->cid_ids[i])
2163 return LLAPI_LAYOUT_ITER_CONT;
2165 cid->cid_ids[cid->cid_count] = id;
2169 return LLAPI_LAYOUT_ITER_CONT;
2173 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2174 * @mirror_id: Mirror id to be checked.
2175 * @layout: Mirror component list.
2177 * This function checks if a mirror with specified @mirror_id is the last
2178 * non-stale mirror of a layout @layout.
2180 * Return: true or false.
2183 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2185 __u16 mirror_ids[128] = { 0 };
2186 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2188 .cid_exclude = mirror_id, };
2191 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2193 for (i = 0; i < cid.cid_count; i++) {
2194 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2197 comp_size = llapi_mirror_find_stale(layout, comp_array,
2198 ARRAY_SIZE(comp_array),
2207 static int mirror_split(const char *fname, __u32 id, const char *pool,
2208 enum mirror_flags mflags, const char *victim_file)
2210 struct llapi_layout *layout;
2211 char parent[PATH_MAX];
2212 char victim[PATH_MAX];
2213 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2215 struct ll_ioc_lease *data;
2216 uint16_t mirror_count;
2220 bool purge = true; /* delete mirror by setting fdv=fd */
2224 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2226 "error %s: the source file '%s' and -f file are the same\n",
2231 /* check fname contains mirror with mirror_id/comp_id */
2232 layout = llapi_layout_get_by_path(fname, 0);
2235 "error %s: file '%s' couldn't get layout\n",
2240 rc = llapi_layout_sanity(layout, false, true);
2242 llapi_layout_sanity_perror(rc);
2246 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2249 "error %s: file '%s' couldn't get mirror count\n",
2253 if (mirror_count < 2) {
2255 "error %s: file '%s' has %d component, cannot split\n",
2256 progname, fname, mirror_count);
2260 if (mflags & MF_COMP_POOL) {
2261 struct pool_to_id_cbdata data = { .pool = pool };
2263 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2265 mirror_id = data.id;
2266 } else if (mflags & MF_COMP_ID) {
2267 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2268 mirror_id = mirror_id_of(id);
2270 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2274 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2277 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2278 if (mflags & MF_COMP_POOL) {
2280 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2281 progname, fname, pool);
2283 } else if (mflags & MF_COMP_ID) {
2285 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2286 progname, fname, id);
2290 "error %s: file '%s' does not contain mirror with id %u\n",
2291 progname, fname, id);
2296 if (last_non_stale_mirror(mirror_id, layout)) {
2299 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2304 if (!victim_file && mflags & MF_DESTROY)
2305 /* Allow mirror split even without the key on encrypted files,
2306 * and in this case of a 'split -d', open file with O_DIRECT
2307 * (no IOs will be done).
2309 fd = open(fname, O_RDWR | O_DIRECT | O_CIPHERTEXT);
2311 fd = open(fname, O_RDWR);
2315 "error %s: open file '%s' failed: %s\n",
2316 progname, fname, strerror(errno));
2320 /* get victim file directory pathname */
2321 if (strlen(fname) > sizeof(parent) - 1) {
2322 fprintf(stderr, "error %s: file name of '%s' too long\n",
2327 strncpy(parent, fname, sizeof(parent));
2328 ptr = strrchr(parent, '/');
2330 if (!getcwd(parent, sizeof(parent))) {
2331 fprintf(stderr, "error %s: getcwd failed: %s\n",
2332 progname, strerror(errno));
2342 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2344 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2349 rc = llapi_file_is_encrypted(fd);
2351 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2352 progname, fname, rc);
2359 /* use a temp file to store the splitted layout */
2360 if (mflags & MF_DESTROY) {
2361 char file_path[PATH_MAX];
2362 unsigned int rnumber;
2365 if (last_non_stale_mirror(mirror_id, layout)) {
2368 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2374 /* don't use volatile file for mirror destroy */
2378 * try the old way to delete mirror using
2383 rc = snprintf(file_path,
2385 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2389 rc >= sizeof(file_path)) {
2390 fdv = -ENAMETOOLONG;
2394 open_flags = O_RDWR |
2395 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2396 O_CREAT | O_EXCL | O_NOFOLLOW |
2397 /* O_DIRECT for mirror split -d */
2399 /* Allow split without the key */
2401 fdv = open(file_path, open_flags,
2405 } while (fdv < 0 && rc == -EEXIST);
2411 "error %s: not permitted on encrypted file '%s': %d\n",
2412 progname, fname, rc);
2416 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2418 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2421 /* user specified victim file */
2425 "error %s: not permitted on encrypted file '%s': %d\n",
2426 progname, fname, rc);
2429 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2434 "error %s: create victim file failed: %s\n",
2435 progname, strerror(errno));
2439 /* get lease lock of fname */
2440 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2443 "error %s: cannot get lease of file '%s': %d\n",
2444 progname, fname, rc);
2448 /* Atomatically put lease, split layouts and close. */
2449 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2455 data->lil_mode = LL_LEASE_UNLCK;
2456 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2457 data->lil_count = 2;
2458 data->lil_ids[0] = fdv;
2459 data->lil_ids[1] = mirror_id;
2460 rc = llapi_lease_set(fd, data);
2462 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2463 /* could be old MDS which prohibit fd==fdv */
2468 if (rc == 0) /* lost lease lock */
2471 "error %s: cannot split '%s': %s\n",
2472 progname, fname, strerror(-rc));
2484 llapi_layout_free(layout);
2489 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2490 __u16 *mirror_ids, int ids_nr,
2491 long stats_interval_sec, long bandwidth_bytes_sec);
2494 static int lfs_migrate_to_dom(int fd_src, int fd_dst, char *name,
2495 __u64 migration_flags,
2496 unsigned long long bandwidth_bytes_sec,
2497 long stats_interval_sec)
2499 struct ll_ioc_lease *data = NULL;
2502 rc = llapi_lease_acquire(fd_src, LL_LEASE_RDLCK);
2504 error_loc = "cannot get lease";
2508 if (stats_interval_sec)
2509 printf("%s:\n", name);
2511 rc = migrate_nonblock(fd_src, fd_dst, bandwidth_bytes_sec,
2512 stats_interval_sec);
2516 /* Atomically put lease, merge layouts, resync and close. */
2517 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2519 error_loc = "memory allocation";
2522 data->lil_mode = LL_LEASE_UNLCK;
2523 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2524 data->lil_count = 1;
2525 data->lil_ids[0] = fd_dst;
2526 rc = llapi_lease_set(fd_src, data);
2528 error_loc = "cannot merge layout";
2530 } else if (rc == 0) {
2532 error_loc = "lost lease lock";
2538 rc = lfs_mirror_resync_file(name, data, NULL, 0,
2540 bandwidth_bytes_sec);
2542 error_loc = "cannot resync file";
2546 /* delete first mirror now */
2547 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2549 error_loc = "cannot delete old layout";
2553 llapi_lease_release(fd_src);
2559 fprintf(stderr, "error: %s: %s: %s: %s\n",
2560 progname, name, error_loc, strerror(-rc));
2561 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2562 printf("%s\n", name);
2569 * Parse a string containing an target index list into an array of integers.
2571 * The input string contains a comma delimited list of individual
2572 * indices and ranges, for example "1,2-4,7". Add the indices into the
2573 * \a tgts array and remove duplicates.
2575 * \param[out] tgts array to store indices in
2576 * \param[in] size size of \a tgts array
2577 * \param[in] offset starting index in \a tgts
2578 * \param[in] arg string containing OST index list
2579 * \param[in/out] overstriping index list may contain duplicates
2581 * \retval positive number of indices in \a tgts
2582 * \retval -EINVAL unable to parse \a arg
2584 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2585 unsigned long long *pattern)
2589 int slots = size - offset;
2591 bool overstriped = false;
2597 end_of_loop = false;
2598 while (!end_of_loop) {
2599 int start_index = 0;
2602 char *endptr = NULL;
2606 ptr = strchrnul(arg, ',');
2608 end_of_loop = *ptr == '\0';
2612 start_index = strtol(arg, &endptr, 0);
2613 if (endptr == arg) /* no data at all */
2615 if (errno != 0 || start_index < -1 ||
2616 (*endptr != '-' && *endptr != '\0'))
2619 end_index = start_index;
2620 if (*endptr == '-') {
2622 end_index = strtol(endptr + 1, &endptr, 0);
2623 if (errno != 0 || *endptr != '\0' || end_index < -1)
2625 if (end_index < start_index)
2629 for (i = start_index; i <= end_index && slots > 0; i++) {
2632 /* remove duplicate */
2633 for (j = 0; j < offset; j++) {
2634 if (tgts[j] == i && pattern &&
2635 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2637 else if (tgts[j] == i)
2643 if (j == offset) { /* check complete */
2649 if (slots == 0 && i < end_index)
2657 if (!end_of_loop && ptr)
2660 if (!overstriped && pattern)
2661 *pattern = LLAPI_LAYOUT_DEFAULT;
2663 return rc < 0 ? rc : nr;
2666 struct lfs_setstripe_args {
2667 unsigned long long lsa_comp_end;
2668 unsigned long long lsa_stripe_size;
2669 unsigned long long lsa_extension_size;
2670 long long lsa_stripe_count;
2671 long long lsa_stripe_off;
2672 __u32 lsa_comp_flags;
2673 __u32 lsa_comp_neg_flags;
2674 unsigned long long lsa_pattern;
2675 unsigned int lsa_mirror_count;
2677 bool lsa_first_comp;
2678 bool lsa_extension_comp;
2680 char *lsa_pool_name;
2683 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2685 unsigned int mirror_count = lsa->lsa_mirror_count;
2686 bool first_comp = lsa->lsa_first_comp;
2688 memset(lsa, 0, sizeof(*lsa));
2690 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2691 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2692 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2693 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2694 lsa->lsa_pool_name = NULL;
2696 lsa->lsa_mirror_count = mirror_count;
2697 lsa->lsa_first_comp = first_comp;
2701 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2702 * @lsa: Stripe options to be initialized and inherited.
2704 * This function initializes stripe options in @lsa and inherit
2705 * stripe_size, stripe_count and OST pool_name options.
2709 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2711 unsigned long long stripe_size;
2712 long long stripe_count;
2713 char *pool_name = NULL;
2715 stripe_size = lsa->lsa_stripe_size;
2716 stripe_count = lsa->lsa_stripe_count;
2717 pool_name = lsa->lsa_pool_name;
2719 setstripe_args_init(lsa);
2721 lsa->lsa_stripe_size = stripe_size;
2722 lsa->lsa_stripe_count = stripe_count;
2723 lsa->lsa_pool_name = pool_name;
2726 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2728 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2729 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2730 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2731 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2732 lsa->lsa_comp_end != 0);
2735 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2737 if (lsa->lsa_nr_tgts) {
2738 if (lsa->lsa_nr_tgts < 0 ||
2739 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2740 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2746 if (lsa->lsa_stripe_count > 0 &&
2747 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2748 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2749 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2750 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2751 lsa->lsa_stripe_count,
2763 * comp_args_to_layout() - Create or extend a composite layout.
2764 * @composite: Pointer to the composite layout.
2765 * @lsa: Stripe options for the new component.
2767 * This function creates or extends a composite layout by adding a new
2768 * component with stripe options from @lsa.
2770 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2772 * Return: 0 on success or an error code on failure.
2774 static int comp_args_to_layout(struct llapi_layout **composite,
2775 struct lfs_setstripe_args *lsa,
2778 struct llapi_layout *layout = *composite;
2779 uint64_t prev_end = 0;
2785 layout = llapi_layout_alloc();
2787 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2792 *composite = layout;
2793 lsa->lsa_first_comp = true;
2798 * Get current component extent, current component
2799 * must be the tail component.
2801 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2803 fprintf(stderr, "Get comp extent failed. %s\n",
2808 if (lsa->lsa_first_comp) {
2810 rc = llapi_layout_add_first_comp(layout);
2812 rc = llapi_layout_comp_add(layout);
2815 fprintf(stderr, "Add component failed. %s\n",
2821 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2823 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2824 lsa->lsa_comp_flags, strerror(errno));
2829 uint64_t comp_end = lsa->lsa_comp_end;
2832 * The extendable component is 0-length, so it can be removed
2833 * if there is insufficient space to extend it.
2835 if (lsa->lsa_extension_comp)
2836 comp_end = prev_end;
2838 rc = llapi_layout_comp_extent_set(layout, prev_end,
2841 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2842 prev_end, comp_end, strerror(errno));
2846 /* reset lsa_first_comp */
2847 lsa->lsa_first_comp = false;
2849 /* Data-on-MDT component setting */
2850 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2852 if (lsa->lsa_stripe_count == 0)
2853 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2854 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2855 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2856 if (lsa->lsa_stripe_off == -1 ||
2857 lsa->lsa_stripe_off == 0)
2858 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2860 * In case of Data-on-MDT patterns the only extra option
2861 * applicable is stripe size option.
2863 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2865 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2866 lsa->lsa_stripe_count);
2870 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2871 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2873 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2874 lsa->lsa_stripe_size);
2878 if (lsa->lsa_nr_tgts != 0) {
2880 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2885 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2887 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2888 lsa->lsa_stripe_off);
2892 if (lsa->lsa_pool_name != 0) {
2894 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2895 lsa->lsa_pool_name);
2900 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2902 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2907 /* Data-on-MDT component has always single stripe up to end */
2908 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2909 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2910 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2912 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2919 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2920 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2922 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2923 rc = llapi_layout_extension_size_set(layout, size);
2925 rc = llapi_layout_stripe_size_set(layout, size);
2928 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2929 size, strerror(errno));
2933 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2935 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2936 lsa->lsa_stripe_count, strerror(errno));
2940 if (lsa->lsa_pool_name) {
2941 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2943 fprintf(stderr, "Set pool name: %s failed. %s\n",
2944 lsa->lsa_pool_name, strerror(errno));
2948 rc = llapi_layout_pool_name_set(layout, "");
2950 fprintf(stderr, "Clear pool name failed: %s\n",
2956 rc = lsa_args_stripe_count_check(lsa);
2960 if (lsa->lsa_nr_tgts > 0) {
2963 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2964 rc = llapi_layout_ost_index_set(layout, i,
2969 /* Make sure stripe offset is in OST list. */
2970 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2974 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2975 lsa->lsa_stripe_off);
2979 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2980 lsa->lsa_stripe_off != -1) {
2981 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2984 fprintf(stderr, "Set ost index %d failed. %s\n",
2985 i, strerror(errno));
2989 /* Create the second, virtual component of extension space */
2990 if (lsa->lsa_extension_comp) {
2991 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2992 lsa->lsa_extension_comp = false;
2999 static int build_component(struct llapi_layout **layout,
3000 struct lfs_setstripe_args *lsa, bool set_extent)
3004 rc = comp_args_to_layout(layout, lsa, set_extent);
3008 if (lsa->lsa_mirror_count > 0) {
3009 rc = llapi_layout_mirror_count_set(*layout,
3010 lsa->lsa_mirror_count);
3014 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
3017 lsa->lsa_mirror_count = 0;
3023 static int build_prev_component(struct llapi_layout **layout,
3024 struct lfs_setstripe_args *prev,
3025 struct lfs_setstripe_args *lsa,
3028 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
3031 if (prev->lsa_stripe_size) {
3033 prev->lsa_comp_end = lsa->lsa_comp_end;
3034 prev->lsa_extension_size = lsa->lsa_extension_size;
3035 prev->lsa_extension_comp = true;
3038 rc = build_component(layout, prev, true);
3044 * Copy lsa to previous lsa;
3045 * if this is an extension component, make the previous invalid;
3048 prev->lsa_stripe_size = 0;
3055 #ifndef LCME_TEMPLATE_FLAGS
3056 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
3060 static int build_layout_from_yaml_node(struct cYAML *node,
3061 struct llapi_layout **layout,
3062 struct lfs_setstripe_args *lsa,
3063 struct lfs_setstripe_args *prevp)
3065 struct lfs_setstripe_args prev = { 0 };
3066 __u32 *osts = lsa->lsa_tgts;
3074 string = node->cy_string;
3076 if (node->cy_type == CYAML_TYPE_OBJECT) {
3077 /* go deep to sub blocks */
3078 if (string && !strncmp(string, "component", 9) &&
3079 strncmp(string, "component0", 10) &&
3080 strncmp(string, "components", 10)) {
3081 rc = build_prev_component(layout, prevp, lsa,
3086 /* initialize lsa. */
3087 setstripe_args_init(lsa);
3088 lsa->lsa_first_comp = false;
3089 lsa->lsa_tgts = osts;
3092 rc = build_layout_from_yaml_node(node->cy_child, layout,
3097 if (!node->cy_string)
3100 /* skip leading lmm_ if present, to simplify parsing */
3101 if (strncmp(string, "lmm_", 4) == 0)
3104 if (node->cy_type == CYAML_TYPE_STRING) {
3105 if (!strcmp(string, "lcme_extent.e_end")) {
3106 if (!strcmp(node->cy_valuestring, "EOF") ||
3107 !strcmp(node->cy_valuestring, "eof"))
3108 lsa->lsa_comp_end = LUSTRE_EOF;
3109 } else if (!strcmp(string, "pool")) {
3110 lsa->lsa_pool_name = node->cy_valuestring;
3111 } else if (!strcmp(string, "pattern")) {
3112 if (!strcmp(node->cy_valuestring, "mdt"))
3113 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3114 if (!strcmp(node->cy_valuestring,
3115 "raid0,overstriped"))
3117 LLAPI_LAYOUT_OVERSTRIPING;
3118 } else if (!strcmp(string, "lcme_flags")) {
3119 rc = comp_str2flags(node->cy_valuestring,
3120 &lsa->lsa_comp_flags,
3121 &lsa->lsa_comp_neg_flags);
3125 * Only template flags have meaning in
3126 * the layout for a new file
3128 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3130 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3131 if (!strcmp(string, "lcm_mirror_count")) {
3132 lsa->lsa_mirror_count = node->cy_valueint;
3133 } else if (!strcmp(string, "lcme_extent.e_start")) {
3134 if (node->cy_valueint == 0)
3135 lsa->lsa_first_comp = true;
3136 } else if (!strcmp(string, "lcme_extent.e_end")) {
3137 if (node->cy_valueint == -1)
3138 lsa->lsa_comp_end = LUSTRE_EOF;
3140 lsa->lsa_comp_end = node->cy_valueint;
3141 } else if (!strcmp(string, "stripe_count")) {
3142 lsa->lsa_stripe_count = node->cy_valueint;
3143 } else if (!strcmp(string, "stripe_size")) {
3144 lsa->lsa_stripe_size = node->cy_valueint;
3145 } else if (!strcmp(string, "extension_size")) {
3146 lsa->lsa_extension_size = node->cy_valueint;
3147 lsa->lsa_extension_comp = true;
3148 } else if (!strcmp(string, "stripe_offset")) {
3149 lsa->lsa_stripe_off = node->cy_valueint;
3150 } else if (!strcmp(string, "l_ost_idx")) {
3151 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3156 node = node->cy_next;
3159 if (prevp == &prev) {
3160 rc = build_prev_component(layout, prevp, lsa, true);
3164 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3165 rc = build_component(layout, lsa, *layout != NULL);
3171 static int lfs_comp_create_from_yaml(char *template,
3172 struct llapi_layout **layout,
3173 struct lfs_setstripe_args *lsa,
3176 struct cYAML *tree = NULL, *err_rc = NULL;
3179 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3181 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3182 progname, template);
3183 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3184 "can't parse", &err_rc);
3185 cYAML_print_tree2file(stderr, err_rc);
3186 cYAML_free_tree(err_rc);
3191 /* initialize lsa for plain file */
3192 setstripe_args_init(lsa);
3193 lsa->lsa_tgts = osts;
3195 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3197 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3198 progname, template);
3201 /* clean clean lsa */
3202 setstripe_args_init(lsa);
3206 cYAML_free_tree(tree);
3211 * Get the extension size from the next (SEL) component and extend the
3212 * current component on it. The start of the next component is to be
3215 * \param[in] layout the current layout
3216 * \param[in] start the start of the current component
3217 * \param[in,out] end the end of the current component
3218 * \param[in] offset the offset to adjust the end position to instead of
3221 * \retval 0 - extended successfully
3222 * \retval < 0 - error
3224 static int layout_extend_comp(struct llapi_layout *layout,
3225 uint64_t start, uint64_t *end,
3228 uint64_t size, next_start, next_end;
3231 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3234 "%s setstripe: cannot move component cursor: %s\n",
3235 progname, strerror(errno));
3240 * Even if the @size will not be used below, this will fail if
3241 * this is not a SEL component - a good confirmation we are
3242 * working on right components.
3244 rc = llapi_layout_extension_size_get(layout, &size);
3247 "%s setstripe: cannot get component ext size: %s\n",
3248 progname, strerror(errno));
3252 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3254 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3255 progname, strerror(errno));
3259 next_start += offset ?: size;
3260 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3262 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3263 progname, strerror(errno));
3267 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3270 "%s setstripe: cannot move component cursor: %s\n",
3271 progname, strerror(errno));
3275 *end += offset ?: size;
3276 rc = llapi_layout_comp_extent_set(layout, start, *end);
3278 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3279 progname, strerror(errno));
3287 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3288 * end of the last component in the existing file, and adjust the
3289 * first extent start of the components to be added accordingly.
3291 * In the create mode, we need to check if the first component is an extendable
3292 * SEL component and extend its length to the extension size (first component
3293 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3295 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3298 struct llapi_layout *head;
3299 uint64_t start = 0, prev_end = 0;
3303 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3308 head = llapi_layout_get_by_path(fname, 0);
3311 "%s setstripe: cannot read layout from '%s': %s\n",
3312 progname, fname, strerror(errno));
3314 } else if (errno == ENODATA) {
3316 * file without LOVEA, this component-add will be turned
3317 * into a component-create.
3319 llapi_layout_free(head);
3323 * the new layout will be added to an empty one, it
3324 * still needs to be adjusted below
3328 } else if (!llapi_layout_is_composite(head)) {
3330 "%s setstripe: '%s' not a composite file\n",
3332 llapi_layout_free(head);
3336 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3339 "%s setstripe: cannot get prev extent: %s\n",
3340 progname, strerror(errno));
3341 llapi_layout_free(head);
3345 llapi_layout_free(head);
3349 /* Make sure we use the first component of the layout to be added. */
3350 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3353 "%s setstripe: cannot move component cursor: %s\n",
3354 progname, strerror(errno));
3358 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3360 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3361 progname, strerror(errno));
3365 if (start == 0 && end == 0) {
3366 rc = layout_extend_comp(layout, start, &end,
3367 comp_add ? prev_end : 0);
3372 if (start > prev_end || end < prev_end) {
3374 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3375 progname, start, end, prev_end);
3379 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3382 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3383 progname, prev_end, end, strerror(errno));
3390 static int mirror_adjust_first_extents(struct mirror_args *list)
3398 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3401 list = list->m_next;
3407 static inline bool arg_is_eof(char *arg)
3409 return !strncmp(arg, "-1", strlen("-1")) ||
3410 !strncmp(arg, "EOF", strlen("EOF")) ||
3411 !strncmp(arg, "eof", strlen("eof"));
3415 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3417 * Return: Valid mirror_args pointer on success and
3418 * NULL if memory allocation fails.
3420 static struct mirror_args *lfs_mirror_alloc(void)
3422 struct mirror_args *mirror = NULL;
3425 mirror = calloc(1, sizeof(*mirror));
3427 mirror->m_inherit = false;
3438 * lfs_mirror_free() - Free memory allocated for a mirror argument
3440 * @mirror: Previously allocated mirror argument structure by
3441 * lfs_mirror_alloc().
3443 * Free memory allocated for @mirror.
3447 static void lfs_mirror_free(struct mirror_args *mirror)
3449 if (mirror->m_layout)
3450 llapi_layout_free(mirror->m_layout);
3455 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3456 * @mirror_list: Previously allocated mirror list.
3458 * Free memory allocated for @mirror_list.
3462 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3464 struct mirror_args *next_mirror = NULL;
3466 while (mirror_list) {
3467 next_mirror = mirror_list->m_next;
3468 lfs_mirror_free(mirror_list);
3469 mirror_list = next_mirror;
3474 LFS_SETQUOTA_DELETE = (CHAR_MAX + 1),
3482 LFS_COMP_NO_VERIFY_OPT,
3484 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3486 LFS_MIRROR_STATE_OPT,
3488 LFS_MIRROR_INDEX_OPT,
3489 LFS_LAYOUT_FOREIGN_OPT,
3498 LFS_STATS_INTERVAL_OPT,
3503 #ifndef LCME_USER_MIRROR_FLAGS
3504 /* The mirror flags can be set by users at creation time. */
3505 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3509 static int lfs_setstripe_internal(int argc, char **argv,
3510 enum setstripe_origin opc)
3512 struct lfs_setstripe_args lsa = { 0 };
3513 struct llapi_stripe_param *param = NULL;
3514 struct find_param migrate_mdt_param = {
3524 unsigned long long size_units = 1;
3525 bool migrate_mode = false;
3526 bool migrate_mdt_mode = false;
3527 bool setstripe_mode = false;
3528 bool migration_block = false;
3529 __u64 migration_flags = 0;
3530 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3531 int comp_del = 0, comp_set = 0;
3534 struct llapi_layout *layout = NULL;
3535 struct llapi_layout **lpp = &layout;
3536 bool mirror_mode = false;
3537 bool has_m_file = false;
3538 __u32 mirror_count = 0;
3539 enum mirror_flags mirror_flags = 0;
3540 struct mirror_args *mirror_list = NULL;
3541 struct mirror_args *new_mirror = NULL;
3542 struct mirror_args *last_mirror = NULL;
3543 __u16 mirror_id = 0;
3545 bool from_yaml = false;
3546 bool from_copy = false;
3547 char *template = NULL;
3548 bool foreign_mode = false;
3550 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3551 char *mode_opt = NULL;
3552 mode_t previous_umask = 0;
3553 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3554 unsigned long long bandwidth_bytes_sec = 0;
3555 unsigned long long bandwidth_unit = ONE_MB;
3556 long stats_interval_sec = 0;
3558 struct option long_opts[] = {
3559 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3560 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3561 /* --block is only valid in migrate mode */
3562 { .val = 'b', .name = "block", .has_arg = no_argument },
3563 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3564 { .val = LFS_COMP_ADD_OPT,
3565 .name = "comp-add", .has_arg = no_argument },
3566 { .val = LFS_COMP_ADD_OPT,
3567 .name = "component-add", .has_arg = no_argument },
3568 { .val = LFS_COMP_DEL_OPT,
3569 .name = "comp-del", .has_arg = no_argument },
3570 { .val = LFS_COMP_DEL_OPT,
3571 .name = "component-del", .has_arg = no_argument },
3572 { .val = LFS_COMP_FLAGS_OPT,
3573 .name = "comp-flags", .has_arg = required_argument },
3574 { .val = LFS_COMP_FLAGS_OPT,
3575 .name = "component-flags",
3576 .has_arg = required_argument },
3577 { .val = LFS_COMP_SET_OPT,
3578 .name = "comp-set", .has_arg = no_argument },
3579 { .val = LFS_COMP_SET_OPT,
3580 .name = "component-set",
3581 .has_arg = no_argument},
3582 { .val = LFS_COMP_NO_VERIFY_OPT,
3583 .name = "no-verify", .has_arg = no_argument},
3584 { .val = LFS_LAYOUT_FLAGS_OPT,
3585 .name = "flags", .has_arg = required_argument},
3586 { .val = LFS_LAYOUT_FOREIGN_OPT,
3587 .name = "foreign", .has_arg = optional_argument},
3588 { .val = LFS_MIRROR_ID_OPT,
3589 .name = "mirror-id", .has_arg = required_argument},
3590 { .val = LFS_MODE_OPT,
3591 .name = "mode", .has_arg = required_argument},
3592 { .val = LFS_LAYOUT_COPY,
3593 .name = "copy", .has_arg = required_argument},
3594 { .val = LFS_STATS_OPT,
3595 .name = "stats", .has_arg = no_argument},
3596 { .val = LFS_STATS_INTERVAL_OPT,
3597 .name = "stats-interval",
3598 .has_arg = required_argument},
3599 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3600 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3601 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3602 { .val = 'C', .name = "overstripe-count",
3603 .has_arg = required_argument},
3604 { .val = 'd', .name = "delete", .has_arg = no_argument},
3605 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3606 /* used with "lfs migrate -m" */
3607 { .val = 'd', .name = "directory", .has_arg = no_argument},
3608 /* --non-direct is only valid in migrate mode */
3609 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3610 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3611 { .val = 'E', .name = "component-end",
3612 .has_arg = required_argument},
3613 { .val = 'f', .name = "file", .has_arg = required_argument },
3614 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3615 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3616 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3617 { .val = 'h', .name = "help", .has_arg = no_argument },
3618 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3619 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3620 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3621 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3622 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3623 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3624 { .val = 'L', .name = "layout", .has_arg = required_argument },
3625 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3626 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3627 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3628 /* --non-block is only valid in migrate mode */
3629 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3630 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3631 { .val = 'o', .name = "ost", .has_arg = required_argument },
3632 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3633 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3634 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3636 { .val = 'p', .name = "pool", .has_arg = required_argument },
3637 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3638 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3639 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3640 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3641 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3642 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3643 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3644 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3645 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3646 /* --verbose is only valid in migrate mode */
3647 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3648 { .val = 'W', .name = "bandwidth", .has_arg = required_argument },
3649 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3650 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3651 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3652 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3653 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3656 setstripe_args_init(&lsa);
3658 migrate_mode = (opc == SO_MIGRATE);
3659 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3660 setstripe_mode = (opc == SO_SETSTRIPE);
3661 if (opc == SO_MIRROR_DELETE) {
3663 mirror_flags = MF_DESTROY;
3666 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3668 while ((c = getopt_long(argc, argv,
3669 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:W:y:z:",
3670 long_opts, NULL)) >= 0) {
3676 case LFS_COMP_ADD_OPT:
3679 case LFS_COMP_DEL_OPT:
3682 case LFS_COMP_FLAGS_OPT:
3683 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3684 &lsa.lsa_comp_neg_flags);
3687 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3689 "%s: inverted flags are not supported\n",
3694 case LFS_COMP_SET_OPT:
3697 case LFS_COMP_NO_VERIFY_OPT:
3698 mirror_flags |= MF_NO_VERIFY;
3700 case LFS_MIRROR_ID_OPT: {
3701 unsigned long int id;
3704 id = strtoul(optarg, &end, 0);
3705 if (errno != 0 || *end != '\0' || id == 0 ||
3708 "%s %s: invalid mirror ID '%s'\n",
3709 progname, argv[0], optarg);
3713 mirror_id = (__u16)id;
3716 case LFS_LAYOUT_FLAGS_OPT: {
3719 /* check for numeric flags (foreign and mirror cases) */
3720 if (setstripe_mode && !mirror_mode && !last_mirror) {
3722 flags = strtoul(optarg, &end, 16);
3723 if (errno != 0 || *end != '\0' ||
3724 flags >= UINT32_MAX) {
3726 "%s %s: invalid hex flags '%s'\n",
3727 progname, argv[0], optarg);
3730 if (!foreign_mode) {
3732 "%s %s: hex flags must be specified with --foreign option\n",
3739 if (!mirror_mode || !last_mirror) {
3741 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3746 result = comp_str2flags(optarg, &last_mirror->m_flags,
3753 "%s: inverted flags are not supported\n",
3758 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3760 "%s: unsupported mirror flags: %s\n",
3767 case LFS_LAYOUT_FOREIGN_OPT:
3769 /* check pure numeric */
3770 type = strtoul(optarg, &end, 0);
3773 type = check_foreign_type_name(optarg);
3774 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3776 "%s %s: unrecognized foreign type '%s'\n",
3781 } else if (type >= UINT32_MAX) {
3783 "%s %s: invalid foreign type '%s'\n",
3784 progname, argv[0], optarg);
3788 foreign_mode = true;
3793 mode = strtoul(mode_opt, &end, 8);
3796 "%s %s: bad mode '%s'\n",
3797 progname, argv[0], mode_opt);
3800 previous_umask = umask(0);
3803 case LFS_LAYOUT_COPY:
3808 stats_interval_sec = 5;
3810 case LFS_STATS_INTERVAL_OPT:
3811 stats_interval_sec = strtol(optarg, &end, 0);
3812 if (stats_interval_sec == 0 && errno) {
3814 "%s %s: invalid stats interval %s\n",
3815 progname, argv[0], optarg);
3820 if (!migrate_mode) {
3822 "%s %s: -b|--block valid only for migrate command\n",
3826 migration_block = true;
3829 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3831 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3835 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3839 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3840 if (errno != 0 || *end != '\0'|| optarg == end ||
3841 lsa.lsa_stripe_count < -1 ||
3842 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3844 "%s %s: invalid stripe count '%s'\n",
3845 progname, argv[0], optarg);
3849 if (lsa.lsa_stripe_count == -1)
3850 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3854 migrate_mdt_param.fp_max_depth = 1;
3856 /* delete the default striping pattern */
3858 if (opc == SO_MIRROR_SPLIT) {
3861 "%s %s: -d cannot used with -f\n",
3865 mirror_flags |= MF_DESTROY;
3870 if (!migrate_mode) {
3872 "%s %s: -D|--non-direct is valid only for migrate command\n",
3876 migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3879 if (lsa.lsa_comp_end != 0) {
3880 result = comp_args_to_layout(lpp, &lsa, true);
3882 fprintf(stderr, "%s: invalid layout\n",
3887 setstripe_args_init_inherit(&lsa);
3890 if (arg_is_eof(optarg)) {
3891 lsa.lsa_comp_end = LUSTRE_EOF;
3893 result = llapi_parse_size(optarg,
3896 /* assume units of KB if too small */
3897 if (lsa.lsa_comp_end < 4096)
3898 lsa.lsa_comp_end *= 1024;
3900 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3902 "%s %s: invalid component end '%s'\n",
3903 progname, argv[0], optarg);
3909 if (!migrate_mode) {
3911 "--mdt-hash is valid only for migrate command\n");
3915 lsa.lsa_pattern = check_hashtype(optarg);
3916 if (lsa.lsa_pattern == 0) {
3918 "%s %s: bad stripe hash type '%s'\n",
3919 progname, argv[0], optarg);
3925 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3926 if (errno != 0 || *end != '\0' || optarg == end ||
3927 lsa.lsa_stripe_off < -1 ||
3928 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3930 "%s %s: invalid stripe offset '%s'\n",
3931 progname, argv[0], optarg);
3934 if (lsa.lsa_stripe_off == -1)
3935 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3938 comp_id = strtoul(optarg, &end, 0);
3939 if (*end != '\0' || comp_id == 0 ||
3940 comp_id > LCME_ID_MAX) {
3942 "%s %s: invalid component ID '%s'\n",
3943 progname, argv[0], optarg);
3948 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3950 "error: %s: invalid option: %s\n",
3951 progname, argv[optopt + 1]);
3954 if (opc == SO_MIRROR_EXTEND) {
3957 "error: %s: '-N' must exist in front of '%s'\n",
3958 progname, argv[optopt + 1]);
3961 last_mirror->m_file = optarg;
3962 last_mirror->m_count = 1;
3966 mirror_list = lfs_mirror_alloc();
3967 mirror_list->m_file = optarg;
3972 if (strcmp(argv[optind - 1], "mdt") == 0) {
3973 /* Can be only the first component */
3977 "error: 'mdt' layout can be only the first one\n");
3980 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3983 "error: 'mdt' layout size is too big\n");
3986 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3987 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3988 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3991 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3997 if (!migrate_mode) {
3999 "%s %s: -m|--mdt-index is valid only for migrate command\n",
4003 migrate_mdt_mode = true;
4004 lsa.lsa_nr_tgts = parse_targets(tgts,
4005 sizeof(tgts) / sizeof(__u32),
4006 lsa.lsa_nr_tgts, optarg, NULL);
4007 if (lsa.lsa_nr_tgts < 0) {
4009 "%s: invalid MDT target(s) '%s'\n",
4014 lsa.lsa_tgts = tgts;
4015 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4016 lsa.lsa_stripe_off = tgts[0];
4019 if (!migrate_mode) {
4021 "%s %s: -n|--non-block valid only for migrate command\n",
4025 migration_flags |= LLAPI_MIGRATION_NONBLOCK;
4028 if (opc == SO_SETSTRIPE) {
4029 opc = SO_MIRROR_CREATE;
4035 mirror_count = strtoul(optarg, &end, 0);
4036 if (errno != 0 || *end != '\0' ||
4037 mirror_count == 0 ||
4038 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
4040 "error: %s: bad mirror count: %s\n",
4047 new_mirror = lfs_mirror_alloc();
4048 new_mirror->m_count = mirror_count;
4051 mirror_list = new_mirror;
4054 /* wrap up last mirror */
4055 if (!setstripe_args_specified(&lsa))
4056 last_mirror->m_inherit = true;
4057 if (lsa.lsa_comp_end == 0)
4058 lsa.lsa_comp_end = LUSTRE_EOF;
4060 result = comp_args_to_layout(lpp, &lsa, true);
4062 lfs_mirror_free(new_mirror);
4066 setstripe_args_init_inherit(&lsa);
4068 last_mirror->m_next = new_mirror;
4071 last_mirror = new_mirror;
4072 lpp = &last_mirror->m_layout;
4075 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4076 if (strcmp(argv[optind - 1], "--ost-list") == 0)
4078 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
4080 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
4082 "%s %s: -o|--ost incompatible with DoM layout\n",
4087 * -o allows overstriping, and must note it because
4088 * parse_targets is shared with MDT striping, which
4089 * does not allow duplicates
4091 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
4092 lsa.lsa_nr_tgts = parse_targets(tgts,
4093 sizeof(tgts) / sizeof(__u32),
4094 lsa.lsa_nr_tgts, optarg,
4096 if (lsa.lsa_nr_tgts < 0) {
4098 "%s %s: invalid OST target(s) '%s'\n",
4099 progname, argv[0], optarg);
4103 lsa.lsa_tgts = tgts;
4104 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4105 lsa.lsa_stripe_off = tgts[0];
4111 if (optarg[0] == '\0' || lov_pool_is_inherited(optarg))
4112 lsa.lsa_pool_name = NULL;
4114 lsa.lsa_pool_name = optarg;
4117 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
4119 /* assume units of KB if too small to be valid */
4120 if (lsa.lsa_stripe_size < 4096)
4121 lsa.lsa_stripe_size *= 1024;
4123 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
4125 "%s %s: invalid stripe size '%s'\n",
4126 progname, argv[0], optarg);
4131 if (!migrate_mode) {
4133 "%s %s: -v|--verbose valid only for migrate command\n",
4137 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
4138 migration_flags = LLAPI_MIGRATION_VERBOSE;
4144 if (!migrate_mode && !mirror_mode) {
4146 "--bandwidth is valid only for migrate and mirror mode\n");
4149 if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
4150 &bandwidth_unit, 0) < 0) {
4152 "error: %s: bad value for bandwidth '%s'\n",
4162 result = llapi_parse_size(optarg,
4163 &lsa.lsa_extension_size,
4167 "%s %s: invalid extension size '%s'\n",
4168 progname, argv[0], optarg);
4172 lsa.lsa_extension_comp = true;
4175 fprintf(stderr, "%s: unrecognized option '%s'\n",
4176 progname, argv[optind - 1]);
4182 fname = argv[optind];
4184 if (optind == argc) {
4185 fprintf(stderr, "%s %s: FILE must be specified\n",
4190 /* lfs migrate $filename should keep the file's layout by default */
4191 if (migrate_mode && !layout && !from_yaml &&
4192 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4195 if (xattr && !foreign_mode) {
4197 * only print a warning as this is harmless and will be ignored
4200 "%s %s: xattr has been specified for non-foreign layout\n",
4202 } else if (foreign_mode && !xattr) {
4204 "%s %s: xattr must be provided in foreign mode\n",
4209 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4210 comp_set || comp_id || delete || from_copy ||
4211 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4214 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4219 if (mirror_mode && mirror_count == 0) {
4221 "error: %s: --mirror-count|-N option is required\n",
4228 if (!setstripe_args_specified(&lsa))
4229 last_mirror->m_inherit = true;
4230 if (lsa.lsa_comp_end == 0)
4231 lsa.lsa_comp_end = LUSTRE_EOF;
4234 if (lsa.lsa_comp_end != 0) {
4235 result = comp_args_to_layout(lpp, &lsa, true);
4237 fprintf(stderr, "error: %s: invalid layout\n",
4244 if (mirror_flags & MF_NO_VERIFY) {
4245 if (opc != SO_MIRROR_EXTEND) {
4247 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4251 } else if (!has_m_file) {
4253 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4260 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4262 "%s %s: --component-set doesn't have component-id set\n",
4267 if ((delete + comp_set + comp_del + comp_add) > 1) {
4269 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4274 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4275 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4277 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4282 if ((comp_set || comp_del) &&
4283 (setstripe_args_specified(&lsa) || layout != NULL)) {
4285 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4290 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4292 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4297 if (comp_add || comp_del) {
4300 result = lstat(fname, &st);
4301 if (result == 0 && S_ISDIR(st.st_mode)) {
4303 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4310 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4319 "%s %s: option -E must be specified with --component-add\n",
4325 if (from_yaml && from_copy) {
4327 "%s: can't specify --yaml and --copy together\n",
4332 if ((from_yaml || from_copy) &&
4333 (setstripe_args_specified(&lsa) || layout != NULL)) {
4335 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4340 if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4342 "%s %s: options --non-block and --block are mutually exclusive\n",
4347 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4348 opc != SO_MIRROR_DELETE && comp_id != 0) {
4350 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4355 if (migrate_mdt_mode) {
4356 struct lmv_user_md *lmu;
4358 /* initialize migrate mdt parameters */
4359 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4360 LMV_USER_MAGIC_SPECIFIC));
4363 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4364 progname, argv[0], strerror(ENOMEM));
4368 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4369 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4370 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4372 "%s %s: migrate should specify MDT index\n",
4377 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4378 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4379 lmu->lum_hash_type = lsa.lsa_pattern;
4381 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4382 if (lsa.lsa_pool_name) {
4383 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4384 sizeof(lmu->lum_pool_name) - 1);
4385 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4387 if (lsa.lsa_nr_tgts > 1) {
4390 if (lsa.lsa_stripe_count > 0 &&
4391 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4392 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4394 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4395 progname, lsa.lsa_stripe_count,
4401 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4402 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4403 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4404 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4406 lmu->lum_magic = LMV_USER_MAGIC;
4409 migrate_mdt_param.fp_lmv_md = lmu;
4410 migrate_mdt_param.fp_migrate = 1;
4411 } else if (!layout) {
4412 if (lsa_args_stripe_count_check(&lsa))
4415 /* initialize stripe parameters */
4416 param = calloc(1, offsetof(typeof(*param),
4417 lsp_osts[lsa.lsa_nr_tgts]));
4420 "%s %s: cannot allocate memory for parameters: %s\n",
4421 progname, argv[0], strerror(ENOMEM));
4426 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4427 param->lsp_stripe_size = lsa.lsa_stripe_size;
4428 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4429 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4430 param->lsp_stripe_count = -1;
4432 param->lsp_stripe_count = lsa.lsa_stripe_count;
4434 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4435 param->lsp_stripe_offset = -1;
4437 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4438 param->lsp_stripe_pattern =
4439 llapi_pattern_to_lov(lsa.lsa_pattern);
4440 if (param->lsp_stripe_pattern == EINVAL) {
4441 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4446 param->lsp_pool = lsa.lsa_pool_name;
4447 param->lsp_is_specific = false;
4449 if (lsa.lsa_nr_tgts > 0) {
4450 param->lsp_is_specific = true;
4451 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4452 memcpy(param->lsp_osts, tgts,
4453 sizeof(*tgts) * lsa.lsa_nr_tgts);
4458 /* generate a layout from a YAML template */
4459 result = lfs_comp_create_from_yaml(template, &layout,
4463 "error: %s: can't create composite layout from template file %s\n",
4469 if (layout != NULL || mirror_list != NULL) {
4471 result = mirror_adjust_first_extents(mirror_list);
4473 result = layout_adjust_first_extent(fname, layout,
4475 if (result == -ENODATA)
4477 else if (result != 0) {
4478 fprintf(stderr, "error: %s: invalid layout\n",
4484 for (fname = argv[optind]; (optind < argc) && (fname != NULL);
4485 fname = argv[++optind]) {
4487 layout = llapi_layout_get_by_path(template ?: fname, 0);
4490 "%s: can't create composite layout from file %s: %s\n",
4491 progname, template ?: fname,
4498 if (migrate_mdt_mode) {
4499 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4500 } else if (migrate_mode) {
4503 * Strip the source layout of specific
4504 * OST object/index values.
4506 result = llapi_layout_ost_index_set(layout, 0,
4507 LLAPI_LAYOUT_DEFAULT);
4510 "%s: set default ost index failed: %s\n",
4511 progname, strerror(errno));
4517 result = lfs_migrate(fname, migration_flags, param,
4518 layout, bandwidth_bytes_sec,
4519 stats_interval_sec);
4520 } else if (comp_set != 0) {
4521 result = lfs_component_set(fname, comp_id,
4524 lsa.lsa_comp_neg_flags);
4525 } else if (comp_del != 0) {
4526 result = lfs_component_del(fname, comp_id,
4528 lsa.lsa_comp_neg_flags);
4529 } else if (comp_add != 0) {
4530 result = lfs_component_add(fname, layout);
4531 } else if (opc == SO_MIRROR_CREATE) {
4532 result = mirror_create(fname, mirror_list);
4533 } else if (opc == SO_MIRROR_EXTEND) {
4534 result = mirror_extend(fname, mirror_list,
4536 bandwidth_bytes_sec,
4537 stats_interval_sec);
4538 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4539 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4541 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4545 if (lsa.lsa_pool_name)
4546 mirror_flags |= MF_COMP_POOL;
4547 else if (mirror_id != 0)
4548 comp_id = mirror_id;
4550 mirror_flags |= MF_COMP_ID;
4551 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4553 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4557 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4559 has_m_file ? mirror_list->m_file :
4561 } else if (layout) {
4562 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4568 } else if (foreign_mode) {
4569 result = llapi_file_create_foreign(fname, mode, type,
4576 result = llapi_file_open_param(fname,
4585 /* Save the first error encountered. */
4593 umask(previous_umask);
4596 free(migrate_mdt_param.fp_lmv_md);
4597 llapi_layout_free(layout);
4598 lfs_mirror_list_free(mirror_list);
4603 llapi_layout_free(layout);
4604 lfs_mirror_list_free(mirror_list);
4608 static int lfs_poollist(int argc, char **argv)
4613 return llapi_poollist(argv[1]);
4616 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4617 static int set_time(struct find_param *param, time_t *time, time_t *set,
4622 char *endptr = "AD";
4627 else if (str[0] == '-')
4633 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4634 long long val = strtoll(timebuf, &endptr, 0);
4639 unit *= 52; /* 52 weeks + 1 day below */
4643 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4644 param->fp_time_margin *= (1 + unit / 52);
4645 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4647 case '\0': /* days are default unit if none used */
4660 /* don't need to multiply by 1 for seconds */
4663 "%s find: bad time string '%s': %s\n",
4664 progname, timebuf, strerror(EINVAL));
4668 if (param->fp_time_margin == 0 ||
4669 (*endptr && unit < param->fp_time_margin))
4670 param->fp_time_margin = unit;
4677 fprintf(stderr, "%s find: bad time '%s': too large\n",
4687 static int str2quotaid(__u32 *id, const char *arg)
4689 unsigned long int projid_tmp = 0;
4690 char *endptr = NULL;
4692 projid_tmp = strtoul(arg, &endptr, 10);
4693 if (*endptr != '\0')
4695 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4696 if (projid_tmp >= UINT32_MAX)
4703 static int name2uid(unsigned int *id, const char *name)
4705 struct passwd *passwd;
4707 passwd = getpwnam(name);
4710 *id = passwd->pw_uid;
4715 static int name2gid(unsigned int *id, const char *name)
4717 struct group *group;
4719 group = getgrnam(name);
4722 *id = group->gr_gid;
4727 static inline int name2projid(unsigned int *id, const char *name)
4732 static int uid2name(char **name, unsigned int id)
4734 struct passwd *passwd;
4736 passwd = getpwuid(id);
4739 *name = passwd->pw_name;
4744 static inline int gid2name(char **name, unsigned int id)
4746 struct group *group;
4748 group = getgrgid(id);
4751 *name = group->gr_name;
4756 static int name2layout(__u32 *layout, char *name)
4758 char *ptr, *layout_name;
4761 for (ptr = name; ; ptr = NULL) {
4762 layout_name = strtok(ptr, ",");
4765 if (strcmp(layout_name, "released") == 0)
4766 *layout |= LOV_PATTERN_F_RELEASED;
4767 else if (strcmp(layout_name, "raid0") == 0)
4768 *layout |= LOV_PATTERN_RAID0;
4769 else if (strcmp(layout_name, "mdt") == 0)
4770 *layout |= LOV_PATTERN_MDT;
4771 else if (strcmp(layout_name, "overstriping") == 0)
4772 *layout |= LOV_PATTERN_OVERSTRIPING;
4779 static int name2attrs(char *name, __u64 *attrs, __u64 *neg_attrs)
4781 char *ptr, *attr_name = name;
4782 struct attrs_name *ap;
4783 int islongopt = 0; /* 1 true; 0 not known yet; -1 false. */
4788 if (strchr(name, ','))
4791 for (ptr = name; ; ptr = NULL) {
4792 if (islongopt != -1)
4793 attr_name = strtok(ptr, ",");
4795 attr_name = attr_name + 1;
4796 if (!attr_name || *attr_name == '\0')
4799 for (ap = (struct attrs_name *)attrs_array;
4802 if (islongopt != -1 &&
4803 strcmp(attr_name, ap->an_name) == 0) {
4804 *attrs |= ap->an_attr;
4807 } else if (islongopt != -1 && attr_name[0] == '^' &&
4808 strcmp(attr_name + 1, ap->an_name) == 0) {
4809 *neg_attrs |= ap->an_attr;
4812 } else if (islongopt != 1 &&
4813 *attr_name == ap->an_shortname) {
4814 *attrs |= ap->an_attr;
4817 } else if (islongopt != 1 && *attr_name == '^' &&
4818 attr_name[1] == ap->an_shortname) {
4819 *neg_attrs |= ap->an_attr;
4826 if (ap->an_attr == 0) {
4827 /* provided attr is unknown */
4828 fprintf(stderr, "error: bad attribute name '%s'\n",
4836 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4839 int user, group, other;
4844 mode_t previous_flags;
4846 user = group = other = 0;
4866 user = group = other = 1;
4877 who = user || group || other;
4880 usermask = umask(0022);
4885 if (*input == '-' || *input == '+' || *input == '=')
4888 /* operation is required */
4891 /* get the flags in *outmode */
4894 previous_flags = (*outmode & 0700);
4895 perm |= user ? previous_flags : 0;
4896 perm |= group ? (previous_flags >> 3) : 0;
4897 perm |= other ? (previous_flags >> 6) : 0;
4901 previous_flags = (*outmode & 0070);
4902 perm |= user ? (previous_flags << 3) : 0;
4903 perm |= group ? previous_flags : 0;
4904 perm |= other ? (previous_flags >> 3) : 0;
4908 previous_flags = (*outmode & 0007);
4909 perm |= user ? (previous_flags << 6) : 0;
4910 perm |= group ? (previous_flags << 3) : 0;
4911 perm |= other ? previous_flags : 0;
4918 /* this part is optional,
4919 * if empty perm = 0 and *outmode is not modified
4926 perm |= user ? 0400 : 0;
4927 perm |= group ? 0040 : 0;
4928 perm |= other ? 0004 : 0;
4929 /* set read permission for uog except for umask's
4932 perm |= who ? 0 : (0444 & ~usermask);
4935 perm |= user ? 0200 : 0;
4936 perm |= group ? 0020 : 0;
4937 perm |= other ? 0002 : 0;
4938 /* set write permission for uog except for umask'
4941 perm |= who ? 0 : (0222 & ~usermask);
4944 perm |= user ? 0100 : 0;
4945 perm |= group ? 0010 : 0;
4946 perm |= other ? 0001 : 0;
4947 /* set execute permission for uog except for umask'
4950 perm |= who ? 0 : (0111 & ~usermask);
4954 * Adds execute permission to 'u', 'g' and/or 'g' if
4955 * specified and either 'u', 'g' or 'o' already has
4956 * execute permissions.
4958 if ((*outmode & 0111) != 0) {
4959 perm |= user ? 0100 : 0;
4960 perm |= group ? 0010 : 0;
4961 perm |= other ? 0001 : 0;
4962 perm |= !who ? 0111 : 0;
4966 /* s is ignored if o is given, but it's not an error */
4967 if (other && !group && !user)
4969 perm |= user ? S_ISUID : 0;
4970 perm |= group ? S_ISGID : 0;
4973 /* 't' should be used when 'a' is given
4976 perm |= (!who || all) ? S_ISVTX : 0;
4977 /* using ugo with t is not an error */
4988 /* uog flags should be only one character long */
4989 if (previous_flags && (*input != '\0' && *input != ','))
4994 /* remove the flags from outmode */
4998 /* add the flags to outmode */
5002 /* set the flags of outmode to perm */
5012 static int str2mode_t(const char *input, mode_t *outmode)
5019 if (*input >= '0' && *input <= '7') {
5020 /* parse octal representation */
5025 /* look for invalid digits in octal representation */
5026 while (isdigit(*iter))
5031 *outmode = strtoul(input, &end, 8);
5033 if (errno != 0 || *outmode > 07777) {
5038 } else if (*input == '8' || *input == '9') {
5039 /* error: invalid octal number */
5042 /* parse coma seperated list of symbolic representation */
5051 rc = parse_symbolic(input, outmode, &end);
5056 } while (*end == ',');
5064 static int lfs_find(int argc, char **argv)
5069 struct find_param param = {
5072 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
5074 struct option long_opts[] = {
5075 { .val = 'A', .name = "atime", .has_arg = required_argument },
5076 { .val = LFS_ATTRS_OPT,
5077 .name = "attrs", .has_arg = required_argument },
5078 { .val = 'b', .name = "blocks", .has_arg = required_argument },
5079 { .val = 'B', .name = "btime", .has_arg = required_argument },
5080 { .val = 'B', .name = "Btime", .has_arg = required_argument },
5081 { .val = LFS_COMP_COUNT_OPT,
5082 .name = "comp-count", .has_arg = required_argument },
5083 { .val = LFS_COMP_COUNT_OPT,
5084 .name = "component-count",
5085 .has_arg = required_argument },
5086 { .val = LFS_COMP_FLAGS_OPT,
5087 .name = "comp-flags", .has_arg = required_argument },
5088 { .val = LFS_COMP_FLAGS_OPT,
5089 .name = "component-flags",
5090 .has_arg = required_argument },
5091 { .val = LFS_COMP_START_OPT,
5092 .name = "comp-start", .has_arg = required_argument },
5093 { .val = LFS_COMP_START_OPT,
5094 .name = "component-start",
5095 .has_arg = required_argument },
5096 { .val = LFS_MIRROR_STATE_OPT,
5097 .name = "mirror-state", .has_arg = required_argument },
5098 { .val = LFS_NEWERXY_OPT,
5099 .name = "newer", .has_arg = required_argument},
5100 { .val = LFS_NEWERXY_OPT,
5101 .name = "neweraa", .has_arg = required_argument},
5102 { .val = LFS_NEWERXY_OPT,
5103 .name = "neweram", .has_arg = required_argument},
5104 { .val = LFS_NEWERXY_OPT,
5105 .name = "newerac", .has_arg = required_argument},
5106 { .val = LFS_NEWERXY_OPT,
5107 .name = "newerab", .has_arg = required_argument},
5108 { .val = LFS_NEWERXY_OPT,
5109 .name = "newerma", .has_arg = required_argument},
5110 { .val = LFS_NEWERXY_OPT,
5111 .name = "newermm", .has_arg = required_argument},
5112 { .val = LFS_NEWERXY_OPT,
5113 .name = "newermc", .has_arg = required_argument},
5114 { .val = LFS_NEWERXY_OPT,
5115 .name = "newermb", .has_arg = required_argument},
5116 { .val = LFS_NEWERXY_OPT,
5117 .name = "newerca", .has_arg = required_argument},
5118 { .val = LFS_NEWERXY_OPT,
5119 .name = "newercm", .has_arg = required_argument},
5120 { .val = LFS_NEWERXY_OPT,
5121 .name = "newercc", .has_arg = required_argument},
5122 { .val = LFS_NEWERXY_OPT,
5123 .name = "newercb", .has_arg = required_argument},
5124 { .val = LFS_NEWERXY_OPT,
5125 .name = "newerba", .has_arg = required_argument},
5126 { .val = LFS_NEWERXY_OPT,
5127 .name = "newerbm", .has_arg = required_argument},
5128 { .val = LFS_NEWERXY_OPT,
5129 .name = "newerbc", .has_arg = required_argument},
5130 { .val = LFS_NEWERXY_OPT,
5131 .name = "newerbb", .has_arg = required_argument},
5132 { .val = LFS_NEWERXY_OPT,
5133 .name = "newerBa", .has_arg = required_argument},
5134 { .val = LFS_NEWERXY_OPT,
5135 .name = "newerBm", .has_arg = required_argument},
5136 { .val = LFS_NEWERXY_OPT,
5137 .name = "newerBc", .has_arg = required_argument},
5138 { .val = LFS_NEWERXY_OPT,
5139 .name = "newerBB", .has_arg = required_argument},
5140 { .val = LFS_NEWERXY_OPT,
5141 .name = "newerat", .has_arg = required_argument},
5142 { .val = LFS_NEWERXY_OPT,
5143 .name = "newermt", .has_arg = required_argument},
5144 { .val = LFS_NEWERXY_OPT,
5145 .name = "newerct", .has_arg = required_argument},
5146 { .val = LFS_NEWERXY_OPT,
5147 .name = "newerbt", .has_arg = required_argument},
5148 { .val = LFS_NEWERXY_OPT,
5149 .name = "newerBt", .has_arg = required_argument},
5150 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
5151 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
5152 { .val = 'C', .name = "ctime", .has_arg = required_argument },
5153 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
5154 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
5155 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
5156 { .val = 'E', .name = "component-end",
5157 .has_arg = required_argument },
5158 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
5159 { .val = LFS_LAYOUT_FOREIGN_OPT,
5160 .name = "foreign", .has_arg = optional_argument},
5161 { .val = 'g', .name = "gid", .has_arg = required_argument },
5162 { .val = 'G', .name = "group", .has_arg = required_argument },
5163 { .val = 'h', .name = "help", .has_arg = no_argument },
5164 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5165 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
5166 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
5167 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
5168 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5169 { .val = 'L', .name = "layout", .has_arg = required_argument },
5170 { .val = LFS_LINKS_OPT,
5171 .name = "links", .has_arg = required_argument },
5172 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5173 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5174 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
5175 { .val = 'M', .name = "mtime", .has_arg = required_argument },
5176 { .val = 'n', .name = "name", .has_arg = required_argument },
5177 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
5178 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
5179 { .val = 'O', .name = "obd", .has_arg = required_argument },
5180 { .val = 'O', .name = "ost", .has_arg = required_argument },
5181 { .val = LFS_FIND_PERM,
5182 .name = "perm", .has_arg = required_argument },
5183 /* no short option for pool yet, can be 'p' after 2.18 */
5184 { .val = LFS_POOL_OPT,
5185 .name = "pool", .has_arg = required_argument },
5186 { .val = '0', .name = "print0", .has_arg = no_argument },
5187 { .val = 'P', .name = "print", .has_arg = no_argument },
5188 { .val = LFS_PRINTF_OPT,
5189 .name = "printf", .has_arg = required_argument },
5190 { .val = LFS_PROJID_OPT,
5191 .name = "projid", .has_arg = required_argument },
5192 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
5193 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
5194 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
5195 { .val = 's', .name = "size", .has_arg = required_argument },
5196 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
5197 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
5198 { .val = 't', .name = "type", .has_arg = required_argument },
5199 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5200 { .val = 'u', .name = "uid", .has_arg = required_argument },
5201 { .val = 'U', .name = "user", .has_arg = required_argument },
5202 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
5203 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
5204 { .val = 'z', .name = "extension-size",
5205 .has_arg = required_argument },
5206 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
5208 int prev_optind = optind;
5220 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
5221 while ((c = getopt_long_only(argc, argv,
5222 "-0A:b:B:c:C:D:E:g:G:hH:i:lL:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
5223 long_opts, &optidx)) >= 0) {
5228 /* '!' is part of option */
5230 * when getopt_long_only() finds a string which is not
5231 * an option nor a known option argument it returns 1
5232 * in that case if we already have found pathstart and pathend
5233 * (i.e. we have the list of pathnames),
5234 * the only supported value is "!"
5236 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5237 if (!isoption && pathend != -1) {
5239 "err: %s: filename|dirname must either precede options or follow options\n",
5244 if (!isoption && pathstart == -1)
5245 pathstart = prev_optind;
5246 if (isoption && pathstart != -1 && pathend == -1)
5247 pathend = prev_optind;
5249 prev_optind = optind;
5257 * unknown; opt is "!" or path component,
5258 * checking done above.
5260 if (strcmp(optarg, "!") == 0)
5264 xtime = ¶m.fp_atime;
5265 xsign = ¶m.fp_asign;
5266 param.fp_exclude_atime = !!neg_opt;
5267 /* no break, this falls through to 'B' for btime */
5271 xtime = ¶m.fp_btime;
5272 xsign = ¶m.fp_bsign;
5273 param.fp_exclude_btime = !!neg_opt;
5275 /* no break, this falls through to 'C' for ctime */
5279 xtime = ¶m.fp_ctime;
5280 xsign = ¶m.fp_csign;
5281 param.fp_exclude_ctime = !!neg_opt;
5283 /* no break, this falls through to 'M' for mtime */
5287 xtime = ¶m.fp_mtime;
5288 xsign = ¶m.fp_msign;
5289 param.fp_exclude_mtime = !!neg_opt;
5291 rc = set_time(¶m, &t, xtime, optarg);
5292 if (rc == INT_MAX) {
5300 ret = name2attrs(optarg, ¶m.fp_attrs,
5301 ¶m.fp_neg_attrs);
5304 param.fp_exclude_attrs = !!neg_opt;
5307 if (optarg[0] == '+') {
5308 param.fp_blocks_sign = -1;
5310 } else if (optarg[0] == '-') {
5311 param.fp_blocks_sign = 1;
5315 param.fp_blocks_units = 512;
5316 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5317 ¶m.fp_blocks_units, 0);
5319 fprintf(stderr, "error: bad blocks '%s'\n",
5323 param.fp_check_blocks = 1;
5324 param.fp_exclude_blocks = !!neg_opt;
5326 case LFS_COMP_COUNT_OPT:
5327 if (optarg[0] == '+') {
5328 param.fp_comp_count_sign = -1;
5330 } else if (optarg[0] == '-') {
5331 param.fp_comp_count_sign = 1;
5336 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5337 if (errno != 0 || *endptr != '\0' ||
5338 param.fp_comp_count > UINT32_MAX) {
5340 "error: bad component count '%s'\n",
5344 param.fp_check_comp_count = 1;
5345 param.fp_exclude_comp_count = !!neg_opt;
5347 case LFS_COMP_FLAGS_OPT:
5348 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5349 ¶m.fp_comp_neg_flags);
5352 "error: bad component flags '%s'\n",
5356 param.fp_check_comp_flags = 1;
5358 __u32 flags = param.fp_comp_neg_flags;
5360 param.fp_comp_neg_flags = param.fp_comp_flags;
5361 param.fp_comp_flags = flags;
5364 case LFS_COMP_START_OPT:
5365 if (optarg[0] == '+') {
5366 param.fp_comp_start_sign = -1;
5368 } else if (optarg[0] == '-') {
5369 param.fp_comp_start_sign = 1;
5373 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5374 ¶m.fp_comp_start_units, 0);
5377 "error: bad component start '%s'\n",
5381 param.fp_check_comp_start = 1;
5382 param.fp_exclude_comp_start = !!neg_opt;
5384 case LFS_MIRROR_STATE_OPT:
5385 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5386 ¶m.fp_mirror_neg_state);
5389 "error: bad mirrored file state '%s'\n",
5393 param.fp_check_mirror_state = 1;
5395 __u16 state = param.fp_mirror_neg_state;
5397 param.fp_mirror_neg_state =
5398 param.fp_mirror_state;
5399 param.fp_mirror_state = state;
5403 if (optarg[0] == '+') {
5404 param.fp_stripe_count_sign = -1;
5406 } else if (optarg[0] == '-') {
5407 param.fp_stripe_count_sign = 1;
5412 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5413 if (errno != 0 || *endptr != '\0' ||
5414 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5416 "error: bad stripe_count '%s'\n",
5421 param.fp_check_stripe_count = 1;
5422 param.fp_exclude_stripe_count = !!neg_opt;
5426 param.fp_max_depth = strtol(optarg, 0, 0);
5427 if (errno != 0 || param.fp_max_depth < 0) {
5429 "error: bad maxdepth '%s'\n",
5436 if (optarg[0] == '+') {
5437 param.fp_comp_end_sign = -1;
5439 } else if (optarg[0] == '-') {
5440 param.fp_comp_end_sign = 1;
5444 if (arg_is_eof(optarg)) {
5445 param.fp_comp_end = LUSTRE_EOF;
5446 param.fp_comp_end_units = 1;
5449 rc = llapi_parse_size(optarg,
5451 ¶m.fp_comp_end_units, 0);
5452 /* assume units of KB if too small */
5453 if (param.fp_comp_end < 4096)
5454 param.fp_comp_end *= 1024;
5458 "error: bad component end '%s'\n",
5462 param.fp_check_comp_end = 1;
5463 param.fp_exclude_comp_end = !!neg_opt;
5465 case LFS_LAYOUT_FOREIGN_OPT: {
5466 /* all types by default */
5467 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5470 /* check pure numeric */
5471 type = strtoul(optarg, &endptr, 0);
5474 type = check_foreign_type_name(optarg);
5475 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5477 "%s %s: unknown foreign type '%s'\n",
5482 } else if (type >= UINT32_MAX) {
5484 "%s %s: invalid foreign type '%s'\n",
5485 progname, argv[0], optarg);
5489 param.fp_foreign_type = type;
5490 param.fp_check_foreign = 1;
5491 param.fp_exclude_foreign = !!neg_opt;
5494 case LFS_NEWERXY_OPT: {
5500 time_t ref = time(NULL);
5502 /* no need to check bad options, they won't get here */
5503 if (strlen(long_opts[optidx].name) == 7) {
5504 x = long_opts[optidx].name[5];
5505 y = long_opts[optidx].name[6];
5509 static const char *const fmts[] = {
5510 "%Y-%m-%d %H:%M:%S",
5513 "%H:%M:%S", /* sometime today */
5522 for (i = 0; fmts[i] != NULL; i++) {
5525 /* Init for times relative to today */
5526 if (strncmp(fmts[i], "%H", 2) == 0) {
5527 localtime_r(&ref, &tm);
5529 memset(&tm, 0, sizeof(tm));
5532 ptr = strptime(optarg, fmts[i], &tm);
5534 while (ptr && isspace(*ptr))
5536 if (ptr == optarg + strlen(optarg)) {
5544 "%s: invalid time '%s'\n",
5547 "supported formats are:\n ");
5548 for (i = 0; fmts[i] != NULL; i++)
5549 fprintf(stderr, "'%s', ",
5551 fprintf(stderr, "\n");
5557 } else if (y == 'b' || y == 'B') {
5560 rc = llapi_get_lum_file(optarg, NULL, &stx,
5562 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5563 if (!(stx.stx_mask & STATX_BTIME))
5568 "%s: get btime failed '%s': %s\n",
5574 ref = stx.stx_btime.tv_sec;
5576 struct stat statbuf;
5578 if (stat(optarg, &statbuf) < 0) {
5580 "%s: cannot stat file '%s': %s\n",
5589 ref = statbuf.st_atime;
5592 ref = statbuf.st_mtime;
5595 ref = statbuf.st_ctime;
5599 "%s: invalid Y argument: '%c'\n",
5608 xidx = NEWERXY_ATIME;
5611 xidx = NEWERXY_MTIME;
5614 xidx = NEWERXY_CTIME;
5618 xidx = NEWERXY_BTIME;
5622 "%s: invalid X argument: '%c'\n",
5629 newery = ¶m.fp_newery[xidx][negidx];
5635 *newery = *newery > ref ? ref : *newery;
5637 *newery = *newery > ref ? *newery : ref;
5639 param.fp_newerxy = 1;
5644 rc = name2gid(¶m.fp_gid, optarg);
5646 if (str2quotaid(¶m.fp_gid, optarg)) {
5648 "Group/GID: %s cannot be found.\n",
5654 param.fp_exclude_gid = !!neg_opt;
5655 param.fp_check_gid = 1;
5658 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5659 ¶m.fp_hash_exflags,
5660 ¶m.fp_hash_type);
5665 if (param.fp_hash_inflags || param.fp_hash_exflags)
5666 param.fp_check_hash_flag = 1;
5667 param.fp_exclude_hash_type = !!neg_opt;
5673 ret = name2layout(¶m.fp_layout, optarg);
5676 param.fp_exclude_layout = !!neg_opt;
5677 param.fp_check_layout = 1;
5680 if (optarg[0] == '+') {
5681 param.fp_nlink_sign = -1;
5683 } else if (optarg[0] == '-') {
5684 param.fp_nlink_sign = 1;
5688 param.fp_nlink = strtoul(optarg, &endptr, 0);
5689 if (errno != 0 || *endptr != '\0' || !param.fp_nlink) {
5690 fprintf(stderr, "error: bad link count '%s'\n",
5695 param.fp_exclude_nlink = !!neg_opt;
5699 rc = name2uid(¶m.fp_uid, optarg);
5701 if (str2quotaid(¶m.fp_uid, optarg)) {
5703 "User/UID: %s cannot be found.\n",
5709 param.fp_exclude_uid = !!neg_opt;
5710 param.fp_check_uid = 1;
5713 param.fp_pattern = (char *)optarg;
5714 param.fp_exclude_pattern = !!neg_opt;
5717 if (optarg[0] == '+') {
5718 param.fp_mirror_count_sign = -1;
5720 } else if (optarg[0] == '-') {
5721 param.fp_mirror_count_sign = 1;
5726 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5727 if (errno != 0 || *endptr != '\0' ||
5728 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5730 "error: bad mirror count '%s'\n",
5734 param.fp_check_mirror_count = 1;
5735 param.fp_exclude_mirror_count = !!neg_opt;
5740 char *buf, *token, *next, *p;
5744 buf = strdup(optarg);
5750 param.fp_exclude_obd = !!neg_opt;
5753 while (token && *token) {
5754 token = strchr(token, ',');
5761 param.fp_exclude_mdt = !!neg_opt;
5762 param.fp_num_alloc_mdts += len;
5763 tmp = realloc(param.fp_mdt_uuid,
5764 param.fp_num_alloc_mdts *
5765 sizeof(*param.fp_mdt_uuid));
5771 param.fp_mdt_uuid = tmp;
5773 param.fp_exclude_obd = !!neg_opt;
5774 param.fp_num_alloc_obds += len;
5775 tmp = realloc(param.fp_obd_uuid,
5776 param.fp_num_alloc_obds *
5777 sizeof(*param.fp_obd_uuid));
5783 param.fp_obd_uuid = tmp;
5785 for (token = buf; token && *token; token = next) {
5786 struct obd_uuid *puuid;
5790 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5793 ¶m.fp_obd_uuid[param.fp_num_obds++];
5795 p = strchr(token, ',');
5802 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5807 strncpy(puuid->uuid, token,
5808 sizeof(puuid->uuid));
5815 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5819 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5821 "Pool name %s is too long (max %d)\n",
5822 optarg, LOV_MAXPOOLNAME);
5827 * We do check for empty pool because empty pool
5828 * is used to find V1 LOV attributes
5830 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5831 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5832 param.fp_exclude_pool = !!neg_opt;
5833 param.fp_check_pool = 1;
5836 param.fp_zero_end = 1;
5838 case 'P': /* we always print, this option is a no-op */
5840 case LFS_PRINTF_OPT:
5841 param.fp_format_printf_str = strdup(optarg);
5843 case LFS_PROJID_OPT:
5844 rc = name2projid(¶m.fp_projid, optarg);
5846 if (str2quotaid(¶m.fp_projid, optarg)) {
5848 "Invalid project ID: %s\n",
5854 param.fp_exclude_projid = !!neg_opt;
5855 param.fp_check_projid = 1;
5858 if (optarg[0] == '+') {
5859 param.fp_size_sign = -1;
5861 } else if (optarg[0] == '-') {
5862 param.fp_size_sign = 1;
5866 param.fp_size_units = 512;
5867 ret = llapi_parse_size(optarg, ¶m.fp_size,
5868 ¶m.fp_size_units, 0);
5870 fprintf(stderr, "error: bad file size '%s'\n",
5874 param.fp_check_size = 1;
5875 param.fp_exclude_size = !!neg_opt;
5878 if (optarg[0] == '+') {
5879 param.fp_stripe_size_sign = -1;
5881 } else if (optarg[0] == '-') {
5882 param.fp_stripe_size_sign = 1;
5886 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5887 ¶m.fp_stripe_size_units, 0);
5888 /* assume units of KB if too small to be valid */
5889 if (param.fp_stripe_size < 4096)
5890 param.fp_stripe_size *= 1024;
5892 fprintf(stderr, "error: bad stripe_size '%s'\n",
5896 param.fp_check_stripe_size = 1;
5897 param.fp_exclude_stripe_size = !!neg_opt;
5900 param.fp_exclude_type = !!neg_opt;
5901 switch (optarg[0]) {
5903 param.fp_type = S_IFBLK;
5906 param.fp_type = S_IFCHR;
5909 param.fp_type = S_IFDIR;
5912 param.fp_type = S_IFREG;
5915 param.fp_type = S_IFLNK;
5918 param.fp_type = S_IFIFO;
5921 param.fp_type = S_IFSOCK;
5924 fprintf(stderr, "%s: bad type '%s'\n",
5931 param.fp_exclude_perm = !!neg_opt;
5932 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5933 if (*optarg == '/') {
5934 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5936 } else if (*optarg == '-') {
5937 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5941 if (str2mode_t(optarg, ¶m.fp_perm)) {
5942 fprintf(stderr, "error: invalid mode '%s'\n",
5949 if (optarg[0] == '+') {
5950 param.fp_mdt_count_sign = -1;
5952 } else if (optarg[0] == '-') {
5953 param.fp_mdt_count_sign = 1;
5958 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5959 if (errno != 0 || *endptr != '\0' ||
5960 param.fp_mdt_count >= UINT32_MAX) {
5961 fprintf(stderr, "error: bad mdt_count '%s'\n",
5966 param.fp_check_mdt_count = 1;
5967 param.fp_exclude_mdt_count = !!neg_opt;
5970 if (optarg[0] == '+') {
5971 param.fp_ext_size_sign = -1;
5973 } else if (optarg[0] == '-') {
5974 param.fp_ext_size_sign = 1;
5978 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5979 ¶m.fp_ext_size_units, 0);
5981 fprintf(stderr, "error: bad ext-size '%s'\n",
5985 param.fp_ext_size /= SEL_UNIT_SIZE;
5986 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5987 param.fp_check_ext_size = 1;
5988 param.fp_exclude_ext_size = !!neg_opt;
5991 fprintf(stderr, "%s: unrecognized option '%s'\n",
5992 progname, argv[optind - 1]);
5998 if (!param.fp_verbose)
5999 param.fp_verbose = VERBOSE_DEFAULT;
6001 if (pathstart == -1) {
6002 fprintf(stderr, "error: %s: no filename|pathname\n",
6006 } else if (pathend == -1) {
6012 rc = llapi_find(argv[pathstart], ¶m);
6017 fprintf(stderr, "%s: failed for '%s': %s\n",
6018 progname, argv[pathstart], strerror(-rc));
6020 } while (++pathstart < pathend);
6023 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
6024 free(param.fp_obd_uuid);
6026 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
6027 free(param.fp_mdt_uuid);
6029 if (param.fp_format_printf_str)
6030 free(param.fp_format_printf_str);
6035 static int lfs_getstripe_internal(int argc, char **argv,
6036 struct find_param *param)
6038 struct option long_opts[] = {
6039 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
6040 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
6041 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
6042 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
6043 { .val = LFS_COMP_COUNT_OPT,
6044 .name = "comp-count", .has_arg = no_argument },
6045 { .val = LFS_COMP_COUNT_OPT,
6046 .name = "component-count", .has_arg = no_argument },
6047 { .val = LFS_COMP_FLAGS_OPT,
6048 .name = "comp-flags", .has_arg = optional_argument },
6049 { .val = LFS_COMP_FLAGS_OPT,
6050 .name = "component-flags", .has_arg = optional_argument },
6051 { .val = LFS_COMP_START_OPT,
6052 .name = "comp-start", .has_arg = optional_argument },
6053 { .val = LFS_COMP_START_OPT,
6054 .name = "component-start", .has_arg = optional_argument },
6055 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
6056 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
6057 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
6058 { .val = 'd', .name = "directory", .has_arg = no_argument },
6059 { .val = 'D', .name = "default", .has_arg = no_argument },
6060 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
6061 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
6062 { .val = 'F', .name = "fid", .has_arg = no_argument },
6063 { .val = 'g', .name = "generation", .has_arg = no_argument },
6064 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
6065 { .val = 'h', .name = "help", .has_arg = no_argument },
6066 { .val = LFS_HEX_IDX_OPT,
6067 .name = "hex-idx", .has_arg = no_argument },
6068 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
6069 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
6070 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
6071 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
6072 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
6073 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6074 { .val = 'L', .name = "layout", .has_arg = no_argument },
6075 { .val = 'm', .name = "mdt", .has_arg = no_argument },
6076 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6077 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
6078 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
6079 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
6080 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
6081 { .val = LFS_MIRROR_INDEX_OPT,
6082 .name = "mirror-index", .has_arg = required_argument },
6083 { .val = LFS_MIRROR_ID_OPT,
6084 .name = "mirror-id", .has_arg = required_argument },
6085 { .val = LFS_NO_FOLLOW_OPT,
6086 .name = "no-follow", .has_arg = no_argument },
6087 { .val = 'O', .name = "obd", .has_arg = required_argument },
6088 { .val = 'O', .name = "ost", .has_arg = required_argument },
6089 { .val = 'p', .name = "pool", .has_arg = no_argument },
6090 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
6091 { .val = 'q', .name = "quiet", .has_arg = no_argument },
6092 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6093 { .val = 'R', .name = "raw", .has_arg = no_argument },
6094 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
6095 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
6096 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
6097 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
6098 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
6099 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
6100 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6101 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
6102 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }*/
6103 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6104 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
6105 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
6109 int pathstart = -1, pathend = -1;
6113 while ((c = getopt_long(argc, argv,
6114 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
6115 long_opts, NULL)) != -1) {
6119 /* '!' is part of option */
6120 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
6121 if (!isoption && pathend != -1) {
6123 "error: %s: filename|dirname must either precede options or follow options\n",
6127 if (!isoption && pathstart == -1)
6128 pathstart = optind - 1;
6129 if (isoption && pathstart != -1 && pathend == -1)
6130 pathend = optind - 2;
6134 /* unknown: opt is "!" */
6135 if (strcmp(optarg, "!") == 0)
6139 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6140 param->fp_verbose |= VERBOSE_COUNT;
6141 param->fp_max_depth = 0;
6144 case LFS_COMP_COUNT_OPT:
6145 param->fp_verbose |= VERBOSE_COMP_COUNT;
6146 param->fp_max_depth = 0;
6148 case LFS_COMP_FLAGS_OPT:
6150 rc = comp_str2flags(optarg,
6151 ¶m->fp_comp_flags,
6152 ¶m->fp_comp_neg_flags);
6155 "error: %s bad component flags '%s'.\n",
6159 param->fp_check_comp_flags = 1;
6161 param->fp_verbose |= VERBOSE_COMP_FLAGS;
6162 param->fp_max_depth = 0;
6165 case LFS_COMP_START_OPT:
6168 if (tmp[0] == '+') {
6169 param->fp_comp_start_sign = -1;
6171 } else if (tmp[0] == '-') {
6172 param->fp_comp_start_sign = 1;
6175 rc = llapi_parse_size(tmp,
6176 ¶m->fp_comp_start,
6177 ¶m->fp_comp_start_units, 0);
6180 "error: %s bad component start '%s'.\n",
6184 param->fp_check_comp_start = 1;
6186 param->fp_verbose |= VERBOSE_COMP_START;
6187 param->fp_max_depth = 0;
6190 case LFS_MIRROR_INDEX_OPT: {
6191 unsigned long int mirror_index;
6193 if (optarg[0] == '+') {
6194 param->fp_mirror_index_sign = -1;
6196 } else if (optarg[0] == '-') {
6197 param->fp_mirror_index_sign = 1;
6202 mirror_index = strtoul(optarg, &end, 0);
6203 if (errno != 0 || *end != '\0' ||
6204 mirror_index > UINT16_MAX || (mirror_index == 0 &&
6205 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
6207 "%s %s: invalid mirror index '%s'\n",
6208 progname, argv[0], optarg);
6212 param->fp_mirror_index = (__u16)mirror_index;
6214 if (param->fp_mirror_id != 0) {
6216 "%s %s: can't specify both mirror index and mirror ID\n",
6220 param->fp_check_mirror_index = 1;
6221 param->fp_exclude_mirror_index = !!neg_opt;
6224 case LFS_MIRROR_ID_OPT: {
6225 unsigned long int mirror_id;
6227 if (optarg[0] == '+') {
6228 param->fp_mirror_id_sign = -1;
6230 } else if (optarg[0] == '-') {
6231 param->fp_mirror_id_sign = 1;
6236 mirror_id = strtoul(optarg, &end, 0);
6237 if (errno != 0 || *end != '\0' ||
6238 mirror_id > UINT16_MAX || (mirror_id == 0 &&
6239 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
6241 "%s %s: invalid mirror ID '%s'\n",
6242 progname, argv[0], optarg);
6246 param->fp_mirror_id = (__u16)mirror_id;
6248 if (param->fp_mirror_index != 0) {
6250 "%s %s: can't specify both mirror index and mirror ID\n",
6254 param->fp_check_mirror_id = 1;
6255 param->fp_exclude_mirror_id = !!neg_opt;
6258 case LFS_NO_FOLLOW_OPT:
6259 param->fp_no_follow = true;
6261 case LFS_HEX_IDX_OPT:
6262 param->fp_hex_idx = true;
6265 param->fp_max_depth = 0;
6268 param->fp_get_default_lmv = 1;
6273 if (tmp[0] == '+') {
6274 param->fp_comp_end_sign = -1;
6276 } else if (tmp[0] == '-') {
6277 param->fp_comp_end_sign = 1;
6281 if (arg_is_eof(tmp)) {
6282 param->fp_comp_end = LUSTRE_EOF;
6283 param->fp_comp_end_units = 1;
6286 rc = llapi_parse_size(tmp,
6287 ¶m->fp_comp_end,
6288 ¶m->fp_comp_end_units, 0);
6289 /* assume units of KB if too small */
6290 if (param->fp_comp_end < 4096)
6291 param->fp_comp_end *= 1024;
6295 "error: %s bad component end '%s'.\n",
6299 param->fp_check_comp_end = 1;
6301 param->fp_verbose |= VERBOSE_COMP_END;
6302 param->fp_max_depth = 0;
6306 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6307 param->fp_verbose |= VERBOSE_DFID;
6308 param->fp_max_depth = 0;
6312 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6313 param->fp_verbose |= VERBOSE_GENERATION;
6314 param->fp_max_depth = 0;
6318 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6319 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6320 param->fp_max_depth = 0;
6325 param->fp_comp_id = strtoul(optarg, &end, 0);
6326 if (*end != '\0' || param->fp_comp_id == 0 ||
6327 param->fp_comp_id > LCME_ID_MAX) {
6329 "error: %s bad component id '%s'\n",
6333 param->fp_check_comp_id = 1;
6335 param->fp_max_depth = 0;
6336 param->fp_verbose |= VERBOSE_COMP_ID;
6340 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6341 param->fp_verbose |= VERBOSE_PATTERN;
6342 param->fp_max_depth = 0;
6345 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6348 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6351 if (!(param->fp_verbose & VERBOSE_DETAIL))
6352 param->fp_max_depth = 0;
6353 param->fp_verbose |= VERBOSE_MDTINDEX;
6356 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6357 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6358 param->fp_max_depth = 0;
6362 if (param->fp_obd_uuid) {
6364 "error: %s: only one obduuid allowed",
6368 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6371 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6372 param->fp_verbose |= VERBOSE_POOL;
6373 param->fp_max_depth = 0;
6380 param->fp_recursive = 1;
6386 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6387 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6388 param->fp_max_depth = 0;
6392 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6398 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6399 param->fp_verbose |= VERBOSE_EXT_SIZE;
6400 param->fp_max_depth = 0;
6404 fprintf(stderr, "%s: unrecognized option '%s'\n",
6405 progname, argv[optind - 1]);
6411 if (pathstart == -1) {
6412 fprintf(stderr, "error: %s: no filename|pathname\n",
6415 } else if (pathend == -1) {
6423 if (param->fp_recursive)
6424 param->fp_max_depth = -1;
6425 else if (param->fp_verbose & VERBOSE_DETAIL)
6426 param->fp_max_depth = 1;
6428 if (!param->fp_verbose)
6429 param->fp_verbose = VERBOSE_DEFAULT;
6430 if (param->fp_quiet)
6431 param->fp_verbose = VERBOSE_OBJID;
6436 rc2 = llapi_getstripe(argv[pathstart], param);
6438 fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6439 progname, argv[0], argv[pathstart],
6444 } while (++pathstart < pathend);
6449 static int lfs_tgts(int argc, char **argv)
6451 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6452 struct find_param param;
6453 int index = 0, rc = 0;
6458 if (argc == 2 && !realpath(argv[1], path)) {
6460 fprintf(stderr, "error: invalid path '%s': %s\n",
6461 argv[1], strerror(-rc));
6465 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6466 /* Check if we have a mount point */
6467 if (mntdir[0] == '\0')
6470 memset(¶m, 0, sizeof(param));
6471 if (!strcmp(argv[0], "mdts"))
6472 param.fp_get_lmv = 1;
6474 rc = llapi_ostlist(mntdir, ¶m);
6476 fprintf(stderr, "error: %s: failed on %s\n",
6479 if (path[0] != '\0')
6481 memset(mntdir, 0, PATH_MAX);
6487 static int lfs_getstripe(int argc, char **argv)
6489 struct find_param param = { 0 };
6491 param.fp_max_depth = 1;
6492 return lfs_getstripe_internal(argc, argv, ¶m);
6496 static int lfs_getdirstripe(int argc, char **argv)
6498 struct find_param param = { 0 };
6499 struct option long_opts[] = {
6500 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6501 { .val = 'D', .name = "default", .has_arg = no_argument },
6502 { .val = 'h', .name = "help", .has_arg = no_argument },
6503 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6504 { .val = LFS_HEX_IDX_OPT,
6505 .name = "hex-idx", .has_arg = no_argument },
6506 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6507 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6508 { .val = 'O', .name = "obd", .has_arg = required_argument },
6509 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6510 { .val = 'R', .name = "raw", .has_arg = no_argument },
6511 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6512 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6513 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6514 { .val = LFS_INHERIT_RR_OPT,
6515 .name = "max-inherit-rr", .has_arg = no_argument },
6516 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6520 param.fp_get_lmv = 1;
6522 while ((c = getopt_long(argc, argv,
6523 "cDhHimO:rRtTvXy", long_opts, NULL)) != -1) {
6527 param.fp_verbose |= VERBOSE_COUNT;
6530 param.fp_get_default_lmv = 1;
6532 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6535 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6539 param.fp_verbose |= VERBOSE_HASH_TYPE;
6541 case LFS_HEX_IDX_OPT:
6542 param.fp_hex_idx = 1;
6547 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6550 if (param.fp_obd_uuid) {
6552 "%s: only one obduuid allowed",
6556 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6559 param.fp_recursive = 1;
6565 param.fp_verbose |= VERBOSE_DEFAULT;
6566 param.fp_verbose |= VERBOSE_DETAIL;
6569 param.fp_verbose |= VERBOSE_INHERIT;
6571 case LFS_INHERIT_RR_OPT:
6572 param.fp_verbose |= VERBOSE_INHERIT_RR;
6578 fprintf(stderr, "%s: unrecognized option '%s'\n",
6579 progname, argv[optind - 1]);
6589 if (param.fp_recursive)
6590 param.fp_max_depth = -1;
6592 if (!param.fp_verbose)
6593 param.fp_verbose = VERBOSE_DEFAULT;
6598 rc2 = llapi_getstripe(argv[optind], ¶m);
6600 fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6601 progname, argv[0], argv[optind],
6606 } while (++optind < argc);
6612 MNTDF_INODES = 0x0001,
6613 MNTDF_COOKED = 0x0002,
6614 MNTDF_LAZY = 0x0004,
6615 MNTDF_VERBOSE = 0x0008,
6616 MNTDF_SHOW = 0x0010,
6617 MNTDF_DECIMAL = 0x0020,
6620 #define COOK(value, base) \
6623 while (value > base) { \
6631 #define CDF "%11llu"
6632 #define HDF "%8.1f%c"
6636 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6638 double avail, used, ratio = 0;
6641 avail = st->os_ffree;
6642 used = st->os_files - st->os_ffree;
6644 avail = st->os_bavail;
6645 used = st->os_blocks - st->os_bfree;
6647 if (avail + used > 0)
6648 ratio = used / (used + avail) * 100;
6650 /* Round up to match df(1) usage percentage */
6651 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6655 * This is to identify various problem states for "lfs df" if .osn_err = true,
6656 * so only show flags reflecting those states by default. Informational states
6657 * are only shown with "-v" and use lower-case names to distinguish them.
6658 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6660 static struct obd_statfs_state_names {
6661 enum obd_statfs_state osn_state;
6662 const char osn_name;
6665 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6666 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6667 { .osn_state = OS_STATFS_NOCREATE, .osn_name = 'N', .osn_err = true },
6668 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6669 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6670 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6671 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6672 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6673 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6676 static int showdf(char *mntdir, struct obd_statfs *stat,
6677 char *uuid, enum mntdf_flags flags,
6678 char *type, int index, int rc)
6680 long long avail, used, total;
6682 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6683 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6684 char tbuf[3 * sizeof(__u64)];
6685 char ubuf[3 * sizeof(__u64)];
6686 char abuf[3 * sizeof(__u64)];
6687 char rbuf[3 * sizeof(__u64)];
6694 if (flags & MNTDF_INODES) {
6695 avail = stat->os_ffree;
6696 used = stat->os_files - stat->os_ffree;
6697 total = stat->os_files;
6699 int shift = flags & MNTDF_COOKED ? 0 : 10;
6701 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6702 used = ((stat->os_blocks - stat->os_bfree) *
6703 stat->os_bsize) >> shift;
6704 total = (stat->os_blocks * stat->os_bsize) >> shift;
6707 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6709 if (flags & MNTDF_COOKED) {
6710 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6714 cook_val = (double)total;
6715 i = COOK(cook_val, base);
6717 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6720 snprintf(tbuf, sizeof(tbuf), CDF, total);
6722 cook_val = (double)used;
6723 i = COOK(cook_val, base);
6725 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6728 snprintf(ubuf, sizeof(ubuf), CDF, used);
6730 cook_val = (double)avail;
6731 i = COOK(cook_val, base);
6733 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6736 snprintf(abuf, sizeof(abuf), CDF, avail);
6738 snprintf(tbuf, sizeof(tbuf), CDF, total);
6739 snprintf(ubuf, sizeof(tbuf), CDF, used);
6740 snprintf(abuf, sizeof(tbuf), CDF, avail);
6743 sprintf(rbuf, RDF, ratio);
6744 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6745 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6747 printf("[%s:%d]", type, index);
6749 if (stat->os_state) {
6753 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6754 if (oss_names[i].osn_state & stat->os_state &&
6755 (oss_names[i].osn_err ||
6756 flags & MNTDF_VERBOSE))
6757 printf("%c", oss_names[i].osn_name);
6764 printf(UUF": inactive device\n", uuid);
6767 printf(UUF": %s\n", uuid, strerror(-rc));
6774 struct ll_stat_type {
6779 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6781 struct ll_statfs_data {
6783 struct obd_statfs sd_st;
6786 struct ll_statfs_buf {
6788 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6791 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6792 int ops, struct ll_statfs_buf *lsb)
6794 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6795 struct obd_uuid uuid_buf;
6796 char *poolname = NULL;
6797 struct ll_stat_type types[] = {
6798 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6799 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6800 { .st_name = NULL } };
6801 struct ll_stat_type *tp;
6802 __u64 ost_files = 0;
6803 __u64 ost_ffree = 0;
6811 poolname = strchr(pool, '.');
6813 if (strncmp(fsname, pool, strlen(fsname))) {
6814 fprintf(stderr, "filesystem name incorrect\n");
6822 fd = open(mntdir, O_RDONLY);
6825 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6830 if (flags & MNTDF_SHOW) {
6831 if (flags & MNTDF_INODES)
6832 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6833 "UUID", "Inodes", "IUsed", "IFree",
6834 "IUse%", "Mounted on");
6836 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6838 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6839 "Used", "Available", "Use%", "Mounted on");
6842 for (tp = types; tp->st_name != NULL; tp++) {
6843 bool have_ost = false;
6845 if (!(tp->st_op & ops))
6848 for (index = 0; index < LOV_ALL_STRIPES &&
6849 (!lsb || lsb->sb_count < LL_STATFS_MAX); index++) {
6850 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6851 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6852 type = flags & MNTDF_LAZY ?
6853 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6854 rc2 = llapi_obd_fstatfs(fd, type, index,
6855 &stat_buf, &uuid_buf);
6860 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6861 if (!(flags & MNTDF_VERBOSE))
6863 } else if (rc2 < 0 && rc == 0) {
6868 * If we have OSTs then don't report MDT block counts.
6869 * For MDT-only filesystems the expectation is that all
6870 * layouts have a DoM component. For filesystems with
6871 * OSTs, files are not necessarily going to store data
6872 * on MDTs, and MDT space is limited to a fraction of
6873 * OST space, so don't include it in the summary.
6875 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6882 if (poolname && tp->st_op == LL_STATFS_LOV &&
6883 llapi_search_ost(fsname, poolname,
6884 obd_uuid2str(&uuid_buf)) != 1)
6888 * the llapi_obd_fstatfs() call may have returned with
6889 * an error, but if it filled in uuid_buf we will at
6890 * lease use that to print out a message for that OBD.
6891 * If we didn't get anything in the uuid_buf, then fill
6892 * it in so that we can print an error message.
6894 if (uuid_buf.uuid[0] == '\0')
6895 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6896 "%s%04x", tp->st_name, index);
6898 lsb->sb_buf[lsb->sb_count].sd_index = index;
6899 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6902 if (flags & MNTDF_SHOW)
6903 showdf(mntdir, &stat_buf,
6904 obd_uuid2str(&uuid_buf), flags,
6905 tp->st_name, index, rc2);
6910 if (tp->st_op == LL_STATFS_LMV) {
6911 sum.os_ffree += stat_buf.os_ffree;
6912 sum.os_files += stat_buf.os_files;
6913 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6914 ost_files += stat_buf.os_files;
6915 ost_ffree += stat_buf.os_ffree;
6917 sum.os_blocks += stat_buf.os_blocks *
6919 sum.os_bfree += stat_buf.os_bfree *
6921 sum.os_bavail += stat_buf.os_bavail *
6929 * If we have _some_ OSTs, but don't have as many free objects on the
6930 * OST as inodes on the MDTs, reduce the reported number of inodes
6931 * to compensate, so that the "inodes in use" number is correct.
6932 * This should be kept in sync with ll_statfs_internal().
6934 if (ost_files && ost_ffree < sum.os_ffree) {
6935 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6936 sum.os_ffree = ost_ffree;
6938 if (flags & MNTDF_SHOW) {
6940 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6948 LAYOUT_INHERIT_UNSET = -2,
6952 static int lfs_setdirstripe(int argc, char **argv)
6955 struct lfs_setstripe_args lsa = { 0 };
6956 struct llapi_stripe_param *param = NULL;
6957 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6960 char *mode_opt = NULL;
6961 bool default_stripe = false;
6962 bool delete = false;
6963 bool foreign_mode = false;
6964 bool mdt_count_set = false;
6965 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6966 mode_t previous_mode = 0;
6968 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6969 int max_inherit = LAYOUT_INHERIT_UNSET;
6970 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6971 struct option long_opts[] = {
6972 { .val = 'c', .name = "count", .has_arg = required_argument },
6973 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6974 { .val = 'd', .name = "delete", .has_arg = no_argument },
6975 { .val = 'D', .name = "default", .has_arg = no_argument },
6976 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6977 { .val = LFS_LAYOUT_FLAGS_OPT,
6978 .name = "flags", .has_arg = required_argument },
6979 { .val = LFS_LAYOUT_FOREIGN_OPT,
6980 .name = "foreign", .has_arg = optional_argument},
6981 { .val = 'h', .name = "help", .has_arg = no_argument },
6982 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6983 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6984 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6985 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6987 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6988 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6989 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6991 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6992 { .val = 'i', .name = "index", .has_arg = required_argument },
6994 { .val = 'o', .name = "mode", .has_arg = required_argument },
6995 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6996 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6998 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6999 { .val = 'x', .name = "xattr", .has_arg = required_argument },
7000 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
7001 { .val = LFS_INHERIT_RR_OPT,
7002 .name = "max-inherit-rr", .has_arg = required_argument},
7003 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
7004 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
7008 setstripe_args_init(&lsa);
7010 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
7011 long_opts, NULL)) >= 0) {
7019 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
7020 if (errno != 0 || *end != '\0' ||
7021 lsa.lsa_stripe_count < -1 ||
7022 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
7024 "%s: invalid stripe count '%s'\n",
7028 mdt_count_set = true;
7032 default_stripe = true;
7035 default_stripe = true;
7037 case LFS_LAYOUT_FOREIGN_OPT:
7039 /* check pure numeric */
7040 type = strtoul(optarg, &end, 0);
7043 type = check_foreign_type_name(optarg);
7044 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
7046 "%s %s: unknown foreign type '%s'\n",
7051 } else if (type >= UINT32_MAX) {
7053 "%s %s: invalid foreign type '%s'\n",
7054 progname, argv[0], optarg);
7058 foreign_mode = true;
7060 case LFS_LAYOUT_FLAGS_OPT:
7062 flags = strtoul(optarg, &end, 16);
7063 if (errno != 0 || *end != '\0' ||
7064 flags >= UINT32_MAX) {
7066 "%s %s: invalid hex flags '%s'\n",
7067 progname, argv[0], optarg);
7070 if (!foreign_mode) {
7072 "%s %s: hex flags must be specified with --foreign option\n",
7077 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7080 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
7084 lsa.lsa_pattern = check_hashtype(optarg);
7085 if (lsa.lsa_pattern == 0) {
7087 "%s %s: bad directory hash type '%s'\n",
7088 progname, argv[0], optarg);
7093 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
7096 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7097 if (strcmp(argv[optind - 1], "--index") == 0)
7099 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
7102 lsa.lsa_nr_tgts = parse_targets(mdts,
7103 sizeof(mdts) / sizeof(__u32),
7104 lsa.lsa_nr_tgts, optarg, NULL);
7105 if (lsa.lsa_nr_tgts < 0) {
7107 "%s %s: invalid MDT target(s) '%s'\n",
7108 progname, argv[0], optarg);
7112 lsa.lsa_tgts = mdts;
7113 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7114 lsa.lsa_stripe_off = mdts[0];
7124 max_inherit = strtol(optarg, &end, 10);
7125 if (errno != 0 || *end != '\0' || max_inherit < -2) {
7127 "%s %s: invalid max-inherit '%s'\n",
7128 progname, argv[0], optarg);
7131 if (max_inherit == 0) {
7132 max_inherit = LMV_INHERIT_NONE;
7133 } else if (max_inherit == -1) {
7134 max_inherit = LMV_INHERIT_UNLIMITED;
7135 } else if (max_inherit > LMV_INHERIT_MAX) {
7137 "%s %s: max-inherit %d exceeds maximum %u\n",
7138 progname, argv[0], max_inherit,
7143 case LFS_INHERIT_RR_OPT:
7144 if (!default_stripe) {
7146 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
7151 max_inherit_rr = strtol(optarg, &end, 10);
7152 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
7154 "%s %s: invalid max-inherit-rr '%s'\n",
7155 progname, argv[0], optarg);
7158 if (max_inherit_rr == 0) {
7159 max_inherit_rr = LMV_INHERIT_RR_NONE;
7160 } else if (max_inherit_rr == -1) {
7161 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
7162 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
7164 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
7165 progname, argv[0], max_inherit_rr,
7166 LMV_INHERIT_RR_MAX);
7171 fprintf(stderr, "%s: unrecognized option '%s'\n",
7172 progname, argv[optind - 1]);
7179 if (optind == argc) {
7180 fprintf(stderr, "%s %s: DIR must be specified\n",
7185 if (xattr && !foreign_mode) {
7187 * only print a warning as this is armless and will be
7191 "%s %s: xattr has been specified for non-foreign layout\n",
7193 } else if (foreign_mode && !xattr) {
7195 "%s %s: xattr must be provided in foreign mode\n",
7200 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
7201 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
7203 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
7208 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
7209 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
7210 /* if no parameters set, create directory on least-used MDTs */
7211 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
7212 lsa.lsa_stripe_count = 1;
7216 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
7217 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
7219 "%s %s: cannot specify -d with -c or -i options\n",
7225 mode = strtoul(mode_opt, &end, 8);
7228 "%s %s: bad MODE '%s'\n",
7229 progname, argv[0], mode_opt);
7232 previous_mode = umask(0);
7235 /* check max-inherit and warn user in some cases */
7236 if (default_stripe &&
7237 (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
7238 if (max_inherit == LMV_INHERIT_UNLIMITED)
7240 "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
7241 progname, argv[0], lsa.lsa_stripe_count);
7242 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
7243 max_inherit != LMV_INHERIT_NONE)
7245 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
7246 progname, argv[0], max_inherit,
7247 lsa.lsa_stripe_count);
7250 if (default_stripe && lsa.lsa_nr_tgts > 1 && !mdt_count_set) {
7252 "%s %s: trying to create unrecommended default striped directory layout,\n"
7253 " '-D -i x,y,z' will stripe every new directory across all MDTs,\n"
7254 " add -c with the number of MDTs to do this anyway\n",
7259 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
7260 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
7261 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
7263 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
7264 progname, argv[0], lsa.lsa_stripe_off);
7268 /* foreign LMV/dir case */
7270 if (argc > optind + 1) {
7272 "%s %s: cannot specify multiple foreign dirs\n",
7277 dname = argv[optind];
7278 result = llapi_dir_create_foreign(dname, mode, type, flags,
7282 "%s mkdir: can't create foreign dir '%s': %s\n",
7283 progname, dname, strerror(-result));
7288 * initialize stripe parameters, in case param is converted to specific,
7289 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
7291 param = calloc(1, offsetof(typeof(*param),
7292 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
7293 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
7296 "%s %s: cannot allocate memory for parameters: %s\n",
7297 progname, argv[0], strerror(ENOMEM));
7301 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
7302 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
7303 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
7304 lsa.lsa_stripe_count == 0))
7305 lsa.lsa_stripe_count = 1;
7306 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
7307 param->lsp_stripe_count = lsa.lsa_stripe_count;
7308 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7309 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
7311 param->lsp_stripe_offset = lsa.lsa_stripe_off;
7312 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
7313 param->lsp_stripe_pattern = lsa.lsa_pattern;
7315 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
7316 param->lsp_pool = lsa.lsa_pool_name;
7317 param->lsp_is_specific = false;
7319 if (max_inherit == LAYOUT_INHERIT_UNSET) {
7320 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1 ||
7321 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT)
7322 max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
7324 max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
7326 param->lsp_max_inherit = max_inherit;
7327 if (default_stripe) {
7329 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
7330 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
7331 param->lsp_max_inherit_rr = max_inherit_rr;
7333 if (strcmp(argv[0], "mkdir") == 0)
7334 param->lsp_is_create = true;
7335 if (lsa.lsa_nr_tgts > 1) {
7336 if (lsa.lsa_stripe_count > 0 &&
7337 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7338 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7340 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7341 argv[0], lsa.lsa_stripe_count,
7347 param->lsp_is_specific = true;
7348 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7349 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7352 dname = argv[optind];
7354 if (default_stripe) {
7355 result = llapi_dir_set_default_lmv(dname, param);
7358 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7359 progname, dname, strerror(-result));
7363 result = llapi_dir_create(dname, mode, param);
7366 "%s setdirstripe: cannot create dir '%s': %s\n",
7367 progname, dname, strerror(-result));
7368 } while ((dname = argv[++optind]));
7371 umask(previous_mode);
7377 static int lfs_rmentry(int argc, char **argv)
7384 fprintf(stderr, "error: %s: missing dirname\n",
7390 dname = argv[index];
7394 rc2 = llapi_direntry_remove(dname);
7397 "%s %s: remove dir entry '%s' failed: %s\n",
7398 progname, argv[0], dname, strerror(-rc2));
7402 dname = argv[++index];
7407 static int lfs_unlink_foreign(int argc, char **argv)
7414 fprintf(stderr, "error: %s: missing pathname\n",
7421 while (name != NULL) {
7422 result = llapi_unlink_foreign(name);
7425 "error: %s: unlink foreign entry '%s' failed\n",
7429 name = argv[++index];
7434 static int lfs_mv(int argc, char **argv)
7436 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7437 struct find_param param = {
7444 struct option long_opts[] = {
7445 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7446 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7447 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7450 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7452 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7455 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7459 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7460 if (errno != 0 || *end != '\0' ||
7461 lmu.lum_stripe_offset >= UINT32_MAX) {
7462 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7468 param.fp_verbose = VERBOSE_DETAIL;
7471 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7472 progname, argv[optind - 1]);
7477 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7478 fprintf(stderr, "%s mv: MDT index must be specified\n",
7483 if (optind >= argc) {
7484 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7488 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7490 /* initialize migrate mdt parameters */
7491 param.fp_lmv_md = &lmu;
7492 param.fp_migrate = 1;
7493 rc = llapi_migrate_mdt(argv[optind], ¶m);
7495 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7496 progname, argv[optind], lmu.lum_stripe_offset,
7501 static int lfs_osts(int argc, char **argv)
7503 return lfs_tgts(argc, argv);
7506 static int lfs_mdts(int argc, char **argv)
7508 return lfs_tgts(argc, argv);
7511 static int lfs_df(int argc, char **argv)
7513 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7514 enum mntdf_flags flags = MNTDF_SHOW;
7515 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7516 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7517 char fsname[PATH_MAX] = "", *pool_name = NULL;
7518 struct option long_opts[] = {
7519 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7520 { .val = 'H', .name = "si", .has_arg = no_argument },
7521 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7522 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7523 { .val = 'p', .name = "pool", .has_arg = required_argument },
7524 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7527 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7530 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7533 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7536 flags |= MNTDF_INODES;
7539 flags |= MNTDF_LAZY;
7545 flags |= MNTDF_VERBOSE;
7548 fprintf(stderr, "%s: unrecognized option '%s'\n",
7549 progname, argv[optind - 1]);
7554 /* Handle case where path is not specified */
7555 if (optind == argc) {
7556 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7557 /* Check if we have a mount point */
7558 if (mntdir[0] == '\0')
7561 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7562 if (rc || path[0] != '\0')
7565 fsname[0] = '\0'; /* avoid matching in next loop */
7566 mntdir[0] = '\0'; /* avoid matching in next loop */
7567 path[0] = '\0'; /* clean for next loop */
7572 /* Loop through all the remaining arguments. These are Lustre FS
7575 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7578 fsname[0] = '\0'; /* start clean */
7579 mntdir[0] = '\0'; /* start clean */
7580 path[0] = '\0'; /* start clean */
7582 /* path does not exists at all */
7583 if (!realpath(argv[arg_idx], path)) {
7585 fprintf(stderr, "error: invalid path '%s': %s\n",
7586 argv[arg_idx], strerror(-rc));
7587 /* save first seen error */
7594 /* path exists but may not be a Lustre filesystem */
7595 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7596 /* Check if we have a mount point */
7597 if (mntdir[0] == '\0')
7600 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7601 if (rc || path[0] != '\0') {
7604 /* save first seen error */
7612 llapi_printf(LLAPI_MSG_ERROR,
7613 "%s:%s Not a Lustre filesystem\n",
7614 argv[0], argv[arg_idx]);
7615 /* save first seen error */
7624 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7625 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7629 if (opt_fsname == opt_instance) { /* both true or both false */
7630 rc = llapi_getname(mntdir, buf, buflen);
7631 } else if (opt_fsname) {
7633 * llapi_search_mounts() fills @buf with fsname, but that is not
7634 * called if explicit paths are specified on the command-line
7637 rc = llapi_get_fsname(mntdir, buf, buflen);
7638 } else /* if (opt_instance) */ {
7639 rc = llapi_get_instance(mntdir, buf, buflen);
7643 fprintf(stderr, "cannot get instance for '%s': %s\n",
7644 mntdir, strerror(-rc));
7649 printf("%s %s\n", buf, mntdir);
7651 printf("%s\n", buf);
7656 static int lfs_getname(int argc, char **argv)
7658 struct option long_opts[] = {
7659 { .val = 'h', .name = "help", .has_arg = no_argument },
7660 { .val = 'i', .name = "instance", .has_arg = no_argument },
7661 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7663 bool opt_instance = false, opt_fsname = false;
7664 char fsname[PATH_MAX] = "";
7667 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7670 opt_instance = true;
7676 fprintf(stderr, "%s: unrecognized option '%s'\n",
7677 progname, argv[optind - 1]);
7684 if (optind == argc) { /* no paths specified, get all paths. */
7685 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7688 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7689 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7690 opt_instance, opt_fsname, true);
7693 path[0] = fsname[0] = mntdir[0] = '\0';
7695 } else { /* paths specified, only attempt to search these. */
7698 /* if only one path is given, print only requested info */
7699 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7701 for (; optind < argc; optind++) {
7702 rc2 = print_instance(argv[optind], fsname,
7703 sizeof(fsname), opt_instance,
7704 opt_fsname, opt_mntdir);
7714 static int lfs_check(int argc, char **argv)
7716 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7724 if (argc < 2 || argc > 3) {
7725 fprintf(stderr, "%s check: server type must be specified\n",
7730 obd_types[0] = obd_type1;
7731 obd_types[1] = obd_type2;
7732 obd_types[2] = obd_type3;
7734 if (strcmp(argv[1], "osts") == 0) {
7735 strcpy(obd_types[0], "osc");
7736 } else if (strcmp(argv[1], "mdts") == 0 ||
7737 strcmp(argv[1], "mds") == 0) {
7738 strcpy(obd_types[0], "mdc");
7739 } else if (strcmp(argv[1], "mgts") == 0) {
7740 strcpy(obd_types[0], "mgc");
7741 } else if (strcmp(argv[1], "all") == 0 ||
7742 strcmp(argv[1], "servers") == 0) {
7744 strcpy(obd_types[0], "osc");
7745 strcpy(obd_types[1], "mdc");
7746 strcpy(obd_types[2], "mgc");
7748 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7753 if (argc >= 3 && !realpath(argv[2], path)) {
7755 fprintf(stderr, "error: invalid path '%s': %s\n",
7756 argv[2], strerror(-rc));
7760 rc = llapi_search_mounts(path, 0, mntdir, NULL);
7761 if (rc < 0 || mntdir[0] == '\0') {
7763 "%s %s: cannot find mounted Lustre filesystem: %s\n",
7765 (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7769 rc = llapi_target_check(num_types, obd_types, path);
7771 fprintf(stderr, "%s %s: cannot check target '%s': %s\n",
7772 progname, argv[0], argv[1], strerror(-rc));
7777 #ifdef HAVE_SYS_QUOTA_H
7778 #define ADD_OVERFLOW(a, b) \
7779 ((((a) + (b)) < (a)) ? \
7780 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7782 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7783 * returns the value or ULONG_MAX on integer overflow or incorrect format
7785 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7786 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7787 * 3. empty integer value is interpreted as 0
7789 static unsigned long str2sec(const char *timestr)
7791 const char spec[] = "smhdw";
7792 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7793 unsigned long val = 0;
7796 if (strpbrk(timestr, spec) == NULL) {
7798 * no specifiers inside the time string,
7799 * should treat it as an integer value
7801 val = strtoul(timestr, &tail, 10);
7802 return *tail ? ULONG_MAX : val;
7805 /* format string is XXwXXdXXhXXmXXs */
7811 v = strtoul(timestr, &tail, 10);
7812 if (v == ULONG_MAX || *tail == '\0')
7814 * value too large (ULONG_MAX or more)
7815 * or missing specifier
7819 ptr = strchr(spec, *tail);
7821 /* unknown specifier */
7826 /* check if product will overflow the type */
7827 if (!(v < ULONG_MAX / mult[ind]))
7830 ADD_OVERFLOW(val, mult[ind] * v);
7831 if (val == ULONG_MAX)
7843 #define ARG2ULL(nr, str, def_units) \
7845 unsigned long long limit, units = def_units; \
7848 rc = llapi_parse_size(str, &limit, &units, 1); \
7850 fprintf(stderr, "%s: invalid limit '%s'\n", \
7857 static inline int has_times_option(int argc, char **argv)
7861 for (i = 1; i < argc; i++)
7862 if (!strcmp(argv[i], "-t"))
7868 static inline int lfs_verify_poolarg(char *pool)
7870 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7872 "Pool name '%.*s' is longer than %d\n",
7873 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7879 /* special grace time, only notify the user when its quota is over soft limit
7880 * but doesn't block new writes until the hard limit is reached.
7882 #define NOTIFY_GRACE "notify"
7883 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7886 static inline __u64 lustre_stoqb(size_t space)
7888 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7891 #define lustre_stoqb toqb
7894 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7897 char *mnt, *obd_type = (char *)qctl->obd_type;
7898 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7899 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7900 struct option long_opts[] = {
7901 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7902 { .val = 'g', .name = "group", .has_arg = no_argument },
7903 { .val = 'h', .name = "help", .has_arg = no_argument },
7904 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7905 { .val = 'p', .name = "projid", .has_arg = no_argument },
7906 { .val = 't', .name = "times", .has_arg = no_argument },
7907 { .val = 'u', .name = "user", .has_arg = no_argument },
7908 { .val = LFS_POOL_OPT,
7909 .name = "pool", .has_arg = required_argument },
7913 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7914 qctl->qc_type = ALLQUOTA;
7916 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7917 long_opts, NULL)) != -1) {
7928 if (qctl->qc_type != ALLQUOTA) {
7930 "%s: -u/g/p cannot be used more than once\n",
7934 qctl->qc_type = qtype;
7937 if (strncmp(optarg, NOTIFY_GRACE,
7938 strlen(NOTIFY_GRACE)) == 0) {
7939 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7941 dqi->dqi_bgrace = str2sec(optarg);
7942 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7944 "%s: bad block-grace: %s\n",
7949 dqb->dqb_valid |= QIF_BTIME;
7952 if (strncmp(optarg, NOTIFY_GRACE,
7953 strlen(NOTIFY_GRACE)) == 0) {
7954 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7956 dqi->dqi_igrace = str2sec(optarg);
7957 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7959 "%s: bad inode-grace: %s\n",
7964 dqb->dqb_valid |= QIF_ITIME;
7966 case 't': /* Yes, of course! */
7969 if (lfs_verify_poolarg(optarg))
7971 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7972 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7974 /* getopt prints error message for us when opterr != 0 */
7976 fprintf(stderr, "%s: unrecognized option '%s'\n",
7977 progname, argv[optind - 1]);
7984 if (qctl->qc_type == ALLQUOTA) {
7985 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7990 if (optind != argc - 1) {
7991 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7992 progname, argv[optind + 1]);
7997 rc = llapi_quotactl(mnt, qctl);
8000 fprintf(stderr, "%s %s ", obd_type,
8001 obd_uuid2str(&qctl->obd_uuid));
8002 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8009 static int lfs_reset_quota(char *mnt, struct if_quotactl *qctl)
8011 struct if_quotactl tmp_qctl;
8012 int index, md_count, dt_count;
8013 int wait_phase = 0, wait_index = 0, wait_count = 0;
8016 /* reset the quota ID, the existing quota setting will be returned */
8017 rc = llapi_quotactl(mnt, qctl);
8022 if ((qctl->qc_dqblk.dqb_valid & QIF_LIMITS) != QIF_LIMITS) {
8024 "the existing quota settings are not returned!\n");
8028 rc = llapi_get_obd_count(mnt, &md_count, 1);
8030 fprintf(stderr, "can not get mdt count: %s\n", strerror(-rc));
8034 rc = llapi_get_obd_count(mnt, &dt_count, 0);
8036 fprintf(stderr, "can not get ost count: %s\n", strerror(-rc));
8040 memset(&tmp_qctl, 0, sizeof(tmp_qctl));
8041 tmp_qctl.qc_type = qctl->qc_type;
8042 tmp_qctl.qc_id = qctl->qc_id;
8043 tmp_qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
8046 if (wait_phase == 0) {
8047 for (index = wait_index; index < md_count; index++) {
8048 tmp_qctl.qc_idx = index;
8049 tmp_qctl.qc_valid = QC_MDTIDX;
8050 rc = llapi_quotactl(mnt, &tmp_qctl);
8051 if (rc == -ENODEV || rc == -ENODATA)
8054 fprintf(stderr, "quotactl mdt%d failed: %s\n",
8055 index, strerror(-rc));
8058 /* check whether the md quota grant is reset */
8059 if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
8060 tmp_qctl.qc_dqblk.dqb_ihardlimit != 0)
8064 if (index < md_count) {
8070 for (index = wait_index; index < dt_count; index++) {
8071 tmp_qctl.qc_idx = index;
8072 tmp_qctl.qc_valid = QC_OSTIDX;
8073 rc = llapi_quotactl(mnt, &tmp_qctl);
8074 if (rc == -ENODEV || rc == -ENODATA)
8077 fprintf(stderr, "quotactl mdt%d failed: %s\n",
8078 index, strerror(-rc));
8081 /* check whether the dt quota grant is reset */
8082 if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
8083 tmp_qctl.qc_dqblk.dqb_bhardlimit != 0)
8087 if (index < dt_count) {
8094 if (wait_phase == 0) {
8102 if (rc || wait_count > 30) {
8103 fprintf(stderr, "fail to reset the quota ID %d on OBDs\n",
8110 fprintf(stdout, "wait %d seconds for OBDs to reset the quota ID %u\n",
8111 wait_count, qctl->qc_id);
8116 /* restore the quota setting */
8117 if (qctl->qc_dqblk.dqb_isoftlimit == 0 &&
8118 qctl->qc_dqblk.dqb_ihardlimit == 0 &&
8119 qctl->qc_dqblk.dqb_bsoftlimit == 0 &&
8120 qctl->qc_dqblk.dqb_bhardlimit == 0)
8123 memcpy(&tmp_qctl, qctl, sizeof(tmp_qctl));
8124 tmp_qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
8125 rc2 = llapi_quotactl(mnt, &tmp_qctl);
8130 "fail to restore the quota setting: %s, please restore it manually by\n lfs setquota %s %d",
8132 qctl->qc_type == USRQUOTA ? "-u" :
8133 (qctl->qc_type == GRPQUOTA ? "-g" : "-p"),
8136 if (qctl->qc_dqblk.dqb_isoftlimit != 0)
8137 fprintf(stderr, " -i %llu",
8138 (unsigned long long)qctl->qc_dqblk.dqb_isoftlimit);
8139 if (qctl->qc_dqblk.dqb_ihardlimit != 0)
8140 fprintf(stderr, " -I %llu",
8141 (unsigned long long)qctl->qc_dqblk.dqb_ihardlimit);
8142 if (qctl->qc_dqblk.dqb_bsoftlimit != 0)
8143 fprintf(stderr, " -b %llu",
8144 (unsigned long long)qctl->qc_dqblk.dqb_bsoftlimit);
8145 if (qctl->qc_dqblk.dqb_bhardlimit != 0)
8146 fprintf(stderr, " -B %llu",
8147 (unsigned long long)qctl->qc_dqblk.dqb_bhardlimit);
8149 fprintf(stderr, " %s\n", mnt);
8156 #define BSLIMIT (1 << 0)
8157 #define BHLIMIT (1 << 1)
8158 #define ISLIMIT (1 << 2)
8159 #define IHLIMIT (1 << 3)
8161 int lfs_setquota(int argc, char **argv)
8164 struct if_quotactl *qctl;
8165 char *mnt, *obd_type;
8166 struct obd_dqblk *dqb;
8167 struct option long_opts[] = {
8168 { .val = 'b', .name = "block-softlimit",
8169 .has_arg = required_argument },
8170 { .val = 'B', .name = "block-hardlimit",
8171 .has_arg = required_argument },
8172 { .val = 'd', .name = "default", .has_arg = no_argument },
8173 { .val = LFS_SETQUOTA_DELETE,
8174 .name = "delete", .has_arg = no_argument },
8175 { .val = 'g', .name = "group", .has_arg = required_argument },
8176 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
8177 { .val = 'h', .name = "help", .has_arg = no_argument },
8178 { .val = 'i', .name = "inode-softlimit",
8179 .has_arg = required_argument },
8180 { .val = 'I', .name = "inode-hardlimit",
8181 .has_arg = required_argument },
8182 { .val = 'p', .name = "projid", .has_arg = required_argument },
8183 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
8184 { .val = 'r', .name = "reset", .has_arg = no_argument },
8185 { .val = 'u', .name = "user", .has_arg = required_argument },
8186 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
8187 { .val = LFS_POOL_OPT,
8188 .name = "pool", .has_arg = required_argument },
8190 unsigned int limit_mask = 0;
8191 bool use_default = false;
8192 int qtype, qctl_len;
8194 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
8195 qctl = malloc(qctl_len);
8199 memset(qctl, 0, qctl_len);
8200 obd_type = (char *)qctl->obd_type;
8201 dqb = &qctl->qc_dqblk;
8203 if (has_times_option(argc, argv)) {
8204 rc = lfs_setquota_times(argc, argv, qctl);
8208 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
8209 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
8210 * so it can be used as a marker that qc_type
8211 * isn't reinitialized from command line
8213 while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pru:U",
8214 long_opts, NULL)) != -1) {
8217 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8220 goto quota_type_def;
8223 rc = name2uid(&qctl->qc_id, optarg);
8226 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8229 goto quota_type_def;
8232 rc = name2gid(&qctl->qc_id, optarg);
8235 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8238 goto quota_type_def;
8241 rc = name2projid(&qctl->qc_id, optarg);
8244 if (str2quotaid(&qctl->qc_id, optarg)) {
8246 "%s setquota: invalid id '%s'\n",
8253 if (qctl->qc_id == 0) {
8255 "%s setquota: can't set quota for root usr/group/project.\n",
8262 if (qctl->qc_type != ALLQUOTA) {
8264 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
8269 qctl->qc_type = qtype;
8271 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
8274 "%s setquota: '-d' deprecated, use '-D' or '--default'\n",
8280 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8282 case LFS_SETQUOTA_DELETE:
8283 qctl->qc_cmd = LUSTRE_Q_DELETEQID;
8286 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
8287 dqb->dqb_bsoftlimit >>= 10;
8288 limit_mask |= BSLIMIT;
8289 if (dqb->dqb_bsoftlimit &&
8290 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
8292 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8294 (unsigned long long)dqb->dqb_bsoftlimit,
8298 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
8299 dqb->dqb_bhardlimit >>= 10;
8300 limit_mask |= BHLIMIT;
8301 if (dqb->dqb_bhardlimit &&
8302 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
8304 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
8305 "See '%s help setquota' or Lustre manual for details\n",
8307 (unsigned long long)dqb->dqb_bhardlimit,
8311 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
8312 limit_mask |= ISLIMIT;
8313 if (dqb->dqb_isoftlimit &&
8314 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
8316 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8318 (unsigned long long)dqb->dqb_isoftlimit,
8322 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
8323 limit_mask |= IHLIMIT;
8324 if (dqb->dqb_ihardlimit &&
8325 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
8327 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8329 (unsigned long long)dqb->dqb_ihardlimit,
8333 if (lfs_verify_poolarg(optarg)) {
8337 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8338 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
8339 LUSTRE_Q_SETDEFAULT_POOL :
8340 LUSTRE_Q_SETQUOTAPOOL;
8343 qctl->qc_cmd = LUSTRE_Q_RESETQID;
8347 "%s setquota: unrecognized option '%s'\n",
8348 progname, argv[optind - 1]);
8356 if (qctl->qc_type == ALLQUOTA) {
8358 "%s setquota: either -u or -g must be specified\n",
8364 if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
8365 qctl->qc_cmd != LUSTRE_Q_RESETQID && limit_mask == 0) {
8367 "%s setquota: at least one limit must be specified\n",
8373 if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8374 qctl->qc_cmd == LUSTRE_Q_RESETQID) && limit_mask != 0) {
8376 "%s setquota: limits should not be specified when using default quota, deleting or resetting quota ID\n",
8382 if (use_default && qctl->qc_id == 0) {
8384 "%s setquota: can not set default quota for root user/group/project\n",
8390 if ((qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8391 qctl->qc_cmd == LUSTRE_Q_RESETQID) && qctl->qc_id == 0) {
8393 "%s setquota: can not delete or reset root user/group/project\n",
8399 if (optind != argc - 1) {
8401 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
8402 progname, argv[optind]);
8410 dqb->dqb_bhardlimit = 0;
8411 dqb->dqb_bsoftlimit = 0;
8412 dqb->dqb_ihardlimit = 0;
8413 dqb->dqb_isoftlimit = 0;
8416 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
8417 /* do not set inode limits for Pool Quotas */
8418 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
8419 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
8420 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
8421 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
8422 /* sigh, we can't just set blimits/ilimits */
8423 struct if_quotactl *tmp_qctl;
8425 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8429 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
8430 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8431 strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
8434 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8436 tmp_qctl->qc_type = qctl->qc_type;
8437 tmp_qctl->qc_id = qctl->qc_id;
8439 rc = llapi_quotactl(mnt, tmp_qctl);
8445 if (!(limit_mask & BHLIMIT))
8446 dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
8447 if (!(limit_mask & BSLIMIT))
8448 dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
8449 if (!(limit_mask & IHLIMIT))
8450 dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
8451 if (!(limit_mask & ISLIMIT))
8452 dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
8454 /* Keep grace times if we have got no softlimit arguments */
8455 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
8456 dqb->dqb_valid |= QIF_BTIME;
8457 dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
8460 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
8461 dqb->dqb_valid |= QIF_ITIME;
8462 dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
8467 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
8468 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
8470 if (qctl->qc_cmd == LUSTRE_Q_RESETQID)
8471 rc = lfs_reset_quota(mnt, qctl);
8473 rc = llapi_quotactl(mnt, qctl);
8478 "%s setquota: cannot quotactl '%s' '%s': %s\n",
8480 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
8483 "%s setquota: quotactl failed: %s\n",
8484 progname, strerror(-rc));
8488 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8494 /* Converts seconds value into format string
8495 * result is returned in buf
8497 * 1. result is in descenting order: 1w2d3h4m5s
8498 * 2. zero fields are not filled (except for p. 3): 5d1s
8499 * 3. zero seconds value is presented as "0s"
8501 static char *__sec2str(time_t seconds, char *buf)
8503 const char spec[] = "smhdw";
8504 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8509 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8510 c = seconds / mult[i];
8512 if (c > 0 || (i == 0 && buf == tail))
8513 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8522 static void sec2str(time_t seconds, char *buf, int rc)
8529 tail = __sec2str(seconds, tail);
8531 if (rc && tail - buf < 39) {
8537 static void diff2str(time_t seconds, char *buf, time_t now)
8542 if (seconds <= now) {
8543 strcpy(buf, "expired");
8546 __sec2str(seconds - now, buf);
8549 static void print_quota_title(char *name, struct if_quotactl *qctl,
8550 bool human_readable, bool show_default)
8553 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8554 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8555 "Filesystem", "bquota", "blimit", "bgrace",
8556 "iquota", "ilimit", "igrace");
8558 printf("Disk quotas for %s %s (%cid %u):\n",
8559 qtype_name(qctl->qc_type), name,
8560 *qtype_name(qctl->qc_type), qctl->qc_id);
8561 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8562 "Filesystem", human_readable ? "used" : "kbytes",
8563 "quota", "limit", "grace",
8564 "files", "quota", "limit", "grace");
8568 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8571 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8574 snprintf(buf, buflen, "%5.4gP",
8575 (double)num / ((__u64)1 << 40));
8577 snprintf(buf, buflen, "%5.4gT",
8578 (double)num / (1 << 30));
8580 snprintf(buf, buflen, "%5.4gG",
8581 (double)num / (1 << 20));
8583 snprintf(buf, buflen, "%5.4gM",
8584 (double)num / (1 << 10));
8586 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8590 #ifdef HAVE_NATIVE_CLIENT
8591 /* In the current Lustre implementation, the grace time is either the time
8592 * or the timestamp to be used after some quota ID exceeds the soft limt,
8593 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8595 #define LQUOTA_GRACE_BITS 48
8596 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8597 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8598 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8599 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8600 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8603 #define STRBUF_LEN 24
8604 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8605 int rc, bool h, bool show_default)
8611 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8612 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8613 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8614 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8615 int bover = 0, iover = 0;
8616 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8617 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8619 char strbuf[STRBUF_LEN];
8621 dqb->dqb_btime &= LQUOTA_GRACE_MASK;
8622 dqb->dqb_itime &= LQUOTA_GRACE_MASK;
8624 if (dqb->dqb_bhardlimit &&
8625 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8627 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8628 if (dqb->dqb_btime > now)
8634 if (dqb->dqb_ihardlimit &&
8635 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8637 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8638 if (dqb->dqb_itime > now)
8644 if (strlen(mnt) > 15)
8645 printf("%s\n%15s", mnt, "");
8647 printf("%15s", mnt);
8650 snprintf(timebuf, sizeof(timebuf), "%llu",
8651 (unsigned long long)dqb->dqb_btime);
8653 diff2str(dqb->dqb_btime, timebuf, now);
8655 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8656 strbuf, sizeof(strbuf), h);
8657 if (rc == -EREMOTEIO)
8658 sprintf(numbuf[0], "%s*", strbuf);
8660 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8661 "%s" : "[%s]", strbuf);
8663 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8664 if (type == QC_GENERAL)
8665 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8666 "%s" : "[%s]", strbuf);
8668 sprintf(numbuf[1], "%s", "-");
8670 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8671 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8672 "%s" : "[%s]", strbuf);
8675 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8677 printf(" %7s%c %6s %7s %7s",
8678 numbuf[0], bover ? '*' : ' ', numbuf[1],
8679 numbuf[2], bover > 1 ? timebuf : "-");
8682 snprintf(timebuf, sizeof(timebuf), "%llu",
8683 (unsigned long long)dqb->dqb_itime);
8685 diff2str(dqb->dqb_itime, timebuf, now);
8687 snprintf(numbuf[0], sizeof(numbuf),
8688 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8689 (uintmax_t)dqb->dqb_curinodes);
8691 if (type == QC_GENERAL)
8692 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8694 (uintmax_t)dqb->dqb_isoftlimit);
8696 sprintf(numbuf[1], "%s", "-");
8698 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8699 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8702 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8703 else if (type != QC_OSTIDX)
8704 printf(" %7s%c %6s %7s %7s",
8705 numbuf[0], iover ? '*' : ' ', numbuf[1],
8706 numbuf[2], iover > 1 ? timebuf : "-");
8708 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8710 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
8711 qctl->qc_cmd == LUSTRE_Q_GETINFOPOOL ||
8712 qctl->qc_cmd == Q_GETOINFO) {
8716 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8717 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8719 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8720 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8721 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8723 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8725 printf("Block grace time: %s; Inode grace time: %s\n",
8726 bgtimebuf, igtimebuf);
8730 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8734 /* format is "lustre-OST0001" */
8735 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8737 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8742 *idx = strtoul(dash, &endp, 16);
8743 if (*idx > 0xffff) {
8744 fprintf(stderr, "wrong index %s\n", tgtname);
8751 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8752 bool h, __u64 *total)
8754 int rc = 0, rc1 = 0, count = 0, i = 0;
8755 char **list = NULL, *buffer = NULL;
8756 __u32 valid = qctl->qc_valid;
8758 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8761 /* Is it correct for the case OST0000, OST0002, OST0003 -
8762 * we will ask OST0001 that is absent and won't ask OST0003? */
8763 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8765 fprintf(stderr, "can not get %s count: %s\n",
8766 is_mdt ? "mdt" : "ost", strerror(-rc));
8770 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8771 char fname[PATH_MAX];
8772 char fsname[LUSTRE_MAXFSNAME + 1];
8773 int bufsize = sizeof(struct obd_uuid) * count;
8775 rc = llapi_search_fsname(mnt, fsname);
8777 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8781 buffer = malloc(bufsize + sizeof(*list) * count);
8784 list = (char **)(buffer + bufsize);
8785 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8786 count = llapi_get_poolmembers(fname, list, count,
8792 for (i = 0; i < count; i++) {
8793 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8796 if (tgt_name2index(list[i], &index))
8798 qctl->qc_idx = index;
8803 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8804 rc = llapi_quotactl(mnt, qctl);
8806 /* It is remote client case. */
8807 if (rc == -EOPNOTSUPP) {
8812 /* no target for this index yet */
8813 if (rc == -ENODEV) {
8818 /* inactive target */
8819 if (rc == -ENODATA) {
8820 char name[UUID_MAX+8];
8822 snprintf(name, sizeof(name), "%s[inact]",
8823 obd_uuid2str(&qctl->obd_uuid));
8824 memset(&qctl->qc_dqinfo, 0,
8825 sizeof(qctl->qc_dqinfo));
8826 memset(&qctl->qc_dqblk, 0,
8827 sizeof(qctl->qc_dqblk));
8828 print_quota(name, qctl, qctl->qc_valid, 0, h,
8836 fprintf(stderr, "quotactl %s%d failed.\n",
8837 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8841 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8842 qctl->qc_valid, 0, h, false);
8843 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8844 qctl->qc_dqblk.dqb_bhardlimit;
8849 qctl->qc_valid = valid;
8853 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8854 int verbose, int quiet, bool human_readable,
8857 int rc1 = 0, rc2 = 0, rc3 = 0;
8858 char *obd_type = (char *)qctl->obd_type;
8859 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8860 __u64 total_ialloc = 0, total_balloc = 0;
8861 bool use_default_for_blk = false;
8862 bool use_default_for_file = false;
8865 rc1 = llapi_quotactl(mnt, qctl);
8869 fprintf(stderr, "%s quotas are not enabled.\n",
8870 qtype_name(qctl->qc_type));
8873 fprintf(stderr, "Permission denied.\n");
8876 /* We already got error message. */
8879 fprintf(stderr, "Unexpected quotactl error: %s\n",
8884 if (!show_default && qctl->qc_id == 0) {
8885 qctl->qc_dqblk.dqb_bhardlimit = 0;
8886 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8887 qctl->qc_dqblk.dqb_ihardlimit = 0;
8888 qctl->qc_dqblk.dqb_isoftlimit = 0;
8889 qctl->qc_dqblk.dqb_btime = 0;
8890 qctl->qc_dqblk.dqb_itime = 0;
8891 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8894 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8895 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8896 use_default_for_blk = true;
8897 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8900 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8901 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8902 use_default_for_file = true;
8903 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8906 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8907 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8908 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8909 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8910 print_quota_title(name, qctl, human_readable, show_default);
8912 if (rc1 && *obd_type)
8913 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8915 if (qctl->qc_valid != QC_GENERAL)
8918 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8919 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8920 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8921 (QIF_LIMITS|QIF_USAGE));
8923 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8925 if (!show_default && verbose &&
8926 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8927 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8928 char strbuf[STRBUF_LEN];
8930 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8932 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8934 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8936 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8937 (uintmax_t)total_ialloc, strbuf);
8940 if (use_default_for_blk)
8941 printf("%cid %u is using default block quota setting\n",
8942 *qtype_name(qctl->qc_type), qctl->qc_id);
8944 if (use_default_for_file)
8945 printf("%cid %u is using default file quota setting\n",
8946 *qtype_name(qctl->qc_type), qctl->qc_id);
8948 if (rc1 || rc2 || rc3 || inacc)
8949 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8963 static int lfs_project(int argc, char **argv)
8965 int ret = 0, err = 0, c, i;
8966 struct project_handle_control phc = { 0 };
8967 enum lfs_project_ops_t op;
8970 phc.assign_projid = false;
8971 /* default action */
8972 op = LFS_PROJECT_LIST;
8974 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8977 if (op != LFS_PROJECT_LIST) {
8979 "%s: cannot specify '-c' '-C' '-s' together\n",
8984 op = LFS_PROJECT_CHECK;
8987 if (op != LFS_PROJECT_LIST) {
8989 "%s: cannot specify '-c' '-C' '-s' together\n",
8994 op = LFS_PROJECT_CLEAR;
8997 if (op != LFS_PROJECT_LIST) {
8999 "%s: cannot specify '-c' '-C' '-s' together\n",
9004 phc.set_inherit = true;
9005 op = LFS_PROJECT_SET;
9011 phc.keep_projid = true;
9014 phc.recursive = true;
9017 if (str2quotaid(&phc.projid, optarg)) {
9019 "Invalid project ID: %s\n",
9024 phc.assign_projid = true;
9028 phc.newline = false;
9031 fprintf(stderr, "%s: invalid option '%c'\n",
9037 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
9038 op = LFS_PROJECT_SET;
9039 phc.set_projid = true;
9040 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
9041 phc.set_projid = true;
9045 case LFS_PROJECT_CHECK:
9046 if (phc.keep_projid) {
9048 "%s: '-k' is useless together with '-c'\n",
9053 case LFS_PROJECT_CLEAR:
9056 "%s: '-0' is useless together with '-C'\n",
9060 if (phc.assign_projid) {
9062 "%s: '-p' is useless together with '-C'\n",
9067 case LFS_PROJECT_SET:
9070 "%s: '-0' is useless together with '-s'\n",
9074 if (phc.keep_projid) {
9076 "%s: '-k' is useless together with '-s'\n",
9084 "%s: '-0' is useless for list operations\n",
9094 fprintf(stderr, "%s: missing file or directory target(s)\n",
9099 for (i = 0; i < argc; i++) {
9101 case LFS_PROJECT_CHECK:
9102 err = lfs_project_check(argv[i], &phc);
9104 case LFS_PROJECT_LIST:
9105 err = lfs_project_list(argv[i], &phc);
9107 case LFS_PROJECT_CLEAR:
9108 err = lfs_project_clear(argv[i], &phc);
9110 case LFS_PROJECT_SET:
9111 err = lfs_project_set(argv[i], &phc);
9123 static int lfs_quota(int argc, char **argv)
9126 char *mnt, *name = NULL;
9127 struct if_quotactl *qctl;
9129 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
9130 __u32 valid = QC_GENERAL, idx = 0;
9131 bool human_readable = false;
9132 bool show_default = false;
9134 bool show_pools = false;
9135 struct option long_opts[] = {
9136 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
9138 char **poollist = NULL;
9142 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
9146 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
9147 qctl->qc_type = ALLQUOTA;
9148 obd_uuid = (char *)qctl->obd_uuid.uuid;
9150 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
9151 long_opts, NULL)) != -1) {
9154 show_default = true;
9159 show_default = true;
9164 show_default = true;
9168 if (qctl->qc_type != ALLQUOTA) {
9170 "%s quota: only one of -u, -g, or -p may be specified\n",
9175 qctl->qc_type = qtype;
9178 qctl->qc_cmd = LUSTRE_Q_GETINFO;
9181 valid = qctl->qc_valid = QC_UUID;
9182 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
9185 valid = qctl->qc_valid = QC_MDTIDX;
9186 idx = qctl->qc_idx = atoi(optarg);
9187 if (idx == 0 && *optarg != '0') {
9189 "%s quota: invalid MDT index '%s'\n",
9196 valid = qctl->qc_valid = QC_OSTIDX;
9197 idx = qctl->qc_idx = atoi(optarg);
9198 if (idx == 0 && *optarg != '0') {
9200 "%s quota: invalid OST index '%s'\n",
9213 human_readable = true;
9216 if ((!optarg) && (argv[optind] != NULL) &&
9217 (argv[optind][0] != '-') &&
9218 (argv[optind][0] != '/')) {
9219 optarg = argv[optind++];
9220 if (lfs_verify_poolarg(optarg)) {
9224 strncpy(qctl->qc_poolname, optarg,
9226 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
9227 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
9229 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9233 /* optarg is NULL */
9235 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9238 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
9239 progname, argv[optind - 1]);
9245 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
9246 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9247 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
9248 qctl->qc_type == ALLQUOTA &&
9249 optind == argc - 1 && !show_default) {
9252 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
9253 qctl->qc_type = qtype;
9254 qctl->qc_valid = valid;
9255 if (qtype == USRQUOTA) {
9256 qctl->qc_id = geteuid();
9257 rc = uid2name(&name, qctl->qc_id);
9259 qctl->qc_id = getegid();
9260 rc = gid2name(&name, qctl->qc_id);
9261 memset(&qctl->qc_dqblk, 0,
9262 sizeof(qctl->qc_dqblk));
9267 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
9268 human_readable, show_default);
9273 /* lfs quota -u username /path/to/lustre/mount */
9274 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9275 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
9276 /* options should be followed by u/g-name and mntpoint */
9277 if ((!show_default && optind + 2 != argc) ||
9278 (show_default && optind + 1 != argc) ||
9279 qctl->qc_type == ALLQUOTA) {
9281 "%s quota: name and mount point must be specified\n",
9287 if (!show_default) {
9288 name = argv[optind++];
9289 switch (qctl->qc_type) {
9291 rc = name2uid(&qctl->qc_id, name);
9294 rc = name2gid(&qctl->qc_id, name);
9297 rc = name2projid(&qctl->qc_id, name);
9304 qctl->qc_valid = QC_GENERAL;
9305 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
9306 LUSTRE_Q_GETDEFAULT_POOL :
9307 LUSTRE_Q_GETDEFAULT;
9312 if (str2quotaid(&qctl->qc_id, name)) {
9313 fprintf(stderr, "%s quota: invalid id '%s'\n",
9319 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
9320 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
9331 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
9335 for (i = 0; i < poolcount; i++) {
9336 p = memchr(poollist[i], '.', MAXNAMLEN);
9338 fprintf(stderr, "bad string format %.*s\n",
9339 MAXNAMLEN, poollist[i]);
9344 printf("Quotas for pool: %s\n", p);
9345 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
9346 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9347 human_readable, show_default);
9354 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9355 human_readable, show_default);
9361 #endif /* HAVE_SYS_QUOTA_H! */
9363 static int flushctx_ioctl(char *mp)
9367 fd = open(mp, O_RDONLY);
9369 fprintf(stderr, "flushctx: error open %s: %s\n",
9370 mp, strerror(errno));
9374 rc = ioctl(fd, LL_IOC_FLUSHCTX);
9376 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
9377 mp, strerror(errno));
9383 static int lfs_flushctx(int argc, char **argv)
9385 int kdestroy = 0, reap = 0, c;
9386 char mntdir[PATH_MAX] = {'\0'};
9390 while ((c = getopt(argc, argv, "kr")) != -1) {
9400 "error: %s: option '-%c' unrecognized\n",
9407 rc = system("kdestroy > /dev/null");
9409 rc = WEXITSTATUS(rc);
9411 "error destroying tickets: %d, continuing\n",
9416 if (optind >= argc) {
9417 /* flush for all mounted lustre fs. */
9418 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
9419 /* Check if we have a mount point */
9420 if (mntdir[0] == '\0')
9423 if (flushctx_ioctl(mntdir))
9426 mntdir[0] = '\0'; /* avoid matching in next loop */
9429 /* flush fs as specified */
9430 while (optind < argc) {
9431 if (flushctx_ioctl(argv[optind++]))
9437 rc = system("keyctl reap > /dev/null");
9439 rc = WEXITSTATUS(rc);
9440 fprintf(stderr, "error reaping keyring: %d\n", rc);
9447 static int lfs_changelog(int argc, char **argv)
9449 void *changelog_priv;
9450 struct changelog_rec *rec;
9451 long long startrec = 0, endrec = 0;
9453 struct option long_opts[] = {
9454 { .val = 'f', .name = "follow", .has_arg = no_argument },
9456 char short_opts[] = "f";
9459 while ((rc = getopt_long(argc, argv, short_opts,
9460 long_opts, NULL)) != -1) {
9467 "%s changelog: unrecognized option '%s'\n",
9468 progname, argv[optind - 1]);
9472 if (optind >= argc) {
9473 fprintf(stderr, "%s changelog: mdtname must be specified\n",
9478 mdd = argv[optind++];
9479 if (argc > optind) {
9481 startrec = strtoll(argv[optind++], NULL, 10);
9482 if (errno != 0 || startrec < 0) {
9484 "%s changelog: bad startrec\n",
9490 if (argc > optind) {
9492 endrec = strtoll(argv[optind++], NULL, 10);
9493 if (errno != 0 || endrec < 0) {
9495 "%s changelog: bad endrec\n",
9501 rc = llapi_changelog_start(&changelog_priv,
9502 CHANGELOG_FLAG_BLOCK |
9503 CHANGELOG_FLAG_JOBID |
9504 CHANGELOG_FLAG_EXTRA_FLAGS |
9505 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
9508 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
9509 progname, strerror(errno = -rc));
9513 rc = llapi_changelog_set_xflags(changelog_priv,
9514 CHANGELOG_EXTRA_FLAG_UIDGID |
9515 CHANGELOG_EXTRA_FLAG_NID |
9516 CHANGELOG_EXTRA_FLAG_OMODE |
9517 CHANGELOG_EXTRA_FLAG_XATTR);
9520 "%s changelog: cannot set xflags for changelog: %s\n",
9521 progname, strerror(errno = -rc));
9525 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9529 if (endrec && rec->cr_index > endrec) {
9530 llapi_changelog_free(&rec);
9533 if (rec->cr_index < startrec) {
9534 llapi_changelog_free(&rec);
9538 secs = rec->cr_time >> 30;
9539 gmtime_r(&secs, &ts);
9540 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9541 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9542 changelog_type2str(rec->cr_type),
9543 ts.tm_hour, ts.tm_min, ts.tm_sec,
9544 (int)(rec->cr_time & ((1 << 30) - 1)),
9545 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9546 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9548 if (rec->cr_flags & CLF_JOBID) {
9549 struct changelog_ext_jobid *jid =
9550 changelog_rec_jobid(rec);
9552 if (jid->cr_jobid[0] != '\0')
9553 printf(" j=%s", jid->cr_jobid);
9556 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
9557 struct changelog_ext_extra_flags *ef =
9558 changelog_rec_extra_flags(rec);
9560 printf(" ef=0x%llx",
9561 (unsigned long long)ef->cr_extra_flags);
9563 if (ef->cr_extra_flags & CLFE_UIDGID) {
9564 struct changelog_ext_uidgid *uidgid =
9565 changelog_rec_uidgid(rec);
9567 printf(" u=%llu:%llu",
9568 (unsigned long long)uidgid->cr_uid,
9569 (unsigned long long)uidgid->cr_gid);
9571 if (ef->cr_extra_flags & CLFE_NID) {
9572 struct changelog_ext_nid *nid =
9573 changelog_rec_nid(rec);
9576 libcfs_nid2str(nid->cr_nid));
9579 if (ef->cr_extra_flags & CLFE_OPEN) {
9580 struct changelog_ext_openmode *omd =
9581 changelog_rec_openmode(rec);
9582 char mode[] = "---";
9584 /* exec mode must be exclusive */
9585 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9588 if (omd->cr_openflags & MDS_FMODE_READ)
9590 if (omd->cr_openflags &
9597 if (strcmp(mode, "---") != 0)
9598 printf(" m=%s", mode);
9601 if (ef->cr_extra_flags & CLFE_XATTR) {
9602 struct changelog_ext_xattr *xattr =
9603 changelog_rec_xattr(rec);
9605 if (xattr->cr_xattr[0] != '\0')
9606 printf(" x=%s", xattr->cr_xattr);
9610 if (!fid_is_zero(&rec->cr_pfid))
9611 printf(" p="DFID, PFID(&rec->cr_pfid));
9612 if (rec->cr_namelen)
9613 printf(" %.*s", rec->cr_namelen,
9614 changelog_rec_name(rec));
9616 if (rec->cr_flags & CLF_RENAME) {
9617 struct changelog_ext_rename *rnm =
9618 changelog_rec_rename(rec);
9620 if (!fid_is_zero(&rnm->cr_sfid))
9621 printf(" s="DFID" sp="DFID" %.*s",
9622 PFID(&rnm->cr_sfid),
9623 PFID(&rnm->cr_spfid),
9624 (int)changelog_rec_snamelen(rec),
9625 changelog_rec_sname(rec));
9629 llapi_changelog_free(&rec);
9632 llapi_changelog_fini(&changelog_priv);
9635 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9636 progname, strerror(errno = -rc));
9638 return (rc == 1 ? 0 : rc);
9641 static int lfs_changelog_clear(int argc, char **argv)
9650 endrec = strtoll(argv[3], NULL, 10);
9651 if (errno != 0 || endrec < 0) {
9653 "%s: bad endrec '%s'\n",
9658 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9661 fprintf(stderr, "%s: record out of range: %llu\n",
9663 else if (rc == -ENOENT)
9664 fprintf(stderr, "%s: no changelog user: %s\n",
9667 fprintf(stderr, "%s error: %s\n", argv[0],
9676 static void rstripc(char *str, int c)
9678 char *end = str + strlen(str);
9680 for (; str < end && end[-1] == c; --end)
9684 /* Helper function to lfs_fid2path. To print out only the file names and
9685 * not the full path. Do not call OBD_IOC_FID2PATH for every file. Instead
9686 * read the trusted.link xattr and loop over all the records to get all the
9689 static int lfs_fid2path_prn_name(char *mnt_dir, char *path_buf,
9690 bool print_linkno, bool print_fid, char *ptr,
9691 const char *fid_str, int linktmp)
9693 char buf[65536]; /* BUFFER_SIZE 65536 */
9694 char full_path[PATH_MAX * 2 + 2];
9695 struct link_ea_header *leh;
9696 struct link_ea_entry *lee;
9698 int reclen, i, rc = 0;
9700 /* Generate full_path */
9701 snprintf(full_path, sizeof(full_path) - 1, "%s/%s", mnt_dir, path_buf);
9703 size = getxattr(full_path, "trusted.link", buf, sizeof(buf));
9705 fprintf(stderr, "%s: failed to read %s xattr: %s\n", path_buf,
9706 "trusted.link", strerror(errno));
9711 leh = (struct link_ea_header *)buf;
9713 if (leh->leh_magic == __swab32(LINK_EA_MAGIC))
9714 leh->leh_reccount = __swab32(leh->leh_reccount);
9716 lee = (struct link_ea_entry *)(leh + 1);
9718 for (i = 0; i < leh->leh_reccount; i++) {
9719 reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
9721 /* handle -n -l case */
9723 ptr = strrchr(path_buf, '/');
9729 if (strcmp(ptr, lee->lee_name) == 0) {
9731 printf("%s ", fid_str);
9733 printf("%d ", linktmp);
9734 printf("%s\n", lee->lee_name);
9739 printf("%s ", fid_str);
9740 printf("%s\n", lee->lee_name);
9743 /* Get next record */
9744 lee = (struct link_ea_entry *)((char *)lee + reclen);
9750 static int lfs_fid2path(int argc, char **argv)
9752 struct option long_opts[] = {
9753 { .val = '0', .name = "print0", .has_arg = no_argument },
9754 { .val = 'c', .name = "cur", .has_arg = no_argument },
9755 { .val = 'c', .name = "current", .has_arg = no_argument },
9756 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9757 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9758 { .val = 'l', .name = "link", .has_arg = required_argument },
9759 { .val = 'n', .name = "name", .has_arg = no_argument },
9761 char short_opts[] = "0cfl:pr:n";
9762 bool print_only_fname = false;
9763 bool print_linkno = false;
9764 bool print_link = false;
9765 bool print_fid = false;
9767 char mnt_dir[PATH_MAX] = "";
9769 char *path_or_fsname;
9770 long long recno = -1;
9772 char *endptr = NULL;
9773 char link_separator = '\n';
9778 while ((c = getopt_long(argc, argv, short_opts,long_opts, NULL)) !=
9782 link_separator = '\0';
9792 linkno = strtol(optarg, &endptr, 10);
9793 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9795 "%s fid2path: invalid linkno '%s'\n",
9799 print_linkno = true;
9802 /* Bypass the full parent path if true
9803 * only print the final filename */
9804 print_only_fname = true;
9807 /* recno is something to do with changelogs
9808 * that was never implemented. We just pass it
9809 * through for the MDT to ignore.
9812 recno = strtoll(optarg, &endptr, 10);
9813 if (errno != 0 || *endptr != '\0' || recno < 0) {
9815 "%s fid2path: invalid recno '%s'\n",
9822 "%s fid2path: unrecognized option '%s'\n",
9823 progname, argv[optind - 1]);
9828 if (argc - optind < 2) {
9830 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9835 path_or_fsname = argv[optind];
9837 if (*path_or_fsname == '/') {
9838 print_mnt_dir = true;
9839 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9841 print_mnt_dir = false;
9842 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9847 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9848 progname, path_or_fsname, strerror(-rc));
9852 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9855 "%s fid2path: cannot open mount point for '%s': %s\n",
9856 progname, path_or_fsname, strerror(-rc));
9860 /* Strip trailing slashes from mnt_dir. */
9861 rstripc(mnt_dir + 1, '/');
9863 for (i = optind + 1; i < argc; i++) {
9864 const char *fid_str = argv[i];
9869 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9872 "%s fid2path: invalid FID '%s'\n",
9880 int linktmp = (linkno >= 0) ? linkno : 0;
9883 int oldtmp = linktmp;
9884 long long rectmp = recno;
9885 char path_buf[PATH_MAX];
9887 rc2 = llapi_fid2path_at(mnt_fd, &fid, path_buf,
9888 sizeof(path_buf), &rectmp,
9892 "%s fid2path: cannot find %s %s: %s\n",
9893 progname, path_or_fsname, fid_str,
9900 if (print_only_fname && !print_link) {
9901 /* '-n' is passed as option here.
9902 * For all other cases of -c fall back
9903 * to default(else) path as to get the link
9904 * count associated with the file name call
9905 * to OBD_IOC_FID2PATH is required
9907 rc = lfs_fid2path_prn_name(mnt_dir,
9912 /* llapi_fid2path_at() is already called once
9913 * in this case. No need to call it again.
9914 * Break out as we have all the filenames.
9920 printf("%s ", fid_str);
9923 printf("%d ", linktmp);
9925 /* You may think this looks wrong or weird (and it is!)
9926 * but we are actually trying to preserve the old quirky
9927 * behaviors (enforced by our old quirky tests!) that
9928 * make lfs so much fun to work on:
9930 * lustre 0x200000007:0x1:0x0 => "/"
9931 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9933 * Note that llapi_fid2path() returns "" for the root
9935 if (!print_only_fname) {
9937 print_mnt_dir ? mnt_dir : "",
9938 (print_mnt_dir || *path_buf == '\0') ?
9939 "/" : "", path_buf, link_separator);
9941 ptr = strrchr(path_buf, '/');
9943 printf("%s\n", path_buf);
9945 printf("%s\n", ptr + 1);
9949 /* specified linkno */
9952 if (oldtmp == linktmp)
9964 static int lfs_path2fid(int argc, char **argv)
9966 struct option long_opts[] = {
9967 { .val = 'p', .name = "parents", .has_arg = no_argument },
9970 const char short_opts[] = "p";
9971 const char *sep = "";
9974 bool show_parents = false;
9976 while ((rc = getopt_long(argc, argv, short_opts,
9977 long_opts, NULL)) != -1) {
9980 show_parents = true;
9984 "%s path2fid: unrecognized option '%s'\n",
9985 progname, argv[optind - 1]);
9990 if (optind > argc - 1) {
9991 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9994 } else if (optind < argc - 1) {
9999 for (path = argv + optind; optind < argc; path++, optind++) {
10002 if (!show_parents) {
10003 err = llapi_path2fid(*path, &fid);
10005 printf("%s%s"DFID"\n",
10006 *sep != '\0' ? *path : "", sep,
10009 char name[NAME_MAX + 1];
10010 unsigned int linkno = 0;
10012 while ((err = llapi_path2parent(*path, linkno, &fid,
10013 name, sizeof(name))) == 0) {
10014 if (*sep != '\0' && linkno == 0)
10015 printf("%s%s", *path, sep);
10017 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
10022 /* err == -ENODATA is end-of-loop */
10023 if (linkno > 0 && err == -ENODATA) {
10031 "%s path2fid: cannot get %sfid for '%s': %s\n",
10032 progname, show_parents ? "parent " : "", *path,
10044 #define MAX_ERRNO 4095
10045 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
10047 static int lfs_rmfid_and_show_errors(int rootfd, struct fid_array *fa)
10051 rc = llapi_rmfid_at(rootfd, fa);
10053 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
10054 progname, strerror(-rc));
10058 for (k = 0; k < fa->fa_nr; k++) {
10059 rc2 = (__s32)fa->fa_fids[k].f_ver;
10060 if (!IS_ERR_VALUE(rc2))
10066 fa->fa_fids[k].f_ver = 0;
10067 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
10068 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
10074 static int lfs_rmfid(int argc, char **argv)
10076 int rc = 0, rc2, rc3 = 0, nr;
10077 struct fid_array *fa;
10078 const char *device;
10082 /* Interactive mode: Adjust optind */
10086 device = argv[optind++];
10088 if (optind > argc - 1) {
10089 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
10093 nr = argc - optind;
10095 rc = llapi_root_path_open(device, &rootfd);
10098 "%s rmfid: error opening device/fsname '%s': %s\n",
10099 progname, device, strerror(-rc));
10103 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
10105 rc = -errno ?: -ENOMEM;
10106 fprintf(stderr, "%s rmfid: error allocating %zd bytes: %s\n",
10107 progname, offsetof(struct fid_array, fa_fids[nr + 1]),
10114 while (optind < argc) {
10118 origfidstr = fidstr = argv[optind++];
10119 while (*fidstr == '[')
10121 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
10123 fprintf(stderr, "lfs rmfid: '%s': Wrong FID format\n",
10126 rc3 = -EINVAL; /* Invalid argument */
10130 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
10131 /* start another batch */
10132 rc2 = lfs_rmfid_and_show_errors(rootfd, fa);
10141 rc2 = lfs_rmfid_and_show_errors(rootfd, fa);
10158 static int lfs_data_version(int argc, char **argv)
10160 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
10161 __u64 data_version;
10168 fprintf(stderr, "%s: FILE must be specified\n",
10173 while ((c = getopt(argc, argv, "hnrw")) != -1) {
10176 data_version_flags = 0;
10179 data_version_flags |= LL_DV_RD_FLUSH;
10182 data_version_flags |= LL_DV_WR_FLUSH;
10186 "%s data_version: unrecognized option '%s'\n",
10187 progname, argv[optind - 1]);
10193 if (optind == argc) {
10194 fprintf(stderr, "%s data_version: FILE must be specified\n",
10199 path = argv[optind];
10200 fd = open(path, O_RDONLY);
10203 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
10204 progname, path, strerror(-rc));
10208 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
10211 "%s data_version: cannot get version for '%s': %s\n",
10212 progname, path, strerror(-rc));
10214 printf("%ju" "\n", (uintmax_t)data_version);
10220 static int lfs_hsm_state(int argc, char **argv)
10225 struct hsm_user_state hus;
10234 rc2 = llapi_hsm_state_get(path, &hus);
10237 "%s %s: get HSM state for '%s' failed: %s\n",
10238 progname, argv[0], path, strerror(-rc2));
10244 /* Display path name and status flags */
10245 printf("%s: (0x%08x)", path, hus.hus_states);
10247 if (hus.hus_states & HS_RELEASED)
10248 printf(" released");
10249 if (hus.hus_states & HS_EXISTS)
10251 if (hus.hus_states & HS_DIRTY)
10253 if (hus.hus_states & HS_ARCHIVED)
10254 printf(" archived");
10255 /* Display user-settable flags */
10256 if (hus.hus_states & HS_NORELEASE)
10257 printf(" never_release");
10258 if (hus.hus_states & HS_NOARCHIVE)
10259 printf(" never_archive");
10260 if (hus.hus_states & HS_LOST)
10261 printf(" lost_from_hsm");
10263 if (hus.hus_archive_id != 0)
10264 printf(", archive_id:%d", hus.hus_archive_id);
10267 } while (++i < argc);
10272 #define LFS_HSM_SET 0
10273 #define LFS_HSM_CLEAR 1
10276 * Generic function to set or clear HSM flags.
10277 * Used by hsm_set and hsm_clear.
10279 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
10281 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
10283 struct option long_opts[] = {
10284 { .val = 'A', .name = "archived", .has_arg = no_argument },
10285 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
10286 { .val = 'd', .name = "dirty", .has_arg = no_argument },
10287 { .val = 'e', .name = "exists", .has_arg = no_argument },
10288 { .val = 'h', .name = "help", .has_arg = no_argument },
10289 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
10290 { .val = 'l', .name = "lost", .has_arg = no_argument },
10291 { .val = 'r', .name = "norelease", .has_arg = no_argument },
10292 { .name = NULL } };
10296 __u32 archive_id = 0;
10302 while ((c = getopt_long(argc, argv, "aAdehi:lr",
10303 long_opts, NULL)) != -1) {
10309 mask |= HS_NOARCHIVE;
10312 mask |= HS_ARCHIVED;
10315 mask |= HS_NORELEASE;
10325 archive_id = strtol(optarg, &end, 10);
10326 if (errno != 0 || *end != '\0' || archive_id < 0) {
10328 "%s: invalid archive_id: '%s'\n",
10334 fprintf(stderr, "%s: unrecognized option '%s'\n",
10335 progname, argv[optind - 1]);
10342 /* User should have specified a flag */
10346 while (optind < argc) {
10348 path = argv[optind];
10350 /* If mode == 0, this means we apply the mask. */
10351 if (mode == LFS_HSM_SET)
10352 rc2 = llapi_hsm_state_set(path, mask, 0, archive_id);
10354 rc2 = llapi_hsm_state_set(path, 0, mask, 0);
10358 "%s %s: change hsm flags for '%s' failed: %s\n",
10359 progname, argv[0], path, strerror(-rc2));
10369 static int lfs_hsm_action(int argc, char **argv)
10371 struct hsm_current_action hca;
10372 struct hsm_extent he;
10373 enum hsm_user_action hua;
10374 enum hsm_progress_states hps;
10386 rc2 = llapi_hsm_current_action(path, &hca);
10389 "%s %s: get hsm action for '%s' failed: %s\n",
10390 progname, argv[0], path, strerror(-rc2));
10396 he = hca.hca_location;
10397 hua = hca.hca_action;
10398 hps = hca.hca_state;
10400 printf("%s: %s", path, hsm_user_action2name(hua));
10402 /* Skip file without action */
10403 if (hca.hca_action == HUA_NONE) {
10408 printf(" %s ", hsm_progress_state2name(hps));
10410 if ((hps == HPS_RUNNING) &&
10411 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
10412 printf("(%llu bytes moved)\n",
10413 (unsigned long long)he.length);
10414 else if ((he.offset + he.length) == LUSTRE_EOF)
10415 printf("(from %llu to EOF)\n",
10416 (unsigned long long)he.offset);
10418 printf("(from %llu to %llu)\n",
10419 (unsigned long long)he.offset,
10420 (unsigned long long)(he.offset + he.length));
10422 } while (++i < argc);
10427 static int lfs_hsm_set(int argc, char **argv)
10429 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
10432 static int lfs_hsm_clear(int argc, char **argv)
10434 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
10438 * Check file state and return its fid, to be used by lfs_hsm_request().
10440 * \param[in] file Path to file to check
10441 * \param[in,out] fid Pointer to allocated lu_fid struct.
10442 * \param[in,out] last_dev Pointer to last device id used.
10444 * \return 0 on success.
10446 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
10452 rc = lstat(file, &st);
10454 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
10458 * Checking for regular file as archiving as posix copytool
10459 * rejects archiving files other than regular files
10461 if (!S_ISREG(st.st_mode)) {
10462 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
10465 /* A request should be ... */
10466 if (*last_dev != st.st_dev && *last_dev != 0) {
10468 "All files should be on the same filesystem: %s\n",
10472 *last_dev = st.st_dev;
10474 rc = llapi_path2fid(file, fid);
10476 fprintf(stderr, "Cannot read FID of %s: %s\n",
10477 file, strerror(-rc));
10483 /* Fill an HSM HUR item with a given file name.
10485 * If mntpath is set, then the filename is actually a FID, and no
10486 * lookup on the filesystem will be performed.
10488 * \param[in] hur the user request to fill
10489 * \param[in] idx index of the item inside the HUR to fill
10490 * \param[in] mntpath mountpoint of Lustre
10491 * \param[in] fname filename (if mtnpath is NULL)
10492 * or FID (if mntpath is set)
10493 * \param[in] last_dev pointer to last device id used
10495 * \retval 0 on success
10496 * \retval CMD_HELP or a negative errno on error
10498 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
10499 const char *mntpath, const char *fname,
10502 struct hsm_user_item *hui = &hur->hur_user_item[idx];
10505 hui->hui_extent.length = -1;
10508 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
10510 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
10513 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
10517 hur->hur_request.hr_itemcount++;
10522 static int lfs_hsm_request(int argc, char **argv, int action)
10524 struct option long_opts[] = {
10525 { .val = 'a', .name = "archive", .has_arg = required_argument },
10526 { .val = 'D', .name = "data", .has_arg = required_argument },
10527 { .val = 'h', .name = "help", .has_arg = no_argument },
10528 { .val = 'l', .name = "filelist", .has_arg = required_argument },
10529 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
10530 { .name = NULL } };
10531 dev_t last_dev = 0;
10532 struct hsm_user_request *hur, *oldhur;
10537 char *filelist = NULL;
10538 char fullpath[PATH_MAX];
10539 char *opaque = NULL;
10540 int opaque_len = 0;
10541 int archive_id = 0;
10543 int nbfile_alloc = 0;
10544 char *some_file = NULL;
10545 char *mntpath = NULL;
10553 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
10554 long_opts, NULL)) != -1) {
10563 if (action != HUA_ARCHIVE &&
10564 action != HUA_REMOVE) {
10566 "error: -a is supported only when archiving or removing\n");
10570 archive_id = atoi(optarg);
10575 some_file = strdup(optarg);
10579 fprintf(stderr, "%s: unrecognized option '%s'\n",
10580 progname, argv[optind - 1]);
10588 /* All remaining args are files, so we have at least nbfile */
10589 nbfile = argc - optind;
10591 if ((nbfile == 0) && (!filelist)) {
10597 opaque_len = strlen(opaque);
10600 * Alloc the request structure with enough place to store all files
10601 * from command line.
10603 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
10605 fprintf(stderr, "Cannot create the request: %s\n",
10610 nbfile_alloc = nbfile;
10612 hur->hur_request.hr_action = action;
10613 hur->hur_request.hr_archive_id = archive_id;
10614 hur->hur_request.hr_flags = 0;
10616 /* All remaining args are files, add them */
10617 if (nbfile != 0 && some_file == NULL)
10618 some_file = strdup(argv[optind]);
10620 for (i = 0; i < nbfile; i++) {
10621 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
10627 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
10629 /* If a filelist was specified, read the filelist from it. */
10631 fp = fopen(filelist, "r");
10633 fprintf(stderr, "Cannot read the file list %s: %s\n",
10634 filelist, strerror(errno));
10639 while ((rc = getline(&line, &len, fp)) != -1) {
10641 * If allocated buffer was too small, get something
10644 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
10647 nbfile_alloc = nbfile_alloc * 2 + 1;
10649 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
10653 "hsm: cannot allocate the request: %s\n",
10660 size = hur_len(oldhur);
10663 "hsm: cannot allocate %u files + %u bytes data\n",
10664 oldhur->hur_request.hr_itemcount,
10665 oldhur->hur_request.hr_data_len);
10672 memcpy(hur, oldhur, size);
10677 if (line[strlen(line) - 1] == '\n')
10678 line[strlen(line) - 1] = '\0';
10680 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
10681 mntpath, line, &last_dev);
10697 /* If a --data was used, add it to the request */
10698 hur->hur_request.hr_data_len = opaque_len;
10700 memcpy(hur_data(hur), opaque, opaque_len);
10702 /* Send the HSM request */
10703 if (realpath(some_file, fullpath) == NULL) {
10704 fprintf(stderr, "Could not find path '%s': %s\n",
10705 some_file, strerror(errno));
10707 rc = llapi_hsm_request(fullpath, hur);
10709 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
10710 some_file, strerror(-rc));
10720 static int lfs_hsm_archive(int argc, char **argv)
10722 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
10725 static int lfs_hsm_restore(int argc, char **argv)
10727 return lfs_hsm_request(argc, argv, HUA_RESTORE);
10730 static int lfs_hsm_release(int argc, char **argv)
10732 return lfs_hsm_request(argc, argv, HUA_RELEASE);
10735 static int lfs_hsm_remove(int argc, char **argv)
10737 return lfs_hsm_request(argc, argv, HUA_REMOVE);
10740 static int lfs_hsm_cancel(int argc, char **argv)
10742 return lfs_hsm_request(argc, argv, HUA_CANCEL);
10745 static int lfs_swap_layouts(int argc, char **argv)
10749 if (argc == 4 && !strcmp(argv[1], "-n"))
10751 else if (argc != 3)
10754 return llapi_swap_layouts(argv[1+noxtime], argv[2+noxtime],
10755 0, 0, noxtime ? 0 :
10756 (SWAP_LAYOUTS_KEEP_MTIME |
10757 SWAP_LAYOUTS_KEEP_ATIME));
10760 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10762 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10764 int lfs_get_mode(const char *string)
10766 enum lock_mode_user mode;
10768 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10769 if (lock_mode_names[mode] == NULL)
10771 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10778 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10780 enum lu_ladvise_type advice;
10783 advice < ARRAY_SIZE(ladvise_names); advice++) {
10784 if (ladvise_names[advice] == NULL)
10786 if (strcmp(string, ladvise_names[advice]) == 0)
10790 return LU_LADVISE_INVALID;
10793 static int lfs_ladvise(int argc, char **argv)
10795 struct option long_opts[] = {
10796 { .val = 'a', .name = "advice", .has_arg = required_argument },
10797 { .val = 'b', .name = "background", .has_arg = no_argument },
10798 { .val = 'e', .name = "end", .has_arg = required_argument },
10799 { .val = 'h', .name = "help", .has_arg = no_argument },
10800 { .val = 'l', .name = "length", .has_arg = required_argument },
10801 { .val = 'm', .name = "mode", .has_arg = required_argument },
10802 { .val = 's', .name = "start", .has_arg = required_argument },
10803 { .val = 'u', .name = "unset", .has_arg = no_argument },
10804 { .name = NULL } };
10805 struct llapi_lu_ladvise advice;
10806 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10807 unsigned long long start = 0;
10808 unsigned long long end = LUSTRE_EOF;
10809 unsigned long long length = 0;
10810 unsigned long long size_units;
10811 unsigned long long flags = 0;
10817 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10818 long_opts, NULL)) != -1) {
10821 advice_type = lfs_get_ladvice(optarg);
10822 if (advice_type == LU_LADVISE_INVALID) {
10824 "%s: invalid advice type '%s'\n",
10826 fprintf(stderr, "Valid types:");
10828 for (advice_type = 0;
10829 advice_type < ARRAY_SIZE(ladvise_names);
10831 if (ladvise_names[advice_type] == NULL)
10833 fprintf(stderr, " %s",
10834 ladvise_names[advice_type]);
10836 fprintf(stderr, "\n");
10849 rc = llapi_parse_size(optarg, &end,
10852 fprintf(stderr, "%s: bad end offset '%s'\n",
10859 rc = llapi_parse_size(optarg, &start,
10863 "%s: bad start offset '%s'\n",
10870 rc = llapi_parse_size(optarg, &length,
10873 fprintf(stderr, "%s: bad length '%s'\n",
10879 mode = lfs_get_mode(optarg);
10882 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10888 fprintf(stderr, "%s: unrecognized option '%s'\n",
10889 progname, argv[optind - 1]);
10896 if (advice_type == LU_LADVISE_INVALID) {
10897 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10898 fprintf(stderr, "Valid types:");
10899 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10901 if (ladvise_names[advice_type] == NULL)
10903 fprintf(stderr, " %s", ladvise_names[advice_type]);
10905 fprintf(stderr, "\n");
10909 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10911 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10916 if (argc <= optind) {
10917 fprintf(stderr, "%s: please give one or more file names\n",
10922 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10923 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10928 if (end == LUSTRE_EOF && length != 0)
10929 end = start + length;
10931 if (end <= start) {
10932 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10933 argv[0], start, end);
10937 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10938 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10943 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10944 fprintf(stderr, "%s: mode is required with lockahead\n",
10949 while (optind < argc) {
10952 path = argv[optind++];
10954 fd = open(path, O_RDONLY);
10957 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10958 argv[0], path, strerror(-rc2));
10964 advice.lla_start = start;
10965 advice.lla_end = end;
10966 advice.lla_advice = advice_type;
10967 advice.lla_value1 = 0;
10968 advice.lla_value2 = 0;
10969 advice.lla_value3 = 0;
10970 advice.lla_value4 = 0;
10971 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10972 advice.lla_lockahead_mode = mode;
10973 advice.lla_peradvice_flags = flags;
10976 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10980 "%s: cannot give advice '%s' to file '%s': %s\n",
10981 argv[0], ladvise_names[advice_type],
10982 path, strerror(errno));
10993 static const char *const heat_names[] = LU_HEAT_NAMES;
10995 static int lfs_heat_get(int argc, char **argv)
10997 struct lu_heat *heat;
11006 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
11008 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
11013 while (optind < argc) {
11014 path = argv[optind++];
11016 fd = open(path, O_RDONLY);
11018 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11019 argv[0], path, strerror(errno));
11024 heat->lh_count = OBD_HEAT_COUNT;
11025 rc2 = llapi_heat_get(fd, heat);
11029 "%s: cannot get heat of file '%s': %s\n",
11030 argv[0], path, strerror(errno));
11034 printf("flags: %x\n", heat->lh_flags);
11035 for (i = 0; i < heat->lh_count; i++)
11036 printf("%s: %llu\n", heat_names[i],
11037 (unsigned long long)heat->lh_heat[i]);
11039 if (rc == 0 && rc2 < 0)
11047 static int lfs_heat_set(int argc, char **argv)
11049 struct option long_opts[] = {
11050 { .val = 'c', .name = "clear", .has_arg = no_argument },
11051 { .val = 'h', .name = "help", .has_arg = no_argument },
11052 { .val = 'o', .name = "off", .has_arg = no_argument },
11053 { .val = 'O', .name = "on", .has_arg = no_argument },
11054 { .name = NULL } };
11055 enum lu_heat_flag flags = 0;
11065 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
11068 flags |= LU_HEAT_FLAG_CLEAR;
11071 flags |= LU_HEAT_FLAG_CLEAR;
11072 flags |= LU_HEAT_FLAG_OFF;
11075 flags &= ~LU_HEAT_FLAG_OFF;
11078 fprintf(stderr, "%s: unrecognized option '%s'\n",
11079 progname, argv[optind - 1]);
11086 if (argc <= optind) {
11087 fprintf(stderr, "%s: please give one or more file names\n",
11092 while (optind < argc) {
11093 path = argv[optind++];
11095 fd = open(path, O_RDONLY);
11097 fprintf(stderr, "%s: cannot open file '%s': %s\n",
11098 argv[0], path, strerror(errno));
11103 rc2 = llapi_heat_set(fd, flags);
11107 "%s: cannot setflags heat of file '%s': %s\n",
11108 argv[0], path, strerror(errno));
11112 if (rc == 0 && rc2 < 0)
11119 * The input string contains a comma delimited list of component ids and
11120 * ranges, for example "1,2-4,7".
11122 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
11124 bool end_of_loop = false;
11132 while (!end_of_loop) {
11136 char *endptr = NULL;
11139 ptr = strchrnul(arg, ',');
11140 end_of_loop = *ptr == '\0';
11143 start_index = strtol(arg, &endptr, 0);
11144 if (endptr == arg) /* no data at all */
11146 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
11148 if (start_index < 0)
11151 end_index = start_index;
11152 if (*endptr == '-') {
11153 end_index = strtol(endptr + 1, &endptr, 0);
11154 if (*endptr != '\0')
11156 if (end_index < start_index)
11160 for (i = start_index; i <= end_index && size > 0; i++) {
11163 /* remove duplicate */
11164 for (j = 0; j < nr; j++) {
11168 if (j == nr) { /* no duplicate */
11174 if (size == 0 && i < end_index)
11181 if (!end_of_loop && ptr)
11184 return rc < 0 ? rc : nr;
11188 * struct verify_mirror_id - Mirror id to be verified.
11189 * @mirror_id: A specified mirror id.
11190 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
11192 struct verify_mirror_id {
11198 * compare_mirror_ids() - Compare mirror ids.
11199 * @layout: Mirror component list.
11200 * @cbdata: Callback data in verify_mirror_id structure.
11202 * This is a callback function called by llapi_layout_comp_iterate()
11203 * to compare the specified mirror id with the one in the current
11204 * component of @layout. If they are the same, then the specified
11205 * mirror id is valid.
11207 * Return: a negative error code on failure or
11208 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
11209 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
11212 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
11214 struct verify_mirror_id *mirror_id_cbdata =
11215 (struct verify_mirror_id *)cbdata;
11216 uint32_t mirror_id;
11219 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11223 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11224 progname, strerror(errno));
11228 if (mirror_id_cbdata->mirror_id == mirror_id) {
11229 mirror_id_cbdata->is_valid_id = true;
11230 return LLAPI_LAYOUT_ITER_STOP;
11233 return LLAPI_LAYOUT_ITER_CONT;
11237 * verify_mirror_ids() - Verify specified mirror ids.
11238 * @fname: Mirrored file name.
11239 * @mirror_ids: Specified mirror ids to be verified.
11240 * @ids_nr: Number of specified mirror ids.
11242 * This function verifies that specified @mirror_ids are valid
11243 * in the mirrored file @fname.
11245 * Return: 0 on success or a negative error code on failure.
11248 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
11250 struct llapi_layout *layout = NULL;
11251 struct verify_mirror_id mirror_id_cbdata = { 0 };
11253 uint32_t flr_state;
11262 if (stat(fname, &stbuf) < 0) {
11263 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11264 progname, fname, strerror(errno));
11269 if (!S_ISREG(stbuf.st_mode)) {
11270 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11276 fd = open(fname, O_DIRECT | O_RDONLY);
11278 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11279 progname, fname, strerror(errno));
11284 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11286 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11287 progname, fname, strerror(errno));
11291 layout = llapi_layout_get_by_fd(fd, 0);
11293 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11294 progname, fname, strerror(errno));
11296 llapi_lease_release(fd);
11300 rc = llapi_layout_flags_get(layout, &flr_state);
11302 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11303 progname, fname, strerror(errno));
11308 flr_state &= LCM_FL_FLR_MASK;
11309 switch (flr_state) {
11312 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11313 progname, fname, llapi_layout_flags_string(flr_state));
11320 for (i = 0; i < ids_nr; i++) {
11321 mirror_id_cbdata.mirror_id = mirror_ids[i];
11322 mirror_id_cbdata.is_valid_id = false;
11324 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
11325 &mirror_id_cbdata);
11329 "%s: '%s' failed to verify mirror id: %u.\n",
11330 progname, fname, mirror_ids[i]);
11334 if (!mirror_id_cbdata.is_valid_id) {
11337 "%s: '%s' invalid specified mirror id: %u.\n",
11338 progname, fname, mirror_ids[i]);
11344 llapi_layout_free(layout);
11345 llapi_lease_release(fd);
11353 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
11354 __u16 *mirror_ids, int ids_nr,
11355 long stats_interval_sec, long bandwidth_bytes_sec)
11357 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11358 struct llapi_layout *layout;
11360 uint32_t flr_state;
11369 if (stat(fname, &stbuf) < 0) {
11370 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11371 progname, fname, strerror(errno));
11375 if (!S_ISREG(stbuf.st_mode)) {
11376 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11382 /* Allow mirror resync even without the key on encrypted files */
11383 fd = open(fname, O_DIRECT | O_RDWR | O_CIPHERTEXT);
11385 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11386 progname, fname, strerror(errno));
11391 layout = llapi_layout_get_by_fd(fd, 0);
11393 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11394 progname, fname, strerror(errno));
11399 rc = llapi_layout_flags_get(layout, &flr_state);
11401 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11402 progname, fname, strerror(errno));
11407 flr_state &= LCM_FL_FLR_MASK;
11408 if (flr_state == LCM_FL_NONE) {
11410 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
11415 /* get stale component info */
11416 comp_size = llapi_mirror_find_stale(layout, comp_array,
11417 ARRAY_SIZE(comp_array),
11418 mirror_ids, ids_nr);
11419 if (comp_size <= 0) {
11424 ioc->lil_mode = LL_LEASE_WRLCK;
11425 ioc->lil_flags = LL_LEASE_RESYNC;
11426 rc = llapi_lease_set(fd, ioc);
11428 if (rc == -EALREADY)
11432 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
11433 progname, fname, strerror(-rc));
11437 /* get the read range [start, end) */
11438 start = comp_array[0].lrc_start;
11439 end = comp_array[0].lrc_end;
11440 for (idx = 1; idx < comp_size; idx++) {
11441 if (comp_array[idx].lrc_start < start)
11442 start = comp_array[idx].lrc_start;
11443 if (end < comp_array[idx].lrc_end)
11444 end = comp_array[idx].lrc_end;
11447 rc = llapi_lease_check(fd);
11448 if (rc != LL_LEASE_WRLCK) {
11449 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11454 rc = llapi_mirror_resync_many_params(fd, layout, comp_array, comp_size,
11455 start, end, stats_interval_sec,
11456 bandwidth_bytes_sec);
11458 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
11459 progname, fname, strerror(-rc));
11461 rc2 = migrate_set_timestamps(fd, &stbuf);
11463 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
11464 progname, fname, strerror(-rc2));
11470 /* need to do the lease unlock even resync fails */
11471 ioc->lil_mode = LL_LEASE_UNLCK;
11472 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11473 ioc->lil_count = 0;
11474 for (idx = 0; idx < comp_size; idx++) {
11475 if (comp_array[idx].lrc_synced) {
11476 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
11481 rc2 = llapi_lease_set(fd, ioc);
11483 * llapi_lease_set returns lease mode when it request to unlock
11487 /* rc2 == 0 means lost lease lock */
11488 if (rc2 == 0 && rc == 0)
11492 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
11494 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
11496 llapi_lease_release(fd);
11501 llapi_layout_free(layout);
11508 static inline int lfs_mirror_resync(int argc, char **argv)
11510 struct option long_opts[] = {
11511 { .val = 'h', .name = "help", .has_arg = no_argument },
11512 { .val = 'o', .name = "only", .has_arg = required_argument },
11513 { .val = 'W', .name = "bandwidth", .has_arg = required_argument },
11514 { .val = LFS_STATS_OPT,
11515 .name = "stats", .has_arg = no_argument},
11516 { .val = LFS_STATS_INTERVAL_OPT,
11517 .name = "stats-interval",
11518 .has_arg = required_argument},
11519 { .name = NULL } };
11520 struct ll_ioc_lease *ioc = NULL;
11521 __u16 mirror_ids[128] = { 0 };
11522 unsigned int stats_interval_sec = 0;
11523 unsigned long long bandwidth_bytes_sec = 0;
11524 unsigned long long bandwidth_unit = ONE_MB;
11529 while ((c = getopt_long(argc, argv, "ho:W:", long_opts, NULL)) >= 0) {
11533 rc = parse_mirror_ids(mirror_ids,
11534 sizeof(mirror_ids) / sizeof(__u16),
11538 "%s: bad mirror ids '%s'.\n",
11545 if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
11546 &bandwidth_unit, 0) < 0) {
11548 "error: %s: bad value for bandwidth '%s'\n",
11553 case LFS_STATS_OPT:
11554 stats_interval_sec = 5;
11556 case LFS_STATS_INTERVAL_OPT:
11557 stats_interval_sec = strtol(optarg, &end, 0);
11560 fprintf(stderr, "%s: unrecognized option '%s'\n",
11561 progname, argv[optind - 1]);
11569 if (argc == optind) {
11570 fprintf(stderr, "%s: no file name given.\n", argv[0]);
11575 if (ids_nr > 0 && argc > optind + 1) {
11577 "%s: option '--only' cannot be used upon multiple files.\n",
11584 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11589 /* set the lease on the file */
11590 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11592 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
11593 argv[0], strerror(errno));
11598 for (; optind < argc; optind++) {
11599 rc = lfs_mirror_resync_file(argv[optind], ioc,
11600 mirror_ids, ids_nr,
11601 stats_interval_sec,
11602 bandwidth_bytes_sec);
11603 /* ignore previous file's error, continue with next file */
11606 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
11614 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
11616 struct llapi_layout *layout;
11619 layout = llapi_layout_get_by_fd(fd, 0);
11621 fprintf(stderr, "could not get layout.\n");
11625 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
11627 fprintf(stderr, "failed to iterate layout\n");
11628 llapi_layout_free(layout);
11631 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
11632 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
11633 llapi_layout_free(layout);
11637 llapi_layout_free(layout);
11643 * Check whether two files are the same file
11644 * \retval 0 same file
11645 * \retval 1 not the same file
11646 * \retval <0 error code
11648 static inline int check_same_file(int fd, const char *f2)
11650 struct stat stbuf1;
11651 struct stat stbuf2;
11653 if (fstat(fd, &stbuf1) < 0)
11656 if (stat(f2, &stbuf2) < 0)
11659 if (stbuf1.st_rdev == stbuf2.st_rdev &&
11660 stbuf1.st_ino == stbuf2.st_ino)
11666 static inline int lfs_mirror_read(int argc, char **argv)
11669 __u16 mirror_id = 0;
11670 const char *outfile = NULL;
11676 const size_t buflen = 4 << 20;
11678 struct option long_opts[] = {
11679 { .val = 'h', .name = "help", .has_arg = no_argument },
11680 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11681 { .val = 'o', .name = "outfile", .has_arg = required_argument },
11682 { .name = NULL } };
11684 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
11689 unsigned long int id;
11692 id = strtoul(optarg, &end, 0);
11693 if (errno != 0 || *end != '\0' || id == 0 ||
11696 "%s %s: invalid mirror ID '%s'\n",
11697 progname, argv[0], optarg);
11701 mirror_id = (__u16)id;
11708 fprintf(stderr, "%s: unrecognized option '%s'\n",
11709 progname, argv[optind - 1]);
11716 if (argc == optind) {
11717 fprintf(stderr, "%s %s: no mirrored file provided\n",
11718 progname, argv[0]);
11720 } else if (argc > optind + 1) {
11721 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11725 if (mirror_id == 0) {
11726 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11727 progname, argv[0]);
11731 /* open mirror file */
11732 fname = argv[optind];
11733 fd = open(fname, O_DIRECT | O_RDONLY);
11735 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11736 progname, argv[0], fname, strerror(errno));
11740 /* verify mirror id */
11741 rc = verify_mirror_id_by_fd(fd, mirror_id);
11744 "%s %s: cannot find mirror with ID %u in '%s'\n",
11745 progname, argv[0], mirror_id, fname);
11749 /* open output file - O_EXCL ensures output is not the same as input */
11751 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
11753 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
11754 progname, argv[0], outfile, strerror(errno));
11759 outfd = STDOUT_FILENO;
11762 /* allocate buffer */
11763 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
11765 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11766 progname, argv[0], rc);
11772 ssize_t bytes_read;
11773 ssize_t written = 0;
11775 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
11776 if (bytes_read < 0) {
11779 "%s %s: fail to read data from mirror %u: %s\n",
11780 progname, argv[0], mirror_id, strerror(-rc));
11785 if (bytes_read == 0)
11788 while (written < bytes_read) {
11791 written2 = write(outfd, buf + written,
11792 bytes_read - written);
11793 if (written2 < 0) {
11795 "%s %s: fail to write %s: %s\n",
11796 progname, argv[0], outfile ? : "STDOUT",
11801 written += written2;
11804 if (written != bytes_read) {
11806 "%s %s: written %ld bytes does not match with %ld read.\n",
11807 progname, argv[0], written, bytes_read);
11829 static inline int lfs_mirror_write(int argc, char **argv)
11832 __u16 mirror_id = 0;
11833 const char *inputfile = NULL;
11839 const size_t buflen = 4 << 20;
11841 size_t page_size = sysconf(_SC_PAGESIZE);
11842 struct ll_ioc_lease_id ioc;
11843 struct option long_opts[] = {
11844 { .val = 'h', .name = "help", .has_arg = no_argument },
11845 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11846 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11847 { .name = NULL } };
11849 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11854 unsigned long int id;
11857 id = strtoul(optarg, &end, 0);
11858 if (errno != 0 || *end != '\0' || id == 0 ||
11861 "%s %s: invalid mirror ID '%s'\n",
11862 progname, argv[0], optarg);
11866 mirror_id = (__u16)id;
11870 inputfile = optarg;
11873 fprintf(stderr, "%s: unrecognized option '%s'\n",
11874 progname, argv[optind - 1]);
11881 if (argc == optind) {
11882 fprintf(stderr, "%s %s: no mirrored file provided\n",
11883 progname, argv[0]);
11885 } else if (argc > optind + 1) {
11886 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11890 if (mirror_id == 0) {
11891 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11892 progname, argv[0]);
11896 /* open mirror file */
11897 fname = argv[optind];
11898 fd = open(fname, O_DIRECT | O_WRONLY);
11900 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11901 progname, argv[0], fname, strerror(errno));
11905 /* verify mirror id */
11906 rc = verify_mirror_id_by_fd(fd, mirror_id);
11909 "%s %s: cannot find mirror with ID %u in '%s'\n",
11910 progname, argv[0], mirror_id, fname);
11914 /* open input file */
11916 rc = check_same_file(fd, inputfile);
11919 "%s %s: input file cannot be the mirrored file '%s'\n",
11920 progname, argv[0], fname);
11926 inputfd = open(inputfile, O_RDONLY, 0644);
11928 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11929 progname, argv[0], inputfile, strerror(errno));
11934 inputfd = STDIN_FILENO;
11937 /* allocate buffer */
11938 rc = posix_memalign(&buf, page_size, buflen);
11940 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11941 progname, argv[0], rc);
11942 goto close_inputfd;
11945 /* prepare target mirror components instantiation */
11946 ioc.lil_mode = LL_LEASE_WRLCK;
11947 ioc.lil_flags = LL_LEASE_RESYNC;
11948 ioc.lil_mirror_id = mirror_id;
11949 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11952 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11953 progname, argv[0], fname, strerror(errno));
11959 ssize_t bytes_read;
11963 rc = llapi_lease_check(fd);
11964 if (rc != LL_LEASE_WRLCK) {
11965 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11966 progname, argv[0], fname);
11970 bytes_read = read(inputfd, buf, buflen);
11971 if (bytes_read < 0) {
11974 "%s %s: fail to read data from '%s': %s\n",
11975 progname, argv[0], inputfile ? : "STDIN",
11982 if (bytes_read == 0)
11985 /* round up to page align to make direct IO happy. */
11986 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11988 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11993 "%s %s: fail to write to mirror %u: %s\n",
11994 progname, argv[0], mirror_id,
12002 if (pos & (page_size - 1)) {
12003 rc = llapi_mirror_truncate(fd, mirror_id, pos);
12008 ioc.lil_mode = LL_LEASE_UNLCK;
12009 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
12011 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
12016 "%s %s: release lease lock of '%s' failed: %s\n",
12017 progname, argv[0], fname, strerror(-rc));
12034 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
12036 struct llapi_layout *layout;
12037 struct collect_ids_data cid = { .cid_ids = ids,
12039 .cid_exclude = exclude_id, };
12042 layout = llapi_layout_get_by_fd(fd, 0);
12044 fprintf(stderr, "could not get layout\n");
12048 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
12050 fprintf(stderr, "failed to iterate layout\n");
12051 llapi_layout_free(layout);
12055 llapi_layout_free(layout);
12057 return cid.cid_count;
12060 #ifndef MIRROR_ID_NEG
12061 #define MIRROR_ID_NEG 0x8000
12064 static inline int lfs_mirror_copy(int argc, char **argv)
12067 __u16 read_mirror_id = 0;
12068 __u16 ids[128] = { 0 };
12070 struct llapi_layout *layout = NULL;
12071 struct llapi_resync_comp comp_array[1024] = { { 0 } };
12078 struct ll_ioc_lease *ioc = NULL;
12079 struct ll_ioc_lease_id *resync_ioc;
12080 struct option long_opts[] = {
12081 { .val = 'h', .name = "help", .has_arg = no_argument },
12082 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
12083 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
12084 { .name = NULL } };
12085 char cmd[PATH_MAX];
12087 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12089 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
12094 unsigned long int id;
12097 id = strtoul(optarg, &end, 0);
12098 if (errno != 0 || *end != '\0' || id == 0 ||
12101 "%s: invalid read mirror ID '%s'\n",
12106 read_mirror_id = (__u16)id;
12110 if (!strcmp(optarg, "-1")) {
12111 /* specify all other mirrors */
12112 ids[0] = (__u16)-1;
12115 count = parse_mirror_ids((__u16 *)ids,
12123 fprintf(stderr, "%s: unrecognized option '%s'\n",
12124 progname, argv[optind - 1]);
12131 if (argc == optind) {
12132 fprintf(stderr, "%s %s: no mirrored file provided\n",
12133 progname, argv[0]);
12135 } else if (argc > optind + 1) {
12136 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
12140 if (read_mirror_id == 0) {
12142 "%s %s: no valid read mirror ID %d is provided\n",
12143 progname, argv[0], read_mirror_id);
12149 "%s %s: no write mirror ID is provided\n",
12150 progname, argv[0]);
12154 for (i = 0; i < count; i++) {
12155 if (read_mirror_id == ids[i]) {
12157 "%s %s: read and write mirror ID cannot be the same\n",
12158 progname, argv[0]);
12163 /* open mirror file */
12164 fname = argv[optind];
12166 fd = open(fname, O_DIRECT | O_RDWR);
12168 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
12169 progname, argv[0], fname, strerror(errno));
12173 /* write to all other mirrors */
12174 if (ids[0] == (__u16)-1) {
12175 count = get_other_mirror_ids(fd, ids, read_mirror_id);
12179 "%s %s: failed to get other mirror ids in '%s': %d\n",
12180 progname, argv[0], fname, rc);
12185 /* verify mirror id */
12186 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
12189 "%s %s: cannot find mirror with ID %u in '%s'\n",
12190 progname, argv[0], read_mirror_id, fname);
12194 for (i = 0; i < count; i++) {
12195 rc = verify_mirror_id_by_fd(fd, ids[i]);
12198 "%s %s: cannot find mirror with ID %u in '%s'\n",
12199 progname, argv[0], ids[i], fname);
12204 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
12207 "%s %s: cannot alloc comp id array for ioc: %s\n",
12208 progname, argv[0], strerror(errno));
12213 /* get stale component info */
12214 layout = llapi_layout_get_by_fd(fd, 0);
12216 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
12217 progname, argv[0], fname, strerror(errno));
12221 comp_size = llapi_mirror_find_stale(layout, comp_array,
12222 ARRAY_SIZE(comp_array),
12224 llapi_layout_free(layout);
12225 if (comp_size < 0) {
12230 /* prepare target mirror components instantiation */
12231 resync_ioc = (struct ll_ioc_lease_id *)ioc;
12232 resync_ioc->lil_mode = LL_LEASE_WRLCK;
12233 resync_ioc->lil_flags = LL_LEASE_RESYNC;
12235 resync_ioc->lil_mirror_id = ids[0];
12237 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
12238 rc = llapi_lease_set(fd, ioc);
12241 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
12242 progname, argv[0], fname, strerror(errno));
12246 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
12249 fprintf(stderr, "%s %s: copy error: %d\n",
12250 progname, argv[0], rc);
12254 fprintf(stdout, "mirror copied successfully: ");
12255 for (i = 0; i < copied; i++)
12256 fprintf(stdout, "%d ", ids[i]);
12257 fprintf(stdout, "\n");
12259 ioc->lil_mode = LL_LEASE_UNLCK;
12260 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
12261 ioc->lil_count = 0;
12262 for (i = 0; i < comp_size; i++) {
12265 for (j = 0; j < copied; j++) {
12266 if (comp_array[i].lrc_mirror_id != ids[j])
12269 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
12273 rc = llapi_lease_set(fd, ioc);
12278 "%s %s: release lease lock of '%s' failed: %s\n",
12279 progname, argv[0], fname, strerror(errno));
12294 * struct verify_chunk - Mirror chunk to be verified.
12295 * @chunk: [start, end) of the chunk.
12296 * @mirror_count: Number of mirror ids in @mirror_id array.
12297 * @mirror_id: Array of valid mirror ids that cover the chunk.
12299 struct verify_chunk {
12300 struct lu_extent chunk;
12301 unsigned int mirror_count;
12302 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
12306 * print_chunks() - Print chunk information.
12307 * @fname: Mirrored file name.
12308 * @chunks: Array of chunks.
12309 * @chunk_count: Number of chunks in @chunks array.
12311 * This function prints [start, end) of each chunk in @chunks
12312 * for mirrored file @fname, and also prints the valid mirror ids
12313 * that cover the chunk.
12318 void print_chunks(const char *fname, struct verify_chunk *chunks,
12324 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
12325 for (i = 0; i < chunk_count; i++) {
12326 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
12328 if (chunks[i].mirror_count == 0)
12329 fprintf(stdout, "\t[");
12331 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
12332 for (j = 1; j < chunks[i].mirror_count; j++)
12333 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
12335 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
12337 fprintf(stdout, "\n");
12341 * print_checksums() - Print CRC-32 checksum values.
12342 * @chunk: A chunk and its corresponding valid mirror ids.
12343 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
12345 * This function prints CRC-32 checksum values on @chunk for
12346 * each valid mirror that covers it.
12351 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
12352 unsigned long long pos, unsigned long long len)
12357 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
12358 for (i = 0; i < chunk->mirror_count; i++)
12359 fprintf(stdout, "Mirror %u:\t%#lx\n",
12360 chunk->mirror_id[i], crc[i]);
12361 fprintf(stdout, "\n");
12365 * filter_mirror_id() - Filter specified mirror ids.
12366 * @chunks: Array of chunks.
12367 * @chunk_count: Number of chunks in @chunks array.
12368 * @mirror_ids: Specified mirror ids to be verified.
12369 * @ids_nr: Number of specified mirror ids.
12371 * This function scans valid mirror ids that cover each chunk in @chunks
12372 * and filters specified mirror ids.
12377 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
12378 __u16 *mirror_ids, int ids_nr)
12383 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12384 unsigned int valid_count = 0;
12386 for (i = 0; i < chunk_count; i++) {
12387 if (chunks[i].mirror_count == 0)
12391 for (j = 0; j < ids_nr; j++) {
12392 for (k = 0; k < chunks[i].mirror_count; k++) {
12393 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
12394 valid_id[valid_count] = mirror_ids[j];
12401 memcpy(chunks[i].mirror_id, valid_id,
12402 sizeof(__u16) * valid_count);
12403 chunks[i].mirror_count = valid_count;
12408 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
12409 * @layout: Mirror component list.
12410 * @chunks: Array of chunks.
12411 * @chunks_size: Array size of @chunks.
12413 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
12414 * to find out chunk segments and store them in @chunks array.
12416 * The @mirror_id array in each element of @chunks will store the valid
12417 * mirror ids that cover the chunk. If a mirror component covering the
12418 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
12419 * will not be stored into the @mirror_id array, and the chunk for that
12420 * mirror will not be verified.
12422 * The @mirror_count in each element of @chunks will store the number of
12423 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
12424 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
12425 * indicates the chunk is valid in only one mirror. In both cases, the
12426 * chunk will not be verified.
12428 * Here is an example:
12430 * 0 1M 2M 3M 4M EOF
12431 * +------+-------------+--------------------+
12432 * | | | S | mirror1
12433 * +------+------+------+------+-------------+
12434 * | | S | S | | mirror2
12435 * +-------------+------+------+-------------+
12437 * prepared @chunks array will contain 5 elements:
12438 * (([0, 1M), [1, 2], 2),
12439 * ([1M, 2M), [1, 2], 2),
12440 * ([2M, 3M), [1], 1),
12441 * ([3M, 4M], [], 0),
12442 * ([4M, EOF), [2], 1))
12444 * Return: the actual array size of @chunks on success
12445 * or a negative error code on failure.
12448 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
12449 struct verify_chunk *chunks,
12450 size_t chunks_size)
12454 uint32_t mirror_id;
12460 memset(chunks, 0, sizeof(*chunks) * chunks_size);
12463 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
12466 "%s: move to the first layout component: %s.\n",
12467 progname, strerror(errno));
12473 chunks[idx].chunk.e_end = LUSTRE_EOF;
12475 rc = llapi_layout_comp_extent_get(layout, &start, &end);
12478 "%s: llapi_layout_comp_extent_get failed: %s.\n",
12479 progname, strerror(errno));
12483 if (start > chunks[idx].chunk.e_start ||
12484 end <= chunks[idx].chunk.e_start)
12487 if (end < chunks[idx].chunk.e_end)
12488 chunks[idx].chunk.e_end = end;
12490 rc = llapi_layout_comp_flags_get(layout, &flags);
12493 "%s: llapi_layout_comp_flags_get failed: %s.\n",
12494 progname, strerror(errno));
12498 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
12501 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
12504 "%s: llapi_layout_mirror_id_get failed: %s.\n",
12505 progname, strerror(errno));
12509 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
12511 "%s: mirror_id array is too small.\n",
12516 chunks[idx].mirror_id[i] = mirror_id;
12520 rc = llapi_layout_comp_use(layout,
12521 LLAPI_LAYOUT_COMP_USE_NEXT);
12524 "%s: move to the next layout component: %s.\n",
12525 progname, strerror(errno));
12528 } /* loop through all components */
12530 chunks[idx].mirror_count = i;
12532 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
12536 if (idx >= chunks_size) {
12537 fprintf(stderr, "%s: chunks array is too small.\n",
12543 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
12547 return rc < 0 ? rc : idx + 1;
12551 * lfs_mirror_verify_chunk() - Verify a chunk.
12552 * @fd: File descriptor of the mirrored file.
12553 * @file_size: Size of the mirrored file.
12554 * @chunk: A chunk and its corresponding valid mirror ids.
12555 * @verbose: Verbose mode.
12557 * This function verifies a @chunk contains exactly the same data
12558 * ammong the mirrors that cover it.
12560 * If @verbose is specified, then the function will print where the
12561 * differences are if the data do not match. Otherwise, it will
12562 * just return an error in that case.
12564 * Return: 0 on success or a negative error code on failure.
12567 int lfs_mirror_verify_chunk(int fd, size_t file_size,
12568 struct verify_chunk *chunk, int verbose)
12570 const size_t buflen = 4 * 1024 * 1024; /* 4M */
12572 size_t page_size = sysconf(_SC_PAGESIZE);
12573 ssize_t bytes_read;
12574 ssize_t bytes_done;
12578 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12582 if (file_size == 0)
12585 rc = posix_memalign(&buf, page_size, buflen);
12586 if (rc) /* error code is returned directly */
12590 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12591 PEXT(&chunk->chunk));
12592 for (i = 0; i < chunk->mirror_count; i++)
12593 fprintf(stdout, " %u", chunk->mirror_id[i]);
12594 fprintf(stdout, "\n");
12598 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
12599 pos = chunk->chunk.e_start;
12600 while (bytes_done < count) {
12601 /* compute initial CRC-32 checksum */
12602 crc = crc32(0L, Z_NULL, 0);
12603 memset(crc_array, 0, sizeof(crc_array));
12606 for (i = 0; i < chunk->mirror_count; i++) {
12607 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
12609 if (bytes_read < 0) {
12612 "%s: failed to read data from mirror %u: %s.\n",
12613 progname, chunk->mirror_id[i],
12618 /* compute new CRC-32 checksum */
12619 crc_array[i] = crc32(crc, buf, bytes_read);
12623 print_checksums(chunk, crc_array, pos, buflen);
12625 /* compare CRC-32 checksum values */
12626 for (i = 1; i < chunk->mirror_count; i++) {
12627 if (crc_array[i] != crc_array[0]) {
12631 "%s: chunk "DEXT" has different checksum value on mirror %u:%lx and mirror %u:%lx.\n",
12632 progname, PEXT(&chunk->chunk),
12633 chunk->mirror_id[0], crc_array[0],
12634 chunk->mirror_id[i], crc_array[i]);
12635 print_checksums(chunk, crc_array, pos, buflen);
12640 bytes_done += bytes_read;
12643 if (verbose > 1 && rc == 0) {
12644 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12645 PEXT(&chunk->chunk));
12646 for (i = 0; i < chunk->mirror_count; i++)
12647 fprintf(stdout, " %u", chunk->mirror_id[i]);
12648 fprintf(stdout, " PASS\n\n");
12657 * lfs_mirror_verify_file() - Verify a mirrored file.
12658 * @fname: Mirrored file name.
12659 * @mirror_ids: Specified mirror ids to be verified.
12660 * @ids_nr: Number of specified mirror ids.
12661 * @verbose: Verbose mode.
12663 * This function verifies that each SYNC mirror of a mirrored file
12664 * specified by @fname contains exactly the same data.
12666 * If @mirror_ids is specified, then the function will verify the
12667 * mirrors specified by @mirror_ids contain exactly the same data.
12669 * If @verbose is specified, then the function will print where the
12670 * differences are if the data do not match. Otherwise, it will
12671 * just return an error in that case.
12673 * Return: 0 on success or a negative error code on failure.
12676 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
12679 struct verify_chunk chunks_array[1024] = { };
12680 struct llapi_layout *layout = NULL;
12682 uint32_t flr_state;
12684 int chunk_count = 0;
12690 if (stat(fname, &stbuf) < 0) {
12691 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
12692 progname, fname, strerror(errno));
12697 if (!S_ISREG(stbuf.st_mode)) {
12698 fprintf(stderr, "%s: '%s' is not a regular file.\n",
12704 if (stbuf.st_size == 0) {
12706 fprintf(stdout, "%s: '%s' file size is 0.\n",
12712 /* Allow mirror verify even without the key on encrypted files */
12713 fd = open(fname, O_DIRECT | O_RDONLY | O_CIPHERTEXT);
12715 fprintf(stderr, "%s: cannot open '%s': %s.\n",
12716 progname, fname, strerror(errno));
12721 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
12723 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
12724 progname, fname, strerror(errno));
12728 layout = llapi_layout_get_by_fd(fd, 0);
12730 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
12731 progname, fname, strerror(errno));
12733 llapi_lease_release(fd);
12737 rc = llapi_layout_flags_get(layout, &flr_state);
12739 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
12740 progname, fname, strerror(errno));
12745 flr_state &= LCM_FL_FLR_MASK;
12746 switch (flr_state) {
12749 fprintf(stderr, "%s: '%s' file state error: %s.\n",
12750 progname, fname, llapi_layout_flags_string(flr_state));
12756 /* find out mirror chunks to be verified */
12757 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
12758 ARRAY_SIZE(chunks_array));
12759 if (chunk_count < 0) {
12765 /* filter specified mirror ids */
12766 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
12769 print_chunks(fname, chunks_array, chunk_count);
12771 for (idx = 0; idx < chunk_count; idx++) {
12772 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
12775 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
12777 PEXT(&chunks_array[idx].chunk),
12778 (unsigned long long)stbuf.st_size);
12782 if (chunks_array[idx].mirror_count == 0) {
12784 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12786 PEXT(&chunks_array[idx].chunk));
12788 fprintf(stderr, "skipped\n");
12792 fprintf(stderr, "failed\n");
12796 if (chunks_array[idx].mirror_count == 1) {
12799 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12801 PEXT(&chunks_array[idx].chunk),
12802 chunks_array[idx].mirror_id[0]);
12806 rc = llapi_lease_check(fd);
12807 if (rc != LL_LEASE_RDLCK) {
12808 fprintf(stderr, "%s: '%s' lost lease lock.\n",
12813 /* verify one chunk */
12814 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12815 &chunks_array[idx], verbose);
12829 llapi_layout_free(layout);
12830 llapi_lease_release(fd);
12838 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12839 * @argc: The count of lfs mirror verify command line arguments.
12840 * @argv: Array of strings for lfs mirror verify command line arguments.
12842 * This function parses lfs mirror verify command and verifies the
12843 * specified mirrored file(s).
12845 * Return: 0 on success or a negative error code on failure.
12847 static inline int lfs_mirror_verify(int argc, char **argv)
12849 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12855 char cmd[PATH_MAX];
12857 struct option long_opts[] = {
12858 { .val = 'h', .name = "help", .has_arg = no_argument },
12859 { .val = 'o', .name = "only", .has_arg = required_argument },
12860 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12861 { .name = NULL } };
12863 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12865 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12868 rc = parse_mirror_ids(mirror_ids,
12869 ARRAY_SIZE(mirror_ids),
12873 "%s: bad mirror ids '%s'.\n",
12880 "%s: at least 2 mirror ids needed with '--only' option.\n",
12890 fprintf(stderr, "%s: unrecognized option '%s'\n",
12891 progname, argv[optind - 1]);
12899 if (argc == optind) {
12900 fprintf(stderr, "%s: no file name given.\n", progname);
12905 if (ids_nr > 0 && argc > optind + 1) {
12907 "%s: '--only' cannot be used upon multiple files.\n",
12914 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12920 for (; optind < argc; optind++) {
12921 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12931 * lfs_mirror() - Parse and execute lfs mirror commands.
12932 * @argc: The count of lfs mirror command line arguments.
12933 * @argv: Array of strings for lfs mirror command line arguments.
12935 * This function parses lfs mirror commands and performs the
12936 * corresponding functions specified in mirror_cmdlist[].
12938 * Return: 0 on success or an error code on failure.
12940 static int lfs_mirror(int argc, char **argv)
12942 char cmd[PATH_MAX];
12945 setlinebuf(stdout);
12947 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12949 program_invocation_short_name = cmd;
12950 rc = cfs_parser(argc, argv, mirror_cmdlist);
12952 return rc < 0 ? -rc : rc;
12955 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12957 #if __BYTE_ORDER == __BIG_ENDIAN
12958 __swab16s(&attrs->lsa_valid);
12959 __swab64s(&attrs->lsa_size);
12960 __swab64s(&attrs->lsa_blocks);
12964 enum lfs_som_type {
12965 LFS_SOM_SIZE = 0x1,
12966 LFS_SOM_BLOCKS = 0x2,
12967 LFS_SOM_FLAGS = 0x4,
12968 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12972 static int lfs_getsom(int argc, char **argv)
12975 struct lustre_som_attrs *attrs;
12976 char buf[sizeof(*attrs) + 64];
12977 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12980 while ((c = getopt(argc, argv, "bfhs")) != -1) {
12983 type = LFS_SOM_BLOCKS;
12986 type = LFS_SOM_FLAGS;
12989 type = LFS_SOM_SIZE;
12992 fprintf(stderr, "%s: unrecognized option '%s'\n",
12993 progname, argv[optind - 1]);
13004 fprintf(stderr, "%s: %s\n",
13005 progname, argc == 0 ? "miss file target" :
13006 "input more than 2 files");
13011 attrs = (void *)buf;
13012 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
13015 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
13016 argv[0], strerror(errno), errno);
13020 lustre_som_swab(attrs);
13023 case LFS_SOM_ATTR_ALL:
13024 printf("file: %s size: %llu blocks: %llu flags: %x\n",
13025 path, (unsigned long long)attrs->lsa_size,
13026 (unsigned long long)attrs->lsa_blocks,
13030 printf("%llu\n", (unsigned long long)attrs->lsa_size);
13032 case LFS_SOM_BLOCKS:
13033 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
13035 case LFS_SOM_FLAGS:
13036 printf("%x\n", attrs->lsa_valid);
13039 fprintf(stderr, "%s: unknown option\n", progname);
13046 static int lfs_pcc_attach(int argc, char **argv)
13048 struct option long_opts[] = {
13049 { .val = 'h', .name = "help", .has_arg = no_argument },
13050 { .val = 'i', .name = "id", .has_arg = required_argument },
13051 { .name = NULL } };
13054 __u32 archive_id = 0;
13057 char fullpath[PATH_MAX];
13058 enum lu_pcc_type type = LU_PCC_READWRITE;
13061 while ((c = getopt_long(argc, argv, "hi:",
13062 long_opts, NULL)) != -1) {
13066 archive_id = strtoul(optarg, &end, 0);
13067 if (errno != 0 || *end != '\0' ||
13068 archive_id == 0 || archive_id > UINT32_MAX) {
13070 "error: %s: bad archive ID '%s'\n",
13076 fprintf(stderr, "%s: unrecognized option '%s'\n",
13077 progname, argv[optind - 1]);
13084 if (archive_id == 0) {
13085 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
13089 if (argc <= optind) {
13090 fprintf(stderr, "%s: must specify one or more file names\n",
13095 while (optind < argc) {
13098 path = argv[optind++];
13099 if (!realpath(path, fullpath)) {
13100 fprintf(stderr, "%s: could not find path '%s': %s\n",
13101 argv[0], path, strerror(errno));
13107 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
13110 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
13111 argv[0], path, archive_id, strerror(-rc2));
13119 static int lfs_pcc_attach_fid(int argc, char **argv)
13121 struct option long_opts[] = {
13122 { .val = 'h', .name = "help", .has_arg = no_argument },
13123 { .val = 'i', .name = "id", .has_arg = required_argument },
13124 { .val = 'm', .name = "mnt", .has_arg = required_argument },
13125 { .name = NULL } };
13128 __u32 archive_id = 0;
13130 const char *mntpath = NULL;
13131 const char *fidstr;
13132 enum lu_pcc_type type = LU_PCC_READWRITE;
13135 while ((c = getopt_long(argc, argv, "hi:m:",
13136 long_opts, NULL)) != -1) {
13140 archive_id = strtoul(optarg, &end, 0);
13141 if (errno != 0 || *end != '\0' ||
13142 archive_id > UINT32_MAX) {
13144 "error: %s: bad archive ID '%s'\n",
13153 fprintf(stderr, "%s: unrecognized option '%s'\n",
13154 progname, argv[optind - 1]);
13161 if (archive_id == 0) {
13162 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
13167 fprintf(stderr, "%s: must specify Lustre mount point\n",
13172 if (argc <= optind) {
13173 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
13177 while (optind < argc) {
13180 fidstr = argv[optind++];
13182 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
13186 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
13187 argv[0], fidstr, mntpath, archive_id,
13190 if (rc == 0 && rc2 < 0)
13196 static int lfs_pcc_detach(int argc, char **argv)
13198 struct option long_opts[] = {
13199 { .val = 'h', .name = "help", .has_arg = no_argument },
13200 { .val = 'k', .name = "keep", .has_arg = no_argument },
13201 { .name = NULL } };
13205 char fullpath[PATH_MAX];
13206 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13209 while ((c = getopt_long(argc, argv, "hk",
13210 long_opts, NULL)) != -1) {
13213 detach_opt = PCC_DETACH_OPT_NONE;
13216 fprintf(stderr, "%s: unrecognized option '%s'\n",
13217 progname, argv[optind - 1]);
13224 while (optind < argc) {
13227 path = argv[optind++];
13228 if (!realpath(path, fullpath)) {
13229 fprintf(stderr, "%s: could not find path '%s': %s\n",
13230 argv[0], path, strerror(errno));
13236 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
13240 "%s: cannot detach '%s' from PCC: %s\n",
13241 argv[0], path, strerror(errno));
13249 static int lfs_pcc_detach_fid(int argc, char **argv)
13251 struct option long_opts[] = {
13252 { .val = 'h', .name = "help", .has_arg = no_argument },
13253 { .val = 'k', .name = "keep", .has_arg = no_argument },
13254 { .name = NULL } };
13258 const char *mntpath;
13259 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13262 while ((c = getopt_long(argc, argv, "hk",
13263 long_opts, NULL)) != -1) {
13266 detach_opt = PCC_DETACH_OPT_NONE;
13269 fprintf(stderr, "%s: unrecognized option '%s'\n",
13270 progname, argv[optind - 1]);
13277 mntpath = argv[optind++];
13279 while (optind < argc) {
13282 fid = argv[optind++];
13284 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
13287 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
13288 argv[0], fid, mntpath, strerror(-rc2));
13296 static int lfs_pcc_state(int argc, char **argv)
13300 char fullpath[PATH_MAX];
13301 struct lu_pcc_state state;
13306 fprintf(stderr, "%s: must specify one or more file names\n",
13311 while (optind < argc) {
13314 path = argv[optind++];
13315 if (!realpath(path, fullpath)) {
13316 fprintf(stderr, "%s: could not find path '%s': %s\n",
13317 argv[0], path, strerror(errno));
13323 rc2 = llapi_pcc_state_get(fullpath, &state);
13328 "%s: cannot get PCC state of '%s': %s\n",
13329 argv[0], path, strerror(-rc2));
13333 printf("file: %s", path);
13334 printf(", type: %s", pcc_type2string(state.pccs_type));
13335 if (state.pccs_type == LU_PCC_NONE &&
13336 state.pccs_open_count == 0) {
13341 printf(", PCC file: %s", state.pccs_path);
13342 printf(", user number: %u", state.pccs_open_count);
13343 printf(", flags: %x", state.pccs_flags);
13350 * lfs_pcc() - Parse and execute lfs pcc commands.
13351 * @argc: The count of lfs pcc command line arguments.
13352 * @argv: Array of strings for lfs pcc command line arguments.
13354 * This function parses lfs pcc commands and performs the
13355 * corresponding functions specified in pcc_cmdlist[].
13357 * Return: 0 on success or an error code on failure.
13359 static int lfs_pcc(int argc, char **argv)
13361 char cmd[PATH_MAX];
13364 setlinebuf(stdout);
13366 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
13368 program_invocation_short_name = cmd;
13369 rc = cfs_parser(argc, argv, pcc_cmdlist);
13371 return rc < 0 ? -rc : rc;
13374 int main(int argc, char **argv)
13378 /* Ensure that liblustreapi constructor has run */
13379 if (!llapi_liblustreapi_initialized())
13380 fprintf(stderr, "liblustreapi was not properly initialized\n");
13382 setlinebuf(stdout);
13385 progname = program_invocation_short_name; /* Used in error messages */
13386 llapi_set_command_name(argv[1]);
13387 rc = cfs_parser(argc, argv, cmdlist);
13388 llapi_clear_command_name();
13390 return rc < 0 ? -rc : rc;